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@162 | 17 | __ref obj = (__ref) calloc(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@159 | 27 | __attr __newdata(__attr args[], unsigned int number) |
paul@159 | 28 | { |
paul@206 | 29 | /* Calculate the size of the fragment. */ |
paul@206 | 30 | |
paul@206 | 31 | __fragment *data = (__fragment *) calloc(1, __FRAGMENT_SIZE(number)); |
paul@159 | 32 | __attr attr = {0, .data=data}; |
paul@159 | 33 | unsigned int i, j; |
paul@159 | 34 | |
paul@159 | 35 | /* Copy the given number of values, starting from the second element. */ |
paul@159 | 36 | |
paul@159 | 37 | for (i = 1, j = 0; i <= number; i++, j++) |
paul@159 | 38 | data->attrs[j] = args[i]; |
paul@159 | 39 | |
paul@159 | 40 | data->size = number; |
paul@206 | 41 | data->capacity = number; |
paul@159 | 42 | return attr; |
paul@159 | 43 | } |
paul@159 | 44 | |
paul@233 | 45 | /* A helper for raising type errors within common operations. */ |
paul@233 | 46 | |
paul@233 | 47 | void __raise_type_error() |
paul@233 | 48 | { |
paul@233 | 49 | __attr args[1]; |
paul@233 | 50 | __attr exc = __TYPE_ERROR_INSTANTIATOR(args); |
paul@233 | 51 | __Raise(exc); |
paul@233 | 52 | } |
paul@233 | 53 | |
paul@126 | 54 | /* Generic invocation operations. */ |
paul@126 | 55 | |
paul@126 | 56 | /* Invoke the given callable, supplying keyword argument details in the given |
paul@126 | 57 | codes and arguments arrays, indicating the number of arguments described. |
paul@126 | 58 | The number of positional arguments is specified, and such arguments then |
paul@126 | 59 | follow as conventional function arguments. Typically, at least one argument |
paul@126 | 60 | is specified, starting with any context argument. |
paul@126 | 61 | */ |
paul@126 | 62 | |
paul@156 | 63 | __attr __invoke(__attr callable, int always_callable, |
paul@126 | 64 | unsigned int nkwargs, __param kwcodes[], __attr kwargs[], |
paul@126 | 65 | unsigned int nargs, __attr args[]) |
paul@126 | 66 | { |
paul@126 | 67 | /* Obtain the __args__ special member, referencing the parameter table. */ |
paul@126 | 68 | |
paul@241 | 69 | __attr minparams = __check_and_load_via_object(callable.value, __pos___args__, __code___args__); |
paul@126 | 70 | |
paul@126 | 71 | /* Refer to the table and minimum/maximum. */ |
paul@126 | 72 | |
paul@126 | 73 | const __ptable *ptable = minparams.ptable; |
paul@126 | 74 | const unsigned int min = minparams.min, max = ptable->size; |
paul@126 | 75 | |
paul@126 | 76 | /* Reserve enough space for the arguments. */ |
paul@126 | 77 | |
paul@126 | 78 | __attr allargs[max]; |
paul@126 | 79 | |
paul@126 | 80 | /* Traverse the arguments. */ |
paul@126 | 81 | |
paul@126 | 82 | unsigned int pos, kwpos; |
paul@126 | 83 | |
paul@126 | 84 | /* Check the number of arguments. */ |
paul@126 | 85 | |
paul@126 | 86 | if ((min > (nargs + nkwargs)) || ((nargs + nkwargs) > max)) |
paul@148 | 87 | return __NULL; |
paul@126 | 88 | |
paul@126 | 89 | /* Copy the arguments. */ |
paul@126 | 90 | |
paul@126 | 91 | for (pos = 0; pos < nargs; pos++) |
paul@126 | 92 | allargs[pos] = args[pos]; |
paul@126 | 93 | |
paul@126 | 94 | /* Erase the remaining arguments. */ |
paul@126 | 95 | |
paul@126 | 96 | for (pos = nargs; pos < max; pos++) |
paul@126 | 97 | { |
paul@126 | 98 | allargs[pos].value = 0; |
paul@126 | 99 | } |
paul@126 | 100 | |
paul@126 | 101 | /* Fill keyword arguments. */ |
paul@126 | 102 | |
paul@126 | 103 | for (kwpos = 0; kwpos < nkwargs; kwpos++) |
paul@126 | 104 | { |
paul@126 | 105 | pos = __HASPARAM(ptable, kwcodes[kwpos].pos, kwcodes[kwpos].code); |
paul@126 | 106 | |
paul@126 | 107 | /* Check the table entry against the supplied argument details. |
paul@126 | 108 | Set the argument but only if it does not overwrite positional |
paul@126 | 109 | arguments. */ |
paul@126 | 110 | |
paul@126 | 111 | if ((pos == -1) || (pos < nargs)) |
paul@148 | 112 | return __NULL; |
paul@126 | 113 | |
paul@126 | 114 | /* Set the argument using the appropriate position. */ |
paul@126 | 115 | |
paul@126 | 116 | allargs[pos] = kwargs[kwpos]; |
paul@126 | 117 | } |
paul@126 | 118 | |
paul@126 | 119 | /* Fill the defaults. */ |
paul@126 | 120 | |
paul@126 | 121 | for (pos = nargs; pos < max; pos++) |
paul@126 | 122 | { |
paul@126 | 123 | if (allargs[pos].value == 0) |
paul@126 | 124 | allargs[pos] = __GETDEFAULT(callable.value, pos - nargs); |
paul@126 | 125 | } |
paul@126 | 126 | |
paul@126 | 127 | /* Call with the prepared arguments. */ |
paul@126 | 128 | |
paul@156 | 129 | return (always_callable ? __load_via_object(callable.value, __pos___fn__) |
paul@156 | 130 | : __check_and_load_via_object(callable.value, __pos___fn__, __code___fn__) |
paul@156 | 131 | ).fn(allargs); |
paul@126 | 132 | } |
paul@126 | 133 | |
paul@126 | 134 | /* Error routines. */ |
paul@126 | 135 | |
paul@126 | 136 | __attr __unbound_method(__attr args[]) |
paul@126 | 137 | { |
paul@193 | 138 | __attr excargs[1]; |
paul@193 | 139 | __attr exc = __new___builtins___core_UnboundMethodInvocation(excargs); |
paul@193 | 140 | __Raise(exc); |
paul@193 | 141 | return __builtins___none_None; /* superfluous */ |
paul@126 | 142 | } |
paul@126 | 143 | |
paul@126 | 144 | /* Generic operations depending on specific program details. */ |
paul@126 | 145 | |
paul@126 | 146 | void __SETDEFAULT(__ref obj, int pos, __attr value) |
paul@126 | 147 | { |
paul@126 | 148 | __store_via_object(obj, __FUNCTION_INSTANCE_SIZE + pos, value); |
paul@126 | 149 | } |
paul@126 | 150 | |
paul@126 | 151 | __attr __GETDEFAULT(__ref obj, int pos) |
paul@126 | 152 | { |
paul@126 | 153 | return __load_via_object(obj, __FUNCTION_INSTANCE_SIZE + pos); |
paul@126 | 154 | } |
paul@144 | 155 | |
paul@144 | 156 | int __BOOL(__attr attr) |
paul@144 | 157 | { |
paul@164 | 158 | __attr args[2] = {{0, 0}, attr}; |
paul@164 | 159 | |
paul@164 | 160 | /* Invoke the bool function with the object and test against True. */ |
paul@164 | 161 | |
paul@164 | 162 | return __fn___builtins___boolean_bool(args).value == __builtins___boolean_True.value; |
paul@144 | 163 | } |