annotate src/lwval.c @ 185:b89adfb0d174

Added support for outputting a linkmap
author lost
date Sat, 21 Mar 2009 19:47:45 +0000
parents 1f598d89b9b0
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 */
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
26
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
27 #include <malloc.h>
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
28
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
29 #define __lwval_c_seen__
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
30 #include "lwval.h"
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
31
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
32 LWVAL *lwval_construct_int(int value)
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
33 {
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
34 LWVAL *v;
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
35
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
36 v = malloc(sizeof(LWVAL));
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
37 if (!v)
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
38 return NULL;
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
39
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
40 v -> lwval_type = LWVAL_TYPE_INT;
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
41 v -> dt.lwval_int = value;
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
42
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
43 return v;
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
44 }
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
45
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
46 LWVAL *lwval_construct_err(int errno)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
47 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
48 LWVAL *v;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
49
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
50 v = malloc(sizeof(LWVAL));
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
51 if (!v)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
52 return NULL;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
53
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
54 v -> lwval_type = LWVAL_TYPE_ERR;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
55 v -> dt.lwval_int = errno;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
56
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
57 return v;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
58 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
59
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
60 LWVAL *lwval_construct_nan(void)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
61 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
62 LWVAL *v;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
63
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
64 v = malloc(sizeof(LWVAL));
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
65 if (!v)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
66 return NULL;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
67
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
68 v -> lwval_type = LWVAL_TYPE_NAN;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
69
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
70 return v;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
71 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
72
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
73 LWVAL *lwval_construct_undef(void)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
74 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
75 LWVAL *v;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
76
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
77 v = malloc(sizeof(LWVAL));
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
78 if (!v)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
79 return NULL;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
80
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
81 v -> lwval_type = LWVAL_TYPE_UNDEF;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
82
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
83 return v;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
84 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
85
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
86 LWVAL *lwval_construct_expr(LWVAL *v1, LWVAL *v2, int op)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
87 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
88 LWVAL *v;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
89 v = malloc(sizeof(LWVAL));
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
90 if (!v)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
91 return NULL;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
92
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
93 v -> lwval_type = LWVAL_TYPE_EXPR;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
94 v -> dt.expr.v1 = v1;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
95 v -> dt.expr.v2 = v2;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
96 v -> dt.expr.op = op;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
97 return v;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
98 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
99
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
100 void lwval_destroy(LWVAL *value)
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
101 {
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
102 if (value)
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
103 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
104 lwval_clear(value);
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
105 free(value);
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
106 }
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 // performs a deep copy of an LWVAL, including ALL referenced values
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
110 void lwval_dup(LWVAL *v1, LWVAL *v2)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
111 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
112 lwval_clear(v2);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
113
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
114 switch (v1 -> lwval_type)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
115 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
116 case LWVAL_TYPE_INT:
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
117 case LWVAL_TYPE_ERR:
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
118 v2 -> dt.lwval_int = v1 -> dt.lwval_int;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
119 break;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
120
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
121 case LWVAL_TYPE_EXPR:
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
122 v2 -> dt.expr.op = v1 -> dt.expr.op;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
123 if (v1 -> dt.expr.v1)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
124 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
125 v2 -> dt.expr.v1 = lwval_construct_undef();
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
126 lwval_dup(v1 -> dt.expr.v1, v2 -> dt.expr.v1);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
127 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
128 else
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
129 v2 -> dt.expr.v1 = NULL;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
130 if (v1 -> dt.expr.v2)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
131 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
132 v2 -> dt.expr.v2 = lwval_construct_undef();
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
133 lwval_dup(v1 -> dt.expr.v2, v2 -> dt.expr.v2);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
134 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
135 else
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
136 v2 -> dt.expr.v2 = NULL;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
137 break;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
138 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
139
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
140 v2 -> lwval_type = v1 -> lwval_type;
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
141 }
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
142
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
143 void lwval_clear(LWVAL *value)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
144 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
145 switch (value -> lwval_type)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
146 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
147 case LWVAL_TYPE_EXPR:
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
148 lwval_destroy(value -> dt.expr.v1);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
149 lwval_destroy(value -> dt.expr.v2);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
150 break;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
151 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
152 value -> lwval_type = LWVAL_TYPE_UNDEF;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
153 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
154
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
155 // for integer, simply negate value
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
156 // for expr, change to "-1 * (expr)"
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
157 // everything else: error
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
158 LWVAL *lwval_neg(LWVAL *v1)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
159 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
160 switch (v1 -> lwval_type)
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
161 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
162 case LWVAL_TYPE_INT:
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
163 v1 -> dt.lwval_int = -(v1 -> dt.lwval_int);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
164 break;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
165
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
166 case LWVAL_TYPE_EXPR:
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
167 {
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
168 LWVAL *v, *v2;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
169 v = lwval_construct_undef();
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
170 lwval_dup(v1, v);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
171 lwval_clear(v1);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
172 v2 = lwval_construct_expr(lwval_construct_int(-1), v, '*');
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
173 lwval_dup(v2, v1);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
174 lwval_destroy(v2);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
175 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
176 break;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
177
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
178 default:
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
179 lwval_clear(v1);
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
180 v1 -> lwval_type = LWVAL_TYPE_ERR;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
181 v1 -> dt.lwval_int = 1;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
182 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
183
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
184 return v1;
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
185 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
186
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
187 // v1 + v2 -> v1
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
188 LWVAL *lwval_add(LWVAL *v1, LWVAL *v2)
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
189 {
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
190 }
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
191
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
192 // v1 - v2 -> v1
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
193 LWVAL *lwval_sub(LWVAL *v1, LWVAL *v2)
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
194 {
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
195 }
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
196
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
197 // v1 * v2 -> v1
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
198 LWVAL *lwval_mul(LWVAL *v1, LWVAL *v2)
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
199 {
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
200 }
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
201
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
202 // v1 / v2 -> v1
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
203 LWVAL *lwval_div(LWVAL *v1, LWVAL *v2)
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
204 {
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
205 }
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
206
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
207 // v1 % v2 -> v1
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
208 LWVAL *lwval_mod(LWVAL *v1, LWVAL *v2)
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
209 {
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
210 }