Permalink
Browse files

Improved reporting on invalid symbols. Adds ! ++ -- to list of operat…

…ors. Bump the version
  • Loading branch information...
1 parent 8ec0fd6 commit e44a3603affc2dc1edb518f6c57bb5a2e264d839 @arohner committed Sep 13, 2010
Showing with 31 additions and 4 deletions.
  1. +1 −1 project.clj
  2. +23 −3 src/com/reasonr/scriptjure.clj
  3. +7 −0 test/test_scriptjure.clj
View
@@ -1,4 +1,4 @@
-(defproject scriptjure "0.1.13"
+(defproject scriptjure "0.1.14"
:description "a clojure DSL for generating javascript"
:url "http://github.com/arohner/scriptjure"
:dependencies [[org.clojure/clojure "1.2.0"]
@@ -55,16 +55,18 @@
(defmethod emit clojure.lang.Ratio [expr]
(str (float expr)))
-(defmethod emit clojure.lang.Keyword [expr]
- (str (name expr)))
-
(defmethod emit java.lang.String [expr]
(str \' expr \'))
(defn valid-symbol? [sym]
;;; This is incomplete, it disallows unicode
(boolean (re-matches #"[_$\p{Alpha}][.\w]*" (str sym))))
+(defmethod emit clojure.lang.Keyword [expr]
+ (when-not (valid-symbol? (str expr))
+ (throwf "%s is not a valid javascript symbol" expr))
+ (str (name expr)))
+
(defmethod emit clojure.lang.Symbol [expr]
(when-not (valid-symbol? (str expr))
(throwf "%s is not a valid javascript symbol" expr))
@@ -78,6 +80,10 @@
(def special-forms (set ['var '. '.. 'if 'funcall 'fn 'set! 'return 'delete 'new 'do 'aget 'while 'doseq 'str 'inc! 'dec!]))
+(def prefix-unary-operators (set ['!]))
+
+(def suffix-unary-operators (set ['++ '--]))
+
(def infix-operators (set ['+ '+= '- '-= '/ '* '% '== '=== '< '> '<= '>= '!= '<< '>> '<<< '>>> '!== '& '| '&& '||]))
(def chainable-infix-operators (set ['+ '- '* '/ '& '| '&& '||]))
@@ -88,6 +94,18 @@
(defn infix-operator? [expr]
(contains? infix-operators expr))
+(defn prefix-unary? [expr]
+ (contains? prefix-unary-operators expr))
+
+(defn suffix-unary? [expr]
+ (contains? suffix-unary-operators expr))
+
+(defn emit-prefix-unary [type [operator arg]]
+ (str operator arg))
+
+(defn emit-suffix-unary [type [operator arg]]
+ (str arg operator))
+
(defn emit-infix [type [operator & args]]
(when (< (count args) 2)
(throw (Exception. "not supported yet")))
@@ -193,6 +211,8 @@
(not (= (cstr/get (str head) 1) \.))) (emit-special 'dot-method expr)
(special-form? head) (emit-special head expr)
(infix-operator? head) (emit-infix head expr)
+ (prefix-unary? head) (emit-prefix-unary expr)
+ (suffix-unary? head) (emit-suffix-unary expr)
:else (emit-special 'funcall expr)))
(throw (new Exception (str "invalid form: " expr)))))
View
@@ -33,6 +33,13 @@
(is (= (js (* x y z a b c)) "(x * y * z * a * b * c)"))
(is (= (js (+ x y z a b c)) "(x + y + z + a + b + c)")))
+(deftest test-prefix-unary
+ (is (= (js (! x) "!x"))))
+
+(deftest test-suffix-unary
+ (is (= (js (++ x) "x++")))
+ (is (= (js (-- x) "x--"))))
+
(deftest test-return
(is (= (strip-whitespace (js (return 42))) "return 42;")))

0 comments on commit e44a360

Please sign in to comment.