forked from runtimeverification/wasm-semantics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessor.py
37 lines (30 loc) · 976 Bytes
/
preprocessor.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
# Preprocessor that converts Wasm concrete syntax into a form parseable by K.
# example usage: python convert.py f32.wast
import re
import sys
def hex2float(h):
h = re.sub("_", "", h)
if "nan" in h:
# TODO: Keep bit pattern of float, don't turn all of them into simple NaNs.
return re.sub("-?nan(:.*$)?", "NaN", h)
elif "inf" in h:
return h.replace("inf", "Infinity")
elif "0x" in h:
try:
return h.split()[0] + " " + "%e" % (float.fromhex(h.split()[1]))
except OverflowError:
return h
else:
return h
def main():
if len(list(sys.argv)) == 1:
infile = sys.stdin
else:
infile = open(sys.argv[1])
for line in (infile.readlines()):
sys.stdout.write(re.sub(r"(?:(?:f32|f64)\.const )([^\)]+)",
lambda m: hex2float(m.group()),
line))
infile.close()
if __name__ == "__main__":
main()