added withFunction

This commit is contained in:
2025-08-20 22:47:26 -06:00
parent 68e51cf8aa
commit 0a315c5615

View File

@@ -1,4 +1,13 @@
import { functionToType, Module, TypeUsage } from "../parse/ast"; import {
Function,
FunctionDeclaration,
functionToType,
Impl,
Module,
StructTypeDeclaration,
TraitTypeDeclaration,
TypeUsage,
} from "../parse/ast";
import { TypeSystem } from "./type_system"; import { TypeSystem } from "./type_system";
interface EnvImpl { interface EnvImpl {
@@ -25,6 +34,27 @@ interface Context {
environment: Record<string, NamedEntity>; environment: Record<string, NamedEntity>;
} }
function typeExists(ctx: Context, type: TypeUsage) {
if (type.typeUsage === "NamedTypeUsage") {
if (
!ctx.environment[type.name.text] ||
ctx.environment[type.name.text].namedEntity !== "NamedType"
) {
throw Error(`${type.name.text} is not a type.`);
}
}
if (type.typeUsage === "FunctionTypeUsage") {
for (const arg of type.arguments) {
typeExists(ctx, arg);
}
typeExists(ctx, type.returnType);
}
}
const deepCopy = <T>(o: T) => {
return JSON.parse(JSON.stringify(o)) as T;
};
class TypeChecker { class TypeChecker {
withModule = (module: Module, typeSystem: TypeSystem) => { withModule = (module: Module, typeSystem: TypeSystem) => {
const ctx: Context = { const ctx: Context = {
@@ -94,17 +124,46 @@ class TypeChecker {
// environment set up, actually recurse. // environment set up, actually recurse.
for (const item of module.items) { for (const item of module.items) {
if (item.moduleItem === "Function") { if (item.moduleItem === "Function") {
// this.withFunction(item, typeSystem); this.withFunction(ctx, item, typeSystem);
} }
if (item.moduleItem === "Impl") { if (item.moduleItem === "Impl") {
// this.withImpl(item, typeSystem); this.withImpl(ctx, item, typeSystem);
} }
if (item.moduleItem === "StructTypeDeclaration") { if (item.moduleItem === "StructTypeDeclaration") {
// this.withImpl(item, typeSystem); this.withStruct(ctx, item, typeSystem);
} }
if (item.moduleItem === "TraitTypeDeclaration") { if (item.moduleItem === "TraitTypeDeclaration") {
// this.withTrait(item, typeSystem); this.withTrait(ctx, item, typeSystem);
} }
} }
}; };
withFunction = (ctx: Context, fn: Function, typeSystem: TypeSystem) => {
this.withFunctionDeclaration(ctx, fn.declaration, typeSystem);
const fnCtx = deepCopy(ctx);
for (const arg of fn.declaration.arguments) {
fnCtx.environment[arg.name.text] = {
namedEntity: "Variable",
type: arg.type,
};
}
// this.withBlock(fnCtx, fn.block, typeSystem);
typeSystem.compare({
left: fn.declaration.returnType,
operation: { operation: "equals" },
right: fn.block.type,
});
};
withFunctionDeclaration = (ctx: Context, def: FunctionDeclaration, typeSystem: TypeSystem) => {
for (const arg of def.arguments) {
typeExists(ctx, arg.type);
}
typeExists(ctx, def.returnType);
};
withImpl = (ctx: Context, impl: Impl, typeSystem: TypeSystem) => {};
withStruct = (ctx: Context, struct: StructTypeDeclaration, typeSystem: TypeSystem) => {};
withTrait = (ctx: Context, trait: TraitTypeDeclaration, typeSystem: TypeSystem) => {};
} }