1
|
/**
|
2
|
*
|
3
|
* OOAS Compiler (Deprecated)
|
4
|
*
|
5
|
* Copyright 2015, Institute for Software Technology, Graz University of
|
6
|
* Technology. Portions are copyright 2015 by the AIT Austrian Institute
|
7
|
* of Technology. All rights reserved.
|
8
|
*
|
9
|
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
|
10
|
*
|
11
|
* Please notice that this version of the OOAS compiler is considered de-
|
12
|
* precated. Only the Java version is maintained.
|
13
|
*
|
14
|
* Contributors:
|
15
|
* Willibald Krenn (TU Graz/AIT)
|
16
|
* Stefan Tiran (TU Graz/AIT)
|
17
|
*/
|
18
|
|
19
|
/*
|
20
|
* Prolog Code Generator
|
21
|
*/
|
22
|
|
23
|
using System;
|
24
|
using System.Collections.Generic;
|
25
|
using System.Text;
|
26
|
using TUG.Mogentes.Codegen;
|
27
|
|
28
|
|
29
|
namespace TUG.Mogentes.Codegen.Prolog
|
30
|
{
|
31
|
public class OoaPrologType : OoaTypeVisitor
|
32
|
{
|
33
|
public class Factory
|
34
|
{
|
35
|
public virtual OoaPrologType create(OoaPrologIdentifier.Factory idFactory) { return new OoaPrologType(idFactory); }
|
36
|
}
|
37
|
|
38
|
private static C5.HashBag<UlyssesType> definedTypes = new C5.HashBag<UlyssesType>();
|
39
|
public static StringBuilder typeDefinitions = new StringBuilder();
|
40
|
|
41
|
public static void EmitType(UlyssesType aType, OoaPrologIdentifier.Factory idFactory)
|
42
|
{
|
43
|
OoaPrologType ptype = new OoaPrologType(idFactory);
|
44
|
OoaCodeEmitter m_emitter = new OoaCodeEmitter();
|
45
|
|
46
|
if (definedTypes.Contains(aType))
|
47
|
return;
|
48
|
|
49
|
definedTypes.Add(aType);
|
50
|
|
51
|
switch (aType.kind)
|
52
|
{
|
53
|
case TypeKind.OoActionSystemType:
|
54
|
aType.Accept(ptype);
|
55
|
OoActionSystemType classtype = (OoActionSystemType)aType;
|
56
|
// m_emitter.Append(String.Format("type( {0}, X) :- member(X,[ 'null'", ptype.ToString()));
|
57
|
if (classtype.objects.Count == 0 && classtype.derivedObjects.Count == 0)
|
58
|
{
|
59
|
m_emitter.AppendLine(String.Format("type( {0}, nil).", ptype.ToString()));
|
60
|
}
|
61
|
else
|
62
|
{
|
63
|
m_emitter.Append(String.Format("type( {0}, X) :- member(X,[", ptype.ToString()));
|
64
|
bool firste = true;
|
65
|
foreach (var inst in classtype.objects)
|
66
|
{
|
67
|
if (firste)
|
68
|
firste = false;
|
69
|
else
|
70
|
m_emitter.Append(", ");
|
71
|
m_emitter.Append(String.Format("'{0}'", inst.Name));
|
72
|
}
|
73
|
firste = true;
|
74
|
if (classtype.objects.Count > 0 && classtype.derivedObjects.Count > 0)
|
75
|
m_emitter.Append(", ");
|
76
|
foreach (var inst in classtype.derivedObjects)
|
77
|
{
|
78
|
if (firste)
|
79
|
firste = false;
|
80
|
else
|
81
|
m_emitter.Append(", ");
|
82
|
m_emitter.Append(String.Format("'{0}'", inst.Name));
|
83
|
}
|
84
|
m_emitter.AppendLine("]).");
|
85
|
}
|
86
|
break;
|
87
|
case TypeKind.EnumeratedType:
|
88
|
aType.Accept(ptype);
|
89
|
if (aType is ValuedEnumType)
|
90
|
{
|
91
|
ValuedEnumType venum = (ValuedEnumType)aType;
|
92
|
m_emitter.Append(String.Format("type({0}, X) :- member(X,[", ptype.ToString()));
|
93
|
bool nocomma = true;
|
94
|
foreach (var val in venum.symbolTable.symbolList)
|
95
|
{
|
96
|
EnumIdentifier id = (EnumIdentifier)val;
|
97
|
System.Diagnostics.Debug.Assert(id.HaveValue);
|
98
|
if (nocomma)
|
99
|
nocomma = false;
|
100
|
else
|
101
|
m_emitter.Append(", ");
|
102
|
m_emitter.Append(id.Value);
|
103
|
}
|
104
|
m_emitter.AppendLine("]).");
|
105
|
}
|
106
|
else
|
107
|
{
|
108
|
EnumType enumtype = (EnumType)aType;
|
109
|
m_emitter.AppendLine(String.Format("type( {0}, X) :- X in 0..{1}, labeling([],[X]).", ptype.ToString(), enumtype.listOfEnumSymbols.Count - 1));
|
110
|
}
|
111
|
break;
|
112
|
case TypeKind.IntType:
|
113
|
aType.Accept(ptype);
|
114
|
IntType inttype = (IntType)aType;
|
115
|
m_emitter.AppendLine(String.Format("type( {0}, X) :- X in {1}..{2}, labeling([],[X]).", ptype.ToString(), inttype.rangeLow, inttype.rangeHigh));
|
116
|
break;
|
117
|
case TypeKind.FloatType:
|
118
|
throw new NotImplementedException();
|
119
|
|
120
|
case TypeKind.Any:
|
121
|
break;
|
122
|
|
123
|
case TypeKind.ListType:
|
124
|
ListType listType = (ListType)aType;
|
125
|
aType.Accept(ptype);
|
126
|
m_emitter.Append(String.Format("type( {0}, ", ptype.ToString()));
|
127
|
m_emitter.Append("[");
|
128
|
Boolean first = true;
|
129
|
OoaPrologType pType = new OoaPrologType(idFactory);
|
130
|
listType.innerType.Accept(pType);
|
131
|
|
132
|
for (int i = 0; i < listType.maxNumberOfElements; i++)
|
133
|
{
|
134
|
if (!first)
|
135
|
{
|
136
|
m_emitter.Append(", ");
|
137
|
}
|
138
|
first = false;
|
139
|
m_emitter.Append(pType.ToString());
|
140
|
}
|
141
|
m_emitter.AppendLine("]).");
|
142
|
break;
|
143
|
|
144
|
case TypeKind.MapType:
|
145
|
throw new NotImplementedException();
|
146
|
|
147
|
case TypeKind.FunctionType:
|
148
|
case TypeKind.OpaqueType:
|
149
|
case TypeKind.Null:
|
150
|
throw new NotImplementedException(); // must not hit this
|
151
|
|
152
|
case TypeKind.TupleType:
|
153
|
TupleType tupleType = (TupleType)aType;
|
154
|
aType.Accept(ptype);
|
155
|
m_emitter.Append(String.Format("type( {0}, ", ptype.ToString()));
|
156
|
m_emitter.Append("[");
|
157
|
first = true;
|
158
|
|
159
|
foreach (var type in tupleType.innerTypes)
|
160
|
{
|
161
|
OoaPrologType iType = new OoaPrologType(idFactory);
|
162
|
type.Accept(iType);
|
163
|
|
164
|
if (!first)
|
165
|
{
|
166
|
m_emitter.Append(", ");
|
167
|
}
|
168
|
first = false;
|
169
|
m_emitter.Append(iType.ToString());
|
170
|
}
|
171
|
m_emitter.AppendLine("]).");
|
172
|
break;
|
173
|
|
174
|
case TypeKind.QrType:
|
175
|
aType.Accept(ptype);
|
176
|
QrType qrtype = (QrType)aType;
|
177
|
|
178
|
StringBuilder landmarks = new StringBuilder();
|
179
|
int cntr = 0;
|
180
|
foreach (var s in qrtype.landmarks)
|
181
|
{
|
182
|
if (cntr != 0)
|
183
|
landmarks.Append(", ");
|
184
|
else
|
185
|
cntr++;
|
186
|
|
187
|
OoaPrologIdentifier identifierVisitor = idFactory.create();
|
188
|
s.Accept(identifierVisitor);
|
189
|
landmarks.Append(identifierVisitor.ToString());
|
190
|
}
|
191
|
m_emitter.AppendLine(String.Format("qspace( {0}, [{1}] ).", ptype.ToString(), landmarks.ToString()));
|
192
|
break;
|
193
|
default:
|
194
|
throw new NotImplementedException();
|
195
|
}
|
196
|
typeDefinitions.Append(m_emitter.ToString());
|
197
|
}
|
198
|
|
199
|
|
200
|
private OoaCodeEmitter m_emitter = new OoaCodeEmitter();
|
201
|
private OoaPrologIdentifier.Factory m_idFactory;
|
202
|
|
203
|
public override void visit(CharType charType)
|
204
|
{
|
205
|
m_emitter.Append("char");
|
206
|
}
|
207
|
|
208
|
public override void visit(IntType intType)
|
209
|
{
|
210
|
OoaPrologIdentifier id = m_idFactory.create();
|
211
|
intType.identifier.Accept(id);
|
212
|
EmitType(intType, m_idFactory);
|
213
|
m_emitter.Append(id.ToString());
|
214
|
}
|
215
|
|
216
|
public override void visit(BoolType boolType)
|
217
|
{
|
218
|
m_emitter.Append("bool");
|
219
|
}
|
220
|
|
221
|
public override void visit(FloatType floatType)
|
222
|
{
|
223
|
throw new NotImplementedException();
|
224
|
}
|
225
|
|
226
|
public override void visit(EnumType enumType)
|
227
|
{
|
228
|
OoaPrologIdentifier id = m_idFactory.create();
|
229
|
enumType.identifier.Accept(id);
|
230
|
EmitType(enumType, m_idFactory);
|
231
|
m_emitter.Append(id.ToString());
|
232
|
}
|
233
|
|
234
|
public override void visit(ListType listType)
|
235
|
{
|
236
|
OoaPrologIdentifier id = m_idFactory.create();
|
237
|
listType.identifier.Accept(id);
|
238
|
EmitType(listType, m_idFactory);
|
239
|
m_emitter.Append(id.ToString());
|
240
|
|
241
|
}
|
242
|
|
243
|
public override void visit(MapType mapType)
|
244
|
{
|
245
|
throw new NotImplementedException();
|
246
|
}
|
247
|
|
248
|
public override void visit(QrType qrType)
|
249
|
{
|
250
|
OoaPrologIdentifier id = m_idFactory.create();
|
251
|
qrType.identifier.Accept(id);
|
252
|
m_emitter.Append(id.ToString());
|
253
|
// m_emitter.Append(qrType.identifier.tokenText.ToLower());
|
254
|
}
|
255
|
|
256
|
public override void visit(TupleType tupleType)
|
257
|
{
|
258
|
OoaPrologIdentifier id = m_idFactory.create();
|
259
|
tupleType.identifier.Accept(id);
|
260
|
EmitType(tupleType, m_idFactory);
|
261
|
m_emitter.Append(id.ToString());
|
262
|
}
|
263
|
|
264
|
public override void visit(FunctionType functionType)
|
265
|
{
|
266
|
throw new NotImplementedException();
|
267
|
}
|
268
|
|
269
|
public override void visit(OoActionSystemType ooActionSystemType)
|
270
|
{
|
271
|
OoaPrologIdentifier id = m_idFactory.create();
|
272
|
ooActionSystemType.identifier.Accept(id);
|
273
|
m_emitter.Append(id.ToString());
|
274
|
}
|
275
|
|
276
|
public override void visit(OpaqueType opaqueType)
|
277
|
{
|
278
|
throw new NotImplementedException();
|
279
|
}
|
280
|
|
281
|
|
282
|
public override string ToString()
|
283
|
{
|
284
|
return m_emitter.ToString();
|
285
|
}
|
286
|
|
287
|
public virtual string GetInternalTypeDefinitions()
|
288
|
{
|
289
|
OoaCodeEmitter result = new OoaCodeEmitter();
|
290
|
|
291
|
result.AppendLine("type(bool, X) :- member(X,[fail,true]).");
|
292
|
result.AppendLine("type(char, X) :- X in 0..255, labeling([],[X]).");
|
293
|
|
294
|
return result.ToString();
|
295
|
}
|
296
|
|
297
|
protected OoaPrologType(OoaPrologIdentifier.Factory idFactory)
|
298
|
{
|
299
|
m_idFactory = idFactory;
|
300
|
}
|
301
|
}
|
302
|
}
|