aboutsummaryrefslogtreecommitdiffstats
path: root/jlox/src/com/craftinginterpreters/lox/LoxFunction.java
diff options
context:
space:
mode:
authorGravatar Tom Willemse2021-07-08 00:14:31 -0700
committerGravatar Tom Willemse2021-07-08 02:25:13 -0700
commit68a2ebd34fc94488e89ffb82b359ec6e7e152ae9 (patch)
tree0dbbe26aff8fc38805b4b3780ae7842c433b9522 /jlox/src/com/craftinginterpreters/lox/LoxFunction.java
parent62bd0f83dc909547a69abb8b0aed40cf098b4c95 (diff)
downloadcrafting-interpreters-68a2ebd34fc94488e89ffb82b359ec6e7e152ae9.tar.gz
crafting-interpreters-68a2ebd34fc94488e89ffb82b359ec6e7e152ae9.zip
Restructure project to make room for clox
Diffstat (limited to 'jlox/src/com/craftinginterpreters/lox/LoxFunction.java')
-rw-r--r--jlox/src/com/craftinginterpreters/lox/LoxFunction.java52
1 files changed, 52 insertions, 0 deletions
diff --git a/jlox/src/com/craftinginterpreters/lox/LoxFunction.java b/jlox/src/com/craftinginterpreters/lox/LoxFunction.java
new file mode 100644
index 0000000..07dd727
--- /dev/null
+++ b/jlox/src/com/craftinginterpreters/lox/LoxFunction.java
@@ -0,0 +1,52 @@
+package com.craftinginterpreters.lox;
+
+import java.util.List;
+
+class LoxFunction implements LoxCallable {
+ private final Stmt.Function declaration;
+ private final Environment closure;
+ private final boolean isInitializer;
+
+ LoxFunction(Stmt.Function declaration, Environment closure, boolean isInitializer) {
+ this.isInitializer = isInitializer;
+ this.closure = closure;
+ this.declaration = declaration;
+ }
+
+ LoxFunction bind(LoxInstance instance) {
+ Environment environment = new Environment(closure);
+ environment.define("this", instance);
+ return new LoxFunction(declaration, environment, isInitializer);
+ }
+
+ @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) {
+ if (isInitializer)
+ return closure.getAt(0, "this");
+ return returnValue.value;
+ }
+
+ if (isInitializer)
+ return closure.getAt(0, "this");
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ return "<fn " + declaration.name.lexeme + ">";
+ }
+}