1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
import java.util.*;
import st.*;
/**
* A small method to test the basic functionality
* of the st Instance library
*/
public class Playground {
public static String HashMapToString(Map<String, AbstractInstance> map) {
StringBuilder mapAsString = new StringBuilder("{");
for (String key : map.keySet()) {
mapAsString.append(key + "=" + map.get(key) + ", ");
}
mapAsString.delete(mapAsString.length()-2, mapAsString.length()).append("}");
return mapAsString.toString();
}
public static void main(String[] args) {
HashMap<String,AbstractInstance> symt = new HashMap<>();
symt.put("x", new TypeInstance("x", TypeEnum.integer));
symt.put("a", new MethodInstance("a", TypeEnum.intarray));
symt.put("A", new ClassInstance("A", new ClassInstance("B")));
System.out.println("Our symbol table has lots of symbols: " + HashMapToString(symt));
System.out.println("It seems class A extends B, but B isn't in the table... " +
((ClassInstance) symt.get("A")).getExtend());
SymbolTable ssymt = new SymbolTable();
ssymt.put("x", new TypeInstance("x", TypeEnum.integer));
ssymt.put("A", new ClassInstance("A"));
System.out.println("A smarter approach. Here's type 'x':" +
ssymt.getType("x"));
System.out.println("Null class 'x':" +
ssymt.getClass("x"));
System.out.println("Null method 'x':" +
ssymt.getMethod("x"));
System.out.println("Null type 'y':" +
ssymt.getType("y"));
System.out.println("Null type 'A':" +
ssymt.getType("A"));
}
}
|