zig/src-self-hosted/main.zig

137 lines
3.6 KiB
Zig
Raw Normal View History

const std = @import("std");
const mem = std.mem;
2017-10-24 22:08:20 +08:00
const builtin = @import("builtin");
const io = std.io;
const os = std.os;
const heap = std.heap;
const warn = std.debug.warn;
const Tokenizer = @import("tokenizer.zig").Tokenizer;
const Token = @import("tokenizer.zig").Token;
const Parser = @import("parser.zig").Parser;
2017-12-05 11:05:27 +08:00
pub fn main() -> %void {
main2() %% |err| {
warn("{}\n", @errorName(err));
2017-10-22 05:31:06 +08:00
return err;
};
2017-12-05 11:05:27 +08:00
}
pub fn main2() -> %void {
var incrementing_allocator = %return heap.IncrementingAllocator.init(10 * 1024 * 1024);
2017-10-22 05:31:06 +08:00
defer incrementing_allocator.deinit();
const allocator = &incrementing_allocator.allocator;
2017-10-24 22:08:20 +08:00
2017-12-07 07:22:52 +08:00
const args = %return os.argsAlloc(allocator);
defer os.argsFree(allocator, args);
const target_file = args[1];
2017-10-24 22:08:20 +08:00
2017-12-05 11:05:27 +08:00
const target_file_buf = %return io.readFileAlloc(target_file, allocator);
defer allocator.free(target_file_buf);
var stderr_file = %return std.io.getStdErr();
var stderr_file_out_stream = std.io.FileOutStream.init(&stderr_file);
const out_stream = &stderr_file_out_stream.stream;
2017-10-22 05:31:06 +08:00
2017-12-09 12:15:43 +08:00
warn("====input:====\n");
2017-12-05 11:05:27 +08:00
warn("{}", target_file_buf);
2017-12-05 12:09:03 +08:00
2017-12-09 12:15:43 +08:00
warn("====tokenization:====\n");
2017-12-05 13:20:23 +08:00
{
var tokenizer = Tokenizer.init(target_file_buf);
while (true) {
const token = tokenizer.next();
tokenizer.dump(token);
if (token.id == Token.Id.Eof) {
2017-12-05 13:20:23 +08:00
break;
}
2017-12-05 12:09:03 +08:00
}
}
2017-12-05 13:20:23 +08:00
2017-12-09 12:15:43 +08:00
warn("====parse:====\n");
2017-12-05 13:20:23 +08:00
var tokenizer = Tokenizer.init(target_file_buf);
2017-12-09 12:15:43 +08:00
var parser = Parser.init(&tokenizer, allocator, target_file);
defer parser.deinit();
2017-12-09 12:15:43 +08:00
const root_node = %return parser.parse();
defer parser.freeAst(root_node);
2017-12-09 12:15:43 +08:00
%return parser.renderAst(out_stream, root_node);
2017-12-09 12:15:43 +08:00
warn("====fmt:====\n");
%return parser.renderSource(out_stream, root_node);
2017-10-22 05:31:06 +08:00
}
2017-12-10 09:01:13 +08:00
fn testCanonical(source: []const u8) {
const allocator = std.debug.global_allocator;
std.debug.global_allocator_index = 0;
var tokenizer = Tokenizer.init(source);
var parser = Parser.init(&tokenizer, allocator, "(memory buffer)");
defer parser.deinit();
const root_node = parser.parse() %% unreachable;
defer parser.freeAst(root_node);
var buffer = std.Buffer.initSize(allocator, 0) %% unreachable;
var buffer_out_stream = io.BufferOutStream.init(&buffer);
parser.renderSource(&buffer_out_stream.stream, root_node) %% unreachable;
if (!mem.eql(u8, buffer.toSliceConst(), source)) {
warn("\n====== expected this output: =========\n");
warn("{}", source);
warn("\n======== instead found this: =========\n");
warn("{}", buffer.toSliceConst());
warn("\n======================================\n");
@panic("test failed");
}
}
test "zig fmt" {
2017-12-12 03:47:20 +08:00
if (builtin.os == builtin.Os.windows and builtin.arch == builtin.Arch.i386) {
// TODO get this test passing
// https://github.com/zig-lang/zig/issues/537
return;
}
testCanonical(
\\extern fn puts(s: &const u8) -> c_int;
\\
);
2017-12-11 11:44:04 +08:00
testCanonical(
\\const a = b;
\\pub const a = b;
\\var a = b;
\\pub var a = b;
\\const a: i32 = b;
\\pub const a: i32 = b;
\\var a: i32 = b;
\\pub var a: i32 = b;
\\
);
testCanonical(
\\extern var foo: c_int;
2017-12-11 11:44:04 +08:00
\\
);
testCanonical(
\\fn main(argc: c_int, argv: &&u8) -> c_int {
\\ const a = b;
\\}
\\
);
testCanonical(
\\fn foo(argc: c_int, argv: &&u8) -> c_int {
\\ return 0;
\\}
\\
);
}