Rename to clnl, add documentation
[clnl] / src / main / transpile.lisp
1 (in-package #:clnl-transpiler)
2
3 ; This is responsible for taking an ast and turning it into valid CL code
4 ; targeting the nvm.  Here is where start to care about commands versus reporters
5 ; and ensuring that things are in the right place.  The reason we wait until here
6 ; is because we want to allow someone else to play with the AST before handing it off
7 ; to us.  For instance, the command center wants to add "show" to reporters, and
8 ; the users dictate based on entry point whether they are expecting a command
9 ; or a reporter.  So monitors can say "hey, transpile this reporter" and we'll check
10 ; to make sure it actually is.
11
12 ; Furthermore, the lisp code that any netlogo code would be transpiled to should
13 ; use exported symbols, such that anyone writing NetLogo code in lisp could use
14 ; the nvm in the same way that comes out of this transpiler
15
16 (defparameter *prims* nil)
17
18 (defun prim-name (prim) (getf prim :name))
19 (defun prim-type (prim) (getf prim :type))
20 (defun prim-func (prim) (getf prim :func))
21 (defun is-reporter (prim) (eql :reporter (getf prim :type)))
22 (defun is-command (prim) (eql :command (getf prim :type)))
23
24 (defun find-prim (symb) (find symb *prims* :key #'prim-name))
25
26 ; Let this grow, slowly but surely, eventually taking on calling context, etc.
27 ; For now, it's just a 
28 (defun transpile-commands (parsed-ast)
29  `(progn
30    ,@(mapcar #'transpile-command parsed-ast)))
31
32 (defun transpile-command (command)
33  (cond
34   ((not (listp command)) (error "Expected a statement of some sort"))
35   ((not (find-prim (car command))) (error "Couldn't find the command for ~S" (car command)))
36   ((not (is-command (find-prim (car command)))) (error "Expected command, got ~S" (car command)))
37   (t `(,(prim-func (find-prim (car command))) ,@(mapcar #'transpile-reporter (cdr command))))))
38
39 (defun transpile-reporter (reporter)
40  (cond
41   ((numberp reporter) reporter) ; The parser converts to double for us
42   ((symbolp reporter) reporter) ; The parser should have checked that having a symbol here is ok
43   ((not (listp reporter)) (error "Expected a statement of some sort"))
44   ((eql :command-block (car reporter)) (transpile-command-block reporter))
45   ((not (find-prim (car reporter))) (error "Couldn't find the reporter for ~S" (car reporter)))
46   ((not (is-reporter (find-prim (car reporter)))) (error "Expected reporter, got ~S" (car reporter)))
47   (t `(,(prim-func (find-prim (car reporter))) ,@(mapcar #'transpile-reporter (cdr reporter))))))
48
49 (defun transpile-command-block (block)
50  `(lambda () ,@(mapcar #'transpile-command (cdr block))))
51
52 (defmacro defprim (name type nvm-func)
53  `(push
54    (list :name ,name :type ,type :func ',nvm-func)
55    *prims*))
56
57 ; We count on the parser to handle arguemnts for us, when collating things.
58 (defprim :ask :command clnl-nvm:ask)
59 (defprim :crt :command clnl-nvm:create-turtles)
60 (defprim :fd :command clnl-nvm:forward)
61 (defprim :random-float :reporter clnl-nvm:random-float)
62 (defprim :show :command clnl-nvm:show)
63 (defprim :turtles :reporter clnl-nvm:turtles)