Пример #1
0
def test_complex():
    """Ensure we tokenize complex numbers properly"""
    # This is a regression test for #143
    entry = tokenize("(1j)")[0][0]
    assert entry == Complex("1.0j")
    entry = tokenize("(1J)")[0][0]
    assert entry == Complex("1.0j")
    entry = tokenize("(j)")[0][0]
    assert entry == Symbol("j")
    entry = tokenize("(J)")[0][0]
    assert entry == Symbol("J")
Пример #2
0
def symbol_like(obj):
    "Try to interpret `obj` as a number or keyword."

    try:
        return Integer(obj)
    except ValueError:
        pass

    if '/' in obj:
        try:
            lhs, rhs = obj.split('/')
            return Expression(
                [Symbol('hy._Fraction'),
                 Integer(lhs),
                 Integer(rhs)])
        except ValueError:
            pass

    try:
        return Float(obj)
    except ValueError:
        pass

    if obj not in ('j', 'J'):
        try:
            return Complex(obj)
        except ValueError:
            pass

    if obj.startswith(":") and "." not in obj:
        return Keyword(obj[1:])
Пример #3
0
def test_lex_digit_separators():

    assert tokenize("1_000_000") == [Integer(1000000)]
    assert tokenize("1,000,000") == [Integer(1000000)]
    assert tokenize("1,000_000") == [Integer(1000000)]
    assert tokenize("1_000,000") == [Integer(1000000)]

    assert tokenize("0x_af") == [Integer(0xaf)]
    assert tokenize("0x,af") == [Integer(0xaf)]
    assert tokenize("0b_010") == [Integer(0b010)]
    assert tokenize("0b,010") == [Integer(0b010)]
    assert tokenize("0o_373") == [Integer(0o373)]
    assert tokenize("0o,373") == [Integer(0o373)]

    assert tokenize('1_2.3,4') == [Float(12.34)]
    assert tokenize('1_2e3,4') == [Float(12e34)]
    assert (tokenize("1,2/3_4") == [
        Expression([Symbol("hy._Fraction"),
                    Integer(12),
                    Integer(34)])
    ])
    assert tokenize("1,0_00j") == [Complex(1000j)]

    assert tokenize("1,,,,___,____,,__,,2__,,,__") == [Integer(12)]
    assert (tokenize("_1,,,,___,____,,__,,2__,,,__") == [
        Symbol("_1,,,,___,____,,__,,2__,,,__")
    ])
    assert (tokenize("1,,,,___,____,,__,,2__,q,__") == [
        Symbol("1,,,,___,____,,__,,2__,q,__")
    ])
Пример #4
0
def symbol_like(obj):
    "Try to interpret `obj` as a number or keyword."

    try:
        return Integer(obj)
    except ValueError:
        pass

    if "/" in obj:
        try:
            lhs, rhs = obj.split("/")
            return Expression(
                [sym("hy._Fraction"),
                 Integer(lhs), Integer(rhs)])
        except ValueError:
            pass

    try:
        return Float(obj)
    except ValueError:
        pass

    if obj not in ("j", "J"):
        try:
            return Complex(obj)
        except ValueError:
            pass

    if obj.startswith(":") and "." not in obj:
        return Keyword(obj[1:], from_parser=True)
Пример #5
0
def test_lex_expression_complex():
    """ Make sure expressions can produce complex """
    def t(x):
        return tokenize("(foo {})".format(x))

    def f(x):
        return [Expression([Symbol("foo"), x])]

    assert t("2.j") == f(Complex(2.j))
    assert t("-0.5j") == f(Complex(-0.5j))
    assert t("1.e7j") == f(Complex(1e7j))
    assert t("j") == f(Symbol("j"))
    assert t("J") == f(Symbol("J"))
    assert isnan(t("NaNj")[0][1].imag)
    assert t("nanj") == f(Symbol("nanj"))
    assert t("Inf+Infj") == f(Complex(complex(float("inf"), float("inf"))))
    assert t("Inf-Infj") == f(Complex(complex(float("inf"), float("-inf"))))
    assert t("Inf-INFj") == f(Symbol("Inf-INFj"))
Пример #6
0
def test_number_model_copy():
    i = Integer(42)
    assert (i == copy.copy(i))
    assert (i == copy.deepcopy(i))

    f = Float(42.)
    assert (f == copy.copy(f))
    assert (f == copy.deepcopy(f))

    c = Complex(42j)
    assert (c == copy.copy(c))
    assert (c == copy.deepcopy(c))
Пример #7
0
def test_number_model_copy():
    i = Integer(42)
    assert i == copy.copy(i)
    assert i == copy.deepcopy(i)

    f = Float(42.0)
    assert f == copy.copy(f)
    assert f == copy.deepcopy(f)

    c = Complex(42j)
    assert c == copy.copy(c)
    assert c == copy.deepcopy(c)
Пример #8
0
def test_lex_big_float():
    # https://github.com/hylang/hy/issues/1448
    assert tokenize("1e900") == [Float(1e900)]
    assert tokenize("1e900-1e900j") == [Complex(1e900, -1e900)]