`(eval-when-compile
,@(mapcar (lambda (decl) `(!proclaim ',decl)) decls)))
- (declaim (constant nil t))
+ (declaim (constant nil t) (special t nil))
(setq nil 'nil)
(setq t 't)
(defmacro defvar (name value &optional docstring)
`(progn
+ (declaim (special ,name))
(unless (boundp ',name) (setq ,name ,value))
,@(when (stringp docstring) `((oset ',name "vardoc" ,docstring)))
',name))
;; Basic functions
(defun = (x y) (= x y))
- (defun + (x y) (+ x y))
- (defun - (x y) (- x y))
(defun * (x y) (* x y))
(defun / (x y) (/ x y))
(defun 1+ (x) (+ x 1))
;;; constructions.
#+ecmalisp
(progn
+ (defun + (&rest args)
+ (let ((r 0))
+ (dolist (x args r)
+ (incf r x))))
+
+ (defun - (x &rest others)
+ (if (null others)
+ (- x)
+ (let ((r x))
+ (dolist (y others r)
+ (decf r y)))))
+
(defun append-two (list1 list2)
(if (null list1)
list2
l))
(defun length (seq)
- (if (stringp seq)
- (string-length seq)
- (list-length seq)))
+ (cond
+ ((stringp seq)
+ (string-length seq))
+ ((arrayp seq)
+ (oget seq "length"))
+ ((listp seq)
+ (list-length seq))))
(defun concat-two (s1 s2)
(concat-two s1 s2))
(defvar *package-list* nil)
+ (defun list-all-packages ()
+ *package-list*)
+
(defun make-package (name &optional use)
(let ((package (new))
(use (mapcar #'find-package-or-fail use)))
(defvar *user-package*
(make-package "CL-USER" (list *common-lisp-package*)))
+ (defvar *keyword-package*
+ (make-package "KEYWORD"))
+
+ (defun keywordp (x)
+ (and (symbolp x) (eq (symbol-package x) *keyword-package*)))
+
(defvar *package* *common-lisp-package*)
(defmacro in-package (package-designator)
(car (%find-symbol name package)))
(defun intern (name &optional (package *package*))
- (let ((result (%find-symbol name package)))
- (if (cdr result)
- (car result)
- (let ((symbols (%package-symbols package)))
- (oget symbols name)
- (let ((symbol (make-symbol name)))
- (oset symbol "package" package)
- (oset symbols name symbol))))))
+ (let ((package (find-package-or-fail package)))
+ (let ((result (%find-symbol name package)))
+ (if (cdr result)
+ (car result)
+ (let ((symbols (%package-symbols package)))
+ (oget symbols name)
+ (let ((symbol (make-symbol name)))
+ (oset symbol "package" package)
+ (when (eq package *keyword-package*)
+ (oset symbol "value" symbol)
+ (export (list symbol) package))
+ (oset symbols name symbol)))))))
(defun symbol-package (symbol)
(unless (symbolp symbol)
(defun setcar (cons new)
(setf (car cons) new))
(defun setcdr (cons new)
- (setf (cdr cons) new)))
+ (setf (cdr cons) new))
+
+ (defun aset (array idx value)
+ (setf (aref array idx) value)))
;;; At this point, no matter if Common Lisp or ecmalisp is compiling
;;; from here, this code will compile on both. We define some helper
(defun mapconcat (func list)
(join (mapcar func list)))
+(defun vector-to-list (vector)
+ (let ((list nil)
+ (size (length vector)))
+ (dotimes (i size (reverse list))
+ (push (aref vector i) list))))
+
+(defun list-to-vector (list)
+ (let ((v (make-array (length list)))
+ (i 0))
+ (dolist (x list v)
+ (aset v i x)
+ (incf i))))
+
;;; Like CONCAT, but prefix each line with four spaces. Two versions
;;; of this function are available, because the Ecmalisp version is
;;; very slow and bootstraping was annoying.
(progn
(defun prin1-to-string (form)
(cond
- ((symbolp form) (symbol-name form))
+ ((symbolp form)
+ (if (cdr (%find-symbol (symbol-name form) *package*))
+ (symbol-name form)
+ (let ((package (symbol-package form))
+ (name (symbol-name form)))
+ (concat (if (eq package (find-package "KEYWORD"))
+ ""
+ (package-name package))
+ ":" name))))
((integerp form) (integer-to-string form))
((stringp form) (concat "\"" (escape-string form) "\""))
((functionp form)
(prin1-to-string (car last))
(concat (prin1-to-string (car last)) " . " (prin1-to-string (cdr last)))))
")"))
+ ((arrayp form)
+ (concat "#" (prin1-to-string (vector-to-list form))))
((packagep form)
(concat "#<PACKAGE " (package-name form) ">"))))
(ecase (%read-char stream)
(#\'
(list 'function (ls-read stream)))
+ (#\( (list-to-vector (%read-list stream)))
(#\\
(let ((cname
(concat (string (%read-char stream))
(setq package (find-package package))
;; TODO: PACKAGE:SYMBOL should signal error if SYMBOL is not an
;; external symbol from PACKAGE.
- (if internalp
+ (if (or internalp (eq package (find-package "KEYWORD")))
(intern name package)
(find-symbol name package))))
(defun !proclaim (decl)
(case (car decl)
+ (special
+ (dolist (name (cdr decl))
+ (let ((b (global-binding name 'variable 'variable)))
+ (push-binding-declaration 'special b))))
(notinline
(dolist (name (cdr decl))
(let ((b (global-binding name 'function 'function)))
(or (cdr (assoc sexp *literal-symbols*))
(let ((v (genlit))
(s #+common-lisp (concat "{name: \"" (escape-string (symbol-name sexp)) "\"}")
- #+ecmalisp (ls-compile `(intern ,(symbol-name sexp)))))
+ #+ecmalisp (ls-compile
+ `(intern ,(symbol-name sexp)
+ ,(package-name (symbol-package sexp))))))
(push (cons sexp v) *literal-symbols*)
(toplevel-compilation (concat "var " v " = " s))
v)))
c
(let ((v (genlit)))
(toplevel-compilation (concat "var " v " = " c))
- v))))))
+ v))))
+ ((arrayp sexp)
+ (let ((elements (vector-to-list sexp)))
+ (let ((c (concat "[" (join (mapcar #'literal elements) ", ") "]")))
+ (if recursive
+ c
+ (let ((v (genlit)))
+ (toplevel-compilation (concat "var " v " = " c))
+ v)))))))
(define-compilation quote (sexp)
(literal sexp))
(define-compilation progn (&rest body)
(js!selfcall (ls-compile-block body t)))
-(defun dynamic-binding-wrapper (bindings body)
- (if (null bindings)
- body
- (concat
- "try {" *newline*
- (indent
- "var tmp;" *newline*
- (join
- (mapcar (lambda (b)
- (let ((s (ls-compile `(quote ,(car b)))))
- (concat "tmp = " s ".value;" *newline*
- s ".value = " (cdr b) ";" *newline*
- (cdr b) " = tmp;" *newline*)))
- bindings))
- body)
- "}" *newline*
- "finally {" *newline*
- (indent
- (join-trailing
- (mapcar (lambda (b)
- (let ((s (ls-compile `(quote ,(car b)))))
- (concat s ".value" " = " (cdr b))))
- bindings)
- (concat ";" *newline*)))
- "}" *newline*)))
-
+(defun special-variable-p (x)
+ (claimp x 'variable 'special))
+
+;;; Wrap CODE to restore the symbol values of the dynamic
+;;; bindings. BINDINGS is a list of pairs of the form
+;;; (SYMBOL . PLACE), where PLACE is a Javascript variable
+;;; name to initialize the symbol value and where to stored
+;;; the old value.
+(defun let-binding-wrapper (bindings body)
+ (when (null bindings)
+ (return-from let-binding-wrapper body))
+ (concat
+ "try {" *newline*
+ (indent "var tmp;" *newline*
+ (mapconcat
+ (lambda (b)
+ (let ((s (ls-compile `(quote ,(car b)))))
+ (concat "tmp = " s ".value;" *newline*
+ s ".value = " (cdr b) ";" *newline*
+ (cdr b) " = tmp;" *newline*)))
+ bindings)
+ body *newline*)
+ "}" *newline*
+ "finally {" *newline*
+ (indent
+ (mapconcat (lambda (b)
+ (let ((s (ls-compile `(quote ,(car b)))))
+ (concat s ".value" " = " (cdr b) ";" *newline*)))
+ bindings))
+ "}" *newline*))
(define-compilation let (bindings &rest body)
(let ((bindings (mapcar #'ensure-list bindings)))
- (let ((variables (mapcar #'first bindings))
- (values (mapcar #'second bindings)))
- (let ((cvalues (mapcar #'ls-compile values))
- (*environment* (extend-local-env (remove-if #'boundp variables)))
+ (let ((variables (mapcar #'first bindings)))
+ (let ((cvalues (mapcar #'ls-compile (mapcar #'second bindings)))
+ (*environment* (extend-local-env (remove-if #'special-variable-p variables)))
(dynamic-bindings))
(concat "(function("
(join (mapcar (lambda (x)
- (if (boundp x)
+ (if (special-variable-p x)
(let ((v (gvarname x)))
(push (cons x v) dynamic-bindings)
v)
",")
"){" *newline*
(let ((body (ls-compile-block body t)))
- (indent (dynamic-binding-wrapper dynamic-bindings body)))
+ (indent (let-binding-wrapper dynamic-bindings body)))
"})(" (join cvalues ",") ")")))))
+;;; Return the code to initialize BINDING, and push it extending the
+;;; current lexical environment if the variable is special.
+(defun let*-initialize-value (binding)
+ (let ((var (first binding))
+ (value (second binding)))
+ (if (special-variable-p var)
+ (concat (ls-compile `(setq ,var ,value)) ";" *newline*)
+ (let ((v (gvarname var)))
+ (let ((b (make-binding var 'variable v)))
+ (prog1 (concat "var " v " = " (ls-compile value) ";" *newline*)
+ (push-to-lexenv b *environment* 'variable)))))))
+
+;;; Wrap BODY to restore the symbol values of SYMBOLS after body. It
+;;; DOES NOT generate code to initialize the value of the symbols,
+;;; unlike let-binding-wrapper.
+(defun let*-binding-wrapper (symbols body)
+ (when (null symbols)
+ (return-from let*-binding-wrapper body))
+ (let ((store (mapcar (lambda (s) (cons s (gvarname s)))
+ (remove-if-not #'special-variable-p symbols))))
+ (concat
+ "try {" *newline*
+ (indent
+ (mapconcat (lambda (b)
+ (let ((s (ls-compile `(quote ,(car b)))))
+ (concat "var " (cdr b) " = " s ".value;" *newline*)))
+ store)
+ body)
+ "}" *newline*
+ "finally {" *newline*
+ (indent
+ (mapconcat (lambda (b)
+ (let ((s (ls-compile `(quote ,(car b)))))
+ (concat s ".value" " = " (cdr b) ";" *newline*)))
+ store))
+ "}" *newline*)))
+
+
+(define-compilation let* (bindings &rest body)
+ (let ((bindings (mapcar #'ensure-list bindings))
+ (*environment* (copy-lexenv *environment*)))
+ (js!selfcall
+ (let ((specials (remove-if-not #'special-variable-p (mapcar #'first bindings)))
+ (body (concat (mapconcat #'let*-initialize-value bindings)
+ (ls-compile-block body t))))
+ (let*-binding-wrapper specials body)))))
+
+
(defvar *block-counter* 0)
(define-compilation block (name &rest body)
decls)
(concat "return " (progn ,@body) ";" *newline*)))
+;;; VARIABLE-ARITY compiles variable arity operations. ARGS stands for
+;;; a variable which holds a list of forms. It will compile them and
+;;; store the result in some Javascript variables. BODY is evaluated
+;;; with ARGS bound to the list of these variables to generate the
+;;; code which performs the transformation on these variables.
+
+(defun variable-arity-call (args function)
+ (unless (consp args)
+ (error "ARGS must be a non-empty list"))
+ (let ((counter 0)
+ (variables '())
+ (prelude ""))
+ (dolist (x args)
+ (let ((v (concat "x" (integer-to-string (incf counter)))))
+ (push v variables)
+ (concatf prelude
+ (concat "var " v " = " (ls-compile x) ";" *newline*
+ "if (typeof " v " !== 'number') throw 'Not a number!';"
+ *newline*))))
+ (js!selfcall prelude (funcall function (reverse variables)))))
+
+
+(defmacro variable-arity (args &body body)
+ (unless (symbolp args)
+ (error "Bad usage of VARIABLE-ARITY, yo must pass a symbol"))
+ `(variable-arity-call ,args
+ (lambda (,args)
+ (concat "return " ,@body ";" *newline*))))
+
+
(defun num-op-num (x op y)
(type-check (("x" "number" x) ("y" "number" y))
(concat "x" op "y")))
-(define-builtin + (x y) (num-op-num x "+" y))
-(define-builtin - (x y) (num-op-num x "-" y))
+(define-raw-builtin + (&rest numbers)
+ (if (null numbers)
+ "0"
+ (variable-arity numbers
+ (join numbers "+"))))
+
+(define-raw-builtin - (x &rest others)
+ (let ((args (cons x others)))
+ (variable-arity args
+ (if (null others)
+ (concat "-" (car args))
+ (join args "-")))))
+
+
(define-builtin * (x y) (num-op-num x "*" y))
(define-builtin / (x y) (num-op-num x "/" y))
(type-check (("x" "string" x))
"lisp.write(x)"))
+(define-builtin make-array (n)
+ (js!selfcall
+ "var r = [];" *newline*
+ "for (var i = 0; i < " n "; i++)" *newline*
+ (indent "r.push(" (ls-compile nil) ");" *newline*)
+ "return r;" *newline*))
+
+(define-builtin arrayp (x)
+ (js!bool
+ (js!selfcall
+ "var x = " x ";" *newline*
+ "return typeof x === 'object' && 'length' in x;")))
+
+(define-builtin aref (array n)
+ (js!selfcall
+ "var x = " "(" array ")[" n "];" *newline*
+ "if (x === undefined) throw 'Out of range';" *newline*
+ "return x;" *newline*))
+
+(define-builtin aset (array n value)
+ (js!selfcall
+ "var x = " array ";" *newline*
+ "var i = " n ";" *newline*
+ "if (i < 0 || i >= x.length) throw 'Out of range';" *newline*
+ "return x[i] = " value ";" *newline*))
+
+
(defun macro (x)
(and (symbolp x)
(let ((b (lookup-in-lexenv x *environment* 'function)))
((symbolp sexp)
(let ((b (lookup-in-lexenv sexp *environment* 'variable)))
(cond
- ((eq (binding-type b) 'lexical-variable)
+ ((and b (not (member 'special (binding-declarations b))))
(binding-value b))
- ((claimp sexp 'variable 'constant)
+ ((or (keywordp sexp)
+ (member 'constant (binding-declarations b)))
(concat (ls-compile `',sexp) ".value"))
(t
(ls-compile `(symbol-value ',sexp))))))
((integerp sexp) (integer-to-string sexp))
((stringp sexp) (concat "\"" (escape-string sexp) "\""))
+ ((arrayp sexp) (literal sexp))
((listp sexp)
(let ((name (car sexp))
(args (cdr sexp)))
(t
(if (macro name)
(ls-compile (ls-macroexpand-1 sexp))
- (compile-funcall name args))))))))
+ (compile-funcall name args))))))
+ (t
+ (error "How should I compile this?"))))
(defun ls-compile-toplevel (sexp)
(let ((*toplevel-compilations* nil))
(ls-compile-toplevel x))))
(js-eval code)))
- (export '(* *gensym-counter* *package* + - / 1+ 1- < <= = = > >= and append
- apply assoc atom block boundp boundp butlast caar cadddr
- caddr cadr car car case catch cdar cdddr cddr cdr cdr char
- char-code char= code-char cond cons consp copy-list decf
- declaim defparameter defun defvar digit-char-p disassemble
- documentation dolist dotimes ecase eq eql equal error eval
- every export fdefinition find-package find-symbol first
- fourth fset funcall function functionp gensym go identity
- in-package incf integerp integerp intern lambda-code last
- length let list listp make-package make-symbol mapcar
- member minusp mod nil not nth nthcdr null numberp or
- package-name package-use-list packagep plusp
- prin1-to-string print proclaim prog1 prog2 pron push quote
- remove remove-if remove-if-not return return-from
- revappend reverse second set setq some string-upcase
- string string= stringp subseq symbol-function symbol-name
- symbol-package symbol-plist symbol-value symbolp t tagbody
- third throw truncate unless unwind-protect variable warn
- when write-line write-string zerop))
+ (export '(&rest &optional &body * *gensym-counter* *package* + - / 1+ 1- < <= =
+ = > >= and append apply aref arrayp aset assoc atom block boundp
+ boundp butlast caar cadddr caddr cadr car car case catch cdar cdddr
+ cddr cdr cdr char char-code char= code-char cond cons consp copy-list
+ decf declaim defparameter defun defvar digit-char-p disassemble
+ documentation dolist dotimes ecase eq eql equal error eval every
+ export fdefinition find-package find-symbol first fourth fset funcall
+ function functionp gensym go identity if in-package incf integerp
+ integerp intern keywordp lambda last length let let* list-all-packages
+ list listp make-array make-package make-symbol mapcar member minusp
+ mod nil not nth nthcdr null numberp or package-name package-use-list
+ packagep plusp prin1-to-string print proclaim prog1 prog2 pron push
+ quote remove remove-if remove-if-not return return-from revappend
+ reverse second set setq some string-upcase string string= stringp
+ subseq symbol-function symbol-name symbol-package symbol-plist
+ symbol-value symbolp t tagbody third throw truncate unless
+ unwind-protect variable warn when write-line write-string zerop))
(setq *package* *user-package*)