This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.zig
119 lines (99 loc) · 3.33 KB
/
parser.zig
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const std = @import("std");
const warn = std.debug.warn;
const ZigParser = @import("zig/zig_parser.zig").Parser;
const ZigNode = @import("zig/zig_parser.zig").Node;
const ZigToken = @import("zig/zig_grammar.tokens.zig").Token;
const ZigIr = struct {
const Instruction = struct {
id: Id,
pub const Id = enum {
Import,
Scope,
Name,
Assign,
Jump,
Branch,
};
pub fn cast(base: *Node, comptime T: type) ?*T {
if (base.id == comptime typeToId(T)) {
return @fieldParentPtr(T, "base", base);
}
return null;
}
pub fn unsafe_cast(base: *Node, comptime T: type) *T {
return @fieldParentPtr(T, "base", base);
}
pub fn iterate(base: *Node, index: usize) ?*Node {
comptime var i = 0;
inline while (i < @memberCount(Id)) : (i += 1) {
if (base.id == @field(Id, @memberName(Id, i))) {
const T = @field(Node, @memberName(Id, i));
return @fieldParentPtr(T, "base", base).iterate(index);
}
}
unreachable;
}
pub fn typeToId(comptime T: type) Id {
comptime var i = 0;
inline while (i < @memberCount(Id)) : (i += 1) {
if (T == @field(Node, @memberName(Id, i))) {
return @field(Id, @memberName(Id, i));
}
}
unreachable;
}
};
const Scope = struct {
allocator: *std.mem.Allocator,
parent: ?*Scope = null,
label: ?*ZigToken = null,
entry: *Block,
exit: *Block,
pub fn init(allocator: *std.mem.Allocator) !Scope {
const entry = try allocator.create(Block);
entry.* = Block.init(allocator);
return Scope{ .allocator = allocator, .entry = entry, .exit = entry };
}
};
const Block = struct {
instructions: std.ArrayList(*Instruction),
pub fn init(allocator: *std.mem.Allocator) Block {
return Block{ .instructions = std.ArrayList(*Instruction).init(allocator) };
}
};
};
usingnamespace @import("errors.zig");
pub fn main() !void {
var allocator = std.heap.c_allocator;
var args = std.process.args();
if(!args.skip()) return;
const filename = if(args.next(allocator)) |arg1| try arg1 else "simple.zig"[0..];
var file = try std.fs.File.openRead(filename);
defer file.close();
const file_size = try file.getEndPos();
const buffer = std.os.mmap(
null,
file_size,
std.os.PROT_READ,
std.os.MAP_SHARED,
file.handle,
0,
) catch return error.OutOfMemory;
defer std.os.munmap(buffer);
var parser = try ZigParser.init(std.heap.c_allocator);
defer parser.deinit();
const ms_begin = std.time.milliTimestamp();
_ = try parser.run(buffer[0..file_size]);
const ms_end = std.time.milliTimestamp();
var eit = parser.engine.errors.iterator(0);
while(eit.next()) |err| {
warn("{}:{}-{} {}\n", err.line, err.start, err.end, parseErrorToString(err.info));
}
else {
warn("No errors\n");
}
warn("Duration: {}ms\n", ms_end-ms_begin);
if(parser.root) |root| {
root.base.dump(0);
}
}