예제 #1
0
def Rename(t, m):
    """ return t renamed according to map m """
    if LiteralP(t): return t
    if VarP(t):
        v = Var(t)
        ok, w = map.Apply(m, v)
        if not ok:
            print('Undefined variable ' + pio.Items(v))
            exit()
        return VarC(w)
    if OperationP(t):
        o, opds = OperationD(t)
        ropds = pop.ConsAll2(opds, Rename, m)
        return OperationC(o, ropds)
    if WhereP(t):
        subj, wk, body = WhereD(t)
        l = WhereLocalsL(t)
        n = NewMap(l)
        m1 = map.Update(m, n)
        rsubj = Rename(subj, m1)
        rbody = pop.Empty
        while body != pop.Empty:  #run through definitions in body
            df, body = pop.ConsD(body)
            rdf = Rdefinition(df, m, m1)
            rbody = pop.Append(rbody, List1(rdf))
        return WhereC(rsubj, wk, rbody)

    if CallP(t):
        fun, actsl = CallD(t)
        rfun = Rename(fun, m)
        ractsl = pop.ConsAll2(actsl, Rename, m)
        return CallC(rfun, ractsl)
    assert False, 'rename a strange term ' + str(t)
예제 #2
0
def ArityCheck(e, m):
    """ check that the vars in e are used consistent with the arity map m """
    if LiteralP(e): return True, None, None
    if VarP(e):
        ok, n = map.Apply(m, Var(e))
        if not ok:
            print('undefined variable ' + pio.Words(Var(e)))
            exit()
        if pop.NumVal(n) != 0: return False, Var(e), pop.NumVal(n)
        return True, None, None
    if OperationP(e):
        o, opds = OperationD(e)
        return ArityCheckList(opds, m)
    if WhereP(e):
        subj, wk, body = WhereD(e)
        ok, var, n = ArityCheckList(body, m)
        if not ok: return False, var, n
        return ArityCheck(subj, m)
    if VarDefinitionP(e):
        v, es, rhs = VarDefinitionD(e)
        ok, n = map.Apply(m, v)
        if not ok:
            print('undefined variable ' + str(v))
            exit()
        if pop.NumVal(n) != 0: return False, v, pop.NumVal(n)
        return ArityCheck(rhs, m)
    if FunDefinitionP(e):
        f, formals, es, rhs = FunDefinitionD(e)
        ok, n = map.Apply(m, f)
        if not ok: print('undefined variable ' + str(f))
        if pop.NumVal(n) != pop.Length(formals): return False, f, pop.NumVal(n)
        return ArityCheck(rhs, m)
    if CallP(e):
        fun, actuals = CallD(e)
        ok, n = map.Apply(m, Var(fun))
        if not ok:
            print('undefined var ' + str(Var(fun)))
            exit()
        if pop.NumVal(n) != pop.Length(actuals):
            return False, Var(fun), pop.NumVal(n)
        return ArityCheckList(actuals, m)
    assert False, 'corrupt expression' + str(e)
예제 #3
0
def ActualDefs(amap, formap):
    """ generate defs for the formals in terms of actuals"""
    """amap is the actuals map formap is the formals map """
    funset = map.Domain(amap)  #set of function variable
    deflist = pop.Empty
    while not pop.EmptyP(funset):  #run  through the functions
        fun, funset = pop.AddD(funset)
        ok, actsl = map.Apply(amap, fun)
        if not ok:
            print('No actuals recorded for ' + str(fun))
            exit()
        ok, formals = map.Apply(formap, fun)
        fms = formals
        al = actsl
        while not pop.EmptyP(fms):
            acts = pop.ConsAll(al, pop.Head)
            al = pop.ConsAll(al, pop.Tail)
            fm, fms = pop.ConsD(fms)
            actexpr = OperationC(ACTUALWord, acts)
            df = DefinitionC(VarC(fm), EQUALWord, actexpr)
            deflist = pop.Cons(df, deflist)
    return deflist
예제 #4
0
def NewVar(v):
    global varcount
    ok, n = map.Apply(varcount, v)
    if not ok:
        map.Extend(varcount, v, 0)
        n = 0
    suffix = str(n)
    varcount = map.MapS(varcount, v, n + 1)
    if n != 0:
        rvname = pop.WordName(v) + '_' + suffix
    else:
        rvname = pop.WordName(v)
    return pop.WordC(rvname)
예제 #5
0
def ActualTable(e, atab, ftab):
    """ collects a table of actuals in the order of appearance
        returns table and yaghified program"""
    if LiteralP(e): return atab, ftab, e
    if VarP(e): return atab, ftab, e
    if OperationP(e):
        o, opds = OperationD(e)
        if pop.ListP(o):
            print(e)
            print(o)
            exit()
        tab, ftab, yopds = ActualTableList(opds, atab, ftab)
        return tab, ftab, OperationC(o, yopds)
    if WhereP(e):
        subj, wk, body = WhereD(e)
        atab, ftab, ysubj = ActualTable(subj, atab, ftab)
        atab, ftab, ybody = ActualTableList(body, atab, ftab)
        return atab, ftab, WhereC(ysubj, wk, ybody)
    if VarDefinitionP(e):
        v, es, rhs = VarDefinitionD(e)
        atab, ftab, yrhs = ActualTable(rhs, atab, ftab)
        return atab, ftab, DefinitionC(VarC(v), es, yrhs)
    if FunDefinitionP(e):
        f, formals, es, rhs = FunDefinitionD(e)
        atab, ftab, yrhs = ActualTable(rhs, atab, ftab)
        ftab = map.Extend(ftab, f, formals)
        return atab, ftab, DefinitionC(VarC(f), es, yrhs)
    if CallP(e):
        fun, actuals = CallD(e)
        funvar = Var(fun)
        atab, ftab, yactuals = ActualTableList(actuals, atab, ftab)
        ok, actlist = map.Apply(atab, funvar)
        if not ok: actlist = pop.Empty
        newactlist = pop.Append(actlist, pop.List1(yactuals))
        atab = map.MapS(atab, funvar, newactlist)
        return atab, ftab, OperationC(
            YCALLWord,
            pop.List2(fun, LiteralC(pop.NumC(pop.Length(newactlist) - 1))))