From: Frank Duncan Date: Tue, 26 Apr 2016 04:52:19 +0000 (-0500) Subject: Prims - Implement let X-Git-Tag: v0.1.0~39 X-Git-Url: https://code.consxy.com/gitweb/gitweb.cgi?a=commitdiff_plain;h=1e4bdde;p=clnl Prims - Implement let --- diff --git a/src/main/code-parse.lisp b/src/main/code-parse.lisp index c0e553b..7d9c42c 100644 --- a/src/main/code-parse.lisp +++ b/src/main/code-parse.lisp @@ -119,5 +119,5 @@ DESCRIPTION: Returns the globals that get declared in the code." (mapcar - (lambda (global) (list (symbol-name global) 0d0)) + (lambda (global) (list (intern (symbol-name global) clnl:*model-package*) 0d0)) (cdr (second (find :globals code-parsed-ast :key #'car))))) diff --git a/src/main/transpile.lisp b/src/main/transpile.lisp index a45183b..690fa3e 100644 --- a/src/main/transpile.lisp +++ b/src/main/transpile.lisp @@ -3,6 +3,8 @@ (defparameter *prims* nil) (defparameter *prim-aliases* nil) +(defvar *local-variables* nil) + (defun prim-name (prim) (getf prim :name)) (defun prim-type (prim) (getf prim :type)) (defun prim-func (prim) (getf prim :func)) @@ -34,7 +36,32 @@ DESCRIPTION: 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))) + ,@(transpile-commands-inner parsed-ast))) + +(defun transpile-commands-inner (parsed-ast) + (cond + ((not parsed-ast) nil) + ((and (listp (car parsed-ast)) (eql :let (caar parsed-ast))) (list (handle-let parsed-ast))) + (t + (cons + (transpile-command (car parsed-ast)) + (transpile-commands-inner (cdr parsed-ast)))))) + +(defun handle-let (parsed-ast &optional vars) + (if + (and (listp (car parsed-ast)) (eql :let (caar parsed-ast))) + (let + ((*local-variables* (cons (second (car parsed-ast)) *local-variables*))) + (handle-let + (cdr parsed-ast) + (cons + (list + (transpile-reporter (second (car parsed-ast))) + (transpile-reporter (third (car parsed-ast)))) + vars))) + `(let* + ,vars + ,@(transpile-commands-inner parsed-ast)))) (defun transpile-command (command) (cond @@ -63,15 +90,18 @@ DESCRIPTION: 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 + ; The parser should have checked that having a symbol here is ok + ((symbolp reporter) (intern (symbol-name reporter) clnl:*model-package*)) ((not (listp reporter)) (error "Expected a statement of some sort")) ((eql :command-block (car reporter)) (transpile-command-block reporter)) + ((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))) (t (apply (prim-func (find-prim (car reporter))) (mapcar #'transpile-reporter (cdr reporter)))))) (defun transpile-command-block (block) - `(lambda () ,@(mapcar #'transpile-command (cdr block)))) + `(lambda () ,@(transpile-commands-inner (cdr block)))) ; Undoes the previous function :) (defun make-command-block-inline (block) diff --git a/src/test/simpletests.lisp b/src/test/simpletests.lisp index c2a11ba..6a3e53f 100644 --- a/src/test/simpletests.lisp +++ b/src/test/simpletests.lisp @@ -137,3 +137,9 @@ (defsimplereportertest "colors 2" "black" "0" "E1DE30F072D785E0D0B59F28B0F7853E3D3E0D8B") + +(defsimplecommandtest "let 1" "let a 5 crt a" + "9FE588C2749CD9CE66CB0EA451EFB80476E881FB") + +(defsimplecommandtest "let 2" "let a 5 let b 6 crt (a + b)" + "4ABB6822402929878AB9E5A1084B9E4AE1F01D5B")