forked from runtimeverification/iele-semantics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kast-json.py
executable file
·51 lines (45 loc) · 1.39 KB
/
kast-json.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
#!/usr/bin/env python2
import json
import sys
from collections import OrderedDict
filename = sys.argv[1]
with open(filename) as data_file:
data = json.load(data_file, object_pairs_hook=OrderedDict)
def escape(data):
return data.encode('unicode_escape')
def print_kast(data):
if isinstance(data, list):
sys.stdout.write('`[_]_IELE-DATA`(')
for elem in data:
sys.stdout.write('`_,__IELE-DATA`(')
print_kast(elem)
sys.stdout.write(',')
sys.stdout.write('`.List{"_,__IELE-DATA"}`(.KList)')
for elem in data:
sys.stdout.write(')')
sys.stdout.write(')')
elif isinstance(data, OrderedDict):
sys.stdout.write('`{_}_IELE-DATA`(')
for key, value in data.iteritems():
sys.stdout.write('`_,__IELE-DATA`(`_:__IELE-DATA`(')
print_kast(key)
sys.stdout.write(',')
print_kast(value)
sys.stdout.write('),')
sys.stdout.write('`.List{"_,__IELE-DATA"}`(.KList)')
for key in data:
sys.stdout.write(')')
sys.stdout.write(')')
elif isinstance(data, unicode):
sys.stdout.write('#token('),
sys.stdout.write(json.dumps(json.dumps(data)))
sys.stdout.write(',"String")')
elif isinstance(data, long) or isinstance(data, int):
sys.stdout.write('#token("'),
sys.stdout.write(str(data))
sys.stdout.write('","Int")')
else:
sys.stdout.write(type(data))
raise AssertionError
print_kast(data)
print