-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeBlock.cs
368 lines (331 loc) · 14.5 KB
/
CodeBlock.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
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
using System;
using System.Collections.Generic;
namespace ProgramModel
{
/// <summary>
/// A CodeBlock object represents a block of code. It has
/// two type parameters, MutationT and ConditionT, which
/// are the types of the mutations and conditions for
/// this programming language respectively.
///
/// Note that "mutations" and "conditions" are
/// treated as opaque by a program builder, this
/// allows a program builder to be used for various
/// kinds of imperative programs. The general
/// guideline for intended use, though, is:
/// *An "mutation" represents an expression in
/// the language that changes the state of the
/// world in some way
/// *A "condition" is something that evaluates to
/// true or false based on the current state of the
/// world.
/// Note that here "the world" could mean machine
/// memory, but "changing the state of the world"
/// could also mean producing output or reading
/// input off of an input stream.
///
/// CodeBlock objects contain various mutator methods, each
/// adds additional things to the code block. Things that
/// can be added are assignments, if structures, if/else
/// structures, while and do/while loops, continue, break
/// and return statements, where continue and break
/// target enclosing loops.
///
/// Note that control structures (if/if-else/while/do-while)
/// have CodeBlock objects associated with them, so CodeBlocks
/// contain control structures, which have CodeBlocks below them,
/// which can have control structures with CodeBlocks below
/// them, etc.
/// </summary>
public partial class CodeBlock<MutationT, ConditionT>
where MutationT : class
where ConditionT : class
{
//sequence of contents in this code block
private readonly IList<Either<MutationT, CodeBlockNonAssignment>> contents;
//loop that immediately encloses this code block, might be null
private readonly Loop enclosingLoop;
//parent code block to this code block, may be null
private CodeBlock<MutationT, ConditionT> parent;
/// <summary>
/// Create a new CodeBlock object, which models an empty
/// block of code. Mutator methods should be called to
/// add contents to the code block.
/// </summary>
public CodeBlock() : this(null) {}
private CodeBlock(CodeBlock<MutationT, ConditionT> parentBlock) : this(parentBlock, null) {}
private CodeBlock(CodeBlock<MutationT, ConditionT> parentBlock, Loop enclosingLoop)
{
contents = new List<Either<MutationT, CodeBlockNonAssignment>>();
this.parent = parentBlock;
this.enclosingLoop = enclosingLoop;
}
/// <summary>
/// Append an abstract mutation to the end of this CodeBlock.
/// </summary>
/// <param name="mutation">Mutation to append to the end of this CodeBlock.</param>
public void addMutation(MutationT mutation)
{
contents.Add (Either<MutationT, CodeBlockNonAssignment>
.left<MutationT, CodeBlockNonAssignment>(mutation));
}
private void addNonAssignment(CodeBlockNonAssignment nonAssignment)
{
contents.Add (Either<MutationT, CodeBlockNonAssignment>
.right<MutationT, CodeBlockNonAssignment>(nonAssignment));
}
/// <summary>
/// Appends an if structure to the end of this code block, which uses the
/// given condition to guard its CodeBlock. The CodeBlock guarded
/// by the if is returned, which can be used to define what code is
/// to be executed conditionally.
/// </summary>
/// <returns>CodeBlock object which can be used to define the code to be executed conditionally by this if.</returns>
/// <param name="condition">Condition that protects the enclosed CodeBlock.</param>
public CodeBlock<MutationT, ConditionT> addIf(ConditionT condition)
{
If iif = new If (condition, this);
addNonAssignment (iif);
return iif.codeBlock;
}
/// <summary>
/// Appends an if-else structure to this CodeBlock, which of its sub blocks
/// will be executed is controlled by the condition object passed as a method
/// parameter.
///
/// This method returns a pair of CodeBlock objects, the first is the CodeBlock
/// that will be executed if the condition is true, the second is the CodeBlock
/// that will be executed if the condition is false. The CodeBlock object references
/// can be used to define the actual code that is to be conditionally executed.
/// </summary>
/// <returns>The two code blocks which are underneath this if-else, first that to be executed
/// on the "if," the second to be executed on the "else."</returns>
/// <param name="condition">Condition that will be used to decide whether the first or second
/// code block is executed.</param>
public Tuple<CodeBlock<MutationT, ConditionT>, CodeBlock<MutationT, ConditionT>> addIfElse(ConditionT condition)
{
IfElse ifElse = new IfElse (condition, this);
addNonAssignment (ifElse);
return new Tuple<CodeBlock<MutationT, ConditionT>, CodeBlock<MutationT, ConditionT>> (ifElse.codeBlock, ifElse.elseCodeBlock);
}
/// <summary>
/// Appends a while structure to the end of this CodeBlock, which uses the given
/// condition as the loop condition.
///
/// This method returns a CodeBlock which can be used
/// to define the code contained in this while loop.
/// </summary>
/// <returns>A CodeBlock which defines the code protected by the loop.</returns>
/// <param name="condition">Condition to check to decide whether or not to loop.</param>
public CodeBlock<MutationT, ConditionT> addWhile(ConditionT condition)
{
While whiile = new While (condition, this);
addNonAssignment (whiile);
return whiile.codeBlock;
}
/// <summary>
/// Appends a while structure to the end of this CodeBlock, which uses the given
/// condition as the loop condition and internally uses the given name to name
/// the loop.
///
/// Note that the name given to the loop does not have any impact on the behavior
/// of the function, but is useful in debugging. For this reason it may be advisable
/// to use unique names for unique loops.
///
/// This method returns a (CodeBlock, Loop) pair. The CodeBlock handle can be used
/// to define the code contained in this while loop, the Loop object can be used as
/// a target for "break" and "continue" statements in the contained code block, or
/// any CodeBlocks contained underneath it hierarchically.
/// </summary>
/// <returns>A CodeBlock/Loop pair, the CodeBlock defines the code protected by the
/// loop, the Loop object can be used as a target for continue/break.</returns>
/// <param name="condition">Condition to check to decide whether or not to loop.</param>
/// <param name="labelName">A string denoting the loop, used only in debugging, otherwise meaningless</param>
public Tuple<CodeBlock<MutationT, ConditionT>, Loop> addWhile(ConditionT condition, String labelName)
{
While whiile = new While (condition, this, labelName);
addNonAssignment (whiile);
return new Tuple<CodeBlock<MutationT, ConditionT>, Loop> (whiile.codeBlock, whiile);
}
/// <summary>
/// Appends a do-while structure to the end of this CodeBlock, which uses the given
/// condition as the loop condition.
///
/// This method returns a CodeBlock which can be used
/// to define the code contained in this do-while loop.
/// </summary>
/// <returns>A CodeBlock which defines the code protected by the loop.</returns>
/// <param name="condition">Condition to check to decide whether or not to loop.</param>
public CodeBlock<MutationT, ConditionT> addDoWhile(ConditionT condition)
{
DoWhile doWhile = new DoWhile (condition, this);
addNonAssignment (doWhile);
return doWhile.codeBlock;
}
/// <summary>
/// Appends a do-while structure to the end of this CodeBlock, which uses the given
/// condition as the loop condition and internally uses the given name to name
/// the loop.
///
/// Note that the name given to the loop does not have any impact on the behavior
/// of the function, but is useful in debugging. For this reason it may be advisable
/// to use unique names for unique loops.
///
/// This method returns a (CodeBlock, Loop) pair. The CodeBlock handle can be used
/// to define the code contained in this do-while loop, the Loop object can be used as
/// a target for "break" and "continue" statements in the contained code block, or
/// any CodeBlocks contained underneath it hierarchically.
/// </summary>
/// <returns>A CodeBlock/Loop pair, the CodeBlock defines the code protected by the
/// loop, the Loop object can be used as a target for continue/break.</returns>
/// <param name="condition">Condition to check to decide whether or not to loop.</param>
/// <param name="labelName">A string denoting the loop, used only in debugging, otherwise meaningless</param>
public Tuple<CodeBlock<MutationT, ConditionT>, Loop> addDoWhile(ConditionT condition, String labelName)
{
DoWhile doWhile = new DoWhile (condition, this, labelName);
addNonAssignment (doWhile);
return new Tuple<CodeBlock<MutationT, ConditionT>, Loop> (doWhile.codeBlock, doWhile);
}
private Loop getNearestEnclosingLoop()
{
for (CodeBlock<MutationT, ConditionT> currBlock = this; currBlock != null; currBlock = currBlock.parent) {
if (currBlock.enclosingLoop != null)
return currBlock.enclosingLoop;
}
return null;
}
private void checkLoopIsEnclosing(Loop loop)
{
for (CodeBlock<MutationT, ConditionT> currBlock = this; currBlock != null; currBlock = currBlock.parent) {
if (currBlock.enclosingLoop == loop)
return;
}
throw new ArgumentException ("loop is not enclosing loop of this block");
}
/// <summary>
/// Appends a "continue" statement to the end of this CodeBlock, which targets the
/// nearest enclosing loop. If there is no enclosing loop, calling this method will
/// cause a runtime exception. Note that it will never make sense to add any more
/// code to the CodeBlock after a "continue," since any such code would never be
/// executed.
/// </summary>
public void addContinue ()
{
Loop nearestEnclosingLoop = getNearestEnclosingLoop ();
if (nearestEnclosingLoop == null)
throw new InvalidOperationException ("Can't add continue, no enclosing loop");
else
addContinue (nearestEnclosingLoop);
}
/// <summary>
/// Appends a "continue" statement to the end of this CodeBlock, which targets the
/// enclosing loop corresponding to the given Loop object. Note that if "loop" does
/// not refer to an enclosing loop, this method will
/// cause a runtime exception. Also note that it will never make sense to add any more
/// code to the CodeBlock after a "continue," since any such code would never be
/// executed.
/// </summary>
/// <param name="loop">Enclosing loop to which we would like to continue. If "loop"
/// is not an enclosing loop, a runtime exception will be generated..</param>
public void addContinue(Loop loop)
{
checkLoopIsEnclosing (loop);
addNonAssignment (new Continue (loop));
}
/// <summary>
/// Appends a "break" statement to the end of this CodeBlock, which targets the
/// nearest enclosing loop. If there is no enclosing loop, calling this method will
/// cause a runtime exception. Note that it will never make sense to add any more
/// code to the CodeBlock after a "break," since any such code would never be
/// executed.
/// </summary>
public void addBreak()
{
Loop nearestEnclosingLoop = getNearestEnclosingLoop ();
if (nearestEnclosingLoop == null)
throw new InvalidOperationException ("Can't add break, no enclosing loop");
else
addBreak (nearestEnclosingLoop);
}
/// <summary>
/// Appends a "break" statement to the end of this CodeBlock, which targets the
/// enclosing loop corresponding to the given Loop object. Note that if "loop" does
/// not refer to an enclosing loop, this method will
/// cause a runtime exception. Also note that it will never make sense to add any more
/// code to the CodeBlock after a "break," since any such code would never be
/// executed.
/// </summary>
/// <param name="loop">Enclosing loop out of which we would like to break. If "loop"
/// is not an enclosing loop, a runtime exception will be generated..</param>
public void addBreak (Loop loop)
{
checkLoopIsEnclosing (loop);
addNonAssignment (new Break (loop));
}
/// <summary>
/// Appends a "return" statement to the end of this CodeBlock. Note that it never
/// makes sense to add any more code after a "return," since any such code would never
/// be executed.
/// </summary>
public void addReturn()
{
addNonAssignment (RETURN);
}
private static readonly Return RETURN = new Return();
private interface CodeBlockNonAssignment {}
private class Return : CodeBlockNonAssignment {}
private abstract class ContinueBreakBase : CodeBlockNonAssignment
{
public readonly Loop loop;
public ContinueBreakBase(Loop loop)
{
this.loop = loop;
}
}
private class Continue : ContinueBreakBase
{
public Continue(Loop loop) : base(loop) {}
}
private class Break : ContinueBreakBase
{
public Break(Loop loop) : base(loop) {}
}
private abstract class IfWhileDoWhileBase : CodeBlockNonAssignment
{
public readonly ConditionT condition;
public readonly CodeBlock<MutationT, ConditionT> codeBlock;
public IfWhileDoWhileBase(ConditionT condition, CodeBlock<MutationT, ConditionT> parent, Boolean isLoop)
{
this.condition = condition;
this.codeBlock = new CodeBlock<MutationT, ConditionT>(parent, isLoop? (Loop) this : null);
}
}
private class If : IfWhileDoWhileBase
{
public If(ConditionT condition, CodeBlock<MutationT, ConditionT> parent) : base(condition, parent, false) {}
}
private class IfElse : If
{
public readonly CodeBlock<MutationT, ConditionT> elseCodeBlock;
public IfElse(ConditionT condition, CodeBlock<MutationT, ConditionT> parent) : base(condition, parent)
{
this.elseCodeBlock = new CodeBlock<MutationT, ConditionT>(parent);
}
}
private abstract class WhileDoWhileBase : IfWhileDoWhileBase, Loop
{
public readonly String label;
public WhileDoWhileBase(ConditionT condition, CodeBlock<MutationT, ConditionT> parent, string label) : base(condition, parent, true) {
this.label = label;
}
}
private class While : WhileDoWhileBase
{
public While(ConditionT condition, CodeBlock<MutationT, ConditionT> parent, string label = null) : base(condition, parent, label) {}
}
private class DoWhile : WhileDoWhileBase
{
public DoWhile(ConditionT condition, CodeBlock<MutationT, ConditionT> parent, string label = null) : base(condition, parent, label) {}
}
}
}