/
IStmt.cs
253 lines (217 loc) · 6.99 KB
/
IStmt.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using Exceptions;
namespace LabCsharp
{
public interface IStmt
{
ProgState execute(ProgState prog);
string ToString();
}
public class CompStmt : IStmt
{
private IStmt st1, st2;
public CompStmt(IStmt st1,IStmt st2)
{
this.st1 = st1;
this.st2 = st2;
}
public ProgState execute(ProgState prog)
{
MyIStack<IStmt> stk=prog.getStk();
stk.push(st2);
stk.push(st1);
return prog;
}
public override string ToString() {return "("+st1.ToString()+";"+st2.ToString()+")";}
}
public class AssignStmt: IStmt
{
private string id;
private Exp exp;
public AssignStmt(string id, Exp exp)
{
this.id = id;
this.exp = exp;
}
public ProgState execute(ProgState prog)
{
MyIDictionary<string, int> dict = prog.getDict();
try
{
if (dict.isDefined(this.id) == 1)
dict.update(id, exp.eval(dict));
else
dict.add(id,exp.eval(dict));
return prog;
}
catch(StmtExceptions e)
{
throw new ControllerException(e);
}
catch (DivizionByZeroException e)
{
throw new ControllerException(e);
}
}
public override string ToString() { return id + "=" + exp.ToString(); }
}
public class IfStmt : IStmt
{
private Exp ex;
private IStmt thenS;
private IStmt elseS;
public IfStmt(Exp ex, IStmt thenS,IStmt elseS)
{
this.ex = ex;
this.thenS = thenS;
this.elseS = elseS;
}
public ProgState execute(ProgState prog)
{
MyIStack<IStmt> stk=prog.getStk();
MyIDictionary<string, int> dict=prog.getDict();
try {
if (ex.eval(dict) > 0)
stk.push(thenS);
else
stk.push(elseS);
} catch (StmtExceptions e) {
throw new ControllerException(e);
}
return prog;
}
public override string ToString()
{
return "IF(" + ex.ToString() + ")THEN(" + thenS.ToString() + ")ELSE(" + elseS.ToString() + ")";
}
}
public class PrintStmt : IStmt
{
private Exp ex;
public PrintStmt(Exp ex) { this.ex = ex; }
public ProgState execute(ProgState prog)
{
MyIList<int> list=prog.getList();
MyIDictionary<string,int> dict= prog.getDict();
try {
list.add(ex.eval(dict));
return prog;
}
catch (StmtExceptions e)
{
throw new ControllerException(e);
}
}
public override string ToString() { return "Print(" + ex.ToString() + ")"; }
}
public class OpenFile : IStmt
{
private string filename,varName;
public OpenFile(string varName, string filename)
{
this.filename = filename;
this.varName = varName;
}
public ProgState execute(ProgState prog)
{
MyIDictionary<string, int> dict = prog.getDict();
MyIFileTable<Tuple> Tpl = prog.getFile();
try
{
if (Tpl.isEmpty()==1)
for (int i=1; i<=Tpl.getKey();i++ )
{
Tuple tpl = Tpl.get(i);
if (tpl != null && tpl.getName() == filename)
throw new ControllerException("The file does not exist!! \n");
}
StreamReader st = new StreamReader(filename);
Tuple tup = new Tuple(filename,st);
Tpl.add(tup);
dict.add(varName,Tpl.getKey());
return prog;
}
catch(FileNotFoundException e)
{
Console.WriteLine(e.ToString());
}
catch(DictExceptions e)
{ throw new ControllerException(e.ToString()); }
return null;
}
public override string ToString(){ return "Opening:" + filename; }
}
public class ReadFile : IStmt
{
private Exp exp;
private string name;
public ReadFile(Exp exp, string name)
{
this.name = name;
this.exp = exp;
}
public ProgState execute(ProgState prog)
{
MyIDictionary<string, int> dict = prog.getDict();
MyIFileTable<Tuple> tpl = prog.getFile();
try
{
int val = exp.eval(dict);
Tuple tup = tpl.get(val);
if (tup != null)
{
string st = tup.getST().ReadLine();
if (st != null)
{
if (dict.isDefined(name) == 0)
dict.add(name, int.Parse(st));
else
dict.update(name, int.Parse(st));
}
}
else
throw new ControllerException("The file does not exist !!! \n");
}
catch(StmtExceptions e)
{
throw new ControllerException(e);
}
catch(IOException e)
{
Console.WriteLine(e.ToString());
}
return prog;
}
public override string ToString() { return "Reading from:" + exp.ToString(); }
}
public class CloseFile : IStmt
{
private Exp exp;
public CloseFile(Exp exp) { this.exp = exp; }
public ProgState execute(ProgState prog)
{
MyIDictionary<string, int> dict = prog.getDict();
MyIFileTable<Tuple> Tpl = prog.getFile();
try
{
int val = exp.eval(dict);
Tuple tpl = Tpl.get(val);
if (tpl != null)
{
tpl.getST().Close();
Tpl.remove(val);
}
else
throw new ControllerException("The file does not exist!! \n");
}
catch (StmtExceptions e)
{ throw new ControllerException(e); }
catch (IOException e)
{ Console.WriteLine(e.ToString()); }
return prog;
}
public override string ToString() { return "Closing the file\n"; }
}
}