Пример #1
0
    def visit(self, node: our_ast.LessOrEqualNode, scope):

        lvar = self.visit(node.lvalue)
        rvar = self.visit(node.rvalue)

        temp_result = self.define_internal_local()
        temp_rest = self.define_internal_local()
        end_label = cil_ast.LabelNode(f'end_{self.less_or_equal_counter}')
        continue_label = cil_ast.LabelNode(
            f'continue_{self.less_or_equal_counter}')
        not_label = cil_ast.LabelNode(f'not_{self.less_or_equal_counter}')

        self.less_or_equal_counter += 1

        self.register_instruction(cil_ast.MinusNode(temp_rest, rvar, lvar))
        self.register_instruction(cil_ast.IfNode(temp_rest, continue_label))
        self.register_instruction(cil_ast.AssignNode(temp_result, 1))

        self.register_instruction(cil_ast.GotoNode(end_label))
        self.register_instruction(continue_label)
        self.register_instruction(
            cil_ast.BranchLessThanZeroNode(temp_result, not_label.label))
        self.register_instruction(cil_ast.AssignNode(temp_result, 1))
        self.register_instruction(cil_ast.GotoNode(end_label))
        self.register_instruction(not_label)
        self.register_instruction(cil_ast.AssignNode(temp_result, 0))

        return temp_result
Пример #2
0
    def visit(self, node: our_ast.IsVoidNode, scope):
        var = self.visit(node.expr)

        type_expr = self.define_internal_local()
        self.register_instruction(cil_ast.TypeOfNode(type_expr, var))

        temp_rest = self.define_internal_local()

        self.register_instruction(
            cil_ast.MinusNode(temp_rest, type_expr, 'Void'))

        isnotvoid_label = cil_ast.LabelNode(
            f'isnotvoid_{self.is_void_counter}')
        endif_label = cil_ast.LabelNode(f'endif_{self.is_void_counter}')

        self.is_void_counter += 1

        temp_result = self.define_internal_local()
        self.register_instruction(cil_ast.IfNode(temp_rest, isnotvoid_label))
        self.register_instruction(cil_ast.AssignNode(temp_result, 1))
        self.register_instruction(cil_ast.GotoNode(endif_label))
        self.register_instruction(isnotvoid_label)
        self.register_instruction(cil_ast.AssignNode(temp_result, 0))
        self.register_instruction(endif_label)

        return temp_result
Пример #3
0
    def visit(self, node: our_ast.EqualNode, scope):

        lvar = self.visit(node.lvalue, scope)
        rvar = self.visit(node.rvalue, scope)

        temp_l = self.define_internal_local()
        temp_r = self.define_internal_local()
        notequal_label = cil_ast.LabelNode(f'notequal_{self.equal_counter}')
        endif_label = cil_ast.LabelNode(f'endif_{self.equal_counter}')

        self.equal_counter += 1

        self.register_instruction(cil_ast.AssignNode(temp_l, lvar))
        self.register_instruction(cil_ast.AssignNode(temp_r, rvar))

        temp_rest = self.define_internal_local()
        temp_result = self.define_internal_local()
        self.register_instruction(cil_ast.MinusNode(temp_rest, temp_l, temp_r))

        self.register_instruction(cil_ast.IfNode(temp_rest, notequal_label))

        self.register_instruction(cil_ast.AssignNode(temp_result, 1))
        self.register_instruction(cil_ast.GotoNode(endif_label))
        self.register_instruction(notequal_label)
        self.register_instruction(cil_ast.AssignNode(temp_result, 0))
        self.register_instruction(endif_label)

        return temp_result
Пример #4
0
    def visit(self, node: our_ast.LessThanNode, scope):

        lvar = self.visit(node.lvalue)
        rvar = self.visit(node.rvalue)

        temp_rest = self.define_internal_local()
        temp_result = self.define_internal_local()

        self.register_instruction(cil_ast.MinusNode(temp_rest, lvar, rvar))

        label_not = cil_ast.LabelNode(f'not_{self.less_than_node_counter}')
        label_endbranch = cil_ast.LabelNode(
            f'endbranch_{self.less_than_node_counter}')

        self.less_than_node_counter += 1
        # todo look this
        self.register_instruction(
            cil_ast.BranchLessThanZeroNode(temp_rest, label_not.label))
        self.register_instruction(cil_ast.AssignNode(temp_result, 1))
        self.register_instruction(cil_ast.GotoNode(label_endbranch))
        self.register_instruction(label_not)
        self.register_instruction(cil_ast.AssignNode(temp_result, 0))
        self.register_instruction(label_endbranch)

        return temp_result
Пример #5
0
    def visit(self, node: our_ast.NotNode, scope):

        expr = self.visit(node.value)

        makefalse_label = cil_ast.LabelNode(f'makefalse_{self.not_counter}')
        endif_label = cil_ast.LabelNode(f'endif_{self.not_counter}')

        self.not_counter += 1

        temp_result = self.define_internal_local()

        self.register_instruction(cil_ast.IfNode(expr, makefalse_label))
        self.register_instruction(cil_ast.AssignNode(temp_result, 1))
        self.register_instruction(cil_ast.GotoNode(endif_label))
        self.register_instruction(makefalse_label)
        self.register_instruction(cil_ast.AssignNode(temp_result, 0))
        self.register_instruction(endif_label)

        return temp_result
Пример #6
0
    def visit(self, node: our_ast.IfNode, scope):

        cond_var = self.visit(node.condition, scope)

        after = cil_ast.LabelNode(f'after_{self.if_counter}')
        endif = cil_ast.LabelNode(f'endif_{self.if_counter}')

        self.if_counter += 1

        self.register_instruction(cil_ast.IfNode(cond_var, after))

        self.visit(node.false_body, scope)

        self.register_instruction(cil_ast.GotoNode(endif))
        self.register_instruction(after)

        self.visit(node.true_body, scope)

        self.register_instruction(endif)
Пример #7
0
    def visit(self, node: our_ast.WhileNode, scope):

        cont = cil_ast.LabelNode(f'continue_{self.while_counter}')
        loop = cil_ast.LabelNode(f'loop_{self.while_counter}')
        endloop = cil_ast.LabelNode(f'endloop_{self.while_counter}')

        self.while_counter += 1

        temp = self.visit(node.condition, scope)

        self.register_instruction(cil_ast.IfNode(temp, loop))
        self.register_instruction(cil_ast.GotoNode(endloop))
        self.register_instruction(loop)

        self.visit(node.body, scope)

        self.register_instruction(cil_ast.GotoNode(cont))

        self.register_instruction(endloop)
        temp1 = self.define_internal_local()
        self.register_instruction(cil_ast.AllocateNode(temp1, 'Void'))

        return temp1