X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=ecmalisp.lisp;h=8b1dcd534102a0ae43ced2f2d5b584a021c5994e;hb=4f3745f9ef0fa39b381e30c3d442245d24520b26;hp=ae075ed6732bc52a2cdcbd8855a9a5fe94b5e0f6;hpb=0006a45b2db78eea44e36db5e1bd9f92f3c62a29;p=jscl.git diff --git a/ecmalisp.lisp b/ecmalisp.lisp index ae075ed..8b1dcd5 100644 --- a/ecmalisp.lisp +++ b/ecmalisp.lisp @@ -94,6 +94,13 @@ (defun null (x) (eq x nil)) + (defun endp (x) + (if (null x) + t + (if (consp x) + nil + (error "type-error")))) + (defmacro return (&optional value) `(return-from nil ,value)) @@ -133,6 +140,7 @@ (defun cadr (x) (car (cdr x))) (defun cdar (x) (cdr (car x))) (defun cddr (x) (cdr (cdr x))) + (defun cadar (x) (car (cdr (car x)))) (defun caddr (x) (car (cdr (cdr x)))) (defun cdddr (x) (cdr (cdr (cdr x)))) (defun cadddr (x) (car (cdr (cdr (cdr x))))) @@ -248,12 +256,12 @@ x (list x))) -(defun !reduce (func list initial) +(defun !reduce (func list &key initial-value) (if (null list) - initial + initial-value (!reduce func (cdr list) - (funcall func initial (car list))))) + :initial-value (funcall func initial-value (car list))))) ;;; Go on growing the Lisp language in Ecmalisp, with more high ;;; level utilities as well as correct versions of other @@ -279,7 +287,7 @@ (append (cdr list1) list2)))) (defun append (&rest lists) - (!reduce #'append-two lists '())) + (!reduce #'append-two lists)) (defun revappend (list1 list2) (while list1 @@ -307,14 +315,14 @@ (setq assignments (reverse assignments)) ;; `(let ,(mapcar #'cdr assignments) - (setq ,@(!reduce #'append (mapcar #'butlast assignments) '()))))) + (setq ,@(!reduce #'append (mapcar #'butlast assignments)))))) (defmacro do (varlist endlist &body body) `(block nil (let ,(mapcar (lambda (x) (list (first x) (second x))) varlist) (while t (when ,(car endlist) - (return (progn ,(cdr endlist)))) + (return (progn ,@(cdr endlist)))) (tagbody ,@body) (psetq ,@(apply #'append @@ -328,7 +336,7 @@ (let* ,(mapcar (lambda (x) (list (first x) (second x))) varlist) (while t (when ,(car endlist) - (return (progn ,(cdr endlist)))) + (return (progn ,@(cdr endlist)))) (tagbody ,@body) (setq ,@(apply #'append @@ -356,15 +364,38 @@ (defun concat-two (s1 s2) (concat-two s1 s2)) - (defun mapcar (func list) - (let* ((head (cons 'sentinel nil)) - (tail head)) - (while (not (null list)) - (let ((new (cons (funcall func (car list)) nil))) - (rplacd tail new) - (setq tail new - list (cdr list)))) - (cdr head))) + (defmacro with-collect (&body body) + (let ((head (gensym)) + (tail (gensym))) + `(let* ((,head (cons 'sentinel nil)) + (,tail ,head)) + (flet ((collect (x) + (rplacd ,tail (cons x nil)) + (setq ,tail (cdr ,tail)) + x)) + ,@body) + (cdr ,head)))) + + (defun map1 (func list) + (with-collect + (while list + (collect (funcall func (car list))) + (setq list (cdr list))))) + + (defmacro loop (&body body) + `(while t ,@body)) + + (defun mapcar (func list &rest lists) + (let ((lists (cons list lists))) + (with-collect + (block loop + (loop + (let ((elems (map1 #'car lists))) + (do ((tail lists (cdr tail))) + ((null tail)) + (when (null (car tail)) (return-from loop)) + (rplaca tail (cdar tail))) + (collect (apply func elems)))))))) (defun identity (x) x) @@ -375,6 +406,13 @@ (defun copy-list (x) (mapcar #'identity x)) + (defun list* (arg &rest others) + (cond ((null others) arg) + ((null (cdr others)) (cons arg (car others))) + (t (do ((x others (cdr x))) + ((null (cddr x)) (rplacd x (cadr x)))) + (cons arg others)))) + (defun code-char (x) x) (defun char-code (x) x) (defun char= (x y) (= x y)) @@ -446,7 +484,7 @@ (defun digit-char (weight) (and (<= 0 weight 9) - (char "0123456789" weight))) + (char "0123456789" weight))) (defun subseq (seq a &optional b) (cond @@ -538,6 +576,145 @@ (defmacro multiple-value-list (value-from) `(multiple-value-call #'list ,value-from)) + +;;; Generalized references (SETF) + + (defvar *setf-expanders* nil) + + (defun get-setf-expansion (place) + (if (symbolp place) + (let ((value (gensym))) + (values nil + nil + `(,value) + `(setq ,place ,value) + place)) + (let ((place (ls-macroexpand-1 place))) + (let* ((access-fn (car place)) + (expander (cdr (assoc access-fn *setf-expanders*)))) + (when (null expander) + (error "Unknown generalized reference.")) + (apply expander (cdr place)))))) + + (defmacro define-setf-expander (access-fn lambda-list &body body) + (unless (symbolp access-fn) + (error "ACCESS-FN must be a symbol.")) + `(progn (push (cons ',access-fn (lambda ,lambda-list ,@body)) + *setf-expanders*) + ',access-fn)) + + (defmacro setf (&rest pairs) + (cond + ((null pairs) + nil) + ((null (cdr pairs)) + (error "Odd number of arguments to setf.")) + ((null (cddr pairs)) + (let ((place (first pairs)) + (value (second pairs))) + (multiple-value-bind (vars vals store-vars writer-form reader-form) + (get-setf-expansion place) + ;; TODO: Optimize the expansion a little bit to avoid let* + ;; or multiple-value-bind when unnecesary. + `(let* ,(mapcar #'list vars vals) + (multiple-value-bind ,store-vars + ,value + ,writer-form))))) + (t + `(progn + ,@(do ((pairs pairs (cddr pairs)) + (result '() (cons `(setf ,(car pairs) ,(cadr pairs)) result))) + ((null pairs) + (reverse result))))))) + + (define-setf-expander car (x) + (let ((cons (gensym)) + (new-value (gensym))) + (values (list cons) + (list x) + (list new-value) + `(progn (rplaca ,cons ,new-value) ,new-value) + `(car ,cons)))) + + (define-setf-expander cdr (x) + (let ((cons (gensym)) + (new-value (gensym))) + (values (list cons) + (list x) + (list new-value) + `(progn (rplacd ,cons ,new-value) ,new-value) + `(car ,cons)))) + +<<<<<<< HEAD + (defmacro push (x place) + (multiple-value-bind (dummies vals newval setter getter) + (get-setf-expansion place) + (let ((g (gensym))) + `(let* ((,g ,x) + ,@(mapcar #'list dummies vals) + (,(car newval) (cons ,g ,getter)) + ,@(cdr newval)) + ,setter)))) +======= + ;; Incorrect typecase, but used in NCONC. + (defmacro typecase (x &rest clausules) + (let ((value (gensym))) + `(let ((,value ,x)) + (cond + ,@(mapcar (lambda (c) + (if (eq (car c) t) + `((t ,@(rest c))) + `((,(ecase (car c) + (integer 'integerp) + (cons 'consp) + (string 'stringp) + (atom 'atom) + (null 'null)) + ,value) + ,@(or (rest c) + (list nil))))) + clausules))))) + + ;; The NCONC function is based on the SBCL's one. + (defun nconc (&rest lists) + (flet ((fail (object) + (error "type-error in nconc"))) + (do ((top lists (cdr top))) + ((null top) nil) + (let ((top-of-top (car top))) + (typecase top-of-top + (cons + (let* ((result top-of-top) + (splice result)) + (do ((elements (cdr top) (cdr elements))) + ((endp elements)) + (let ((ele (car elements))) + (typecase ele + (cons (rplacd (last splice) ele) + (setf splice ele)) + (null (rplacd (last splice) nil)) + (atom (if (cdr elements) + (fail ele) + (rplacd (last splice) ele)))))) + (return result))) + (null) + (atom + (if (cdr top) + (fail top-of-top) + (return top-of-top)))))))) + + (defun nreconc (x y) + (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st))) + (2nd x 1st) ; 2nd follows first down the list. + (3rd y 2nd)) ;3rd follows 2nd down the list. + ((atom 2nd) 3rd) + (rplacd 2nd 3rd))) + + (defun notany (fn seq) + (not (some fn seq))) + +>>>>>>> backquote + ;; Packages (defvar *package-list* nil) @@ -545,7 +722,7 @@ (defun list-all-packages () *package-list*) - (defun make-package (name &optional use) + (defun make-package (name &key use) (let ((package (new)) (use (mapcar #'find-package-or-fail use))) (oset package "packageName" name) @@ -590,7 +767,7 @@ (make-package "CL")) (defvar *user-package* - (make-package "CL-USER" (list *common-lisp-package*))) + (make-package "CL-USER" :use (list *common-lisp-package*))) (defvar *keyword-package* (make-package "KEYWORD")) @@ -690,7 +867,7 @@ (defvar *newline* (string (code-char 10))) (defun concat (&rest strs) - (!reduce #'concat-two strs "")) + (!reduce #'concat-two strs :initial-value "")) (defmacro concatf (variable &body form) `(setq ,variable (concat ,variable (progn ,@form)))) @@ -736,39 +913,6 @@ (defun values (&rest args) (values-list args))) - -;;; 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. - -#+ecmalisp -(defun indent (&rest string) - (let ((input (join string))) - (let ((output "") - (index 0) - (size (length input))) - (when (plusp (length input)) (concatf output " ")) - (while (< index size) - (let ((str - (if (and (char= (char input index) #\newline) - (< index (1- size)) - (not (char= (char input (1+ index)) #\newline))) - (concat (string #\newline) " ") - (string (char input index))))) - (concatf output str)) - (incf index)) - output))) - -#+common-lisp -(defun indent (&rest string) - (with-output-to-string (*standard-output*) - (with-input-from-string (input (join string)) - (loop - for line = (read-line input nil) - while line - do (write-string " ") - do (write-line line))))) - (defun integer-to-string (x) (cond ((zerop x) @@ -784,20 +928,6 @@ digits))))) -;;; Wrap X with a Javascript code to convert the result from -;;; Javascript generalized booleans to T or NIL. -(defun js!bool (x) - (concat "(" x "?" (ls-compile t) ": " (ls-compile nil) ")")) - -;;; Concatenate the arguments and wrap them with a self-calling -;;; Javascript anonymous function. It is used to make some Javascript -;;; statements valid expressions and provide a private scope as well. -;;; It could be defined as function, but we could do some -;;; preprocessing in the future. -(defmacro js!selfcall (&body body) - `(concat "(function(){" *newline* (indent ,@body) "})()")) - - ;;; Printer #+ecmalisp @@ -850,6 +980,7 @@ x)) + ;;;; Reader ;;; The Lisp reader, parse strings and return Lisp objects. The main @@ -1069,6 +1200,61 @@ ;;; too. The respective real functions are defined in the target (see ;;; the beginning of this file) as well as some primitive functions. +(defun code (&rest args) + (mapconcat (lambda (arg) + (cond + ((null arg) "") + ((integerp arg) (integer-to-string arg)) + ((stringp arg) arg) + (t (error "Unknown argument.")))) + args)) + +;;; Wrap X with a Javascript code to convert the result from +;;; Javascript generalized booleans to T or NIL. +(defun js!bool (x) + (code "(" x "?" (ls-compile t) ": " (ls-compile nil) ")")) + +;;; Concatenate the arguments and wrap them with a self-calling +;;; Javascript anonymous function. It is used to make some Javascript +;;; statements valid expressions and provide a private scope as well. +;;; It could be defined as function, but we could do some +;;; preprocessing in the future. +(defmacro js!selfcall (&body body) + `(code "(function(){" *newline* (indent ,@body) "})()")) + +;;; Like CODE, 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. + +#+ecmalisp +(defun indent (&rest string) + (let ((input (apply #'code string))) + (let ((output "") + (index 0) + (size (length input))) + (when (plusp (length input)) (concatf output " ")) + (while (< index size) + (let ((str + (if (and (char= (char input index) #\newline) + (< index (1- size)) + (not (char= (char input (1+ index)) #\newline))) + (concat (string #\newline) " ") + (string (char input index))))) + (concatf output str)) + (incf index)) + output))) + +#+common-lisp +(defun indent (&rest string) + (with-output-to-string (*standard-output*) + (with-input-from-string (input (apply #'code string)) + (loop + for line = (read-line input nil) + while line + do (write-string " ") + do (write-line line))))) + + ;;; 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 @@ -1077,7 +1263,6 @@ ;;; function call. (defvar *multiple-value-p* nil) - (defun make-binding (name type value &optional declarations) (list name type value declarations)) @@ -1125,7 +1310,7 @@ (defvar *variable-counter* 0) (defun gvarname (symbol) - (concat "v" (integer-to-string (incf *variable-counter*)))) + (code "v" (incf *variable-counter*))) (defun translate-variable (symbol) (binding-value (lookup-in-lexenv symbol *environment* 'variable))) @@ -1193,56 +1378,58 @@ *compilations*)) (define-compilation if (condition true false) - (concat "(" (ls-compile condition) " !== " (ls-compile nil) - " ? " (ls-compile true *multiple-value-p*) - " : " (ls-compile false *multiple-value-p*) - ")")) + (code "(" (ls-compile condition) " !== " (ls-compile nil) + " ? " (ls-compile true *multiple-value-p*) + " : " (ls-compile false *multiple-value-p*) + ")")) -(defvar *lambda-list-keywords* '(&optional &rest &key)) +(defvar *ll-keywords* '(&optional &rest &key)) (defun list-until-keyword (list) - (if (or (null list) (member (car list) *lambda-list-keywords*)) + (if (or (null list) (member (car list) *ll-keywords*)) nil (cons (car list) (list-until-keyword (cdr list))))) -(defun lambda-list-section (keyword lambda-list) - (list-until-keyword (cdr (member keyword lambda-list)))) +(defun ll-section (keyword ll) + (list-until-keyword (cdr (member keyword ll)))) -(defun lambda-list-required-arguments (lambda-list) - (list-until-keyword lambda-list)) +(defun ll-required-arguments (ll) + (list-until-keyword ll)) -(defun lambda-list-optional-arguments-with-default (lambda-list) - (mapcar #'ensure-list (lambda-list-section '&optional lambda-list))) +(defun ll-optional-arguments-canonical (ll) + (mapcar #'ensure-list (ll-section '&optional ll))) -(defun lambda-list-optional-arguments (lambda-list) - (mapcar #'car (lambda-list-optional-arguments-with-default lambda-list))) +(defun ll-optional-arguments (ll) + (mapcar #'car (ll-optional-arguments-canonical ll))) -(defun lambda-list-rest-argument (lambda-list) - (let ((rest (lambda-list-section '&rest lambda-list))) +(defun ll-rest-argument (ll) + (let ((rest (ll-section '&rest ll))) (when (cdr rest) (error "Bad lambda-list")) (car rest))) -(defun lambda-list-keyword-arguments-canonical (lambda-list) - (flet ((canonalize (keyarg) +(defun ll-keyword-arguments-canonical (ll) + (flet ((canonicalize (keyarg) ;; Build a canonical keyword argument descriptor, filling ;; the optional fields. The result is a list of the form ;; ((keyword-name var) init-form). - (let* ((arg (ensure-list keyarg)) - (init-form (cadr arg)) - var - keyword-name) - (if (listp (car arg)) - (setq var (cadr (car arg)) - keyword-name (car (car arg))) - (setq var (car arg) - keyword-name (intern (symbol-name (car arg)) "KEYWORD"))) - `((,keyword-name ,var) ,init-form)))) - (mapcar #'canonalize (lambda-list-section '&key lambda-list)))) - -(defun lambda-list-keyword-arguments (lambda-list) + (let ((arg (ensure-list keyarg))) + (cons (if (listp (car arg)) + (car arg) + (list (intern (symbol-name (car arg)) "KEYWORD") (car arg))) + (cdr arg))))) + (mapcar #'canonicalize (ll-section '&key ll)))) + +(defun ll-keyword-arguments (ll) (mapcar (lambda (keyarg) (second (first keyarg))) - (lambda-list-keyword-arguments-canonical lambda-list))) + (ll-keyword-arguments-canonical ll))) + +(defun ll-svars (lambda-list) + (let ((args + (append + (ll-keyword-arguments-canonical lambda-list) + (ll-optional-arguments-canonical lambda-list)))) + (remove nil (mapcar #'third args)))) (defun lambda-docstring-wrapper (docstring &rest strs) (if docstring @@ -1250,7 +1437,7 @@ "var func = " (join strs) ";" *newline* "func.docstring = '" docstring "';" *newline* "return func;" *newline*) - (join strs))) + (apply #'code strs))) (defun lambda-check-argument-count (n-required-arguments n-optional-arguments rest-p) @@ -1261,115 +1448,119 @@ (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*))) + (return (code "checkArgs(arguments, " 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-optional (lambda-list) - (let* ((optional-arguments (lambda-list-optional-arguments lambda-list)) - (n-required-arguments (length (lambda-list-required-arguments lambda-list))) + (code + (when (< 1 min) + (code "checkArgsAtLeast(arguments, " min ");" *newline*)) + (when (numberp max) + (code "checkArgsAtMost(arguments, " max ");" *newline*)))))) + +(defun compile-lambda-optional (ll) + (let* ((optional-arguments (ll-optional-arguments-canonical ll)) + (n-required-arguments (length (ll-required-arguments ll))) (n-optional-arguments (length optional-arguments))) - (if optional-arguments - (concat "switch(arguments.length-1){" *newline* - (let ((optional-and-defaults - (lambda-list-optional-arguments-with-default lambda-list)) - (cases nil) - (idx 0)) - (progn - (while (< idx n-optional-arguments) - (let ((arg (nth idx optional-and-defaults))) - (push (concat "case " - (integer-to-string (+ idx n-required-arguments)) ":" *newline* - (translate-variable (car arg)) - "=" - (ls-compile (cadr arg)) - ";" *newline*) - cases) - (incf idx))) - (push (concat "default: break;" *newline*) cases) - (join (reverse cases)))) - "}" *newline*) - ""))) - -(defun compile-lambda-rest (lambda-list) - (let ((n-required-arguments (length (lambda-list-required-arguments lambda-list))) - (n-optional-arguments (length (lambda-list-optional-arguments lambda-list))) - (rest-argument (lambda-list-rest-argument lambda-list))) - (if rest-argument - (let ((js!rest (translate-variable rest-argument))) - (concat "var " js!rest "= " (ls-compile nil) ";" *newline* - "for (var i = arguments.length-1; i>=" - (integer-to-string (+ 1 n-required-arguments n-optional-arguments)) - "; i--)" *newline* - (indent js!rest " = " - "{car: arguments[i], cdr: ") js!rest "};" - *newline*)) - ""))) - -(defun compile-lambda-parse-keywords (lambda-list) + (when optional-arguments + (code (mapconcat (lambda (arg) + (code "var " (translate-variable (first arg)) "; " *newline* + (when (third arg) + (code "var " (translate-variable (third arg)) + " = " (ls-compile t) + "; " *newline*)))) + optional-arguments) + "switch(arguments.length-1){" *newline* + (let ((cases nil) + (idx 0)) + (progn + (while (< idx n-optional-arguments) + (let ((arg (nth idx optional-arguments))) + (push (code "case " (+ idx n-required-arguments) ":" *newline* + (indent (translate-variable (car arg)) + "=" + (ls-compile (cadr arg)) ";" *newline*) + (when (third arg) + (indent (translate-variable (third arg)) + "=" + (ls-compile nil) + ";" *newline*))) + cases) + (incf idx))) + (push (code "default: break;" *newline*) cases) + (join (reverse cases)))) + "}" *newline*)))) + +(defun compile-lambda-rest (ll) + (let ((n-required-arguments (length (ll-required-arguments ll))) + (n-optional-arguments (length (ll-optional-arguments ll))) + (rest-argument (ll-rest-argument ll))) + (when rest-argument + (let ((js!rest (translate-variable rest-argument))) + (code "var " js!rest "= " (ls-compile nil) ";" *newline* + "for (var i = arguments.length-1; i>=" + (+ 1 n-required-arguments n-optional-arguments) + "; i--)" *newline* + (indent js!rest " = {car: arguments[i], cdr: ") js!rest "};" + *newline*))))) + +(defun compile-lambda-parse-keywords (ll) (let ((n-required-arguments - (length (lambda-list-required-arguments lambda-list))) + (length (ll-required-arguments ll))) (n-optional-arguments - (length (lambda-list-optional-arguments lambda-list))) + (length (ll-optional-arguments ll))) (keyword-arguments - (lambda-list-keyword-arguments-canonical lambda-list))) - (concat - "var i;" *newline* + (ll-keyword-arguments-canonical ll))) + (code ;; Declare variables (mapconcat (lambda (arg) (let ((var (second (car arg)))) - (concat "var " (translate-variable var) "; " *newline*))) + (code "var " (translate-variable var) "; " *newline* + (when (third arg) + (code "var " (translate-variable (third arg)) + " = " (ls-compile nil) + ";" *newline*))))) keyword-arguments) ;; Parse keywords (flet ((parse-keyword (keyarg) ;; ((keyword-name var) init-form) - (concat "for (i=" - (integer-to-string (+ 1 n-required-arguments n-optional-arguments)) - "; i (LIST* a b c foo) +;;; provided a, b, c are not splicing frobs +;;; (APPEND (LIST* a b c) foo) => (LIST* a b (APPEND c foo)) +;;; provided a, b, c are not splicing frobs +;;; (APPEND (QUOTE (x)) foo) => (LIST* (QUOTE x) foo) +;;; (APPEND (CLOBBERABLE x) foo) => (NCONC x foo) +(defun bq-simplify (x) + (if (atom x) + x + (let ((x (if (eq (car x) *bq-quote*) + x + (maptree #'bq-simplify x)))) + (if (not (eq (car x) *bq-append*)) + x + (bq-simplify-args x))))) + +(defun bq-simplify-args (x) + (do ((args (reverse (cdr x)) (cdr args)) + (result + nil + (cond ((atom (car args)) + (bq-attach-append *bq-append* (car args) result)) + ((and (eq (caar args) *bq-list*) + (notany #'bq-splicing-frob (cdar args))) + (bq-attach-conses (cdar args) result)) + ((and (eq (caar args) *bq-list**) + (notany #'bq-splicing-frob (cdar args))) + (bq-attach-conses + (reverse (cdr (reverse (cdar args)))) + (bq-attach-append *bq-append* + (car (last (car args))) + result))) + ((and (eq (caar args) *bq-quote*) + (consp (cadar args)) + (not (bq-frob (cadar args))) + (null (cddar args))) + (bq-attach-conses (list (list *bq-quote* + (caadar args))) + result)) + ((eq (caar args) *bq-clobberable*) + (bq-attach-append *bq-nconc* (cadar args) result)) + (t (bq-attach-append *bq-append* + (car args) + result))))) + ((null args) result))) + +(defun null-or-quoted (x) + (or (null x) (and (consp x) (eq (car x) *bq-quote*)))) + +;;; When BQ-ATTACH-APPEND is called, the OP should be #:BQ-APPEND +;;; or #:BQ-NCONC. This produces a form (op item result) but +;;; some simplifications are done on the fly: +;;; +;;; (op '(a b c) '(d e f g)) => '(a b c d e f g) +;;; (op item 'nil) => item, provided item is not a splicable frob +;;; (op item 'nil) => (op item), if item is a splicable frob +;;; (op item (op a b c)) => (op item a b c) +(defun bq-attach-append (op item result) + (cond ((and (null-or-quoted item) (null-or-quoted result)) + (list *bq-quote* (append (cadr item) (cadr result)))) + ((or (null result) (equal result *bq-quote-nil*)) + (if (bq-splicing-frob item) (list op item) item)) + ((and (consp result) (eq (car result) op)) + (list* (car result) item (cdr result))) + (t (list op item result)))) + +;;; The effect of BQ-ATTACH-CONSES is to produce a form as if by +;;; `(LIST* ,@items ,result) but some simplifications are done +;;; on the fly. +;;; +;;; (LIST* 'a 'b 'c 'd) => '(a b c . d) +;;; (LIST* a b c 'nil) => (LIST a b c) +;;; (LIST* a b c (LIST* d e f g)) => (LIST* a b c d e f g) +;;; (LIST* a b c (LIST d e f g)) => (LIST a b c d e f g) +(defun bq-attach-conses (items result) + (cond ((and (every #'null-or-quoted items) + (null-or-quoted result)) + (list *bq-quote* + (append (mapcar #'cadr items) (cadr result)))) + ((or (null result) (equal result *bq-quote-nil*)) + (cons *bq-list* items)) + ((and (consp result) + (or (eq (car result) *bq-list*) + (eq (car result) *bq-list**))) + (cons (car result) (append items (cdr result)))) + (t (cons *bq-list** (append items (list result)))))) + +;;; Removes funny tokens and changes (#:BQ-LIST* a b) into +;;; (CONS a b) instead of (LIST* a b), purely for readability. +(defun bq-remove-tokens (x) + (cond ((eq x *bq-list*) 'list) + ((eq x *bq-append*) 'append) + ((eq x *bq-nconc*) 'nconc) + ((eq x *bq-list**) 'list*) + ((eq x *bq-quote*) 'quote) + ((atom x) x) + ((eq (car x) *bq-clobberable*) + (bq-remove-tokens (cadr x))) + ((and (eq (car x) *bq-list**) + (consp (cddr x)) + (null (cdddr x))) + (cons 'cons (maptree #'bq-remove-tokens (cdr x)))) + (t (maptree #'bq-remove-tokens x)))) (define-transformation backquote (form) - (backquote-expand-1 form)) + (bq-completely-process form)) + ;;; Primitives @@ -1903,27 +2309,26 @@ *builtins*)) (defmacro define-builtin (name args &body body) - `(progn - (define-raw-builtin ,name ,args - (let ,(mapcar (lambda (arg) `(,arg (ls-compile ,arg))) args) - ,@body)))) + `(define-raw-builtin ,name ,args + (let ,(mapcar (lambda (arg) `(,arg (ls-compile ,arg))) args) + ,@body))) ;;; DECLS is a list of (JSVARNAME TYPE LISPFORM) declarations. (defmacro type-check (decls &body body) `(js!selfcall ,@(mapcar (lambda (decl) - `(concat "var " ,(first decl) " = " ,(third decl) ";" *newline*)) - decls) + `(code "var " ,(first decl) " = " ,(third decl) ";" *newline*)) + decls) ,@(mapcar (lambda (decl) - `(concat "if (typeof " ,(first decl) " != '" ,(second decl) "')" *newline* - (indent "throw 'The value ' + " - ,(first decl) - " + ' is not a type " - ,(second decl) - ".';" - *newline*))) + `(code "if (typeof " ,(first decl) " != '" ,(second decl) "')" *newline* + (indent "throw 'The value ' + " + ,(first decl) + " + ' is not a type " + ,(second decl) + ".';" + *newline*))) decls) - (concat "return " (progn ,@body) ";" *newline*))) + (code "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 @@ -1935,16 +2340,18 @@ (unless (consp args) (error "ARGS must be a non-empty list")) (let ((counter 0) - (variables '()) + (fargs '()) (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))))) + (if (numberp x) + (push (integer-to-string x) fargs) + (let ((v (code "x" (incf counter)))) + (push v fargs) + (concatf prelude + (code "var " v " = " (ls-compile x) ";" *newline* + "if (typeof " v " !== 'number') throw 'Not a number!';" + *newline*))))) + (js!selfcall prelude (funcall function (reverse fargs))))) (defmacro variable-arity (args &body body) @@ -1952,11 +2359,11 @@ (error "Bad usage of VARIABLE-ARITY, you must pass a symbol")) `(variable-arity-call ,args (lambda (,args) - (concat "return " ,@body ";" *newline*)))) + (code "return " ,@body ";" *newline*)))) (defun num-op-num (x op y) (type-check (("x" "number" x) ("y" "number" y)) - (concat "x" op "y"))) + (code "x" op "y"))) (define-raw-builtin + (&rest numbers) (if (null numbers) @@ -2011,14 +2418,14 @@ (define-builtin-comparison = "==") (define-builtin numberp (x) - (js!bool (concat "(typeof (" x ") == \"number\")"))) + (js!bool (code "(typeof (" x ") == \"number\")"))) (define-builtin floor (x) (type-check (("x" "number" x)) "Math.floor(x)")) (define-builtin cons (x y) - (concat "({car: " x ", cdr: " y "})")) + (code "({car: " x ", cdr: " y "})")) (define-builtin consp (x) (js!bool @@ -2042,11 +2449,11 @@ (define-builtin rplaca (x new) (type-check (("x" "object" x)) - (concat "(x.car = " new ", x)"))) + (code "(x.car = " new ", x)"))) (define-builtin rplacd (x new) (type-check (("x" "object" x)) - (concat "(x.cdr = " new ", x)"))) + (code "(x.cdr = " new ", x)"))) (define-builtin symbolp (x) (js!bool @@ -2059,16 +2466,16 @@ "({name: name})")) (define-builtin symbol-name (x) - (concat "(" x ").name")) + (code "(" x ").name")) (define-builtin set (symbol value) - (concat "(" symbol ").value = " value)) + (code "(" symbol ").value = " value)) (define-builtin fset (symbol value) - (concat "(" symbol ").fvalue = " value)) + (code "(" symbol ").fvalue = " value)) (define-builtin boundp (x) - (js!bool (concat "(" x ".value !== undefined)"))) + (js!bool (code "(" x ".value !== undefined)"))) (define-builtin symbol-value (x) (js!selfcall @@ -2085,20 +2492,20 @@ "return func;" *newline*)) (define-builtin symbol-plist (x) - (concat "((" x ").plist || " (ls-compile nil) ")")) + (code "((" x ").plist || " (ls-compile nil) ")")) (define-builtin lambda-code (x) - (concat "(" x ").toString()")) + (code "(" x ").toString()")) -(define-builtin eq (x y) (js!bool (concat "(" x " === " y ")"))) -(define-builtin equal (x y) (js!bool (concat "(" x " == " y ")"))) +(define-builtin eq (x y) (js!bool (code "(" x " === " y ")"))) +(define-builtin equal (x y) (js!bool (code "(" x " == " y ")"))) (define-builtin char-to-string (x) (type-check (("x" "number" x)) "String.fromCharCode(x)")) (define-builtin stringp (x) - (js!bool (concat "(typeof(" x ") == \"string\")"))) + (js!bool (code "(typeof(" x ") == \"string\")"))) (define-builtin string-upcase (x) (type-check (("x" "string" x)) @@ -2113,9 +2520,7 @@ "var str = " (ls-compile string) ";" *newline* "var a = " (ls-compile a) ";" *newline* "var b;" *newline* - (if b - (concat "b = " (ls-compile b) ";" *newline*) - "") + (when b (code "b = " (ls-compile b) ";" *newline*)) "return str.slice(a,b);" *newline*)) (define-builtin char (string index) @@ -2129,15 +2534,17 @@ "string1.concat(string2)")) (define-raw-builtin funcall (func &rest args) - (concat "(" (ls-compile func) ")(" - (join (cons (if *multiple-value-p* "values" "pv") - (mapcar #'ls-compile args)) - ", ") - ")")) + (js!selfcall + "var f = " (ls-compile func) ";" *newline* + "return (typeof f === 'function'? f: f.fvalue)(" + (join (cons (if *multiple-value-p* "values" "pv") + (mapcar #'ls-compile args)) + ", ") + ")")) (define-raw-builtin apply (func &rest args) (if (null args) - (concat "(" (ls-compile func) ")()") + (code "(" (ls-compile func) ")()") (let ((args (butlast args)) (last (car (last args)))) (js!selfcall @@ -2151,7 +2558,7 @@ " args.push(tail.car);" *newline* " tail = tail.cdr;" *newline* "}" *newline* - "return f.apply(this, args);" *newline*)))) + "return (typeof f === 'function'? f : f.fvalue).apply(this, args);" *newline*)))) (define-builtin js-eval (string) (type-check (("string" "string" string)) @@ -2171,7 +2578,7 @@ (define-builtin new () "{}") (define-builtin objectp (x) - (js!bool (concat "(typeof (" x ") === 'object')"))) + (js!bool (code "(typeof (" x ") === 'object')"))) (define-builtin oget (object key) (js!selfcall @@ -2179,13 +2586,13 @@ "return tmp == undefined? " (ls-compile nil) ": tmp ;" *newline*)) (define-builtin oset (object key value) - (concat "((" object ")[" key "] = " value ")")) + (code "((" object ")[" key "] = " value ")")) (define-builtin in (key object) - (js!bool (concat "((" key ") in (" object "))"))) + (js!bool (code "((" key ") in (" object "))"))) (define-builtin functionp (x) - (js!bool (concat "(typeof " x " == 'function')"))) + (js!bool (code "(typeof " x " == 'function')"))) (define-builtin write-string (x) (type-check (("x" "string" x)) @@ -2218,17 +2625,17 @@ "return x[i] = " value ";" *newline*)) (define-builtin get-unix-time () - (concat "(Math.round(new Date() / 1000))")) + (code "(Math.round(new Date() / 1000))")) (define-builtin values-array (array) (if *multiple-value-p* - (concat "values.apply(this, " array ")") - (concat "pv.apply(this, " array ")"))) + (code "values.apply(this, " array ")") + (code "pv.apply(this, " array ")"))) (define-raw-builtin values (&rest args) (if *multiple-value-p* - (concat "values(" (join (mapcar #'ls-compile args) ", ") ")") - (concat "pv(" (join (mapcar #'ls-compile args) ", ") ")"))) + (code "values(" (join (mapcar #'ls-compile args) ", ") ")") + (code "pv(" (join (mapcar #'ls-compile args) ", ") ")"))) (defun macro (x) (and (symbolp x) @@ -2263,14 +2670,14 @@ ((and (symbolp function) #+ecmalisp (eq (symbol-package function) (find-package "COMMON-LISP")) #+common-lisp t) - (concat (ls-compile `',function) ".fvalue" arglist)) + (code (ls-compile `',function) ".fvalue" arglist)) (t - (concat (ls-compile `#',function) arglist))))) + (code (ls-compile `#',function) arglist))))) (defun ls-compile-block (sexps &optional return-last-p) (if return-last-p - (concat (ls-compile-block (butlast sexps)) - "return " (ls-compile (car (last sexps)) *multiple-value-p*) ";") + (code (ls-compile-block (butlast sexps)) + "return " (ls-compile (car (last sexps)) *multiple-value-p*) ";") (join-trailing (remove-if #'null-or-empty-p (mapcar #'ls-compile sexps)) (concat ";" *newline*)))) @@ -2285,11 +2692,11 @@ (binding-value b)) ((or (keywordp sexp) (member 'constant (binding-declarations b))) - (concat (ls-compile `',sexp) ".value")) + (code (ls-compile `',sexp) ".value")) (t (ls-compile `(symbol-value ',sexp)))))) ((integerp sexp) (integer-to-string sexp)) - ((stringp sexp) (concat "\"" (escape-string sexp) "\"")) + ((stringp sexp) (code "\"" (escape-string sexp) "\"")) ((arrayp sexp) (literal sexp)) ((listp sexp) (let ((name (car sexp)) @@ -2309,7 +2716,7 @@ (ls-compile (ls-macroexpand-1 sexp) multiple-value-p) (compile-funcall name args)))))) (t - (error "How should I compile this?"))))) + (error (concat "How should I compile " (prin1-to-string sexp) "?")))))) (defun ls-compile-toplevel (sexp &optional multiple-value-p) (let ((*toplevel-compilations* nil)) @@ -2321,11 +2728,10 @@ (join (remove-if #'null-or-empty-p subs)))) (t (let ((code (ls-compile sexp multiple-value-p))) - (concat (join-trailing (get-toplevel-compilations) - (concat ";" *newline*)) - (if code - (concat code ";" *newline*) - ""))))))) + (code (join-trailing (get-toplevel-compilations) + (code ";" *newline*)) + (when code + (code code ";" *newline*)))))))) ;;; Once we have the compiler, we define the runtime environment and @@ -2337,31 +2743,27 @@ (defun eval (x) (js-eval (ls-compile-toplevel x t))) - (export '(&rest &key &optional &body * *gensym-counter* *package* + - - / 1+ 1- < <= = = > >= and append apply aref arrayp 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 constantly copy-list decf - declaim defconstant defparameter defun defmacro defvar - digit-char digit-char-p disassemble do do* documentation - dolist dotimes ecase eq eql equal error eval every export - fdefinition find-package find-symbol first flet fourth - fset funcall function functionp gensym get-universal-time - go identity if in-package incf integerp integerp intern - keywordp labels lambda last length let let* - list-all-packages list listp make-array make-package - make-symbol mapcar member minusp mod multiple-value-bind - multiple-value-call multiple-value-list - multiple-value-prog1 nil not nth nthcdr null numberp or - package-name package-use-list packagep parse-integer plusp - prin1-to-string print proclaim prog1 prog2 progn psetq - push quote remove remove-if remove-if-not return - return-from revappend reverse rplaca rplacd 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 values values-list variable warn when - write-line write-string zeropt)) + (export '(&rest &key &optional &body * *gensym-counter* *package* + - / 1+ 1- < + <= = = > >= and append apply aref arrayp assoc atom block boundp + boundp butlast caar cadddr caddr cadr car car case catch cdar cdddr + cddr cdr cdr char char-code fdefinition find-package find-symbol first + flet fourth fset funcall function functionp gensym get-setf-expansion + get-universal-time go identity if in-package incf integerp integerp + intern keywordp labels lambda last length let let* char= code-char + cond cons consp constantly copy-list decf declaim define-setf-expander + defconstant defparameter defun defmacro defvar digit-char digit-char-p + disassemble do do* documentation dolist dotimes ecase eq eql equal + error eval every export list-all-packages list list* listp loop make-array + make-package make-symbol mapcar member minusp mod multiple-value-bind + multiple-value-call multiple-value-list multiple-value-prog1 nconc nil not + nth nthcdr null numberp or package-name package-use-list packagep + parse-integer plusp prin1-to-string print proclaim prog1 prog2 progn + psetq push quote nreconc remove remove-if remove-if-not return return-from + revappend reverse rplaca rplacd second set setf 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 values values-list variable + warn when write-line write-string zerop)) (setq *package* *user-package*) @@ -2378,12 +2780,15 @@ ;; environment at this point of the compilation. (eval-when-compile (toplevel-compilation + (ls-compile `(setq *environment* ',*environment*)))) + + (eval-when-compile + (toplevel-compilation (ls-compile `(progn ,@(mapcar (lambda (s) `(%intern-symbol (js-vref ,(cdr s)))) *literal-symbols*) (setq *literal-symbols* ',*literal-symbols*) - (setq *environment* ',*environment*) (setq *variable-counter* ,*variable-counter*) (setq *gensym-counter* ,*gensym-counter*) (setq *block-counter* ,*block-counter*)))))