/
SimpleScript.java
167 lines (149 loc) · 5.27 KB
/
SimpleScript.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
/**
* 一个简单的脚本解释器。
* 所支持的语法,请参见SimpleParser.java
*
* 运行脚本:
* 在命令行下,键入:java SimpleScript
* 则进入一个REPL界面。你可以依次敲入命令。比如:
* > 2+3;
* > int age = 10;
* > int b;
* > b = 10*2;
* > age = age + b;
* > exit(); //退出REPL界面。
*
* 你还可以使用一个参数 -v,让每次执行脚本的时候,都输出AST和整个计算过程。
*
*/
public class SimpleScript {
private HashMap<String, Integer> variables = new HashMap<String, Integer>();
private static boolean verbose = false;
/**
* 实现一个简单的REPL
* @param args
*/
public static void main(String[] args) {
if (args.length > 0 && args[0].equals("-v")) {
verbose = true;
System.out.println("verbose mode");
}
System.out.println("Simple script language!");
SimpleParser parser = new SimpleParser();
SimpleScript script = new SimpleScript();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String scriptText = "";
System.out.print("\n>"); //提示符
while (true) {
try {
String line = reader.readLine().trim();
if (line.equals("exit();")) {
System.out.println("good bye!");
break;
}
scriptText += line + "\n";
if (line.endsWith(";")) {
ASTNode tree = parser.parse(scriptText);
if (verbose) {
parser.dumpAST(tree, "");
}
script.evaluate(tree, "");
System.out.print("\n>"); //提示符
scriptText = "";
}
} catch (Exception e) {
// e.printStackTrace();
System.out.println(e.getLocalizedMessage());
System.out.print("\n>"); //提示符
scriptText = "";
}
}
}
/**
* 遍历AST,计算值。
* @param node
* @param indent
* @return
* @throws Exception
*/
private Integer evaluate(ASTNode node, String indent) throws Exception {
Integer result = null;
if (verbose) {
System.out.println(indent + "Calculating: " + node.getType());
}
switch (node.getType()) {
case Programm:
for (ASTNode child : node.getChildren()) {
result = evaluate(child, indent);
}
break;
case Additive:
ASTNode child1 = node.getChildren().get(0);
Integer value1 = evaluate(child1, indent + "\t");
ASTNode child2 = node.getChildren().get(1);
Integer value2 = evaluate(child2, indent + "\t");
if (node.getText().equals("+")) {
result = value1 + value2;
} else {
result = value1 - value2;
}
break;
case Multiplicative:
child1 = node.getChildren().get(0);
value1 = evaluate(child1, indent + "\t");
child2 = node.getChildren().get(1);
value2 = evaluate(child2, indent + "\t");
if (node.getText().equals("*")) {
result = value1 * value2;
} else {
result = value1 / value2;
}
break;
case IntLiteral:
result = Integer.valueOf(node.getText()).intValue();
break;
case Identifier:
String varName = node.getText();
if (variables.containsKey(varName)) {
Integer value = variables.get(varName);
if (value != null) {
result = value.intValue();
} else {
throw new Exception("variable " + varName + " has not been set any value");
}
}
else{
throw new Exception("unknown variable: " + varName);
}
break;
case AssignmentStmt:
varName = node.getText();
if (!variables.containsKey(varName)){
throw new Exception("unknown variable: " + varName);
} //接着执行下面的代码
case IntDeclaration:
varName = node.getText();
Integer varValue = null;
if (node.getChildren().size() > 0) {
ASTNode child = node.getChildren().get(0);
result = evaluate(child, indent + "\t");
varValue = Integer.valueOf(result);
}
variables.put(varName, varValue);
break;
default:
}
if (verbose) {
System.out.println(indent + "Result: " + result);
} else if (indent.equals("")) { // 顶层的语句
if (node.getType() == ASTNodeType.IntDeclaration || node.getType() == ASTNodeType.AssignmentStmt) {
System.out.println(node.getText() + ": " + result);
}else if (node.getType() != ASTNodeType.Programm){
System.out.println(result);
}
}
return result;
}
}