def p_expr(p): '''Expression : UnaryExpr | Expression BinaryOp Expression''' p[0] = Node('Expression') if len(p) == 2: p[0].typeList = p[1].typeList p[0].placeList = p[1].placeList p[0].sizeList = p[1].sizeList p[0].code = p[1].code else: if p[1].typeList[0] != p[3].typeList[0]: compilation_errors.add('TypeMismatch', line_number.get()+1, 'Type should be same across binary operator') elif p[1].typeList[0][0] not in p[2].extra: compilation_errors.add('TypeMismatch', line_number.get()+1, 'Invalid type for binary expression') else: if len(p[2].typeList) > 0: # for boolean p[0].typeList = p[2].typeList else: p[0].typeList = p[1].typeList newVar = helper.newVar(p[0].typeList[0], p[1].sizeList[0]) p[0].code = p[1].code p[0].code += p[3].code if len(p[2].extra) < 3: p[0].code.append([p[2].extra['opcode'], newVar, p[1].placeList[0], p[3].placeList[0]]) else: p[0].code.append([p[2].extra['opcode'] + p[1].typeList[0][0], newVar, p[1].placeList[0], p[3].placeList[0]]) p[0].sizeList = p[1].sizeList p[0].placeList.append(newVar)
def p_unary_expr(p): '''UnaryExpr : PrimaryExpr | UnaryOp UnaryExpr | NOT UnaryExpr''' p[0] = Node('UnaryExpr') if len(p) == 2: p[0].typeList = p[1].typeList p[0].placeList = p[1].placeList p[0].sizeList = p[1].sizeList p[0].code = p[1].code elif p[1] == '!': if p[2].typeList[0] != ['bool']: compilation_errors.add('TypeMismatch', line_number.get()+1, 'Type should be boolean') else: p[0].typeList = p[2].typeList p[0].placeList = p[2].placeList p[0].sizeList = p[2].sizeList p[0].code = p[2].code newVar = helper.newVar(p[0].typeList[0], p[0].sizeList[0]) p[0].code.append(['!', newVar, p[2].placeList[0]]) else: if p[2].typeList[0][0] not in p[1].extra: compilation_errors.add('TypeMismatch', line_number.get()+1, 'Invalid type for unary expression') else: p[0].typeList = p[2].typeList p[0].placeList = p[2].placeList p[0].sizeList = p[2].sizeList p[0].code = p[2].code newVar = helper.newVar(p[0].typeList[0], p[0].sizeList[0]) p[0].code.append([p[1].extra['opcode'], newVar, p[2].placeList[0]])
def p_struct_type(p): '''StructType : STRUCT LBRACE FieldDeclRep RBRACE''' p[0] = Node('StructType') for index_ in range(len(p[3].identList)): if p[3].identList[index_] in p[3].identList[:index_]: compilation_errors.add('Redeclaration Error',line_number.get()+1, 'Field %s redeclared'%p[3].identList[index_]) return p[0] = p[3] dict_ = {} offset_ = 0 for index_ in range(len(p[3].identList)): dict_[p[3].identList[index_]] = {'type':p[3].typeList[index_], 'size': p[3].sizeList[index_], 'offset':offset_} offset_ += p[3].sizeList[index_] p[0].typeList = [['struct', dict_]] p[0].sizeList = [sum(p[3].sizeList)]