annotate old-trunk/lwasm/old/lwval.c @ 348:11a95c6414b4

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