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

First version of partial evaluator: support for +,-,* operators and associative rules #528

Merged
merged 25 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
fa5bb05
First commit for partial evaluator.
jeshecdom Jun 28, 2024
aae1dd0
Finished associative rule 3. Refactored Rule type into an interface. …
jeshecdom Jul 2, 2024
4533de6
Merge branch 'partial_evaluator'. Resolved conflicts in constEval. Ad…
jeshecdom Jul 2, 2024
34970cb
Added jest test cases for partial evaluator.
jeshecdom Jul 4, 2024
a109d38
Merge branch 'main' of https://github.com/tact-lang/tact
jeshecdom Jul 4, 2024
99b687f
Fixes for the linter, spell checker, and code prettifier.
jeshecdom Jul 4, 2024
2688771
Fixed another linter problem.
jeshecdom Jul 4, 2024
9e871ca
Merge branch 'main' of https://github.com/tact-lang/tact
jeshecdom Jul 5, 2024
bd89e02
Merge branch 'main' into partial_evaluator
jeshecdom Jul 5, 2024
cdb7cb2
Fixed compilation errors after merge.
jeshecdom Jul 5, 2024
15998a5
Merge branch 'main' into partial_evaluator
jeshecdom Jul 5, 2024
bfb37af
Fixed problems pointed out by linter and prettier.
jeshecdom Jul 5, 2024
648f188
- Changed all `Ref` in parameters for `Loc`. (https://github.com/tact…
jeshecdom Jul 5, 2024
0426245
fixes due to linter and prettier.
jeshecdom Jul 5, 2024
e207389
Merge branch 'main' of https://github.com/tact-lang/tact
jeshecdom Jul 5, 2024
82a9873
Merge branch 'main' into partial_evaluator
jeshecdom Jul 5, 2024
45289f8
- Added try/catch in switch of eqExpressions in ast.ts.
jeshecdom Jul 12, 2024
8d7b977
Merge branch 'main' of https://github.com/tact-lang/tact
jeshecdom Jul 12, 2024
0c77158
Merge branch 'main' into partial_evaluator
jeshecdom Jul 12, 2024
9405f98
Forgot to add test cases for initOf.
jeshecdom Jul 12, 2024
e10a838
Fixed problems indicated by knit.
jeshecdom Jul 12, 2024
8991852
- Moved `isValue` to ast.ts.
jeshecdom Jul 15, 2024
2ebd128
Merge branch 'main' of https://github.com/tact-lang/tact
jeshecdom Jul 15, 2024
4204dc0
Merge branch 'main' into partial_evaluator
jeshecdom Jul 15, 2024
607da81
Fixed compilation error after merge with main.
jeshecdom Jul 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
348 changes: 252 additions & 96 deletions src/constEval.ts

Large diffs are not rendered by default.

147 changes: 147 additions & 0 deletions src/grammar/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,8 @@ export type AstNull = {
loc: SrcInfo;
};

export type AstValue = AstNumber | AstBoolean | AstNull | AstString;

export type AstConstantAttribute =
| { type: "virtual"; loc: SrcInfo }
| { type: "overrides"; loc: SrcInfo }
Expand Down Expand Up @@ -679,4 +681,149 @@ export function __DANGER_resetNodeId() {
nextId = 1;
}

// Test equality of AstExpressions.
export function eqExpressions(
ast1: AstExpression,
ast2: AstExpression,
): boolean {
if (ast1.kind !== ast2.kind) {
return false;
}

switch (ast1.kind) {
case "null":
return true;
case "boolean":
return ast1.value === (ast2 as AstBoolean).value;
anton-trunov marked this conversation as resolved.
Show resolved Hide resolved
case "number":
return ast1.value === (ast2 as AstNumber).value;
case "string":
return ast1.value === (ast2 as AstString).value;
case "id":
return eqNames(ast1, ast2 as AstId);
case "method_call":
return (
eqNames(ast1.method, (ast2 as AstMethodCall).method) &&
eqExpressions(ast1.self, (ast2 as AstMethodCall).self) &&
eqExpressionArrays(ast1.args, (ast2 as AstMethodCall).args)
);
case "init_of":
return (
eqNames(ast1.contract, (ast2 as AstInitOf).contract) &&
eqExpressionArrays(ast1.args, (ast2 as AstInitOf).args)
);
case "op_unary":
return (
ast1.op === (ast2 as AstOpUnary).op &&
eqExpressions(ast1.operand, (ast2 as AstOpUnary).operand)
);
case "op_binary":
return (
ast1.op === (ast2 as AstOpBinary).op &&
eqExpressions(ast1.left, (ast2 as AstOpBinary).left) &&
eqExpressions(ast1.right, (ast2 as AstOpBinary).right)
);
case "conditional":
return (
eqExpressions(
ast1.condition,
(ast2 as AstConditional).condition,
) &&
eqExpressions(
ast1.thenBranch,
(ast2 as AstConditional).thenBranch,
) &&
eqExpressions(
ast1.elseBranch,
(ast2 as AstConditional).elseBranch,
)
);
case "struct_instance":
return (
eqNames(ast1.type, (ast2 as AstStructInstance).type) &&
eqParameterArrays(ast1.args, (ast2 as AstStructInstance).args)
);
case "field_access":
return (
eqNames(ast1.field, (ast2 as AstFieldAccess).field) &&
eqExpressions(
ast1.aggregate,
(ast2 as AstFieldAccess).aggregate,
)
);
case "static_call":
return (
eqNames(ast1.function, (ast2 as AstStaticCall).function) &&
eqExpressionArrays(ast1.args, (ast2 as AstStaticCall).args)
);
}
}

function eqParameters(
arg1: AstStructFieldInitializer,
arg2: AstStructFieldInitializer,
): boolean {
return (
eqNames(arg1.field, arg2.field) &&
eqExpressions(arg1.initializer, arg2.initializer)
);
}

function eqParameterArrays(
arr1: AstStructFieldInitializer[],
arr2: AstStructFieldInitializer[],
): boolean {
if (arr1.length !== arr2.length) {
return false;
}

for (let i = 0; i < arr1.length; i++) {
if (!eqParameters(arr1[i]!, arr2[i]!)) {
return false;
}
}

return true;
}

function eqExpressionArrays(
arr1: AstExpression[],
arr2: AstExpression[],
): boolean {
if (arr1.length !== arr2.length) {
return false;
}

for (let i = 0; i < arr1.length; i++) {
if (!eqExpressions(arr1[i]!, arr2[i]!)) {
return false;
}
}

return true;
}

export function isValue(ast: AstExpression): boolean {
switch (ast.kind) {
case "null":
case "boolean":
case "number":
case "string":
return true;

case "struct_instance":
return ast.args.every((arg) => isValue(arg.initializer));

case "id":
case "method_call":
case "init_of":
case "op_unary":
case "op_binary":
case "conditional":
case "field_access":
case "static_call":
return false;
}
}

export { SrcInfo };
Loading
Loading