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)
12 (list :name global :type :reporter :macro `(lambda () ',(intern (symbol-name global) clnl:*model-package*))))
14 (defun breed->prims (breed-list)
16 ((plural-name (symbol-name (car breed-list))))
18 (list :name (car breed-list))
19 (list :name (intern (format nil "~A-HERE" plural-name) :keyword))
20 (list :name (intern (format nil "CREATE-~A" plural-name) :keyword) :args '(:number :command-block)))))
22 (defun parse (lexed-ast &optional external-globals)
23 "PARSE LEXED-AST &optional EXTERNAL-GLOBALS => AST, PRIMS
27 LEXED-AST: An ambigious ast
28 EXTERNAL-GLOBALS: A list of symbols in keyword package
29 AST: An unambigious ast that represents the code block of a model
30 PRIMS: Primitives that can be sent to the parser and transpiler
34 PARSE takes a ambigious LEXED-AST and converts it to an unambigious one.
35 It also returns the primitives that are defined in the code file, including
36 ones generated from the EXTERNAL-GLOBALS, that can then be passed to both
37 the parser and the transpiler.
39 EXTERNAL-GLOBALS is a list of symbols representing global variables that
40 are not defined within the code. Normally these come from widgets defined
41 in the model file, but could arguably come from elsewhere.
43 This parser, unlike CLNL-PARSE:PARSE, should not be fed into the transpiler.
45 Rather, the ast that's returned can be queried with other functions included
46 in the CLNL-CODE-PARSER package to tease out necessary information. Some of
47 those things will involve code blocks that can then be transpiled."
51 (mapcar #'global->prim external-globals)
52 (procedures->prims lexed-ast)))
53 (parsed (parse-internal lexed-ast)))
58 (defun procedures->prims (lexed-ast)
61 ; We'll need argument handling here sometime :)
62 ((eql :to (car lexed-ast)) (cons (list :name (cadr lexed-ast)) (procedures->prims (cdr lexed-ast))))
63 (t (procedures->prims (cdr lexed-ast)))))
65 (defun parse-internal (lexed-ast)
67 ((not lexed-ast) *dynamic-prims*)
68 ((eql :to (car lexed-ast)) (parse-procedure lexed-ast))
69 ((find (car lexed-ast) '(:globals :turtles-own :patches-own))
70 (parse-with-unevaluated-list lexed-ast))
71 ((eql (car lexed-ast) :breed) (parse-breed lexed-ast))))
73 ; Due to the non expression style syntax of procedures, this must be special cased
74 (defun parse-procedure (tokens)
75 (multiple-value-bind (in-block after-block) (find-end tokens)
80 (clnl-parser:parse (cddr in-block) *dynamic-prims*))
81 (parse-internal after-block))))
83 (defun find-end (tokens)
85 ((not tokens) (error "Failed to find end"))
86 ((eql :end (car tokens)) (values nil (cdr tokens)))
87 (t (multiple-value-bind (in-block after-block) (find-end (cdr tokens))
88 (values (cons (car tokens) in-block) after-block)))))
90 ; This is a special case but left with a little wiggle room for future
91 ; enhancements, like code blocks
92 (defun parse-with-unevaluated-list (lexed-ast)
93 (when (not (eql :[ (cadr lexed-ast))) (error "Expected list literal here"))
94 (multiple-value-bind (in-list after-list) (find-closing-bracket (cddr lexed-ast))
96 (list (car lexed-ast) (cons :list-literal in-list))
98 ((*dynamic-prims* (append (mapcar #'global->prim in-list) *dynamic-prims*)))
99 (parse-internal after-list)))))
101 (defun parse-breed (lexed-ast)
102 (when (not (eql :[ (cadr lexed-ast))) (error "Expected list literal here"))
103 (multiple-value-bind (in-list after-list) (find-closing-bracket (cddr lexed-ast))
105 (list (car lexed-ast) (cons :list-literal in-list))
107 ((*dynamic-prims* (append (breed->prims in-list) *dynamic-prims*)))
108 (parse-internal after-list)))))
110 (defun find-closing-bracket (tokens)
112 ((not tokens) (error "Failed to find a matching closing bracket"))
113 ((eql :] (car tokens)) (values nil (cdr tokens)))
114 ((eql :[ (car tokens)) (error "Expected name or ]"))
115 (t (multiple-value-bind (in-block after-block) (find-closing-bracket (cdr tokens))
116 (values (cons (car tokens) in-block) after-block)))))
118 (defun globals (code-parsed-ast)
119 "GLOBALS MODEL => GLOBALS
123 ARGUMENTS AND VALUES:
125 MODEL: An ast as created by clnl-code-parse:parse
126 GLOBAL: A symbol interned in :keyword
130 Returns the globals that get declared in the code."
132 (lambda (global) (list (intern (symbol-name global) :keyword) 0d0))
133 (cdr (second (find :globals code-parsed-ast :key #'car)))))