(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
(declaim (constant nil t) (special t nil))
(setq nil 'nil)
+ (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))
;;; 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))
(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)
"return func;" *newline*)
(join strs)))
-(define-compilation lambda (lambda-list &rest body)
+(defun lambda-check-argument-count
+ (n-required-arguments n-optional-arguments rest-p)
+ ;; Note: Remember that we assume that the number of arguments of a
+ ;; call is at least 1 (the values argument).
+ (let ((min (1+ n-required-arguments))
+ (max (if rest-p 'n/a (+ 1 n-required-arguments n-optional-arguments))))
+ (block nil
+ ;; Special case: a positive exact number of arguments.
+ (when (and (< 1 min) (eql min max))
+ (return (concat "checkArgs(arguments, " (integer-to-string min) ");" *newline*)))
+ ;; General case:
+ (concat
+ (if (< 1 min)
+ (concat "checkArgsAtLeast(arguments, " (integer-to-string min) ");" *newline*)
+ "")
+ (if (numberp max)
+ (concat "checkArgsAtMost(arguments, " (integer-to-string max) ");" *newline*)
+ "")))))
+
+(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))
(append required-arguments optional-arguments)))
",")
"){" *newline*
- ;; Check number of arguments
(indent
- (if required-arguments
- (concat "checkArgsAtLeast("
- (integer-to-string (1+ n-required-arguments))
- ");" *newline*)
- "")
- (if (not rest-argument)
- (concat "checkArgsAtLeast("
- (integer-to-string (+ 1 n-required-arguments n-optional-arguments))
- ");" *newline*)
- "")
+ ;; Check number of arguments
+ (lambda-check-argument-count n-required-arguments
+ n-optional-arguments
+ rest-argument)
;; Optional arguments
(if optional-arguments
(concat "switch(arguments.length-1){" *newline*
(toplevel-compilation (concat "var " v " = " s))
v)))
((consp sexp)
- (let ((c (concat "{car: " (literal (car sexp) t) ", "
- "cdr: " (literal (cdr sexp) t) "}")))
+ (let* ((head (butlast sexp))
+ (tail (last sexp))
+ (c (concat "QIList("
+ (join-trailing (mapcar (lambda (x) (literal x t)) head) ",")
+ (literal (car tail) t)
+ ","
+ (literal (cdr tail) t)
+ ")")))
(if recursive
c
(let ((v (genlit)))
- (toplevel-compilation (concat "var " v " = " c))
- v))))
+ (toplevel-compilation (concat "var " v " = " c))
+ v))))
((arrayp sexp)
(let ((elements (vector-to-list sexp)))
(let ((c (concat "[" (join (mapcar #'literal elements) ", ") "]")))
(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
(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) ".'"
"})")))
(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.'"
"})"))
"return args;" *newline*))
-#+common-lisp
-(progn
-
- )
-
;;; A little backquote implementation without optimizations of any
;;; kind for ecmalisp.
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))