Compare commits

...

2 Commits

Author SHA1 Message Date
Luna b80cd52c50 main: readd runPrompt and runFile
- main: make run() use the VM struct instance
2019-06-01 16:17:28 -03:00
Luna a9dca436bd remove chunk running code, add draft compiler struct 2019-06-01 16:12:39 -03:00
3 changed files with 40 additions and 61 deletions

View File

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

View File

@ -6,38 +6,20 @@ const Allocator = std.mem.Allocator;
const chunk = @import("chunk.zig");
const vm = @import("vm.zig");
const InterpretResult = vm.InterpretResult;
//const Compiler = @import("compiler.zig").Compiler;
pub var hadError = false;
fn run(allocator: *Allocator, data: []u8) !void {
fn run(allocator: *Allocator, data: []u8) !InterpretResult {
var stdout_file = try std.io.getStdOut();
const stdout = &stdout_file.outStream().stream;
var scanner = try Scanner.init(allocator, data);
var tokens = try scanner.scanTokens();
var it = tokens.iterator();
while (it.next()) |token| {
switch (token) {
.Simple => |value| {
try value.printToken(stdout);
},
.Slice => |value| {
try value.printToken(stdout);
},
.Number => |value| {
try value.printToken(stdout);
},
}
hadError = false;
}
var vmach = try vm.VM.init(allocator, stdout, data, true);
return vmach.interpret();
}
// fn run() !void {}
pub fn doError(line: usize, message: []const u8) !void {
try errorReport(line, "", message);
}
@ -58,8 +40,9 @@ fn runFile(allocator: *Allocator, path: []const u8) !void {
var slice = try allocator.alloc(u8, total_bytes);
_ = try lox_file.read(slice);
try run(allocator, slice);
if (hadError) std.os.exit(65);
var res = try run(allocator, slice);
if (res == vm.InterpretResult.CompileError) std.os.exit(65);
if (res == vm.InterpretResult.RuntimeError) std.os.exit(70);
}
fn runPrompt(allocator: *Allocator) !void {
@ -72,14 +55,15 @@ fn runPrompt(allocator: *Allocator) !void {
var line = std.io.readLine(&buffer) catch |err| {
if (err == error.EndOfStream) return;
return err;
};
try run(allocator, line);
_ = try run(allocator, line);
}
}
pub fn mainOld() anyerror!void {
pub fn main() anyerror!void {
var da = std.heap.DirectAllocator.init();
var arena = std.heap.ArenaAllocator.init(&da.allocator);
defer arena.deinit();
@ -93,15 +77,14 @@ pub fn mainOld() anyerror!void {
});
var lox_path = try (args_it.next(allocator) orelse {
// try runPrompt(allocator);
unreachable;
try runPrompt(allocator);
return;
});
//var vm = VM.init();
//try runFile(allocator, lox_path);
try runFile(allocator, lox_path);
}
pub fn main() !void {
pub fn oldMain() !void {
var da = std.heap.DirectAllocator.init();
var arena = std.heap.ArenaAllocator.init(&da.allocator);
defer arena.deinit();
@ -110,19 +93,8 @@ pub fn main() !void {
var stdout_file = try std.io.getStdOut();
var stdout = &stdout_file.outStream().stream;
var chk = try chunk.Chunk.init(allocator);
// this crashes zig??? lol
// var chk = try chunk.Chunk.init(allocator);
//var opcode_byte: u8 = @enumToInt(chunk.OpCode.Return);
//try chk.write(chunk.OpCode.Return);
try chk.writeConstant(1.2, 123);
try chk.writeConstant(3.4, 123);
try chk.write(chunk.OpCode.Add, 123);
try chk.writeConstant(5.6, 123);
try chk.write(chunk.OpCode.Divide, 123);
try chk.write(chunk.OpCode.Return, 123);
var vmach = try vm.VM.init(allocator, stdout, &chk, true);
_ = try vmach.interpret();
}

View File

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