Lichen

Annotated templates/native/list.c

1018:ff5db4d0d5d4
14 months ago Paul Boddie Fixed low-level attribute initialisation, avoiding object copying tests. value-replacement
paul@354 1
/* Native functions for list operations.
paul@354 2
paul@971 3
Copyright (C) 2016, 2017, 2021, 2023 Paul Boddie <paul@boddie.org.uk>
paul@354 4
paul@354 5
This program is free software; you can redistribute it and/or modify it under
paul@354 6
the terms of the GNU General Public License as published by the Free Software
paul@354 7
Foundation; either version 3 of the License, or (at your option) any later
paul@354 8
version.
paul@354 9
paul@354 10
This program is distributed in the hope that it will be useful, but WITHOUT
paul@354 11
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@354 12
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@354 13
details.
paul@354 14
paul@354 15
You should have received a copy of the GNU General Public License along with
paul@354 16
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@354 17
*/
paul@354 18
paul@354 19
#include "native/common.h"
paul@354 20
#include "types.h"
paul@354 21
#include "exceptions.h"
paul@354 22
#include "ops.h"
paul@354 23
#include "progconsts.h"
paul@354 24
#include "progops.h"
paul@354 25
#include "progtypes.h"
paul@354 26
#include "main.h"
paul@354 27
paul@354 28
/* List operations. */
paul@354 29
paul@971 30
__attr __fn_native_list_list_init(__attr __result, __attr __self, __attr size)
paul@354 31
{
paul@758 32
    /* size interpreted as int */
paul@941 33
    __int n = __TOINT(size);
paul@569 34
    __attr attr = {.seqvalue=__new_fragment(n)};
paul@354 35
paul@354 36
    /* Return the __data__ attribute. */
paul@354 37
    return attr;
paul@354 38
}
paul@354 39
paul@971 40
__attr __fn_native_list_list_setsize(__attr __result, __attr __self, __attr _data, __attr size)
paul@354 41
{
paul@664 42
    /* _data interpreted as list.__data__ */
paul@664 43
    __fragment *data = _data.seqvalue;
paul@758 44
    /* size interpreted as int */
paul@941 45
    __int n = __TOINT(size);
paul@354 46
paul@354 47
    data->size = n;
paul@354 48
    return __builtins___none_None;
paul@354 49
}
paul@354 50
paul@971 51
__attr __fn_native_list_list_append(__attr __result, __attr __self, __attr self, __attr value)
paul@354 52
{
paul@354 53
    /* self.__data__ interpreted as list */
paul@763 54
    __fragment *data = __load_via_object(__VALUE(self), __data__).seqvalue;
paul@354 55
    __fragment *newdata = __fragment_append(data, value);
paul@354 56
paul@354 57
    /* Replace the __data__ attribute if appropriate. */
paul@354 58
    if (newdata != data)
paul@1018 59
        __store_via_attr_ref__(__get_object_attr_ref(__VALUE(self), __data__), ((__attr) {.seqvalue=newdata}));
paul@354 60
    return __builtins___none_None;
paul@354 61
}
paul@354 62
paul@971 63
__attr __fn_native_list_list_concat(__attr __result, __attr __self, __attr self, __attr other)
paul@354 64
{
paul@664 65
    /* self, interpreted as list, other interpreted as list.__data__ */
paul@763 66
    __fragment *data = __load_via_object(__VALUE(self), __data__).seqvalue;
paul@664 67
    __fragment *other_data = other.seqvalue;
paul@354 68
    __fragment *newdata = data;
paul@941 69
    __int size = data->size, capacity = data->capacity;
paul@941 70
    __int other_size = other_data->size;
paul@941 71
    __int i, j, n = size + other_size;
paul@354 72
paul@354 73
    /* Re-allocate the fragment if the capacity has been reached. */
paul@659 74
    if (n >= capacity)
paul@354 75
    {
paul@354 76
        newdata = (__fragment *) __REALLOCATE(data, __FRAGMENT_SIZE(n));
paul@354 77
        newdata->capacity = n;
paul@354 78
    }
paul@354 79
paul@354 80
    /* Copy the elements from the other list and increment the list size. */
paul@354 81
    for (i = size, j = 0; j < other_size; i++, j++)
paul@354 82
        newdata->attrs[i] = other_data->attrs[j];
paul@354 83
    newdata->size = n;
paul@354 84
paul@354 85
    /* Replace the __data__ attribute if appropriate. */
paul@354 86
    if (newdata != data)
paul@1018 87
        __store_via_attr_ref__(__get_object_attr_ref(__VALUE(self), __data__), ((__attr) {.seqvalue=newdata}));
paul@354 88
    return __builtins___none_None;
paul@354 89
}
paul@354 90
paul@971 91
__attr __fn_native_list_list_len(__attr __result, __attr __self, __attr _data)
paul@354 92
{
paul@664 93
    /* _data interpreted as list.__data__ */
paul@941 94
    __int size = _data.seqvalue->size;
paul@354 95
paul@354 96
    /* Return the new integer. */
paul@354 97
    return __new_int(size);
paul@354 98
}
paul@354 99
paul@971 100
__attr __fn_native_list_list_nonempty(__attr __result, __attr __self, __attr _data)
paul@354 101
{
paul@664 102
    return _data.seqvalue->size ? __builtins___boolean_True : __builtins___boolean_False;
paul@354 103
}
paul@354 104
paul@971 105
__attr __fn_native_list_list_element(__attr __result, __attr __self, __attr _data, __attr index)
paul@354 106
{
paul@664 107
    /* _data interpreted as list.__data__ */
paul@664 108
    __attr *elements = _data.seqvalue->attrs;
paul@758 109
    /* index interpreted as int */
paul@941 110
    __int i = __TOINT(index);
paul@354 111
paul@354 112
    return elements[i];
paul@354 113
}
paul@354 114
paul@971 115
__attr __fn_native_list_list_setelement(__attr __result, __attr __self, __attr _data, __attr index, __attr value)
paul@354 116
{
paul@664 117
    /* _data interpreted as list.__data__ */
paul@664 118
    __attr *elements = _data.seqvalue->attrs;
paul@758 119
    /* index interpreted as int */
paul@941 120
    __int i = __TOINT(index);
paul@354 121
paul@354 122
    /* Set the element. */
paul@664 123
    elements[i] = value;
paul@354 124
    return __builtins___none_None;
paul@354 125
}
paul@354 126
paul@354 127
/* Module initialisation. */
paul@354 128
paul@354 129
void __main_native_list()
paul@354 130
{
paul@354 131
}