content: package lox;
  
  import java.util.List;
  
  class LoxFunction implements LoxCallable {
      private final Stmt.Function declaration;
      private final Environment closure;
      // TODO: The local vars overrides the arguments if they have the same name
      // should that be an error or should that be the normal behaviour ? i guess it's ok as it is
  
      LoxFunction(Stmt.Function 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 " + declaration.name.lexeme + ">";
      }
  }