/* Given                            should become
 *            OperatorToken *                 OperatorToken *
 *            --------------                  --------------
 * type:      TOKEN_OPERATOR_TYPE             TOKEN_OPERATOR_TYPE
 * symbol:       '('                          '('
 * arity:         0                           PREFIX
 * assoc:         0                           LEFT_TO_RIGHT
 * precedence:    0                           13
 *
 */
void test_tryConvertToPrefix_given_openBracket_should_convert_to_prefix_successfully(void)
{
  OperatorToken *op = (OperatorToken*)createOperatorToken("(");

  OperatorToken **opP = malloc(sizeof(OperatorToken));
  opP = &op;
  tryConvertToPrefix((Token ***)&opP);

  TEST_ASSERT_EQUAL_ATTRIBUTE_OPERATOR(PREFIX, LEFT_TO_RIGHT, 13, "(", op);
}
/* Given                            should become
 *            OperatorToken *                 OperatorToken *
 *            --------------                  --------------
 * type:      TOKEN_OPERATOR_TYPE             TOKEN_OPERATOR_TYPE
 * symbol:       '!'                          '!'
 * arity:         0                           PREFIX
 * assoc:         0                           RIGHT_TO_LEFT
 * precedence:    0                           12
 *
 */
void test_tryConvertToPrefix_given_logicalNOT_should_convert_to_prefix_successfully(void)
{
  OperatorToken *op = (OperatorToken*)createOperatorToken("!");

  OperatorToken **opP = malloc(sizeof(OperatorToken));
  opP = &op;
  tryConvertToPrefix((Token ***)&opP);

  TEST_ASSERT_EQUAL_ATTRIBUTE_OPERATOR(PREFIX, RIGHT_TO_LEFT, 12, "!", op);
}
Beispiel #3
0
int evaluateExpression(char *expression){
	Token *token;
	Stack *numberStack=createStack();
	Stack *operatorStack=createStack();

	if(expression ==NULL){
		Throw(ERR_NO_ARGUMENT);
	}
	Text *newText=textNew(expression);
	String *tokenizer = stringNew(newText);
	
	
	token=getToken(tokenizer);
	if(token->type == IDENTIFIER_TOKEN){
		Throw(ERR_NOT_ACCEPT_IDENTIFIER);
	}else{
	while(token!=NULL){
		if(isOperator(token)){
			if(((Operator*)token)->info->affix!=PREFIX)
			tryConvertToPrefix((Operator*)token);
		stackPush(token,operatorStack);
		}else if(isNumber(token)){
			stackPush(token,numberStack);
			break;
		}else
			Throw(ERR_INVALID_IDENTIFIER);
		token=getToken(tokenizer);
		if(token==NULL)
			break;
	}
		
	}
	
	while((token=getToken(tokenizer))!=NULL ){
		evaluatePostfixesPrefixesAndInfixes(expression,token,numberStack,operatorStack);
	}
	evaluateAllOperatorOnStack(numberStack,operatorStack);
	Number *result=(Number*)stackPop(numberStack);
	destroyStack(numberStack);
	if(operatorStack !=NULL){
		destroyStack(operatorStack);
	}
	return result->value;
	
}
/* Given                            should catch the error.
 *            OperatorToken *
 *            --------------
 * type:      TOKEN_OPERATOR_TYPE
 * symbol:       '+++'
 * arity:         0
 * assoc:         0
 * precedence:    0
 *
 */
void test_tryConvertToPrefix_given_plus_plus_plus_should_fail_to_convert_to_prefix(void)
{
  OperatorToken *op = (OperatorToken*)createOperatorToken("+++");
  Attributes *attr = &operatorAttributesTable[(int)*(op->symbol)];
  OperatorToken **opP = malloc(sizeof(OperatorToken));
  opP = &op;
  ErrorObject *err;
  Try
  {
    tryConvertToPrefix((Token ***)&opP);
    TEST_FAIL_MESSAGE("Expected to catch Error here, but didn't.\n");
  }
  Catch(err)
  {
    TEST_ASSERT_EQUAL_STRING("Hey! This symbol is not belong to prefix type.", \
                               err->errorMsg);
    TEST_ASSERT_EQUAL(FAIL_TO_CONVERT_TO_PREFIX, err->errorCode);

    freeError(err);
  }
}
Beispiel #5
0
void evaluatePrefixesAndNumber(char *expression,Token *token,Stack *numberStack,Stack *operatorStack){
	Text *newText=textNew(expression);
	String *tokenizer = stringNew(newText);
	
	if(token==NULL){
		Throw(ERR_EXPECTING_NUMBER);
	}else {
		while(1){
			if(isNumber(token)){
				stackPush(token,numberStack);
				break;
			}else if(isOperator(token)){
				if(((Operator*)token)->info->affix !=PREFIX)
					tryConvertToPrefix((Operator*)token);
				stackPush(token,operatorStack);
			}else
				Throw(ERR_INVALID_IDENTIFIER);
			token=getToken(tokenizer);
			if(token==NULL)
				break;
		}
	}
	
}