diff --git a/generate_all.ts b/generate_all.ts index fd92b33..8b00f6a 100644 --- a/generate_all.ts +++ b/generate_all.ts @@ -6,7 +6,7 @@ import { parse } from './src' import { ast } from './src' import { generate } from './src/codegen/main' import { Program } from './src/ast/nodes' -import { CodeBuilder } from './src/codegen/CodeBuilder' +import { CodeBuilder } from './src/codegen/generators/CodeBuilder' const fixturesDir = path.resolve(__dirname, 'tests/fixtures') diff --git a/src/codegen/field_handler.ts b/src/codegen/field_handler.ts index e489e09..71cbf51 100644 --- a/src/codegen/field_handler.ts +++ b/src/codegen/field_handler.ts @@ -2,14 +2,14 @@ import { BuiltinZeroArgs, FieldCurlyExprDef, FieldNamedDef, Program, Declaration import { tIdentifier, tArrowFunctionExpression, tArrowFunctionType, tBinaryExpression, tBinaryNumericLiteral, tDeclareVariable, tExpressionStatement, tFunctionCall, tFunctionDeclaration, tIfStatement, tImportDeclaration, tMemberExpression, tNumericLiteral, tObjectExpression, tObjectProperty, tReturnStatement, tStringLiteral, tStructDeclaration, tTypeParametersExpression, tTypeWithParameters, tTypedIdentifier, GenDeclaration, tUnionTypeDeclaration, toCode, TypeWithParameters, ArrowFunctionExpression, FunctionDeclaration } from './tsgen' import { TLBMathExpr, TLBVarExpr, TLBNumberExpr, TLBBinaryOp, TLBCode, TLBType, TLBConstructor, TLBParameter, TLBVariable } from './ast' import { Expression, Statement, Identifier, BinaryExpression, ASTNode, TypeExpression, TypeParametersExpression, ObjectProperty, TypedIdentifier } from './tsgen' -import { fillConstructors, firstLower, getCurrentSlice, bitLen, convertToMathExpr, splitForTypeValue, deriveMathExpression } from './util' -import { getCondition } from "./helpers" -import { getTypeParametersExpression } from "./helpers" -import { convertToAST } from "./helpers" +import { fillConstructors, firstLower, getCurrentSlice, bitLen, convertToMathExpr, splitForTypeValue, deriveMathExpression } from './utils' +import { getCondition } from "./generators/typescript/utils" +import { getTypeParametersExpression } from "./generators/typescript/utils" +import { convertToAST } from "./generators/typescript/utils" import { constructorNodes } from '../parsing' import { handleType } from './type_handler' -import { addLoadProperty, getNegationDerivationFunctionBody, getParamVarExpr, sliceLoad } from './helpers' -import { goodVariableName } from './util' +import { addLoadProperty, getNegationDerivationFunctionBody, getParamVarExpr, sliceLoad } from './generators/typescript/utils' +import { goodVariableName } from './utils' export function handleField(field: FieldDefinition, slicePrefix: Array, 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'); diff --git a/src/codegen/CodeBuilder.ts b/src/codegen/generators/CodeBuilder.ts similarity index 100% rename from src/codegen/CodeBuilder.ts rename to src/codegen/generators/CodeBuilder.ts diff --git a/src/codegen/helpers.ts b/src/codegen/generators/typescript/utils.ts similarity index 96% rename from src/codegen/helpers.ts rename to src/codegen/generators/typescript/utils.ts index 6dc860e..e20df81 100644 --- a/src/codegen/helpers.ts +++ b/src/codegen/generators/typescript/utils.ts @@ -1,8 +1,8 @@ -import { BuiltinZeroArgs, FieldCurlyExprDef, FieldNamedDef, Program, BuiltinOneArgExpr, NumberExpr, NameExpr, CombinatorExpr, FieldBuiltinDef, MathExpr, SimpleExpr, NegateExpr, CellRefExpr, FieldDefinition, FieldAnonymousDef, CondExpr, CompareExpr, Expression as ParserExpression } from '../../src/ast/nodes' -import { tIdentifier, tArrowFunctionExpression, tArrowFunctionType, tBinaryExpression, tBinaryNumericLiteral, tDeclareVariable, tExpressionStatement, tFunctionCall, tFunctionDeclaration, tIfStatement, tImportDeclaration, tMemberExpression, tNumericLiteral, tObjectExpression, tObjectProperty, tReturnStatement, tStringLiteral, tStructDeclaration, tTypeWithParameters, tTypedIdentifier, tUnionTypeDeclaration, toCode, TypeWithParameters, ArrowFunctionExpression, tForCycle, tTypeParametersExpression } from './tsgen' -import { TLBMathExpr, TLBVarExpr, TLBNumberExpr, TLBBinaryOp, TLBCode, TLBType, TLBConstructor, TLBParameter, TLBVariable } from './ast' -import { Expression, Statement, Identifier, BinaryExpression, ASTNode, TypeExpression, TypeParametersExpression, ObjectProperty, TypedIdentifier } from './tsgen' -import { getCalculatedExpression, getSubStructName, fillConstructors, firstLower, getCurrentSlice, bitLen, convertToMathExpr, splitForTypeValue, deriveMathExpression, goodVariableName } from './util' +import { BuiltinZeroArgs, FieldCurlyExprDef, FieldNamedDef, Program, BuiltinOneArgExpr, NumberExpr, NameExpr, CombinatorExpr, FieldBuiltinDef, MathExpr, SimpleExpr, NegateExpr, CellRefExpr, FieldDefinition, FieldAnonymousDef, CondExpr, CompareExpr, Expression as ParserExpression } from '../../../ast/nodes' +import { tIdentifier, tArrowFunctionExpression, tArrowFunctionType, tBinaryExpression, tBinaryNumericLiteral, tDeclareVariable, tExpressionStatement, tFunctionCall, tFunctionDeclaration, tIfStatement, tImportDeclaration, tMemberExpression, tNumericLiteral, tObjectExpression, tObjectProperty, tReturnStatement, tStringLiteral, tStructDeclaration, tTypeWithParameters, tTypedIdentifier, tUnionTypeDeclaration, toCode, TypeWithParameters, ArrowFunctionExpression, tForCycle, tTypeParametersExpression } from '../../tsgen' +import { TLBMathExpr, TLBVarExpr, TLBNumberExpr, TLBBinaryOp, TLBCode, TLBType, TLBConstructor, TLBParameter, TLBVariable } from '../../ast' +import { Expression, Statement, Identifier, BinaryExpression, ASTNode, TypeExpression, TypeParametersExpression, ObjectProperty, TypedIdentifier } from '../../tsgen' +import { getCalculatedExpression, getSubStructName, fillConstructors, firstLower, getCurrentSlice, bitLen, convertToMathExpr, splitForTypeValue, deriveMathExpression, goodVariableName } from '../../utils' export function sliceLoad(slicePrefix: number[], currentSlice: string) { diff --git a/src/codegen/main.ts b/src/codegen/main.ts index 476e149..f34ffb8 100644 --- a/src/codegen/main.ts +++ b/src/codegen/main.ts @@ -2,17 +2,17 @@ import { BuiltinZeroArgs, FieldCurlyExprDef, FieldNamedDef, Program, Declaration import { tIdentifier, tArrowFunctionExpression, tArrowFunctionType, tBinaryExpression, tBinaryNumericLiteral, tDeclareVariable, tExpressionStatement, tFunctionCall, tFunctionDeclaration, tIfStatement, tImportDeclaration, tMemberExpression, tNumericLiteral, tObjectExpression, tObjectProperty, tReturnStatement, tStringLiteral, tStructDeclaration, tTypeParametersExpression, tTypeWithParameters, tTypedIdentifier, tUnionTypeDeclaration, toCode, GenDeclaration, TypeWithParameters, ArrowFunctionExpression, tUnionTypeExpression, tUnaryOpExpression, StructDeclaration, FunctionDeclaration, tComment } from './tsgen' import { TLBMathExpr, TLBVarExpr, TLBNumberExpr, TLBBinaryOp, TLBCode, TLBType, TLBConstructor, TLBParameter, TLBVariable, TLBConstructorTag } from './ast' import { Expression, Statement, Identifier, BinaryExpression, ASTNode, TypeExpression, TypeParametersExpression, ObjectProperty, TypedIdentifier } from './tsgen' -import { fillConstructors, firstLower, getCurrentSlice, bitLen, convertToMathExpr, splitForTypeValue, deriveMathExpression, getStringDeclaration } from './util' -import { getCondition } from "./helpers" -import { getTypeParametersExpression } from "./helpers" -import { convertToAST } from "./helpers" +import { fillConstructors, firstLower, getCurrentSlice, bitLen, convertToMathExpr, splitForTypeValue, deriveMathExpression, getStringDeclaration } from './utils' +import { getCondition } from "./generators/typescript/utils" +import { getTypeParametersExpression } from "./generators/typescript/utils" +import { convertToAST } from "./generators/typescript/utils" import { constructorNodes } from '../parsing' import { handleType } from './type_handler' import { handleField } from './field_handler' -import { getParamVarExpr } from './helpers' -import { getSubStructName } from './util' -import { goodVariableName } from './util' -import { CodeBuilder } from './CodeBuilder' +import { getParamVarExpr } from './generators/typescript/utils' +import { getSubStructName } from './utils' +import { goodVariableName } from './utils' +import { CodeBuilder } from './generators/CodeBuilder' export function generate(tree: Program, input: string) { let jsCodeDeclarations: GenDeclaration[] = [] diff --git a/src/codegen/tsgen.ts b/src/codegen/tsgen.ts index f1269ca..18409f4 100644 --- a/src/codegen/tsgen.ts +++ b/src/codegen/tsgen.ts @@ -1,4 +1,4 @@ -import { CodeBuilder } from "./CodeBuilder"; +import { CodeBuilder } from "./generators/CodeBuilder"; import util from 'util' export interface ASTNode { diff --git a/src/codegen/type_handler.ts b/src/codegen/type_handler.ts index c0263af..a495fd5 100644 --- a/src/codegen/type_handler.ts +++ b/src/codegen/type_handler.ts @@ -2,12 +2,12 @@ import { BuiltinZeroArgs, FieldCurlyExprDef, FieldNamedDef, Program, Declaration import { tIdentifier, tArrowFunctionExpression, tArrowFunctionType, tBinaryExpression, tBinaryNumericLiteral, tDeclareVariable, tExpressionStatement, tFunctionCall, tFunctionDeclaration, tIfStatement, tImportDeclaration, tMemberExpression, tNumericLiteral, tObjectExpression, tObjectProperty, tReturnStatement, tStringLiteral, tStructDeclaration, tTypeParametersExpression, tTypeWithParameters, tTypedIdentifier, tUnionTypeDeclaration, toCode, TypeWithParameters, ArrowFunctionExpression, tMultiStatement, tUnionTypeExpression, tTernaryExpression, FunctionDeclaration, GenDeclaration } from './tsgen' import { TLBMathExpr, TLBVarExpr, TLBNumberExpr, TLBBinaryOp, TLBCode, TLBType, TLBConstructor, TLBParameter, TLBVariable } from './ast' import { Expression, Statement, Identifier, BinaryExpression, ASTNode, TypeExpression, TypeParametersExpression, ObjectProperty, TypedIdentifier } from './tsgen' -import { fillConstructors, firstLower, getCurrentSlice, bitLen, convertToMathExpr, splitForTypeValue, deriveMathExpression } from './util' -import { getCondition } from "./helpers" -import { getTypeParametersExpression } from "./helpers" -import { convertToAST } from "./helpers" -import { getNegationDerivationFunctionBody, getParamVarExpr, getVarExprByName, simpleCycle, sliceLoad } from './helpers' -import { goodVariableName } from './util' +import { fillConstructors, firstLower, getCurrentSlice, bitLen, convertToMathExpr, splitForTypeValue, deriveMathExpression } from './utils' +import { getCondition } from "./generators/typescript/utils" +import { getTypeParametersExpression } from "./generators/typescript/utils" +import { convertToAST } from "./generators/typescript/utils" +import { getNegationDerivationFunctionBody, getParamVarExpr, getVarExprByName, simpleCycle, sliceLoad } from './generators/typescript/utils' +import { goodVariableName } from './utils' type FieldInfoType = { typeParamExpr: TypeExpression | undefined diff --git a/src/codegen/util.ts b/src/codegen/utils.ts similarity index 99% rename from src/codegen/util.ts rename to src/codegen/utils.ts index 8766929..ecfbdd6 100644 --- a/src/codegen/util.ts +++ b/src/codegen/utils.ts @@ -1,5 +1,5 @@ import { SimpleExpr, NameExpr, NumberExpr, MathExpr, FieldBuiltinDef, NegateExpr, Declaration, CompareExpr, FieldCurlyExprDef, FieldNamedDef } from "../ast/nodes"; -import { TLBMathExpr, TLBVarExpr, TLBNumberExpr, TLBBinaryOp, TLBCode, TLBType, TLBConstructorTag, TLBConstructor, TLBParameter, TLBVariable } from "../codegen/ast" +import { TLBMathExpr, TLBVarExpr, TLBNumberExpr, TLBBinaryOp, TLBCode, TLBType, TLBConstructorTag, TLBConstructor, TLBParameter, TLBVariable } from "./ast" import { tArrowFunctionExpression, tArrowFunctionType, tBinaryNumericLiteral, tDeclareVariable, tExpressionStatement, tFunctionCall, tFunctionDeclaration, tIfStatement, tImportDeclaration, tObjectExpression, tObjectProperty, tReturnStatement, tStringLiteral, tStructDeclaration, tTypeWithParameters, tTypedIdentifier, tUnionTypeDeclaration, toCode } from './tsgen' import util from 'util' import * as crc32 from "crc-32";