def test_list(self): assert parse('(+ 1 2)') == [Symbol.ADD, 1, 2] assert parse('(1 2 3 4)') == [1, 2, 3, 4] assert parse('(func)') == [ Symbol('func'), ] assert parse('()') == []
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
def _gen_macros(self): lisp._eval( lisp.parse('''(begin (define-macro and (lambda args (if (null? args) #t (if (= (length args) 1) (car args) `(if ,(car args) (and ,@(cdr args)) #f))))) ;; More macros can also go here )'''), self.env)
def test_strings(self): assert parse('"foobar"') == "foobar" assert parse('"foo bar"') == "foo bar" assert parse(r'"foo\nbar"') == "foo\nbar" assert parse(r'"foo\tbar"') == "foo\tbar" assert parse(r'"foo\tbar"') == "foo\tbar" assert parse(r'"foo\"bar\""') == "foo\"bar\""
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!")
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]]
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]]
def test_quote_is_converted_to_sexpr(self): assert parse("'42") == [Symbol.QUOTE, 42] assert parse("'1") == [Symbol.QUOTE, 1] assert parse("'x") == [Symbol.QUOTE, x] assert parse("'(1 2 3)") == [Symbol.QUOTE, [1, 2, 3]] assert parse("'(+ 40 2)") == [Symbol.QUOTE, [Symbol.ADD, 40, 2]]
def _parse(self, cmd): return lisp.parse(cmd)
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 1)} in (+ x 1)') == [Symbol.LET, [[x, [Symbol.ADD, 1, 1]]], [Symbol.ADD, x, 1]]
def test_atomic(self): assert parse('#t') is True assert parse('#f') is False assert parse('x') == x assert parse('+') == Symbol('+')
def _postprocess_line(line): res = '' for word in lispy.parse(line): res += _postprocess_word(word) + ' ' # remove the last space return res[:-1]
words = [edge.there.name for edge in edges if(re.match("^"+string, edge.there.name))] return words, len(words) stageFile = raw_input("Enter the stage text filename: ").strip() reader = StageReader(stageFile) reader.printMatrix(reader.graph.m) #reader.printMatrix(reader.graph.mPrime) current = reader.graph.nodes[0] while True: outer = re.compile("\((.+)\)") conv = outer.search(current.description) if conv: for string in conv.groups(): for part in lispy.sexp("("+string+")"): eval(lispy.parse(part)) print "\n"+ current.description if len(current.out) > 0: for edge in current.out: print "(",edge.there.name,")", edge.description next = raw_input("\nWhat would you like to do next? > ").strip() matched, length = match(next, current.out) if length > 1 or length < 1: current = current print "\nThe name you entered matched", "none" if length < 1 else "more than one", "!" else: current = reader.graph.node(matched[0]) else: print "Congratulations, you have finished the game!" break
def test_if(self): assert parse(':if #t then: 42 :else: 0') == [Symbol.IF, True, 42, 0]
def test_nested_list(self): assert parse('(1 (2 (3 4)))') == [1, [2, [3, 4]]] assert parse('((1 2 3))') == [[1, 2, 3]]
def test_infix(self): assert parse('[(f a) (op) (g b)]') == [[op], [f, a], [g, b]]
def test_numbers(self): assert parse('42') == 42 assert parse('3.14') == 3.14 assert parse('-3.14') == -3.14
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]]
def test_e2e(code, expected): assert evaluate(compile_bytecode(parse(tokenize(code)))) == expected
("'(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:
def test_parse(tokens, expected): """assert tokens parse correctly""" assert parse(tokens) == expected