Prims - Implement -, *, +, /
[clnl] / src / main / transpile.lisp
1 (in-package #:clnl-transpiler)
2
3 (defparameter *prims* nil)
4
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)))
10
11 (defun find-prim (symb) (find symb *prims* :key #'prim-name))
12
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
17
18 ARGUMENTS AND VALUES:
19
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
22
23 DESCRIPTION:
24
25   TRANSPILE-COMMANDS takes a unambigious PARSED-AST and converts it to
26   Common Lisp code.
27
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."
31  `(progn
32    ,@(mapcar #'transpile-command parsed-ast)))
33
34 (defun transpile-command (command)
35  (cond
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 (apply (prim-func (find-prim (car command))) (mapcar #'transpile-reporter (cdr command))))))
40
41 (defun transpile-reporter (reporter)
42  "TRANSPILE-REPORTER REPORTER => AST
43
44 ARGUMENTS AND VALUES:
45
46   REPORTER: An ast returned from the parser.
47   AST: An common lisp AST that can be actually run in a common lisp instance
48
49 DESCRIPTION:
50
51   TRANSPILE-REPORTER takes a unambigious PARSED-AST and converts it to
52   Common Lisp code.
53
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.
57
58   The Common lisp code that is returned, when run, will return some value."
59  (cond
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 (apply (prim-func (find-prim (car reporter))) (mapcar #'transpile-reporter (cdr reporter))))))
67
68 (defun transpile-command-block (block)
69  `(lambda () ,@(mapcar #'transpile-command (cdr block))))
70
71 (defmacro defprim (name type func)
72  `(push (list :name ,name :type ,type :func ,func) *prims*))
73
74 (defmacro defsimpleprim (name type simple-func)
75  `(defprim ,name ,type (lambda (&rest args) `(,',simple-func ,@args))))
76
77 ; We count on the parser to handle arguemnts for us, when collating things.
78
79 (defsimpleprim := :reporter cl:equalp)
80 (defprim :!= :reporter (lambda (a b) `(not (equalp ,a ,b))))
81 (defsimpleprim :- :reporter cl:-)
82 (defsimpleprim :+ :reporter cl:+)
83 (defsimpleprim :* :reporter cl:*)
84 (defsimpleprim :/ :reporter cl:/)
85 (defsimpleprim :ask :command clnl-nvm:ask)
86 (defsimpleprim :crt :command clnl-nvm:create-turtles)
87 (defsimpleprim :fd :command clnl-nvm:forward)
88 (defsimpleprim :random-float :reporter clnl-nvm:random-float)
89 (defsimpleprim :show :command clnl-nvm:show)
90 (defsimpleprim :turtles :reporter clnl-nvm:turtles)