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