main: readd runPrompt and runFile

- main: make run() use the VM struct instance
This commit is contained in:
Luna 2019-06-01 16:17:28 -03:00
parent a9dca436bd
commit b80cd52c50

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);
}
@ -73,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();
@ -94,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();
@ -111,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, "", true);
_ = vmach.interpret();
}