Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / parser / visitors / ooaPrioritizedCompositionVisitor.cs @ 3

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
using System.Text;
22

    
23
namespace TUG.Mogentes
24
{
25
    /// <summary>
26
    /// removes trivial prioritized compositions.
27
    /// </summary>
28
    public sealed class OoaRemoveTrivialPrioritizedCompositionVisitor : OoaCompleteAstTraversalVisitor
29
    {
30
        /// <summary>
31
        /// replaces the prioBlock by its child (most likely a parBlock)
32
        /// </summary>
33
        private IAst ConvertPrioBlock(PrioBlock prioBlock, IAst parent)
34
        {
35
            if (prioBlock.statements.Count <= 1)
36
            {
37
                LinkedListNode<Statement> child = prioBlock.statements.First;
38
                Block newBlock = (Block)child.Value;
39

    
40
                switch (parent.nodeType)
41
                {
42
                    case AstNodeTypeEnum.type:
43
                        switch (((UlyssesType)parent).kind)
44
                        {
45
                            case TypeKind.OoActionSystemType:
46
                                OoActionSystemType atype = (OoActionSystemType)parent;
47
                                Block ablock = atype.doOdBlock;
48
                                System.Diagnostics.Debug.Assert(ReferenceEquals(ablock, prioBlock));
49
                                atype.SetDoOdBlock(newBlock);
50
                                break;
51
                            default:
52
                                throw new NotImplementedException();
53
                        }
54
                        break;
55
                    case AstNodeTypeEnum.statement:
56
                        switch (((Statement)parent).kind)
57
                        {
58
                            case StatementKind.SeqBlock:
59
                                SeqBlock aseqblock = (SeqBlock)parent;
60
                                LinkedListNode<Statement> toChange = aseqblock.statements.Find(prioBlock);
61
                                toChange.Value = newBlock;
62
                                break;
63
                            case StatementKind.GuardedCommand:
64
                                GuardedCommand gc = (GuardedCommand)parent;
65
                                System.Diagnostics.Debug.Assert(ReferenceEquals(gc.body, prioBlock));
66
                                gc.SetBody(newBlock);
67
                                break;
68
                            default:
69
                                throw new NotImplementedException();
70
                        }
71
                        break;
72
                    case AstNodeTypeEnum.identifier:
73
                        switch (((Identifier)parent).kind)
74
                        {
75
                            case IdentifierKind.MethodIdentifier:
76
                                MethodIdentifier method = (MethodIdentifier)parent;
77
                                System.Diagnostics.Debug.Assert(ReferenceEquals(method.body, prioBlock));
78
                                method.SetBody(newBlock);
79
                                break;
80
                            default:
81
                                throw new NotImplementedException();
82
                        }
83
                        break;
84
                    default:
85
                        throw new NotImplementedException();
86
                }
87
                return newBlock;
88
            }
89
            else
90
                return prioBlock;
91
        }
92

    
93

    
94
        protected override void VisitAstElement(IAst element, IAst parent)
95
        {
96
            if ((element.nodeType == AstNodeTypeEnum.statement) &&
97
                (((Statement)element).kind == StatementKind.PrioBlock) &&
98
                // we're only interested in actionblock and actionbody composition, not in type composition
99
                !(parent.nodeType == AstNodeTypeEnum.identifier && ((Identifier)parent).kind == IdentifierKind.List))
100
            {
101
                IAst newchild = ConvertPrioBlock((PrioBlock)element, parent);
102
                base.VisitAstElement(newchild, parent);
103
            }
104
            else
105
                base.VisitAstElement(element, parent);
106
        }
107

    
108

    
109
        public override void visit(MainModule mainModule)
110
        {
111
            // get rid of any top-level spurious block
112
            if (mainModule.systemDescription != null)
113
                while (mainModule.systemDescription.identifiers.Count == 1
114
                    && mainModule.systemDescription.identifiers.First.Value.kind == IdentifierKind.List)
115
                {
116
                    mainModule.SetSystemDescription((IdentifierList)mainModule.systemDescription.identifiers.First.Value);
117
                }
118
            base.visit(mainModule);
119
        }
120

    
121

    
122
        public OoaRemoveTrivialPrioritizedCompositionVisitor(ParserState aState)
123
            : base(aState)
124
        { }
125
    }
126
}