-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathbasic2.py
More file actions
43 lines (41 loc) · 1007 Bytes
/
basic2.py
File metadata and controls
43 lines (41 loc) · 1007 Bytes
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
def main():
code = {}
prompt = True
while True:
try:
s = input('Ready.\n' if prompt else '').strip()
except Exception as e:
break
if s == '':
prompt = False
continue
if s == 'exit': break
prompt = True
if s[0] == '?':
s = 'print(' + s[1:] + ')'
if s == 'list' or s == 'lI':
print()
for n, ln in sorted(code.items()):
print(n, ln)
elif s == 'run':
lines = '\n'.join(ln for _, ln in sorted(code.items()))
run(lines)
elif s == 'new':
code = {}
print()
elif s[0].isdigit():
ln = s[1:]
if ln:
code[s[0]] = ln
else:
del code[s[0]]
prompt = False
else:
run(s)
def run(s):
try:
exec(s, globals())
except Exception as e:
print(e)
print()
main()