def test_sets(): """ Ensure that we can tokenize a set. """ objs = tokenize("#{1 2}") assert objs == [HySet([HyInteger(1), HyInteger(2)])] objs = tokenize("(bar #{foo bar baz})") assert objs == [ HyExpression([HySymbol("bar"), HySet(["foo", "bar", "baz"])]) ] objs = tokenize("#{(foo bar) (baz quux)}") assert objs == [ HySet([ HyExpression([HySymbol("foo"), HySymbol("bar")]), HyExpression([HySymbol("baz"), HySymbol("quux")]) ]) ] # Duplicate items in a literal set should be okay (and should # be preserved). objs = tokenize("#{1 2 1 1 2 1}") assert objs == [HySet([HyInteger(n) for n in [1, 2, 1, 1, 2, 1]])] assert len(objs[0]) == 6 # https://github.com/hylang/hy/issues/1120 objs = tokenize("#{a 1}") assert objs == [HySet([HySymbol("a"), HyInteger(1)])]
def test_compiler_yield_return(self): """ Check that the compiler correctly generates return statements for a generator function. In Python versions prior to 3.3, the return statement in a generator can't take a value, so the final expression should not generate a return statement. From 3.3 onwards a return value should be generated. """ ret = self.c.compile_function_def( self._make_expression(HySymbol("fn"), HyList(), HyExpression([HySymbol("yield"), HyInteger(2)]), HyExpression([HySymbol("+"), HyInteger(1), HyInteger(1)]))) self.assertEqual(len(ret.stmts), 1) stmt = ret.stmts[0] self.assertIsInstance(stmt, ast.FunctionDef) body = stmt.body self.assertEquals(len(body), 2) self.assertIsInstance(body[0], ast.Expr) self.assertIsInstance(body[0].value, ast.Yield) if PY33: # From 3.3+, the final statement becomes a return value self.assertIsInstance(body[1], ast.Return) self.assertIsInstance(body[1].value, ast.BinOp) else: # In earlier versions, the expression is not returned self.assertIsInstance(body[1], ast.Expr) self.assertIsInstance(body[1].value, ast.BinOp)
def t_identifier(p): obj = p[0].value try: return HyInteger(obj) except ValueError: pass if '/' in obj: try: lhs, rhs = obj.split('/') return HyExpression( [HySymbol('fraction'), HyInteger(lhs), HyInteger(rhs)]) except ValueError: pass try: return HyFloat(obj) except ValueError: pass if obj != 'j': try: return HyComplex(obj) except ValueError: pass if obj.startswith(":"): return HyKeyword(obj) obj = ".".join([hy_symbol_mangle(part) for part in obj.split(".")]) return HySymbol(obj)
def test_lex_fractions(): """ Make sure that fractions are valid expressions""" objs = tokenize("1/2") assert objs == [ HyExpression([HySymbol("fraction"), HyInteger(1), HyInteger(2)]) ]
def take_macro(tree): tree.pop(0) # "take" n = tree.pop(0) ret = tree.pop(0) return HyExpression([HySymbol('slice'), ret, HyInteger(0), HyInteger(n)])
def t_identifier(p): obj = p[0].value try: return HyInteger(obj) except ValueError: pass if '/' in obj: try: lhs, rhs = obj.split('/') return HyExpression( [HySymbol('fraction'), HyInteger(lhs), HyInteger(rhs)]) except ValueError: pass try: return HyFloat(obj) except ValueError: pass if obj != 'j': try: return HyComplex(obj) except ValueError: pass table = { "true": "True", "false": "False", "nil": "None", "null": "None", } if obj in table: return HySymbol(table[obj]) if obj.startswith(":"): return HyKeyword(obj) def mangle(p): if p.startswith("*") and p.endswith("*") and p not in ("*", "**"): p = p[1:-1].upper() if "-" in p and p != "-": p = p.replace("-", "_") if p.endswith("?") and p != "?": p = "is_%s" % (p[:-1]) return p obj = ".".join([mangle(part) for part in obj.split(".")]) return HySymbol(obj)
def drop_macro(tree): tree.pop(0) # "drop" n = tree.pop(0) ret = tree.pop(0) return HyExpression([HySymbol('slice'), ret, HyInteger(n)])
def rest_macro(tree): tree.pop(0) # "first" ret = tree.pop(0) # the list # assert tree is empty return HyExpression([HySymbol('slice'), ret, HyInteger(1)])
def test_sets(): """ Ensure that we can tokenize a set. """ objs = tokenize("#{1 2}") assert objs == [HySet([HyInteger(1), HyInteger(2)])] objs = tokenize("(bar #{foo bar baz})") assert objs == [ HyExpression([HySymbol("bar"), HySet(["foo", "bar", "baz"])]) ] objs = tokenize("#{(foo bar) (baz quux)}") assert objs == [ HySet([ HyExpression([HySymbol("foo"), HySymbol("bar")]), HyExpression([HySymbol("baz"), HySymbol("quux")]) ]) ]
def _resolve_atom(obj): """ Resolve a bare atom into one of the following (in order): - Integer - LambdaListKeyword - Float - Complex - Symbol """ try: return HyInteger(obj) except ValueError: pass if obj.startswith("&"): return HyLambdaListKeyword(obj) try: return HyFloat(obj) except ValueError: pass if obj != "j": try: return HyComplex(obj) except ValueError: pass table = { "true": "True", "false": "False", "null": "None", } if obj in table: return HySymbol(table[obj]) if obj.startswith(":"): return HyKeyword(obj) if obj.startswith("*") and obj.endswith("*") and obj not in ("*", "**"): obj = obj[1:-1].upper() if "-" in obj and obj != "-": obj = obj.replace("-", "_") return HySymbol(obj)
def t_identifier(p): obj = p[0].value try: return HyInteger(obj) except ValueError: pass try: return HyFloat(obj) except ValueError: pass if obj != 'j': try: return HyComplex(obj) except ValueError: pass table = { "true": "True", "false": "False", "null": "None", } if obj in table: return HySymbol(table[obj]) if obj.startswith(":"): return HyKeyword(obj) if obj.startswith("&"): return HyLambdaListKeyword(obj) if obj.startswith("*") and obj.endswith("*") and obj not in ("*", "**"): obj = obj[1:-1].upper() if "-" in obj and obj != "-": obj = obj.replace("-", "_") return HySymbol(obj)
def test_replace_tuple(): """ Test replacing tuples.""" replaced = replace_hy_obj((long_type(0), ), HyInteger(13)) assert type(replaced) == HyList assert type(replaced[0]) == HyInteger assert replaced == HyList([HyInteger(0)])
def first_macro(lst): return HyExpression([HySymbol('get'), lst, HyInteger(0)])
def rest_macro(lst): return HyExpression([HySymbol('slice'), lst, HyInteger(1)])
def test_lex_expression_integer(): """ Make sure expressions can produce integers """ objs = tokenize("(foo 2)") assert objs == [HyExpression([HySymbol("foo"), HyInteger(2)])]
def test_wrap_nested_expr(): """ Test conversion of HyExpressions with embedded non-HyObjects.""" wrapped = _wrap_value(HyExpression([long_type(0)])) assert type(wrapped) == HyExpression assert type(wrapped[0]) == HyInteger assert wrapped == HyExpression([HyInteger(0)])
def test_wrap_tuple(): """ Test conversion of tuples.""" wrapped = _wrap_value((HyInteger(0),)) assert type(wrapped) == HyList assert type(wrapped[0]) == HyInteger assert wrapped == HyList([HyInteger(0)])
def drop_macro(n, lst): return HyExpression([HySymbol('slice'), lst, HyInteger(n)])
def test_lex_integers(): """ Make sure that integers are valid expressions""" objs = tokenize("42 ") assert objs == [HyInteger(42)]
def test_replace_long_type(): """ Test replacing integers.""" replaced = replace_hy_obj(long_type(0), HyInteger(13)) assert replaced == HyInteger(0)