Class LoxFunction

java.lang.Object
tech.ixor.calclox.LoxFunction
All Implemented Interfaces:
LoxCallable

public class LoxFunction extends Object implements LoxCallable
Runtime representation of a user-defined CalcLox function.

A function retains the environment in which it was declared, allowing its body to access captured variables when invoked later.

  • Field Details

    • declaration

      protected final Statement.Function declaration
      Parsed declaration containing the function name, parameters, and body.
    • closure

      protected final Environment closure
      The variable scope of the function.
  • Constructor Details

    • LoxFunction

      public LoxFunction(Statement.Function declaration, Environment closure)
      Creates a function closure.
      Parameters:
      declaration - parsed function declaration
      closure - declaration-time lexical environment
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • arity

      public int arity()
      Description copied from interface: LoxCallable
      Returns the required argument count.
      Specified by:
      arity in interface LoxCallable
      Returns:
      the exact number of arguments accepted by this callable
    • call

      public Object call(Interpreter interpreter, List<Object> arguments)
      Description copied from interface: LoxCallable
      Invokes the callable with already evaluated arguments.
      Specified by:
      call in interface LoxCallable
      Parameters:
      interpreter - active interpreter
      arguments - argument values in source order
      Returns:
      the call result, or null for CalcLox nil