예제 #1
0
def p_simple_expr1(p):
    '''simple_expr1 :   literal
                    |   ID LSQRB access RSQRB
                    |   path
                    |   LPARAN exprs_1 RPARAN
                    |   simple_expr1 argument_exprs'''
    #|   simple_expr DOT id
    p[0] = dict()
    if p.slice[1].type == 'literal':
        p[0] = p[1]
    elif p.slice[1].type == 'path':
        p[0] = p[1]
    elif len(p) == 5:
        p[0]['idVal'] = p[1]
        p[0]['arrAccess'] = True
        p[0]['type'] = ST.getAttribute(p[0]['idVal'], 'type')
        p[0]['place'] = p[1]
        size = ST.getId(p[1])['size']  #a,b,c size
        place = p[3]  # i,j,k access index
        temp = makeIndex(size, place)
        p[0]['index'] = temp
        #p[0]['index'] = p[3]['place']
    elif p.slice[1].type == 'LPARAN':
        p[0] = p[2]
    elif p.slice[2].type == 'argument_exprs':
        #       x = p[1]['idVal'].split('.')
        if (p[1]['name'] == 'println'):
            if (is_number(p[2][0]['place'])):
                temp = ST.getTemp()
                emit("=", temp, p[2][0]['place'])
                emit('printInt', temp)
            else:
                p[2][0] = evalArray(p[2][0])
                emit('printInt', p[2][0]['place'])
        elif (p[1]['name'] == 'readInt'):
            emit('scanInt', p[2][0]['place'])
        else:
            #temp=ST.getTemp()
            #print("=,"+temp+","+str(len(p[2])))
            #print "HUl"
            l = []
            for i in p[2]:
                if (is_number(i['place'])):
                    temp = ST.getTemp()
                    print("=," + temp + "," + str(i['place']))
                    l.append(temp)
                else:
                    if i['type'] == 'Array':
                        l.append(i['place'] + "@" + "ARRAY")
                    else:
                        l.append(i['place'])
            for i in reversed(l):
                emit("param", i)
            name = p[1]["place"] + "@" + str(len(p[2]))
            rtype = ST.getRType(name)
            if (rtype != "void"):
                temp1 = ST.getTemp()
                p[0]["place"] = temp1
                p[0]['type'] = p[1]['rType']['type']
                #print p[1]
                emit("fcall", temp1, p[1]["place"] + "__" + p[1]['objname'],
                     len(p[2]))
            else:
                emit('call', None, p[1]['place'] + "__" + p[1]['objname'],
                     temp)
    printp(p)
예제 #2
0
def p_path(p):
    '''path :   id
            |   R_THIS
            |   path DOT id
            |   R_SUPER DOT path
            '''
    # ST.printSymbolTable(ST,1)
    if (p.slice[1].type == 'id'):
        if p[1] not in ST.function:
            p[0] = ST.getId(p[1])
        else:
            p[0] = ST.getFunc(p[1])
    elif (p.slice[1].type == 'R_THIS'):
        p[0] = {
            'isthis': True,
        }
    elif (p.slice[1].type == 'path'):
        #print ST.function
        #ST.printSymbolTable()
        #print p[1]['type'],p[3]
        #print p[1]
        if 'isthis' in p[1].keys():
            parent = ST.SymbolTable[ST.currScope]['classname']
            extends = ST.SymbolTable[parent]['extends']
            flagfunc = 1
            while parent is not None:
                if "_ZXY" + parent + p[3] in ST.function:
                    flagfunc = 0
                    break
                if extends is None:
                    break
                parent = extends
                extends = ST.SymbolTable[parent]['extends']
            #if "_ZXY"+parent+ p[3] not in ST.function:
            if flagfunc:
                parent = ST.SymbolTable[ST.currScope]['classname']
                extends = ST.SymbolTable[parent]['extends']
                if p[3] in ST.SymbolTable[parent]['identifiers']:
                    p[0] = dict(ST.SymbolTable[parent]['identifiers'][p[3]])
                    p[0]['isthis'] = True
                    p[0]['place'] = 'this.' + p[3]
                elif extends is not None:
                    flag = 1
                    p[3] = extends + p[3]
                    me = parent
                    while extends is not None:
                        #p[3] = extends + p[3]
                        if p[3] in ST.SymbolTable[me]['identifiers']:
                            p[0] = dict(
                                ST.SymbolTable[me]['identifiers'][p[3]])
                            p[0]['isthis'] = True
                            p[0]['place'] = 'this.' + p[3]
                            flag = 0
                            break
                        print me
                        parent = extends
                        print p[3]
                        # ST.printSymbolTable()
                        extends = ST.SymbolTable[parent]['extends']
                        if extends is not None:
                            p[3] = p[3].replace(parent, parent + extends)

                    #if p[3] in ST.SymbolTable[extends]['identifiers']:
                    #    p[0] = dict(ST.SymbolTable[parent]['identifiers'][extends+p[3]])
                    #    p[0]['isthis']=True
                    #    p[0]['place']='this.'+extends+p[3]
                    if flag:
                        sys.exit("Variable not found")
                else:
                    sys.exit("Variable not found")
            else:
                p[0] = dict(ST.getClassFunc("_ZXY" + parent + p[3]))
                p[0]['objname'] = "this"
        else:
            parent = p[1]['type']
            flagfunc = 1
            if parent in ST.SymbolTable:
                extends = ST.SymbolTable[parent]['extends']
                while parent is not None:
                    if "_ZXY" + parent + p[3] in ST.function:
                        flagfunc = 0
                        break
                    if extends is None:
                        break
                    parent = extends
                    extends = ST.SymbolTable[parent]['extends']

            if flagfunc:
                l = ST.getAttribute(p[1]['place'], 'list')
                p[0] = l[p[3]]
            else:
                p[0] = dict(ST.getClassFunc("_ZXY" + parent + p[3]))
                p[0]['objname'] = p[1]['place']
        #print p[0]
    printp(p)