-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbril2json.py
More file actions
350 lines (279 loc) · 8.09 KB
/
Copy pathbril2json.py
File metadata and controls
350 lines (279 loc) · 8.09 KB
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
"""A text format for Bril.
This module defines both a parser and a pretty-printer for a
human-editable representation of Bril programs. There are two commands:
`bril2txt`, which takes a Bril program in its (canonical) JSON format and
pretty-prints it in the text format, and `bril2json`, which parses the
format and emits the ordinary JSON representation.
"""
import lark
import sys
import json
__version__ = "0.0.1"
# Text format parser.
GRAMMAR = r"""
start: (struct | func)*
struct: STRUCT IDENT "=" "{" mbr* "}"
mbr: IDENT ":" type ";"
func: FUNC ["(" arg_list? ")"] [tyann] "{" instr* "}"
arg_list: | arg ("," arg)*
arg: IDENT ":" type
?instr: const | vop | eop | label
const.4: IDENT [tyann] "=" "const" lit ";"
vop.3: IDENT [tyann] "=" op ";"
eop.2: op ";"
label.1: LABEL ":"
op: IDENT (FUNC | LABEL | IDENT)*
?tyann: ":" type
lit: SIGNED_INT -> int
| BOOL -> bool
| SIGNED_FLOAT -> float
| "nullptr" -> nullptr
| CHAR -> char
type: IDENT "<" type ">" -> paramtype
| IDENT -> primtype
BOOL: "true" | "false"
STRUCT: "struct"
CHAR: /'.'/ | /'\\[0abtnvfr]'/
IDENT: ("_"|"%"|LETTER) ("_"|"%"|"."|LETTER|DIGIT)*
FUNC: "@" IDENT
LABEL: "." IDENT
COMMENT: /#.*/
%import common.SIGNED_INT
%import common.SIGNED_FLOAT
%import common.WS
%import common.LETTER
%import common.DIGIT
%ignore WS
%ignore COMMENT
""".strip()
control_chars = {
"\\0": 0,
"\\a": 7,
"\\b": 8,
"\\t": 9,
"\\n": 10,
"\\v": 11,
"\\f": 12,
"\\r": 13,
}
def _pos(token):
"""Generate a position dict from a Lark token."""
return {"row": token.line, "col": token.column}
class JSONTransformer(lark.Transformer):
def __init__(self, include_pos=False):
super().__init__()
self.include_pos = include_pos
def start(self, items):
structs = [i for i in items if "mbrs" in i]
funcs = [i for i in items if "mbrs" not in i]
if structs:
return {
"structs": structs,
"functions": funcs,
}
else:
return {
"functions": funcs,
}
def func(self, items):
name, args, typ = items[:3]
instrs = items[3:]
func = {
"name": str(name)[1:], # Strip `@`.
"instrs": instrs,
}
if args:
func["args"] = args
if typ:
func["type"] = typ
if self.include_pos:
func["pos"] = _pos(name)
return func
def arg(self, items):
name = items.pop(0)
typ = items.pop(0)
return {
"name": name,
"type": typ,
}
def struct(self, items):
name = items[1]
mbrs = items[2:]
return {
"name": name,
"mbrs": mbrs,
}
def mbr(self, items):
name = items.pop(0)
typ = items.pop(0)
return {
"name": name,
"type": typ,
}
def arg_list(self, items):
return items
def const(self, items):
dest, type, val = items
out = {
"op": "const",
"dest": str(dest),
"value": val,
}
if type:
out["type"] = type
if self.include_pos:
out["pos"] = _pos(dest)
return out
def vop(self, items):
dest, type, op = items
out = {"dest": str(dest)}
if type:
out["type"] = type
out.update(op)
if self.include_pos:
out["pos"] = _pos(dest)
return out
def op(self, items):
op_token = items.pop(0)
opcode = str(op_token)
funcs = []
labels = []
args = []
for item in items:
if item.type == "FUNC":
funcs.append(str(item)[1:])
elif item.type == "LABEL":
labels.append(str(item)[1:])
else:
args.append(str(item))
out = {"op": opcode}
if args:
out["args"] = args
if funcs:
out["funcs"] = funcs
if labels:
out["labels"] = labels
if self.include_pos:
out["pos"] = _pos(op_token)
return out
def eop(self, items):
(op,) = items
return op
def label(self, items):
(name,) = items
out = {
"label": str(name)[1:] # Strip `.`.
}
if self.include_pos:
out["pos"] = _pos(name)
return out
def int(self, items):
return int(str(items[0]))
def bool(self, items):
if str(items[0]) == "true":
return True
else:
return False
def paramtype(self, items):
return {items[0]: items[1]}
def primtype(self, items):
return str(items[0])
def float(self, items):
return float(items[0])
def nullptr(self, items):
return 0
def char(self, items):
value = str(items[0])[1:-1] # Strip `'`.
if value in control_chars:
return chr(control_chars[value])
return value
def parse_bril(txt, include_pos=False):
"""Parse a Bril program and return a JSON string.
Optionally include source position information.
"""
parser = lark.Lark(GRAMMAR, maybe_placeholders=True)
tree = parser.parse(txt)
data = JSONTransformer(include_pos).transform(tree)
return json.dumps(data, indent=2, sort_keys=True)
# Text format pretty-printer.
def type_to_str(type):
if isinstance(type, dict):
assert len(type) == 1
key, value = next(iter(type.items()))
return "{}<{}>".format(key, type_to_str(value))
else:
return type
def value_to_str(type, value):
if not isinstance(type, dict) and type.lower() == "char":
control_chars_reverse = {y: x for x, y in control_chars.items()}
if ord(value) in control_chars_reverse:
value = control_chars_reverse[ord(value)]
return "'{}'".format(value)
else:
return str(value).lower()
def instr_to_string(instr):
if instr["op"] == "const":
tyann = ": {}".format(type_to_str(instr["type"])) if "type" in instr else ""
return "{}{} = const {}".format(
instr["dest"],
tyann,
value_to_str(instr["type"], instr["value"]),
)
else:
rhs = instr["op"]
if instr.get("funcs"):
rhs += " {}".format(" ".join("@{}".format(f) for f in instr["funcs"]))
if instr.get("args"):
rhs += " {}".format(" ".join(instr["args"]))
if instr.get("labels"):
rhs += " {}".format(" ".join(".{}".format(f) for f in instr["labels"]))
if "dest" in instr:
tyann = ": {}".format(type_to_str(instr["type"])) if "type" in instr else ""
return "{}{} = {}".format(
instr["dest"],
tyann,
rhs,
)
else:
return rhs
def print_instr(instr):
print(" {};".format(instr_to_string(instr)))
def print_label(label):
print(".{}:".format(label["label"]))
def args_to_string(args):
if args:
return "({})".format(
", ".join(
"{}: {}".format(arg["name"], type_to_str(arg["type"])) for arg in args
)
)
else:
return ""
def print_func(func):
typ = func.get("type", "void")
print(
"@{}{}{} {{".format(
func["name"],
args_to_string(func.get("args", [])),
": {}".format(type_to_str(typ)) if typ != "void" else "",
)
)
for instr_or_label in func["instrs"]:
if "label" in instr_or_label:
print_label(instr_or_label)
else:
print_instr(instr_or_label)
print("}")
def print_prog(prog):
for func in prog["functions"]:
print_func(func)
# Command-line entry points.
def bril2json():
print(parse_bril(sys.stdin.read(), "-p" in sys.argv[1:]))
def bril2txt():
print_prog(json.load(sys.stdin))
with open("program.bril", "r") as f:
# Read the entire file
content = f.read()
with open("out.json", "w+") as o:
o.write(parse_bril(content))