Package tech.ixor.calclox
Class Environment
java.lang.Object
tech.ixor.calclox.Environment
Stores variables for one lexical scope in a CalcLox execution.
Environments form an enclosing chain. Normal lookup and assignment walk that chain, while resolved lookup uses a precomputed lexical distance.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a global environment with no enclosing scope.Environment(Environment enclosing) Creates a nested lexical environment. -
Method Summary
Modifier and TypeMethodDescriptionancestor(int distance) Returns an enclosing environment at an exact lexical distance.voidUpdates the nearest existing declaration in the environment chain.voidUpdates a resolved local value without performing a name search.voidDeclares or replaces a value directly in this scope.Looks up a variable in this scope or an enclosing scope.Returns a resolved local value without performing a name search.Returns the values declared directly in this scope.toString()
-
Constructor Details
-
Environment
public Environment()Creates a global environment with no enclosing scope. -
Environment
Creates a nested lexical environment.- Parameters:
enclosing- immediately enclosing scope
-
-
Method Details
-
getValues
Returns the values declared directly in this scope.- Returns:
- the live, mutable scope map
-
get
Looks up a variable in this scope or an enclosing scope.- Parameters:
name- variable token used for lookup and error location- Returns:
- stored value, including
null - Throws:
RuntimeError- if the variable is undefined
-
assign
Updates the nearest existing declaration in the environment chain.- Parameters:
name- variable token used for lookup and error locationvalue- replacement value- Throws:
RuntimeError- if the variable is undefined
-
define
Declares or replaces a value directly in this scope.- Parameters:
name- variable namevalue- initial value
-
ancestor
Returns an enclosing environment at an exact lexical distance.- Parameters:
distance- number of enclosing links to traverse; zero returns this environment- Returns:
- resolved ancestor
-
getAt
Returns a resolved local value without performing a name search.- Parameters:
distance- lexical distance to the declaring environmentname- variable name- Returns:
- stored value, including
null
-
assignAt
Updates a resolved local value without performing a name search.- Parameters:
distance- lexical distance to the declaring environmentname- variable tokenvalue- replacement value
-
toString
-