def __init__(self, left, op, right): super().__init__() if (typeChecking(left.eval(), right.eval()) == True): if (isinstance(left.eval(), str) or numberCheck(left.eval(), right.eval())): self.left = left self.left.parent = self self.right = right self.right.parent = self self.op = op else: raise sbml_lexer.SemanticError() else: raise sbml_lexer.SemanticError()
def __init__(self, left, oper, right): if (typeChecking(left.eval(), right.eval()) == True): super().__init__() self.left = left self.right = right self.oper = oper self.type = "binOp" if ((oper == '-') and (numberCheck(left.eval(), right.eval()) == False)): raise sbml_lexer.SemanticError() elif ((oper == '+') and (not (isinstance(left.eval(), str) or numberCheck(left.eval(), right.eval()) or isinstance(left.eval(), list)))): raise sbml_lexer.SemanticError() elif ((oper == '*') and (numberCheck(left.eval(), right.eval()) == False)): raise sbml_lexer.SemanticError() elif ((oper == '/') and (numberCheck(left.eval(), right.eval()) == False)): raise sbml_lexer.SemanticError() elif ((oper == '**') and (numberCheck(left.eval(), right.eval()) == False)): raise sbml_lexer.SemanticError() elif ((oper == 'div') and (not (isinstance(left.eval(), int) and isinstance(right.eval(), int)))): raise sbml_lexer.SemanticError() elif ((oper == 'mod') and (not (isinstance(left.eval(), int) and isinstance(right.eval(), int)))): raise sbml_lexer.SemanticError() else: raise sbml_lexer.SemanticError()
def eval(self): tempLeft = self.left tempRight = self.right if(self.left in names): tempLeft = names[self.left] elif (primTypeCheck(self.left)==False): #CHECK IF PRIM TYPE tempLeft = self.left.eval() if(self.right in names): tempRight = names[self.right] elif (primTypeCheck(self.right)==False): #CHECK IF PRIM TYPE tempRight = self.right.eval() if((self.oper =='-')and (numberCheck(tempLeft,tempRight)==False)): raise sbml_lexer.SemanticError() if (self.oper== '-'): return (tempLeft-tempRight) # return (self.left-self.right) elif((self.oper=='+') and (typeChecking(tempLeft,tempRight)==False)): raise sbml_lexer.SemanticError() elif((self.oper == '+') and (not (isinstance(tempLeft, str) or numberCheck(tempLeft,tempRight) or isinstance(tempLeft, list)))): raise sbml_lexer.SemanticError() elif (self.oper=='+'): return (tempLeft+tempRight) elif((self.oper == '*') and (numberCheck(tempLeft,tempRight)==False)): raise sbml_lexer.SemanticError() elif (self.oper=='*'): return (tempLeft*tempRight) elif((self.oper == '/') and (numberCheck(tempLeft,tempRight)==False)): raise sbml_lexer.SemanticError() elif (self.oper=='/'): if(tempRight==0): raise sbml_lexer.SemanticError() return float(tempLeft)/float(tempRight) elif((self.oper == '**') and (numberCheck(tempLeft,tempRight)==False)): raise sbml_lexer.SemanticError() elif (self.oper=='**'): if(numberCheck(tempLeft, tempRight)==False): raise sbml_lexer.SemanticError() return (tempLeft**tempRight) elif((self.oper=='div') and (not (isinstance(tempLeft, int) and isinstance(tempRight, int)))): raise sbml_lexer.SemanticError() elif (self.oper=='div'): return int(tempLeft/tempRight) #DOUBLE CHECK HTIS elif((self.oper=='mod') and (not (isinstance(tempLeft, int) and isinstance(tempRight, int)))): raise sbml_lexer.SemanticError() elif(self.oper=='mod'): return (tempLeft)%(tempRight)
def eval(self): tempRight = self.right #if in stack if (len(stack) > 0): if (type(tempRight) != list): if (tempRight in stack[len(stack) - 1]["names"]): tempRight = stack[len(stack) - 1]["names"][tempRight] else: if (tempRight in stack[len(stack) - 1]["names"].values()): tempRight = stack[len(stack) - 1]["names"][tempRight] if (type(tempRight) != list): if (tempRight in names): tempRight = names[tempRight] elif (type(tempRight) != str): tempRight = self.right.eval() else: if (tempRight in names.values()): tempRight = names[tempRight] elif (type(tempRight) != str): tempRight = self.right.eval() tempLeft = self.left.eval() if ((not isinstance(tempRight, list)) and (not isinstance(tempRight, tuple)) and (not isinstance(tempRight, str))): raise sbml_lexer.SemanticError() return tempLeft in tempRight #double check
def eval(self): val = self.value if(val in names): val = names[val] if((type(val)==str) or (type(val)==int) or (type(val)==float) or (type(val)==list) or (type(val)==tuple)): raise sbml_lexer.SemanticError() if(type(val)!= bool): val = self.value.eval() if((val != True) and (val!= False) ): raise sbml_lexer.SemanticError() if(val==False): return else: self.block.eval() return
def eval(self): val = self.value if (len(stack) > 0): if (type(val) != list): if (val in stack[len(stack) - 1]["names"]): val = stack[len(stack) - 1]["names"][val] else: if (val in stack[len(stack) - 1]["names"].values): val = stack[len(stack) - 1]["names"][val] if (type(val) != list): if (val in names): val = names[val] else: val = self.value.eval() else: if (val in names.values()): val = names[val] else: val = self.value.eval() if ((val != True) and (val != False)): raise sbml_lexer.SemanticError() if (val == False): self.blockElse.eval() else: self.blockIf.eval()
def __init__(self, left, right): if (isinstance(right.eval(), list) or isinstance(right, tuple)): super().__init__() self.left = left self.right = right else: raise sbml_lexer.SemanticError()
def __init__(self, value): super().__init__() #type checking if ((value == "True") or (value == "False")): self.value = value else: raise sbml_lexer.SemanticError()
def eval(self): temp = self.value if(temp in names): temp = names[temp] else: temp = self.value.eval() if ((temp!=True) and (temp!=False)): raise sbml_lexer.SemanticError() return not temp
def __init__(self, value): if (value == "True"): super().__init__() self.value = True elif (value == "False"): super().__init__() self.value = False else: raise sbml_lexer.SemanticError()
def eval(self): if (self.oper == '-'): return (self.left.eval() - self.right.eval()) elif (self.oper == '+'): return (self.left.eval() + self.right.eval()) elif (self.oper == '*'): return (self.left.eval() * self.right.eval()) elif (self.oper == '/'): if (self.right.eval() == 0): raise sbml_lexer.SemanticError() return float(self.left.eval()) / float(self.right.eval()) elif (self.oper == '**'): if (numberCheck(self.left, self.right) == False): raise sbml_lexer.SemanticError() return (self.left.eval()**self.right.eval()) elif (self.oper == 'div'): return int(self.left.eval() / self.right.eval()) #DOUBLE CHECK HTIS elif (self.oper == 'mod'): return (self.left.eval()) % (self.right.eval())
def __init__(self, left, op, right): if (typeChecking(left, right) == True): if (left == "True"): super().__init__() self.left = True elif (left == "False"): super().__init__() self.left = False else: raise sbml_lexer.SemanticError() if (right == "True"): self.right = True elif (right == "False"): self.right = False else: raise sbml_lexer.SemanticError() self.op = op else: raise sbml_lexer.SemanticError()
def eval(self): tempRight = self.right if(tempRight in names): tempRight = names[tempRight] elif(type(tempRight)!= str): tempRight = self.right.eval() tempLeft = self.left.eval() if((not isinstance(tempRight, list)) and (not isinstance(tempRight,tuple)) and (not isinstance(tempRight, str))): raise sbml_lexer.SemanticError() return tempLeft in tempRight #double check
def eval(self): count = 0 for item in self.right.eval(): count = count + 1 if ((count - 1) < self.left.eval()): raise sbml_lexer.SemanticError() count2 = 0 for item in self.right.eval(): if count2 == self.left.eval(): return item count2 = count2 + 1
def eval(self): val = self.value if(val in names): val = names[val] else: val = self.value.eval() if((val != True) and (val!= False) ): raise sbml_lexer.SemanticError() if(val==False): self.blockElse.eval() else: self.blockIf.eval()
def eval(self): count = 0 temp = [] if (isinstance(self.left.eval(), str)): temp = list(self.left.eval()) else: temp = self.left.eval() for item in temp: count = count + 1 if ((count - 1) < self.right.eval()): raise sbml_lexer.SemanticError() return temp[self.right.eval()]
def eval(self): global names if(self.right in names): self.right = names[self.right] if(type(self.right)==str): if((self.right[0] != "\"") and (self.right[0]!= "'")): raise sbml_lexer.SemanticError() if(type(self.right)!=int): if (self.right in names): names[self.left] = names[self.right] else: names[self.left] = self.right.eval() else: names[self.left] = self.right
def eval(self): tempLeft = self.left.eval() tempRight = self.right.eval() if(not isinstance(tempLeft,int)): raise sbml_lexer.SyntaxError() if(not isinstance(tempRight,tuple)): raise sbml_lexer.SyntaxError() count = 0 for item in tempRight: count = count + 1 if ((count)<tempLeft): raise sbml_lexer.SemanticError() count2 = 1 for item in tempRight: if count2==tempLeft: return item count2 = count2 + 1
def eval(self): tempLeft = self.left.eval() tempRight = self.right #if in stack if (len(stack) > 0): if (type(tempRight) != list): if (tempRight in stack[len(stack) - 1]["names"]): tempRight = stack[len(stack) - 1]["names"][tempRight] else: if (tempRight in stack[len(stack) - 1]["names"].values()): tempRight = stack[len(stack) - 1]["names"][tempRight] #if in names if (type(tempRight) != list): if (tempRight in names): tempRight = names[tempRight] else: tempRight = self.right.eval() else: if (tempRight in names.values()): tempRight = names[tempRight] else: tempRight = self.right.eval() if (not isinstance(tempLeft, int)): raise sbml_lexer.SyntaxError() if (not isinstance(tempRight, tuple)): raise sbml_lexer.SyntaxError() count = 0 for item in tempRight: count = count + 1 if ((count) < tempLeft): raise sbml_lexer.SemanticError() count2 = 1 for item in tempRight: if count2 == tempLeft: return item count2 = count2 + 1
def eval(self): temp = self.value if (len(stack) > 0): if (type(temp) != list): if (temp in stack[len(stack) - 1]["names"]): temp = stack[len(stack) - 1]["names"][temp] else: if (temp in stack[len(stack) - 1]["names"].values()): temp = stack[len(stack) - 1]["names"][temp] if (type(temp) != list): if (temp in names): temp = names[temp] else: temp = self.value.eval() else: if (temp in names.values()): temp = names[temp] else: temp = self.value.eval() if ((temp != True) and (temp != False)): raise sbml_lexer.SemanticError() return not temp
def __init__(self, name, expressions, block, output): self.name = name self.expressions = expressions self.block = block self.output = output tempNames = {} # expressions = mid #make sure the name is a string if (type(name) != str): raise sbml_lexer.SemanticError() fun_dict = { "funName": name, "expression": expressions, "block": block, "names": tempNames, "output": output } tempStack.append(fun_dict)
def eval(self): inStack = 0 global names tempRight = self.right tempLeft = self.left #I check if the stack has more than 1 element in it if (len(stack) > 0): #if the right is a name itself if (type(tempRight) != list): if (tempRight in stack[len(stack) - 1]["names"]): tempRight = stack[len(stack) - 1]["names"][tempRight] else: if (tempRight in stack[len(stack) - 1]["names"].values()): tempRight = stack[len(stack) - 1]["names"][tempRight] if (len(names) != 0): if (type(tempRight) != list): if (tempRight in names): tempRight = names[tempRight] else: if (tempRight in names.values()): tempRight = names[tempRight] if (type(tempRight) == str): if ((tempRight[0] != "\"") and (tempRight[0] != "'")): raise sbml_lexer.SemanticError() #if the stack is greater than 1 if (len(stack) > 0): if (type(tempRight) != int): if (type(tempRight) != list): if (tempRight in stack[-1]["names"]): stack[len(stack) - 1]["names"][tempLeft] = stack[ len(stack) - 1]["names"][tempRight] else: while (primTypeCheck(tempRight) == False): tempRight = tempRight.eval() stack[-1]["names"][tempLeft] = tempRight else: if (tempRight in stack[-1]["names"].values()): stack[len(stack) - 1]["names"][tempLeft] = stack[ len(stack) - 1]["names"][tempRight] else: while (primTypeCheck(tempRight) == False): tempRight = tempRight.eval() stack[-1]["names"][tempLeft] = tempRight else: stack[len(stack) - 1]["names"][tempLeft] = tempRight inStack = 1 if (inStack == 0): if (type(tempRight) != int): if (type(tempRight) != list): if (tempRight in names): names[tempLeft] = names[tempRight] else: names[tempLeft] = tempRight.eval() else: if (tempRight in names.values()): names[tempLeft] = names[tempRight] else: names[tempLeft] = tempRight.eval() else: names[tempLeft] = tempRight
def eval(self): tempName = self.name tempRight = self.right for j in range(0, len(tempRight)): if (primTypeCheck(tempRight[j]) == False): tempRight[j] = tempRight[j].eval() if (not isinstance(tempName, str)): # tempName.eval() raise sbml_lexer.SemanticError() found = 0 for i in range(0, len(tempStack)): tempDic = copy.deepcopy(tempStack[i]) #I see if there is a functin name for the one i'm looking for if (tempDic['funName'] == tempName): found = 1 #I assign the local variable passed to the name associated with it expressions = tempDic["expression"] #I check that the right number of expressions are passed if (len(expressions) != len(tempRight)): raise sbml_lexer.SemanticError() #I check if the expression is in the local variables if (len(stack) > 0): for j in range(0, len(tempRight)): if (tempRight[j] in stack[len(stack) - 1]["names"]): tempRight[j] = stack[len(stack) - 1]["names"][tempRight[j]] # nameFound = 1 #try to see if any parameters passed are in the global dictionary for j in range(0, len(tempRight)): if (type(tempRight[j]) != list): if (tempRight[j] in names): tempRight[j] = names[tempRight[j]] else: if (tempRight[j] in names.values()): tempRight[j] = names[tempRight[j]] dicNames = {} for j in range(0, len(tempRight)): dicNames[expressions[j]] = tempRight[j] tempDic["names"] = dicNames #I push this onto the stack stack.append(tempDic) #I evaluate the element on the stack stack[len(stack) - 1]["block"].eval() #get output outputName = stack[-1]["output"] outputValue = None if (type(outputName) != list): if outputName in stack[-1]["names"]: outputValue = stack[-1]["names"][outputName] else: if outputName in stack[-1]["names"].values(): outputValue = stack[-1]["names"][outputName] #I remove the last element of the list stack.pop() #I evaluate with the right parameter if (outputValue != None): return outputValue if (found == 0): # print("error raised in this spot") raise sbml_lexer.SemanticError()
def eval(self): tempLeft = self.left tempRight = self.right #if there is something present in the stack if (len(stack) > 0): if (type(self.left) != list): if (self.left in stack[len(stack) - 1]["names"]): tempLeft = stack[len(stack) - 1]["names"][self.left] else: if (self.left in stack[len(stack) - 1]["names"].values()): tempLeft = stack[len(stack) - 1]["names"][self.left] if (type(self.right) != list): if (self.right in stack[len(stack) - 1]["names"]): tempRight = stack[len(stack) - 1]["names"][self.right] else: if (self.right in stack[len(stack) - 1]["names"].values()): tempRight = stack[len(stack) - 1]["names"][self.right] if (type(self.left) != list): if (self.left in names): tempLeft = names[self.left] elif (primTypeCheck(self.left) == False): #CHECK IF PRIM TYPE while (primTypeCheck(tempLeft) == False): tempLeft = tempLeft.eval() tempLeft = self.left.eval() else: if (self.left in names.values()): tempLeft = names[self.left] elif (primTypeCheck(self.left) == False): #CHECK IF PRIM TYPE while (primTypeCheck(tempLeft) == False): tempLeft = tempLeft.eval() tempLeft = self.left.eval() if (type(self.right) != list): if (self.right in names): tempRight = names[self.right] elif (primTypeCheck(self.right) == False): #CHECK IF PRIM TYPE while (primTypeCheck(tempRight) == False): tempRight = tempRight.eval() else: if (self.right in names.values()): tempRight = names[self.right] elif (primTypeCheck(self.right) == False): #CHECK IF PRIM TYPE while (primTypeCheck(tempRight) == False): tempRight = tempRight.eval() if ((self.oper == '-') and (numberCheck(tempLeft, tempRight) == False)): raise sbml_lexer.SemanticError() if (self.oper == '-'): return (tempLeft - tempRight) elif ((self.oper == '+') and (typeChecking(tempLeft, tempRight) == False)): raise sbml_lexer.SemanticError() elif ((self.oper == '+') and (not (isinstance(tempLeft, str) or numberCheck( tempLeft, tempRight) or isinstance(tempLeft, list)))): raise sbml_lexer.SemanticError() elif (self.oper == '+'): return (tempLeft + tempRight) elif ((self.oper == '*') and (numberCheck(tempLeft, tempRight) == False)): raise sbml_lexer.SemanticError() elif (self.oper == '*'): return (tempLeft * tempRight) elif ((self.oper == '/') and (numberCheck(tempLeft, tempRight) == False)): raise sbml_lexer.SemanticError() elif (self.oper == '/'): if (tempRight == 0): raise sbml_lexer.SemanticError() return float(tempLeft) / float(tempRight) elif ((self.oper == '**') and (numberCheck(tempLeft, tempRight) == False)): raise sbml_lexer.SemanticError() elif (self.oper == '**'): if (numberCheck(tempLeft, tempRight) == False): raise sbml_lexer.SemanticError() return (tempLeft**tempRight) elif ( (self.oper == 'div') and (not (isinstance(tempLeft, int) and isinstance(tempRight, int)))): raise sbml_lexer.SemanticError() elif (self.oper == 'div'): return int(tempLeft / tempRight) #DOUBLE CHECK HTIS elif ( (self.oper == 'mod') and (not (isinstance(tempLeft, int) and isinstance(tempRight, int)))): raise sbml_lexer.SemanticError() elif (self.oper == 'mod'): return (tempLeft) % (tempRight)
def __init__(self, value): super().__init__() if(isinstance(value,int)or isinstance(value,float)): self.value = value else: raise sbml_lexer.SemanticError()
def eval(self): tempL = self.left tempR = self.right #if either is a name in the stack if (len(stack) > 0): if (type(tempL) != list): if (tempL in stack[len(stack) - 1]["names"]): if (type(tempR) != list): if (tempR in stack[len(stack) - 1]["names"]): return stack[len(stack) - 1]["names"][tempL][stack[ len(stack) - 1]["names"][tempR]] return stack[len(stack) - 1]["names"][tempL][tempR.eval()] else: if (tempR in stack[len(stack) - 1]["names"].values()): return stack[len(stack) - 1]["names"][tempL][stack[ len(stack) - 1]["names"][tempR]] return stack[len(stack) - 1]["names"][tempL][tempR.eval()] else: if (tempL in stack[len(stack) - 1]["names"].values()): if (type(tempR) != list): if (tempR in stack[len(stack) - 1]["names"]): return stack[len(stack) - 1]["names"][tempL][stack[ len(stack) - 1]["names"][tempR]] return stack[len(stack) - 1]["names"][tempL][tempR.eval()] else: if (tempR in stack[len(stack) - 1]["names"].values()): return stack[len(stack) - 1]["names"][tempL][stack[ len(stack) - 1]["names"][tempR]] return stack[len(stack) - 1]["names"][tempL][tempR.eval()] #if either is a name if (type(tempL) != list): if (tempL in names): if (type(tempR) != list): if (tempR in names): return names[tempL][names[tempR]] return names[tempL][tempR.eval()] else: if (tempR in names.values()): return names[tempL][names[tempR]] return names[tempL][tempR.eval()] else: if (tempL in names.values()): if (type(tempR) != list): if (tempR in names): return names[tempL][names[tempR]] return names[tempL][tempR.eval()] else: if (tempR in names.values()): return names[tempL][names[tempR]] return names[tempL][tempR.eval()] tempL = self.left.eval() tempR = self.right.eval() #error checking if (isinstance(tempL, str) or isinstance(tempL, list)): if (type(tempR) != int): raise sbml_lexer.SemanticError() else: raise sbml_lexer.SemanticError() count = 0 temp = [] if (isinstance(tempL, str)): temp = list(tempL) else: temp = tempL for item in temp: count = count + 1 if ((count - 1) < tempR): raise sbml_lexer.SemanticError() return temp[tempR]