Skip to content

Commit

Permalink
feat(constant): Show an error on integer overflow in compile-time
Browse files Browse the repository at this point in the history
Closes #189
  • Loading branch information
byakuren-hijiri committed Mar 27, 2024
1 parent 7ef2ae1 commit a54dad0
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/types/resolveConstantValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ASTExpression, throwError } from "../grammar/ast";
import { printTypeRef, TypeRef } from "./types";
import { sha256_sync } from "@ton/crypto";

function reduceInt(ast: ASTExpression): bigint {
function reduceIntImpl(ast: ASTExpression): bigint {
if (ast.kind === 'number') {
return ast.value;
} else if (ast.kind === 'op_binary') {
Expand Down Expand Up @@ -59,6 +59,18 @@ function reduceInt(ast: ASTExpression): bigint {
throwError('Cannot reduce expression to a constant integer', ast.ref);
}

function reduceInt(ast: ASTExpression): bigint {
try {
return reduceIntImpl(ast)
} catch (error) {
if (error instanceof RangeError) {
throwError('Cannot evaluate expression due to integer overflow', ast.ref);
} else {
throw error;
}
}
}

function reduceBool(ast: ASTExpression): boolean {
if (ast.kind === 'boolean') {
return ast.value;
Expand Down

0 comments on commit a54dad0

Please sign in to comment.