add basics of virtual machine object list

- object: move functions to accept VM pointer, not Allocator
This commit is contained in:
Luna 2019-06-02 14:52:19 -03:00
parent 38715af200
commit cf53b6fc86
3 changed files with 32 additions and 13 deletions

View file

@ -121,6 +121,7 @@ pub const Compiler = struct {
scanr: Scanner = undefined,
chunk: *chunks.Chunk,
debug_flag: bool = false,
vmach: *vm.VM,
pub fn init(
allocator: *Allocator,
@ -128,6 +129,7 @@ pub const Compiler = struct {
stdout: vm.StdOut,
source: []const u8,
debug_flag: bool,
vmach: *vm.VM,
) Compiler {
return Compiler{
.src = source,
@ -136,6 +138,7 @@ pub const Compiler = struct {
.stdout = stdout,
.parser = Parser{},
.debug_flag = debug_flag,
.vmach = vmach,
};
}
@ -231,8 +234,9 @@ pub const Compiler = struct {
fn string(self: *Compiler) !void {
const lexeme_len = self.parser.previous.lexeme.len;
try self.emitConstant(values.ObjVal(try objects.copyString(
self.allocator,
self.vmach,
self.parser.previous.lexeme[1 .. lexeme_len - 1],
)));
}

View file

@ -1,4 +1,5 @@
const std = @import("std");
const vm = @import("vm.zig");
const Allocator = std.mem.Allocator;
@ -13,25 +14,36 @@ pub const ObjValue = struct {
pub const Object = struct {
otype: ObjType,
value: ObjValue,
next: ?*Object = null,
};
fn createString(allocator: *Allocator, data: []u8) !*Object {
var obj = try allocator.create(Object);
obj.otype = ObjType.String;
obj.value = ObjValue{ .String = data };
pub fn allocateObject(
vmach: *vm.VM,
otype: ObjType,
value: ObjValue,
) !*Object {
var obj = try vmach.allocator.create(Object);
obj.otype = otype;
obj.value = value;
obj.next = vmach.objs;
vmach.objs = obj;
return obj;
}
pub fn copyString(allocator: *Allocator, data: []const u8) !*Object {
var str = try allocator.alloc(u8, data.len);
std.mem.copy(u8, str, data);
fn createString(vmach: *vm.VM, data: []u8) !*Object {
return allocateObject(vmach, ObjType.String, ObjValue{ .String = data });
}
return try createString(allocator, str);
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);
return try createString(vmach, str);
}
/// Assumes it can take ownership of the given data.
pub fn takeString(allocator: *Allocator, data: []u8) !*Object {
return try createString(allocator, data);
pub fn takeString(vmach: *vm.VM, data: []u8) !*Object {
return try createString(vmach, data);
}
pub fn printObject(stdout: var, obj: Object) !void {

View file

@ -46,7 +46,9 @@ pub const VM = struct {
stdout: StdOut,
debug_flag: bool,
allocator: *std.mem.Allocator,
pub allocator: *std.mem.Allocator,
objs: ?*objects.Object = null,
fn resetStack(self: *VM) void {
self.stackTop = 0;
@ -135,7 +137,7 @@ pub const VM = struct {
[][]u8{ a, b },
);
var val = values.ObjVal(try objects.takeString(self.allocator, res_str));
var val = values.ObjVal(try objects.takeString(self, res_str));
try self.push(val);
}
@ -273,6 +275,7 @@ pub const VM = struct {
self.stdout,
self.src,
self.debug_flag,
self,
);
if (!try cmpr.compile(&chk)) {
return InterpretResult.CompileError;