Add export to common lisp form, forms
[clnl] / src / main / transpile.lisp
index b0fcb47790654cbc05e6868b20c732b1533caab1..327bdbd6d96c43e86a12394a5631cf994fbf67a8 100644 (file)
 (in-package #:clnl-transpiler)
 
 (defparameter *prims* nil)
-(defparameter *prim-aliases* nil)
 
 (defvar *local-variables* nil)
+(defvar *dynamic-prims* nil)
 
 (defun prim-name (prim) (getf prim :name))
 (defun prim-type (prim) (getf prim :type))
 (defun prim-func (prim) (getf prim :func))
-(defun is-reporter (prim) (eql :reporter (getf prim :type)))
-(defun is-command (prim) (eql :command (getf prim :type)))
+(defun prim-reporter-p (prim) (eql :reporter (getf prim :type)))
+(defun prim-command-p (prim) (eql :command (getf prim :type)))
 
 (defun find-prim (symb)
  (when symb
   (find-if
    (lambda (prim-name) (or (eql symb prim-name) (and (listp prim-name) (find symb prim-name))))
-   *prims* :key #'prim-name)))
+   (append *prims* *dynamic-prims*)
+   :key #'prim-name)))
 
-; Let this grow, slowly but surely, eventually taking on calling context, etc.
-; For now, it's just a
-(defun transpile-commands (parsed-ast)
- "TRANSPILE-COMMANDS PARSED-AST => AST
+(defun transpile (parsed-ast &optional dynamic-prims)
+ "TRANSPILE PARSED-AST &optional DYNAMIC-PRIMS => AST
+
+  DYNAMIC-PRIMS: DYNAMIC-PRIM*
+  DYNAMIC-PRIM: (:name NAME :type TYPE :macro MACRO :func FUNC)
+  TYPE: :reporter | :command
 
 ARGUMENTS AND VALUES:
 
   PARSED-AST: An ast as returned by the parser
   AST: An common lisp AST that can be actually run in a common lisp instance
+  NAME: A symbol in the keyword package
+  MACRO: A macro that will be called with the arguments ast
+  FUNC: A function that will be called with the transpiled arguments
 
 DESCRIPTION:
 
-  TRANSPILE-COMMANDS takes a unambigious PARSED-AST and converts it to
-  Common Lisp code.
+  TRANSPILE takes a unambigious PARSED-AST and converts it to
+  Common Lisp code.  The PARSED-AST must be either a list of commands,
+  or a single reporter.
+
+  When a set of DYNAMIC-PRIMS is included, external language constructs
+  can be also transpiled.  The provided functions will be inserted into
+  the returned AST with a call to FUNCALL.  If :macro is included, instead
+  of having a call to FUNCALL provided, the macro will be run at netlogo
+  transpile time, with the arguments it should have specified to the
+  parser.  The result of that function call will then be dropped into
+  the ast.
 
   Calling eval on that code should work correctly as long as you have a
-  running engine.  This is the entry point for commands, so it does
-  extra checking to ensure that commands are actually in the PARSED-AST."
+  running engine."
+
+ (let
+  ((*dynamic-prims*
+    (mapcar
+     (lambda (prim)
+      (if (getf prim :macro)
+       (append (list :func (getf prim :macro)) prim)
+       (append (list :func (lambda (&rest args) `(funcall ,(getf prim :func) ,@args))) prim)))
+     dynamic-prims)))
+  (cond
+   ((command-list-p parsed-ast) (transpile-commands parsed-ast))
+   ((and (listp parsed-ast) (= 1 (length parsed-ast)) (reporter-p (car parsed-ast)))
+    (transpile-reporter (car parsed-ast)))
+   (t (error "Is neither a list of commands nor a reporter: ~S" parsed-ast)))))
+
+(defun command-list-p (parsed-ast)
+ "COMMAND-LIST-P PARSED-AST => RESULT
+
+ARGUMENTS AND VALUES:
+
+  PARSED-AST: An ast as returned by the parser
+  RESULT: A boolean
+
+DESCRIPTION:
+
+  COMMAND-LIST-P returns whether the parsed-ast is a valid list
+  of commands."
+ (and
+  (every #'listp parsed-ast)
+  (every #'prim-command-p (mapcar #'find-prim (mapcar #'car parsed-ast)))))
+
+(defun reporter-p (parsed-ast)
+ "REPORTER-P PARSED-AST => RESULT
+
+ARGUMENTS AND VALUES:
+
+  PARSED-AST: An ast as returned by the parser
+  RESULT: A boolean
+
+DESCRIPTION:
+
+  REPORTER-P returns whether the parsed-ast is a valid reporter."
+ (and
+  (symbolp (car parsed-ast))
+  (prim-reporter-p (find-prim (car parsed-ast)))))
+
+; Let this grow, slowly but surely, eventually taking on calling context, etc.
+; For now, it's just a
+(defun transpile-commands (parsed-ast)
  `(progn
    ,@(transpile-commands-inner parsed-ast)))
 
@@ -67,27 +130,10 @@ DESCRIPTION:
  (cond
   ((not (listp command)) (error "Expected a statement of some sort"))
   ((not (find-prim (car command))) (error "Couldn't find the command for ~S" (car command)))
-  ((not (is-command (find-prim (car command)))) (error "Expected command, got ~S" (car command)))
+  ((not (prim-command-p (find-prim (car command)))) (error "Expected command, got ~S" (car command)))
   (t (apply (prim-func (find-prim (car command))) (mapcar #'transpile-reporter (cdr command))))))
 
 (defun transpile-reporter (reporter)
- "TRANSPILE-REPORTER REPORTER => AST
-
-ARGUMENTS AND VALUES:
-
-  REPORTER: An ast returned from the parser.
-  AST: An common lisp AST that can be actually run in a common lisp instance
-
-DESCRIPTION:
-
-  TRANSPILE-REPORTER takes a unambigious PARSED-AST and converts it to
-  Common Lisp code.
-
-  Calling eval on that code should work correctly as long as you have a
-  running engine.  This is the entry point for reporters, so it does
-  extra checking to ensure that the reporter is actually in the REPORTER.
-
-  The Common lisp code that is returned, when run, will return some value."
  (cond
   ((numberp reporter) reporter) ; The parser converts to double for us
   ((stringp reporter) reporter)
@@ -100,7 +146,7 @@ DESCRIPTION:
   ((and (symbolp (car reporter)) (find (car reporter) *local-variables*))
    (intern (symbol-name (car reporter)) clnl:*model-package*))
   ((not (find-prim (car reporter))) (error "Couldn't find the reporter for ~S" (car reporter)))
-  ((not (is-reporter (find-prim (car reporter)))) (error "Expected reporter, got ~S" (car reporter)))
+  ((not (prim-reporter-p (find-prim (car reporter)))) (error "Expected reporter, got ~S" (car reporter)))
   (t (apply (prim-func (find-prim (car reporter))) (mapcar #'transpile-reporter (cdr reporter))))))
 
 (defun transpile-command-block (block)
@@ -124,9 +170,6 @@ DESCRIPTION:
 (defmacro defkeywordprim (name)
  `(defprim ,name :reporter (lambda () ',name)))
 
-(defmacro defprim-alias (name real-symb)
- `(push (list :name ,name :real-symb ,real-symb) *prim-aliases*))
-
 (defmacro defagentvalueprim (name)
  `(defprim ,name :reporter (lambda () `(clnl-nvm:agent-value ,,name))))
 
@@ -159,6 +202,7 @@ DESCRIPTION:
 
 (defagentvalueprim :label)
 (defagentvalueprim :label-color)
+(defsimpleprim :let :command nil)
 (defsimpleprim :lt :command clnl-nvm:turn-left)
 (defsimpleprim :not :reporter cl:not)
 (defkeywordprim :nobody)