mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-14 08:25:02 +01:00
more cleanup
This commit is contained in:
parent
b273b20a75
commit
3ab116d7f0
4 changed files with 44 additions and 41 deletions
35
src/typeck/base.ts
Normal file
35
src/typeck/base.ts
Normal 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);
|
||||
}
|
||||
|
|
@ -28,14 +28,10 @@ import {
|
|||
} from "../ast";
|
||||
import { CompilerError, ErrorEmitted, Span, unreachable } from "../error";
|
||||
import { printTy } from "../printer";
|
||||
import { TypeckCtx, emitError, mkTyFn, tyError, tyErrorFrom } from "./base";
|
||||
import { InferContext } from "./infer";
|
||||
import {
|
||||
TypeckCtx,
|
||||
emitError,
|
||||
lowerAstTy,
|
||||
mkTyFn,
|
||||
tyError,
|
||||
tyErrorFrom,
|
||||
typeOfItem,
|
||||
} from "./item";
|
||||
|
||||
|
|
|
|||
|
|
@ -17,9 +17,10 @@ import {
|
|||
import { GlobalContext } from "../context";
|
||||
import { CompilerError, ErrorEmitted, Span } from "../error";
|
||||
import { ComplexMap } from "../utils";
|
||||
import { emitError } from "./base";
|
||||
import { checkBody, exprError } from "./expr";
|
||||
import { InferContext } from "./infer";
|
||||
import { emitError, typeOfItem } from "./item";
|
||||
import { typeOfItem } from "./item";
|
||||
|
||||
export function typeck(
|
||||
gcx: GlobalContext,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import {
|
||||
Pkg,
|
||||
ItemId,
|
||||
Resolved,
|
||||
Ty,
|
||||
|
|
@ -12,41 +11,9 @@ import {
|
|||
Type,
|
||||
substituteTy,
|
||||
} from "../ast";
|
||||
import { GlobalContext } from "../context";
|
||||
import { CompilerError, ErrorEmitted, Span } from "../error";
|
||||
import { CompilerError, Span } from "../error";
|
||||
import { printTy } from "../printer";
|
||||
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);
|
||||
}
|
||||
import { TypeckCtx, tyError, tyErrorFrom } from "./base";
|
||||
|
||||
function builtinAsTy(cx: TypeckCtx, name: string, span: Span): Ty {
|
||||
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.
|
||||
export function lowerAstTy(cx: TypeckCtx, type: Type<Resolved>): Ty {
|
||||
switch (type.kind) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue