-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaiorepl.py
196 lines (179 loc) · 7.4 KB
/
aiorepl.py
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
# MIT license; Copyright (c) 2022 Jim Mussared
import micropython
import re
import sys
import time
import uasyncio as asyncio
# Import statement (needs to be global, and does not return).
_RE_IMPORT = re.compile("^import ([^ ]+)( as ([^ ]+))?")
_RE_FROM_IMPORT = re.compile("^from [^ ]+ import ([^ ]+)( as ([^ ]+))?")
# Global variable assignment.
_RE_GLOBAL = re.compile("^([a-zA-Z0-9_]+) ?=[^=]")
# General assignment expression or import statement (does not return a value).
_RE_ASSIGN = re.compile("[^=]=[^=]")
# Command hist (One reserved slot for the current command).
_HISTORY_LIMIT = const(10 + 1)
async def execute(code, g, s):
if not code.strip():
return
try:
if "await " in code:
# Execute the code snippet in an async context.
if m := _RE_IMPORT.match(code) or _RE_FROM_IMPORT.match(code):
code = f"global {m.group(3) or m.group(1)}\n {code}"
elif m := _RE_GLOBAL.match(code):
code = f"global {m.group(1)}\n {code}"
elif not _RE_ASSIGN.search(code):
code = f"return {code}"
code = f"""
import uasyncio as asyncio
async def __code():
{code}
__exec_task = asyncio.create_task(__code())
"""
async def kbd_intr_task(exec_task, s):
while True:
if ord(await s.read(1)) == 0x03:
exec_task.cancel()
return
l = {"__exec_task": None}
exec(code, g, l)
exec_task = l["__exec_task"]
# Concurrently wait for either Ctrl-C from the stream or task
# completion.
intr_task = asyncio.create_task(kbd_intr_task(exec_task, s))
try:
try:
return await exec_task
except asyncio.CancelledError:
pass
finally:
intr_task.cancel()
try:
await intr_task
except asyncio.CancelledError:
pass
else:
# Excute code snippet directly.
try:
try:
micropython.kbd_intr(3)
try:
return eval(code, g)
except SyntaxError:
# Maybe an assignment, try with exec.
return exec(code, g)
except KeyboardInterrupt:
pass
finally:
micropython.kbd_intr(-1)
except Exception as err:
print(f"{type(err).__name__}: {err}")
# REPL task. Invoke this with an optional mutable globals dict.
async def task(g=None, prompt="--> "):
# print("Starting asyncio REPL...")
if g is None:
g = __import__("__main__").__dict__
pass
try:
micropython.kbd_intr(-1)
s = asyncio.StreamReader(sys.stdin)
# clear = True
hist = [None] * _HISTORY_LIMIT
hist_i = 0 # Index of most recent entry.
hist_n = 0 # Number of history entries.
c = 0 # ord of most recent character.
t = 0 # timestamp of most recent character.
while True:
hist_b = 0 # How far back in the history are we currently.
sys.stdout.write(prompt)
cmd = ""
while True:
b = await s.read(1)
c = ord(b)
pc = c # save previous character
pt = t # save previous time
t = time.ticks_ms()
if c < 0x20 or c > 0x7E:
if c == 0x0A:
# CR
sys.stdout.write("\n")
if cmd:
# Push current command.
hist[hist_i] = cmd
# Increase history length if possible, and rotate ring forward.
hist_n = min(_HISTORY_LIMIT - 1, hist_n + 1)
hist_i = (hist_i + 1) % _HISTORY_LIMIT
result = await execute(cmd, g, s)
if result is not None:
sys.stdout.write(repr(result))
sys.stdout.write("\n")
break
elif c == 0x08 or c == 0x7F:
# Backspace.
if cmd:
cmd = cmd[:-1]
sys.stdout.write("\x08 \x08")
elif c == 0x02:
# Ctrl-B
continue
elif c == 0x03:
# Ctrl-C
if pc == 0x03 and time.ticks_diff(t, pt) < 20:
# Two very quick Ctrl-C (faster than a human
# typing) likely means mpremote trying to
# escape.
asyncio.new_event_loop()
return
sys.stdout.write("\n")
break
elif c == 0x04:
# Ctrl-D
sys.stdout.write("\n")
# Shutdown asyncio.
asyncio.new_event_loop()
return
elif c == 0x1B:
# Start of escape sequence.
key = await s.read(2)
if key in ("[A", "[B"):
# Stash the current command.
hist[(hist_i - hist_b) % _HISTORY_LIMIT] = cmd
# Clear current command.
b = "\x08" * len(cmd)
sys.stdout.write(b)
sys.stdout.write(" " * len(cmd))
sys.stdout.write(b)
# Go backwards or forwards in the history.
if key == "[A":
hist_b = min(hist_n, hist_b + 1)
else:
hist_b = max(0, hist_b - 1)
# Update current command.
cmd = hist[(hist_i - hist_b) % _HISTORY_LIMIT]
sys.stdout.write(cmd)
elif c == 0x0a or c == 0x0b: # ctrl-j or ctrl-k
# Stash the current command.
hist[(hist_i - hist_b) % _HISTORY_LIMIT] = cmd
# Clear current command.
b = "\x08" * len(cmd)
sys.stdout.write(b)
sys.stdout.write(" " * len(cmd))
sys.stdout.write(b)
# Go backwards or forwards in the history.
if key == 0x0a:
hist_b = min(hist_n, hist_b + 1)
else:
hist_b = max(0, hist_b - 1)
# Update current command.
cmd = hist[(hist_i - hist_b) % _HISTORY_LIMIT]
sys.stdout.write(cmd)
else:
# sys.stdout.write("\\x")
# sys.stdout.write(hex(c))
pass
else:
sys.stdout.write(b)
cmd += b
finally:
micropython.kbd_intr(3)