X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=lispstrack.lisp;h=d531561a08b090e54d0f725cdaceeec62b4fbc87;hb=2bcc11276620557ab635a9698bd9b230a60e2e61;hp=99a7473ff8c210e0c8bd67bc6b22c80c9d0906c6;hpb=3bb4bb39dd162eb1b0bbf4a359402b1b49fb4c2f;p=jscl.git diff --git a/lispstrack.lisp b/lispstrack.lisp index 99a7473..d531561 100644 --- a/lispstrack.lisp +++ b/lispstrack.lisp @@ -1,5 +1,10 @@ ;;; Utils +(defmacro while (condition &body body) + `(do () + ((not ,condition)) + ,@body)) + ;;; simplify me, please (defun concat (&rest strs) (reduce (lambda (s1 s2) (concatenate 'string s1 s2)) @@ -11,10 +16,34 @@ (defun make-binding (symbol) (cons symbol (format nil "V_~d" (incf counter))))) +;;; Concatenate a list of strings, with a separator +(defun join (list separator) + (cond + ((null list) + "") + ((null (cdr list)) + (car list)) + (t + (concat (car list) + separator + (join (cdr list) separator))))) + ;;; Compiler (defvar *compilations* nil) +(defun ls-compile-sexps (sexps env) + (concat (join (mapcar (lambda (x) + (concat (ls-compile x env) ";")) + sexps) + "; +"))) + +(defun ls-compile-block (sexps env) + (concat (ls-compile-sexps (butlast sexps) env) + "return " (ls-compile (car (last sexps)) env) ";")) + + (defun extend-env (args env) (append (mapcar #'make-binding args) env)) @@ -25,8 +54,9 @@ (error "Undefined variable `~a'" symbol)))) (defmacro define-compilation (name args &body body) - "creates a new primitive `name' with parameters args and @body. The -body can access to the local environment through the variable env" + ;; Creates a new primitive `name' with parameters args and + ;; @body. The body can access to the local environment through the + ;; variable ENV. `(push (list ',name (lambda (env ,@args) ,@body)) *compilations*)) @@ -39,17 +69,59 @@ body can access to the local environment through the variable env" (define-compilation lambda (args &rest body) (let ((new-env (extend-env args env))) (concat "(function (" - (format nil "~{~a~^, ~}" (mapcar - (lambda (x) (ls-lookup x new-env)) - args)) - "){ " + (join (mapcar (lambda (x) (ls-lookup x new-env)) + args) + ",") + "){ +" (ls-compile-block body new-env) - "}) -"))) + " +})"))) (define-compilation setq (var val) (format nil "~a = ~a" (ls-lookup var env) (ls-compile val env))) +(defun lisp->js (sexp) + (cond + ((integerp sexp) (format nil "~a" sexp)) + ((stringp sexp) (format nil "\"~a\"" sexp)) + ((listp sexp) (concat "[" (join (mapcar 'lisp->js sexp) ",") "]")))) + +(define-compilation quote (sexp) + (lisp->js sexp)) + +(define-compilation debug (form) + (format nil "console.log(~a)" (ls-compile form env))) + +(define-compilation while (pred &rest body) + (format nil "(function(){while(~a){~a}})() " + (ls-compile pred env) + (ls-compile-sexps body env))) + +(defmacro eval-when-compile (&body body) + `(eval-when (:compile-toplevel :execute) + ,@body)) + +(define-compilation eval-when-compile (when &rest body) + (eval body)) + +;;; aritmetic primitives +(define-compilation + (x y) + (concat "((" (ls-compile x env) ") + (" (ls-compile y env) "))")) + +(define-compilation - (x y) + (concat "((" (ls-compile x env) ") - (" (ls-compile y env) "))")) + +(define-compilation * (x y) + (concat "((" (ls-compile x env) ") * (" (ls-compile y env) "))")) + +(define-compilation / (x y) + (concat "((" (ls-compile x env) ") / (" (ls-compile y env) "))")) + +(define-compilation = (x y) + (concat "((" (ls-compile x env) ") == (" (ls-compile y env) "))")) + + (defparameter *env* '()) (defparameter *env-fun* '()) @@ -58,17 +130,20 @@ body can access to the local environment through the variable env" ((symbolp sexp) (ls-lookup sexp env)) ((integerp sexp) (format nil "~a" sexp)) ((stringp sexp) (format nil " \"~a\" " sexp)) - ; list ((listp sexp) (let ((compiler-func (second (assoc (car sexp) *compilations*)))) (if compiler-func (apply compiler-func env (cdr sexp)) + (funcall (ls-compile (car sexp) env) ) ;; funcall ))))) -(defun ls-compile-block (sexps env) - (format nil - "~{~#[~; return ~a;~:;~a;~%~]~}" - (mapcar #'(lambda (x) - (ls-compile x env)) - sexps))) + +;;; Testing + +(defun compile-test () + (with-open-file (in "test.lisp") + (with-open-file (out "test.js" :direction :output :if-exists :supersede) + (loop + for x = (read in nil) while x + do (write-string (concat (ls-compile x) "; ") out)))))