-
Notifications
You must be signed in to change notification settings - Fork 7
/
bchc
executable file
·88 lines (72 loc) · 2.95 KB
/
bchc
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
#!/usr/bin/python3
import pdb
import re
import sys
import argparse
import io
import json
from bchscript.bchutil import *
from bchscript.bchprimitives import *
from bchscript.bchimmediates import immediates, BchAddress
from bchscript.bchscript import *
def main(argv):
parser = argparse.ArgumentParser(prog="bchc", description='Compile BchScript')
parser.add_argument('input', nargs="?", help='input file or string program', default=None)
parser.add_argument('--hex', action='store_true', default=False, dest='hex', help='compile into hex')
parser.add_argument('--opc', action='store_true', default=False, dest='opcode', help='compile into opcodes')
parser.add_argument('--display', action='store', default='main', dest='display', help='what to output')
parser.add_argument('-I', action='append', dest='includeDirs', help='specify a directory for included files')
parser.add_argument('-o', action='store', dest='output', help='write output to specified file')
params = parser.parse_args(argv[1:])
if params.hex == False and params.opcode == False:
params.hex = True
inp = None
if params.input:
try:
inp = open(params.input, "r")
except FileNotFoundError as e:
if "OP" in params.input: # try it as a text script
inp = params.input
else:
inp = sys.stdin
inputFileDir = os.path.dirname(os.path.abspath(params.input))
allIncludeDirs = [os.getcwd(), inputFileDir] + (params.includeDirs if params.includeDirs else [])
out = []
if inp:
result = compile(inp, allIncludeDirs)
if params.display == 'all':
ret = {}
for (key, value) in result.items():
d = {}
for (scriptType, t) in value.items():
if scriptType == "satisfier":
xformParams = (True, True)
else:
xformParams = (False, True)
outp = []
if params.hex:
outp.append(ToHex(applyTemplate(script2bin(t, *xformParams))))
if params.opcode:
outp.append(prettyPrint(t))
d[scriptType] = outp
ret[key] = d
out = [json.dumps(ret,sort_keys=True, indent=4)]
else:
try:
t = result[params.display]["constraint"]
except KeyError:
sys.stderr.write("Error: script must include a '%s' scriptify! command\n" % params.display)
return
xformParams = (False, True)
if params.hex:
out.append(ToHex(applyTemplate(script2bin(t, *xformParams))))
if params.opcode:
out.append(prettyPrint(t))
if params.output:
outf = open(params.output, "w")
else:
outf = sys.stdout
outf.write("\n".join(out))
outf.write("\n")
if __name__ == "__main__":
main(sys.argv)