-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser_expr.zig
More file actions
265 lines (248 loc) · 9.37 KB
/
parser_expr.zig
File metadata and controls
265 lines (248 loc) · 9.37 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
const std = @import("std");
const Token = @import("clocktree_context.zig").Token;
const ClockTreeContext = @import("clocktree_context.zig");
const Defaults = @import("defaults.zig");
const TreeContext = ClockTreeContext.TreeContext;
const PreprocessedRef = ClockTreeContext.PreProcessedRef;
const PreProcessedNode = ClockTreeContext.PreProcessedNode;
const DefaultToTrue = Defaults.DefaultToTrue;
const DefaultToFalse = Defaults.DefaultToFalse;
const logger = std.log.scoped(.parser);
pub fn parser(
writer: *std.io.Writer,
tokens: []const Token,
tree: *const TreeContext,
recursive_expr: ?*const std.StringArrayHashMap(void),
) !void {
for (tokens) |token| {
switch (token) {
.open_paren => try writer.writeAll(" ( "),
.close_paren => try writer.writeAll(" ) "),
.and_op => try writer.writeAll(" and "),
.or_op => try writer.writeAll(" or "),
.not_op => try writer.writeAll("!"),
.scalar => |name| {
if (name.len == 0) {
try writer.writeAll("false");
} else if (tree.base.semaphores.contains(name)) {
try writer.print(
\\ @"{s}"
, .{name});
} else if (tree.base.extra_flags.contains(name)) {
try writer.print(
\\ config.flags.@"{s}"
, .{name});
} else if (DefaultToTrue.has(name)) {
try writer.writeAll("true");
} else if (DefaultToFalse.has(name)) {
try writer.writeAll("false");
} else if (is_numeric(name)) {
try writer.print(
\\ {s}
, .{name});
} else {
try writer.print(
\\ check_MCU("{s}")
, .{name});
}
},
.eq, .bt, .lt => |op| {
const op_char: u8 = switch (token) {
.eq => '=',
.bt => '>',
.lt => '<',
else => unreachable,
};
const op_str: [:0]const u8 = switch (token) {
.eq => "==",
.bt => ">",
.lt => "<",
else => unreachable,
};
if (tree.pre.ref_process_map.get(op.op1)) |ref| {
const recursive = if (recursive_expr) |d| d.contains(op.op1) else false;
try write_ref_logic_op(writer, ref, tree, op_char, op.op2, recursive);
} else if (tree.pre.node_process_map.get(op.op1)) |node| {
logger.debug("Unsupported Logic: Node:{s} {s} {s}", .{ node.name, op_str, op.op2 });
} else if (tree.base.extra_flags.contains(op.op1)) {
if (std.mem.eql(u8, op.op2, "true") or std.mem.eql(u8, op.op2, "false")) {
try writer.print(
\\config.flags.@"{s}" {s} {s}
, .{ op.op1, op_str, op.op2 });
} else {
try writer.print(
\\config.flags.@"{s}"
, .{
op.op1,
});
}
} else if (DefaultToTrue.has(op.op1)) {
try writer.writeAll("true");
} else {
logger.debug("Unknown Op: {s} {s} {s}, defaulting to false", .{ op.op1, op_str, op.op2 });
try writer.writeAll("false");
}
},
.ref_scalar => |name| {
const ref = tree.pre.ref_process_map.get(name) orelse {
logger.debug("Unknown scalar ref: {s}", .{name});
return;
};
if (ref.is_flag()) {
switch (ref.type_helper) {
.list_flag => try write_ref_logic_op(writer, ref, tree, '=', "true", false),
.numeric_flag => {
try write_ref_logic_op(writer, ref, tree, '=', "1", false);
},
.tag_flag => |item| {
if (item.keys().len == 1) {
// if the list contains only one item, there can't be a false comparison
try writer.writeAll("true");
} else {
logger.debug("MULTI ITEM TAG-FLAG on TREE {s}-{s}", .{ tree.name, name });
}
},
else => unreachable,
}
} else {
if (!ref.is_nullable) {
switch (ref.type_helper) {
.list, .list_flag, .tag_flag => try writer.print("@\"{s}\".get()", .{ref.name}),
.float => try writer.print("@\"{s}Value\"", .{ref.name}),
.integer => try writer.print("@floatFromInt(@\"{s}Value\")", .{ref.name}),
else => {
logger.debug("ref {s} has a invalid type {}", .{ ref.name, ref.type_helper });
while (true) {}
},
}
} else {
logger.debug("ref {s} is nullable, skipping", .{ref.name});
}
}
},
.add, .div, .mul, .sub => |op| {
const op_char: u8 = switch (token) {
.add => '+',
.sub => '-',
.div => '/',
.mul => '*',
else => unreachable,
};
if (tree.pre.ref_process_map.get(op.op1)) |ref| {
try write_ref_math_op(writer, ref, op_char, op.op2);
} else if (is_numeric(op.op1)) {
try writer.print(
\\try math_op(?f32, {s}, {s}, .@"{c}", "")
, .{ op.op1, op.op2, op_char });
}
},
.expr_scalar => |to_write| {
try writer.writeAll(to_write);
},
else => {
logger.debug("Unknown token: {any}", .{token});
},
}
}
}
fn write_ref_logic_op(
writer: *std.io.Writer,
ref: *const PreprocessedRef,
tree: *const TreeContext,
op: u8,
val2: []const u8,
recursive: bool,
) !void {
//if the ref is list based, we need to check if the value is in the list before doing any other checks
if (ref.is_list_based_ref()) {
if (!ref.check_list_item(val2)) {
try writer.writeAll("false");
return;
}
}
const prefix = blk: {
if (recursive) {
if (ref.is_base_config()) {
break :blk "config.base.";
} else if (ref.is_extra_config()) {
break :blk "config.extra_config.";
}
}
break :blk "";
};
const postfix = if (prefix.len == 0) "Value" else "";
if (ref.is_list_based_ref()) {
const all = try ref.get_list_items();
if (all.contains(val2)) {
try writer.print(
\\check_ref(@TypeOf({0s}@"{1s}{2s}"), {0s}@"{1s}{2s}", .@"{3s}", .@"{4c}")
, .{
prefix,
ref.name,
postfix,
val2,
op,
});
} else {
logger.debug("Reference {s} is a list but {s} is not a item, default to false", .{ ref.name, val2 });
try writer.writeAll("false");
}
} else {
if (is_numeric(val2)) {
try writer.print(
\\check_ref(@TypeOf({0s}@"{1s}{2s}"), {0s}@"{1s}{2s}", {3s}, .@"{4c}")
, .{
prefix,
ref.name,
postfix,
val2,
op,
});
} else if (tree.pre.ref_process_map.contains(val2)) {
try writer.print(
\\check_ref(@TypeOf({0s}@"{1s}{2s}"), {0s}@"{1s}{2s}", @"{3s}Value", .@"{4c}")
, .{
prefix,
ref.name,
postfix,
val2,
op,
});
} else {
logger.debug("Invalid Expr between Reference {s} and Val {s}", .{ ref.name, val2 });
try writer.writeAll("false");
}
}
}
fn write_ref_math_op(
writer: *std.io.Writer,
ref: *const PreprocessedRef,
op: u8,
val2: []const u8,
) !void {
const nullable_str = if (ref.is_nullable) "" else "?";
try writer.print(
\\try math_op({s}@TypeOf(@"{s}Value"),
, .{ nullable_str, ref.name });
switch (ref.type_helper) {
.list => try writer.print(
\\@"{s}Value".get(),
, .{ref.name}),
.integer, .float => try writer.print(
\\@"{s}Value",
, .{ref.name}),
else => unreachable,
}
try writer.print(
\\{s}, .@"{c}", "{s}")
, .{ val2, op, ref.name });
}
fn is_numeric(str: []const u8) bool {
for (str) |c| {
switch (c) {
'0'...'9', '.' => {},
else => return false,
}
}
return true;
}