Skip to content

Commit

Permalink
more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
i582 committed Jan 21, 2025
1 parent b850df2 commit 811ff79
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 362 deletions.
106 changes: 44 additions & 62 deletions src/ast/util.ts
Original file line number Diff line number Diff line change
@@ -1,159 +1,141 @@
import * as A from "./ast";
import { Address, Cell, Slice } from "@ton/core";
import {
AstExpression,
AstUnaryOperation,
AstBinaryOperation,
isLiteral,
AstNumber,
AstBoolean,
AstSimplifiedString,
AstNull,
AstCell,
AstSlice,
AstAddress,
AstLiteral,
AstStructValue,
AstStructFieldValue,
AstId,
AstCommentValue,
FactoryAst,
} from "./ast";
import { dummySrcInfo, SrcInfo } from "../grammar";

export const getAstUtil = ({ createNode }: FactoryAst) => {
export const getAstUtil = ({ createNode }: A.FactoryAst) => {
function makeUnaryExpression(
op: AstUnaryOperation,
operand: AstExpression,
): AstExpression {
op: A.AstUnaryOperation,
operand: A.AstExpression,
): A.AstExpression {
const result = createNode({
kind: "op_unary",
op: op,
operand: operand,
loc: dummySrcInfo,
});
return result as AstExpression;
return result as A.AstExpression;
}

function makeBinaryExpression(
op: AstBinaryOperation,
left: AstExpression,
right: AstExpression,
): AstExpression {
op: A.AstBinaryOperation,
left: A.AstExpression,
right: A.AstExpression,
): A.AstExpression {
const result = createNode({
kind: "op_binary",
op: op,
left: left,
right: right,
loc: dummySrcInfo,
});
return result as AstExpression;
return result as A.AstExpression;
}

function makeNumberLiteral(n: bigint, loc: SrcInfo): AstNumber {
function makeNumberLiteral(n: bigint, loc: SrcInfo): A.AstNumber {
const result = createNode({
kind: "number",
base: 10,
value: n,
loc: loc,
});
return result as AstNumber;
return result as A.AstNumber;
}

function makeBooleanLiteral(b: boolean, loc: SrcInfo): AstBoolean {
function makeBooleanLiteral(b: boolean, loc: SrcInfo): A.AstBoolean {
const result = createNode({
kind: "boolean",
value: b,
loc: loc,
});
return result as AstBoolean;
return result as A.AstBoolean;
}

function makeSimplifiedStringLiteral(
s: string,
loc: SrcInfo,
): AstSimplifiedString {
): A.AstSimplifiedString {
const result = createNode({
kind: "simplified_string",
value: s,
loc: loc,
});
return result as AstSimplifiedString;
return result as A.AstSimplifiedString;
}

function makeCommentLiteral(s: string, loc: SrcInfo): AstCommentValue {
function makeCommentLiteral(s: string, loc: SrcInfo): A.AstCommentValue {
const result = createNode({
kind: "comment_value",
value: s,
loc: loc,
});
return result as AstCommentValue;
return result as A.AstCommentValue;
}

function makeNullLiteral(loc: SrcInfo): AstNull {
function makeNullLiteral(loc: SrcInfo): A.AstNull {
const result = createNode({
kind: "null",
loc: loc,
});
return result as AstNull;
return result as A.AstNull;
}

function makeCellLiteral(c: Cell, loc: SrcInfo): AstCell {
function makeCellLiteral(c: Cell, loc: SrcInfo): A.AstCell {
const result = createNode({
kind: "cell",
value: c,
loc: loc,
});
return result as AstCell;
return result as A.AstCell;
}

function makeSliceLiteral(s: Slice, loc: SrcInfo): AstSlice {
function makeSliceLiteral(s: Slice, loc: SrcInfo): A.AstSlice {
const result = createNode({
kind: "slice",
value: s,
loc: loc,
});
return result as AstSlice;
return result as A.AstSlice;
}

function makeAddressLiteral(a: Address, loc: SrcInfo): AstAddress {
function makeAddressLiteral(a: Address, loc: SrcInfo): A.AstAddress {
const result = createNode({
kind: "address",
value: a,
loc: loc,
});
return result as AstAddress;
return result as A.AstAddress;
}

function makeStructFieldValue(
fieldName: string,
val: AstLiteral,
val: A.AstLiteral,
loc: SrcInfo,
): AstStructFieldValue {
): A.AstStructFieldValue {
const result = createNode({
kind: "struct_field_value",
field: createNode({
kind: "id",
text: fieldName,
loc: loc,
}) as AstId,
}) as A.AstId,
initializer: val,
loc: loc,
});
return result as AstStructFieldValue;
return result as A.AstStructFieldValue;
}

function makeStructValue(
fields: AstStructFieldValue[],
type: AstId,
fields: A.AstStructFieldValue[],
type: A.AstId,
loc: SrcInfo,
): AstStructValue {
): A.AstStructValue {
const result = createNode({
kind: "struct_value",
args: fields,
loc: loc,
type: type,
});
return result as AstStructValue;
return result as A.AstStructValue;
}

return {
Expand All @@ -175,38 +157,38 @@ export const getAstUtil = ({ createNode }: FactoryAst) => {
export type AstUtil = ReturnType<typeof getAstUtil>;

// Checks if the top level node is an unary op node
export function checkIsUnaryOpNode(ast: AstExpression): boolean {
export function checkIsUnaryOpNode(ast: A.AstExpression): boolean {
return ast.kind === "op_unary";
}

// Checks if the top level node is a binary op node
export function checkIsBinaryOpNode(ast: AstExpression): boolean {
export function checkIsBinaryOpNode(ast: A.AstExpression): boolean {
return ast.kind === "op_binary";
}

// Checks if top level node is a binary op node
// with a value node on the right
export function checkIsBinaryOp_With_RightValue(ast: AstExpression): boolean {
return ast.kind === "op_binary" ? isLiteral(ast.right) : false;
export function checkIsBinaryOp_With_RightValue(ast: A.AstExpression): boolean {
return ast.kind === "op_binary" ? A.isLiteral(ast.right) : false;
}

// Checks if top level node is a binary op node
// with a value node on the left
export function checkIsBinaryOp_With_LeftValue(ast: AstExpression): boolean {
return ast.kind === "op_binary" ? isLiteral(ast.left) : false;
export function checkIsBinaryOp_With_LeftValue(ast: A.AstExpression): boolean {
return ast.kind === "op_binary" ? A.isLiteral(ast.left) : false;
}

// Checks if the top level node is the specified number
export function checkIsNumber(ast: AstExpression, n: bigint): boolean {
export function checkIsNumber(ast: A.AstExpression, n: bigint): boolean {
return ast.kind === "number" ? ast.value == n : false;
}

export function checkIsName(ast: AstExpression): boolean {
export function checkIsName(ast: A.AstExpression): boolean {
return ast.kind === "id";
}

// Checks if the top level node is the specified boolean
export function checkIsBoolean(ast: AstExpression, b: boolean): boolean {
export function checkIsBoolean(ast: A.AstExpression, b: boolean): boolean {
return ast.kind === "boolean" ? ast.value == b : false;
}

Expand Down
Loading

0 comments on commit 811ff79

Please sign in to comment.