From: David Vázquez Date: Thu, 2 May 2013 13:59:19 +0000 (+0100) Subject: Implement characters as Javascript strings of length 1 X-Git-Url: http://repo.macrolet.net/gitweb/?a=commitdiff_plain;h=c08b369e02f12e4a5c9fa79c7332dcdae24fd356;p=jscl.git Implement characters as Javascript strings of length 1 --- diff --git a/src/boot.lisp b/src/boot.lisp index d3dc898..845b601 100644 --- a/src/boot.lisp +++ b/src/boot.lisp @@ -351,9 +351,14 @@ (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))) @@ -401,8 +406,8 @@ (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) diff --git a/src/compiler.lisp b/src/compiler.lisp index ada104e..dec5eaa 100644 --- a/src/compiler.lisp +++ b/src/compiler.lisp @@ -591,6 +591,7 @@ (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*)) @@ -1459,7 +1460,19 @@ (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)) @@ -1487,7 +1500,7 @@ (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) @@ -1692,6 +1705,7 @@ (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) diff --git a/src/read.lisp b/src/read.lisp index 3cfd0fe..ac9e0f3 100644 --- a/src/read.lisp +++ b/src/read.lisp @@ -141,7 +141,7 @@ ((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