annotate lwasm/lwval.c @ 247:d8b5ff5c4ef9 2.x

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