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) { private checkAssignableExpr(expr: ast.Expr, ty: Ty, valSpan: Span) {
todo(); const k = expr.kind;
switch (ty.kind.tag) { switch (k.tag) {
case "error": case "error":
this.exprTys.set(expr.id, ty);
return; return;
case "unknown": case "path": {
this.exprTys.set(expr.id, ty); if (k.qty || k.path.segments.length !== 1) {
this.report("cannot assign to expression", expr.span);
return; 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 "null":
case "int": case "int":
case "bool": 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 { 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) { } else if (rep.pos) {
console.error( 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) ctx.filePosLineText(rep.file, rep.pos)
}`, }`,
"color: cyan",
"",
); );
console.error( console.error(
` %c| %c${" ".repeat(rep.pos.col - 1)}^\x1b[0m`, ` %c| %c${" ".repeat(rep.pos.col - 1)}^\x1b[0m`,

View File

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