1 (in-package #:clnl-code-parser)
3 ; This is different from the general parser (in clnl-parser) in that
4 ; it's made for parsing the code section of nlogo files, and so works
5 ; outside of the constraints. In NetLogo, I believe this is analagous
6 ; to the StructureParser, but I'm guessing there's weird overlap with
9 (defvar *dynamic-prims* nil)
11 (defun global->prim (global)
16 :macro `(lambda () ',(intern (symbol-name global) clnl:*model-package*))))
18 (defun own->prim (symb)
19 (list :name symb :type :reporter :precedence 10 :macro `(lambda () '(clnl-nvm:agent-value ,symb))))
21 (defun breed->prims (breed-list)
23 ((plural (car breed-list))
24 (plural-name (symbol-name plural)))
26 (list :name plural :type :reporter :precedence 10 :macro `(lambda () ,plural))
28 :name (intern (format nil "~A-HERE" plural-name) :keyword)
31 :macro `(lambda () '(clnl-nvm:turtles-here ,plural)))
33 :name (intern (format nil "CREATE-~A" plural-name) :keyword)
35 :args '(:number (:command-block :optional))
37 :macro `(lambda (num &optional command-block)
38 `(clnl-nvm:create-turtles ,num ,,plural ,command-block))))))
40 (defun parse (lexed-ast &optional external-globals)
41 "PARSE LEXED-AST &optional EXTERNAL-GLOBALS => AST, PRIMS
45 LEXED-AST: An ambigious ast
46 EXTERNAL-GLOBALS: A list of symbols in keyword package
47 AST: An unambigious ast that represents the code block of a model
48 PRIMS: Primitives that can be sent to the parser and transpiler
52 PARSE takes a ambigious LEXED-AST and converts it to an unambigious one.
53 It also returns the primitives that are defined in the code file, including
54 ones generated from the EXTERNAL-GLOBALS, that can then be passed to both
55 the parser and the transpiler.
57 EXTERNAL-GLOBALS is a list of symbols representing global variables that
58 are not defined within the code. Normally these come from widgets defined
59 in the model file, but could arguably come from elsewhere.
61 This parser, unlike CLNL-PARSE:PARSE, should not be fed into the transpiler.
63 Rather, the ast that's returned can be queried with other functions included
64 in the CLNL-CODE-PARSER package to tease out necessary information. Some of
65 those things will involve code blocks that can then be transpiled."
69 (mapcar #'global->prim (mapcar #'car external-globals))
70 (procedures->prims lexed-ast)
71 (clnl-extensions:load-extension :cli)))
72 (parsed (parse-internal lexed-ast)))
75 (car (last parsed)))))
77 (defun procedures->prims (lexed-ast)
80 ; We'll need argument handling here sometime :)
81 ((eql :to (car lexed-ast))
84 :name (cadr lexed-ast)
87 :func `(function ,(intern (symbol-name (cadr lexed-ast)) clnl:*model-package*)))
88 (procedures->prims (cddr lexed-ast))))
89 (t (procedures->prims (cdr lexed-ast)))))
91 (defun parse-internal (lexed-ast)
94 (list *dynamic-prims*))
95 ((eql :to (car lexed-ast)) (parse-procedure lexed-ast))
96 ((find (car lexed-ast) '(:globals :turtles-own :patches-own))
97 (parse-with-unevaluated-list lexed-ast))
98 ((eql (car lexed-ast) :breed) (parse-breed lexed-ast))))
100 ; Due to the non expression style syntax of procedures, this must be special cased
101 (defun parse-procedure (tokens)
102 (multiple-value-bind (in-block after-block) (find-end tokens)
107 (clnl-parser:parse (cddr in-block) *dynamic-prims*))
108 (parse-internal after-block))))
110 (defun find-end (tokens)
112 ((not tokens) (error "Failed to find end"))
113 ((eql :end (car tokens)) (values nil (cdr tokens)))
114 (t (multiple-value-bind (in-block after-block) (find-end (cdr tokens))
115 (values (cons (car tokens) in-block) after-block)))))
117 ; This is a special case but left with a little wiggle room for future
118 ; enhancements, like code blocks
119 (defun parse-with-unevaluated-list (lexed-ast)
120 (when (not (eql :[ (cadr lexed-ast))) (error "Expected list literal here"))
121 (multiple-value-bind (in-list after-list) (find-closing-bracket (cddr lexed-ast))
123 (list (car lexed-ast) (cons :list-literal in-list))
128 (case (car lexed-ast)
129 (:globals #'global->prim)
130 (:turtles-own #'own->prim)
131 (:patches-own #'own->prim)
133 in-list) *dynamic-prims*)))
134 (parse-internal after-list)))))
136 (defun parse-breed (lexed-ast)
137 (when (not (eql :[ (cadr lexed-ast))) (error "Expected list literal here"))
138 (multiple-value-bind (in-list after-list) (find-closing-bracket (cddr lexed-ast))
140 (list (car lexed-ast) (cons :list-literal in-list))
142 ((*dynamic-prims* (append (breed->prims in-list) *dynamic-prims*)))
143 (parse-internal after-list)))))
145 (defun find-closing-bracket (tokens)
147 ((not tokens) (error "Failed to find a matching closing bracket"))
148 ((eql :] (car tokens)) (values nil (cdr tokens)))
149 ((eql :[ (car tokens)) (error "Expected name or ]"))
150 (t (multiple-value-bind (in-block after-block) (find-closing-bracket (cdr tokens))
151 (values (cons (car tokens) in-block) after-block)))))
153 (defun globals (code-parsed-ast)
154 "GLOBALS CODE-PARSED-AST => GLOBALS
158 ARGUMENTS AND VALUES:
160 CODE-PARSED-AST: An ast as created by clnl-code-parse:parse
161 GLOBAL: A symbol interned in :keyword
165 Returns the globals that get declared in the code."
167 (lambda (global) (list (intern (symbol-name global) :keyword) 0d0))
168 (cdr (second (find :globals code-parsed-ast :key #'car)))))
170 (defun turtles-own-vars (code-parsed-ast)
171 "TURTLES-OWN-VARS CODE-PARSED-AST => TURTLES-OWN-VARS
173 TURTLES-OWN-VARS: TURTLES-OWN-VAR*
175 ARGUMENTS AND VALUES:
177 CODE-PARSED-AST: An ast as created by clnl-code-parse:parse
178 TURTLES-OWN-VAR: A symbol interned in :keyword
182 Returns the turtles own variables that get declared in the code."
183 (cdr (second (find :turtles-own code-parsed-ast :key #'car))))
185 (defun patches-own-vars (code-parsed-ast)
186 "PATCHES-OWN-VARS CODE-PARSED-AST => PATCHES-OWN-VARS
188 PATCHES-OWN-VARS: PATCHES-OWN-VAR*
190 ARGUMENTS AND VALUES:
192 CODE-PARSED-AST: An ast as created by clnl-code-parse:parse
193 PATCHES-OWN-VAR: A symbol interned in :keyword
197 Returns the turtles own variables that get declared in the code."
198 (cdr (second (find :patches-own code-parsed-ast :key #'car))))
200 (defun breeds (code-parsed-ast)
201 "BREEDS CODE-PARSED-AST => BREEDS
205 ARGUMENTS AND VALUES:
207 CODE-PARSED-AST: An ast as created by clnl-code-parse:parse
208 BREED: A symbol interned in :keyword
212 Returns the breeds that get declared in the code."
213 (mapcar #'cadadr (remove :breed code-parsed-ast :test-not #'equal :key #'car)))
215 (defun procedures (code-parsed-ast)
216 "PROCEDURES CODE-PARSED-AST => PROCEDURES
218 PROCEDURES: PROCEDURE*
219 PROCEDURE: (NAME BODY)
221 ARGUMENTS AND VALUES:
223 CODE-PARSED-AST: An ast as created by clnl-code-parse:parse
224 NAME: A symbol interned in :keyword
225 BODY: A list of lexed forms
229 Returns the procedures that were defined in the code. These can
230 then be translated into common lisp by using mapcar on the BODY, and
231 set to some function defined by NAME"
233 (lambda (proc) (cdr proc))
234 (remove-if-not (lambda (form) (find (car form) '(:to :to-report))) code-parsed-ast)))