Beispiel #1
0
    def Expression_ST(self):  # 7. Expression = ["+"|"-"] Term {("+"|"-") Term}
        tmpStartPos = self.curPos

        op = None
        zero_num_node = Node.Number(None, 0, tmpStartPos, tmpStartPos,
                                    self.myErrHandler)
        if self.current.Type == "keyword" and self.current.Value_Str in [
                "+", "-"
        ]:
            op = self.match(["+", "-"])
        ret_node = self.Term_ST()
        tmpEndPos = self.curPos - 1
        if op == "-":
            if isinstance(ret_node, Node.Number):
                ret_node = self.Constant_Folding(op, zero_num_node, ret_node,
                                                 tmpStartPos, tmpEndPos,
                                                 self.myErrHandler)
            else:
                ret_node = Node.Binary(op, zero_num_node, ret_node,
                                       tmpStartPos, tmpEndPos,
                                       self.myErrHandler)

        while self.current.Type == "keyword" and self.current.Value_Str in [
                "+", "-"
        ]:
            op = self.match(["+", "-"])
            Right_node = self.Term_ST()
            tmpEndPos = self.curPos - 1
            if isinstance(ret_node, Node.Number) and isinstance(
                    Right_node, Node.Number):  # Constant folding
                ret_node = self.Constant_Folding(op, ret_node, Right_node,
                                                 tmpStartPos, tmpEndPos,
                                                 self.myErrHandler)
            else:
                ret_node = Node.Binary(op, ret_node, Right_node, tmpStartPos,
                                       tmpEndPos, self.myErrHandler)
        return ret_node
Beispiel #2
0
    def Term_ST(self):  # 8. Term = Factor {("*"|"DIV"|"MOD") Factor}
        tmpStartPos = self.curPos

        ret_node = self.Factor_ST()
        while self.current.Type == "keyword" and self.current.Value_Str in [
                "*", "DIV", "MOD"
        ]:
            op = self.match(["*", "DIV", "MOD"])
            Right_node = self.Factor_ST()
            tmpEndPos = self.curPos - 1

            if isinstance(ret_node, Node.Number) and isinstance(
                    Right_node, Node.Number):  # Constant folding
                ret_node = self.Constant_Folding(op, ret_node, Right_node,
                                                 tmpStartPos, tmpEndPos,
                                                 self.myErrHandler)
            else:
                ret_node = Node.Binary(op, ret_node, Right_node, tmpStartPos,
                                       tmpEndPos, self.myErrHandler)

        return ret_node