Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

* Command (命令模式)

* Interpreter (解释模式
* Interpreter (解释器模式

* Iterator(迭代器模式)

Expand Down
51 changes: 51 additions & 0 deletions interpreter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

MIT License

Copyright (c) 2017 James

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java_design_patterns</artifactId>
<groupId>com.github.JamesZBL</groupId>
<version>1.11.9-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>interpreter</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
107 changes: 107 additions & 0 deletions interpreter/src/main/java/me/zbl/interpreter/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.interpreter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Stack;

/**
* Interpreter
*/
public class Application {

private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) {
try {
String tokenString = "5 3 2 1 + 2 * 3 /";
Stack<Expression> stack = new Stack<>();

String[] stringList = tokenString.split(" ");
for (String s : stringList) {
if (isOperator(s)) {
Expression expressionRight = stack.pop();
Expression expressionLeft = stack.pop();
LOGGER.info("左操作数:{},右操作数:{}", expressionLeft.interpret(), expressionRight.interpret());
Expression expression = getExpressionInstance(s, expressionLeft, expressionRight);
LOGGER.info("操作符:{}", expression);
Expression result;
if (expression != null) {
result = new NumberExpression(expression.interpret());
LOGGER.info("运算结果为:{}", result.interpret());
stack.push(result);
}
} else {
NumberExpression expression = new NumberExpression(s);
stack.push(expression);
LOGGER.info("数字入栈:{}", expression.interpret());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 判断字符串是否为四则运算的操作符
*
* @param s 待判断的字符串
*
* @return 是否为操作符
*/
public static boolean isOperator(String s) {
return s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/");
}

/**
* 根据字符串生成四则运算表达式
*
* @param s 字符串
* @param expressionLeft 左表达式
* @param expressionRight 右表达式
*
* @return 四则运算表达式
*/
public static Expression getExpressionInstance(String s, Expression expressionLeft, Expression expressionRight) {
if (isOperator(s)) {
switch (s) {
case "+": {
return new PlusExpression(expressionLeft, expressionRight);
}
case "-": {
return new MinusExpression(expressionLeft, expressionRight);
}
case "*": {
return new MultipleExpression(expressionLeft, expressionRight);
}
case "/": {
return new DivisionExpression(expressionLeft, expressionRight);
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.interpreter;

/**
* 除法表达式
*/
public class DivisionExpression extends Expression {

private Expression expressionLeft;
private Expression expressionRight;

public DivisionExpression(Expression expressionLeft, Expression expressionRight) {
this.expressionLeft = expressionLeft;
this.expressionRight = expressionRight;
}

@Override
public int interpret() throws Exception{
return expressionLeft.interpret() / expressionRight.interpret();
}

@Override
public String toString() {
return "/";
}
}
35 changes: 35 additions & 0 deletions interpreter/src/main/java/me/zbl/interpreter/Expression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.interpreter;

/**
* 表达式
*/
public abstract class Expression {

public abstract int interpret() throws Exception;

@Override
public abstract String toString();
}
48 changes: 48 additions & 0 deletions interpreter/src/main/java/me/zbl/interpreter/MinusExpression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.interpreter;

/**
* 减法表达式
*/
public class MinusExpression extends Expression {

private Expression expressionLeft;
private Expression expressionRight;

public MinusExpression(Expression expressionLeft, Expression expressionRight) {
this.expressionLeft = expressionLeft;
this.expressionRight = expressionRight;
}

@Override
public int interpret() throws Exception {
return expressionLeft.interpret() - expressionRight.interpret();
}

@Override
public String toString() {
return "-";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.interpreter;

/**
* 乘法表达式
*/
public class MultipleExpression extends Expression {

private Expression expressionLeft;
private Expression expressionRight;

public MultipleExpression(Expression expressionLeft, Expression expressionRight) {
this.expressionLeft = expressionLeft;
this.expressionRight = expressionRight;
}

@Override
public int interpret() throws Exception {
return expressionLeft.interpret() * expressionRight.interpret();
}

@Override
public String toString() {
return "*";
}
}
Loading