def callFunction(name, parameters): func = functionExist(name) if not func: error("UnknownFunction", "La fonction '"+name+"' est inconnu") else: if func.nbParameters == 0 and parameters[0] == "": return func.call() if func.nbParameters == len(parameters): return func.call(parameters) else: error("NumberParameters", "Nombre de parametres acceptés : "+str(func.nbParameters)+" - Nombre de parametres donnés : "+str(len(parameters)))
def changeType(self, type_): if type_ == "int": try: self.type_ = type_ self.value = int(self.value) except: error("ErrorConversion", "Impossible de convertir '" + self.value + "' en entier") elif type_ == "float": try: self.type_ = type_ self.value = float(self.value) except: error( "ErrorConversion", "Impossible de convertir '" + self.value + "' en flottant") elif type_ == "str": try: self.type_ = type_ self.value = str(self.value) except: error("ErrorConversion", "Impossible de convertir '" + self.value + "' en string") else: error("UnknownType", "Type inconnu : " + self.type_)
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 verifyVariable(self): if not self.type_ in ["int", "str", "float"]: error("UnknownType", "Type inconnu : " + self.type_)
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 interpreter_on_script(text): setInteractif(False) code = "" ligne = 0 bloc = 0 suivant = [False] * 12 suivant[0] = True lastIf = [False] * 12 While = [""] * 12 while ligne < len(text): code = text[ligne] #print("\n\n",code) #print("Bloc ->", bloc, ": Suivant -> ", suivant,"\n\n") if len(code) > 1 and code[0] + code[1] == "//": pass elif code == "": pass else: todo, info = parser(code, bloc, suivant[bloc]) if todo == "defVariable": result = createVariable(info[0], info[1], info[2]) if suivant: lastIf[bloc] = False elif todo == "setVariable": result = setVariable(info[0], info[1]) if suivant: lastIf[bloc] = False elif todo == "callFunction": result = callFunction(info[0], info[1]) if suivant: lastIf[bloc] = False elif todo == "Erreur": error(info[0], info[1]) if suivant: lastIf[bloc] = False elif todo == "WhileValue": bloc += 1 if bloc > 0 and suivant[bloc - 1]: suivant[bloc] = callSimpleCondition(info[0]) if suivant[bloc]: While[bloc] = ligne else: While[bloc] = "" elif suivant[bloc]: suivant[bloc] = callSimpleCondition(info[0]) if suivant[bloc]: While[bloc] = ligne else: While[bloc] = "" elif todo == "WhileValues": bloc += 1 if bloc > 0 and suivant[bloc - 1]: suivant[bloc] = callMultipleCondition( info[0], info[1], info[2]) if suivant[bloc]: While[bloc] = ligne else: While[bloc] = "" elif suivant[bloc]: suivant[bloc] = callMultipleCondition( info[0], info[1], info[2]) if suivant[bloc]: While[bloc] = ligne else: While[bloc] = "" elif todo == "ConditionValue": bloc += 1 if bloc > 0 and suivant[bloc - 1]: suivant[bloc] = callSimpleCondition(info[0]) lastIf[bloc] = suivant[bloc] elif suivant[bloc]: suivant[bloc] = callSimpleCondition(info[0]) lastIf[bloc] = suivant[bloc] elif todo == "ConditionValues": bloc += 1 if bloc > 0 and suivant[bloc - 1]: suivant[bloc] = callMultipleCondition( info[0], info[1], info[2]) lastIf[bloc] = suivant[bloc] elif suivant[bloc]: suivant[bloc] = callMultipleCondition( info[0], info[1], info[2]) lastIf[bloc] = suivant[bloc] elif todo == "ConditionValueELIF": bloc += 1 if lastIf[bloc]: suivant[bloc] = False else: if bloc > 0 and suivant[bloc - 1]: suivant[bloc] = callSimpleCondition(info[0]) lastIf[bloc] = suivant[bloc] elif suivant[bloc]: suivant[bloc] = callSimpleCondition(info[0]) lastIf[bloc] = suivant[bloc] elif todo == "ConditionValuesELIF": bloc += 1 if lastIf[bloc]: suivant[bloc] = False else: if bloc > 0 and suivant[bloc - 1]: suivant[bloc] = callMultipleCondition( info[0], info[1], info[2]) lastIf[bloc] = suivant[bloc] elif suivant[bloc]: suivant[bloc] = callMultipleCondition( info[0], info[1], info[2]) lastIf[bloc] = suivant[bloc] elif todo == "ConditionELSE": bloc += 1 if lastIf[bloc]: suivant[bloc] = False else: if bloc - 1 >= 0: if suivant[bloc - 1] and lastIf[bloc - 1]: suivant[bloc] = True else: suivant[bloc] = False else: if suivant[bloc]: suivant[bloc] = True else: suivant[bloc] = False lastIf[bloc] = False elif todo == "EndBloc": bloc -= 1 if bloc < 0: bloc = 0 if While[bloc + 1] != "": ligne = While[bloc + 1] - 1 While[bloc + 1] = "" elif bloc == 0: suivant[bloc] = True else: if suivant[bloc]: lastIf[bloc] = False try: if result is not None: print(result) del result except: pass ligne += 1
def interpreter(): setInteractif(True) code = "" bloc = 0 suivant = True lastIf = [False] * 12 while True: code = input(">>> ") if len(code) > 1 and code[0] + code[1] == "//": pass elif code == "": pass else: todo, info = parser(code, bloc, suivant) if todo == "defVariable": result = createVariable(info[0], info[1], info[2]) if suivant: lastIf[bloc] = False elif todo == "setVariable": result = setVariable(info[0], info[1]) if suivant: lastIf[bloc] = False elif todo == "callFunction": result = callFunction(info[0], info[1]) if suivant: lastIf[bloc] = False elif todo == "Erreur": error(info[0], info[1]) if suivant: lastIf[bloc] = False elif todo == "ConditionValue": if info[1]: suivant = callSimpleCondition(info[0]) lastIf[bloc] = suivant bloc += 1 elif todo == "ConditionValues": if info[3]: suivant = callMultipleCondition(info[0], info[1], info[2]) lastIf[bloc] = suivant bloc += 1 elif todo == "ConditionValueELIF": if lastIf[bloc]: suivant = False else: if info[1]: suivant = callSimpleCondition(info[0]) lastIf[bloc] = suivant bloc += 1 elif todo == "ConditionValuesELIF": if lastIf[bloc]: suivant = False else: if info[2]: suivant = callMultipleCondition( info[0], info[1], info[2]) lastIf[bloc] = suivant bloc += 1 elif todo == "ConditionELSE": if lastIf[bloc]: suivant = False else: if bloc - 1 >= 0: if info[0] and lastIf[bloc - 1]: suivant = True else: suivant = False else: if info[0]: suivant = True else: suivant = False bloc += 1 lastIf[bloc] = False elif todo == "EndBloc": bloc -= 1 if bloc < 0: bloc = 0 suivant = True else: if suivant: lastIf[bloc] = False try: if result is not None: print(result) del result except: pass
def callVariable(name): var = variableExist(name) if not var: error("UnknownVariable", "La variable '"+name+"' est inconnu") else: return var.value