Use comment instead of docstring to make bootstrapping easier
[jscl.git] / lispstrack.lisp
index c3b6642..d531561 100644 (file)
   (concat (join (mapcar (lambda (x)
                           (concat (ls-compile x env) ";"))
                         sexps)
-                "
+                ";
 ")))
 
 (defun ls-compile-block (sexps env)
-  (concat (ls-compile-sexps (butlast sexps env) env)
-          ";
-return " (ls-compile (car (last sexps)) env) ";"))
+  (concat (ls-compile-sexps (butlast sexps) env)
+          "return " (ls-compile (car (last sexps)) env) ";"))
 
 
 (defun extend-env (args env)
@@ -55,8 +54,9 @@ return " (ls-compile (car (last sexps)) env) ";"))
        (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*))
 
@@ -98,6 +98,30 @@ body can access to the local environment through the variable env"
          (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* '())
 
@@ -110,5 +134,16 @@ body can access to the local environment through the variable env"
      (let ((compiler-func (second (assoc (car sexp) *compilations*))))
        (if compiler-func
            (apply compiler-func env (cdr sexp))
+          (funcall (ls-compile (car sexp) env)  )
            ;; funcall
            )))))
+
+
+;;; 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)))))