Skip to content

Commit

Permalink
Merge pull request #3 from Rexicon226/function-frames
Browse files Browse the repository at this point in the history
Basic Function Frames
  • Loading branch information
Rexicon226 authored Feb 29, 2024
2 parents 41ca2b1 + 9e3fff8 commit da03453
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 28 deletions.
9 changes: 7 additions & 2 deletions demo/test.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
a = 1
print(a)
a = 10
def b(x):
# c = "test"
print(x)


b(a)
3 changes: 3 additions & 0 deletions src/compiler/CodeObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const std = @import("std");
const Marshal = @import("Marshal.zig");
const Instruction = @import("Instruction.zig");
const OpCode = @import("opcodes.zig").OpCode;
const Object = @import("../vm/Object.zig");
const Result = Marshal.Result;
const Reference = Marshal.Reference;
const FlagRef = Marshal.FlagRef;
Expand Down Expand Up @@ -31,6 +32,8 @@ stack_size: u32,
/// ByteCode
code: []const u8,

varnames: []Object,

// Interal reference table.
flag_refs: []const ?FlagRef,

Expand Down
9 changes: 9 additions & 0 deletions src/compiler/Marshal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const std = @import("std");
const ObjType = @import("objtype.zig").ObjType;
const CodeObject = @import("CodeObject.zig");
const Object = @import("../vm/Object.zig");
const Vm = @import("../vm/Vm.zig");
const tracer = @import("tracer");

const Marshal = @This();
Expand Down Expand Up @@ -198,6 +200,13 @@ fn read_codeobject(marshal: *Marshal) Result {
const filename = dict.get("filename").?;
co.filename = filename.String;

const varname_tuple = dict.get("varnames").?.Tuple;
const varnames = marshal.allocator.alloc(Object, varname_tuple.len) catch @panic("OOM");
for (varname_tuple, 0..) |elem, i| {
varnames[i] = Vm.loadConst(marshal.allocator, elem) catch @panic("OOM");
}
co.varnames = varnames;

co.consts = dict.get("consts").?.Tuple;
co.stack_size = @intCast(dict.get("stacksize").?.Int);
co.code = dict.get("code").?.String;
Expand Down
20 changes: 20 additions & 0 deletions src/vm/Object.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const Vm = @import("Vm.zig");
const builtins = @import("../builtins.zig");
const Co = @import("../compiler/CodeObject.zig");

const BigIntConst = std.math.big.int.Const;
const BigIntMutable = std.math.big.int.Mutable;
Expand Down Expand Up @@ -43,6 +44,9 @@ pub const Tag = enum(usize) {
/// A builtin Zig defined function.
zig_function,

codeobject,
function,

pub fn PayloadType(comptime t: Tag) type {
assert(@intFromEnum(t) >= Tag.first_payload);

Expand All @@ -57,6 +61,8 @@ pub const Tag = enum(usize) {
.list => Payload.List,

.zig_function => Payload.ZigFunc,
.codeobject => Payload.CodeObject,
.function => Payload.PythonFunction,

.none => unreachable,
else => @compileError("TODO: PayloadType " ++ @tagName(t)),
Expand Down Expand Up @@ -111,6 +117,8 @@ pub const Payload = union(enum) {
zig_func: ZigFunc,
tuple: Tuple,
list: List,
codeobject: CodeObject,
function: PythonFunction,

pub const Value = union(enum) {
int: BigIntManaged,
Expand All @@ -119,7 +127,9 @@ pub const Payload = union(enum) {
};

pub const ZigFunc = *const builtins.func_proto;

pub const Tuple = []const Object;

pub const List = struct {
list: std.ArrayListUnmanaged(Object),

Expand All @@ -140,6 +150,15 @@ pub const Payload = union(enum) {
try vm.stack.append(vm.allocator, return_val);
}
};

pub const CodeObject = struct {
co: *Co,
};

pub const PythonFunction = struct {
name: []const u8,
co: *Co,
};
};

pub fn format(
Expand Down Expand Up @@ -191,6 +210,7 @@ pub fn format(

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

else => try writer.print("TODO: Object.format '{s}'", .{@tagName(object.tag)}),
}
}
Loading

0 comments on commit da03453

Please sign in to comment.