-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.c
26 lines (25 loc) · 883 Bytes
/
evaluator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "evaluator.h"
#include "operand/operand_stack.h"
#include "my_libc/_atoi.h"
/***
* @param tokens: A data structure holding the tokens in reverse-polish notation order
* (see https://en.wikipedia.org/wiki/Reverse_Polish_notation)
* @return The value of the expression represented by the input parameter
*/
int eval(TokenQueue* tokens, int* error)
{
OperandStack* operands = new_operand_stack();
int result;
for (Token* token = tokens->dequeue(tokens); token; token = tokens->dequeue(tokens)) {
if (token->isOperator(token)) {
result = token->applyAsOperator(token, operands, error);
} else {
result = _atoi(token->value);
}
operands->push(operands, result);
token->delete(token);
}
result = operands->pop(operands);
operands->delete(operands);
return result;
}