ref: ld-export mrsh/arithm.c -rw-r--r-- 3.6 KiB View raw
                                                                                
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
#include <assert.h>
#include <mrsh/arithm.h>
#include <mrsh/ast.h>
#include <stdlib.h>

void mrsh_arithm_expr_destroy(struct mrsh_arithm_expr *expr) {
16 references
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_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; } assert(false); } struct mrsh_arithm_literal *mrsh_arithm_literal_create(long value) {
1 reference
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_unop *mrsh_arithm_unop_create(
1 reference
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(
6 references
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(
1 reference
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(
1 reference
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(
1 reference
const struct mrsh_arithm_expr *expr) { assert(expr->type == MRSH_ARITHM_LITERAL); return (struct mrsh_arithm_literal *)expr; } struct mrsh_arithm_unop *mrsh_arithm_expr_get_unop(
1 reference
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(
1 reference
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(
1 reference
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(
1 reference
const struct mrsh_arithm_expr *expr) { assert(expr->type == MRSH_ARITHM_ASSIGN); return (struct mrsh_arithm_assign *)expr; }