3 (defvar *model-package* (find-package :cl-user)
12 The common-lisp-user package
16 *MODEL-PACKAGE* is used for interning symbols as a NetLogo code
19 Any local symbols are interned in this package, for use either
20 by other code, or in order to have all symbols interned in the
21 same placakge. This is also the package in which a model should
22 be run, whether by clnl code or independently.")
28 ((lexed-ast (let ((ast (clnl-lexer:lex str)))
29 (format t "Via lexing, AST for~%~S~% became~%~S~%~%" str ast) ast))
30 (parsed-ast (let ((ast (clnl-parser:parse lexed-ast)))
31 (format t "Via parsing, AST for~%~S~% became~%~S~%~%" lexed-ast ast) ast))
32 (transpiled-ast (let ((ast (clnl-transpiler:transpile-commands parsed-ast)))
33 (format t "Via transpiling, AST for~%~S~% became~%~S~%" parsed-ast ast) ast)))
34 (eval transpiled-ast)))
36 (defun p (result) result)
43 RESULT: undefined, the system terminates at the end of the loop
47 RUN starts up the CLNL system."
50 (sb-thread:make-thread #'clnl-cli:run)
53 (defun boot (&optional file)
54 "BOOT &optional FILE => RESULT
58 FILE: nlogo file with which to initialize state
63 BOOT does exactly that, boots the clnl system in a clean state. The seed
64 is set so that multiple runs will evaluate to the same.
66 When FILE is not provided, a default model is used."
70 (if file (with-open-file (str file) (clnl-model:read-from-nlogo str)) (clnl-model:default-model))))
71 (*package* *model-package*))
72 (eval netlogoed-lisp)))
74 (defun run-commands (cmds)
75 "RUN-COMMANDS CMDS => RESULT
79 CMDS: A string that may have one more NetLogo commands
84 RUN-COMMANDS will take NetLogo commands, put them through the various
85 stages need to turn them into Common Lisp code, and run it."
86 (eval (clnl-transpiler:transpile-commands (clnl-parser:parse (clnl-lexer:lex cmds)))))
88 (defun run-reporter (reporter)
89 "RUN-REPORTER REPORTER => RESULT
93 REPORTER: A string that should have only one reporter
94 RESULT: The value reported by the NVM
98 RUN-REPORTER will take a NetLogo REPORTER, put it through the various
99 stages need to turn them into Common Lisp code, run it, and return the RESULT."
100 (eval (clnl-transpiler:transpile-reporter (car (clnl-parser:parse (clnl-lexer:lex reporter))))))
102 ; Everything gets tied together here
103 ; The intention of this method is to generate the common lisp equivalent of a model file,
104 ; such that if you decided to no longer use nlogo, you could use the engine without it.
105 (defun model->lisp (model)
107 ,(clnl-model:globals model)
108 (clnl-random:set-seed 15) ; should the seed always be 15?
109 (clnl-nvm:create-world :dims ',(clnl-model:world-dimensions model))
110 (clnl-interface:initialize :dims ',(clnl-model:world-dimensions model))))