Skip to content

Commit

Permalink
fix(pointfree-lang): update grammar (parse order), add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 3, 2018
1 parent 769e84d commit 5450e50
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/pointfree-lang/src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ MapVal

Atom
= String
/ Sym
/ Number
/ Boolean
/ Nil
/ Sym

Nil
= "nil" {
Expand Down
29 changes: 29 additions & 0 deletions packages/pointfree-lang/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ describe("pointfree-lang", () => {
assert.deepEqual(run(`'nil dup`)[0], [[null], [null]]);
});

it("number (hex)", () => {
assert.deepEqual(run(`0x1 0xa 0xff 0xdecafbad`)[0], [1, 10, 255, 0xdecafbad]);
});

it("number (decimal)", () => {
assert.deepEqual(run(`0 -1 +2`)[0], [0, -1, 2]);
assert.deepEqual(run(`-123. +12.3`)[0], [-123, 12.3]);
assert.deepEqual(run(`-123e4`)[0], [-1230000]);
assert.deepEqual(run(`+1.23e-2`)[0], [0.0123]);
assert.deepEqual(run(`+1.23e-2 0.0123 =`)[0], [true]);
});

it("litquote", () => {
assert.deepEqual(runU(`'nil`), [null]);
assert.deepEqual(runU(`'+`), [pf.add]);
Expand All @@ -17,6 +29,23 @@ describe("pointfree-lang", () => {
assert.deepEqual(run(`1 2 '+ exec`)[0], [3]);
});

it("var deref (quote)", () => {
assert.deepEqual(runU(`[@a [@a {@a: @a} {@a: [@a]}]]`, { a: 1 }), [1, [1, { 1: 1 }, { 1: [1] }]]);
});

it("var deref (litquote)", () => {
assert.deepEqual(runU(`'@a`, { a: 1 }), [1]);
assert.deepEqual(runU(`'[@a]`, { a: 1 }), [[1]]);
assert.deepEqual(runU(`''@a`, { a: 1 }), [[1]]);
});

it("var deref (word)", () => {
assert.deepEqual(runU(`: foo [@a [@a {@a: @a} {@a: [@a]}]]; foo`, { a: 1 }), [1, [1, { 1: 1 }, { 1: [1] }]]);
assert.deepEqual(
run(`: foo [@a [@a {@a: @a} {@a: [@a]}]]; foo 2 a! foo`, { a: 1 })[0],
[[1, [1, { 1: 1 }, { 1: [1] }]], [2, [2, { 2: 2 }, { 2: [2] }]]]);
});

// setDebug(true);

// console.log(run(`"result: " 1 2 + + .`));
Expand Down

0 comments on commit 5450e50

Please sign in to comment.