LaadleLang v1.0.1

Architecture & Virtual Machine

LaadleLang is designed as a fully standalone, end-to-end language implementation. It does not transpile to another language; instead, it compiles directly to custom bytecode and executes on its own tailored Virtual Machine.

Here is the complete execution pipeline:

  1. Tokenizer (Lexer): Iterates over raw source code character by character. It implements dynamic tracking of whitespace (like Python) to emit explicit INDENT and DEDENT tokens when the indentation level changes, allowing blocks to be structured without curly braces.

  2. Parser: A handcrafted Recursive Descent parser. It reads the token stream and builds an Abstract Syntax Tree (AST). The AST uses distinct enums for Stmt (Statements, which don't evaluate to a value but perform actions) and Expr (Expressions, which always leave exactly one mathematical/logical result on the stack).

  3. Compiler: A single-pass bytecode compiler. It walks the AST from top to bottom, emitting a flat vector of enum-based OpCode instructions. It handles complex control-flow (like while jumps and short-circuit evaluation for && and ||) by backpatching jump addresses.

  4. VM (Virtual Machine): A stack-based execution engine (LaadleVirtualMachineV1) that evaluates the OpCode vector linearly.


The Virtual Machine Execution

The VM executes a stack-machine architecture. Instead of storing math outputs in intermediate registers, everything is pushed onto and popped off an Operand Stack.

A Basic Stack Example

For bol 10 + 20:

VM State Components

The VM is extremely lightweight. Its state consists of:

  • program: The immutable vector of Opcodes.
  • ip: The Instruction Pointer (current index in program).
  • stack: The Operand Stack (where intermediate math and logic values live).
  • globals: A Hash Map storing variables and functions defined at the top level.
  • call_stack: A stack of CallFrame structs, managing isolated scope bounds during function calls.
  • err_stack: A stack of ErrHandler structs indicating where to jump and unwind to during a gopgop (Throw).

Call Frames & Scope Isolation

When a function is called, the VM pushes a CallFrame containing:

  • return_ip: Where to jump back to after the function returns.
  • locals: An isolated HashMap for variables inside the function.
  • stack_base: The length of the operand stack before the function ran.

When the Return opcode is reached, the VM safely truncates the operand stack back to exactly stack_base, guaranteeing that garbage is not leaked from leftover calculations inside the function body.


Detailed OpCode Reference

The LaadleLang VM executes the following instruction set. Every opcode is modeled as a Rust enum variant. In the stack effects below, elements are listed left-to-right (the top of the stack is on the right).

Stack Operations

OpcodeStack EffectDescription
Push(Value)→ ValuePushes a literal (Int, Float, Bool, String, Null) onto the stack.
Popv →Discards the top value on the stack.
Dupv → v, vDuplicates the top value on the stack.
Swapa, b → b, aSwaps the order of the top two values.

Arithmetic

OpcodeStack EffectDescription
Adda, b → (a+b)Adds two numbers, widening Int to Float if mixed. Also concatenates Strings.
Suba, b → (a-b)Subtracts b from a.
Mula, b → (a*b)Multiplies a and b.
Diva, b → (a/b)Divides a by b. Triggers a Rust panic on integer division by zero.
Nega → -aUnary negation. Flips the mathematical sign of a.

Logic & Comparison

OpcodeStack EffectDescription
Nota → !aUnary NOT. Converts a to its truthy/falsy boolean equivalent, then flips it.
And / Ora, b → boolLogical AND / OR. Computes the logical constraint. (Note: The compiler short-circuits these using Jumps instead of emitting the raw opcodes!)
Eq / Neqa, b → boolEvaluates if a == b or a != b. Works securely across distinct Types.
Gt / Lta, b → boolEvaluates greater-than (>) or less-than (<).
Gte / Ltea, b → boolEvaluates greater-than-or-equal (>=) or less-than-or-equal (<=).

Variables (Scope)

OpcodeStack EffectDescription
SetGlobal(Name)v →Pops v and stores it strictly into the global HashMap under Name.
GetGlobal(Name)→ vFetches Name from the global HashMap and pushes it.
SetLocal(Name)v →Pops v and stores it into the purely internal CallFrame's local variables scope.
GetLocal(Name)→ vFetches Name from the inner CallFrame's block scope variables. If not explicitly declared, seamlessly traverses outwards to GetGlobal.

Control Flow

OpcodeStack EffectDescription
Jump(Addr)Unconditionally forcefully sets the Instruction Pointer (ip) to absolute Addr.
JumpIfFalse(Addr)cond →Pops the condition. If falsy, branches execution ip to Addr. Primarily constructs agar (if) statements and jabtak (loops).
JumpIfTrue(Addr)cond →Pops the condition. If truthy, sets ip to Addr.

Functions

OpcodeStack EffectDescription
MakeFunctionRegisters a function descriptor (name, addr, params) directly into the global table.
Call {n, argc}args... →Pops argc dynamic arguments off the stackFetches the declared function from globalsCreates new CallFrame and binds argumentsHops pointer to subroutine block addr
Returnv → vPops the return variable vDestroys the isolated CallFrame containerTruncates the runtime stack to prevent leaksRestores jump instruction pointer ipPushes v exactly once back onto the stack

Input / Output

OpcodeStack EffectDescription
Printv →Pops v and forwards it directly to the console terminal via bol.

Exceptions & Errors

OpcodeStack EffectDescription
PushErrHandler(A)Pushes an ErrHandler to the VM error stackRecords the catch block jump address ASnapshots the current Call Stack depthSnapshots the current Operand Stack base length
PopErrHandlerPops the innermost ErrHandler stateSignals the successful completion of a koshish (try) block
Throwv →Pops the thrown error value vShrinks Call Stack to safely escape nested functionsShrinks Operand Stack back to safetyPushes v back into memoryJumps to the pakad block using gopgop logic

Lifecycle

OpcodeStack EffectDescription
HaltSafely evaluates logic end-to-end terminating entire Virtual Machine main core thread cycle.
NoopDoes exactly nothingRetains padding for compiler backpatch jumps

On this page