-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.c
85 lines (64 loc) · 1.11 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
#include "visitor.h"
#include "common.h"
#define RET_TYPE f32 *
#define ARG p_res
VISITOR_DEF(AST, eval, RET_TYPE);
f32 ast_eval(AST_t *node) {
f32 ARG;
VISITOR_DISPATCH(AST, eval, node, &ARG);
return ARG;
}
VISIT(EXPR_INT) {
RETURN(node->value);
}
VISIT(EXPR_FLT) {
RETURN(node->value);
}
VISIT(EXPR_BIN) {
f32 lhs = ast_eval(node->lhs);
f32 rhs = ast_eval(node->rhs);
switch (node->op) {
case OP_ADD: RETURN(lhs + rhs); break;
case OP_SUB: RETURN(lhs - rhs); break;
case OP_MUL: RETURN(lhs * rhs); break;
case OP_DIV: RETURN(lhs / rhs); break;
default: UNREACHABLE;
}
}
VISIT(EXPR_UNR) {
f32 sub = ast_eval(node->sub);
switch (node->op) {
case OP_NEG: *p_res = -sub; break;
default: UNREACHABLE;
}
}
VISIT(EXPR_IDEN) {
}
VISIT(STMT_RET) {
}
VISIT(STMT_WHLE) {
}
VISIT(STMT_IFTE) {
}
VISIT(STMT_SCOP) {
}
VISIT(EXPR_DOT) {
}
VISIT(EXPR_ASS) {
}
VISIT(CONS_PROG) {
}
VISIT(CONS_FUN) {
}
VISIT(DECL_VAR) {
}
VISIT(EXPR_ARR) {
}
VISIT(STMT_EXPR) {
}
VISIT(EXPR_CALL) {
}
VISIT(DECL_TYP) {
}
VISIT(CONS_SPEC) {
}