-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfor.kat
executable file
·81 lines (69 loc) · 1.9 KB
/
for.kat
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
class ForLoop : Statement
{
pattern
{
"for" "(" init:Expression ";" cond:Expression ";" inc:Expression ")"
body:Statement
}
method Run()
{
this.init.Get...();
while (this.cond.Get...())
{
this.body.Run...();
this.inc.Get...();
}
}
}
class ForInLoop : Statement
{
pattern
{
"for" "(" var:Expression "in" collection:Expression ")"
body:Statement
}
method Run()
{
collection = this.collection.Get...();
/*
Remember that lots of things are IEnumerable - check more specific
types like IDictionary first.
*/
if (collection is System.String)
{
string = collection;
for (n = 0; n < string.Length; n++)
{
this.var.Set...(string[n]);
this.body.Run...();
}
}
else if (collection is System.Collections.IDictionary)
{
enumerator = collection.GetEnumerator();
while (enumerator.MoveNext())
{
entry = enumerator.Current;
// Can't use List - not defined yet
tupple = new System.Collections.ArrayList();
tupple.Add(entry.Key);
tupple.Add(entry.Value);
this.var.Set...(tupple);
this.body.Run...();
}
}
else if (collection is System.Collections.IEnumerable)
{
enumerator = collection.GetEnumerator();
while (enumerator.MoveNext())
{
this.var.Set...(enumerator.Current);
this.body.Run...();
}
}
else
{
throw "not enumerable";
}
}
}