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