def _parse(self): level=0 for element in self.elements: if element[0]=='<': if element[1]=='!': continue if element[1]!='/': node=Node(element,level) parent=self.stack[-1] node.set_parent(parent) parent.add_child(node) self.nodes.append(node) self.stack.append(node) if not node.is_single(): level+=1 else: self.stack.pop() else: self.stack.pop() level-=1 else: self.nodes[-1].set_content(element)
def type_check_add(node1, node2, op, token): allowed_class = [('PointerType', 'BasicType'), ('BasicType', 'PointerType'), ('BasicType', 'BasicType')] allowed_base = [{'int', 'long', 'char'}, {'int', 'long', 'char'}, {'int', 'long', 'char', 'float', 'bool'}] if node1.type == "error" or node2.type == "error": return Node(type="error") if "sconst@" in node1.place or "sconst@" in node2.place: Errors(errorType='TypeError', errorText=op + ' not support string constant', token_object=token) return Node(type="error") class1 = node1.type.class_type class2 = node2.type.class_type if (class1, class2) not in allowed_class: Errors(errorType='TypeError', errorText=op + ' not support type ' + node1.type.stype + ',' + node2.type.stype, token_object=token) return Node(type="error") i = allowed_class.index((class1, class2)) if i == 0: if node2.type.type not in allowed_base[i]: Errors(errorType='TypeError', errorText=op + ' not support type ' + node1.type.stype + ',' + node2.type.stype, token_object=token) return Node(type="error") node = Node(name="binary_op", value=node1.type.stype + op, type=node1.type, children=[node1, node2]) node.code = node1.code + node2.code width = node1.type.type_size const_place = get_const(width, type="long") tmp, code = get_opcode(op="long*", place1=node2.place, place2=const_place, type="long") node.code += [code] tmp, code = get_opcode(op="long" + op, place1=node1.place, place2=tmp, type="long") node.code += [code] node.place = tmp return node if i == 1: if node1.type.type not in allowed_base[i]: Errors(errorType='TypeError', errorText=op + ' not support type ' + node1.type.stype + ',' + node2.type.stype, token_object=token) return Node(type="error") node = Node(name="binary_op", value=node2.type.stype + op, type=node2.type, children=[node2, node1]) node.code = node1.code + node2.code width = node2.type.type_size const_place = get_const(width, type="long") tmp, code = get_opcode(op="long*", place1=node1.place, place2=const_place, type="long") node.code += [code] tmp, code = get_opcode(op="long" + op, place1=node2.place, place2=tmp, type="long") node.code += [code] node.place = tmp return node if i == 2: if node1.type.type not in allowed_base[ i] or node2.type.type not in allowed_base[i]: Errors(errorType='TypeError', errorText=op + ' not support type ' + node1.type.stype + ',' + node2.type.stype, token_object=token) return Node(type="error") node1, node2, typ = implicit_casting(node1, node2) node = Node(name="binary_op", value=typ.stype + op, children=[node1, node2], type=typ) node.code = node1.code + node2.code tmp, code = get_opcode(op=typ.stype + op, place1=node1.place, place2=node2.place, type=typ) node.code += [code] node.place = tmp return node