示例#1
0
def do_keymap(kmap, key):
    try:
        symbol = kmap.get(key)
        if symbol is not None:
            if type(symbol) is types.FunctionType:
                symbol()
                return True
            else:
                logging.debug('(' + lispy.to_string(symbol) + ')')
                lispy.eval([symbol])
                return True
        else:
            return False
    except Exception as e:
        logging.exception(e)
示例#2
0
 def command(self, cmd: str):
     print('Executing:', cmd)
     result = eval(parse(cmd), env=self.global_env)
     if result is not None:
         if isinstance(result, int) or isinstance(result, float):
             print('Result:', result)
         else:
             print('Result:', type(result))
             print(result)
     return result
示例#3
0
文件: main.py 项目: masahitojp/lispy3
def main(prompt="lispy> "):
    scheme_completer = WordCompleter(["begin", "call/cc"])
    session = PromptSession(lexer=PygmentsLexer(SchemeLexer),
                            completer=scheme_completer)

    while True:
        try:
            text = session.prompt(prompt)
            x = parse(text)
            if x is eof_object:
                return
            val = eval(x)
            if val is not None:
                print(to_string(val))
        except KeyboardInterrupt:
            continue
        except EOFError:
            break
        except Exception as e:
            print(f"{type(e).__name__}: {e}")
    print("GoodBye!")
示例#4
0
            ("'(one 2 3)", ['one', 2, 3]),
            ("(define L (list 1 2 3))", None),
            ("`(testing ,@L testing)", ['testing', 1, 2, 3, 'testing']),
            ("`(testing ,L testing)", ['testing', [1, 2, 3], 'testing']),
            ("`,@L", SyntaxError),
            ("""'(1 ;test comments '
             ;skip this line
             2 ; more ; comments ; ) )
             3) ; final comment""", [1, 2, 3]),
        ]

        for (x, expected) in tests:
            result = ''

            try:
                result = lispy.eval(lispy.parse(x))
                print(x, '=>', lispy.to_string(result))
                ok = (result == expected)
            except Exception as e:
                print(x, '=raises=>', type(e).__name__, e)
                ok = inspect.isclass(expected) and issubclass(
                    expected, Exception) and isinstance(e, expected)
            if not ok:
                print('expression: %s' % x)
                print('    actual: %s' % result)
                print('  expected: %s' % expected)
                break
        else:
            print('PASS')

    else:
示例#5
0
from lispy import var, env, Symbol, parse, eval

run = lambda src, env=None: eval(parse(src), env)
x, y, a, b, c, f, g, h, op = map(Symbol, 'x y a b c f g h op'.split())


class TestGrammarSimple:
    def test_infix(self):
        assert parse('[1 + 2]') == [Symbol.ADD, 1, 2]
        assert parse('[a b c]') == [b, a, c]

    def test_let(self):
        assert parse(':let {x = 1} in (+ x 1)')       == [Symbol.LET, [[x, 1]], [Symbol.ADD, x, 1]]
        assert parse(':let {x = 1 y = 2} in (+ x y)') == [Symbol.LET, [[x, 1], [y, 2]], [Symbol.ADD, x, y]]

    def test_if(self):
        assert parse(':if #t then: 42 :else: 0') == [Symbol.IF, True, 42, 0]


class TestGrammarExtended:
    def test_infix(self):
        assert parse('[(f a) (op) (g b)]') == [[op], [f, a], [g, b]]

    def test_let(self):
        assert parse(':let {x = (+ 1 1)} in (+ x 1)') == [Symbol.LET, [[x, [Symbol.ADD, 1, 1]]], [Symbol.ADD, x, 1]]

    def test_if(self):
        assert parse(':if (h a) then: (f b) :else: (g c)') == [Symbol.IF, [h, a], [f, b], [g, c]]
        assert parse(':if #f then: 40 :elif #t then: 42 :else: 0') == [Symbol.IF, False, 40, [Symbol.IF, True, 42, 0]]