@weborigami/language 0.0.69 → 0.0.71-beta.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.
- package/main.js +2 -0
- package/package.json +3 -3
- package/src/compiler/compile.js +64 -41
- package/src/compiler/origami.pegjs +40 -28
- package/src/compiler/parse.js +286 -193
- package/src/compiler/parserHelpers.js +9 -13
- package/src/runtime/WatchFilesMixin.js +1 -0
- package/src/runtime/evaluate.js +15 -15
- package/src/runtime/extensions.js +40 -14
- package/src/runtime/ops.js +64 -32
- package/src/runtime/symbols.js +3 -0
- package/src/runtime/taggedTemplate.js +9 -0
- package/test/compiler/compile.test.js +47 -0
- package/test/compiler/parse.test.js +166 -195
- package/test/compiler/stripCodeLocations.js +18 -0
- package/test/runtime/ops.test.js +16 -9
- package/test/runtime/taggedTemplate.test.js +10 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { isPlainObject } from "@weborigami/async-tree";
|
|
2
|
+
|
|
3
|
+
// For comparison purposes, strip the `location` property added by the parser.
|
|
4
|
+
export function stripCodeLocations(parseResult) {
|
|
5
|
+
if (Array.isArray(parseResult)) {
|
|
6
|
+
return parseResult.map(stripCodeLocations);
|
|
7
|
+
} else if (isPlainObject(parseResult)) {
|
|
8
|
+
const result = {};
|
|
9
|
+
for (const key in parseResult) {
|
|
10
|
+
if (key !== "location") {
|
|
11
|
+
result[key] = stripCodeLocations(parseResult[key]);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return result;
|
|
15
|
+
} else {
|
|
16
|
+
return parseResult;
|
|
17
|
+
}
|
|
18
|
+
}
|
package/test/runtime/ops.test.js
CHANGED
|
@@ -5,6 +5,20 @@ import { describe, test } from "node:test";
|
|
|
5
5
|
import { evaluate, ops } from "../../src/runtime/internal.js";
|
|
6
6
|
|
|
7
7
|
describe("ops", () => {
|
|
8
|
+
test("ops.cache looks up a value in scope and memoizes it", async () => {
|
|
9
|
+
let count = 0;
|
|
10
|
+
const tree = new ObjectTree({
|
|
11
|
+
get count() {
|
|
12
|
+
return ++count;
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
const code = createCode([ops.cache, "count", {}]);
|
|
16
|
+
const result = await evaluate.call(tree, code);
|
|
17
|
+
assert.equal(result, 1);
|
|
18
|
+
const result2 = await evaluate.call(tree, code);
|
|
19
|
+
assert.equal(result2, 1);
|
|
20
|
+
});
|
|
21
|
+
|
|
8
22
|
test("ops.concat concatenates tree value text", async () => {
|
|
9
23
|
const scope = new ObjectTree({
|
|
10
24
|
name: "world",
|
|
@@ -47,20 +61,13 @@ describe("ops", () => {
|
|
|
47
61
|
message: "Hello",
|
|
48
62
|
});
|
|
49
63
|
|
|
50
|
-
const code = createCode([ops.lambda,
|
|
64
|
+
const code = createCode([ops.lambda, ["_"], [ops.scope, "message"]]);
|
|
51
65
|
|
|
52
66
|
const fn = await evaluate.call(scope, code);
|
|
53
67
|
const result = await fn.call(scope);
|
|
54
68
|
assert.equal(result, "Hello");
|
|
55
69
|
});
|
|
56
70
|
|
|
57
|
-
test("ops.lambda adds input to scope as `_`", async () => {
|
|
58
|
-
const code = createCode([ops.lambda, null, [ops.scope, "_"]]);
|
|
59
|
-
const fn = await evaluate.call(null, code);
|
|
60
|
-
const result = await fn("Hello");
|
|
61
|
-
assert.equal(result, "Hello");
|
|
62
|
-
});
|
|
63
|
-
|
|
64
71
|
test("ops.lambda adds input parameters to scope", async () => {
|
|
65
72
|
const code = createCode([
|
|
66
73
|
ops.lambda,
|
|
@@ -73,7 +80,7 @@ describe("ops", () => {
|
|
|
73
80
|
});
|
|
74
81
|
|
|
75
82
|
test("ops.lambda function can reference itself with @recurse", async () => {
|
|
76
|
-
const code = createCode([ops.lambda,
|
|
83
|
+
const code = createCode([ops.lambda, ["_"], [ops.scope, "@recurse"]]);
|
|
77
84
|
const fn = await evaluate.call(null, code);
|
|
78
85
|
const result = await fn();
|
|
79
86
|
// We're expecting the function to return itself, but testing recursion is
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { describe, test } from "node:test";
|
|
3
|
+
import taggedTemplate from "../../src/runtime/taggedTemplate.js";
|
|
4
|
+
|
|
5
|
+
describe("taggedTemplate", () => {
|
|
6
|
+
test("joins strings and values together", () => {
|
|
7
|
+
const result = taggedTemplate`a ${"b"} c`;
|
|
8
|
+
assert.equal(result, "a b c");
|
|
9
|
+
});
|
|
10
|
+
});
|