def p_prefix_expr(p): '''prefix_expr : simple_expr | OP_SUB infix_expr | OP_ADD infix_expr | OP_NOT infix_expr''' if len(p) == 2: p[0] = p[1] else: if p.slice[1].type == 'OP_ADD': p[0] = p[2] elif p.slice[1].type == 'OP_SUB': temp = ST.getTemp() emit("-", temp, 0, p[2]['place']) p[0] = {} p[0]['type'] = p[2]['type'] p[0]['place'] = temp p[0]['idVal'] = temp else: temp = ST.getTemp() emit("!", temp, p[2]['place']) p[0] = {} p[0]['type'] = p[2]['type'] p[0]['place'] = temp p[0]['idVal'] = temp printp(p)
def p_assign(p): ''' assign : simple_expr1 asgn or_expression ''' if 'islit' in p[1].keys(): sys.exit("Type Error: Assignment to constant not defined.") temp = ST.getTemp() p[0] = {'place': temp, 'isarray': False, 'type': 'Not defined'} if p[2] == '=': c1 = p[1]['type'] c2 = p[3]['type'] if p[1]['type'] == 'Array': c1 = 'INT' if p[3]['type'] == 'Array': c2 = 'INT' if c1 != c2: sys.exit("Type error bla") p[3] = evalArray(p[3]) if p[1]['type'] == 'Array': emit('star', p[1]['place'], p[1]['index'], p[3]['place']) else: emit(op='=', out=p[1]['place'], in1=p[3]['place']) else: #print "ds is awesome" p[3] = evalArray(p[3]) t1 = ST.getTemp() t2 = evalArray(p[1]) #print p[2][0] emit(p[2][0], t1, t2['place'], p[3]['place']) if p[1]['type'] == 'Array': emit('star', p[1]['place'], p[1]['index'], t1) else: emit('=', p[1]['place'], t1) ##Check printp(p)
def evalArray(temp): if type(temp) == type({}) and 'type' in temp and temp['type'] == 'Array': t1 = ST.getTemp() #print "I am here" emit(op='ldar', out=t1, in1=temp['place'], in2=temp['index']) r = {'place': t1, 'type': 'Array'} return r return temp
def check(var, op): if is_number(var) and op != "=": t = ST.getTemp() l = ['=', t, var] myList = ','.join(map(str, l)) print myList return t else: return var
def makeIndex(size, place): t = ST.getTemp() t1 = ST.getTemp() old = 0 old1 = 0 for i in range(0, len(place) - 1): for j in range(i + 1, len(size)): if (j == i + 1 and len(size) > 1): t = ST.getTemp() emit("*", t, place[i]['place'], size[j]) old = t elif (len(size) > 1): t = ST.getTemp() emit("*", t, old, size[j]) old = t t1 = ST.getTemp() emit("+", t1, old1, t) old1 = t1 emit("+", t1, old1, place[len(place) - 1]["place"]) return t1
def p_switch_block(p): ''' switch_block : BLOCKBEGIN s_mark1 switch_block_statements_0 BLOCKEND ''' emit('goto', p[2]['label'][1]) emit('label', p[2]['label'][0]) for d in p[3]: temp = ST.getTemp() emit(op='==', out=temp, in1=d['place'], in2=p[2]['idVal']['place']) emit('if', d['label'], temp) emit('label', p[2]['label'][1]) ST.endScope() printp(p)
def p_xor_expression(p): ''' xor_expression : bit_and_expression | xor_expression XOR bit_and_expression ''' if len(p) == 2: p[0] = p[1] else: c1 = p[1]['type'] c2 = p[3]['type'] if p[1]['type'] == 'Array': c1 = 'INT' if p[3]['type'] == 'Array': c2 = 'INT' if c1 != c2: sys.exit("Type error bla") temp = ST.getTemp() p[0] = {'place': temp, 'type': 'INT'} p[3] = evalArray(p[3]) p[1] = evalArray(p[1]) emit(op='^', out=temp, in1=p[1]['place'], in2=p[3]['place']) printp(p)
def p_add_expression(p): '''add_expression : mul_expression | add_expression OP_ADD mul_expression | add_expression OP_SUB mul_expression ''' if len(p) == 2: p[0] = p[1] else: c1 = p[1]['type'] c2 = p[3]['type'] if p[1]['type'] == 'Array': c1 = 'INT' if p[3]['type'] == 'Array': c2 = 'INT' if c1 != c2: sys.exit("Type error bla") temp = ST.getTemp() p[0] = {'place': temp, 'type': 'INT'} p[3] = evalArray(p[3]) p[1] = evalArray(p[1]) emit(op=p[2], out=temp, in1=p[1]['place'], in2=p[3]['place']) printp(p)
def p_eq_expression(p): '''eq_expression : comp_expression | eq_expression EQ comp_expression | eq_expression NEQ comp_expression ''' if len(p) == 2: p[0] = p[1] else: c1 = p[1]['type'] c2 = p[3]['type'] if p[1]['type'] == 'Array': c1 = 'INT' if p[3]['type'] == 'Array': c2 = 'INT' if c1 != c2: sys.exit("Type error bla") temp = ST.getTemp() p[0] = {'place': temp, 'type': 'bool'} p[3] = evalArray(p[3]) p[1] = evalArray(p[1]) emit(op=p[2], out=temp, in1=p[1]['place'], in2=p[3]['place']) #printp(p) printp(p)
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)