-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vm.c
116 lines (110 loc) · 2.83 KB
/
Vm.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
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include <math.h>
#include "UGL.H"
STACK StackEval;
double Eval( LIST *Expr )
{
TOK T, A, B;
while (Expr != NULL)
{
T = Expr->Data;
Expr = Expr->Next;
if (T.Id == TOK_NUM || T.Id == TOK_NAME) {
Push(&StackEval, T);
}
else
{
if (T.Op != '@')
{
Pop(&StackEval, &B);
if (B.Id == TOK_NAME) {
B.Num = GetValue(B.Name);
B.Id = TOK_NUM;
}
}
Pop(&StackEval, &A);
if (A.Id == TOK_NAME && T.Op != '=') {
A.Num = GetValue(A.Name);
A.Id = TOK_NUM;
}
switch(T.Op)
{
case '+':
A.Num += B.Num;
break;
case '@':
A.Num = -A.Num;
break;
case '-':
A.Num -= B.Num;
break;
case '*':
A.Num *= B.Num;
break;
case '/':
A.Num /= B.Num;
break;
case '%':
A.Num = fmod(A.Num, B.Num);
break;
case '^':
A.Num = pow(A.Num, B.Num);
break;
case '(':
case ')':
break;
case '=':
SetValue(A.Name, B.Num);
A.Id = TOK_NUM;
A.Num = B.Num;
break;
case OP_EQ:
A.Num = A.Num == B.Num;
break;
case OP_NE:
A.Num = A.Num != B.Num;
break;
case OP_GE:
A.Num = A.Num >= B.Num;
break;
case OP_LE:
A.Num = A.Num <= B.Num;
break;
case ',':
A.Id = TOK_NUM;
A.Num = B.Num;
break;
}
Push(&StackEval, A);
}
}
Pop(&StackEval, &T);
if (T.Id == TOK_NAME) {
T.Num = GetValue(T.Name);
T.Id = TOK_NUM;
}
return T.Num;
}
void DoCmd( CMD *C )
{
while (C != NULL)
{
if (C->Id == CMD_EXPR)
Eval(C->Expr);
else if (C->Id == CMD_IF)
if (Eval(C->Expr))
DoCmd(C->C1);
else
DoCmd(C->C2);
else if (C->Id == CMD_WHILE)
while (Eval(C->Expr))
DoCmd(C->C1);
else if (C->Id == CMD_PRINT)
{
printf("%f\n", Eval(C->Expr));
}
C = C->Next;
}
}