void tryEvaluatePrefixOperatorOnStackThenPush(Operator *newToken,Stack *numberStack,Stack *operatorStack) { Operator *previousToken=(Operator*)stackPop(operatorStack); if(previousToken == NULL){ stackPush(newToken,operatorStack); } else{ while(previousToken!=NULL) { if(((Operator*)newToken)->info->id ==CLOSING_BRACKET_OP){ if(((Operator*)previousToken)->info == NULL){ Throw(ERR_EXPECTING_OPENING_BRACKET); }else if(((Operator*)previousToken)->info->id == OPENING_BRACKET_OP){ operatorPrefixEvaluate(numberStack ,previousToken); free(newToken); if(((Operator*)previousToken)->info->id == OPENING_BRACKET_OP){ previousToken=(Operator*)stackPop(operatorStack); break; } }else { operatorEvaluate(numberStack,previousToken); } }else if(newToken->info->precedence >= previousToken->info->precedence || ((Operator*)newToken)->info->id==OPENING_BRACKET_OP ){ break; } else{ operatorEvaluate(numberStack,previousToken); } previousToken=(Operator*)stackPop(operatorStack); } if(previousToken!=NULL ){ stackPush(previousToken,operatorStack); } if(newToken->info->affix == PREFIX){ stackPush(newToken,operatorStack); } } }
void tryEvaluateOperatorOnStackThenPush(Operator *newToken,Stack *numberStack,Stack *operatorStack){ Operator *previousToken; previousToken=(Operator*)stackPop(operatorStack); if(previousToken==NULL){ stackPush(newToken,operatorStack); }else{ while(previousToken!=NULL){ if(newToken->info->precedence > previousToken->info->precedence){ break; } else{ operatorEvaluate(numberStack,previousToken); } previousToken=(Operator*)stackPop(operatorStack); } if(previousToken!=NULL ){ stackPush(previousToken,operatorStack); } stackPush(newToken,operatorStack); } }
void test_operator_evaluate(){ Tokenizer tokenizer; Operator multiply = {.operators=MULTIPLY}; Token *token1 = (Token*)&multiply; Number number4 = {.value=4}; Token *token2 = (Token*)&number4; Number number3 = {.value=3}; Token *token3 = (Token*)&number3; Operator addition = {.operators=ADD}; Token *token4 = (Token*)&addition; Number number2 = {.value=2}; Token *token5 = (Token*)&number2; int tempResult,finalResult; Stack numStack; Stack opeStack; Token resultToken; Token finalToken; operatorPop_CMockExpectAndReturn(37, &opeStack, token1); numberPop_CMockExpectAndReturn(39, &numStack, token2); numberPop_CMockExpectAndReturn(41, &numStack, token3); tempResult = number4.value*number3.value; createNumberToken_CMockExpectAndReturn(44, tempResult, &resultToken); numberPush_CMockExpect(48, &resultToken); operatorPop_CMockExpectAndReturn(50, &opeStack, token4); numberPop_CMockExpectAndReturn(52, &numStack, &resultToken); numberPop_CMockExpectAndReturn(54, &numStack, token5); finalResult = tempResult+number2.value; createNumberToken_CMockExpectAndReturn(57, finalResult, &finalToken); operatorEvaluate(&numStack,&opeStack,tempResult,finalResult); }