Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / codegen / prologCodeGenerator.cs @ 3

1

2
/*
3
 *  This is a quick'n dirty hack to see whether the internal data structures are sane enough to emit some
4
 *  prolog code..
5
 * 
6
 * 
7
 */
8

    
9

    
10

    
11
using System;
12
using System.Collections.Generic;
13
using System.Text;
14

    
15
namespace TUG.Mogentes.Codegen
16
{
17
    class prologCodeGenerator : SyntaxTreeVisitor
18
    {
19
        protected int indentLevel = 0;
20
        protected int indentTabWidth = 4;
21
        protected bool writeTypes = true;
22
        protected StringBuilder output = new StringBuilder();
23

    
24
        protected Dictionary<NamedTypeSymbol, string> definedTypes = new Dictionary<NamedTypeSymbol, string>();
25

    
26
        public void Append(string text)
27
        {
28
            output.Append(text);
29
        }
30

    
31
        public void Indent()
32
        {
33
            for (int i = 0; i < indentLevel * indentTabWidth; i++)
34
                output.Append(" ");
35
        }
36

    
37
        public void AppendLine(string text)
38
        {
39
            output.AppendLine(text);
40
            Indent();
41
        }
42

    
43

    
44
        public override string ToString()
45
        {
46
            return output.ToString();
47
        }
48

    
49

    
50

    
51
        public void visit<T>(CompositionList<T> compositionList, int stage) where T : SyntaxTree
52
        {
53

    
54
        }
55

    
56
        public void visit(OoaType anObject, int stage)
57
        {
58
            if (stage != 0)
59
                return;
60

    
61
            // VARIABLES
62

    
63
            List<string> varlist = new List<string>();
64
            List<string> init = new List<string>();
65

    
66
            AppendLine("");
67
            AppendLine("% variable defs");
68
            foreach (var sym in anObject.symbols.Values)
69
            {
70
                if (sym.GetType() == typeof(VariableSymbol))
71
                {
72
                    init.Add(((VariableSymbol)sym).initializer.ToString());
73

    
74
                    Append("var([");
75
                    Append(sym.tokenText);
76
                    Append("],");
77

    
78
                    varlist.Add(sym.tokenText);
79

    
80
                    if (sym.type is OpaqueType)
81
                    {
82
                        Append(((OpaqueType)sym.type).referencedType.tokenText);
83
                        AppendLine(").");
84
                    }
85
                    else
86
                    {
87
                        AppendLine("). % anonymous types not supported");
88
                    }
89
                }
90
            }
91

    
92
            /*define state vector*/
93
            AppendLine("");
94
            Append("state_def([");
95
            int cntr = varlist.Count;
96
            varlist.ForEach(i => { cntr--; Append(i); if (cntr > 0) Append(","); });
97
            AppendLine("]).");
98

    
99
            /*define start values (just a copy/paste of the initializer expressions)*/
100
            AppendLine("");
101
            Append("init([");
102
            cntr = init.Count;
103
            init.ForEach(i => { cntr--; Append(i); if (cntr > 0) Append(","); });
104
            AppendLine("]).");
105

    
106

    
107
            // ACTIONS
108
            AppendLine("");
109
            List<NamedActionSymbol> actions = new List<NamedActionSymbol>();
110
            foreach (var sym in anObject.symbols.Values)
111
            {
112
                if (sym.GetType() == typeof(NamedActionSymbol))
113
                    actions.Add((NamedActionSymbol)sym);
114
            }
115

    
116
            if (actions.Count > 0)
117
            {
118
                Append("as :- ");
119
                cntr = actions.Count;
120
                foreach (var na in actions)
121
                {
122
                    cntr--;
123
                    Append(na.tokenText); Append(":");
124
                    foreach (UlyssesStatement s in ((FunctionType)na.type).body)
125
                    {
126
                        if (s is GuardedCommandStatement)
127
                        {
128
                            Append("(");
129
                            Append(((GuardedCommandStatement)s).guard.ToString());
130
                            Append(") => (");
131
                            bool first = true;
132
                            foreach (UlyssesStatement sb in ((GuardedCommandStatement)s).body)
133
                            {
134
                                if (first)
135
                                    first = false;
136
                                else
137
                                    Append(",");
138
                                Append(sb.ToString());
139

    
140
                            }
141
                            Append(")");
142
                        }
143
                    }
144
                    if (cntr > 0)
145
                        AppendLine(",");
146
                }
147
                AppendLine(".");
148
            }
149

    
150
        }
151

    
152
        public void visit(BoolType anObject, int stage)
153
        {
154
        }
155

    
156
        public void visit(IntType intType, int p)
157
        {
158
        }
159

    
160
        public void visit(FloatType floatType, int p)
161
        {
162

    
163
        }
164

    
165
        public void visit(CharType charType, int p)
166
        {
167

    
168
        }
169

    
170
        public void visit(EnumType enumType, int p)
171
        {
172

    
173
        }
174

    
175
        public void visit(ListType listType, int p)
176
        {
177

    
178
        }
179

    
180
        public void visit(MapType mapType, int p)
181
        {
182

    
183
        }
184

    
185
        public void visit(TupleType tupleType, int p)
186
        {
187

    
188
        }
189

    
190
        public void visit(OoaSystem ooaSystem, int stage)
191
        {
192
            if (stage == 0)
193
            {
194
                AppendLine(":- public(as/0).");
195
                AppendLine(":- public(asM/0).");
196
                AppendLine("");
197
                AppendLine(":- dynamic(qstate_init/1).");
198
                AppendLine(":- dynamic(qstate_constraints/1).");
199
                AppendLine(":- public(qstate_constraints/1).");
200
                AppendLine(":- public(qstate_init/1).");
201

    
202
                AppendLine("");
203
                AppendLine("% defintion of maximal search depth");
204
                AppendLine("searchDepth(15).");
205
                AppendLine("");
206
            }
207
            if (stage == 2)
208
                writeTypes = false;
209

    
210
        }
211

    
212
        public bool visit(NamedTypeSymbol namedTypeSymbol, int p)
213
        {
214
            if (p != 0)
215
                return writeTypes;
216

    
217
            if (!definedTypes.ContainsKey(namedTypeSymbol))
218
            {
219
                definedTypes.Add(namedTypeSymbol, namedTypeSymbol.tokenText);
220
                int i;
221
                Type aType = namedTypeSymbol.type.GetType();
222
                string atypedef = "type(" + namedTypeSymbol.tokenText + ",";
223
                if (aType == typeof(IntType))
224
                {
225
                    IntType intType = (IntType)namedTypeSymbol.type;
226
                    Append(atypedef);
227
                    Append("X) :- "); Append("fd_domain(X,"); Append(intType.low.ToString()); Append(",");
228
                    Append(intType.high.ToString()); Append("),");
229
                    AppendLine("fd_labeling(X).");
230
                }
231
                else if (aType == typeof(EnumType))
232
                {
233
                    EnumType enumType = (EnumType)namedTypeSymbol.type;
234
                    i = enumType.enumSymbols.Count;
235
                    Append(atypedef);
236
                    Append("[");
237
                    foreach (var sym in enumType.enumSymbols)
238
                    {
239
                        i--;
240
                        Append("'");
241
                        Append(sym.tokenText);
242
                        Append("'");
243
                        if (i > 0)
244
                            Append(",");
245
                    }
246
                    AppendLine("]).");
247

    
248
                }
249
                else if (aType == typeof(BoolType))
250
                {
251
                    AppendLine(" % bool not supported yet. ");
252
                }
253
                else if (aType == typeof(FloatType))
254
                {
255
                    AppendLine(" % float not supported yet. ");
256
                }
257
                else if (aType == typeof(CharType))
258
                {
259
                    AppendLine(" % char not supported yet. ");
260
                }
261
                else if (aType == typeof(ListType))
262
                {
263
                    AppendLine(" % list not supported yet. ");
264
                }
265
                else if (aType == typeof(MapType))
266
                {
267
                    AppendLine(" % map not supported yet. ");
268
                }
269
                else if (aType == typeof(TupleType))
270
                {
271
                    AppendLine(" % tuple not supported yet. ");
272
                }
273
            }
274
            return writeTypes;
275
        }
276

    
277
        public bool visit(ParameterSymbol parameterSymbol, int p)
278
        {
279
            return true;
280
        }
281

    
282
        public bool visit(VariableSymbol variableSymbol, int p)
283
        {
284
            return true;
285
        }
286

    
287
        public bool visit(MethodSymbol methodSymbol, int p)
288
        {
289

    
290

    
291

    
292
            return true;
293
        }
294

    
295
        public void visit(EnumIdSymbol enumIdSymbol, int p)
296
        {
297

    
298
        }
299

    
300
        public void visit(AbortStatement abortStatement, int p)
301
        {
302

    
303
        }
304

    
305
        public void visit(SkipStatement skipStatement, int p)
306
        {
307

    
308
        }
309

    
310
        public void visit(KillStatement killStatement, int p)
311
        {
312

    
313
        }
314

    
315
        public void visit(GuardedCommandStatement guardedCommandStatement, int p)
316
        {
317

    
318
        }
319

    
320
        public void visit(NamedActionInclusionStatement namedActionInclusionStatement, int p)
321
        {
322

    
323
        }
324

    
325
        public void visit(MethodCallStatement methodCallStatement, int p)
326
        {
327

    
328
        }
329

    
330
        public void visit(AssignmentStatement assignmentStatement, int p)
331
        {
332

    
333
        }
334

    
335
        public void visit(LocalVarStatement localVarStatement, int p)
336
        {
337

    
338
        }
339

    
340
        public void visit(ConstantChar constantChar, int p)
341
        {
342

    
343
        }
344

    
345
        public void visit(ConstantInt constantInt, int p)
346
        {
347

    
348
        }
349

    
350
        public void visit(ConstantFloat constantFloat, int p)
351
        {
352

    
353
        }
354

    
355
        public void visit(ConstantNil constantNil, int p)
356
        {
357

    
358
        }
359

    
360
        public void visit(ConstantBool constantBool, int p)
361
        {
362

    
363
        }
364

    
365
        public void visit(ConstantList constantList, int p)
366
        {
367

    
368
        }
369

    
370
        public void visit(ConstantSet constantSet, int p)
371
        {
372

    
373
        }
374

    
375
        public void visit(ConstantMap constantMap, int p)
376
        {
377

    
378
        }
379

    
380
        public void visit(UnaryOperator unaryOperator, int p)
381
        {
382

    
383
        }
384

    
385
        public void visit(SetListUnaryOperator setListUnaryOperator, int p)
386
        {
387

    
388
        }
389

    
390
        public void visit(MapUnaryOperator mapUnaryOperator, int p)
391
        {
392

    
393
        }
394

    
395
        public void visit(BinaryOperator binaryOperator, int p)
396
        {
397

    
398
        }
399

    
400
        public void visit(BoolBinaryOperator boolBinaryOperator, int p)
401
        {
402

    
403
        }
404

    
405
        public void visit(NumericBinaryOperator numericBinaryOperator, int p)
406
        {
407

    
408
        }
409

    
410
        public void visit(SetListBinaryOperator setListBinaryOperator, int p)
411
        {
412

    
413
        }
414

    
415
        public void visit(MapBinaryOperator mapBinaryOperator, int p)
416
        {
417

    
418
        }
419

    
420
        public void visit(QualifiedIdentifier qualifiedIdentifier, int p)
421
        {
422

    
423
        }
424

    
425
        public void visit(Reference reference, int p)
426
        {
427

    
428
        }
429

    
430
        public void visit(AccessExpression accessExpression, int p)
431
        {
432

    
433
        }
434

    
435
        public void visit(ConditionalOperator conditionalOperator, int p)
436
        {
437

    
438
        }
439

    
440
        public void visit(OpaqueType namedType, int p)
441
        {
442

    
443
        }
444

    
445
        public bool visit(NamedActionSymbol namedActionSymbol, int p)
446
        {
447
            return true;
448
        }
449

    
450

    
451
        public void visit(ConstantTuple constantTuple, int p)
452
        {
453
        }
454

    
455

    
456
        #region SyntaxTreeVisitor Member
457

    
458

    
459
        public bool visit(FunctionType functionType, int p)
460
        {
461
            return true;
462
        }
463

    
464
        #endregion
465
    }
466
}