compiler: check assign

This commit is contained in:
SimonFJ20 2025-02-11 22:23:44 +01:00
parent 6ba6bce276
commit 3d161efb99
3 changed files with 54 additions and 8 deletions

View File

@ -141,19 +141,59 @@ export class Checker {
}
private checkAssignableExpr(expr: ast.Expr, ty: Ty, valSpan: Span) {
todo();
switch (ty.kind.tag) {
const k = expr.kind;
switch (k.tag) {
case "error":
this.exprTys.set(expr.id, ty);
return;
case "unknown":
this.exprTys.set(expr.id, ty);
case "path": {
if (k.qty || k.path.segments.length !== 1) {
this.report("cannot assign to expression", expr.span);
return;
}
const re = this.re.exprRes(expr.id);
if (re.kind.tag !== "local") {
this.report("cannot assign to expression", expr.span);
return;
}
const patRe = this.re.patRes(re.kind.id);
if (patRe.pat.kind.tag !== "bind") {
throw new Error();
}
if (!patRe.pat.kind.mut) {
this.report("local is not declared mutable", expr.span);
return;
}
return;
}
case "group":
this.checkAssignableExpr(expr, ty, valSpan);
return;
case "array":
case "repeat":
case "struct":
case "deref":
case "elem":
case "field":
case "index":
return todo();
case "null":
case "int":
case "bool":
case "fn":
case "str":
case "ref":
case "call":
case "unary":
case "binary":
case "block":
case "if":
case "loop":
case "while":
case "for":
case "c_for":
this.report("cannot assign to expression", expr.span);
return;
}
exhausted(k);
}
public fnItemTy(item: ast.Item, kind: ast.FnItem): Ty {

View File

@ -127,9 +127,15 @@ export function prettyPrintReport(ctx: Ctx, rep: Report) {
}
} else if (rep.pos) {
console.error(
`${rep.pos.line.toString().padStart(4, " ")}| ${
` %c|`,
"color: cyan",
);
console.error(
`%c${rep.pos.line.toString().padStart(4, " ")}| %c${
ctx.filePosLineText(rep.file, rep.pos)
}`,
"color: cyan",
"",
);
console.error(
` %c| %c${" ".repeat(rep.pos.col - 1)}^\x1b[0m`,

View File

@ -1,6 +1,6 @@
fn main() {
let a = 5;
let mut a = 5;
a = 10;
}