1 (in-package #:clnl-transpiler)
3 (defparameter *prims* nil)
5 (defun prim-name (prim) (getf prim :name))
6 (defun prim-type (prim) (getf prim :type))
7 (defun prim-func (prim) (getf prim :func))
8 (defun is-reporter (prim) (eql :reporter (getf prim :type)))
9 (defun is-command (prim) (eql :command (getf prim :type)))
11 (defun find-prim (symb) (find symb *prims* :key #'prim-name))
13 ; Let this grow, slowly but surely, eventually taking on calling context, etc.
14 ; For now, it's just a
15 (defun transpile-commands (parsed-ast)
16 "TRANSPILE-COMMANDS PARSED-AST => AST
20 PARSED-AST: An ast as returned by the parser
21 AST: An common lisp AST that can be actually run in a common lisp instance
25 TRANSPILE-COMMANDS takes a unambigious PARSED-AST and converts it to
28 Calling eval on that code should work correctly as long as you have a
29 running engine. This is the entry point for commands, so it does
30 extra checking to ensure that commands are actually in the PARSED-AST."
32 ,@(mapcar #'transpile-command parsed-ast)))
34 (defun transpile-command (command)
36 ((not (listp command)) (error "Expected a statement of some sort"))
37 ((not (find-prim (car command))) (error "Couldn't find the command for ~S" (car command)))
38 ((not (is-command (find-prim (car command)))) (error "Expected command, got ~S" (car command)))
39 (t `(,(prim-func (find-prim (car command))) ,@(mapcar #'transpile-reporter (cdr command))))))
41 (defun transpile-reporter (reporter)
42 "TRANSPILE-REPORTER REPORTER => AST
46 REPORTER: An ast returned from the parser.
47 AST: An common lisp AST that can be actually run in a common lisp instance
51 TRANSPILE-REPORTER takes a unambigious PARSED-AST and converts it to
54 Calling eval on that code should work correctly as long as you have a
55 running engine. This is the entry point for reporters, so it does
56 extra checking to ensure that the reporter is actually in the REPORTER.
58 The Common lisp code that is returned, when run, will return some value."
60 ((numberp reporter) reporter) ; The parser converts to double for us
61 ((symbolp reporter) reporter) ; The parser should have checked that having a symbol here is ok
62 ((not (listp reporter)) (error "Expected a statement of some sort"))
63 ((eql :command-block (car reporter)) (transpile-command-block reporter))
64 ((not (find-prim (car reporter))) (error "Couldn't find the reporter for ~S" (car reporter)))
65 ((not (is-reporter (find-prim (car reporter)))) (error "Expected reporter, got ~S" (car reporter)))
66 (t `(,(prim-func (find-prim (car reporter))) ,@(mapcar #'transpile-reporter (cdr reporter))))))
68 (defun transpile-command-block (block)
69 `(lambda () ,@(mapcar #'transpile-command (cdr block))))
71 (defmacro defprim (name type nvm-func)
73 (list :name ,name :type ,type :func ',nvm-func)
76 ; We count on the parser to handle arguemnts for us, when collating things.
77 (defprim :ask :command clnl-nvm:ask)
78 (defprim :crt :command clnl-nvm:create-turtles)
79 (defprim :fd :command clnl-nvm:forward)
80 (defprim :random-float :reporter clnl-nvm:random-float)
81 (defprim :show :command clnl-nvm:show)
82 (defprim :turtles :reporter clnl-nvm:turtles)