Skip to content

Commit 10e5671

Browse files
authored
Merge pull request JamesZBL#6 from JamesZBL/develop
Interpreter 模式
2 parents 1250d2d + 4ec82bf commit 10e5671

File tree

10 files changed

+437
-1
lines changed

10 files changed

+437
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
* Command (命令模式)
5858

59-
* Interpreter (解释模式
59+
* Interpreter (解释器模式
6060

6161
* Iterator(迭代器模式)
6262

interpreter/pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
MIT License
5+
6+
Copyright (c) 2017 James
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
26+
-->
27+
<project xmlns="http://maven.apache.org/POM/4.0.0"
28+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
29+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
30+
<parent>
31+
<artifactId>java_design_patterns</artifactId>
32+
<groupId>com.github.JamesZBL</groupId>
33+
<version>1.11.9-SNAPSHOT</version>
34+
</parent>
35+
<modelVersion>4.0.0</modelVersion>
36+
37+
<artifactId>interpreter</artifactId>
38+
<dependencies>
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.mockito</groupId>
46+
<artifactId>mockito-core</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
</project>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.interpreter;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
import java.util.Stack;
30+
31+
/**
32+
* Interpreter
33+
*/
34+
public class Application {
35+
36+
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
37+
38+
public static void main(String[] args) {
39+
try {
40+
String tokenString = "5 3 2 1 + 2 * 3 /";
41+
Stack<Expression> stack = new Stack<>();
42+
43+
String[] stringList = tokenString.split(" ");
44+
for (String s : stringList) {
45+
if (isOperator(s)) {
46+
Expression expressionRight = stack.pop();
47+
Expression expressionLeft = stack.pop();
48+
LOGGER.info("左操作数:{},右操作数:{}", expressionLeft.interpret(), expressionRight.interpret());
49+
Expression expression = getExpressionInstance(s, expressionLeft, expressionRight);
50+
LOGGER.info("操作符:{}", expression);
51+
Expression result;
52+
if (expression != null) {
53+
result = new NumberExpression(expression.interpret());
54+
LOGGER.info("运算结果为:{}", result.interpret());
55+
stack.push(result);
56+
}
57+
} else {
58+
NumberExpression expression = new NumberExpression(s);
59+
stack.push(expression);
60+
LOGGER.info("数字入栈:{}", expression.interpret());
61+
}
62+
}
63+
} catch (Exception e) {
64+
e.printStackTrace();
65+
}
66+
}
67+
68+
/**
69+
* 判断字符串是否为四则运算的操作符
70+
*
71+
* @param s 待判断的字符串
72+
*
73+
* @return 是否为操作符
74+
*/
75+
public static boolean isOperator(String s) {
76+
return s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/");
77+
}
78+
79+
/**
80+
* 根据字符串生成四则运算表达式
81+
*
82+
* @param s 字符串
83+
* @param expressionLeft 左表达式
84+
* @param expressionRight 右表达式
85+
*
86+
* @return 四则运算表达式
87+
*/
88+
public static Expression getExpressionInstance(String s, Expression expressionLeft, Expression expressionRight) {
89+
if (isOperator(s)) {
90+
switch (s) {
91+
case "+": {
92+
return new PlusExpression(expressionLeft, expressionRight);
93+
}
94+
case "-": {
95+
return new MinusExpression(expressionLeft, expressionRight);
96+
}
97+
case "*": {
98+
return new MultipleExpression(expressionLeft, expressionRight);
99+
}
100+
case "/": {
101+
return new DivisionExpression(expressionLeft, expressionRight);
102+
}
103+
}
104+
}
105+
return null;
106+
}
107+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.interpreter;
25+
26+
/**
27+
* 除法表达式
28+
*/
29+
public class DivisionExpression extends Expression {
30+
31+
private Expression expressionLeft;
32+
private Expression expressionRight;
33+
34+
public DivisionExpression(Expression expressionLeft, Expression expressionRight) {
35+
this.expressionLeft = expressionLeft;
36+
this.expressionRight = expressionRight;
37+
}
38+
39+
@Override
40+
public int interpret() throws Exception{
41+
return expressionLeft.interpret() / expressionRight.interpret();
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "/";
47+
}
48+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.interpreter;
25+
26+
/**
27+
* 表达式
28+
*/
29+
public abstract class Expression {
30+
31+
public abstract int interpret() throws Exception;
32+
33+
@Override
34+
public abstract String toString();
35+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.interpreter;
25+
26+
/**
27+
* 减法表达式
28+
*/
29+
public class MinusExpression extends Expression {
30+
31+
private Expression expressionLeft;
32+
private Expression expressionRight;
33+
34+
public MinusExpression(Expression expressionLeft, Expression expressionRight) {
35+
this.expressionLeft = expressionLeft;
36+
this.expressionRight = expressionRight;
37+
}
38+
39+
@Override
40+
public int interpret() throws Exception {
41+
return expressionLeft.interpret() - expressionRight.interpret();
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "-";
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.interpreter;
25+
26+
/**
27+
* 乘法表达式
28+
*/
29+
public class MultipleExpression extends Expression {
30+
31+
private Expression expressionLeft;
32+
private Expression expressionRight;
33+
34+
public MultipleExpression(Expression expressionLeft, Expression expressionRight) {
35+
this.expressionLeft = expressionLeft;
36+
this.expressionRight = expressionRight;
37+
}
38+
39+
@Override
40+
public int interpret() throws Exception {
41+
return expressionLeft.interpret() * expressionRight.interpret();
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "*";
47+
}
48+
}

0 commit comments

Comments
 (0)