forked from ChristophKirst/SimKernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression_sim.h
502 lines (419 loc) · 16.4 KB
/
expression_sim.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/***************************************************************************
expression_sim.h - sim specific expressions
Christoph Kirst
Max Planck Institue for Dynamics and Self-Organisation Göttingen
HU Berlin, BCCN Göttingen & Berlin (2008)
****************************************************************************/
#ifndef EXPRESSION_SIM_H
#define EXPRESSION_SIM_H
#include <string>
#include <sstream>
#include <assert.h>
#include <iostream>
#include <vector>
#include <deque>
#include "expression.h"
#include "expression_basic.h"
//SimControl: reset_iteration() = init_iteration()
// next_iteration()
// bool end_iteration()
// unsigned int n_interations()
//#include <iostream>
//#define EXPR_SIM_DEBUG(s1, s2) std::cout << "ExprDebug: SIM: " << s1 << " " << s2 << std::endl; std::cout.flush();
#define EXPR_SIM_DEBUG(s1, s2)
//Creator[expr] evaluates expr after iterators have been defined
// checks for expr->listQ()
class ExprCreator : public Expression
{
public:
ExprPtrT list;
ExprIndexT list_step;
ExprPtrT step, start, end, delta;
typedef std::vector<ExprPtrT> CreatorListT;
static CreatorListT creators;
enum CreatorStatusT {
Ndef = 0, // kernel initialization
Init, // Creator initialization
Done // ready
};
CreatorStatusT status;
public:
EXPR_NAME_DECL()
ExprCreator()
: Expression(), status(Ndef),
list(ExprNullPtr()), list_step(0),
step(ExprNullPtr()), end(ExprNullPtr()), delta(ExprNullPtr())
{
creators.push_back(ExprPtrT(this));
};
// Creator[{expr1, expr2, ...}] (list creator)
ExprCreator(const ExprPtrT& l)
: Expression(l), status(Ndef),
list(ExprNullPtr()), list_step(0),
step(ExprNullPtr()), end(ExprNullPtr()), delta(ExprNullPtr())
{
creators.push_back(ExprPtrT(this));
};
// Creator[expr, {i,s,e,d}] (works as Table)
ExprCreator(const ExprPtrT& b, const ExprPtrT& it)
: Expression(b, it), status(Ndef),
list(ExprNullPtr()), list_step(0),
step(ExprNullPtr()), end(ExprNullPtr()), delta(ExprNullPtr())
{
creators.push_back(ExprPtrT(this));
};
static void init(ExprScopeT* scope)
{
EXPR_SIM_DEBUG("Creator: init()", "")
for (CreatorListT::iterator i = creators.begin(); i != creators.end(); i++)
{
((ExprCreator*)(*i).get())->status = Init;
};
for (CreatorListT::iterator i = creators.begin(); i != creators.end(); i++)
{
EXPR_SIM_DEBUG("Creator: init() creators:", (*i))
(*i)->evaluate(scope);
};
for (CreatorListT::iterator i = creators.begin(); i != creators.end(); i++)
{
//EXPR_EVAL_ASSERT(((ExprCreator*)(*i).get())->status == Defd, CreatorError, this)
((ExprCreator*)(*i).get())->status = Done;
};
};
public:
ExprPtrT evaluate(ExprScopeT* scope)
{
EXPR_SIM_DEBUG("Creator: evaluate():", print())
EXPR_SIM_DEBUG("Creator: evaluate(): status=", status)
// the iteration value
if (status == Done)
{
if (nargs() == 1) // list creator
{
ExprPtrT value = list->arg[list_step]->evaluate(scope);
list_step++;
if (list_step == list->nargs()) list_step = 0;
return value;
} else { // start, end, delta creator
scope->push();
scope->define_local(arg[1]->arg[0]->symbolname(), step);
ExprPtrT ret = arg[0]->evaluate(scope);
scope->pop();
if (step->integerQ() && delta->integerQ())
{
int s = int(*step) + int(*delta);
if (s > int(*end)) step = start;
else step = ExprPtrT(new ExprInteger(s));
} else {
double s = double(*step) + double(*delta);
if (s > double(*end)) step = start;
else step = ExprPtrT(new ExprReal(s));
};
return ret;
};
};
// Kenerl initialization
if (status == Ndef) return ExprPtrT(this);
// Creator initialization
if (status == Init)
{
if (nargs() == 1) // list iterator
{
list = arg[0]->evaluate(scope);
list_step = 0;
EXPR_EVAL_ASSERT(list->listQ() && list->nargs()>0, CreatorExpectList, this);
} else { // nargs() == 2 // start, end, delta iterator
start = arg[1]->arg[1]->evaluate(scope);
end = arg[1]->arg[2]->evaluate(scope);
delta = ExprPtrT(new ExprInteger(1));
if (arg[1]->nargs()>3) delta = arg[1]->arg[3]->evaluate(scope);
step = start;
EXPR_EVAL_ASSERT(start->numberQ() && end->numberQ() && delta->numberQ(), CreatorExpectNumber, this)
};
return ExprNullPtr();
};
};
ExprSyntaxErrorT check_syntax() const
{
if (nargs() == 1) return NoSyntaxError;
if (nargs() == 2)
{
if (!(arg[1]->listQ() && (arg[1]->nargs() == 4 || arg[1]->nargs() ==3))) return CreatorExpectIterationList;
if (!(arg[1]->arg[0]->symbolQ())) return CreatorExpectSymbol;
return NoSyntaxError;
};
return CreatorSyntaxError;
};
};
class ExprIterator : public Expression
{
public:
typedef std::vector<ExprPtrT> IteratorListT;
static IteratorListT iterators;
typedef std::deque<unsigned int> IteratorOrderT;
static IteratorOrderT ordering; // dependency ordering - most dependend is last
static int count; // actual iteration number
static int n_iters; // total number of iterations
static int level; // actual level for increasing the iterator
static enum IteratorCounterStatusT {CNdef = 0, CDefd} counter_status; // n_iters defined
ExprIndexT id; // position in IteratorList
ExprPtrT step, end, delta; // iteration varaible
ExprPtrT list; // list iterator
ExprPtrT value; // value of iterated variable or list
ExprIndexT list_step; // actual position in list
enum IteratorStatusT {Ndef = 0, // initial initialization of kernel
// retrun unevaluated pointer to this Iterator
// -> all non-iterated parameter are now defined
// -> iterator symbols are defined as ExprIterator
Init, // init() sets status to Init and evaluates all
// iterators then status is set to First
// -> automatic dependency ordering (assert no loops !)
// -> value, end, delta or list are set (assert this prop)
Defd, // init is done (exclude reevaluation by second symbol ref)
// -> returns unevaluated pointer to this Iterator
Eval, // Iterator is evaluated -> to exclude loop dependencies !
Done, // return actual iteration values !
//
// usage:
// rootkernel->evaluate(scope)
// ExprIterator::init(scope); ??
// while (ExprIterator::next_iteration(scope)) { ... };
};
IteratorStatusT status;
public:
EXPR_NAME_DECL()
ExprIterator() : Expression(), status(Ndef),
value(ExprNullPtr()), list(ExprNullPtr()), list_step(0),
step(ExprNullPtr()), end(ExprNullPtr()), delta(ExprNullPtr())
{
id = iterators.size();
iterators.push_back(ExprPtrT(this));
count = -1;
};
// Iterator[{it1, it2, ...}] (iterate over list)
ExprIterator(const ExprPtrT& l) : Expression(l), status(Ndef),
value(ExprNullPtr()), list(ExprNullPtr()), list_step(0),
step(ExprNullPtr()), end(ExprNullPtr()), delta(ExprNullPtr())
{
id = iterators.size();
iterators.push_back(ExprPtrT(this));
count = -1;
};
// Iterator[expr, {i,s,e,d}] (works as Table)
ExprIterator(const ExprPtrT& b, const ExprPtrT& it)
: Expression(b, it), status(Ndef),
value(ExprNullPtr()), list(ExprNullPtr()), list_step(0),
step(ExprNullPtr()), end(ExprNullPtr()), delta(ExprNullPtr())
{
id = iterators.size();
iterators.push_back(ExprPtrT(this));
count = -1;
};
static int count_iterations(ExprScopeT* scope)
{
// assume kernel is initialized
count = -1;
n_iters = 0;
while(next_iteration(scope)) {n_iters++;};
counter_status = CDefd;
count = -1;
return n_iters;
};
static int iteration() { return count; };
static int n_iterations() { return n_iters; }
static void init(ExprScopeT* scope)
{
EXPR_SIM_DEBUG("Iterator: init()", "")
ordering.clear();
for (IteratorListT::iterator i = iterators.begin(); i != iterators.end(); i++)
{
((ExprIterator*)(*i).get())->status = Init;
};
for (IteratorListT::iterator i = iterators.begin(); i != iterators.end(); i++)
{
EXPR_SIM_DEBUG("Iterator: init() iterators: ", (*i))
(*i)->evaluate(scope);
};
for (IteratorListT::iterator i = iterators.begin(); i != iterators.end(); i++)
{
EXPR_EVAL_ASSERT(((ExprIterator*)(*i).get())->status == Defd, IteratorInternalError, (*i))
((ExprIterator*)(*i).get())->status = Done;
};
//iter_status = CDefd;
};
static bool next_iteration(ExprScopeT* scope)
{
EXPR_SIM_DEBUG("Iterator: next_iteration", count)
count++;
level = 0;
//no iterators -> iterate once
if (iterators.size() == 0)
{
if (count == 0)
{
ExprCreator::init(scope);
return true;
} else return false;
};
// first call -> initialize iterators
if (count == 0) {init(scope); ExprCreator::init(scope); return true;};
// increase most dependent iterator
if ( ((ExprIterator*)(iterators[ordering[0]].get()))->increase_iteration(scope) )
{
ExprCreator::init(scope); return true;
} else return false;
};
bool increase_iteration(ExprScopeT* scope)
{
EXPR_SIM_DEBUG("Iterator: increase_iteration:", print())
if (nargs() == 1) //list
{
list_step++;
if (list_step == list->nargs())
{
level++;
if (level == iterators.size()) return false;
if (!((ExprIterator*)(iterators[ordering[level]].get()))->increase_iteration(scope))
return false;
list_step = 0;
status = Eval;
list = arg[0]->evaluate(scope);
status = Done;
EXPR_EVAL_ASSERT(list->listQ() && list->nargs()>0, IteratorExpectList, this);
};
value = list->arg[list_step];
return true;
} else { // nargs() == 2: start, end, delta iterator
bool next = false;
if (step->integerQ() && delta->integerQ())
{
int s = int(*step) + int(*delta);
step = ExprPtrT(new ExprInteger(s));
next = (s > int(*end));
} else {
double s = double(*step) + double(*delta);
step = ExprPtrT(new ExprReal(s));
next = (s > double(*end));
};
if (next)
{
level++;
if (level > iterators.size()) return false;
if (!((ExprIterator*)(iterators[ordering[level]].get()))->increase_iteration(scope))
return false;
// initialize the iterator anew
status = Eval;
step = arg[1]->arg[1]->evaluate(scope);
end = arg[1]->arg[2]->evaluate(scope);
if (arg[1]->nargs() > 3) delta = arg[1]->arg[3]->evaluate(scope);
EXPR_EVAL_ASSERT(step->numberQ() && end->numberQ() && delta->numberQ(), IteratorExpectNumber, this)
//status = Done;
};
status = Eval;
scope->push();
scope->define_local(arg[1]->arg[0]->symbolname(), step);
value = arg[0]->evaluate(scope);
scope->pop();
status = Done;
return true;
};
};
public:
ExprPtrT evaluate(ExprScopeT* scope)
{
EXPR_SIM_DEBUG("Iterator: evaluate: status=", int(status))
EXPR_EVAL_ASSERT(status != Eval, IteratorLoop, this)
// the iteration value
if (status == Done) return value;
// Kernel initialization
if (status == Ndef) return ExprPtrT(this);
// Iterator initialization
if (status == Init)
{
if (nargs() == 1) // list iterator
{
status = Eval;
list = arg[0]->evaluate(scope);
list_step = 0;
EXPR_EVAL_ASSERT(list->listQ() && list->nargs()>0, IteratorExpectList, this);
value = list->arg[0]->evaluate(scope);
status = Defd;
} else { // nargs() == 2 // start, end, delta iterator
status = Eval;
step = arg[1]->arg[1]->evaluate(scope);
end = arg[1]->arg[2]->evaluate(scope);
delta = ExprPtrT(new ExprInteger(1));
if (arg[1]->nargs()>3) delta = arg[1]->arg[3]->evaluate(scope);
EXPR_EVAL_ASSERT(step->numberQ() && end->numberQ() && delta->numberQ(), IteratorExpectNumber, this)
scope->push();
scope->define_local(arg[1]->arg[0]->symbolname(), step);
value = arg[0]->evaluate(scope);
scope->pop();
status = Defd;
};
// automatic dependency ordering
ordering.push_front(id);
};
// Iteration initialization evaluation call after initializing
if (status == Defd) {return value;}
EXPR_EVAL_ASSERT(false, IteratorInternalError, this)
return ExprNullPtr();
};
ExprSyntaxErrorT check_syntax() const
{
if (nargs() == 1) return NoSyntaxError;
if (nargs() == 2)
{
if (!(arg[1]->listQ() && (arg[1]->nargs() == 4 || arg[1]->nargs()==3))) return IteratorExpectIterationList;
if (!(arg[1]->arg[0]->symbolQ())) return IteratorExpectSymbol;
return NoSyntaxError;
};
return IteratorSyntaxError;
};
};
class ExprIteratorIter : public Expression
{
public:
ExprIteratorIter() : Expression() {};
public:
EXPR_NAME_DECL()
ExprPtrT evaluate(ExprScopeT* scope)
{
EXPR_EVAL_CHECK_SYNTAX()
if (ExprIterator::counter_status == ExprIterator::CDefd) {
return ExprPtrT(new ExprInteger(ExprIterator::count));
} else {
return ExprPtrT(this);
}
};
ExprSyntaxErrorT check_syntax() const
{
if (nargs() != 0) return IllegalArgumentNumber;
else return NoSyntaxError;
}
};
class ExprIteratorTotal : public Expression
{
public:
ExprIteratorTotal() : Expression() {};
public:
EXPR_NAME_DECL()
ExprPtrT evaluate(ExprScopeT* scope)
{
EXPR_EVAL_CHECK_SYNTAX()
if (ExprIterator::counter_status == ExprIterator::CDefd) {
return ExprPtrT(new ExprInteger(ExprIterator::n_iters));
} else {
return ExprPtrT(this);
}
//if (ExprIterator::n_iters<0) return ExprPtrT(this);
//return ExprPtrT(new ExprInteger(ExprIterator::n_iters));
}
ExprSyntaxErrorT check_syntax() const
{
if (nargs() != 0) return IllegalArgumentNumber;
else return NoSyntaxError;
}
};
#endif