vig/src/runner.zig

57 lines
1.6 KiB
Zig
Raw Normal View History

2019-06-05 00:27:05 +00:00
const std = @import("std");
const scanners = @import("scanner.zig");
2019-06-05 01:11:11 +00:00
const parsers = @import("parser.zig");
2019-06-05 00:27:05 +00:00
const main = @import("main.zig");
2019-06-05 01:11:11 +00:00
const ast = @import("ast.zig");
2019-06-05 00:27:05 +00:00
2019-06-05 23:29:03 +00:00
const tokens = @import("tokens.zig");
const printer = @import("ast_printer.zig");
2019-06-05 00:27:05 +00:00
const Allocator = std.mem.Allocator;
const Result = main.Result;
2019-06-05 01:11:11 +00:00
const Parser = parsers.Parser;
2019-06-05 00:27:05 +00:00
pub const Runner = struct {
allocator: *Allocator,
stdout: main.StdOut,
2019-06-05 01:11:11 +00:00
pub fn init(allocator: *Allocator, stdout: main.StdOut) Runner {
2019-06-05 00:27:05 +00:00
return Runner{ .allocator = allocator, .stdout = stdout };
}
2019-06-05 01:11:11 +00:00
fn testScanner(self: *Runner, scanner: *scanners.Scanner) !void {
2019-06-05 00:27:05 +00:00
while (true) {
var tok_opt = scanner.nextToken() catch |err| {
try self.stdout.print(
"error at '{}': {}\n",
scanner.currentLexeme(),
err,
);
2019-06-06 01:06:12 +00:00
return Result.ScannerError;
2019-06-05 00:27:05 +00:00
};
if (tok_opt) |tok| {
if (tok.ttype == .EOF) break;
try self.stdout.print("{x}\n", tok);
}
}
2019-06-05 01:11:11 +00:00
}
pub fn execute(self: *Runner, code: []u8) !void {
var scanner = scanners.Scanner.init(self.allocator, code);
try self.testScanner(&scanner);
2019-06-05 00:27:05 +00:00
2019-06-05 01:11:11 +00:00
scanner = scanners.Scanner.init(self.allocator, code);
2019-06-06 01:06:12 +00:00
var parser = Parser.init(self.allocator, &scanner);
var root = try parser.parse();
2019-06-06 01:06:12 +00:00
var it = root.Root.iterator();
2019-06-06 01:06:12 +00:00
2019-08-23 18:42:50 +00:00
std.debug.warn("parse tree\n");
printer.printNode(root, 0);
2019-06-05 00:27:05 +00:00
return Result.Ok;
}
};