let-binding-wrapper
authorDavid Vázquez <davazp@gmail.com>
Sun, 30 Jun 2013 14:27:42 +0000 (16:27 +0200)
committerDavid Vázquez <davazp@gmail.com>
Sun, 30 Jun 2013 14:27:42 +0000 (16:27 +0200)
src/compiler.lisp

index 2da9fd2..e6e441b 100644 (file)
 (defun let-binding-wrapper (bindings body)
   (when (null bindings)
     (return-from let-binding-wrapper body))
-  `(code
-    "try {"
-    (code "var tmp;"
-          ,@(mapcar
-             (lambda (b)
-               (let ((s (ls-compile `(quote ,(car b)))))
-                 `(code "tmp = " ,s ".value;"
-                        ,s ".value = " ,(cdr b) ";"
-                        ,(cdr b) " = tmp;" )))
-             bindings)
-          ,body
-          )
-    "}"
-    "finally {"
-    (code
-     ,@(mapcar (lambda (b)
-                 (let ((s (ls-compile `(quote ,(car b)))))
-                   `(code ,s ".value" " = " ,(cdr b) ";" )))
-               bindings))
-    "}" ))
+  `(progn
+     (try (var tmp)
+          ,@(with-collect
+             (dolist (b bindings)
+               (let ((s (ls-compile `',(car b))))
+                 (collect `(= tmp (get ,s "value")))
+                 (collect `(= (get ,s "value") ,(cdr b)))
+                 (collect `(= ,(cdr b) tmp)))))
+          ,body)
+     (finally
+      ,@(with-collect
+         (dolist (b bindings)
+           (let ((s (ls-compile `(quote ,(car b)))))
+             (collect `(= (get ,s "value") ,(cdr b)))))))))
 
 (define-compilation let (bindings &rest body)
   (let* ((bindings (mapcar #'ensure-list bindings))
     `(call (function ,(mapcar (lambda (x)
                                 (if (special-variable-p x)
                                     (let ((v (gvarname x)))
-                                      (push (cons x v) dynamic-bindings)
+                                      (push (cons x (make-symbol v)) dynamic-bindings)
                                       (make-symbol v))
                                     (make-symbol (translate-variable x))))
                               variables)
                      ,(let ((body (ls-compile-block body t t)))
-                           `(code ,(let-binding-wrapper dynamic-bindings body))))
+                           `,(let-binding-wrapper dynamic-bindings body)))
            ,@cvalues)))