db31840ddd35b7f5c4e940a0dee535a94dfaad41
[clnl] / src / main / transpile.lisp
1 (in-package #:cl-nl.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 (defparameter *prims* nil)
13
14 (defun prim-name (prim) (getf prim :name))
15 (defun prim-type (prim) (getf prim :type))
16 (defun prim-func (prim) (getf prim :func))
17 (defun is-reporter (prim) (eql :reporter (getf prim :type)))
18 (defun is-command (prim) (eql :command (getf prim :type)))
19
20 (defun find-prim (symb) (find symb *prims* :key #'prim-name))
21
22 ; Let this grow, slowly but surely, eventually taking on calling context, etc.
23 ; For now, it's just a 
24 (defun transpile-commands (parsed-ast)
25  `(progn
26    ,@(mapcar #'transpile-command parsed-ast)))
27
28 (defun transpile-command (command)
29  (cond
30   ((not (listp command)) (error "Expected a statement of some sort"))
31   ((not (find-prim (car command))) (error "Couldn't find the command for ~S" (car command)))
32   ((not (is-command (find-prim (car command)))) (error "Expected command, got ~S" (car command)))
33   (t `(,(prim-func (find-prim (car command))) ,@(mapcar #'transpile-reporter (cdr command))))))
34
35 (defun transpile-reporter (reporter)
36  (cond
37   ((numberp reporter) reporter) ; The parser converts to double for us
38   ((symbolp reporter) reporter) ; The parser should have checked that having a symbol here is ok
39   ((not (listp reporter)) (error "Expected a statement of some sort"))
40   ((eql :command-block (car reporter)) (transpile-command-block reporter))
41   ((not (find-prim (car reporter))) (error "Couldn't find the reporter for ~S" (car reporter)))
42   ((not (is-reporter (find-prim (car reporter)))) (error "Expected reporter, got ~S" (car reporter)))
43   (t `(,(prim-func (find-prim (car reporter))) ,@(mapcar #'transpile-reporter (cdr reporter))))))
44
45 (defun transpile-command-block (block)
46  `(lambda () ,@(mapcar #'transpile-command (cdr block))))
47
48 (defmacro defprim (name type nvm-func)
49  `(push
50    (list :name ,name :type ,type :func ',nvm-func)
51    *prims*))
52
53 ; We count on the parser to handle arguemnts for us, when collating things.
54 (defprim :ask :command cl-nl.nvm::ask)
55 (defprim :crt :command cl-nl.nvm::create-turtles)
56 (defprim :fd :command cl-nl.nvm::fd)
57 (defprim :random-float :reporter cl-nl.nvm::random-float)
58 (defprim :show :command cl-nl.nvm::show)
59 (defprim :turtles :reporter cl-nl.nvm::turtles)