Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove children field from ASTNode #956

Merged
merged 5 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions packages/core/src/analysis/SubstanceAnalysis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
} from "compiler/Substance";
import { dummyIdentifier } from "engine/EngineUtils";
import { intersectionWith } from "lodash";
import { similarMappings, similarNodes } from "synthesis/Search";
import { A, ASTNode } from "types/ast";
import { similarMappings, similarNodes, SubNode } from "synthesis/Search";
import { A } from "types/ast";
import { Env } from "types/domain";
import { SubProg, SubStmt } from "types/substance";
import {
Expand All @@ -33,8 +33,8 @@ const compile = (src: string): SubProg<A> =>

describe("Substance AST queries", () => {
test("Similar AST nodes", () => {
let node1: ASTNode<A>;
let node2: ASTNode<A>;
let node1: SubNode<A>;
let node2: SubNode<A>;
node1 = compile("Set A");
node2 = compile("Set B");
expect(similarNodes(node1, node2)).toBe(true);
Expand Down Expand Up @@ -104,16 +104,14 @@ describe("Substance AST queries", () => {
// const id2 = dummyIdentifier("B", "SyntheticSubstance");
// expect(nodesEqual(id1, id2)).toBe(false);
// expect(nodesEqual(id2, id2)).toBe(true);
// // create two SubStmts with different source locs and children and the equality check should still return true
// // create two SubStmts with different source locs and the equality check should still return true
// const prog1: DefaultLabels = {
// children: [],
// tag: "DefaultLabels",
// nodeType: "Substance",
// start: { line: 0, col: 0 },
// end: { line: 0, col: 0 },
// };
// const prog2: DefaultLabels = {
// children: [prog1],
// tag: "DefaultLabels",
// nodeType: "Substance",
// start: { line: 5, col: 0 },
Expand Down Expand Up @@ -156,13 +154,11 @@ Set C`;
const originalAST = compileSubstance(original, env).unsafelyUnwrap()[0].ast;
const newStmt: SubStmt<A> = {
nodeType: "SyntheticSubstance",
children: [],
tag: "Decl",
name: dummyIdentifier("C", "SyntheticSubstance"),
type: {
tag: "TypeConstructor",
nodeType: "SyntheticSubstance",
children: [],
args: [],
name: dummyIdentifier("Set", "SyntheticSubstance"),
},
Expand Down Expand Up @@ -190,13 +186,11 @@ Set ZZZ`;
const toReplace = originalAST.statements[1];
const newStmt: SubStmt<A> = {
nodeType: "SyntheticSubstance",
children: [],
tag: "Decl",
name: dummyIdentifier("ZZZ", "SyntheticSubstance"),
type: {
tag: "TypeConstructor",
nodeType: "SyntheticSubstance",
children: [],
args: [],
name: dummyIdentifier("Set", "SyntheticSubstance"),
},
Expand Down
12 changes: 2 additions & 10 deletions packages/core/src/analysis/SubstanceAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ export const applyConstructor = (
tag: "ApplyConstructor",
name,
nodeType: "SyntheticSubstance",
children: [],
args,
};
};
Expand All @@ -394,7 +393,6 @@ export const applyFunction = (
tag: "ApplyFunction",
name,
nodeType: "SyntheticSubstance",
children: [],
args,
};
};
Expand All @@ -408,15 +406,13 @@ export const applyPredicate = (
tag: "ApplyPredicate",
name,
nodeType: "SyntheticSubstance",
children: [],
args,
};
};

export const subProg = (statements: SubStmt<A>[]): SubProg<A> => ({
tag: "SubProg",
statements,
children: statements,
nodeType: "SyntheticSubstance",
});

Expand All @@ -431,7 +427,6 @@ export const applyBind = (
expr: SubExpr<A>
): Bind<A> => ({
tag: "Bind",
children: [],
nodeType: "SyntheticSubstance",
variable,
expr,
Expand All @@ -440,7 +435,6 @@ export const applyBind = (
export const nullaryTypeCons = (name: Identifier<A>): TypeConsApp<A> => ({
tag: "TypeConstructor",
nodeType: "SyntheticSubstance",
children: [],
name,
args: [],
});
Expand All @@ -450,14 +444,12 @@ export const autoLabelStmt: AutoLabel<A> = {
option: {
tag: "DefaultLabels",
nodeType: "SyntheticSubstance",
children: [],
},
nodeType: "SyntheticSubstance",
children: [],
};

/**
* Compare two AST nodes by their contents, ignoring structural properties such as `children` and positional properties like `start` and `end`.
* Compare two AST nodes by their contents, ignoring structural properties such as `nodeType` and positional properties like `start` and `end`.
*
* @param node1 the first AST node
* @param node2 the second AST node
Expand All @@ -469,7 +461,7 @@ export const nodesEqual = (node1: AbstractNode, node2: AbstractNode): boolean =>
});

/**
* Compare all statements of two ASTs by their contents, ignoring structural properties such as `children` and positional properties like `start` and `end`.
* Compare all statements of two ASTs by their contents, ignoring structural properties such as `nodeType` and positional properties like `start` and `end`.
*
* @param left the first Substance program
* @param right the second Substance program
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/compiler/Domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ const builtinTypes: [string, TypeDecl<C>][] = [
start: { line: 1, col: 1 },
end: { line: 1, col: 1 },
nodeType: "Substance",
children: [],
tag: "TypeDecl",
name: idOf("String", "Domain"),
params: [],
Expand Down Expand Up @@ -145,7 +144,6 @@ const checkStmt = (stmt: DomainStmt<C>, env: Env): CheckerResult => {
const subType: TypeConstructor<C> = {
tag: "TypeConstructor",
nodeType: "Substance",
children: [name],
start: stmt.start,
end: stmt.end,
name,
Expand Down Expand Up @@ -388,7 +386,6 @@ const topName = idOf("type", "Domain");
export const topType: TypeConsApp<A> = {
tag: "TypeConstructor",
nodeType: "Domain",
children: [topName],
name: topName,
args: [],
};
Expand All @@ -397,7 +394,6 @@ const bottomName = idOf("void", "Domain");
export const bottomType: TypeConsApp<A> = {
tag: "TypeConstructor",
nodeType: "Domain",
children: [bottomName],
name: bottomName,
args: [],
};
Expand Down
28 changes: 6 additions & 22 deletions packages/core/src/compiler/Style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,6 @@ const toSubstanceType = (styT: StyT<A>): TypeConsApp<A> => {
return {
tag: "TypeConstructor",
nodeType: "Substance",
children: [styT],
name: styT,
args: [],
};
Expand Down Expand Up @@ -809,10 +808,8 @@ const substitutePath = (
case "LocalVar": {
return {
nodeType: "SyntheticStyle",
children: [],
tag: "FieldPath",
name: {
children: [],
nodeType: "SyntheticStyle",
tag: "SubVar",
contents: {
Expand All @@ -830,11 +827,9 @@ const substitutePath = (
// COMBAK / HACK: Is there some way to get rid of all these dummy values?
return {
nodeType: "SyntheticStyle",
children: [],
tag: "FieldPath",
name: {
nodeType: "SyntheticStyle",
children: [],
tag: "SubVar",
contents: {
...dummyId(mkLocalVarName(lv)),
Expand Down Expand Up @@ -1577,14 +1572,12 @@ const nameAnonStatement = (i: number, s: Stmt<A>): [number, Stmt<A>] => {
type: {
tag: "TypeOf",
nodeType: "SyntheticStyle",
children: [],
contents: "Nothing",
}, // TODO: Why is it parsed like this?
path: {
tag: "InternalLocalVar",
contents: `\$${ANON_KEYWORD}_${i}`,
nodeType: "SyntheticStyle",
children: [], // Unused bc compiler internal
},
value: s.contents,
};
Expand Down Expand Up @@ -2265,10 +2258,8 @@ const mkPath = (strs: string[]): Path<A> => {
return {
tag: "FieldPath",
nodeType: "SyntheticStyle",
children: [],
name: {
nodeType: "SyntheticStyle",
children: [],
tag: "SubVar",
contents: {
...dummyId(name),
Expand All @@ -2281,10 +2272,8 @@ const mkPath = (strs: string[]): Path<A> => {
return {
tag: "PropertyPath",
nodeType: "SyntheticStyle",
children: [],
name: {
nodeType: "SyntheticStyle",
children: [],
tag: "SubVar",
contents: {
...dummyId(name),
Expand Down Expand Up @@ -2332,7 +2321,6 @@ const findPropertyVarying = (
tag: "OptEval",
contents: {
nodeType: "SyntheticStyle",
children: [],
tag: "Vector",
contents: [
dummyASTNode({ tag: "Vary" }, "SyntheticStyle") as Expr<A>,
Expand Down Expand Up @@ -2366,16 +2354,12 @@ const findNestedVarying = (e: TagExpr<VarAD>, p: Path<A>): Path<A>[] => {
.map((e: Expr<A>, i): [Expr<A>, number] => [e, i])
.filter((e: [Expr<A>, number]): boolean => isVarying(e[0]))
.map(
([, i]: [Expr<A>, number]): IAccessPath<A> =>
({
nodeType: "SyntheticStyle",
children: [],
tag: "AccessPath",
path: p,
indices: [
dummyASTNode({ tag: "Fix", contents: i }, "SyntheticStyle"),
],
} as IAccessPath<A>)
([, i]: [Expr<A>, number]): IAccessPath<A> => ({
nodeType: "SyntheticStyle",
tag: "AccessPath",
path: p,
indices: [{ tag: "Fix", nodeType: "SyntheticStyle", contents: i }],
})
);

return indices;
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/compiler/Substance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export const postprocessSubstance = (

const toSubDecl = (idString: string, decl: TypeConstructor<C>): Decl<A> => ({
nodeType: "SyntheticSubstance",
children: [],
tag: "Decl",
type: {
...decl,
Expand Down Expand Up @@ -216,7 +215,6 @@ const stringName = idOf("String", "Substance");
const stringType: TypeConsApp<A> = {
tag: "TypeConstructor",
nodeType: "SyntheticSubstance",
children: [stringName],
name: stringName,
args: [],
};
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/engine/EngineUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,6 @@ export const dummyIdentifier = (
): Identifier<A> => {
return {
nodeType,
children: [],
type: "value",
value: name,
tag: "Identifier",
Expand All @@ -448,7 +447,6 @@ const floatValToExpr = (e: Value<VarAD>): Expr<A> => {

return {
nodeType: "SyntheticStyle",
children: [],
tag: "VaryAD",
contents: e.contents,
};
Expand Down Expand Up @@ -1060,7 +1058,6 @@ export const exprToNumber = (e: Expr<A>): number => {
export const numToExpr = (n: number): Expr<A> => {
return {
nodeType: "SyntheticStyle",
children: [],
tag: "Fix",
contents: n,
};
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/engine/Evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,6 @@ export const evalExpr = (
p = {
// convert to AccessPath schema
nodeType: "SyntheticStyle",
children: [],
tag: "AccessPath",
// contents: [p.contents[0], [p.contents[1].contents]],
path: p.contents[0],
Expand All @@ -625,7 +624,6 @@ export const evalExpr = (
p = {
// convert to AccessPath schema
nodeType: "SyntheticStyle",
children: [],
tag: "AccessPath",
path: p.contents[0],
indices: p.contents[1],
Expand Down