7 ((lexed-ast (let ((ast (clnl-lexer:lex str)))
8 (format t "Via lexing, AST for~%~S~% became~%~S~%~%" str ast) ast))
9 (parsed-ast (let ((ast (clnl-parser:parse lexed-ast)))
10 (format t "Via parsing, AST for~%~S~% became~%~S~%~%" lexed-ast ast) ast))
11 (transpiled-ast (let ((ast (clnl-transpiler:transpile parsed-ast)))
12 (format t "Via transpiling, AST for~%~S~% became~%~S~%" parsed-ast ast) ast)))
13 (eval transpiled-ast)))
15 (defun p (result) result)
22 RESULT: undefined, the system terminates at the end of the loop
26 RUN starts up the CLNL system."
29 (sb-thread:make-thread #'clnl-cli:run)
32 (defun boot (&optional file headless-mode)
33 "BOOT &optional FILE HEADLESS-MODE => RESULT
37 FILE: nlogo file with which to initialize state
38 HEADLESS-MODE: a boolean, defaults to nil
43 BOOT does exactly that, boots the clnl system in a clean state. The seed
44 is set so that multiple runs will evaluate to the same.
46 When FILE is not provided, a default model is used.
48 When HEADLESS-MODE is set to nil, the opengl interface is initialized.
49 Otherwise, the model will run headlessly, with no view."
52 (model->single-form-lisp
53 (if file (with-open-file (str file) (clnl-model:read-from-nlogo str)) (clnl-model:default-model))
54 :initialize-interface (not headless-mode)))
55 (*package* *model-package*))
56 (eval netlogoed-lisp)))
58 (defun run-commands (cmds)
59 "RUN-COMMANDS CMDS => RESULT
63 CMDS: A string that may have one more NetLogo commands
68 RUN-COMMANDS will take NetLogo commands, put them through the various
69 stages need to turn them into Common Lisp code, and run it."
70 (clnl-nvm:with-stop-handler (eval (clnl-transpiler:transpile (clnl-parser:parse (clnl-lexer:lex cmds))))))
72 (defun run-reporter (reporter)
73 "RUN-REPORTER REPORTER => RESULT
77 REPORTER: A string that should have only one reporter
78 RESULT: The value reported by the NVM
82 RUN-REPORTER will take a NetLogo REPORTER, put it through the various
83 stages need to turn them into Common Lisp code, run it, and return the RESULT."
84 (eval (clnl-transpiler:transpile (clnl-parser:parse (clnl-lexer:lex reporter)))))
86 (defun model->single-form-lisp (model &key (seed 15) initialize-interface netlogo-callback)
89 (clnl-code-parser:parse (clnl-lexer:lex (clnl-model:code model)) (clnl-model:widget-globals model))
93 (clnl-model:widget-globals model)
94 (clnl-code-parser:globals code-ast))))
96 ; First declare is in case we don't use it, it shows up in export correctly
97 (declare (special ,@(mapcar (lambda (pair) (intern (string-upcase (car pair)) *model-package*)) globals)))
99 ,(mapcar (lambda (pair) (list (intern (string-upcase (car pair)) *model-package*) (cadr pair))) globals)
100 ; We declare twice rather than once and doing a bunch of setfs
101 (declare (special ,@(mapcar (lambda (pair) (intern (string-upcase (car pair)) *model-package*)) globals)))
105 `(,(intern (string-upcase (car proc)) *model-package*) ()
106 ,@(cdr ; remove the progn, cuz it looks nicer
107 (clnl-transpiler:transpile (cadr proc)
110 (if (getf prim :macro) ; The reason we do this is because with macros, we want to evaluate them in
111 ; this scope while preserving them for the generational purposes below
112 (append (list :macro (eval (getf prim :macro))) prim)
114 (clnl-code-parser:procedures code-ast))
115 (clnl-random:set-seed ,seed)
116 (clnl-nvm:create-world
117 :dims ',(clnl-model:world-dimensions model)
121 `(list ,(car pair) (lambda () ,(intern (string-upcase (car pair)) *model-package*))))
123 :turtles-own-vars ',(clnl-code-parser:turtles-own-vars code-ast)
124 :patches-own-vars ',(clnl-code-parser:patches-own-vars code-ast))
125 ,@(when netlogo-callback
126 `((funcall ,netlogo-callback
127 (lambda (netlogo-code)
129 (clnl-transpiler:transpile
131 (clnl-lexer:lex netlogo-code)
132 (list ,@(mapcar (lambda (prim) `(list ,@prim)) prims)))
133 (list ,@(mapcar (lambda (prim) `(list ,@prim)) prims))))))))
134 ,@(when initialize-interface `((clnl-interface:initialize :dims ',(clnl-model:world-dimensions model))))))))))
136 (setf (documentation 'model->single-form-lisp 'function)
137 "MODEL->SINGLE-FORM-LISP MODEL &key SEED INITIALIZE-INTERFACE NETLOGO-CALLBACK => FORM
139 ARGUMENTS AND VALUES:
142 SEED: An integer, defaults to 15
143 INITIALIZE-INTERFACE: A boolean
144 NETLOGO-CALLBACK: A function of one argument, or a symbol
145 FORM: A common lisp form
149 MODEL->SINGLE-FORM-LISP takes a model and returns a lisp program as a single form,
150 that when executed runs the model. The SEED passed in is used to start the
153 INITIALIZE-INTERFACE, when non nil, leads to initialization code for the
154 opengl interface being included.
156 NETLOGO-CALLBACK is a function that when called with a single argument,
157 a function that when called with netlogo code, will compile and run that
158 code in the environment of the model.
160 Of note, all globals defined either in the model code or via the widgets
161 are declared special in order to remain in the lexical environment for EVAL.")
163 (defun model->multi-form-lisp (model boot-fn &key (seed 15) initialize-interface netlogo-callback-fn)
166 (clnl-code-parser:parse (clnl-lexer:lex (clnl-model:code model)) (clnl-model:widget-globals model))
170 (clnl-model:widget-globals model)
171 (clnl-code-parser:globals code-ast))))
172 `((in-package ,(intern (package-name *model-package*) :keyword))
175 `(defvar ,(intern (string-upcase (car pair)) *model-package*) ,(cadr pair)))
179 `(defun ,(intern (string-upcase (car proc)) *model-package*) ()
180 ,@(cdr ; remove the progn, cuz it looks nicer
181 (clnl-transpiler:transpile (cadr proc)
184 (if (getf prim :macro) ; The reason we do this is because with macros, we want to evaluate them in
185 ; this scope while preserving them for the generational purposes below
186 (append (list :macro (eval (getf prim :macro))) prim)
188 (clnl-code-parser:procedures code-ast))
190 (clnl-random:set-seed ,seed)
191 (clnl-nvm:create-world
192 :dims ',(clnl-model:world-dimensions model)
195 (lambda (pair) `(list ,(car pair) (lambda () ,(intern (string-upcase (car pair)) *model-package*))))
197 :turtles-own-vars ',(clnl-code-parser:turtles-own-vars code-ast)
198 :patches-own-vars ',(clnl-code-parser:patches-own-vars code-ast))
199 ,@(when initialize-interface `((clnl-interface:initialize :dims ',(clnl-model:world-dimensions model)))))
200 ,@(when netlogo-callback-fn
201 `((defun ,netlogo-callback-fn (,(intern "NETLOGO-CODE" *model-package*))
203 (clnl-transpiler:transpile
205 (clnl-lexer:lex ,(intern "NETLOGO-CODE" *model-package*))
206 (list ,@(mapcar (lambda (prim) `(list ,@prim)) prims)))
207 (list ,@(mapcar (lambda (prim) `(list ,@prim)) prims)))))))))))
209 (setf (documentation 'model->multi-form-lisp 'function)
210 "MODEL->MULTI-FORM-LISP MODEL BOOT-FN &key SEED INITIALIZE-INTERFACE NETLOGO-CALLBACK-FN => FORMS
212 ARGUMENTS AND VALUES:
215 BOOT-FN: A function name
216 SEED: An integer, defaults to 15
217 INITIALIZE-INTERFACE: A boolean
218 NETLOGO-CALLBACK-FN: a symbol
219 FORMS: A list of common lisp form
223 MODEL->MULTI-FORM-LISP takes a model and returns a multi form lisp program,
224 that when executed, sets up the model. Procedures map to defuns, globals
225 to defvars, etc. This can be output to load up quickly later. A function
226 named by BOOT-FN will be set for booting the program.
228 The SEED passed in is used to start the clnl-random RNG.
230 INITIALIZE-INTERFACE, when non nil, leads to initialization code for the
231 opengl interface being included.
233 NETLOGO-CALLBACK-FN is a symbol that will be defined as a function
234 to be called to execute code in the running netlogo instance.")