paul@126 | 1 | /* Operations depending on program specifics. |
paul@353 | 2 | |
paul@523 | 3 | Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@353 | 4 | |
paul@353 | 5 | This program is free software; you can redistribute it and/or modify it under |
paul@353 | 6 | the terms of the GNU General Public License as published by the Free Software |
paul@353 | 7 | Foundation; either version 3 of the License, or (at your option) any later |
paul@353 | 8 | version. |
paul@353 | 9 | |
paul@353 | 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@353 | 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@353 | 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@353 | 13 | details. |
paul@353 | 14 | |
paul@353 | 15 | You should have received a copy of the GNU General Public License along with |
paul@353 | 16 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@126 | 17 | */ |
paul@126 | 18 | |
paul@126 | 19 | #include <stdlib.h> |
paul@126 | 20 | #include "types.h" |
paul@126 | 21 | #include "ops.h" |
paul@126 | 22 | #include "progconsts.h" |
paul@126 | 23 | #include "progops.h" |
paul@126 | 24 | #include "progtypes.h" |
paul@164 | 25 | #include "main.h" |
paul@193 | 26 | #include "exceptions.h" |
paul@664 | 27 | #include "calls.h" |
paul@126 | 28 | |
paul@126 | 29 | /* Generic instantiation operations, defining common members. */ |
paul@126 | 30 | |
paul@162 | 31 | __attr __new(const __table * table, __ref cls, size_t size) |
paul@126 | 32 | { |
paul@260 | 33 | __ref obj = (__ref) __ALLOCATE(1, size); |
paul@126 | 34 | obj->table = table; |
paul@649 | 35 | obj->pos = __INSTANCEPOS; |
paul@669 | 36 | __store_via_object(obj, __class__, __ATTRVALUE(cls)); |
paul@577 | 37 | return (__attr) {.value=obj}; |
paul@577 | 38 | } |
paul@577 | 39 | |
paul@577 | 40 | __attr __new_wrapper(__ref context, __attr attr) |
paul@577 | 41 | { |
paul@669 | 42 | return __new___builtins___core_wrapper(__NULL, __ATTRVALUE(context), attr); |
paul@126 | 43 | } |
paul@126 | 44 | |
paul@159 | 45 | /* Generic internal data allocation. */ |
paul@159 | 46 | |
paul@283 | 47 | __fragment *__new_fragment(unsigned int n) |
paul@283 | 48 | { |
paul@283 | 49 | /* Allocate space for the list. */ |
paul@302 | 50 | |
paul@283 | 51 | __fragment *data = (__fragment *) __ALLOCATE(1, __FRAGMENT_SIZE(n)); |
paul@283 | 52 | |
paul@283 | 53 | /* The initial capacity is the same as the given size. */ |
paul@302 | 54 | |
paul@283 | 55 | data->size = 0; |
paul@283 | 56 | data->capacity = n; |
paul@283 | 57 | return data; |
paul@283 | 58 | } |
paul@283 | 59 | |
paul@669 | 60 | __attr __newdata_sequence(__attr self, __attr args[], unsigned int number) |
paul@159 | 61 | { |
paul@206 | 62 | /* Calculate the size of the fragment. */ |
paul@206 | 63 | |
paul@283 | 64 | __fragment *data = __new_fragment(number); |
paul@569 | 65 | __attr attr = {.seqvalue=data}; |
paul@669 | 66 | unsigned int i; |
paul@159 | 67 | |
paul@669 | 68 | /* Copy the given number of values. */ |
paul@159 | 69 | |
paul@669 | 70 | for (i = 0; i <= number; i++) |
paul@669 | 71 | data->attrs[i] = args[i]; |
paul@159 | 72 | |
paul@159 | 73 | data->size = number; |
paul@283 | 74 | |
paul@283 | 75 | /* Store a reference to the data in the object's __data__ attribute. */ |
paul@283 | 76 | |
paul@669 | 77 | __store_via_object(self.value, __data__, attr); |
paul@669 | 78 | return self; |
paul@283 | 79 | } |
paul@283 | 80 | |
paul@291 | 81 | #ifdef __HAVE___builtins___dict_dict |
paul@669 | 82 | __attr __newdata_mapping(__attr self, __attr args[], unsigned int number) |
paul@283 | 83 | { |
paul@304 | 84 | /* Create a temporary list using the arguments. */ |
paul@283 | 85 | |
paul@669 | 86 | __attr tmp = __newliteral___builtins___list_list(args, number); |
paul@283 | 87 | |
paul@304 | 88 | /* Call __init__ with the dict object and list argument. */ |
paul@283 | 89 | |
paul@669 | 90 | __fn___builtins___dict_dict___init__(self, tmp); |
paul@669 | 91 | return self; |
paul@159 | 92 | } |
paul@291 | 93 | #endif /* __HAVE___builtins___dict_dict */ |
paul@291 | 94 | |
paul@306 | 95 | /* Helpers for raising errors within common operations. */ |
paul@306 | 96 | |
paul@336 | 97 | void __raise_eof_error() |
paul@336 | 98 | { |
paul@382 | 99 | #ifdef __HAVE___builtins___exception_io_EOFError |
paul@669 | 100 | __Raise(__new___builtins___exception_io_EOFError(__NULL)); |
paul@382 | 101 | #endif /* __HAVE___builtins___exception_io_EOFError */ |
paul@336 | 102 | } |
paul@336 | 103 | |
paul@331 | 104 | void __raise_io_error(__attr value) |
paul@327 | 105 | { |
paul@382 | 106 | #ifdef __HAVE___builtins___exception_io_IOError |
paul@669 | 107 | __Raise(__new___builtins___exception_io_IOError(__NULL, value)); |
paul@382 | 108 | #endif /* __HAVE___builtins___exception_io_IOError */ |
paul@327 | 109 | } |
paul@327 | 110 | |
paul@306 | 111 | void __raise_memory_error() |
paul@306 | 112 | { |
paul@669 | 113 | __Raise(__new___builtins___core_MemoryError(__NULL)); |
paul@306 | 114 | } |
paul@306 | 115 | |
paul@379 | 116 | void __raise_os_error(__attr value, __attr arg) |
paul@379 | 117 | { |
paul@379 | 118 | #ifdef __HAVE___builtins___exception_system_OSError |
paul@669 | 119 | __Raise(__new___builtins___exception_system_OSError(__NULL, value, arg)); |
paul@379 | 120 | #endif /* __HAVE___builtins___exception_system_OSError */ |
paul@379 | 121 | } |
paul@379 | 122 | |
paul@306 | 123 | void __raise_overflow_error() |
paul@306 | 124 | { |
paul@669 | 125 | __Raise(__new___builtins___core_OverflowError(__NULL)); |
paul@306 | 126 | } |
paul@233 | 127 | |
paul@233 | 128 | void __raise_type_error() |
paul@233 | 129 | { |
paul@669 | 130 | __Raise(__new___builtins___core_TypeError(__NULL)); |
paul@233 | 131 | } |
paul@233 | 132 | |
paul@306 | 133 | void __raise_zero_division_error() |
paul@260 | 134 | { |
paul@669 | 135 | __Raise(__new___builtins___core_ZeroDivisionError(__NULL)); |
paul@260 | 136 | } |
paul@260 | 137 | |
paul@317 | 138 | /* Helper for raising exception instances. */ |
paul@317 | 139 | |
paul@317 | 140 | __attr __ensure_instance(__attr arg) |
paul@317 | 141 | { |
paul@317 | 142 | /* Reserve space for the instance. */ |
paul@317 | 143 | |
paul@582 | 144 | __attr args[1] = {__NULL}; |
paul@317 | 145 | |
paul@317 | 146 | /* Return instances as provided. */ |
paul@317 | 147 | |
paul@317 | 148 | if (__is_instance(arg.value)) |
paul@317 | 149 | return arg; |
paul@317 | 150 | |
paul@317 | 151 | /* Invoke non-instances to produce instances. */ |
paul@317 | 152 | |
paul@317 | 153 | else |
paul@317 | 154 | return __invoke(arg, 0, 0, 0, 0, 1, args); |
paul@317 | 155 | } |
paul@317 | 156 | |
paul@126 | 157 | /* Generic invocation operations. */ |
paul@126 | 158 | |
paul@126 | 159 | /* Invoke the given callable, supplying keyword argument details in the given |
paul@126 | 160 | codes and arguments arrays, indicating the number of arguments described. |
paul@126 | 161 | The number of positional arguments is specified, and such arguments then |
paul@126 | 162 | follow as conventional function arguments. Typically, at least one argument |
paul@126 | 163 | is specified, starting with any context argument. |
paul@126 | 164 | */ |
paul@126 | 165 | |
paul@156 | 166 | __attr __invoke(__attr callable, int always_callable, |
paul@126 | 167 | unsigned int nkwargs, __param kwcodes[], __attr kwargs[], |
paul@126 | 168 | unsigned int nargs, __attr args[]) |
paul@126 | 169 | { |
paul@577 | 170 | /* Unwrap any wrapped function. */ |
paul@577 | 171 | |
paul@577 | 172 | __attr target = __unwrap_callable(callable); |
paul@577 | 173 | |
paul@126 | 174 | /* Obtain the __args__ special member, referencing the parameter table. */ |
paul@126 | 175 | /* Refer to the table and minimum/maximum. */ |
paul@126 | 176 | |
paul@624 | 177 | const __ptable *ptable = __check_and_load_via_object(target.value, __args__).ptable; |
paul@579 | 178 | const unsigned int min = ptable->min, max = ptable->max; |
paul@126 | 179 | |
paul@126 | 180 | /* Reserve enough space for the arguments. */ |
paul@126 | 181 | |
paul@661 | 182 | __attr *allargs = args, moreargs[max]; |
paul@126 | 183 | |
paul@126 | 184 | /* Traverse the arguments. */ |
paul@126 | 185 | |
paul@126 | 186 | unsigned int pos, kwpos; |
paul@126 | 187 | |
paul@126 | 188 | /* Check the number of arguments. */ |
paul@546 | 189 | /* NOTE: Should use a more specific exception. */ |
paul@126 | 190 | |
paul@126 | 191 | if ((min > (nargs + nkwargs)) || ((nargs + nkwargs) > max)) |
paul@546 | 192 | __raise_type_error(); |
paul@126 | 193 | |
paul@126 | 194 | /* Copy the arguments. */ |
paul@126 | 195 | |
paul@661 | 196 | if (nargs < max) |
paul@661 | 197 | { |
paul@661 | 198 | allargs = moreargs; |
paul@126 | 199 | |
paul@661 | 200 | for (pos = 0; pos < nargs; pos++) |
paul@661 | 201 | allargs[pos] = args[pos]; |
paul@661 | 202 | |
paul@661 | 203 | /* Erase the remaining arguments. */ |
paul@126 | 204 | |
paul@661 | 205 | for (pos = nargs; pos < max; pos++) |
paul@661 | 206 | allargs[pos].value = 0; |
paul@126 | 207 | |
paul@661 | 208 | /* Fill keyword arguments. */ |
paul@126 | 209 | |
paul@661 | 210 | for (kwpos = 0; kwpos < nkwargs; kwpos++) |
paul@661 | 211 | { |
paul@661 | 212 | pos = __HASPARAM(ptable, kwcodes[kwpos].pos, kwcodes[kwpos].code); |
paul@126 | 213 | |
paul@661 | 214 | /* Check the table entry against the supplied argument details. |
paul@661 | 215 | Set the argument but only if it does not overwrite positional |
paul@661 | 216 | arguments. */ |
paul@661 | 217 | /* NOTE: Should use a more specific exception. */ |
paul@126 | 218 | |
paul@661 | 219 | if ((pos == -1) || (pos < nargs)) |
paul@661 | 220 | __raise_type_error(); |
paul@126 | 221 | |
paul@661 | 222 | /* Set the argument using the appropriate position. */ |
paul@126 | 223 | |
paul@661 | 224 | allargs[pos] = kwargs[kwpos]; |
paul@661 | 225 | } |
paul@126 | 226 | |
paul@661 | 227 | /* Fill the defaults. */ |
paul@126 | 228 | |
paul@661 | 229 | for (pos = nargs; pos < max; pos++) |
paul@661 | 230 | { |
paul@661 | 231 | if (allargs[pos].value == 0) |
paul@661 | 232 | allargs[pos] = __GETDEFAULT(target.value, pos - min); |
paul@661 | 233 | } |
paul@126 | 234 | } |
paul@126 | 235 | |
paul@664 | 236 | /* Call with the prepared arguments via a special adaptor function that |
paul@664 | 237 | converts the array to an argument list. */ |
paul@126 | 238 | |
paul@664 | 239 | return __call_with_args( |
paul@664 | 240 | always_callable ? |
paul@664 | 241 | __get_function(allargs[0].value, target) : |
paul@664 | 242 | __check_and_get_function(allargs[0].value, target), |
paul@664 | 243 | allargs, max); |
paul@126 | 244 | } |
paul@126 | 245 | |
paul@126 | 246 | /* Error routines. */ |
paul@126 | 247 | |
paul@664 | 248 | __attr __unbound_method(__attr __self) |
paul@126 | 249 | { |
paul@669 | 250 | __Raise(__new___builtins___core_UnboundMethodInvocation(__NULL)); |
paul@193 | 251 | return __builtins___none_None; /* superfluous */ |
paul@126 | 252 | } |
paul@126 | 253 | |
paul@126 | 254 | /* Generic operations depending on specific program details. */ |
paul@126 | 255 | |
paul@126 | 256 | void __SETDEFAULT(__ref obj, int pos, __attr value) |
paul@126 | 257 | { |
paul@624 | 258 | __store_via_object__(obj, __FUNCTION_INSTANCE_SIZE + pos, value); |
paul@126 | 259 | } |
paul@126 | 260 | |
paul@126 | 261 | __attr __GETDEFAULT(__ref obj, int pos) |
paul@126 | 262 | { |
paul@624 | 263 | return __load_via_object__(obj, __FUNCTION_INSTANCE_SIZE + pos); |
paul@126 | 264 | } |
paul@144 | 265 | |
paul@144 | 266 | int __BOOL(__attr attr) |
paul@144 | 267 | { |
paul@164 | 268 | /* Invoke the bool function with the object and test against True. */ |
paul@164 | 269 | |
paul@656 | 270 | return (attr.value == __builtins___boolean_True.value) || |
paul@664 | 271 | (__fn___builtins___boolean_bool(__NULL, attr).value == __builtins___boolean_True.value); |
paul@144 | 272 | } |