Skip to content

Commit

Permalink
figured out positional function args!
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexicon226 committed Feb 29, 2024
1 parent 7583f78 commit 9e3fff8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
12 changes: 5 additions & 7 deletions demo/test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
a = 1
def b():
c = 20
print(c)
a = 10
def b(x):
# c = "test"
print(x)


b()
print(c)

b(a)
1 change: 1 addition & 0 deletions src/vm/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ pub fn format(

try writer.writeAll(")");
},

else => try writer.print("TODO: Object.format '{s}'", .{@tagName(object.tag)}),
}
}
9 changes: 8 additions & 1 deletion src/vm/Vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ fn execLoadGlobal(vm: *Vm, inst: Instruction) !void {
fn execLoadFast(vm: *Vm, inst: Instruction) !void {
const var_num = inst.extra;
const obj = vm.current_co.varnames[var_num];
log.debug("LoadFast obj tag: {s}", .{@tagName(obj.tag)});
try vm.stack.append(vm.allocator, obj);
}

fn execStoreName(vm: *Vm, inst: Instruction) !void {
const name = vm.current_co.getName(inst.extra);
const tos = vm.stack.pop();

try vm.scopes.items[vm.depth].put(vm.allocator, name, tos);
}

Expand Down Expand Up @@ -242,6 +242,11 @@ fn execCallFunction(vm: *Vm, inst: Instruction) !void {
vm.current_co.* = func.co.*;

vm.depth += 1;

// Set the args.
for (args, 0..) |arg, i| {
vm.current_co.varnames[i] = arg;
}
}
}

Expand Down Expand Up @@ -462,6 +467,8 @@ pub fn loadConst(allocator: Allocator, inst: Marshal.Result) !Object {
fn lookUpwards(vm: *Vm, name: []const u8) ?Object {
const scopes = vm.scopes.items;

log.debug("lookUpwards depth {}", .{vm.depth});

return obj: {
// Check the immediate scope.
if (scopes[vm.depth].get(name)) |val| {
Expand Down

0 comments on commit 9e3fff8

Please sign in to comment.