Skip to content

Commit

Permalink
feat: std\args()
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Sep 30, 2024
1 parent e0587d1 commit 19bb942
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/buzz_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/buzz_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/lib/buzz_std.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/lib/std.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ export extern fun currentFiber() > fib<any, any?>;
/// 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;
1 change: 1 addition & 0 deletions tests/068-testing.buzz
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "testing" as _;
import "std";

test "Test std lib" {
const t = Tester.init(
Expand Down

0 comments on commit 19bb942

Please sign in to comment.