blob: d938d9b7e7dcf6e794d85a964cabe6df8cd1968d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
const std = @import("std");
const parse = @import("./parse.zig");
const peek = @import("./peek.zig");
const compile = @import("./compile.zig");
pub const peekable = peek.peekable;
pub const Peekable = peek.Peekable;
pub const Expr = parse.Expr;
pub const Lexer = @import("./lexer.zig");
pub fn main() !void {
var arena: std.heap.ArenaAllocator = .init(std.heap.smp_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var bw = std.io.bufferedWriter(std.io.getStdOut().writer());
const stdout = bw.writer();
// var br = std.io.bufferedReader(std.io.getStdIn().reader());
// const stdin = br.reader();
//
// var line: std.ArrayList(u8) = .init(alloc);
// defer line.deinit();
// while (true) {
// try stdin.streamUntilDelimiter(line.writer(), '\n', null);
//
// const lexer = Lexer{.source = line};
//
// try stdout.print("{s}\n", .{line.items});
// }
const source = "1 + 2 + 3";
// var lexer = Lexer{ .source = source };
// while (true) {
// const token = lexer.next().?;
// try stdout.print("{}\n", .{token});
// if (token.type == .Eof) break;
// }
// try stdout.print("\n", .{});
var lexer = peekable(Lexer{ .source = source });
const expr = try parse.expression(allocator, &lexer);
try stdout.print("{}\n", .{expr});
if (lexer.next()) |token| if (token.type != .eof) {
try stdout.print("Unexpected token {}, expected end of file\n", .{token});
};
const block = try compile.compile(allocator, expr);
for (block.instrs) |instr| {
try stdout.print("{}\n", .{instr});
}
try bw.flush(); // Don't forget to flush!
}
test {
_ = peek;
}
|