Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bgk- committed Aug 20, 2024
1 parent 3f39725 commit 93bc38e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/backend/compiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,9 @@ pub const Compiler = struct {
self.jump_tree.current.*.dest_ip = self.instructionPos();
defer self.jump_tree.pop();

const locals_count = self.scope.count;
try self.enterScope(.local);
self.scope.count = locals_count;
errdefer self.exitScope() catch {};

const path = try std.mem.join(self.allocator, ".", self.visit_tree.list.items);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/state.zig
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub const State = struct {
if (entry.object.get("number")) |v| return .{ .number = @floatCast(v.float) };
if (entry.object.get("string")) |v| return try vm.gc.create(vm, .{ .string = try vm.allocator.dupe(u8, v.string) });
if (entry.object.get("nil") != null) return Nil;
if (entry.object.get("boolean")) |v| return if (v.bool) True else False;
if (entry.object.get("bool")) |v| return if (v.bool) True else False;
if (entry.object.get("visit")) |v| return .{ .visit = @intCast(v.integer) };
if (entry.object.get("ref")) |v| {
if (refs.get(UUID.fromString(v.string))) |ref| return ref;
Expand Down
12 changes: 8 additions & 4 deletions src/runtime/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,10 @@ pub const Vm = struct {
},
.pop => _ = try self.pop(),
.add => {
const right = try self.pop();
const left = try self.pop();
var right = try self.pop();
var left = try self.pop();
if (right == .visit) right = .{ .number = @floatFromInt(right.visit) };
if (left == .visit) left = .{ .number = @floatFromInt(left.visit) };
if (@intFromEnum(right) != @intFromEnum(left)) {
return self.fail("Cannot add types {s} and {s}", .{ left.typeName(), right.typeName() });
}
Expand Down Expand Up @@ -1044,8 +1046,10 @@ pub const Vm = struct {
}

fn binaryNumberOp(self: *Vm, op: OpCode) !void {
const right = try self.pop();
const left = try self.pop();
var right = try self.pop();
var left = try self.pop();
if (right == .visit) right = .{ .number = @floatFromInt(right.visit) };
if (left == .visit) left = .{ .number = @floatFromInt(left.visit) };
if (right != .number or left != .number)
return self.fail("Cannot perform binary operation on types of '{s}' and '{s}'", .{ left.typeName(), right.typeName() });
const right_num = right.number;
Expand Down
37 changes: 33 additions & 4 deletions test/vm.test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ test "Closures" {
}
}

test "Loops" {
test "While and For Loops" {
const test_cases = .{
.{ .input =
\\ var x = 0
Expand Down Expand Up @@ -639,6 +639,21 @@ test "Loops" {
\\ x
, .value = 10 },
.{ .input =
\\ var x = 0
\\ while true {
\\ var y = 1
\\ while true {
\\ y += x + 1
\\ if y < 10 continue
\\ break;
\\ }
\\ x = x + y
\\ if x < 100 continue
\\ break
\\ }
\\ x
, .value = 190 },
.{ .input =
\\ const list = List{1,2,3,4,5}
\\ var sum = 0
\\ for list |item| {
Expand Down Expand Up @@ -684,6 +699,7 @@ test "Loops" {
var vm = try initTestVm(case.input, mod, false);
defer vm.deinit();
defer vm.bytecode.free(testing.allocator);
errdefer std.log.warn("Error Case: {s}", .{case.input});
vm.interpret() catch |err| {
vm.err.print(std.io.getStdErr().writer());
return err;
Expand Down Expand Up @@ -943,11 +959,15 @@ test "Boughs" {
},
.{ .input =
\\ === START {
\\ var str = "string"
\\ :speaker: "Text goes here"
\\ === OUTER {
\\ var num = 50
\\ :speaker: "Outer text here doesn't happen"
\\ === INNER {
\\ :speaker: "Inner and final text here"
\\ num += 5
\\ str += " gnirts"
\\ :speaker: "Inner and final text here {str} {num}"
\\ }
\\ }
\\ :speaker: "More goes here"
Expand All @@ -961,10 +981,13 @@ test "Boughs" {
var mod = try Module.initEmpty(allocator);
defer mod.deinit();
std.debug.print("\n======\n", .{});
var vm = try initTestVm(case.input, mod, false);
var vm = try initTestVm(case.input, mod, true);
defer vm.deinit();
defer vm.bytecode.free(testing.allocator);
try vm.interpret();
vm.interpret() catch |err| {
vm.err.print(std.io.getStdErr().writer());
return err;
};
}
}

Expand Down Expand Up @@ -1150,6 +1173,12 @@ test "Visits" {
\\ print("START.NAMED: {START.NAMED}")
\\ print("START.NAMED.ONE: {START.NAMED.ONE}")
\\ print("START.NAMED.TWO: {START.NAMED.TWO}")
\\ // test binary operation
\\ print("ADD: {START + 1}")
\\ print("SUBTRACT: {START - 1}")
\\ print("PRODUCT: {START * 2}")
\\ print("DIVISION: {START / 3}")
\\ print("MODULUS: {START % 4}")
\\ }
,
},
Expand Down

0 comments on commit 93bc38e

Please sign in to comment.