authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-01-04 14:34:00+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-01-05 20:25:50+02:00
loge1b01d32f0869bae9b770a613f3f02fbda6c6556
tree5a5565c8235c5960c9ec82b3eb3546c3f15544d1
parent2183c4bb444d80921783fc5a26d217c0e4a68d31
signature Commit is signed but in an unrecognized format.

std-c ast base


3 files changed, 74 insertions(+), 0 deletions(-)

lib/std/c.zig+5
...@@ -2,6 +2,11 @@ const builtin = @import("builtin");...@@ -2,6 +2,11 @@ const builtin = @import("builtin");
2const std = @import("std");2const std = @import("std");
3const page_size = std.mem.page_size;3const page_size = std.mem.page_size;
44
5const tokenizer = @import("c/tokenizer.zig");
6pub const Token = tokenizer.Token;
7pub const Tokenizer = tokenizer.Tokenizer;
8pub const ast = @import("c/ast.zig");
9
5pub usingnamespace @import("os/bits.zig");10pub usingnamespace @import("os/bits.zig");
611
7pub usingnamespace switch (builtin.os) {12pub usingnamespace switch (builtin.os) {
lib/std/c/ast.zig created+66
...@@ -0,0 +1,66 @@
1const std = @import("std.zig");
2const SegmentedList = std.SegmentedList;
3const Token = std.c.Token;
4const Source = std.c.tokenizer.Source;
5
6pub const TokenIndex = usize;
7
8pub const Tree = struct {
9 tokens: TokenList,
10 sources: SourceList,
11 root_node: *Node.Root,
12 arena_allocator: std.heap.ArenaAllocator,
13 errors: ErrorList,
14
15 pub const SourceList = SegmentedList(Source, 4);
16 pub const TokenList = Source.TokenList;
17 pub const ErrorList = SegmentedList(Error, 0);
18
19 pub fn deinit(self: *Tree) void {
20 // Here we copy the arena allocator into stack memory, because
21 // otherwise it would destroy itself while it was still working.
22 var arena_allocator = self.arena_allocator;
23 arena_allocator.deinit();
24 // self is destroyed
25 }
26};
27
28pub const Error = union(enum) {
29 InvalidToken: InvalidToken,
30
31 pub fn render(self: *const Error, tokens: *Tree.TokenList, stream: var) !void {
32 switch (self.*) {
33 .InvalidToken => |*x| return x.render(tokens, stream),
34 }
35 }
36
37 pub fn loc(self: *const Error) TokenIndex {
38 switch (self.*) {
39 .InvalidToken => |x| return x.token,
40 }
41 }
42
43 pub const InvalidToken = SingleTokenError("Invalid token '{}'");
44
45 fn SingleTokenError(comptime msg: []const u8) type {
46 return struct {
47 token: TokenIndex,
48
49 pub fn render(self: *const @This(), tokens: *Tree.TokenList, stream: var) !void {
50 const actual_token = tokens.at(self.token);
51 return stream.print(msg, .{actual_token.id.symbol()});
52 }
53 };
54 }
55};
56
57pub const Root = struct {
58 decls: DeclList,
59 eof_token: TokenIndex,
60
61 pub const DeclList = SegmentedList(*Decl, 4);
62};
63
64pub const Decl = struct {
65
66};
\ No newline at end of file
lib/std/c/tokenizer.zig+3
...@@ -4,6 +4,9 @@ const mem = std.mem;...@@ -4,6 +4,9 @@ const mem = std.mem;
4pub const Source = struct {4pub const Source = struct {
5 buffer: []const u8,5 buffer: []const u8,
6 file_name: []const u8,6 file_name: []const u8,
7 tokens: TokenList,
8
9 pub const TokenList = SegmentedList(Token, 64);
7};10};
811
9pub const Token = struct {12pub const Token = struct {