Revision 7
Added by Willibald K. over 8 years ago
SymbolTable.java | ||
---|---|---|
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 |
*/
|
|
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 | 26 |
|
27 | 27 |
|
28 |
package org.momut.ooas.parser; |
|
29 |
|
|
30 |
import java.util.ArrayList; |
|
31 |
import java.util.Collections; |
|
32 |
import java.util.List; |
|
33 |
import java.util.Map.Entry; |
|
28 |
package org.momut.ooas.parser; |
|
34 | 29 |
|
30 |
import java.util.ArrayList; |
|
31 |
import java.util.Collections; |
|
32 |
import java.util.List; |
|
33 |
import java.util.Map.Entry; |
|
34 |
|
|
35 | 35 |
import org.momut.ooas.ast.identifiers.Identifier; |
36 | 36 |
import org.momut.ooas.utils.OoasCompilerHashMap; |
37 |
|
|
38 |
public class SymbolTable |
|
39 |
{ |
|
40 |
private OoasCompilerHashMap<String, Identifier> m_symbols; |
|
41 |
|
|
42 |
public List<Identifier> symbolList() { return new ArrayList<Identifier>(m_symbols.values());} |
|
43 |
|
|
44 |
public SymbolTable() |
|
45 |
{ |
|
46 |
m_symbols = new OoasCompilerHashMap<String, Identifier>(); |
|
47 |
} |
|
48 |
|
|
49 |
public SymbolTable(SymbolTable toCopy) |
|
50 |
{ |
|
51 |
if (toCopy != null) |
|
52 |
m_symbols = new OoasCompilerHashMap<String, Identifier>(toCopy.m_symbols); |
|
53 |
else |
|
54 |
m_symbols = new OoasCompilerHashMap<String, Identifier>(); |
|
55 |
} |
|
56 |
|
|
57 |
public boolean Defined(Identifier aValue) |
|
58 |
{ |
|
59 |
return aValue == m_symbols.get(aValue.tokenText()); // ref comp! |
|
60 |
} |
|
61 |
|
|
62 |
public boolean Defined(String aKey) |
|
63 |
{ |
|
64 |
return m_symbols.containsKey(aKey); |
|
65 |
} |
|
66 |
|
|
67 |
public Identifier Get(String aKey) |
|
68 |
{ |
|
69 |
return m_symbols.get(aKey); |
|
70 |
} |
|
71 |
|
|
72 |
public void AddIdentifier(Identifier anIdent) |
|
73 |
{ |
|
74 |
if (anIdent != null) { |
|
75 |
final Identifier oldValue = m_symbols.put(anIdent.tokenText(), anIdent); |
|
76 |
if (oldValue != null) |
|
77 |
throw new UnsupportedOperationException( |
|
78 |
String.format("Identifier '%s' already exists", anIdent.tokenText())); |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
public void AddSymbolTable(SymbolTable otherTable) |
|
83 |
{ |
|
84 |
for (final Entry<String,Identifier> x: otherTable.m_symbols.entrySet()) |
|
85 |
if ( ! x.getKey().equals("self")) { |
|
86 |
final Identifier oldValue = m_symbols.put(x.getKey(), x.getValue()); |
|
87 |
if (oldValue != null) |
|
88 |
throw new UnsupportedOperationException( |
|
89 |
String.format("Identifier '%s' already exists", x.getKey())); |
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
public void Replace(String aKey, Identifier anIdent) |
|
94 |
{ |
|
95 |
if (anIdent != null) |
|
96 |
{ |
|
97 |
m_symbols.remove(aKey); |
|
98 |
m_symbols.put(anIdent.tokenText(), anIdent); |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
public void SetSymbolList(List<Identifier> newList) |
|
103 |
{ |
|
104 |
Collections.sort(newList); |
|
105 |
m_symbols = new OoasCompilerHashMap<String, Identifier>(); |
|
106 |
for (final Identifier x : newList) |
|
107 |
AddIdentifier(x); |
|
108 |
} |
|
109 |
|
|
110 |
public void Sort() |
|
111 |
{ |
|
112 |
final List<Identifier> newList = new ArrayList<Identifier>(m_symbols.values()); |
|
113 |
Collections.sort(newList); |
|
114 |
m_symbols = new OoasCompilerHashMap<String, Identifier>(); |
|
115 |
for (final Identifier x : newList) |
|
116 |
AddIdentifier(x); |
|
117 |
} |
|
118 |
|
|
119 |
public int size() { |
|
120 |
return m_symbols.size(); |
|
121 |
} |
|
122 |
} |
|
37 |
|
|
38 |
public class SymbolTable |
|
39 |
{ |
|
40 |
private OoasCompilerHashMap<String, Identifier> m_symbols; |
|
41 |
|
|
42 |
public List<Identifier> symbolList() { return new ArrayList<Identifier>(m_symbols.values());} |
|
43 |
|
|
44 |
public SymbolTable() |
|
45 |
{ |
|
46 |
m_symbols = new OoasCompilerHashMap<String, Identifier>(); |
|
47 |
} |
|
48 |
|
|
49 |
public SymbolTable(SymbolTable toCopy) |
|
50 |
{ |
|
51 |
if (toCopy != null) |
|
52 |
m_symbols = new OoasCompilerHashMap<String, Identifier>(toCopy.m_symbols); |
|
53 |
else |
|
54 |
m_symbols = new OoasCompilerHashMap<String, Identifier>(); |
|
55 |
} |
|
56 |
|
|
57 |
public boolean Defined(Identifier aValue) |
|
58 |
{ |
|
59 |
return aValue == m_symbols.get(aValue.tokenText()); // ref comp! |
|
60 |
} |
|
61 |
|
|
62 |
public boolean Defined(String aKey) |
|
63 |
{ |
|
64 |
return m_symbols.containsKey(aKey); |
|
65 |
} |
|
66 |
|
|
67 |
public Identifier Get(String aKey) |
|
68 |
{ |
|
69 |
return m_symbols.get(aKey); |
|
70 |
} |
|
71 |
|
|
72 |
public void AddIdentifier(Identifier anIdent) |
|
73 |
{ |
|
74 |
if (anIdent != null) { |
|
75 |
final Identifier oldValue = m_symbols.put(anIdent.tokenText(), anIdent); |
|
76 |
if (oldValue != null) |
|
77 |
throw new UnsupportedOperationException( |
|
78 |
String.format("Identifier '%s' already exists", anIdent.tokenText())); |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
public void AddSymbolTable(SymbolTable otherTable) |
|
83 |
{ |
|
84 |
for (final Entry<String,Identifier> x: otherTable.m_symbols.entrySet()) |
|
85 |
if ( ! x.getKey().equals("self")) { |
|
86 |
final Identifier oldValue = m_symbols.put(x.getKey(), x.getValue()); |
|
87 |
if (oldValue != null) |
|
88 |
throw new UnsupportedOperationException( |
|
89 |
String.format("Identifier '%s' already exists", x.getKey())); |
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
public void Replace(String aKey, Identifier anIdent) |
|
94 |
{ |
|
95 |
if (anIdent != null) |
|
96 |
{ |
|
97 |
m_symbols.remove(aKey); |
|
98 |
m_symbols.put(anIdent.tokenText(), anIdent); |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
public void SetSymbolList(List<Identifier> newList) |
|
103 |
{ |
|
104 |
Collections.sort(newList); |
|
105 |
m_symbols = new OoasCompilerHashMap<String, Identifier>(); |
|
106 |
for (final Identifier x : newList) |
|
107 |
AddIdentifier(x); |
|
108 |
} |
|
109 |
|
|
110 |
public void Sort() |
|
111 |
{ |
|
112 |
final List<Identifier> newList = new ArrayList<Identifier>(m_symbols.values()); |
|
113 |
Collections.sort(newList); |
|
114 |
m_symbols = new OoasCompilerHashMap<String, Identifier>(); |
|
115 |
for (final Identifier x : newList) |
|
116 |
AddIdentifier(x); |
|
117 |
} |
|
118 |
|
|
119 |
public int size() { |
|
120 |
return m_symbols.size(); |
|
121 |
} |
|
122 |
} |
Also available in: Unified diff
changing java, cpp, hpp files to unix line endings