X-Git-Url: https://code.consxy.com/gitweb/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Ftranspile.lisp;h=9b4b5ee4f499d44d179f78a46954eb2f04817f22;hb=d546c89;hp=5f943678cc0588cdd6562f1188dc43059efdd43d;hpb=76f94e0a768f1f6f206c07eda27c690c42f1ddd4;p=clnl diff --git a/src/main/transpile.lisp b/src/main/transpile.lisp index 5f94367..9b4b5ee 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,10 @@ (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 @@ -36,7 +40,7 @@ DESCRIPTION: ((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))) - (t `(,(prim-func (find-prim (car command))) ,@(mapcar #'transpile-reporter (cdr command)))))) + (t (apply (prim-func (find-prim (car command))) (mapcar #'transpile-reporter (cdr command)))))) (defun transpile-reporter (reporter) "TRANSPILE-REPORTER REPORTER => AST @@ -63,21 +67,48 @@ DESCRIPTION: ((eql :command-block (car reporter)) (transpile-command-block reporter)) ((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))) - (t `(,(prim-func (find-prim (car reporter))) ,@(mapcar #'transpile-reporter (cdr reporter)))))) + (t (apply (prim-func (find-prim (car reporter))) (mapcar #'transpile-reporter (cdr reporter)))))) (defun transpile-command-block (block) `(lambda () ,@(mapcar #'transpile-command (cdr block)))) -(defmacro defprim (name type nvm-func) - `(push - (list :name ,name :type ,type :func ',nvm-func) - *prims*)) +; 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. -(defprim := :reporter cl:equalp) -(defprim :ask :command clnl-nvm:ask) -(defprim :crt :command clnl-nvm:create-turtles) -(defprim :fd :command clnl-nvm:forward) -(defprim :random-float :reporter clnl-nvm:random-float) -(defprim :show :command clnl-nvm:show) -(defprim :turtles :reporter clnl-nvm:turtles) + +(defsimpleprim := :reporter cl:equalp) +(defprim :!= :reporter (lambda (a b) `(not (equalp ,a ,b)))) +(defsimpleprim :<= :reporter cl:<=) +(defsimpleprim :< :reporter cl:<) +(defsimpleprim :- :reporter cl:-) +(defsimpleprim :+ :reporter cl:+) +(defsimpleprim :* :reporter cl:*) +(defsimpleprim :/ :reporter cl:/) +(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) +(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)