Ejemplo n.º 1
0
def flattenSumType(p, memo=None, history=None):
    if memo is None:
        memo = {}
    if history is None:
        history = []
    if p.type_id:
        memo[p.type_id] = True
    l = []
    for i in range(len(p)):
        e = p[i]
        if e.isSumType():
            history.append(i)
            l.extend(flattenSumType(e, memo, history))
            history.pop()
        elif e.isId() and not e.id in builtin:
            t = lookupType(e.id_type_id)
            if t.id_type_id not in memo and t.isSumType():
                history.append(i)
                l.extend(flattenSumType(t, memo))
                history.pop()
            else:
                l.append((t, history[:] + [i]))
        else:
            l.append((e, history[:] + [i]))
    return l
Ejemplo n.º 2
0
def flattenSumType(p, memo = None, history=None):
    if memo is None:
        memo = {}
    if history is None:
        history = []
    if p.type_id:
        memo[p.type_id] = True
    l = []
    for i in range(len(p)):
        e = p[i]
        if e.isSumType():
            history.append(i)
            l.extend(flattenSumType(e, memo, history))
            history.pop()
        elif e.isId() and not e.id in builtin:
            t = lookupType(e.id_type_id)
            if t.id_type_id not in memo and t.isSumType():
                history.append(i)
                l.extend(flattenSumType(t, memo))
                history.pop()
            else:
                l.append((t, history[:] + [i]))
        else:
            l.append((e, history[:] + [i]))
    return l
Ejemplo n.º 3
0
def doesMatch_types(tup, templ):
    if isinstance(tup, tuple):
        if len(tup) != len(templ):
            raise NoTupleMatch
        l = []
        for tup_e, templ_e in zip(tup, templ):
            e = doesMatch(tup_e, templ_e)
            if e is None:
                raise NoTupleMatch
            l.append(e)
        return tuple(l)
    elif templ.isType():
        assert templ.type is not None, (str(tup), str(templ))
        if tup.type.type_id == templ.type_id:
            return tup
        templ_e = lookupType(templ.type_id)
        tup_e = lookupType(tup.type.type_id)
        iso = lookupIso(tup_e, templ_e)
        if iso is None:
            print "failed to match", tup, tup.type, templ
            raise NoTupleMatch
        v = iso(tup)
        return v
    else:
        if tup.type.type_id == templ.type.type_id:
            if tup == templ:
                return templ
            else:
                raise NoTupleMatch
        tup_e = lookupType(tup.type.type_id)
        templ_e = lookupType(templ.type.type_id)
        iso = lookupIso(tup_e, templ_e)
        if iso is None:
            raise NoTupleMatch
        v = iso(tup)
        if v == templ:
            return templ
        else:
            raise NoTupleMatch
Ejemplo n.º 4
0
def doesMatch_types(tup, templ):
    if isinstance(tup, tuple):
        if len(tup) != len(templ):
            raise NoTupleMatch
        l = []
        for tup_e, templ_e in zip(tup, templ):
            e = doesMatch(tup_e, templ_e)
            if e is None:
                raise NoTupleMatch
            l.append(e)
        return tuple(l)
    elif templ.isType():
        assert templ.type is not None, (str(tup), str(templ))
        if tup.type.type_id == templ.type_id:
            return tup
        templ_e = lookupType(templ.type_id)
        tup_e = lookupType(tup.type.type_id)
        iso = lookupIso(tup_e, templ_e)
        if iso is None:
            print "failed to match", tup, tup.type, templ
            raise NoTupleMatch
        v = iso(tup)
        return v
    else:
        if tup.type.type_id == templ.type.type_id:
            if tup == templ:
                return templ
            else:
                raise NoTupleMatch
        tup_e = lookupType(tup.type.type_id)
        templ_e = lookupType(templ.type.type_id)
        iso = lookupIso(tup_e, templ_e)
        if iso is None:
            raise NoTupleMatch
        v = iso(tup)
        if v == templ:
            return templ
        else:
            raise NoTupleMatch
Ejemplo n.º 5
0
def construct(reg, type):
    if type.isProductType():
        v = []
        for i in range(len(type)):
            v.append(construct(reg, type[i]))
        return _linda_server.Value(tuple(v))
    elif type.isId() and type.id not in builtin:
        t = lookupType(type.id_type_id)
        if t.isProductType():
            return construct(reg, t)
        else:
            v = reg[0]
            del reg[0]
            return v
    else:
        v = reg[0]
        del reg[0]
        return v
Ejemplo n.º 6
0
def flattenProductType(p, memo=None):
    if memo is None:
        memo = {}
    if p.type_id:
        memo[p.type_id] = True
    l = []
    for i in range(len(p)):
        e = p[i]
        if e.isProductType():
            l.extend(flattenProductType(e, memo))
        elif e.isId() and not e.id in builtin:
            t = lookupType(e.id_type_id)
            if t.id_type_id not in memo and t.isProductType():
                l.extend(flattenProductType(t, memo))
            else:
                l.append(t)
        else:
            l.append(t)
    return l
Ejemplo n.º 7
0
def flattenProductType(p, memo = None):
    if memo is None:
        memo = {}
    if p.type_id:
        memo[p.type_id] = True
    l = []
    for i in range(len(p)):
        e = p[i]
        if e.isProductType():
            l.extend(flattenProductType(e, memo))
        elif e.isId() and not e.id in builtin:
            t = lookupType(e.id_type_id)
            if t.id_type_id not in memo and t.isProductType():
                l.extend(flattenProductType(t, memo))
            else:
                l.append(t)
        else:
            l.append(t)
    return l
Ejemplo n.º 8
0
def compare_types(t1, t2, checked=None):
    assert t1.isType()
    assert t2.isType()

    if checked is None:
        checked = {(t1, t2): []}
    elif (t1, t2) in checked:
        return lambda x: convertValue(checked(t1, t2), x)
    else:
        checked[(t1, t2)] = []

    code = checked[(t1, t2)]

    print t1, t2, t1.isFunctionType(), t2.isFunctionType()

    if t1.isNil() and t2.isNil():
        code.append(("SETTYPEID", t2.type_id))
    elif t1.isId() and t2.isId():
        if t1.id in builtin and t2.id in builtin:
            if t1.id == t2.id:
                code.append(("SETTYPEID", t2.type_id))
                return lambda x: convertValue(checked[(t1, t2)], x)
            else:
                return None
        else:
            print t1.id, t2.id
            if t1.id not in builtin:
                nt = lookupType(t1.id_type_id)
                func = compare_types(nt, t2, checked)
                if func:
                    code.append(("EVAL", checked[(nt, t2)]))
                    return lambda x: convertValue(checked[(t1, t2)], x)
            if t2.id not in builtin:
                nt = lookupType(t2.id_type_id)
                func = compare_types(t1, nt, checked)
                if func:
                    code.append(("EVAL", checked[(t1, nt)]))
                    return lambda x: convertValue(checked[(t1, t2)], x)
            if t1.id not in builtin and t2.id not in builtin:
                nt1 = lookupType(t1.id_type_id)
                nt2 = lookupType(t2.id_type_id)
                func = compare_types(nt1, nt2, checked)
                if func:
                    code.append(("EVAL", checked[(nt1, nt2)]))
                    return lambda x: convertValue(checked[(t1, t2)], x)
                else:
                    return None
    elif t1.isProductType() and t2.isProductType():
        t1e = flattenProductType(t1)
        t2e = flattenProductType(t2)
        if len(t1e) != len(t2e):
            return None
        funcs = []
        for e1, e2 in zip(t1e, t2e):
            f = compare_types(e1, e2, checked)
            if f is None:
                return
            else:
                funcs.append(checked[(e1, e2)])
        code.append(("EXPLODE", t1))
        for i in range(len(funcs)):
            code.append(("EVAL", funcs[i]))
            code.append(("ROTATE", ))
        code.append(("CONSTRUCT", t2))
        code.append(("SETTYPEID", t2.type_id))
        return lambda x: convertValue(checked[(t1, t2)], x)
    elif t1.isSumType() and t2.isSumType():
        t1e = flattenSumType(t1)
        t2e = flattenSumType(t2)
        if len(t1e) != len(t2e):
            return None
        for perm in all_perms(range(len(t2))):
            funcs = []
            for i in range(len(perm)):
                f = compare_types(t1e[i][0], t2e[perm[i]][0], checked)
                if f is None:
                    break
                else:
                    funcs.append(checked[(t1e[i][0], t2e[perm[i]][0])])
                    for pos in t2e[perm[i]][1][::-1]:
                        funcs[-1].append(("CREATESUM", perm[i]))
            if len(funcs) == len(perm):
                code.append(("SUMCASE", funcs))
        code.append(("SETTYPEID", t2.type_id))
        return lambda x: convertValue(checked[(t1, t2)], x)
    elif t1.isPtrType() and t2.isPtrType():
        f = compare_types(t1.ptrtype, t2.ptrtype, checked)
        if f is None:
            return
        code.append(("PTRCONV", checked[(t1.ptrtype, t2.ptrtype)]))
        return lambda x: convertValue(checked[(t1, t2)], x)
    elif t1.isFunctionType() and t2.isFunctionType():
        print "got functions", t1, t2
        arg = compare_types(t1.arg, t2.arg, checked)
        if arg is None:
            return None
        result = compare_types(t2.result, t1.result, checked)
        if result is None:
            return None
        return lambda x: wrap_function(t2.arg, checked[
            (t1.arg, t2.arg)], t2.result, checked[(t2.result, t1.result)], x)
Ejemplo n.º 9
0
def compare_types(t1, t2, checked=None):
    assert t1.isType()
    assert t2.isType()

    if checked is None:
        checked = {(t1, t2): []}
    elif (t1, t2) in checked:
        return lambda x: convertValue(checked(t1, t2), x)
    else:
        checked[(t1, t2)] = []

    code = checked[(t1, t2)]

    print t1, t2, t1.isFunctionType(), t2.isFunctionType()

    if t1.isNil() and t2.isNil():
        code.append(("SETTYPEID", t2.type_id))
    elif t1.isId() and t2.isId():
        if t1.id in builtin and t2.id in builtin:
            if t1.id == t2.id:
                code.append(("SETTYPEID", t2.type_id))
                return lambda x: convertValue(checked[(t1, t2)], x)
            else:
                return None
        else:
            print t1.id, t2.id
            if t1.id not in builtin:
                nt = lookupType(t1.id_type_id)
                func = compare_types(nt, t2, checked)
                if func:
                    code.append(("EVAL", checked[(nt, t2)]))
                    return lambda x: convertValue(checked[(t1, t2)], x)
            if t2.id not in builtin:
                nt = lookupType(t2.id_type_id)
                func = compare_types(t1, nt, checked)
                if func:
                    code.append(("EVAL", checked[(t1, nt)]))
                    return lambda x: convertValue(checked[(t1, t2)], x)
            if t1.id not in builtin and t2.id not in builtin:
                nt1 = lookupType(t1.id_type_id)
                nt2 = lookupType(t2.id_type_id)
                func = compare_types(nt1, nt2, checked)
                if func:
                    code.append(("EVAL", checked[(nt1, nt2)]))
                    return lambda x: convertValue(checked[(t1, t2)], x)
                else:
                    return None
    elif t1.isProductType() and t2.isProductType():
        t1e = flattenProductType(t1)
        t2e = flattenProductType(t2)
        if len(t1e) != len(t2e):
            return None
        funcs = []
        for e1, e2 in zip(t1e, t2e):
            f = compare_types(e1, e2, checked)
            if f is None:
                return
            else:
                funcs.append(checked[(e1, e2)])
        code.append(("EXPLODE", t1))
        for i in range(len(funcs)):
            code.append(("EVAL", funcs[i]))
            code.append(("ROTATE", ))
        code.append(("CONSTRUCT", t2))
        code.append(("SETTYPEID", t2.type_id))
        return lambda x: convertValue(checked[(t1, t2)], x)
    elif t1.isSumType() and t2.isSumType():
        t1e = flattenSumType(t1)
        t2e = flattenSumType(t2)
        if len(t1e) != len(t2e):
            return None
        for perm in all_perms(range(len(t2))):
            funcs = []
            for i in range(len(perm)):
                f = compare_types(t1e[i][0], t2e[perm[i]][0], checked)
                if f is None:
                    break
                else:
                    funcs.append(checked[(t1e[i][0], t2e[perm[i]][0])])
                    for pos in t2e[perm[i]][1][::-1]:
                        funcs[-1].append(("CREATESUM", perm[i]))
            if len(funcs) == len(perm):
                code.append(("SUMCASE", funcs))
        code.append(("SETTYPEID", t2.type_id))
        return lambda x: convertValue(checked[(t1, t2)], x)
    elif t1.isPtrType() and t2.isPtrType():
        f = compare_types(t1.ptrtype, t2.ptrtype, checked)
        if f is None:
            return
        code.append(("PTRCONV", checked[(t1.ptrtype, t2.ptrtype)]))
        return lambda x: convertValue(checked[(t1, t2)], x)
    elif t1.isFunctionType() and t2.isFunctionType():
        print "got functions", t1, t2
        arg = compare_types(t1.arg, t2.arg, checked)
        if arg is None:
            return None
        result = compare_types(t2.result, t1.result, checked)
        if result is None:
            return None
        return lambda x: wrap_function(t2.arg, checked[(t1.arg, t2.arg)], t2.result, checked[(t2.result, t1.result)], x)