-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpclc.py
159 lines (136 loc) · 4.17 KB
/
pclc.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
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
import argparse
import sys
import os
import warnings
import copy
from pcl import PCLCError, PCLError
from pcl import PCLLexer
from pcl import PCLParser
from pcl import PCLCodegen
__version__ = '0.0.1'
ABOUT_MSG = '''
The PCL Language Compiler
😷 Made with <3 during quarantine by (alphabetically)
1. Giannis Daras <[email protected]>
2. Marios Papachristou <[email protected]>
Version: {}
Repo 🌟 : https://github.com/papachristoumarios/pcl
Docs 📖 : https://github.com/papachristoumarios/pcl/wiki
'''.format(__version__)
def hook(exctype, value, tb):
sys.stderr.write('{}: {}\n'.format(exctype.__name__, value))
sys.exit(1)
def get_argparser():
argparser = argparse.ArgumentParser(usage=ABOUT_MSG)
argparser.add_argument(
'filename',
nargs='?',
default='',
type=str,
help='Input filename')
argparser.add_argument('-O', default=0, type=int,
help='Optimization level')
argparser.add_argument(
'--pipeline',
nargs='+',
default=[
'lex',
'parse',
'sem',
'codegen'])
argparser.add_argument('-W', action='store_true', help='Enable warnings')
argparser.add_argument(
'-f',
action='store_true',
help='Input from stdin, bin output to stdout')
argparser.add_argument(
'-i',
action='store_true',
help='Input from stdin, IR output to stdout')
argparser.add_argument(
'-v',
action='store_true',
help='Output version'
)
argparser.add_argument(
'--debug',
action='store_true',
help='Print the whole traceback (useful for debugging)'
)
argparser.add_argument(
'--encoding',
default='ascii',
type=str
)
return argparser
class PCLCDriver:
def __init__(self, program):
self.program = program
self.lexer = PCLLexer()
self.parser = PCLParser()
self.parsed = None
self.tokens = None
def lex(self):
self.tokens = self.lexer.tokenize(self.program)
if len(list(self.tokens)) == 0:
raise PCLCError('Please enter a non-empty program')
else:
self.tokens = self.lexer.tokenize(self.program)
def parse(self):
self.parsed = self.parser.parse(self.tokens)
def pprint(self):
if self.parsed:
self.parsed.pprint()
else:
for token in self.tokens:
print(token)
def sem(self):
self.parsed.sem()
def codegen(self):
self.parsed.codegen()
def print_module(self):
self.parsed.print_module()
if __name__ == '__main__':
argparser = get_argparser()
args = argparser.parse_args()
if not args.debug:
# sys.tracebacklimit = 0
sys.excepthook = hook
if not args.W:
warnings.simplefilter("ignore")
if args.v:
print(__version__)
exit(0)
else:
if (args.filename != '') ^ args.i ^ args.f:
if args.filename != '':
with open(args.filename, encoding=args.encoding) as f:
program = f.read()
else:
sys.stdin.reconfigure(encoding=args.encoding)
program = sys.stdin.read()
args.filename = 'a.pcl'
else:
sys.stderr.write('Multiple Inputs defined\n')
exit(1)
driver = PCLCDriver(program)
pipeline_funcs = {
'lex': driver.lex,
'parse': driver.parse,
'sem': driver.sem,
'pprint': driver.pprint,
'codegen': driver.codegen,
}
for stage in args.pipeline:
pipeline_funcs[stage]()
if 'codegen' == args.pipeline[-1]:
driver.parser.codegen.postprocess_module(level=args.O)
name = os.path.splitext(args.filename)[0]
if args.i:
# IR to stdout
print(driver.parser.codegen.module)
elif args.f:
# Object file to stdout
driver.parser.codegen.generate_outputs(name, llc_to_stdout=True)
else:
driver.parser.codegen.generate_outputs(name, llc_to_stdout=False)