예제 #1
0
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
예제 #2
0
def generate_args(variable_to_use=None):
    args_length = choice([1, 1, 1, 2, 2, 2, 3])
    if args_length == 0:
        return ("", "()")

    args = [generate_value(value_types, depth=1) for _ in range(args_length)]
    if variable_to_use is not None and len(args) > 0:
        args[0] = (variable_to_use, variable_to_use)

    args_input = [x[0] for x in args]
    args_output = [x[1] for x in args]

    return choice([", ".join(args_input),
                   " ".join(args_input)]), f"({', '.join(args_output)})"
예제 #3
0
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}'
예제 #4
0
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}"
예제 #5
0
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}"
예제 #6
0
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}):'
예제 #7
0
def generate_call_without_args():
    name = generate_name()

    return choice([f"{name}()", f"call {name}"]), f"{name}()"
예제 #8
0
def generate_call_sample(variable_to_use=None):
    return choice([
        generate_call_with_args(variable_to_use),
        generate_call_without_args()
    ])
예제 #9
0
def generate_args(variable_to_use=None):
    args_length = choice([1, 1, 1, 2, 2, 2, 3])
    if args_length == 0:
        return ("", "()")

    args = [generate_value(value_types, depth=1) for _ in range(args_length)]
    if variable_to_use is not None and len(args) > 0:
        args[0] = (variable_to_use, variable_to_use)

    args_input = [x[0] for x in args]
    args_output = [x[1] for x in args]

    return choice([", ".join(args_input),
                   " ".join(args_input)]), f"({', '.join(args_output)})"


inputs = []
outputs = []
for i in range(2000):
    (input, output) = choice([
        generate_declaration_sample(),
        generate_lambda_sample(),
        generate_call_sample(),
    ])
    inputs.append(input)
    outputs.append(output)

if __name__ == '__main__':
    save_generated(__file__, inputs, outputs)
    print("Done!")
예제 #10
0
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_
    ])