X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=test.lisp;h=adf8c1230c9ce1a3e19e1ad3eaf72f7ef386556a;hb=4f17b56fa136f97d11975d081a861351eb64db76;hp=a77545ac2ddec1e2a0d9727891b7340519a9ec54;hpb=7e334dbc8e81a72fbf2682b0c4f7809bbc6d5c1f;p=jscl.git diff --git a/test.lisp b/test.lisp index a77545a..adf8c12 100644 --- a/test.lisp +++ b/test.lisp @@ -66,15 +66,20 @@ (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 cdr (x) (cdr x)) + (defun caar (x) (car (car x))) (defun cadr (x) (car (cdr x))) -(defun caddr (x) (car (cdr (cdr x)))) -(defun cadddr (x) (car (cdr (cdr (cdr x))))) -(defun cdr (x) (cdr x)) (defun cdar (x) (cdr (car x))) (defun cddr (x) (cdr (cdr x))) -(defun cdddr (x) (cdr (cdr x))) + +(defun caddr (x) (car (cdr (cdr x)))) +(defun cdddr (x) (cdr (cdr (cdr x)))) + +(defun cadddr (x) (car (cdr (cdr (cdr x))))) (defun first (x) (car x)) (defun second (x) (cadr x)) @@ -92,12 +97,15 @@ x (list x))) -(defun append (list1 list2) +(defun append-two (list1 list2) (if (null list1) list2 (cons (car list1) (append (cdr list1) list2)))) +(defun append (&rest lists) + (!reduce #'append-two lists '())) + (defun reverse-aux (list acc) (if (null list) acc @@ -276,6 +284,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) @@ -428,13 +454,15 @@ (skip-whitespaces-and-comments stream) (let ((ch (%peek-char stream))) (cond + ((null ch) + (error "Unspected EOF")) ((char= ch #\)) (%read-char stream) nil) ((char= ch #\.) (%read-char stream) - (skip-whitespaces-and-comments stream) (prog1 (ls-read stream) + (skip-whitespaces-and-comments stream) (unless (char= (%read-char stream) #\)) (error "')' was expected.")))) (t @@ -444,8 +472,10 @@ (let ((string "") (ch nil)) (setq ch (%read-char stream)) - (while (not (char= ch #\")) - (when (char= ch #\\) + (while (not (eql ch #\")) + (when (null ch) + (error "Unexpected EOF")) + (when (eql ch #\\) (setq ch (%read-char stream))) (setq string (concat string (string ch))) (setq ch (%read-char stream))) @@ -527,50 +557,51 @@ (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) @@ -689,11 +720,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)) @@ -733,8 +764,8 @@ (define-transformation let (bindings &rest body) (let ((bindings (mapcar #'ensure-list bindings))) - `((lambda ,(mapcar 'car bindings) ,@body) - ,@(mapcar 'cadr bindings)))) + `((lambda ,(mapcar #'car bindings) ,@body) + ,@(mapcar #'cadr bindings)))) ;;; A little backquote implementation without optimizations of any ;;; kind for lispstrack. @@ -808,7 +839,7 @@ (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) "}")) + (concat "({car: " (ls-compile x env fenv) ", cdr: " (ls-compile y env fenv) "})")) (define-compilation consp (x) (compile-bool @@ -817,10 +848,16 @@ "; 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) ")")) @@ -906,7 +943,7 @@ "})()" *newline*)))) (define-compilation js-eval (string) - (concat "eval(" (ls-compile string env fenv) ")")) + (concat "eval.apply(window, [" (ls-compile string env fenv) "])")) (define-compilation error (string) @@ -938,11 +975,12 @@ (and (symbolp x) (eq (binding-type (lookup-function x *fenv*)) 'macro))) (defun ls-macroexpand-1 (form env fenv) - (when (macrop (car form)) - (let ((binding (lookup-function (car form) *env*))) - (if (eq (binding-type binding) 'macro) - (apply (eval (binding-translation binding)) (cdr form)) - form)))) + (if (macrop (car form)) + (let ((binding (lookup-function (car form) *env*))) + (if (eq (binding-type binding) 'macro) + (apply (eval (binding-translation binding)) (cdr form)) + form)) + form)) (defun compile-funcall (function args env fenv) (cond @@ -977,7 +1015,7 @@ (setq *toplevel-compilations* nil) (let ((code (ls-compile sexp nil nil))) (prog1 - (concat + (concat #+common-lisp (concat "/* " (princ-to-string sexp) " */") (join (mapcar (lambda (x) (concat x ";" *newline*)) *toplevel-compilations*) "") @@ -1011,14 +1049,17 @@ (defun bootstrap () (ls-compile-file "lispstrack.lisp" "lispstrack.js"))) - ;;; ---------------------------------------------------------- (defmacro with-compilation-unit (&rest body) - `(prog1 (progn ,@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)) - (setq *compilation-unit-checks* nil))) + (funcall check)))) (defun eval (x) (let ((code @@ -1026,13 +1067,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 = {};"