@weborigami/language 0.2.0 → 0.2.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/package.json +3 -3
- package/src/compiler/origami.pegjs +104 -19
- package/src/compiler/parse.js +1352 -597
- package/src/compiler/parserHelpers.js +38 -22
- package/src/runtime/ops.js +125 -15
- package/test/compiler/parse.test.js +150 -5
- package/test/runtime/ops.test.js +144 -26
package/test/runtime/ops.test.js
CHANGED
|
@@ -5,12 +5,42 @@ 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.addition adds two numbers", async () => {
|
|
9
|
+
assert.strictEqual(ops.addition(2, 2), 4);
|
|
10
|
+
assert.strictEqual(ops.addition(2, true), 3);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("ops.addition concatenates two strings", async () => {
|
|
14
|
+
assert.strictEqual(ops.addition("hello ", "everyone"), "hello everyone");
|
|
15
|
+
assert.strictEqual(
|
|
16
|
+
ops.addition("2001", ": A Space Odyssey"),
|
|
17
|
+
"2001: A Space Odyssey"
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
|
|
8
21
|
test("ops.array creates an array", async () => {
|
|
9
22
|
const code = createCode([ops.array, 1, 2, 3]);
|
|
10
23
|
const result = await evaluate.call(null, code);
|
|
11
24
|
assert.deepEqual(result, [1, 2, 3]);
|
|
12
25
|
});
|
|
13
26
|
|
|
27
|
+
test("ops.bitwiseAnd", () => {
|
|
28
|
+
assert.strictEqual(ops.bitwiseAnd(5, 3), 1);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("ops.bitwiseNot", () => {
|
|
32
|
+
assert.strictEqual(ops.bitwiseNot(5), -6);
|
|
33
|
+
assert.strictEqual(ops.bitwiseNot(-3), 2);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("ops.bitwiseOr", () => {
|
|
37
|
+
assert.strictEqual(ops.bitwiseOr(5, 3), 7);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("ops.bitwiseXor", () => {
|
|
41
|
+
assert.strictEqual(ops.bitwiseXor(5, 3), 6);
|
|
42
|
+
});
|
|
43
|
+
|
|
14
44
|
test("ops.builtin gets a value from the top of the scope chain", async () => {
|
|
15
45
|
const root = new ObjectTree({
|
|
16
46
|
a: 1,
|
|
@@ -22,6 +52,12 @@ describe("ops", () => {
|
|
|
22
52
|
assert.strictEqual(result, 1);
|
|
23
53
|
});
|
|
24
54
|
|
|
55
|
+
test("ops.comma returns the last value", async () => {
|
|
56
|
+
const code = createCode([ops.comma, 1, 2, 3]);
|
|
57
|
+
const result = await evaluate.call(null, code);
|
|
58
|
+
assert.strictEqual(result, 3);
|
|
59
|
+
});
|
|
60
|
+
|
|
25
61
|
test("ops.concat concatenates tree value text", async () => {
|
|
26
62
|
const scope = new ObjectTree({
|
|
27
63
|
name: "world",
|
|
@@ -43,12 +79,24 @@ describe("ops", () => {
|
|
|
43
79
|
assert.strictEqual(await ops.conditional(false, errorFn, trueFn), true);
|
|
44
80
|
});
|
|
45
81
|
|
|
46
|
-
test("ops.
|
|
47
|
-
assert.strictEqual(
|
|
48
|
-
assert.strictEqual(
|
|
49
|
-
assert.strictEqual(
|
|
50
|
-
assert.strictEqual(
|
|
51
|
-
|
|
82
|
+
test("ops.division divides two numbers", async () => {
|
|
83
|
+
assert.strictEqual(ops.division(12, 2), 6);
|
|
84
|
+
assert.strictEqual(ops.division(3, 2), 1.5);
|
|
85
|
+
assert.strictEqual(ops.division(6, "3"), 2);
|
|
86
|
+
assert.strictEqual(ops.division(2, 0), Infinity);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("ops.equal", () => {
|
|
90
|
+
assert(ops.equal(1, 1));
|
|
91
|
+
assert(!ops.equal(1, 2));
|
|
92
|
+
assert(ops.equal("1", 1));
|
|
93
|
+
assert(ops.equal("1", "1"));
|
|
94
|
+
assert(ops.equal(null, undefined));
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("ops.exponentiation", () => {
|
|
98
|
+
assert.strictEqual(ops.exponentiation(2, 3), 8);
|
|
99
|
+
assert.strictEqual(ops.exponentiation(2, 0), 1);
|
|
52
100
|
});
|
|
53
101
|
|
|
54
102
|
test("ops.external looks up a value in scope and memoizes it", async () => {
|
|
@@ -65,6 +113,18 @@ describe("ops", () => {
|
|
|
65
113
|
assert.strictEqual(result2, 1);
|
|
66
114
|
});
|
|
67
115
|
|
|
116
|
+
test("ops.greaterThan", () => {
|
|
117
|
+
assert(ops.greaterThan(5, 3));
|
|
118
|
+
assert(!ops.greaterThan(3, 3));
|
|
119
|
+
assert(ops.greaterThan("ab", "aa"));
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("ops.greaterThanOrEqual", () => {
|
|
123
|
+
assert(ops.greaterThanOrEqual(5, 3));
|
|
124
|
+
assert(ops.greaterThanOrEqual(3, 3));
|
|
125
|
+
assert(ops.greaterThanOrEqual("ab", "aa"));
|
|
126
|
+
});
|
|
127
|
+
|
|
68
128
|
test("ops.inherited searches inherited scope", async () => {
|
|
69
129
|
const parent = new ObjectTree({
|
|
70
130
|
a: 1, // This is the inherited value we want
|
|
@@ -109,6 +169,18 @@ describe("ops", () => {
|
|
|
109
169
|
assert.strictEqual(result, "yx");
|
|
110
170
|
});
|
|
111
171
|
|
|
172
|
+
test("ops.lessThan", () => {
|
|
173
|
+
assert(!ops.lessThan(5, 3));
|
|
174
|
+
assert(!ops.lessThan(3, 3));
|
|
175
|
+
assert(ops.lessThan("aa", "ab"));
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("ops.lessThanOrEqual", () => {
|
|
179
|
+
assert(!ops.lessThanOrEqual(5, 3));
|
|
180
|
+
assert(ops.lessThanOrEqual(3, 3));
|
|
181
|
+
assert(ops.lessThanOrEqual("aa", "ab"));
|
|
182
|
+
});
|
|
183
|
+
|
|
112
184
|
test("ops.logicalAnd", async () => {
|
|
113
185
|
assert.strictEqual(await ops.logicalAnd(true, trueFn), true);
|
|
114
186
|
assert.strictEqual(await ops.logicalAnd(true, falseFn), false);
|
|
@@ -141,20 +213,27 @@ describe("ops", () => {
|
|
|
141
213
|
assert.strictEqual(await ops.logicalOr(true, errorFn), true);
|
|
142
214
|
});
|
|
143
215
|
|
|
144
|
-
test("ops.
|
|
145
|
-
assert.strictEqual(
|
|
146
|
-
assert.strictEqual(
|
|
147
|
-
assert.strictEqual(
|
|
148
|
-
assert.strictEqual(
|
|
149
|
-
assert.strictEqual(await ops.notEqual(null, undefined), false);
|
|
216
|
+
test("ops.multiplication multiplies two numbers", async () => {
|
|
217
|
+
assert.strictEqual(ops.multiplication(3, 4), 12);
|
|
218
|
+
assert.strictEqual(ops.multiplication(-3, 4), -12);
|
|
219
|
+
assert.strictEqual(ops.multiplication("3", 2), 6);
|
|
220
|
+
assert.strictEqual(ops.multiplication("foo", 2), NaN);
|
|
150
221
|
});
|
|
151
222
|
|
|
152
|
-
test("ops.
|
|
153
|
-
assert
|
|
154
|
-
assert
|
|
155
|
-
assert
|
|
156
|
-
assert
|
|
157
|
-
assert
|
|
223
|
+
test("ops.notEqual", () => {
|
|
224
|
+
assert(!ops.notEqual(1, 1));
|
|
225
|
+
assert(ops.notEqual(1, 2));
|
|
226
|
+
assert(!ops.notEqual("1", 1));
|
|
227
|
+
assert(!ops.notEqual("1", "1"));
|
|
228
|
+
assert(!ops.notEqual(null, undefined));
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test("ops.notStrictEqual", () => {
|
|
232
|
+
assert(!ops.notStrictEqual(1, 1));
|
|
233
|
+
assert(ops.notStrictEqual(1, 2));
|
|
234
|
+
assert(ops.notStrictEqual("1", 1));
|
|
235
|
+
assert(!ops.notStrictEqual("1", "1"));
|
|
236
|
+
assert(ops.notStrictEqual(null, undefined));
|
|
158
237
|
});
|
|
159
238
|
|
|
160
239
|
test("ops.nullishCoalescing", async () => {
|
|
@@ -196,14 +275,53 @@ describe("ops", () => {
|
|
|
196
275
|
assert.deepEqual(result, ["Hello", 1, "WORLD"]);
|
|
197
276
|
});
|
|
198
277
|
|
|
199
|
-
test("ops.
|
|
200
|
-
assert.strictEqual(
|
|
201
|
-
assert.strictEqual(
|
|
202
|
-
assert.strictEqual(
|
|
203
|
-
assert.strictEqual(
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
278
|
+
test("ops.remainder calculates the remainder of two numbers", async () => {
|
|
279
|
+
assert.strictEqual(ops.remainder(13, 5), 3);
|
|
280
|
+
assert.strictEqual(ops.remainder(-13, 5), -3);
|
|
281
|
+
assert.strictEqual(ops.remainder(4, 2), 0);
|
|
282
|
+
assert.strictEqual(ops.remainder(-4, 2), -0);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test("ops.shiftLeft", () => {
|
|
286
|
+
assert.strictEqual(ops.shiftLeft(5, 2), 20);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
test("ops.shiftRightSigned", () => {
|
|
290
|
+
assert.strictEqual(ops.shiftRightSigned(20, 2), 5);
|
|
291
|
+
assert.strictEqual(ops.shiftRightSigned(-20, 2), -5);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test("ops.shiftRightUnsigned", () => {
|
|
295
|
+
assert.strictEqual(ops.shiftRightUnsigned(20, 2), 5);
|
|
296
|
+
assert.strictEqual(ops.shiftRightUnsigned(-5, 2), 1073741822);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test("ops.strictEqual", () => {
|
|
300
|
+
assert(ops.strictEqual(1, 1));
|
|
301
|
+
assert(!ops.strictEqual(1, 2));
|
|
302
|
+
assert(!ops.strictEqual("1", 1));
|
|
303
|
+
assert(ops.strictEqual("1", "1"));
|
|
304
|
+
assert(!ops.strictEqual(null, undefined));
|
|
305
|
+
assert(ops.strictEqual(null, null));
|
|
306
|
+
assert(ops.strictEqual(undefined, undefined));
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
test("ops.subtraction subtracts two numbers", async () => {
|
|
310
|
+
assert.strictEqual(ops.subtraction(5, 3), 2);
|
|
311
|
+
assert.strictEqual(ops.subtraction(3.5, 5), -1.5);
|
|
312
|
+
assert.strictEqual(ops.subtraction(5, "hello"), NaN);
|
|
313
|
+
assert.strictEqual(ops.subtraction(5, true), 4);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
test("ops.unaryMinus", () => {
|
|
317
|
+
assert.strictEqual(ops.unaryMinus(4), -4);
|
|
318
|
+
assert.strictEqual(ops.unaryMinus(-4), 4);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
test("ops.unaryPlus", () => {
|
|
322
|
+
assert.strictEqual(ops.unaryPlus(1), 1);
|
|
323
|
+
assert.strictEqual(ops.unaryPlus(-1), -1);
|
|
324
|
+
assert.strictEqual(ops.unaryPlus(""), 0);
|
|
207
325
|
});
|
|
208
326
|
|
|
209
327
|
test("ops.unpack unpacks a value", async () => {
|