(in-package #:clnl-transpiler)
(defparameter *prims* nil)
+(defparameter *prim-aliases* nil)
(defun prim-name (prim) (getf prim :name))
(defun prim-type (prim) (getf prim :type))
(defun is-reporter (prim) (eql :reporter (getf prim :type)))
(defun is-command (prim) (eql :command (getf prim :type)))
-(defun find-prim (symb) (find symb *prims* :key #'prim-name))
+(defun find-prim (symb)
+ (or
+ (find symb *prims* :key #'prim-name)
+ (find-prim (getf (find symb *prim-aliases* :key #'prim-name) :real-symb))))
; Let this grow, slowly but surely, eventually taking on calling context, etc.
; For now, it's just a
(defun transpile-command-block (block)
`(lambda () ,@(mapcar #'transpile-command (cdr block))))
+; Undoes the previous function :)
+(defun make-command-block-inline (block)
+ (cddr block))
+
(defmacro defprim (name type func)
`(push (list :name ,name :type ,type :func ,func) *prims*))
(defmacro defsimpleprim (name type simple-func)
`(defprim ,name ,type (lambda (&rest args) `(,',simple-func ,@args))))
+(defmacro defprim-alias (name real-symb)
+ `(push (list :name ,name :real-symb ,real-symb) *prim-aliases*))
+
; We count on the parser to handle arguemnts for us, when collating things.
(defsimpleprim := :reporter cl:equalp)
(defsimpleprim :crt :command clnl-nvm:create-turtles)
(defsimpleprim :die :command clnl-nvm:die)
(defsimpleprim :fd :command clnl-nvm:forward)
+(defprim :if :command (lambda (pred a) `(when ,pred ,@(make-command-block-inline a))))
+(defprim :ifelse :command (lambda (pred a b)
+ `(if ,pred
+ ,@(make-command-block-inline a)
+ ,@(make-command-block-inline b))))
+
+(defprim-alias :if-else :ifelse)
(defsimpleprim :lt :command clnl-nvm:turn-left)
(defsimpleprim :random-float :reporter clnl-nvm:random-float)
(defsimpleprim :rt :command clnl-nvm:turn-right)
(defsimplecommandtest "lt 2" "crt 100 ask turtles [ fd random-float 5 lt random-float 1080 fd random-float 4 ]"
"07DEB6F4F007DB86CD8F2C2E10BD4E35CAD2B0CE")
+
+(defsimplecommandtest "if 1" "if 5 = 5 [ crt 10 ]"
+ "A925E39EC022967568D238D31F70F0A375024A89")
+
+(defsimplecommandtest "if 2" "if 5 = 4 [ crt 10 ]"
+ "E1DE30F072D785E0D0B59F28B0F7853E3D3E0D8B")
+
+(defsimplecommandtest "ifelse 1" "ifelse 5 = 5 [ crt 10 ] [crt 5 ] if-else 5 = 5 [ crt 10 ] [ crt 5 ]"
+ "2CF70DC9135754E77B64422C10E947E776E731E6")
+
+(defsimplecommandtest "ifelse 2" "ifelse 5 = 4 [ crt 10 ] [ crt 5 ] if-else 5 = 4 [ crt 10 ] [ crt 5 ]"
+ "A925E39EC022967568D238D31F70F0A375024A89")