forked from jdah/jdh-8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.c
148 lines (125 loc) · 3.69 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
#include "asm.h"
static i64 eval_exp_op(
struct Context *ctx, struct Token *op, i64 lhs, i64 rhs
) {
switch (op->kind) {
case TK_MINUS:
if (lhs == EVAL_EXP_UNDEFINED) {
return -rhs;
}
return lhs - rhs;
case TK_NOT:
assert(lhs == EVAL_EXP_UNDEFINED);
return ~rhs;
case TK_PLUS: return lhs + rhs;
case TK_AMPERSAND: return lhs & rhs;
case TK_PIPE: return lhs | rhs;
case TK_STAR: return lhs * rhs;
case TK_CARET: return lhs ^ rhs;
case TK_SLASH: return lhs / rhs;
case TK_LESS: return lhs << rhs;
case TK_GREATER: return lhs >> rhs;
default:
asmchk(false, ctx, op, "Invalid operation");
}
assert(false);
return EVAL_EXP_UNDEFINED;
}
i64 eval_exp(struct Context *ctx, struct Token *start, struct Token *end) {
assert(start->kind == TK_LPAREN);
assert(end->kind == TK_RPAREN);
asmchk((usize) (start - end) > 1, ctx, start, "Empty expression");
i64 acc = EVAL_EXP_UNDEFINED, tmp = EVAL_EXP_UNDEFINED;
struct Token *op = NULL, *t = start->next, *u = NULL;
while (t != end) {
switch (t->kind) {
case TK_NUMBER:
tmp = t->value;
break;
case TK_LPAREN:
u = t;
usize depth = 0;
while (u != end) {
if (u->kind == TK_LPAREN) {
depth++;
} else if (u->kind == TK_RPAREN) {
depth--;
}
if (depth == 0) {
break;
}
u = u->next;
}
asmchk(depth == 0, ctx, u, "Unbalanced expression");
tmp = eval_exp(ctx, t, u);
t = u;
break;
default:
asmchk(!op, ctx, t, "Double operation");
op = t;
t = t->next;
// do not attempt to eval, wait for rhs
continue;
}
if (op) {
acc = eval_exp_op(ctx, op, acc, tmp);
op = NULL;
} else {
acc = tmp;
}
tmp = EVAL_EXP_UNDEFINED;
t = t->next;
}
assert(acc != EVAL_EXP_UNDEFINED);
asmchk(
(acc < 0 && acc >= INT16_MIN) ||
(acc >= 0 && acc <= 0xFFFF),
ctx, start, "Expression out of bounds (value is %" PRId64 ")",
acc
);
asmdbg(
ctx, NULL,
"Evaluated %s to %" PRIu16,
token_between(start, end, NULL, 0), acc
);
return acc;
}
// evaluates expressions between the two tokens (inclusive),
// leaving other tokens alone
// returns erroneous token on error, otherwise returns NULL
struct Token *eval_between(
struct Context *ctx,
struct Token *start,
struct Token *end
) {
struct Token *tk = start;
while (tk != end->next) {
if (tk->kind != TK_LPAREN) {
tk = tk->next;
continue;
}
struct Token *end = tk;
usize depth = 0;
while (end->kind != TK_EOL) {
if (end->kind == TK_LPAREN) {
depth++;
} else if (end->kind == TK_RPAREN) {
depth--;
}
if (depth == 0) {
break;
}
end = end->next;
}
if (depth != 0) {
return tk;
}
i64 value = eval_exp(ctx, tk, end);
tk->kind = TK_NUMBER;
tk->value = value;
// skip tokens in between
tk->next = end->next;
tk = end->next;
}
return NULL;
}