コード例 #1
0
ファイル: test_interpreter.py プロジェクト: e-dorigatti/lispy
def test_import():
    inpr = IterativeInterpreter()
    eval_expr('(pyimport json)', inpr)

    import json
    assert inpr.ctx.get('json') == json
    assert eval_expr('(json.loads "[1, 2, 3]")', inpr) == [1, 2, 3]
コード例 #2
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_last():
    inpr = load_stdlib(IterativeInterpreter())

    with pytest.raises(IndexError):
        eval_expr('(last (list))', inpr)

    assert eval_expr('(last (list 3 2 1))', inpr) == 1
コード例 #3
0
ファイル: test_interpreter.py プロジェクト: e-dorigatti/lispy
def test_do():
    inpr = IterativeInterpreter()
    assert eval_expr('(do (+ 1 1) (- 1 1))', inpr) == 0

    assert eval_expr('(do (defn inc (x) (+ x 1)) (inc 1))', inpr) == 2
    assert 'inc' in inpr.ctx

    assert eval_expr('(do 3 & (list (+ 1 1) (- 1 1)))', inpr) == 0
コード例 #4
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_first():
    inpr = load_stdlib(IterativeInterpreter())

    inpr.ctx['x'] = []
    with pytest.raises(IndexError):
        eval_expr('(first x)', inpr)

    inpr.ctx['x'] = [3, 2, 1]
    assert eval_expr('(first x)', inpr) == 3
コード例 #5
0
ファイル: test_interpreter.py プロジェクト: e-dorigatti/lispy
def test_let_unpack():
    inpr = IterativeInterpreter()
    assert eval_expr('(let ((a b c) (list 1 2 3)) (+ a b c))', inpr) == 6

    with pytest.raises(RuntimeError):
        eval_expr('(let ((a b c) (list 1)) 3)', inpr)

    with pytest.raises(RuntimeError):
        eval_expr('(let ((a) (list 1 2 3)) 3)', inpr)
コード例 #6
0
ファイル: test_interpreter.py プロジェクト: e-dorigatti/lispy
def test_function_arg_unpack():
    inpr = IterativeInterpreter()
    eval_expr('(defn f (a (b c) (d (e (f g)))) (+ a b c d e f g))', inpr)
    assert eval_expr(
        '(let (a 1 b 1 c 1 d 1 x (list 1 (list 1 1))) (f a (list b c) (list d x)))',
        inpr) == 7

    with pytest.raises(SyntaxError):
        eval_expr('(defn g (a & (b c)) (+ a b c))', inpr)
コード例 #7
0
ファイル: test_interpreter.py プロジェクト: e-dorigatti/lispy
def test_or():
    inpr = IterativeInterpreter()
    eval_expr(
        '(def calls (list)) (defn canary (x) (do ((. append calls) x) x))',
        inpr)
    assert eval_expr('(or (canary 0) (canary 1) (canary 2))', inpr)
    assert inpr.ctx['calls'] == [0, 1]

    assert eval_expr('(or 0 & (list 0 2 3))', inpr)
コード例 #8
0
ファイル: test_interpreter.py プロジェクト: e-dorigatti/lispy
def test_and():
    inpr = IterativeInterpreter()
    eval_expr(
        '(def calls (list)) (defn canary (x) (do ((. append calls) x) x))',
        inpr)
    assert not eval_expr('(and (canary 0) (canary 1) (canary 2))', inpr)
    assert inpr.ctx['calls'] == [0]

    assert not eval_expr('(and 1 & (list 2 0 3))', inpr)
コード例 #9
0
ファイル: test_interpreter.py プロジェクト: e-dorigatti/lispy
def test_macro():
    inpr = IterativeInterpreter()
    eval_expr(
        '(defmacro infix (args) (list (nth args 1) (nth args 0) (nth args 2)))',
        inpr)
    assert eval_expr('(let (i 1) (infix (i + 1)))', inpr) == 2

    assert eval_expr('(macroexpand infix (1 + 1))', inpr) == [
        Token('+', Token.TOKEN_OTHER),
        Token(1, Token.TOKEN_LITERAL),
        Token(1, Token.TOKEN_LITERAL)
    ]

    assert eval_expr('(infix (1 + 1))', inpr) == 2
コード例 #10
0
ファイル: test_interpreter.py プロジェクト: e-dorigatti/lispy
def test_quote():
    inpr = IterativeInterpreter()

    def t(val):
        return Token(val, Token.guess_token_type(str(val)[0]))

    assert eval_expr('(quote + 1 2)', inpr) == [t('+'), t(1), t(2)]
    assert eval_expr('(quote 1 2 (3 (4 5) 6) 7)',
                     inpr) == [t(1),
                               t(2), [t(3), [t(4), t(5)],
                                      t(6)],
                               t(7)]
    assert eval_expr('(quote + 1 ~(+ 1 1))', inpr) == [t('+'), t(1), 2]

    with pytest.raises(RuntimeError):
        eval_expr('(+ ~(= 1 0) 2)', inpr)
コード例 #11
0
def main(input_file, expression, without_stdlib, do_repl, **kwargs):
    '''
    Python-based LISP interpreter.

    Starts the REPL when invoked without arguments. Otherwise, executes the code in
    the file (if given), then executes the provided expression (if given), then
    enters the REPL (if the flag is specified).
    '''
    inpr = IterativeInterpreter(with_stdlib=not without_stdlib)

    if input_file:
        for f in input_file:
            eval_expr(f.read(), inpr)

    if expression:
        result = eval_expr(expression, inpr)

        if isinstance(result, list):
            print(ExpressionTree.to_string(result))
        else:
            print(result)

    if do_repl or (not expression and not input_file):
        repl(inpr, **kwargs)
コード例 #12
0
ファイル: test_interpreter.py プロジェクト: e-dorigatti/lispy
def test_match():
    inpr = IterativeInterpreter()

    match = '(match %s ((a) 1) ((a b) 2) ((a b c) 3) (a -1))'

    assert eval_expr(match % '(list 1)', inpr) == 1
    assert eval_expr(match % '(list 1 2)', inpr) == 2
    assert eval_expr(match % '(list 1 2 3)', inpr) == 3
    assert eval_expr(match % '4', inpr) == -1

    with pytest.raises(RuntimeError):
        eval_expr('(match (list 1 2) ((a) 1))', inpr)
コード例 #13
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_when():
    inpr = load_stdlib(IterativeInterpreter())
    assert eval_expr('(when (= 1 1) 1 2 3)', inpr) == 3
    assert eval_expr('(when (!= 1 1) 1 2 3)', inpr) == None
コード例 #14
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_append():
    inpr = load_stdlib(IterativeInterpreter())
    assert eval_expr('(append (list 2 3) 1)', inpr) == [2, 3, 1]
    assert eval_expr('(append (list) 1)', inpr) == [1]
コード例 #15
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_inc():
    inpr = load_stdlib(IterativeInterpreter())
    assert eval_expr('(inc 0)', inpr) == 1
コード例 #16
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_cons():
    inpr = load_stdlib(IterativeInterpreter())
    assert eval_expr('(cons 1 (list 2 3))', inpr) == [1, 2, 3]
    assert eval_expr('(cons 1 (list))', inpr) == [1]
コード例 #17
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_curry():
    inpr = load_stdlib(IterativeInterpreter())
    assert eval_expr('((curry * 2) 2)', inpr) == 4
コード例 #18
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_skip():
    inpr = load_stdlib(IterativeInterpreter())

    assert eval_expr('(skip 0 (list 1 2 3))', inpr) == [1, 2, 3]
    assert eval_expr('(skip 2 (list))', inpr) == []
    assert eval_expr('(skip 2 (list 0 1 2 3))', inpr) == [2, 3]
コード例 #19
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_rest():
    inpr = load_stdlib(IterativeInterpreter())

    assert eval_expr('(rest (list))', inpr) == []
    assert eval_expr('(rest (list 1))', inpr) == []
    assert eval_expr('(rest (list 1 2))', inpr) == [2]
コード例 #20
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_dec():
    inpr = load_stdlib(IterativeInterpreter())
    assert eval_expr('(dec 1)', inpr) == 0
コード例 #21
0
ファイル: test_interpreter.py プロジェクト: e-dorigatti/lispy
def test_varargs():
    inpr = IterativeInterpreter()
    assert eval_expr('(defn sum (& args) (+ &args)) (sum 1 2 3 4)', inpr) == 10
    assert eval_expr('(defn f (x y z) (+ x y z)) (f 1 & (list 2 3))',
                     inpr) == 6
コード例 #22
0
ファイル: test_interpreter.py プロジェクト: e-dorigatti/lispy
def test_import_from():
    inpr = IterativeInterpreter()
    eval_expr('(pyimport_from json.decoder JSONArray)', inpr)

    from json.decoder import JSONArray
    assert inpr.ctx.get('JSONArray') == JSONArray
コード例 #23
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_extend():
    inpr = load_stdlib(IterativeInterpreter())
    assert eval_expr('(extend (list 1 2 3) (list 4 5 6))',
                     inpr) == [1, 2, 3, 4, 5, 6]
    assert eval_expr('(extend (list) (list 4 5 6))', inpr) == [4, 5, 6]
    assert eval_expr('(extend (list 1 2 3) (list))', inpr) == [1, 2, 3]
コード例 #24
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_unless():
    inpr = load_stdlib(IterativeInterpreter())
    assert eval_expr('(unless (= 1 1) 1)', inpr) == None
    assert eval_expr('(unless (!= 1 1) 1)', inpr) == 1
コード例 #25
0
ファイル: test_interpreter.py プロジェクト: e-dorigatti/lispy
def test_def():
    inpr = IterativeInterpreter()
    assert eval_expr('(def x 1 y (+ x 1))', inpr) == 2
    assert 'x' in inpr.ctx and 'y' in inpr.ctx
    assert eval_expr('(+ x y)', inpr) == 3
コード例 #26
0
ファイル: test_interpreter.py プロジェクト: e-dorigatti/lispy
def test_member():
    ctx = ExecutionContext(None)
    ctx['s'] = '  abc  '
    inpr = IterativeInterpreter(ctx)
    assert eval_expr(r'(. strip s)', inpr) == ctx['s'].strip
コード例 #27
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_zip():
    inpr = load_stdlib(IterativeInterpreter())
    assert eval_expr('(zip (list 1 2) (list 3 4 5) (list 6 7 8 9))',
                     inpr) == [[1, 3, 6], [2, 4, 7]]
    assert eval_expr('(zip (list) (list 1 2 3))', inpr) == []
コード例 #28
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_reduce():
    inpr = load_stdlib(IterativeInterpreter())
    assert eval_expr('(reduce (# extend %0 %1) (list 1 2) (list 3 4))',
                     inpr) == [1, 2, 3, 4]
コード例 #29
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_flatten():
    inpr = load_stdlib(IterativeInterpreter())
    assert eval_expr('(flatten (list 1 2 (list 3 (list 4 (list )) 6 ) 7))',
                     inpr) == [1, 2, 3, 4, 6, 7]
コード例 #30
0
ファイル: test_stdlib.py プロジェクト: e-dorigatti/lispy
def test_letfn():
    inpr = load_stdlib(IterativeInterpreter())
    assert eval_expr('(letfn (add (x y z) (+ x y z)) (add 1 2 3))', inpr) == 6