X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=ecmalisp.lisp;h=149a7779fbb38d971d882b9e9e08aceacc87abaa;hb=4e103a7d0be6bf1e3cd0ed39934afb36e525a243;hp=28554e1547dc71216169c24a61a04a1ec76db45f;hpb=e943f22043799553b113dc6955367b60bc16c683;p=jscl.git diff --git a/ecmalisp.lisp b/ecmalisp.lisp index 28554e1..149a777 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))) @@ -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,7 +561,75 @@ (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) @@ -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,10 +1291,10 @@ *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)) @@ -1237,14 +1342,13 @@ (mapcar (lambda (keyarg) (second (first keyarg))) (lambda-list-keyword-arguments-canonical lambda-list))) - (defun lambda-docstring-wrapper (docstring &rest strs) (if docstring (js!selfcall "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) @@ -1255,23 +1359,107 @@ (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*) - ""))))) + (code + (when (< 1 min) + (code "checkArgsAtLeast(arguments, " min ");" *newline*)) + (when (numberp max) + (code "checkArgsAtMost(arguments, " 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))) + (n-optional-arguments (length optional-arguments))) + (when optional-arguments + (code "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 (code "case " (+ idx n-required-arguments) ":" *newline* + (translate-variable (car arg)) + "=" + (ls-compile (cadr arg)) + ";" *newline*) + cases) + (incf idx))) + (push (code "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))) + (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 (lambda-list) + (let ((n-required-arguments + (length (lambda-list-required-arguments lambda-list))) + (n-optional-arguments + (length (lambda-list-optional-arguments lambda-list))) + (keyword-arguments + (lambda-list-keyword-arguments-canonical lambda-list))) + (code + ;; Declare variables + (mapconcat (lambda (arg) + (let ((var (second (car arg)))) + (code "var " (translate-variable var) "; " *newline*))) + keyword-arguments) + ;; Parse keywords + (flet ((parse-keyword (keyarg) + ;; ((keyword-name var) init-form) + (code "for (i=" (+ 1 n-required-arguments n-optional-arguments) + "; i=" - (integer-to-string (+ 1 n-required-arguments n-optional-arguments)) - "; i--)" *newline* - (indent js!rest " = " - "{car: arguments[i], cdr: ") js!rest "};" - *newline*)) - "") - - ;; &key arguments - "var i;" *newline* - (mapconcat (lambda (arg) - (concat "var " (translate-variable arg) "; " *newline*)) - keyword-arguments) - (mapconcat (lambda (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 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*) @@ -2352,12 +2464,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*)))))