-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.py
53 lines (46 loc) · 1.3 KB
/
converter.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
import sys
import os
import numParser
import getopt
import exceptions
import configParser
def main():
# Gets the command line arguments
opts, _ = getopt.getopt(sys.argv[1:], "ri:o:")
inputfile = ""
outputfile = "output.txt"
# Variable that checks if we're trying to generate from a machine parser
fromMachineTable = False
for opt, arg in opts:
if(opt == "-r"):
fromMachineTable = True
elif(opt == "-i"):
inputfile = arg
elif(opt == "-o"):
outputfile = arg
# Read file
try:
file = open(inputfile, "r")
except:
print("Input file not found! Usage: converter.py (-r) -i inputfile.txt -o outputfile.txt")
sys.exit(2)
number = file.read()
file.close()
# Creating teh correct parser
if(not fromMachineTable):
parser = numParser.numParser()
else:
parser = configParser.configParser()
# Write output to file
outFile = open(outputfile, "w")
try:
outFile.write(parser.parse(number))
except exceptions.IllegalMachineNumberException as err:
print(err)
sys.exit(2)
except exceptions.IllegalMachineInstructionException as err:
print(err)
sys.exit(2)
outFile.close()
if __name__ == "__main__":
main()