@graffiticode/parser 0.1.0 → 0.1.1

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.
Files changed (67) hide show
  1. package/dist/api/src/config/config.d.ts +3 -0
  2. package/dist/api/src/config/config.js +4 -0
  3. package/dist/api/src/config/config.js.map +1 -0
  4. package/dist/api/src/config/index.d.ts +1 -0
  5. package/dist/api/src/config/index.js +3 -0
  6. package/dist/api/src/config/index.js.map +1 -0
  7. package/dist/api/src/lang/base-url.d.ts +7 -0
  8. package/dist/api/src/lang/base-url.js +24 -0
  9. package/dist/api/src/lang/base-url.js.map +1 -0
  10. package/dist/api/src/lang/compile.d.ts +4 -0
  11. package/dist/api/src/lang/compile.js +6 -0
  12. package/dist/api/src/lang/compile.js.map +1 -0
  13. package/dist/api/src/lang/get-asset.d.ts +4 -0
  14. package/dist/api/src/lang/get-asset.js +9 -0
  15. package/dist/api/src/lang/get-asset.js.map +1 -0
  16. package/dist/api/src/lang/index.d.ts +4 -0
  17. package/dist/api/src/lang/index.js +22 -0
  18. package/dist/api/src/lang/index.js.map +1 -0
  19. package/dist/api/src/lang/ping-lang.d.ts +5 -0
  20. package/dist/api/src/lang/ping-lang.js +31 -0
  21. package/dist/api/src/lang/ping-lang.js.map +1 -0
  22. package/dist/api/src/util.d.ts +23 -0
  23. package/dist/api/src/util.js +187 -0
  24. package/dist/api/src/util.js.map +1 -0
  25. package/dist/parser/src/ast.d.ts +58 -0
  26. package/dist/parser/src/ast.js +683 -0
  27. package/dist/parser/src/ast.js.map +1 -0
  28. package/dist/parser/src/env.d.ts +8 -0
  29. package/dist/parser/src/env.js +38 -0
  30. package/dist/parser/src/env.js.map +1 -0
  31. package/dist/parser/src/fold.d.ts +4 -0
  32. package/dist/parser/src/fold.js +217 -0
  33. package/dist/parser/src/fold.js.map +1 -0
  34. package/dist/parser/src/folder.d.ts +30 -0
  35. package/dist/parser/src/folder.js +231 -0
  36. package/dist/parser/src/folder.js.map +1 -0
  37. package/dist/parser/src/index.d.ts +5 -0
  38. package/dist/parser/src/index.js +6 -0
  39. package/dist/parser/src/index.js.map +1 -0
  40. package/dist/parser/src/parse.d.ts +56 -0
  41. package/dist/parser/src/parse.js +902 -0
  42. package/dist/parser/src/parse.js.map +1 -0
  43. package/dist/parser/src/parser.d.ts +17 -0
  44. package/dist/parser/src/parser.js +89 -0
  45. package/dist/parser/src/parser.js.map +1 -0
  46. package/dist/parser/src/parserForTests.d.ts +3 -0
  47. package/dist/parser/src/parserForTests.js +4 -0
  48. package/dist/parser/src/parserForTests.js.map +1 -0
  49. package/dist/parser/src/stringstream.d.ts +10 -0
  50. package/dist/parser/src/stringstream.js +21 -0
  51. package/dist/parser/src/stringstream.js.map +1 -0
  52. package/dist/parser/src/testing/index.d.ts +2 -0
  53. package/dist/parser/src/testing/index.js +17 -0
  54. package/dist/parser/src/testing/index.js.map +1 -0
  55. package/dist/parser/src/types.d.ts +44 -0
  56. package/dist/parser/src/types.js +2 -0
  57. package/dist/parser/src/types.js.map +1 -0
  58. package/package.json +2 -4
  59. package/src/ast.js +724 -0
  60. package/src/env.js +46 -0
  61. package/src/folder.js +244 -0
  62. package/src/parse.js +35 -935
  63. package/src/parse.ts~ +1037 -0
  64. package/src/{parser.js~ → parser.ts~} +25 -7
  65. package/src/stringstream.js +22 -0
  66. package/src/fold.js +0 -235
  67. package/src/parser.spec.js~ +0 -175
package/src/env.js ADDED
@@ -0,0 +1,46 @@
1
+ // env
2
+
3
+ export class Env {
4
+ static findWord(ctx, lexeme) {
5
+ const env = ctx.state.env;
6
+ for (let i = env.length - 1; i >= 0; i--) {
7
+ const word = env[i].lexicon[lexeme];
8
+ if (word) {
9
+ return word;
10
+ }
11
+ }
12
+ return null;
13
+ }
14
+
15
+ static addWord(ctx, lexeme, entry) {
16
+ window.gcexports.topEnv(ctx).lexicon[lexeme] = entry;
17
+ return null;
18
+ }
19
+
20
+ static addPattern(ctx, pattern) {
21
+ window.gcexports.topEnv(ctx).pattern.push(pattern);
22
+ }
23
+
24
+ static enterEnv(ctx, name) {
25
+ // recursion guard
26
+ if (ctx.state.env.length > 380) {
27
+ console.trace(
28
+ "enterEnv()",
29
+ "name=" + name,
30
+ );
31
+ // return; // just stop recursing
32
+ throw new Error("runaway recursion");
33
+ }
34
+ window.gcexports.topEnv(ctx).paramc = ctx.state.paramc;
35
+ ctx.state.env.push({
36
+ name,
37
+ lexicon: {},
38
+ pattern: []
39
+ });
40
+ }
41
+
42
+ static exitEnv(ctx) {
43
+ ctx.state.env.pop();
44
+ ctx.state.paramc = window.gcexports.topEnv(ctx).paramc;
45
+ }
46
+ }
package/src/folder.js ADDED
@@ -0,0 +1,244 @@
1
+ import assert from "assert";
2
+ import { Ast } from "./ast.js";
3
+ import { Env } from "./env.js";
4
+ import { assertErr } from "./parse.js";
5
+
6
+ // Helper to check if a value is a function
7
+ function isFunction(v) {
8
+ return v instanceof Function;
9
+ }
10
+
11
+ export class Folder {
12
+ static #nodePool;
13
+ static #ctx;
14
+ static #table;
15
+
16
+ static {
17
+ Folder.#table = {
18
+ PROG: Folder.program,
19
+ EXPRS: Folder.exprs,
20
+ PAREN: Folder.parenExpr,
21
+ IDENT: Folder.ident,
22
+ BOOL: Folder.bool,
23
+ NUM: Folder.num,
24
+ STR: Folder.str,
25
+ PARENS: Folder.unaryExpr,
26
+ APPLY: Folder.apply,
27
+ LAMBDA: Folder.lambda,
28
+ // "MUL": mul,
29
+ // "DIV": div,
30
+ // "SUB": sub,
31
+ ADD: Folder.add,
32
+ POW: Folder.pow,
33
+ MOD: Folder.mod,
34
+ CONCAT: Folder.concat,
35
+ // "OR": orelse,
36
+ // "AND": andalso,
37
+ // "NE": ne,
38
+ // "EQ": eq,
39
+ // "LT": lt,
40
+ // "GT": gt,
41
+ // "LE": le,
42
+ // "GE": ge,
43
+ NEG: Folder.neg,
44
+ LIST: Folder.list
45
+ // "CASE": caseExpr,
46
+ // "OF": ofClause,
47
+ };
48
+ }
49
+
50
+ static fold(cx, nid) {
51
+ Folder.#ctx = cx;
52
+ Folder.#nodePool = cx.state.nodePool;
53
+ Folder.#visit(nid);
54
+ }
55
+
56
+ static #visit(nid) {
57
+ const node = Folder.#nodePool[nid];
58
+ if (!node) {
59
+ return null;
60
+ }
61
+
62
+ if (node.tag === undefined) {
63
+ return []; // clean up stubs;
64
+ } else if (isFunction(Folder.#table[node.tag])) {
65
+ // Have a primitive operation so apply it to construct a new node.
66
+ const ret = Folder.#table[node.tag](node);
67
+ return ret;
68
+ }
69
+
70
+ Folder.#expr(node);
71
+ }
72
+
73
+ // BEGIN VISITOR METHODS
74
+
75
+ static program(node) {
76
+ Folder.#visit(node.elts[0]);
77
+ Ast.program(Folder.#ctx);
78
+ }
79
+
80
+ static #pushNodeStack() {
81
+ Folder.#ctx.state.nodeStackStack.push(Folder.#ctx.state.nodeStack);
82
+ Folder.#ctx.state.nodeStack = [];
83
+ }
84
+
85
+ static #popNodeStack() {
86
+ const stack = Folder.#ctx.state.nodeStack;
87
+ Folder.#ctx.state.nodeStack = Folder.#ctx.state.nodeStackStack.pop().concat(stack);
88
+ }
89
+
90
+ static list(node) {
91
+ Folder.#pushNodeStack();
92
+ for (let i = node.elts.length - 1; i >= 0; i--) {
93
+ Folder.#visit(node.elts[i]); // Keep original order.
94
+ }
95
+ Ast.list(Folder.#ctx, Folder.#ctx.state.nodeStack.length, null, true);
96
+ Folder.#popNodeStack();
97
+ }
98
+
99
+ static exprs(node) {
100
+ // Fold exprs in reverse order to get precedence right.
101
+ for (let i = node.elts.length - 1; i >= 0; i--) {
102
+ Folder.#visit(node.elts[i]); // Keep original order.
103
+ }
104
+ Folder.#ctx.state.exprc = node.elts.length;
105
+ }
106
+
107
+ static lambda(node) {
108
+ // Fold initializers and apply args.
109
+ const inits = Ast.node(Folder.#ctx, node.elts[3]).elts;
110
+ inits.forEach((init, i) => {
111
+ if (init) {
112
+ // If we have an init then fold it and replace in inits list.
113
+ Folder.fold(Folder.#ctx, Ast.intern(Folder.#ctx, init));
114
+ inits[i] = Ast.pop(Folder.#ctx);
115
+ }
116
+ });
117
+ // FIXME don't patch old node. construct a new one.
118
+ node.elts[3] = Ast.intern(Folder.#ctx, { tag: "LIST", elts: inits });
119
+ const fnId = Ast.intern(Folder.#ctx, node);
120
+ const argc = Folder.#ctx.state.nodeStack.length;
121
+ Ast.apply(Folder.#ctx, fnId, argc);
122
+ }
123
+
124
+ static apply(node) {
125
+ for (let i = node.elts.length - 1; i >= 0; i--) {
126
+ Folder.#visit(node.elts[i]);
127
+ }
128
+ Ast.applyLate(Folder.#ctx, node.elts.length);
129
+ }
130
+
131
+ static #expr(node) {
132
+ // Construct an expression node for the compiler.
133
+ Ast.name(Folder.#ctx, node.tag, node.coord);
134
+ for (let i = node.elts.length - 1; i >= 0; i--) {
135
+ Folder.#visit(node.elts[i]);
136
+ }
137
+ Ast.expr(Folder.#ctx, node.elts.length, node.coord);
138
+ }
139
+
140
+ static neg(node) {
141
+ Folder.#visit(node.elts[0]);
142
+ Ast.neg(Folder.#ctx);
143
+ }
144
+
145
+ static parenExpr(node) {
146
+ Folder.#pushNodeStack();
147
+ Folder.#visit(node.elts[0]);
148
+ Ast.parenExpr(Folder.#ctx);
149
+ Folder.#popNodeStack();
150
+ }
151
+
152
+ static unaryExpr(node) {
153
+ Folder.#visit(node.elts[0]);
154
+ Ast.unaryExpr(Folder.#ctx, node.tag);
155
+ }
156
+
157
+ static add(node) {
158
+ Folder.#visit(node.elts[0]);
159
+ Folder.#visit(node.elts[1]);
160
+ Ast.add(Folder.#ctx);
161
+ }
162
+
163
+ static pow(node) {
164
+ Folder.#visit(node.elts[0]);
165
+ Folder.#visit(node.elts[1]);
166
+ Ast.pow(Folder.#ctx);
167
+ }
168
+
169
+ static concat(node) {
170
+ Folder.#visit(node.elts[0]);
171
+ Ast.concat(Folder.#ctx);
172
+ }
173
+
174
+ static mod(node) {
175
+ Folder.#visit(node.elts[0]);
176
+ Folder.#visit(node.elts[1]);
177
+ Ast.mod(Folder.#ctx);
178
+ }
179
+
180
+ static ident(node) {
181
+ const ctx = Folder.#ctx;
182
+ const name = node.elts[0];
183
+ const word = Env.findWord(ctx, name);
184
+ if (word) {
185
+ if (word.cls === "val") {
186
+ if (word.val) {
187
+ Ast.string(ctx, word.val); // strip quotes;
188
+ } else if (word.nid) {
189
+ let wrd;
190
+ if ((wrd = Ast.node(ctx, word.nid)).tag === "LAMBDA") {
191
+ const argc = wrd.elts[0].elts.length;
192
+ Ast.apply(ctx, word.nid, argc);
193
+ } else {
194
+ Ast.push(ctx, word.nid);
195
+ }
196
+ } else if (word.name) {
197
+ Ast.push(ctx, node);
198
+ } else {
199
+ // push the original node to be resolved later.
200
+ Ast.push(ctx, node);
201
+ }
202
+ } else if (word.cls === "function") {
203
+ const elts = [];
204
+ for (let i = 0; i < word.length; i++) {
205
+ const elt = Ast.pop(ctx);
206
+ assertErr(ctx, elt, `Too few arguments for ${word.name}. Expected ${word.length}.`, node.coord);
207
+ elts.push(elt);
208
+ }
209
+ if (word.nid) {
210
+ Ast.fold(ctx, word, elts);
211
+ } else {
212
+ Ast.push(ctx, {
213
+ tag: word.name,
214
+ elts,
215
+ coord: node.coord,
216
+ });
217
+ Folder.fold(ctx, Ast.pop(ctx));
218
+ }
219
+ } else {
220
+ assert(false);
221
+ }
222
+ } else {
223
+ // assert(false, "unresolved ident "+name);
224
+ Ast.push(ctx, node);
225
+ }
226
+ }
227
+
228
+ static num(node) {
229
+ Ast.number(Folder.#ctx, node.elts[0], node.coord);
230
+ }
231
+
232
+ static str(node) {
233
+ Ast.string(Folder.#ctx, node.elts[0]);
234
+ }
235
+
236
+ static bool(node) {
237
+ Ast.bool(Folder.#ctx, node.elts[0]);
238
+ }
239
+ }
240
+
241
+ // Keep backward compatibility export
242
+ export const folder = {
243
+ fold: Folder.fold
244
+ };