paul@126 | 1 | /* Operations depending on program specifics. |
paul@126 | 2 | */ |
paul@126 | 3 | |
paul@126 | 4 | #include <stdlib.h> |
paul@126 | 5 | #include "types.h" |
paul@126 | 6 | #include "ops.h" |
paul@126 | 7 | #include "progconsts.h" |
paul@126 | 8 | #include "progops.h" |
paul@126 | 9 | #include "progtypes.h" |
paul@164 | 10 | #include "main.h" |
paul@193 | 11 | #include "exceptions.h" |
paul@126 | 12 | |
paul@126 | 13 | /* Generic instantiation operations, defining common members. */ |
paul@126 | 14 | |
paul@162 | 15 | __attr __new(const __table * table, __ref cls, size_t size) |
paul@126 | 16 | { |
paul@260 | 17 | __ref obj = (__ref) __ALLOCATE(1, size); |
paul@126 | 18 | __attr self = {obj, obj}; |
paul@126 | 19 | __attr tmp = {0, cls}; |
paul@126 | 20 | obj->table = table; |
paul@126 | 21 | __store_via_object(obj, __pos___class__, tmp); |
paul@126 | 22 | return self; |
paul@126 | 23 | } |
paul@126 | 24 | |
paul@159 | 25 | /* Generic internal data allocation. */ |
paul@159 | 26 | |
paul@283 | 27 | __fragment *__new_fragment(unsigned int n) |
paul@283 | 28 | { |
paul@283 | 29 | /* Allocate space for the list. */ |
paul@283 | 30 | __fragment *data = (__fragment *) __ALLOCATE(1, __FRAGMENT_SIZE(n)); |
paul@283 | 31 | |
paul@283 | 32 | /* The initial capacity is the same as the given size. */ |
paul@283 | 33 | data->size = 0; |
paul@283 | 34 | data->capacity = n; |
paul@283 | 35 | return data; |
paul@283 | 36 | } |
paul@283 | 37 | |
paul@283 | 38 | __mapping *__new_mapping(unsigned int n) |
paul@283 | 39 | { |
paul@283 | 40 | /* Allocate a number of buckets. */ |
paul@283 | 41 | __mapping *data = (__mapping *) __ALLOCATE(1, __MAPPING_SIZE(__MAPPING_BUCKETS)); |
paul@283 | 42 | unsigned int i; |
paul@283 | 43 | |
paul@283 | 44 | /* Allocate fragments with an initial size of 2 * n / __MAPPING_BUCKETS, |
paul@283 | 45 | assuming a mostly uniform distribution of values across the buckets. */ |
paul@283 | 46 | |
paul@283 | 47 | for (i = 0; i < __MAPPING_BUCKETS; i++) |
paul@283 | 48 | { |
paul@283 | 49 | data->keys[i] = __new_fragment(2 * n / __MAPPING_BUCKETS); |
paul@283 | 50 | data->values[i] = __new_fragment(2 * n / __MAPPING_BUCKETS); |
paul@283 | 51 | } |
paul@283 | 52 | |
paul@283 | 53 | return data; |
paul@283 | 54 | } |
paul@283 | 55 | |
paul@283 | 56 | void __newdata_sequence(__attr args[], unsigned int number) |
paul@159 | 57 | { |
paul@206 | 58 | /* Calculate the size of the fragment. */ |
paul@206 | 59 | |
paul@283 | 60 | __fragment *data = __new_fragment(number); |
paul@280 | 61 | __attr attr = {0, .seqvalue=data}; |
paul@159 | 62 | unsigned int i, j; |
paul@159 | 63 | |
paul@159 | 64 | /* Copy the given number of values, starting from the second element. */ |
paul@159 | 65 | |
paul@159 | 66 | for (i = 1, j = 0; i <= number; i++, j++) |
paul@159 | 67 | data->attrs[j] = args[i]; |
paul@159 | 68 | |
paul@159 | 69 | data->size = number; |
paul@283 | 70 | |
paul@283 | 71 | /* Store a reference to the data in the object's __data__ attribute. */ |
paul@283 | 72 | |
paul@283 | 73 | __store_via_object(args[0].value, __pos___data__, attr); |
paul@283 | 74 | } |
paul@283 | 75 | |
paul@291 | 76 | #ifdef __HAVE___builtins___dict_dict |
paul@291 | 77 | |
paul@283 | 78 | void __newdata_mapping(__attr args[], unsigned int number) |
paul@283 | 79 | { |
paul@283 | 80 | __mapping *data = __new_mapping(number); |
paul@283 | 81 | __attr attr = {0, .mapvalue=data}; |
paul@283 | 82 | __fragment *f; |
paul@283 | 83 | __attr callargs[3]; |
paul@283 | 84 | unsigned int i; |
paul@283 | 85 | |
paul@283 | 86 | /* Store a reference to the data in the object's __data__ attribute. */ |
paul@283 | 87 | |
paul@283 | 88 | __store_via_object(args[0].value, __pos___data__, attr); |
paul@283 | 89 | |
paul@283 | 90 | /* Store the given number of values, starting from the second element. */ |
paul@283 | 91 | |
paul@283 | 92 | for (i = 1; i <= number; i++) |
paul@283 | 93 | { |
paul@283 | 94 | /* Obtain the tuple elements. */ |
paul@283 | 95 | |
paul@283 | 96 | f = __load_via_object(args[i].value, __pos___data__).seqvalue; |
paul@283 | 97 | callargs[0] = args[0]; |
paul@283 | 98 | callargs[1] = f->attrs[0]; |
paul@283 | 99 | callargs[2] = f->attrs[1]; |
paul@283 | 100 | |
paul@283 | 101 | /* Call __setitem__ with the key and value. */ |
paul@283 | 102 | |
paul@283 | 103 | __fn___builtins___dict_dict___setitem__(callargs); |
paul@283 | 104 | } |
paul@159 | 105 | } |
paul@159 | 106 | |
paul@291 | 107 | #endif /* __HAVE___builtins___dict_dict */ |
paul@291 | 108 | |
paul@233 | 109 | /* A helper for raising type errors within common operations. */ |
paul@233 | 110 | |
paul@233 | 111 | void __raise_type_error() |
paul@233 | 112 | { |
paul@233 | 113 | __attr args[1]; |
paul@233 | 114 | __attr exc = __TYPE_ERROR_INSTANTIATOR(args); |
paul@233 | 115 | __Raise(exc); |
paul@233 | 116 | } |
paul@233 | 117 | |
paul@260 | 118 | /* A helper for raising memory errors within common operations. */ |
paul@260 | 119 | |
paul@260 | 120 | void __raise_memory_error() |
paul@260 | 121 | { |
paul@260 | 122 | __attr args[1]; |
paul@260 | 123 | __attr exc = __MEMORY_ERROR_INSTANTIATOR(args); |
paul@260 | 124 | __Raise(exc); |
paul@260 | 125 | } |
paul@260 | 126 | |
paul@126 | 127 | /* Generic invocation operations. */ |
paul@126 | 128 | |
paul@126 | 129 | /* Invoke the given callable, supplying keyword argument details in the given |
paul@126 | 130 | codes and arguments arrays, indicating the number of arguments described. |
paul@126 | 131 | The number of positional arguments is specified, and such arguments then |
paul@126 | 132 | follow as conventional function arguments. Typically, at least one argument |
paul@126 | 133 | is specified, starting with any context argument. |
paul@126 | 134 | */ |
paul@126 | 135 | |
paul@156 | 136 | __attr __invoke(__attr callable, int always_callable, |
paul@126 | 137 | unsigned int nkwargs, __param kwcodes[], __attr kwargs[], |
paul@126 | 138 | unsigned int nargs, __attr args[]) |
paul@126 | 139 | { |
paul@126 | 140 | /* Obtain the __args__ special member, referencing the parameter table. */ |
paul@126 | 141 | |
paul@241 | 142 | __attr minparams = __check_and_load_via_object(callable.value, __pos___args__, __code___args__); |
paul@126 | 143 | |
paul@126 | 144 | /* Refer to the table and minimum/maximum. */ |
paul@126 | 145 | |
paul@126 | 146 | const __ptable *ptable = minparams.ptable; |
paul@126 | 147 | const unsigned int min = minparams.min, max = ptable->size; |
paul@126 | 148 | |
paul@126 | 149 | /* Reserve enough space for the arguments. */ |
paul@126 | 150 | |
paul@126 | 151 | __attr allargs[max]; |
paul@126 | 152 | |
paul@126 | 153 | /* Traverse the arguments. */ |
paul@126 | 154 | |
paul@126 | 155 | unsigned int pos, kwpos; |
paul@126 | 156 | |
paul@126 | 157 | /* Check the number of arguments. */ |
paul@126 | 158 | |
paul@126 | 159 | if ((min > (nargs + nkwargs)) || ((nargs + nkwargs) > max)) |
paul@148 | 160 | return __NULL; |
paul@126 | 161 | |
paul@126 | 162 | /* Copy the arguments. */ |
paul@126 | 163 | |
paul@126 | 164 | for (pos = 0; pos < nargs; pos++) |
paul@126 | 165 | allargs[pos] = args[pos]; |
paul@126 | 166 | |
paul@126 | 167 | /* Erase the remaining arguments. */ |
paul@126 | 168 | |
paul@126 | 169 | for (pos = nargs; pos < max; pos++) |
paul@126 | 170 | { |
paul@126 | 171 | allargs[pos].value = 0; |
paul@126 | 172 | } |
paul@126 | 173 | |
paul@126 | 174 | /* Fill keyword arguments. */ |
paul@126 | 175 | |
paul@126 | 176 | for (kwpos = 0; kwpos < nkwargs; kwpos++) |
paul@126 | 177 | { |
paul@126 | 178 | pos = __HASPARAM(ptable, kwcodes[kwpos].pos, kwcodes[kwpos].code); |
paul@126 | 179 | |
paul@126 | 180 | /* Check the table entry against the supplied argument details. |
paul@126 | 181 | Set the argument but only if it does not overwrite positional |
paul@126 | 182 | arguments. */ |
paul@126 | 183 | |
paul@126 | 184 | if ((pos == -1) || (pos < nargs)) |
paul@148 | 185 | return __NULL; |
paul@126 | 186 | |
paul@126 | 187 | /* Set the argument using the appropriate position. */ |
paul@126 | 188 | |
paul@126 | 189 | allargs[pos] = kwargs[kwpos]; |
paul@126 | 190 | } |
paul@126 | 191 | |
paul@126 | 192 | /* Fill the defaults. */ |
paul@126 | 193 | |
paul@126 | 194 | for (pos = nargs; pos < max; pos++) |
paul@126 | 195 | { |
paul@126 | 196 | if (allargs[pos].value == 0) |
paul@264 | 197 | allargs[pos] = __GETDEFAULT(callable.value, pos - min); |
paul@126 | 198 | } |
paul@126 | 199 | |
paul@126 | 200 | /* Call with the prepared arguments. */ |
paul@126 | 201 | |
paul@156 | 202 | return (always_callable ? __load_via_object(callable.value, __pos___fn__) |
paul@156 | 203 | : __check_and_load_via_object(callable.value, __pos___fn__, __code___fn__) |
paul@156 | 204 | ).fn(allargs); |
paul@126 | 205 | } |
paul@126 | 206 | |
paul@126 | 207 | /* Error routines. */ |
paul@126 | 208 | |
paul@126 | 209 | __attr __unbound_method(__attr args[]) |
paul@126 | 210 | { |
paul@193 | 211 | __attr excargs[1]; |
paul@193 | 212 | __attr exc = __new___builtins___core_UnboundMethodInvocation(excargs); |
paul@193 | 213 | __Raise(exc); |
paul@193 | 214 | return __builtins___none_None; /* superfluous */ |
paul@126 | 215 | } |
paul@126 | 216 | |
paul@126 | 217 | /* Generic operations depending on specific program details. */ |
paul@126 | 218 | |
paul@126 | 219 | void __SETDEFAULT(__ref obj, int pos, __attr value) |
paul@126 | 220 | { |
paul@126 | 221 | __store_via_object(obj, __FUNCTION_INSTANCE_SIZE + pos, value); |
paul@126 | 222 | } |
paul@126 | 223 | |
paul@126 | 224 | __attr __GETDEFAULT(__ref obj, int pos) |
paul@126 | 225 | { |
paul@126 | 226 | return __load_via_object(obj, __FUNCTION_INSTANCE_SIZE + pos); |
paul@126 | 227 | } |
paul@144 | 228 | |
paul@144 | 229 | int __BOOL(__attr attr) |
paul@144 | 230 | { |
paul@164 | 231 | __attr args[2] = {{0, 0}, attr}; |
paul@164 | 232 | |
paul@164 | 233 | /* Invoke the bool function with the object and test against True. */ |
paul@164 | 234 | |
paul@164 | 235 | return __fn___builtins___boolean_bool(args).value == __builtins___boolean_True.value; |
paul@144 | 236 | } |