-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDecl.java
More file actions
176 lines (169 loc) · 3.82 KB
/
Decl.java
File metadata and controls
176 lines (169 loc) · 3.82 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
import java.util.ArrayList;
class Decl extends Stmt implements Token
{
Type type;
String id;
OptionalAsn asn;
boolean isArray;
int arraySize;
boolean isFunction;
ArgList argList;
StmtList fnm;
StmtList stmtList;
Expr returnExpr;
FullType datum;
public Decl(Type t, String i, OptionalAsn o)
{
type = t;
id = i;
asn = o;
isArray = false;
isFunction = false;
}
public Decl(Type t, String i, int n)
{
type = t;
id = i;
isArray = true;
arraySize = n;
asn = null;
isFunction = false;
}
public Decl(Type t, String i, ArgList a, StmtList f, StmtList s, Expr e)
{
type = t;
id = i;
isArray = false;
isFunction = true;
asn = null;
argList = a;
fnm = f;
stmtList = s;
returnExpr = e;
}
public Decl(String i, ArgList a, StmtList f, StmtList s)
{
type = null;
id = i;
isArray = false;
isFunction = true;
asn = null;
argList = a;
fnm = f;
stmtList = s;
returnExpr = null;
}
public String toString(int t)
{
String ret = T(t);
if (type != null)
ret += type.toString(t);
else
ret += "void";
ret += " ";
ret += id;
if (asn != null)
ret += asn.toString(t);
if (isArray)
ret += "[" + Integer.toString(arraySize) + "]";
if (isFunction)
{
ret += "(";
if (argList != null)
ret += argList.toString(t);
ret += ")\n" + T(t) + "{\n";
if (fnm != null)
ret += fnm.toString(t+1);
ret += stmtList.toString(t+1);
if (returnExpr != null)
ret += T(t+1) + "return " + returnExpr.toString(t+1) + "\n";
ret += T(t) + "}";
}
else
ret += ";";
ret += super.toString(t);
return ret;
}
public void typeCheck() throws ExampleException
{
FullType t;
if (type != null)
t = new FullType(type.toString(0));
else
t = new FullType("void");
t.isArray = isArray;
if (isArray)
{
t.value = new ArrayList<Object>();
for (int x = 0; x < arraySize; ++x)
if (t.baseType.equals("var"))
((ArrayList<Object>)t.value).add(new Integer(0));
else
((ArrayList<Object>)t.value).add(new Float(0));
}
t.isFunction = isFunction;
if (isFunction)
{
table.enterScope();
if (argList != null)
t.args = argList.typeCheck();
else
t.args = new ArrayList<Pair<String,FullType>>();
t.value = this;
}
if (asn != null)
{
FullType optType = asn.typeCheck();
if (!t.isAble(optType))
throw new ExampleException("Error: cannot convert varF to var");
}
boolean res;
if (!isFunction)
{
res = table.add(id,t);
datum = t;
}
else
{
res = table.addFun(id,t);
if (fnm != null)
fnm.typeCheck();
stmtList.typeCheck();
}
if (!res)
throw new ExampleException("Error: " + id + " can't be redeclared");
if (returnExpr != null)
{
FullType rt = returnExpr.typeCheck();
FullType x = new FullType(type.toString(0));
if (!x.isAble(rt))
throw new ExampleException("Error: bad return");
}
if (isFunction)
table.exitScope();
}
public void execute()
{
if (asn != null)
{
Object o = asn.execute();
if (o instanceof Integer)
datum.value = new Integer((Integer)o);
if (o instanceof Float)
datum.value = new Float((Float)o);
if (o instanceof ArrayList)
datum.value = o;
}
}
public Object call(ArrayList<Object> params)
{
argList.execute(params);
if (fnm != null)
fnm.execute();
stmtList.execute();
if (type == null)
return null;
else
return returnExpr.execute();
}
}