diff options
| author | Baitinq <[email protected]> | 2025-05-10 19:57:09 +0200 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-05-10 19:57:09 +0200 |
| commit | e8e0bcdab4b154d064b79fa654f8c826d431db4e (patch) | |
| tree | c3dd03103f25474fc6dad1412ce111da77c1ab27 | |
| parent | Codegen: Add varags hack for use in params (diff) | |
| download | pry-lang-e8e0bcdab4b154d064b79fa654f8c826d431db4e.tar.gz pry-lang-e8e0bcdab4b154d064b79fa654f8c826d431db4e.tar.bz2 pry-lang-e8e0bcdab4b154d064b79fa654f8c826d431db4e.zip | |
Feature: Add initial stdlib
| -rw-r--r-- | examples/21.src | 9 | ||||
| -rw-r--r-- | src/parser.zig | 27 | ||||
| -rw-r--r-- | std/stdlib.src | 7 |
3 files changed, 29 insertions, 14 deletions
diff --git a/examples/21.src b/examples/21.src index c4c8032..ca4f24f 100644 --- a/examples/21.src +++ b/examples/21.src @@ -1,8 +1,9 @@ -extern printf = (*i64, varargs) => void; extern rand = () => i64; extern malloc = (i64) => *i64; extern free = (*i64) => void; +import "!stdlib.src"; + let init_array = (n: i64, arr: *i64) => void { let i = 0; while i < n { @@ -15,7 +16,7 @@ let init_array = (n: i64, arr: *i64) => void { let print_array = (n: i64, arr: *i64) => void { let i = 0; while i < n { - printf("%d\n", *(arr + i)); + println("%d", *(arr + i)); i = i + 1; }; return; @@ -50,12 +51,12 @@ let main = () => i64 { let arr = malloc(n * 8); init_array(n, arr); - printf("Pre-sorted:\n"); + println("Pre-sorted:"); print_array(n, arr); sort_array(n, arr); - printf("Sorted:\n"); + println("Sorted:"); print_array(n, arr); free(arr); diff --git a/src/parser.zig b/src/parser.zig index e89479f..d5672d0 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -205,19 +205,26 @@ pub const Parser = struct { std.debug.assert(expr.PRIMARY_EXPRESSION == .STRING); - const filename = expr.PRIMARY_EXPRESSION.STRING.value; + var import_filename = expr.PRIMARY_EXPRESSION.STRING.value; + var current_file = self.filename; - // Open the directory containing self.filename - const dir_path = std.fs.path.dirname(self.filename) orelse "."; - var x = std.fs.cwd().openDir(dir_path, .{}) catch { - std.debug.print("COULDNT OPEN DIR {s}\n", .{self.filename}); + // stdlib. TODO: this is very hacky and won't work if running the compiler binary by itself + if (import_filename.ptr[0] == '!') { + import_filename = std.fmt.allocPrint(self.arena, "./std/{s}", .{import_filename[1..]}) catch return ParserError.OutOfMemory; + current_file = "."; + } + + // Open the directory containing current_file + const dir_path = std.fs.path.dirname(current_file) orelse "."; + var dir = std.fs.cwd().openDir(dir_path, .{}) catch { + std.debug.print("Couldn't open directory {s}\n", .{current_file}); return ParserError.OutOfMemory; }; - defer x.close(); + defer dir.close(); // Open the target file - const file = x.openFile(filename, .{}) catch { - std.debug.print("COULDNT OPEN FILENAME {s}\n", .{filename}); + const file = dir.openFile(import_filename, .{}) catch { + std.debug.print("Couldn't open file {s}\n", .{import_filename}); return ParserError.OutOfMemory; }; defer file.close(); @@ -230,14 +237,14 @@ pub const Parser = struct { const tokens = inner_tokenizer.tokenize() catch return ParserError.OutOfMemory; // Resolve the full path of the imported file - const full_path = try std.fs.path.resolve(self.arena, &.{ dir_path, filename }); + const full_path = try std.fs.path.resolve(self.arena, &.{ dir_path, import_filename }); const inner_parser = try Parser.init(tokens, self.arena, full_path); const ast = try inner_parser.parse(); return self.create_node(.{ .IMPORT_DECLARATION = .{ - .filename = filename, + .filename = import_filename, .program = ast, }, }); diff --git a/std/stdlib.src b/std/stdlib.src new file mode 100644 index 0000000..5c09a55 --- /dev/null +++ b/std/stdlib.src @@ -0,0 +1,7 @@ +extern printf = (*i8, varargs) => void; + +let println = (str: *i8, args: varargs) => void { + printf(str, args); + printf("\n"); + return; +}; |