Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / parser / ooaStatements.cs

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
using System;
20
using System.Collections.Generic;
21

    
22

    
23
namespace TUG.Mogentes
24
{
25
    public enum StatementKind
26
    {
27
        GuardedCommand,
28
        MethodCall,
29
        Assignment,
30
        SeqBlock,
31
        NondetBlock,
32
        PrioBlock,
33
        Skip, Abort, Kill,
34
        QualConstraint
35
    }
36

    
37

    
38
    ///////////////////////////////////////////////
39
    ///  basic Statement
40
    ///  
41
    public abstract class Statement : UlyssesBasicClass, IAst
42
    {
43
        private StatementKind m_kind;
44
        private int m_line;
45
        private int m_pos;
46

    
47
        public StatementKind kind { get { return m_kind; } }
48
        public int line { get { return m_line; } }
49
        public int pos { get { return m_pos; } }
50

    
51
        public Statement(StatementKind aKind, int aline, int apos)
52
        {
53
            m_kind = aKind;
54
            m_line = aline;
55
            m_pos = apos;
56
        }
57

    
58
        public Statement(Statement toCopy)
59
        {
60
            m_kind = toCopy.m_kind;
61
            m_line = toCopy.m_line;
62
            m_pos = toCopy.m_pos;
63
        }
64

    
65
        public virtual Statement Clone()
66
        {
67
            throw new NotImplementedException();
68
        }
69

    
70
        public AstNodeTypeEnum nodeType { get { return AstNodeTypeEnum.statement; } }
71

    
72
        public virtual void Accept(IAstVisitor visitor)
73
        {
74
            throw new NotImplementedException();
75
        }
76
    }
77

    
78

    
79
    ///////////////////////////////////////////////
80
    ///  Qualitative Constraints
81
    ///  
82
    public enum QualitativeConstraintOperation { Equal, Deriv, Sum, Prod, Diff }
83
    public sealed class QualitativeConstraintStatement : Statement
84
    {
85
        private Identifier m_variable0;
86
        private Identifier m_variable1;
87
        private Identifier m_variable2;
88
        private QualitativeConstraintOperation m_operation;
89
        public bool tag = false; // says that this is an obs assignment (important for prolog code target)
90

    
91
        public Identifier variable0 { get { return m_variable0; } }
92
        public Identifier variable1 { get { return m_variable1; } }
93
        public Identifier variable2 { get { return m_variable2; } }
94
        public QualitativeConstraintOperation operation { get { return m_operation; } }
95

    
96
        public QualitativeConstraintStatement(Identifier var0, Identifier var1, QualitativeConstraintOperation op, int aline, int apos)
97
            : base(StatementKind.QualConstraint, aline, apos)
98
        {
99
            if (op == QualitativeConstraintOperation.Diff
100
                || op == QualitativeConstraintOperation.Prod
101
                || op == QualitativeConstraintOperation.Sum)
102
                throw new ArgumentException();
103
            m_variable0 = var0;
104
            m_variable1 = var1;
105
            m_variable2 = null;
106
            m_operation = op;
107
        }
108

    
109
        public QualitativeConstraintStatement(QualitativeConstraintStatement toCopy)
110
            : base(toCopy)
111
        {
112
            m_variable0 = toCopy.m_variable0;
113
            m_variable1 = toCopy.m_variable1;
114
            m_variable2 = toCopy.m_variable2;
115
            m_operation = toCopy.m_operation;
116
            tag = toCopy.tag;
117
        }
118

    
119

    
120
        public QualitativeConstraintStatement(Identifier var0, Identifier var1, Identifier var2, QualitativeConstraintOperation op, int aline, int apos)
121
            : base(StatementKind.QualConstraint, aline, apos)
122
        {
123
            if (op == QualitativeConstraintOperation.Deriv
124
                || op == QualitativeConstraintOperation.Equal)
125
                throw new ArgumentException();
126
            m_variable0 = var0;
127
            m_variable1 = var1;
128
            m_variable2 = var2;
129
            m_operation = op;
130
        }
131

    
132
        public override void Accept(IAstVisitor visitor)
133
        {
134
            visitor.visit(this);
135
        }
136

    
137
        public override Statement Clone()
138
        {
139
            return new QualitativeConstraintStatement(this);
140
        }
141

    
142
        public void SetVariable0(Identifier newVal)
143
        {
144
            m_variable0 = newVal;
145
        }
146

    
147
        public void SetVariable1(Identifier newVal)
148
        {
149
            m_variable1 = newVal;
150
        }
151

    
152
        public void SetVariable2(Identifier newVal)
153
        {
154
            m_variable2 = newVal;
155
        }
156
    }
157

    
158
    ///////////////////////////////////////////////
159
    ///  Blocks
160
    ///  
161
    public abstract class Block : Statement, IScope
162
    {
163
        private SymbolTable m_symbols;
164
        private LinkedList<Statement> m_statements;
165
        private Expression m_filter; /*gets transformed to guarded command in resolvevisitor!*/
166
        private IScope m_parentScope;
167

    
168
        public SymbolTable symbols { get { return m_symbols; } }
169
        public LinkedList<Statement> statements { get { return m_statements; } }
170
        public Expression filter { get { return m_filter; } }
171

    
172
        public Block(StatementKind aKind, int aline, int apos)
173
            : base(aKind, aline, apos)
174
        {
175
            m_symbols = new SymbolTable();
176
            m_statements = new LinkedList<Statement>();
177
        }
178

    
179
        public Block(Block toCopy)
180
            : base(toCopy)
181
        {
182
            m_symbols = new SymbolTable(toCopy.m_symbols);
183
            m_statements = new LinkedList<Statement>(toCopy.m_statements);
184
            m_parentScope = toCopy.m_parentScope;
185
            m_filter = toCopy.m_filter;
186
        }
187

    
188
        public override Statement Clone()
189
        {
190
            throw new NotImplementedException();
191
        }
192

    
193
        public void AddStatement(Statement toAdd)
194
        {
195
            m_statements.AddLast(toAdd);
196
        }
197

    
198
        public void SetStatements(LinkedList<Statement> newStatements)
199
        {
200
            m_statements = newStatements;
201
        }
202

    
203
        public void AddIdentifier(Identifier anIdentifier)
204
        {
205
            m_symbols.AddIdentifier(anIdentifier);
206
        }
207

    
208
        public Identifier ResolveIdentifier(string aName)
209
        {
210
            if (m_symbols.Defined(aName))
211
                return m_symbols.Get(aName);
212
            else
213
                return null;
214
        }
215

    
216
        public IScope GetParentScope()
217
        {
218
            return m_parentScope;
219
        }
220

    
221
        public void SetParentScope(IScope parentScope)
222
        {
223
            m_parentScope = parentScope;
224
        }
225

    
226
        public void AddIdentifier(Identifier anIdentifier, object tag)
227
        {
228
            m_symbols.AddIdentifier(anIdentifier);
229
        }
230

    
231

    
232
        internal void SetFilter(Expression sexpr)
233
        {
234
            m_filter = sexpr;
235
        }
236
    }
237

    
238
    public sealed class NondetBlock : Block
239
    {
240
        public NondetBlock(int aline, int apos)
241
            : base(StatementKind.NondetBlock, aline, apos)
242
        { }
243

    
244
        public NondetBlock(Block aParent, int aline, int apos)
245
            : base(StatementKind.NondetBlock, aline, apos)
246
        {
247
            if (aParent != null)
248
                aParent.AddStatement(this);
249
        }
250

    
251
        public NondetBlock(NondetBlock toCopy)
252
            : base(toCopy)
253
        { }
254

    
255
        public override Statement Clone()
256
        {
257
            return new NondetBlock(this);
258
        }
259

    
260
        public override void Accept(IAstVisitor visitor)
261
        {
262
            visitor.visit(this);
263
        }
264
    }
265

    
266
    public sealed class SeqBlock : Block
267
    {
268
        public SeqBlock(int aline, int apos)
269
            : base(StatementKind.SeqBlock, aline, apos)
270
        { }
271

    
272
        public SeqBlock(Block aParent, int aline, int apos)
273
            : base(StatementKind.SeqBlock, aline, apos)
274
        {
275
            if (aParent != null)
276
                aParent.AddStatement(this);
277
        }
278

    
279
        public SeqBlock(SeqBlock toCopy)
280
            : base(toCopy)
281
        { }
282

    
283
        public override Statement Clone()
284
        {
285
            return new SeqBlock(this);
286
        }
287

    
288
        public override void Accept(IAstVisitor visitor)
289
        {
290
            visitor.visit(this);
291
        }
292
    }
293

    
294
    public sealed class PrioBlock : Block
295
    {
296
        public PrioBlock(int aline, int apos)
297
            : base(StatementKind.PrioBlock, aline, apos)
298
        { }
299

    
300
        public PrioBlock(Block aParent, int aline, int apos)
301
            : base(StatementKind.PrioBlock, aline, apos)
302
        {
303
            if (aParent != null)
304
                aParent.AddStatement(this);
305
        }
306

    
307
        public PrioBlock(PrioBlock toCopy)
308
            : base(toCopy)
309
        { }
310

    
311
        public override Statement Clone()
312
        {
313
            return new PrioBlock(this);
314
        }
315

    
316
        public override void Accept(IAstVisitor visitor)
317
        {
318
            visitor.visit(this);
319
        }
320
    }
321

    
322

    
323

    
324
    ///////////////////////////////////////////////
325
    ///  Guarded Command
326
    ///  
327
    public sealed class GuardedCommand : Statement
328
    {
329
        private Expression m_guard;
330
        private Block m_body;
331
        private bool m_isQualitative;
332

    
333
        public Expression guard { get { return m_guard; } }
334
        public Block body { get { return m_body; } }
335
        public bool isQualitative { get { return m_isQualitative; } }
336

    
337
        public GuardedCommand(Expression aGuard, Block aBlock, int aline, int apos)
338
            : base(StatementKind.GuardedCommand, aline, apos)
339
        {
340
            m_guard = aGuard;
341
            m_body = aBlock;
342
            m_isQualitative = false;
343
        }
344

    
345
        public GuardedCommand(GuardedCommand toCopy)
346
            : base(toCopy)
347
        {
348
            m_guard = toCopy.m_guard;
349
            m_body = toCopy.m_body;
350
            m_isQualitative = toCopy.m_isQualitative;
351
        }
352

    
353
        public override Statement Clone()
354
        {
355
            return new GuardedCommand(this);
356
        }
357

    
358
        public override void Accept(IAstVisitor visitor)
359
        {
360
            visitor.visit(this);
361
        }
362

    
363
        public void SetGuard(Expression newGuard)
364
        {
365
            if (newGuard == null)
366
                throw new ArgumentException();
367
            m_guard = newGuard;
368
        }
369

    
370
        public void SetBody(Block newBody)
371
        {
372
            if (newBody == null)
373
                throw new ArgumentException();
374
            m_body = newBody;
375
        }
376

    
377
        public void SetIsQualitative(bool newVal)
378
        {
379
            m_isQualitative = newVal;
380
        }
381
    }
382

    
383

    
384
    ///////////////////////////////////////////////
385
    ///  Assignment
386
    ///  
387
    public sealed class Assignment : Statement, IScope
388
    {
389
        private LinkedList<Expression> m_places;
390
        private LinkedList<Expression> m_values;
391
        private Expression m_nondetExpression;
392
        private IScope m_prarentScope;
393
        private SymbolTable m_symbols; // needed for nondeterminsitic assignment (we most likely will have new variables)
394

    
395
        public LinkedList<Expression> places { get { return m_places; } }
396
        public LinkedList<Expression> values { get { return m_values; } }
397
        public Expression nondetExpression { get { return m_nondetExpression; } }
398
        public SymbolTable symbols { get { return m_symbols; } }
399

    
400
        public Assignment(Expression place, Expression value, Expression nondetExpression, int aline, int apos)
401
            : base(StatementKind.Assignment, aline, apos)
402
        {
403
            m_places = new LinkedList<Expression>();
404
            m_values = new LinkedList<Expression>();
405
            if (place != null)
406
                m_places.AddFirst(place);
407
            if (value != null)
408
                m_values.AddFirst(value);
409
            m_nondetExpression = nondetExpression;
410
            m_symbols = new SymbolTable();
411
        }
412

    
413
        public Assignment(Assignment toCopy)
414
            : base(toCopy)
415
        {
416
            m_places = new LinkedList<Expression>(toCopy.m_places);
417
            m_values = new LinkedList<Expression>(toCopy.m_values);
418
            m_nondetExpression = toCopy.m_nondetExpression;
419
            m_symbols = new SymbolTable(toCopy.symbols);
420
        }
421

    
422
        public override Statement Clone()
423
        {
424
            return new Assignment(this);
425
        }
426

    
427
        public void AddPlace(Expression aPlace)
428
        {
429
            m_places.AddLast(aPlace);
430
        }
431

    
432
        public void AddValue(Expression aValue)
433
        {
434
            m_values.AddLast(aValue);
435
        }
436

    
437
        public void SetNondetExpression(Expression anExpr)
438
        {
439
            m_nondetExpression = anExpr;
440
        }
441

    
442
        public override void Accept(IAstVisitor visitor)
443
        {
444
            visitor.visit(this);
445
        }
446

    
447
        #region IScope Member
448

    
449
        public Identifier ResolveIdentifier(string aName)
450
        {
451
            if (m_symbols.Defined(aName))
452
                return m_symbols.Get(aName);
453
            else
454
                return null;
455
        }
456

    
457
        public IScope GetParentScope()
458
        {
459
            return m_prarentScope;
460
        }
461

    
462
        public void SetParentScope(IScope parentScope)
463
        {
464
            m_prarentScope = parentScope;
465
        }
466

    
467
        public void AddIdentifier(Identifier anIdentifier, object tag)
468
        {
469
            m_symbols.AddIdentifier(anIdentifier);
470
        }
471

    
472
        #endregion
473
    }
474

    
475

    
476
    ///////////////////////////////////////////////
477
    ///  Methodcall
478
    ///  
479
    public sealed class Call : Statement
480
    {
481
        private Expression m_callExpression;
482

    
483
        public Expression callExpression { get { return m_callExpression; } }
484

    
485
        public Call(Expression callExpression, int aline, int apos)
486
            : base(StatementKind.MethodCall, aline, apos)
487
        {
488
            m_callExpression = callExpression;
489
        }
490

    
491
        public Call(Call toCopy)
492
            : base(toCopy)
493
        {
494
            m_callExpression = toCopy.m_callExpression;
495
        }
496

    
497
        public override Statement Clone()
498
        {
499
            return new Call(this);
500
        }
501

    
502
        public override void Accept(IAstVisitor visitor)
503
        {
504
            visitor.visit(this);
505
        }
506

    
507
        public void SetCallExpression(Expression newExpression)
508
        {
509
            //if (newExpression == null)
510
            //    throw new ArgumentException();
511
            m_callExpression = newExpression;
512
        }
513
    }
514

    
515

    
516
    ///////////////////////////////////////////////
517
    ///  Simple Statements
518
    ///  
519
    public sealed class SkipStatement : Statement
520
    {
521
        public SkipStatement(int aline, int apos)
522
            : base(StatementKind.Skip, aline, apos)
523
        { }
524

    
525
        public SkipStatement(SkipStatement copy)
526
            : base(copy)
527
        { }
528

    
529
        public override Statement Clone()
530
        {
531
            return new SkipStatement(this);
532
        }
533

    
534
        public override void Accept(IAstVisitor visitor)
535
        {
536
            visitor.visit(this);
537
        }
538
    }
539

    
540
    public sealed class AbortStatement : Statement
541
    {
542
        public AbortStatement(int aline, int apos)
543
            : base(StatementKind.Abort, aline, apos)
544
        { }
545

    
546
        public AbortStatement(AbortStatement toCopy)
547
            : base(toCopy)
548
        { }
549

    
550
        public override Statement Clone()
551
        {
552
            return new AbortStatement(this);
553
        }
554

    
555
        public override void Accept(IAstVisitor visitor)
556
        {
557
            visitor.visit(this);
558
        }
559
    }
560

    
561
    public sealed class KillStatement : Statement
562
    {
563
        public Identifier someOne;
564

    
565
        public KillStatement(Identifier toKill, int aline, int apos)
566
            : base(StatementKind.Kill, aline, apos)
567
        {
568
            someOne = toKill;
569
        }
570

    
571
        public KillStatement(KillStatement toCopy)
572
            : base(toCopy)
573
        {
574
            someOne = toCopy.someOne;
575
        }
576

    
577
        public override Statement Clone()
578
        {
579
            return new KillStatement(this);
580
        }
581

    
582
        public override void Accept(IAstVisitor visitor)
583
        {
584
            visitor.visit(this);
585
        }
586

    
587
        public void SetIdentifier(Identifier target)
588
        {
589
            someOne = target;
590
        }
591
    }
592
}