File size: 7,758 Bytes
119e586 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | # 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
```emojiscript
π’40 π’2 β β©οΈ β 42
π’100 π’40 β β©οΈ β 60
π’6 π’7 βοΈ β©οΈ β 42
π’84 π’2 β β©οΈ β 42
```
### Bitwise
```emojiscript
π’15 π’7 π€ β©οΈ β 7 (AND: 1111 & 0111 = 0111)
π’12 π’5 π β©οΈ β 13 (OR: 1100 | 0101 = 1101)
π’12 π’5 π β©οΈ β 9 (XOR: 1100 ^ 0101 = 1001)
```
### Capability Gate
```emojiscript
π’3 π’5 π β©οΈ β CapGate check (slot=3, rights=5)
```
### Control Flow
```emojiscript
π’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:**
```json
{
"source": "π’40 π’2 β β©οΈ"
}
```
**Output:**
```json
{
"content": [
{
"type": "text",
"text": "β
Compiled: 4 instructions\nHash: abc123def456...\nBytecode: [{:op :Push, :value 40}, ...]"
}
]
}
```
### 2. `execute_emojiscript`
Execute compiled bytecode.
**Input:**
```json
{
"source": "π’40 π’2 β β©οΈ",
"max-steps": 10000
}
```
**Output:**
```json
{
"content": [
{
"type": "text",
"text": "β
Result: 42\nStack: [42]\nSteps: 4 / 10000"
}
]
}
```
## Runtime Implementation
**File:** `src/snapkitty/lisp/emojiscript.cljs`
### Compilation
```clojure
(emoji/compile-emojiscript "π’6 π’7 βοΈ β©οΈ")
; => {:source "...", :bytecode [...], :hash "...", :instructions-count 4, :valid? true}
```
### Execution
```clojure
(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:
```clojure
{: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:
```bash
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
- **Ahmad's EmojiScript (Rust):** https://github.com/SNAPKITTYWEST/snap-os/blob/main/craft-crypto/src/emoji.rs
- **SNAP OS:** https://github.com/SNAPKITTYWEST/snap-os
- **SoulVM:** https://github.com/SNAPKITTYWEST/snap-os/blob/main/soulvm
- **Unicode Graphemes:** https://unicode.org/reports/tr29/
|