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 servicec.converter; | |
6 | |
7 import 'node.dart' show | |
8 CompilationUnitNode, | |
9 FieldNode, | |
10 FormalNode, | |
11 FunctionNode, | |
12 ListType, | |
13 MemberNode, | |
14 PointerType, | |
15 RecursiveVisitor, | |
16 ServiceNode, | |
17 SimpleType, | |
18 StructNode, | |
19 TopLevelNode, | |
20 TypeNode, | |
21 UnionNode; | |
22 | |
23 import 'package:old_servicec/src/parser.dart' as old; | |
24 | |
25 import 'dart:collection' show | |
26 Queue; | |
27 | |
28 // Validation functions. | |
29 old.Unit convert(CompilationUnitNode compilationUnit) { | |
30 Iterable<TopLevelNode> services = | |
31 compilationUnit.topLevels.where((topLevel) => topLevel is ServiceNode); | |
32 Iterable<TopLevelNode> structs = | |
33 compilationUnit.topLevels.where((topLevel) => topLevel is StructNode); | |
34 return new old.Unit( | |
35 services.map(convertService).toList(), | |
36 structs.map(convertStruct).toList()); | |
37 } | |
38 | |
39 old.Service convertService(ServiceNode service) { | |
40 return new old.Service( | |
41 service.identifier.value, | |
42 service.functions.map(convertFunction).toList()); | |
43 } | |
44 | |
45 old.Struct convertStruct(StructNode struct) { | |
46 Iterable<MemberNode> fields = | |
47 struct.members.where((member) => member is FieldNode); | |
48 Iterable<MemberNode> unions = | |
49 struct.members.where((member) => member is UnionNode); | |
50 return new old.Struct( | |
51 struct.identifier.value, | |
52 fields.map(convertField).toList(), | |
53 unions.map(convertUnion).toList()); | |
54 } | |
55 | |
56 old.Method convertFunction(FunctionNode function) { | |
57 return new old.Method( | |
58 function.identifier.value, | |
59 function.formals.map(convertFormal).toList(), | |
60 convertType(function.returnType)); | |
61 } | |
62 | |
63 old.Formal convertFormal(FormalNode formal) { | |
64 return new old.Formal( | |
65 convertType(formal.type), | |
66 formal.identifier.value); | |
67 } | |
68 | |
69 old.Formal convertField(FieldNode field) { | |
70 return new old.Formal( | |
71 convertType(field.type), | |
72 field.identifier.value); | |
73 } | |
74 | |
75 old.Union convertUnion(UnionNode union) { | |
76 return new old.Union( | |
77 union.fields.map(convertField).toList()); | |
78 } | |
79 | |
80 old.Type convertType(TypeNode type) { | |
81 if (type.isString()) { | |
82 return new old.StringType(); | |
83 } else if (type.isPointer()) { | |
84 return new old.SimpleType(type.identifier.value, true); | |
85 } else if (type.isList()) { | |
86 ListType listType = type; | |
87 return new old.ListType(convertType(listType.typeParameter)); | |
88 } else { | |
89 return new old.SimpleType(type.identifier.value, false); | |
90 } | |
91 } | |
OLD | NEW |