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@126 | 10 | #include <stdio.h> |
paul@126 | 11 | |
paul@126 | 12 | /* Generic instantiation operations, defining common members. */ |
paul@126 | 13 | |
paul@126 | 14 | __attr __new(const __table * table, __ref cls, int size) |
paul@126 | 15 | { |
paul@126 | 16 | __ref obj = calloc(1, size); |
paul@126 | 17 | __attr self = {obj, obj}; |
paul@126 | 18 | __attr tmp = {0, cls}; |
paul@126 | 19 | obj->table = table; |
paul@126 | 20 | __store_via_object(obj, __pos___class__, tmp); |
paul@126 | 21 | return self; |
paul@126 | 22 | } |
paul@126 | 23 | |
paul@126 | 24 | /* Generic invocation operations. */ |
paul@126 | 25 | |
paul@126 | 26 | /* Invoke the given callable, supplying keyword argument details in the given |
paul@126 | 27 | codes and arguments arrays, indicating the number of arguments described. |
paul@126 | 28 | The number of positional arguments is specified, and such arguments then |
paul@126 | 29 | follow as conventional function arguments. Typically, at least one argument |
paul@126 | 30 | is specified, starting with any context argument. |
paul@126 | 31 | */ |
paul@126 | 32 | |
paul@156 | 33 | __attr __invoke(__attr callable, int always_callable, |
paul@126 | 34 | unsigned int nkwargs, __param kwcodes[], __attr kwargs[], |
paul@126 | 35 | unsigned int nargs, __attr args[]) |
paul@126 | 36 | { |
paul@126 | 37 | /* Obtain the __args__ special member, referencing the parameter table. */ |
paul@126 | 38 | |
paul@126 | 39 | __attr minparams = __load_via_object(callable.value, __pos___args__); |
paul@126 | 40 | |
paul@126 | 41 | /* Refer to the table and minimum/maximum. */ |
paul@126 | 42 | |
paul@126 | 43 | const __ptable *ptable = minparams.ptable; |
paul@126 | 44 | const unsigned int min = minparams.min, max = ptable->size; |
paul@126 | 45 | |
paul@126 | 46 | /* Reserve enough space for the arguments. */ |
paul@126 | 47 | |
paul@126 | 48 | __attr allargs[max]; |
paul@126 | 49 | |
paul@126 | 50 | /* Traverse the arguments. */ |
paul@126 | 51 | |
paul@126 | 52 | unsigned int pos, kwpos; |
paul@126 | 53 | |
paul@126 | 54 | /* Check the number of arguments. */ |
paul@126 | 55 | |
paul@126 | 56 | if ((min > (nargs + nkwargs)) || ((nargs + nkwargs) > max)) |
paul@148 | 57 | return __NULL; |
paul@126 | 58 | |
paul@126 | 59 | /* Copy the arguments. */ |
paul@126 | 60 | |
paul@126 | 61 | for (pos = 0; pos < nargs; pos++) |
paul@126 | 62 | allargs[pos] = args[pos]; |
paul@126 | 63 | |
paul@126 | 64 | /* Erase the remaining arguments. */ |
paul@126 | 65 | |
paul@126 | 66 | for (pos = nargs; pos < max; pos++) |
paul@126 | 67 | { |
paul@126 | 68 | allargs[pos].value = 0; |
paul@126 | 69 | } |
paul@126 | 70 | |
paul@126 | 71 | /* Fill keyword arguments. */ |
paul@126 | 72 | |
paul@126 | 73 | for (kwpos = 0; kwpos < nkwargs; kwpos++) |
paul@126 | 74 | { |
paul@126 | 75 | pos = __HASPARAM(ptable, kwcodes[kwpos].pos, kwcodes[kwpos].code); |
paul@126 | 76 | |
paul@126 | 77 | /* Check the table entry against the supplied argument details. |
paul@126 | 78 | Set the argument but only if it does not overwrite positional |
paul@126 | 79 | arguments. */ |
paul@126 | 80 | |
paul@126 | 81 | if ((pos == -1) || (pos < nargs)) |
paul@148 | 82 | return __NULL; |
paul@126 | 83 | |
paul@126 | 84 | /* Set the argument using the appropriate position. */ |
paul@126 | 85 | |
paul@126 | 86 | allargs[pos] = kwargs[kwpos]; |
paul@126 | 87 | } |
paul@126 | 88 | |
paul@126 | 89 | /* Fill the defaults. */ |
paul@126 | 90 | |
paul@126 | 91 | for (pos = nargs; pos < max; pos++) |
paul@126 | 92 | { |
paul@126 | 93 | if (allargs[pos].value == 0) |
paul@126 | 94 | allargs[pos] = __GETDEFAULT(callable.value, pos - nargs); |
paul@126 | 95 | } |
paul@126 | 96 | |
paul@126 | 97 | /* Call with the prepared arguments. */ |
paul@126 | 98 | |
paul@156 | 99 | return (always_callable ? __load_via_object(callable.value, __pos___fn__) |
paul@156 | 100 | : __check_and_load_via_object(callable.value, __pos___fn__, __code___fn__) |
paul@156 | 101 | ).fn(allargs); |
paul@126 | 102 | } |
paul@126 | 103 | |
paul@126 | 104 | /* Error routines. */ |
paul@126 | 105 | |
paul@126 | 106 | __attr __unbound_method(__attr args[]) |
paul@126 | 107 | { |
paul@126 | 108 | /* NOTE: Should raise an exception. */ |
paul@126 | 109 | |
paul@126 | 110 | fprintf(stderr, "Unbound method called!\n"); |
paul@126 | 111 | exit(1); |
paul@126 | 112 | } |
paul@126 | 113 | |
paul@126 | 114 | /* Generic operations depending on specific program details. */ |
paul@126 | 115 | |
paul@126 | 116 | void __SETDEFAULT(__ref obj, int pos, __attr value) |
paul@126 | 117 | { |
paul@126 | 118 | __store_via_object(obj, __FUNCTION_INSTANCE_SIZE + pos, value); |
paul@126 | 119 | } |
paul@126 | 120 | |
paul@126 | 121 | __attr __GETDEFAULT(__ref obj, int pos) |
paul@126 | 122 | { |
paul@126 | 123 | return __load_via_object(obj, __FUNCTION_INSTANCE_SIZE + pos); |
paul@126 | 124 | } |
paul@144 | 125 | |
paul@144 | 126 | int __BOOL(__attr attr) |
paul@144 | 127 | { |
paul@144 | 128 | return attr.value == __builtins___bool_True.value; |
paul@144 | 129 | } |