1 | /*
|
---|
2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @file Run-time data representation. */
|
---|
30 |
|
---|
31 |
|
---|
32 | #ifndef RDATA_T_H_
|
---|
33 | #define RDATA_T_H_
|
---|
34 |
|
---|
35 | #include "intmap_t.h"
|
---|
36 |
|
---|
37 | /** Boolean variable. */
|
---|
38 | typedef struct {
|
---|
39 | bool_t value;
|
---|
40 | } rdata_bool_t;
|
---|
41 |
|
---|
42 | /** Character variable.
|
---|
43 | *
|
---|
44 | * Sysel character type should be able to store arbitrarily (or at least
|
---|
45 | * very) large character sets.
|
---|
46 | */
|
---|
47 | typedef struct {
|
---|
48 | bigint_t value;
|
---|
49 | } rdata_char_t;
|
---|
50 |
|
---|
51 | /** Integer variable.
|
---|
52 | *
|
---|
53 | * Sysel int type should be able to store arbitrarily (or at least
|
---|
54 | * very) large numbers.
|
---|
55 | */
|
---|
56 | typedef struct {
|
---|
57 | bigint_t value;
|
---|
58 | } rdata_int_t;
|
---|
59 |
|
---|
60 | /** String variable */
|
---|
61 | typedef struct {
|
---|
62 | const char *value;
|
---|
63 | } rdata_string_t;
|
---|
64 |
|
---|
65 | /** Reference variable */
|
---|
66 | typedef struct {
|
---|
67 | struct rdata_var *vref;
|
---|
68 | } rdata_ref_t;
|
---|
69 |
|
---|
70 | /** Delegate variable
|
---|
71 | *
|
---|
72 | * A delegate variable points to a static or non-static symbol. If the
|
---|
73 | * symbol is non static, @c obj points to the object the symbol instance
|
---|
74 | * belongs to.
|
---|
75 | */
|
---|
76 | typedef struct {
|
---|
77 | /** Object or @c NULL if deleg. points to a CSI or static member. */
|
---|
78 | struct rdata_var *obj;
|
---|
79 |
|
---|
80 | /** Member symbol. */
|
---|
81 | struct stree_symbol *sym;
|
---|
82 | } rdata_deleg_t;
|
---|
83 |
|
---|
84 | /** Array variable */
|
---|
85 | typedef struct {
|
---|
86 | /** Rank */
|
---|
87 | int rank;
|
---|
88 |
|
---|
89 | /** Extents (@c rank entries) */
|
---|
90 | int *extent;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Elements (extent[0] * extent[1] * ... extent[rank - 1] entries)
|
---|
94 | * stored in lexicographical order. Each element is (rdata_var_t *).
|
---|
95 | */
|
---|
96 | struct rdata_var **element;
|
---|
97 | } rdata_array_t;
|
---|
98 |
|
---|
99 | /** Object variable */
|
---|
100 | typedef struct {
|
---|
101 | /** Class of this object (symbol) */
|
---|
102 | struct stree_symbol *class_sym;
|
---|
103 |
|
---|
104 | /** Map field name SID to field data */
|
---|
105 | intmap_t fields; /* of (rdata_var_t *) */
|
---|
106 | } rdata_object_t;
|
---|
107 |
|
---|
108 | /** Resource handle
|
---|
109 | *
|
---|
110 | * Binding to external data. This type can be used to refer to data used
|
---|
111 | * by builtin functions (such as files).
|
---|
112 | */
|
---|
113 | typedef struct {
|
---|
114 | /** Only understood by the right builtin function. */
|
---|
115 | void *data;
|
---|
116 | } rdata_resource_t;
|
---|
117 |
|
---|
118 | typedef enum var_class {
|
---|
119 | /** Boolean */
|
---|
120 | vc_bool,
|
---|
121 |
|
---|
122 | /** Character **/
|
---|
123 | vc_char,
|
---|
124 |
|
---|
125 | /** Integer */
|
---|
126 | vc_int,
|
---|
127 |
|
---|
128 | /** String */
|
---|
129 | vc_string,
|
---|
130 |
|
---|
131 | /** Reference */
|
---|
132 | vc_ref,
|
---|
133 |
|
---|
134 | /** Delegate */
|
---|
135 | vc_deleg,
|
---|
136 |
|
---|
137 | /** Array */
|
---|
138 | vc_array,
|
---|
139 |
|
---|
140 | /** Object */
|
---|
141 | vc_object,
|
---|
142 |
|
---|
143 | /** Interpreter builtin resource */
|
---|
144 | vc_resource
|
---|
145 | } var_class_t;
|
---|
146 |
|
---|
147 | /** Variable.
|
---|
148 | *
|
---|
149 | * A piece of memory holding one of the basic types of data element.
|
---|
150 | * It is addressable (via rdata_var_t *) and mutable, at least from
|
---|
151 | * internal point of view of the interpreter.
|
---|
152 | */
|
---|
153 | typedef struct rdata_var {
|
---|
154 | var_class_t vc;
|
---|
155 |
|
---|
156 | union {
|
---|
157 | rdata_bool_t *bool_v;
|
---|
158 | rdata_char_t *char_v;
|
---|
159 | rdata_int_t *int_v;
|
---|
160 | rdata_string_t *string_v;
|
---|
161 | rdata_ref_t *ref_v;
|
---|
162 | rdata_deleg_t *deleg_v;
|
---|
163 | rdata_array_t *array_v;
|
---|
164 | rdata_object_t *object_v;
|
---|
165 | rdata_resource_t *resource_v;
|
---|
166 | } u;
|
---|
167 | } rdata_var_t;
|
---|
168 |
|
---|
169 | /** Address class */
|
---|
170 | typedef enum {
|
---|
171 | /** Variable address */
|
---|
172 | ac_var,
|
---|
173 |
|
---|
174 | /** Property address */
|
---|
175 | ac_prop
|
---|
176 | } address_class_t;
|
---|
177 |
|
---|
178 | /** Variable address */
|
---|
179 | typedef struct {
|
---|
180 | /** Targeted variable */
|
---|
181 | rdata_var_t *vref;
|
---|
182 | } rdata_addr_var_t;
|
---|
183 |
|
---|
184 | /** Named property address */
|
---|
185 | typedef struct {
|
---|
186 | /** Delegate to the property */
|
---|
187 | rdata_deleg_t *prop_d;
|
---|
188 | } rdata_aprop_named_t;
|
---|
189 |
|
---|
190 | /** Indexed property address */
|
---|
191 | typedef struct {
|
---|
192 | /** Delegate to the object (or CSI) which is being indexed. */
|
---|
193 | rdata_deleg_t *object_d;
|
---|
194 |
|
---|
195 | /** Arguments (indices) */
|
---|
196 | list_t args; /* of rdata_item_t */
|
---|
197 | } rdata_aprop_indexed_t;
|
---|
198 |
|
---|
199 | typedef enum {
|
---|
200 | /* Named property address */
|
---|
201 | apc_named,
|
---|
202 |
|
---|
203 | /* Indexed property address */
|
---|
204 | apc_indexed
|
---|
205 | } aprop_class_t;
|
---|
206 |
|
---|
207 | /** Property address.
|
---|
208 | *
|
---|
209 | * When an access or index operation is performed on a property, the getter
|
---|
210 | * is run and the prefetched value is stored in @c tvalue. If the property
|
---|
211 | * is a non-scalar value type (a struct), then we might want to point to
|
---|
212 | * the proper @c var node inside it. @c tpos is used for this purpose.
|
---|
213 | *
|
---|
214 | * The assignment operator will modify @c tvalue and at the end the setter
|
---|
215 | * is called to store @c tvalue back into the property.
|
---|
216 | */
|
---|
217 | typedef struct {
|
---|
218 | aprop_class_t apc;
|
---|
219 |
|
---|
220 | /** Temporary copy of property value or @c NULL when not used. */
|
---|
221 | struct rdata_value *tvalue;
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Points to the specific var node within @c tvalue that is addressed
|
---|
225 | * or @c NULL when @c tvalue is not used.
|
---|
226 | */
|
---|
227 | rdata_var_t *tpos;
|
---|
228 |
|
---|
229 | union {
|
---|
230 | rdata_aprop_named_t *named;
|
---|
231 | rdata_aprop_indexed_t *indexed;
|
---|
232 | } u;
|
---|
233 | } rdata_addr_prop_t;
|
---|
234 |
|
---|
235 | /** Address item */
|
---|
236 | typedef struct rdata_address {
|
---|
237 | address_class_t ac;
|
---|
238 |
|
---|
239 | union {
|
---|
240 | rdata_addr_var_t *var_a;
|
---|
241 | rdata_addr_prop_t *prop_a;
|
---|
242 | } u;
|
---|
243 | } rdata_address_t;
|
---|
244 |
|
---|
245 | /** Value item. */
|
---|
246 | typedef struct rdata_value {
|
---|
247 | /**
|
---|
248 | * Read-only Variable holding a copy of the data. The same @c var
|
---|
249 | * can be shared between different instances of @c rdata_value_t.
|
---|
250 | */
|
---|
251 | rdata_var_t *var;
|
---|
252 | } rdata_value_t;
|
---|
253 |
|
---|
254 | typedef enum {
|
---|
255 | /** Address of a variable. */
|
---|
256 | ic_address,
|
---|
257 | /** Value */
|
---|
258 | ic_value
|
---|
259 | } item_class_t;
|
---|
260 |
|
---|
261 | /** Data item.
|
---|
262 | *
|
---|
263 | * Data item is the result of evaluating an expression. An address expression
|
---|
264 | * yields an address item (a.k.a. L-value), a value expression yields a data
|
---|
265 | * item (a.k.a. R-value). This model is used to accomodate semantics of the
|
---|
266 | * assignment operator.
|
---|
267 | */
|
---|
268 | typedef struct rdata_item {
|
---|
269 | item_class_t ic;
|
---|
270 |
|
---|
271 | union {
|
---|
272 | rdata_address_t *address;
|
---|
273 | rdata_value_t *value;
|
---|
274 | } u;
|
---|
275 | } rdata_item_t;
|
---|
276 |
|
---|
277 | #endif
|
---|