Example #1
0
    def execute_ASSIGNMENT_STATEMENT(self, strLine):
        # print("HI:",strLine)
        terms = self.removeGarbageFromArray(re.split(' |(\=\=)|(\=)', strLine))
        equation = ""
        indexOfEQ = 0
        for i, e in reversed(list(enumerate(terms))):
            if e == "=":
                break
            equation = e + equation
            indexOfEQ = i
        del terms[indexOfEQ:]
        isBooleanEquation = self.interpreter.isValidBooleanOperation(equation)
        if (isBooleanEquation):
            eqTerms = re.split(
                ' |(\()|(\))|(\=\=)|(\<\=)|(\>\=)|(\&\&)|(\|\|)|(\<)|(\>)',
                equation)
        else:
            eqTerms = re.split(' |(\+)|(\/)|(\-)|(\*)|(\%)|(\()|(\))',
                               equation)
        eqTerms = self.removeGarbageFromArray(eqTerms)

        nodeList = []
        for term in eqTerms:
            temp = Interpreter.Node(term)
            nodeList.append(temp)
        if isBooleanEquation:
            node = self.nodeCreate(nodeList,
                                   ["==", "<=", ">=", "<", ">", "&&", "||"])
        else:
            node = self.nodeCreate(nodeList)
        val = self.evaluateNode(node)
        variables = [x for x in terms if x is not '=']
        for variable in variables:
            self.setVariable(variable, val)
Example #2
0
 def solveArithmeticEquation(self, equation):
     eqTerms = re.split(' |(\+)|(\/)|(\-)|(\*)|(\%)|(\()|(\))', equation)
     eqTerms = self.removeGarbageFromArray(eqTerms)
     nodeList = []
     for term in eqTerms:
         temp = Interpreter.Node(term)
         nodeList.append(temp)
     node = self.nodeCreate(nodeList)
     return self.evaluateNode(node)
Example #3
0
 def solveBooleanEquation(self, equation):
     isBooleanEquation = self.interpreter.isValidBooleanOperation(equation)
     eqTerms = re.split(
         '(\()|(\))|(\=\=)|(\<\=)|(\>\=)|(\&\&)|(\|\|)|(\<)|(\>)', equation)
     eqTerms = self.removeGarbageFromArray(eqTerms)
     nodeList = []
     for term in eqTerms:
         if (self.interpreter.isBooleanOperator(term)):
             print('Boolean Operator:', term)
         if (not self.interpreter.isBooleanOperator(term)
                 and self.interpreter.isValidArithmeticOperation(term)):
             term = self.solveArithmeticEquation(term)
             print('New term', term)
         temp = Interpreter.Node(term)
         nodeList.append(temp)
     node = self.nodeCreate(nodeList,
                            ["==", "<=", ">=", "<", ">", "&&", "||"])
     return self.evaluateNode(node)