remove chunk running code, add draft compiler struct

This commit is contained in:
Luna 2019-06-01 16:12:39 -03:00
parent 088674bf0b
commit a9dca436bd
3 changed files with 29 additions and 21 deletions

View File

@ -1,13 +1,15 @@
const token = @import("token.zig"); const std = @import("std");
const scanner = @import("scanner.zig"); const scanner = @import("new_scanner.zig");
const main = @import("main.zig");
const Allocator = std.mem.Allocator;
pub const Compiler = struct { pub const Compiler = struct {
tokens: *scanner.TokenList, src: []const u8,
allocator: *Allocator,
fn init(tokens: *scanner.TokenList) Compiler { pub fn init(allocator: *Allocator, source: []const u8) Compiler {
return Compiler{ .tokens = tokens }; return Compiler{ .src = source, .allocator = allocator };
} }
fn advance(self: *Compiler) void {} pub fn compile(self: *Compiler) void {}
}; };

View File

@ -58,8 +58,9 @@ fn runFile(allocator: *Allocator, path: []const u8) !void {
var slice = try allocator.alloc(u8, total_bytes); var slice = try allocator.alloc(u8, total_bytes);
_ = try lox_file.read(slice); _ = try lox_file.read(slice);
try run(allocator, slice); var res = try run(allocator, slice);
if (hadError) std.os.exit(65); if (res == vm.InterpretResult.CompileError) std.os.exit(65);
if (res == vm.InterpretResult.RuntimeError) std.os.exit(70);
} }
fn runPrompt(allocator: *Allocator) !void { fn runPrompt(allocator: *Allocator) !void {
@ -123,6 +124,6 @@ pub fn main() !void {
try chk.write(chunk.OpCode.Divide, 123); try chk.write(chunk.OpCode.Divide, 123);
try chk.write(chunk.OpCode.Return, 123); try chk.write(chunk.OpCode.Return, 123);
var vmach = try vm.VM.init(allocator, stdout, &chk, true); var vmach = try vm.VM.init(allocator, stdout, "", true);
_ = try vmach.interpret(); _ = vmach.interpret();
} }

View File

@ -1,9 +1,11 @@
const std = @import("std"); const std = @import("std");
const chunk = @import("chunk.zig"); const chunk = @import("chunk.zig");
const value = @import("value.zig"); const value = @import("value.zig");
const compiler = @import("compiler.zig");
const Chunk = chunk.Chunk; const Chunk = chunk.Chunk;
const Value = value.Value; const Value = value.Value;
const Compiler = compiler.Compiler;
const StdOut = *std.io.OutStream(std.fs.File.WriteError); const StdOut = *std.io.OutStream(std.fs.File.WriteError);
@ -14,7 +16,8 @@ pub const InterpretResult = enum {
}; };
pub const VM = struct { pub const VM = struct {
chk: *Chunk, chk: *Chunk = undefined,
src: []const u8,
ip: usize = 0, ip: usize = 0,
stack: []Value, stack: []Value,
@ -31,11 +34,11 @@ pub const VM = struct {
pub fn init( pub fn init(
allocator: *std.mem.Allocator, allocator: *std.mem.Allocator,
stdout: StdOut, stdout: StdOut,
chk: *Chunk, source: []const u8,
debug_flag: bool, debug_flag: bool,
) !VM { ) !VM {
var self = VM{ var self = VM{
.chk = chk, .src = source,
.stack = try allocator.alloc(Value, 256), .stack = try allocator.alloc(Value, 256),
.stdout = stdout, .stdout = stdout,
@ -156,13 +159,15 @@ pub const VM = struct {
} }
} }
pub fn interpret(self: *VM) !InterpretResult { pub fn interpret(self: *VM) InterpretResult {
self.ip = 0; //self.ip = 0;
//self.debug("VM start\n");
self.debug("VM start\n"); //var res = try self.run();
var res = try self.run(); //self.debug("VM end\n");
self.debug("VM end\n"); //return res;
return res; var cmpr = Compiler.init(self.allocator, self.src);
cmpr.compile();
return InterpretResult.Ok;
} }
pub fn push(self: *VM, val: Value) !void { pub fn push(self: *VM, val: Value) !void {