-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.hy
More file actions
381 lines (317 loc) · 11.9 KB
/
repl.hy
File metadata and controls
381 lines (317 loc) · 11.9 KB
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
(import
array
atexit
io
json
os
readline
socket
struct
subprocess [Popen]
time
traceback
hy.models [Expression Symbol]
hyrule [dec parse-args]
hyrule.hypprint [pprint]
serial
tqdm
compile
link)
(require
hyrule [ecase unless]
hyrule.oop [meth])
;;;
;;; Low-level debug protocol
;;;
(setv SEGMENT:BC 0
SEGMENT:FUNC 1
SEGMENT:GLOB 2)
(setv
OP:BEGIN-EXEC (ord "x")
OP:SUSPEND (ord "s")
OP:STATE (ord "S")
OP:WRITE-MEM (ord "w"))
;; Stream-oriented transport -- need to do our own framing
(defclass StreamTransport []
(meth send-frame [frame-bytes]
(let [f (io.BytesIO)]
(for [chr frame-bytes]
(if (in chr #{0x7D 0x7E})
(f.write (bytes [0x7D (^ chr 0x20)]))
(f.write (bytes [chr]))))
(f.write b"\x7E")
(@send (f.getvalue)))))
(defclass SerialTransport [StreamTransport]
(meth __init__ [port baud #* args]
(setv @baud baud)
(setv @ser (serial.Serial port baud #* args)))
(meth close []
(@ser.close))
(meth recvall [count]
(@ser.read count))
(meth send [data]
;; if transmission is due to take more than a half-second, display a progress bar
(defn tqdm-chunked [sliceable chunk-size #* args #** kwargs]
(let [progress (tqdm.tqdm :total (len sliceable) #* args #** kwargs)]
(for [offset (range 0 (len sliceable) chunk-size)]
(let [chunk (cut data offset (+ offset chunk-size))]
(yield chunk)
(progress.update (len chunk))))))
(if (>= (* (len data) 8) (* 0.5 @baud))
(for [chunk (tqdm-chunked data :chunk-size 100 :desc "Transferring")]
(@ser.write chunk))
(@ser.write data))))
(defclass SocketTransport [StreamTransport]
(meth __init__ [address-tuple]
(setv @sock (socket.socket socket.AF_INET socket.SOCK_STREAM))
(@sock.connect address-tuple))
(meth close []
(@sock.close))
(meth recvall [count]
(let [f (io.BytesIO)
received 0]
(while (< received count)
(f.write (@sock.recv (- count received)))
(setv received (. f (getbuffer) nbytes)))
(f.getvalue)))
(meth send [data]
(@sock.send data)))
(defn expect [t expected]
(setv reply (.recvall t (len expected)))
;; (print (hello.hex) (hello.decode :errors "ignore"))
(unless (= reply expected)
(raise (Exception f"bad reply, expected {(expected.hex " ")}, received {(reply.hex " ")}"))))
(defn write-memory [t segment offset data]
(when (> (len data) 0)
(.send-frame t (+ (struct.pack "<BBHH" OP:WRITE-MEM segment offset (len data)) data))
(expect t (bytes [OP:WRITE-MEM 0x7E]))))
;; Keep trying to connect for up to 3 seconds
(defn retry-connect [process address-tuple [attempts 30] [interval-sec 0.1]]
(for [i (range attempts)]
(try
(when (is-not (process.poll) None)
(raise (Exception f"Interpreter exited with code {process.returncode}")))
(let [t (SocketTransport address-tuple)]
(return t))
(except [ConnectionRefusedError]
(when (= i (dec attempts))
(raise))
(time.sleep interval-sec)
(continue)))))
;;;
;;; Higher-level operations
;;;
(defclass Session []
(meth __init__ [transport]
(setv @transport transport)
(setv @program-state
(link.LinkInfo :bc-end 0
:function-table {}
:global-table {})))
(meth close []
(.close @transport))
(meth eval [program
execute ; one of: False "async" "sync"
[filename "stdin"]]
;; If "main" exists in function table, erase it
;; When executing a stand-alone program, self.program-state will be empty,
;; but for REPL'd statements this matters
(let [function-table @program-state.function-table]
(when (in "main" function-table)
;; Make sure "main" is the last function and erase it
;; Length of function table is used to allocate function ids, so can't delete in the middle
(assert (= (. function-table ["main"] id) (dec (len function-table))))
(del (get function-table "main"))
;; TODO: can also trim bc-end
))
(setv unit (compile.compile-unit builtin-constants
builtin-functions
filename
program
:repl-globals (list (.keys @program-state.global-table))))
(print unit)
;; link
(setv link-info (link.link-program [unit]
:output "lnk.tmp"
:builtin-functions builtin-functions
:repl-initial-state @program-state
:allow-no-main True))
(with [f (open "lnk.tmp" "rb")]
;; read header
(setv #(bc-len num-func num-glob main-func-idx) (struct.unpack "<HBBBxxx" (f.read 8)))
;; functions
(setv functions-bytes (f.read (* num-func 4)))
;; (for [func program.functions]
;; (f.write (struct.pack "<BBHHxx" func.argc func.num-locals func.bytecode-offset func.constants-offset)))
;; globals
(setv globals-bytes (f.read (* num-glob 2)))
;; bytecode
(setv bc-bytes (f.read bc-len))
)
;; make sure program is not running before we start to patch up memory
;; (it may also be in TERMINATED state, that's fine too)
(.suspend self)
(let [t @transport]
(write-memory t SEGMENT:BC @program-state.bc-end bc-bytes)
(write-memory t SEGMENT:FUNC (* 4 (len @program-state.function-table)) functions-bytes)
(write-memory t SEGMENT:GLOB (* 2 (len @program-state.global-table)) globals-bytes)
(ecase execute
"async" (do
(t.send-frame (struct.pack "<BBBB" OP:BEGIN-EXEC main-func-idx 0 0))
(expect t (bytes [OP:BEGIN-EXEC 0x7E])))
"sync" (do
(t.send-frame (struct.pack "<BBBB" OP:BEGIN-EXEC main-func-idx 0 1))
(expect t (bytes [OP:BEGIN-EXEC 0x7E]))
;; expecting: OP:STATE OP:SUSPEND <retc> <retv...>
(expect t (bytes [OP:STATE OP:SUSPEND]))
(let [retc (ord (.recvall t 1))
retv-bytes (.recvall t (* 2 retc))]
(expect t (bytes [0x7E]))
;; Note that array.array doesn't support explicit endian
(setv retv (.tolist (array.array "h" retv-bytes))))
;; print result (can be any number of values)
(if (= (len retv) 1)
(print (get retv 0))
(let [values-str (gfor x retv (str x))]
(print f"(values {(.join " " values-str)})"))))
False None))
(setv @program-state link-info)
(print "New program-state:" :end " ")
(pprint @program-state))
;; reset REPL state
(meth reset []
(setv @program-state (link.LinkInfo :bc-end 0
:function-table {}
:global-table {})))
(meth suspend []
(.send-frame @transport (bytes [OP:SUSPEND]))
(expect @transport (bytes [OP:SUSPEND 0x7E]))))
;; Compile and execute a complete STAK program
(defn execute-file [session filename]
(with [f (open filename "r")]
(let [forms (list (hy.read-many f))]
(.eval session
forms
:execute "async"
:filename filename))))
;;;
;;; Misc
;;;
(defn alternative-filenames [filename]
;; If file doesn't exist, try appending .scm
(when (not (os.path.exists filename))
(let [alt-filename (+ filename ".scm")]
(when (os.path.exists alt-filename)
(setv filename alt-filename))))
filename)
;; Execute callback once and then each time the file changes
;; Can be interrupted by an exception (e.g., KeyboardInterrupt)
(defn watch-file [path callback]
(let [last-stamp None]
(while True
(let [stamp (. (os.stat path) st_mtime)]
(when (!= stamp last-stamp)
(callback)
(setv last-stamp stamp))
(time.sleep 0.5)))))
;;;
;;; MAIN
;;;
(setv args (parse-args :spec [["-t" "--target"
:help "Attach to a running target (instead of starting new interpreter). Use 'tcp:<host>:<port>' or 'serial:<port>:<baudrate>'"]]))
(with [f (open "constants.json")]
(setv builtin-constants (json.load f)))
(with [f (open "builtins.json")]
(setv builtin-functions (json.load f)))
(cond
;; no target provided (launch our own)
(is args.target None) (do
(setv process (Popen ["./vm/stak" "-g"]
;; prevent Ctrl-C (SIGINT) propagating to VM and killing it
;; credit to https://stackoverflow.com/questions/3232613/how-to-stop-sigint-being-passed-to-subprocess-in-python#comment55906369_3731948
:preexec-fn os.setpgrp))
;; make sure interpreter doesn't outlive us
(atexit.register process.terminate)
(setv transport (retry-connect process #("localhost" 5000))))
;; TCP target
(args.target.startswith "tcp:") (do
(let [[_ host port-str] (args.target.split ":")
port (int port-str)]
(setv transport (SocketTransport #(host port)))))
;; serial target
(args.target.startswith "serial:") (do
(let [[_ port baud-str] (args.target.split ":")
baud (int baud-str)]
(try
(setv transport (SerialTransport port baud))
(except [exc serial.SerialException]
(print exc)
(exit 1)))))
:else (do
(print "Invalid target format. See(k) help.")
(exit 1)))
(.send transport b"\x7Eh\x7E") ;; send hello
(expect transport b"\x7EhSTAK\x7E")
(print "REPL is connected. Press Ctrl-D to exit.")
(setv session (Session transport))
(while True
(try
(let [inp (input ">")
f (io.StringIO inp)]
;; (print "input: " inp)
(cond
(= inp "reset") (do
(.reset session))
(.startswith inp "exec ") (do
(let [filename (.removeprefix inp "exec ")
filename (alternative-filenames filename)]
(execute-file session filename)))
(.startswith inp "watch ") (do
(let [filename (.removeprefix inp "watch ")
filename (alternative-filenames filename)]
(try
(watch-file filename (fn []
;; source changed; reset compiler state and re-run
(.reset session)
(try
(execute-file session filename)
(except [exc Exception]
;; in case of an error, print it, but keep watching the file
(print exc)))))
(except [exc FileNotFoundError]
(print exc))
(except [KeyboardInterrupt]
(print)))))
:else (do
(setv forms (list (hy.read-many f)))
;; Iterate over forms. Some forms must be evaluated in global context (define), the rest are banched and evaluated in the context of a function.
;; (define <var> <value>) is special. It is split up into a global declaration, and the assignment of the initial value which is done in a functional context.
;; This way, it can be initialized with an expression, which is normally not possible (globals must be initialized with a literal)
(setv batch [])
(defn flush []
(when (> (len batch) 0)
(.eval session
[`(define (main) ~@batch)]
:execute "sync")
(batch.clear)))
(for [form forms]
(if (and (isinstance form Expression)
(>= (len form) 1)
(= (get form 0) (Symbol "define")))
(do
(flush)
(.eval session
[form]
:execute False))
;; not a (define) form
(batch.append form)
)
)
(flush))))
(except [EOFError]
(break))
(except [e Exception]
(traceback.print-exc))
)
)
(.close session)