Exemplo n.º 1
0
def common_type(a, b):
    """ Returns a type which is common for both a and b types.
    Returns None if no common types allowed.
    """
    from symbols.type_ import SymbolBASICTYPE as BASICTYPE
    from symbols.type_ import Type as TYPE
    from symbols.type_ import SymbolTYPE

    if a is None or b is None:
        return None

    if not isinstance(a, SymbolTYPE):
        a = a.type_

    if not isinstance(b, SymbolTYPE):
        b = b.type_

    if a == b:  # Both types are the same?
        return a  # Returns it

    if a == TYPE.unknown and b == TYPE.unknown:
        return BASICTYPE(global_.DEFAULT_TYPE)

    if a == TYPE.unknown:
        return b

    if b == TYPE.unknown:
        return a

    # TODO: This will removed / expanded in the future
    assert a.is_basic
    assert b.is_basic

    types = (a, b)

    if TYPE.float_ in types:
        return TYPE.float_

    if TYPE.fixed in types:
        return TYPE.fixed

    if TYPE.string in types:  # TODO: Check this ??
        return TYPE.unknown

    result = a if a.size > b.size else b

    if not TYPE.is_unsigned(a) or not TYPE.is_unsigned(b):
        result = TYPE.to_signed(result)

    return result
Exemplo n.º 2
0
def is_numeric(*p):
    from symbols.type_ import Type

    try:
        for i in p:
            if not i.type_.is_basic or not Type.is_numeric(i.type_):
                return False

        return True
    except:
        pass

    return False
Exemplo n.º 3
0
def is_integer(*p):
    from symbols.type_ import Type

    try:
        for i in p:
            if not i.is_basic or not Type.is_integral(i.type_):
                return False

        return True
    except:
        pass

    return False
Exemplo n.º 4
0
def is_signed(*p):
    """ Returns false unless all types in p are signed
    """
    from symbols.type_ import Type

    try:
        for i in p:
            if not i.type_.is_basic or not Type.is_signed(i.type_):
                return False

        return True
    except:
        pass

    return False
Exemplo n.º 5
0
def is_numeric(*p):
    """ Returns false unless all elements in p are of numerical type
    """
    from symbols.type_ import Type

    try:
        for i in p:
            if not i.type_.is_basic or not Type.is_numeric(i.type_):
                return False

        return True
    except:
        pass

    return False