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