0
|
1 #include <iostream>
|
|
2 #include "node.h"
|
|
3
|
|
4 int cnode::expr(std::map<std::string, int> &values) const
|
|
5 {
|
|
6 switch (op_) {
|
|
7 case OP_NEG:
|
|
8 return -left_->expr(values);
|
|
9
|
|
10 case OP_PLUS:
|
|
11 return left_->expr(values) + right_->expr(values);
|
|
12
|
|
13 case OP_MINUS:
|
|
14 return left_->expr(values) - right_->expr(values);
|
|
15
|
|
16 case OP_TIMES:
|
|
17 return left_->expr(values) * right_->expr(values);
|
|
18
|
|
19 case OP_DIVIDE:
|
|
20 return left_->expr(values) / right_->expr(values);
|
|
21
|
|
22 case OP_NUMBER:
|
|
23 return number_;
|
|
24
|
|
25 case OP_IDENT:
|
|
26 return values[string_];
|
|
27
|
|
28 default:
|
|
29 return 0; // error
|
|
30 }
|
|
31 }
|