Package org.daiitech.naftah.builtin.lang
Class DeclaredVariable
java.lang.Object
org.daiitech.naftah.builtin.lang.Declaration
org.daiitech.naftah.builtin.lang.DeclaredVariable
Represents a variable declared in the Naftah scripting language.
A declared variable has a name, type, constant modifier, default value, and may hold a current value if updated during execution.
Attempting to modify a constant variable will result in a NaftahBugError.
This class also tracks the original parsing context for reference or debugging.
- Author:
- Chakib Daii
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final booleanWhether the variable is declared as constant.private ObjectThe current value assigned to the variable (if updated).private final ObjectThe default value assigned to the variable at declaration.private final StringThe name of the variable.private org.antlr.v4.runtime.ParserRuleContextThe original parse context where this variable was declared.private final JavaTypeThe Java class representing the type of the variable.private booleanWhether the current value has been explicitly set.Fields inherited from class org.daiitech.naftah.builtin.lang.Declaration
depth -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateDeclaredVariable(int depth, org.antlr.v4.runtime.ParserRuleContext originalContext, String name, boolean constant, JavaType type, Object defaultValue) Constructs a new declared variable with the given properties. -
Method Summary
Modifier and TypeMethodDescriptionbooleanCompares thisDeclaredVariableto another object for equality.Returns the default value of the variable.getName()Returns the name of the variable.org.antlr.v4.runtime.ParserRuleContextReturns the original parser context of this variable declaration.getType()Returns the type of the variable.getValue()Returns the current value of the variable if updated; otherwise returns the default value.inthashCode()Computes the hash code for thisDeclaredVariable.booleanIndicates whether the variable is a constant.static DeclaredVariableof(int depth, org.antlr.v4.runtime.ParserRuleContext originalContext, String name, boolean constant, JavaType type, Object defaultValue) Factory method to create aDeclaredVariableinstance.voidsetOriginalContext(org.antlr.v4.runtime.ParserRuleContext originalContext) Sets the original parser context of this variable declaration.voidUpdates the current value of the variable.toString()Returns a string representation of the variable using helper formatting.Methods inherited from class org.daiitech.naftah.builtin.lang.Declaration
getDepth
-
Field Details
-
name
The name of the variable. -
constant
private final boolean constantWhether the variable is declared as constant. -
type
The Java class representing the type of the variable. -
defaultValue
The default value assigned to the variable at declaration. -
originalContext
private org.antlr.v4.runtime.ParserRuleContext originalContextThe original parse context where this variable was declared. -
currentValue
The current value assigned to the variable (if updated). -
updatedCurrentValue
private boolean updatedCurrentValueWhether the current value has been explicitly set.
-
-
Constructor Details
-
DeclaredVariable
private DeclaredVariable(int depth, org.antlr.v4.runtime.ParserRuleContext originalContext, String name, boolean constant, JavaType type, Object defaultValue) Constructs a new declared variable with the given properties.- Parameters:
depth- the depth of context where declaredoriginalContext- the original parser context of the declarationname- the name of the variableconstant- whether the variable is a constanttype- the type of the variabledefaultValue- the default value of the variable
-
-
Method Details
-
of
public static DeclaredVariable of(int depth, org.antlr.v4.runtime.ParserRuleContext originalContext, String name, boolean constant, JavaType type, Object defaultValue) Factory method to create aDeclaredVariableinstance.- Parameters:
depth- the depth of context where declaredoriginalContext- the original parser contextname- the variable nameconstant- whether the variable is constanttype- the variable typedefaultValue- the default value- Returns:
- a new
DeclaredVariableinstance
-
getOriginalContext
public org.antlr.v4.runtime.ParserRuleContext getOriginalContext()Returns the original parser context of this variable declaration.- Returns:
- the original context
-
setOriginalContext
public void setOriginalContext(org.antlr.v4.runtime.ParserRuleContext originalContext) Sets the original parser context of this variable declaration.- Parameters:
originalContext- the parser context to set
-
getName
Returns the name of the variable.- Returns:
- the variable name
-
isConstant
public boolean isConstant()Indicates whether the variable is a constant.- Returns:
- true if constant; false otherwise
-
getType
Returns the type of the variable.- Returns:
- the variable type
-
getDefaultValue
Returns the default value of the variable.- Returns:
- the default value
-
getValue
Returns the current value of the variable if updated; otherwise returns the default value.- Returns:
- the effective value of the variable
-
setValue
Updates the current value of the variable.Throws an error if the variable is constant.
- Parameters:
currentValue- the value to set- Throws:
NaftahBugError- if attempting to modify a constant variable
-
equals
Compares thisDeclaredVariableto another object for equality.Two
DeclaredVariableinstances are considered equal if and only if:- They are of the same runtime class, and
- They have equal values for all of the following properties:
name,constant,type,defaultValue,currentValue, andupdatedCurrentValue.
-
hashCode
public int hashCode()Computes the hash code for thisDeclaredVariable.The hash code is based on the same set of fields used in
equals(Object)to ensure consistency between equality and hashing (as required by the Java specification). -
toString
Returns a string representation of the variable using helper formatting.
-