-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A "boolio" tree is an AST that contains boolean operators and "atoms"
- Loading branch information
Patrick McElhaney
committed
Aug 3, 2019
1 parent
2d07ed5
commit c7275c9
Showing
2 changed files
with
136 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
const acorn = require("acorn"); | ||
|
||
function atomFromAcorn(node) { | ||
if (node.raw) { | ||
return node.raw; | ||
} | ||
if (node.type === "Identifier") { | ||
return node.name; | ||
} | ||
if (node.type === "CallExpression") { | ||
return `${node.callee.name}(${node.arguments.map(atomFromAcorn)})`; | ||
} | ||
|
||
if (node.type === "LogicalExpression") { | ||
return `${atomFromAcorn(node.left)} ${node.operator} ${atomFromAcorn( | ||
node.right | ||
)}`; | ||
} | ||
|
||
return `?${node.type}?`; | ||
} | ||
|
||
function fromAcorn(node) { | ||
if (node.type === "Program") { | ||
return fromAcorn(node.body[0]); | ||
} | ||
|
||
if (node.type === "ExpressionStatement") { | ||
return fromAcorn(node.expression); | ||
} | ||
|
||
if (node.type === "LogicalExpression") { | ||
return { | ||
type: node.operator === "||" ? "or" : "and", | ||
left: fromAcorn(node.left), | ||
right: fromAcorn(node.right) | ||
}; | ||
} | ||
|
||
if (node.type === "Identifier") { | ||
return { | ||
type: "atom", | ||
name: node.name | ||
}; | ||
} | ||
|
||
if (node.type === "CallExpression") { | ||
return { | ||
type: "atom", | ||
name: atomFromAcorn(node) | ||
}; | ||
} | ||
|
||
throw new Error(`Unrecognized node type: ${node.type}`); | ||
} | ||
|
||
describe("builds a boolio tree from an acorn tree", () => { | ||
it("simple or expression", () => { | ||
const tree = acorn.parse("a || b"); | ||
expect(fromAcorn(tree)).toEqual({ | ||
type: "or", | ||
left: { type: "atom", name: "a" }, | ||
right: { type: "atom", name: "b" } | ||
}); | ||
}); | ||
|
||
it("simple and expression", () => { | ||
const tree = acorn.parse("a && b"); | ||
expect(fromAcorn(tree)).toEqual({ | ||
type: "and", | ||
left: { type: "atom", name: "a" }, | ||
right: { type: "atom", name: "b" } | ||
}); | ||
}); | ||
|
||
it("multiple operators", () => { | ||
const tree = acorn.parse("a || b && c"); | ||
expect(fromAcorn(tree)).toEqual({ | ||
type: "or", | ||
left: { type: "atom", name: "a" }, | ||
right: { | ||
type: "and", | ||
left: { type: "atom", name: "b" }, | ||
right: { type: "atom", name: "c" } | ||
} | ||
}); | ||
}); | ||
|
||
it("grouping", () => { | ||
const tree = acorn.parse("(a || b)"); | ||
expect(fromAcorn(tree)).toEqual({ | ||
type: "or", | ||
left: { type: "atom", name: "a" }, | ||
right: { type: "atom", name: "b" } | ||
}); | ||
}); | ||
|
||
it("grouping with multiple operators", () => { | ||
const tree = acorn.parse("(a || b) && c"); | ||
expect(fromAcorn(tree)).toEqual({ | ||
type: "and", | ||
left: { | ||
type: "or", | ||
left: { type: "atom", name: "a" }, | ||
right: { type: "atom", name: "b" } | ||
}, | ||
right: { type: "atom", name: "c" } | ||
}); | ||
}); | ||
|
||
it("call expression", () => { | ||
const tree = acorn.parse("foo() && bar(1,2,x)"); | ||
expect(fromAcorn(tree)).toEqual({ | ||
type: "and", | ||
left: { type: "atom", name: "foo()" }, | ||
right: { type: "atom", name: "bar(1,2,x)" } | ||
}); | ||
}); | ||
|
||
it("nested call expression", () => { | ||
const tree = acorn.parse("foo(bar(1)) && x"); | ||
expect(fromAcorn(tree)).toEqual({ | ||
type: "and", | ||
left: { type: "atom", name: "foo(bar(1))" }, | ||
right: { type: "atom", name: "x" } | ||
}); | ||
}); | ||
|
||
it("call with operator", () => { | ||
const tree = acorn.parse("foo(a && b)"); | ||
expect(fromAcorn(tree)).toEqual({ | ||
type: "atom", | ||
name: "foo(a && b)" | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.