This repository was archived by the owner on Jun 30, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathc.py
More file actions
executable file
·137 lines (112 loc) · 4.38 KB
/
Copy pathc.py
File metadata and controls
executable file
·137 lines (112 loc) · 4.38 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
# ---------------------------------------------------------------
# c.py
#
# Atul Varma
# Python C Compiler - Front-end
# $Id: c.py,v 1.3 2004/05/27 17:52:19 varmaa Exp $
#
# This is the main program for the compiler, which just parses
# command-line options, figures out which source files to read
# and write to, and invokes the different stages of the
# compiler proper.
# ---------------------------------------------------------------
import yacc
import cparse, cvisitors, cx86
import sys
class Compiler:
"""This object encapsulates the front-end for the compiler and
serves as a facade interface to the 'meat' of the compiler
underneath."""
class CompileError(Exception):
"""Exception raised when there's been a compilation error."""
pass
def __init__(self):
self.total_errors = 0
self.total_warnings = 0
def _parse(self):
"""Parses the source code."""
self.ast = yacc.parse(self.code)
def _compile_phase(self, visitor):
"""Applies a visitor to the abstract syntax tree."""
visitor.visit(self.ast)
self.total_errors += visitor.errors
self.total_warnings += visitor.warnings
if visitor.has_errors():
raise Compiler.CompileError()
def _do_compile(self, outfile, ast_file, show_comments):
"""Compiles the code to the given file object. Enabling
show_ast prints out the abstract syntax tree."""
self._parse()
self._compile_phase(cvisitors.SymtabVisitor())
self._compile_phase(cvisitors.TypeCheckVisitor())
self._compile_phase(cvisitors.FlowControlVisitor())
self._compile_phase(cx86.CodeGenVisitor(outfile,
show_comments))
if ast_file != None:
self._compile_phase(cvisitors.ASTPrinterVisitor(ast_file))
def _print_stats(self):
"""Prints the total number of errors/warnings from compilation."""
print "%d errors, %d warnings." % (self.total_errors, self.total_warnings)
def compile(self, code, outfile, show_ast, show_comments):
"""Compiles the given code string to the given file object."""
self.code = code
try:
self._do_compile(outfile, show_ast, show_comments)
except cparse.ParseError:
print "Errors encountered, bailing."
return 1
except Compiler.CompileError:
self._print_stats()
print "Errors encountered, bailing."
return 1
self._print_stats()
print "Compile successful."
return 0
def run_compiler():
"""Runs the command-line compiler."""
if len(sys.argv) < 2:
print "Usage: c.py <source-file-1> [[source-file-2] ...] [-ast] [-annotate]"
sys.exit(1)
show_ast = 0
show_comments = 0
params = sys.argv[1:]
files = sys.argv[1:]
for param in params:
if param[0] == '-':
if param == '-ast':
show_ast = 1
elif param == '-annotate':
print "Annotated assembly generation enabled."
show_comments = 1
else:
print "Unknown option: %s" % param
sys.exit(1)
files.remove(param)
for file in files:
source_filename = file
dest_filename = file[:-2]+'.s'
print "Compiling %s -> %s." % (source_filename, dest_filename)
open_files = []
ast_file = None
if show_ast:
ast_filename = file[:-2]+'.ast'
print "Outputting AST to %s." % ast_filename
ast_file = open(ast_filename, 'w')
open_files.append(ast_file)
source = open(source_filename, 'r')
code = source.read()
source.close()
dest = open(dest_filename, 'w')
open_files.append(dest)
retval = Compiler().compile(code, dest, ast_file, show_comments)
for f in open_files:
f.close()
if retval != 0:
sys.exit(retval)
print
sys.exit(retval)
if __name__ == '__main__':
run_compiler()
# ---------------------------------------------------------------
# End of c.py
# ---------------------------------------------------------------