1 /* Common operations for native functions. 2 3 Copyright (C) 2016, 2017, 2018, 2021, 2023 Paul Boddie <paul@boddie.org.uk> 4 5 This program is free software; you can redistribute it and/or modify it under 6 the terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3 of the License, or (at your option) any later 8 version. 9 10 This program is distributed in the hope that it will be useful, but WITHOUT 11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 details. 14 15 You should have received a copy of the GNU General Public License along with 16 this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #include "types.h" 20 #include "exceptions.h" 21 #include "ops.h" 22 #include "progconsts.h" 23 #include "progops.h" 24 #include "progtypes.h" 25 #include "main.h" 26 27 /* Utility functions. */ 28 29 __attr __new_str(char *s, __int size) 30 { 31 /* Create a new string and mutate the __data__, __size__ and __key__ attributes. */ 32 __attr attr = __NEWINSTANCE(__builtins___str_str); 33 __store_via_object(__VALUE(attr), __data__, (__attr) {.strvalue=s}); 34 __store_via_object(__VALUE(attr), __size__, (__attr) {.sizevalue=size}); 35 __store_via_object(__VALUE(attr), __key__, __NULL); 36 return attr; 37 } 38 39 __attr __new_list(__fragment *f) 40 { 41 /* Create a new list and mutate the __data__ attribute. */ 42 __attr attr = __NEWINSTANCE(__builtins___list_list); 43 __store_via_object(__VALUE(attr), __data__, (__attr) {.seqvalue=f}); 44 return attr; 45 } 46 47 /* Temporary result target. */ 48 49 __attr __new_float(__attr __result, double n) 50 { 51 __attr attr; 52 53 /* Identify any suitable target for value replacement. The assumption is 54 that locally referenced float objects are always replaceable since they 55 will be appropriately initialised. */ 56 57 __ref obj = __VALUE(__result); 58 int replace = (obj != NULL) && __is_instance(obj) && (__get_class(obj) == &__builtins___float_float); 59 60 if (replace) 61 attr = __result; 62 63 /* Alternatively, create a new float. */ 64 else 65 attr = __NEWINSTANCEIM(__builtins___float_float); 66 67 /* Set the trailing data, replacing an existing value if applicable. */ 68 __set_trailing_data(attr, __builtins___float_float, n); 69 70 if (replace) 71 return __REPLACEMENT(attr); 72 else 73 return attr; 74 } 75 76 __fragment *__fragment_append(__fragment *data, __attr value) 77 { 78 __fragment *newdata = data; 79 __int size = data->size, capacity = data->capacity; 80 __int n; 81 82 /* Re-allocate the fragment if the capacity has been reached. */ 83 if (size >= capacity) 84 { 85 /* NOTE: Consider various restrictions on capacity increases. */ 86 n = capacity ? capacity * 2 : 1; 87 newdata = (__fragment *) __REALLOCATE(data, __FRAGMENT_SIZE(n)); 88 newdata->capacity = n; 89 } 90 91 /* Insert the new element and increment the list size. */ 92 newdata->attrs[size] = value; 93 newdata->size = size + 1; 94 95 return newdata; 96 }