def createVariable(type_, name, value): var = variableExist(name) if not var: var = Variable(type_, name) var.verifyVariable() var.setValue(value) variables.append([name, var]) else: var.changeType(type_) var.setValue(value)
def call(self, parameters): value, type_ = parameters var = variableExist(value) if var: value = var.value if type_ == "int": try: int(value) return True except: return False elif type_ == "float": try: float(value) return True except: return False elif type_ == "str": return True else: return False
def callSimpleCondition(value): no = False if "not " in value: value = value[4:] no = True if "(" in value and ")" in value: if '"' in value: temp = value.split("(") if len(temp) == 2: name, parametre = temp else: name = temp[0] del temp[0] parametre = "(".join(temp) else: name, parametre = value.split("(") if ", " in parametre: parameters = parametre[:-1].split(", ") else: parameters = [parametre[:-1]] temp = callFunction(name, parameters) if no: return not bool(temp) else: return bool(temp) else: var = variableExist(value) if not var: if no: return not bool(value) else: return bool(value) else: if no: return not bool(var.value) else: return bool(var.value)
def setValue(self, value): if "(" in value and ")" in value: if '"' in value: temp = value.split("(") if len(temp) == 2: name, parametre = temp else: name = temp[0] del temp[0] parametre = "(".join(temp) else: name, parametre = value.split("(") if ", " in parametre: parameters = parametre[:-1].split(", ") else: parameters = [parametre[:-1]] if self.type_ == "str": self.value = str(callFunction(name, parameters)) elif self.type_ == "int": temp = callFunction(name, parameters) try: self.value = int(temp) except: error("ErrorConversion", "Impossible de convertir '" + temp + "' en entier") elif self.type_ == "float": temp = callFunction(name, parameters) try: self.value = float(temp) except: error("ErrorConversion", "Impossible de convertir '" + temp + "' en flottant") else: var = variableExist(value) if not var: if len(value) > 1 and value[0] == '"' and value[-1] == '"': if self.type_ == "str": if len(value ) > 1 and value[0] == '"' and value[-1] == '"': value = value[1:-1] self.value = value else: error( "ErrorConversion", "Impossible de convertir '" + value + "' en string") else: error( "ErrorConversion", "Impossible de convertir '" + value + "' en " + self.type_) else: if " + " in value: values = value.split(" + ") while len(values) > 1: result, info = add(values[0], values[1], self.type_) if result: values[0] = info del values[1] else: error("ErrorAddition", info) del values[1] self.value = values[0] elif "+" in value: values = value.split("+") while len(values) > 1: result, info = add(values[0], values[1], self.type_) if result: values[0] = info del values[1] else: error("ErrorAddition", info) self.value = values[0] elif " - " in value: values = value.split(" - ") while len(values) > 1: result, info = subtract(values[0], values[1], self.type_) if result: values[0] = info del values[1] else: error("ErrorSubstraction", info) self.value = values[0] elif "-" in value: values = value.split("-") while len(values) > 1: result, info = subtract(values[0], values[1], self.type_) if result: values[0] = info del values[1] else: error("ErrorSubstraction", info) self.value = values[0] elif " * " in value: values = value.split(" * ") while len(values) > 1: result, info = multiply(values[0], values[1], self.type_) if result: values[0] = info del values[1] else: error("ErrorMultiplication", info) self.value = values[0] elif "*" in value: values = value.split("*") while len(values) > 1: result, info = multiply(values[0], values[1], self.type_) if result: values[0] = info del values[1] else: error("ErrorMultiplication", info) self.value = values[0] elif " / " in value: values = value.split(" / ") while len(values) > 1: result, info = divide(values[0], values[1], self.type_) if result: values[0] = info del values[1] else: error("ErrorDivision", info) self.value = values[0] elif "/" in value: values = value.split("/") while len(values) > 1: result, info = divide(values[0], values[1], self.type_) if result: values[0] = info del values[1] else: error("ErrorDivision", info) self.value = values[0] else: if self.type_ == "int": try: self.value = int(value) except: error( "ErrorConversion", "Impossible de convertir '" + value + "' en entier") elif self.type_ == "float": try: self.value = float(value) except: error( "ErrorConversion", "Impossible de convertir '" + value + "' en float") elif self.type_ == "str": if len(value) > 1 and value[0] == '"' and value[ -1] == '"': value = value[1:-1] self.value = value else: error( "ErrorConversion", "Impossible de convertir '" + value + "' en string") else: if var.type_ == self.type_: self.value = var.value else: error( "ErrorConversion", "Impossible de convertir '" + var.value + "' en " + self.type_)
def setVariable(name, value): var = variableExist(name) if not var: return "La variable '" + name + "' n'existe pas" else: result = var.setValue(value)
def call(self, parameters): parameters = parameters[0] if " + " in parameters: values = parameters.split(" + ") while len(values) > 1: var1 = variableExist(values[0]) if not var1: var1 = variableExist(values[1]) if not var1: if '"' in values[0] or '"' in values[1]: result, info = add(values[0], values[1], "str") elif '.' in values[0] or '.' in values[1]: result, info = add(values[0], values[1], "float") else: result, info = add(values[0], values[1], "int") else: result, info = add(values[0], values[1], var1.type_) else: result, info = add(values[0], values[1], var1.type_) if result: values[0] = info del values[1] else: error("ErrorAddition", info) return values[0] elif "+" in parameters: values = parameters.split("+") while len(values) > 1: var1 = variableExist(values[0]) if not var1: var1 = variableExist(values[1]) if not var1: if '"' in values[0] or '"' in values[1]: result, info = add(values[0], values[1], "str") elif '.' in values[0] or '.' in values[1]: result, info = add(values[0], values[1], "float") else: result, info = add(values[0], values[1], "int") else: result, info = add(values[0], values[1], var1.type_) else: result, info = add(values[0], values[1], var1.type_) if result: values[0] = info del values[1] else: error("ErrorAddition", info) return values[0] elif " - " in parameters: values = parameters.split(" - ") while len(values) > 1: var1 = variableExist(values[0]) if not var1: var1 = variableExist(values[1]) if not var1: if '"' in values[0] or '"' in values[1]: result, info = subtract(values[0], values[1], "str") elif '.' in values[0] or '.' in values[1]: result, info = subtract(values[0], values[1], "float") else: result, info = subtract(values[0], values[1], "int") else: result, info = subtract(values[0], values[1], var1.type_) else: result, info = subtract(values[0], values[1], var1.type_) if result: values[0] = info del values[1] else: error("ErrorSubstraction", info) return values[0] elif "-" in parameters: values = parameters.split("-") while len(values) > 1: var1 = variableExist(values[0]) if not var1: var1 = variableExist(values[1]) if not var1: if '"' in values[0] or '"' in values[1]: result, info = subtract(values[0], values[1], "str") elif '.' in values[0] or '.' in values[1]: result, info = subtract(values[0], values[1], "float") else: result, info = subtract(values[0], values[1], "int") else: result, info = subtract(values[0], values[1], var1.type_) else: result, info = subtract(values[0], values[1], var1.type_) if result: values[0] = info del values[1] else: error("ErrorSubstraction", info) return values[0] elif " * " in parameters: values = parameters.split(" * ") while len(values) > 1: var1 = variableExist(values[0]) if not var1: var1 = variableExist(values[1]) if not var1: if '"' in values[0] or '"' in values[1]: result, info = multiply(values[0], values[1], "str") elif '.' in values[0] or '.' in values[1]: result, info = multiply(values[0], values[1], "float") else: result, info = multiply(values[0], values[1], "int") else: result, info = multiply(values[0], values[1], var1.type_) else: result, info = multiply(values[0], values[1], var1.type_) if result: values[0] = info del values[1] else: error("ErrorMultiplication", info) return values[0] elif "*" in parameters: values = parameters.split("*") while len(values) > 1: var1 = variableExist(values[0]) if not var1: var1 = variableExist(values[1]) if not var1: if '"' in values[0] or '"' in values[1]: result, info = multiply(values[0], values[1], "str") elif '.' in values[0] or '.' in values[1]: result, info = multiply(values[0], values[1], "float") else: result, info = multiply(values[0], values[1], "int") else: result, info = multiply(values[0], values[1], var1.type_) else: result, info = multiply(values[0], values[1], var1.type_) if result: values[0] = info del values[1] else: error("ErrorMultiplication", info) return values[0] elif " / " in parameters: values = parameters.split(" / ") while len(values) > 1: var1 = variableExist(values[0]) if not var1: var1 = variableExist(values[1]) if not var1: if '"' in values[0] or '"' in values[1]: result, info = divide(values[0], values[1], "str") elif '.' in values[0] or '.' in values[1]: result, info = divide(values[0], values[1], "float") else: result, info = divide(values[0], values[1], "int") else: result, info = divide(values[0], values[1], var1.type_) else: result, info = divide(values[0], values[1], var1.type_) if result: values[0] = info del values[1] else: error("ErrorDivision", info) return values[0] elif "/" in parameters: values = parameters.split("/") while len(values) > 1: var1 = variableExist(values[0]) if not var1: var1 = variableExist(values[1]) if not var1: if '"' in values[0] or '"' in values[1]: result, info = divide(values[0], values[1], "str") elif '.' in values[0] or '.' in values[1]: result, info = divide(values[0], values[1], "float") else: result, info = divide(values[0], values[1], "int") else: result, info = divide(values[0], values[1], var1.type_) else: result, info = divide(values[0], values[1], var1.type_) if result: values[0] = info del values[1] else: error("ErrorDivision", info) return values[0] else: var = variableExist(parameters) if not var: if len(parameters) > 1 and parameters[0] == '"' and parameters[ -1] == '"': return parameters[1:-1] elif is_number(parameters): return parameters else: error( "ErrorConversion", "Impossible de convertir '" + parameters + "' en string") else: return var.value
def callMultipleCondition(value1, value2, type_): if "(" in value1 and ")" in value1 and "(" in value2 and ")" in value2: if '"' in value1: temp = value1.split("(") if len(temp) == 2: name, parametre = temp else: name = temp[0] del temp[0] parametre = "(".join(temp) else: name, parametre = value1.split("(") if ", " in parametre: parameters = parametre[:-1].split(", ") else: parameters = [parametre[:-1]] temp = callFunction(name, parameters) value1 = temp if '"' in value2: temp = value2.split("(") if len(temp) == 2: name, parametre = temp else: name = temp[0] del temp[0] parametre = "(".join(temp) else: name, parametre = value2.split("(") if ", " in parametre: parameters = parametre[:-1].split(", ") else: parameters = [parametre[:-1]] temp = callFunction(name, parameters) value2 = temp if "(" in value1 and ")" in value1: if '"' in value1: temp = value1.split("(") if len(temp) == 2: name, parametre = temp else: name = temp[0] del temp[0] parametre = "(".join(temp) else: name, parametre = value1.split("(") if ", " in parametre: parameters = parametre[:-1].split(", ") else: parameters = [parametre[:-1]] temp = callFunction(name, parameters) value1 = temp var2 = variableExist(value2) if not var2: pass else: value2 = var2.value if "(" in value2 and ")" in value2: if '"' in value2: temp = value2.split("(") if len(temp) == 2: name, parametre = temp else: name = temp[0] del temp[0] parametre = "(".join(temp) else: name, parametre = value2.split("(") if ", " in parametre: parameters = parametre[:-1].split(", ") else: parameters = [parametre[:-1]] temp = callFunction(name, parameters) value2 = temp var1 = variableExist(value1) if not var1: pass else: value1 = var1.value else: var1 = variableExist(value1) var2 = variableExist(value2) if not var1 and not var2: pass elif not var1: value2 = var2.value if var2.type_ == "int": try: value1 = int(value1) except: pass elif var2.type_ == "float": try: value1 = float(value1) except: pass elif not var2: value1 = var1.value if var1.type_ == "int": try: value2 = int(value2) except: pass elif var1.type_ == "float": try: value2 = float(value2) except: pass else: value1 = var1.value value2 = var2.value if type_ == "=": return value1 == value2 elif type_ == "<": return value1 < value2 elif type_ == "<=": return value1 <= value2 elif type_ == ">": return value1 > value2 elif type_ == ">=": return value1 >= value2 elif type_ == "!=": return value1 != value2
def add(value1, value2, type_): var1 = variableExist(value1) var2 = variableExist(value2) if not var1 and not var2: if type_ == "int": try: value = int(value1) + int(value2) value = int(value) return True, value except: return False, "Impossible de convertir '" + value1 + "' et/ou '" + value2 + "' en entier" elif type_ == "float": try: value = float(value1) + float(value2) value = float(value) return True, value except: return False, "Impossible de convertir '" + value1 + "' et/ou '" + value2 + "' en flottant" elif type_ == "str": if len(value1) > 1 and len( value2) > 1 and value1[0] == '"' and value1[ -1] == '"' and value2[0] == '"' and value2[-1] == '"': value1 = value1[1:-1] value2 = value2[1:-1] return True, value1 + value2 elif len(value1) > 1 and value1[0] == '"' and value1[-1] == '"': value1 = value1[1:-1] return True, value1 + value2 elif len(value2) > 1 and value2[0] == '"' and value2[-1] == '"': value2 = value2[1:-1] return True, value1 + value2 else: return True, value1 + value2 elif not var1: if type_ == "int" and var2.type_ == "int": try: value = int(value1) + var2.value value = int(value) return True, value except: return False, "Impossible de convertir '" + value1 + "' en entier" elif type_ == "float" and var2.type_ == "float": try: value = float(value1) + var2.value value = float(value) return True, value except: return False, "Impossible de convertir '" + value1 + "' en flottant" elif type_ == "str" and var2.type_ == "str": if len(value1) > 1 and value1[0] == '"' and value1[-1] == '"': value1 = value1[1:-1] value2 = str(var2.value) return True, value1 + value2 else: value2 = str(var2.value) return True, value1 + value2 else: return False, "Impossible de convertir la variable '" + var2.name + "' en '" + type_ + "'" elif not var2: if type_ == "int" and var1.type_ == "int": try: value = int(value2) + var1.value value = int(value) return True, value except: return False, "Impossible de convertir '" + value2 + "' en entier" elif type_ == "float" and var1.type_ == "float": try: value = float(value2) + var1.value value = float(value) return True, value except: return False, "Impossible de convertir '" + value2 + "' en flottant" elif type_ == "str" and var1.type_ == "str": if len(value2) > 1 and value2[0] == '"' and value2[-1] == '"': value1 = value2[1:-1] value2 = str(var1.value) return True, value2 + value1 else: value2 = str(var1.value) return True, value2 + value1 else: return False, "Impossible de convertir la variable '" + var1.name + "' en '" + type_ + "'" else: if type_ == "int" and var1.type_ == "int" and var2.type_ == "int": value = var2.value + var1.value value = int(value) return True, value elif type_ == "float" and var1.type_ == "float" and var2.type_ == "float": value = var2.value + var1.value value = float(value) return True, value elif type_ == "str" and var1.type_ == "str" and var2.type_ == "str": value1 = str(var2.value) value2 = str(var1.value) return True, value2 + value1 else: return False, "Impossible de convertir la variable '" + var1.name + "' et/ou '" + var2.name + "' en '" + type_ + "'"
def subtract(value1, value2, type_): var1 = variableExist(value1) var2 = variableExist(value2) if not var1 and not var2: if type_ == "int": try: value = int(value1) - int(value2) value = int(value) return True, value except: return False, "Impossible de convertir '" + value1 + "' et/ou '" + value2 + "' en entier" elif type_ == "float": try: value = float(value1) - float(value2) value = float(value) return True, value except: return False, "Impossible de convertir '" + value1 + "' et/ou '" + value2 + "' en flottant" elif type_ == "str": return False, "Impossible de soustraire des strings" elif not var1: if type_ == "int" and var2.type_ == "int": try: value = int(value1) - var2.value value = int(value) return True, value except: return False, "Impossible de convertir '" + value1 + "' en entier" elif type_ == "float" and var2.type_ == "float": try: value = float(value1) - var2.value value = float(value) return True, value except: return False, "Impossible de convertir '" + value1 + "' en flottant" elif type_ == "str" and var2.type_ == "str": return False, "Impossible de soustraire des strings" else: return False, "Impossible de convertir la variable '" + var2.name + "' en '" + type_ + "'" elif not var2: if type_ == "int" and var1.type_ == "int": try: value = var1.value - int(value2) value = int(value) return True, value except: return False, "Impossible de convertir '" + value2 + "' en entier" elif type_ == "float" and var1.type_ == "float": try: value = var1.value - float(value2) value = float(value) return True, value except: return False, "Impossible de convertir '" + value2 + "' en flottant" elif type_ == "str" and var1.type_ == "str": return False, "Impossible de soustraire des strings" else: return False, "Impossible de convertir la variable '" + var1.name + "' en '" + type_ + "'" else: if type_ == "int" and var1.type_ == "int" and var2.type_ == "int": value = var1.value - var2.value value = int(value) return True, value elif type_ == "float" and var1.type_ == "float" and var2.type_ == "float": value = var1.value - var2.value value = float(value) return True, value elif type_ == "str" and var1.type_ == "str" and var2.type_ == "str": return False, "Impossible de soustraire des strings" else: return False, "Impossible de convertir la variable '" + var1.name + "' et/ou '" + var2.name + "' en '" + type_ + "'"
def divide(value1, value2, type_): var1 = variableExist(value1) var2 = variableExist(value2) if not var1 and not var2: if type_ == "int": try: value = int(value1) / int(value2) try: value = int(value) return True, value except: return False, "Le résultat n'est pas un entier" except: return False, "Impossible de convertir '" + value1 + "' et/ou '" + value2 + "' en entier" elif type_ == "float": try: value = float(value1) / float(value2) value = float(value) return True, value except: return False, "Impossible de convertir '" + value1 + "' et/ou '" + value2 + "' en flottant" elif type_ == "str": return False, "Impossible de diviser des strings" elif not var1: if type_ == "int" and var2.type_ == "int": try: value = int(value1) / var2.value try: value = int(value) return True, value except: return False, "Le résultat n'est pas un entier" except: return False, "Impossible de convertir '" + value1 + "' en entier" elif type_ == "float" and var2.type_ == "float": try: value = float(value1) / var2.value value = float(value) return True, value except: return False, "Impossible de convertir '" + value1 + "' en flottant" elif type_ == "str" and var2.type_ == "str": return False, "Impossible de diviser des strings" else: return False, "Impossible de convertir la variable '" + var2.name + "' en '" + type_ + "'" elif not var2: if type_ == "int" and var1.type_ == "int": try: value = var1.value / int(value2) try: value = int(value) return True, value except: return False, "Le résultat n'est pas un entier" except: return False, "Impossible de convertir '" + value2 + "' en entier" elif type_ == "float" and var1.type_ == "float": try: value = var1.value / float(value2) value = float(value) return True, value except: return False, "Impossible de convertir '" + value2 + "' en flottant" elif type_ == "str" and var1.type_ == "str": return False, "Impossible de diviser des strings" else: return False, "Impossible de convertir la variable '" + var1.name + "' en '" + type_ + "'" else: if type_ == "int" and var1.type_ == "int" and var2.type_ == "int": value = var1.value / var2.value try: value = int(value) return True, value except: return False, "Le résultat n'est pas un entier" return True, value elif type_ == "float" and var1.type_ == "float" and var2.type_ == "float": value = var1.value / var2.value value = float(value) return True, value elif type_ == "str" and var1.type_ == "str" and var2.type_ == "str": return False, "Impossible de diviser des strings" else: return False, "Impossible de convertir la variable '" + var1.name + "' et/ou '" + var2.name + "' en '" + type_ + "'"