Esempio n. 1
0
def Assembly(aprog):
    """ convert atomic program aprog to assembly program asprog """
    asprog = {}
    subj, wk, dl = exp.WhereD(aprog)
    if exp.VarP(subj):
        subj = exp.Operation1C(exp.IDWord, subj)
    odef = exp.DefinitionC(exp.VarC(exp.OUTPUTWord), pop.EQUALWord, subj)
    dl = pop.Cons(odef, dl)
    asprog = {}  #initialize the assembly program, a dictionary
    #that assigns to every variable name a tuple consisting
    #of the operation symbol and the array of the names of the variables
    #all python strings
    while dl != pop.Empty:  #loop through the definitions of the atomized program
        df, dl = pop.DeCons(dl)
        lhs, es, rhs = exp.DefinitionD(df)  #dismantle current definition
        vname = pio.Words(exp.Var(lhs))  #get name of var being defined
        if exp.LiteralP(
                rhs):  #if its a literal treat quote mark as a pseudo op
            asprog[vname] = ('"', exp.LiteralValue(rhs))
            continue
        if exp.VarP(rhs):  # rhs a single variable, add id operator
            rhs = exp.Operation1C(exp.IDWord, rhs)
            #its an operation applied to operands
        o, ods = exp.OperationD(
            rhs)  #break rhs into operation symbol and operand list
        odsa = []  #initialize array of operands
        while ods != pop.Empty:  #iterate through operand list
            vw, ods = pop.ConsD(ods)  #get current operand, a var, and advance
            vws = pio.Words(
                exp.Var(vw))  #name of current operand, a var, as a string
            odsa.append(vws)  #append to the array
            #finished looping throug operands, so odsa complete
        asprog[vname] = (pio.Words(o), odsa)
    return asprog
Esempio n. 2
0
def ActualTableList(l, atab, ftab):
    yl = pop.Empty
    while l != pop.Empty:
        e, l = pop.ConsD(l)
        atab, ftab, ye = ActualTable(e, atab, ftab)
        yl = pop.Append(yl, pop.List1(ye))
    return atab, ftab, yl
Esempio n. 3
0
def Aritytab(e, m):
    """ compile a table of arities for funs in e """
    if LiteralP(e): return m
    if VarP(e): return m
    if OperationP(e):
        o, opds = OperationD(e)
        return AritytabList(opds, m)
    if WhereP(e):
        subj, wk, body = WhereD(e)
        m = Aritytab(subj, m)
        return AritytabList(body, m)
    if VarDefinitionP(e):
        v, es, rhs = VarDefinitionD(e)
        m = map.Extend(m, v, pop.NumC(0))
        return Aritytab(rhs, m)
    if FunDefinitionP(e):
        f, formals, es, rhs = FunDefinitionD(e)
        n = pop.Length(formals)
        while formals != pop.Empty:
            formal, formals = pop.ConsD(formals)
            m = map.Extend(m, formal, pop.NumC(0))
        m = map.Extend(m, f, pop.NumC(n))
        return Aritytab(rhs, m)
    if CallP(e):
        fun, actuals = CallD(e)
        return AritytabList(actuals, m)
    assert False, 'corrupt expression' + str(e)
Esempio n. 4
0
def Eprogram(pg):
    """ return pg with only where clauses for function defs left """
    """ assume function defs at top level only """
    subj, wk, body = WhereD(pg)
    b = body
    ebody = pop.Empty
    while not pop.EmptyP(b):  #run through dfs in body
        df, b = pop.ConsD(b)  #get current def and advance
        lhs, es, rhs = DefinitionD(df)  #dismantle current def
        if not WhereP(rhs):  # no where's
            ebody = pop.AddElement(ebody, df)  #add current def unchanged
            #print('ebody is',pio.Items(ebody));exit()
            continue
        rsubj, rwk, rbody = WhereD(rhs)  #dismantle rhs
        if VarDefinitionP(df):  #a var defined by a where
            rhs1 = Eprogram(rhs)  #flatten rhs
            r1subj, rwk1, r1body = WhereD(
                rhs1)  #dismantle new flat whereclause
            ebody = pop.Append(
                ebody, r1body)  #add the emptied defs to the program body
            df1 = DefinitionC(lhs, es,
                              r1subj)  #change df so var is equated to subject
            ebody = pop.AddElement(ebody, df1)  #add simplified def
            continue
        #a function where definition
        rhsf = Eprogram(rhs)  #flatten the rhs where clause
        df1 = DefinitionC(lhs, es,
                          rhsf)  #construct new definition with flattened rhs
        ebody = pop.AddElement(ebody, df1)
    return WhereC(subj, wk, ebody)
Esempio n. 5
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)
Esempio n. 6
0
def Formals(w):
    """ returns a table of lists of formals """
    formap = map.EmptyMap
    subj, wk, body = WhereD(w)
    while body != pop.Empty:
        d, body = pop.ConsD(body)
        if VarDefinitionP(d): continue
        f, formals, es, rhs = FunDefinitionD(d)
        formap = map.Extend(formap, f, formals)
    return formap
Esempio n. 7
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
Esempio n. 8
0
def AritytabList(l, m):
    while l != pop.Empty:
        e, l = pop.ConsD(l)
        m = Aritytab(e, m)
    return m
Esempio n. 9
0
def ArityCheckList(l, m):
    while l != pop.Empty:
        e, l = pop.ConsD(l)
        ok, v, n = ArityCheck(e, m)
        if not ok: return False, v, n
    return True, None, None
Esempio n. 10
0
    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


if __name__ == "__main__":
    pg = prs.ParseFile("testprog.lu")
    atab, ftab, ypg = ActualTable(pg, map.EmptyMap, map.EmptyMap)
    prp.Term(ypg)
    print()
    pio.WriteItemln(atab)
    pio.WriteItemln(ftab)
    defs = ActualDefs(atab, ftab)
    while not pop.EmptyP(defs):
        df, defs = pop.ConsD(defs)
        prp.Term(df)
        print()