Skip to content

Commit

Permalink
Merge pull request #2 from Rexicon226/dump-compiler
Browse files Browse the repository at this point in the history
Banish the middle-step compiler
  • Loading branch information
Rexicon226 authored Feb 29, 2024
2 parents e0432be + 6b7384f commit 41ca2b1
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 419 deletions.
3 changes: 1 addition & 2 deletions demo/test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
a = [1, 2]
a.append(3)
a = 1
print(a)
7 changes: 1 addition & 6 deletions src/Manager.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const tracer = @import("tracer");

const Marshal = @import("compiler/Marshal.zig");
const Vm = @import("vm/Vm.zig");
const Compiler = @import("compiler/Compiler.zig");

const log = std.log.scoped(.manager);

Expand Down Expand Up @@ -46,12 +45,8 @@ pub fn run_pyc(manager: *Manager, file_name: []const u8) !void {
// Parse the code object
const object = try Marshal.load(manager.allocator, source);

// Convert into the nice Instruction format
var compiler = Compiler.init(manager.allocator);
const instructions = try compiler.compile(object);

var vm = try Vm.init();
try vm.run(manager.allocator, instructions);
try vm.run(manager.allocator, object);
}

pub fn run_file(manager: *Manager, file_name: []const u8) !void {
Expand Down
55 changes: 51 additions & 4 deletions src/compiler/CodeObject.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
//! A 3.11 CodeObject
//! A 3.10 CodeObject

const std = @import("std");
const Marshal = @import("Marshal.zig");
const Instruction = @import("Instruction.zig");
const OpCode = @import("opcodes.zig").OpCode;
const Result = Marshal.Result;
const Reference = Marshal.Reference;
const FlagRef = Marshal.FlagRef;

const CodeObject = @This();

/// File name
filename: []const u8,

Expand All @@ -22,16 +26,22 @@ names: []const Result,
name: []const u8,

/// Stack Size
stacksize: u32,
stack_size: u32,

/// ByteCode
code: []const u8,

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

/// Only exist after `co.process()` is run.
instructions: []Instruction = undefined,

/// Where the VM is at in running this CodeObject
index: usize = 0,

pub fn format(
self: @This(),
self: CodeObject,
comptime fmt: []const u8,
_: std.fmt.FormatOptions,
writer: anytype,
Expand All @@ -41,7 +51,7 @@ pub fn format(
try writer.print("Name: {s}\n", .{self.name});
try writer.print("Filename: {s}\n", .{self.filename});
try writer.print("Argument count: {d}\n", .{self.argcount});
try writer.print("Stack size: {d}\n", .{self.stacksize});
try writer.print("Stack size: {d}\n", .{self.stack_size});

try writer.print("Consts:\n", .{});
for (self.consts) |con| {
Expand All @@ -53,3 +63,40 @@ pub fn format(
try writer.print("\t{}\n", .{name.fmt(self)});
}
}

pub fn process(
co: *CodeObject,
allocator: std.mem.Allocator,
) !void {
var instructions = std.ArrayList(Instruction).init(allocator);

const bytes = co.code;

var cursor: u32 = 0;
while (cursor < bytes.len) {
const byte = bytes[cursor];
const op: OpCode = @enumFromInt(byte);

const has_arg = byte >= 90;

const inst: Instruction = .{
.op = op,
.extra = if (has_arg) bytes[cursor + 1] else undefined,
};
try instructions.append(inst);
cursor += 2;
continue;
}

co.instructions = try instructions.toOwnedSlice();
co.index = 0;
}

// Helper functions

pub fn getName(
co: *const CodeObject,
namei: u8,
) []const u8 {
return co.names[namei].String;
}
Loading

0 comments on commit 41ca2b1

Please sign in to comment.