statement.c
9.79 KB
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
#include <malloc.h>
#include <stdio.h>
#include <expValue.h>
#include <ident.h>
#include <evalExpr.h>
#include <evalCond.h>
#include <variable.h>
#include <assign.h>
#include <cast.h>
#include <tepal_pars.h>
#include <block.h>
#include <statement.h>
/*
* Deklaration und Definition
*/
#include <stmtQueue.h>
struct stmt
{
int sId;
s_stmtQueue * sQueue;
u_stmtType sConst;
int sConstTyp;
};
/*
* statische Funktionen (interner gebrauch)
*/
static
inline
u_stmtType
stmtEval (s_stmt * stmt, s_block * actBlock, int op)
{
s_stmtQueue * args = stmt->sQueue;
s_expVal * op1 = stmtDo (stmtQueueGet (args, 0), actBlock).eVal,
* op2 = stmtDo (stmtQueueGet (args, 1), actBlock).eVal,
* ret;
ret = evalExpr (op, op1, op2);
if (op1 != NULL) expValueFree (op1);
if (op2 != NULL) expValueFree (op2);
return (u_stmtType) ret;
}
static
inline
u_stmtType
stmtIdentVar (s_stmt * stmt, s_block * actBlock)
{
s_ident * ret = NULL;
s_stmtQueue * args = stmt->sQueue;
s_expVal * _op = stmtDo (stmtQueueGet (args, 0), actBlock).eVal;
char * op = expValueString (_op);
s_block * run = actBlock;
while (ret == NULL && run != NULL)
{
ret = identListSeekKey (blockIdl (run), op);
run = blockPrev (run);
}
if (ret == NULL)
ret = identListPutVal (blockIdl (actBlock), identUndefNew (-1, op));
expValueFree (_op);
free (op);
return (u_stmtType) ret;
}
static
inline
u_stmtType
stmtIdentArray (s_stmt * stmt, s_block * actBlock)
{
s_stmtQueue * args = stmt->sQueue;
s_ident * op1 = stmtDo (stmtQueueGet (args, 0), actBlock).idVal;
s_expVal * op2 = stmtDo (stmtQueueGet (args, 1), actBlock).eVal;
s_ident * ret;
ret = getArray (op1, op2);
if (op2 != NULL) expValueFree (op2);
return (u_stmtType) ret;
}
static
inline
u_stmtType
stmtIdentVal (s_stmt * stmt, s_block * actBlock)
{
s_stmtQueue * args = stmt->sQueue;
s_ident * op1 = stmtDo (stmtQueueGet (args, 0), actBlock).idVal;
return (u_stmtType) identExp (op1);
}
static
inline
u_stmtType
stmtEvalComp (s_stmt * stmt, s_block * actBlock, int op)
{
s_stmtQueue * args = stmt->sQueue;
s_expVal * op1 = stmtDo (stmtQueueGet (args, 0), actBlock).eVal,
* op2 = stmtDo (stmtQueueGet (args, 1), actBlock).eVal;
int ret;
ret = evalComp (op, op1, op2);
if (op1 != NULL) expValueFree (op1);
if (op2 != NULL) expValueFree (op2);
return (u_stmtType) ret;
}
static
inline
u_stmtType
stmtAssign (s_stmt * stmt, s_block * actBlock)
{
s_stmtQueue * args = stmt->sQueue;
s_ident * op1 = stmtDo (stmtQueueGet (args, 0), actBlock).idVal;
s_expVal * op2 = stmtDo (stmtQueueGet (args, 1), actBlock).eVal,
* ret;
if (op1 == NULL)
op1 = getVariable (NULL, NULL);
ret = assign (op1, op2);
if (op2 != NULL) expValueFree (op2);
return (u_stmtType) ret;
}
static
inline
u_stmtType
stmtPrint (s_stmt * stmt, s_block * actBlock)
{
s_stmtQueue * args = stmt->sQueue;
s_expVal * op = stmtDo (stmtQueueGet (args, 0), actBlock).eVal;
if (op != NULL)
{
printExpr (op);
expValueFree (op);
}
return (u_stmtType) 0;
}
static
inline
u_stmtType
stmtEvalCondExpr (s_stmt * stmt, s_block * actBlock)
{
s_stmtQueue * args = stmt->sQueue;
s_expVal * op = stmtDo (stmtQueueGet (args, 0), actBlock).eVal;
int ret;
ret = evalCondExpr (op);
if (op != NULL) expValueFree (op);
return (u_stmtType) ret;
}
static
inline
u_stmtType
stmtEvalCond (s_stmt * stmt, s_block * actBlock, int op)
{
s_stmtQueue * args = stmt->sQueue;
int op1 = stmtDo (stmtQueueGet (args, 0), actBlock).cond;
switch (op)
{
case LOGAND:
{
int op2 = stmtDo (stmtQueueGet (args, 1), actBlock).cond;
return (u_stmtType) (op1 && op2);
}
case LOGOR:
{
int op2 = stmtDo (stmtQueueGet (args, 1), actBlock).cond;
return (u_stmtType) (op1 || op2);
}
case LOGNEG:
return (u_stmtType) (! op1);
}
}
static
inline
u_stmtType
stmtCastInt (s_stmt * stmt, s_block * actBlock)
{
s_stmtQueue * args = stmt->sQueue;
s_expVal * op = stmtDo (stmtQueueGet (args, 0), actBlock).eVal,
* ret;
ret = castExprToInt (op);
if (op != NULL) expValueFree (op);
return (u_stmtType) ret;
}
static
inline
u_stmtType
stmtCastFloat (s_stmt * stmt, s_block * actBlock)
{
s_stmtQueue * args = stmt->sQueue;
s_expVal * op = stmtDo (stmtQueueGet (args, 0), actBlock).eVal,
* ret;
ret = castExprToFloat (op);
if (op != NULL) expValueFree (op);
return (u_stmtType) ret;
}
static
inline
u_stmtType
stmtCastString (s_stmt * stmt, s_block * actBlock)
{
s_stmtQueue * args = stmt->sQueue;
s_expVal * op = stmtDo (stmtQueueGet (args, 0), actBlock).eVal,
* ret;
ret = castExprToString (op);
if (op != NULL) expValueFree (op);
return (u_stmtType) ret;
}
static
inline
u_stmtType
stmtBlock (s_stmt * stmt, s_block * actBlock)
{
unsigned int i;
s_stmtQueue * gIds = stmtQueueGet (stmt->sQueue, 0)->sQueue;
s_stmtQueue * args = stmtQueueGet (stmt->sQueue, 1)->sQueue;
s_block * gBlock = NULL;
gBlock = actBlock = blockPush (&actBlock, blockNew (args));
/* find the global block */
while (blockPrev (gBlock) != NULL)
gBlock = blockPrev (gBlock);
if (gIds != NULL)
{
for (i = 0; i < stmtQueueGetSize (gIds); i++)
{
s_expVal * tmp = stmtDo (stmtQueueGet (gIds, i), actBlock).eVal;
char * _id;
s_ident * id;
if (tmp == NULL)
exitError (0);
_id = expValueString (tmp);
id = identListSeekKey (blockGetIdl (gBlock), _id);
expValueFree (tmp);
free (_id);
if (id == NULL)
exitError (0);
blockSetNonLocalId (args, id);
}
}
blockDo (actBlock);
blockFree (actBlock);
return (u_stmtType) 0;
}
static
inline
u_stmtType
stmtIf (s_stmt * stmt, s_block * actBlock)
{
s_stmtQueue * args = stmt->sQueue;
int cond = stmtDo (stmtQueueGet (args, 0), actBlock).cond;
s_stmt * _do;
if (cond)
_do = stmtQueueGet (args, 1);
else
_do = stmtQueueGet (args, 2);
if (_do != NULL)
stmtDo (_do);
return (u_stmtType) 0;
}
static
inline
u_stmtType
stmtForeach (s_stmt * stmt, s_block * actBlock)
{
s_stmtQueue * args = stmt->sQueue;
s_expVal * _id = stmtDo (stmtQueueGet (args, 0), actBlock).eVal,
* _key = stmtDo (stmtQueueGet (args, 1), actBlock).eVal,
* _val = stmtDo (stmtQueueGet (args, 2), actBlock).eVal;
char * id = expValueString (_id);
char * key = expValueString (_key);
char * val = expValueString (_val);
printf ("[DEBUG]found foreach statement: id=%s, key=%s, val=%s\n",
id, key, val);
free (id);
free (key);
free (val);
expValueFree (_id);
expValueFree (_key);
expValueFree (_val);
return (u_stmtType) 0;
}
static
inline
u_stmtType
stmtRepeat (s_stmt * stmt, s_block * actBlock)
{
s_stmtQueue * args = stmt->sQueue;
s_expVal * _id = stmtDo (stmtQueueGet (args, 0), actBlock).eVal,
* _count = stmtDo (stmtQueueGet (args, 1), actBlock).eVal;
char * id = expValueString (_id);
int count = expValueInt (_count);
printf ("[DEBUG]found repeat statement: id=%s, count=%d\n", id, count);
free (id);
expValueFree (_id);
expValueFree (_count);
return (u_stmtType) 0;
}
/*
* Interface
*/
s_stmt *
stmtNew (int id, s_stmtQueue * queue, int conTyp, u_stmtType con)
{
s_stmt * new = (s_stmt *) malloc (sizeof (s_stmt));
new->sId = id;
new->sQueue = queue;
new->sConstTyp = conTyp;
new->sConst = con;
return new;
}
s_stmtQueue *
stmtGetArgs (s_stmt * stmt)
{
return stmt->sQueue;
}
u_stmtType
stmtGetVal (s_stmt * stmt)
{
return stmt->sConst;
}
int
stmtGetConstTyp (s_stmt * stmt)
{
return stmt->sConstTyp;
}
void
stmtFree (s_stmt * stmt)
{
if (stmt == NULL)
return;
switch (stmt->sConstTyp)
{
case STYP_NONE: stmtQueueFree (stmt->sQueue); break;
case STYP_EVAL: expValueFree (stmt->sConst.eVal); break;
case STYP_IDVAL: identFree (stmt->sConst.idVal); break;
}
free (stmt);
}
u_stmtType
stmtDo (s_stmt * stmt, s_block * actBlock)
{
if (stmt == NULL)
return (u_stmtType) 0;
switch (stmt->sId)
{
case STMT_CONST: return (u_stmtType) expValueClone (stmt->sConst.eVal);
case STMT_BLOCK: return stmtBlock (stmt, actBlock);
case STMT_PRINT: return stmtPrint (stmt, actBlock);
case STMT_IF: return stmtIf (stmt, actBlock);
case STMT_FOREACH: return stmtForeach (stmt, actBlock);
case STMT_REPEAT: return stmtRepeat (stmt, actBlock);
case STMT_ASSIGN: return stmtAssign (stmt, actBlock);
case STMT_UNSET: return (u_stmtType) 0;
case STMT_EVAL_PLUS: return stmtEval (stmt, actBlock, PLUS);
case STMT_EVAL_MINUS: return stmtEval (stmt, actBlock, MINUS);
case STMT_EVAL_TIMES: return stmtEval (stmt, actBlock, TIMES);
case STMT_EVAL_OVER: return stmtEval (stmt, actBlock, OVER);
case STMT_EVAL_MODULO: return stmtEval (stmt, actBlock, MODULO);
case STMT_EVAL_NEG: return stmtEval (stmt, actBlock, NEG);
case STMT_IDENT_VAR: return stmtIdentVar (stmt, actBlock);
case STMT_IDENT_ARRAY: return stmtIdentArray (stmt, actBlock);
case STMT_IDENT_VAL: return stmtIdentVal (stmt, actBlock);
case STMT_CAST_INT: return stmtCastInt (stmt, actBlock);
case STMT_CAST_FLOAT: return stmtCastFloat (stmt, actBlock);
case STMT_CAST_STRING: return stmtCastString (stmt, actBlock);
case STMT_COMP_EQ: return stmtEvalComp (stmt, actBlock, EQ);
case STMT_COMP_NE: return stmtEvalComp (stmt, actBlock, NE);
case STMT_COMP_LT: return stmtEvalComp (stmt, actBlock, LT);
case STMT_COMP_GT: return stmtEvalComp (stmt, actBlock, GT);
case STMT_COMP_LE: return stmtEvalComp (stmt, actBlock, LE);
case STMT_COMP_GE: return stmtEvalComp (stmt, actBlock, GE);
case STMT_COND_EXPR: return stmtEvalCondExpr (stmt, actBlock);
case STMT_COND_AND: return stmtEvalCond (stmt, actBlock, LOGAND);
case STMT_COND_OR: return stmtEvalCond (stmt, actBlock, LOGOR);
case STMT_COND_NEG: return stmtEvalCond (stmt, actBlock, LOGNEG);
}
}