authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-12-18 01:10:31+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-16 16:37:07+02:00
log7051ef32bf8e1a16cfd73f2bfa09fdbdf39ffc54
treee7749590482763aa0879da16b25d1cafed8f8c28
parent4006a3afb31f89be28721bdcd50fa64de63d6cbb
signature Commit is signed but in an unrecognized format.

translate-c: start creating intermediate AST


1 files changed, 207 insertions(+), 0 deletions(-)

src/translate_c/ast.zig created+207
......@@ -0,0 +1,207 @@
1const std = @import("std");
2const Type = @import("../type.zig").Type;
3
4pub const Node = struct {
5 tag: Tag,
6 // type: Type = Type.initTag(.noreturn),
7
8 pub const Tag = enum {
9 null_literal,
10 undefined_literal,
11 opaque_literal,
12 bool_literal,
13 int,
14 float,
15 string,
16 char,
17 identifier,
18 @"if",
19 @"while",
20 @"switch",
21 @"continue",
22 @"break",
23 @"return",
24 field_access,
25 field_access_arrow,
26 array_access,
27 call,
28 std_mem_zeroes,
29 var_decl,
30 func,
31 warning,
32 failed_decl,
33 @"enum",
34 @"struct",
35 @"union",
36 array_init,
37 container_init,
38 std_meta_cast,
39 discard,
40 block,
41
42 pub fn Type(tag: Tag) ?type {
43 return switch (tag) {
44 .null_literal => null,
45 .undefined_literal => null,
46 .opaque_literal => null,
47 .bool_literal,
48 .int,
49 .float,
50 .string,
51 .char,
52 .identifier,
53 .field_access,
54 .field_access_arrow,
55 .warning,
56 .failed_decl,
57 => Value,
58 .@"if" => If,
59 .@"while" => While,
60 .@"switch" => Switch,
61 .@"break" => Break,
62 .call => Call,
63 .array_access,
64 .std_mem_zeroes,
65 .@"return",
66 .discard,
67 => SingleArg,
68 .var_decl => VarDecl,
69 .func => Func,
70 .@"enum" => Enum,
71 .@"struct", .@"union" => Record,
72 .array_init => ArrayInit,
73 .container_init => ContainerInit,
74 .std_meta_cast => Infix,
75 .block => Block,
76 };
77 }
78 };
79
80 pub const Infix = struct {
81 base: Node,
82 lhs: *Node,
83 rhs: *Node,
84 };
85
86 pub const Value = struct {
87 base: Node,
88 val: []const u8,
89 };
90
91 pub const SingleArg = struct {
92 base: Node,
93 index: *Node,
94 };
95
96 pub const If = struct {
97 base: Node = .{ .tag = .@"if" },
98 cond: *Node,
99 then: *Node,
100 @"else": ?*Node,
101 };
102
103 pub const While = struct {
104 base: Node = .{ .tag = .@"while" },
105 cond: *Node,
106 body: *Node,
107 };
108
109 pub const Switch = struct {
110 base: Node = .{ .tag = .@"switch" },
111 cond: *Node,
112 cases: []Prong,
113 default: ?[]const u8,
114
115 pub const Prong = struct {
116 lhs: *Node,
117 rhs: ?*Node,
118 label: []const u8,
119 };
120 };
121
122 pub const Break = struct {
123 base: Node = .{ .tag = .@"break" },
124 label: ?[]const u8,
125 rhs: ?*Node,
126 };
127
128 pub const Call = struct {
129 base: Node = .{.call},
130 lhs: *Node,
131 args: []*Node,
132 };
133
134 pub const VarDecl = struct {
135 base: Node = .{ .tag = .var_decl },
136 @"pub": bool,
137 @"const": bool,
138 @"extern": bool,
139 @"export": bool,
140 name: []const u8,
141 type: Type,
142 init: *Node,
143 };
144
145 pub const Func = struct {
146 base: Node = .{.func},
147 @"pub": bool,
148 @"extern": bool,
149 @"export": bool,
150 name: []const u8,
151 cc: std.builtin.CallingConvention,
152 params: []Param,
153 return_type: Type,
154 body: ?*Node,
155
156 pub const Param = struct {
157 @"noalias": bool,
158 name: ?[]const u8,
159 type: Type,
160 };
161 };
162
163 pub const Enum = struct {
164 base: Node = .{ .tag = .@"enum" },
165 name: ?[]const u8,
166 fields: []Field,
167
168 pub const Field = struct {
169 name: []const u8,
170 value: ?[]const u8,
171 };
172 };
173
174 pub const Record = struct {
175 base: Node,
176 name: ?[]const u8,
177 @"packed": bool,
178 fields: []Field,
179
180 pub const Field = struct {
181 name: []const u8,
182 type: Type,
183 alignment: c_uint,
184 };
185 };
186
187 pub const ArrayInit = struct {
188 base: Node = .{ .tag = .array_init },
189 values: []*Node,
190 };
191
192 pub const ContainerInit = struct {
193 base: Node = .{ .tag = .container_init },
194 values: []Initializer,
195
196 pub const Initializer = struct {
197 name: []const u8,
198 value: *Node,
199 };
200 };
201
202 pub const Block = struct {
203 base: Node = .{ .tag = .block },
204 label: ?[]const u8,
205 stmts: []*Node,
206 };
207};