forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathloops.h
329 lines (277 loc) · 10.4 KB
/
loops.h
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
#ifndef LOOPS_H
#define LOOPS_H
// ****************************************************************************
// loops.h DB48X project
// ****************************************************************************
//
// File Description:
//
// Implement basic loops
//
//
//
//
//
//
//
//
// ****************************************************************************
// (C) 2022 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
//
// Payload format:
//
// Loops have the same format:
// - ID for the type
// - Total length
// - Condition object, typically a program
// - Body object, typically a program, which is executed repeatedly
#include "command.h"
#include "program.h"
#include "symbol.h"
struct loop : command
// ----------------------------------------------------------------------------
// Loop structures
// ----------------------------------------------------------------------------
{
loop(id type, object_g body, symbol_g name);
result condition(bool &value) const;
static size_t required_memory(id i, object_g body, symbol_g name)
{
return leb128size(i)
+ (name ? object_p(symbol_p(name))->size() : 0)
+ body->size();
}
static bool interrupted() { return program::interrupted(); }
static result evaluate_condition(id type, bool (runtime::*method)(bool));
protected:
// Shared code for parsing and rendering, taking delimiters as input
static result object_parser(parser &p,
cstring open,
cstring middle,
cstring close2, id id2,
cstring close1, id id1,
cstring terminator,
bool loopvar);
static result object_parser(parser UNUSED &p,
cstring op,
cstring mid,
cstring cl1, id id1,
cstring cl2, id id2,
bool loopvar)
{
// Warning: close1/close2 intentionally swapped here
return object_parser(p, op, mid, cl1, id1, cl2, id2, nullptr, loopvar);
}
static result object_parser(parser UNUSED &p,
cstring op,
cstring mid,
cstring cl1, id id1)
{
// Warning: close1/close2 intentionally swapped here
return object_parser(p, op, mid, cl1, id1, 0, id1, 0, false);
}
intptr_t object_renderer(renderer &r,
cstring open, cstring middle, cstring close,
bool loopvar = false) const;
public:
SIZE_DECL(loop);
};
struct conditional_loop : loop
// ----------------------------------------------------------------------------
// Loop structures
// ----------------------------------------------------------------------------
{
conditional_loop(id type, object_g condition, object_g body);
static size_t required_memory(id i, object_g condition, object_g body)
{
return leb128size(i) + condition->size() + body->size();
}
public:
SIZE_DECL(conditional_loop);
};
struct DoUntil : conditional_loop
// ----------------------------------------------------------------------------
// do...until...end loop
// ----------------------------------------------------------------------------
{
DoUntil(id type, object_g condition, object_g body)
: conditional_loop(type, condition, body) {}
public:
OBJECT_DECL(DoUntil);
PARSE_DECL(DoUntil);
EVAL_DECL(DoUntil);
RENDER_DECL(DoUntil);
INSERT_DECL(DoUntil);
};
struct WhileRepeat : conditional_loop
// ----------------------------------------------------------------------------
// while...repeat...end loop
// ----------------------------------------------------------------------------
{
WhileRepeat(id type, object_g condition, object_g body)
: conditional_loop(type, condition, body) {}
public:
OBJECT_DECL(WhileRepeat);
PARSE_DECL(WhileRepeat);
EVAL_DECL(WhileRepeat);
RENDER_DECL(WhileRepeat);
INSERT_DECL(WhileRepeat);
};
struct StartNext : loop
// ----------------------------------------------------------------------------
// start..next loop
// ----------------------------------------------------------------------------
{
StartNext(id type, object_g body): loop(type, body, nullptr) {}
StartNext(id type, object_g body, symbol_g n): loop(type, body, n) {}
public:
OBJECT_DECL(StartNext);
PARSE_DECL(StartNext);
EVAL_DECL(StartNext);
RENDER_DECL(StartNext);
INSERT_DECL(StartNext);
};
struct StartStep : StartNext
// ----------------------------------------------------------------------------
// start..step loop
// ----------------------------------------------------------------------------
{
StartStep(id type, object_g body): StartNext(type, body) {}
public:
OBJECT_DECL(StartStep);
EVAL_DECL(StartStep);
RENDER_DECL(StartStep);
INSERT_DECL(StartStep);
};
struct ForNext : StartNext
// ----------------------------------------------------------------------------
// for..next loop
// ----------------------------------------------------------------------------
{
ForNext(id type, object_g body, symbol_g name)
: StartNext(type, body, name) {}
static result counted(object_p o, bool stepping);
public:
OBJECT_DECL(ForNext);
PARSE_DECL(ForNext);
SIZE_DECL(ForNext);
EVAL_DECL(ForNext);
RENDER_DECL(ForNext);
INSERT_DECL(ForNext);
};
struct ForStep : ForNext
// ----------------------------------------------------------------------------
// for..step loop
// ----------------------------------------------------------------------------
{
ForStep(id type, object_g body, symbol_g name): ForNext(type, body, name) {}
public:
OBJECT_DECL(ForStep);
EVAL_DECL(ForStep);
RENDER_DECL(ForStep);
INSERT_DECL(ForStep);
};
struct conditional : object
// ----------------------------------------------------------------------------
// A non-parseable object used to select conditionals
// ----------------------------------------------------------------------------
{
conditional(id type) : object(type) {}
public:
OBJECT_DECL(conditional);
RENDER_DECL(conditional);
EVAL_DECL(conditional);
};
struct need_conditional : object
// ----------------------------------------------------------------------------
// A non-parseable object used to ensure a conditional is a truth value
// ----------------------------------------------------------------------------
{
need_conditional(id type) : object(type) {}
public:
OBJECT_DECL(need_conditional);
RENDER_DECL(need_conditional);
EVAL_DECL(need_conditional);
};
struct while_conditional : conditional
// ----------------------------------------------------------------------------
// A non-parseable object used to select branches in while loops
// ----------------------------------------------------------------------------
{
while_conditional(id type): conditional(type) {}
OBJECT_DECL(while_conditional);
RENDER_DECL(while_conditional);
EVAL_DECL(while_conditional);
};
struct start_next_conditional : conditional
// ----------------------------------------------------------------------------
// A non-parseable object used to select branches in a start-next
// ----------------------------------------------------------------------------
{
start_next_conditional(id type): conditional(type) {}
OBJECT_DECL(start_next_conditional);
RENDER_DECL(start_next_conditional);
EVAL_DECL(start_next_conditional);
};
struct start_step_conditional : conditional
// ----------------------------------------------------------------------------
// A non-parseable object used to select branches in a start-step
// ----------------------------------------------------------------------------
{
start_step_conditional(id type): conditional(type) {}
OBJECT_DECL(start_step_conditional);
RENDER_DECL(start_step_conditional);
EVAL_DECL(start_step_conditional);
};
struct for_next_conditional : conditional
// ----------------------------------------------------------------------------
// A non-parseable object used to select branches in a for-next
// ----------------------------------------------------------------------------
{
for_next_conditional(id type): conditional(type) {}
OBJECT_DECL(for_next_conditional);
RENDER_DECL(for_next_conditional);
EVAL_DECL(for_next_conditional);
};
struct for_step_conditional : conditional
// ----------------------------------------------------------------------------
// A non-parseable object used to select branches in a for-step
// ----------------------------------------------------------------------------
{
for_step_conditional(id type): conditional(type) {}
OBJECT_DECL(for_step_conditional);
RENDER_DECL(for_step_conditional);
EVAL_DECL(for_step_conditional);
};
struct for_next_list : conditional
// ----------------------------------------------------------------------------
// A non-parseable object used to select branches in a for-next
// ----------------------------------------------------------------------------
{
for_next_list(id type): conditional(type) {}
OBJECT_DECL(for_next_list);
RENDER_DECL(for_next_list);
EVAL_DECL(for_next_list);
};
struct start_next_list : conditional
// ----------------------------------------------------------------------------
// A non-parseable object used to select branches in a for-step
// ----------------------------------------------------------------------------
{
start_next_list(id type): conditional(type) {}
OBJECT_DECL(start_next_list);
RENDER_DECL(start_next_list);
EVAL_DECL(start_next_list);
};
#endif // LOOPS_H