add file reads and main prompt

This commit is contained in:
Luna 2019-05-31 17:07:08 -03:00
parent b3ea9637bd
commit 31b0fa783c

View file

@ -1,5 +1,53 @@
const std = @import("std");
pub fn main() anyerror!void {
std.debug.warn("All your base are belong to us.\n");
const Allocator = std.mem.Allocator;
fn run(data: []u8) void {}
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);
run(slice);
}
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;
};
run(line);
}
}
pub fn main() anyerror!void {
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);
}