[09ababb7] | 1 | /*
|
---|
[051b3db8] | 2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
[09ababb7] | 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 |
|
---|
[074444f] | 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 |
|
---|
[23de644] | 51 | /** Integer variable.
|
---|
| 52 | *
|
---|
| 53 | * Sysel int type should be able to store arbitrarily (or at least
|
---|
| 54 | * very) large numbers.
|
---|
| 55 | */
|
---|
[09ababb7] | 56 | typedef struct {
|
---|
[23de644] | 57 | bigint_t value;
|
---|
[09ababb7] | 58 | } rdata_int_t;
|
---|
| 59 |
|
---|
| 60 | /** String variable */
|
---|
| 61 | typedef struct {
|
---|
[38aaacc2] | 62 | const char *value;
|
---|
[09ababb7] | 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
|
---|
[c5cb943d] | 73 | * symbol is non static, @c obj points to the object the symbol
|
---|
[09ababb7] | 74 | * belongs to.
|
---|
| 75 | */
|
---|
| 76 | typedef struct {
|
---|
[c5cb943d] | 77 | /** Object or @c NULL if deleg. points to a static function. */
|
---|
[09ababb7] | 78 | struct rdata_var *obj;
|
---|
| 79 |
|
---|
| 80 | /** Member symbol. */
|
---|
| 81 | struct stree_symbol *sym;
|
---|
| 82 | } rdata_deleg_t;
|
---|
| 83 |
|
---|
[051bc69a] | 84 | /** Enumerated type value. */
|
---|
| 85 | typedef struct {
|
---|
| 86 | /** Enum member declaration */
|
---|
| 87 | struct stree_embr *value;
|
---|
| 88 | } rdata_enum_t;
|
---|
| 89 |
|
---|
[94d484a] | 90 | /** Array variable */
|
---|
| 91 | typedef struct {
|
---|
| 92 | /** Rank */
|
---|
| 93 | int rank;
|
---|
| 94 |
|
---|
| 95 | /** Extents (@c rank entries) */
|
---|
| 96 | int *extent;
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * Elements (extent[0] * extent[1] * ... extent[rank - 1] entries)
|
---|
| 100 | * stored in lexicographical order. Each element is (rdata_var_t *).
|
---|
| 101 | */
|
---|
| 102 | struct rdata_var **element;
|
---|
| 103 | } rdata_array_t;
|
---|
| 104 |
|
---|
[09ababb7] | 105 | /** Object variable */
|
---|
| 106 | typedef struct {
|
---|
| 107 | /** Class of this object (symbol) */
|
---|
| 108 | struct stree_symbol *class_sym;
|
---|
| 109 |
|
---|
[c5cb943d] | 110 | /** @c sn_static if this is a static object (i.e. class object) */
|
---|
| 111 | statns_t static_obj;
|
---|
| 112 |
|
---|
[09ababb7] | 113 | /** Map field name SID to field data */
|
---|
[fa36f29] | 114 | intmap_t fields; /* of (rdata_var_t *) */
|
---|
[09ababb7] | 115 | } rdata_object_t;
|
---|
| 116 |
|
---|
[37f527b] | 117 | /** Resource handle
|
---|
| 118 | *
|
---|
| 119 | * Binding to external data. This type can be used to refer to data used
|
---|
| 120 | * by builtin functions (such as files).
|
---|
| 121 | */
|
---|
| 122 | typedef struct {
|
---|
| 123 | /** Only understood by the right builtin function. */
|
---|
| 124 | void *data;
|
---|
| 125 | } rdata_resource_t;
|
---|
| 126 |
|
---|
[051bc69a] | 127 | /** Symbol reference variable
|
---|
| 128 | *
|
---|
| 129 | * A symbol reference points to a program symbol.
|
---|
| 130 | */
|
---|
| 131 | typedef struct {
|
---|
| 132 | /** Program symbol. */
|
---|
| 133 | struct stree_symbol *sym;
|
---|
| 134 | } rdata_symbol_t;
|
---|
| 135 |
|
---|
[09ababb7] | 136 | typedef enum var_class {
|
---|
[074444f] | 137 | /** Boolean */
|
---|
| 138 | vc_bool,
|
---|
| 139 |
|
---|
| 140 | /** Character **/
|
---|
| 141 | vc_char,
|
---|
| 142 |
|
---|
[09ababb7] | 143 | /** Integer */
|
---|
| 144 | vc_int,
|
---|
| 145 |
|
---|
| 146 | /** String */
|
---|
| 147 | vc_string,
|
---|
| 148 |
|
---|
| 149 | /** Reference */
|
---|
| 150 | vc_ref,
|
---|
| 151 |
|
---|
| 152 | /** Delegate */
|
---|
| 153 | vc_deleg,
|
---|
| 154 |
|
---|
[051bc69a] | 155 | /** Enumerated type value */
|
---|
| 156 | vc_enum,
|
---|
| 157 |
|
---|
[94d484a] | 158 | /** Array */
|
---|
| 159 | vc_array,
|
---|
| 160 |
|
---|
[09ababb7] | 161 | /** Object */
|
---|
[37f527b] | 162 | vc_object,
|
---|
| 163 |
|
---|
| 164 | /** Interpreter builtin resource */
|
---|
[051bc69a] | 165 | vc_resource,
|
---|
| 166 |
|
---|
| 167 | /** Symbol reference */
|
---|
| 168 | vc_symbol
|
---|
[09ababb7] | 169 | } var_class_t;
|
---|
| 170 |
|
---|
| 171 | /** Variable.
|
---|
| 172 | *
|
---|
| 173 | * A piece of memory holding one of the basic types of data element.
|
---|
| 174 | * It is addressable (via rdata_var_t *) and mutable, at least from
|
---|
| 175 | * internal point of view of the interpreter.
|
---|
| 176 | */
|
---|
| 177 | typedef struct rdata_var {
|
---|
| 178 | var_class_t vc;
|
---|
| 179 |
|
---|
| 180 | union {
|
---|
[074444f] | 181 | rdata_bool_t *bool_v;
|
---|
| 182 | rdata_char_t *char_v;
|
---|
[09ababb7] | 183 | rdata_int_t *int_v;
|
---|
| 184 | rdata_string_t *string_v;
|
---|
| 185 | rdata_ref_t *ref_v;
|
---|
| 186 | rdata_deleg_t *deleg_v;
|
---|
[051bc69a] | 187 | rdata_enum_t *enum_v;
|
---|
[94d484a] | 188 | rdata_array_t *array_v;
|
---|
[09ababb7] | 189 | rdata_object_t *object_v;
|
---|
[37f527b] | 190 | rdata_resource_t *resource_v;
|
---|
[051bc69a] | 191 | rdata_symbol_t *symbol_v;
|
---|
[09ababb7] | 192 | } u;
|
---|
| 193 | } rdata_var_t;
|
---|
| 194 |
|
---|
[d0febca] | 195 | /** Address class */
|
---|
| 196 | typedef enum {
|
---|
| 197 | /** Variable address */
|
---|
| 198 | ac_var,
|
---|
| 199 |
|
---|
| 200 | /** Property address */
|
---|
| 201 | ac_prop
|
---|
| 202 | } address_class_t;
|
---|
| 203 |
|
---|
| 204 | /** Variable address */
|
---|
| 205 | typedef struct {
|
---|
[09ababb7] | 206 | /** Targeted variable */
|
---|
| 207 | rdata_var_t *vref;
|
---|
[d0febca] | 208 | } rdata_addr_var_t;
|
---|
| 209 |
|
---|
| 210 | /** Named property address */
|
---|
| 211 | typedef struct {
|
---|
| 212 | /** Delegate to the property */
|
---|
| 213 | rdata_deleg_t *prop_d;
|
---|
| 214 | } rdata_aprop_named_t;
|
---|
| 215 |
|
---|
| 216 | /** Indexed property address */
|
---|
| 217 | typedef struct {
|
---|
| 218 | /** Delegate to the object (or CSI) which is being indexed. */
|
---|
| 219 | rdata_deleg_t *object_d;
|
---|
| 220 |
|
---|
| 221 | /** Arguments (indices) */
|
---|
| 222 | list_t args; /* of rdata_item_t */
|
---|
| 223 | } rdata_aprop_indexed_t;
|
---|
| 224 |
|
---|
| 225 | typedef enum {
|
---|
| 226 | /* Named property address */
|
---|
| 227 | apc_named,
|
---|
| 228 |
|
---|
| 229 | /* Indexed property address */
|
---|
| 230 | apc_indexed
|
---|
| 231 | } aprop_class_t;
|
---|
| 232 |
|
---|
| 233 | /** Property address.
|
---|
| 234 | *
|
---|
[39e8406] | 235 | * When an access or index operation is performed on a property, the getter
|
---|
| 236 | * is run and the prefetched value is stored in @c tvalue. If the property
|
---|
| 237 | * is a non-scalar value type (a struct), then we might want to point to
|
---|
| 238 | * the proper @c var node inside it. @c tpos is used for this purpose.
|
---|
| 239 | *
|
---|
| 240 | * The assignment operator will modify @c tvalue and at the end the setter
|
---|
| 241 | * is called to store @c tvalue back into the property.
|
---|
[d0febca] | 242 | */
|
---|
| 243 | typedef struct {
|
---|
| 244 | aprop_class_t apc;
|
---|
| 245 |
|
---|
| 246 | /** Temporary copy of property value or @c NULL when not used. */
|
---|
| 247 | struct rdata_value *tvalue;
|
---|
| 248 |
|
---|
| 249 | /**
|
---|
| 250 | * Points to the specific var node within @c tvalue that is addressed
|
---|
| 251 | * or @c NULL when @c tvalue is not used.
|
---|
| 252 | */
|
---|
| 253 | rdata_var_t *tpos;
|
---|
| 254 |
|
---|
| 255 | union {
|
---|
| 256 | rdata_aprop_named_t *named;
|
---|
| 257 | rdata_aprop_indexed_t *indexed;
|
---|
| 258 | } u;
|
---|
| 259 | } rdata_addr_prop_t;
|
---|
| 260 |
|
---|
| 261 | /** Address item */
|
---|
| 262 | typedef struct rdata_address {
|
---|
| 263 | address_class_t ac;
|
---|
| 264 |
|
---|
| 265 | union {
|
---|
| 266 | rdata_addr_var_t *var_a;
|
---|
| 267 | rdata_addr_prop_t *prop_a;
|
---|
| 268 | } u;
|
---|
[09ababb7] | 269 | } rdata_address_t;
|
---|
| 270 |
|
---|
| 271 | /** Value item. */
|
---|
[94d484a] | 272 | typedef struct rdata_value {
|
---|
[09ababb7] | 273 | /**
|
---|
[051b3db8] | 274 | * Read-only Variable holding a copy of the data. Currently we don't
|
---|
| 275 | * allow sharing the same @c var node between different value nodes
|
---|
| 276 | * so that when destroying the value we can destroy the var.
|
---|
| 277 | *
|
---|
| 278 | * We could share this, but would need to reference-count it.
|
---|
[09ababb7] | 279 | */
|
---|
| 280 | rdata_var_t *var;
|
---|
| 281 | } rdata_value_t;
|
---|
| 282 |
|
---|
| 283 | typedef enum {
|
---|
| 284 | /** Address of a variable. */
|
---|
| 285 | ic_address,
|
---|
| 286 | /** Value */
|
---|
| 287 | ic_value
|
---|
| 288 | } item_class_t;
|
---|
| 289 |
|
---|
| 290 | /** Data item.
|
---|
| 291 | *
|
---|
| 292 | * Data item is the result of evaluating an expression. An address expression
|
---|
| 293 | * yields an address item (a.k.a. L-value), a value expression yields a data
|
---|
| 294 | * item (a.k.a. R-value). This model is used to accomodate semantics of the
|
---|
| 295 | * assignment operator.
|
---|
| 296 | */
|
---|
[fa36f29] | 297 | typedef struct rdata_item {
|
---|
[09ababb7] | 298 | item_class_t ic;
|
---|
| 299 |
|
---|
| 300 | union {
|
---|
| 301 | rdata_address_t *address;
|
---|
| 302 | rdata_value_t *value;
|
---|
| 303 | } u;
|
---|
| 304 | } rdata_item_t;
|
---|
| 305 |
|
---|
| 306 | #endif
|
---|