def exec(self, code): """ Execute the string ``code`` on the interpreter, returning the result of the last evaluation. """ r = NIL for expr in parse(lex(code)): r = self.eval(expr) return r
def test_many_quotes(quotes): parser = parse(iter([q] * quotes + [lp, rp])) r = next(parser) for _ in range(quotes): assert isinstance(r, Quoted) r = r.elem assert r is NIL with pytest.raises(StopIteration): next(parser)
def test_many_quote_nested(quotes, inner): parser = parse(iter([q] * quotes + [lp, inner, rp])) r = next(parser) for _ in range(quotes): assert isinstance(r, Quoted) r = r.elem assert r == SExpression(inner) with pytest.raises(StopIteration): next(parser)
def parse_string(code: String): """ ``code`` is a ``String`` containing a single thing once parsed, such as an s-expression, or a symbol. Simply just lex it, parse it and return it! Woo-hoo! >>> from slyther.types import * >>> parse_string(String("(print x)")) (list print x) Note that the ``BuiltinFunction`` decorator takes care of downgrading an ``SExpression`` to a ``ConsList`` for you. """ return next(parse(lex(code)))
def test_bad_quotation_rp(): parser = parse(iter([q, s('a'), lp, q, q, q, rp])) assert next(parser) == Quoted(s('a')) with pytest.raises(SyntaxError): next(parser)
def test_bad_quotation_end(): parser = parse(iter([q, -15, q, q])) assert next(parser) == Quoted(-15) with pytest.raises(SyntaxError): next(parser)
def test_missing_lp(): parser = parse(iter([10, rp])) assert next(parser) is 10 with pytest.raises(SyntaxError): next(parser)
def test_missing_rp(): parser = parse(iter([lp, rp, lp, lp, rp])) assert next(parser) is NIL with pytest.raises(SyntaxError): next(parser)
def test_valid_parses(ast): assert list(parse(deparse(ast))) == ast