paul@126 | 1 | /* Operations depending on program specifics. |
paul@353 | 2 | |
paul@930 | 3 | Copyright (C) 2015-2019, 2021 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@778 | 31 | void __init(__ref obj, const __table * table, __ref cls) |
paul@778 | 32 | { |
paul@778 | 33 | obj->table = table; |
paul@778 | 34 | obj->pos = __INSTANCEPOS; |
paul@778 | 35 | __store_via_object(obj, __class__, __ATTRVALUE(cls)); |
paul@778 | 36 | } |
paul@778 | 37 | |
paul@756 | 38 | __attr __new(const __table * table, __ref cls, size_t size, int immutable) |
paul@126 | 39 | { |
paul@756 | 40 | __ref obj = (__ref) (immutable ? __ALLOCATEIM : __ALLOCATE)(1, size); |
paul@778 | 41 | __init(obj, table, cls); |
paul@757 | 42 | return __ATTRVALUE(obj); |
paul@577 | 43 | } |
paul@577 | 44 | |
paul@964 | 45 | __attr __new_stack(const __table * table, __ref cls, size_t size) |
paul@964 | 46 | { |
paul@964 | 47 | __attr attr = __stack_allocate(__stack, size); |
paul@964 | 48 | __init(__VALUE(attr), table, cls); |
paul@964 | 49 | return attr; |
paul@964 | 50 | } |
paul@964 | 51 | |
paul@757 | 52 | __attr __new_wrapper(__attr context, __attr attr) |
paul@577 | 53 | { |
paul@763 | 54 | return __new___builtins___core_wrapper(__NULL, context, attr); |
paul@126 | 55 | } |
paul@126 | 56 | |
paul@159 | 57 | /* Generic internal data allocation. */ |
paul@159 | 58 | |
paul@941 | 59 | __fragment *__new_fragment(__int n) |
paul@283 | 60 | { |
paul@283 | 61 | /* Allocate space for the list. */ |
paul@302 | 62 | |
paul@283 | 63 | __fragment *data = (__fragment *) __ALLOCATE(1, __FRAGMENT_SIZE(n)); |
paul@283 | 64 | |
paul@283 | 65 | /* The initial capacity is the same as the given size. */ |
paul@302 | 66 | |
paul@283 | 67 | data->size = 0; |
paul@283 | 68 | data->capacity = n; |
paul@283 | 69 | return data; |
paul@283 | 70 | } |
paul@283 | 71 | |
paul@941 | 72 | void __newdata_sequence(__int number, __fragment *data, __attr args[]) |
paul@159 | 73 | { |
paul@941 | 74 | __int i; |
paul@159 | 75 | |
paul@669 | 76 | /* Copy the given number of values. */ |
paul@159 | 77 | |
paul@770 | 78 | for (i = 0; i < number; i++) |
paul@964 | 79 | { |
paul@964 | 80 | data->attrs[i] = __RAWVALUE(0); |
paul@964 | 81 | __store_target(&data->attrs[i], args[i]); |
paul@964 | 82 | } |
paul@159 | 83 | |
paul@159 | 84 | data->size = number; |
paul@786 | 85 | } |
paul@786 | 86 | |
paul@941 | 87 | __attr __newdata_list(__int number, __attr args[]) |
paul@786 | 88 | { |
paul@786 | 89 | __attr self = __NEWINSTANCE(__builtins___list_list); |
paul@786 | 90 | __fragment *data = __new_fragment(number); |
paul@283 | 91 | |
paul@283 | 92 | /* Store a reference to the data in the object's __data__ attribute. */ |
paul@283 | 93 | |
paul@964 | 94 | __store_via_object_internal(__VALUE(self), __data__, (__attr) {.seqvalue=data}); |
paul@786 | 95 | __newdata_sequence(number, data, args); |
paul@778 | 96 | return self; |
paul@778 | 97 | } |
paul@778 | 98 | |
paul@941 | 99 | __attr __newdata_tuple(__int number, __attr args[]) |
paul@778 | 100 | { |
paul@778 | 101 | /* Allocate the tuple and fragment together. */ |
paul@778 | 102 | |
paul@785 | 103 | __attr self = __new(&__INSTANCETABLE(__builtins___tuple_tuple), |
paul@786 | 104 | &__builtins___tuple_tuple, |
paul@786 | 105 | __INSTANCESIZE(__builtins___tuple_tuple) + __FRAGMENT_SIZE(number), 0); |
paul@786 | 106 | __fragment *data = (__fragment *) ((void *) __VALUE(self) + __INSTANCESIZE(__builtins___tuple_tuple)); |
paul@778 | 107 | |
paul@786 | 108 | /* Store a reference to the data in the object's __data__ attribute. */ |
paul@778 | 109 | |
paul@964 | 110 | __store_via_object_internal(__VALUE(self), __data__, (__attr) {.seqvalue=data}); |
paul@786 | 111 | __newdata_sequence(number, data, args); |
paul@669 | 112 | return self; |
paul@283 | 113 | } |
paul@283 | 114 | |
paul@291 | 115 | #ifdef __HAVE___builtins___dict_dict |
paul@941 | 116 | __attr __newdata_dict(__int number, __attr args[]) |
paul@283 | 117 | { |
paul@778 | 118 | __attr self = __NEWINSTANCE(__builtins___dict_dict); |
paul@778 | 119 | |
paul@304 | 120 | /* Create a temporary list using the arguments. */ |
paul@283 | 121 | |
paul@790 | 122 | __attr tmp = __newdata_list(number, args); |
paul@283 | 123 | |
paul@304 | 124 | /* Call __init__ with the dict object and list argument. */ |
paul@283 | 125 | |
paul@669 | 126 | __fn___builtins___dict_dict___init__(self, tmp); |
paul@669 | 127 | return self; |
paul@159 | 128 | } |
paul@291 | 129 | #endif /* __HAVE___builtins___dict_dict */ |
paul@291 | 130 | |
paul@306 | 131 | /* Helpers for raising errors within common operations. */ |
paul@306 | 132 | |
paul@336 | 133 | void __raise_eof_error() |
paul@336 | 134 | { |
paul@382 | 135 | #ifdef __HAVE___builtins___exception_io_EOFError |
paul@669 | 136 | __Raise(__new___builtins___exception_io_EOFError(__NULL)); |
paul@382 | 137 | #endif /* __HAVE___builtins___exception_io_EOFError */ |
paul@336 | 138 | } |
paul@336 | 139 | |
paul@879 | 140 | void __raise_floating_point_error() |
paul@879 | 141 | { |
paul@879 | 142 | __Raise(__new___builtins___core_FloatingPointError(__NULL)); |
paul@879 | 143 | } |
paul@879 | 144 | |
paul@331 | 145 | void __raise_io_error(__attr value) |
paul@327 | 146 | { |
paul@382 | 147 | #ifdef __HAVE___builtins___exception_io_IOError |
paul@669 | 148 | __Raise(__new___builtins___exception_io_IOError(__NULL, value)); |
paul@382 | 149 | #endif /* __HAVE___builtins___exception_io_IOError */ |
paul@327 | 150 | } |
paul@327 | 151 | |
paul@306 | 152 | void __raise_memory_error() |
paul@306 | 153 | { |
paul@669 | 154 | __Raise(__new___builtins___core_MemoryError(__NULL)); |
paul@306 | 155 | } |
paul@306 | 156 | |
paul@379 | 157 | void __raise_os_error(__attr value, __attr arg) |
paul@379 | 158 | { |
paul@379 | 159 | #ifdef __HAVE___builtins___exception_system_OSError |
paul@669 | 160 | __Raise(__new___builtins___exception_system_OSError(__NULL, value, arg)); |
paul@379 | 161 | #endif /* __HAVE___builtins___exception_system_OSError */ |
paul@379 | 162 | } |
paul@379 | 163 | |
paul@306 | 164 | void __raise_overflow_error() |
paul@306 | 165 | { |
paul@669 | 166 | __Raise(__new___builtins___core_OverflowError(__NULL)); |
paul@306 | 167 | } |
paul@233 | 168 | |
paul@852 | 169 | void __raise_unbound_method_error() |
paul@852 | 170 | { |
paul@852 | 171 | __Raise(__new___builtins___core_UnboundMethodInvocation(__NULL)); |
paul@852 | 172 | } |
paul@852 | 173 | |
paul@233 | 174 | void __raise_type_error() |
paul@233 | 175 | { |
paul@669 | 176 | __Raise(__new___builtins___core_TypeError(__NULL)); |
paul@233 | 177 | } |
paul@233 | 178 | |
paul@850 | 179 | void __raise_underflow_error() |
paul@850 | 180 | { |
paul@850 | 181 | __Raise(__new___builtins___core_UnderflowError(__NULL)); |
paul@850 | 182 | } |
paul@850 | 183 | |
paul@928 | 184 | void __raise_value_error(__attr value) |
paul@928 | 185 | { |
paul@928 | 186 | #ifdef __HAVE___builtins___exception_base_ValueError |
paul@928 | 187 | __Raise(__new___builtins___exception_base_ValueError(__NULL, value)); |
paul@928 | 188 | #endif /* __HAVE___builtins___exception_base_ValueError */ |
paul@928 | 189 | } |
paul@928 | 190 | |
paul@306 | 191 | void __raise_zero_division_error() |
paul@260 | 192 | { |
paul@669 | 193 | __Raise(__new___builtins___core_ZeroDivisionError(__NULL)); |
paul@260 | 194 | } |
paul@260 | 195 | |
paul@317 | 196 | /* Helper for raising exception instances. */ |
paul@317 | 197 | |
paul@317 | 198 | __attr __ensure_instance(__attr arg) |
paul@317 | 199 | { |
paul@317 | 200 | /* Reserve space for the instance. */ |
paul@317 | 201 | |
paul@582 | 202 | __attr args[1] = {__NULL}; |
paul@317 | 203 | |
paul@317 | 204 | /* Return instances as provided. */ |
paul@317 | 205 | |
paul@757 | 206 | if (__is_instance(__VALUE(arg))) |
paul@317 | 207 | return arg; |
paul@317 | 208 | |
paul@317 | 209 | /* Invoke non-instances to produce instances. */ |
paul@317 | 210 | |
paul@317 | 211 | else |
paul@317 | 212 | return __invoke(arg, 0, 0, 0, 0, 1, args); |
paul@317 | 213 | } |
paul@317 | 214 | |
paul@126 | 215 | /* Generic invocation operations. */ |
paul@126 | 216 | |
paul@126 | 217 | /* Invoke the given callable, supplying keyword argument details in the given |
paul@126 | 218 | codes and arguments arrays, indicating the number of arguments described. |
paul@126 | 219 | The number of positional arguments is specified, and such arguments then |
paul@964 | 220 | follow as conventional function arguments. |
paul@964 | 221 | |
paul@964 | 222 | Typically, at least one argument is specified, starting with any context |
paul@964 | 223 | argument. |
paul@126 | 224 | */ |
paul@126 | 225 | |
paul@156 | 226 | __attr __invoke(__attr callable, int always_callable, |
paul@126 | 227 | unsigned int nkwargs, __param kwcodes[], __attr kwargs[], |
paul@126 | 228 | unsigned int nargs, __attr args[]) |
paul@126 | 229 | { |
paul@577 | 230 | /* Unwrap any wrapped function. */ |
paul@577 | 231 | |
paul@577 | 232 | __attr target = __unwrap_callable(callable); |
paul@577 | 233 | |
paul@126 | 234 | /* Obtain the __args__ special member, referencing the parameter table. */ |
paul@964 | 235 | |
paul@964 | 236 | const __ptable *ptable = __check_and_load_via_object(__VALUE(target), __args__).ptable; |
paul@964 | 237 | |
paul@126 | 238 | /* Refer to the table and minimum/maximum. */ |
paul@126 | 239 | |
paul@579 | 240 | const unsigned int min = ptable->min, max = ptable->max; |
paul@126 | 241 | |
paul@126 | 242 | /* Reserve enough space for the arguments. */ |
paul@126 | 243 | |
paul@661 | 244 | __attr *allargs = args, moreargs[max]; |
paul@126 | 245 | |
paul@126 | 246 | /* Traverse the arguments. */ |
paul@126 | 247 | |
paul@126 | 248 | unsigned int pos, kwpos; |
paul@126 | 249 | |
paul@126 | 250 | /* Check the number of arguments. */ |
paul@760 | 251 | |
paul@760 | 252 | if ((nargs == max) && (nkwargs == 0)) |
paul@760 | 253 | { |
paul@760 | 254 | /* pass */ |
paul@760 | 255 | } |
paul@760 | 256 | |
paul@546 | 257 | /* NOTE: Should use a more specific exception. */ |
paul@126 | 258 | |
paul@760 | 259 | else if ((min > (nargs + nkwargs)) || ((nargs + nkwargs) > max)) |
paul@760 | 260 | { |
paul@546 | 261 | __raise_type_error(); |
paul@760 | 262 | } |
paul@126 | 263 | |
paul@126 | 264 | /* Copy the arguments. */ |
paul@126 | 265 | |
paul@760 | 266 | else if (nargs < max) |
paul@661 | 267 | { |
paul@661 | 268 | allargs = moreargs; |
paul@126 | 269 | |
paul@661 | 270 | for (pos = 0; pos < nargs; pos++) |
paul@661 | 271 | allargs[pos] = args[pos]; |
paul@661 | 272 | |
paul@661 | 273 | /* Erase the remaining arguments. */ |
paul@126 | 274 | |
paul@661 | 275 | for (pos = nargs; pos < max; pos++) |
paul@758 | 276 | __SETNULL(allargs[pos]); |
paul@126 | 277 | |
paul@661 | 278 | /* Fill keyword arguments. */ |
paul@126 | 279 | |
paul@661 | 280 | for (kwpos = 0; kwpos < nkwargs; kwpos++) |
paul@661 | 281 | { |
paul@661 | 282 | pos = __HASPARAM(ptable, kwcodes[kwpos].pos, kwcodes[kwpos].code); |
paul@126 | 283 | |
paul@661 | 284 | /* Check the table entry against the supplied argument details. |
paul@661 | 285 | Set the argument but only if it does not overwrite positional |
paul@661 | 286 | arguments. */ |
paul@126 | 287 | |
paul@661 | 288 | if ((pos == -1) || (pos < nargs)) |
paul@964 | 289 | { |
paul@964 | 290 | /* NOTE: Should use a more specific exception. */ |
paul@964 | 291 | |
paul@661 | 292 | __raise_type_error(); |
paul@964 | 293 | } |
paul@126 | 294 | |
paul@661 | 295 | /* Set the argument using the appropriate position. */ |
paul@126 | 296 | |
paul@661 | 297 | allargs[pos] = kwargs[kwpos]; |
paul@661 | 298 | } |
paul@126 | 299 | |
paul@661 | 300 | /* Fill the defaults. */ |
paul@126 | 301 | |
paul@661 | 302 | for (pos = nargs; pos < max; pos++) |
paul@661 | 303 | { |
paul@758 | 304 | if (__ISNULL(allargs[pos])) |
paul@757 | 305 | allargs[pos] = __GETDEFAULT(__VALUE(target), pos - min); |
paul@661 | 306 | } |
paul@126 | 307 | } |
paul@126 | 308 | |
paul@664 | 309 | /* Call with the prepared arguments via a special adaptor function that |
paul@964 | 310 | converts the array to an argument list. The context argument occupies |
paul@964 | 311 | position #0. */ |
paul@126 | 312 | |
paul@664 | 313 | return __call_with_args( |
paul@664 | 314 | always_callable ? |
paul@764 | 315 | __get_function_unwrapped(allargs[0], target) : |
paul@764 | 316 | __check_and_get_function_unwrapped(allargs[0], target), |
paul@664 | 317 | allargs, max); |
paul@126 | 318 | } |
paul@126 | 319 | |
paul@126 | 320 | /* Error routines. */ |
paul@126 | 321 | |
paul@664 | 322 | __attr __unbound_method(__attr __self) |
paul@126 | 323 | { |
paul@669 | 324 | __Raise(__new___builtins___core_UnboundMethodInvocation(__NULL)); |
paul@193 | 325 | return __builtins___none_None; /* superfluous */ |
paul@126 | 326 | } |
paul@126 | 327 | |
paul@126 | 328 | /* Generic operations depending on specific program details. */ |
paul@126 | 329 | |
paul@126 | 330 | void __SETDEFAULT(__ref obj, int pos, __attr value) |
paul@126 | 331 | { |
paul@624 | 332 | __store_via_object__(obj, __FUNCTION_INSTANCE_SIZE + pos, value); |
paul@126 | 333 | } |
paul@126 | 334 | |
paul@126 | 335 | __attr __GETDEFAULT(__ref obj, int pos) |
paul@126 | 336 | { |
paul@624 | 337 | return __load_via_object__(obj, __FUNCTION_INSTANCE_SIZE + pos); |
paul@126 | 338 | } |
paul@144 | 339 | |
paul@144 | 340 | int __BOOL(__attr attr) |
paul@144 | 341 | { |
paul@893 | 342 | __ref value; |
paul@893 | 343 | |
paul@941 | 344 | /* Non-zero integers yield a non-zero result. Since the integer type can be |
paul@941 | 345 | larger than int, a conversion is performed. */ |
paul@164 | 346 | |
paul@893 | 347 | if (__INTEGER(attr)) |
paul@941 | 348 | return __TOINT(attr) ? 1 : 0; |
paul@893 | 349 | |
paul@893 | 350 | /* Test against True and False explicitly. If necessary, invoke the bool |
paul@893 | 351 | function with the object and test against True. */ |
paul@893 | 352 | |
paul@952 | 353 | value = __VALUE(attr); |
paul@834 | 354 | |
paul@850 | 355 | return value == (__ref) &__predefined___builtins___boolean_True ? 1 : |
paul@850 | 356 | value == (__ref) &__predefined___builtins___boolean_False ? 0 : |
paul@964 | 357 | __VALUE(__fn___builtins___boolean_bool(__NULL, attr)) == |
paul@964 | 358 | (__ref) &__predefined___builtins___boolean_True; |
paul@144 | 359 | } |
paul@947 | 360 | |
paul@947 | 361 | /* Conversion of trailing data to an integer. */ |
paul@947 | 362 | |
paul@947 | 363 | __int __TOINT(__attr attr) |
paul@947 | 364 | { |
paul@947 | 365 | return __get_trailing_data(attr, __builtins___int_int); |
paul@947 | 366 | } |
paul@947 | 367 | |
paul@947 | 368 | /* Instance test functions, to be replaced by tagged pointer usage. */ |
paul@947 | 369 | |
paul@947 | 370 | int __INTEGER(__attr attr) |
paul@947 | 371 | { |
paul@947 | 372 | __ref value = __VALUE(attr); |
paul@947 | 373 | return __get_class(value) == &__builtins___int_int; |
paul@947 | 374 | } |
paul@947 | 375 | |
paul@947 | 376 | int __FLOAT(__attr attr) |
paul@947 | 377 | { |
paul@947 | 378 | __ref value = __VALUE(attr); |
paul@947 | 379 | return __get_class(value) == &__builtins___float_float; |
paul@947 | 380 | } |