EOS 2  1.1.0
Einfache Objektbasierte Sprache
Environment.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.baseparser;
2 
3 import java.util.LinkedList;
4 import java.util.Map.Entry;
5 import java.util.TreeMap;
6 
7 import de.lathanda.eos.baseparser.exceptions.WrongPlaceForDeclarationException;
8 import de.lathanda.eos.vm.ErrorInformation;
9 import de.lathanda.eos.vm.Marker;
10 
18 public class Environment {
19 
23  private final LinkedList<ErrorInformation> errors = new LinkedList<>();
24  /*
25  * currently valid local variables
26  */
27  private final TreeMap<String, Type> variables = new TreeMap<>();
28  /*
29  * currently valid global variables
30  */
31  private final TreeMap<String, Type> storedVariables = new TreeMap<>();
32  /*
33  * user definied functions
34  */
35  private final TreeMap<String, MethodType> functions;
36  /*
37  * flag indicates if a window is part of the program
38  */
39  private boolean hasWindow = false;
40  /*
41  * flag indicates if the program contains at least one figure
42  */
43  private boolean hasFigure = false;
44  /*
45  * name of default window
46  */
47  private final String defaultWindowName;
48  /*
49  * lock properties
50  */
51  private final boolean lockProperties;
52  /*
53  * variable declaration is only allow on level 0 or within procedures.
54  */
55  private int variableLock = 0;
56  /*
57  * next unique id, for creating artifical unique variables, eg counting
58  * variable for counting loop
59  */
60  private int uid = 0;
64  private final Program program;
65 
66  public Environment(Program p, String defaultWindowName, boolean lockProperties) {
67  this.lockProperties = lockProperties;
68  this.defaultWindowName = defaultWindowName;
69  this.functions = new TreeMap<>();
70  program = p;
71  }
72 
73  public Environment(boolean lockProperties, Program p, String defaultWIndowName) {
74  this.lockProperties = lockProperties;
75  this.defaultWindowName = defaultWIndowName;
76  this.functions = new TreeMap<>();
77  program = p;
78  }
79 
80  public Program getProgram() {
81  return program;
82  }
83 
84  public void addError(Marker marker, String errorId, Object... data) {
85  errors.add(new CompilerError(marker, errorId, data));
86  }
87 
88  public LinkedList<ErrorInformation> getErrors() {
89  return errors;
90  }
91 
92  public Type getVariableType(String name) {
93  return variables.getOrDefault(name, Type.getUnknown());
94  }
95 
96  public void setVariableType(String name, Type type) {
97  if (variableLock == 0) {
98  variables.put(name, type);
99  } else {
100  throw new WrongPlaceForDeclarationException(name, type.getID());
101  }
102  }
103 
104  public boolean isVariableDefined(String name) {
105  return variables.containsKey(name);
106  }
107 
108  public void resetVariables() {
109  variables.clear();
110  }
111 
112  public void resetAll() {
113  variables.clear();
114  errors.clear();
115  functions.clear();
116  storedVariables.clear();
117  hasFigure = false;
118  hasWindow = false;
119  }
120 
121  public MethodType getFunctionSignature(String name, int args) {
122  return functions.get(name.toLowerCase() + "(" + args + ")");
123  }
124 
125  public void setFunctionSignature(String name, int args, MethodType methodType) {
126  functions.put(name.toLowerCase() + "(" + args + ")", methodType);
127  }
128 
129  public boolean isFunctionDefined(String name, int args) {
130  return functions.containsKey(name.toLowerCase() + "(" + args + ")");
131  }
132 
133  public int getUID() {
134  return uid++;
135  }
136 
137  public void setWindowExists() {
138  hasWindow = true;
139  }
140 
141  public void setFigureExists() {
142  hasFigure = true;
143  }
144 
145  public boolean getAutoWindow() {
146  return !hasWindow && hasFigure;
147  }
149  variableLock++;
150  }
151  public void allowVariableDeclaration() {
152  variableLock--;
153  }
154 
155  public void storeVariables() {
156  storedVariables.putAll(variables);
157  }
158 
159  public void restoreVariables() {
160  variables.clear();
161  variables.putAll(storedVariables);
162  }
163 
164  @Override
165  public String toString() {
166  StringBuilder res = new StringBuilder();
167  res.append("environment\n");
168  res.append("hasWindow = ").append(hasWindow).append("\n");
169  res.append("hasFigure = ").append(hasFigure).append("\n");
170  res.append("variableLock = ").append(variableLock).append("\n");
171  res.append("local variable:\n");
172  for (Entry<String, Type> v : variables.entrySet()) {
173  res.append(v.getKey()).append(":").append(v.getValue()).append("\n");
174  }
175  res.append("global variable:\n");
176  for (Entry<String, Type> v : storedVariables.entrySet()) {
177  res.append(v.getKey()).append(":").append(v.getValue()).append("\n");
178  }
179  res.append("user defined functions:\n");
180  for (Entry<String, MethodType> f : functions.entrySet()) {
181  res.append(f.getValue()).append("\n");
182  }
183  res.append("endenvironment\n");
184  return res.toString();
185  }
186 
187  public String getDefaultWindowName() {
188  return defaultWindowName;
189  }
190 
191  public boolean getLockProperties() {
192  return lockProperties;
193  }
194 
195 }
void setVariableType(String name, Type type)
LinkedList< ErrorInformation > getErrors()
MethodType getFunctionSignature(String name, int args)
void setFunctionSignature(String name, int args, MethodType methodType)
Environment(boolean lockProperties, Program p, String defaultWIndowName)
Environment(Program p, String defaultWindowName, boolean lockProperties)
boolean isFunctionDefined(String name, int args)
void addError(Marker marker, String errorId, Object... data)
static SystemType getUnknown()
Definition: Type.java:80
Impressum