comparison src/lwval.c @ 15:1f598d89b9b0

Started creating expression parser
author lost
date Thu, 23 Oct 2008 03:37:48 +0000
parents src/expr.c@b28d7cb60779
children
comparison
equal deleted inserted replaced
14:b28d7cb60779 15:1f598d89b9b0
1 /*
2 lwval.c
3 Copyright © 2008 William Astle
4
5 This file is part of LWASM.
6
7 LWASM is free software: you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation, either version 3 of the License, or (at your option) any later
10 version.
11
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 more details.
16
17 You should have received a copy of the GNU General Public License along with
18 this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22 This file contains implementations associated with the expression evaluator
23 used by LWASM.
24
25 */
26
27 #include <malloc.h>
28
29 #define __lwval_c_seen__
30 #include "lwval.h"
31
32 LWVAL *lwval_construct_int(int value)
33 {
34 LWVAL *v;
35
36 v = malloc(sizeof(LWVAL));
37 if (!v)
38 return NULL;
39
40 v -> lwval_type = LWVAL_TYPE_INT;
41 v -> dt.lwval_int = value;
42
43 return v;
44 }
45
46 LWVAL *lwval_construct_err(int errno)
47 {
48 LWVAL *v;
49
50 v = malloc(sizeof(LWVAL));
51 if (!v)
52 return NULL;
53
54 v -> lwval_type = LWVAL_TYPE_ERR;
55 v -> dt.lwval_int = errno;
56
57 return v;
58 }
59
60 LWVAL *lwval_construct_nan(void)
61 {
62 LWVAL *v;
63
64 v = malloc(sizeof(LWVAL));
65 if (!v)
66 return NULL;
67
68 v -> lwval_type = LWVAL_TYPE_NAN;
69
70 return v;
71 }
72
73 LWVAL *lwval_construct_undef(void)
74 {
75 LWVAL *v;
76
77 v = malloc(sizeof(LWVAL));
78 if (!v)
79 return NULL;
80
81 v -> lwval_type = LWVAL_TYPE_UNDEF;
82
83 return v;
84 }
85
86 LWVAL *lwval_construct_expr(LWVAL *v1, LWVAL *v2, int op)
87 {
88 LWVAL *v;
89 v = malloc(sizeof(LWVAL));
90 if (!v)
91 return NULL;
92
93 v -> lwval_type = LWVAL_TYPE_EXPR;
94 v -> dt.expr.v1 = v1;
95 v -> dt.expr.v2 = v2;
96 v -> dt.expr.op = op;
97 return v;
98 }
99
100 void lwval_destroy(LWVAL *value)
101 {
102 if (value)
103 {
104 lwval_clear(value);
105 free(value);
106 }
107 }
108
109 // performs a deep copy of an LWVAL, including ALL referenced values
110 void lwval_dup(LWVAL *v1, LWVAL *v2)
111 {
112 lwval_clear(v2);
113
114 switch (v1 -> lwval_type)
115 {
116 case LWVAL_TYPE_INT:
117 case LWVAL_TYPE_ERR:
118 v2 -> dt.lwval_int = v1 -> dt.lwval_int;
119 break;
120
121 case LWVAL_TYPE_EXPR:
122 v2 -> dt.expr.op = v1 -> dt.expr.op;
123 if (v1 -> dt.expr.v1)
124 {
125 v2 -> dt.expr.v1 = lwval_construct_undef();
126 lwval_dup(v1 -> dt.expr.v1, v2 -> dt.expr.v1);
127 }
128 else
129 v2 -> dt.expr.v1 = NULL;
130 if (v1 -> dt.expr.v2)
131 {
132 v2 -> dt.expr.v2 = lwval_construct_undef();
133 lwval_dup(v1 -> dt.expr.v2, v2 -> dt.expr.v2);
134 }
135 else
136 v2 -> dt.expr.v2 = NULL;
137 break;
138 }
139
140 v2 -> lwval_type = v1 -> lwval_type;
141 }
142
143 void lwval_clear(LWVAL *value)
144 {
145 switch (value -> lwval_type)
146 {
147 case LWVAL_TYPE_EXPR:
148 lwval_destroy(value -> dt.expr.v1);
149 lwval_destroy(value -> dt.expr.v2);
150 break;
151 }
152 value -> lwval_type = LWVAL_TYPE_UNDEF;
153 }
154
155 // for integer, simply negate value
156 // for expr, change to "-1 * (expr)"
157 // everything else: error
158 LWVAL *lwval_neg(LWVAL *v1)
159 {
160 switch (v1 -> lwval_type)
161 {
162 case LWVAL_TYPE_INT:
163 v1 -> dt.lwval_int = -(v1 -> dt.lwval_int);
164 break;
165
166 case LWVAL_TYPE_EXPR:
167 {
168 LWVAL *v, *v2;
169 v = lwval_construct_undef();
170 lwval_dup(v1, v);
171 lwval_clear(v1);
172 v2 = lwval_construct_expr(lwval_construct_int(-1), v, '*');
173 lwval_dup(v2, v1);
174 lwval_destroy(v2);
175 }
176 break;
177
178 default:
179 lwval_clear(v1);
180 v1 -> lwval_type = LWVAL_TYPE_ERR;
181 v1 -> dt.lwval_int = 1;
182 }
183
184 return v1;
185 }
186
187 // v1 + v2 -> v1
188 LWVAL *lwval_add(LWVAL *v1, LWVAL *v2)
189 {
190 }
191
192 // v1 - v2 -> v1
193 LWVAL *lwval_sub(LWVAL *v1, LWVAL *v2)
194 {
195 }
196
197 // v1 * v2 -> v1
198 LWVAL *lwval_mul(LWVAL *v1, LWVAL *v2)
199 {
200 }
201
202 // v1 / v2 -> v1
203 LWVAL *lwval_div(LWVAL *v1, LWVAL *v2)
204 {
205 }
206
207 // v1 % v2 -> v1
208 LWVAL *lwval_mod(LWVAL *v1, LWVAL *v2)
209 {
210 }