-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
52 lines (44 loc) · 1.88 KB
/
driver.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
import sys
sys.path.insert(0, 'src/')
from cparser import Parser
from symboltable import SymbolTable
from MIPSGenerator import MipsGenerator
import argparse
# for more information goto https://docs.python.org/2/library/argparse.html
parser = argparse.ArgumentParser(description='Compiler for cs660.')
parser.add_argument("source",nargs='?',type=str,help="Specifies the input source file.")
parser.add_argument("-p",nargs=1,default=" ",type=str,dest="parselogfile",metavar='Parse Log Output', help="Specifies the parse log output file for production shifts and reduces.")
parser.add_argument("-t",nargs=1,default="tokenfile.log",type=str,dest="tokenfile", metavar='Token Log Output', help="The token log output file specifier.")
parser.add_argument("-v",default="Version 1.0.0",type=str,metavar='Version information.')
parser.add_argument("-g",default="tree.png",metavar="The graph picture file.",dest="graphfile")
parser.add_argument("-i",default="3AC.tac",metavar="The name of the .tac file.",dest="tacfile")
parser.add_argument("-c",action='store_true',dest="codeout",help="If exists then write the code to file.")
args = parser.parse_args()
if args.source != None:
#print args.source
# Read the file from command line
input_file = open(args.source, 'r')
data = input_file.read()
input_file.close()
# Build and Call the scanner
if type(args.tokenfile) != str:
args.tokenfile = args.tokenfile[0]
scan = Parser(data, args.parselogfile != " ", args.parselogfile[0], args.tokenfile, args.graphfile, args.tacfile, args.codeout)
st = SymbolTable()
scan.set_symbol_table(st)
#scan.scan(data)
scan.run()
bas = open("log/3AC.tac",'r')
a = bas.read()
bas.close()
generator = MipsGenerator()
assembly = generator.Parse(a)
temp = args.source.split(".")
filename = ""
for i in temp[:len(temp)-1]:
filename += i
filename += "."
filename += "asm"
asm = open(filename, "w")
asm.write(assembly)
asm.close()