File size: 14,740 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | (ns snapkitty.lisp.emojiscript
"Ahmad's EmojiScript bytecode dialect β emoji tokens β SigilOp bytecode"
(:require [goog.string :as gstring]
[goog.string.format]))
;; ============================================================================
;; SigilOp Bytecode Instructions
;; ============================================================================
(def bin-ops
{"++" :Add
"--" :Sub
"**" :Mul
"//" :Div
"&&" :And
"||" :Or
"^^" :Xor})
(defn- bin-op? [k]
(contains? bin-ops k))
;; ============================================================================
;; EmojiToken AST
;; ============================================================================
(def emoji-tokens
{
; Numeric literal
"π’" :Number
; Binary ops
"β" :Add
"β" :Sub
"β" :Mul
"βοΈ" :Mul
"β" :Mul
"β" :Div
"π€" :And
"π" :And
"π" :Or
"π" :Or
"π" :Xor
"β" :Xor
; Control & memory
"π" :CapGate ; (pops 2 args: slot, rights)
"β‘" :Call ; (pops 1 arg: func_idx)
"ποΈ" :Alloc ; (pops 2 args: size, type_tag)
"π€" :Load ; (pops 1 arg: offset)
"π¦" :Store ; (pops 1 arg: offset)
"β‘οΈ" :Jump ; (pops 1 arg: target)
"β" :JumpIf ; (pops 1 arg: target)
"β©" :Ret
"β©οΈ" :Ret
"π" :Ret
"π" :Ret
; Future semantic passes (Sprint 2)
"π" :Stream ; routes to telemetry-bus
"π§ " :PolicyCheck ; routes to policy-immune
"π" :Seal ; Bifrost seal hint
"π" :ReadOnly ; Downgrade rights hint
})
;; ============================================================================
;; Lexer β parse grapheme by grapheme (Unicode-aware)
;; ============================================================================
(defn- split-graphemes [s]
"Split string into grapheme clusters (emoji-safe)"
(let [arr (js-array)]
(doseq [char (seq s)]
(.push arr (str char)))
(vec arr)))
(defn- pop-number [graphemes idx]
"Parse π’<digits> starting at idx, return [number, next_idx]"
(let [start (inc idx)
digits (atom "")]
(loop [i start]
(if (< i (count graphemes))
(let [g (get graphemes i)]
(if (re-matches #"^\d$" g)
(do
(swap! digits str g)
(recur (inc i)))
[i @digits]))
[i @digits]))))
(defn- lex [input]
"Tokenize EmojiScript input β vector of SigilOp maps"
(let [graphemes (split-graphemes input)
ops (atom [])]
(loop [idx 0]
(if (>= idx (count graphemes))
@ops
(let [g (get graphemes idx)]
(cond
; Skip whitespace
(re-matches #"^\s$" g)
(recur (inc idx))
; Number: π’<digits>
(= g "π’")
(let [[next-idx digits-str] (pop-number graphemes idx)]
(if (empty? digits-str)
(throw (ex-info "Invalid number after π’" {:input input :at idx}))
(let [n (js/parseInt digits-str 10)]
(swap! ops conj {:op :Push :value n})
(recur next-idx))))
; Zero-arg ops (return immediately)
(contains? #{:Add :Sub :Mul :Div :And :Or :Xor :Ret :Stream :PolicyCheck :Seal :ReadOnly} (get emoji-tokens g))
(do
(swap! ops conj {:op (get emoji-tokens g)})
(recur (inc idx)))
; Arg-consuming ops (need to pop preceding π’ pushes)
(= g "π")
(let [;; Pop two preceding Push ops (rights, then slot)
rights-op (when (> (count @ops) 0) (peek @ops))
_ (when-not (= (:op rights-op) :Push)
(throw (ex-info "π requires π’ arguments before it" {:at idx})))
rights (-> @ops pop (.-value))
_ (swap! ops pop)
slot-op (when (> (count @ops) 0) (peek @ops))
_ (when-not (= (:op slot-op) :Push)
(throw (ex-info "π requires 2 π’ arguments before it" {:at idx})))
slot (-> @ops peek (.-value))
_ (swap! ops pop)]
(swap! ops conj {:op :CapGate :slot slot :rights rights})
(recur (inc idx)))
(= g "β‘")
(let [func-op (peek @ops)]
(when-not (= (:op func-op) :Push)
(throw (ex-info "β‘ requires π’ argument before it" {:at idx})))
(let [func (-> @ops peek (.-value))]
(swap! ops pop)
(swap! ops conj {:op :Call :func func})
(recur (inc idx))))
(= g "ποΈ")
(let [type-op (peek @ops)]
(when-not (= (:op type-op) :Push)
(throw (ex-info "ποΈ requires 2 π’ arguments before it" {:at idx})))
(let [type-tag (-> @ops peek (.-value))
_ (swap! ops pop)
size-op (peek @ops)
_ (when-not (= (:op size-op) :Push)
(throw (ex-info "ποΈ requires 2 π’ arguments before it" {:at idx})))
size (-> @ops peek (.-value))
_ (swap! ops pop)]
(swap! ops conj {:op :Alloc :size size :type-tag type-tag})
(recur (inc idx))))
(= g "π€")
(let [offset-op (peek @ops)]
(when-not (= (:op offset-op) :Push)
(throw (ex-info "π€ requires π’ argument before it" {:at idx})))
(let [offset (-> @ops peek (.-value))]
(swap! ops pop)
(swap! ops conj {:op :Load :offset offset})
(recur (inc idx))))
(= g "π¦")
(let [offset-op (peek @ops)]
(when-not (= (:op offset-op) :Push)
(throw (ex-info "π¦ requires π’ argument before it" {:at idx})))
(let [offset (-> @ops peek (.-value))]
(swap! ops pop)
(swap! ops conj {:op :Store :offset offset})
(recur (inc idx))))
(= g "β‘οΈ")
(let [target-op (peek @ops)]
(when-not (= (:op target-op) :Push)
(throw (ex-info "β‘οΈ requires π’ argument before it" {:at idx})))
(let [target (-> @ops peek (.-value))]
(swap! ops pop)
(swap! ops conj {:op :Jump :target target})
(recur (inc idx))))
(= g "β")
(let [target-op (peek @ops)]
(when-not (= (:op target-op) :Push)
(throw (ex-info "β requires π’ argument before it" {:at idx})))
(let [target (-> @ops peek (.-value))]
(swap! ops pop)
(swap! ops conj {:op :JumpIf :target target})
(recur (inc idx))))
; Unknown emoji
:else
(throw (ex-info (str "Unknown emoji: " g) {:input input :at idx})))))))
(if (empty? @ops)
(throw (ex-info "Empty program" {:input input}))
@ops))
;; ============================================================================
;; Compilation to bytecode
;; ============================================================================
(defn compile-emojiscript [input]
"Compile EmojiScript source β bytecode object
Returns:
{:source string
:bytecode [ops]
:hash string
:instructions-count number}"
(let [bytecode (lex input)
source-hash (js/btoa (js/encodeURIComponent input))
instructions-count (count bytecode)]
{:source input
:bytecode bytecode
:hash source-hash
:instructions-count instructions-count
:valid? true}))
;; ============================================================================
;; Execution (stack-based interpreter)
;; ============================================================================
;; ============================================================================
;; Semantic Passes (Phase 2)
;; ============================================================================
(defn- telemetry-log [event data]
"Log event to telemetry-bus (stub for now)"
{:type :telemetry :event event :data data :timestamp (js/Date.now)})
(defn- policy-route [value policy-id]
"Route to policy-immune handler (stub for now)"
{:type :policy-route :value value :policy-id policy-id})
(defn- bifrost-seal [value seal-id worm-ledger]
"Seal value to WORM ledger (stub for now)"
{:type :bifrost-seal :value value :seal-id seal-id :ledger worm-ledger})
(defn- capability-downgrade [rights from-level to-level]
"Downgrade capability rights (stub for now)"
(max 0 (bit-shift-right rights (- from-level to-level))))
;; ============================================================================
;; Execution (stack-based interpreter)
;; ============================================================================
(defn execute-emojiscript [bytecode & {:keys [max-steps telemetry-bus policy-registry worm-ledger]
:or {max-steps 10000}}]
"Execute compiled EmojiScript bytecode with semantic passes
Returns:
{:result (top of stack)
:stack [values]
:steps number
:halted? boolean
:events [telemetry events]}"
(let [stack (atom [])
pc (atom 0)
steps (atom 0)
halted? (atom false)
events (atom [])]
(try
(while (and (< @pc (count bytecode))
(< @steps max-steps)
(not @halted?))
(let [instr (get bytecode @pc)]
(swap! steps inc)
(case (:op instr)
:Push
(do
(swap! stack conj (:value instr))
(swap! pc inc))
:Add
(let [b (peek @stack) _ (swap! stack pop)
a (peek @stack) _ (swap! stack pop)]
(swap! stack conj (+ a b))
(swap! pc inc))
:Sub
(let [b (peek @stack) _ (swap! stack pop)
a (peek @stack) _ (swap! stack pop)]
(swap! stack conj (- a b))
(swap! pc inc))
:Mul
(let [b (peek @stack) _ (swap! stack pop)
a (peek @stack) _ (swap! stack pop)]
(swap! stack conj (* a b))
(swap! pc inc))
:Div
(let [b (peek @stack) _ (swap! stack pop)
a (peek @stack) _ (swap! stack pop)]
(if (= b 0)
(throw (ex-info "Division by zero" {}))
(do
(swap! stack conj (quot a b))
(swap! pc inc))))
:And
(let [b (peek @stack) _ (swap! stack pop)
a (peek @stack) _ (swap! stack pop)]
(swap! stack conj (bit-and a b))
(swap! pc inc))
:Or
(let [b (peek @stack) _ (swap! stack pop)
a (peek @stack) _ (swap! stack pop)]
(swap! stack conj (bit-or a b))
(swap! pc inc))
:Xor
(let [b (peek @stack) _ (swap! stack pop)
a (peek @stack) _ (swap! stack pop)]
(swap! stack conj (bit-xor a b))
(swap! pc inc))
:CapGate
(do
; For now, just verify both args are present (no-op gate)
(swap! pc inc))
:Call
(throw (ex-info "Function calls not yet supported" {}))
:Alloc
(throw (ex-info "Memory allocation not yet supported" {}))
:Load
(throw (ex-info "Memory load not yet supported" {}))
:Store
(throw (ex-info "Memory store not yet supported" {}))
:Jump
(swap! pc (:target instr))
:JumpIf
(let [cond (peek @stack) _ (swap! stack pop)]
(if (zero? cond)
(swap! pc inc)
(swap! pc (:target instr))))
:Ret
(reset! halted? true)
:Stream
(let [value (peek @stack)]
(if value
(do
(swap! events conj (telemetry-log :stream-push {:value value}))
(swap! pc inc))
(throw (ex-info "π Stream requires value on stack" {}))))
:PolicyCheck
(let [value (peek @stack)
policy-id (:policy-id instr 0)]
(if value
(do
(swap! events conj (policy-route value policy-id))
(swap! pc inc))
(throw (ex-info "π§ PolicyCheck requires value on stack" {}))))
:Seal
(let [value (peek @stack)
seal-id (:seal-id instr (str "seal-" @steps))]
(if value
(do
(swap! events conj (bifrost-seal value seal-id worm-ledger))
(swap! pc inc))
(throw (ex-info "π Seal requires value on stack" {}))))
:ReadOnly
(let [rights (peek @stack)
from-level (:from-level instr 7)
to-level (:to-level instr 3)]
(if rights
(do
(let [downgraded (capability-downgrade rights from-level to-level)]
(swap! stack pop)
(swap! stack conj downgraded))
(swap! events conj {:type :capability-downgrade :from from-level :to to-level})
(swap! pc inc))
(throw (ex-info "π ReadOnly requires capability on stack" {}))))
(throw (ex-info "Unknown instruction" {:instr instr})))))
{:result (peek @stack)
:stack (vec @stack)
:steps @steps
:halted? @halted?
:events @events}
(catch js/Error e
{:error (.-message e)
:stack (vec @stack)
:steps @steps
:halted? false
:events @events}))))
;; ============================================================================
;; Public API
;; ============================================================================
(def emojiscript-api
{:compile compile-emojiscript
:execute execute-emojiscript})
|