1 ; Copyright 2022 Frank Duncan (frank@consxy.com) under AGPL3. See distributed LICENSE.txt.
2 (in-package #:clnl-code-parser)
4 ; This is different from the general parser (in clnl-parser) in that
5 ; it's made for parsing the code section of nlogo files, and so works
6 ; outside of the constraints. In NetLogo, I believe this is analagous
7 ; to the StructureParser, but I'm guessing there's weird overlap with
10 (defvar *dynamic-prims* nil)
12 (defun global->prim (global)
17 :macro `(lambda () ',(intern (symbol-name global) clnl:*model-package*))))
19 (defun own->prim (symb)
20 (list :name symb :type :reporter :precedence 10 :macro `(lambda () '(clnl-nvm:agent-value ,symb))))
22 (defun breed->prims (breed-list)
24 ((plural (car breed-list))
25 (plural-name (symbol-name plural)))
27 (list :name plural :type :reporter :precedence 10 :macro `(lambda () ,plural))
29 :name (intern (format nil "~A-HERE" plural-name) :keyword)
32 :macro `(lambda () '(clnl-nvm:turtles-here ,plural)))
34 :name (intern (format nil "CREATE-~A" plural-name) :keyword)
36 :args '(:number (:command-block :optional))
38 :macro `(lambda (num &optional command-block)
39 `(clnl-nvm:create-turtles ,num ,,plural ,command-block))))))
41 (defun parse (lexed-ast &optional external-globals)
42 "PARSE LEXED-AST &optional EXTERNAL-GLOBALS => AST, PRIMS
46 LEXED-AST: An ambigious ast
47 EXTERNAL-GLOBALS: A list of symbols in keyword package
48 AST: An unambigious ast that represents the code block of a model
49 PRIMS: Primitives that can be sent to the parser and transpiler
53 PARSE takes a ambigious LEXED-AST and converts it to an unambigious one.
54 It also returns the primitives that are defined in the code file, including
55 ones generated from the EXTERNAL-GLOBALS, that can then be passed to both
56 the parser and the transpiler.
58 EXTERNAL-GLOBALS is a list of symbols representing global variables that
59 are not defined within the code. Normally these come from widgets defined
60 in the model file, but could arguably come from elsewhere.
62 This parser, unlike CLNL-PARSE:PARSE, should not be fed into the transpiler.
64 Rather, the ast that's returned can be queried with other functions included
65 in the CLNL-CODE-PARSER package to tease out necessary information. Some of
66 those things will involve code blocks that can then be transpiled."
70 (mapcar #'global->prim (mapcar #'car external-globals))
71 (procedures->prims lexed-ast)
72 (clnl-extensions:load-extension :cli)))
73 (parsed (parse-internal lexed-ast)))
76 (car (last parsed)))))
78 (defun procedures->prims (lexed-ast)
81 ; We'll need argument handling here sometime :)
82 ((eql :to (car lexed-ast))
85 :name (cadr lexed-ast)
88 :func `(function ,(intern (symbol-name (cadr lexed-ast)) clnl:*model-package*)))
89 (procedures->prims (cddr lexed-ast))))
90 (t (procedures->prims (cdr lexed-ast)))))
92 (defun parse-internal (lexed-ast)
95 (list *dynamic-prims*))
96 ((eql :to (car lexed-ast)) (parse-procedure lexed-ast))
97 ((find (car lexed-ast) '(:globals :turtles-own :patches-own))
98 (parse-with-unevaluated-list lexed-ast))
99 ((eql (car lexed-ast) :breed) (parse-breed lexed-ast))))
101 ; Due to the non expression style syntax of procedures, this must be special cased
102 (defun parse-procedure (tokens)
103 (multiple-value-bind (in-block after-block) (find-end tokens)
108 (clnl-parser:parse (cddr in-block) *dynamic-prims*))
109 (parse-internal after-block))))
111 (defun find-end (tokens)
113 ((not tokens) (error "Failed to find end"))
114 ((eql :end (car tokens)) (values nil (cdr tokens)))
115 (t (multiple-value-bind (in-block after-block) (find-end (cdr tokens))
116 (values (cons (car tokens) in-block) after-block)))))
118 ; This is a special case but left with a little wiggle room for future
119 ; enhancements, like code blocks
120 (defun parse-with-unevaluated-list (lexed-ast)
121 (when (not (eql :[ (cadr lexed-ast))) (error "Expected list literal here"))
122 (multiple-value-bind (in-list after-list) (find-closing-bracket (cddr lexed-ast))
124 (list (car lexed-ast) (cons :list-literal in-list))
129 (case (car lexed-ast)
130 (:globals #'global->prim)
131 (:turtles-own #'own->prim)
132 (:patches-own #'own->prim)
134 in-list) *dynamic-prims*)))
135 (parse-internal after-list)))))
137 (defun parse-breed (lexed-ast)
138 (when (not (eql :[ (cadr lexed-ast))) (error "Expected list literal here"))
139 (multiple-value-bind (in-list after-list) (find-closing-bracket (cddr lexed-ast))
141 (list (car lexed-ast) (cons :list-literal in-list))
143 ((*dynamic-prims* (append (breed->prims in-list) *dynamic-prims*)))
144 (parse-internal after-list)))))
146 (defun find-closing-bracket (tokens)
148 ((not tokens) (error "Failed to find a matching closing bracket"))
149 ((eql :] (car tokens)) (values nil (cdr tokens)))
150 ((eql :[ (car tokens)) (error "Expected name or ]"))
151 (t (multiple-value-bind (in-block after-block) (find-closing-bracket (cdr tokens))
152 (values (cons (car tokens) in-block) after-block)))))
154 (defun globals (code-parsed-ast)
155 "GLOBALS CODE-PARSED-AST => GLOBALS
159 ARGUMENTS AND VALUES:
161 CODE-PARSED-AST: An ast as created by clnl-code-parse:parse
162 GLOBAL: A symbol interned in :keyword
166 Returns the globals that get declared in the code."
168 (lambda (global) (list (intern (symbol-name global) :keyword) 0d0))
169 (cdr (second (find :globals code-parsed-ast :key #'car)))))
171 (defun turtles-own-vars (code-parsed-ast)
172 "TURTLES-OWN-VARS CODE-PARSED-AST => TURTLES-OWN-VARS
174 TURTLES-OWN-VARS: TURTLES-OWN-VAR*
176 ARGUMENTS AND VALUES:
178 CODE-PARSED-AST: An ast as created by clnl-code-parse:parse
179 TURTLES-OWN-VAR: A symbol interned in :keyword
183 Returns the turtles own variables that get declared in the code."
184 (cdr (second (find :turtles-own code-parsed-ast :key #'car))))
186 (defun patches-own-vars (code-parsed-ast)
187 "PATCHES-OWN-VARS CODE-PARSED-AST => PATCHES-OWN-VARS
189 PATCHES-OWN-VARS: PATCHES-OWN-VAR*
191 ARGUMENTS AND VALUES:
193 CODE-PARSED-AST: An ast as created by clnl-code-parse:parse
194 PATCHES-OWN-VAR: A symbol interned in :keyword
198 Returns the turtles own variables that get declared in the code."
199 (cdr (second (find :patches-own code-parsed-ast :key #'car))))
201 (defun breeds (code-parsed-ast)
202 "BREEDS CODE-PARSED-AST => BREEDS
206 ARGUMENTS AND VALUES:
208 CODE-PARSED-AST: An ast as created by clnl-code-parse:parse
209 BREED: A symbol interned in :keyword
213 Returns the breeds that get declared in the code."
214 (mapcar #'cadadr (remove :breed code-parsed-ast :test-not #'equal :key #'car)))
216 (defun procedures (code-parsed-ast)
217 "PROCEDURES CODE-PARSED-AST => PROCEDURES
219 PROCEDURES: PROCEDURE*
220 PROCEDURE: (NAME BODY)
222 ARGUMENTS AND VALUES:
224 CODE-PARSED-AST: An ast as created by clnl-code-parse:parse
225 NAME: A symbol interned in :keyword
226 BODY: A list of lexed forms
230 Returns the procedures that were defined in the code. These can
231 then be translated into common lisp by using mapcar on the BODY, and
232 set to some function defined by NAME"
234 (lambda (proc) (cdr proc))
235 (remove-if-not (lambda (form) (find (car form) '(:to :to-report))) code-parsed-ast)))