check symbols in lambdas
authorRaimon Grau <raimonster@gmail.com>
Thu, 13 Dec 2012 00:48:09 +0000 (01:48 +0100)
committerRaimon Grau <raimonster@gmail.com>
Thu, 13 Dec 2012 00:48:09 +0000 (01:48 +0100)
lispstrack.lisp

index 1419eb9..d463be5 100644 (file)
@@ -11,6 +11,8 @@
 (defvar *compilations* nil)
 
 (defmacro define-compilation (name args &body body)
 (defvar *compilations* nil)
 
 (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"
   `(push (list ',name (lambda (env ,@args) ,@body))
          *compilations*))
 
   `(push (list ',name (lambda (env ,@args) ,@body))
          *compilations*))
 
   (concat "(function ("
           (format nil "~{V_~a~^, ~}" args)
           "){ "
   (concat "(function ("
           (format nil "~{V_~a~^, ~}" args)
           "){ "
-          (ls-compile-block body env)
+          (ls-compile-block body (extend-env args env))
           "})
 "))
 
           "})
 "))
 
+(defun extend-env (args env)
+  (append (mapcar #'list args) env))
+
 (defparameter *env* '())
 (defparameter *env-fun* '())
 
 (defparameter *env* '())
 (defparameter *env-fun* '())
 
+
 (defun ls-compile (sexp &optional env)
   (cond
 (defun ls-compile (sexp &optional env)
   (cond
-    ((symbolp sexp) (format nil "V_~a" sexp))
+    ((symbolp sexp) (if (assoc sexp env)
+                       (format nil "V_~a" sexp)
+                       (error "Undefined variable `~a'" sexp)))
     ((integerp sexp) (format nil " ~a " sexp))
     ((stringp sexp) (format nil " \"~a\" " sexp))
     ; list
     ((integerp sexp) (format nil " ~a " sexp))
     ((stringp sexp) (format nil " \"~a\" " sexp))
     ; list