Esempio n. 1
0
 def evaluate(self, st):
     if self.value == "TRUE":
         Assembler.clean_line()
         Assembler.write_line("MOV EBX, {}".format("True"))
         return (True, "BOOLEAN")
     else:
         Assembler.write_line("MOV EBX, {}".format("False"))
         return (False, "BOOLEAN")
Esempio n. 2
0
 def evaluate(self, st):
     # _, relexp_type = self.children[0].evaluate(st)
     Assembler.clean_line()
     Assembler.write_line("LOOP_{}:".format(self.id))
     _, relexp_type = self.children[0].evaluate(st)
     
     if relexp_type == "BOOLEAN":
         Assembler.write_line("CMP EBX, False")
         Assembler.write_line("JE EXIT_{}".format(self.id))
         for child in self.children[1]:
             child.evaluate(st)
         Assembler.write_line("JMP LOOP_{}".format(self.id))
         Assembler.write_line("EXIT_{}:".format(self.id))
         Assembler.clean_line()
     else:
         raise ValueError("AST Error (While): {} is not a valid relational expression type".format(relexp_type))
Esempio n. 3
0
    def evaluate(self, st):
        child_r = self.children[0].evaluate(st)
        Assembler.write_line("PUSH EBX")

        child_l = self.children[1].evaluate(st)
        Assembler.write_line("POP EAX")
        Assembler.clean_line()

        if child_r[1] == child_l[1]:
            if self.value == "-":
                Assembler.write_line("SUB EAX, EBX      ; Subtraction: {} - {}".format(child_r[0], child_l[0]))
                Assembler.write_line("MOV EBX, EAX")
                Assembler.clean_line()
                return (child_r[0] - child_l[0], "INT")

            elif self.value == "+":
                Assembler.write_line("ADD EAX, EBX      ; Addition: {} + {}".format(child_r[0], child_l[0]))
                Assembler.write_line("MOV EBX, EAX")
                Assembler.clean_line()
                return (child_r[0] + child_l[0], "INT")

            elif self.value == "*":
                Assembler.write_line("IMUL EBX          ; Multiplication: {} * {}".format(child_r[0], child_l[0]))
                Assembler.write_line("MOV EBX, EAX")
                Assembler.clean_line()
                return (child_r[0] * child_l[0], "INT")

            elif self.value == "/":
                Assembler.write_line("IDIV EBX          ; Division: {} / {}".format(child_r[0], child_l[0]))
                Assembler.write_line("MOV EBX, EAX")
                Assembler.clean_line()
                return (child_r[0] // child_l[0], "INT")

            elif self.value == ">":
                Assembler.write_line("CMP EAX, EBX      ; Greater-than: {} > {}".format(child_r[0], child_l[0]))
                Assembler.write_line("CALL binop_jg")
                Assembler.clean_line()
                return (child_r[0] > child_l[0], "BOOLEAN")

            elif self.value == "<":
                Assembler.write_line("CMP EAX, EBX      ; Less-than: {} < {}".format(child_r[0], child_l[0]))
                Assembler.write_line("CALL binop_jl")
                Assembler.clean_line()
                return (child_r[0] < child_l[0], "BOOLEAN")

            elif self.value == "=":
                Assembler.write_line("CMP EAX, EBX      ; Equal: {} == {}".format(child_r[0], child_l[0]))
                Assembler.write_line("CALL binop_je")
                Assembler.clean_line()
                return (child_r[0] == child_l[0], "BOOLEAN")

            elif self.value == "OR":
                Assembler.write_line("OR EAX, EBX       ; Or: {} | {}".format(child_r[0], child_l[0]))
                Assembler.write_line("MOV EBX, EAX")
                Assembler.clean_line()
                return (child_r[0] or child_l[0], "BOOLEAN")

            elif self.value == "AND":
                Assembler.write_line("AND EAX, EBX      ; And: {} & {}".format(child_r[0], child_l[0]))
                Assembler.write_line("MOV EBX, EAX")
                Assembler.clean_line()
                return (child_r[0] and child_l[0], "BOOLEAN")
        else:
            raise ValueError("AST Error (BinOp): Operation can not be performed: {} {} {} ".format(child_r[0], self.value, child_l[0]))
Esempio n. 4
0
 def evaluate(self, st):
     self.children[0].evaluate(st)
     Assembler.write_line("PUSH EBX")
     Assembler.write_line("CALL print")
     Assembler.write_line("POP EBX")
     Assembler.clean_line()
Esempio n. 5
0
 def evaluate(self, st):
     Assembler.clean_line()
     Assembler.write_line("MOV EBX, {}".format(self.value))
     return (self.value, "INT")