Permalink
Browse files

added custom forms

Signed-off-by: Chris Granger <ibdknox@gmail.com>
  • Loading branch information...
1 parent 312ff3d commit 634e0388d3f0c19e1b7f3d1b307bbc537cdf0a65 @ibdknox ibdknox committed Jun 9, 2011
Showing with 38 additions and 2 deletions.
  1. +26 −0 src/com/reasonr/scriptjure.clj
  2. +12 −2 test/test_scriptjure.clj
@@ -91,9 +91,11 @@
(def chainable-infix-operators (set ['+ '- '* '/ '& '| '&& '||]))
+
(defn special-form? [expr]
(contains? special-forms expr))
+
(defn infix-operator? [expr]
(contains? infix-operators expr))
@@ -248,6 +250,8 @@
body (rest expr)]
(str (emit-function nil signature body))))))
+(declare emit-custom custom-form?)
+
(derive clojure.lang.Cons ::list)
(derive clojure.lang.IPersistentList ::list)
@@ -260,6 +264,7 @@
(> (count (str head)) 1)
(not (= (rstr/get (str head) 1) \.))) (emit-special 'dot-method expr)
+ (custom-form? head) (emit-custom head expr)
(special-form? head) (emit-special head expr)
(infix-operator? head) (emit-infix head expr)
(prefix-unary? head) (emit-prefix-unary head expr)
@@ -334,3 +339,24 @@
"takes one or more forms. Returns a string of the forms translated into javascript"
[& forms]
`(_js (quasiquote ~forms)))
+
+
+;;**********************************************************
+;; Custom forms
+;;**********************************************************
+
+(defn add-custom-form [form func]
+ (swap! custom-forms assoc form func))
+
+(defn get-custom [form]
+ (get @custom-forms form))
+
+(defn emit-custom [head expr]
+ (when-let [func (get-custom head)]
+ (let [v (apply func (next expr))]
+ (emit v))))
+
+(defonce custom-forms (atom {}))
+
+(defn custom-form? [expr]
+ (get @custom-forms expr))
View
@@ -7,7 +7,10 @@
"strip extraneous whitespace so tests don't fail because of differences in whitespace"
[str]
(str/trim (str/replace (str/replace str #"\n" " ") #"[ ]+" " ")))
-
+
+(defn prn-hw [n z]
+ (js* (alert (str "hello world " (clj n)))))
+
(deftest number-literal
(is (= (js 42) "42"))
(is (= (js 1/2) "0.5")))
@@ -166,4 +169,11 @@
(is (= (strip-whitespace(js (set! x 1 y 2)))
"x = 1; y = 2;")))
-(run-tests)
+(deftest custom-form-add
+ (add-custom-form 'prn-hw prn-hw))
+ (is (contains? @custom-forms 'prn-hw))
+
+(deftest custom-form-use
+ (is (= (js (prn-hw "chris"))) "alert(\"hello world chris\")"))
+
+(run-tests)

0 comments on commit 634e038

Please sign in to comment.