forked from IoLanguage/io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IoObject_flow.c
241 lines (192 loc) · 4.95 KB
/
IoObject_flow.c
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
#include "IoObject.h"
#include "IoNumber.h"
// loops ---------------------------------------
IO_METHOD(IoObject, while)
{
/*doc Object while(<condition>, expression)
Keeps evaluating message until condition return Nil.
Returns the result of the last message evaluated or Nil if none were evaluated.
*/
IoMessage_assertArgCount_receiver_(m, 2, self);
{
IoObject *result = IONIL(self);
IoState *state = IOSTATE;
unsigned char c;
IoState_resetStopStatus(state);
IoState_pushRetainPool(state);
for (;;)
{
IoState_clearTopPool(state);
IoState_stackRetain_(state, result);
IoObject *v = IoMessage_locals_valueArgAt_(m, locals, 0);
c = ISTRUE(IoMessage_locals_performOn_(IOSTATE->asBooleanMessage, v, v));
if (!c)
{
break;
}
result = (IoObject *)IoMessage_locals_valueArgAt_(m, locals, 1);
if (IoState_handleStatus(state))
{
goto done;
}
}
done:
IoState_popRetainPoolExceptFor_(state, result);
return result;
}
}
IO_METHOD(IoObject, loop)
{
/*doc Object loop(expression)
Keeps evaluating message until a break.
*/
IoMessage_assertArgCount_receiver_(m, 1, self);
{
IoState *state = IOSTATE;
IoObject *result;
IoState_resetStopStatus(IOSTATE);
IoState_pushRetainPool(state);
for (;;)
{
IoState_clearTopPool(state);
result = IoMessage_locals_valueArgAt_(m, locals, 0);
if (IoState_handleStatus(IOSTATE))
{
goto done;
}
}
done:
IoState_popRetainPoolExceptFor_(state, result);
return result;
}
}
IO_METHOD(IoObject, for)
{
/*doc Object for(<counter>, <start>, <end>, <do message>)
A for-loop control structure. See the io Programming Guide for a full description.
*/
IoMessage_assertArgCount_receiver_(m, 4, self);
{
IoState *state = IOSTATE;
IoMessage *indexMessage = IoMessage_rawArgAt_(m, 0);
IoMessage *doMessage;
IoObject *result = IONIL(self);
double i;
IoSymbol *slotName = IoMessage_name(indexMessage);
double startValue = IoMessage_locals_doubleArgAt_(m, locals, 1);
double endValue = IoMessage_locals_doubleArgAt_(m, locals, 2);
double increment = 1;
IoNumber *num = NULL;
if (IoMessage_argCount(m) > 4)
{
increment = IoMessage_locals_doubleArgAt_(m, locals, 3);
doMessage = IoMessage_rawArgAt_(m, 4);
}
else
{
doMessage = IoMessage_rawArgAt_(m, 3);
// if (startValue > endValue)
// {
// increment = -1;
// }
}
IoState_resetStopStatus(state);
IoState_pushRetainPool(state);
for (i = startValue; ; i += increment)
{
if (increment > 0)
{
if (i > endValue) break;
}
else
{
if (i < endValue) break;
}
/*if (result != locals && result != self) IoState_immediatelyFreeIfUnreferenced_(state, result);*/
IoState_clearTopPool(state);
{
num = IONUMBER(i);
IoObject_addingRef_(locals, num);
PHash_at_put_(IoObject_slots(locals), slotName, num);
//IoObject_setSlot_to_(self, slotName, num);
}
/*IoObject_setSlot_to_(locals, slotName, IONUMBER(i));*/
result = IoMessage_locals_performOn_(doMessage, locals, self);
if (IoState_handleStatus(IOSTATE))
{
goto done;
}
}
done:
IoState_popRetainPoolExceptFor_(state, result);
return result;
}
}
IO_METHOD(IoObject, return)
{
/*doc Object return(anObject)
Return anObject from the current execution block.
*/
IoObject *v = IoMessage_locals_valueArgAt_(m, locals, 0);
IoState_return(IOSTATE, v);
return self;
}
IO_METHOD(IoObject, returnIfNonNil)
{
/*doc Object returnIfNonNil
Returns the receiver from the current execution block if it is non nil.
Otherwise returns the receiver locally.
*/
if(!ISNIL(self))
{
IoState_return(IOSTATE, self);
}
return self;
}
IO_METHOD(IoObject, break)
{
/*doc Object break(optionalReturnValue)
Break the current loop, if any.
*/
IoObject *v = IONIL(self);
if (IoMessage_argCount(m) > 0)
{
v = IoMessage_locals_valueArgAt_(m, locals, 0);
}
IoState_break(IOSTATE, v);
return self;
}
IO_METHOD(IoObject, continue)
{
/*doc Object continue
Skip the rest of the current loop iteration and start on
the next, if any.
*/
IoState_continue(IOSTATE);
return self;
}
IO_METHOD(IoObject, stopStatus)
{
/*doc Object stopStatus
Returns the internal IoState->stopStatus.
*/
int stopStatus;
IoMessage_locals_valueArgAt_(m, locals, 0);
stopStatus = IOSTATE->stopStatus;
IoState_resetStopStatus(IOSTATE);
return IoState_stopStatusObject(IOSTATE, stopStatus);
}
IO_METHOD(IoObject, if)
{
/*doc Object if(<condition>, <trueMessage>, <optionalFalseMessage>)
Evaluates trueMessage if condition evaluates to a non-Nil.
Otherwise evaluates optionalFalseMessage if it is present.
Returns the result of the evaluated message or Nil if none was evaluated.
*/
IoObject *r = IoMessage_locals_valueArgAt_(m, locals, 0);
const int condition = ISTRUE(IoMessage_locals_performOn_(IOSTATE->asBooleanMessage, r, r));
const int index = condition ? 1 : 2;
if (index < IoMessage_argCount(m))
return IoMessage_locals_valueArgAt_(m, locals, index);
return IOBOOL(self, condition);
}