From 8c172ccbe4e7038b2763a3fb4382f8ee3c468d22 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Fri, 13 Dec 2024 09:55:09 +0100 Subject: [PATCH] =?UTF-8?q?fix=20f=C3=A6rdighedsproblem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compiler/assembler.ts | 13 ++++++-- compiler/lowerer.ts | 63 ++++++++++++++++++++---------------- compiler/main.ts | 2 +- compiler/temputils.ts | 4 +-- editors/vim/syntax/slige.vim | 3 ++ examples/std.slg | 10 +++--- runtime/main.cpp | 2 +- runtime/value.hpp | 2 +- web/public/deno.jsonc | 2 +- 9 files changed, 61 insertions(+), 40 deletions(-) diff --git a/compiler/assembler.ts b/compiler/assembler.ts index 07eca03..75024e8 100644 --- a/compiler/assembler.ts +++ b/compiler/assembler.ts @@ -49,9 +49,16 @@ export class Assembler { const locs: { [key: string]: number } = {}; const refs: { [key: number]: string } = {}; + let selectedLabel = ""; for (const line of this.lines) { for (const label of line.labels ?? []) { - locs[label] = ip; + const isAbsLabel = !label.startsWith("."); + if (isAbsLabel) { + selectedLabel = label; + locs[label] = ip; + } else { + locs[`${selectedLabel}${label}`] = ip; + } } for (const lit of line.ins as Lit[]) { if (typeof lit === "number") { @@ -69,7 +76,9 @@ export class Assembler { } } else { output.push(0); - refs[ip] = lit.label; + refs[ip] = lit.label.startsWith(".") + ? `${selectedLabel}${lit.label}` + : refs[ip] = lit.label; ip += 1; } } diff --git a/compiler/lowerer.ts b/compiler/lowerer.ts index 6f78e42..c4dd06f 100644 --- a/compiler/lowerer.ts +++ b/compiler/lowerer.ts @@ -4,6 +4,7 @@ import { LocalLeaf, Locals, LocalsFnRoot } from "./lowerer_locals.ts"; import { Ops } from "./mod.ts"; import { Assembler, Label } from "./assembler.ts"; import { vtypeToString } from "./vtype.ts"; +import { Pos } from "./token.ts"; export class Lowerer { private program = new Assembler(); @@ -11,7 +12,10 @@ export class Lowerer { private fnStmtIdLabelMap: { [key: number]: string } = {}; private breakStack: Label[] = []; + public constructor(private lastPos: Pos) {} + public lower(stmts: Stmt[]) { + this.addSourceMap({ index: 0, line: 1, col: 1 }); this.program.add(Ops.PushPtr, { label: "_start" }); this.program.add(Ops.Jump); this.scoutFnHeaders(stmts); @@ -19,6 +23,7 @@ export class Lowerer { this.lowerStaticStmt(stmt); } this.program.setLabel({ label: "_start" }); + this.addSourceMap(this.lastPos); this.program.add(Ops.PushPtr, { label: "main" }); this.program.add(Ops.Call, 0); this.program.add(Ops.Pop); @@ -28,6 +33,10 @@ export class Lowerer { return this.program.assemble(); } + private addSourceMap({ index, line, col }: Pos) { + this.program.add(Ops.SourceMap, index, line, col); + } + private scoutFnHeaders(stmts: Stmt[]) { for (const stmt of stmts) { if (stmt.kind.type !== "fn") { @@ -41,6 +50,7 @@ export class Lowerer { } private lowerStaticStmt(stmt: Stmt) { + this.addSourceMap(stmt.pos); switch (stmt.kind.type) { case "fn": return this.lowerFnStmt(stmt); @@ -55,6 +65,7 @@ export class Lowerer { } private lowerStmt(stmt: Stmt) { + this.addSourceMap(stmt.pos); switch (stmt.kind.type) { case "error": break; @@ -117,28 +128,6 @@ export class Lowerer { this.program.add(Ops.Jump); } - private lowerBuiltinAnno(annoArgs: Expr[]) { - if (annoArgs.length !== 1) { - throw new Error("invalid # of arguments to builtin annotation"); - } - const anno = annoArgs[0]; - if (anno.kind.type !== "ident") { - throw new Error( - `unexpected argument type '${anno.kind.type}' expected 'ident'`, - ); - } - const value = anno.kind.value; - const builtin = Object.entries(Builtins).find((entry) => - entry[0] === value - )?.[1]; - if (builtin === undefined) { - throw new Error( - `unrecognized builtin '${value}'`, - ); - } - this.program.add(Ops.Builtin, builtin); - } - private lowerFnStmt(stmt: Stmt) { if (stmt.kind.type !== "fn") { throw new Error(); @@ -158,7 +147,7 @@ export class Lowerer { this.locals.allocSym(ident); } if (stmt.kind.anno?.ident === "builtin") { - this.lowerBuiltinAnno(stmt.kind.anno.values); + this.lowerFnBuiltinBody(stmt.kind.anno.values); } else { this.lowerExpr(stmt.kind.body); } @@ -176,6 +165,28 @@ export class Lowerer { this.program = outerProgram; } + private lowerFnBuiltinBody(annoArgs: Expr[]) { + if (annoArgs.length !== 1) { + throw new Error("invalid # of arguments to builtin annotation"); + } + const anno = annoArgs[0]; + if (anno.kind.type !== "ident") { + throw new Error( + `unexpected argument type '${anno.kind.type}' expected 'ident'`, + ); + } + const value = anno.kind.value; + const builtin = Object.entries(Builtins).find((entry) => + entry[0] === value + )?.[1]; + if (builtin === undefined) { + throw new Error( + `unrecognized builtin '${value}'`, + ); + } + this.program.add(Ops.Builtin, builtin); + } + private lowerLetStmt(stmt: Stmt) { if (stmt.kind.type !== "let") { throw new Error(); @@ -231,10 +242,8 @@ export class Lowerer { throw new Error(); } if (expr.kind.sym.type === "let") { - this.program.add( - Ops.LoadLocal, - this.locals.symId(expr.kind.ident), - ); + const symId = this.locals.symId(expr.kind.ident); + this.program.add(Ops.LoadLocal, symId); return; } if (expr.kind.sym.type === "fn_param") { diff --git a/compiler/main.ts b/compiler/main.ts index e0b7b84..bcbd72f 100644 --- a/compiler/main.ts +++ b/compiler/main.ts @@ -36,7 +36,7 @@ if (reporter.errorOccured()) { Deno.exit(1); } -const lowerer = new Lowerer(); +const lowerer = new Lowerer(lexer.currentPos()); lowerer.lower(ast); lowerer.printProgram(); const program = lowerer.finish(); diff --git a/compiler/temputils.ts b/compiler/temputils.ts index 56f361c..e1dce64 100644 --- a/compiler/temputils.ts +++ b/compiler/temputils.ts @@ -14,7 +14,7 @@ export async function compileWithDebug(path: string): Promise { const lexer = new Lexer(text, reporter); const parser = new Parser(lexer, reporter); - const ast = parser.parseStmts(); + const ast = parser.parse(); new Resolver(reporter).resolve(ast); new Checker(reporter).check(ast); @@ -23,7 +23,7 @@ export async function compileWithDebug(path: string): Promise { console.error("Errors occurred, stopping compilation."); } - const lowerer = new Lowerer(); + const lowerer = new Lowerer(lexer.currentPos()); lowerer.lower(ast); lowerer.printProgram(); const program = lowerer.finish(); diff --git a/editors/vim/syntax/slige.vim b/editors/vim/syntax/slige.vim index 46f95ca..dd6da3a 100644 --- a/editors/vim/syntax/slige.vim +++ b/editors/vim/syntax/slige.vim @@ -40,6 +40,9 @@ syn match Comment "//.*$" contains=Todo syn region Comment start=+/\*+ end=+\*/+ contains=Todo +syn match Identifier '[a-z_]\w*' +syn match Type '[A-Z]\w*' + syn match Function '[a-zA-Z_]\w*\ze(' syn region sligeBlock start="{" end="}" transparent fold diff --git a/examples/std.slg b/examples/std.slg index 9c82740..fc55225 100644 --- a/examples/std.slg +++ b/examples/std.slg @@ -21,8 +21,9 @@ fn split(str: string, seperator: int) -> [string] { let i = 0; let current_str = ""; + let str_length = string_length(str); loop { - if i >= string_length(str) { + if i >= str_length { break; } let char = string_char_at(str, i); @@ -30,7 +31,7 @@ fn split(str: string, seperator: int) -> [string] { array_push_string(result, current_str); current_str = ""; } else { - string_push_char(current_str, char); + current_str = string_push_char(current_str, char); } i = i + 1; } @@ -38,15 +39,14 @@ fn split(str: string, seperator: int) -> [string] { } fn main() { - let array = split("aoisfjasoifjsaiofjsa", char("a")); + let array = split("yaoisfjasoifjsaiofjsa", char("a")); let i = 0; let array_length = array_length_string(array); loop { if i >= array_length { break; } - let v = array_at_string(array, 0); - println(v); + println(array_at_string(array, i)); i = i + 1; } } diff --git a/runtime/main.cpp b/runtime/main.cpp index ce28b71..1529b22 100644 --- a/runtime/main.cpp +++ b/runtime/main.cpp @@ -9,7 +9,7 @@ #include #include -bool print_stack_debug = true; +bool print_stack_debug = false; int execute_file_and_exit(std::string filename) { diff --git a/runtime/value.hpp b/runtime/value.hpp index 0798072..4860983 100644 --- a/runtime/value.hpp +++ b/runtime/value.hpp @@ -171,7 +171,7 @@ public: inline auto to_repr_string() const -> std::string { return std::format( - "{}({})", value_type_to_string(this->m_type), to_string()); + "{}({:.4s})", value_type_to_string(this->m_type), to_string()); } private: diff --git a/web/public/deno.jsonc b/web/public/deno.jsonc index e8da863..7466083 100644 --- a/web/public/deno.jsonc +++ b/web/public/deno.jsonc @@ -1,6 +1,6 @@ { "tasks": { - "bundle": "deno run -A bundle.ts" + "bundle": "deno run --allow-read --allow-write --allow-env --allow-run bundle.ts" }, "compilerOptions": { "checkJs": false,