def execute(self, action_num, token): if action_num == 1: self.insert = True elif action_num == 2: self.insert = False elif action_num == 3: self.action_3() elif action_num == 4: self.action_4(token) elif action_num == 6: self.is_array = True elif action_num == 7: self.action_7(token) elif action_num == 9: self.action_9() elif action_num == 13: self.action_13(token) elif action_num == 30: id = self.lookup(token) if not id: raise SemanticActionError("undeclared variable", token.line()) self.stack.append(id) elif action_num == 31: self.action_31(token) # Don't think we need to pass in a token elif action_num == 40: if token.type() != "UNARYPLUS" and token.type() != "UNARYMINUS": raise SemanticActionError("expected uplus or uminus", token.line()) self.stack.append(token) elif action_num == 42: # oh jeez, so hackish if token.type()[-2:] != "OP": raise SemanticActionError("expected an operator", token.line()) self.stack.append(token) elif action_num == 44: # oh jeez, so hackish if token.type()[-2:] != "OP": raise SemanticActionError("expected an operator", token.line()) self.stack.append(token) elif action_num == 55: self.backpatch(self.global_store, self.global_mem) self.generate("free", self.global_mem) self.generate("procend") elif action_num == 56: self.generate("procbegin", "main") self.global_store = self.quads.get_next_quad() self.generate("alloc", "_") else: # TODO: in the future, when valid numbers are implemented, raise error print("Action number invalid or currently not supported.")
def action_9(self): if len(self.stack) < 3 or \ self.stack[-1].type() != "IDENTIFIER" or \ self.stack[-2].type() != "IDENTIFIER" or \ self.stack[-3].type() != "IDENTIFIER": raise SemanticActionError("expected three identifiers", token.line()) id1 = self.stack.pop() id2 = self.stack.pop() id3 = self.stack.pop() io_entry1 = IODeviceEntry(id1.value()) io_entry2 = IODeviceEntry(id2.value()) procedure_entry = ProcedureEntry(id3.value(), 0, None) io_entry1.reserved = True io_entry2.reserved = True procedure_entry.reserved = True self.global_table.insert(id1.value(), io_entry1) self.global_table.insert(id2.value(), io_entry2) self.global_table.insert(id3.value(), procedure_entry) self.insert = False self.generate("call", "main", "0") self.generate("exit")
def action_4(self, token): # Only possible types passing through here are either integer or real if token.type() != "INTEGER" and token.type() != "REAL": raise SemanticActionError("expected type of integer or real", token.line()) else: self.stack.append(token)
def action_31(self, token): self.dump() # TODO: make sure you check for empty list errors when popping anything # these are all entry types id2 = self.stack.pop() offset = None id1 = self.stack.pop() if typecheck(id1, id2) == 3: raise SemanticActionError("type mismatch", token.line()) # which line should we use? if typecheck(id1, id2) == 2: tmp = self.create("tmp", id1.type()) # id1 is a real here?
def action_45(self, token): id2 = self.stack.pop() op = self.stack.pop() id1 = self.stack.pop() opcode = op.op_code() check = self.type_check(id1, id2) if check != 0 and (op.is_div() or op.is_mod()): # Only integers are valid operand for DIV and MOD raise SemanticActionError("bad parameter, expected type integer", token.line()) if check == 0: # Assume MULOPS if op.is_mod(): tmp1 = self.create("tmp", "INTEGER") tmp2 = self.create("tmp", "INTEGER") tmp3 = self.create("tmp", "INTEGER") self.generate("div", id1, id2, tmp1) self.generate("mul", id2, tmp1, tmp2) self.generate("sub", id1, tmp2, tmp3) self.stack.append(tmp3) elif op.value() == 2: # / tmp1 = self.create("tmp", "REAL") tmp2 = self.create("tmp", "REAL") tmp3 = self.create("tmp", "REAL") self.generate("ltof", id1, tmp1) self.generate("ltof", id2, tmp2) self.generate("fdiv", tmp1, tmp2, tmp3) self.stack.append(tmp3) else: tmp = self.create("tmp", "INTEGER") self.generate(opcode, id1, id2, tmp) self.stack.append(tmp) elif check == 1: tmp = self.create("tmp", "REAL") self.generate("f" + opcode, id1, id2, tmp) self.stack.append(tmp) elif check == 2: tmp1 = self.create("tmp", "REAL") tmp2 = self.create("tmp", "REAL") self.generate("ltof", id2, tmp1) self.generate("f" + opcode, id1, tmp1, tmp2) self.stack.append(tmp2) elif check == 3: tmp1 = self.create("tmp", "REAL") tmp2 = self.create("tmp", "REAL") self.generate("ltof", id1, tmp1) self.generate("f" + opcode, tmp1, id2, tmp2) self.stack.append(tmp2)
def action_46(self, token): if token.type() == "IDENTIFIER": id = self.lookup(token.value()) if id is None: raise SemanticActionError("undeclared variable", token.line()) self.stack.append(id) elif token.type() == "INTCONSTANT" or token.type() == "REALCONSTANT": id = self.lookup(token.value()) if id is None: if (token.type() == "INTCONSTANT"): id = ConstantEntry(token.value(), "INTEGER") elif (token.type() == "REALCONSTANT"): id = ConstantEntry(token.value(), "REAL") self.constant_table.insert(id.name, id) self.stack.append(id)
def action_31(self, token): id2 = self.stack.pop() offset = None id1 = self.stack.pop() check = self.type_check(id1, id2) if check == 3: raise SemanticActionError("type mismatch", token.line()) if check == 2: tmp = self.create("tmp", id1.type) self.generate("ltof", id2, tmp) if offset is None: self.generate("move", tmp, id1) else: self.generate("stor", tmp, offset, id1) else: if offset is None: self.generate("move", id2, id1) else: self.generate("stor", id2, offset, id1)
def action_13(self, token): if token.type() == "IDENTIFIER": self.stack.append(token) else: raise SemanticActionError("expected identifier", token.line())
def action_7(self, token): if token.type() == "INTCONSTANT": self.stack.append(token) else: raise SemanticActionError("expected integer constant", token.line())
def action_4(self, token): if token.type() != "INTEGER" and token.type() != "REAL": raise SemanticActionError("expected type of integer or real", token.line()) else: self.stack.append(token)
def execute(self, action_num, token): if action_num == 1: self.insert = True elif action_num == 2: self.insert = False elif action_num == 3: self.action_3() elif action_num == 4: self.action_4(token) elif action_num == 6: self.is_array = True elif action_num == 7: self.action_7(token) elif action_num == 9: self.action_9() elif action_num == 13: self.action_13(token) elif action_num == 30: id = self.lookup(token.value()) if not id: raise SemanticActionError("undeclared variable", token.line()) print(id.name) self.stack.append(id) elif action_num == 31: self.action_31(token) elif action_num == 40: if token.type() != "UNARYPLUS" and token.type() != "UNARYMINUS": raise SemanticActionError("expected uplus or uminus", token.line()) self.stack.append(token) elif action_num == 41: self.action_41(token) elif action_num == 42: if token.type()[-2:] != "OP": raise SemanticActionError("expected an operator", token.line()) self.stack.append(token) elif action_num == 43: self.action_43(token) elif action_num == 44: if token.type()[-2:] != "OP": raise SemanticActionError("expected an operator", token.line()) self.stack.append(token) elif action_num == 45: self.action_45(token) elif action_num == 46: self.action_46(token) elif action_num == 48: self.action_48(token) elif action_num == 55: self.backpatch(self.global_store, self.global_mem) self.generate("free", self.global_mem) self.generate("PROCEND") elif action_num == 56: self.generate("PROCBEGIN", "main") self.global_store = self.quads.get_next_quad() self.generate("alloc", "_") else: # TODO: in the future, when valid numbers are implemented, raise error print("Action number invalid or currently not supported.")