-
Notifications
You must be signed in to change notification settings - Fork 36
/
arithm.c
162 lines (149 loc) · 4.22 KB
/
arithm.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
#include <assert.h>
#include <mrsh/arithm.h>
#include <mrsh/ast.h>
#include <stdlib.h>
void mrsh_arithm_expr_destroy(struct mrsh_arithm_expr *expr) {
if (expr == NULL) {
return;
}
switch (expr->type) {
case MRSH_ARITHM_LITERAL:;
struct mrsh_arithm_literal *al = mrsh_arithm_expr_get_literal(expr);
free(al);
return;
case MRSH_ARITHM_VARIABLE:;
struct mrsh_arithm_variable *av = mrsh_arithm_expr_get_variable(expr);
free(av->name);
free(av);
return;
case MRSH_ARITHM_UNOP:;
struct mrsh_arithm_unop *au = mrsh_arithm_expr_get_unop(expr);
mrsh_arithm_expr_destroy(au->body);
free(au);
return;
case MRSH_ARITHM_BINOP:;
struct mrsh_arithm_binop *ab = mrsh_arithm_expr_get_binop(expr);
mrsh_arithm_expr_destroy(ab->left);
mrsh_arithm_expr_destroy(ab->right);
free(ab);
return;
case MRSH_ARITHM_COND:;
struct mrsh_arithm_cond *ac = mrsh_arithm_expr_get_cond(expr);
mrsh_arithm_expr_destroy(ac->condition);
mrsh_arithm_expr_destroy(ac->body);
mrsh_arithm_expr_destroy(ac->else_part);
free(ac);
return;
case MRSH_ARITHM_ASSIGN:;
struct mrsh_arithm_assign *aa = mrsh_arithm_expr_get_assign(expr);
free(aa->name);
mrsh_arithm_expr_destroy(aa->value);
free(aa);
return;
}
abort();
}
struct mrsh_arithm_literal *mrsh_arithm_literal_create(long value) {
struct mrsh_arithm_literal *al =
calloc(1, sizeof(struct mrsh_arithm_literal));
if (al == NULL) {
return NULL;
}
al->expr.type = MRSH_ARITHM_LITERAL;
al->value = value;
return al;
}
struct mrsh_arithm_variable *mrsh_arithm_variable_create(char *name) {
struct mrsh_arithm_variable *av =
calloc(1, sizeof(struct mrsh_arithm_variable));
if (av == NULL) {
return NULL;
}
av->expr.type = MRSH_ARITHM_VARIABLE;
av->name = name;
return av;
}
struct mrsh_arithm_unop *mrsh_arithm_unop_create(
enum mrsh_arithm_unop_type type, struct mrsh_arithm_expr *body) {
struct mrsh_arithm_unop *au =
calloc(1, sizeof(struct mrsh_arithm_unop));
if (au == NULL) {
return NULL;
}
au->expr.type = MRSH_ARITHM_UNOP;
au->type = type;
au->body = body;
return au;
}
struct mrsh_arithm_binop *mrsh_arithm_binop_create(
enum mrsh_arithm_binop_type type, struct mrsh_arithm_expr *left,
struct mrsh_arithm_expr *right) {
struct mrsh_arithm_binop *ab =
calloc(1, sizeof(struct mrsh_arithm_binop));
if (ab == NULL) {
return NULL;
}
ab->expr.type = MRSH_ARITHM_BINOP;
ab->type = type;
ab->left = left;
ab->right = right;
return ab;
}
struct mrsh_arithm_cond *mrsh_arithm_cond_create(
struct mrsh_arithm_expr *condition, struct mrsh_arithm_expr *body,
struct mrsh_arithm_expr *else_part) {
struct mrsh_arithm_cond *ac =
calloc(1, sizeof(struct mrsh_arithm_cond));
if (ac == NULL) {
return NULL;
}
ac->expr.type = MRSH_ARITHM_COND;
ac->condition = condition;
ac->body = body;
ac->else_part = else_part;
return ac;
}
struct mrsh_arithm_assign *mrsh_arithm_assign_create(
enum mrsh_arithm_assign_op op, char *name,
struct mrsh_arithm_expr *value) {
struct mrsh_arithm_assign *aa =
calloc(1, sizeof(struct mrsh_arithm_assign));
if (aa == NULL) {
return NULL;
}
aa->expr.type = MRSH_ARITHM_ASSIGN;
aa->op = op;
aa->name = name;
aa->value = value;
return aa;
}
struct mrsh_arithm_literal *mrsh_arithm_expr_get_literal(
const struct mrsh_arithm_expr *expr) {
assert(expr->type == MRSH_ARITHM_LITERAL);
return (struct mrsh_arithm_literal *)expr;
}
struct mrsh_arithm_variable *mrsh_arithm_expr_get_variable(
const struct mrsh_arithm_expr *expr) {
assert(expr->type == MRSH_ARITHM_VARIABLE);
return (struct mrsh_arithm_variable *)expr;
}
struct mrsh_arithm_unop *mrsh_arithm_expr_get_unop(
const struct mrsh_arithm_expr *expr) {
assert(expr->type == MRSH_ARITHM_UNOP);
return (struct mrsh_arithm_unop *)expr;
}
struct mrsh_arithm_binop *mrsh_arithm_expr_get_binop(
const struct mrsh_arithm_expr *expr) {
assert(expr->type == MRSH_ARITHM_BINOP);
return (struct mrsh_arithm_binop *)expr;
}
struct mrsh_arithm_cond *mrsh_arithm_expr_get_cond(
const struct mrsh_arithm_expr *expr) {
assert(expr->type == MRSH_ARITHM_COND);
return (struct mrsh_arithm_cond *)expr;
}
struct mrsh_arithm_assign *mrsh_arithm_expr_get_assign(
const struct mrsh_arithm_expr *expr) {
assert(expr->type == MRSH_ARITHM_ASSIGN);
return (struct mrsh_arithm_assign *)expr;
}