Project

General

Profile

root / trunk / compiler / ooasCompiler / src / org / momut / ooas / ast / statements / Block.java @ 7

1
/**
2
  *
3
  *                      OOAS Compiler
4
  *
5
  *       Copyright 2015, AIT Austrian Institute of Technology.
6
  * This code is based on the C# Version of the OOAS Compiler, which is
7
  * copyright 2015 by the Institute of Software Technology, Graz University
8
  * of Technology with portions copyright by the AIT Austrian Institute of
9
  * Technology. All rights reserved.
10
  *
11
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
12
  *
13
  * If you modify the file please update the list of contributors below to in-
14
  * clude your name. Please also stick to the coding convention of using TABs
15
  * to do the basic (block-level) indentation and spaces for anything after
16
  * that. (Enable the display of special chars and it should be pretty obvious
17
  * what this means.) Also, remove all trailing whitespace.
18
  *
19
  * Contributors:
20
  *               Willibald Krenn (AIT)
21
  *               Stephan Zimmerer (AIT)
22
  *               Markus Demetz (AIT)
23
  *               Christoph Czurda (AIT)
24
  *
25
  */
26

    
27

    
28
package org.momut.ooas.ast.statements;
29

    
30
import java.util.LinkedList;
31

    
32
import org.momut.ooas.ast.IScope;
33
import org.momut.ooas.ast.expressions.Expression;
34
import org.momut.ooas.ast.identifiers.Identifier;
35
import org.momut.ooas.parser.SymbolTable;
36
import org.momut.ooas.utils.exceptions.NotImplementedException;
37
import org.momut.ooas.utils.exceptions.InternalCompilerException;
38

    
39
///////////////////////////////////////////////
40
///  Blocks
41
///
42
public abstract class Block extends Statement implements IScope
43
{
44
        private final SymbolTable m_symbols;
45
        private LinkedList<Statement> m_statements;
46
        private Expression m_filter; /*gets transformed to guarded command in resolvevisitor!*/
47
        private IScope m_parentScope;
48

    
49
        public SymbolTable symbols() { return m_symbols; }
50
        public LinkedList<Statement> statements() { return m_statements; }
51
        public Expression filter() { return m_filter; }
52

    
53
        public Block(StatementKind aKind, int aline, int apos)
54
        {
55
                super (aKind, aline, apos);
56
                m_symbols = new SymbolTable();
57
                m_statements = new LinkedList<Statement>();
58
        }
59

    
60
        public Block(Block toCopy)
61
        {
62
                super (toCopy);
63
                m_symbols = new SymbolTable(toCopy.m_symbols);
64
                m_statements = new LinkedList<Statement>(toCopy.m_statements);
65
                m_parentScope = toCopy.m_parentScope;
66
                m_filter = toCopy.m_filter;
67
        }
68

    
69
        @Override
70
        public /*override*/ Statement Clone()
71
        {
72
                throw new NotImplementedException();
73
        }
74

    
75
        public void AddStatement(Statement toAdd)
76
        {
77
                m_statements.addLast(toAdd);
78
                checkParentScope(toAdd);
79
        }
80

    
81
        private void checkParentScope(Statement toAdd) {
82
                if (toAdd instanceof IScope) {
83
                        final IScope child = (IScope) toAdd;
84
                        if (child.GetParentScope() == null)
85
                                child.SetParentScope(this);
86
                        else if (child.GetParentScope() != this)
87
                                throw new InternalCompilerException("Internal error: Statement added to block has parent set to different object.");
88
                }
89
        }
90

    
91
        public void SetStatements(LinkedList<Statement> newStatements)
92
        {
93
                m_statements = newStatements;
94
                for (final Statement s: newStatements)
95
                        checkParentScope(s);
96
        }
97

    
98
        public void AddIdentifier(Identifier anIdentifier)
99
        {
100
                m_symbols.AddIdentifier(anIdentifier);
101
        }
102

    
103
        @Override
104
        public Identifier ResolveIdentifier(String aName)
105
        {
106
                if (m_symbols.Defined(aName))
107
                        return m_symbols.Get(aName);
108
                else
109
                        return null;
110
        }
111

    
112
        @Override
113
        public IScope GetParentScope()
114
        {
115
                return m_parentScope;
116
        }
117

    
118
        @Override
119
        public void SetParentScope(IScope parentScope)
120
        {
121
                if (parentScope == null)
122
                        System.out.println("");
123
                m_parentScope = parentScope;
124
        }
125

    
126
        @Override
127
        public void AddIdentifier(Identifier anIdentifier, Object tag)
128
        {
129
                m_symbols.AddIdentifier(anIdentifier);
130
        }
131

    
132

    
133
        public void SetFilter(Expression sexpr)
134
        {
135
                m_filter = sexpr;
136
        }
137

    
138
        /** Checks whether there are no local variables etc. for this block */
139
        public boolean isSimpleBlock() {
140
                return m_symbols.size() == 0 && m_filter == null;
141
        }
142
}