vig/src/runner.zig

68 lines
2 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 expr_opt = try parser.parse();
//var expr = ast.mkBinary(
// &ast.mkUnary(
// tokens.Token{ .ttype = .Minus, .lexeme = "-", .line = 1 },
// &ast.mkNum(i32, 123),
// ),
// tokens.Token{ .ttype = .Star, .lexeme = "*", .line = 1 },
// &ast.mkGrouping(&ast.mkNum(f32, 45.67)),
//);
if (expr_opt) |expr_ptr| {
printer.printAst(expr_ptr);
std.debug.warn("\n");
} else {
return Result.CompileError;
}
2019-06-05 00:27:05 +00:00
return Result.Ok;
}
};