示例#1
0
    def parseRelExpression():

        node = Parser.parseExpression()

        if Parser.tokens.actual.tp == "IS":
            Parser.tokens.selectNext()

            if Parser.tokens.actual.tp in ["EQUAL", "GREATER", "LESS"]:

                if Parser.tokens.actual.tp == "EQUAL":
                    Parser.tokens.selectNext()
                    if Parser.tokens.actual.tp == "TO":
                        node = BinOp("=", [node, Parser.parseExpression()])

                elif Parser.tokens.actual.tp == "GREATER":
                    Parser.tokens.selectNext()

                    if Parser.tokens.actual.tp == "THAN":
                        Parser.tokens.selectNext()
                        node = BinOp(">", [node, Parser.parseExpression()])

                elif Parser.tokens.actual.tp == "LESS":
                    Parser.tokens.selectNext()

                    if Parser.tokens.actual.tp == "THAN":
                        Parser.tokens.selectNext()
                        node = BinOp("<", [node, Parser.parseExpression()])

        return node
示例#2
0
 def parseArgumentsFunction(self, function_name):
     token = self.tokens.actual
     if token.type == "OPEN_PAR": 
         while True:
             token = self.tokens.selectNext()
             if token.type == "IDE": 
                 list_arguments.append(token.value) 
                 token = self.tokens.selectNext()
                 if token.type == "VAR_DECLARATION":
                     break
                 elif token.type == "COMMA":
                     pass
                 else:
                     raise ValueError("Invalid token, expecting a : or , on position \
                          {}".format(self.tokens.position))
             else:
                 raise ValueError("Invalid token, expecting a identifier on position \
                          {}".format(self.tokens.position))
         token = self.tokens.selectNext()
         if token.type == "TYPE":
             arguments = MultiVarDec(None, [])
             for var_name in list_arguments:
                 var_name = StrVal(var_name, [])
                 value = StrVal(token.value, [])
                 variable = BinOp(":", [var_name, value])
                 arguments.children.append(variable)
             token = self.tokens.selectNext()
             if token.type == "CLOSE_PAR": 
                 token = self.tokens.selectNext()
                 if token.type == "VAR_DECLARATION":
                     token = self.tokens.selectNext()
                     if token.type == "TYPE":
                         return_var_name = StrVal(function_name, [])
                         return_type = StrVal(token.value, [])
                         variable = BinOp(":", [return_var_name,
                                                return_type])
                         arguments.children.append(variable)
                         token = self.tokens.selectNext()
                         if token.type == "SEMI_COLON":
                             return arguments
                         else:
                             raise ValueError("Invalid token, expecting a ; on position \
                          {}".format(self.tokens.position))
                     else:
                         raise ValueError("Invalid token, expecting a type on position \
                          {}".format(self.tokens.position))
                 else:
                     raise ValueError("Invalid token, expecting a : on position \
                          {}".format(self.tokens.position))
             else:
                 raise ValueError("Invalid token, expecting a ) on position \
                          {}".format(self.tokens.position))
         else:
             raise ValueError("Invalid token, expecting a type on position \
                          {}".format(self.tokens.position))
     else:
         raise ValueError("Invalid token, expecting a ( on position \
                          {}".format(self.tokens.position))
示例#3
0
 def parseVariables(self):
     token = self.tokens.actual
     result = VarDec(None, [])
     if token.type != "begin":
         if token.type == "var":
             token = self.tokens.selectNext()
             while True:
                 list_vars = []
                 while True:
                     if token.type == "IDE":
                         list_vars.append(token.value)
                         token = self.tokens.selectNext()
                         if token.type == "COMMA":
                             token = self.tokens.selectNext()
                         elif token.type == "VAR_DECLARATION":
                             break
                         else:
                             raise ValueError(
                                 "Invalid token, expecting a , or : on position \
                                  {}".format(self.tokens.position))
                     else:
                         raise ValueError(
                             "Invalid token, expecting a identifier on position \
                                  {}".format(self.tokens.position))
                 token = self.tokens.selectNext()
                 if token.type == "TYPE":
                     for var_name in list_vars:
                         var_name = StrVal(var_name, [])
                         value = StrVal(token.value, [])
                         variable = BinOp(":", [var_name, value])
                         result.children.append(variable)
                     token = self.tokens.selectNext()
                     if token.type == "SEMI_COLON":
                         token = self.tokens.selectNext()
                         if token.type == "begin":
                             break
                         elif token.type == "function":
                             break
                         elif token.type == "IDE":
                             pass
                         else:
                             raise ValueError(
                                 "Invalid token, expecting a begin \
                                              or identifier on position {}".
                                 format(self.tokens.position))
                     else:
                         raise ValueError(
                             "Invalid token, expecting a ; on position \
                                          {}".format(self.tokens.position))
                 else:
                     raise ValueError(
                         "Invalid token, expecting a type on position \
                              {}".format(self.tokens.position))
         else:
             raise ValueError("Invalid token, expecting a var on position \
                              {}".format(self.tokens.position))
     return result
示例#4
0
    def parseExpression():

        node = Parser.parseTerm()

        while Parser.tokens.actual.tp in ["PLUS","MINUS","OR"]:

            if Parser.tokens.actual.tp == "PLUS":
                Parser.tokens.selectNext()
                node = BinOp("+",[node,Parser.parseTerm()])

            elif Parser.tokens.actual.tp == "MINUS":
                Parser.tokens.selectNext()
                node = BinOp("-",[node,Parser.parseTerm()])

            elif Parser.tokens.actual.tp == "OR":
                Parser.tokens.selectNext()
                node = BinOp("OR",[node,Parser.parseTerm()])

        return node
示例#5
0
    def parseTerm():

        node = Parser.parseFactor()

        while Parser.tokens.actual.tp in ["DIV","MULT","AND"]:

            if Parser.tokens.actual.tp == "MULT":
                Parser.tokens.selectNext()
                node = BinOp("*",[node,Parser.parseFactor()])

            elif Parser.tokens.actual.tp == "DIV":
                Parser.tokens.selectNext()
                node = BinOp("/",[node,Parser.parseFactor()])

            elif Parser.tokens.actual.tp == "AND":
                Parser.tokens.selectNext()
                node = BinOp("AND",[node,Parser.parseFactor()])

        return node
示例#6
0
 def parseTerm(self):
     result = self.parseFactor()
     while True:
         token = self.tokens.actual
         if token is None:
             break
         elif token.type == "MULT":
             self.tokens.selectNext()
             second_value = self.parseFactor()
             result = BinOp("*", [result, second_value])
         elif token.type == "DIV":
             self.tokens.selectNext()
             second_value = self.parseFactor()
             result = BinOp("/", [result, second_value])
         elif token.type == "and":
             self.tokens.selectNext()
             second_value = self.parseFactor()
             result = BinOp("and", [result, second_value])
         else:
             break
     return result
示例#7
0
 def parseExpression(self):
     result = self.parseTerm()
     while True:
         token = self.tokens.actual
         if token is None:
             break
         if token.type == "PLUS":
             self.tokens.selectNext()
             second_value = self.parseTerm()
             result = BinOp("+", [result, second_value])
         elif token.type == "MINUS":
             self.tokens.selectNext()
             second_value = self.parseTerm()
             result = BinOp("-", [result, second_value])
         elif token.type == "or":
             self.tokens.selectNext()
             second_value = self.parseTerm()
             result = BinOp("or", [result, second_value])
         else:
             break
     return result
示例#8
0
 def parseRelExpression(self):
     self.tokens.selectNext()
     value1 = self.parseExpression()
     token = self.tokens.actual
     if token.type == 'COMP':
         self.tokens.selectNext()
         value2 = self.parseExpression()
         result = BinOp(token.value, [value1, value2])
     else:
         raise ValueError("Invalid token, expecting a <, >, = or != \
                          on position {}".format(self.tokens.position))
     return result
    def expr(self):
        node = self.term()

        while self.current_token.type in (lexer.PLUS, lexer.MINUS):
            token = self.current_token
            if token.type == lexer.PLUS:
                self.eat(lexer.PLUS)
            elif token.type == lexer.MINUS:
                self.eat(lexer.MINUS)

            node = BinOp(left=node, op=token, right=self.term())

        return node
    def term(self):
        node = self.factor()

        while self.current_token.type in (lexer.MUL, lexer.DIV):
            token = self.current_token
            if token.type == lexer.MUL:
                self.eat(lexer.MUL)
            elif token.type == lexer.DIV:
                self.eat(lexer.DIV)

            node = BinOp(left=node, op=token, right=self.factor())

        return node
示例#11
0
    def parseTerm():

        node = Parser.parseFactor()

        while Parser.tokens.actual.tp in ["DIVIDED", "TIMES", "AND"]:

            if Parser.tokens.actual.tp == "TIMES":
                Parser.tokens.selectNext()
                node = BinOp("*", [node, Parser.parseFactor()])

            elif Parser.tokens.actual.tp == "DIVIDED":
                Parser.tokens.selectNext()
                if Parser.tokens.actual.tp == "BY":
                    Parser.tokens.selectNext()
                    node = BinOp("/", [node, Parser.parseFactor()])

                else:
                    raise ValueError("to do a division must write divided by")

            elif Parser.tokens.actual.tp == "AND":
                Parser.tokens.selectNext()
                node = BinOp("AND", [node, Parser.parseFactor()])

        return node
示例#12
0
 def parseAtribution(self):
     value1 = StrVal(self.tokens.actual.value, [])
     token = self.tokens.selectNext()
     if (token.type == "ATRIBUTE"):
         token = self.tokens.selectNext()
         if (token.type == "read"):
             value2 = self.parseRead()
         else:
             value2 = self.parseExpression()
         result = BinOp(":=", [value1, value2])
     else:
         raise ValueError(
             "Invalid token, expecting a := on position {}".format(
                 self.tokens.position))
     return result