while
[jscl.git] / lispstrack.lisp
1 ;;; Utils
2
3 (defmacro while (condition &body body)
4   `(do ()
5        ((not ,condition))
6      ,@body))
7
8 ;;; simplify me, please
9 (defun concat (&rest strs)
10   (reduce (lambda (s1 s2) (concatenate 'string s1 s2))
11           strs
12           :initial-value ""))
13
14
15 (let ((counter 0))
16   (defun make-binding (symbol)
17     (cons symbol (format nil "V_~d" (incf counter)))))
18
19 ;;; Concatenate a list of strings, with a separator
20 (defun join (list separator)
21   (cond
22     ((null list)
23      "")
24     ((null (cdr list))
25      (car list))
26     (t
27      (concat (car list)
28              separator
29              (join (cdr list) separator)))))
30
31 ;;; Compiler
32
33 (defvar *compilations* nil)
34
35 (defun ls-compile-sexps (sexps env)
36   (concat (join (mapcar (lambda (x)
37                           (ls-compile x env))
38                         sexps)
39                 ";
40 ")))
41
42 (defun ls-compile-block (sexps env)
43   (concat (ls-compile-sexps (butlast sexps env) env)
44           ";
45 return " (ls-compile (car (last sexps)) env) ";"))
46
47
48 (defun extend-env (args env)
49   (append (mapcar #'make-binding args) env))
50
51 (defun ls-lookup (symbol env)
52   (let ((binding (assoc symbol env)))
53     (if binding
54         (format nil "~a" (cdr binding))
55         (error "Undefined variable `~a'"  symbol))))
56
57 (defmacro define-compilation (name args &body body)
58   "creates a new primitive `name' with parameters args and @body. The
59 body can access to the local environment through the variable env"
60   `(push (list ',name (lambda (env ,@args) ,@body))
61          *compilations*))
62
63 (define-compilation if (condition true false)
64   (format nil "((~a)? (~a) : (~a))"
65           (ls-compile condition env)
66           (ls-compile true env)
67           (ls-compile false env)))
68
69 (define-compilation lambda (args &rest body)
70   (let ((new-env (extend-env args env)))
71     (concat "(function ("
72             (join (mapcar (lambda (x) (ls-lookup x new-env))
73                           args)
74                   ",")
75             "){
76 "
77             (ls-compile-block body new-env)
78             "
79 })")))
80
81 (define-compilation setq (var val)
82   (format nil "~a = ~a" (ls-lookup var env) (ls-compile val env)))
83
84 (defun lisp->js (sexp)
85   (cond
86     ((integerp sexp) (format nil "~a" sexp))
87     ((stringp sexp) (format nil "\"~a\"" sexp))
88     ((listp sexp)   (concat "[" (join (mapcar 'lisp->js sexp) ",") "]"))))
89
90 (define-compilation quote (sexp)
91   (lisp->js sexp))
92
93 (define-compilation while (pred &rest body)
94   (format nil "(function(){while(~a){~a}})() "
95           (ls-compile pred env)
96           (ls-compile-sexps body env)))
97
98 (defparameter *env* '())
99 (defparameter *env-fun* '())
100
101 (defun ls-compile (sexp &optional env)
102   (cond
103     ((symbolp sexp) (ls-lookup sexp env))
104     ((integerp sexp) (format nil "~a" sexp))
105     ((stringp sexp) (format nil " \"~a\" " sexp))
106                                         ; list
107     ((listp sexp)
108      (let ((compiler-func (second (assoc (car sexp) *compilations*))))
109        (if compiler-func
110            (apply compiler-func env (cdr sexp))
111            ;; funcall
112            )))))