Exemplo n.º 1
0
    def generate_ast_representation(self) -> str:
        if isinstance(self.condition, Expression):
            stringified_condition = \
                self.condition.generate_ast_representation()
        elif isinstance(self.condition, Variable):
            stringified_condition = f'"{self.condition.name}"'
        elif is_number(self.condition):
            stringified_condition = f'"{self.condition}"'
        else:
            stringified_condition = ""

        if isinstance(self.left_operand, Expression):
            stringified_left_operand = \
                self.left_operand.generate_ast_representation()
        elif isinstance(self.left_operand, Variable):
            stringified_left_operand = f'"{self.left_operand.name}"'
        elif is_number(self.left_operand):
            stringified_left_operand = f'"{self.left_operand}"'
        else:
            stringified_left_operand = ""

        if isinstance(self.right_operand, Expression):
            stringified_right_operand = \
                self.right_operand.generate_ast_representation()
        elif isinstance(self.right_operand, Variable):
            stringified_right_operand = f'"{self.right_operand.name}"'
        elif is_number(self.right_operand):
            stringified_right_operand = f'"{self.right_operand}"'
        else:
            stringified_right_operand = ""
        return (f'{{"id":"{self.id}", "condition":{stringified_condition},'
                f' "left_operand":{stringified_left_operand},'
                f' "right_operand":{stringified_right_operand}}}')
Exemplo n.º 2
0
    def generate_asm_code(self) -> list:
        salt = Program.DYNAMIC_SALT
        Program.DYNAMIC_SALT += 1

        asm_code = []

        code_of_condition, condition_in_asm = \
            Expression._process_operand(self.condition)

        code_of_left_expression, left_operand_in_asm = \
            Expression._process_operand(self.left_operand)

        code_of_right_expression, right_operand_in_asm = \
            Expression._process_operand(self.right_operand)

        local_active_regs = []

        if condition_in_asm == 'reg':
            condition_in_asm = Expression.get_inactive_reg()
            code_of_condition.append(f'  pop {condition_in_asm}')
            local_active_regs.append(condition_in_asm)
        elif is_number(condition_in_asm):
            reg = Expression.get_inactive_reg()
            code_of_condition.append(f'  mov {reg}, {condition_in_asm}')
            condition_in_asm = reg
            local_active_regs.append(reg)

        if not code_of_left_expression:
            reg = Expression.get_inactive_reg()
            code_of_left_expression = (f'  mov {reg}, {left_operand_in_asm}',
                                       f'  push {reg}')
            Expression.set_reg_inactive(reg)

        if not code_of_right_expression:
            reg = Expression.get_inactive_reg()
            code_of_right_expression = (f'  mov {reg}, {right_operand_in_asm}',
                                        f'  push {reg}')
            Expression.set_reg_inactive(reg)

        indentation_separator = '\n  '

        asm_code.extend((
            *code_of_condition,
            f'  cmp {condition_in_asm}, 0',
            f'  je false{salt}',
            f'  jne true{salt}',
            f'  true{salt}:',
            f'  {indentation_separator.join(code_of_left_expression)}',
            f'    jmp continue{salt}',
            f'  false{salt}:',
            f'  {indentation_separator.join(code_of_right_expression)}',
            f'    jmp continue{salt}',
            '',
            f'  continue{salt}:'
        ))

        for reg in local_active_regs:
            Expression.set_reg_inactive(reg)
        return asm_code
Exemplo n.º 3
0
    def generate_asm_code(self) -> list:
        asm_code = []
        if isinstance(self.argument, Expression):
            return self.argument.generate_asm_code()
        elif isinstance(self.argument, Variable):
            asm_code.append(f'  mov eax, {self.argument.name_with_salt}')
        elif is_number(self.argument):
            asm_code.append(f'  mov eax, {self.argument}')
        asm_code.append('  push eax')

        return asm_code
Exemplo n.º 4
0
 def generate_ast_representation(self) -> str:
     if isinstance(self.value, Expression):
         stringified_value = \
             self.value.generate_ast_representation()
     elif isinstance(self.value, Variable):
         stringified_value = f'"{self.value.name}"'
     elif is_number(self.value):
         stringified_value = f'"{self.value}"'
     else:
         stringified_value = ""
     return (f'{{"id":"{self.id}", "operator":"{self.operator}",'
             f' "value":{stringified_value}}}')
Exemplo n.º 5
0
 def generate_ast_representation(self) -> str:
     if isinstance(self.expression, Expression):
         stringified_expression = \
             self.expression.generate_ast_representation()
     elif isinstance(self.expression, Variable):
         stringified_expression = f'"{self.expression.name}"'
     elif is_number(self.expression):
         stringified_expression = f'"{self.expression}"'
     else:
         stringified_expression = ""
     return (f'{{"id":"{self.id}", "name":"{self.name}",'
             f' "expression":{stringified_expression}}}')
Exemplo n.º 6
0
    def generate_asm_code(self) -> list:
        asm_code = []
        arguments_in_asm = []
        for argument in self.arguments:
            asm_code_of_argument, _argument_in_asm = \
                Expression._process_operand(argument)
            asm_code.extend(asm_code_of_argument)
            arguments_in_asm.append(_argument_in_asm)

        for idx, argument_in_asm in enumerate(arguments_in_asm):
            if argument_in_asm == 'reg':
                reg = Expression.get_inactive_reg()
                asm_code.append(f'  pop {reg}')
                arguments_in_asm[idx] = reg
            elif is_number(argument_in_asm):
                reg = Expression.get_inactive_reg()
                asm_code.append(f'  mov {reg}, {int(argument_in_asm)}')
                arguments_in_asm[idx] = reg

        if len(arguments_in_asm):
            repr_of_arguments_in_asm = ', '.join(arguments_in_asm)
            asm_code.append(
                f'  invoke {self.function.name_with_salt}, '
                f'{repr_of_arguments_in_asm}'
            )
        else:
            asm_code.append(f'  invoke {self.function.name_with_salt}')

        for reg in Expression.get_active_regs():
            if reg in arguments_in_asm:
                Expression.set_reg_inactive(reg)

        asm_code.append('  push eax')

        Expression.set_reg_inactive('eax')

        return asm_code
Exemplo n.º 7
0
    def generate_asm_code(self) -> list:
        salt = Program.DYNAMIC_SALT
        Program.DYNAMIC_SALT += 1

        asm_code = [f'  continue{salt}:']
        for instruction in self.body:
            if instruction is None or isinstance(instruction, Variable):
                continue
            if instruction in ('break', 'continue'):
                asm_code.append(f'  jmp {instruction}{salt}')
            else:
                asm_code.extend(instruction.generate_asm_code())

        asm_code_of_expression,\
            expression_in_asm = Expression._process_operand(self.expression)

        local_active_regs = []

        if expression_in_asm == 'reg':
            expression_in_asm = Expression.get_inactive_reg()
            asm_code_of_expression.append(f'  pop {expression_in_asm}')
            local_active_regs.append(expression_in_asm)
        elif is_number(expression_in_asm):
            reg = Expression.get_inactive_reg()
            asm_code_of_expression.append(f'  mov {reg}, {expression_in_asm}')
            expression_in_asm = reg
            local_active_regs.append(reg)

        asm_code.extend((*asm_code_of_expression,
                         f'  cmp {expression_in_asm}, 0',
                         f'  je break{salt}',
                         f'  jne continue{salt}',
                         f'  break{salt}:'))

        for reg in local_active_regs:
            Expression.set_reg_inactive(reg)
        return asm_code
Exemplo n.º 8
0
    def generate_asm_code(self) -> list:
        asm_code = []

        asm_code_of_expression,\
            operand_in_asm = Expression._process_operand(
                self.value)
        asm_code.extend(asm_code_of_expression)

        reg = Expression.get_inactive_reg()

        if operand_in_asm == 'reg':
            operand_in_asm = reg
            asm_code.append(f'  pop {operand_in_asm}')
        elif is_number(self.value):
            operand_in_asm = reg
            asm_code.append(f'  mov {operand_in_asm}, {int(self.value)}')

        asm_code.extend((
            f'  neg {operand_in_asm}',
            f'  push {operand_in_asm}'
        ))

        Expression.set_reg_inactive(reg)
        return asm_code