The standalone OMNIcode executable is a complete self-hosting compiler and interpreter written in Rust, with zero external dependencies beyond libc.
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
Purpose: Convert token stream into Abstract Syntax Tree (AST)
Algorithm: Recursive descent parser with operator precedence climbing
Precedence Levels (lowest to highest):
- Logical OR (
or) - Logical AND (
and) - Logical NOT (
not) - Comparison (
==,!=,<,>,<=,>=) - Addition/Subtraction (
+,-) - Multiplication/Division (
*,/,%) - 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())
Purpose: Execute AST statements and evaluate expressions
Design: Tree-walk interpreter with explicit scope management
Key Components:
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,
}execute_stmt(): Route to appropriate handlerexecute_block(): Execute multiple statements in sequence- Scope pushed/popped for function calls
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]
}Purpose: Define core data types and operations
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
pub struct HArray {
pub items: Vec<Value>,
}pub enum Value {
HInt(HInt),
String(String),
Bool(bool),
Array(HArray),
Null,
}fibonacci(n)→ i64 (O(n))is_fibonacci(x)→ bool (O(1), array lookup)
str_len(s)→ HIntstr_concat(s1, s2)→ Stringstr_uppercase(s)→ Stringstr_lowercase(s)→ Stringstr_reverse(s)→ Stringstr_contains(s, substr)→ HInt (1/0)str_slice(s, start, end)→ String- [And 23 more...]
arr_new(size, default)→ HArrayarr_from_range(start, end)→ HArrayarr_len(arr)→ HIntarr_get(arr, idx)→ Valuearr_set(arr, idx, value)→ voidarr_push(arr, value)→ void (mutating)arr_sum(arr)→ HIntarr_min(arr)→ HIntarr_max(arr)→ HInt- [And 26 more...]
Modes:
-
File Mode:
./standalone.omc program.omc
- Read file
- Parse
- Execute
- Exit
-
REPL Mode:
./standalone.omc
- Interactive prompt
- Line-by-line parsing and execution
- Persistent variable scope
Input (program.omc)
│
▼
┌────────┐
│ LEXER │ Tokenize
└────────┘
│
▼
┌──────────┐
│ PARSER │ Build AST
└──────────┘
│
▼
┌─────────────┐
│ INTERPRETER │ Execute
│ - Execute statements
│ - Manage scopes
│ - Call functions
└─────────────┘
│
▼
┌──────────────┐
│ RUNTIME │
│ - HInt ops
│ - φ-math
│ - Stdlib
└──────────────┘
│
▼
Output
h x = 89;
print(res(x));
[Harmonic, Ident("x"), Eq, Number(89), Semicolon,
Print, LParen, Res, LParen, Ident("x"), RParen, RParen, Semicolon, Eof]
Statement::VarDecl {
name: "x",
value: Expression::Number(89),
is_harmonic: true
}
Statement::Print(
Expression::Call {
name: "res",
args: [Expression::Variable("x")]
}
)
- Create HInt(89) with computed resonance (~0.99 since 89 is Fibonacci)
- Store in scope as "x"
- Evaluate
res(x)→ calls HInt resonance computation - Print result:
HInt(99, φ=0.990, HIM=0.xxx)
┌─────────────────────────────┐
│ 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 │
└─────────────────────────────┘
- Check current scope (top of stack)
- Check parent scopes (down to global)
- Return first match
- Error if not found
| 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 |
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
- Lazy Evaluation: Short-circuit
and/or - Direct Dispatch: Function calls via HashMap
- Inline Operations: Simple ops don't call functions
- String Interning: Considered for future (not current)
- Native Compilation: Rust compiler applies LLVM optimizations
- Define in
omnimcode-core/src/interpreter.rscall_function()match block - Add test case
- Recompile:
cargo build --release
- Add AST node in
omnimcode-core/src/ast.rs - Add lexer token in
omnimcode-core/src/parser.rsToken enum - Add parser rule in Parser impl
- Add interpreter handler in Interpreter impl
- Test with .omc program
- Define struct in
omnimcode-core/src/value.rs - Implement Display and conversion methods
- Add to Value enum
- Update interpreter matching
| 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 | 1× | 50-100× |
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)