Compare commits
No commits in common. "3f2a8f3801e5dfc0688768360cb5c808c51274e0" and "38715af2000a8f5686792996fa94d368d93d0165" have entirely different histories.
3f2a8f3801
...
38715af200
4 changed files with 13 additions and 63 deletions
|
@ -121,7 +121,6 @@ pub const Compiler = struct {
|
||||||
scanr: Scanner = undefined,
|
scanr: Scanner = undefined,
|
||||||
chunk: *chunks.Chunk,
|
chunk: *chunks.Chunk,
|
||||||
debug_flag: bool = false,
|
debug_flag: bool = false,
|
||||||
vmach: *vm.VM,
|
|
||||||
|
|
||||||
pub fn init(
|
pub fn init(
|
||||||
allocator: *Allocator,
|
allocator: *Allocator,
|
||||||
|
@ -129,7 +128,6 @@ pub const Compiler = struct {
|
||||||
stdout: vm.StdOut,
|
stdout: vm.StdOut,
|
||||||
source: []const u8,
|
source: []const u8,
|
||||||
debug_flag: bool,
|
debug_flag: bool,
|
||||||
vmach: *vm.VM,
|
|
||||||
) Compiler {
|
) Compiler {
|
||||||
return Compiler{
|
return Compiler{
|
||||||
.src = source,
|
.src = source,
|
||||||
|
@ -138,7 +136,6 @@ pub const Compiler = struct {
|
||||||
.stdout = stdout,
|
.stdout = stdout,
|
||||||
.parser = Parser{},
|
.parser = Parser{},
|
||||||
.debug_flag = debug_flag,
|
.debug_flag = debug_flag,
|
||||||
.vmach = vmach,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,9 +231,8 @@ pub const Compiler = struct {
|
||||||
|
|
||||||
fn string(self: *Compiler) !void {
|
fn string(self: *Compiler) !void {
|
||||||
const lexeme_len = self.parser.previous.lexeme.len;
|
const lexeme_len = self.parser.previous.lexeme.len;
|
||||||
|
|
||||||
try self.emitConstant(values.ObjVal(try objects.copyString(
|
try self.emitConstant(values.ObjVal(try objects.copyString(
|
||||||
self.vmach,
|
self.allocator,
|
||||||
self.parser.previous.lexeme[1 .. lexeme_len - 1],
|
self.parser.previous.lexeme[1 .. lexeme_len - 1],
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,6 @@ fn run(allocator: *Allocator, data: []u8) !void {
|
||||||
const stdout = &stdout_file.outStream().stream;
|
const stdout = &stdout_file.outStream().stream;
|
||||||
|
|
||||||
var vmach = try vm.VM.init(allocator, stdout, data, true);
|
var vmach = try vm.VM.init(allocator, stdout, data, true);
|
||||||
defer vmach.deinit();
|
|
||||||
try vmach.interpret();
|
try vmach.interpret();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const vm = @import("vm.zig");
|
|
||||||
|
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
|
|
||||||
|
@ -14,36 +13,25 @@ pub const ObjValue = struct {
|
||||||
pub const Object = struct {
|
pub const Object = struct {
|
||||||
otype: ObjType,
|
otype: ObjType,
|
||||||
value: ObjValue,
|
value: ObjValue,
|
||||||
next: ?*Object = null,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn allocateObject(
|
fn createString(allocator: *Allocator, data: []u8) !*Object {
|
||||||
vmach: *vm.VM,
|
var obj = try allocator.create(Object);
|
||||||
otype: ObjType,
|
obj.otype = ObjType.String;
|
||||||
value: ObjValue,
|
obj.value = ObjValue{ .String = data };
|
||||||
) !*Object {
|
|
||||||
var obj = try vmach.allocator.create(Object);
|
|
||||||
obj.otype = otype;
|
|
||||||
obj.value = value;
|
|
||||||
|
|
||||||
obj.next = vmach.objs;
|
|
||||||
vmach.objs = obj;
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn createString(vmach: *vm.VM, data: []u8) !*Object {
|
pub fn copyString(allocator: *Allocator, data: []const u8) !*Object {
|
||||||
return allocateObject(vmach, ObjType.String, ObjValue{ .String = data });
|
var str = try allocator.alloc(u8, data.len);
|
||||||
}
|
|
||||||
|
|
||||||
pub fn copyString(vmach: *vm.VM, data: []const u8) !*Object {
|
|
||||||
var str = try vmach.allocator.alloc(u8, data.len);
|
|
||||||
std.mem.copy(u8, str, data);
|
std.mem.copy(u8, str, data);
|
||||||
return try createString(vmach, str);
|
|
||||||
|
return try createString(allocator, str);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Assumes it can take ownership of the given data.
|
/// Assumes it can take ownership of the given data.
|
||||||
pub fn takeString(vmach: *vm.VM, data: []u8) !*Object {
|
pub fn takeString(allocator: *Allocator, data: []u8) !*Object {
|
||||||
return try createString(vmach, data);
|
return try createString(allocator, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn printObject(stdout: var, obj: Object) !void {
|
pub fn printObject(stdout: var, obj: Object) !void {
|
||||||
|
|
37
src/vm.zig
37
src/vm.zig
|
@ -46,9 +46,7 @@ pub const VM = struct {
|
||||||
|
|
||||||
stdout: StdOut,
|
stdout: StdOut,
|
||||||
debug_flag: bool,
|
debug_flag: bool,
|
||||||
pub allocator: *std.mem.Allocator,
|
allocator: *std.mem.Allocator,
|
||||||
|
|
||||||
objs: ?*objects.Object = null,
|
|
||||||
|
|
||||||
fn resetStack(self: *VM) void {
|
fn resetStack(self: *VM) void {
|
||||||
self.stackTop = 0;
|
self.stackTop = 0;
|
||||||
|
@ -74,36 +72,6 @@ pub const VM = struct {
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deinitObject(self: *VM, obj: *objects.Object) void {
|
|
||||||
switch (obj.otype) {
|
|
||||||
.String => blk: {
|
|
||||||
self.allocator.free(obj.value.String);
|
|
||||||
self.allocator.destroy(obj);
|
|
||||||
break :blk;
|
|
||||||
},
|
|
||||||
else => unreachable,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deinitObjects(self: *VM) void {
|
|
||||||
var obj_opt: ?*objects.Object = self.objs;
|
|
||||||
|
|
||||||
// doing a while(obj != null) but with optionals
|
|
||||||
while (true) {
|
|
||||||
if (obj_opt) |obj| {
|
|
||||||
var next = obj.next;
|
|
||||||
self.deinitObject(obj);
|
|
||||||
obj_opt = next;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: *VM) void {
|
|
||||||
self.deinitObjects();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn debug(self: *VM, comptime fmt: []const u8, args: ...) void {
|
pub fn debug(self: *VM, comptime fmt: []const u8, args: ...) void {
|
||||||
if (self.debug_flag) {
|
if (self.debug_flag) {
|
||||||
std.debug.warn(fmt, args);
|
std.debug.warn(fmt, args);
|
||||||
|
@ -167,7 +135,7 @@ pub const VM = struct {
|
||||||
[][]u8{ a, b },
|
[][]u8{ a, b },
|
||||||
);
|
);
|
||||||
|
|
||||||
var val = values.ObjVal(try objects.takeString(self, res_str));
|
var val = values.ObjVal(try objects.takeString(self.allocator, res_str));
|
||||||
try self.push(val);
|
try self.push(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -305,7 +273,6 @@ pub const VM = struct {
|
||||||
self.stdout,
|
self.stdout,
|
||||||
self.src,
|
self.src,
|
||||||
self.debug_flag,
|
self.debug_flag,
|
||||||
self,
|
|
||||||
);
|
);
|
||||||
if (!try cmpr.compile(&chk)) {
|
if (!try cmpr.compile(&chk)) {
|
||||||
return InterpretResult.CompileError;
|
return InterpretResult.CompileError;
|
||||||
|
|
Loading…
Reference in a new issue