Skip to content

Commit

Permalink
Added .. as a special form
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoduncan authored and arohner committed Jun 3, 2010
1 parent 501ae87 commit 7716d98
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/com/reasonr/scriptjure.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
(defmethod emit :default [expr]
(str expr))

(def special-forms (set ['var '. 'if 'funcall 'fn 'set! 'return 'delete 'new 'do 'aget 'doseq 'str]))
(def special-forms (set ['var '. '.. 'if 'funcall 'fn 'set! 'return 'delete 'new 'do 'aget 'doseq 'str]))

(def infix-operators (set ['+ '- '/ '* '% '== '=== '< '> '<= '>= '!= '<< '>> '<<< '>>> '!== '& '| '&& '||]))

Expand Down Expand Up @@ -98,6 +98,9 @@
(defmethod emit-special '. [type [period obj method & args]]
(emit-method obj method args))

(defmethod emit-special '.. [type [dotdot & args]]
(apply str (interpose "." (map emit args))))

(defmethod emit-special 'if [type [if test true-form & false-form]]
(str "if (" (emit test) ") { \n"
(emit true-form)
Expand Down Expand Up @@ -161,8 +164,10 @@
(if (symbol? (first expr))
(let [head (symbol (name (first expr))) ; remove any ns resolution
expr (conj (rest expr) head)]
(cond
(and (= (str/get (str head) 0) \.) (> (count (str head)) 1)) (emit-special 'dot-method expr)
(cond
(and (= (str/get (str head) 0) \.)
(> (count (str head)) 1)
(not (= (str/get (str head) 1) \.))) (emit-special 'dot-method expr)
(special-form? head) (emit-special head expr)
(infix-operator? head) (emit-infix head expr)
:else (emit-special 'funcall expr)))
Expand Down
3 changes: 3 additions & 0 deletions test/test_scriptjure.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
(deftest test-dot-method-call
(is (= (js (.bar google.chart :a :b)) "google.chart.bar(a, b)")))

(deftest test-dotdot
(is (= (js (.. google chart (bar :a :b))) "google.chart.bar(a, b)")))

(deftest test-if
(is (= (strip-whitespace (js (if (&& (== foo bar) (!= foo baz)) (.draw google.chart))))
"if (((foo == bar) && (foo != baz))) { google.chart.draw() }"))
Expand Down

0 comments on commit 7716d98

Please sign in to comment.