Parse widgets v2 - globals
[clnl] / src / main / main.lisp
1 (in-package #:clnl)
2
3 (defvar *model-package* (find-package :cl-user)
4  "*MODEL-PACKAGE*
5
6 VALUE TYPE:
7
8   a package
9
10 INITIAL VALUE:
11
12   The common-lisp-user package
13
14 DESCRIPTION:
15
16   *MODEL-PACKAGE* is used for interning symbols as a NetLogo code
17   gets compiled.
18
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.")
23
24 (defun e (ast) ast)
25
26 (defun r (str)
27  (let*
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)))
35
36 (defun p (result) result)
37
38 (defun run ()
39  "RUN => RESULT
40
41 ARGUMENTS AND VALUES:
42
43   RESULT: undefined, the system terminates at the end of the loop
44
45 DESCRIPTION:
46
47   RUN starts up the CLNL system."
48
49  (boot)
50  (sb-thread:make-thread #'clnl-cli:run)
51  (clnl-interface:run))
52
53 (defun boot (&optional file)
54  "BOOT &optional FILE => RESULT
55
56 ARGUMENTS AND VALUES:
57
58   FILE: nlogo file with which to initialize state
59   RESULT: undefined
60
61 DESCRIPTION:
62
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.
65
66   When FILE is not provided, a default model is used."
67  (let
68   ((netlogoed-lisp
69     (model->lisp
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)))
73
74 (defun run-commands (cmds)
75  "RUN-COMMANDS CMDS => RESULT
76
77 ARGUMENTS AND VALUES:
78
79   CMDS: A string that may have one more NetLogo commands
80   RESULT: undefined
81
82 DESCRIPTION:
83
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)))))
87
88 (defun run-reporter (reporter)
89  "RUN-REPORTER REPORTER => RESULT
90
91 ARGUMENTS AND VALUES:
92
93   REPORTER: A string that should have only one reporter
94   RESULT: The value reported by the NVM
95
96 DESCRIPTION:
97
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))))))
101
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)
106  `(let
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))))