@graffiticode/parser 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@graffiticode/parser",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "exports": {
9
+ "./parser": "./src/index.js"
10
+ },
11
+ "devDependencies": {
12
+ "eslint": "^8.38.0",
13
+ "jest": "^29.5.0",
14
+ "supertest": "^6.3.3"
15
+ },
16
+ "scripts": {
17
+ "lint": "eslint src/",
18
+ "lint:fix": "eslint --fix src/",
19
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest"
20
+ },
21
+ "keywords": [],
22
+ "author": "",
23
+ "license": "ISC",
24
+ "description": "",
25
+ "dependencies": {
26
+ }
27
+ }
package/src/fold.js ADDED
@@ -0,0 +1,235 @@
1
+ import assert from "assert";
2
+ import { Ast, env } from "./parse.js";
3
+
4
+ export const folder = (function () {
5
+ const table = {
6
+ PROG: program,
7
+ EXPRS: exprs,
8
+ PAREN: parenExpr,
9
+ IDENT: ident,
10
+ BOOL: bool,
11
+ NUM: num,
12
+ STR: str,
13
+ PARENS: unaryExpr,
14
+ APPLY: apply,
15
+ LAMBDA: lambda,
16
+ // "MUL": mul,
17
+ // "DIV": div,
18
+ // "SUB": sub,
19
+ ADD: add,
20
+ POW: pow,
21
+ MOD: mod,
22
+ CONCAT: concat,
23
+ // "OR": orelse,
24
+ // "AND": andalso,
25
+ // "NE": ne,
26
+ // "EQ": eq,
27
+ // "LT": lt,
28
+ // "GT": gt,
29
+ // "LE": le,
30
+ // "GE": ge,
31
+ NEG: neg,
32
+ LIST: list
33
+ // "CASE": caseExpr,
34
+ // "OF": ofClause,
35
+ };
36
+
37
+ let nodePool;
38
+ let ctx;
39
+
40
+ function fold(cx, nid) {
41
+ ctx = cx;
42
+ nodePool = ctx.state.nodePool;
43
+ visit(nid);
44
+ }
45
+
46
+ function visit(nid) {
47
+ const node = nodePool[nid];
48
+ if (!node) {
49
+ return null;
50
+ }
51
+ if (node.tag === undefined) {
52
+ return []; // clean up stubs;
53
+ } else if (isFunction(table[node.tag])) {
54
+ // Have a primitive operation so apply it to construct a new node.
55
+ const ret = table[node.tag](node);
56
+ return ret;
57
+ }
58
+ expr(node);
59
+ }
60
+
61
+ function isFunction(v) {
62
+ return v instanceof Function;
63
+ }
64
+
65
+ // BEGIN VISITOR METHODS
66
+
67
+ function program(node) {
68
+ visit(node.elts[0]);
69
+ Ast.program(ctx);
70
+ }
71
+
72
+ function pushNodeStack(ctx) {
73
+ ctx.state.nodeStackStack.push(ctx.state.nodeStack);
74
+ ctx.state.nodeStack = [];
75
+ }
76
+ function popNodeStack(ctx) {
77
+ const stack = ctx.state.nodeStack;
78
+ ctx.state.nodeStack = ctx.state.nodeStackStack.pop().concat(stack);
79
+ }
80
+
81
+ function list(node) {
82
+ // Fold list
83
+ // for (var i = 0; i < node.elts.length; i++) {
84
+ // visit(node.elts[i]);
85
+ // }
86
+ pushNodeStack(ctx);
87
+ for (let i = node.elts.length - 1; i >= 0; i--) {
88
+ visit(node.elts[i]); // Keep original order.
89
+ }
90
+ Ast.list(ctx, ctx.state.nodeStack.length, null, true);
91
+ popNodeStack(ctx);
92
+ }
93
+
94
+ function exprs(node) {
95
+ // Fold exprs in reverse order to get precedence right.
96
+ for (let i = node.elts.length - 1; i >= 0; i--) {
97
+ visit(node.elts[i]); // Keep original order.
98
+ }
99
+ ctx.state.exprc = node.elts.length;
100
+ }
101
+
102
+ function lambda(node) {
103
+ // Fold initializers and apply args.
104
+ const inits = Ast.node(ctx, node.elts[3]).elts;
105
+ inits.forEach((init, i) => {
106
+ if (init) {
107
+ // If we have an init then fold it and replace in inits list.
108
+ folder.fold(ctx, Ast.intern(ctx, init));
109
+ inits[i] = Ast.pop(ctx);
110
+ }
111
+ });
112
+ // FIXME don't patch old node. construct a new one.
113
+ node.elts[3] = Ast.intern(ctx, { tag: "LIST", elts: inits });
114
+ const fnId = Ast.intern(ctx, node);
115
+ const argc = ctx.state.nodeStack.length;
116
+ Ast.apply(ctx, fnId, argc);
117
+ }
118
+
119
+ function apply(node) {
120
+ for (let i = node.elts.length - 1; i >= 0; i--) {
121
+ visit(node.elts[i]);
122
+ }
123
+ Ast.applyLate(ctx, node.elts.length);
124
+ }
125
+
126
+ function expr(node) {
127
+ // Construct an expression node for the compiler.
128
+ Ast.name(ctx, node.tag);
129
+ for (let i = node.elts.length - 1; i >= 0; i--) {
130
+ visit(node.elts[i]);
131
+ }
132
+ Ast.expr(ctx, node.elts.length);
133
+ }
134
+
135
+ function neg(node) {
136
+ visit(node.elts[0]);
137
+ Ast.neg(ctx);
138
+ }
139
+
140
+ function parenExpr(node) {
141
+ pushNodeStack(ctx);
142
+ visit(node.elts[0]);
143
+ Ast.parenExpr(ctx);
144
+ popNodeStack(ctx);
145
+ }
146
+
147
+ function unaryExpr(node) {
148
+ visit(node.elts[0]);
149
+ Ast.unaryExpr(ctx, node.tag);
150
+ }
151
+
152
+ function add(node) {
153
+ visit(node.elts[0]);
154
+ visit(node.elts[1]);
155
+ Ast.add(ctx);
156
+ }
157
+
158
+ function pow(node) {
159
+ visit(node.elts[0]);
160
+ visit(node.elts[1]);
161
+ Ast.pow(ctx);
162
+ }
163
+
164
+ function concat(node) {
165
+ visit(node.elts[0]);
166
+ Ast.concat(ctx);
167
+ }
168
+
169
+ function mod(node) {
170
+ visit(node.elts[0]);
171
+ visit(node.elts[1]);
172
+ Ast.mod(ctx);
173
+ }
174
+
175
+ function ident(node) {
176
+ const name = node.elts[0];
177
+ const word = env.findWord(ctx, name);
178
+ if (word) {
179
+ if (word.cls === "val") {
180
+ if (word.val) {
181
+ Ast.string(ctx, word.val); // strip quotes;
182
+ } else if (word.nid) {
183
+ let wrd;
184
+ if ((wrd = Ast.node(ctx, word.nid)).tag === "LAMBDA") {
185
+ const argc = wrd.elts[0].elts.length;
186
+ Ast.apply(ctx, word.nid, argc);
187
+ } else {
188
+ Ast.push(ctx, word.nid);
189
+ }
190
+ } else if (word.name) {
191
+ Ast.push(ctx, node);
192
+ } else {
193
+ // push the original node to be resolved later.
194
+ Ast.push(ctx, node);
195
+ }
196
+ } else if (word.cls === "function") {
197
+ const elts = [];
198
+ for (let i = 0; i < word.length; i++) {
199
+ const elt = Ast.pop(ctx);
200
+ elts.push(elt);
201
+ }
202
+ if (word.nid) {
203
+ Ast.fold(ctx, word, elts);
204
+ } else {
205
+ Ast.push(ctx, {
206
+ tag: word.name,
207
+ elts
208
+ });
209
+ folder.fold(ctx, Ast.pop(ctx));
210
+ }
211
+ } else {
212
+ assert(false);
213
+ }
214
+ } else {
215
+ // assert(false, "unresolved ident "+name);
216
+ Ast.push(ctx, node);
217
+ }
218
+ }
219
+
220
+ function num(node) {
221
+ Ast.number(ctx, node.elts[0]);
222
+ }
223
+
224
+ function str(node) {
225
+ Ast.string(ctx, node.elts[0]);
226
+ }
227
+
228
+ function bool(node) {
229
+ Ast.bool(ctx, node.elts[0]);
230
+ }
231
+
232
+ return {
233
+ fold
234
+ };
235
+ }());
package/src/index.js ADDED
@@ -0,0 +1 @@
1
+ export { parser } from "./parser.js";