Skip to content

Latest commit

 

History

History
402 lines (332 loc) · 10.4 KB

File metadata and controls

402 lines (332 loc) · 10.4 KB

OMNIcode Standalone - Architecture Documentation

System Overview

The standalone OMNIcode executable is a complete self-hosting compiler and interpreter written in Rust, with zero external dependencies beyond libc.

Component Architecture

1. Lexer (omnimcode-core/src/parser.rs - Lines 60-330)

Purpose: Convert raw source code into tokens

Key Features:

  • Character-by-character scanning
  • Multi-character operators (==, !=, ->, etc.)
  • String literal handling with escape sequences
  • Numeric parsing (integers and floats)
  • Identifier/keyword classification
  • Comment skipping

Token Types:

pub enum Token {
    // Keywords
    Harmonic, If, Else, While, For, Fn, Return, ...
    
    // Operators
    Plus, Minus, Star, Slash, EqEq, Lt, And, Or, ...
    
    // Literals
    Number(i64), Float(f64), String(String), Ident(String)
    
    // Delimiters
    LParen, RParen, LBrace, RBrace, LBracket, RBracket, ...
}

Performance: O(n) where n = source length, single pass

2. Parser (omnimcode-core/src/parser.rs - Lines 330-850)

Purpose: Convert token stream into Abstract Syntax Tree (AST)

Algorithm: Recursive descent parser with operator precedence climbing

Precedence Levels (lowest to highest):

  1. Logical OR (or)
  2. Logical AND (and)
  3. Logical NOT (not)
  4. Comparison (==, !=, <, >, <=, >=)
  5. Addition/Subtraction (+, -)
  6. Multiplication/Division (*, /, %)
  7. Primary (literals, identifiers, function calls)

AST Structure:

pub enum Statement {
    Print(Expression),
    VarDecl { name, value, is_harmonic },
    Assignment { name, value },
    If { condition, then_body, elif_parts, else_body },
    While { condition, body },
    For { var, iterable, body },
    FunctionDef { name, params, body, return_type },
    // ... more statement types
}

pub enum Expression {
    Number(i64),
    String(String),
    Variable(String),
    Add(Box<Expression>, Box<Expression>),
    Call { name, args },
    // ... more expression types
}

Features:

  • Error recovery (meaningful error messages)
  • Support for nested structures (blocks, functions, arrays)
  • Harmonic operation support (res(), fold())

3. Interpreter (omnimcode-core/src/interpreter.rs - Lines 1-520)

Purpose: Execute AST statements and evaluate expressions

Design: Tree-walk interpreter with explicit scope management

Key Components:

Scope Management

pub struct Interpreter {
    globals: HashMap<String, Value>,
    functions: HashMap<String, (Vec<String>, Vec<Statement>)>,
    locals: Vec<HashMap<String, Value>>,  // Stack of scopes
    return_value: Option<Value>,
    break_flag: bool,
    continue_flag: bool,
}

Statement Execution

  • execute_stmt(): Route to appropriate handler
  • execute_block(): Execute multiple statements in sequence
  • Scope pushed/popped for function calls

Expression Evaluation

  • eval_expr(): Recursive descent through expression tree
  • Short-circuit evaluation for and/or
  • Automatic type coercion (int ↔ string ↔ bool)

Harmonic Operations:

Expression::Resonance(e) => {
    // Compute φ-alignment score (0-1)
    let value = eval_expr(e)?;
    // Result is HInt with resonance field
}

Expression::Fold(e) => {
    // Find nearest Fibonacci attractor
    let value = eval_expr(e)?;
    // Snap to [0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
}

4. Runtime Value Types (omnimcode-core/src/value.rs - Lines 1-240)

Purpose: Define core data types and operations

Harmonic Integer (HInt)

pub struct HInt {
    pub value: i64,           // Actual number
    pub resonance: f64,       // φ-alignment (0-1)
    pub him_score: f64,       // HIM encoding (0-1)
    pub is_singularity: bool, // Division-by-zero flag
}

Resonance Calculation (φ-mathematics):

For value N:
- Find nearest Fibonacci: F
- resonance = 1.0 - |N - F| / (|N| + 1)
- If N is Fibonacci: resonance = 1.0
- If N far from any Fibonacci: resonance → 0.0

Harmonic Integer Map (HIM):

him_score = frac(N * φ)
where frac(x) = x - floor(x)
Measures alignment with golden ratio

HArray (Collections)

pub struct HArray {
    pub items: Vec<Value>,
}

Supported Value Types

pub enum Value {
    HInt(HInt),
    String(String),
    Bool(bool),
    Array(HArray),
    Null,
}

5. Standard Library Functions

Built-in Math

  • fibonacci(n) → i64 (O(n))
  • is_fibonacci(x) → bool (O(1), array lookup)

String Functions (30+)

  • str_len(s) → HInt
  • str_concat(s1, s2) → String
  • str_uppercase(s) → String
  • str_lowercase(s) → String
  • str_reverse(s) → String
  • str_contains(s, substr) → HInt (1/0)
  • str_slice(s, start, end) → String
  • [And 23 more...]

Array Functions (35+)

  • arr_new(size, default) → HArray
  • arr_from_range(start, end) → HArray
  • arr_len(arr) → HInt
  • arr_get(arr, idx) → Value
  • arr_set(arr, idx, value) → void
  • arr_push(arr, value) → void (mutating)
  • arr_sum(arr) → HInt
  • arr_min(arr) → HInt
  • arr_max(arr) → HInt
  • [And 26 more...]

6. Entry Point (omnimcode-core/src/main.rs)

Modes:

  1. File Mode:

    ./standalone.omc program.omc
    • Read file
    • Parse
    • Execute
    • Exit
  2. REPL Mode:

    ./standalone.omc
    • Interactive prompt
    • Line-by-line parsing and execution
    • Persistent variable scope

Execution Flow

Input (program.omc)
       │
       ▼
   ┌────────┐
   │ LEXER  │  Tokenize
   └────────┘
       │
       ▼
  ┌──────────┐
  │ PARSER   │  Build AST
  └──────────┘
       │
       ▼
 ┌─────────────┐
 │ INTERPRETER │  Execute
 │  - Execute statements
 │  - Manage scopes
 │  - Call functions
 └─────────────┘
       │
       ▼
 ┌──────────────┐
 │ RUNTIME      │
 │  - HInt ops
 │  - φ-math
 │  - Stdlib
 └──────────────┘
       │
       ▼
    Output

Data Flow Example

Simple Program

h x = 89;
print(res(x));

Token Stream

[Harmonic, Ident("x"), Eq, Number(89), Semicolon,
 Print, LParen, Res, LParen, Ident("x"), RParen, RParen, Semicolon, Eof]

AST

Statement::VarDecl {
    name: "x",
    value: Expression::Number(89),
    is_harmonic: true
}
Statement::Print(
    Expression::Call {
        name: "res",
        args: [Expression::Variable("x")]
    }
)

Execution

  1. Create HInt(89) with computed resonance (~0.99 since 89 is Fibonacci)
  2. Store in scope as "x"
  3. Evaluate res(x) → calls HInt resonance computation
  4. Print result: HInt(99, φ=0.990, HIM=0.xxx)

Memory Model

Stack-Based Scopes

┌─────────────────────────────┐
│ Global Variables            │  (Persistent)
│ "global_var" → Value        │
└─────────────────────────────┘
         ▲
         │ (Function call)
┌─────────────────────────────┐
│ Function Scope Layer 1      │  (Temporary)
│ "param1" → Value            │
│ "local_var" → Value         │
└─────────────────────────────┘
         ▲
         │ (Nested function)
┌─────────────────────────────┐
│ Function Scope Layer 2      │  (Most temporary)
│ "nested_param" → Value      │
└─────────────────────────────┘

Variable Lookup (O(n) in scope depth)

  1. Check current scope (top of stack)
  2. Check parent scopes (down to global)
  3. Return first match
  4. Error if not found

Performance Characteristics

Operation Complexity Notes
Parse program O(n) Single-pass lexer/parser
Lookup variable O(d) d = scope depth, usually < 10
Array access O(1) Direct vector indexing
Array iteration O(m) m = array size
Function call O(1) Plus execution of body
Fibonacci calc O(n) Linear iteration
Resonance check O(16) Fixed 16 Fibonacci lookups
String concat O(n+m) n,m = string lengths

Error Handling

Compile-time (Parse Phase):

  • Invalid syntax → descriptive error message
  • Unknown keywords → error + expected token
  • Mismatched delimiters → error with context

Runtime (Execution Phase):

  • Undefined variable → error name
  • Type mismatch → automatic coercion or error
  • Array index out of bounds → error
  • Division by zero → Singularity (not crash)
  • Function not found → error

Optimization Strategies

  1. Lazy Evaluation: Short-circuit and/or
  2. Direct Dispatch: Function calls via HashMap
  3. Inline Operations: Simple ops don't call functions
  4. String Interning: Considered for future (not current)
  5. Native Compilation: Rust compiler applies LLVM optimizations

Future Extensibility

Adding New Built-in Function

  1. Define in omnimcode-core/src/interpreter.rs call_function() match block
  2. Add test case
  3. Recompile: cargo build --release

Adding New Language Feature

  1. Add AST node in omnimcode-core/src/ast.rs
  2. Add lexer token in omnimcode-core/src/parser.rs Token enum
  3. Add parser rule in Parser impl
  4. Add interpreter handler in Interpreter impl
  5. Test with .omc program

Adding New Value Type

  1. Define struct in omnimcode-core/src/value.rs
  2. Implement Display and conversion methods
  3. Add to Value enum
  4. Update interpreter matching

Comparison: Python vs Native

Aspect Python Native (Rust)
Parse + Execute 50-100ms < 1ms
Memory per HInt 200+ bytes 32 bytes
Startup Python init Instant
Distribution Needs Python Single binary
Speed Factor 50-100×

Thread Safety

Current Design: Single-threaded tree-walk interpreter

Future Threading:

  • Interpreter is NOT thread-safe
  • Each thread would need own Interpreter instance
  • Global state protected via Arc<Mutex<...>>

Architecture Version: 1.0
Last Updated: April 30, 2026
Total Lines of Code: ~5,868 (Rust)