-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoxFunction.java
More file actions
38 lines (31 loc) · 937 Bytes
/
Copy pathLoxFunction.java
File metadata and controls
38 lines (31 loc) · 937 Bytes
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
package com.interpreter.lox;
import java.util.List;
class LoxFunction implements LoxCallable {
private final Stmt.Function declaration;
private final Environment closure;
LoxFunction(Stmt.Function declaration, Environment closure) {
this.closure = closure;
this.declaration = declaration;
}
@Override
public Object call(Interpreter interpreter, List<Object> arguments) {
Environment environment = new Environment(closure);
for (int i = 0; i < declaration.params.size(); i++) {
environment.define(declaration.params.get(i).lexeme, arguments.get(i));
}
try {
interpreter.executeBlock(declaration.body, environment);
} catch (Return returnValue) {
return returnValue.value;
}
return null;
}
@Override
public int arity() {
return declaration.params.size();
}
@Override
public String toString() {
return "<fn " + declaration.name.lexeme + ">";
}
}