2019-06-01 19:12:39 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const scanner = @import("new_scanner.zig");
|
2019-06-01 19:37:24 +00:00
|
|
|
const vm = @import("vm.zig");
|
2019-06-01 19:12:39 +00:00
|
|
|
|
|
|
|
const Allocator = std.mem.Allocator;
|
2019-06-01 19:37:24 +00:00
|
|
|
const TokenType = @import("token.zig").TokenType;
|
2019-06-01 04:20:06 +00:00
|
|
|
|
|
|
|
pub const Compiler = struct {
|
2019-06-01 19:12:39 +00:00
|
|
|
src: []const u8,
|
2019-06-01 19:37:24 +00:00
|
|
|
stdout: vm.StdOut,
|
2019-06-01 19:12:39 +00:00
|
|
|
allocator: *Allocator,
|
2019-06-01 04:20:06 +00:00
|
|
|
|
2019-06-01 19:37:24 +00:00
|
|
|
pub fn init(
|
|
|
|
allocator: *Allocator,
|
|
|
|
stdout: vm.StdOut,
|
|
|
|
source: []const u8,
|
|
|
|
) Compiler {
|
|
|
|
return Compiler{
|
|
|
|
.src = source,
|
|
|
|
.allocator = allocator,
|
|
|
|
.stdout = stdout,
|
|
|
|
};
|
2019-06-01 04:20:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 19:37:24 +00:00
|
|
|
pub fn compile(self: *Compiler) !void {
|
2019-06-01 20:20:50 +00:00
|
|
|
var scanr = try scanner.Scanner.init(self.allocator, self.src);
|
2019-06-01 19:37:24 +00:00
|
|
|
var line: usize = 0;
|
|
|
|
while (true) {
|
2019-06-01 20:07:22 +00:00
|
|
|
var token_opt = scanr.scanToken() catch |err| {
|
|
|
|
std.debug.warn("Scan Error: {x}\n", err);
|
|
|
|
std.debug.warn(
|
|
|
|
"line: {}, cur lexeme: {}\n",
|
|
|
|
scanr.line,
|
|
|
|
scanr.currentLexeme(),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
};
|
2019-06-01 19:37:24 +00:00
|
|
|
|
2019-06-01 20:07:22 +00:00
|
|
|
if (token_opt) |token| {
|
|
|
|
if (token.line != line) {
|
|
|
|
try self.stdout.print("{} ", token.line);
|
|
|
|
line = token.line;
|
|
|
|
} else {
|
|
|
|
try self.stdout.print(" | ");
|
|
|
|
}
|
|
|
|
|
|
|
|
try self.stdout.print("{} '{}'\n", token.ttype, token.lexeme);
|
|
|
|
if (token.ttype == TokenType.EOF) break;
|
2019-06-01 19:37:24 +00:00
|
|
|
} else {
|
2019-06-01 20:07:22 +00:00
|
|
|
break;
|
2019-06-01 19:37:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-01 19:21:36 +00:00
|
|
|
}
|
2019-06-01 04:20:06 +00:00
|
|
|
};
|