This commit is contained in:
nora 2023-12-15 18:32:53 +01:00
parent bf9fbcc069
commit 7ca78530a1
4 changed files with 58 additions and 36 deletions

View file

@ -10,7 +10,7 @@ A Riverdelta program consists of many items, like functions. Every item ends wit
semicolon. semicolon.
```js ```js
item = item_function | item_type | item_import | item_extern | item_mod item = item_function | item_type | item_import | item_extern | item_mod;
``` ```
### Functions ### Functions
@ -25,6 +25,7 @@ item_function := "function" function_sig "=" expr ";"
```js ```js
function helloWorld() = ; function helloWorld() = ;
``` ```
```js ```js
function block() = ( function block() = (
1; 1;
@ -136,7 +137,6 @@ the module statement.
`.nil` files cannot declare file submodules but only inline modules. `.nil` files cannot declare file submodules but only inline modules.
If the current file is `a/a.mod.nil`, then `mod foo;` will look for `a/b.nil` or `a/b/b.mod.nil`. If the current file is `a/a.mod.nil`, then `mod foo;` will look for `a/b.nil` or `a/b/b.mod.nil`.
### Globals ### Globals
Globals are mutable values. Globals are mutable values.
@ -153,4 +153,4 @@ They can be accessed like local variables. Their initial value must be literal e
## Expressions ## Expressions
there are many expressions and im not going to list a single one. there are many expressions and im not going to list a single one.

View file

@ -133,7 +133,7 @@ function resolveModule(
for (let i = tyParamScopes.length - 1; i >= 0; i--) { for (let i = tyParamScopes.length - 1; i >= 0; i--) {
const candidate = tyParamScopes[i]; const candidate = tyParamScopes[i];
if (candidate === ident.name) { if (candidate === ident.name) {
return { return {
kind: "tyParam", kind: "tyParam",
@ -228,7 +228,7 @@ function resolveModule(
}; };
} }
case "type": { case "type": {
tyParamScopes = item.generics.map(({name}) => name); tyParamScopes = item.generics.map(({ name }) => name);
const type = { ...superFoldItem(item, this) }; const type = { ...superFoldItem(item, this) };

View file

@ -1,5 +1,5 @@
import { import {
BuiltinName, BuiltinName,
COMPARISON_KINDS, COMPARISON_KINDS,
Crate, Crate,
EQUALITY_KINDS, EQUALITY_KINDS,
@ -29,7 +29,15 @@ import {
import { CompilerError, ErrorEmitted, Span, unreachable } from "../error"; import { CompilerError, ErrorEmitted, Span, unreachable } from "../error";
import { printTy } from "../printer"; import { printTy } from "../printer";
import { InferContext } from "./infer"; import { InferContext } from "./infer";
import { TypeckCtx, emitError, lowerAstTy, mkTyFn, tyError, tyErrorFrom, typeOfItem } from "./item"; import {
TypeckCtx,
emitError,
lowerAstTy,
mkTyFn,
tyError,
tyErrorFrom,
typeOfItem,
} from "./item";
export function exprError(err: ErrorEmitted, span: Span): Expr<Typecked> { export function exprError(err: ErrorEmitted, span: Span): Expr<Typecked> {
return { return {
@ -41,36 +49,35 @@ export function exprError(err: ErrorEmitted, span: Span): Expr<Typecked> {
} }
type FuncCtx = { type FuncCtx = {
cx: TypeckCtx; cx: TypeckCtx;
infcx: InferContext; infcx: InferContext;
localTys: Ty[]; localTys: Ty[];
loopState: LoopState[]; loopState: LoopState[];
checkExpr: (expr: Expr<Resolved>) => Expr<Typecked>; checkExpr: (expr: Expr<Resolved>) => Expr<Typecked>;
}; };
type LoopState = { hasBreak: boolean; loopId: LoopId }; type LoopState = { hasBreak: boolean; loopId: LoopId };
function typeOfValue(fcx: FuncCtx, res: Resolution, span: Span): Ty { function typeOfValue(fcx: FuncCtx, res: Resolution, span: Span): Ty {
switch (res.kind) { switch (res.kind) {
case "local": { case "local": {
const idx = fcx.localTys.length - 1 - res.index; const idx = fcx.localTys.length - 1 - res.index;
return fcx.localTys[idx]; return fcx.localTys[idx];
}
case "item": {
return typeOfItem(fcx.cx, res.id, [], span);
}
case "builtin":
return typeOfBuiltinValue(fcx, res.name, span);
case "tyParam":
return tyError(
fcx.cx,
new CompilerError(`type parameter cannot be used as value`, span),
);
case "error":
return tyErrorFrom(res);
} }
case "item": {
return typeOfItem(fcx.cx, res.id, [], span);
}
case "builtin":
return typeOfBuiltinValue(fcx, res.name, span);
case "tyParam":
return tyError(
fcx.cx,
new CompilerError(`type parameter cannot be used as value`, span),
);
case "error":
return tyErrorFrom(res);
} }
}
export function typeOfBuiltinValue( export function typeOfBuiltinValue(
fcx: FuncCtx, fcx: FuncCtx,

View file

@ -1,4 +1,19 @@
import { Crate, Expr, Folder, Item, ItemId, Resolved, TY_I32, TY_INT, Ty, TyFn, Typecked, foldAst, mkDefaultFolder, tyIsUnit } from "../ast"; import {
Crate,
Expr,
Folder,
Item,
ItemId,
Resolved,
TY_I32,
TY_INT,
Ty,
TyFn,
Typecked,
foldAst,
mkDefaultFolder,
tyIsUnit,
} from "../ast";
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";