Mercurial > hg-old > index.cgi
annotate src/lwval.c @ 116:817ab0e124fa
reading files implemented
author | lost |
---|---|
date | Wed, 21 Jan 2009 03:15:49 +0000 |
parents | 1f598d89b9b0 |
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 */ |
14 | 26 |
27 #include <malloc.h> | |
28 | |
15 | 29 #define __lwval_c_seen__ |
30 #include "lwval.h" | |
14 | 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 | |
15 | 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 | |
14 | 100 void lwval_destroy(LWVAL *value) |
101 { | |
102 if (value) | |
15 | 103 { |
104 lwval_clear(value); | |
14 | 105 free(value); |
15 | 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; | |
14 | 141 } |
142 | |
15 | 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) | |
14 | 189 { |
190 } | |
191 | |
15 | 192 // v1 - v2 -> v1 |
193 LWVAL *lwval_sub(LWVAL *v1, LWVAL *v2) | |
14 | 194 { |
195 } | |
196 | |
15 | 197 // v1 * v2 -> v1 |
198 LWVAL *lwval_mul(LWVAL *v1, LWVAL *v2) | |
14 | 199 { |
200 } | |
201 | |
15 | 202 // v1 / v2 -> v1 |
203 LWVAL *lwval_div(LWVAL *v1, LWVAL *v2) | |
14 | 204 { |
205 } | |
206 | |
15 | 207 // v1 % v2 -> v1 |
208 LWVAL *lwval_mod(LWVAL *v1, LWVAL *v2) | |
14 | 209 { |
210 } |