27 lines
574 B
C
27 lines
574 B
C
/*
|
|
* Copyright (c) Camden Dixie O'Brien
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
#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;
|
|
}
|
|
}
|