more cleanup

This commit is contained in:
nora 2023-12-15 20:28:47 +01:00
parent b273b20a75
commit 3ab116d7f0
4 changed files with 44 additions and 41 deletions

35
src/typeck/base.ts Normal file
View file

@ -0,0 +1,35 @@
import { ItemId, Pkg, Resolved, Ty } from "../ast";
import { GlobalContext } from "../context";
import { CompilerError, ErrorEmitted } from "../error";
import { ComplexMap } from "../utils";
export type TypeckCtx = {
gcx: GlobalContext;
/**
* A cache of all item types.
* Starts off as undefined, then gets set to null
* while computing the type (for cycle detection) and
* afterwards, we get the ty.
*/
itemTys: ComplexMap<ItemId, Ty | null>;
ast: Pkg<Resolved>;
};
export function mkTyFn(params: Ty[], returnTy: Ty): Ty {
return { kind: "fn", params, returnTy };
}
export function tyError(cx: TypeckCtx, err: CompilerError): Ty {
return {
kind: "error",
err: emitError(cx, err),
};
}
export function tyErrorFrom(prev: { err: ErrorEmitted }): Ty {
return { kind: "error", err: prev.err };
}
export function emitError(cx: TypeckCtx, err: CompilerError): ErrorEmitted {
return cx.gcx.error.emit(err);
}

View file

@ -28,14 +28,10 @@ import {
} from "../ast"; } from "../ast";
import { CompilerError, ErrorEmitted, Span, unreachable } from "../error"; import { CompilerError, ErrorEmitted, Span, unreachable } from "../error";
import { printTy } from "../printer"; import { printTy } from "../printer";
import { TypeckCtx, emitError, mkTyFn, tyError, tyErrorFrom } from "./base";
import { InferContext } from "./infer"; import { InferContext } from "./infer";
import { import {
TypeckCtx,
emitError,
lowerAstTy, lowerAstTy,
mkTyFn,
tyError,
tyErrorFrom,
typeOfItem, typeOfItem,
} from "./item"; } from "./item";

View file

@ -17,9 +17,10 @@ import {
import { GlobalContext } from "../context"; import { GlobalContext } from "../context";
import { CompilerError, ErrorEmitted, Span } from "../error"; import { CompilerError, ErrorEmitted, Span } from "../error";
import { ComplexMap } from "../utils"; import { ComplexMap } from "../utils";
import { emitError } from "./base";
import { checkBody, exprError } from "./expr"; import { checkBody, exprError } from "./expr";
import { InferContext } from "./infer"; import { InferContext } from "./infer";
import { emitError, typeOfItem } from "./item"; import { typeOfItem } from "./item";
export function typeck( export function typeck(
gcx: GlobalContext, gcx: GlobalContext,

View file

@ -1,5 +1,4 @@
import { import {
Pkg,
ItemId, ItemId,
Resolved, Resolved,
Ty, Ty,
@ -12,41 +11,9 @@ import {
Type, Type,
substituteTy, substituteTy,
} from "../ast"; } from "../ast";
import { GlobalContext } from "../context"; import { CompilerError, Span } from "../error";
import { CompilerError, ErrorEmitted, Span } from "../error";
import { printTy } from "../printer"; import { printTy } from "../printer";
import { ComplexMap } from "../utils"; import { TypeckCtx, tyError, tyErrorFrom } from "./base";
export type TypeckCtx = {
gcx: GlobalContext;
/**
* A cache of all item types.
* Starts off as undefined, then gets set to null
* while computing the type (for cycle detection) and
* afterwards, we get the ty.
*/
itemTys: ComplexMap<ItemId, Ty | null>;
ast: Pkg<Resolved>;
};
export function mkTyFn(params: Ty[], returnTy: Ty): Ty {
return { kind: "fn", params, returnTy };
}
export function tyError(cx: TypeckCtx, err: CompilerError): Ty {
return {
kind: "error",
err: emitError(cx, err),
};
}
export function tyErrorFrom(prev: { err: ErrorEmitted }): Ty {
return { kind: "error", err: prev.err };
}
export function emitError(cx: TypeckCtx, err: CompilerError): ErrorEmitted {
return cx.gcx.error.emit(err);
}
function builtinAsTy(cx: TypeckCtx, name: string, span: Span): Ty { function builtinAsTy(cx: TypeckCtx, name: string, span: Span): Ty {
switch (name) { switch (name) {
@ -68,6 +35,10 @@ function builtinAsTy(cx: TypeckCtx, name: string, span: Span): Ty {
} }
} }
/**
* Lowers the AST representation of a type into its resolved Ty representation.
* Will also validate the type, for example ensuring that generic arguments match up.
*/
// TODO: Cleanup, maybe get the ident switch into this function because typeOfItem is unused. // TODO: Cleanup, maybe get the ident switch into this function because typeOfItem is unused.
export function lowerAstTy(cx: TypeckCtx, type: Type<Resolved>): Ty { export function lowerAstTy(cx: TypeckCtx, type: Type<Resolved>): Ty {
switch (type.kind) { switch (type.kind) {