File size: 8,105 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
(ns snapkitty.lisp.emojiscript-adapter
  "Bridge between EmojiScript and Lisp Machine CLI

   Exposes Ahmad's EmojiScript bytecode dialect to the machine REPL"
  (:require [snapkitty.lisp.emojiscript :as emoji]))

;; ============================================================================
;; Command Handlers for Lisp Machine REPL
;; ============================================================================

(defn handle-emoji-command [repl-state input]
  "Parse and execute EmojiScript commands from the REPL



   Commands:

   - (emoji:compile \"πŸ”’6 πŸ”’7 βœ–οΈ ↩️\") β†’ bytecode

   - (emoji:exec \"πŸ”’40 πŸ”’2 βž• ↩️\") β†’ result

   - (emoji:disasm <bytecode>) β†’ instruction list"
  (try
    (cond
      ; Compile command: (emoji:compile <source>)
      (string/starts-with? input "(emoji:compile")
      (let [source-match (re-find #"\(emoji:compile\s+\"([^\"]+)\"\)" input)
            source (second source-match)]
        (if source
          (let [result (emoji/compile-emojiscript source)]
            {:type :success
             :command :emoji:compile
             :source source
             :bytecode (:bytecode result)
             :hash (:hash result)
             :instructions (:instructions-count result)
             :output (str "βœ… EmojiScript compiled\n"
                         "  Instructions: " (:instructions-count result) "\n"
                         "  Hash: " (subs (:hash result) 0 16) "...")})
          {:type :error :message "Invalid emoji:compile syntax"}))

      ; Execute command: (emoji:exec <source>)
      (string/starts-with? input "(emoji:exec")
      (let [source-match (re-find #"\(emoji:exec\s+\"([^\"]+)\"\)" input)
            source (second source-match)]
        (if source
          (try
            (let [compiled (emoji/compile-emojiscript source)
                  result (emoji/execute-emojiscript (:bytecode compiled))]
              {:type :success
               :command :emoji:exec
               :source source
               :result (:result result)
               :stack (:stack result)
               :steps (:steps result)
               :halted? (:halted? result)
               :output (str "βœ… EmojiScript executed\n"
                           "  Result: " (:result result) "\n"
                           "  Stack: " (pr-str (:stack result)) "\n"
                           "  Steps: " (:steps result))})
            (catch js/Error e
              {:type :error :message (str "Execution failed: " (.-message e))}))
          {:type :error :message "Invalid emoji:exec syntax"}))

      ; Disassemble: (emoji:disasm <bytecode>)
      (string/starts-with? input "(emoji:disasm")
      (let [bytecode-str (re-find #"\(emoji:disasm\s+'([^)]+)\)" input)]
        (if bytecode-str
          {:type :success
           :command :emoji:disasm
           :output "πŸ” EmojiScript disassembly not yet implemented"}
          {:type :error :message "Invalid emoji:disasm syntax"}))

      ; Info command: (emoji:info)
      (= input "(emoji:info)")
      {:type :success
       :command :emoji:info
       :output (str "╔══════════════════════════════════════════════════════════╗\n"
                    "β•‘        Ahmad's EmojiScript β€” Bytecode Language         β•‘\n"
                    "╠══════════════════════════════════════════════════════════╣\n"
                    "β•‘                                                          β•‘\n"
                    "β•‘  15 Instructions:                                        β•‘\n"
                    "β•‘    πŸ”’<digits>      Push number literal                  β•‘\n"
                    "β•‘    βž• βž– βœ–οΈ βž—  Binary ops: add, sub, mul, div           β•‘\n"
                    "β•‘    🀝 πŸ‘ πŸŒ€       Bitwise: and, or, xor                 β•‘\n"
                    "β•‘    ➑️ ❓ ↩️        Control: jump, jumpif, return        β•‘\n"
                    "β•‘    πŸ”‘ ⚑ πŸ—οΈ       Gate, call, alloc                     β•‘\n"
                    "β•‘    πŸ“€ πŸ“¦          Load, store                            β•‘\n"
                    "β•‘                                                          β•‘\n"
                    "β•‘  Usage:                                                  β•‘\n"
                    "β•‘    (emoji:compile \"πŸ”’6 πŸ”’7 βœ–οΈ ↩️\")  β†’ bytecode        β•‘\n"
                    "β•‘    (emoji:exec \"πŸ”’40 πŸ”’2 βž• ↩️\")    β†’ result           β•‘\n"
                    "β•‘                                                          β•‘\n"
                    "β•‘  Example: πŸ”’40 πŸ”’2 βž• ↩️ = 42                            β•‘\n"
                    "β•‘                                                          β•‘\n"
                    "β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•")}

      :else
      {:type :unknown :input input})

    (catch js/Error e
      {:type :error :message (str "EmojiScript error: " (.-message e))})))

;; ============================================================================
;; Lisp Machine Integration
;; ============================================================================

(defn register-emoji-dialect! [repl-context]
  "Register EmojiScript dialect handlers with the Lisp Machine REPL



   Adds to the machine's command registry:

   - emoji:compile

   - emoji:exec

   - emoji:info"

  (update repl-context
          :command-handlers
          (fn [handlers]
            (merge handlers
                   {:emoji:compile (partial handle-emoji-command repl-context)
                    :emoji:exec (partial handle-emoji-command repl-context)
                    :emoji:disasm (partial handle-emoji-command repl-context)
                    :emoji:info (partial handle-emoji-command repl-context)}))))

;; ============================================================================
;; REPL Integration β€” Pretty Print Results
;; ============================================================================

(defn format-emoji-result [result]
  "Format EmojiScript results for REPL display"
  (case (:type result)
    :success
    (str "\n" (:output result) "\n\n")

    :error
    (str "❌ " (:message result) "\n\n")

    :unknown
    (str "❓ Unknown command: " (:input result) "\n\n")

    (str result)))

;; ============================================================================
;; Benchmarking & Profiling
;; ============================================================================

(defn bench-emoji-program [source iterations]
  "Benchmark EmojiScript execution



   Returns:

   {:source string

    :iterations number

    :total-time-ms number

    :avg-time-ms number

    :result any}"
  (let [compiled (emoji/compile-emojiscript source)
        bytecode (:bytecode compiled)
        start (js/Date.now)]
    (doseq [_ (range iterations)]
      (emoji/execute-emojiscript bytecode))
    (let [end (js/Date.now)
          total (- end start)]
      {:source source
       :iterations iterations
       :total-time-ms total
       :avg-time-ms (/ total iterations)
       :ops-per-sec (* 1000 (/ iterations total))})))

;; ============================================================================
;; Export API for Lisp Machine
;; ============================================================================

(def emojiscript-cli-api
  {:compile emoji/compile-emojiscript
   :execute emoji/execute-emojiscript
   :handle-command handle-emoji-command
   :register register-emoji-dialect!
   :format-result format-emoji-result
   :bench bench-emoji-program})