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, 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,
@ -128,6 +129,7 @@ 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,
@ -136,6 +138,7 @@ pub const Compiler = struct {
.stdout = stdout, .stdout = stdout,
.parser = Parser{}, .parser = Parser{},
.debug_flag = debug_flag, .debug_flag = debug_flag,
.vmach = vmach,
}; };
} }
@ -231,8 +234,9 @@ 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.allocator, self.vmach,
self.parser.previous.lexeme[1 .. lexeme_len - 1], self.parser.previous.lexeme[1 .. lexeme_len - 1],
))); )));
} }

View File

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

View File

@ -46,7 +46,9 @@ pub const VM = struct {
stdout: StdOut, stdout: StdOut,
debug_flag: bool, debug_flag: bool,
allocator: *std.mem.Allocator, pub allocator: *std.mem.Allocator,
objs: ?*objects.Object = null,
fn resetStack(self: *VM) void { fn resetStack(self: *VM) void {
self.stackTop = 0; self.stackTop = 0;
@ -135,7 +137,7 @@ pub const VM = struct {
[][]u8{ a, b }, [][]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); try self.push(val);
} }
@ -273,6 +275,7 @@ 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;