blob: 8c874603ab9d9ccaec5230b9862f46b3d02e3ed3 (
plain)
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
|
package com.craftinginterpreters.lox;
import java.util.List;
class LoxLambda implements LoxCallable {
private final Expr.Lambda declaration;
private final Environment closure;
LoxLambda(Expr.Lambda declaration, Environment closure) {
this.closure = closure;
this.declaration = declaration;
}
@Override
public int arity() {
return declaration.params.size();
}
@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 String toString() {
return "<fn anonymous>";
}
}
|