diff --git a/README.md b/README.md index ff7acefe..b1628141 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ A small/lightweight statically typed scripting language written in Zig ## How to build and install -_Latest zig version supported: 0.14.0-dev.1588+2111f4c38_ +_Latest zig version supported: 0.14.0-dev.2162+3054486d1_ ### Requirements - Since this is built with Zig, you should be able to build buzz on a wide variety of architectures even though this has only been tested on x86/M1. diff --git a/src/FFI.zig b/src/FFI.zig index 9610703a..6e3c637d 100644 --- a/src/FFI.zig +++ b/src/FFI.zig @@ -14,28 +14,28 @@ const Self = @This(); const basic_types = std.StaticStringMap(o.ObjTypeDef).initComptime( .{ - .{ "u8", .{ .def_type = .Integer } }, - .{ "i8", .{ .def_type = .Integer } }, - .{ "u16", .{ .def_type = .Integer } }, - .{ "i16", .{ .def_type = .Integer } }, - .{ "i32", .{ .def_type = .Integer } }, + .{ "u8", o.ObjTypeDef{ .def_type = .Integer } }, + .{ "i8", o.ObjTypeDef{ .def_type = .Integer } }, + .{ "u16", o.ObjTypeDef{ .def_type = .Integer } }, + .{ "i16", o.ObjTypeDef{ .def_type = .Integer } }, + .{ "i32", o.ObjTypeDef{ .def_type = .Integer } }, // Could it be > 32bits one some systems? - .{ "c_int", .{ .def_type = .Integer } }, + .{ "c_int", o.ObjTypeDef{ .def_type = .Integer } }, - .{ "c_uint", .{ .def_type = .Double } }, - .{ "u32", .{ .def_type = .Double } }, - .{ "i64", .{ .def_type = .Double } }, - .{ "f32", .{ .def_type = .Double } }, - .{ "f64", .{ .def_type = .Double } }, + .{ "c_uint", o.ObjTypeDef{ .def_type = .Double } }, + .{ "u32", o.ObjTypeDef{ .def_type = .Double } }, + .{ "i64", o.ObjTypeDef{ .def_type = .Double } }, + .{ "f32", o.ObjTypeDef{ .def_type = .Double } }, + .{ "f64", o.ObjTypeDef{ .def_type = .Double } }, - .{ "u64", .{ .def_type = .UserData } }, - .{ "usize", .{ .def_type = .UserData } }, + .{ "u64", o.ObjTypeDef{ .def_type = .UserData } }, + .{ "usize", o.ObjTypeDef{ .def_type = .UserData } }, - .{ "bool", .{ .def_type = .Bool } }, + .{ "bool", o.ObjTypeDef{ .def_type = .Bool } }, - .{ "void", .{ .def_type = .Void } }, - .{ "anyopaque", .{ .def_type = .Void } }, + .{ "void", o.ObjTypeDef{ .def_type = .Void } }, + .{ "anyopaque", o.ObjTypeDef{ .def_type = .Void } }, }, ); diff --git a/src/Parser.zig b/src/Parser.zig index 0c60679f..8d6d5790 100644 --- a/src/Parser.zig +++ b/src/Parser.zig @@ -1479,25 +1479,7 @@ fn statement(self: *Self, hanging: bool, loop_scope: ?LoopScope) !?Ast.Node.Inde std.debug.assert(!hanging); return try self.outStatement(); } else if (try self.match(.Throw)) { - const start_location = self.current_token.? - 1; - // For now we don't care about the type. Later if we have `Error` type of data, we'll type check this - const error_value = try self.expression(false); - - try self.consume(.Semicolon, "Expected `;` after statement."); - - return try self.ast.appendNode( - .{ - .tag = .Throw, - .location = start_location, - .end_location = self.current_token.? - 1, - .components = .{ - .Throw = .{ - .expression = error_value, - .unconditional = self.current.?.scope_depth == 1, - }, - }, - }, - ); + return try self.throw(); } } @@ -8647,6 +8629,28 @@ fn outStatement(self: *Self) Error!Ast.Node.Index { ); } +fn throw(self: *Self) Error!Ast.Node.Index { + const start_location = self.current_token.? - 1; + // For now we don't care about the type. Later if we have `Error` type of data, we'll type check this + const error_value = try self.expression(false); + + try self.consume(.Semicolon, "Expected `;` after statement."); + + return try self.ast.appendNode( + .{ + .tag = .Throw, + .location = start_location, + .end_location = self.current_token.? - 1, + .components = .{ + .Throw = .{ + .expression = error_value, + .unconditional = self.current.?.scope_depth == 1, + }, + }, + }, + ); +} + fn namespaceStatement(self: *Self) Error!Ast.Node.Index { const start_location = self.current_token.? - 1; diff --git a/src/lib/buzz_fs.zig b/src/lib/buzz_fs.zig index b3ffaf49..284d4aab 100644 --- a/src/lib/buzz_fs.zig +++ b/src/lib/buzz_fs.zig @@ -181,16 +181,22 @@ pub export fn move(ctx: *api.NativeCtx) c_int { return -1; }; } else { - const source_absolute = if (source_is_absolute) source_slice else std.fs.cwd().realpathAlloc(api.VM.allocator, source_slice) catch |err| { - handleRealpathError(ctx, err); - - return -1; - }; - const destination_absolute = if (destination_is_absolute) destination_slice else std.fs.cwd().realpathAlloc(api.VM.allocator, destination_slice) catch |err| { - handleRealpathError(ctx, err); - - return -1; - }; + const source_absolute = if (source_is_absolute) + source_slice + else + std.fs.cwd().realpathAlloc(api.VM.allocator, source_slice) catch |err| { + handleRealpathError(ctx, err); + + return -1; + }; + const destination_absolute = if (destination_is_absolute) + destination_slice + else + std.fs.cwd().realpathAlloc(api.VM.allocator, destination_slice) catch |err| { + handleRealpathError(ctx, err); + + return -1; + }; defer { if (source_is_absolute) { api.VM.allocator.free(source_absolute); diff --git a/src/lib/buzz_io.zig b/src/lib/buzz_io.zig index 3e8a0e93..c9a8123b 100644 --- a/src/lib/buzz_io.zig +++ b/src/lib/buzz_io.zig @@ -129,8 +129,12 @@ fn handleFileReadWriteError(ctx: *api.NativeCtx, err: anytype) void { error.ConnectionResetByPeer, error.ConnectionTimedOut, error.NotOpenForReading, + error.LockViolation, => ctx.vm.pushErrorEnum("errors.ReadWriteError", @errorName(err)), + error.ProcessNotFound, + => ctx.vm.pushErrorEnum("errors.ExecError", @errorName(err)), + error.Unexpected => ctx.vm.pushError("errors.UnexpectedError", null), error.OutOfMemory => { ctx.vm.bz_panic("Out of memory", "Out of memory".len); @@ -192,8 +196,12 @@ fn handleFileReadLineError(ctx: *api.NativeCtx, err: anytype) void { error.NotOpenForReading, error.OperationAborted, error.StreamTooLong, + error.LockViolation, => ctx.vm.pushErrorEnum("errors.ReadWriteError", @errorName(err)), + error.ProcessNotFound, + => ctx.vm.pushErrorEnum("errors.ExecError", @errorName(err)), + error.OutOfMemory => { ctx.vm.bz_panic("Out of memory", "Out of memory".len); unreachable; @@ -258,8 +266,12 @@ fn handleFileReadAllError(ctx: *api.NativeCtx, err: anytype) void { error.ConnectionResetByPeer, error.ConnectionTimedOut, error.NotOpenForReading, + error.LockViolation, => ctx.vm.pushErrorEnum("errors.ReadWriteError", @errorName(err)), + error.ProcessNotFound, + => ctx.vm.pushErrorEnum("errors.ExecError", @errorName(err)), + error.Unexpected => ctx.vm.pushError("errors.UnexpectedError", null), } } @@ -333,15 +345,20 @@ pub export fn FileWrite(ctx: *api.NativeCtx) c_int { error.SystemResources, error.WouldBlock, => ctx.vm.pushErrorEnum("errors.FileSystemError", @errorName(err)), + error.BrokenPipe, error.ConnectionResetByPeer, error.LockViolation, error.NotOpenForWriting, error.OperationAborted, => ctx.vm.pushErrorEnum("errors.ReadWriteError", @errorName(err)), + error.InputOutput => ctx.vm.pushErrorEnum("errors.SocketError", "InputOutput"), error.InvalidArgument => ctx.vm.pushError("errors.InvalidArgumentError", null), error.Unexpected => ctx.vm.pushError("errors.UnexpectedError", null), + + error.ProcessNotFound, + => ctx.vm.pushErrorEnum("errors.ExecError", @errorName(err)), } return -1; diff --git a/src/lib/buzz_os.zig b/src/lib/buzz_os.zig index ec0944be..34025607 100644 --- a/src/lib/buzz_os.zig +++ b/src/lib/buzz_os.zig @@ -283,12 +283,14 @@ fn handleConnectError(ctx: *api.NativeCtx, err: anytype) void { error.BrokenPipe, error.NotOpenForReading, error.OperationAborted, + error.LockViolation, => ctx.vm.pushErrorEnum("errors.ReadWriteError", @errorName(err)), error.OutOfMemory => { ctx.vm.bz_panic("Out of memory", "Out of memory".len); unreachable; }, + error.ProcessNotFound => ctx.vm.pushErrorEnum("errors.ExecError", @errorName(err)), error.Unexpected => ctx.vm.pushError("errors.UnexpectedError", null), error.Overflow => ctx.vm.pushError("errors.OverflowError", null), } @@ -474,8 +476,12 @@ fn handleReadLineError(ctx: *api.NativeCtx, err: anytype) void { error.NotOpenForReading, error.OperationAborted, error.StreamTooLong, + error.LockViolation, => ctx.vm.pushErrorEnum("errors.ReadWriteError", @errorName(err)), + error.ProcessNotFound, + => ctx.vm.pushErrorEnum("errors.ExecError", @errorName(err)), + error.Unexpected => ctx.vm.pushError("errors.UnexpectedError", null), error.OutOfMemory => { @@ -607,6 +613,8 @@ pub export fn SocketWrite(ctx: *api.NativeCtx) c_int { => ctx.vm.pushErrorEnum("errors.ReadWriteError", @errorName(err)), error.Unexpected => ctx.vm.pushError("errors.UnexpectedError", null), error.InvalidArgument => ctx.vm.pushError("errors.InvalidArgumentError", null), + error.ProcessNotFound, + => ctx.vm.pushErrorEnum("errors.ExecError", @errorName(err)), } return -1; diff --git a/src/lib/buzz_std.zig b/src/lib/buzz_std.zig index 7581950e..3b570959 100644 --- a/src/lib/buzz_std.zig +++ b/src/lib/buzz_std.zig @@ -60,12 +60,7 @@ pub export fn toInt(ctx: *api.NativeCtx) c_int { const value = ctx.vm.bz_peek(0); ctx.vm.bz_push( - api.Value.fromInteger( - if (value.isFloat()) - @intFromFloat(value.double()) - else - value.integer(), - ), + api.Value.fromInteger(@intFromFloat(value.double())), ); return 1; @@ -75,12 +70,7 @@ pub export fn toDouble(ctx: *api.NativeCtx) c_int { const value = ctx.vm.bz_peek(0); ctx.vm.bz_push( - api.Value.fromFloat( - if (value.isInteger()) - @floatFromInt(value.integer()) - else - value.double(), - ), + api.Value.fromFloat(@floatFromInt(value.integer())), ); return 1;