X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=test.lisp;h=7652409ca28c56625efc5d6995c3f7710d42e289;hb=d7e11a79df63bd65bc7f31d2747a6c8e59968b6c;hp=bfbabe2ec68a5b471ebe039ab26cd574615ec93a;hpb=11c4a9d8ddc48209283cd0f2ce4b21d5ce18c964;p=jscl.git diff --git a/test.lisp b/test.lisp index bfbabe2..7652409 100644 --- a/test.lisp +++ b/test.lisp @@ -20,16 +20,23 @@ (defvar *package* (new)) +(defvar nil (make-symbol "NIL")) +(set *package* "NIL" nil) + +(defvar t (make-symbol "T")) +(set *package* "T" t) + +(defun internp (name) + (in name *package*)) + (defun intern (name) - (let ((s (get *package* name))) - (if s s (set *package* name (make-symbol name))))) + (if (internp name) + (get *package* name) + (set *package* name (make-symbol name)))) (defun find-symbol (name) (get *package* name)) -(defvar t 't) -(defvar nil 'nil) - (defmacro when (condition &rest body) `(if ,condition (progn ,@body) nil)) @@ -59,6 +66,8 @@ (defun truncate (x y) (floor (/ x y))) (defun cons (x y ) (cons x y)) +(defun consp (x) (consp x)) + (defun car (x) (car x)) (defun caar (x) (car (car x))) (defun cadr (x) (car (cdr x))) @@ -269,6 +278,24 @@ (t (cons (car list) (remove x (cdr list)))))) +(defun remove-if (func list) + (cond + ((null list) + nil) + ((funcall func (car list)) + (remove-if func (cdr list))) + (t + (cons (car list) (remove-if func (cdr list)))))) + +(defun remove-if-not (func list) + (cond + ((null list) + nil) + ((funcall func (car list)) + (cons (car list) (remove-if-not func (cdr list)))) + (t + (remove-if-not func (cdr list))))) + (defun digit-char-p (x) (if (and (<= #\0 x) (<= x #\9)) (- x #\0) @@ -309,6 +336,64 @@ (defun string= (s1 s2) (equal s1 s2)) +;; ---------------------------------------------------------- + +;;; Utils + +#+common-lisp +(progn + (defmacro while (condition &body body) + `(do () + ((not ,condition)) + ,@body)) + + (defun concat-two (s1 s2) + (concatenate 'string s1 s2)) + + (defun setcar (cons new) + (setf (car cons) new)) + (defun setcdr (cons new) + (setf (cdr cons) new))) + +(defvar *newline* (string (code-char 10))) + +(defun concat (&rest strs) + (!reduce (lambda (s1 s2) (concat-two s1 s2)) + strs + "")) + +;;; Concatenate a list of strings, with a separator +(defun join (list separator) + (cond + ((null list) + "") + ((null (cdr list)) + (car list)) + (t + (concat (car list) + separator + (join (cdr list) separator))))) + +(defun join-trailing (list separator) + (cond + ((null list) + "") + ((null (car list)) + (join-trailing (cdr list) separator)) + (t + (concat (car list) separator (join-trailing (cdr list) separator))))) + +(defun integer-to-string (x) + (if (zerop x) + "0" + (let ((digits nil)) + (while (not (= x 0)) + (push (mod x 10) digits) + (setq x (truncate x 10))) + (join (mapcar (lambda (d) (string (char "0123456789" d))) + digits) + "")))) + ;;;; Reader ;;; It is a basic Lisp reader. It does not use advanced stuff @@ -328,7 +413,7 @@ (setcdr stream (1+ (cdr stream)))))) (defun whitespacep (ch) - (or (char= ch #\space) (char= ch #\newline))) + (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab))) (defun skip-whitespaces (stream) (let (ch) @@ -421,6 +506,7 @@ (read-until stream #'terminalp)))) (cond ((string= cname "space") (char-code #\space)) + ((string= cname "tab") (char-code #\tab)) ((string= cname "newline") (char-code #\newline)) (t (char-code (char cname 0)))))) (#\+ @@ -461,50 +547,50 @@ (defun mark-binding-as-declared (b) (setcar (cdddr b) t)) -(let ((counter 0)) - (defun gvarname (symbol) - (concat "v" (integer-to-string (incf counter)))) - - (defun lookup-variable (symbol env) - (or (assoc symbol env) - (assoc symbol *env*) - (let ((name (symbol-name symbol)) - (binding (make-binding symbol 'variable (gvarname symbol) nil))) - (push binding *env*) - (push (lambda () - (unless (binding-declared (assoc symbol *env*)) - (error (concat "Undefined variable `" name "'")))) - *compilation-unit-checks*) - binding))) - - (defun lookup-variable-translation (symbol env) - (binding-translation (lookup-variable symbol env))) - - (defun extend-local-env (args env) - (append (mapcar (lambda (symbol) - (make-binding symbol 'variable (gvarname symbol) t)) - args) - env))) - -(let ((counter 0)) - (defun lookup-function (symbol env) - (or (assoc symbol env) - (assoc symbol *fenv*) - (let ((name (symbol-name symbol)) - (binding - (make-binding symbol - 'function - (concat "f" (integer-to-string (incf counter))) - nil))) - (push binding *fenv*) - (push (lambda () - (unless (binding-declared (assoc symbol *fenv*)) - (error (concat "Undefined function `" name "'")))) - *compilation-unit-checks*) - binding))) - - (defun lookup-function-translation (symbol env) - (binding-translation (lookup-function symbol env)))) +(defvar *variable-counter* 0) +(defun gvarname (symbol) + (concat "v" (integer-to-string (incf *variable-counter*)))) + +(defun lookup-variable (symbol env) + (or (assoc symbol env) + (assoc symbol *env*) + (let ((name (symbol-name symbol)) + (binding (make-binding symbol 'variable (gvarname symbol) nil))) + (push binding *env*) + (push (lambda () + (unless (binding-declared (assoc symbol *env*)) + (error (concat "Undefined variable `" name "'")))) + *compilation-unit-checks*) + binding))) + +(defun lookup-variable-translation (symbol env) + (binding-translation (lookup-variable symbol env))) + +(defun extend-local-env (args env) + (append (mapcar (lambda (symbol) + (make-binding symbol 'variable (gvarname symbol) t)) + args) + env)) + +(defvar *function-counter* 0) +(defun lookup-function (symbol env) + (or (assoc symbol env) + (assoc symbol *fenv*) + (let ((name (symbol-name symbol)) + (binding + (make-binding symbol + 'function + (concat "f" (integer-to-string (incf *function-counter*))) + nil))) + (push binding *fenv*) + (push (lambda () + (unless (binding-declared (assoc symbol *fenv*)) + (error (concat "Undefined function `" name "'")))) + *compilation-unit-checks*) + binding))) + +(defun lookup-function-translation (symbol env) + (binding-translation (lookup-function symbol env))) (defvar *toplevel-compilations* nil) @@ -541,7 +627,7 @@ (define-compilation if (condition true false) (concat "(" - (ls-compile condition env fenv) + (ls-compile condition env fenv) " !== " (ls-compile nil nil nil) " ? " (ls-compile true env fenv) " : " @@ -573,7 +659,7 @@ *newline* (if rest-argument (let ((js!rest (lookup-variable-translation rest-argument new-env))) - (concat "var " js!rest "= false;" *newline* + (concat "var " js!rest "= " (ls-compile nil env fenv) ";" *newline* "for (var i = arguments.length-1; i>=" (integer-to-string (length required-arguments)) "; i--)" *newline* @@ -615,7 +701,6 @@ (defun literal->js (sexp) (cond - ((null sexp) "false") ((integerp sexp) (integer-to-string sexp)) ((stringp sexp) (concat "\"" (escape-string sexp) "\"")) ((symbolp sexp) (ls-compile `(intern ,(escape-string (symbol-name sexp))) *env* *fenv*)) @@ -624,11 +709,11 @@ ", cdr: " (literal->js (cdr sexp)) "}")))) -(let ((counter 0)) - (defun literal (form) - (let ((var (concat "l" (integer-to-string (incf counter))))) - (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*) - var))) +(defvar *literal-counter* 0) +(defun literal (form) + (let ((var (concat "l" (integer-to-string (incf *literal-counter*))))) + (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*) + var)) (define-compilation quote (sexp) (literal sexp)) @@ -638,7 +723,7 @@ (define-compilation while (pred &rest body) (concat "(function(){ while(" - (ls-compile pred env fenv) + (ls-compile pred env fenv) " !== " (ls-compile nil nil nil) "){" (ls-compile-block body env fenv) "}})()")) @@ -651,7 +736,7 @@ (lookup-function-translation x fenv)))) #+common-lisp -(defmacro eval-when-compile (&body body) +c(defmacro eval-when-compile (&body body) `(eval-when (:compile-toplevel :load-toplevel :execute) ,@body)) @@ -708,6 +793,9 @@ ;;; Primitives +(defun compile-bool (x) + (concat "(" x "?" (ls-compile t nil nil) ": " (ls-compile nil nil nil) ")")) + (define-compilation + (x y) (concat "((" (ls-compile x env fenv) ") + (" (ls-compile y env fenv) "))")) @@ -721,13 +809,13 @@ (concat "((" (ls-compile x env fenv) ") / (" (ls-compile y env fenv) "))")) (define-compilation < (x y) - (concat "((" (ls-compile x env fenv) ") < (" (ls-compile y env fenv) "))")) + (compile-bool (concat "((" (ls-compile x env fenv) ") < (" (ls-compile y env fenv) "))"))) (define-compilation = (x y) - (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))")) + (compile-bool (concat "((" (ls-compile x env fenv) ") == (" (ls-compile y env fenv) "))"))) (define-compilation numberp (x) - (concat "(typeof (" (ls-compile x env fenv) ") == \"number\")")) + (compile-bool (concat "(typeof (" (ls-compile x env fenv) ") == \"number\")"))) (define-compilation mod (x y) @@ -737,21 +825,28 @@ (concat "(Math.floor(" (ls-compile x env fenv) "))")) (define-compilation null (x) - (concat "(" (ls-compile x env fenv) "== false)")) + (compile-bool (concat "(" (ls-compile x env fenv) "===" (ls-compile nil env fenv) ")"))) (define-compilation cons (x y) (concat "{car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "}")) (define-compilation consp (x) - (concat "(function(){ var tmp = " - (ls-compile x env fenv) - "; return (typeof tmp == 'object' && 'car' in tmp);})()")) + (compile-bool + (concat "(function(){ var tmp = " + (ls-compile x env fenv) + "; return (typeof tmp == 'object' && 'car' in tmp);})()"))) (define-compilation car (x) - (concat "(" (ls-compile x env fenv) ").car")) + (concat "(function () { var tmp = " (ls-compile x env fenv) + "; return tmp === " (ls-compile nil nil nil) "? " + (ls-compile nil nil nil) + ": tmp.car; })()")) (define-compilation cdr (x) - (concat "(" (ls-compile x env fenv) ").cdr")) + (concat "(function () { var tmp = " (ls-compile x env fenv) + "; return tmp === " (ls-compile nil nil nil) "? " + (ls-compile nil nil nil) + ": tmp.cdr; })()")) (define-compilation setcar (x new) (concat "((" (ls-compile x env fenv) ").car = " (ls-compile new env fenv) ")")) @@ -760,9 +855,10 @@ (concat "((" (ls-compile x env fenv) ").cdr = " (ls-compile new env fenv) ")")) (define-compilation symbolp (x) - (concat "(function(){ var tmp = " - (ls-compile x env fenv) - "; return (typeof tmp == 'object' && 'name' in tmp); })()")) + (compile-bool + (concat "(function(){ var tmp = " + (ls-compile x env fenv) + "; return (typeof tmp == 'object' && 'name' in tmp); })()"))) (define-compilation make-symbol (name) (concat "{name: " (ls-compile name env fenv) "}")) @@ -771,16 +867,19 @@ (concat "(" (ls-compile x env fenv) ").name")) (define-compilation eq (x y) - (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")")) + (compile-bool + (concat "(" (ls-compile x env fenv) " === " (ls-compile y env fenv) ")"))) (define-compilation equal (x y) - (concat "(" (ls-compile x env fenv) " == " (ls-compile y env fenv) ")")) + (compile-bool + (concat "(" (ls-compile x env fenv) " == " (ls-compile y env fenv) ")"))) (define-compilation string (x) (concat "String.fromCharCode(" (ls-compile x env fenv) ")")) (define-compilation stringp (x) - (concat "(typeof(" (ls-compile x env fenv) ") == \"string\")")) + (compile-bool + (concat "(typeof(" (ls-compile x env fenv) ") == \"string\")"))) (define-compilation string-upcase (x) (concat "(" (ls-compile x env fenv) ").toUpperCase()")) @@ -812,7 +911,6 @@ ", ") ")")) - (define-compilation apply (func &rest args) (if (null args) (concat "(" (ls-compile func env fenv) ")()") @@ -826,14 +924,13 @@ ", ") "];" *newline* "var tail = (" (ls-compile last env fenv) ");" *newline* - "while (tail != false){" *newline* + "while (tail != " (ls-compile nil env fenv) "){" *newline* " args.push(tail.car);" *newline* " tail = tail.cdr;" *newline* "}" *newline* "return f.apply(this, args);" *newline* "})()" *newline*)))) - (define-compilation js-eval (string) (concat "eval(" (ls-compile string env fenv) ")")) @@ -845,7 +942,11 @@ "{}") (define-compilation get (object key) - (concat "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]")) + (concat "(function(){ var tmp = " + "(" (ls-compile object env fenv) ")[" (ls-compile key env fenv) "]" + ";" + "return tmp == undefined? " (ls-compile nil nil nil) ": tmp ;" + "})()")) (define-compilation set (object key value) (concat "((" @@ -854,6 +955,11 @@ (ls-compile key env fenv) "]" " = " (ls-compile value env fenv) ")")) +(define-compilation in (key object) + (compile-bool + (concat "(" (ls-compile key env fenv) " in " (ls-compile object env fenv) ")"))) + + (defun macrop (x) (and (symbolp x) (eq (binding-type (lookup-function x *fenv*)) 'macro))) @@ -897,22 +1003,13 @@ (setq *toplevel-compilations* nil) (let ((code (ls-compile sexp nil nil))) (prog1 - (concat (join (mapcar (lambda (x) (concat x ";" *newline*)) - *toplevel-compilations*) - "") + (concat #+common-lisp (concat "/* " (princ-to-string sexp) " */") + (join (mapcar (lambda (x) (concat x ";" *newline*)) + *toplevel-compilations*) + "") code) (setq *toplevel-compilations* nil)))) - -(defmacro with-compilation-unit (&rest body) - `(progn - (setq *compilation-unit-checks* nil) - (prog1 (progn ,@body) - (dolist (check *compilation-unit-checks*) - (funcall check)) - (setq *compilation-unit-checks* nil)))) - - #+common-lisp (progn (defun read-whole-file (filename) @@ -940,6 +1037,17 @@ (defun bootstrap () (ls-compile-file "lispstrack.lisp" "lispstrack.js"))) +;;; ---------------------------------------------------------- + +(defmacro with-compilation-unit (&rest body) + `(prog1 + (progn + (setq *compilation-unit-checks* nil) + (setq *env* (remove-if-not #'binding-declared *env*)) + (setq *fenv* (remove-if-not #'binding-declared *fenv*)) + ,@body) + (dolist (check *compilation-unit-checks*) + (funcall check)))) (defun eval (x) (let ((code @@ -947,13 +1055,17 @@ (ls-compile-toplevel x nil nil)))) (js-eval code))) + ;; Set the initial global environment to be equal to the host global ;; environment at this point of the compilation. (eval-when-compile (let ((c1 (ls-compile `(setq *fenv* ',*fenv*) nil nil)) - (c2 (ls-compile `(setq *env* ',*env*) nil nil))) + (c2 (ls-compile `(setq *env* ',*env*) nil nil)) + (c3 (ls-compile `(setq *variable-counter* ',*variable-counter*) nil nil)) + (c4 (ls-compile `(setq *function-counter* ',*function-counter*) nil nil)) + (c5 (ls-compile `(setq *literal-counter* ',*literal-counter*) nil nil))) (setq *toplevel-compilations* - (append *toplevel-compilations* (list c1 c2))))) + (append *toplevel-compilations* (list c1 c2 c3 c4 c5))))) (js-eval (concat "var lisp = {};"