added interpreter sample
This commit is contained in:
		
							
								
								
									
										23
									
								
								interpreter/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								interpreter/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | <?xml version="1.0"?> | ||||||
|  | <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" | ||||||
|  |     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||||||
|  |   <modelVersion>4.0.0</modelVersion> | ||||||
|  |   <parent> | ||||||
|  |     <groupId>com.iluwatar</groupId> | ||||||
|  |     <artifactId>java-design-patterns</artifactId> | ||||||
|  |     <version>1.0-SNAPSHOT</version> | ||||||
|  |   </parent> | ||||||
|  |   <groupId>com.iluwatar</groupId> | ||||||
|  |   <artifactId>interpreter</artifactId> | ||||||
|  |   <version>1.0-SNAPSHOT</version> | ||||||
|  |   <name>interpreter</name> | ||||||
|  |   <url>http://maven.apache.org</url> | ||||||
|  |   <dependencies> | ||||||
|  |     <dependency> | ||||||
|  |       <groupId>junit</groupId> | ||||||
|  |       <artifactId>junit</artifactId> | ||||||
|  |       <version>3.8.1</version> | ||||||
|  |       <scope>test</scope> | ||||||
|  |     </dependency> | ||||||
|  |   </dependencies> | ||||||
|  | </project> | ||||||
							
								
								
									
										61
									
								
								interpreter/src/main/java/com/iluwatar/App.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								interpreter/src/main/java/com/iluwatar/App.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,61 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | import java.util.Stack; | ||||||
|  |  | ||||||
|  | public class App  | ||||||
|  | { | ||||||
|  | 	 | ||||||
|  | 	/** | ||||||
|  | 	 *  | ||||||
|  | 	 * Expressions can be evaluated using prefix, infix or postfix notations | ||||||
|  | 	 * This sample uses postfix, where operator comes after the operands | ||||||
|  | 	 *  | ||||||
|  | 	 */ | ||||||
|  |     public static void main( String[] args ) | ||||||
|  |     { | ||||||
|  |     	String tokenString = "4 3 2 - 1 + *"; | ||||||
|  | 		Stack<Expression> stack = new Stack<>(); | ||||||
|  |  | ||||||
|  | 		String[] tokenList = tokenString.split(" "); | ||||||
|  | 		for (String s : tokenList) { | ||||||
|  | 			if (isOperator(s)) { | ||||||
|  | 				Expression rightExpression = stack.pop(); | ||||||
|  | 				Expression leftExpression = stack.pop(); | ||||||
|  | 				System.out.println(String.format("popped from stack left: %d right: %d",  | ||||||
|  | 						leftExpression.interpret(), rightExpression.interpret())); | ||||||
|  | 				Expression operator = getOperatorInstance(s, leftExpression, | ||||||
|  | 						rightExpression); | ||||||
|  | 				System.out.println(String.format("operator: %s", operator)); | ||||||
|  | 				int result = operator.interpret(); | ||||||
|  | 				NumberExpression resultExpression = new NumberExpression(result); | ||||||
|  | 				stack.push(resultExpression); | ||||||
|  | 				System.out.println(String.format("push result to stack: %d", resultExpression.interpret())); | ||||||
|  | 			} else { | ||||||
|  | 				Expression i = new NumberExpression(s); | ||||||
|  | 				stack.push(i); | ||||||
|  | 				System.out.println(String.format("push to stack: %d", i.interpret())); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		System.out.println(String.format("result: %d", stack.pop().interpret()));    	 | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     public static boolean isOperator(String s) { | ||||||
|  | 		if (s.equals("+") || s.equals("-") || s.equals("*")) | ||||||
|  | 			return true; | ||||||
|  | 		else | ||||||
|  | 			return false; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	public static Expression getOperatorInstance(String s, Expression left, | ||||||
|  | 			Expression right) { | ||||||
|  | 		switch (s) { | ||||||
|  | 		case "+": | ||||||
|  | 			return new PlusExpression(left, right); | ||||||
|  | 		case "-": | ||||||
|  | 			return new MinusExpression(left, right); | ||||||
|  | 		case "*": | ||||||
|  | 			return new MultiplyExpression(left, right); | ||||||
|  | 		} | ||||||
|  | 		return null; | ||||||
|  | 	}	 | ||||||
|  | } | ||||||
							
								
								
									
										9
									
								
								interpreter/src/main/java/com/iluwatar/Expression.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								interpreter/src/main/java/com/iluwatar/Expression.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | public abstract class Expression { | ||||||
|  |  | ||||||
|  | 	public abstract int interpret(); | ||||||
|  | 	 | ||||||
|  | 	@Override | ||||||
|  | 	public abstract String toString(); | ||||||
|  | } | ||||||
							
								
								
									
										23
									
								
								interpreter/src/main/java/com/iluwatar/MinusExpression.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								interpreter/src/main/java/com/iluwatar/MinusExpression.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | public class MinusExpression extends Expression { | ||||||
|  |  | ||||||
|  | 	private Expression leftExpression; | ||||||
|  | 	private Expression rightExpression; | ||||||
|  |  | ||||||
|  | 	public MinusExpression(Expression leftExpression, Expression rightExpression) { | ||||||
|  | 		this.leftExpression = leftExpression; | ||||||
|  | 		this.rightExpression = rightExpression; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	@Override | ||||||
|  | 	public int interpret() { | ||||||
|  | 		return leftExpression.interpret() - rightExpression.interpret(); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	public String toString() { | ||||||
|  | 		return "-"; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -0,0 +1,23 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | public class MultiplyExpression extends Expression { | ||||||
|  |  | ||||||
|  | 	private Expression leftExpression; | ||||||
|  | 	private Expression rightExpression; | ||||||
|  |  | ||||||
|  | 	public MultiplyExpression(Expression leftExpression, Expression rightExpression) { | ||||||
|  | 		this.leftExpression = leftExpression; | ||||||
|  | 		this.rightExpression = rightExpression; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	@Override | ||||||
|  | 	public int interpret() { | ||||||
|  | 		return leftExpression.interpret() * rightExpression.interpret(); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	public String toString() { | ||||||
|  | 		return "*"; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
							
								
								
									
										25
									
								
								interpreter/src/main/java/com/iluwatar/NumberExpression.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								interpreter/src/main/java/com/iluwatar/NumberExpression.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | public class NumberExpression extends Expression { | ||||||
|  |  | ||||||
|  | 	private int number; | ||||||
|  |  | ||||||
|  | 	public NumberExpression(int number) { | ||||||
|  | 		this.number = number; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	public NumberExpression(String s) { | ||||||
|  | 		this.number = Integer.parseInt(s); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	@Override | ||||||
|  | 	public int interpret() { | ||||||
|  | 		return number; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	public String toString() { | ||||||
|  | 		return "number"; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
							
								
								
									
										23
									
								
								interpreter/src/main/java/com/iluwatar/PlusExpression.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								interpreter/src/main/java/com/iluwatar/PlusExpression.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | package com.iluwatar; | ||||||
|  |  | ||||||
|  | public class PlusExpression extends Expression { | ||||||
|  |  | ||||||
|  | 	private Expression leftExpression; | ||||||
|  | 	private Expression rightExpression; | ||||||
|  |  | ||||||
|  | 	public PlusExpression(Expression leftExpression, Expression rightExpression) { | ||||||
|  | 		this.leftExpression = leftExpression; | ||||||
|  | 		this.rightExpression = rightExpression; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	@Override | ||||||
|  | 	public int interpret() { | ||||||
|  | 		return leftExpression.interpret() + rightExpression.interpret(); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@Override | ||||||
|  | 	public String toString() { | ||||||
|  | 		return "+"; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user