-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSint.java
More file actions
439 lines (393 loc) · 13.3 KB
/
Sint.java
File metadata and controls
439 lines (393 loc) · 13.3 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Sint.java
// Interpreter for S
import java.util.Iterator;
import java.util.Scanner;
public class Sint {
static Scanner sc = new Scanner(System.in);
static State state = new State();
State Eval(Command c, State state) {
if (c instanceof Decl) {
Decls decls = new Decls();
decls.add((Decl) c);
return allocate(decls, state);
}
if (c instanceof Function) {
Function f = (Function) c;
state.push(f.id, new Value(f));
return state;
}
if (c instanceof Stmt)
return Eval((Stmt) c, state);
throw new IllegalArgumentException("no command");
}
State Eval(Stmt s, State state) {
if (s instanceof Empty)
return Eval((Empty)s, state);
if (s instanceof Assignment)
return Eval((Assignment)s, state);
if (s instanceof If)
return Eval((If)s, state);
if (s instanceof While)
return Eval((While)s, state);
if (s instanceof Stmts)
return Eval((Stmts)s, state);
if (s instanceof Let)
return Eval((Let)s, state);
if (s instanceof Read)
return Eval((Read)s, state);
if (s instanceof Print)
return Eval((Print)s, state);
if (s instanceof Call) {
//Call c = (Call) s;
//Value v = state.get(c.fid);
//if(v.funValue().type == Type.VOID)
return Eval((Call)s, state);
//else
//return V((Call)s, state);
}
if (s instanceof Return)
return Eval((Return)s, state);
throw new IllegalArgumentException("no statement");
}
// call without return value
State Eval(Call c, State state) {
//
// evaluate call without return value
Value v = state.get(c.fid); // find function
Function f = v.funValue();
State s = newFrame(state, c, f); // create new frame on the stack
s = Eval(f.stmt, s); // interpret the call
s = deleteFrame(s, c, f); // delete the frame on the stack
return s;
}
// value-returning call
Value V (Call c, State state) {
Value v = state.get(c.fid); // find function
Function f = v.funValue();
State s = newFrame(state, c, f); // create new frame on the stack
s = Eval(f.stmt, s); // interpret the call
v = s.pop().val; // remove the return value
s = deleteFrame(s, c, f); // delete the frame on the stack
return v;
}
State Eval(Return r, State state) {
Value v = V(r.expr, state);
return state.push(r.fid, v);
}
State newFrame (State state, Call c, Function f) {
if (c.args.size() == 0)
return state;
// evaluate arguments
// activate a new stack frame in the stack
Value val[] = new Value[f.params.size()];
int i = 0;
for(Expr e: c.args)
val[i++] = V(e,state);
allocate(f.params, state);
//인자값을 매개변수에 전달.................
for(i=0;i<f.params.size();i++)
state.set(f.params.get(i).id,val[i]);
return state;
}
State deleteFrame (State state, Call c, Function f) {
//
// free a stack frame from the stack
//
free(f.params,state);
return state;
}
State Eval(Empty s, State state) {
return state;
}
State Eval(Assignment a, State state) {
//add array assignment
if(a.ar == null) {
Value v = V(a.expr, state);
return state.set(a.id, v);
}
else {
Value v = V(a.expr, state);
Identifier i = a.ar.id;
Value n = V(a.ar.expr,state);
(state.get(i).arrValue())[n.intValue()] = v;
return state;
}
}
State Eval(Read r, State state) {
Value v = state.get(r.id);
if (v.type == Type.INT) {
int i = sc.nextInt();
state.set(r.id, new Value(i));
}
if (v.type == Type.BOOL) {
boolean b = sc.nextBoolean();
state.set(r.id, new Value(b));
}
//
// input string
if(v.type == Type.STRING) {
String s = sc.next();
state.set(r.id, new Value(s));
}
return state;
}
State Eval(Print p, State state) {
System.out.println(V(p.expr, state));
return state;
}
State Eval(Stmts ss, State state) {
for (Stmt s : ss.stmts) {
state = Eval(s, state);
}
return state;
}
State Eval(If c, State state) {
if (V(c.expr, state).boolValue( ))
return Eval(c.stmt1, state);
else
return Eval(c.stmt2, state);
}
State Eval(While l, State state) {
if (V(l.expr, state).boolValue( ))
return Eval(l, Eval(l.stmt, state));
else
return state;
}
State Eval(Let l, State state) {
State s = allocate(l.decls, state);
s = Eval(l.stmts,s);
return free(l.decls, s);
}
State allocate(Decls ds, State state) {
// add entries for declared variables on the state
//
if (ds != null)
{
Iterator<Decl> it = ds.iterator();
while(it.hasNext()) {
Decl d = it.next();
if(d.expr != null && d.arraysize == 0)
state.push(d.id, (Value)d.expr);
else if(d.expr == null && d.arraysize == 0) {
if(d.type == Type.INT) state.push(d.id, new Value(0));
if(d.type == Type.BOOL) state.push(d.id, new Value(false));
if(d.type == Type.STRING) state.push(d.id, new Value(""));
}
else state.push(d.id, new Value(new Value[d.arraysize]));
}
return state;
}
return null;
}
State free (Decls ds, State state) {
// free the entries for declared variables from the state
//
if (ds != null) {
Iterator<Decl> it = ds.iterator();
while(it.hasNext()) {
state.pop();
it.next();
}
return state;
}
return null;
}
Value binaryOperation(Operator op, Value v1, Value v2) {
check(!v1.undef && !v2.undef,"reference to undef value");
switch (op.val) {
case "+":
return new Value(v1.intValue() + v2.intValue());
case "-":
return new Value(v1.intValue() - v2.intValue());
case "*":
return new Value(v1.intValue() * v2.intValue());
case "/":
return new Value(v1.intValue() / v2.intValue());
//
// relational operations
case "<":
if(v1.type == Type.INT && v2.type == Type.INT) {
return new Value(v1.intValue() < v2.intValue());
}
if(v1.type == Type.STRING && v2.type == Type.STRING) {
int num = v1.toString().compareTo(v2.toString());
if(num < 0)
return new Value(true);
else
return new Value(false);
}
else
throw new IllegalArgumentException("wrong operator type");
case "<=":
if(v1.type == Type.INT && v2.type == Type.INT) {
return new Value(v1.intValue() <= v2.intValue());
}
if(v1.type == Type.STRING && v2.type == Type.STRING) {
int num = v1.toString().compareTo(v2.toString());
if(num <= 0)
return new Value(true);
else
return new Value(false);
}
else
throw new IllegalArgumentException("wrong operator type");
case ">":
if(v1.type == Type.INT && v2.type == Type.INT) {
return new Value(v1.intValue() > v2.intValue());
}
if(v1.type == Type.STRING && v2.type == Type.STRING) {
int num = v1.toString().compareTo(v2.toString());
if(num > 0)
return new Value(true);
else
return new Value(false);
}
else
throw new IllegalArgumentException("wrong operator type");
case ">=":
if(v1.type == Type.INT && v2.type == Type.INT) {
return new Value(v1.intValue() >= v2.intValue());
}
if(v1.type == Type.STRING && v2.type == Type.STRING) {
int num = v1.toString().compareTo(v2.toString());
if(num >= 0)
return new Value(true);
else
return new Value(false);
}
else
throw new IllegalArgumentException("wrong operator type");
case "==":
if(v1.type == Type.INT && v2.type == Type.INT) {
return new Value(v1.intValue() == v2.intValue());
}
if(v1.type == Type.STRING && v2.type == Type.STRING) {
int num = v1.toString().compareTo(v2.toString());
if(num == 0)
return new Value(true);
else
return new Value(false);
}
else
throw new IllegalArgumentException("wrong operator type");
case "!=":
if(v1.type == Type.INT && v2.type == Type.INT) {
return new Value(v1.intValue() != v2.intValue());
}
if(v1.type == Type.STRING && v2.type == Type.STRING) {
int num = v1.toString().compareTo(v2.toString());
if(num != 0)
return new Value(true);
else
return new Value(false);
}
else
throw new IllegalArgumentException("wrong operator type");
//
// logical operations
case "&":
return new Value(v1.boolValue() && v2.boolValue());
case "|":
return new Value(v1.boolValue() || v2.boolValue());
default:
throw new IllegalArgumentException("no operation");
}
}
Value unaryOperation(Operator op, Value v) {
check( !v.undef, "reference to undef value");
switch (op.val) {
case "!":
return new Value(!v.boolValue( ));
case "-":
return new Value(-v.intValue( ));
default:
throw new IllegalArgumentException("no operation: " + op.val);
}
}
static void check(boolean test, String msg) {
if (test) return;
System.err.println(msg);
}
Value V(Expr e, State state) {
if (e instanceof Value)
return (Value) e;
if (e instanceof Identifier) {
Identifier v = (Identifier) e;
return (Value)(state.get(v));
}
if (e instanceof Binary) {
Binary b = (Binary) e;
Value v1 = V(b.expr1, state);
Value v2 = V(b.expr2, state);
return binaryOperation (b.op, v1, v2);
}
if (e instanceof Unary) {
Unary u = (Unary) e;
Value v = V(u.expr, state);
return unaryOperation(u.op, v);
}
if (e instanceof Call)
return V((Call)e, state);
if (e instanceof Array) {
Identifier i = ((Array) e).id;
Value n = V(((Array)e).expr,state);
return (Value)((state.get(i).arrValue())[n.intValue()]);
}
throw new IllegalArgumentException("no operation");
}
public static void main(String args[]) {
if (args.length == 0) {
Sint sint = new Sint(); Lexer.interactive = true;
System.out.println("Language S Interpreter 1.0");
System.out.print(">> ");
Parser parser = new Parser(new Lexer());
do { // Program = Command*
if (parser.token == Token.EOF)
parser.token = parser.lexer.getToken();
Command command=null;
try {
command = parser.command();
//command.type = TypeChecker.Check(command);
} catch (Exception e) {
System.out.println(e);
System.out.print(">> ");
continue;
}
if (command.type != Type.ERROR) {
System.out.println("\nInterpreting..." );
try {
state = sint.Eval(command, state);
} catch (Exception e) {
System.err.println(e);
}
}
System.out.print(">> ");
} while (true);
}
else {
System.out.println("Begin parsing... " + args[0]);
Command command = null;
Parser parser = new Parser(new Lexer(args[0]));
Sint sint = new Sint();
do { // Program = Command*
if (parser.token == Token.EOF)
break;
try {
command = parser.command();
// command.type = TypeChecker.Check(command);
} catch (Exception e) {
System.out.println(e);
continue;
}
if (command.type!=Type.ERROR) {
System.out.println("\nInterpreting..." + args[0]);
try {
state = sint.Eval(command, state);
} catch (Exception e) {
System.err.println(e);
}
}
} while (command != null);
}
}
}