Class DeclaredVariable

java.lang.Object
org.daiitech.naftah.builtin.lang.Declaration
org.daiitech.naftah.builtin.lang.DeclaredVariable

public final class DeclaredVariable extends Declaration
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

    Fields
    Modifier and Type
    Field
    Description
    private final boolean
    Whether the variable is declared as constant.
    private Object
    The current value assigned to the variable (if updated).
    private final Object
    The default value assigned to the variable at declaration.
    private final String
    The name of the variable.
    private org.antlr.v4.runtime.ParserRuleContext
    The original parse context where this variable was declared.
    private final JavaType
    The Java class representing the type of the variable.
    private boolean
    Whether the current value has been explicitly set.

    Fields inherited from class org.daiitech.naftah.builtin.lang.Declaration

    depth
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    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.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Compares this DeclaredVariable to another object for equality.
    Returns the default value of the variable.
    Returns the name of the variable.
    org.antlr.v4.runtime.ParserRuleContext
    Returns the original parser context of this variable declaration.
    Returns the type of the variable.
    Returns the current value of the variable if updated; otherwise returns the default value.
    int
    Computes the hash code for this DeclaredVariable.
    boolean
    Indicates whether the variable is a constant.
    of(int depth, org.antlr.v4.runtime.ParserRuleContext originalContext, String name, boolean constant, JavaType type, Object defaultValue)
    Factory method to create a DeclaredVariable instance.
    void
    setOriginalContext(org.antlr.v4.runtime.ParserRuleContext originalContext)
    Sets the original parser context of this variable declaration.
    void
    setValue(Object currentValue)
    Updates the current value of the variable.
    Returns a string representation of the variable using helper formatting.

    Methods inherited from class org.daiitech.naftah.builtin.lang.Declaration

    getDepth

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • name

      private final String name
      The name of the variable.
    • constant

      private final boolean constant
      Whether the variable is declared as constant.
    • type

      private final JavaType type
      The Java class representing the type of the variable.
    • defaultValue

      private final Object defaultValue
      The default value assigned to the variable at declaration.
    • originalContext

      private org.antlr.v4.runtime.ParserRuleContext originalContext
      The original parse context where this variable was declared.
    • currentValue

      private Object currentValue
      The current value assigned to the variable (if updated).
    • updatedCurrentValue

      private boolean updatedCurrentValue
      Whether 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 declared
      originalContext - the original parser context of the declaration
      name - the name of the variable
      constant - whether the variable is a constant
      type - the type of the variable
      defaultValue - 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 a DeclaredVariable instance.
      Parameters:
      depth - the depth of context where declared
      originalContext - the original parser context
      name - the variable name
      constant - whether the variable is constant
      type - the variable type
      defaultValue - the default value
      Returns:
      a new DeclaredVariable instance
    • 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

      public String 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

      public JavaType getType()
      Returns the type of the variable.
      Returns:
      the variable type
    • getDefaultValue

      public Object getDefaultValue()
      Returns the default value of the variable.
      Returns:
      the default value
    • getValue

      public Object getValue()
      Returns the current value of the variable if updated; otherwise returns the default value.
      Returns:
      the effective value of the variable
    • setValue

      public void setValue(Object currentValue)
      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

      public boolean equals(Object o)
      Compares this DeclaredVariable to another object for equality.

      Two DeclaredVariable instances 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, and updatedCurrentValue.

      Overrides:
      equals in class Object
      Parameters:
      o - the object to compare with this instance
      Returns:
      true if the specified object is equal to this one; otherwise false
    • hashCode

      public int hashCode()
      Computes the hash code for this DeclaredVariable.

      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).

      Overrides:
      hashCode in class Object
      Returns:
      a hash code value for this object
    • toString

      public String toString()
      Returns a string representation of the variable using helper formatting.
      Overrides:
      toString in class Object
      Returns:
      a formatted string representing the variable