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