def generate_call_with_args(variable_to_use=None): name = generate_name() (args_input, args_output) = generate_args(variable_to_use) return choice([ f"{name}({args_input})", f"{name} {args_input}", f"call {name} with ({args_input})", f"call {name} with {args_input}", ]), f"{name}{args_output}"
def generate_lambda_sample(): name = generate_name() (params_input, params_output, params) = generate_params() variable_to_use = params[0] if len(params) > 0 else None (expr_input, expr_output) = generate_expression_sample(variable_to_use) return choice([ f"{name} {params_input} = {expr_input}", f"{name} = lambda {params_input}: {expr_input}", f"{name} = {params_input} => {expr_input}", f"{name} = \{params_input} -> {expr_input}" ]), f"{name} = lambda {params_output}: {expr_output}"
def generate_params(): params_length = choice([0, 1, 1, 1, 2, 2, 2, 3]) if params_length == 0: return ("()", "", []) params = [generate_name() for _ in range(params_length)] input = choice([ \ f"({', '.join(params)})", ", ".join(params), " ".join(params) ]) return input, ", ".join(params), params
def generate_declaration_sample(): name = generate_name() (params_input, params_output, _) = generate_params() return choice([ f"def {name} {params_input}", f"define function {name} {params_input}", f"let function {name} {params_input}", f"create function {name} {params_input}", f"function {name} {params_input}", f"fun {name} {params_input}", f"fn {name} {params_input}", f"subroutine {name} {params_input}", f"sub {name} {params_input}", ]), f'def {name}({params_output}):'
def generate_sample(variable_to_use=None): name = variable_to_use if variable_to_use else generate_name() (value, value_output) = choice([generate_value(), generate_expression_sample()]) return choice([ f'set {name} to {value}', f'let {name} be {value}', f'{name} = {value}', f'{name} is {value}', f'set {name} equals {value}', f'let {name} equals {value}', f'put {name} into {value}', f'define {name} as {value}', f'initialize {name} as {value}', ]), f'{name} = {value_output}'
def generate_sample_update(variable_to_use=None): operation = choice(list(update_templates.keys())) possible_types = ['number', 'written_number', 'variable'] template = choice(update_templates[operation]) if 'add' in template or '+' in template: possible_types += ['string'] name = generate_name() (value, real_value) = generate_value(possible_types) if variable_to_use: value = real_value = variable_to_use input = template.replace("#NAME", name).replace("#VALUE", value) if "square" in input: output = "%s **= 2" % name elif operation == "++": output = "%s += 1" % name else: output = "%s %s= %s" % (name, operation, real_value) return (input, output)
def generate_call_without_args(): name = generate_name() return choice([f"{name}()", f"call {name}"]), f"{name}()"
def loop(): var = generate_name() (list_input, list_output) = generate_value(['variable', 'list']) min = choice([0, 1, 5, 10]) max = str( min + choice([1, 5, 10, 50, 100])) if rand() < 0.5 else generate_name() min = str(min) if rand() < 0.9 else generate_name() (comparison_input, comparison_output) = ifs.generate_comparison() (expr_input, expr_output) = generate_statement_or_expression(variable_to_use=var) for_each = choice([ \ f"for {var} in {list_input}", f"each {var} in {list_input}" ]), f"for {var} in {list_output}:" for_each_do = choice([ \ f"{expr_input} for each {var} in {list_input}", f"{expr_input} for each {var} of {list_input}" ]), f"for {var} in {list_output}:\\n {expr_output}" times_loop = choice([ \ f"{max} times do", f"repeat {max} times", f"do {max} times" ]), f"for _ in range({max}):" times_loop_do = choice([ \ f"{expr_input} {max} times", f"repeat {expr_input} {max} times", f"do {expr_input} {max} times" ]), f"for _ in range({max}):\\n {expr_output}" for_range = choice([ \ f"for {var} in {min} to {max}", f"for {var} in {min}..{max}" ]), f"for {var} in range({min}, {max}):" for_range_do = choice([ \ f"{expr_input} for {var} in {min} to {max}", f"{expr_input} for {var} in {min}..{max}" ]), f"for {var} in range({min}, {max}):\\n {expr_output}" while_ = choice([ \ f"while {comparison_input}", f"while {comparison_input} do" ]), f"while {comparison_output}:" while_do = f"{expr_input} while {comparison_input}", \ f"while {comparison_output}:\\n {expr_output}" until_do = f"{expr_input} until {comparison_input}", \ f"while not {comparison_output}:\\n {expr_output}" infinite = choice([ \ "while true", "loop" ]), "while True:" break_ = choice([ \ "break", "exit loop", "end the loop" ]), "break" continue_ = choice([ \ "continue", "next", "jump to next item in the loop" ]), "continue" return choice([ for_each, for_each_do, times_loop, times_loop_do, for_range, for_range_do, while_, while_do, until_do, infinite, break_, continue_ ])