def _parse_name(self, name): name_split = name.split("@", 1) if len(name_split) < 2: errors.xml_struct("fail to decode name {}".format(name)) return {'type': name_split[0].lower(), 'name': name_split[1]}
def _check_header(self): root = self._xml_tree.getroot() if (root.tag.lower() != "program"): errors.xml_struct("root tag is not program") if ('language' not in self._get_lower_attrib(root) or self._get_lower_attrib(root)['language'].lower() != 'ippcode21'): errors.xml_struct("bad language attribute for root tag")
def _opcode(self, inscruction): attrs = self.program._get_lower_attrib(inscruction) if 'opcode' not in attrs: errors.xml_struct("no opcode for inscruction ip:{}".format( self.ip)) return attrs['opcode'].lower()
def _sort_params(self, params): sorted_params = [] for j in range(len(params)): for i in range(len(params)): if params[i].tag.lower() == "arg{}".format(j + 1): sorted_params.append(params[i]) if len(sorted_params) != len(params): errors.xml_struct("unexpected tag in arguments for {}".format( params.tag.lower())) return sorted_params
def _sort_tree(self): last_min = -math.inf root = self._xml_tree.getroot() self._sort_index = [] for index in range(self.length()): local_min = {'value': math.inf, 'index': math.nan} for inst in range(self.length()): if ('order' not in self._get_lower_attrib(root[inst])): errors.xml_struct("missing order attribute for inscruction") if root[inst].tag.lower() != "instruction": errors.xml_struct("bad tag {}".format(root[inst].tag)) try: el_order = int(self._get_lower_attrib(root[inst])['order']) except ValueError as error: errors.xml_struct("bad order attribute for inscruction: {}".format(error)) if (el_order < local_min['value'] and el_order > last_min): local_min['value'] = el_order local_min['index'] = inst elif el_order == local_min['value']: errors.xml_struct("two or more same orders: {}".format(el_order)) self._sort_index.append(local_min['index']) last_min = local_min['value']
def _get_typeval(self, arg): try: vartype = self.program._get_lower_attrib(arg)['type'] except KeyError: errors.xml_struct("no type attribute for arg") if vartype not in ['int', 'float', 'string', 'label', 'bool', 'var', 'type', 'nil']: errors.xml_struct("bad type in type attrib {}".format(vartype)) if vartype != 'var' and vartype != 'label': varvalue = arg.text if varvalue == None: varvalue = "" return "{}@{}".format(vartype, varvalue) else: # @todo check var/label is valid return arg.text
def args_check(self, needs, args, uninit = False): if len(needs) != len(args): errors.xml_struct("bad args count for opcodes ip:{}".format(self.ip)) for i in range(len(needs)): try: vartype = self.program._get_lower_attrib(args[i])['type'] except KeyError: errors.xml_struct("no type attribute for arg ip:{}".format(self.ip)) if args[i].tag.lower() == ("arg" + str(i)): errors.xml_struct("bad arg tag name name for {}".format(self.ip)) if "var" in needs[i] or "label" in needs[i]: if vartype not in needs[i]: errors.operands_types("bad type of operand expected {} given ['{}'] ip:{}".format(needs[i], vartype,self.ip)) elif self._type_of(args[i]) not in needs[i]: if not uninit: self._value(args[i]) errors.operands_types("bad type of operand expected {} given {} ip:{}".format(needs[i], self._type_of(args[i]), self.ip))
def undefined(self, args): errors.xml_struct("Undefined OPCODE ip:{}".format(self.ip))