X-Git-Url: https://code.consxy.com/gitweb/gitweb.cgi?a=blobdiff_plain;f=src%2Fmain%2Ftranspile.lisp;h=a45183b921ee018f9b65263548aafaf5256a83c2;hb=399b297;hp=d29980ec419a1daec9f45db4e674effc80da0c12;hpb=daa3b99095a3219506e930c7dd684bfbab4cf107;p=clnl diff --git a/src/main/transpile.lisp b/src/main/transpile.lisp index d29980e..a45183b 100644 --- a/src/main/transpile.lisp +++ b/src/main/transpile.lisp @@ -1,6 +1,7 @@ (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)) @@ -8,7 +9,11 @@ (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) + (when 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 @@ -68,12 +73,22 @@ DESCRIPTION: (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 defkeywordprim (name) + `(defprim ,name :reporter (lambda () ',name))) + +(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) @@ -87,7 +102,26 @@ DESCRIPTION: (defprim :any? :reporter (lambda (agentset) `(> (length ,agentset) 0))) (defsimpleprim :ask :command clnl-nvm:ask) (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) +(defkeywordprim :nobody) (defsimpleprim :random-float :reporter clnl-nvm:random-float) +(defsimpleprim :rt :command clnl-nvm:turn-right) (defsimpleprim :show :command clnl-nvm:show) (defsimpleprim :turtles :reporter clnl-nvm:turtles) + +; Colors +(defmacro defcolorprim (color) `(defprim ,color :reporter (lambda () `(clnl-nvm:lookup-color ,,color)))) +(defcolorprim :black) +(defcolorprim :blue) +(defcolorprim :brown) +(defcolorprim :green) +(defcolorprim :white)