+/TECS

git clone https://git.julia.blue/+/TECS

P1n3appl3Finish project 11c8053ff

v1.0
1021 B28 linesraw
1# Handles input parsing and sanatization
2class CodeParser:
3    currentLine = 0
4
5    def __init__(self, fileName):
6        reader = open(fileName)
7        self.lines = filter(None, [i.split('/', 1)[0].strip() for i in reader])
8        reader.close()
9
10    def hasMoreCommands(self):
11        return self.currentLine < len(self.lines)
12
13    def advance(self):
14        self.currentLine += 1
15
16    def commandType(self):
17        types = {"push": "C_PUSH", "pop": "C_POP", "label": "C_LABEL", "goto": "C_GOTO",
18                         "if-goto": "C_IF", "function": "C_FUNCTION", "call": "C_CALL", "return": "C_RETURN"}
19        temp = self.lines[self.currentLine].split(' ')[0]
20        if temp in {"add", "sub", "neg", "eq", "gt", "lt", "and", "not", "or"}:
21            return "C_ARITHMETIC"
22        return types[temp]
23
24    def arg1(self):
25        return self.lines[self.currentLine].split(' ')[0 if self.commandType() == "C_ARITHMETIC" else 1]
26
27    def arg2(self):
28        return int(self.lines[self.currentLine].split(' ')[2])