-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeinInstance.java
More file actions
47 lines (40 loc) · 1.05 KB
/
FeinInstance.java
File metadata and controls
47 lines (40 loc) · 1.05 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
import java.util.HashMap;
import java.util.Map;
/**
* Class for FeinInstance runtime representation of lox
*/
public class FeinInstance {
private FeinClass klass;
private final Map<String, Object> fields = new HashMap<>();
FeinInstance(FeinClass klass){
this.klass = klass;
}
/**
* Method to get property from instance
*
* @param name Token
*
* @return Object
*/
Object get(Token name) {
if(fields.containsKey(name.lexeme)) {
return fields.get(name.lexeme);
}
FeinFunction method = klass.findMethod(name.lexeme);
if(method != null) return method.bind(this);
throw new RuntimeError(name, "Undefined property '" + name.lexeme + "'.");
}
/**
* Method to set the value to the field in the instance
*
* @param name Token
* @param value Object
*/
void set(Token name, Object value) {
fields.put(name.lexeme, value);
}
@Override
public String toString() {
return klass.name + " instance";
}
}