-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbllsh
executable file
·367 lines (318 loc) · 9.82 KB
/
bllsh
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
#!/usr/bin/env python3
import cmd
import functools
import os
import sys
import traceback
from dataclasses import dataclass, field
from typing import Optional, Tuple, List, Any, Self
readline : Optional[Any] = None
try:
import readline
except ImportError:
pass
from element import SExpr, Atom, ALLOCATOR
from opcodes import Set_GLOBAL_TX, Get_GLOBAL_TX, Set_GLOBAL_TX_INPUT_IDX, Get_GLOBAL_TX_INPUT_IDX, Set_GLOBAL_TX_SCRIPT, Get_GLOBAL_TX_SCRIPT, Set_GLOBAL_UTXOS, Get_GLOBAL_UTXOS
import bll
import symbll
import verystable.core.messages
##########
# Todo:
#
# * implement "softfork"
# * make "blleval" / "apply" available in symbll
# * misc additional opcodes
#
# * add support for "@SYM" to mean SYM turned into a bll program in general?
#
# * default values for fn arguments
# * destructuring of fn arguments?
# * defconst for compile-time constants (with quote support)
# * quasiquote support
#
# * do stuff about costs?
#
# * add tui debugger (via "textual" module?)
##########
####
def handle_exc(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception:
traceback.print_exc()
return wrapper
####
class BTCLispRepl(cmd.Cmd):
HISTSIZE = 2000
@classmethod
def histpath(cls):
HISTPATH = os.getenv("BLLSH_HISTPATH", None)
if HISTPATH is None:
HISTPATH = os.path.expanduser("~/.bllsh.history")
return HISTPATH
def __init__(self, prompt=None):
self.prompt = ">>> " if prompt is None else prompt
self.symbols = symbll.SymbolTable()
self.wi = None
cmd.Cmd.__init__(self)
def preloop(self):
if readline and os.path.exists(self.histpath()):
readline.read_history_file(self.histpath())
def postloop(self):
if readline:
readline.set_history_length(self.HISTSIZE)
readline.write_history_file(self.histpath())
def show_state(self):
if self.wi is None: return
s = ""
for ri, c in enumerate(reversed(self.wi.continuations)):
i = len(self.wi.continuations) - ri
if hasattr(c, "localsyms"):
s = " ".join(f"{k}={v}" for k,v in c.localsyms.syms.items())
if s != "": s = f" [{s}]"
print(f" -- {i}. {c.fn} {c.args}{s}")
def default(self, line):
if line.strip().startswith(";"):
# comment
return
return super().default(line)
def do_exit(self, arg):
return True
def do_EOF(self, arg):
if self.prompt != "": print()
return True
def bllparse(self, arg):
se = SExpr.parse(arg, manypy=True)
if len(se) < 1 or len(se) > 2:
for x in se: x.deref()
print("too many arguments")
return
if se[0].is_symbol() and se[0].val2[0] == '@':
prog = symbll.compile_program(se[0].val2[1:], self.symbols)
else:
prog = bll.ToBLL(se[0])
se[0].deref()
if len(se) > 1:
env = bll.ToBLL(se[1])
se[1].deref()
else:
env = Atom(0)
return prog, env
@handle_exc
def do_import(self, arg):
with open(arg, "r") as f:
for l in f:
l = l.strip()
if l == "":
continue
self.onecmd(l)
@handle_exc
def do_blleval(self, arg):
before = ALLOCATOR.x
ALLOCATOR.max = 0
prog, env = self.bllparse(arg)
r = bll.eval(prog, env)
print(r)
r.deref()
print(f"allocation: {before} -> {ALLOCATOR.x} (max: {ALLOCATOR.max})")
if before < ALLOCATOR.x:
print("allocated:")
for x in ALLOCATOR.allocated:
print(f" {x.refcnt} {x}")
@handle_exc
def do_blldebug(self, arg):
if self.wi is not None:
print("Already debugging an expression")
return
prog, env = self.bllparse(arg)
self.wi = bll.WorkItem.begin(prog, env)
self.show_state()
@handle_exc
def do_compile(self, arg):
before = ALLOCATOR.x
s = SExpr.parse(arg)
r = symbll.compile_expr(s, symbll.SymbolIndex(self.symbols, offset=2), symbll.SymbolIndex([], offset=3))
print(r)
r.deref()
s.deref()
if before < ALLOCATOR.x:
print("allocated:")
for x in ALLOCATOR.allocated:
print(f" {x.refcnt} {x}")
@handle_exc
def do_program(self, arg):
symname = arg
if symname not in self.symbols.syms:
print(f"Unknown symbol {symname}")
return
r = symbll.compile_program(symname, self.symbols)
print(r)
r.deref()
@handle_exc
def do_eval(self, arg):
before = ALLOCATOR.x
ALLOCATOR.max = 0
s = SExpr.parse(arg)
r = symbll.symbolic_eval(s, self.symbols)
print(r)
r.deref()
print(f"allocation: {before} -> {ALLOCATOR.x} (max: {ALLOCATOR.max})")
if before < ALLOCATOR.x:
print("allocated:")
for x in ALLOCATOR.allocated:
print(f" {x.refcnt} {x}")
@handle_exc
def do_debug(self, arg):
if self.wi is not None:
print("Already debugging an expression")
return
sexpr = SExpr.parse(arg)
self.wi = symbll.WorkItem.begin(sexpr, self.symbols)
self.show_state()
@handle_exc
def do_step(self, arg):
if self.wi is None:
print("No expression being debugged")
elif not self.wi.finished():
self.wi.step()
self.show_state()
else:
r = self.wi.get_result()
print(f"Result: {r}")
r.deref()
self.wi = None
@handle_exc
def do_next(self, arg):
if self.wi is None:
print("No expression being debugged")
target = len(self.wi.continuations)
while not self.wi.finished():
self.wi.step()
if len(self.wi.continuations) <= target:
break
if not self.wi.finished():
self.show_state()
else:
r = self.wi.get_result()
print(f"Result: {r}")
r.deref()
self.wi = None
@handle_exc
def do_cont(self, arg):
if self.wi is None:
print("No expression being debugged")
return
while not self.wi.finished():
self.wi.step()
r = self.wi.get_result()
print(f"Result: {r}")
r.deref()
self.wi = None
@handle_exc
def do_trace(self, arg):
if self.wi is None:
print("No expression being debugged")
return
while not self.wi.finished():
self.wi.step()
self.show_state()
print("")
r = self.wi.get_result()
print(f"Result: {r}")
r.deref()
self.wi = None
@handle_exc
def do_def(self, arg):
s = SExpr.parse(arg, manypy=True)
if len(s) != 2:
print("Expected symbol name (plus parameters) and definition")
for e in s: e.deref()
return
sym, val = s
if sym.is_symbol():
self.symbols.set(sym.val2, (Atom(0), val.bumpref()))
elif sym.is_cons() and sym.val1.is_symbol():
self.symbols.set(sym.val1.val2, (sym.val2.bumpref(), val.bumpref()))
else:
print("Expected symbol name (plus parameters) and definition")
for e in s:
e.deref()
@handle_exc
def do_undef(self, arg):
for x in arg.split():
x = x.strip()
if x == "": continue
self.symbols.unset(x)
@handle_exc
def do_tx(self, arg):
if arg == "":
tx = Get_GLOBAL_TX()
if tx is None:
print("No tx set")
else:
print(tx.serialize().hex())
else:
try:
tx = verystable.core.messages.tx_from_hex(arg)
except:
print("Could not parse tx")
return
Set_GLOBAL_TX(tx)
@handle_exc
def do_tx_in_idx(self, arg):
if arg == "":
idx = Get_GLOBAL_TX_INPUT_IDX()
if idx is None:
print("No tx input idx set")
else:
print(idx)
else:
try:
idx = int(arg)
except:
idx = None
if idx is None or idx < 0:
print("Could not parse index")
else:
Set_GLOBAL_TX_INPUT_IDX(idx)
@handle_exc
def do_tx_script(self, arg):
if arg == "":
scr = Get_GLOBAL_TX_SCRIPT()
if scr is None:
print("No tx script set")
else:
print(scr.hex())
else:
try:
scr = bytes.fromhex(arg)
except:
print("Could not parse tx script")
return
Set_GLOBAL_TX_SCRIPT(scr)
@handle_exc
def do_utxos(self, arg):
if arg == "":
utxos = Get_GLOBAL_UTXOS()
if utxos is None:
print("No utxos set")
else:
for i,u in enumerate(utxos):
print(f"{i} {u.serialize().hex()}")
else:
try:
utxos = []
for uh in arg.split(" "):
utxos.append(verystable.core.messages.from_hex(verystable.core.messages.CTxOut(), uh))
except:
print("Could not parse utxos")
return
Set_GLOBAL_UTXOS(utxos)
if __name__ == "__main__":
if os.isatty(sys.stdin.fileno()):
repl = BTCLispRepl()
else:
repl = BTCLispRepl(prompt="")
repl.cmdloop()