Prims - Implement not
[clnl] / src / main / transpile.lisp
1 (in-package #:clnl-transpiler)
2
3 (defparameter *prims* nil)
4 (defparameter *prim-aliases* nil)
5
6 (defvar *local-variables* nil)
7
8 (defun prim-name (prim) (getf prim :name))
9 (defun prim-type (prim) (getf prim :type))
10 (defun prim-func (prim) (getf prim :func))
11 (defun is-reporter (prim) (eql :reporter (getf prim :type)))
12 (defun is-command (prim) (eql :command (getf prim :type)))
13
14 (defun find-prim (symb)
15  (when symb
16   (or
17    (find symb *prims* :key #'prim-name)
18    (find-prim (getf (find symb *prim-aliases* :key #'prim-name) :real-symb)))))
19
20 ; Let this grow, slowly but surely, eventually taking on calling context, etc.
21 ; For now, it's just a
22 (defun transpile-commands (parsed-ast)
23  "TRANSPILE-COMMANDS PARSED-AST => AST
24
25 ARGUMENTS AND VALUES:
26
27   PARSED-AST: An ast as returned by the parser
28   AST: An common lisp AST that can be actually run in a common lisp instance
29
30 DESCRIPTION:
31
32   TRANSPILE-COMMANDS takes a unambigious PARSED-AST and converts it to
33   Common Lisp code.
34
35   Calling eval on that code should work correctly as long as you have a
36   running engine.  This is the entry point for commands, so it does
37   extra checking to ensure that commands are actually in the PARSED-AST."
38  `(progn
39    ,@(transpile-commands-inner parsed-ast)))
40
41 (defun transpile-commands-inner (parsed-ast)
42  (cond
43   ((not parsed-ast) nil)
44   ((and (listp (car parsed-ast)) (eql :let (caar parsed-ast))) (list (handle-let parsed-ast)))
45   (t
46    (cons
47     (transpile-command (car parsed-ast))
48     (transpile-commands-inner (cdr parsed-ast))))))
49
50 (defun handle-let (parsed-ast &optional vars)
51  (if
52   (and (listp (car parsed-ast)) (eql :let (caar parsed-ast)))
53   (let
54    ((*local-variables* (cons (second (car parsed-ast)) *local-variables*)))
55    (handle-let
56     (cdr parsed-ast)
57     (cons
58      (list
59       (transpile-reporter (second (car parsed-ast)))
60       (transpile-reporter (third (car parsed-ast))))
61      vars)))
62   `(let*
63     ,vars
64     ,@(transpile-commands-inner parsed-ast))))
65
66 (defun transpile-command (command)
67  (cond
68   ((not (listp command)) (error "Expected a statement of some sort"))
69   ((not (find-prim (car command))) (error "Couldn't find the command for ~S" (car command)))
70   ((not (is-command (find-prim (car command)))) (error "Expected command, got ~S" (car command)))
71   (t (apply (prim-func (find-prim (car command))) (mapcar #'transpile-reporter (cdr command))))))
72
73 (defun transpile-reporter (reporter)
74  "TRANSPILE-REPORTER REPORTER => AST
75
76 ARGUMENTS AND VALUES:
77
78   REPORTER: An ast returned from the parser.
79   AST: An common lisp AST that can be actually run in a common lisp instance
80
81 DESCRIPTION:
82
83   TRANSPILE-REPORTER takes a unambigious PARSED-AST and converts it to
84   Common Lisp code.
85
86   Calling eval on that code should work correctly as long as you have a
87   running engine.  This is the entry point for reporters, so it does
88   extra checking to ensure that the reporter is actually in the REPORTER.
89
90   The Common lisp code that is returned, when run, will return some value."
91  (cond
92   ((numberp reporter) reporter) ; The parser converts to double for us
93   ; The parser should have checked that having a symbol here is ok
94   ((symbolp reporter) (intern (symbol-name reporter) clnl:*model-package*))
95   ((not (listp reporter)) (error "Expected a statement of some sort"))
96   ((eql :command-block (car reporter)) (transpile-command-block reporter))
97   ((eql :list-literal (car reporter)) (cons 'list (mapcar #'transpile-reporter (cdr reporter))))
98   ((eql :reporter-block (car reporter)) (transpile-reporter-block reporter))
99   ((and (symbolp (car reporter)) (find (car reporter) *local-variables*))
100    (intern (symbol-name (car reporter)) clnl:*model-package*))
101   ((not (find-prim (car reporter))) (error "Couldn't find the reporter for ~S" (car reporter)))
102   ((not (is-reporter (find-prim (car reporter)))) (error "Expected reporter, got ~S" (car reporter)))
103   (t (apply (prim-func (find-prim (car reporter))) (mapcar #'transpile-reporter (cdr reporter))))))
104
105 (defun transpile-command-block (block)
106  `(lambda () ,@(transpile-commands-inner (cdr block))))
107
108 (defun transpile-reporter-block (block)
109  (when (/= (length block) 2) (error "Reporter block invalid ~S" block))
110  `(lambda ()
111    ,(transpile-reporter (cadr block))))
112
113 ; Undoes the previous function :)
114 (defun make-command-block-inline (block)
115  (cddr block))
116
117 (defmacro defprim (name type func)
118  `(push (list :name ,name :type ,type :func ,func) *prims*))
119
120 (defmacro defsimpleprim (name type simple-func)
121  `(defprim ,name ,type (lambda (&rest args) `(,',simple-func ,@args))))
122
123 (defmacro defkeywordprim (name)
124  `(defprim ,name :reporter (lambda () ',name)))
125
126 (defmacro defprim-alias (name real-symb)
127  `(push (list :name ,name :real-symb ,real-symb) *prim-aliases*))
128
129 (defmacro defagentvalueprim (name)
130  `(defprim ,name :reporter (lambda () `(clnl-nvm:agent-value ,,name))))
131
132 ; We count on the parser to handle arguemnts for us, when collating things.
133
134 (defsimpleprim := :reporter cl:equalp)
135 (defprim :!= :reporter (lambda (a b) `(not (equalp ,a ,b))))
136 (defsimpleprim :<= :reporter cl:<=)
137 (defsimpleprim :< :reporter cl:<)
138 (defsimpleprim :- :reporter cl:-)
139 (defsimpleprim :+ :reporter cl:+)
140 (defsimpleprim :* :reporter cl:*)
141 (defsimpleprim :/ :reporter cl:/)
142 (defprim :any? :reporter (lambda (agentset) `(> (clnl-nvm:count ,agentset) 0)))
143 (defsimpleprim :ask :command clnl-nvm:ask)
144 (defagentvalueprim :color)
145 (defsimpleprim :count :reporter clnl-nvm:count)
146 (defsimpleprim :crt :command clnl-nvm:create-turtles)
147 (defsimpleprim :die :command clnl-nvm:die)
148 (defsimpleprim :fd :command clnl-nvm:forward)
149 (defprim :if :command (lambda (pred a) `(when ,pred ,@(make-command-block-inline a))))
150 (defprim :ifelse :command (lambda (pred a b)
151                            `(if ,pred
152                              ,@(make-command-block-inline a)
153                              ,@(make-command-block-inline b))))
154
155 (defprim-alias :if-else :ifelse)
156 (defagentvalueprim :label)
157 (defagentvalueprim :label-color)
158 (defsimpleprim :lt :command clnl-nvm:turn-left)
159 (defsimpleprim :not :reporter cl:not)
160 (defkeywordprim :nobody)
161 (defsimpleprim :one-of :reporter clnl-nvm:one-of)
162 (defsimpleprim :of :reporter clnl-nvm:of)
163 (defsimpleprim :patches :reporter clnl-nvm:patches)
164 (defagentvalueprim :pcolor)
165 (defsimpleprim :reset-ticks :command clnl-nvm:reset-ticks)
166 (defsimpleprim :random :reporter clnl-nvm:random)
167 (defsimpleprim :random-float :reporter clnl-nvm:random-float)
168 (defsimpleprim :random-xcor :reporter clnl-nvm:random-xcor)
169 (defsimpleprim :random-ycor :reporter clnl-nvm:random-ycor)
170 (defsimpleprim :rt :command clnl-nvm:turn-right)
171 (defsimpleprim :show :command clnl-nvm:show)
172 (defsimpleprim :set :command cl:setf)
173 (defsimpleprim :setxy :command clnl-nvm:setxy)
174 (defagentvalueprim :size)
175 (defsimpleprim :tick :command clnl-nvm:tick)
176 (defsimpleprim :ticks :reporter clnl-nvm:ticks)
177 (defsimpleprim :turtles :reporter clnl-nvm:turtles)
178 (defagentvalueprim :who)
179 (defsimpleprim :with :reporter clnl-nvm:with)
180
181 ; Colors
182 (defmacro defcolorprim (color) `(defprim ,color :reporter (lambda () `(clnl-nvm:lookup-color ,,color))))
183 (defcolorprim :black)
184 (defcolorprim :blue)
185 (defcolorprim :brown)
186 (defcolorprim :green)
187 (defcolorprim :white)