X-Git-Url: https://code.consxy.com/gitweb/gitweb.cgi?a=blobdiff_plain;f=src%2Fmain%2Ftranspile.lisp;h=d3f96124c0a3745d39183616acb17556ee644140;hb=ede7df20b380cf9d943a8e004965c5712d7b8ce6;hp=80fe4afe9b4220059dbdd04089550445f4ef3820;hpb=c5613b87ab91b845b628d9201ad053da92912838;p=clnl diff --git a/src/main/transpile.lisp b/src/main/transpile.lisp index 80fe4af..d3f9612 100644 --- a/src/main/transpile.lisp +++ b/src/main/transpile.lisp @@ -1,13 +1,4 @@ -(in-package #:cl-nl.transpiler) - -; This is responsible for taking an ast and turning it into valid CL code -; targeting the nvm. Here is where start to care about commands versus reporters -; and ensuring that things are in the right place. The reason we wait until here -; is because we want to allow someone else to play with the AST before handing it off -; to us. For instance, the command center wants to add "show" to reporters, and -; the users dictate based on entry point whether they are expecting a command -; or a reporter. So monitors can say "hey, transpile this reporter" and we'll check -; to make sure it actually is. +(in-package #:clnl-transpiler) (defparameter *prims* nil) @@ -20,8 +11,23 @@ (defun find-prim (symb) (find symb *prims* :key #'prim-name)) ; Let this grow, slowly but surely, eventually taking on calling context, etc. -; For now, it's just a -(defun transpile-command-block (parsed-ast) +; For now, it's just a +(defun transpile-commands (parsed-ast) + "TRANSPILE-COMMANDS PARSED-AST => AST + +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 + +DESCRIPTION: + + TRANSPILE-COMMANDS 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 commands, so it does + extra checking to ensure that commands are actually in the PARSED-AST." `(progn ,@(mapcar #'transpile-command parsed-ast))) @@ -30,22 +36,55 @@ ((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 + +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 ((symbolp reporter) reporter) ; The parser should have checked that having a symbol here is ok ((not (listp reporter)) (error "Expected a statement of some sort")) + ((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)))))) -(defmacro defprim (name type nvm-func) - `(push - (list :name ,name :type ,type :func ',nvm-func) - *prims*)) +(defun transpile-command-block (block) + `(lambda () ,@(mapcar #'transpile-command (cdr 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)))) ; We count on the parser to handle arguemnts for us, when collating things. -(defprim :crt :command cl-nl.nvm::create-turtles) -(defprim :show :command cl-nl.nvm::show) + +(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 :ask :command clnl-nvm:ask) +(defsimpleprim :crt :command clnl-nvm:create-turtles) +(defsimpleprim :fd :command clnl-nvm:forward) +(defsimpleprim :random-float :reporter clnl-nvm:random-float) +(defsimpleprim :show :command clnl-nvm:show) +(defsimpleprim :turtles :reporter clnl-nvm:turtles)