Repository navigation
Expand file tree
/
Copy pathScope.java
More file actions
143 lines (122 loc) · 3.81 KB
/
Copy pathScope.java
File metadata and controls
143 lines (122 loc) · 3.81 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package play;
import java.util.LinkedList;
import java.util.List;
public abstract class Scope extends Symbol{
// 该Scope中的成员,包括变量、方法、类等。
protected List<Symbol> symbols = new LinkedList<Symbol>();
/**
* 向scope中添加符号,同时设置好该符号的enclosingScope
* @param symbol
*/
protected void addSymbol(Symbol symbol){
symbols.add(symbol);
symbol.enclosingScope = this;
}
/**
* 是否包含某个Variable
* @param name
* @return
*/
protected Variable getVariable(String name){
return getVariable(this,name);
}
protected static Variable getVariable(Scope scope, String name){
for (Symbol s : scope.symbols) {
if (s instanceof Variable && s.name.equals(name)){
return (Variable) s;
}
}
return null;
}
/**
* 是否包含某个Function
* @param name
* @param paramTypes 参数类型。不允许为空。该参数不允许为空。如果没有参数,需要传入一个0长度的列表。
* @return
*/
protected Function getFunction(String name, List<Type> paramTypes){
return getFunction(this,name,paramTypes);
}
/**
* 是否包含某个Function。这是个静态方法,可以做为工具方法重用。避免类里要超找父类的情况。
* @param scope
* @param name
* @param paramTypes
* @return
*/
protected static Function getFunction(Scope scope, String name, List<Type> paramTypes){
Function rtn = null;
for (Symbol s : scope.symbols) {
if (s instanceof Function && s.name.equals(name)) {
Function function = (Function) s;
if (function.matchParameterTypes(paramTypes)){
rtn = function;
break;
}
}
}
return rtn;
}
/**
* 获取一个函数类型的变量,能匹配相应的参数类型
* @param name
* @param paramTypes
* @return
*/
protected Variable getFunctionVariable(String name, List<Type> paramTypes){
return getFunctionVariable(this,name,paramTypes);
}
protected static Variable getFunctionVariable(Scope scope, String name, List<Type> paramTypes){
Variable rtn = null;
for (Symbol s : scope.symbols) {
if (s instanceof Variable && ((Variable) s).type instanceof FunctionType && s.name.equals(name)) {
Variable v = (Variable) s;
FunctionType functionType = (FunctionType) v.type;
if (functionType.matchParameterTypes(paramTypes)){
rtn = v;
break;
}
}
}
return rtn;
}
/**
* 是否包含某个Class
* @param name
* @return
*/
protected Class getClass(String name){
return getClass(this,name);
}
protected static Class getClass(Scope scope, String name){
for (Symbol s : scope.symbols) {
if (s instanceof Class && s.name.equals(name)){
return (Class) s;
}
}
return null;
}
/**
* 是否包含某个Symbol
* @param symbol
* @return
*/
protected boolean containsSymbol(Symbol symbol){
return symbols.contains(symbol);
}
// /**
// * 是否能在该Scope中访问这个symbol,包括层层的enclosingScope
// */
// protected boolean accessible(Symbol symbol){
// boolean rtn = false;
// rtn = containsSymbol(symbol);
// if (!rtn && enclosingScope!=null){
// rtn = enclosingScope.accessible(symbol);
// }
// return rtn;
// }
@Override
public String toString(){
return "Scope: " + name;
}
}