infix-calculator/lib/evaluator.c

22 lines
486 B
C

#include "evaluator.h"
int evaluate(const expression_t *expression)
{
if (expression->is_number)
return expression->number;
const int x = evaluate(expression->application.operands[0]);
const int y = evaluate(expression->application.operands[1]);
switch (expression->application.operator) {
case OPERATOR_ADD:
return x + y;
case OPERATOR_SUBTRACT:
return x - y;
case OPERATOR_MULTIPLY:
return x * y;
case OPERATOR_DIVIDE:
return x / y;
default:
return 0xdead;
}
}