(defun mark-binding-as-declared (b)
(setcar (cdddr b) t))
-(let ((counter 0))
- (defun gvarname (symbol)
- (concat "v" (integer-to-string (incf counter))))
-
- (defun lookup-variable (symbol env)
- (or (assoc symbol env)
- (assoc symbol *env*)
- (let ((name (symbol-name symbol))
- (binding (make-binding symbol 'variable (gvarname symbol) nil)))
- (push binding *env*)
- (push (lambda ()
- (unless (binding-declared (assoc symbol *env*))
- (error (concat "Undefined variable `" name "'"))))
- *compilation-unit-checks*)
- binding)))
-
- (defun lookup-variable-translation (symbol env)
- (binding-translation (lookup-variable symbol env)))
-
- (defun extend-local-env (args env)
- (append (mapcar (lambda (symbol)
- (make-binding symbol 'variable (gvarname symbol) t))
- args)
- env)))
-
-(let ((counter 0))
- (defun lookup-function (symbol env)
- (or (assoc symbol env)
- (assoc symbol *fenv*)
- (let ((name (symbol-name symbol))
- (binding
- (make-binding symbol
- 'function
- (concat "f" (integer-to-string (incf counter)))
- nil)))
- (push binding *fenv*)
- (push (lambda ()
- (unless (binding-declared (assoc symbol *fenv*))
- (error (concat "Undefined function `" name "'"))))
- *compilation-unit-checks*)
- binding)))
-
- (defun lookup-function-translation (symbol env)
- (binding-translation (lookup-function symbol env))))
+(defvar *variable-counter* 0)
+(defun gvarname (symbol)
+ (concat "v" (integer-to-string (incf *variable-counter*))))
+
+(defun lookup-variable (symbol env)
+ (or (assoc symbol env)
+ (assoc symbol *env*)
+ (let ((name (symbol-name symbol))
+ (binding (make-binding symbol 'variable (gvarname symbol) nil)))
+ (push binding *env*)
+ (push (lambda ()
+ (unless (binding-declared (assoc symbol *env*))
+ (error (concat "Undefined variable `" name "'"))))
+ *compilation-unit-checks*)
+ binding)))
+
+(defun lookup-variable-translation (symbol env)
+ (binding-translation (lookup-variable symbol env)))
+
+(defun extend-local-env (args env)
+ (append (mapcar (lambda (symbol)
+ (make-binding symbol 'variable (gvarname symbol) t))
+ args)
+ env))
+
+(defvar *function-counter* 0)
+(defun lookup-function (symbol env)
+ (or (assoc symbol env)
+ (assoc symbol *fenv*)
+ (let ((name (symbol-name symbol))
+ (binding
+ (make-binding symbol
+ 'function
+ (concat "f" (integer-to-string (incf *function-counter*)))
+ nil)))
+ (push binding *fenv*)
+ (push (lambda ()
+ (unless (binding-declared (assoc symbol *fenv*))
+ (error (concat "Undefined function `" name "'"))))
+ *compilation-unit-checks*)
+ binding)))
+
+(defun lookup-function-translation (symbol env)
+ (binding-translation (lookup-function symbol env)))
(defvar *toplevel-compilations* nil)
", cdr: "
(literal->js (cdr sexp)) "}"))))
-(let ((counter 0))
- (defun literal (form)
- (let ((var (concat "l" (integer-to-string (incf counter)))))
- (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
- var)))
+(defvar *literal-counter* 0)
+(defun literal (form)
+ (let ((var (concat "l" (integer-to-string (incf *literal-counter*)))))
+ (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
+ var))
(define-compilation quote (sexp)
(literal sexp))
(lookup-function-translation x fenv))))
#+common-lisp
-c(defmacro eval-when-compile (&body body)
+(defmacro eval-when-compile (&body body)
`(eval-when (:compile-toplevel :load-toplevel :execute)
,@body))
(setq *toplevel-compilations* nil)
(let ((code (ls-compile sexp nil nil)))
(prog1
- (concat "/* " (princ-to-string sexp) " */"
+ (concat #+common-lisp (concat "/* " (princ-to-string sexp) " */")
(join (mapcar (lambda (x) (concat x ";" *newline*))
*toplevel-compilations*)
"")
(defun mark-binding-as-declared (b)
(setcar (cdddr b) t))
-(let ((counter 0))
- (defun gvarname (symbol)
- (concat "v" (integer-to-string (incf counter))))
-
- (defun lookup-variable (symbol env)
- (or (assoc symbol env)
- (assoc symbol *env*)
- (let ((name (symbol-name symbol))
- (binding (make-binding symbol 'variable (gvarname symbol) nil)))
- (push binding *env*)
- (push (lambda ()
- (unless (binding-declared (assoc symbol *env*))
- (error (concat "Undefined variable `" name "'"))))
- *compilation-unit-checks*)
- binding)))
-
- (defun lookup-variable-translation (symbol env)
- (binding-translation (lookup-variable symbol env)))
-
- (defun extend-local-env (args env)
- (append (mapcar (lambda (symbol)
- (make-binding symbol 'variable (gvarname symbol) t))
- args)
- env)))
-
-(let ((counter 0))
- (defun lookup-function (symbol env)
- (or (assoc symbol env)
- (assoc symbol *fenv*)
- (let ((name (symbol-name symbol))
- (binding
- (make-binding symbol
- 'function
- (concat "f" (integer-to-string (incf counter)))
- nil)))
- (push binding *fenv*)
- (push (lambda ()
- (unless (binding-declared (assoc symbol *fenv*))
- (error (concat "Undefined function `" name "'"))))
- *compilation-unit-checks*)
- binding)))
-
- (defun lookup-function-translation (symbol env)
- (binding-translation (lookup-function symbol env))))
+(defvar *variable-counter* 0)
+(defun gvarname (symbol)
+ (concat "v" (integer-to-string (incf *variable-counter*))))
+
+(defun lookup-variable (symbol env)
+ (or (assoc symbol env)
+ (assoc symbol *env*)
+ (let ((name (symbol-name symbol))
+ (binding (make-binding symbol 'variable (gvarname symbol) nil)))
+ (push binding *env*)
+ (push (lambda ()
+ (unless (binding-declared (assoc symbol *env*))
+ (error (concat "Undefined variable `" name "'"))))
+ *compilation-unit-checks*)
+ binding)))
+
+(defun lookup-variable-translation (symbol env)
+ (binding-translation (lookup-variable symbol env)))
+
+(defun extend-local-env (args env)
+ (append (mapcar (lambda (symbol)
+ (make-binding symbol 'variable (gvarname symbol) t))
+ args)
+ env))
+
+(defvar *function-counter* 0)
+(defun lookup-function (symbol env)
+ (or (assoc symbol env)
+ (assoc symbol *fenv*)
+ (let ((name (symbol-name symbol))
+ (binding
+ (make-binding symbol
+ 'function
+ (concat "f" (integer-to-string (incf *function-counter*)))
+ nil)))
+ (push binding *fenv*)
+ (push (lambda ()
+ (unless (binding-declared (assoc symbol *fenv*))
+ (error (concat "Undefined function `" name "'"))))
+ *compilation-unit-checks*)
+ binding)))
+
+(defun lookup-function-translation (symbol env)
+ (binding-translation (lookup-function symbol env)))
(defvar *toplevel-compilations* nil)
", cdr: "
(literal->js (cdr sexp)) "}"))))
-(let ((counter 0))
- (defun literal (form)
- (let ((var (concat "l" (integer-to-string (incf counter)))))
- (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
- var)))
+(defvar *literal-counter* 0)
+(defun literal (form)
+ (let ((var (concat "l" (integer-to-string (incf *literal-counter*)))))
+ (push (concat "var " var " = " (literal->js form)) *toplevel-compilations*)
+ var))
(define-compilation quote (sexp)
(literal sexp))
(lookup-function-translation x fenv))))
#+common-lisp
-(defmacro eval-when-compile (&body body)
+c(defmacro eval-when-compile (&body body)
`(eval-when (:compile-toplevel :load-toplevel :execute)
,@body))
(setq *toplevel-compilations* nil)
(let ((code (ls-compile sexp nil nil)))
(prog1
- (concat
+ (concat #+common-lisp (concat "/* " (princ-to-string sexp) " */")
(join (mapcar (lambda (x) (concat x ";" *newline*))
*toplevel-compilations*)
"")
(defun bootstrap ()
(ls-compile-file "lispstrack.lisp" "lispstrack.js")))
-
;;; ----------------------------------------------------------
(defmacro with-compilation-unit (&rest body)
(setq *fenv* (remove-if-not #'binding-declared *fenv*))
,@body)
(dolist (check *compilation-unit-checks*)
- (funcall check))
- ))
+ (funcall check))))
(defun eval (x)
(let ((code
(ls-compile-toplevel x nil nil))))
(js-eval code)))
+
;; Set the initial global environment to be equal to the host global
;; environment at this point of the compilation.
(eval-when-compile
(let ((c1 (ls-compile `(setq *fenv* ',*fenv*) nil nil))
- (c2 (ls-compile `(setq *env* ',*env*) nil nil)))
+ (c2 (ls-compile `(setq *env* ',*env*) nil nil))
+ (c3 (ls-compile `(setq *variable-counter* ',*variable-counter*) nil nil))
+ (c4 (ls-compile `(setq *function-counter* ',*function-counter*) nil nil))
+ (c5 (ls-compile `(setq *literal-counter* ',*literal-counter*) nil nil)))
(setq *toplevel-compilations*
- (append *toplevel-compilations* (list c1 c2)))))
+ (append *toplevel-compilations* (list c1 c2 c3 c4 c5)))))
(js-eval
(concat "var lisp = {};"