-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialize.jai
More file actions
348 lines (285 loc) · 12.7 KB
/
serialize.jai
File metadata and controls
348 lines (285 loc) · 12.7 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
Serialization procedures will allow you to print out the AST so you can save it back to a file after modifying it internally.
Should be structure almost the exact same as the typecheck_node or execute_node procs, except that we will be appending shit to a string builder
TODO:
make it not ugly
preserve whitespace in some manner, preserve comments in file as well
probably won't preseve whitespace on a given line, but will preseve blank lines between lines with content
keeping line numbers of all statements shoudl work, but we will also want to track line number that we are currently on, so that we know we are accurate
otherwise I could see a situation where we end up overshooting where we should be in the file and things get fugged in some manner
remove extra space before `{` in loops, but keep that extra space for single-line statements
*/
SPACES_STRING :: " "; // 16 characters long
INDENT_SIZE :: 4;
append_spaces :: (builder: *String_Builder, spaces: int) {
for 1..spaces/SPACES_STRING.count append(builder, SPACES_STRING);
append(builder, slice(SPACES_STRING, 0, spaces%SPACES_STRING.count));
}
print_script :: (
using script: *Script,
builder: *String_Builder
) -> bool {
for ast_root.statements {
print_node_or_return(script, it, builder, 0);
if !should_skip_placing_semicolon(it) {
append(builder, ";");
}
// if it_index != ast_root.statements.count-1 then append(builder, "\n");
}
return true;
}
sprint_node :: (script: *Script, node: *Node) -> string {
builder: String_Builder;
print_node(script, node, *builder, 0);
return builder_to_string(*builder);
}
append_trivia :: (builder: *String_Builder, node: *Node) {
append(builder, ifx node.trivia);
}
print_node_or_return :: (
using script: *Script,
node: *Node,
builder: *String_Builder,
indent: int
) #expand {
if !print_node(script, node, builder, indent) `return false;
}
// TODO: make sure this works when node has not passed typechecking, so that we can at least still print parts of AST in error messages
print_node :: (
using script: *Script,
node: *Node,
builder: *String_Builder,
indent: int
) -> bool {
if node == null {
log("Tried to read from null pointer in print_node().");
print_stack_trace(context.stack_trace);
return false;
}
// TODO: why do we have this exception?
if node.node_type != Node_Operation {
append_trivia(builder, node);
}
// wrap expression in parenthesized if explicitly flagged as such
if node.flags & .PARENTHESIZED append(builder, "(");
defer if node.flags & .PARENTHESIZED append(builder, ")");
if node.node_type == {
case Node_Directive;
directive := node.(*Node_Directive);
print_to_builder(builder, "#%(", directive.name);
for directive.arguments {
print_node_or_return(script, it, builder, indent);
if it_index != directive.arguments.count-1 then append(builder, ",");
}
print_to_builder(builder, ")", );
return true;
case Node_Literal;
literal := node.(*Node_Literal);
if #complete literal.literal_type == {
case .ANY;
print_to_builder(builder, "<literal ANY of type %>", as_type(literal.value_type));
case .STRING;
print_to_builder(builder, "\"%\"", literal.text);
case .NUMBER;
print_to_builder(builder, "%", to_any(*literal.number));
case .BOOLEAN;
print_to_builder(builder, "%", to_any(*literal.number));
case .STRUCT;
ti_struct := literal.value_type.(*Type_Info_Struct);
append(builder, ".{");
// NOTE: we only want to serialize a struct using its current value if it is a malleable literal
if (node.flags & .TYPECHECKED) && (node.flags & .IS_MALLEABLE) {
log("serializing struct literal with value: %", Any.{ node.value_type, literal.aggr.value_pointer });
// ignoring #place members. Wish there were a better way to do this.
last_member_offset := -1;
for ti_struct.members {
if it.flags & .CONSTANT continue;
if it.offset_in_bytes < last_member_offset break;
if last_member_offset != -1 append(builder, ",");
last_member_offset = it.offset_in_bytes;
if it_index < literal.aggr.expressions.count {
append_trivia(builder, literal.aggr.expressions[it_index]);
}
print_to_builder(builder, "%", Any.{ it.type, literal.aggr.value_pointer + it.offset_in_bytes });
}
} else {
for literal.aggr.expressions {
print_node(script, it, builder, indent);
if it_index != literal.aggr.expressions.count-1 append(builder, ",");
}
}
append(builder, " }");
}
if literal.flags & .IS_MALLEABLE {
append(builder, "?");
}
return true;
case Node_Identifier;
name := get_identifier_name(script, xx node);
do_backtick := !is_legal_identifier(name);
if do_backtick append(builder, "`");
append(builder, name);
if do_backtick append(builder, "`");
return true;
case Node_Operation;
operation := node.(*Node_Operation);
_operator := get_operator(script, operation);
if _operator.kind == {
case .PREFIX;
append(builder, operation.name);
print_node(script, operation.left, builder, indent);
return true;
case .POSTFIX;
print_node(script, operation.left, builder, indent);
append(builder, operation.name);
return true;
}
// we only parenthesize expressions implicitly here if the precedence requires it
// and the subexpressions weren't already explicitly flagged as parenthesized
node_prec := _operator.precedence;
do_parenthesize_left, do_parenthesize_right := false;
if operation.left.node_type == Node_Operation {
left_prec := get_precedence(script, xx operation.left);
do_parenthesize_left = left_prec < node_prec && !(operation.left.flags & .PARENTHESIZED);
}
if operation.right.node_type == Node_Operation {
right_prec := get_precedence(script, xx operation.right);
do_parenthesize_right = right_prec < node_prec && !(operation.right.flags & .PARENTHESIZED);
}
if do_parenthesize_left append(builder, "(");
print_node(script, operation.left, builder, indent);
if do_parenthesize_left append(builder, ")");
// append(builder, " ");
append_trivia(builder, node);
append(builder, operation.name);
// append(builder, " ");
if do_parenthesize_right append(builder, "(");
print_node(script, operation.right, builder, indent);
if do_parenthesize_right append(builder, ")");
return true;
case Node_Procedure_Call;
procedure_call := node.(*Node_Procedure_Call);
print_node(script, procedure_call.procedure_expression, builder, indent);
append(builder, "(");
for procedure_call.arguments {
print_node_or_return(script, it, builder, indent);
if it_index != procedure_call.arguments.count-1 append(builder, ",");
}
append(builder, ")");
return true;
case Node_Cast;
_cast := node.(*Node_Cast);
print_node_or_return(script, _cast.value, builder, indent);
if !(_cast.flags & .IMPLICIT) {
append(builder, ".(");
print_node_or_return(script, _cast.type_expression, builder, indent);
append(builder, ")");
}
return true;
case Node_Dot;
dot := node.(*Node_Dot);
if dot.left print_node_or_return(script, dot.left, builder, indent);
if !(dot.right.node_type == Node_Literal && dot.right.(*Node_Literal).literal_type == .STRUCT) { // @Hack
append(builder, ".");
}
print_node_or_return(script, dot.right, builder, indent);
return true;
case Node_Subscript;
subscript := node.(*Node_Subscript);
print_node_or_return(script, subscript.base_expression, builder, indent);
append(builder, "[");
print_node_or_return(script, subscript.indexing_expression, builder, indent);
append(builder, "]");
return true;
case Node_Declaration;
declaration := node.(*Node_Declaration);
print_node_or_return(script, declaration.left, builder, indent);
if !declaration.type_expression append(builder, " ");
append(builder, ":");
if declaration.type_expression {
print_node_or_return(script, declaration.type_expression, builder, indent);
if declaration.init_expression append(builder, " ");
}
if declaration.init_expression {
if declaration.flags & .MACRO {
append(builder, ":");
} else {
append(builder, "=");
}
print_node_or_return(script, declaration.init_expression, builder, indent);
}
return true;
case Node_Block;
block := node.(*Node_Block);
if block.name {
print_to_builder(builder, "%: ", block.name);
}
append(builder, "{");
indent += INDENT_SIZE;
for block.statements {
// append_spaces(builder, indent);
print_node_or_return(script, it, builder, indent);
if !should_skip_placing_semicolon(it) {
append(builder, ";");
}
// if it_index != block.statements.count-1 then append(builder, "\n");
}
indent -= INDENT_SIZE;
append(builder, "\n");
append_spaces(builder, indent);
append(builder, "}\n");
return true;
case Node_If_Statement;
if_statement := node.(*Node_If_Statement);
append(builder, "if");
print_node_or_return(script, if_statement.condition, builder, indent);
// append(builder, " ");
print_node_or_return(script, if_statement.statement, builder, indent);
return true;
case Node_While_Loop;
while_loop := node.(*Node_While_Loop);
append(builder, "while");
print_node_or_return(script, while_loop.condition, builder, indent);
// append(builder, " ");
print_node_or_return(script, while_loop.statement, builder, indent);
return true;
case Node_For_Loop;
for_loop := node.(*Node_For_Loop);
if for_loop.control_type == {
case .RANGE;
append(builder, "for");
print_node_or_return(script, for_loop.range.lower, builder, indent);
append(builder, "..");
print_node_or_return(script, for_loop.range.upper, builder, indent);
case .ARRAY;
append(builder, "for");
print_node_or_return(script, for_loop.array_expression, builder, indent);
case .LIST;
append(builder, "foreach");
append(builder, "(");
for for_loop.list {
print_node_or_return(script, it, builder, indent);
if it_index != for_loop.list.count-1 then append(builder, ",");
}
append(builder, ")");
}
// append(builder, " ");
print_node_or_return(script, for_loop.statement, builder, indent);
return true;
}
assert(false, "Invalid node type '%' in print_node().", node.node_type);
return false;
}
should_skip_placing_semicolon :: (node: *Node) -> bool {
if node.node_type == {
case Node_Block;
return true;
case Node_If_Statement;
return should_skip_placing_semicolon(node.(*Node_If_Statement).statement);
case Node_While_Loop;
return should_skip_placing_semicolon(node.(*Node_While_Loop).statement);
case Node_For_Loop;
return should_skip_placing_semicolon(node.(*Node_For_Loop).statement);
}
return false;
}