OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library old_servicec.pretty_printer; | |
6 | |
7 import 'parser.dart'; | |
8 import 'struct_layout.dart'; | |
9 | |
10 import 'dart:core' hide Type; | |
11 | |
12 class PrettyPrinter extends Visitor { | |
13 final StringBuffer buffer = new StringBuffer(); | |
14 | |
15 visit(Node node) => node.accept(this); | |
16 | |
17 visitUnit(Unit node) { | |
18 node.services.forEach(visit); | |
19 node.structs.forEach(visit); | |
20 } | |
21 | |
22 visitService(Service node) { | |
23 buffer.writeln("service ${node.name} {"); | |
24 node.methods.forEach(visit); | |
25 buffer.writeln("}"); | |
26 } | |
27 | |
28 visitMethod(Method node) { | |
29 buffer.write(" "); | |
30 writeType(node.returnType); | |
31 buffer.write(" ${node.name}("); | |
32 bool first = true; | |
33 node.arguments.forEach((Formal formal) { | |
34 if (!first) buffer.write(", "); | |
35 first = false; | |
36 visit(formal); | |
37 }); | |
38 buffer.writeln(");"); | |
39 } | |
40 | |
41 visitStruct(Struct node) { | |
42 StructLayout layout = node.layout; | |
43 buffer.writeln("struct ${node.name} { // size = ${layout.size} bytes"); | |
44 for (Formal slot in node.slots) { | |
45 buffer.write(" "); | |
46 visit(slot); | |
47 buffer.writeln("; // offset = ${layout[slot].offset}"); | |
48 } | |
49 node.unions.forEach(visit); | |
50 buffer.writeln("}"); | |
51 } | |
52 | |
53 visitUnion(Union node) { | |
54 StructLayout layout = node.struct.layout; | |
55 buffer.writeln(" union {"); | |
56 for (Formal slot in node.slots) { | |
57 buffer.write(" "); | |
58 visit(slot); | |
59 buffer.writeln("; // offset = ${layout[slot].offset}"); | |
60 } | |
61 buffer.writeln(" }"); | |
62 } | |
63 | |
64 visitFormal(Formal node) { | |
65 writeType(node.type); | |
66 buffer.write(" ${node.name}"); | |
67 } | |
68 | |
69 void writeType(Type node) { | |
70 if (node.isList) buffer.write("List<"); | |
71 buffer.write(node.identifier); | |
72 if (node.isList) buffer.write(">"); | |
73 } | |
74 } | |
OLD | NEW |