-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoxClass.java
50 lines (43 loc) · 1.27 KB
/
LoxClass.java
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
package SLox;
import java.util.List;
import java.util.Map;
class LoxClass extends LoxInstance implements LoxCallable {
final String name;
final LoxClass superclass;
private final Map<String, LoxFunction> methods;
LoxClass(String name, LoxClass superclass,Map<String, LoxFunction> methods) {
super(null);
this.name = name;
this.superclass = superclass;
this.methods = methods;
}
@Override
public Object call(Interpreter interpreter,
List<Object> arguments) {
LoxInstance instance = new LoxInstance(this);
LoxFunction initializer = findMethod("init");
if (initializer != null) {
initializer.bind(instance).call(interpreter, arguments);
}
return instance;
}
@Override
public int arity() {
LoxFunction initializer = findMethod("init");
if (initializer == null) return 0;
return initializer.arity();
}
@Override
public String toString() {
return name;
}
LoxFunction findMethod(String name) {
if (methods.containsKey(name)) {
return methods.get(name);
}
if (superclass != null) {
return superclass.findMethod(name);
}
return null;
}
}