def test_lex_expression_complex(): """ Make sure expressions can produce complex """ objs = tokenize("(foo 2.j)") assert objs == [HyExpression([HySymbol("foo"), HyComplex(2.j)])] objs = tokenize("(foo -0.5j)") assert objs == [HyExpression([HySymbol("foo"), HyComplex(-0.5j)])] objs = tokenize("(foo 1.e7j)") assert objs == [HyExpression([HySymbol("foo"), HyComplex(1.e7j)])]
def test_complex(): """Ensure we tokenize complex numbers properly""" # This is a regression test for #143 entry = tokenize("(1j)")[0][0] assert entry == HyComplex("1.0j") entry = tokenize("(j)")[0][0] assert entry == HySymbol("j")
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 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 _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)