Skip to content
← Back

src/syntax/process/printer/ghul.ghul

1
namespace Syntax.Process.Printer is
2
use IO.Std;
3
4
use Trees;
5
6
class GHUL: Base is
7
init() is
8
super.init(false);
9
si
10
11
visit(variable: Variables.VARIABLE) is
12
variable.left.accept(self);
13
if !isa TypeExpressions.INFER(variable.type_expression) then
14
write(": ");
15
variable.type_expression.accept(self);
16
fi
17
let initializer = variable.initializer;
18
if initializer? then
19
write(" = ");
20
initializer.accept(self);
21
fi
22
si
23
24
visit(destructure_element: Variables.SIMPLE_VARIABLE_LEFT) is
25
destructure_element.name.accept(self);
26
si
27
28
visit(literal_leaf: Variables.LITERAL_VARIABLE_LEFT) is
29
literal_leaf.expression.accept(self);
30
si
31
32
visit(destructure_element_list: Variables.DESTRUCTURING_VARIABLE_LEFT) is
33
write("(");
34
35
let seen_any mut = false;
36
for e in destructure_element_list.elements do
37
if seen_any then
38
write(", ");
39
fi
40
41
e.accept(self);
42
43
seen_any = true;
44
od
45
46
write(")");
47
si
48
49
visit(`namespace: Definitions.NAMESPACE) is
50
write("namespace ");
51
`namespace.name.accept(self);
52
write_line(" is");
53
indent();
54
`namespace.body.accept(self);
55
outdent();
56
write_line("si");
57
si
58
59
visit(`use: Definitions.USE) is
60
write("use ");
61
let seen_any = false;
62
if `use.name? then
63
`use.name.accept(self);
64
write(" = ");
65
fi
66
67
if `use.`use? then
68
`use.`use.accept(self);
69
fi
70
71
write_line(";");
72
si
73
74
visit(`class: Definitions.CLASS) is
75
write("class ");
76
`class.name.accept(self);
77
if `class.arguments? then
78
write("[");
79
`class.arguments!.accept(self);
80
write("]");
81
fi
82
if `class.ancestors? then
83
write(": ");
84
`class.ancestors!.accept(self);
85
fi
86
`class.modifiers.accept(self);
87
write_line(" is");
88
indent();
89
`class.body.accept(self);
90
outdent();
91
write_line("si");
92
si
93
94
visit(`partial: Definitions.PARTIAL) is
95
write("partial ");
96
`partial.name.accept(self);
97
if `partial.arguments? then
98
write("[");
99
`partial.arguments!.accept(self);
100
write("]");
101
fi
102
`partial.modifiers.accept(self);
103
write_line(" is");
104
indent();
105
`partial.body.accept(self);
106
outdent();
107
write_line("si");
108
si
109
110
visit(`impl: Definitions.IMPL) is
111
write("impl ");
112
if `impl.ancestors? then
113
`impl.ancestors.accept(self);
114
fi
115
write(" for ");
116
`impl.name.accept(self);
117
if `impl.arguments? then
118
write("[");
119
`impl.arguments!.accept(self);
120
write("]");
121
fi
122
`impl.modifiers.accept(self);
123
write_line(" is");
124
indent();
125
`impl.body.accept(self);
126
outdent();
127
write_line("si");
128
si
129
130
visit(`trait: Definitions.TRAIT) is
131
write("trait ");
132
`trait.name.accept(self);
133
if `trait.arguments? then
134
write("[");
135
`trait.arguments!.accept(self);
136
write("]");
137
fi
138
if `trait.ancestors? then
139
write(": ");
140
`trait.ancestors!.accept(self);
141
fi
142
`trait.modifiers.accept(self);
143
write_line(" is");
144
indent();
145
`trait.body.accept(self);
146
outdent();
147
write_line("si");
148
si
149
150
visit(`struct: Definitions.STRUCT) is
151
write("trait ");
152
`struct.name.accept(self);
153
154
if `struct.arguments? then
155
write("[");
156
`struct.arguments!.accept(self);
157
write("]");
158
fi
159
160
`struct.modifiers.accept(self);
161
write_line(" is");
162
indent();
163
`struct.body.accept(self);
164
outdent();
165
write_line("si");
166
si
167
168
visit(`union: Definitions.UNION) is
169
write("union ");
170
`union.name.accept(self);
171
if `union.arguments? then
172
write("[");
173
`union.arguments!.accept(self);
174
write("]");
175
fi
176
`union.modifiers.accept(self);
177
write_line(" is");
178
indent();
179
`union.body.accept(self);
180
outdent();
181
write_line("si");
182
si
183
184
visit(variant: Definitions.VARIANT) is
185
variant.name.accept(self);
186
187
if variant.fields.count > 0 then
188
write("(");
189
variant.fields.accept(self);
190
write(")");
191
fi
192
193
variant.modifiers.accept(self);
194
195
write_line(" is");
196
indent();
197
variant.body.accept(self);
198
outdent();
199
write_line("si");
200
si
201
202
after_body(node: Bodies.Body?) is
203
if node==null \/ !node.is_block then
204
write(";");
205
fi
206
write_line();
207
si
208
209
visit(function: Definitions.FUNCTION) is
210
if let function.name? then
211
name.accept(self);
212
fi
213
write("(");
214
215
function.arguments.accept(self);
216
write(")");
217
218
if !isa TypeExpressions.INFER(function.type_expression) then
219
write(" -> ");
220
function.type_expression.accept(self);
221
function.modifiers.accept(self);
222
elif !function.modifiers.is_empty then
223
function.modifiers.accept(self);
224
fi
225
226
let body = function.body;
227
228
if body? then
229
write(" ");
230
body.accept(self);
231
fi
232
233
after_body(body);
234
si
235
236
write_member_type_and_modifiers(type_expression: TypeExpressions.TypeExpression, modifiers: Modifiers.LIST) is
237
if !isa TypeExpressions.INFER(type_expression) then
238
write(": ");
239
type_expression.accept(self);
240
write(" ");
241
modifiers.accept(self);
242
elif !modifiers.is_empty then
243
modifiers.accept(self);
244
fi
245
si
246
247
indent_property(has_getter: bool, has_setter: bool) -> bool is
248
if has_getter /\ has_setter then
249
write_line();
250
indent();
251
return true;
252
elif has_getter \/ has_setter then
253
write(" ");
254
fi
255
return false;
256
si
257
258
visit(property: Definitions.PROPERTY) is
259
if property.name? then
260
property.name.accept(self);
261
fi
262
263
write_member_type_and_modifiers(property.type_expression, property.modifiers);
264
let out_again = indent_property(property.read_body?, property.assign_body?);
265
if property.read_body? then
266
property.read_body.accept(self);
267
if property.assign_body? then
268
write_line(",");
269
else
270
after_body(property.read_body!);
271
fi
272
else
273
write(" ");
274
fi
275
if property.assign_body? then
276
write("= ");
277
property.assign_argument!.accept(self);
278
property.assign_body!.accept(self);
279
after_body(property.assign_body!);
280
fi
281
if out_again then
282
outdent();
283
fi
284
si
285
286
visit(indexer: Definitions.INDEXER) is
287
if indexer.name? then
288
indexer.name.accept(self);
289
fi
290
write("[");
291
indexer.index_argument.accept(self);
292
write("]");
293
write_member_type_and_modifiers(indexer.type_expression, indexer.modifiers);
294
let out_again = indent_property(indexer.read_body?, indexer.assign_body?);
295
if indexer.read_body? then
296
indexer.read_body.accept(self);
297
if indexer.assign_body? then
298
write_line(",");
299
else
300
after_body(indexer.read_body!);
301
fi
302
else
303
write(' ');
304
fi
305
if indexer.assign_body? then
306
write("= ");
307
indexer.assign_argument!.accept(self);
308
write(' ');
309
indexer.assign_body!.accept(self);
310
after_body(indexer.assign_body!);
311
fi
312
if out_again then
313
outdent();
314
fi
315
si
316
317
visit(generic: TypeExpressions.GENERIC) is
318
generic.name.accept(self);
319
write('[');
320
generic.arguments.accept(self);
321
write(']');
322
si
323
324
visit(function: TypeExpressions.FUNCTION) is
325
write("(");
326
function.arguments.accept(self);
327
write(")");
328
if !isa TypeExpressions.INFER(function.result) then
329
write(" -> ");
330
function.result.accept(self);
331
fi
332
if function.is_pure then
333
write(" pure");
334
fi
335
si
336
337
visit(tuple: TypeExpressions.TUPLE) is
338
write("(");
339
tuple.elements.accept(self);
340
write(")");
341
si
342
343
visit(element: TypeExpressions.NAMED_TUPLE_ELEMENT) is
344
element.name.accept(self);
345
write(": ");
346
element.type_expression.accept(self);
347
si
348
349
visit(constraint: TypeExpressions.TYPE_PARAMETER_CONSTRAINT) is
350
write(constraint.keyword);
351
si
352
353
visit(`null: Expressions.NULL) is
354
write("null");
355
si
356
357
visit(`self: Expressions.SELF) is
358
write("self");
359
si
360
361
visit(variable: Expressions.VARIABLE) is
362
variable.name.accept(self);
363
364
if !isa TypeExpressions.INFER(variable.type_expression) then
365
write(": ");
366
variable.type_expression.accept(self);
367
fi
368
369
if variable.initializer? then
370
write(" = ");
371
variable.initializer!.accept(self);
372
fi
373
si
374
375
visit(element: Expressions.TUPLE_ELEMENT) is
376
element.name.accept(self);
377
378
if !isa TypeExpressions.INFER(element.type_expression) then
379
write(": ");
380
element.type_expression.accept(self);
381
fi
382
383
if element.initializer? then
384
write(" = ");
385
element.initializer!.accept(self);
386
fi
387
si
388
389
visit(ambiguous_expression: Expressions.AMBIGUOUS_EXPRESSION) is
390
write("(ambiguous ");
391
392
if let ambiguous_expression.left? then
393
left.accept(self);
394
write(".");
395
fi
396
ambiguous_expression.identifier.accept(self);
397
write("[");
398
ambiguous_expression.type_arguments.accept(self);
399
write("] or ");
400
ambiguous_expression.index.accept(self);
401
write(")");
402
si
403
404
visit(generic_application: Expressions.GENERIC_APPLICATION) is
405
if let generic_application.left? then
406
left.accept(self);
407
write(".");
408
fi
409
generic_application.identifier.accept(self);
410
write("[");
411
generic_application.type_arguments.accept(self);
412
write("]");
413
si
414
415
visit(function: Syntax.Trees.Expressions.FUNCTION) is
416
function.arguments.accept(self);
417
if !isa TypeExpressions.INFER(function.type_expression) then
418
write(" -> ");
419
function.type_expression.accept(self);
420
fi
421
function.body.accept(self);
422
si
423
424
visit(sequence: Expressions.SEQUENCE) is
425
write('[');
426
sequence.elements.accept(self);
427
write(']');
428
if !isa Trees.TypeExpressions.INFER(sequence.type_expression) then
429
write(": ");
430
sequence.type_expression.accept(self);
431
fi
432
si
433
434
visit(unwrap: Expressions.HAS_VALUE) is
435
unwrap.left.accept(self);
436
write("?");
437
si
438
439
visit(has_value: Expressions.UNWRAP) is
440
has_value.left.accept(self);
441
write("!");
442
si
443
444
visit(has_value: Expressions.REFERENCE) is
445
has_value.left.accept(self);
446
write(" ref ");
447
si
448
449
visit(statement: Expressions.STATEMENT) is
450
statement.statement.accept(self);
451
write(";");
452
si
453
454
visit(block: Expressions.VAL_BLOCK) is
455
write("val ");
456
block.body.accept(self);
457
write("lav");
458
si
459
460
visit(l: Statements.LET) is
461
write("let ");
462
l.variables.accept(self);
463
write_line(";");
464
si
465
466
visit(rb: Statements.REFUTABLE_BINDING) is
467
let first mut = true;
468
469
for c in rb.clauses do
470
if !first then
471
write(", ");
472
fi
473
474
if c.is_inferred_name then
475
// Leaf-name shorthand: `path?` / `path: T`.
476
c.scrutinee.accept(self);
477
478
if let c.narrow_type_expression? then
479
write(": ");
480
narrow_type_expression.accept(self);
481
else
482
write("?");
483
fi
484
else
485
c.pattern.accept(self);
486
487
if let c.narrow_type_expression? then
488
write(": ");
489
narrow_type_expression.accept(self);
490
fi
491
492
write(" = ");
493
c.scrutinee.accept(self);
494
fi
495
496
if let c.guard? then
497
write(" /\\ ");
498
guard.accept(self);
499
fi
500
501
first = false;
502
od
503
si
504
505
visit(`for: Statements.FOR) is
506
write_line("for ");
507
508
let variable = `for.variable;
509
if variable? then
510
variable.accept(self);
511
fi
512
513
write(" in ");
514
515
let expression = `for.expression;
516
if expression? then
517
expression.accept(self);
518
fi
519
520
write_line(" do");
521
indent();
522
523
let body = `for.body;
524
if body? then
525
body.accept(self);
526
fi
527
528
outdent();
529
write_line("od");
530
si
531
532
visit(expression: Bodies.EXPRESSION) is
533
write("=> ");
534
expression.expression.accept(self);
535
si
536
537
visit(block: Bodies.BLOCK) is
538
write_line("is");
539
indent();
540
block.statements.accept(self);
541
outdent();
542
write("si");
543
si
544
545
visit(`innate: Bodies.INNATE) is
546
`innate.name.accept(self);
547
si
548
si
549
si