9862515065b33a5bd3e550bfe8d57aabf2e07e15
[clnl] / src / main / main.lisp
1 (in-package #:clnl)
2
3 (defun e (ast) ast)
4
5 (defun r (str)
6  (let*
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)))
14
15 (defun p (result) result)
16
17 (defun run ()
18  "RUN => RESULT
19
20 ARGUMENTS AND VALUES:
21
22   RESULT: undefined, the system terminates at the end of the loop
23
24 DESCRIPTION:
25
26   RUN starts up the CLNL system."
27
28  (boot)
29  (sb-thread:make-thread #'clnl-cli:run)
30  (clnl-interface:run))
31
32 (defun boot (&optional file headless-mode)
33  "BOOT &optional FILE HEADLESS-MODE => RESULT
34
35 ARGUMENTS AND VALUES:
36
37   FILE: nlogo file with which to initialize state
38   HEADLESS-MODE: a boolean, defaults to nil
39   RESULT: undefined
40
41 DESCRIPTION:
42
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.
45
46   When FILE is not provided, a default model is used.
47
48   When HEADLESS-MODE is set to nil, the opengl interface is initialized.
49   Otherwise, the model will run headlessly, with no view."
50  (let
51   ((netlogoed-lisp
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)))
57
58 (defun run-commands (cmds)
59  "RUN-COMMANDS CMDS => RESULT
60
61 ARGUMENTS AND VALUES:
62
63   CMDS: A string that may have one more NetLogo commands
64   RESULT: undefined
65
66 DESCRIPTION:
67
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))))))
71
72 (defun run-reporter (reporter)
73  "RUN-REPORTER REPORTER => RESULT
74
75 ARGUMENTS AND VALUES:
76
77   REPORTER: A string that should have only one reporter
78   RESULT: The value reported by the NVM
79
80 DESCRIPTION:
81
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)))))
85
86 (defun model->single-form-lisp (model &key (seed 15) initialize-interface netlogo-callback)
87  (multiple-value-bind
88   (code-ast prims)
89   (clnl-code-parser:parse (clnl-lexer:lex (clnl-model:code model)) (clnl-model:widget-globals model))
90   (let
91    ((globals
92      (append
93       (clnl-model:widget-globals model)
94       (clnl-code-parser:globals code-ast))))
95    `(prog ()
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)))
98      (let
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)))
102       (labels
103        ,(mapcar
104          (lambda (proc)
105           `(,(intern (string-upcase (car proc)) *model-package*) ()
106             ,@(cdr ; remove the progn, cuz it looks nicer
107                (clnl-transpiler:transpile (cadr proc)
108                 (mapcar
109                  (lambda (prim)
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)
113                    prim)) prims)))))
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)
118         :globals (list
119                   ,@(mapcar
120                      (lambda (pair)
121                       `(list ,(car pair) (lambda () ,(intern (string-upcase (car pair)) *model-package*))))
122                      globals)))
123        ,@(when netlogo-callback
124           `((funcall ,netlogo-callback
125              (lambda (netlogo-code)
126               (eval
127                (clnl-transpiler:transpile
128                 (clnl-parser:parse
129                  (clnl-lexer:lex netlogo-code)
130                  (list ,@(mapcar (lambda (prim) `(list ,@prim)) prims)))
131                 (list ,@(mapcar (lambda (prim) `(list ,@prim)) prims))))))))
132        ,@(when initialize-interface `((clnl-interface:initialize :dims ',(clnl-model:world-dimensions model))))))))))
133
134 (setf (documentation 'model->single-form-lisp 'function)
135  "MODEL->SINGLE-FORM-LISP MODEL &key SEED INITIALIZE-INTERFACE NETLOGO-CALLBACK => FORM
136
137 ARGUMENTS AND VALUES:
138
139   MODEL: A valid model
140   SEED: An integer, defaults to 15
141   INITIALIZE-INTERFACE: A boolean
142   NETLOGO-CALLBACK: A function of one argument, or a symbol
143   FORM: A common lisp form
144
145 DESCRIPTION:
146
147   MODEL->SINGLE-FORM-LISP takes a model and returns a lisp program as a single form,
148   that when executed runs the model.  The SEED passed in is used to start the
149   clnl-random RNG.
150
151   INITIALIZE-INTERFACE, when non nil, leads to initialization code for the
152   opengl interface being included.
153
154   NETLOGO-CALLBACK is a function that when called with a single argument,
155   a function that when called with netlogo code, will compile and run that
156   code in the environment of the model.
157
158   Of note, all globals defined either in the model code or via the widgets
159   are declared special in order to remain in the lexical environment for EVAL.")
160
161 (defun model->multi-form-lisp (model boot-fn &key (seed 15) initialize-interface netlogo-callback-fn)
162  (multiple-value-bind
163   (code-ast prims)
164   (clnl-code-parser:parse (clnl-lexer:lex (clnl-model:code model)) (clnl-model:widget-globals model))
165   (let
166    ((globals
167      (append
168       (clnl-model:widget-globals model)
169       (clnl-code-parser:globals code-ast))))
170    `((in-package ,(intern (package-name *model-package*) :keyword))
171      ,@(mapcar
172         (lambda (pair)
173          `(defvar ,(intern (string-upcase (car pair)) *model-package*) ,(cadr pair)))
174         globals)
175      ,@(mapcar
176         (lambda (proc)
177          `(defun ,(intern (string-upcase (car proc)) *model-package*) ()
178            ,@(cdr ; remove the progn, cuz it looks nicer
179               (clnl-transpiler:transpile (cadr proc)
180                (mapcar
181                 (lambda (prim)
182                  (if (getf prim :macro) ; The reason we do this is because with macros, we want to evaluate them in
183                                         ; this scope while preserving them for the generational purposes below
184                   (append (list :macro (eval (getf prim :macro))) prim)
185                   prim)) prims)))))
186         (clnl-code-parser:procedures code-ast))
187      (defun ,boot-fn ()
188       (clnl-random:set-seed ,seed)
189       (clnl-nvm:create-world
190        :dims ',(clnl-model:world-dimensions model)
191        :globals (list
192                  ,@(mapcar
193                     (lambda (pair) `(list ,(car pair) (lambda () ,(intern (string-upcase (car pair)) *model-package*))))
194                     globals)))
195       ,@(when initialize-interface `((clnl-interface:initialize :dims ',(clnl-model:world-dimensions model)))))
196      ,@(when netlogo-callback-fn
197         `((defun ,netlogo-callback-fn (,(intern "NETLOGO-CODE" *model-package*))
198            (eval
199             (clnl-transpiler:transpile
200              (clnl-parser:parse
201               (clnl-lexer:lex ,(intern "NETLOGO-CODE" *model-package*))
202               (list ,@(mapcar (lambda (prim) `(list ,@prim)) prims)))
203              (list ,@(mapcar (lambda (prim) `(list ,@prim)) prims)))))))))))
204
205 (setf (documentation 'model->multi-form-lisp 'function)
206  "MODEL->MULTI-FORM-LISP MODEL BOOT-FN &key SEED INITIALIZE-INTERFACE NETLOGO-CALLBACK-FN => FORMS
207
208 ARGUMENTS AND VALUES:
209
210   MODEL: A valid model
211   BOOT-FN: A function name
212   SEED: An integer, defaults to 15
213   INITIALIZE-INTERFACE: A boolean
214   NETLOGO-CALLBACK-FN: a symbol
215   FORMS: A list of common lisp form
216
217 DESCRIPTION:
218
219   MODEL->MULTI-FORM-LISP takes a model and returns a multi form lisp program,
220   that when executed, sets up the model.  Procedures map to defuns, globals
221   to defvars, etc.  This can be output to load up quickly later.  A function
222   named by BOOT-FN will be set for booting the program.
223
224   The SEED passed in is used to start the clnl-random RNG.
225
226   INITIALIZE-INTERFACE, when non nil, leads to initialization code for the
227   opengl interface being included.
228
229   NETLOGO-CALLBACK-FN is a symbol that will be defined as a function
230   to be called to execute code in the running netlogo instance.")