refactored and symbol lookup
authorRaimon Grau <raimonster@gmail.com>
Thu, 13 Dec 2012 01:29:44 +0000 (02:29 +0100)
committerRaimon Grau <raimonster@gmail.com>
Thu, 13 Dec 2012 01:29:44 +0000 (02:29 +0100)
lispstrack.lisp

index d463be5..99a7473 100644 (file)
@@ -6,10 +6,24 @@
           strs
           :initial-value ""))
 
           strs
           :initial-value ""))
 
+
+(let ((counter 0))
+  (defun make-binding (symbol)
+    (cons symbol (format nil "V_~d" (incf counter)))))
+
 ;;; Compiler
 
 (defvar *compilations* nil)
 
 ;;; Compiler
 
 (defvar *compilations* nil)
 
+(defun extend-env (args env)
+  (append (mapcar #'make-binding args) env))
+
+(defun ls-lookup (symbol env)
+  (let ((binding (assoc symbol env)))
+    (if binding
+       (format nil "~a" (cdr binding))
+       (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"
 (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"
@@ -23,28 +37,28 @@ body can access to the local environment through the variable env"
           (ls-compile false env)))
 
 (define-compilation lambda (args &rest body)
           (ls-compile false env)))
 
 (define-compilation lambda (args &rest body)
-  (concat "(function ("
-          (format nil "~{V_~a~^, ~}" args)
-          "){ "
-          (ls-compile-block body (extend-env args env))
-          "})
-"))
+  (let ((new-env (extend-env args env)))
+    (concat "(function ("
+           (format nil "~{~a~^, ~}" (mapcar
+                                     (lambda (x) (ls-lookup x new-env))
+                                     args))
+           "){ "
+           (ls-compile-block body new-env)
+           "})
+")))
 
 
-(defun extend-env (args env)
-  (append (mapcar #'list args) env))
+(define-compilation setq (var val)
+  (format nil "~a = ~a" (ls-lookup var env) (ls-compile val 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) (if (assoc sexp env)
-                       (format nil "V_~a" sexp)
-                       (error "Undefined variable `~a'" sexp)))
-    ((integerp sexp) (format nil " ~a " sexp))
+    ((symbolp sexp) (ls-lookup sexp env))
+    ((integerp sexp) (format nil "~a" sexp))
     ((stringp sexp) (format nil " \"~a\" " sexp))
     ((stringp sexp) (format nil " \"~a\" " sexp))
-    ; list
+                                       ; list
     ((listp sexp)
      (let ((compiler-func (second (assoc (car sexp) *compilations*))))
        (if compiler-func
     ((listp sexp)
      (let ((compiler-func (second (assoc (car sexp) *compilations*))))
        (if compiler-func