2019-05-31 19:15:27 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2019-05-31 20:07:08 +00:00
|
|
|
const Allocator = std.mem.Allocator;
|
2019-06-01 00:23:51 +00:00
|
|
|
const Scanner = @import("scanner.zig").Scanner;
|
2019-05-31 20:07:08 +00:00
|
|
|
|
2019-06-01 00:23:51 +00:00
|
|
|
pub var hadError = false;
|
|
|
|
|
|
|
|
fn run(allocator: *Allocator, data: []u8) !void {
|
|
|
|
var stdout_file = try std.io.getStdOut();
|
|
|
|
const stdout = &stdout_file.outStream().stream;
|
|
|
|
|
|
|
|
var scanner = Scanner.init(allocator, data);
|
|
|
|
var tokens = try scanner.scanTokens();
|
|
|
|
var it = tokens.iterator();
|
|
|
|
|
|
|
|
while (it.next()) |token| {
|
2019-06-01 00:46:18 +00:00
|
|
|
switch (token) {
|
|
|
|
.Simple => |value| {
|
|
|
|
try value.printToken(stdout);
|
|
|
|
},
|
|
|
|
.Slice => |value| {
|
|
|
|
try value.printToken(stdout);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-01 00:23:51 +00:00
|
|
|
hadError = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn doError(line: usize, message: []const u8) !void {
|
|
|
|
try errorReport(line, "", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn errorReport(line: usize, where: []const u8, message: []const u8) !void {
|
|
|
|
var stdout_file = try std.io.getStdOut();
|
|
|
|
const stdout = &stdout_file.outStream().stream;
|
|
|
|
|
|
|
|
try stdout.print("[line {}] Error {}: {}\n", line, where, message);
|
|
|
|
hadError = true;
|
|
|
|
}
|
2019-05-31 20:07:08 +00:00
|
|
|
|
|
|
|
fn runFile(allocator: *Allocator, path: []const u8) !void {
|
|
|
|
var lox_file = try std.fs.File.openRead(path);
|
|
|
|
defer lox_file.close();
|
|
|
|
|
|
|
|
const total_bytes = try lox_file.getEndPos();
|
|
|
|
var slice = try allocator.alloc(u8, total_bytes);
|
|
|
|
_ = try lox_file.read(slice);
|
|
|
|
|
2019-06-01 00:23:51 +00:00
|
|
|
try run(allocator, slice);
|
|
|
|
if (hadError) std.os.exit(65);
|
2019-05-31 20:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn runPrompt(allocator: *Allocator) !void {
|
|
|
|
var stdout_file = try std.io.getStdOut();
|
|
|
|
const stdout = &stdout_file.outStream().stream;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
try stdout.print(">");
|
|
|
|
var buffer = try std.Buffer.init(allocator, ""[0..]);
|
|
|
|
|
|
|
|
var line = std.io.readLine(&buffer) catch |err| {
|
|
|
|
if (err == error.EndOfStream) return;
|
|
|
|
return err;
|
|
|
|
};
|
|
|
|
|
2019-06-01 00:23:51 +00:00
|
|
|
try run(allocator, line);
|
2019-05-31 20:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 19:15:27 +00:00
|
|
|
pub fn main() anyerror!void {
|
2019-05-31 20:07:08 +00:00
|
|
|
var da = std.heap.DirectAllocator.init();
|
|
|
|
var arena = std.heap.ArenaAllocator.init(&da.allocator);
|
|
|
|
var allocator = &arena.allocator;
|
|
|
|
|
|
|
|
var args_it = std.process.args();
|
|
|
|
|
|
|
|
const jorts_arg0 = try (args_it.next(allocator) orelse {
|
|
|
|
// if you ever reach this, tell me what is your os lmao
|
|
|
|
unreachable;
|
|
|
|
});
|
|
|
|
|
|
|
|
const lox_path = try (args_it.next(allocator) orelse {
|
|
|
|
try runPrompt(allocator);
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
|
|
|
try runFile(allocator, lox_path);
|
2019-05-31 19:15:27 +00:00
|
|
|
}
|