Returning from calls 10.5.1
This commit is contained in:
parent
10cc5e6e89
commit
7f4946f1e9
10 changed files with 196 additions and 23 deletions
28
src/jlox/loxfunction.d
Normal file
28
src/jlox/loxfunction.d
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
module jlox.loxfunction;
|
||||
|
||||
import std.conv;
|
||||
|
||||
import jlox.token;
|
||||
import jlox.stmt;
|
||||
import jlox.interpreter;
|
||||
import jlox.environment;
|
||||
import jlox.util;
|
||||
|
||||
class LoxFunction : LoxCallable{
|
||||
private Stmt.Function declaration;
|
||||
mixin defaultCtor;
|
||||
|
||||
int arity() => declaration.params.length.to!int;
|
||||
TValue call(Interpreter interpreter, TValue[] arguments){
|
||||
Environment environment = new Environment(interpreter.globals);
|
||||
foreach(i; 0 .. declaration.params.length)
|
||||
environment.define(declaration.params[i].lexeme, arguments[i]);
|
||||
try{
|
||||
interpreter.executeBlock(declaration.body, environment);
|
||||
} catch(Return returnValue){
|
||||
return returnValue.value;
|
||||
}
|
||||
return TValue.nil(tvalueNil);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue