Skip to content

Commit

Permalink
Update prints, revert to number as f32
Browse files Browse the repository at this point in the history
  • Loading branch information
bgk- committed Jul 31, 2024
1 parent e73ca92 commit 3f39725
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 141 deletions.
68 changes: 27 additions & 41 deletions src/backend/bytecode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -155,29 +155,28 @@ pub const Bytecode = struct {
};
}

pub fn print(code: *Bytecode, writer: anytype) void {
writer.print("\n==BYTECODE==\n", .{});
printInstructions(writer, code.instructions, code.constants);
writer.print("\n==DEBUG==\n", .{});
printDebugInfo(writer, code.debug_info);
pub fn print(code: *Bytecode, writer: anytype) !void {
try writer.print("\n==BYTECODE==\n", .{});
try printInstructions(writer, code.instructions);
try writer.print("\n==DEBUG==\n", .{});
try printDebugInfo(writer, code.debug_info);
}

pub fn printDebugInfo(writer: anytype, debug: []DebugInfo) void {
pub fn printDebugInfo(writer: anytype, debug: []DebugInfo) !void {
for (debug) |info| {
writer.print("{s}\n", .{info.file});
try writer.print("{s}\n", .{info.file});
for (info.ranges.items) |r| {
writer.print(" start: {}, end: {}, line: {}\n", .{ r.start, r.end, r.line });
try writer.print(" start: {}, end: {}, line: {}\n", .{ r.start, r.end, r.line });
}
}
}

pub fn printInstructions(writer: anytype, instructions: []const u8, constants: ?[]Value) void {
pub fn printInstructions(writer: anytype, instructions: []const u8) !void {
var i: usize = 0;
while (i < instructions.len) {
errdefer writer.print("{any}", .{instructions[i..]});
writer.print("{d:0>4} ", .{i});
try writer.print("{d:0>4} ", .{i});
const op: OpCode = @enumFromInt(instructions[i]);
writer.print("{s: <16} ", .{op.toString()});
try writer.print("{s: <16} ", .{op.toString()});
i += 1;
switch (op) {
.jump,
Expand All @@ -189,19 +188,19 @@ pub const Bytecode = struct {
.visit,
=> {
const dest = std.mem.readVarInt(u32, instructions[i..(i + 4)], .little);
writer.print("{d: >8}", .{dest});
try writer.print("{d: >8}", .{dest});
i += 4;
},
.divert => {
var count = instructions[i];
i += 1;
const dest = std.mem.readVarInt(u32, instructions[i..(i + 4)], .little);
writer.print("{d: >8} ", .{dest});
try writer.print("{d: >8} ", .{dest});
i += 4;
count -= 1;
while (count > 0) : (count -= 1) {
const next = std.mem.readVarInt(u32, instructions[i..(i + 4)], .little);
writer.print(" {d}", .{next});
try writer.print(" {d}", .{next});
i += 4;
}
},
Expand All @@ -212,7 +211,7 @@ pub const Bytecode = struct {
.set,
=> {
const dest = std.mem.readVarInt(u16, instructions[i..(i + 2)], .little);
writer.print("{d: >8}", .{dest});
try writer.print("{d: >8}", .{dest});
i += 2;
},
.call,
Expand All @@ -223,32 +222,23 @@ pub const Bytecode = struct {
.set_free,
=> {
const dest = instructions[i];
writer.print("{d: >8}", .{dest});
try writer.print("{d: >8}", .{dest});
i += 1;
},
.constant => {
const index = std.mem.readVarInt(u32, instructions[i..(i + 4)], .little);
writer.print("{d: >8} ", .{index});
try writer.print("{d: >8} ", .{index});
i += 4;
if (constants) |c| {
if (index >= c.len) {
writer.print("Constant Index {} out of bounds. Total length {}", .{ index, c.len });
break;
}
var value = c[index];
writer.print(" = ", .{});
value.print(writer, c);
}
},
.dialogue => {
const has_speaker = instructions[i] == 1;
const tag_count = instructions[i + 1];
_ = tag_count;
const id = std.mem.readVarInt(u32, instructions[(i + 2)..(i + 6)], .little);
i += 6;
writer.print("{: >8}", .{has_speaker});
writer.print(" = ", .{});
writer.print("{}", .{id});
try writer.print("{: >8}", .{has_speaker});
try writer.print(" = ", .{});
try writer.print("{}", .{id});
},
.choice => {
const dest = std.mem.readVarInt(u32, instructions[i..(i + 4)], .little);
Expand All @@ -263,31 +253,27 @@ pub const Bytecode = struct {
const tag_count = instructions[i + 1];
_ = tag_count;
i += 1;
writer.print("{d: >8}", .{dest});
writer.print(" unique: {}", .{is_unique});
try writer.print("{d: >8}", .{dest});
try writer.print(" unique: {}", .{is_unique});
},
.string, .closure => {
const index = std.mem.readVarInt(u32, instructions[i..(i + 4)], .little);
i += 4;
writer.print("{d: >8}", .{index});
try writer.print("{d: >8}", .{index});
i += 1;
writer.print(" = ", .{});
if (constants) |c| {
var value = c[index];
value.print(writer, constants);
}
try writer.print(" = ", .{});
},
.prong => {
const index = std.mem.readVarInt(u32, instructions[i..(i + 4)], .little);
i += 4;
writer.print("{d: >8}", .{index});
try writer.print("{d: >8}", .{index});
const count = instructions[i];
i += 1;
writer.print(" = {d}", .{count});
try writer.print(" = {d}", .{count});
},
else => {},
}
writer.print("\n", .{});
try writer.print("\n", .{});
}
}
};
2 changes: 1 addition & 1 deletion src/export/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ pub export fn setExtern(vm_ptr: usize, name_ptr: [*:0]const u8, exp_value: Expor
const runner: *ExportRunner = @fieldParentPtr("runner", vm.runner);
const logger = runner.logger;
const name = std.mem.sliceTo(name_ptr, 0);
logger.log("Setting {s} to {s}", .{ name, @tagName(exp_value.tag) }, .debug);
const value = exp_value.toValue(vm, @ptrFromInt(free_ptr)) catch |err| {
logger.log("Could not create Value \"{s}\": {s}", .{ name, @errorName(err) }, .err);
return;
};
logger.log("Setting {s} to {s}", .{ name, value }, .debug);

vm.setExtern(name, value) catch |err| {
logger.log("Could not set Export value \"{s}\": {s}", .{ name, @errorName(err) }, .err);
Expand Down
2 changes: 1 addition & 1 deletion src/export/value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub const ExportValue = extern struct {
data: extern union {
nil: void,
bool: bool,
number: f64,
number: f32,
string: ExportString,
list: extern struct {
items: [*c]ExportValue,
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub const Expression = struct {
},
@"extern": void,
boolean: bool,
number: f64,
number: f32,
string: struct {
raw: []const u8,
value: []const u8,
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ pub const Parser = struct {
.identifier => try self.identifierExpression(),
.number => blk: {
const string_number = self.file.source[self.current_token.start..self.current_token.end];
const value = try std.fmt.parseFloat(f64, string_number);
const value = try std.fmt.parseFloat(f32, string_number);
break :blk .{
.token = start,
.type = .{
Expand Down
18 changes: 9 additions & 9 deletions src/runtime/builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ pub const Rnd = struct {
},
};
fn builtin(_: *Vm, args: []Value) Value {
if (r == null) r = std.rand.DefaultPrng.init(std.crypto.random.int(u64));
const start = @as(i64, @intFromFloat(args[0].number));
const end = @as(i64, @intFromFloat(args[1].number));
return .{ .number = @as(f64, @floatFromInt(r.?.random().intRangeAtMost(i64, start, end))) };
if (r == null) r = std.rand.DefaultPrng.init(std.crypto.random.int(u32));
const start = @as(i32, @intFromFloat(args[0].number));
const end = @as(i32, @intFromFloat(args[1].number));
return .{ .number = @as(f32, @floatFromInt(r.?.random().intRangeAtMost(i32, start, end))) };
}
};

Expand All @@ -47,7 +47,7 @@ const Rnd01 = struct {
fn builtin(_: *Vm, args: []Value) Value {
if (r == null) r = std.rand.DefaultPrng.init(std.crypto.random.int(u64));
_ = args;
return .{ .number = r.?.random().float(f64) };
return .{ .number = r.?.random().float(f32) };
}
};

Expand Down Expand Up @@ -88,9 +88,9 @@ const Print = struct {
},
};
fn builtin(_: *Vm, args: []Value) Value {
const writer = std.debug;
args[0].print(writer, null);
writer.print("\n", .{});
const writer = std.io.getStdErr().writer();
args[0].print(writer) catch unreachable;
writer.print("\n", .{}) catch unreachable;
return Void;
}
};
Expand Down Expand Up @@ -196,7 +196,7 @@ pub const Count = struct {
.set => |s| s.count(),
else => 0,
};
return .{ .number = @as(f64, @floatFromInt(count)) };
return .{ .number = @as(f32, @floatFromInt(count)) };
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ pub const Vm = struct {
try self.push(builtins.Has.value);
try self.push(target);
} else if (std.mem.eql(u8, name, "count")) {
try self.push(.{ .number = @as(f64, @floatFromInt(o.data.string.len)) });
try self.push(.{ .number = @as(f32, @floatFromInt(o.data.string.len)) });
} else return self.fail("Unknown method '{s}' on string. Only \"count\", \"has\" are allowed.", .{index.obj.data.string});
}
},
Expand Down
Loading

0 comments on commit 3f39725

Please sign in to comment.