File tree Expand file tree Collapse file tree
Behavioral/InterpreterPattern/src/io/csie/chris Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package io .csie .chris ;
2+
3+ import io .csie .chris .expression .Expression ;
4+ import io .csie .chris .expression .Minus ;
5+ import io .csie .chris .expression .Plus ;
6+ import io .csie .chris .expression .Variable ;
7+
8+ import java .util .Map ;
9+ import java .util .Stack ;
10+
11+ public class Evaluator implements Expression {
12+
13+ private Expression syntaxTree ;
14+
15+ public Evaluator (final String expression ) {
16+
17+ final Stack <Expression > expressionStack = new Stack <Expression >();
18+
19+ for (final String token : expression .split (" " )) {
20+ if (token .equals ("+" )) {
21+ final Expression subExpression = new Plus (expressionStack .pop (), expressionStack .pop ());
22+ expressionStack .push (subExpression );
23+ } else if (token .equals ("-" )) {
24+ // it's necessary remove first the right operand from the stack
25+ final Expression right = expressionStack .pop ();
26+ // ..and after the left one
27+ final Expression left = expressionStack .pop ();
28+ final Expression subExpression = new Minus (left , right );
29+ expressionStack .push (subExpression );
30+ } else
31+ expressionStack .push (new Variable (token ));
32+ }
33+ syntaxTree = expressionStack .pop ();
34+ }
35+
36+ public int interpret (final Map <String , Expression > context ) {
37+ return syntaxTree .interpret (context );
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ package io .csie .chris ;
2+
3+ import io .csie .chris .expression .Expression ;
4+ import io .csie .chris .expression .Number ;
5+
6+ import java .util .HashMap ;
7+ import java .util .Map ;
8+
9+ public class Main {
10+
11+ public static void main (final String [] args ) {
12+
13+ final String expression = "w x z - +" ;
14+
15+ final Evaluator sentence = new Evaluator (expression );
16+
17+ final Map <String , Expression > variables = new HashMap <>();
18+
19+ variables .put ("w" , new Number (5 ));
20+ variables .put ("x" , new Number (10 ));
21+ variables .put ("z" , new Number (42 ));
22+
23+ final int result = sentence .interpret (variables );
24+
25+ System .out .println (result );
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ package io .csie .chris .expression ;
2+
3+ import java .util .Map ;
4+
5+ public interface Expression {
6+
7+ int interpret (final Map <String , Expression > variables );
8+ }
Original file line number Diff line number Diff line change 1+ package io .csie .chris .expression ;
2+
3+ import java .util .Map ;
4+
5+ public class Minus implements Expression {
6+
7+ private Expression leftOperand ;
8+
9+ private Expression rightOperand ;
10+
11+ public Minus (final Expression left , final Expression right ) {
12+ leftOperand = left ;
13+ rightOperand = right ;
14+ }
15+
16+ public int interpret (final Map <String , Expression > variables ) {
17+ return leftOperand .interpret (variables ) - rightOperand .interpret (variables );
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package io .csie .chris .expression ;
2+
3+ import java .util .Map ;
4+
5+ public class Number implements Expression {
6+
7+ private int number ;
8+
9+ public Number (final int number ) {
10+ this .number = number ;
11+ }
12+
13+ public int interpret (final Map <String , Expression > variables ) {
14+ return number ;
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ package io .csie .chris .expression ;
2+
3+ import java .util .Map ;
4+
5+ public class Plus implements Expression {
6+
7+ private Expression leftOperand ;
8+
9+ private Expression rightOperand ;
10+
11+ public Plus (final Expression left , final Expression right ) {
12+ leftOperand = left ;
13+ rightOperand = right ;
14+ }
15+
16+ public int interpret (final Map <String , Expression > variables ) {
17+ return leftOperand .interpret (variables ) + rightOperand .interpret (variables );
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package io .csie .chris .expression ;
2+
3+ import java .util .Map ;
4+
5+ public class Variable implements Expression {
6+
7+ private String name ;
8+
9+ public Variable (final String name ) {
10+ this .name = name ;
11+ }
12+
13+ public int interpret (final Map <String , Expression > variables ) {
14+ if (null == variables .get (name ))
15+ return 0 ; // Either return new Number(0).
16+
17+ return variables .get (name ).interpret (variables );
18+ }
19+ }
You can’t perform that action at this time.
0 commit comments