Parse widgets v2 - globals
[clnl] / src / main / main.lisp
index dcc7ca0c4573b732192e8d5bc4de4d29a3855b2d..09244a02229c2f11e8c54799557cef8dae6985a3 100644 (file)
@@ -1,5 +1,26 @@
 (in-package #:clnl)
 
+(defvar *model-package* (find-package :cl-user)
+ "*MODEL-PACKAGE*
+
+VALUE TYPE:
+
+  a package
+
+INITIAL VALUE:
+
+  The common-lisp-user package
+
+DESCRIPTION:
+
+  *MODEL-PACKAGE* is used for interning symbols as a NetLogo code
+  gets compiled.
+
+  Any local symbols are interned in this package, for use either
+  by other code, or in order to have all symbols interned in the
+  same placakge.  This is also the package in which a model should
+  be run, whether by clnl code or independently.")
+
 (defun e (ast) ast)
 
 (defun r (str)
@@ -25,22 +46,30 @@ DESCRIPTION:
 
   RUN starts up the CLNL system."
 
+ (boot)
  (sb-thread:make-thread #'clnl-cli:run)
  (clnl-interface:run))
 
-(defun boot ()
- "BOOT => RESULT
+(defun boot (&optional file)
+ "BOOT &optional FILE => RESULT
 
 ARGUMENTS AND VALUES:
 
+  FILE: nlogo file with which to initialize state
   RESULT: undefined
 
 DESCRIPTION:
 
   BOOT does exactly that, boots the clnl system in a clean state.  The seed
-  is set so that multiple runs will evaluate to the same."
- (clnl-random:set-seed 15)
- (clnl-nvm:create-world))
+  is set so that multiple runs will evaluate to the same.
+
+  When FILE is not provided, a default model is used."
+ (let
+  ((netlogoed-lisp
+    (model->lisp
+     (if file (with-open-file (str file) (clnl-model:read-from-nlogo str)) (clnl-model:default-model))))
+   (*package* *model-package*))
+  (eval netlogoed-lisp)))
 
 (defun run-commands (cmds)
  "RUN-COMMANDS CMDS => RESULT
@@ -69,3 +98,13 @@ DESCRIPTION:
   RUN-REPORTER will take a NetLogo REPORTER, put it through the various
   stages need to turn them into Common Lisp code, run it, and return the RESULT."
  (eval (clnl-transpiler:transpile-reporter (car (clnl-parser:parse (clnl-lexer:lex reporter))))))
+
+; Everything gets tied together here
+; The intention of this method is to generate the common lisp equivalent of a model file,
+; such that if you decided to no longer use nlogo, you could use the engine without it.
+(defun model->lisp (model)
+ `(let
+   ,(clnl-model:globals model)
+   (clnl-random:set-seed 15) ; should the seed always be 15?
+   (clnl-nvm:create-world :dims ',(clnl-model:world-dimensions model))
+   (clnl-interface:initialize :dims ',(clnl-model:world-dimensions model))))