Class Naftah

java.lang.Object
org.daiitech.naftah.Naftah

public final class Naftah extends Object
Main entry point of the Naftah programming language.

Naftah is an interpreted JVM language. This class handles command line argument parsing and execution of different commands such as running scripts, initializing classpath scanning, and starting an interactive shell (REPL).

Supports debug mode, encoding options, and classpath scanning configuration.

The supported file extensions for Naftah scripts are: ".naftah", ".nfth", ".na", ".nsh".

Author:
Chakib Daii
  • Field Details

    • JAVA_VERSION_PROPERTY

      public static final String JAVA_VERSION_PROPERTY
      System property key for the Java version.
      See Also:
    • JAVA_VM_VENDOR_PROPERTY

      public static final String JAVA_VM_VENDOR_PROPERTY
      System property key for the Java Virtual Machine vendor.
      See Also:
    • SCAN_CLASSPATH_PROPERTY

      public static final String SCAN_CLASSPATH_PROPERTY
      Property to enable scanning the Java classpath for Naftah types.
      See Also:
    • FORCE_CLASSPATH_PROPERTY

      public static final String FORCE_CLASSPATH_PROPERTY
      Property to force scanning the Java classpath.
      See Also:
    • INCLUDE_ALL_IN_COMPLETIONS_PROPERTY

      public static final String INCLUDE_ALL_IN_COMPLETIONS_PROPERTY
      Property to include jvm functions and instantiatable classes in repl completions, disabled by default.
      See Also:
    • SCAN_JDK_PROPERTY

      public static final String SCAN_JDK_PROPERTY
      Property to enable scanning the Jdk classes for Naftah types.
      See Also:
    • CACHE_SCANNING_RESULTS_PROPERTY

      public static final String CACHE_SCANNING_RESULTS_PROPERTY
      Cache results of classpath and JDK scanning.
      See Also:
    • DEBUG_PROPERTY

      public static final String DEBUG_PROPERTY
      Property to enable debug mode.
      See Also:
    • INSIDE_REPL_PROPERTY

      public static final String INSIDE_REPL_PROPERTY
      Property set when inside the REPL (shell).
      See Also:
    • INSIDE_INIT_PROPERTY

      public static final String INSIDE_INIT_PROPERTY
      Property set when inside the init command.
      See Also:
    • INSIDE_MAN_PROPERTY

      public static final String INSIDE_MAN_PROPERTY
      Property set when inside the man command.
      See Also:
    • LOAD_CLASSES_AND_FUNCTIONS_PROPERTY

      public static final String LOAD_CLASSES_AND_FUNCTIONS_PROPERTY
      Controls whether class and function definitions should be loaded on startup.
      See Also:
    • INSIDE_RUN_PROPERTY

      public static final String INSIDE_RUN_PROPERTY
      Property set when inside the run command.
      See Also:
    • FILE_ENCODING_PROPERTY

      public static final String FILE_ENCODING_PROPERTY
      Property key for file encoding.
      See Also:
    • VECTOR_API_PROPERTY

      public static final String VECTOR_API_PROPERTY
      Property to enable Vector API optimizations.
      See Also:
    • NUMBER_FORMATTER_PROPERTY

      public static final String NUMBER_FORMATTER_PROPERTY
      Property to enable number formatter in Naftah.
      See Also:
    • ARABIC_INDIC_PROPERTY

      public static final String ARABIC_INDIC_PROPERTY
      Property to enable Arabic-Indic digit formatting in Naftah.
      See Also:
    • MULTILINE_CACHE_PROPERTY

      public static final String MULTILINE_CACHE_PROPERTY
      Property to enable caching of multiline text processing in Naftah.
      See Also:
    • WORD_CHUNK_PROPERTY

      public static final String WORD_CHUNK_PROPERTY
      Property to enable chunks of long words in text processing in Naftah.
      See Also:
    • INTERPOLATION_CACHE_PROPERTY

      public static final String INTERPOLATION_CACHE_PROPERTY
      Property to enable caching of string interpolation text processing in Naftah.
      See Also:
    • CONFIG_FILE_PROPERTY

      public static final String CONFIG_FILE_PROPERTY
      Property to specify the path to a Naftah configuration file.
      See Also:
    • BUILTIN_CLASSES_PROPERTY

      public static final String BUILTIN_CLASSES_PROPERTY
      Property to specify which builtin function set to use in Naftah.
      See Also:
    • BUILTIN_PACKAGES_PROPERTY

      public static final String BUILTIN_PACKAGES_PROPERTY
      Property to specify which package of builtin function set to use in Naftah.
      See Also:
    • JAVA_OBJECT_REFLECT_ACTIVE_PROPERTY

      public static final String JAVA_OBJECT_REFLECT_ACTIVE_PROPERTY
      System property enabling or disabling deep reflection for Java object mapping.
      See Also:
    • JAVA_OBJECT_REFLECT_MAX_DEPTH_PROPERTY

      public static final String JAVA_OBJECT_REFLECT_MAX_DEPTH_PROPERTY
      System property specifying the maximum depth allowed during deep reflection.
      See Also:
    • JAVA_OBJECT_REFLECT_SKIP_NULLS_PROPERTY

      public static final String JAVA_OBJECT_REFLECT_SKIP_NULLS_PROPERTY
      System property controlling whether null fields are skipped during reflection.
      See Also:
    • BUILTIN_CLASSES

      public static final String BUILTIN_CLASSES
      Configuration key representing the classes of builtin function set.
      See Also:
    • BUILTIN_PACKAGES

      public static final String BUILTIN_PACKAGES
      Configuration key representing the packages of builtin function set.
      See Also:
    • CONFIG_FILE

      public static final String CONFIG_FILE
      Default filename for the Naftah configuration file.
      See Also:
    • STANDARD_EXTENSIONS

      public static final String[] STANDARD_EXTENSIONS
      The recognized standard file extensions for Naftah scripts.
    • UNDERSCORE

      public static final String UNDERSCORE
      Constant representing a single underscore character ("_").
      See Also:
    • LOGGER

      private static Logger LOGGER
      Logger instance for logging Naftah program.
    • args

      private List<String> args
    • isScriptFile

      private boolean isScriptFile
    • script

      private String script
    • debug

      private boolean debug
  • Constructor Details

    • Naftah

      private Naftah()
      Private constructor to prevent instantiation.
  • Method Details

    • main

      public static void main(String[] args)
      Main method of the Naftah interpreter. Parses command line arguments and dispatches commands.
      Parameters:
      args - the command line arguments
    • initLogger

      private static void initLogger(boolean debug)
      Initializes the Java Util Logging configuration. If debug is true, logging levels are set to verbose.
      Parameters:
      debug - true to enable debug logging, false otherwise
    • processConfig

      private static void processConfig(Properties properties)
      Processes the given configuration properties and updates corresponding system properties for built-in function classes and packages.

      If the provided properties include entries for BUILTIN_CLASSES or BUILTIN_PACKAGES, their values are appended to the existing system properties BUILTIN_CLASSES_PROPERTY and BUILTIN_PACKAGES_PROPERTY, respectively. This behavior ensures that built-in configurations are cumulative—existing values are preserved and new ones are appended.

      Parameters:
      properties - the configuration properties to process; may be null
    • initConfig

      public static void initConfig()
      Initializes Naftah configuration by loading default and optional external property files.

      The initialization process follows these steps:

      1. Load default configuration properties from the resource file CONFIG_FILE.
      2. Apply built-in configuration values via processConfig(Properties).
      3. If a system property CONFIG_FILE_PROPERTY is defined, attempt to load external configuration from the specified file. External values override defaults.
      4. If loading fails (e.g., due to NaftahBugError), fall back to default properties.

      All successfully processed properties update system properties, including BUILTIN_CLASSES_PROPERTY and BUILTIN_PACKAGES_PROPERTY.

    • waitForUserInterruption

      private static void waitForUserInterruption()
      Blocks until the user presses Ctrl+C, using the REPL terminal and line reader.

      This is a convenient wrapper around REPLHelper.waitForUserInterruption(LineReader), automatically obtaining the terminal and reader.

      All other input is ignored; the user sees a friendly Arabic message and can exit gracefully.

    • processArgs

      public static void processArgs(String[] args)
      Processes the command line arguments and dispatches the appropriate command.
      Parameters:
      args - the raw command line arguments
    • printPaddedToString

      private static void printPaddedToString(Object o)
      Prints the string representation of the given object with padding applied.
      Parameters:
      o - the object to convert to a string and print with padding
    • printPaddedErrorMessageToString

      public static void printPaddedErrorMessageToString(Throwable t)
      Prints the error message of the given throwable with padding applied. If the throwable is not an instance of NaftahBugError, it will be wrapped inside a NaftahBugError. The error message is formatted in Arabic script as "تم التقاط الخطأ: 'message'".
      Parameters:
      t - the throwable whose error message will be printed with padding
    • run

      private boolean run(Naftah.NaftahCommand naftahCommand)
      Runs the given NaftahCommand.
      Parameters:
      naftahCommand - the command to run
      Returns:
      true if successful, false otherwise