From 3d161efb998842cc9cc46bb109d589c74893df80 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Tue, 11 Feb 2025 22:23:44 +0100 Subject: [PATCH] compiler: check assign --- slige/compiler/check/checker.ts | 52 ++++++++++++++++++++++++---- slige/compiler/common/diagnostics.ts | 8 ++++- slige/compiler/program.slg | 2 +- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/slige/compiler/check/checker.ts b/slige/compiler/check/checker.ts index 6882396..7cbb76c 100644 --- a/slige/compiler/check/checker.ts +++ b/slige/compiler/check/checker.ts @@ -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 { diff --git a/slige/compiler/common/diagnostics.ts b/slige/compiler/common/diagnostics.ts index bacf7b9..aa8eb13 100644 --- a/slige/compiler/common/diagnostics.ts +++ b/slige/compiler/common/diagnostics.ts @@ -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`, diff --git a/slige/compiler/program.slg b/slige/compiler/program.slg index 91607c7..88879f2 100644 --- a/slige/compiler/program.slg +++ b/slige/compiler/program.slg @@ -1,6 +1,6 @@ fn main() { - let a = 5; + let mut a = 5; a = 10; }