paul@126 | 1 | /* Common operations. */ |
paul@126 | 2 | |
paul@151 | 3 | #include <stdlib.h> |
paul@126 | 4 | #include "ops.h" |
paul@126 | 5 | #include "progconsts.h" |
paul@126 | 6 | #include "progtypes.h" |
paul@126 | 7 | |
paul@126 | 8 | /* Direct access and manipulation of static objects. */ |
paul@126 | 9 | |
paul@200 | 10 | __attr __load_static(__ref parent, __ref obj) |
paul@126 | 11 | { |
paul@200 | 12 | __attr out = {.context=parent, .value=obj}; |
paul@126 | 13 | return out; |
paul@126 | 14 | } |
paul@126 | 15 | |
paul@126 | 16 | /* Direct retrieval operations, returning and setting attributes. */ |
paul@126 | 17 | |
paul@126 | 18 | __attr __load_via_object(__ref obj, int pos) |
paul@126 | 19 | { |
paul@126 | 20 | return obj->attrs[pos]; |
paul@126 | 21 | } |
paul@126 | 22 | |
paul@126 | 23 | __attr __load_via_class(__ref obj, int pos) |
paul@126 | 24 | { |
paul@126 | 25 | return __load_via_object(__get_class(obj), pos); |
paul@126 | 26 | } |
paul@126 | 27 | |
paul@126 | 28 | __attr __get_class_and_load(__ref obj, int pos) |
paul@126 | 29 | { |
paul@153 | 30 | if (__is_instance(obj)) |
paul@153 | 31 | return __load_via_class(obj, pos); |
paul@153 | 32 | else |
paul@126 | 33 | return __load_via_object(obj, pos); |
paul@126 | 34 | } |
paul@126 | 35 | |
paul@126 | 36 | /* Direct storage operations. */ |
paul@126 | 37 | |
paul@126 | 38 | int __store_via_object(__ref obj, int pos, __attr value) |
paul@126 | 39 | { |
paul@126 | 40 | obj->attrs[pos] = value; |
paul@126 | 41 | return 1; |
paul@126 | 42 | } |
paul@126 | 43 | |
paul@126 | 44 | /* Introspection. */ |
paul@126 | 45 | |
paul@126 | 46 | int __is_instance(__ref obj) |
paul@126 | 47 | { |
paul@126 | 48 | return obj->pos == __INSTANCEPOS; |
paul@126 | 49 | } |
paul@126 | 50 | |
paul@126 | 51 | __ref __get_class(__ref obj) |
paul@126 | 52 | { |
paul@126 | 53 | return __load_via_object(obj, __pos___class__).value; |
paul@126 | 54 | } |
paul@126 | 55 | |
paul@231 | 56 | __attr __get_class_attr(__ref obj) |
paul@231 | 57 | { |
paul@231 | 58 | return __load_via_object(obj, __pos___class__); |
paul@231 | 59 | } |
paul@231 | 60 | |
paul@126 | 61 | /* Attribute testing operations. */ |
paul@126 | 62 | |
paul@144 | 63 | __ref __test_specific_instance(__ref obj, __ref type) |
paul@126 | 64 | { |
paul@144 | 65 | return __get_class(obj) == type ? obj : 0; |
paul@126 | 66 | } |
paul@126 | 67 | |
paul@237 | 68 | __ref __test_specific_object(__ref obj, __ref type) |
paul@237 | 69 | { |
paul@237 | 70 | return __test_specific_type(obj, type) || __test_specific_instance(obj, type) ? obj : 0; |
paul@237 | 71 | } |
paul@237 | 72 | |
paul@237 | 73 | __ref __test_specific_type(__ref obj, __ref type) |
paul@237 | 74 | { |
paul@237 | 75 | return obj == type ? obj : 0; |
paul@237 | 76 | } |
paul@237 | 77 | |
paul@144 | 78 | __ref __test_common_instance(__ref obj, int pos, int code) |
paul@144 | 79 | { |
paul@144 | 80 | return __HASATTR(__get_class(obj), pos, code) ? obj : 0; |
paul@144 | 81 | } |
paul@144 | 82 | |
paul@144 | 83 | __ref __test_common_object(__ref obj, int pos, int code) |
paul@126 | 84 | { |
paul@144 | 85 | return __test_common_type(obj, pos, code) || __test_common_instance(obj, pos, code) ? obj : 0; |
paul@144 | 86 | } |
paul@144 | 87 | |
paul@144 | 88 | __ref __test_common_type(__ref obj, int pos, int code) |
paul@144 | 89 | { |
paul@144 | 90 | return __HASATTR(obj, pos, code) ? obj : 0; |
paul@126 | 91 | } |
paul@126 | 92 | |
paul@126 | 93 | /* Attribute testing and retrieval operations. */ |
paul@126 | 94 | |
paul@233 | 95 | static __attr __check_and_load_via_object_null(__ref obj, int pos, int code) |
paul@233 | 96 | { |
paul@233 | 97 | if (__HASATTR(obj, pos, code)) |
paul@233 | 98 | return __load_via_object(obj, pos); |
paul@233 | 99 | else |
paul@233 | 100 | return __NULL; |
paul@233 | 101 | } |
paul@233 | 102 | |
paul@126 | 103 | __attr __check_and_load_via_class(__ref obj, int pos, int code) |
paul@126 | 104 | { |
paul@126 | 105 | return __check_and_load_via_object(__get_class(obj), pos, code); |
paul@126 | 106 | } |
paul@126 | 107 | |
paul@126 | 108 | __attr __check_and_load_via_object(__ref obj, int pos, int code) |
paul@126 | 109 | { |
paul@233 | 110 | if (__HASATTR(obj, pos, code)) |
paul@233 | 111 | return __load_via_object(obj, pos); |
paul@233 | 112 | |
paul@233 | 113 | __raise_type_error(); |
paul@233 | 114 | return __NULL; |
paul@126 | 115 | } |
paul@126 | 116 | |
paul@126 | 117 | __attr __check_and_load_via_any(__ref obj, int pos, int code) |
paul@126 | 118 | { |
paul@233 | 119 | __attr out = __check_and_load_via_object_null(obj, pos, code); |
paul@126 | 120 | if (out.value == 0) |
paul@126 | 121 | out = __check_and_load_via_class(obj, pos, code); |
paul@126 | 122 | return out; |
paul@126 | 123 | } |
paul@126 | 124 | |
paul@126 | 125 | /* Attribute testing and storage operations. */ |
paul@126 | 126 | |
paul@126 | 127 | int __check_and_store_via_object(__ref obj, int pos, int code, __attr value) |
paul@126 | 128 | { |
paul@126 | 129 | if (__HASATTR(obj, pos, code)) |
paul@126 | 130 | { |
paul@126 | 131 | __store_via_object(obj, pos, value); |
paul@126 | 132 | return 1; |
paul@126 | 133 | } |
paul@126 | 134 | return 0; |
paul@126 | 135 | } |
paul@126 | 136 | |
paul@126 | 137 | int __check_and_store_via_any(__ref obj, int pos, int code, __attr value) |
paul@126 | 138 | { |
paul@126 | 139 | if (__check_and_store_via_object(obj, pos, code, value)) |
paul@126 | 140 | return 1; |
paul@126 | 141 | return __check_and_store_via_object(__get_class(obj), pos, code, value); |
paul@126 | 142 | } |
paul@126 | 143 | |
paul@126 | 144 | /* Context-related operations. */ |
paul@126 | 145 | |
paul@126 | 146 | __attr __test_context(__ref context, __attr attr) |
paul@126 | 147 | { |
paul@230 | 148 | /* Preserve any existing instance context. */ |
paul@230 | 149 | |
paul@126 | 150 | if (__is_instance(attr.context)) |
paul@126 | 151 | return attr; |
paul@235 | 152 | |
paul@235 | 153 | /* Test any instance context against the context employed by the |
paul@235 | 154 | attribute. */ |
paul@126 | 155 | |
paul@235 | 156 | if (__is_instance(context)) |
paul@235 | 157 | if (__test_common_instance(context, __TYPEPOS(attr.context), __TYPECODE(attr.context))) |
paul@235 | 158 | return __replace_context(context, attr); |
paul@235 | 159 | else |
paul@235 | 160 | __raise_type_error(); |
paul@235 | 161 | |
paul@235 | 162 | /* Otherwise, preserve the attribute as retrieved. */ |
paul@235 | 163 | |
paul@235 | 164 | return attr; |
paul@126 | 165 | } |
paul@126 | 166 | |
paul@126 | 167 | __attr __replace_context(__ref context, __attr attr) |
paul@126 | 168 | { |
paul@126 | 169 | __attr out; |
paul@126 | 170 | |
paul@126 | 171 | /* Set the context. */ |
paul@126 | 172 | |
paul@126 | 173 | out.context = context; |
paul@126 | 174 | |
paul@126 | 175 | /* Reference a callable version of the attribute by obtaining the bound |
paul@126 | 176 | method reference from the __fn__ special attribute. */ |
paul@126 | 177 | |
paul@126 | 178 | out.value = __load_via_object(attr.value, __ATTRPOS(__fn__)).b; |
paul@126 | 179 | return out; |
paul@126 | 180 | } |
paul@126 | 181 | |
paul@126 | 182 | __attr __update_context(__ref context, __attr attr) |
paul@126 | 183 | { |
paul@126 | 184 | __attr out = {context, .fn=attr.fn}; |
paul@126 | 185 | return out; |
paul@126 | 186 | } |
paul@126 | 187 | |
paul@126 | 188 | /* Basic structure tests. */ |
paul@126 | 189 | |
paul@126 | 190 | int __WITHIN(__ref obj, int pos) |
paul@126 | 191 | { |
paul@126 | 192 | return pos < obj->table->size; |
paul@126 | 193 | } |
paul@126 | 194 | |
paul@126 | 195 | int __HASATTR(__ref obj, int pos, int code) |
paul@126 | 196 | { |
paul@126 | 197 | return __WITHIN(obj, pos) && (obj->table->attrs[pos] == code); |
paul@126 | 198 | } |
paul@126 | 199 | |
paul@126 | 200 | /* Parameter position operations. */ |
paul@126 | 201 | |
paul@126 | 202 | int __HASPARAM(const __ptable *ptable, int ppos, int pcode) |
paul@126 | 203 | { |
paul@126 | 204 | __param param; |
paul@126 | 205 | |
paul@126 | 206 | if (ppos < ptable->size) |
paul@126 | 207 | { |
paul@126 | 208 | param = ptable->params[ppos]; |
paul@126 | 209 | if (param.code == pcode) |
paul@126 | 210 | return param.pos; |
paul@126 | 211 | } |
paul@126 | 212 | |
paul@126 | 213 | return -1; |
paul@126 | 214 | } |
paul@126 | 215 | |
paul@126 | 216 | /* Conversions. */ |
paul@126 | 217 | |
paul@126 | 218 | __attr __CONTEXT_AS_VALUE(__attr attr) |
paul@126 | 219 | { |
paul@126 | 220 | __attr out; |
paul@126 | 221 | out.context = attr.context; |
paul@126 | 222 | out.value = attr.context; |
paul@126 | 223 | return out; |
paul@126 | 224 | } |
paul@126 | 225 | |
paul@126 | 226 | /* Type testing. */ |
paul@126 | 227 | |
paul@144 | 228 | __ref __ISFUNC(__ref obj) |
paul@126 | 229 | { |
paul@126 | 230 | return __test_specific_instance(obj, &__FUNCTION_TYPE); |
paul@126 | 231 | } |
paul@126 | 232 | |
paul@126 | 233 | int __ISNULL(__attr value) |
paul@126 | 234 | { |
paul@143 | 235 | /* (value.context == __NULL.context) is superfluous */ |
paul@143 | 236 | return (value.value == 0); /* __NULL.value */ |
paul@126 | 237 | } |
paul@126 | 238 | |
paul@126 | 239 | /* Attribute codes and positions for type objects. */ |
paul@126 | 240 | |
paul@126 | 241 | unsigned int __TYPECODE(__ref obj) |
paul@126 | 242 | { |
paul@126 | 243 | return obj->table->attrs[obj->pos]; |
paul@126 | 244 | } |
paul@126 | 245 | |
paul@126 | 246 | unsigned int __TYPEPOS(__ref obj) |
paul@126 | 247 | { |
paul@126 | 248 | return obj->pos; |
paul@126 | 249 | } |
paul@151 | 250 | |
paul@151 | 251 | /* Copying of structures. */ |
paul@151 | 252 | |
paul@151 | 253 | __ref __COPY(__ref obj, int size) |
paul@151 | 254 | { |
paul@151 | 255 | __ref copy = calloc(1, size); |
paul@151 | 256 | memcpy(copy, obj, size); |
paul@151 | 257 | return copy; |
paul@151 | 258 | } |