1 using System;
2 using System.Collections;
3
4 namespace Calculator
5 {
6 public class SymbolTable
7 {
8 static SymbolTable() : instance(new SymbolTable())
9 {
10 }
11 public static SymbolTable& Instance()
12 {
13 return *instance;
14 }
15 private SymbolTable() : variableMap()
16 {
17 }
18 public void SetVariable(const ustring& variableName, double variableValue)
19 {
20 variableMap[variableName] = variableValue;
21 }
22 public double GetVariableValue(const ustring& variableName)
23 {
24 Map<ustring, double>.ConstIterator it = variableMap.CFind(variableName);
25 if (it != variableMap.CEnd())
26 {
27 double value = it->second;
28 return value;
29 }
30 else
31 {
32 throw VariableNotFoundException("variable '" + ToUtf8(variableName) + "' not found");
33 }
34 }
35 public void Print()
36 {
37 Console.WriteLine("variables:");
38 for (const Pair<ustring, double>& pair : variableMap)
39 {
40 Console.Out() << pair.first << " = " << pair.second << endl();
41 }
42 }
43 private static UniquePtr<SymbolTable> instance;
44 private Map<ustring, double> variableMap;
45 }
46 }