@graffiticode/parser 1.4.0 → 1.4.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.
@@ -6,7 +6,9 @@
6
6
  "Bash(npm test:*)",
7
7
  "Bash(npm publish:*)",
8
8
  "Bash(grep:*)",
9
- "Bash(npm install:*)"
9
+ "Bash(npm install:*)",
10
+ "Bash(NODE_OPTIONS=--experimental-vm-modules node -e \"\nimport { parser } from './src/parser.js';\nimport { unparse } from './src/unparse.js';\nimport { lexicon as basisLexicon } from '@graffiticode/basis';\n\nconst src = 'case x of 1: \\\\\"one\\\\\" 2: \\\\\"two\\\\\" end..';\nconsole.log\\('Input:', src\\);\nconst ast = await parser.parse\\(0, src, basisLexicon\\);\nconsole.log\\('AST:', JSON.stringify\\(ast, null, 2\\)\\);\nconst result = unparse\\(ast, basisLexicon\\);\nconsole.log\\('Unparsed:', result\\);\n\")",
11
+ "Bash(NODE_OPTIONS=--experimental-vm-modules node -e \"\nimport { parser } from './src/parser.js';\nimport { unparse } from './src/unparse.js';\nimport { lexicon as basisLexicon } from '@graffiticode/basis';\n\n// Test nested case-of\nconst src1 = 'case x of 1: case y of 3: \\\\\"a\\\\\" 4: \\\\\"b\\\\\" end 2: \\\\\"two\\\\\" end..';\nconsole.log\\('Input:', src1\\);\ntry {\n const ast1 = await parser.parse\\(0, src1, basisLexicon\\);\n console.log\\('Unparsed:', unparse\\(ast1, basisLexicon\\)\\);\n} catch\\(e\\) { console.log\\('Error:', e.message\\); }\n\nconsole.log\\('---'\\);\n\n// Test with expression as case target\nconst src2 = 'case add 1 2 of 3: \\\\\"yes\\\\\" 4: \\\\\"no\\\\\" end..';\nconsole.log\\('Input:', src2\\);\ntry {\n const ast2 = await parser.parse\\(0, src2, basisLexicon\\);\n console.log\\('AST:', JSON.stringify\\(ast2, null, 2\\)\\);\n console.log\\('Unparsed:', unparse\\(ast2, basisLexicon\\)\\);\n} catch\\(e\\) { console.log\\('Error:', e.message\\); }\n\nconsole.log\\('---'\\);\n\n// Test with identifier patterns\nconst src3 = 'case x of y: y end..';\nconsole.log\\('Input:', src3\\);\ntry {\n const ast3 = await parser.parse\\(0, src3, basisLexicon\\);\n console.log\\('AST:', JSON.stringify\\(ast3, null, 2\\)\\);\n console.log\\('Unparsed:', unparse\\(ast3, basisLexicon\\)\\);\n} catch\\(e\\) { console.log\\('Error:', e.message\\); }\n\nconsole.log\\('---'\\);\n\n// Test case-of inside a let or other expression\nconst src4 = 'let x = 5 in case x of 5: \\\\\"five\\\\\" end..';\nconsole.log\\('Input:', src4\\);\ntry {\n const ast4 = await parser.parse\\(0, src4, basisLexicon\\);\n console.log\\('Unparsed:', unparse\\(ast4, basisLexicon\\)\\);\n} catch\\(e\\) { console.log\\('Error:', e.message\\); }\n\nconsole.log\\('---'\\);\n\n// Test case-of as argument to a function\nconst src5 = 'add \\(case x of 1: 10 2: 20 end\\) 5..';\nconsole.log\\('Input:', src5\\);\ntry {\n const ast5 = await parser.parse\\(0, src5, basisLexicon\\);\n console.log\\('Unparsed:', unparse\\(ast5, basisLexicon\\)\\);\n} catch\\(e\\) { console.log\\('Error:', e.message\\); }\n\")"
10
12
  ]
11
13
  }
12
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graffiticode/parser",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -46,4 +46,33 @@ describe("folder", () => {
46
46
  expect(n2.tag).toBe("NUM");
47
47
  expect(n2.elts[0]).toBe("3");
48
48
  });
49
+
50
+ it("should pass 'pow 2 3' through as POW node", () => {
51
+ const ctx = {
52
+ state: {
53
+ nodePool: ["unused"],
54
+ nodeStack: [],
55
+ nodeStackStack: [],
56
+ nodeMap: {},
57
+ env: [{ name: "global", lexicon: {} }]
58
+ }
59
+ };
60
+
61
+ const n1Id = Ast.intern(ctx, { tag: "NUM", elts: ["2"] });
62
+ const n2Id = Ast.intern(ctx, { tag: "NUM", elts: ["3"] });
63
+ const powNodeId = Ast.intern(ctx, { tag: "POW", elts: [n1Id, n2Id] });
64
+
65
+ Folder.fold(ctx, powNodeId);
66
+ const resultId = ctx.state.nodeStack.pop();
67
+ const resultNode = ctx.state.nodePool[resultId];
68
+
69
+ expect(resultNode.tag).toBe("POW");
70
+ expect(resultNode.elts.length).toBe(2);
71
+ const n1 = Ast.node(ctx, resultNode.elts[0]);
72
+ const n2 = Ast.node(ctx, resultNode.elts[1]);
73
+ expect(n1.tag).toBe("NUM");
74
+ expect(n1.elts[0]).toBe("2");
75
+ expect(n2.tag).toBe("NUM");
76
+ expect(n2.elts[0]).toBe("3");
77
+ });
49
78
  });
package/src/unparse.js CHANGED
@@ -166,10 +166,10 @@ function unparseNode(node, lexicon, indent = 0, options = {}) {
166
166
  return "";
167
167
 
168
168
  case "LAMBDA":
169
- // Lambda function
170
- if (node.elts && node.elts.length >= 3) {
171
- const params = node.elts[1];
172
- const body = node.elts[2];
169
+ // Lambda function: elts = [param_names_list, body, pattern_list, init_list]
170
+ if (node.elts && node.elts.length >= 2) {
171
+ const params = node.elts[0];
172
+ const body = node.elts[1];
173
173
 
174
174
  // Extract parameter names
175
175
  let paramStr = "";