Menu

CLI Tools and Utilities

Relevant source files

This document covers the command-line development tools and utilities provided by OwnLang for code analysis, optimization, debugging, and development workflow support. These tools are integrated into the main OwnLang executable and provide various modes for analyzing, formatting, and optimizing OwnLang programs.

For information about the interactive REPL environment, see Interactive REPL. For package management and project utilities, see Package Manager.

Command-Line Interface Structure

The OwnLang CLI provides a unified interface for accessing various development tools through command-line flags and options. The main entry point processes arguments and routes execution to the appropriate tool or execution mode.

The CLI argument parsing supports both short and long form options, with intelligent defaults and mode detection.

Sources: ownlang-desktop/src/main/java/com/annimon/ownlang/Main.java26-130

Code Analysis Tools

Beautifier

The beautifier tool reformats OwnLang source code according to consistent style guidelines. It processes the source through the lexical and parsing stages, then applies formatting rules to produce clean, readable code output.

Usage:

  • -b, --beautify: Format and output the beautified source code

The beautifier operates as a separate execution mode that bypasses normal program execution, instead outputting the formatted source code to standard output.

Implementation Flow:

Sources: ownlang-desktop/src/main/java/com/annimon/ownlang/Main.java40-128

Linter

The linter performs static analysis to identify potential bugs, style violations, and semantic issues in OwnLang code. It supports multiple analysis levels with different depths of checking.

Usage:

  • -l, --lint <mode>: Run linting with specified mode
    • none: Disable linting
    • semantic: Perform semantic analysis (default)
    • full: Comprehensive analysis including style checks

Linter Modes:

ModeDescriptionAnalysis Level
NONENo linting performedNone
SEMANTICSemantic validation and basic checksMedium
FULLComprehensive analysis with style validationHigh

The linter integrates into the standard execution pipeline as an optional stage, running after optimization but before execution.

Sources: ownlang-desktop/src/main/java/com/annimon/ownlang/Main.java74-84 ownlang-desktop/src/main/java/com/annimon/ownlang/RunOptions.java32-34

AST and Token Viewers

These diagnostic tools provide visibility into the internal representation of parsed OwnLang programs.

AST Viewer (-a, --showast):

  • Displays the Abstract Syntax Tree structure of the parsed program
  • Shows both original and optimized AST when optimization is enabled
  • Includes optimization summary information

Token Viewer (-t, --showtokens):

  • Displays the complete sequence of lexical tokens
  • Shows token types, values, and positions
  • Useful for debugging lexical analysis issues

Both tools output their information after program execution completes, making them suitable for post-execution analysis.

Sources: ownlang-desktop/src/main/java/com/annimon/ownlang/Main.java36-199

Optimization Tools

The optimization system provides configurable code optimization with multiple passes and detailed reporting capabilities. The optimizer applies various transformation techniques to improve program performance.

Optimization Pipeline

Usage:

  • -o, --optimize N: Enable optimization with N passes (0-9)
    • 0: No optimization
    • 1-8: Fixed number of optimization passes
    • 9: Run until convergence (no further optimizations possible)

Optimization Techniques

The optimization stage implements several transformation techniques:

TechniqueDescriptionImplementation
Constant FoldingEvaluate constant expressions at compile timeConstantFolding
Constant PropagationReplace variable references with known constant valuesConstantPropagation
Dead Code EliminationRemove unreachable or unused code sectionsDeadCodeElimination
Expression SimplificationSimplify complex expressions to equivalent simpler formsExpressionSimplification
Instruction CombiningMerge adjacent instructions where possibleInstructionCombining

The optimization process runs iteratively until either the maximum number of passes is reached or no further optimizations are possible.

Sources: ownlang-desktop/src/main/java/com/annimon/ownlang/Main.java55-67 ownlang-parser/src/main/java/com/annimon/ownlang/parser/optimization/OptimizationStage.java16-56

Development Utilities

Performance Measurement

The timing measurement utility provides detailed performance analysis of each stage in the OwnLang processing pipeline.

Usage:

  • -m, --showtime: Display elapsed time for parsing and execution stages

The measurement system uses TimeMeasurement with ScopedStageFactory to automatically track the duration of each processing stage, providing insights into performance bottlenecks.

Measurement Output:

  • Individual stage timings
  • Total execution time
  • Summary formatted in milliseconds

Sources: ownlang-desktop/src/main/java/com/annimon/ownlang/Main.java51-203

Sandbox Mode

The sandbox provides an isolated execution environment for testing and experimentation with OwnLang programs.

Usage:

  • --sandbox: Launch OwnLang in sandbox mode

The sandbox mode delegates to Sandbox.main() with forwarded command-line arguments, providing a controlled environment for program execution with restricted system access.

Sources: ownlang-desktop/src/main/java/com/annimon/ownlang/Main.java95-97

Script Execution

The CLI provides built-in script execution capabilities for common development tasks.

Usage:

  • run <scriptName>: Execute named script from the scripts resource directory
  • run (without arguments): List available scripts

Scripts are loaded from the /scripts/ resource directory with automatic .own extension resolution. This feature enables quick access to utility and example programs bundled with the OwnLang distribution.

Sources: ownlang-desktop/src/main/java/com/annimon/ownlang/Main.java99-112 ownlang-desktop/src/main/java/com/annimon/ownlang/RunOptions.java25-30

Tool Integration Pipeline

The CLI tools integrate seamlessly into the OwnLang execution pipeline through the stage-based architecture. Each tool operates as either a separate execution mode or as an optional stage in the main pipeline.

Pipeline Integration:

The scoped stage factory (ScopedStageFactory) provides automatic timing measurement for each stage when the timing option is enabled, creating a transparent performance monitoring layer throughout the execution pipeline.

Sources: ownlang-desktop/src/main/java/com/annimon/ownlang/Main.java160-205