From 19bb942499a78bb12ce5cb15b0ec6f5768758caa Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Mon, 30 Sep 2024 11:21:10 +0200 Subject: [PATCH] feat: std\args() --- CHANGELOG.md | 1 + src/buzz_api.zig | 9 +++++++++ src/lib/buzz_api.zig | 1 + src/lib/buzz_std.zig | 7 ++++++- src/lib/std.buzz | 3 +++ tests/068-testing.buzz | 1 + 6 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d682e78b..21cacfbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ list[?10] == null; ``` - User input is syntax highlighted in REPL (https://github.com/buzz-language/buzz/issues/217) - REPL handles multilines input (https://github.com/buzz-language/buzz/issues/218) +- `std\args()`: returns the command line arguments with which the script was launched ## Modified - Enum can now have `rg`, `ud`, `void`, `pat` has value type diff --git a/src/buzz_api.zig b/src/buzz_api.zig index b507c002..fdc34acc 100644 --- a/src/buzz_api.zig +++ b/src/buzz_api.zig @@ -79,6 +79,15 @@ export fn bz_peek(vm: *VM, dist: u32) Value { return vm.peek(dist); } +/// Absolute access to the stack. +export fn bz_at(vm: *VM, at: u32) Value { + if (at < vm.current_fiber.stack.len) { + return vm.current_fiber.stack[at]; + } + + return Value.Null; +} + /// Converts a value to a string export fn bz_valueToString(value: Value, len: *usize) ?[*]const u8 { if (!value.isObj() or value.obj().obj_type != .String) { diff --git a/src/lib/buzz_api.zig b/src/lib/buzz_api.zig index 29439400..da9bcd49 100644 --- a/src/lib/buzz_api.zig +++ b/src/lib/buzz_api.zig @@ -202,6 +202,7 @@ pub const VM = opaque { pub extern fn bz_push(self: *VM, value: Value) void; pub extern fn bz_pop(self: *VM) Value; pub extern fn bz_peek(self: *VM, distance: u32) Value; + pub extern fn bz_at(vm: *VM, at: u32) Value; pub extern fn bz_pushError(self: *VM, qualified_name: [*]const u8, len: usize, message: ?[*]const u8, mlen: usize) void; pub extern fn bz_pushErrorEnum(self: *VM, qualified_name: [*]const u8, name_len: usize, case: [*]const u8, case_len: usize) void; pub extern fn bz_stringToValue(vm: *VM, string: ?[*]const u8, len: usize) Value; diff --git a/src/lib/buzz_std.zig b/src/lib/buzz_std.zig index 43da1520..7581950e 100644 --- a/src/lib/buzz_std.zig +++ b/src/lib/buzz_std.zig @@ -9,7 +9,12 @@ pub const os = if (is_wasm) else std.os; -// fun random(int? min = null, int? max = null) > int +pub export fn args(ctx: *api.NativeCtx) c_int { + ctx.vm.bz_push(ctx.vm.bz_at(1)); + + return 1; +} + pub export fn random(ctx: *api.NativeCtx) c_int { if (is_wasm) { unreachable; diff --git a/src/lib/std.buzz b/src/lib/std.buzz index 3cb63d3b..cde2d96d 100644 --- a/src/lib/std.buzz +++ b/src/lib/std.buzz @@ -55,4 +55,7 @@ export extern fun currentFiber() > fib; /// Print message and exit program extern fun buzzPanic(message: str) > void; +/// Returns the command line arguments with which the script was launched +export extern fun args() > [str]; + export buzzPanic as panic; diff --git a/tests/068-testing.buzz b/tests/068-testing.buzz index f8e5d166..cdcbb2d8 100644 --- a/tests/068-testing.buzz +++ b/tests/068-testing.buzz @@ -1,4 +1,5 @@ import "testing" as _; +import "std"; test "Test std lib" { const t = Tester.init(