File size: 11,055 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 | ;; SKC-LISP: Lisp Machine REPL (CLI + Browser)
;; Interactive frontend connecting reader β compiler β LTMS β WASM β JIT β WORM
(ns snapkitty.lisp.cli.repl
(:require
[snapkitty.lisp.bridge.reader :as reader]
[snapkitty.lisp.bridge.compiler :as compiler]
[snapkitty.lisp.emojiscript :as emoji]
[snapkitty.lisp.ltms.ltms :as ltms]
[snapkitty.lisp.native :as native]
[snapkitty.lisp.jit :as jit]
[snapkitty.lisp.jit-ledger :as ledger]
[snapkitty.lisp.wasm-bridge :as wasm]
[cljs.reader :as edn]
[promesa.core :as p]))
;; ============================================================================
;; REPL STATE
;; ============================================================================
(defonce repl-context
(atom {:history []
:variables {}
:loaded-files []
:proof-certs {}
:wasm-ready? false}))
;; ============================================================================
;; CORE REPL FUNCTIONS
;; ============================================================================
(defn read-lisp
"Parse LISP code into forms."
[source]
(try
(reader/read-lisp source)
(catch js/Error e
{:error :parse-error :message (.toString e)})))
(defn compile-form
"Compile a LISP form to semantic graph + bytecode."
[form]
(try
(let [compiled (compiler/compile-form form)]
(ltms/assert-fact! form :lisp-compiler 0.95 50)
compiled)
(catch js/Error e
{:error :compile-error :message (.toString e)})))
(defn eval-form
"Evaluate a compiled form (semantic + execution)."
[form]
(let [compiled (compile-form form)]
(if (:error compiled)
compiled
(try
(eval compiled)
(catch js/Error e
{:error :eval-error :message (.toString e)})))))
;; ============================================================================
;; EMOJI SCRIPT INTEGRATION
;; ============================================================================
(defn compile-emoji
"Compile EmojiScript source to bytecode."
[source]
(try
(emoji/compile-emojiscript source)
(catch js/Error e
{:error :emoji-compile-error :message (.toString e)})))
(defn execute-emoji
"Execute EmojiScript bytecode with full pipeline."
[source]
(try
(let [compiled (compile-emoji source)]
(if (:error compiled)
compiled
(let [result (emoji/execute-emojiscript (:bytecode compiled))]
(ltms/assert-fact! {:emoji-result result} :emoji-executor 0.95 50)
result)))
(catch js/Error e
{:error :emoji-exec-error :message (.toString e)})))
;; ============================================================================
;; JIT COMPILATION PIPELINE
;; ============================================================================
(defn compile-with-jit
"Compile LISP β EmojiScript β bytecode β native (JIT)."
[source proof-cert]
(try
(p/let [emoji-compiled (compile-emoji source)
jit-result (jit/compile-with-proof
source proof-cert "x86_64")]
(if (:error jit-result)
jit-result
(do
;; Record in WORM ledger
(ledger/compile-and-seal-to-ledger!
source proof-cert "x86_64" "repl-user" (make-array 32)
(atom {:entries [] :generation 0}))
jit-result)))
(catch js/Error e
{:error :jit-error :message (.toString e)})))
;; ============================================================================
;; KNOWLEDGE LAYER QUERIES
;; ============================================================================
(defn query-belief
"Query the knowledge base for a belief."
[value]
(let [belief (ltms/query value)]
(if belief
{:found true :value value :confidence (:confidence belief) :source (:source belief)}
{:found false :value value :confidence 0.0})))
(defn assert-belief!
"Assert a new belief to the knowledge base."
[value source confidence priority]
(ltms/assert-fact! value source confidence priority)
{:status "asserted" :value value :source source})
(defn disambiguate-concept
"Find senses of a concept."
[concept-name context]
(let [senses (ltms/disambiguate concept-name context)]
{:concept concept-name :senses senses :count (count senses)}))
;; ============================================================================
;; WASM CRYPTO INTEGRATION
;; ============================================================================
(defn verify-blake3-wasm
"Verify Blake3 digest (via WASM)."
[payload expected-digest]
(try
(wasm/blake3-verify payload expected-digest)
(catch js/Error e
{:error :blake3-error :message (.toString e)})))
(defn verify-ed25519-wasm
"Verify Ed25519 signature (via WASM)."
[message signature public-key]
(try
(wasm/ed25519-verify message signature public-key)
(catch js/Error e
{:error :ed25519-error :message (.toString e)})))
;; ============================================================================
;; REPL COMMANDS
;; ============================================================================
(def repl-commands
{
;; LISP operations
:read "(read <source>) β Parse LISP code"
:compile "(compile <form>) β Compile to semantic graph"
:eval "(eval <form>) β Evaluate form"
;; EmojiScript operations
:emoji:info "(emoji:info) β Show emoji instruction reference"
:emoji:compile "(emoji:compile <source>) β Compile EmojiScript to bytecode"
:emoji:exec "(emoji:exec <source>) β Execute EmojiScript"
;; JIT operations
:jit:compile "(jit:compile <source> <proof-cert>) β Compile with JIT"
:jit:status "(jit:status) β Show JIT compilation stats"
;; Knowledge layer
:kb:query "(kb:query <value>) β Query belief base"
:kb:assert "(kb:assert <value> <source> <confidence>) β Assert belief"
:kb:disambiguate "(kb:disambiguate <concept> <context>) β Find senses"
;; WASM crypto
:crypto:blake3 "(crypto:blake3 <payload> <digest>) β Verify Blake3"
:crypto:ed25519 "(crypto:ed25519 <msg> <sig> <pk>) β Verify Ed25519"
;; System
:help "(help) β Show this list"
:history "(history) β Show command history"
:clear "(clear) β Clear history"
:exit "(exit) β Exit REPL"
})
;; ============================================================================
;; COMMAND PARSER & EXECUTOR
;; ============================================================================
(defn parse-command
"Parse REPL command like (command arg1 arg2)."
[input]
(try
(let [forms (reader/read-lisp input)]
(if (seq? forms)
{:command (keyword (first forms))
:args (rest forms)}
{:error "Not a command"}))
(catch js/Error e
{:error (.toString e)})))
(defn execute-command
"Execute a REPL command with args."
[command args]
(case command
;; LISP
:read (read-lisp (first args))
:compile (compile-form (first args))
:eval (eval-form (first args))
;; EmojiScript
:emoji:info (emoji/emoji-reference)
:emoji:compile (compile-emoji (first args))
:emoji:exec (execute-emoji (first args))
;; JIT
:jit:compile (compile-with-jit (first args) (second args))
:jit:status {:status "JIT ready" :target "x86_64"}
;; Knowledge
:kb:query (query-belief (first args))
:kb:assert (assert-belief! (first args) (second args) (nth args 2 0.9) (nth args 3 50))
:kb:disambiguate (disambiguate-concept (first args) (second args))
;; Crypto
:crypto:blake3 (verify-blake3-wasm (first args) (second args))
:crypto:ed25519 (verify-ed25519-wasm (first args) (second args) (nth args 2))
;; System
:help repl-commands
:history (:history @repl-context)
:clear (do (swap! repl-context assoc :history []) {:status "history cleared"})
{:error (str "Unknown command: " command)}))
(defn eval-input
"Parse and execute REPL input."
[input]
(let [trimmed (.trim input)]
(if (.startsWith trimmed "(")
;; Command
(let [{:keys [command args error]} (parse-command trimmed)]
(if error
{:error error}
(let [result (execute-command command args)]
(swap! repl-context update :history conj {:input trimmed :result result})
result)))
;; EmojiScript or bare LISP
(if (clojure.string/includes? trimmed "π’")
;; Emoji code
(execute-emoji trimmed)
;; Bare LISP expression
(let [forms (read-lisp trimmed)]
(if (:error forms)
forms
(eval-form (first forms))))))))
;; ============================================================================
;; BROWSER REPL INTEGRATION
;; ============================================================================
(defn format-output
"Pretty-print REPL output for display."
[result]
(cond
(:error result)
(str "ERROR: " (:message result))
(map? result)
(str (clj->js result))
(vector? result)
(str (pr-str result))
:else
(str result)))
(defn ^:export init-repl
"Initialize REPL (called from browser/Node.js)."
[]
(p/let [_ (wasm/init-wasm!)]
(swap! repl-context assoc :wasm-ready? true)
(println "β REPL initialized")))
(defn ^:export repl-eval
"Execute REPL input from browser console."
[input]
(let [result (eval-input input)]
(println (format-output result))
result))
(defn ^:export get-context
"Get current REPL context (for debugging)."
[]
@repl-context)
;; ============================================================================
;; TEST HARNESS
;; ============================================================================
(defn run-repl-tests []
(println "=== Lisp Machine REPL Tests ===\n")
;; Test 1: Parse LISP
(println "Test 1: Parse LISP")
(let [result (read-lisp "(+ 1 2)")]
(println "β Parsed:" result))
(println)
;; Test 2: Compile form
(println "Test 2: Compile form")
(let [result (compile-form '(+ 1 2))]
(println "β Compiled:" (:type result)))
(println)
;; Test 3: Emoji script
(println "Test 3: EmojiScript")
(let [result (execute-emoji "π’6 π’7 βοΈ β©οΈ")]
(println "β Result:" result))
(println)
;; Test 4: Knowledge query
(println "Test 4: Knowledge layer")
(assert-belief! :test-fact :repl 0.95 100)
(let [result (query-belief :test-fact)]
(println "β Query:" result))
(println)
;; Test 5: REPL command
(println "Test 5: REPL command")
(let [result (eval-input "(emoji:exec \"π’40 π’2 β β©οΈ\")")]
(println "β Command result:" result))
(println)
(println "=== REPL Tests Complete ==="))
|