-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.c
744 lines (654 loc) · 24.2 KB
/
eval.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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*************************************************************************
> File Name: eval.c
> Author: weapon
> Mail: [email protected]
> Created Time: 2016年07月11日 星期一 22时29分53秒
************************************************************************/
/* 表达式求值 */
#include <math.h>
#include <string.h>
#include "ORG.h"
#include "origin.h"
#include "ORG_dev.h"
//布尔表达式
static ORG_Value eval_boolean_expression(ORG_Boolean boolean_value) {
ORG_Value v;
v.type = ORG_BOOLEAN_VALUE;
v.u.boolean_value = boolean_value;
return v;
}
static ORG_Value eval_int_expression(int int_value) {
ORG_Value v;
v.type = ORG_INT_VALUE;
v.u.int_value = int_value;
return v;
}
static ORG_Value eval_double_expression(double double_value) {
ORG_Value v;
v.type = ORG_DOUBLE_VALUE;
v.u.double_value = double_value;
return v;
}
static ORG_Value eval_string_expression(ORG_Interpreter *inter, char *string_value) {
ORG_Value v;
v.type = ORG_STRING_VALUE;
v.u.string_value = org_literal_to_org_string(inter, string_value);
return v;
}
static ORG_Value eval_null_expression(void) {
ORG_Value v;
v.type = ORG_NULL_VALUE;
return v;
}
//大数表达式
static ORG_Value eval_bignum_expression(ORG_Interpreter *inter, LocalEnvironment *env, Expression *expr) {
ORG_Value value;
value.type = ORG_BIGNUM_VALUE;
value.u.big_num = expr->u.big_num;
return value;
}
static void refer_if_string(ORG_Value *v) {
if (v->type == ORG_STRING_VALUE) {
org_refer_string(v->u.string_value);
}
}
static void release_if_string(ORG_Value *v) {
if (v->type == ORG_STRING_VALUE) {
org_release_string(v->u.string_value);
}
}
static Variable *search_global_variable_form_env(ORG_Interpreter *inter, LocalEnvironment *env, char *name) {
GlobalVariableRef *pos;
if (env == NULL) {
return org_search_global_variable(inter, name);
}
for (pos = env->global_variable; pos; pos = pos->next) {
if (!strcmp(pos->variable->name, name)) {
return pos->variable;
}
}
return NULL;
}
//变量名出现在表达式中,此函数被调用
static ORG_Value eval_identifier_expression(ORG_Interpreter *inter, LocalEnvironment *env, Expression *expr) {
ORG_Value v;
Variable *vp;
//首先查找局部变量
vp = org_search_local_variable(env, expr->u.identifier);
if (vp != NULL) {
v = vp->value;
} else {
//如果没有,则通过全局查找
vp = search_global_variable_form_env(inter, env, expr->u.identifier);
if (vp != NULL) {
v = vp->value;
} else {
//仍然没有则报错
//runtime error
exit(1);
}
}
refer_if_string(&v);
return v;
}
static ORG_Value eval_expression(ORG_Interpreter *inter, LocalEnvironment *env, Expression *expr);
/**
* 变量赋值时调用
* identifier
*/
static ORG_Value eval_assign_expression(ORG_Interpreter *inter, LocalEnvironment *env, char *identifier, Expression *expression) {
ORG_Value v;
Variable *left;
//获得右侧表达式结果
v = eval_expression(inter, env, expression);
//查找局部变量
left = org_search_local_variable(env, identifier);
if (left == NULL) {//找不到在全局查找
left = search_global_variable_form_env(inter, env, identifier);
}
if (left != NULL) {
release_if_string(&left->value);
left->value = v;
refer_if_string(&v);
} else { // 没有找到变量,注册新的变量
if (env != NULL) {
// 函数内的布局变量
org_add_local_variable(env, identifier, &v);
} else {
// 函数外的全局变量
ORG_add_global_variable(inter, identifier, &v);
}
refer_if_string(&v);
}
return v;
}
static ORG_Boolean eval_binary_boolean(ORG_Interpreter *inter, ExpressionType op, ORG_Boolean left,
ORG_Boolean right, int line_number) {
ORG_Boolean result;
if (op == EQ_EXPRESSION) {
result = (left == right);
} else if (op == NE_EXPRESSION) {
result = (left != right);
} else {
char *op_str = org_get_operator_string(op);
//run time error
}
return result;
}
static void eval_binary_int(ORG_Interpreter *inter, ExpressionType op,
int left, int right, ORG_Value *result, int line_number) {
if (dkc_is_math_operator(op)) {
result->type = ORG_INT_VALUE;
} else if (dkc_is_compare_operator(op)) {
result->type = ORG_BOOLEAN_VALUE;
} else {
//debug
}
switch (op) {
case BOOLEAN_EXPRESSION:
case INT_EXPRESSION:
case DOUBLE_EXPRESSION:
case STRING_EXPRESSION:
case IDENTIFIER_EXPRESSION:
case ASSIGN_EXPRESSION:
//debug
break;
case ADD_EXPRESSION:
result->u.int_value = left + right;
break;
case SUB_EXPRESSION:
result->u.int_value = left - right;
break;
case MUL_EXPRESSION:
result->u.int_value = left * right;
break;
case DIV_EXPRESSION:
result->u.int_value = left / right;
case MOD_EXPRESSION:
result->u.int_value = left % right;
break;
case LOGICAL_AND_EXPRESSION:
case LOGICAL_OR_EXPRESSION:
//debug
break;
case EQ_EXPRESSION:
result->u.int_value = left == right;
break;
case NE_EXPRESSION:
result->u.boolean_value = left != right;
break;
case GT_EXPRESSION:
result->u.boolean_value = left > right;
break;
case GE_EXPRESSION:
result->u.boolean_value = left >= right;
break;
case LT_EXPRESSION:
result->u.boolean_value = left < right;
break;
case LE_EXPRESSION:
result->u.boolean_value = left <= right;
break;
case BIT_LEFT_EXPRESSION:
result->u.int_value = left << right;
break;
case BIT_RIGHT_EXPRESSION:
result->u.int_value = left >> right;
break;
case BIT_OR_EXPRESSION:
result->u.int_value = left | right;
break;
case BIT_AND_EXPRESSION:
result->u.int_value = left & right;
break;
case BIT_XOR_EXPRESSION:
result->u.int_value = left ^ right;
break;
case MINUS_EXPRESSION: /* FALLTHRU */
case FUNCTION_CALL_EXPRESSION: /* FALLTHRU */
case NULL_EXPRESSION: /* FALLTHRU */
case EXPRESSION_TYPE_COUNT_PLUS_1: /* FALLTHRU */
default:
printf("");
//DBG_panic(("bad case...%d", operator));
}
}
static void eval_binary_double(ORG_Interpreter *inter, ExpressionType op, double left, double right,
ORG_Value *result, int line_number) {
if (dkc_is_math_operator(op)) {
result->type = ORG_BOOLEAN_VALUE;
} else if (dkc_is_compare_operator(op)) {
result->type = ORG_BOOLEAN_VALUE;
} else {
//debug
}
switch (op) {
case BOOLEAN_EXPRESSION: /* fail */
case INT_EXPRESSION: /* fail */
case DOUBLE_EXPRESSION: /* fail */
case STRING_EXPRESSION: /* fail */
case IDENTIFIER_EXPRESSION: /* fail */
case ASSIGN_EXPRESSION:
//DBG_panic(("bad case...%d", operator));
break;
case ADD_EXPRESSION:
result->u.double_value = left + right;
break;
case SUB_EXPRESSION:
result->u.double_value = left - right;
break;
case MUL_EXPRESSION:
result->u.double_value = left * right;
break;
case DIV_EXPRESSION:
result->u.double_value = left / right;
break;
case MOD_EXPRESSION:
result->u.double_value = fmod(left, right);
break;
case LOGICAL_AND_EXPRESSION: /* fail */
case LOGICAL_OR_EXPRESSION:
//DBG_panic(("bad case...%d", operator));
break;
case EQ_EXPRESSION:
result->u.int_value = left == right;
break;
case NE_EXPRESSION:
result->u.int_value = left != right;
break;
case GT_EXPRESSION:
result->u.int_value = left > right;
break;
case GE_EXPRESSION:
result->u.int_value = left >= right;
break;
case LT_EXPRESSION:
result->u.int_value = left < right;
break;
case LE_EXPRESSION:
result->u.int_value = left <= right;
break;
case MINUS_EXPRESSION: /* fail */
case FUNCTION_CALL_EXPRESSION: /* fail */
case NULL_EXPRESSION: /* fail */
case EXPRESSION_TYPE_COUNT_PLUS_1: /* fail */
default:
printf("");
//DBG_panic(("bad default...%d", operator));
}
}
static ORG_Boolean eval_compare_string(ExpressionType op, ORG_Value *left, ORG_Value *right, int line_number) {
ORG_Boolean result;
int cmp;
cmp = strcmp(left->u.string_value, right->u.string_value);
if (op == EQ_EXPRESSION) {
result = (cmp == 0);
} else if (op == NE_EXPRESSION) {
result = (cmp != 0);
} else if (op == GT_EXPRESSION) {
result = (cmp > 0);
} else if (op == GE_EXPRESSION) {
result = (cmp >= 0);
} else if (op == LT_EXPRESSION) {
result = (cmp < 0);
} else if (op == LE_EXPRESSION) {
result = (cmp <= 0);
} else {
char *op_str = org_get_operator_string(op);
//runtime error
}
org_release_string(left->u.string_value);
org_release_string(right->u.string_value);
return result;
}
static ORG_Boolean eval_binary_null(ORG_Interpreter *inter, ExpressionType op, ORG_Value *left, ORG_Value *right, int line_number) {
ORG_Boolean result;
if (op == EQ_EXPRESSION) {
result = (left->type == ORG_NULL_VALUE && right->type == ORG_NULL_VALUE);
} else if (op == NE_EXPRESSION) {
result = !(left->type == ORG_NULL_VALUE && right->type == ORG_NULL_VALUE);
} else {
char *op_str = org_get_operator_string(op);
//runtime error
}
release_if_string(left);
release_if_string(right);
return result;
}
ORG_String *chain_string(ORG_Interpreter *inter, ORG_String *left, ORG_String *right) {
//like "123" + "adc"
int len;
char *str;
ORG_String *ret;
len = strlen(left->string) + strlen(right->string);
str = MEM_malloc(len + 1);
strcpy(str, left->string);
strcat(str, right->string);
ret = org_create_origin_string(inter, str);
org_release_string(left);
org_release_string(right);
return ret;
}
/**
* 函数对所有二元运算进行评估
*/
ORG_Value org_eval_binary_expression(ORG_Interpreter *inter, LocalEnvironment *env,
ExpressionType op, Expression *left, Expression *right) {
ORG_Value left_val;
ORG_Value right_val;
ORG_Value result;
left_val = eval_expression(inter, env, left);
right_val = eval_expression(inter, env, right);
//printf("%s\n", right_val.type);
/**
* 对于加法运算
* 1 整数 + 整数 返回整数
* 2 实数 + 实数 返回实数
* 3 整数 + 实数 左边转换为实数
* 4 实数 + 整数 右边转换为实数
* 5 字符串 + 字符串 返回连接后的字符串
* 6 字符串 + 整数 右边转换为字符串
* 7 字符串 + 实数 右边转换为字符串
* 8 字符串 + 布尔 右边转换为true或false的字符串
* 9 字符串 + null 右边转换为null的字符串
*/
if (left_val.type == ORG_INT_VALUE && right_val.type == ORG_INT_VALUE) { // 1 + 2
eval_binary_int(inter, op, left_val.u.int_value, right_val.u.int_value, &result, left->line_number);
} else if (left_val.type == ORG_DOUBLE_VALUE && right_val.type == ORG_DOUBLE_VALUE) { // 1.0 + 2.3
eval_binary_double(inter, op , left_val.u.double_value, right_val.u.double_value, &result, left->line_number);
} else if (left_val.type == ORG_INT_VALUE && right_val.type == ORG_DOUBLE_VALUE) { // 1 + 2.0
left_val.u.double_value = (double) left_val.u.int_value;
eval_binary_double(inter, op , left_val.u.double_value, right_val.u.double_value, &result, left->line_number);
} else if (left_val.type == ORG_DOUBLE_VALUE && right_val.type == ORG_INT_VALUE) { // 3.0 + 1
right_val.u.double_value = (double) right_val.u.int_value;
eval_binary_double(inter, op , left_val.u.double_value, right_val.u.double_value, &result, left->line_number);
} else if (left_val.type == ORG_STRING_VALUE && op == ADD_EXPRESSION) {
char buf[LINE_BUF_SIZE];
ORG_String *right_str;
if (right_val.type == ORG_INT_VALUE) {
sprintf(buf, "%d", right_val.u.int_value);
right_str = org_create_origin_string(inter, MEM_strdup(buf));
} else if(right_val.type == ORG_DOUBLE_VALUE) {
sprintf(buf, "%lf", right_val.u.double_value);
right_str = org_create_origin_string(inter, MEM_strdup(buf));
} else if (right_val.type == ORG_BOOLEAN_VALUE) {
right_str = (right_val.u.boolean_value) ? org_create_origin_string(inter, MEM_strdup("true")) : org_create_origin_string(inter, MEM_strdup("false"));
} else if (right_val.type == ORG_STRING_VALUE) {
right_str = right_val.u.string_value;
} else if (right_val.type == ORG_BIGNUM_VALUE) {
right_str = org_create_origin_string(inter, MEM_strdup(org_value_to_string(right_val)));
}
result.type = ORG_STRING_VALUE;
result.u.string_value = chain_string(inter, left_val.u.string_value, right_str);
} else if (left_val.type == ORG_BIGNUM_VALUE || right_val.type == ORG_BIGNUM_VALUE) {
//大数运算
org_eval_binary_bignum_expression(inter, env, op, left_val, right_val, &result, left->line_number);
}else if (left_val.type == ORG_BOOLEAN_VALUE && right_val.type == ORG_BOOLEAN_VALUE) {
//
result.type = ORG_BOOLEAN_VALUE;
result.u.boolean_value = eval_binary_boolean(inter, op, left_val.u.boolean_value, right_val.u.boolean_value, left->line_number);
} else if (left_val.type == ORG_STRING_VALUE && right_val.type == ORG_STRING_VALUE) {
// "12" > "abc"
result.type == ORG_DOUBLE_VALUE;
result.u.boolean_value = eval_compare_string(op, &left_val, &right_val, left->line_number);
} else if (left_val.type == ORG_NULL_VALUE || right_val.type == ORG_NULL_VALUE) {
result.type = ORG_NULL_VALUE;
result.u.boolean_value = eval_binary_null(inter, op, &left_val, &right_val, left->line_number);
} else {
char *op_str = org_get_operator_string(op);
//runtime error
exit(1);
}
return result;
}
void org_eval_binary_bignum_expression(ORG_Interpreter *inter, LocalEnvironment *env, ExpressionType op,
ORG_Value left, ORG_Value right, ORG_Value *result, int line_number) {
mpi left_val = org_create_origin_bignum(left);
mpi right_val = org_create_origin_bignum(right);
mpi_init(&result->u.big_num);
if (op == ADD_EXPRESSION) {
mpi_add_mpi(&result->u.big_num, &left_val, &right_val);
} else if (op == SUB_EXPRESSION) {
mpi_sub_mpi(&result->u.big_num, &left_val, &right_val);
} else if (op == MUL_EXPRESSION) {
mpi_mul_mpi(&result->u.big_num, &left_val, &right_val);
} else if (op == DIV_EXPRESSION) {
mpi temp;
mpi_init(&temp);// 存放余数
mpi_div_mpi(&result->u.big_num, &temp, &left_val, &right_val);
}else {
//不支持这种大数运算
printf("不支持这种大数运算");
exit(1);
}
result->type = ORG_BIGNUM_VALUE;
}
// 布尔运算 支持短路求值
static ORG_Value eval_logical_and_or_expression(ORG_Interpreter *inter, LocalEnvironment *env, ExpressionType op, Expression *left, Expression *right) {
ORG_Value left_val;
ORG_Value right_val;
ORG_Value result;
result.type = ORG_BOOLEAN_VALUE;
left_val = eval_expression(inter, env, left);
if (left_val.type != ORG_BOOLEAN_VALUE) {
//runtime error
exit(1);
}
if (op == LOGICAL_AND_EXPRESSION) {
if (!left_val.u.boolean_value) {
result.u.boolean_value = ORG_FALSE;
return result;//短路求值
}
} else if (op == LOGICAL_OR_EXPRESSION) {
if (left_val.u.boolean_value) {
result.u.boolean_value = ORG_TRUE;
return result;
}
} else {
//debug 不是布尔运算符
}
right_val = eval_expression(inter, env, right);
if (right_val.type != ORG_BOOLEAN_VALUE) {
//runtime error
exit(1);
}
result.u.boolean_value = right_val.u.boolean_value;
return result;
}
// 单元取反
ORG_Value org_eval_minus_expression(ORG_Interpreter *inter, LocalEnvironment *env, Expression *operand) {
ORG_Value operand_val; //操作数
ORG_Value result;
operand_val = eval_expression(inter, env, operand);
if (operand_val.type == ORG_INT_VALUE) {
result.type = ORG_INT_VALUE;
result.u.int_value = - operand_val.u.int_value;
} else if (operand_val.type == ORG_DOUBLE_VALUE) {
result.type = ORG_DOUBLE_VALUE;
result.u.double_value = -operand_val.u.double_value;
} else {
//runtime error
exit(1);
}
return result;
}
static LocalEnvironment *alloc_local_environment() {
LocalEnvironment *ret;
ret = MEM_malloc(sizeof(LocalEnvironment));
ret->variable = NULL;
ret->global_variable = NULL;
return ret;
}
//处理
static void dispose_local_environment(ORG_Interpreter *inter, LocalEnvironment *env) {
while (env->variable) {
Variable *temp;
temp = env->variable;
if (env->variable->value.type == ORG_STRING_VALUE) {
org_release_string(env->variable->value.u.string_value);
}
env->variable = temp->next;
MEM_free(temp);
}
while (env->global_variable) {
GlobalVariableRef *ref;
ref = env->global_variable;
env->global_variable = ref->next;
MEM_free(ref);
}
MEM_free(env);
}
static ORG_Value call_native_function(ORG_Interpreter *inter, LocalEnvironment *env, Expression *expr, ORG_NativeFunctionPro *proc) {
//proc 是个函数指针
ORG_Value value;
int arg_count;
ArgumentList *arg_p;
ORG_Value *args;
int i;
for (arg_count = 0, arg_p = expr->u.function_call_expression.argument; arg_p; arg_p = arg_p->next) {
arg_count++;
}
args = MEM_malloc(sizeof(ORG_Value) * arg_count);
for (i = 0, arg_p = expr->u.function_call_expression.argument; arg_p; arg_p = arg_p->next) {
args[i] = eval_expression(inter, env, arg_p->expression);
i++;
}
value = proc(inter, arg_count, args);
for (i = 0; i < arg_count; i++) {
release_if_string(&args[i]);
}
MEM_free(args);
return value;
}
// 函数调用
static ORG_Value call_origin_function(ORG_Interpreter *inter, LocalEnvironment *env, Expression *expr, FunctionDefine *func) {
ORG_Value value;
StatementResult result;
ArgumentList *arg_p;
ParameterList *param_p;
LocalEnvironment *local_env;
//开辟空间用于存放被调用的函数的局部变量
local_env = alloc_local_environment();
/**
* 对参数进行评估,并用于存放到函数的局部变量中
* arg_p : 指向函数调用的实参链表
* param_p : 指向函数定义的形参链表
*/
for (arg_p = expr->u.function_call_expression.argument, param_p = func->u.origin_f.parameter;
arg_p;
arg_p = arg_p->next, param_p = param_p->next) {
ORG_Value arg_val;
if (param_p == NULL) { // param_p被用尽,说明实参过多
//runtime error
exit(1);
}
arg_val = eval_expression(inter, env, arg_p->expression);
org_add_local_variable(local_env, param_p->name, &arg_val);
}
if (param_p) { // param_p 剩余,说明实参数量不够
//runtime error
exit(1);
}
//执行函数内部的语句
result = org_execute_statement_list(inter, local_env, func->u.origin_f.block->statement_list);
if (result.type == RETURN_STATEMENT_RESULT) {
value = result.u.return_value;
} else {
//若函数没有return,则默认有null的返回
value.type = ORG_NULL_VALUE;
}
dispose_local_environment(inter, local_env);
return value;
}
/**
* 调用函数时执行
*/
static ORG_Value eval_function_call_expression(ORG_Interpreter *inter, LocalEnvironment *env, Expression *expr) {
ORG_Value value;
FunctionDefine *func;
char *identifier = expr->u.function_call_expression.identifier;
func = org_search_function(identifier);
if (func == NULL) {
//runtime error
exit(1);
}
switch (func->type) {
case ORIGIN_FUNCTION_DEF:
value = call_origin_function(inter, env, expr, func);
break;
case NATIVE_FUNCTION_DEF:
value = call_native_function(inter, env, expr, func->u.native_f.pro);
break;
default:
break;
}
return value;
}
// 运行表达式 或者表达式结果
static ORG_Value eval_expression(ORG_Interpreter *inter, LocalEnvironment *env, Expression *expr) {
ORG_Value v;
switch (expr->type) {
case BOOLEAN_EXPRESSION:
v = eval_boolean_expression(expr->u.boolean_value);
break;
case INT_EXPRESSION:
v = eval_int_expression(expr->u.int_value);
break;
case DOUBLE_EXPRESSION:
v = eval_double_expression(expr->u.double_value);
break;
case STRING_EXPRESSION:
v = eval_string_expression(inter, expr->u.string_value);
break;
case IDENTIFIER_EXPRESSION:
v = eval_identifier_expression(inter, env, expr);
break;
case ASSIGN_EXPRESSION:
v = eval_assign_expression(inter, env, expr->u.assign_expression.variable, expr->u.assign_expression.operand);
break;
case ADD_EXPRESSION: /* fail */
case SUB_EXPRESSION: /* fail */
case MUL_EXPRESSION: /* fail */
case DIV_EXPRESSION: /* fail */
case MOD_EXPRESSION: /* fail */
case EQ_EXPRESSION: /* fail */
case NE_EXPRESSION: /* fail */
case GT_EXPRESSION: /* fail */
case GE_EXPRESSION: /* fail */
case LT_EXPRESSION: /* fail */
case LE_EXPRESSION:
case BIT_LEFT_EXPRESSION:
case BIT_RIGHT_EXPRESSION:
case BIT_OR_EXPRESSION:
case BIT_AND_EXPRESSION:
case BIT_XOR_EXPRESSION:
v = org_eval_binary_expression(inter, env, expr->type, expr->u.binary_expression.left, expr->u.binary_expression.right);
break;
case LOGICAL_AND_EXPRESSION:/* FALLTHRU */
case LOGICAL_OR_EXPRESSION:
v = eval_logical_and_or_expression(inter, env, expr->type, expr->u.binary_expression.left, expr->u.binary_expression.right);
break;
case MINUS_EXPRESSION:
v = org_eval_minus_expression(inter, env, expr->u.minus_expression);
break;
case FUNCTION_CALL_EXPRESSION:
v = eval_function_call_expression(inter, env, expr);
break;
case NULL_EXPRESSION:
v = eval_null_expression();
break;
case BIG_DATA_EXPRESSION:
// 这里需要加eval_bignum_expression()
v = eval_bignum_expression(inter, env, expr);
break;
case EXPRESSION_TYPE_COUNT_PLUS_1: /* FALLTHRU */
default:
//DBG_panic(("bad case. type..%d\n", expr->type));
printf("bad case. type..%d\n", expr->type);
break;
}
return v;
}
ORG_Value org_eval_expression(ORG_Interpreter *inter, LocalEnvironment *env, Expression *expr) {
return eval_expression(inter, env, expr);
}