Class REPLContext

java.lang.Object
org.daiitech.naftah.parser.DefaultContext
org.daiitech.naftah.parser.REPLContext

public class REPLContext extends DefaultContext
REPLContext extends DefaultContext to provide a managed context environment for REPL (Read-Eval-Print Loop) execution.

It maintains an eternal context shared across all instances and supports registering and deregistering contexts with inheritance of variables, functions, and parse tree execution states.

Author:
Chakib Daii
  • Field Details

    • ETERNAL_CONTEXT

      protected static final DefaultContext ETERNAL_CONTEXT
      The eternal, shared base context for all REPL executions.

      Initialized statically with empty parameters and arguments, and prepared for parse tree execution.

  • Constructor Details

    • REPLContext

      public REPLContext()
  • Method Details

    • registerContext

      public static DefaultContext registerContext()
      Registers a new context using the eternal context as the parent.

      The new context is prepared for parse tree execution before returning.

      Returns:
      a new DefaultContext instance with ETERNAL_CONTEXT as parent
    • registerContext

      public static DefaultContext registerContext(Map<String,DeclaredParameter> parameters, Map<String,Object> arguments)
      Registers a new context with specified parameters and arguments.

      The new context uses the eternal context as its parent. It is prepared for parse tree execution before returning.

      Parameters:
      parameters - the declared parameters for the new context
      arguments - the argument values for the new context
      Returns:
      a new DefaultContext initialized with given parameters and arguments
    • registerContext

      public static DefaultContext registerContext(DefaultContext parent)
      Registers a new DefaultContext with the specified parent context.

      The new context inherits configuration and state from the parent context, and is automatically prepared for parse tree execution before being returned.

      Parameters:
      parent - the parent DefaultContext of the new context; may be null
      Returns:
      a new DefaultContext instance linked to the specified parent
    • registerContext

      public static DefaultContext registerContext(DefaultContext parent, Map<String,String> blockImports)
      Registers a new DefaultContext with the specified parent context and an initial set of block imports.

      The new context inherits its parent’s configuration and is initialized with the provided blockImports mapping. It is automatically prepared for parse tree execution before being returned.

      Parameters:
      parent - the parent DefaultContext of the new context; may be null
      blockImports - a map of block-level imports to associate with the new context; may be null
      Returns:
      a new DefaultContext instance configured with the specified parent and block imports
    • registerContext

      public static DefaultContext registerContext(DefaultContext parent, Map<String,String> blockImports, Map<String,DeclaredParameter> parameters, Map<String,Object> arguments)
      Registers a fully configured DefaultContext with the specified parent context, block imports, declared parameters, and argument values.

      This method provides the most flexible initialization for new contexts. The created context inherits from its parent, applies the given block imports, parameters, and argument mappings, and is automatically prepared for parse tree execution before being returned.

      Parameters:
      parent - the parent DefaultContext of the new context; may be null
      blockImports - a map of block-level imports to associate with the new context; may be null
      parameters - a map of declared parameters to register with the new context; may be null
      arguments - a map of argument values corresponding to the declared parameters; may be null
      Returns:
      a new DefaultContext instance initialized with the specified configuration
    • clear

      public static void clear()
      Clears all REPL-related contexts.

      This method performs two main actions:

      1. Calls DefaultContext.clear() to remove all globally stored contexts.
      2. Invokes DefaultContext.cleanThreadLocals() to reset any thread-local data associated with the REPL, preventing leakage across sessions.
      After calling this method, the REPL state is fully reset and ready for a fresh session.

    • getImports

      public static Collection<String> getImports()
      Returns all currently imported elements in the REPL session.

      Each import is returned as a string in the format: <imported_element> تحت_إسم <alias> (meaning "under the name").

      Returns:
      a collection of strings representing all imports and their aliases
    • dropImports

      public static void dropImports(String[] ids)
      Removes the specified imports from the REPL session.
      Parameters:
      ids - an array of import identifiers to remove; whitespace is trimmed
    • getVariables

      public static Collection<DeclaredVariable> getVariables()
      Returns all declared variables in the current REPL session.
      Returns:
      a collection of DeclaredVariable objects
    • dropVariables

      public static void dropVariables(String[] ids)
      Removes the specified variables from the current REPL session.
      Parameters:
      ids - an array of variable names to remove; whitespace is trimmed
    • getFunctions

      public static Collection<DeclaredFunction<?>> getFunctions()
      Returns all declared functions in the current REPL session.
      Returns:
      a collection of DeclaredFunction objects
    • dropFunctions

      public static void dropFunctions(String[] ids)
      Removes the specified functions from the current REPL session.
      Parameters:
      ids - an array of function names to remove; whitespace is trimmed
    • getImplementations

      public static Collection<DeclaredImplementation> getImplementations()
      Returns all declared implementations (behaviors) in the current REPL session.
      Returns:
      a collection of DeclaredImplementation objects
    • dropImplementations

      public static void dropImplementations(String[] ids)
      Removes the specified implementations (behaviors) from the current REPL session.
      Parameters:
      ids - an array of implementation names to remove; whitespace is trimmed