-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathReadVar.java
More file actions
64 lines (60 loc) · 1.51 KB
/
ReadVar.java
File metadata and controls
64 lines (60 loc) · 1.51 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
import java.util.Scanner;
import java.util.ArrayList;
class ReadVar extends Stmt implements Token
{
String id;
Expr inner;
FullType datum;
public ReadVar(String i)
{
id = i;
inner = null;
}
public ReadVar(String i, Expr e)
{
id = i;
inner = e;
}
public String toString(int t)
{
return T(t) + "read " + id + (inner != null ? "[" + inner.toString(t) + "]" : "") + ";" + super.toString(t);
}
public void typeCheck() throws ExampleException
{
FullType thisType = table.getType(id);
datum = thisType;
if (thisType == null)
throw new ExampleException("Error: " + id + " not declared");
if (thisType.isFunction)
throw new ExampleException("Error: functions aren't readable");
if (thisType.isArray)
{
FullType innerType = inner.typeCheck();
if (!innerType.baseType.equals("var"))
throw new ExampleException("Error: varf in array access");
}
}
public void execute()
{
Integer i = new Integer(-1);
if (inner != null)
i = (Integer)inner.execute();
Scanner inp = new Scanner(System.in);
if (datum.baseType.equals("var"))
{
Integer in = new Integer(inp.nextInt());
if (inner != null)
((ArrayList<Object>)datum.value).set(i,in);
else
datum.value = in;
}
else
{
Float in = new Float(inp.nextFloat());
if (inner != null)
((ArrayList<Object>)datum.value).set(i,in);
else
datum.value = in;
}
}
}