mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-14 16:35:03 +01:00
refactor items
This commit is contained in:
parent
cdbb26352e
commit
b021a9e218
7 changed files with 199 additions and 257 deletions
88
src/ast.ts
88
src/ast.ts
|
|
@ -98,38 +98,21 @@ export class ItemId {
|
|||
}
|
||||
|
||||
export type ItemKind<P extends Phase> =
|
||||
| {
|
||||
kind: "function";
|
||||
node: ItemFunction<P>;
|
||||
}
|
||||
| {
|
||||
kind: "type";
|
||||
node: ItemType<P>;
|
||||
}
|
||||
| {
|
||||
kind: "import";
|
||||
node: ItemImport<P>;
|
||||
}
|
||||
| {
|
||||
kind: "mod";
|
||||
node: ItemMod<P>;
|
||||
}
|
||||
| {
|
||||
kind: "extern";
|
||||
node: ItemExtern;
|
||||
}
|
||||
| {
|
||||
kind: "global";
|
||||
node: ItemGlobal<P>;
|
||||
};
|
||||
| ItemFunction<P>
|
||||
| ItemType<P>
|
||||
| ItemImport<P>
|
||||
| ItemMod<P>
|
||||
| ItemExtern
|
||||
| ItemGlobal<P>;
|
||||
|
||||
export type Item<P extends Phase> = ItemKind<P> & {
|
||||
span: Span;
|
||||
id: ItemId;
|
||||
name: string;
|
||||
} & P["defPath"];
|
||||
|
||||
export type ItemFunction<P extends Phase> = {
|
||||
name: string;
|
||||
kind: "function";
|
||||
params: FunctionArg<P>[];
|
||||
body: Expr<P>;
|
||||
returnType?: Type<P>;
|
||||
|
|
@ -143,7 +126,7 @@ export type FunctionArg<P extends Phase> = {
|
|||
};
|
||||
|
||||
export type ItemType<P extends Phase> = {
|
||||
name: string;
|
||||
kind: "type";
|
||||
type: TypeDefKind<P>;
|
||||
ty?: TyStruct;
|
||||
};
|
||||
|
|
@ -164,23 +147,23 @@ export type FieldDef<P extends Phase> = {
|
|||
};
|
||||
|
||||
export type ItemImport<P extends Phase> = {
|
||||
kind: "import";
|
||||
module: StringLiteral;
|
||||
func: StringLiteral;
|
||||
name: string;
|
||||
params: FunctionArg<P>[];
|
||||
returnType?: Type<P>;
|
||||
ty?: TyFn;
|
||||
};
|
||||
|
||||
export type ItemMod<P extends Phase> = {
|
||||
name: string;
|
||||
kind: "mod";
|
||||
contents: Item<P>[];
|
||||
};
|
||||
|
||||
export type ItemExtern = { name: string };
|
||||
export type ItemExtern = { kind: "extern" };
|
||||
|
||||
export type ItemGlobal<P extends Phase> = {
|
||||
name: string;
|
||||
kind: "global";
|
||||
type: Type<P>;
|
||||
init: Expr<P>;
|
||||
ty?: Ty;
|
||||
|
|
@ -616,7 +599,7 @@ export function superFoldItem<From extends Phase, To extends Phase>(
|
|||
): Item<To> {
|
||||
switch (item.kind) {
|
||||
case "function": {
|
||||
const args = item.node.params.map(({ name, type, span }) => ({
|
||||
const args = item.params.map(({ name, type, span }) => ({
|
||||
name,
|
||||
type: folder.type(type),
|
||||
span,
|
||||
|
|
@ -625,16 +608,14 @@ export function superFoldItem<From extends Phase, To extends Phase>(
|
|||
return {
|
||||
...item,
|
||||
kind: "function",
|
||||
node: {
|
||||
name: item.node.name,
|
||||
params: args,
|
||||
body: folder.expr(item.node.body),
|
||||
returnType: item.node.returnType && folder.type(item.node.returnType),
|
||||
},
|
||||
name: item.name,
|
||||
params: args,
|
||||
body: folder.expr(item.body),
|
||||
returnType: item.returnType && folder.type(item.returnType),
|
||||
};
|
||||
}
|
||||
case "type": {
|
||||
const typeKind = item.node.type;
|
||||
const typeKind = item.type;
|
||||
let type: TypeDefKind<To>;
|
||||
switch (typeKind.kind) {
|
||||
case "struct": {
|
||||
|
|
@ -656,11 +637,12 @@ export function superFoldItem<From extends Phase, To extends Phase>(
|
|||
return {
|
||||
...item,
|
||||
kind: "type",
|
||||
node: { name: item.node.name, type },
|
||||
name: item.name,
|
||||
type,
|
||||
};
|
||||
}
|
||||
case "import": {
|
||||
const args = item.node.params.map(({ name, type, span }) => ({
|
||||
const args = item.params.map(({ name, type, span }) => ({
|
||||
name,
|
||||
type: folder.type(type),
|
||||
span,
|
||||
|
|
@ -668,23 +650,19 @@ export function superFoldItem<From extends Phase, To extends Phase>(
|
|||
return {
|
||||
...item,
|
||||
kind: "import",
|
||||
node: {
|
||||
module: item.node.module,
|
||||
func: item.node.func,
|
||||
name: item.node.name,
|
||||
params: args,
|
||||
returnType: item.node.returnType && folder.type(item.node.returnType),
|
||||
},
|
||||
module: item.module,
|
||||
func: item.func,
|
||||
name: item.name,
|
||||
params: args,
|
||||
returnType: item.returnType && folder.type(item.returnType),
|
||||
};
|
||||
}
|
||||
case "mod": {
|
||||
return {
|
||||
...item,
|
||||
kind: "mod",
|
||||
node: {
|
||||
name: item.node.name,
|
||||
contents: item.node.contents.map((item) => folder.item(item)),
|
||||
},
|
||||
name: item.name,
|
||||
contents: item.contents.map((item) => folder.item(item)),
|
||||
};
|
||||
}
|
||||
case "extern": {
|
||||
|
|
@ -694,11 +672,9 @@ export function superFoldItem<From extends Phase, To extends Phase>(
|
|||
return {
|
||||
...item,
|
||||
kind: "global",
|
||||
node: {
|
||||
name: item.node.name,
|
||||
type: folder.type(item.node.type),
|
||||
init: folder.expr(item.node.init),
|
||||
},
|
||||
name: item.name,
|
||||
type: folder.type(item.type),
|
||||
init: folder.expr(item.init),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -190,19 +190,19 @@ export function lower(gcx: GlobalContext): wasm.Module {
|
|||
items.forEach((item) => {
|
||||
switch (item.kind) {
|
||||
case "function": {
|
||||
lowerFunc(cx, item, item.node);
|
||||
lowerFunc(cx, item);
|
||||
break;
|
||||
}
|
||||
case "import": {
|
||||
lowerImport(cx, item, item.node);
|
||||
lowerImport(cx, item);
|
||||
break;
|
||||
}
|
||||
case "mod": {
|
||||
lowerMod(item.node.contents);
|
||||
lowerMod(item.contents);
|
||||
break;
|
||||
}
|
||||
case "global": {
|
||||
lowerGlobal(cx, item, item.node);
|
||||
lowerGlobal(cx, item);
|
||||
break;
|
||||
}
|
||||
case "extern":
|
||||
|
|
@ -258,11 +258,7 @@ export function lower(gcx: GlobalContext): wasm.Module {
|
|||
return mod;
|
||||
}
|
||||
|
||||
function lowerImport(
|
||||
cx: Context,
|
||||
item: Item<Typecked>,
|
||||
def: ItemImport<Typecked>,
|
||||
) {
|
||||
function lowerImport(cx: Context, def: ItemImport<Typecked> & Item<Typecked>) {
|
||||
const existing = cx.mod.imports.findIndex(
|
||||
(imp) => imp.module === def.module.value && imp.name === def.func.value,
|
||||
);
|
||||
|
|
@ -286,14 +282,10 @@ function lowerImport(
|
|||
});
|
||||
}
|
||||
|
||||
cx.funcIndices.set({ kind: "item", id: item.id }, { kind: "import", idx });
|
||||
cx.funcIndices.set({ kind: "item", id: def.id }, { kind: "import", idx });
|
||||
}
|
||||
|
||||
function lowerGlobal(
|
||||
cx: Context,
|
||||
item: Item<Typecked>,
|
||||
def: ItemGlobal<Typecked>,
|
||||
) {
|
||||
function lowerGlobal(cx: Context, def: ItemGlobal<Typecked> & Item<Typecked>) {
|
||||
const globalIdx = cx.mod.globals.length;
|
||||
|
||||
let valtype: "i32" | "i64";
|
||||
|
|
@ -318,18 +310,17 @@ function lowerGlobal(
|
|||
};
|
||||
|
||||
cx.mod.globals.push({
|
||||
_name: mangleDefPath(item.defPath),
|
||||
_name: mangleDefPath(def.defPath),
|
||||
type: { type: valtype, mut: "var" },
|
||||
init: [init],
|
||||
});
|
||||
|
||||
cx.globalIndices.set({ kind: "item", id: item.id }, globalIdx);
|
||||
cx.globalIndices.set({ kind: "item", id: def.id }, globalIdx);
|
||||
}
|
||||
|
||||
type FuncContext = {
|
||||
cx: Context;
|
||||
item: Item<Typecked>;
|
||||
func: ItemFunction<Typecked>;
|
||||
func: Item<Typecked> & ItemFunction<Typecked>;
|
||||
wasmType: wasm.FuncType;
|
||||
wasm: wasm.Func;
|
||||
varLocations: VarLocation[];
|
||||
|
|
@ -355,17 +346,13 @@ type StructLayout = {
|
|||
fields: StructFieldLayout[];
|
||||
};
|
||||
|
||||
function lowerFunc(
|
||||
cx: Context,
|
||||
item: Item<Typecked>,
|
||||
func: ItemFunction<Typecked>,
|
||||
) {
|
||||
function lowerFunc(cx: Context, func: Item<Typecked> & ItemFunction<Typecked>) {
|
||||
const abi = computeAbi(func.ty!);
|
||||
const { type: wasmType, paramLocations } = wasmTypeForAbi(abi, func.ty!);
|
||||
const type = internFuncType(cx, wasmType);
|
||||
|
||||
const wasmFunc: wasm.Func = {
|
||||
_name: mangleDefPath(item.defPath),
|
||||
_name: mangleDefPath(func.defPath),
|
||||
type,
|
||||
locals: [],
|
||||
body: [],
|
||||
|
|
@ -373,7 +360,6 @@ function lowerFunc(
|
|||
|
||||
const fcx: FuncContext = {
|
||||
cx,
|
||||
item,
|
||||
func,
|
||||
wasmType,
|
||||
wasm: wasmFunc,
|
||||
|
|
@ -398,7 +384,7 @@ function lowerFunc(
|
|||
fcx.cx.mod.funcs.push(wasmFunc);
|
||||
|
||||
fcx.cx.funcIndices.set(
|
||||
{ kind: "item", id: fcx.item.id },
|
||||
{ kind: "item", id: fcx.func.id },
|
||||
{ kind: "func", idx },
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,27 +28,36 @@ export class GlobalContext {
|
|||
public findItem<P extends Phase>(
|
||||
id: ItemId,
|
||||
localCrate?: Crate<P>,
|
||||
): Item<P | Final> {
|
||||
const crate = unwrap(
|
||||
[...(localCrate ? [localCrate] : []), ...this.finalizedCrates].find(
|
||||
(crate) => crate.id === id.crateId,
|
||||
),
|
||||
): Item<P> | Item<Final> {
|
||||
const allCrates: (Crate<P> | Crate<Final>)[] = [
|
||||
...(localCrate ? [localCrate] : []),
|
||||
...this.finalizedCrates,
|
||||
];
|
||||
|
||||
const crate: Crate<P> | Crate<Final> = unwrap(
|
||||
allCrates.find((crate) => crate.id === id.crateId),
|
||||
);
|
||||
|
||||
if (id.itemIdx === 0) {
|
||||
const contents: Item<P>[] | Item<Final>[] = crate.rootItems;
|
||||
// Typescript does not seem to be able to understand this here.
|
||||
// The type of this is supposed to be (Item<P> | Item<Final>)["contents"] which is
|
||||
// "too complex to represent".
|
||||
const erasedContents: any = contents;
|
||||
|
||||
// Return a synthetic module representing the crate root.
|
||||
return {
|
||||
const mod: Item<P> | Item<Final> = {
|
||||
kind: "mod",
|
||||
node: {
|
||||
contents: crate.rootItems,
|
||||
name: crate.packageName,
|
||||
},
|
||||
name: crate.packageName,
|
||||
contents: erasedContents,
|
||||
span: Span.startOfFile(crate.rootFile),
|
||||
id,
|
||||
};
|
||||
return mod;
|
||||
}
|
||||
|
||||
return unwrap(crate.itemsById.get(id));
|
||||
const mod = unwrap(crate.itemsById.get(id));
|
||||
return mod;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,14 +11,10 @@ import {
|
|||
FieldDef,
|
||||
Folder,
|
||||
FunctionArg,
|
||||
ItemFunction,
|
||||
Ident,
|
||||
ItemImport,
|
||||
Item,
|
||||
LOGICAL_KINDS,
|
||||
ItemMod,
|
||||
Type,
|
||||
ItemType,
|
||||
UNARY_KINDS,
|
||||
UnaryKind,
|
||||
binaryExprPrecedenceClass,
|
||||
|
|
@ -27,7 +23,6 @@ import {
|
|||
superFoldItem,
|
||||
Built,
|
||||
Parsed,
|
||||
ItemExtern,
|
||||
ItemId,
|
||||
ItemGlobal,
|
||||
StructLiteralField,
|
||||
|
|
@ -79,7 +74,7 @@ function parseItem(t: State): [State, Item<Parsed>] {
|
|||
let tok;
|
||||
[t, tok] = next(t);
|
||||
if (tok.kind === "function") {
|
||||
let sig;
|
||||
let sig: FunctionSig;
|
||||
[t, sig] = parseFunctionSig(t);
|
||||
|
||||
[t] = expectNext(t, "=");
|
||||
|
|
@ -89,16 +84,12 @@ function parseItem(t: State): [State, Item<Parsed>] {
|
|||
|
||||
[t] = expectNext(t, ";");
|
||||
|
||||
const def: ItemFunction<Parsed> = {
|
||||
...sig,
|
||||
body,
|
||||
};
|
||||
|
||||
return [
|
||||
t,
|
||||
{
|
||||
kind: "function",
|
||||
node: def,
|
||||
...sig,
|
||||
body,
|
||||
span: tok.span,
|
||||
// Assigned later.
|
||||
id: ItemId.dummy(),
|
||||
|
|
@ -145,14 +136,15 @@ function parseItem(t: State): [State, Item<Parsed>] {
|
|||
|
||||
[t] = expectNext(t, ";");
|
||||
|
||||
const def: ItemType<Parsed> = {
|
||||
name: name.ident,
|
||||
type,
|
||||
};
|
||||
|
||||
return [
|
||||
t,
|
||||
{ kind: "type", node: def, span: name.span, id: ItemId.dummy() },
|
||||
{
|
||||
kind: "type",
|
||||
name: name.ident,
|
||||
type,
|
||||
span: name.span,
|
||||
id: ItemId.dummy(),
|
||||
},
|
||||
];
|
||||
} else if (tok.kind === "import") {
|
||||
[t] = expectNext(t, "(");
|
||||
|
|
@ -167,28 +159,28 @@ function parseItem(t: State): [State, Item<Parsed>] {
|
|||
|
||||
[t] = expectNext(t, ";");
|
||||
|
||||
const def: ItemImport<Parsed> = {
|
||||
module: { kind: "str", value: module.value, span: module.span },
|
||||
func: { kind: "str", value: func.value, span: func.span },
|
||||
...sig,
|
||||
};
|
||||
|
||||
return [
|
||||
t,
|
||||
{ kind: "import", node: def, span: tok.span, id: ItemId.dummy() },
|
||||
{
|
||||
kind: "import",
|
||||
...sig,
|
||||
module: { kind: "str", value: module.value, span: module.span },
|
||||
func: { kind: "str", value: func.value, span: func.span },
|
||||
span: tok.span,
|
||||
id: ItemId.dummy(),
|
||||
},
|
||||
];
|
||||
} else if (tok.kind === "extern") {
|
||||
[t] = expectNext(t, "mod");
|
||||
let name;
|
||||
[t, name] = expectNext<TokenIdent>(t, "identifier");
|
||||
|
||||
const node: ItemExtern = {
|
||||
name: name.ident,
|
||||
};
|
||||
|
||||
[t] = expectNext(t, ";");
|
||||
|
||||
return [t, { kind: "extern", node, span: name.span, id: ItemId.dummy() }];
|
||||
return [
|
||||
t,
|
||||
{ kind: "extern", name: name.ident, span: name.span, id: ItemId.dummy() },
|
||||
];
|
||||
} else if (tok.kind === "mod") {
|
||||
let name;
|
||||
[t, name] = expectNext<TokenIdent>(t, "identifier");
|
||||
|
|
@ -221,12 +213,16 @@ function parseItem(t: State): [State, Item<Parsed>] {
|
|||
|
||||
[t] = expectNext(t, ";");
|
||||
|
||||
const node: ItemMod<Parsed> = {
|
||||
name: name.ident,
|
||||
contents,
|
||||
};
|
||||
|
||||
return [t, { kind: "mod", node, span: name.span, id: ItemId.dummy() }];
|
||||
return [
|
||||
t,
|
||||
{
|
||||
kind: "mod",
|
||||
name: name.ident,
|
||||
contents,
|
||||
span: name.span,
|
||||
id: ItemId.dummy(),
|
||||
},
|
||||
];
|
||||
} else if (tok.kind === "global") {
|
||||
let name;
|
||||
[t, name] = expectNext<TokenIdent>(t, "identifier");
|
||||
|
|
@ -238,13 +234,15 @@ function parseItem(t: State): [State, Item<Parsed>] {
|
|||
[t, init] = parseExpr(t);
|
||||
[t] = expectNext(t, ";");
|
||||
|
||||
const node: ItemGlobal<Parsed> = {
|
||||
const global: ItemGlobal<Parsed> & Item<Parsed> = {
|
||||
kind: "global",
|
||||
name: name.ident,
|
||||
type,
|
||||
init,
|
||||
span: name.span,
|
||||
id: ItemId.dummy(),
|
||||
};
|
||||
|
||||
return [t, { kind: "global", node, span: name.span, id: ItemId.dummy() }];
|
||||
return [t, global];
|
||||
} else {
|
||||
unexpectedToken(tok, "item");
|
||||
}
|
||||
|
|
@ -757,7 +755,7 @@ function validateAst(ast: Crate<Built>) {
|
|||
itemInner(item: Item<Built>): Item<Built> {
|
||||
if (seenItemIds.has(item.id)) {
|
||||
throw new Error(
|
||||
`duplicate item id: ${item.id.toString()} for ${item.node.name}`,
|
||||
`duplicate item id: ${item.id.toString()} for ${item.name}`,
|
||||
);
|
||||
}
|
||||
seenItemIds.add(item.id);
|
||||
|
|
|
|||
|
|
@ -28,25 +28,25 @@ function printItem(item: Item<AnyPhase>): string {
|
|||
|
||||
switch (item.kind) {
|
||||
case "function": {
|
||||
return id + printFunction(item.node);
|
||||
return id + printFunction(item);
|
||||
}
|
||||
case "type": {
|
||||
return id + printTypeDef(item.node);
|
||||
return id + printTypeDef(item);
|
||||
}
|
||||
case "import": {
|
||||
return id + printImportDef(item.node);
|
||||
return id + printImportDef(item);
|
||||
}
|
||||
case "mod": {
|
||||
return id + printMod(item.node);
|
||||
return id + printMod(item);
|
||||
}
|
||||
case "extern": {
|
||||
return id + `extern mod ${item.node.name};`;
|
||||
return id + `extern mod ${item.name};`;
|
||||
}
|
||||
case "global": {
|
||||
return (
|
||||
id +
|
||||
`global ${item.node.name}: ${printType(item.node.type)} = ${printExpr(
|
||||
item.node.init,
|
||||
`global ${item.name}: ${printType(item.type)} = ${printExpr(
|
||||
item.init,
|
||||
0,
|
||||
)};`
|
||||
);
|
||||
|
|
@ -54,7 +54,7 @@ function printItem(item: Item<AnyPhase>): string {
|
|||
}
|
||||
}
|
||||
|
||||
function printFunction(func: ItemFunction<AnyPhase>): string {
|
||||
function printFunction(func: ItemFunction<AnyPhase> & Item<AnyPhase>): string {
|
||||
const args = func.params
|
||||
.map(({ name, type }) => `${name}: ${printType(type)}`)
|
||||
.join(", ");
|
||||
|
|
@ -62,7 +62,7 @@ function printFunction(func: ItemFunction<AnyPhase>): string {
|
|||
return `function ${func.name}(${args})${ret} = ${printExpr(func.body, 0)};`;
|
||||
}
|
||||
|
||||
function printTypeDef(type: ItemType<AnyPhase>): string {
|
||||
function printTypeDef(type: ItemType<AnyPhase> & Item<AnyPhase>): string {
|
||||
switch (type.type.kind) {
|
||||
case "struct": {
|
||||
const { fields } = type.type;
|
||||
|
|
@ -92,7 +92,7 @@ function printImportDef(def: ItemImport<AnyPhase>): string {
|
|||
)}(${args})${ret};`;
|
||||
}
|
||||
|
||||
function printMod(mod: ItemMod<AnyPhase>): string {
|
||||
function printMod(mod: ItemMod<AnyPhase> & Item<AnyPhase>): string {
|
||||
return `mod ${mod.name} (\n${mod.contents.map(printItem).join("\n ")});`;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ function loadCrate(cx: Context, name: string, span: Span): Map<string, ItemId> {
|
|||
const loadedCrate = cx.gcx.crateLoader(cx.gcx, name, span);
|
||||
|
||||
const contents = new Map(
|
||||
loadedCrate.rootItems.map((item) => [item.node.name, item.id]),
|
||||
loadedCrate.rootItems.map((item) => [item.name, item.id]),
|
||||
);
|
||||
|
||||
return contents;
|
||||
|
|
@ -43,11 +43,10 @@ function loadCrate(cx: Context, name: string, span: Span): Map<string, ItemId> {
|
|||
|
||||
function resolveModItem(
|
||||
cx: Context,
|
||||
mod: ItemMod<Built> | ItemExtern,
|
||||
item: Item<Built>,
|
||||
mod: (ItemMod<Built> | ItemExtern) & Item<Built>,
|
||||
name: string,
|
||||
): ItemId | undefined {
|
||||
const cachedContents = cx.modContentsCache.get(item.id);
|
||||
const cachedContents = cx.modContentsCache.get(mod.id);
|
||||
if (cachedContents) {
|
||||
return cachedContents.get(name);
|
||||
}
|
||||
|
|
@ -55,12 +54,12 @@ function resolveModItem(
|
|||
let contents: Map<string, ItemId>;
|
||||
|
||||
if ("contents" in mod) {
|
||||
contents = new Map(mod.contents.map((item) => [item.node.name, item.id]));
|
||||
contents = new Map(mod.contents.map((item) => [item.name, item.id]));
|
||||
} else {
|
||||
contents = loadCrate(cx, item.node.name, item.span);
|
||||
contents = loadCrate(cx, mod.name, mod.span);
|
||||
}
|
||||
|
||||
cx.modContentsCache.set(item.id, contents);
|
||||
cx.modContentsCache.set(mod.id, contents);
|
||||
return contents.get(name);
|
||||
}
|
||||
|
||||
|
|
@ -93,14 +92,14 @@ function resolveModule(
|
|||
const items = new Map<string, ItemId>();
|
||||
|
||||
contents.forEach((item) => {
|
||||
const existing = items.get(item.node.name);
|
||||
const existing = items.get(item.name);
|
||||
if (existing !== undefined) {
|
||||
throw new CompilerError(
|
||||
`item \`${item.node.name}\` has already been declared`,
|
||||
`item \`${item.name}\` has already been declared`,
|
||||
item.span,
|
||||
);
|
||||
}
|
||||
items.set(item.node.name, item.id);
|
||||
items.set(item.name, item.id);
|
||||
});
|
||||
|
||||
const scopes: string[] = [];
|
||||
|
|
@ -157,43 +156,41 @@ function resolveModule(
|
|||
const resolver: Folder<Built, Resolved> = {
|
||||
...mkDefaultFolder(),
|
||||
itemInner(item): Item<Resolved> {
|
||||
const defPath = [...modName, item.node.name];
|
||||
const defPath = [...modName, item.name];
|
||||
|
||||
switch (item.kind) {
|
||||
case "function": {
|
||||
const params = item.node.params.map(({ name, span, type }) => ({
|
||||
const params = item.params.map(({ name, span, type }) => ({
|
||||
name,
|
||||
span,
|
||||
type: this.type(type),
|
||||
}));
|
||||
const returnType =
|
||||
item.node.returnType && this.type(item.node.returnType);
|
||||
const returnType = item.returnType && this.type(item.returnType);
|
||||
|
||||
item.node.params.forEach(({ name }) => scopes.push(name));
|
||||
const body = this.expr(item.node.body);
|
||||
const revParams = item.node.params.slice();
|
||||
item.params.forEach(({ name }) => scopes.push(name));
|
||||
const body = this.expr(item.body);
|
||||
const revParams = item.params.slice();
|
||||
revParams.reverse();
|
||||
revParams.forEach(({ name }) => popScope(name));
|
||||
|
||||
return {
|
||||
kind: "function",
|
||||
span: item.span,
|
||||
node: {
|
||||
name: item.node.name,
|
||||
params,
|
||||
returnType,
|
||||
body,
|
||||
},
|
||||
name: item.name,
|
||||
params,
|
||||
returnType,
|
||||
body,
|
||||
|
||||
id: item.id,
|
||||
defPath,
|
||||
};
|
||||
}
|
||||
case "mod": {
|
||||
const contents = resolveModule(cx, defPath, item.node.contents);
|
||||
const contents = resolveModule(cx, defPath, item.contents);
|
||||
return {
|
||||
...item,
|
||||
kind: "mod",
|
||||
node: { ...item.node, contents },
|
||||
contents,
|
||||
defPath,
|
||||
};
|
||||
}
|
||||
|
|
@ -201,15 +198,10 @@ function resolveModule(
|
|||
// Eagerly resolve the crate.
|
||||
// Note that because you can reference extern crates before the item,
|
||||
// we still need the loadCrate in the field access code above.
|
||||
loadCrate(cx, item.name, item.span);
|
||||
|
||||
loadCrate(cx, item.node.name, item.span);
|
||||
|
||||
const node: ItemExtern = {
|
||||
...item.node,
|
||||
};
|
||||
return {
|
||||
...item,
|
||||
node,
|
||||
defPath,
|
||||
};
|
||||
}
|
||||
|
|
@ -277,13 +269,12 @@ function resolveModule(
|
|||
|
||||
const pathResItem = resolveModItem(
|
||||
cx,
|
||||
module.node,
|
||||
module,
|
||||
expr.field.value,
|
||||
);
|
||||
if (pathResItem === undefined) {
|
||||
throw new CompilerError(
|
||||
`module ${module.node.name} has no item ${expr.field.value}`,
|
||||
`module ${module.name} has no item ${expr.field.value}`,
|
||||
expr.field.span,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
142
src/typeck.ts
142
src/typeck.ts
|
|
@ -134,16 +134,16 @@ export function typeck(
|
|||
case "import":
|
||||
case "type":
|
||||
case "global":
|
||||
return item.node.ty!;
|
||||
return item.ty!;
|
||||
case "mod": {
|
||||
throw new CompilerError(
|
||||
`module ${item.node.name} cannot be used as a type or value`,
|
||||
`module ${item.name} cannot be used as a type or value`,
|
||||
cause,
|
||||
);
|
||||
}
|
||||
case "extern": {
|
||||
throw new CompilerError(
|
||||
`extern declaration ${item.node.name} cannot be used as a type or value`,
|
||||
`extern declaration ${item.name} cannot be used as a type or value`,
|
||||
cause,
|
||||
);
|
||||
}
|
||||
|
|
@ -165,9 +165,9 @@ export function typeck(
|
|||
switch (item.kind) {
|
||||
case "function":
|
||||
case "import": {
|
||||
const args = item.node.params.map((arg) => lowerAstTy(arg.type));
|
||||
const returnTy: Ty = item.node.returnType
|
||||
? lowerAstTy(item.node.returnType)
|
||||
const args = item.params.map((arg) => lowerAstTy(arg.type));
|
||||
const returnTy: Ty = item.returnType
|
||||
? lowerAstTy(item.returnType)
|
||||
: TY_UNIT;
|
||||
|
||||
const ty: Ty = { kind: "fn", params: args, returnTy };
|
||||
|
|
@ -175,11 +175,11 @@ export function typeck(
|
|||
return ty;
|
||||
}
|
||||
case "type": {
|
||||
switch (item.node.type.kind) {
|
||||
switch (item.type.kind) {
|
||||
case "struct": {
|
||||
const ty: Ty = {
|
||||
kind: "struct",
|
||||
name: item.node.name,
|
||||
name: item.name,
|
||||
fields: [
|
||||
/*dummy*/
|
||||
],
|
||||
|
|
@ -187,7 +187,7 @@ export function typeck(
|
|||
|
||||
itemTys.set(item.id, ty);
|
||||
|
||||
const fields = item.node.type.fields.map<[string, Ty]>(
|
||||
const fields = item.type.fields.map<[string, Ty]>(
|
||||
({ name, type }) => [name.name, lowerAstTy(type)],
|
||||
);
|
||||
|
||||
|
|
@ -195,24 +195,24 @@ export function typeck(
|
|||
return ty;
|
||||
}
|
||||
case "alias": {
|
||||
return lowerAstTy(item.node.type.type);
|
||||
return lowerAstTy(item.type.type);
|
||||
}
|
||||
}
|
||||
}
|
||||
case "mod": {
|
||||
throw new CompilerError(
|
||||
`module ${item.node.name} cannot be used as a type or value`,
|
||||
`module ${item.name} cannot be used as a type or value`,
|
||||
cause,
|
||||
);
|
||||
}
|
||||
case "extern": {
|
||||
throw new CompilerError(
|
||||
`extern declaration ${item.node.name} cannot be used as a type or value`,
|
||||
`extern declaration ${item.name} cannot be used as a type or value`,
|
||||
cause,
|
||||
);
|
||||
}
|
||||
case "global": {
|
||||
const ty = lowerAstTy(item.node.type);
|
||||
const ty = lowerAstTy(item.type);
|
||||
itemTys.set(item.id, ty);
|
||||
return ty;
|
||||
}
|
||||
|
|
@ -246,24 +246,22 @@ export function typeck(
|
|||
switch (item.kind) {
|
||||
case "function": {
|
||||
const fnTy = typeOfItem(item.id, item.span) as TyFn;
|
||||
const body = checkBody(gcx, ast, item.node.body, fnTy, typeOfItem);
|
||||
const body = checkBody(gcx, ast, item.body, fnTy, typeOfItem);
|
||||
|
||||
const returnType = item.node.returnType && {
|
||||
...item.node.returnType,
|
||||
const returnType = item.returnType && {
|
||||
...item.returnType,
|
||||
ty: fnTy.returnTy,
|
||||
};
|
||||
return {
|
||||
...item,
|
||||
node: {
|
||||
name: item.node.name,
|
||||
params: item.node.params.map((arg, i) => ({
|
||||
...arg,
|
||||
type: { ...arg.type, ty: fnTy.params[i] },
|
||||
})),
|
||||
body,
|
||||
returnType,
|
||||
ty: fnTy,
|
||||
},
|
||||
name: item.name,
|
||||
params: item.params.map((arg, i) => ({
|
||||
...arg,
|
||||
type: { ...arg.type, ty: fnTy.params[i] },
|
||||
})),
|
||||
body,
|
||||
returnType,
|
||||
ty: fnTy,
|
||||
};
|
||||
}
|
||||
case "import": {
|
||||
|
|
@ -277,7 +275,7 @@ export function typeck(
|
|||
default: {
|
||||
throw new CompilerError(
|
||||
`import parameters must be I32 or Int`,
|
||||
item.node.params[i].span,
|
||||
item.params[i].span,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -291,41 +289,39 @@ export function typeck(
|
|||
default: {
|
||||
throw new CompilerError(
|
||||
`import return must be I32 or Int`,
|
||||
item.node.returnType!.span,
|
||||
item.returnType!.span,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const returnType = item.node.returnType && {
|
||||
...item.node.returnType,
|
||||
const returnType = item.returnType && {
|
||||
...item.returnType,
|
||||
ty: fnTy.returnTy,
|
||||
};
|
||||
|
||||
return {
|
||||
...item,
|
||||
kind: "import",
|
||||
node: {
|
||||
module: item.node.module,
|
||||
func: item.node.func,
|
||||
name: item.node.name,
|
||||
params: item.node.params.map((arg, i) => ({
|
||||
...arg,
|
||||
type: { ...arg.type, ty: fnTy.params[i] },
|
||||
})),
|
||||
returnType,
|
||||
ty: fnTy,
|
||||
},
|
||||
module: item.module,
|
||||
func: item.func,
|
||||
name: item.name,
|
||||
params: item.params.map((arg, i) => ({
|
||||
...arg,
|
||||
type: { ...arg.type, ty: fnTy.params[i] },
|
||||
})),
|
||||
returnType,
|
||||
ty: fnTy,
|
||||
};
|
||||
}
|
||||
case "type": {
|
||||
switch (item.node.type.kind) {
|
||||
switch (item.type.kind) {
|
||||
case "struct": {
|
||||
const fieldNames = new Set();
|
||||
item.node.type.fields.forEach(({ name }) => {
|
||||
item.type.fields.forEach(({ name }) => {
|
||||
if (fieldNames.has(name)) {
|
||||
throw new CompilerError(
|
||||
`type ${item.node.name} has a duplicate field: ${name.name}`,
|
||||
`type ${item.name} has a duplicate field: ${name.name}`,
|
||||
name.span,
|
||||
);
|
||||
}
|
||||
|
|
@ -336,30 +332,26 @@ export function typeck(
|
|||
|
||||
return {
|
||||
...item,
|
||||
node: {
|
||||
name: item.node.name,
|
||||
type: {
|
||||
kind: "struct",
|
||||
fields: item.node.type.fields.map((field, i) => ({
|
||||
name: field.name,
|
||||
type: {
|
||||
...field.type,
|
||||
ty: ty.fields[i][1],
|
||||
},
|
||||
})),
|
||||
},
|
||||
name: item.name,
|
||||
type: {
|
||||
kind: "struct",
|
||||
fields: item.type.fields.map((field, i) => ({
|
||||
name: field.name,
|
||||
type: {
|
||||
...field.type,
|
||||
ty: ty.fields[i][1],
|
||||
},
|
||||
})),
|
||||
},
|
||||
};
|
||||
}
|
||||
case "alias": {
|
||||
return {
|
||||
...item,
|
||||
node: {
|
||||
name: item.node.name,
|
||||
type: {
|
||||
kind: "alias",
|
||||
type: item.node.type.type,
|
||||
},
|
||||
name: item.name,
|
||||
type: {
|
||||
kind: "alias",
|
||||
type: item.type.type,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
@ -368,22 +360,16 @@ export function typeck(
|
|||
case "mod": {
|
||||
return {
|
||||
...item,
|
||||
node: {
|
||||
...item.node,
|
||||
contents: item.node.contents.map((item) => this.item(item)),
|
||||
},
|
||||
contents: item.contents.map((item) => this.item(item)),
|
||||
};
|
||||
}
|
||||
case "extern": {
|
||||
// Nothing to check.
|
||||
return {
|
||||
...item,
|
||||
node: { ...item.node },
|
||||
};
|
||||
return item;
|
||||
}
|
||||
case "global": {
|
||||
const ty = typeOfItem(item.id, item.span);
|
||||
const { init } = item.node;
|
||||
const { init } = item;
|
||||
|
||||
if (init.kind !== "literal" || init.value.kind !== "int") {
|
||||
throw new CompilerError(
|
||||
|
|
@ -398,11 +384,8 @@ export function typeck(
|
|||
|
||||
return {
|
||||
...item,
|
||||
node: {
|
||||
...item.node,
|
||||
ty,
|
||||
init: { ...init, ty },
|
||||
},
|
||||
ty,
|
||||
init: { ...init, ty },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -421,10 +404,9 @@ export function typeck(
|
|||
const typecked = foldAst(ast, checker);
|
||||
|
||||
const main = typecked.rootItems.find((item) => {
|
||||
if (item.kind === "function" && item.node.name === "main") {
|
||||
const func = item.node;
|
||||
if (func.returnType !== undefined) {
|
||||
const ty = func.body.ty;
|
||||
if (item.kind === "function" && item.name === "main") {
|
||||
if (item.returnType !== undefined) {
|
||||
const ty = item.body.ty;
|
||||
if (ty.kind !== "tuple" || ty.elems.length !== 0) {
|
||||
throw new CompilerError(
|
||||
`\`main\` has an invalid signature. main takes no arguments and returns nothing`,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue