From d981e60f8f783458394ad3825a4a23d6073217af Mon Sep 17 00:00:00 2001 From: sfja Date: Tue, 31 Dec 2024 00:54:58 +0100 Subject: [PATCH] mod std --- compiler/compiler.ts | 13 +++++++------ compiler/desugar/special_loop.ts | 6 +++++- compiler/parser.ts | 5 ++++- editors/vim/syntax/slige.vim | 5 +++++ examples/generic_array.slg | 29 +++++++++-------------------- stdlib.slg => std/lib.slg | 3 +-- 6 files changed, 31 insertions(+), 30 deletions(-) rename stdlib.slg => std/lib.slg (98%) diff --git a/compiler/compiler.ts b/compiler/compiler.ts index a018798..9851c90 100644 --- a/compiler/compiler.ts +++ b/compiler/compiler.ts @@ -104,13 +104,14 @@ export class ModTree implements AstVisitor<[string]> { } const { ident, filePath: modFilePath } = stmt.kind; const ast = this.parseFile( - path.join(path.dirname(filePath), modFilePath), + ident === "std" + ? path.join( + path.dirname(path.fromFileUrl(Deno.mainModule)), + "../std/lib.slg", + ) + : path.join(path.dirname(filePath), modFilePath), ); - stmt.kind = { - type: "mod", - ident, - mod: { filePath, ast }, - }; + stmt.kind = { type: "mod", ident, mod: { filePath, ast } }; visitStmts(ast, this, filePath); return "stop"; } diff --git a/compiler/desugar/special_loop.ts b/compiler/desugar/special_loop.ts index 425e062..7c6b579 100644 --- a/compiler/desugar/special_loop.ts +++ b/compiler/desugar/special_loop.ts @@ -79,7 +79,11 @@ export class SpecialLoopDesugarer implements AstVisitor { value: Expr({ type: "call", subject: Expr({ - type: "ident", + type: "path", + subject: Expr({ + type: "ident", + ident: "std", + }), ident: "array_length", }), args: [ diff --git a/compiler/parser.ts b/compiler/parser.ts index 5bbf616..1f6b002 100644 --- a/compiler/parser.ts +++ b/compiler/parser.ts @@ -1,5 +1,4 @@ import { - Anno, AssignType, AstCreator, BinaryType, @@ -245,6 +244,10 @@ export class Parser { } const ident = this.current().identValue!; this.step(); + if (this.test(";")) { + this.eatSemicolon(); + return this.stmt({ type: "mod_file", ident, filePath: ident }, pos); + } if (this.test("string")) { const filePath = this.current().stringValue!; this.step(); diff --git a/editors/vim/syntax/slige.vim b/editors/vim/syntax/slige.vim index 610364c..2883ccb 100644 --- a/editors/vim/syntax/slige.vim +++ b/editors/vim/syntax/slige.vim @@ -52,8 +52,12 @@ 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\s\{-}(.\{-})' + +syn match sligePath '[a-zA-Z_]\w*\ze\s\{-}::' syn match Function '[a-zA-Z_]\w*\ze\s\{-}::<.\{-}>' + syn match Function ' \zs[a-zA-Z_]\w*\ze\s\{-}<.\{-}>\s\{-}(.\{-})' syn region sligeBlock start="{" end="}" transparent fold @@ -61,5 +65,6 @@ syn region sligeBlock start="{" end="}" transparent fold syn region sligeAnno start="#!\?\[" end="]" contains=Identifier,Type hi def link sligeAnno PreProc +hi def link sligePath Include let b:current_syntax = "slige" diff --git a/examples/generic_array.slg b/examples/generic_array.slg index d223118..2b35a11 100644 --- a/examples/generic_array.slg +++ b/examples/generic_array.slg @@ -1,31 +1,20 @@ -// - -//fn print(msg: string) #[builtin(Print)] {} -//fn println(msg: string) { print(msg + "\n") } -// -//fn itos(number: int) -> string #[builtin(IntToString)] {} -// -//fn array_new() -> [T] #[builtin(ArrayNew)] {} -//fn array_push(array: [T], value: T) #[builtin(ArrayPush)] {} -//fn array_length(array: [T]) -> int #[builtin(ArrayLength)] {} -//fn array_at(array: [T], index: int) -> T #[builtin(ArrayAt)] {} - +mod std; fn main() { - let strings = array_new::(); - array_push(strings, "hello"); - array_push(strings, "world"); + let strings = std::array_new::(); + std::array_push(strings, "hello"); + std::array_push(strings, "world"); - let ints = array_new::(); - array_push(ints, 1); - array_push(ints, 2); + let ints = std::array_new::(); + std::array_push(ints, 1); + std::array_push(ints, 2); for v in strings { - println(v) + std::println(v) } for v in ints { - println(itos(v)) + std::println(std::itos(v)) } } diff --git a/stdlib.slg b/std/lib.slg similarity index 98% rename from stdlib.slg rename to std/lib.slg index 5832530..80e7579 100644 --- a/stdlib.slg +++ b/std/lib.slg @@ -6,8 +6,7 @@ pub fn exit(status_code: int) {} #[builtin(Print)] pub fn print(msg: string) {} -msg + "\n" -pub fn println(msg: string) { print) } +pub fn println(msg: string) { print(msg + "\n") } #[builtin(IntToString)] pub fn int_to_string(number: int) -> string {}