Implement characters as Javascript strings of length 1
authorDavid Vázquez <davazp@gmail.com>
Thu, 2 May 2013 13:59:19 +0000 (14:59 +0100)
committerDavid Vázquez <davazp@gmail.com>
Thu, 2 May 2013 14:02:10 +0000 (15:02 +0100)
src/boot.lisp
src/compiler.lisp
src/read.lisp

index d3dc898..845b601 100644 (file)
   (lambda (&rest args)
     x))
 
-(defun code-char (x) x)
-(defun char-code (x) x)
-(defun char= (x y) (= x y))
+(defun code-char (x)
+  (code-char x))
+
+(defun char-code (x)
+  (char-code x))
+
+(defun char= (x y)
+  (eql x y))
 
 (defun integerp (x)
   (and (numberp x) (= (floor x) x)))
      (remove-if-not func (cdr list)))))
 
 (defun digit-char-p (x)
-  (if (and (<= #\0 x) (<= x #\9))
-      (- x #\0)
+  (if (and (<= (char-code #\0) (char-code x) (char-code #\9)))
+      (- (char-code x) (char-code #\0))
       nil))
 
 (defun digit-char (weight)
index ada104e..dec5eaa 100644 (file)
   (cond
     ((integerp sexp) (integer-to-string sexp))
     ((floatp sexp) (float-to-string sexp))
+    ((characterp sexp) (code "\"" (escape-string (string sexp)) "\""))
     ((stringp sexp) (code "\"" (escape-string sexp) "\""))
     (t
      (or (cdr (assoc sexp *literal-table*))
 (define-builtin lambda-code (x)
   (code "(" x ").toString()"))
 
-(define-builtin eq    (x y) (js!bool (code "(" x " === " y ")")))
+(define-builtin eq (x y)
+  (js!bool (code "(" x " === " y ")")))
+
+(define-builtin char-code (x)
+  (type-check (("x" "string" x))
+    "x.charCodeAt(0)"))
+
+(define-builtin code-char (x)
+  (type-check (("x" "number" x))
+    "String.fromCharCode(x)"))
+
+(define-builtin characterp (x)
+  (js!bool (code "(typeof(" x ") == \"string\")")))
 
 (define-builtin char-to-string (x)
   (type-check (("x" "number" x))
 (define-builtin char (string index)
   (type-check (("string" "string" string)
                ("index" "number" index))
-    "string.charCodeAt(index)"))
+    "string.charAt(index)"))
 
 (define-builtin concat-two (string1 string2)
   (type-check (("string1" "string" string1)
               (ls-compile `(symbol-value ',sexp))))))
         ((integerp sexp) (integer-to-string sexp))
         ((floatp sexp) (float-to-string sexp))
+        ((characterp sexp) (code "\"" (escape-string (string sexp)) "\""))
         ((stringp sexp) (code "\"" (escape-string sexp) "\""))
         ((arrayp sexp) (literal sexp))
         ((listp sexp)
index 3cfd0fe..ac9e0f3 100644 (file)
          ((string= cname "space") (char-code #\space))
          ((string= cname "tab") (char-code #\tab))
          ((string= cname "newline") (char-code #\newline))
-         (t (char-code (char cname 0))))))
+         (t (char cname 0)))))
     (#\+
      (let ((feature (read-until stream #'terminalp)))
        (cond