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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
package org.shedlang.compiler.patio;
import org.shedlang.compiler.patio.instructions.*;
import org.shedlang.compiler.patio.operands.*;
import org.shedlang.compiler.patio.types.*;
import org.shedlang.compiler.patio.values.*;
import org.shedlang.compiler.treetext.Tree;
import java.util.List;
import java.util.Optional;
import static org.shedlang.compiler.treetext.TreeSerializer.serializeTree;
import static org.shedlang.compiler.treetext.Trees.*;
public class PatioSerializer {
private PatioSerializer() {}
public static void serializeCompilationUnit(
PatioCompilationUnit compilationUnit,
StringBuilder builder
) {
var tree = compilationUnitToTree(compilationUnit);
serializeTree(tree, builder, 0);
}
public static void serializeCompilationUnitElement(
PatioCompilationUnitElement element,
StringBuilder builder
) {
var tree = compilationUnitElementToTree(element);
serializeTree(tree, builder, 0);
}
public static void serializeDataLayout(
PatioDataLayout layout,
StringBuilder builder
) {
var tree = dataLayoutToTree(layout);
serializeTree(tree, builder, 0);
}
public static void serializeDataPtrValue(
PatioDataPtrValue value,
StringBuilder builder
) {
var tree = dataPtrValueToTree(value);
serializeTree(tree, builder, 0);
}
public static void serializeDataValue(
PatioDataValue value,
StringBuilder builder
) {
var tree = dataValueToTree(value);
serializeTree(tree, builder, 0);
}
public static void serializeInstructions(List<PatioInstruction> instructions, StringBuilder builder) {
var tree = instructionsToTree(instructions);
serializeTree(tree, builder, 0);
}
public static void serializeInstruction(PatioInstruction instruction, StringBuilder builder) {
var tree = instructionToTree(instruction);
serializeTree(tree, builder, 0);
}
public static void serializeIntValue(PatioIntValue value, StringBuilder builder) {
var tree = intValueToTree(value);
serializeTree(tree, builder, 0);
}
public static void serializeLocal(PatioLocal local, StringBuilder builder) {
var tree = localToTree(local);
serializeTree(tree, builder, 0);
}
public static void serializeOperand(PatioOperand operand, StringBuilder builder) {
var tree = operandToTree(operand);
serializeTree(tree, builder, 0);
}
public static void serializeType(PatioType type, StringBuilder builder) {
var tree = typeToTree(type);
serializeTree(tree, builder, 0);
}
private static Tree compilationUnitToTree(PatioCompilationUnit compilationUnit) {
return treeListIndented(
compilationUnit.elements().stream()
.map(element -> compilationUnitElementToTree(element))
.toList()
);
}
private static Tree compilationUnitElementToTree(PatioCompilationUnitElement element) {
return switch (element) {
case PatioDataElementImmutableDefinition dataElementImmutableDefinition ->
treeList(
treeIdentifier("data-immutable"),
treeString(dataElementImmutableDefinition.name()),
visibilityToTree(dataElementImmutableDefinition.visibility()),
dataValueToTree(dataElementImmutableDefinition.value())
);
case PatioDataElementMutableDeclaration dataElementMutableDeclaration ->
treeList(
treeIdentifier("data-mutable-decl"),
treeString(dataElementMutableDeclaration.name()),
dataLayoutToTree(dataElementMutableDeclaration.layout())
);
case PatioDataElementMutableDefinition dataElementMutableDefinition ->
treeList(
treeIdentifier("data-mutable"),
treeString(dataElementMutableDefinition.name()),
visibilityToTree(dataElementMutableDefinition.visibility()),
dataValueToTree(dataElementMutableDefinition.initialValue())
);
case PatioExceptionTypeDefinition exceptionTypeDefinition ->
treeList(
treeIdentifier("exception-type"),
treeString(exceptionTypeDefinition.name()),
typeToTree(exceptionTypeDefinition.valueType())
);
case PatioFunctionDeclaration functionDeclaration ->
treeListIndented(
treeIdentifier("function-decl"),
treeString(element.name()),
treeNamed("params", treeList(
functionDeclaration.params().stream()
.map(param -> localToTree(param))
.toList()
)),
treeNamed("return-types", treeList(
functionDeclaration.returnTypes().stream()
.map(returnType -> typeToTree(returnType))
.toList()
))
);
case PatioFunctionDefinition functionDefinition ->
treeListIndented(
treeIdentifier("function"),
treeString(element.name()),
treeNamed("visibility", visibilityToTree(functionDefinition.visibility())),
treeNamed("locals", treeList(
functionDefinition.locals().stream()
.map(local -> localToTree(local))
.toList()
)),
treeNamed("return-types", treeList(
functionDefinition.returnTypes().stream()
.map(returnType -> typeToTree(returnType))
.toList()
)),
treeNamed("body", instructionsToTree(functionDefinition.body()))
);
};
}
private static Tree instructionsToTree(List<PatioInstruction> instructions) {
return treeListIndented(
instructions.stream()
.map(instruction -> instructionToTree(instruction))
.toList()
);
}
private static Tree instructionToTree(PatioInstruction instruction) {
return switch (instruction) {
case PatioBlock block ->
blockToTree(block);
case PatioBoolToInt boolToInt ->
treeList(
treeIdentifier("bool-to-int"),
operandToTree(boolToInt.destination()),
operandToTree(boolToInt.value())
);
case PatioCall call ->
treeList(
treeIdentifier("call"),
operandsToTree(call.destinations()),
treeString(call.functionName()),
operandsToTree(call.args())
);
case PatioCallPointer callPointer ->
treeList(
treeIdentifier("call-pointer"),
operandsToTree(callPointer.destinations()),
operandToTree(callPointer.functionPointer()),
operandsToTree(callPointer.args())
);
case PatioDataPtrToInt dataPtrToInt ->
treeList(
treeIdentifier("data-ptr-to-int"),
operandToTree(dataPtrToInt.destination()),
operandToTree(dataPtrToInt.value())
);
case PatioIf if_ ->
treeListIndented(
treeIdentifier("if"),
operandsToTree(if_.destinations()),
operandToTree(if_.condition()),
blockToTree(if_.trueBody()),
blockToTree(if_.falseBody())
);
case PatioIntAdd intAdd ->
treeList(
treeIdentifier("int-add"),
operandToTree(intAdd.destination()),
operandToTree(intAdd.left()),
operandToTree(intAdd.right())
);
case PatioIntEquals intEquals ->
treeList(
treeIdentifier("int-eq"),
operandToTree(intEquals.destination()),
operandToTree(intEquals.left()),
operandToTree(intEquals.right())
);
case PatioIntNotEqual intNotEqual ->
treeList(
treeIdentifier("int-ne"),
operandToTree(intNotEqual.destination()),
operandToTree(intNotEqual.left()),
operandToTree(intNotEqual.right())
);
case PatioIntSub intSub ->
treeList(
treeIdentifier("int-sub"),
operandToTree(intSub.destination()),
operandToTree(intSub.left()),
operandToTree(intSub.right())
);
case PatioIntToBool intToBool ->
treeList(
treeIdentifier("int-to-bool"),
operandToTree(intToBool.destination()),
operandToTree(intToBool.value())
);
case PatioIntToDataPtr intToDataPtr ->
treeList(
treeIdentifier("int-to-data-ptr"),
operandToTree(intToDataPtr.destination()),
operandToTree(intToDataPtr.value())
);
case PatioLocalStore localStore ->
treeList(
treeIdentifier("local-store"),
operandToTree(localStore.destination()),
operandToTree(localStore.value())
);
case PatioLoop loop ->
treeListIndented(
treeIdentifier("loop"),
instructionsToTree(loop.body())
);
case PatioLoopBreak loopBreak ->
treeList(
treeIdentifier("break")
);
case PatioLoopContinue loopContinue ->
treeList(
treeIdentifier("continue")
);
case PatioMemoryLoad memoryLoad ->
treeListIndented(
treeIdentifier("memory-load"),
operandToTree(memoryLoad.destination()),
treeNamed("pointer", operandToTree(memoryLoad.pointer())),
treeNamed("layout", dataLayoutToTree(memoryLoad.layout())),
treeNamed("element-offsets", elementOffsetsToTree(memoryLoad.elementOffsets()))
);
case PatioMemoryStore memoryStore ->
treeListIndented(
treeIdentifier("memory-store"),
treeNamed("pointer", operandToTree(memoryStore.pointer())),
treeNamed("layout", dataLayoutToTree(memoryStore.layout())),
treeNamed("element-offsets", elementOffsetsToTree(memoryStore.elementOffsets())),
treeNamed("value", operandToTree(memoryStore.value()))
);
case PatioReturn return_ ->
treeList(
treeIdentifier("return"),
operandsToTree(return_.values())
);
case PatioSwitch switch_ ->
treeListIndented(
treeIdentifier("switch"),
operandsToTree(switch_.destinations()),
operandToTree(switch_.value()),
treeListIndented(
switch_.cases().stream()
.map(switchCase -> treeListIndented(
treeIdentifier("case"),
treeInt(switchCase.value()),
blockToTree(switchCase.body())
))
.toList()
),
blockToTree(switch_.defaultBody())
);
case PatioTry try_ ->
treeListIndented(
treeIdentifier("try"),
operandsToTree(try_.destinations()),
treeListIndented(
try_.catchClauses().stream()
.map(catchClause -> treeListIndented(
treeIdentifier("catch"),
treeString(catchClause.exceptionTypeName()),
operandToTree(catchClause.destination()),
blockToTree(catchClause.body())
))
.toList()
),
blockToTree(try_.body())
);
case PatioThrow throw_ ->
treeList(
treeIdentifier("throw"),
treeString(throw_.exceptionTypeName()),
operandToTree(throw_.value())
);
case PatioUnreachable unreachable ->
treeList(treeIdentifier("unreachable"));
case PatioVarLoad varLoad ->
treeList(
treeIdentifier("var-load"),
operandToTree(varLoad.destination()),
operandToTree(varLoad.var())
);
case PatioVarStore varStore ->
treeList(
treeIdentifier("var-store"),
operandToTree(varStore.var()),
operandToTree(varStore.value())
);
};
}
private static Tree blockToTree(PatioBlock block) {
return treeListIndented(
treeIdentifier("block"),
instructionsToTree(block.body()),
treeNamed(
"results",
treeList(
block.results().stream()
.map(result -> operandToTree(result))
.toList()
)
)
);
}
private static Tree localToTree(PatioLocal local) {
var localType = switch (local.localType()) {
case PARAM ->
"param";
case VAL ->
"val";
case VAR ->
"var";
};
return treeList(
treeIdentifier(localType),
treeString(local.name()),
typeToTree(local.type())
);
}
private static Tree optionalOperandToTree(Optional<PatioLocalRef> destination) {
if (destination.isEmpty()) {
return treeIdentifier("_");
} else {
return operandToTree(destination.get());
}
}
private static Tree operandsToTree(List<? extends PatioOperand> operands) {
return treeList(
operands.stream()
.map(destination -> operandToTree(destination))
.toList()
);
}
private static Tree operandToTree(PatioOperand operand) {
return switch (operand) {
case PatioBoolConst bool ->
treeList(treeIdentifier("bool-const"), treeBool(bool.value()));
case PatioByteConst byte_ ->
treeList(treeIdentifier("byte-const"), treeInt(byte_.value()));
case PatioDataPtrConst dataPtr ->
treeList(treeIdentifier("data-ptr-const"), dataPtrValueToTree(dataPtr.value()));
case PatioFunctionPtrConst functionPtr ->
treeList(treeIdentifier("function-ptr-const"), treeString(functionPtr.name()));
case PatioIntConst int_ ->
treeList(treeIdentifier("int-const"), intValueToTree(int_.value()));
case PatioInt32Const int32 ->
treeList(treeIdentifier("int32-const"), intValueToTree(int32.value()));
case PatioLocalRef localRef ->
treeList(treeIdentifier("local"), treeString(localRef.name()));
};
}
private static Tree typeToTree(PatioType type) {
return switch (type) {
case PatioBoolType boolType ->
treeIdentifier("bool");
case PatioByteType byteType ->
treeIdentifier("byte");
case PatioDataPtrType dataPointerType ->
treeIdentifier("data-ptr");
case PatioFunctionPtrType functionPointerType ->
treeIdentifier("function-ptr");
case PatioIntType intType ->
treeIdentifier("int");
case PatioInt32Type int32Type ->
treeIdentifier("int32");
};
}
private static Tree dataLayoutToTree(PatioDataLayout layout) {
return switch (layout) {
case PatioDataLayoutArray array ->
treeList(
treeIdentifier("array"),
dataLayoutToTree(array.element()),
treeInt(array.size())
);
case PatioDataLayoutBool bool ->
treeIdentifier("bool");
case PatioDataLayoutByte byte_ ->
treeIdentifier("byte");
case PatioDataLayoutDataPtr dataPointer ->
treeIdentifier("data-ptr");
case PatioDataLayoutFunctionPtr functionPointer ->
treeIdentifier("function-ptr");
case PatioDataLayoutInt int_ ->
treeIdentifier("int");
case PatioDataLayoutInt32 int32 ->
treeIdentifier("int32");
case PatioDataLayoutStruct struct ->
treeList(
treeIdentifier("struct"),
treeList(
struct.elements().stream()
.map(element -> dataLayoutToTree(element))
.toList()
)
);
};
}
private static Tree dataValueToTree(PatioDataValue value) {
return switch (value) {
case PatioDataValueArray array ->
treeList(
treeIdentifier("array"),
dataLayoutToTree(array.elementLayout()),
treeList(
array.elements().stream()
.map(element -> dataValueToTree(element))
.toList()
)
);
case PatioDataValueBool bool ->
treeList(
treeIdentifier("bool"),
treeIdentifier(bool.value() ? "true" : "false")
);
case PatioDataValueByte byte_ ->
treeList(
treeIdentifier("byte"),
treeIdentifier(String.format("0x%02x", byte_.value()))
);
case PatioDataValueDataPtr dataPointer ->
treeList(
treeIdentifier("data-ptr"),
dataPtrValueToTree(dataPointer.value())
);
case PatioDataValueInt int_ ->
treeList(
treeIdentifier("int"),
intValueToTree(int_.value())
);
case PatioDataValueInt32 int32 ->
treeList(
treeIdentifier("int32"),
intValueToTree(int32.value())
);
case PatioDataValueStruct struct ->
treeList(
treeIdentifier("struct"),
treeList(
struct.elements().stream()
.map(element -> dataValueToTree(element))
.toList()
)
);
};
}
private static Tree dataPtrValueToTree(PatioDataPtrValue dataPtrValue) {
return switch (dataPtrValue) {
case PatioDataPtrValueDataElement dataElement ->
treeString(dataElement.name());
case PatioDataPtrValueNull null_ ->
treeIdentifier("null");
};
}
private static Tree intValueToTree(PatioIntValue intValue) {
return switch (intValue) {
case PatioIntValueByteOffsetOf byteOffsetOf ->
treeList(
treeIdentifier("byte-offset-of"),
dataLayoutToTree(byteOffsetOf.layout()),
elementOffsetsToTree(byteOffsetOf.elementOffsets())
);
case PatioIntValueByteSizeOf byteSizeOf ->
treeList(
treeIdentifier("byte-size-of"),
dataLayoutToTree(byteSizeOf.layout())
);
case PatioIntValueLiteral literal ->
treeInt(literal.value());
};
}
private static Tree elementOffsetsToTree(List<Integer> elementOffsets) {
return treeList(
elementOffsets.stream()
.map(offset -> treeInt(offset))
.toList()
);
}
private static Tree visibilityToTree(PatioVisibility visibility) {
return treeIdentifier(switch (visibility) {
case PRIVATE -> "private";
case PUBLIC -> "public";
});
}
}
|