Exemplo n.º 1
0
def test_apply_user_function():
    # backup function_env
    import evaluator
    initial_fundefs = evaluator.function_env
    evaluator.function_env = {}
    # test
    define_function('triple', ['n'], ['*', 'n', 3])
    ast = ['triple', 7]
    assert 21 == evaluate({}, ast)
    # restore function_env
    evaluator.function_env = initial_fundefs
Exemplo n.º 2
0
def test_apply_user_function():
    # backup function_definitions
    import evaluator
    initial_fundefs = evaluator.function_definitions
    evaluator.function_definitions = {}
    # test
    parts = 'triple', ['n'], ['*', 'n', 3]
    define_function(parts)
    ast = ['triple', 7]
    assert 21 == evaluate({}, ast)
    # restore function_definitions
    evaluator.function_definitions = initial_fundefs
Exemplo n.º 3
0
def test_evaluate_user_function_missing_argument(mod_body):
    # backup function_definitions
    import evaluator
    initial_fundefs = evaluator.function_definitions
    evaluator.function_definitions = {}
    # test
    define_function(('mod', ['m', 'n'], mod_body))
    ast = ['mod', 19]
    with raises(errors.MissingArgument) as excinfo:
        evaluate({}, ast)
    assert "Missing argument: 'mod' needs 2." == str(excinfo.value)
    # restore function_definitions
    evaluator.function_definitions = initial_fundefs
Exemplo n.º 4
0
def repl(input_fn=input):
    """Read-Eval-Print-Loop"""
    print(f'To exit, type {QUIT_COMMAND}', file=sys.stderr)

    while True:
        # ___________________________________________ Read
        try:
            line = input_fn('> ')
        except EOFError:
            break
        if line == QUIT_COMMAND:
            break
        if not line:
            continue

        # ___________________________________________ Eval
        current_exp = parse_exp(tokenize(line))
        if isinstance(current_exp, list) and current_exp[0] == 'define':
            result = define_function(current_exp[1:])
        else:
            try:
                result = evaluate({}, current_exp)
            except (errors.UndefinedVariable, errors.UndefinedFunction) as exc:
                print('***', exc)
                continue

        # ___________________________________________ Print
        print(result)
Exemplo n.º 5
0
def repl(input_fn=input):
    """Read-Eval-Print-Loop"""
    print(f'To exit, type {QUIT_COMMAND}', file=sys.stderr)

    while True:
        # ___________________________________________ Read
        try:
            source = multiline_input('> ',
                                     '... ',
                                     quit_cmd=QUIT_COMMAND,
                                     input_fn=input_fn)
        except (EOFError, QuitRequest):
            break
        except errors.UnexpectedCloseParen as exc:
            print('***', exc)
            continue
        if not source:
            continue

        # ___________________________________________ Eval
        current_exp = parse_exp(tokenize(source))
        if isinstance(current_exp, list) and current_exp[0] == 'define':
            result = define_function(current_exp[1:])
        else:
            try:
                result = evaluate({}, current_exp)
            except errors.EvaluatorException as exc:
                print('***', exc)
                continue

        # ___________________________________________ Print
        print(result)
Exemplo n.º 6
0
def run(source_file, env=None):
    """Read and execute opened source file"""
    source = source_file.read()
    if env is None:
        env = {}
    tokens = tokenize(source)

    while tokens:
        try:
            current_exp = parse_exp(tokens)
        except errors.UnexpectedCloseParen as exc:
            print('***', exc, file=sys.stderr)
            break

        if isinstance(current_exp, list) and current_exp[0] == 'define':
            define_function(current_exp[1:])
        else:
            try:
                evaluate(env, current_exp)
            except errors.EvaluatorException as exc:
                print('***', exc, file=sys.stderr)
                continue
Exemplo n.º 7
0
def test_define_function():
    # backup function_definitions
    import evaluator
    initial_fundefs = evaluator.function_definitions
    evaluator.function_definitions = {}
    # test
    parts = ['double', ['n'], ['*', 'n', 2]]
    want_name = 'double'
    want_formals = ['n']
    want_body = ['*', 'n', 2]
    got = define_function(parts)
    assert '<UserFunction (double n)>' == got
    new_func = evaluator.function_definitions[want_name]
    assert want_name == new_func.name
    assert want_formals == new_func.formals
    assert want_body == new_func.body
    # restore function_definitions
    evaluator.function_definitions = initial_fundefs