0057c2eb80ce6f6c042e9111f88319bf506afd58
[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 extend-env (args env)
36   (append (mapcar #'make-binding args) env))
37
38 (defun ls-lookup (symbol env)
39   (let ((binding (assoc symbol env)))
40     (if binding
41         (format nil "~a" (cdr binding))
42         (error "Undefined variable `~a'"  symbol))))
43
44 (defmacro define-compilation (name args &body body)
45   "creates a new primitive `name' with parameters args and @body. The
46 body can access to the local environment through the variable env"
47   `(push (list ',name (lambda (env ,@args) ,@body))
48          *compilations*))
49
50 (define-compilation if (condition true false)
51   (format nil "((~a)? (~a) : (~a))"
52           (ls-compile condition env)
53           (ls-compile true env)
54           (ls-compile false env)))
55
56 (define-compilation lambda (args &rest body)
57   (let ((new-env (extend-env args env)))
58     (concat "(function ("
59             (join (mapcar (lambda (x) (ls-lookup x new-env))
60                           args)
61                   ",")
62             "){
63 "
64             (ls-compile-block body new-env)
65             "
66 })")))
67
68 (define-compilation setq (var val)
69   (format nil "~a = ~a" (ls-lookup var env) (ls-compile val env)))
70
71 (defun lisp->js (sexp)
72   (cond
73     ((integerp sexp) (format nil "~a" sexp))
74     ((stringp sexp) (format nil "\"~a\"" sexp))
75     ((listp sexp)   (concat "[" (join (mapcar 'lisp->js sexp) ",") "]"))))
76
77 (define-compilation quote (sexp)
78   (lisp->js sexp))
79
80 (define-compilation while (pred &rest body)
81   (format nil "(function(){while(~a){~{~a~}}})()"
82           (ls-compile pred env)
83           (mapcar (lambda (x) (ls-compile x env)) body)))
84
85 (defparameter *env* '())
86 (defparameter *env-fun* '())
87
88 (defun ls-compile (sexp &optional env)
89   (cond
90     ((symbolp sexp) (ls-lookup sexp env))
91     ((integerp sexp) (format nil "~a" sexp))
92     ((stringp sexp) (format nil " \"~a\" " sexp))
93                                         ; list
94     ((listp sexp)
95      (let ((compiler-func (second (assoc (car sexp) *compilations*))))
96        (if compiler-func
97            (apply compiler-func env (cdr sexp))
98            ;; funcall
99            )))))
100
101 (defun ls-compile-sexps (sexps env)
102   (concat (join (mapcar (lambda (x)
103                           (ls-compile x env))
104                         sexps)
105                 ";
106 ")))
107
108 (defun ls-compile-block (sexps env)
109   (concat (ls-compile-sexps (butlast sexps env) env)
110           ";
111 return " (ls-compile (car (last sexps)) env) ";"))