Skip to content

Commit

Permalink
refactor(ast): refactor the rest of AST (tact-lang#524)
Browse files Browse the repository at this point in the history
* refactor(ast): AstField -> AstFieldDecl

* refactor(ast): ASTReceive -> AstReceiver

* refactor(ast): ASTInitFunction -> ContractInit

* refactor(ast): statements

* fix(init-var-analysis): augmented assignment was not treated correctly

* refactor(ast): expressions

* refactor(ast): attributes

* refactor(ast): receiver kind

* refactor(ast): ASTType -> AstTypeDecl

* refactor(ast): contract and trait declarations renamed

* refactor(ast): ASTNode -> AstNode

* refactor(ast): AstFieldDecl.init -> AstFieldDecl.initializer for
consistensy

* refactor(ast): types, separate and generalize optional types

but the grammar still only accepts optional types with type idents,
not general type arguments as AST allows now
  • Loading branch information
anton-trunov authored Jul 3, 2024
1 parent c9e383a commit eb2bbdc
Show file tree
Hide file tree
Showing 25 changed files with 3,802 additions and 5,165 deletions.
4 changes: 2 additions & 2 deletions src/abi/AbiFunction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ASTExpression, SrcInfo } from "../grammar/ast";
import { AstExpression, SrcInfo } from "../grammar/ast";
import { CompilerContext } from "../context";
import { WriterContext } from "../generator/Writer";
import { TypeRef } from "../types/types";
Expand All @@ -9,7 +9,7 @@ export type AbiFunction = {
generate: (
ctx: WriterContext,
args: TypeRef[],
resolved: ASTExpression[],
resolved: AstExpression[],
loc: SrcInfo,
) => string;
};
60 changes: 30 additions & 30 deletions src/constEval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Address, Cell, toNano } from "@ton/core";
import { enabledMasterchain } from "./config/features";
import { CompilerContext } from "./context";
import {
ASTBinaryOperation,
ASTExpression,
AstBinaryOperation,
AstExpression,
AstId,
ASTNewParameter,
AstStructFieldInitializer,
SrcInfo,
ASTUnaryOperation,
AstUnaryOperation,
isSelfId,
eqNames,
idText,
Expand Down Expand Up @@ -75,7 +75,7 @@ function ensureString(val: Value, source: SrcInfo): string {
return val;
}

function ensureFunArity(arity: number, args: ASTExpression[], source: SrcInfo) {
function ensureFunArity(arity: number, args: AstExpression[], source: SrcInfo) {
if (args.length !== arity) {
throwErrorConstEval(
`function expects ${arity} argument(s), but got ${args.length}`,
Expand All @@ -86,7 +86,7 @@ function ensureFunArity(arity: number, args: ASTExpression[], source: SrcInfo) {

function ensureMethodArity(
arity: number,
args: ASTExpression[],
args: AstExpression[],
source: SrcInfo,
) {
if (args.length !== arity) {
Expand All @@ -98,8 +98,8 @@ function ensureMethodArity(
}

function evalUnaryOp(
op: ASTUnaryOperation,
operand: ASTExpression,
op: AstUnaryOperation,
operand: AstExpression,
source: SrcInfo,
ctx: CompilerContext,
): Value {
Expand Down Expand Up @@ -149,9 +149,9 @@ function modFloor(a: bigint, b: bigint): bigint {
}

function evalBinaryOp(
op: ASTBinaryOperation,
left: ASTExpression,
right: ASTExpression,
op: AstBinaryOperation,
left: AstExpression,
right: AstExpression,
source: SrcInfo,
ctx: CompilerContext,
): Value {
Expand Down Expand Up @@ -303,9 +303,9 @@ function evalBinaryOp(
}

function evalConditional(
condition: ASTExpression,
thenBranch: ASTExpression,
elseBranch: ASTExpression,
condition: AstExpression,
thenBranch: AstExpression,
elseBranch: AstExpression,
ctx: CompilerContext,
): Value {
// here we rely on the typechecker that both branches have the same type
Expand All @@ -322,13 +322,13 @@ function evalConditional(

function evalStructInstance(
structTypeId: AstId,
structFields: ASTNewParameter[],
structFields: AstStructFieldInitializer[],
ctx: CompilerContext,
): StructValue {
return structFields.reduce(
(resObj, fieldWithInit) => {
resObj[fieldWithInit.name.text] = evalConstantExpression(
fieldWithInit.exp,
resObj[fieldWithInit.field.text] = evalConstantExpression(
fieldWithInit.initializer,
ctx,
);
return resObj;
Expand All @@ -338,7 +338,7 @@ function evalStructInstance(
}

function evalFieldAccess(
structExpr: ASTExpression,
structExpr: AstExpression,
fieldId: AstId,
source: SrcInfo,
ctx: CompilerContext,
Expand Down Expand Up @@ -392,8 +392,8 @@ function evalFieldAccess(

function evalMethod(
methodName: AstId,
object: ASTExpression,
args: ASTExpression[],
object: AstExpression,
args: AstExpression[],
source: SrcInfo,
ctx: CompilerContext,
): Value {
Expand All @@ -416,7 +416,7 @@ function evalMethod(

function evalBuiltins(
builtinName: AstId,
args: ASTExpression[],
args: AstExpression[],
source: SrcInfo,
ctx: CompilerContext,
): Value {
Expand Down Expand Up @@ -638,7 +638,7 @@ function interpretEscapeSequences(stringLiteral: string) {
}

export function evalConstantExpression(
ast: ASTExpression,
ast: AstExpression,
ctx: CompilerContext,
): Value {
switch (ast.kind) {
Expand All @@ -656,8 +656,8 @@ export function evalConstantExpression(
}
throwNonFatalErrorConstEval("cannot evaluate a variable", ast.loc);
break;
case "op_call":
return evalMethod(ast.name, ast.src, ast.args, ast.loc, ctx);
case "method_call":
return evalMethod(ast.method, ast.self, ast.args, ast.loc, ctx);
case "init_of":
throwNonFatalErrorConstEval(
"initOf is not supported at this moment",
Expand All @@ -673,7 +673,7 @@ export function evalConstantExpression(
case "string":
return ensureString(interpretEscapeSequences(ast.value), ast.loc);
case "op_unary":
return evalUnaryOp(ast.op, ast.right, ast.loc, ctx);
return evalUnaryOp(ast.op, ast.operand, ast.loc, ctx);
case "op_binary":
return evalBinaryOp(ast.op, ast.left, ast.right, ast.loc, ctx);
case "conditional":
Expand All @@ -683,11 +683,11 @@ export function evalConstantExpression(
ast.elseBranch,
ctx,
);
case "op_new":
case "struct_instance":
return evalStructInstance(ast.type, ast.args, ctx);
case "op_field":
return evalFieldAccess(ast.src, ast.name, ast.loc, ctx);
case "op_static_call":
return evalBuiltins(ast.name, ast.args, ast.loc, ctx);
case "field_access":
return evalFieldAccess(ast.aggregate, ast.field, ast.loc, ctx);
case "static_call":
return evalBuiltins(ast.function, ast.args, ast.loc, ctx);
}
}
8 changes: 6 additions & 2 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MatchResult } from "ohm-js";
import path from "path";
import { cwd } from "process";
import { AstId, SrcInfo } from "./grammar/ast";
import { AstFuncId, AstId, AstTypeId, SrcInfo } from "./grammar/ast";
import { ItemOrigin } from "./grammar/grammar";

export class TactError extends Error {
Expand Down Expand Up @@ -98,7 +98,11 @@ export function throwConstEvalError(

export function idTextErr(ident: string): string;
export function idTextErr(ident: AstId): string;
export function idTextErr(ident: AstId | string): string {
export function idTextErr(ident: AstFuncId): string;
export function idTextErr(ident: AstTypeId): string;
export function idTextErr(
ident: AstId | AstFuncId | AstTypeId | string,
): string {
if (typeof ident === "string") {
return `"${ident}"`;
}
Expand Down
Loading

0 comments on commit eb2bbdc

Please sign in to comment.