Repository navigation
Expand file tree
/
Copy pathInterpreter.java
More file actions
146 lines (144 loc) · 5.39 KB
/
Copy pathInterpreter.java
File metadata and controls
146 lines (144 loc) · 5.39 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
import java.io.*;
class Id {
private String id;
public double value;
public Id(String s, double d) {
id = s; value = d;
}
public boolean equals(Object node) {
return id.equals(((Id)node).id);
}
public String toString() {
return id + " = " + value + "; ";
}
}
public class Interpreter {
private StreamTokenizer fIn = new StreamTokenizer(
new BufferedReader(
new InputStreamReader(System.in)));
private java.util.LinkedList idList = new java.util.LinkedList();
public Interpreter() {
fIn.wordChars('$','$');// include underscores and dollar signs as
fIn.wordChars('_','_');// word constituents; examples of identifiers:
// var1, x, _pqr123xyz, $aName;
fIn.ordinaryChar('/'); // by default, '/' is a comment character;
fIn.ordinaryChar('.'); // otherwise "n-123.45"
fIn.ordinaryChar('-'); // is considered a token;
}
private void issueError(String s) {
System.out.println(s);
Runtime.getRuntime().exit(-1);
}
private void addOrModify(String id, double e) {
Id tmp = new Id(new String(id),e);
int pos;
if ((pos = idList.indexOf(tmp)) != -1)
((Id)idList.get(pos)).value = e;
else idList.add(tmp);
}
private double findValue(String id) {
int pos;
if ((pos = idList.indexOf(new Id(id,0.0))) != -1)
return ((Id)idList.get(pos)).value;
else issueError("Unknown variable " + id);
return 0.0; // this statement is never reached;
}
private double factor() throws IOException {
double val, minus = 1.0;
fIn.nextToken();
while (fIn.ttype == '+' || fIn.ttype == '-') { // take all '+'s
if (fIn.ttype == '-') // and '-'s;
minus *= -1.0;
fIn.nextToken();
}
if (fIn.ttype == fIn.TT_NUMBER || fIn.ttype == '.') {
if (fIn.ttype == fIn.TT_NUMBER) { // factor can be a number:
val = fIn.nval; // 123, .123, 123., 12.3;
fIn.nextToken();
}
else val = 0;
if (fIn.ttype == '.') {
fIn.nextToken();
if (fIn.ttype == fIn.TT_NUMBER) {
String s = fIn.nval + "";
s = "." + s.substring(0,s.indexOf('.'));
val += Double.valueOf(s).doubleValue();
}
else fIn.pushBack();
}
else fIn.pushBack();
}
else if (fIn.ttype == '(') { // or a parenthesized
val = expression(); // expression,
if (fIn.ttype == ')')
fIn.nextToken();
else issueError("Right parenthesis is left out.");
}
else {
val = findValue(fIn.sval); // or an identifier;
}
return minus*val;
}
private double term() throws IOException {
double f = factor();
while (true) {
fIn.nextToken();
switch (fIn.ttype) {
case '*' : f *= factor(); break;
case '/' : f /= factor(); break;
default : fIn.pushBack(); return f;
}
}
}
private double expression() throws IOException {
double t = term();
while (true) {
fIn.nextToken();
switch (fIn.ttype) {
case '+' : t += term(); break;
case '-' : t -= term(); break;
default : fIn.pushBack(); return t;
}
}
}
public void run() {
try {
System.out.println("The program processes statements in the "
+ "following format:\n"
+ "\t<id> = <expr>;\n\tprint <id>\n\tstatus\n\tend");
while (true) {
System.out.print("Enter a statement: ");
fIn.nextToken();
String str = fIn.sval;
if (str.toUpperCase().equals("STATUS")) {
java.util.Iterator it = idList.iterator();
while (it.hasNext())
System.out.println(it.next());
}
else if (str.toUpperCase().equals("PRINT")) {
fIn.nextToken();
str = fIn.sval;
System.out.println(str + " = " + findValue(str));
}
else if (str.toUpperCase().equals("END"))
return;
else {
fIn.nextToken();
if (fIn.ttype == '=') {
double e = expression();
fIn.nextToken();
if (fIn.ttype != ';')
issueError("There are some extras in the statement.");
else addOrModify(str,e);
}
else issueError("'=' is missing.");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
(new Interpreter()).run();
}
}