X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=ecmalisp.lisp;h=514e25991baf2cfc591ad5a8d9af95f59cb33395;hb=fd65b669013476e4fd4348dd4e415bc44b812966;hp=77495e808eeba42f9eb8c98c9b581b6c543c25a8;hpb=b8a93d871991dd05aefa4a674320f824f5149795;p=jscl.git diff --git a/ecmalisp.lisp b/ecmalisp.lisp index 77495e8..514e259 100644 --- a/ecmalisp.lisp +++ b/ecmalisp.lisp @@ -42,10 +42,17 @@ `(eval-when-compile ,@(mapcar (lambda (decl) `(!proclaim ',decl)) decls))) - (declaim (constant nil t) (special t nil)) - (setq nil 'nil) + (defmacro defconstant (name value &optional docstring) + `(progn + (declaim (special ,name)) + (declaim (constant ,name)) + (setq ,name ,value) + ,@(when (stringp docstring) `((oset ',name "vardoc" ,docstring))) + ',name)) + + (defconstant t 't) + (defconstant nil 'nil) (js-vset "nil" nil) - (setq t 't) (defmacro lambda (args &body body) `(function (lambda ,args ,@body))) @@ -241,12 +248,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 @@ -272,7 +279,7 @@ (append (cdr list1) list2)))) (defun append (&rest lists) - (!reduce #'append-two lists '())) + (!reduce #'append-two lists)) (defun revappend (list1 list2) (while list1 @@ -300,7 +307,7 @@ (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 @@ -349,15 +356,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) @@ -439,7 +469,7 @@ (defun digit-char (weight) (and (<= 0 weight 9) - (char "0123456789" weight))) + (char "0123456789" weight))) (defun subseq (seq a &optional b) (cond @@ -531,14 +561,82 @@ (defmacro multiple-value-list (value-from) `(multiple-value-call #'list ,value-from)) - ;; Packages + + ;;; 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* ((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)))) + + ;;; Packages (defvar *package-list* nil) (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) @@ -583,7 +681,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")) @@ -683,7 +781,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)))) @@ -729,39 +827,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) @@ -777,20 +842,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 @@ -1062,6 +1113,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 @@ -1070,7 +1176,6 @@ ;;; function call. (defvar *multiple-value-p* nil) - (defun make-binding (name type value &optional declarations) (list name type value declarations)) @@ -1118,7 +1223,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))) @@ -1186,56 +1291,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 @@ -1243,7 +1350,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) @@ -1254,115 +1361,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 >= 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 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 zerop)) + (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 loop 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 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*) @@ -2371,12 +2481,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*)))))