X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=ecmalisp.lisp;h=d8806141993b254c4e1f0070e8e3d34d91dbc051;hb=2d5ef7b50ce3a117e48eaa5bf04e6735a746b3a8;hp=83d41df565744795a0a27c1fb83bba139b5c34b3;hpb=3873c3aa6bd817ec6b3acf522f00680063757290;p=jscl.git diff --git a/ecmalisp.lisp b/ecmalisp.lisp index 83d41df..d880614 100644 --- a/ecmalisp.lisp +++ b/ecmalisp.lisp @@ -26,15 +26,17 @@ (progn (eval-when-compile (%compile-defmacro 'defmacro - '(lambda (name args &rest body) - `(eval-when-compile - (%compile-defmacro ',name - '(lambda ,(mapcar (lambda (x) - (if (eq x '&body) - '&rest - x)) - args) - ,@body)))))) + '(function + (lambda (name args &rest body) + `(eval-when-compile + (%compile-defmacro ',name + '(function + (lambda ,(mapcar #'(lambda (x) + (if (eq x '&body) + '&rest + x)) + args) + ,@body)))))))) (defmacro declaim (&rest decls) `(eval-when-compile @@ -45,6 +47,9 @@ (js-vset "nil" nil) (setq t 't) + (defmacro lambda (args &body body) + `(function (lambda ,args ,@body))) + (defmacro when (condition &body body) `(if ,condition (progn ,@body) nil)) @@ -987,12 +992,14 @@ ;;; too. The respective real functions are defined in the target (see ;;; the beginning of this file) as well as some primitive functions. -;;; If the special variable `*multiple-value-p*' is NON-NIL, then the -;;; compilation of the current form is allowed to return multiple -;;; values, using the VALUES variable. +;;; A Form can return a multiple values object calling VALUES, like +;;; values(arg1, arg2, ...). It will work in any context, as well as +;;; returning an individual object. However, if the special variable +;;; `*multiple-value-p*' is NIL, is granted that only the primary +;;; value will be used, so we can optimize to avoid the VALUES +;;; function call. (defvar *multiple-value-p* nil) -(defvar *compilation-unit-checks* '()) (defun make-binding (name type value &optional declarations) (list name type value declarations)) @@ -1066,7 +1073,8 @@ (defun %compile-defmacro (name lambda) (toplevel-compilation (ls-compile `',name)) - (push-to-lexenv (make-binding name 'macro lambda) *environment* 'function)) + (push-to-lexenv (make-binding name 'macro lambda) *environment* 'function) + name) (defun global-binding (name type namespace) (or (lookup-in-lexenv name *environment* namespace) @@ -1162,7 +1170,7 @@ (concat "checkArgsAtMost(arguments, " (integer-to-string max) ");" *newline*) ""))))) -(define-compilation lambda (lambda-list &rest body) +(defun compile-lambda (lambda-list body) (let ((required-arguments (lambda-list-required-arguments lambda-list)) (optional-arguments (lambda-list-optional-arguments lambda-list)) (rest-argument (lambda-list-rest-argument lambda-list)) @@ -1333,13 +1341,17 @@ (define-compilation function (x) (cond ((and (listp x) (eq (car x) 'lambda)) - (ls-compile x)) + (compile-lambda (cadr x) (cddr x))) ((symbolp x) (ls-compile `(symbol-function ',x))))) +(defvar *compiling-file* nil) (define-compilation eval-when-compile (&rest body) - (eval (cons 'progn body)) - nil) + (if *compiling-file* + (progn + (eval (cons 'progn body)) + nil) + (ls-compile `(progn ,@body)))) (defmacro define-transformation (name args form) `(define-compilation ,name ,args @@ -1453,33 +1465,41 @@ (define-compilation block (name &rest body) (let* ((tr (integer-to-string (incf *block-counter*))) - (b (make-binding name 'block tr)) - (*environment* (extend-lexenv (list b) *environment* 'block)) - (cbody (ls-compile-block body t))) - (if (member 'used (binding-declarations b)) - (js!selfcall - "try {" *newline* - (indent cbody) - "}" *newline* - "catch (cf){" *newline* - " if (cf.type == 'block' && cf.id == " tr ")" *newline* - " return cf.value;" *newline* - " else" *newline* - " throw cf;" *newline* - "}" *newline*) - (js!selfcall - (indent cbody))))) + (b (make-binding name 'block tr))) + (when *multiple-value-p* + (push-binding-declaration 'multiple-value b)) + (let* ((*environment* (extend-lexenv (list b) *environment* 'block)) + (cbody (ls-compile-block body t))) + (if (member 'used (binding-declarations b)) + (js!selfcall + "try {" *newline* + (indent cbody) + "}" *newline* + "catch (cf){" *newline* + " if (cf.type == 'block' && cf.id == " tr ")" *newline* + (if *multiple-value-p* + " return values.apply(this, forcemv(cf.values));" + " return cf.values;") + *newline* + " else" *newline* + " throw cf;" *newline* + "}" *newline*) + (js!selfcall cbody))))) (define-compilation return-from (name &optional value) - (let ((b (lookup-in-lexenv name *environment* 'block))) + (let* ((b (lookup-in-lexenv name *environment* 'block)) + (multiple-value-p (member 'multiple-value (binding-declarations b)))) (when (null b) (error (concat "Unknown block `" (symbol-name name) "'."))) (push-binding-declaration 'used b) (js!selfcall + (if multiple-value-p + (concat "var values = mv;" *newline*) + "") "throw ({" "type: 'block', " "id: " (binding-value b) ", " - "value: " (ls-compile value) ", " + "values: " (ls-compile value multiple-value-p) ", " "message: 'Return from unknown block " (symbol-name name) ".'" "})"))) @@ -1487,22 +1507,25 @@ (js!selfcall "var id = " (ls-compile id) ";" *newline* "try {" *newline* - (indent "return " (ls-compile `(progn ,@body)) - ";" *newline*) + (indent (ls-compile-block body t)) *newline* "}" *newline* "catch (cf){" *newline* " if (cf.type == 'catch' && cf.id == id)" *newline* - " return cf.value;" *newline* + (if *multiple-value-p* + " return values.apply(this, forcemv(cf.values));" + " return pv.apply(this, forcemv(cf.values));") + *newline* " else" *newline* " throw cf;" *newline* "}" *newline*)) (define-compilation throw (id value) (js!selfcall + "var values = mv;" *newline* "throw ({" "type: 'catch', " "id: " (ls-compile id) ", " - "value: " (ls-compile value) ", " + "values: " (ls-compile value t) ", " "message: 'Throw uncatched.'" "})")) @@ -1612,11 +1635,6 @@ "return args;" *newline*)) -#+common-lisp -(progn - - ) - ;;; A little backquote implementation without optimizations of any ;;; kind for ecmalisp. @@ -2164,20 +2182,17 @@ seq))) (defun ls-compile-file (filename output) - (setq *compilation-unit-checks* nil) - (with-open-file (out output :direction :output :if-exists :supersede) - (write-string (read-whole-file "prelude.js") out) - (let* ((source (read-whole-file filename)) - (in (make-string-stream source))) - (loop - for x = (ls-read in) - until (eq x *eof*) - for compilation = (ls-compile-toplevel x) - when (plusp (length compilation)) - do (write-string compilation out)) - (dolist (check *compilation-unit-checks*) - (funcall check)) - (setq *compilation-unit-checks* nil)))) + (let ((*compiling-file* t)) + (with-open-file (out output :direction :output :if-exists :supersede) + (write-string (read-whole-file "prelude.js") out) + (let* ((source (read-whole-file filename)) + (in (make-string-stream source))) + (loop + for x = (ls-read in) + until (eq x *eof*) + for compilation = (ls-compile-toplevel x) + when (plusp (length compilation)) + do (write-string compilation out)))))) (defun bootstrap () (setq *environment* (make-lexenv))