def eval_command(command): # checks for the instruction command = voice_conversion(command, "command") instruction = command.split()[0] # exits if instruction is "exit" if instruction.lower() == "exit": print("Exiting...") quit() # unindent a multiline statement if instruction.lower() == "end": if Code.multiline: Code.code[len(Code.code)] = "{0}end".format(Code.amount_nested) Code.amount_nested = Code.amount_nested[:-4] return # cancel multiline statement elif instruction.lower() == "cancel": Code.multiline = False Code.if_else_loop = False Code.code = "" Code.amount_nested = "" for i in Code.loop_func_vars: Code.defined_vars.remove(i) Code.loop_func_vars = [] return # if there is nothing after the instruction, invalid command if len(command.split()) == 1: return False # gets and runs voice command that will be used with the instruction to_parse = " ".join(command.split()[1:]) if instruction in instructions: # special formatting if currently in a # loop/if-else statement/function definition if Code.if_else_loop or Code.def_func: to_parse = voice_conversion(to_parse, "if-else/loop") to_add = instructions[instruction](to_parse) if to_add not in [None, False]: Code.code[len(Code.code)] = "{0} {1}".format( Code.amount_nested, to_add ) return elif to_add is False: print("Invalid command") return False else: return instructions[instruction](to_parse) else: return False
def check_func(val): val = voice_conversion(val, "function") parameters = get_params(val) function_name = val.split("parameters")[0] # attempts to make function a Python builtin if to_builtin(function_name): function_name = to_builtin(function_name) else: # formats the name as snake_case if it is not a builtin function_name = format_var_func_name(function_name.rstrip()) if parameters is False: return False else: return "{0}{1}".format(function_name, parameters)
def verify(val, val_type=None): # checks if there is a method, returns it, and modifies val method = get_method(val) val = voice_conversion(val, "method").replace(method[1], "").rstrip() # checks for assumed data types (int, float, bool, str) if no data is named if val_type is None: for i in assumed_data_types: if i(val) is False: pass else: return "{0}{1}".format(i(val), method[0]) # check for data type if data type is named if val_type in data_types: return "{0}{1}".format(data_types[val_type](val), method[0]) else: return False
def get_method(val): val = voice_conversion(val, "method") # makes sure that command contains a method call if "method" in val: method_spot = val.find("method") parameters = get_params(val) # gets method name and converts it to builtin if it can method = val.split("parameters")[0][method_spot+7:] if to_builtin(method): method = to_builtin(method) else: method = format_var_func_name(method.rstrip()) if parameters is False: return False # returns the method and the text that needs to be removed from val else: return [".{0}{1}".format(method, parameters), val[method_spot:]] else: return ["", ""]
def format_value(val): val = val.strip() # checks if data type is named try: data_type = val.split()[0].lower() data_type = voice_conversion(val.split()[0].lower(), "data_type") except: data_type = None # verifies a named data type if data_type in data_types: data_value = " ".join(val.split()[1:]) var_val = verify(data_value, data_type) else: var_val = verify(val) # checks if a data type was returned if var_val is False: return False else: return var_val
def def_func(to_parse): Code.multiline = True Code.def_func = True if type(Code.code) != dict: Code.code = {} to_parse = voice_conversion(to_parse, "function") if len(to_parse.split("parameters")) > 1: param_items = to_parse.split("parameters")[-1].split("cut") for i in range(len(param_items)): if param_items[i].lstrip().startswith("variable"): param_items[i] = param_items[i].lstrip() else: param_items[i] = "variable {0}".format(param_items[i].lstrip()) to_parse = "{0} parameters {1}".format( to_parse.split("parameters")[0], " cut ".join(param_items) ) # creates implied function data type if to_parse.startswith("function"): function = format_value(to_parse) else: function = format_value("function {0}".format(to_parse)) if function is False: return False # add function parameters to defined_vars parameters = function[function.find("(")+1:function.find(")")].split(",") for i in parameters: Code.defined_vars.add(i) Code.loop_func_vars.append(i) # indent if not the first line if len(Code.code) > 0: Code.amount_nested += " " Code.code[len(Code.code)] = "{0}def {1}:".format( Code.amount_nested, function ) else: Code.code[len(Code.code)] = "def {0}:".format(function)