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@302 | 30 | |
paul@283 | 31 | __fragment *data = (__fragment *) __ALLOCATE(1, __FRAGMENT_SIZE(n)); |
paul@283 | 32 | |
paul@283 | 33 | /* The initial capacity is the same as the given size. */ |
paul@302 | 34 | |
paul@283 | 35 | data->size = 0; |
paul@283 | 36 | data->capacity = n; |
paul@283 | 37 | return data; |
paul@283 | 38 | } |
paul@283 | 39 | |
paul@283 | 40 | void __newdata_sequence(__attr args[], unsigned int number) |
paul@159 | 41 | { |
paul@206 | 42 | /* Calculate the size of the fragment. */ |
paul@206 | 43 | |
paul@283 | 44 | __fragment *data = __new_fragment(number); |
paul@280 | 45 | __attr attr = {0, .seqvalue=data}; |
paul@159 | 46 | unsigned int i, j; |
paul@159 | 47 | |
paul@159 | 48 | /* Copy the given number of values, starting from the second element. */ |
paul@159 | 49 | |
paul@159 | 50 | for (i = 1, j = 0; i <= number; i++, j++) |
paul@159 | 51 | data->attrs[j] = args[i]; |
paul@159 | 52 | |
paul@159 | 53 | data->size = number; |
paul@283 | 54 | |
paul@283 | 55 | /* Store a reference to the data in the object's __data__ attribute. */ |
paul@283 | 56 | |
paul@283 | 57 | __store_via_object(args[0].value, __pos___data__, attr); |
paul@283 | 58 | } |
paul@283 | 59 | |
paul@291 | 60 | #ifdef __HAVE___builtins___dict_dict |
paul@291 | 61 | |
paul@283 | 62 | void __newdata_mapping(__attr args[], unsigned int number) |
paul@283 | 63 | { |
paul@304 | 64 | __attr dict = args[0]; |
paul@304 | 65 | __attr callargs[2]; |
paul@283 | 66 | |
paul@304 | 67 | /* Create a temporary list using the arguments. */ |
paul@283 | 68 | |
paul@304 | 69 | __newliteral___builtins___list_list(args, number); |
paul@283 | 70 | |
paul@304 | 71 | /* Call __init__ with the dict object and list argument. */ |
paul@283 | 72 | |
paul@304 | 73 | callargs[0] = dict; |
paul@304 | 74 | callargs[1] = args[0]; |
paul@283 | 75 | |
paul@304 | 76 | __fn___builtins___dict_dict___init__(callargs); |
paul@304 | 77 | args[0] = dict; |
paul@159 | 78 | } |
paul@159 | 79 | |
paul@291 | 80 | #endif /* __HAVE___builtins___dict_dict */ |
paul@291 | 81 | |
paul@306 | 82 | /* Helpers for raising errors within common operations. */ |
paul@306 | 83 | |
paul@306 | 84 | void __raise_memory_error() |
paul@306 | 85 | { |
paul@306 | 86 | __attr args[1]; |
paul@306 | 87 | __attr exc = __MEMORY_ERROR_INSTANTIATOR(args); |
paul@306 | 88 | __Raise(exc); |
paul@306 | 89 | } |
paul@306 | 90 | |
paul@306 | 91 | void __raise_overflow_error() |
paul@306 | 92 | { |
paul@306 | 93 | __attr args[1]; |
paul@306 | 94 | __attr exc = __OVERFLOW_ERROR_INSTANTIATOR(args); |
paul@306 | 95 | __Raise(exc); |
paul@306 | 96 | } |
paul@233 | 97 | |
paul@233 | 98 | void __raise_type_error() |
paul@233 | 99 | { |
paul@233 | 100 | __attr args[1]; |
paul@233 | 101 | __attr exc = __TYPE_ERROR_INSTANTIATOR(args); |
paul@233 | 102 | __Raise(exc); |
paul@233 | 103 | } |
paul@233 | 104 | |
paul@306 | 105 | void __raise_zero_division_error() |
paul@260 | 106 | { |
paul@260 | 107 | __attr args[1]; |
paul@306 | 108 | __attr exc = __ZERO_DIVISION_ERROR_INSTANTIATOR(args); |
paul@260 | 109 | __Raise(exc); |
paul@260 | 110 | } |
paul@260 | 111 | |
paul@126 | 112 | /* Generic invocation operations. */ |
paul@126 | 113 | |
paul@126 | 114 | /* Invoke the given callable, supplying keyword argument details in the given |
paul@126 | 115 | codes and arguments arrays, indicating the number of arguments described. |
paul@126 | 116 | The number of positional arguments is specified, and such arguments then |
paul@126 | 117 | follow as conventional function arguments. Typically, at least one argument |
paul@126 | 118 | is specified, starting with any context argument. |
paul@126 | 119 | */ |
paul@126 | 120 | |
paul@156 | 121 | __attr __invoke(__attr callable, int always_callable, |
paul@126 | 122 | unsigned int nkwargs, __param kwcodes[], __attr kwargs[], |
paul@126 | 123 | unsigned int nargs, __attr args[]) |
paul@126 | 124 | { |
paul@126 | 125 | /* Obtain the __args__ special member, referencing the parameter table. */ |
paul@126 | 126 | |
paul@241 | 127 | __attr minparams = __check_and_load_via_object(callable.value, __pos___args__, __code___args__); |
paul@126 | 128 | |
paul@126 | 129 | /* Refer to the table and minimum/maximum. */ |
paul@126 | 130 | |
paul@126 | 131 | const __ptable *ptable = minparams.ptable; |
paul@126 | 132 | const unsigned int min = minparams.min, max = ptable->size; |
paul@126 | 133 | |
paul@126 | 134 | /* Reserve enough space for the arguments. */ |
paul@126 | 135 | |
paul@126 | 136 | __attr allargs[max]; |
paul@126 | 137 | |
paul@126 | 138 | /* Traverse the arguments. */ |
paul@126 | 139 | |
paul@126 | 140 | unsigned int pos, kwpos; |
paul@126 | 141 | |
paul@126 | 142 | /* Check the number of arguments. */ |
paul@126 | 143 | |
paul@126 | 144 | if ((min > (nargs + nkwargs)) || ((nargs + nkwargs) > max)) |
paul@148 | 145 | return __NULL; |
paul@126 | 146 | |
paul@126 | 147 | /* Copy the arguments. */ |
paul@126 | 148 | |
paul@126 | 149 | for (pos = 0; pos < nargs; pos++) |
paul@126 | 150 | allargs[pos] = args[pos]; |
paul@126 | 151 | |
paul@126 | 152 | /* Erase the remaining arguments. */ |
paul@126 | 153 | |
paul@126 | 154 | for (pos = nargs; pos < max; pos++) |
paul@126 | 155 | { |
paul@126 | 156 | allargs[pos].value = 0; |
paul@126 | 157 | } |
paul@126 | 158 | |
paul@126 | 159 | /* Fill keyword arguments. */ |
paul@126 | 160 | |
paul@126 | 161 | for (kwpos = 0; kwpos < nkwargs; kwpos++) |
paul@126 | 162 | { |
paul@126 | 163 | pos = __HASPARAM(ptable, kwcodes[kwpos].pos, kwcodes[kwpos].code); |
paul@126 | 164 | |
paul@126 | 165 | /* Check the table entry against the supplied argument details. |
paul@126 | 166 | Set the argument but only if it does not overwrite positional |
paul@126 | 167 | arguments. */ |
paul@126 | 168 | |
paul@126 | 169 | if ((pos == -1) || (pos < nargs)) |
paul@148 | 170 | return __NULL; |
paul@126 | 171 | |
paul@126 | 172 | /* Set the argument using the appropriate position. */ |
paul@126 | 173 | |
paul@126 | 174 | allargs[pos] = kwargs[kwpos]; |
paul@126 | 175 | } |
paul@126 | 176 | |
paul@126 | 177 | /* Fill the defaults. */ |
paul@126 | 178 | |
paul@126 | 179 | for (pos = nargs; pos < max; pos++) |
paul@126 | 180 | { |
paul@126 | 181 | if (allargs[pos].value == 0) |
paul@264 | 182 | allargs[pos] = __GETDEFAULT(callable.value, pos - min); |
paul@126 | 183 | } |
paul@126 | 184 | |
paul@126 | 185 | /* Call with the prepared arguments. */ |
paul@126 | 186 | |
paul@156 | 187 | return (always_callable ? __load_via_object(callable.value, __pos___fn__) |
paul@156 | 188 | : __check_and_load_via_object(callable.value, __pos___fn__, __code___fn__) |
paul@156 | 189 | ).fn(allargs); |
paul@126 | 190 | } |
paul@126 | 191 | |
paul@126 | 192 | /* Error routines. */ |
paul@126 | 193 | |
paul@126 | 194 | __attr __unbound_method(__attr args[]) |
paul@126 | 195 | { |
paul@193 | 196 | __attr excargs[1]; |
paul@193 | 197 | __attr exc = __new___builtins___core_UnboundMethodInvocation(excargs); |
paul@193 | 198 | __Raise(exc); |
paul@193 | 199 | return __builtins___none_None; /* superfluous */ |
paul@126 | 200 | } |
paul@126 | 201 | |
paul@126 | 202 | /* Generic operations depending on specific program details. */ |
paul@126 | 203 | |
paul@126 | 204 | void __SETDEFAULT(__ref obj, int pos, __attr value) |
paul@126 | 205 | { |
paul@126 | 206 | __store_via_object(obj, __FUNCTION_INSTANCE_SIZE + pos, value); |
paul@126 | 207 | } |
paul@126 | 208 | |
paul@126 | 209 | __attr __GETDEFAULT(__ref obj, int pos) |
paul@126 | 210 | { |
paul@126 | 211 | return __load_via_object(obj, __FUNCTION_INSTANCE_SIZE + pos); |
paul@126 | 212 | } |
paul@144 | 213 | |
paul@144 | 214 | int __BOOL(__attr attr) |
paul@144 | 215 | { |
paul@164 | 216 | __attr args[2] = {{0, 0}, attr}; |
paul@164 | 217 | |
paul@164 | 218 | /* Invoke the bool function with the object and test against True. */ |
paul@164 | 219 | |
paul@164 | 220 | return __fn___builtins___boolean_bool(args).value == __builtins___boolean_True.value; |
paul@144 | 221 | } |