X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=ecmalisp.lisp;h=5b40d7cbb60fc7fe6b395b96115c6e7acd75d1a4;hb=718c909c1cca49aa45488505a721766ce94b2377;hp=e265af9514a8b847a5b6085c3285962b441e03c4;hpb=7f57ec1fea83cf05ef11c4b83920a63d361506be;p=jscl.git diff --git a/ecmalisp.lisp b/ecmalisp.lisp index e265af9..5b40d7c 100644 --- a/ecmalisp.lisp +++ b/ecmalisp.lisp @@ -356,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) @@ -446,7 +469,7 @@ (defun digit-char (weight) (and (<= 0 weight 9) - (char "0123456789" weight))) + (char "0123456789" weight))) (defun subseq (seq a &optional b) (cond @@ -736,39 +759,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 +774,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 @@ -1069,6 +1045,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 +1108,6 @@ ;;; function call. (defvar *multiple-value-p* nil) - (defun make-binding (name type value &optional declarations) (list name type value declarations)) @@ -1125,7 +1155,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,10 +1223,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)) @@ -1250,7 +1280,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,56 +1291,50 @@ (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))) - (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*) - ""))) + (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))) - (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*)) - ""))) + (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 @@ -1319,51 +1343,48 @@ (length (lambda-list-optional-arguments lambda-list))) (keyword-arguments (lambda-list-keyword-arguments-canonical lambda-list))) - (concat + (code "var i;" *newline* ;; Declare variables (mapconcat (lambda (arg) (let ((var (second (car arg)))) - (concat "var " (translate-variable var) "; " *newline*))) + (code "var " (translate-variable var) "; " *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