b6cce463cf762402adfa246bbdad7c6959a67ef0
[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-commands 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)
33  "BOOT &optional FILE => RESULT
34
35 ARGUMENTS AND VALUES:
36
37   FILE: nlogo file with which to initialize state
38   RESULT: undefined
39
40 DESCRIPTION:
41
42   BOOT does exactly that, boots the clnl system in a clean state.  The seed
43   is set so that multiple runs will evaluate to the same.
44
45   When FILE is not provided, a default model is used."
46  (let
47   ((netlogoed-lisp
48     (model->lisp
49      (if file (with-open-file (str file) (clnl-model:read-from-nlogo str)) (clnl-model:default-model))))
50    (*package* (find-package :cl)))
51   (eval netlogoed-lisp)))
52
53 (defun run-commands (cmds)
54  "RUN-COMMANDS CMDS => RESULT
55
56 ARGUMENTS AND VALUES:
57
58   CMDS: A string that may have one more NetLogo commands
59   RESULT: undefined
60
61 DESCRIPTION:
62
63   RUN-COMMANDS will take NetLogo commands, put them through the various
64   stages need to turn them into Common Lisp code, and run it."
65  (eval (clnl-transpiler:transpile-commands (clnl-parser:parse (clnl-lexer:lex cmds)))))
66
67 (defun run-reporter (reporter)
68  "RUN-REPORTER REPORTER => RESULT
69
70 ARGUMENTS AND VALUES:
71
72   REPORTER: A string that should have only one reporter
73   RESULT: The value reported by the NVM
74
75 DESCRIPTION:
76
77   RUN-REPORTER will take a NetLogo REPORTER, put it through the various
78   stages need to turn them into Common Lisp code, run it, and return the RESULT."
79  (eval (clnl-transpiler:transpile-reporter (car (clnl-parser:parse (clnl-lexer:lex reporter))))))
80
81 ; Everything gets tied together here
82 ; The intention of this method is to generate the common lisp equivalent of a model file,
83 ; such that if you decided to no longer use nlogo, you could use the engine without it.
84 (defun model->lisp (model)
85  `(progn
86    (clnl-random:set-seed 15) ; should the seed always be 15?
87    (clnl-nvm:create-world :dims ',(clnl-model:world-dimensions model))
88    (clnl-interface:initialize :dims ',(clnl-model:world-dimensions model))))