Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyProgrammist committed Dec 22, 2023
1 parent f0d7056 commit 875a27d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 112 deletions.
115 changes: 4 additions & 111 deletions src/codegen/astbuilder/handle_field.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,8 @@
import { BuiltinOneArgExpr, BuiltinZeroArgs, CellRefExpr, CombinatorExpr, CondExpr, FieldExprDef, FieldNamedDef, MathExpr, NameExpr, NegateExpr, NumberExpr, Expression as ParserExpression } from "../../ast/nodes";
import { TLBBinaryOp, TLBCode, TLBConstructor, TLBField, TLBFieldType, TLBMathExpr, TLBNumberExpr, TLBNumberType, TLBType, TLBUnaryOp, TLBVarExpr } from "../ast";
import { BuiltinOneArgExpr, BuiltinZeroArgs, CombinatorExpr, CondExpr, FieldExprDef, FieldNamedDef, MathExpr, NameExpr } from "../../ast/nodes";
import { TLBCode, TLBField, TLBNumberType, TLBType } from "../ast";
import { GenDeclaration, ObjectProperty } from "../generators/typescript/tsgen";
import { convertToMathExpr, firstLower, getSubStructName, goodVariableName, splitForTypeValue } from "../utils";

export function getType(expr: ParserExpression, fieldName: string, isField: boolean, needArg: boolean, variableCombinatorName: string, variableSubStructName: string, constructor: TLBConstructor, fieldTypeName: string, argIndex: number, tlbCode: TLBCode): TLBFieldType {
if (expr instanceof BuiltinZeroArgs) {
if (expr.name == '#') {
return { kind: 'TLBNumberType', bits: new TLBNumberExpr(32), storeBits: new TLBNumberExpr(32), signed: false, maxBits: 32}
} else {
throw new Error('Expression not supported' + expr)
}
} else if (expr instanceof BuiltinOneArgExpr) {
if (expr.name.toString() == '##' || expr.name.toString() == '(##)') {
if (expr.arg instanceof NumberExpr) {
return { kind: 'TLBNumberType', bits: new TLBNumberExpr(expr.arg.num), storeBits: new TLBNumberExpr(expr.arg.num), signed: false, maxBits: expr.arg.num }
}
if (expr.arg instanceof NameExpr) {
let parameter = constructor.parametersMap.get(expr.arg.name)
if (!parameter || !parameter.variable.deriveExpr || !parameter.variable.initialExpr) {
throw new Error('')
}
return { kind: 'TLBNumberType', bits: parameter.variable.deriveExpr, storeBits: parameter.variable.initialExpr, signed: false, maxBits: undefined }
} // TODO: handle other cases
} else if (expr.name == '#<') {
if (expr.arg instanceof NumberExpr || expr.arg instanceof NameExpr) {
let bits = new TLBUnaryOp(new TLBBinaryOp(convertToMathExpr(expr.arg), new TLBNumberExpr(1), '-'), '.');
return { kind: 'TLBNumberType', bits: bits, storeBits: bits, signed: false, maxBits: 32}
} // TODO: handle other cases
} else if (expr.name == '#<=') {
if (expr.arg instanceof NumberExpr || expr.arg instanceof NameExpr) {
let bits = new TLBUnaryOp(convertToMathExpr(expr.arg), '.');
return { kind: 'TLBNumberType', bits: bits, storeBits: bits, signed: false, maxBits: 32}
} // TODO: handle other cases
}
} else if (expr instanceof CombinatorExpr) {
if (expr.name == 'int' && expr.args.length == 1 && (expr.args[0] instanceof MathExpr || expr.args[0] instanceof NumberExpr || expr.args[0] instanceof NameExpr)) {
return { kind: 'TLBNumberType', bits: convertToMathExpr(expr.args[0]), storeBits: convertToMathExpr(expr.args[0]), signed: true, maxBits: undefined}
} else if (expr.name == 'uint' && expr.args.length == 1 && (expr.args[0] instanceof MathExpr || expr.args[0] instanceof NumberExpr || expr.args[0] instanceof NameExpr)) {
return { kind: 'TLBNumberType', bits: convertToMathExpr(expr.args[0]), storeBits: convertToMathExpr(expr.args[0]), signed: false, maxBits: undefined}
} else if (expr.name == 'bits' && expr.args.length == 1 && (expr.args[0] instanceof MathExpr || expr.args[0] instanceof NumberExpr || expr.args[0] instanceof NameExpr)) {
return { kind: 'TLBBitsType', bits: convertToMathExpr(expr.args[0]) }
} else {
let argumentTypes: TLBFieldType[] = []
expr.args.forEach((arg) => {
let thefield = getType(arg, fieldName, false, needArg, variableCombinatorName, variableSubStructName, constructor, fieldTypeName, argIndex, tlbCode);
argumentTypes.push(thefield)
});
return { kind: 'TLBNamedType', name: expr.name, arguments: argumentTypes}
}
} else if (expr instanceof NameExpr) {
let theNum;
if (expr.name == 'Int') {
return { kind: 'TLBNumberType', bits: new TLBNumberExpr(257), storeBits: new TLBNumberExpr(257), signed: true, maxBits: 257}
} else if (expr.name == 'Bits') {
return { kind: 'TLBBitsType', bits: new TLBNumberExpr(1023) }
} else if (expr.name == 'Bit') {
return { kind: 'TLBBitsType', bits: new TLBNumberExpr(1) }
} else if (expr.name == 'Uint') {
return { kind: 'TLBNumberType', bits: new TLBNumberExpr(257), storeBits: new TLBNumberExpr(257), signed: false, maxBits: 257}
} else if (expr.name == 'Any' || expr.name == 'Cell') {
return { kind: 'TLBCellType'}
} else if ((theNum = splitForTypeValue(expr.name, 'int')) != undefined) {
return { kind: 'TLBNumberType', bits: new TLBNumberExpr(theNum), storeBits: new TLBNumberExpr(theNum), signed: true, maxBits: theNum}
} else if ((theNum = splitForTypeValue(expr.name, 'uint')) != undefined) {
return { kind: 'TLBNumberType', bits: new TLBNumberExpr(theNum), storeBits: new TLBNumberExpr(theNum), signed: false, maxBits: theNum}
} else if ((theNum = splitForTypeValue(expr.name, 'bits')) != undefined) {
return { kind: 'TLBBitsType', bits: new TLBNumberExpr(theNum) }
} else if (expr.name == 'Bool') {
return { kind: 'TLBBoolType'}
} else if (expr.name == 'MsgAddressInt') {
return { kind: 'TLBAddressType' }
} else {
if (constructor.variablesMap.get(expr.name)?.type == '#') {
return {kind: 'TLBExprMathType', expr: new TLBVarExpr(expr.name)}
} else {
return {kind: 'TLBNamedType', name: expr.name, arguments: []}
}
}
} else if (expr instanceof NumberExpr) {
return {kind: 'TLBExprMathType', expr: new TLBNumberExpr(expr.num)}
} else if (expr instanceof NegateExpr && expr.expr instanceof NameExpr) { // TODO: handle other case
return {kind: 'TLBNegatedType', variableName: expr.expr.name}
} else if (expr instanceof CellRefExpr) {
let subExprInfo = getType(expr.expr, fieldName, true, true, variableCombinatorName, variableSubStructName, constructor, fieldTypeName, argIndex, tlbCode);
return {kind: 'TLBCellInsideType', value: subExprInfo}
} else if (expr instanceof MathExpr) {
if (fieldTypeName == '') {
if (expr.op == '*') {
let subExprInfo = getType(expr.right, fieldName, false, needArg, variableCombinatorName, variableSubStructName, constructor, fieldTypeName, argIndex, tlbCode)
return {kind: 'TLBMultipleType', times: convertToMathExpr(expr.left), value: subExprInfo}
} else {
throw new Error('')
}
} else {
return {kind: 'TLBExprMathType', expr: convertToMathExpr(expr)}
}
} else if (expr instanceof CondExpr) {
let subExprInfo = getType(expr.condExpr, fieldName, true, false, variableCombinatorName, variableSubStructName, constructor, fieldTypeName, argIndex, tlbCode);
if (expr.left instanceof NameExpr) {
let condition: TLBMathExpr = convertToMathExpr(expr.left);
if (expr.dotExpr != null) {
condition = new TLBBinaryOp(condition, new TLBBinaryOp(new TLBNumberExpr(1), new TLBNumberExpr(expr.dotExpr), '<<'), '&')
}
return {kind: 'TLBCondType', value: subExprInfo, condition: condition}
}
} else {
throw new Error('Expression not supported: ' + expr);
}
throw new Error('Type unknown')
}

import { firstLower, getSubStructName, goodVariableName } from "../utils";
import { getType } from "./handle_type";

export function fillFields(tlbCode: TLBCode) {
tlbCode.types.forEach(tlbType => {
Expand Down
111 changes: 111 additions & 0 deletions src/codegen/astbuilder/handle_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { BuiltinOneArgExpr, BuiltinZeroArgs, CellRefExpr, CombinatorExpr, CondExpr, MathExpr, NameExpr, NegateExpr, NumberExpr, Expression as ParserExpression } from "../../ast/nodes";
import { TLBBinaryOp, TLBCode, TLBConstructor, TLBFieldType, TLBMathExpr, TLBNumberExpr, TLBUnaryOp, TLBVarExpr } from "../ast";
import { convertToMathExpr, splitForTypeValue } from "../utils";


export function getType(expr: ParserExpression, fieldName: string, isField: boolean, needArg: boolean, variableCombinatorName: string, variableSubStructName: string, constructor: TLBConstructor, fieldTypeName: string, argIndex: number, tlbCode: TLBCode): TLBFieldType {
if (expr instanceof BuiltinZeroArgs) {
if (expr.name == '#') {
return { kind: 'TLBNumberType', bits: new TLBNumberExpr(32), storeBits: new TLBNumberExpr(32), signed: false, maxBits: 32 };
} else {
throw new Error('Expression not supported' + expr);
}
} else if (expr instanceof BuiltinOneArgExpr) {
if (expr.name.toString() == '##' || expr.name.toString() == '(##)') {
if (expr.arg instanceof NumberExpr) {
return { kind: 'TLBNumberType', bits: new TLBNumberExpr(expr.arg.num), storeBits: new TLBNumberExpr(expr.arg.num), signed: false, maxBits: expr.arg.num };
}
if (expr.arg instanceof NameExpr) {
let parameter = constructor.parametersMap.get(expr.arg.name);
if (!parameter || !parameter.variable.deriveExpr || !parameter.variable.initialExpr) {
throw new Error('');
}
return { kind: 'TLBNumberType', bits: parameter.variable.deriveExpr, storeBits: parameter.variable.initialExpr, signed: false, maxBits: undefined };
} // TODO: handle other cases
} else if (expr.name == '#<') {
if (expr.arg instanceof NumberExpr || expr.arg instanceof NameExpr) {
let bits = new TLBUnaryOp(new TLBBinaryOp(convertToMathExpr(expr.arg), new TLBNumberExpr(1), '-'), '.');
return { kind: 'TLBNumberType', bits: bits, storeBits: bits, signed: false, maxBits: 32 };
} // TODO: handle other cases
} else if (expr.name == '#<=') {
if (expr.arg instanceof NumberExpr || expr.arg instanceof NameExpr) {
let bits = new TLBUnaryOp(convertToMathExpr(expr.arg), '.');
return { kind: 'TLBNumberType', bits: bits, storeBits: bits, signed: false, maxBits: 32 };
} // TODO: handle other cases
}
} else if (expr instanceof CombinatorExpr) {
if (expr.name == 'int' && expr.args.length == 1 && (expr.args[0] instanceof MathExpr || expr.args[0] instanceof NumberExpr || expr.args[0] instanceof NameExpr)) {
return { kind: 'TLBNumberType', bits: convertToMathExpr(expr.args[0]), storeBits: convertToMathExpr(expr.args[0]), signed: true, maxBits: undefined };
} else if (expr.name == 'uint' && expr.args.length == 1 && (expr.args[0] instanceof MathExpr || expr.args[0] instanceof NumberExpr || expr.args[0] instanceof NameExpr)) {
return { kind: 'TLBNumberType', bits: convertToMathExpr(expr.args[0]), storeBits: convertToMathExpr(expr.args[0]), signed: false, maxBits: undefined };
} else if (expr.name == 'bits' && expr.args.length == 1 && (expr.args[0] instanceof MathExpr || expr.args[0] instanceof NumberExpr || expr.args[0] instanceof NameExpr)) {
return { kind: 'TLBBitsType', bits: convertToMathExpr(expr.args[0]) };
} else {
let argumentTypes: TLBFieldType[] = [];
expr.args.forEach((arg) => {
let thefield = getType(arg, fieldName, false, needArg, variableCombinatorName, variableSubStructName, constructor, fieldTypeName, argIndex, tlbCode);
argumentTypes.push(thefield);
});
return { kind: 'TLBNamedType', name: expr.name, arguments: argumentTypes };
}
} else if (expr instanceof NameExpr) {
let theNum;
if (expr.name == 'Int') {
return { kind: 'TLBNumberType', bits: new TLBNumberExpr(257), storeBits: new TLBNumberExpr(257), signed: true, maxBits: 257 };
} else if (expr.name == 'Bits') {
return { kind: 'TLBBitsType', bits: new TLBNumberExpr(1023) };
} else if (expr.name == 'Bit') {
return { kind: 'TLBBitsType', bits: new TLBNumberExpr(1) };
} else if (expr.name == 'Uint') {
return { kind: 'TLBNumberType', bits: new TLBNumberExpr(257), storeBits: new TLBNumberExpr(257), signed: false, maxBits: 257 };
} else if (expr.name == 'Any' || expr.name == 'Cell') {
return { kind: 'TLBCellType' };
} else if ((theNum = splitForTypeValue(expr.name, 'int')) != undefined) {
return { kind: 'TLBNumberType', bits: new TLBNumberExpr(theNum), storeBits: new TLBNumberExpr(theNum), signed: true, maxBits: theNum };
} else if ((theNum = splitForTypeValue(expr.name, 'uint')) != undefined) {
return { kind: 'TLBNumberType', bits: new TLBNumberExpr(theNum), storeBits: new TLBNumberExpr(theNum), signed: false, maxBits: theNum };
} else if ((theNum = splitForTypeValue(expr.name, 'bits')) != undefined) {
return { kind: 'TLBBitsType', bits: new TLBNumberExpr(theNum) };
} else if (expr.name == 'Bool') {
return { kind: 'TLBBoolType' };
} else if (expr.name == 'MsgAddressInt') {
return { kind: 'TLBAddressType' };
} else {
if (constructor.variablesMap.get(expr.name)?.type == '#') {
return { kind: 'TLBExprMathType', expr: new TLBVarExpr(expr.name) };
} else {
return { kind: 'TLBNamedType', name: expr.name, arguments: [] };
}
}
} else if (expr instanceof NumberExpr) {
return { kind: 'TLBExprMathType', expr: new TLBNumberExpr(expr.num) };
} else if (expr instanceof NegateExpr && expr.expr instanceof NameExpr) { // TODO: handle other case
return { kind: 'TLBNegatedType', variableName: expr.expr.name };
} else if (expr instanceof CellRefExpr) {
let subExprInfo = getType(expr.expr, fieldName, true, true, variableCombinatorName, variableSubStructName, constructor, fieldTypeName, argIndex, tlbCode);
return { kind: 'TLBCellInsideType', value: subExprInfo };
} else if (expr instanceof MathExpr) {
if (fieldTypeName == '') {
if (expr.op == '*') {
let subExprInfo = getType(expr.right, fieldName, false, needArg, variableCombinatorName, variableSubStructName, constructor, fieldTypeName, argIndex, tlbCode);
return { kind: 'TLBMultipleType', times: convertToMathExpr(expr.left), value: subExprInfo };
} else {
throw new Error('');
}
} else {
return { kind: 'TLBExprMathType', expr: convertToMathExpr(expr) };
}
} else if (expr instanceof CondExpr) {
let subExprInfo = getType(expr.condExpr, fieldName, true, false, variableCombinatorName, variableSubStructName, constructor, fieldTypeName, argIndex, tlbCode);
if (expr.left instanceof NameExpr) {
let condition: TLBMathExpr = convertToMathExpr(expr.left);
if (expr.dotExpr != null) {
condition = new TLBBinaryOp(condition, new TLBBinaryOp(new TLBNumberExpr(1), new TLBNumberExpr(expr.dotExpr), '<<'), '&');
}
return { kind: 'TLBCondType', value: subExprInfo, condition: condition };
}
} else {
throw new Error('Expression not supported: ' + expr);
}
throw new Error('Type unknown');
}
2 changes: 1 addition & 1 deletion src/codegen/generators/typescript/field_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fillConstructors, firstLower, getCurrentSlice, bitLen, convertToMathExp
import { handleType } from './type_handler'
import { addLoadProperty, getNegationDerivationFunctionBody, getParamVarExpr, sliceLoad } from './utils'
import { goodVariableName } from '../../utils'
import { getType } from '../../astbuilder/handle_field'
import { getType } from "../../astbuilder/handle_type"

export function handleField(field: TLBField, fieldDefinition: FieldDefinition, slicePrefix: Array<number>, tlbCode: TLBCode, constructor: TLBConstructor, constructorLoadStatements: Statement[], subStructStoreStatements: Statement[], subStructProperties: TypedIdentifier[], subStructLoadProperties: ObjectProperty[], variableCombinatorName: string, variableSubStructName: string, jsCodeFunctionsDeclarations: GenDeclaration[], fieldIndex: string) {
let currentSlice = getCurrentSlice(slicePrefix, 'slice');
Expand Down

0 comments on commit 875a27d

Please sign in to comment.