Improve parser - generate prims from globals
[clnl] / src / main / code-parse.lisp
1 (in-package #:clnl-code-parser)
2
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
7 ; other things
8
9 (defvar *dynamic-prims* nil)
10 (defun global->prim (global) (list :name global))
11
12 (defun parse (lexed-ast &optional external-globals)
13  "PARSE LEXED-AST &optional EXTERNAL-GLOBALS => AST
14
15 ARGUMENTS AND VALUES:
16
17   LEXED-AST: An ambigious ast
18   EXTERNAL-GLOBALS: A list of symbols in keyword package
19   AST: An unambigious ast that represents the code block of a model
20
21 DESCRIPTION:
22
23   PARSE takes a ambigious LEXED-AST and converts it to an unambigious one.
24
25   EXTERNAL-GLOBALS is a list of symbols representing global variables that
26   are not defined within the code.  Normally these come from widgets defined
27   in the model file, but could arguably come from elsewhere.
28
29   This parser, unlike CLNL-PARSE:PARSE, should not be fed into the transpiler.
30
31   Rather, the ast that's returned can be queried with other functions included
32   in the CLNL-CODE-PARSER package to tease out necessary information.  Some of
33   those things will involve code blocks that can then be transpiled."
34  (let
35   ((*dynamic-prims* (mapcar #'global->prim external-globals)))
36   (parse-internal lexed-ast)))
37
38 (defun parse-internal (lexed-ast)
39  (cond
40   ((not lexed-ast) nil)
41   ((eql :to (car lexed-ast)) (parse-procedure lexed-ast))
42   ((find (car lexed-ast) '(:globals :turtles-own :patches-own))
43    (parse-with-unevaluated-list lexed-ast))
44   ((eql (car lexed-ast) :breed) (parse-breed lexed-ast))))
45
46 ; Due to the non expression style syntax of procedures, this must be special cased
47 (defun parse-procedure (tokens)
48  (multiple-value-bind (in-block after-block) (find-end tokens)
49   (cons
50    (list
51     (first in-block)
52     (second in-block)
53     (clnl-parser:parse (cddr in-block) *dynamic-prims*))
54    (parse-internal after-block))))
55
56 (defun find-end (tokens)
57  (cond
58   ((not tokens) (error "Failed to find end"))
59   ((eql :end (car tokens)) (values nil (cdr tokens)))
60   (t (multiple-value-bind (in-block after-block) (find-end (cdr tokens))
61       (values (cons (car tokens) in-block) after-block)))))
62
63 ; This is a special case but left with a little wiggle room for future
64 ; enhancements, like code blocks
65 (defun parse-with-unevaluated-list (lexed-ast)
66  (when (not (eql :[ (cadr lexed-ast))) (error "Expected list literal here"))
67  (multiple-value-bind (in-list after-list) (find-closing-bracket (cddr lexed-ast))
68   (cons
69    (list (car lexed-ast) (cons :list-literal in-list))
70    (let
71     ((*dynamic-prims* (append (mapcar #'global->prim in-list) *dynamic-prims*)))
72     (parse-internal after-list)))))
73
74 (defun parse-breed (lexed-ast)
75  (when (not (eql :[ (cadr lexed-ast))) (error "Expected list literal here"))
76  (multiple-value-bind (in-list after-list) (find-closing-bracket (cddr lexed-ast))
77   (cons
78    (list (car lexed-ast) (cons :list-literal in-list))
79    (parse-internal after-list))))
80
81 (defun find-closing-bracket (tokens)
82  (cond
83   ((not tokens) (error "Failed to find a matching closing bracket"))
84   ((eql :] (car tokens)) (values nil (cdr tokens)))
85   ((eql :[ (car tokens)) (error "Expected name or ]"))
86   (t (multiple-value-bind (in-block after-block) (find-closing-bracket (cdr tokens))
87       (values (cons (car tokens) in-block) after-block)))))
88
89 (defun globals (code-parsed-ast)
90  "GLOBALS MODEL => GLOBALS
91
92   GLOBALS: GLOBAL*
93
94 ARGUMENTS AND VALUES:
95
96   MODEL: An ast as created by clnl-code-parse:parse
97   GLOBAL: A symbol interned in clnl:*model-package*
98
99 DESCRIPTION:
100
101   Returns the globals that get declared in the code."
102  (mapcar
103   (lambda (global) (list (symbol-name global) 0d0))
104   (cdr (second (find :globals code-parsed-ast :key #'car)))))