Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/chad-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import {
addLinkLib,
addLinkPath,
} from "./compiler.js";
import { LogLevel, logger } from "./utils/logger.js";
import {
LogLevel_Normal,
LogLevel_Verbose,
LogLevel_Debug,
LogLevel_Trace,
logger,
} from "./utils/logger.js";
import { runInit } from "./codegen/stdlib/init-templates.js";
import { ArgumentParser } from "./argparse.js";
import { parseWithTSAPI } from "./parser-ts/index.js";
Expand Down Expand Up @@ -291,10 +297,10 @@ if (
}

// Configure compiler options from parsed flags
let logLevel = LogLevel.Normal;
if (parser.getFlag("verbose")) logLevel = LogLevel.Verbose;
if (parser.getFlag("debug")) logLevel = LogLevel.Debug;
if (parser.getFlag("trace")) logLevel = LogLevel.Trace;
let logLevel = LogLevel_Normal;
if (parser.getFlag("verbose")) logLevel = LogLevel_Verbose;
if (parser.getFlag("debug")) logLevel = LogLevel_Debug;
if (parser.getFlag("trace")) logLevel = LogLevel_Trace;

if (parser.getFlag("skip-semantic-analysis")) setSkipSemanticAnalysis(true);
if (parser.getFlag("keep-temps")) setKeepTemps(true);
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/expressions/method-calls/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
UnaryNode,
} from "../../../ast/types.js";
import type { MethodCallGeneratorContext } from "../method-calls.js";
import { SymbolKind } from "../../infrastructure/symbol-table.js";
import { SymbolKind, SymbolKind_Boolean } from "../../infrastructure/symbol-table.js";

function emitPrint(
ctx: MethodCallGeneratorContext,
Expand Down Expand Up @@ -505,7 +505,7 @@ function emitSingleArg(
if (arg.type === "variable") {
const varName = (arg as VariableNode).name;
const varKind = ctx.symbolTable.getKind(varName);
if (varKind === SymbolKind.Boolean) {
if (varKind === SymbolKind_Boolean) {
const argValue = ctx.generateExpression(arg, params);
emitBooleanPrint(ctx, useStderr, argValue);
return;
Expand Down
50 changes: 47 additions & 3 deletions src/codegen/infrastructure/base-generator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,53 @@
import { Expression } from "../../ast/types.js";
import { SymbolTable, SymbolKind, SymbolMetadata } from "./symbol-table.js";
import {
SymbolTable,
SymbolKind,
SymbolMetadata,
SymbolKind_Number,
SymbolKind_String,
SymbolKind_Boolean,
SymbolKind_Array,
SymbolKind_StringArray,
SymbolKind_BooleanArray,
SymbolKind_ObjectArray,
SymbolKind_Object,
SymbolKind_Map,
SymbolKind_Set,
SymbolKind_Class,
SymbolKind_Regex,
SymbolKind_JSON,
SymbolKind_ProcessArgv,
SymbolKind_Closure,
SymbolKind_Pointer,
SymbolKind_Uint8Array,
SymbolKind_Url,
SymbolKind_UrlSearchParams,
} from "./symbol-table.js";
import type { ResolvedType } from "./type-system.js";

// Re-export for convenience
export { SymbolTable, SymbolKind };
export {
SymbolTable,
SymbolKind,
SymbolKind_Number,
SymbolKind_String,
SymbolKind_Boolean,
SymbolKind_Array,
SymbolKind_StringArray,
SymbolKind_BooleanArray,
SymbolKind_ObjectArray,
SymbolKind_Object,
SymbolKind_Map,
SymbolKind_Set,
SymbolKind_Class,
SymbolKind_Regex,
SymbolKind_JSON,
SymbolKind_ProcessArgv,
SymbolKind_Closure,
SymbolKind_Pointer,
SymbolKind_Uint8Array,
SymbolKind_Url,
SymbolKind_UrlSearchParams,
};

// ============================================
// BASE GENERATOR - Shared state and utilities
Expand Down
63 changes: 37 additions & 26 deletions src/codegen/infrastructure/function-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ import {
} from "../../ast/types.js";
import {
SymbolKind,
SymbolKind_Number,
SymbolKind_String,
SymbolKind_Boolean,
SymbolKind_Array,
SymbolKind_StringArray,
SymbolKind_ObjectArray,
SymbolKind_Object,
SymbolKind_Map,
SymbolKind_Set,
SymbolKind_Class,
SymbolKind_Pointer,
SymbolTable,
createPointerAllocaMetadata,
createInterfacePointerAllocaMetadata,
Expand Down Expand Up @@ -304,9 +315,9 @@ export class FunctionGenerator {

if (llvmType === "i8*") {
if (paramTypes[i] === "string") {
this.ctx.defineVariable(paramName, allocaReg, "i8*", SymbolKind.String, "local");
this.ctx.defineVariable(paramName, allocaReg, "i8*", SymbolKind_String, "local");
} else if (paramName === "nodePtr" || paramName === "treePtr") {
this.ctx.defineVariable(paramName, allocaReg, "i8*", SymbolKind.Pointer, "local");
this.ctx.defineVariable(paramName, allocaReg, "i8*", SymbolKind_Pointer, "local");
} else {
const ast = this.ctx.getAst();
let strippedParamType = paramTypes[i];
Expand Down Expand Up @@ -371,7 +382,7 @@ export class FunctionGenerator {
paramName,
allocaReg,
"i8*",
SymbolKind.Object,
SymbolKind_Object,
"local",
createObjectMetadataWithInterface({ keys: biKeys, types: biTypes }, ptype),
);
Expand All @@ -392,7 +403,7 @@ export class FunctionGenerator {
paramName,
allocaReg,
"i8*",
SymbolKind.Class,
SymbolKind_Class,
"local",
createClassMetadata({ className: classDefName }),
);
Expand All @@ -418,7 +429,7 @@ export class FunctionGenerator {
paramName,
allocaReg,
"i8*",
SymbolKind.Object,
SymbolKind_Object,
"local",
createObjectMetadataWithInterface({ keys, types }, paramTypes[i]),
);
Expand All @@ -427,12 +438,12 @@ export class FunctionGenerator {
paramName,
allocaReg,
"i8*",
SymbolKind.Object,
SymbolKind_Object,
"local",
createObjectMetadataWithInterface(typeAliasCommonProps, paramTypes[i]),
);
} else {
this.ctx.defineVariable(paramName, allocaReg, "i8*", SymbolKind.Object, "local");
this.ctx.defineVariable(paramName, allocaReg, "i8*", SymbolKind_Object, "local");
}
}
this.ctx.emit(`${allocaReg} = alloca i8*`);
Expand All @@ -446,7 +457,7 @@ export class FunctionGenerator {
paramName,
allocaReg,
"%StringArray*",
SymbolKind.StringArray,
SymbolKind_StringArray,
"local",
createPointerAllocaMetadata(),
);
Expand All @@ -461,7 +472,7 @@ export class FunctionGenerator {
paramName,
allocaReg,
"%Array*",
SymbolKind.Array,
SymbolKind_Array,
"local",
createPointerAllocaMetadata(),
);
Expand All @@ -482,7 +493,7 @@ export class FunctionGenerator {
paramName,
allocaReg,
"%ObjectArray*",
SymbolKind.ObjectArray,
SymbolKind_ObjectArray,
"local",
createInterfacePointerAllocaMetadata(elementType),
);
Expand All @@ -491,7 +502,7 @@ export class FunctionGenerator {
paramName,
allocaReg,
"%ObjectArray*",
SymbolKind.ObjectArray,
SymbolKind_ObjectArray,
"local",
createPointerAllocaMetadata(),
);
Expand All @@ -507,7 +518,7 @@ export class FunctionGenerator {
paramName,
allocaReg,
"%Set*",
SymbolKind.Set,
SymbolKind_Set,
"local",
createPointerAllocaMetadata(),
);
Expand All @@ -522,7 +533,7 @@ export class FunctionGenerator {
paramName,
allocaReg,
"%StringSet*",
SymbolKind.Set,
SymbolKind_Set,
"local",
createPointerAllocaMetadata(),
);
Expand All @@ -544,7 +555,7 @@ export class FunctionGenerator {
paramName,
allocaReg,
"%StringMap*",
SymbolKind.Map,
SymbolKind_Map,
"local",
mapMeta,
);
Expand All @@ -564,7 +575,7 @@ export class FunctionGenerator {
paramName,
allocaReg,
llvmType,
SymbolKind.Object,
SymbolKind_Object,
"local",
createInterfacePointerAllocaMetadata(interfaceName),
);
Expand All @@ -575,7 +586,7 @@ export class FunctionGenerator {
this.ctx.emit(`store ${llvmType} %arg${i}, ${llvmType}* ${allocaReg}`);
}
} else {
this.ctx.defineVariable(paramName, allocaReg, "double", SymbolKind.Number, "local");
this.ctx.defineVariable(paramName, allocaReg, "double", SymbolKind_Number, "local");
this.ctx.emit(`${allocaReg} = alloca double`);
if (isOptional && hasOptionalParams) {
this.generateOptionalParamInit(i, allocaReg, llvmType, paramInfo!, funcParams);
Expand Down Expand Up @@ -814,18 +825,18 @@ export class FunctionGenerator {
}

private llvmTypeToSymbolKindPrimitive(llvmType: string): number | null {
if (llvmType === "double") return SymbolKind.Number;
if (llvmType === "i8*") return SymbolKind.String;
if (llvmType === "%Array*") return SymbolKind.Array;
if (llvmType === "%ObjectArray*") return SymbolKind.ObjectArray;
if (llvmType === "double") return SymbolKind_Number;
if (llvmType === "i8*") return SymbolKind_String;
if (llvmType === "%Array*") return SymbolKind_Array;
if (llvmType === "%ObjectArray*") return SymbolKind_ObjectArray;
return null;
}

private llvmTypeToSymbolKindCollection(llvmType: string): number | null {
if (llvmType === "%StringArray*") return SymbolKind.StringArray;
if (llvmType === "%Map*") return SymbolKind.Map;
if (llvmType === "%StringMap*") return SymbolKind.Map;
if (llvmType === "%Set*") return SymbolKind.Set;
if (llvmType === "%StringArray*") return SymbolKind_StringArray;
if (llvmType === "%Map*") return SymbolKind_Map;
if (llvmType === "%StringMap*") return SymbolKind_Map;
if (llvmType === "%Set*") return SymbolKind_Set;
return null;
}

Expand All @@ -834,8 +845,8 @@ export class FunctionGenerator {
if (prim !== null) return prim;
const coll = this.llvmTypeToSymbolKindCollection(llvmType);
if (coll !== null) return coll;
if (llvmType === "%StringSet*") return SymbolKind.Set;
return SymbolKind.Object;
if (llvmType === "%StringSet*") return SymbolKind_Set;
return SymbolKind_Object;
}

private getUnionCommonFields(memberNames: string[]): { keys: string[]; types: string[] } {
Expand Down
7 changes: 4 additions & 3 deletions src/codegen/infrastructure/interface-allocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "../../ast/types.js";
import {
SymbolKind,
SymbolKind_Object,
SymbolTable,
ObjectMetadata,
SymbolMetadata,
Expand Down Expand Up @@ -194,7 +195,7 @@ export class InterfaceAllocator {
stmt.name,
allocaReg,
"i8*",
SymbolKind.Object,
SymbolKind_Object,
"local",
createObjectMetadataWithInterface({ keys, types, tsTypes }, interfaceName),
);
Expand Down Expand Up @@ -252,7 +253,7 @@ export class InterfaceAllocator {
stmt.name,
allocaReg,
"i8*",
SymbolKind.Object,
SymbolKind_Object,
"local",
createObjectMetadataWithInterface(
{ keys: reorderedKeys, types: reorderedTypes, tsTypes: reorderedTsTypes },
Expand Down Expand Up @@ -289,7 +290,7 @@ export class InterfaceAllocator {
stmt.name,
allocaReg,
"i8*",
SymbolKind.Object,
SymbolKind_Object,
"local",
createObjectMetadataWithInterface({ keys, types, tsTypes }, interfaceName),
);
Expand Down
Loading
Loading