1 (in-package #:clnl-transpiler)
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.
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
16 (defparameter *prims* nil)
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)))
24 (defun find-prim (symb) (find symb *prims* :key #'prim-name))
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)
30 ,@(mapcar #'transpile-command parsed-ast)))
32 (defun transpile-command (command)
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))))))
39 (defun transpile-reporter (reporter)
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))))))
49 (defun transpile-command-block (block)
50 `(lambda () ,@(mapcar #'transpile-command (cdr block))))
52 (defmacro defprim (name type nvm-func)
54 (list :name ,name :type ,type :func ',nvm-func)
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)