SNAPKITTYWEST's picture
push from SNAPKITTYWEST/snapkitty-clojure-lisp-bridge
119e586 verified
|
Raw History Blame Contribute Delete
7.76 kB

Ahmad's EmojiScript β€” Bytecode Dialect

High-performance emoji-based programming language compiled to bytecode and executed on a stack-based VM. Part of the SNAP OS sovereign runtime and now integrated into the Clojure Lisp MCP.

Overview

EmojiScript is Ahmad's low-level bytecode language where each instruction is a single emoji (potentially multi-codepoint). Programs compile to SigilOp bytecode and execute on the SoulVM with cryptographic sealing to the WORM chain.

EmojiScript source β†’ lex() β†’ bytecode β†’ execute() β†’ result + WORM seal

The 15 Instructions

Stack Operations

Emoji Name Description Example
πŸ”’<digits> Push Push number literal (i64) πŸ”’42

Binary Operators (pop 2, push result)

Emoji Name Description
βž• Add Integer addition
βž– Sub Integer subtraction
βœ–οΈ (or βœ– or ❌) Mul Integer multiplication
βž— Div Integer division (raises on /0)
🀝 (or πŸ”—) And Bitwise AND
πŸ‘ (or πŸ”€) Or Bitwise OR
πŸŒ€ (or βŠ•) Xor Bitwise XOR

Control Flow

Emoji Name Args Description
➑️ Jump target (i64) Unconditional jump to PC
❓ JumpIf target (i64) Jump if top-of-stack β‰  0, pop predicate
↩️ (or πŸ”™ or πŸ”š) Ret β€” Return & halt execution

Capability & Memory

Emoji Name Args Description
πŸ”‘ CapGate slot (i64), rights (i64) Capability proof check (pops 2 args)
⚑ Call func_idx (i64) Indirect function call (future)
πŸ—οΈ Alloc size (i64), type_tag (i64) Allocate memory (pops 2 args)
πŸ“€ Load offset (i64) Load from memory (pops 1 arg)
πŸ“¦ Store offset (i64) Store to memory (pops 1 arg)

Future Semantic Passes (Sprint 2)

Emoji Name Routes To Purpose
🌊 Stream telemetry-bus Emit telemetry event (reserved)
🧠 PolicyCheck policy-immune Check policy constraint (reserved)
πŸ”’ Seal bifrost Explicit WORM seal hint (reserved)
πŸ”“ ReadOnly bifrost-policy Downgrade rights hint (reserved)

Syntax

Programs are whitespace-separated emoji tokens. Numbers follow πŸ”’:

πŸ”’40 πŸ”’2 βž• ↩️

Parsing rules:

  • Each emoji is a single grapheme cluster (handles multi-codepoint emoji)
  • Whitespace (space, tab, newline) is ignored
  • Numbers: πŸ”’ followed immediately by ASCII digits (no separator)
  • Arguments are written before their opcode
    • Example: πŸ”’3 πŸ”’5 πŸ”‘ means "push 3, push 5, then execute CapGate(3, 5)"
    • Args are popped in reverse order (last arg pushed = first arg consumed)

Examples

Arithmetic

πŸ”’40 πŸ”’2 βž• ↩️          β†’ 42
πŸ”’100 πŸ”’40 βž– ↩️        β†’ 60
πŸ”’6 πŸ”’7 βœ–οΈ ↩️           β†’ 42
πŸ”’84 πŸ”’2 βž— ↩️          β†’ 42

Bitwise

πŸ”’15 πŸ”’7 🀝 ↩️         β†’ 7 (AND: 1111 & 0111 = 0111)
πŸ”’12 πŸ”’5 πŸ‘ ↩️         β†’ 13 (OR: 1100 | 0101 = 1101)
πŸ”’12 πŸ”’5 πŸŒ€ ↩️         β†’ 9 (XOR: 1100 ^ 0101 = 1001)

Capability Gate

πŸ”’3 πŸ”’5 πŸ”‘ ↩️          β†’ CapGate check (slot=3, rights=5)

Control Flow

πŸ”’1 ❓ ↩️               β†’ Jump if predicate β‰  0
πŸ”’10 ➑️ ↩️             β†’ Unconditional jump to PC 10

MCP Tools

Three new MCP tools expose EmojiScript:

1. compile_emojiscript

Compile source to bytecode.

Input:

{
  "source": "πŸ”’40 πŸ”’2 βž• ↩️"
}

Output:

{
  "content": [
    {
      "type": "text",
      "text": "βœ… Compiled: 4 instructions\nHash: abc123def456...\nBytecode: [{:op :Push, :value 40}, ...]"
    }
  ]
}

2. execute_emojiscript

Execute compiled bytecode.

Input:

{
  "source": "πŸ”’40 πŸ”’2 βž• ↩️",
  "max-steps": 10000
}

Output:

{
  "content": [
    {
      "type": "text",
      "text": "βœ… Result: 42\nStack: [42]\nSteps: 4 / 10000"
    }
  ]
}

Runtime Implementation

File: src/snapkitty/lisp/emojiscript.cljs

Compilation

(emoji/compile-emojiscript "πŸ”’6 πŸ”’7 βœ–οΈ ↩️")
; => {:source "...", :bytecode [...], :hash "...", :instructions-count 4, :valid? true}

Execution

(let [compiled (emoji/compile-emojiscript "πŸ”’40 πŸ”’2 βž• ↩️")]
  (emoji/execute-emojiscript (:bytecode compiled) :max-steps 10000))
; => {:result 42, :stack [42], :steps 4, :halted? true}

Stack-Based Interpreter

  • Stack: Mutable vector, push/pop operations
  • PC (Program Counter): Jump targets are bytecode indices
  • Registers: None (pure stack machine)
  • Memory: Stubs (future implementation)
  • Step Limit: Prevents infinite loops (default 10,000)

Error Handling

  • Parse errors: Unknown emoji, missing arguments, invalid numbers
  • Runtime errors: Division by zero, unknown instructions, step limit exceeded
  • Graceful degradation: Future semantic tokens (🌊, 🧠, πŸ”’, πŸ”“) skip execution

Bytecode Format

Each instruction is a ClojureScript map:

{:op :Push :value 42}
{:op :Add}
{:op :CapGate :slot 3 :rights 5}
{:op :Jump :target 10}
{:op :Load :offset 0}
{:op :Store :offset 8}
{:op :Ret}

Instruction fields:

  • :op β€” Instruction type (keyword)
  • :value β€” For :Push, the i64 literal
  • :slot, :rights β€” For :CapGate
  • :func β€” For :Call, function table index
  • :size, :type-tag β€” For :Alloc
  • :offset β€” For :Load, :Store
  • :target β€” For :Jump, :JumpIf, bytecode index

Testing

Run integration tests:

npm test

Tests cover:

  • Compilation of all 15 opcodes
  • Execution of arithmetic, bitwise, control flow
  • Error cases: empty program, unknown emoji, division by zero
  • Step limit enforcement
  • MCP tool integration

Performance Notes

  • Compilation: O(n) in source length, single pass
  • Execution: O(instructions), typical 1-10Β΅s per instruction
  • Memory: Minimal (stack + bytecode vector)
  • Future: Link to SoulVM JIT (Cranelift) for native code generation

Design Principles

  1. Emoji as first-class syntax: Every instruction is a single grapheme cluster
  2. Argument-before-opcode: Enables post-fix compilation and clarity
  3. Stack machine: No registers, minimal state, deterministic
  4. Stateless lexer: Grapheme-by-grapheme parsing, Unicode-aware
  5. Extensible: Semantic-pass tokens allow future subsystems without breaking existing code

Future Work (Sprint 2+)

  • Memory ops: Implement :Load, :Store, :Alloc
  • Function calls: Implement :Call with function tables
  • Semantic passes: Route 🌊, 🧠, πŸ”’, πŸ”“ to subsystems
  • JIT compilation: Link to SoulVM Cranelift backend
  • WORM sealing: Bifrost signing of execution results
  • ScratchBlocks visual editor: Drag-drop blocks ↔ sync emoji text

References