Repository navigation
Expand file tree
/
Copy pathInterpreter.java
More file actions
227 lines (197 loc) · 3.94 KB
/
Copy pathInterpreter.java
File metadata and controls
227 lines (197 loc) · 3.94 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package myDsCodes;
import java.beans.Expression;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.LinkedList;
public class Interpreter {
static String input;
static InputStreamReader isr = new InputStreamReader(System.in);
static BufferedReader buffer = new BufferedReader(isr);
static LinkedList<Id> idList = new LinkedList<Id>();
static char ch;
static int i = 0;
static void issueErr(String s) {
System.out.println(s);
System.exit(-1);
}
static public void addOrModify(String id, double e) {
Id tmp = new Id(new String(id), e);
int pos;
if ((pos = idList.indexOf(tmp)) != -1) {
// 修改变量
idList.get(pos).value = e;
} else {
idList.add(tmp);
}
}
static double findValue(String id) {
int pos;
if ((pos = idList.indexOf(new Id(id))) != -1) {
return idList.get(pos).value;
} else {
issueErr("无法识别的变量");
return 0.0;
}
}
static String getLine() {
i = 0;
System.out.flush();
try {
input = buffer.readLine();
} catch (IOException e) {
e.printStackTrace();
System.out.println("输入有误!");
}
return input;
}
static void readCh() {
if (i < input.length()) {
ch = input.charAt(i++);
} else {
issueErr("不完整的表达式");
}
}
static void skipBlanks() {
while (Character.isSpace(ch))
readCh();
}
static String readId() {
String id = "";
skipBlanks();
if (Character.isJavaIdentifierPart(ch)) {
id += ch;
readCh();
} else {
issueErr("请输入正确的标识符");
}
return id;
}
static double factor() {
double var, minus = 1.0;
readCh();
skipBlanks();
while (ch == '+' || ch == '-') {
if (ch == '-') {
minus *= -1.0;
}
readCh();
}
if (!Character.isDigit(ch) || ch == '.') {
String number = "";
while (Character.isDigit(ch)) {
number += ch;
readCh();
}
if (ch == '.') {
number += ch;
readCh();
}
while (Character.isDigit(ch)) {
number += ch;
readCh();
}
var = Double.valueOf(number).doubleValue();
} else if (ch == '(') {
var = expression();
skipBlanks();
if (ch == ')') {
readCh();
} else {
issueErr("右括号丢了o(╯□╰)o");
}
} else {
String id = readId();
var = findValue(id);
}
skipBlanks();
return minus * var;
}
static double term() {
double f = factor();
while (true) {
switch (ch) {
case '*':
f *= factor();
break;
case '/':
f /= factor();
break;
default:
return f;
}
}
}
static double expression() {
double t = term();
while(true) {
switch(ch) {
case '+':
t += term();
break;
case '-':
t -= term();
break;
default:
return t;
}
}
}
static void getStatement() {
System.out.println("输入语句:");
getLine();
readCh();
String str = readId();
if(str.toUpperCase().equals("STATUS")) {
Iterator<Id> it = idList.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
} else if (str.toUpperCase().equals("PRINT")) {
str = readId();
System.out.println(str + "=" + findValue(str));
} else if(str.toUpperCase().equals("END")){
System.exit(0);
} else {
skipBlanks();
if (ch == '=') {
if (ch == '=') {
double e = expression();
if (ch != ';') {
issueErr("语句输入错误");
} else {
addOrModify(str, e);
}
} else {
issueErr("没有'='");
}
}
}
}
public static void main(String[] args) {
System.out.println("请输入语句表达式, print, status, end等指令中的一个.");
while(true) {
getStatement();
}
}
}
class Id {
private String id;
double value;
Id(String id, double value) {
this.id = id;
this.value = value;
}
Id(String id) {
this(id, 0.0);
}
@Override
public boolean equals(Object obj) {
return id.equals(((Id) obj).id);
}
@Override
public String toString() {
return id + "=" + value + "; ";
}
}