def for_loop(to_parse): # makes sure it is a for-in loop if " in " not in to_parse: return False else: Code.if_else_loop = True Code.multiline = True if type(Code.code) != dict: Code.code = {} # splits loop into variable and iterable parts = to_parse.split(" in ") # add loop variable to defined_vars variable = format_var_func_name(parts[0]) Code.defined_vars.add(variable) Code.loop_func_vars.append(variable) iterable = format_value(parts[1]) # increases indentation if not the first line in the loop if len(Code.code) > 0: Code.amount_nested += " " Code.code[len(Code.code)] = "{0}for {1} in {2}:".format( Code.amount_nested, variable, iterable ) else: Code.code[len(Code.code)] = "for {0} in {1}:".format( variable, iterable )
def assign_variable(to_parse): v = "to variable" to_parse = to_parse.replace("the variable", v) to_parse = to_parse.replace("two variable", v) to_parse = to_parse.replace("- variable ", v) # makes sure that "to variable" is in the command unformatted_name_place = to_parse.find(" to variable ") if unformatted_name_place == -1: return False # where the name should be unformatted_name = to_parse[unformatted_name_place+13:] # where the value should be unparsed_value = to_parse[:unformatted_name_place] # converts the variable name and value to proper forms var_name = format_var_func_name(unformatted_name) var_value = format_value(unparsed_value) # adds variable to defined_vars Code.defined_vars.add(var_name) if not var_name or not var_value: return False else: return "{0} = {1}".format(var_name, var_value)
def call_func_method(to_parse): # makes sure the command is a function or method if to_parse.startswith("function") or to_parse.find("method") != -1: to_call = format_value(to_parse) if to_call is False: return False else: return "{0}".format(to_call) else: return False
def return_func(to_parse): # can only be used in functions if Code.def_func is False: return False else: to_return = format_value(to_parse) if to_return is False: return False else: return "return {0}".format(to_return)
def while_loop(to_parse): Code.if_else_loop = True Code.multiline = True if type(Code.code) != dict: Code.code = {} # creates implied comparison if to_parse.startswith("comparison"): comparison = format_value(to_parse) else: comparison = format_value("comparison {0}".format(to_parse)) if comparison is False: return False # indents code if not the first line in the loop if len(Code.code) > 0: Code.amount_nested += " " Code.code[len(Code.code)] = "{0}while {1}:".format( Code.amount_nested, comparison ) else: Code.code[len(Code.code)] = "while {0}:".format(comparison)
def if_else(to_parse): Code.if_else_loop = True Code.multiline = True # if a multiline statement has been started, initialize it if type(Code.code) != dict: Code.code = {} # inputs `else:` if the user said else if to_parse == "else command": Code.code[len(Code.code)] = "{0}else:".format(Code.amount_nested) # creates an elif statement elif to_parse.startswith("elif"): # implies comparison if it isn't said if to_parse.startswith("comparison"): comparison = format_value(to_parse[4:]) else: comparison = format_value("comparison {0}".format(to_parse[4:])) if to_parse == "elif" or comparison is False: return False else: Code.code[len(Code.code)] = "{0}elif {1}:".format( Code.amount_nested, comparison ) else: if to_parse.startswith("comparison"): comparison = format_value(to_parse) else: comparison = format_value("comparison {0}".format(to_parse)) if comparison is False: return False # increases indentation is another if statement is said if len(Code.code) > 0: Code.amount_nested += " " Code.code[len(Code.code)] = "{0}if {1}:".format( Code.amount_nested, comparison ) else: Code.code[len(Code.code)] = "if {0}:".format(comparison) return
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)
def print_data(to_parse): print_value = format_value(to_parse) if print_value == "False": return False else: return "print({0})".format(print_value)