Lichen

Annotated templates/native/list.c

610:2c1ae8f292a9
2017-02-22 Paul Boddie Fixed the testing of attribute usage. method-wrapper-for-context
paul@354 1
/* Native functions for list operations.
paul@354 2
paul@354 3
Copyright (C) 2016 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@354 30
__attr __fn_native_list_list_init(__attr __args[])
paul@354 31
{
paul@354 32
    __attr * const size = &__args[1];
paul@354 33
    /* size.__data__ interpreted as int */
paul@354 34
    unsigned int n = __load_via_object(size->value, __pos___data__).intvalue;
paul@569 35
    __attr attr = {.seqvalue=__new_fragment(n)};
paul@354 36
paul@354 37
    /* Return the __data__ attribute. */
paul@354 38
    return attr;
paul@354 39
}
paul@354 40
paul@354 41
__attr __fn_native_list_list_setsize(__attr __args[])
paul@354 42
{
paul@354 43
    __attr * const _data = &__args[1];
paul@354 44
    __attr * const size = &__args[2];
paul@354 45
    /* _data interpreted as list */
paul@354 46
    __fragment *data = _data->seqvalue;
paul@354 47
    /* size.__data__ interpreted as int */
paul@354 48
    unsigned int n = __load_via_object(size->value, __pos___data__).intvalue;
paul@354 49
paul@354 50
    data->size = n;
paul@354 51
    return __builtins___none_None;
paul@354 52
}
paul@354 53
paul@354 54
__attr __fn_native_list_list_append(__attr __args[])
paul@354 55
{
paul@354 56
    __attr * const self = &__args[1];
paul@354 57
    __attr * const value = &__args[2];
paul@354 58
    /* self.__data__ interpreted as list */
paul@354 59
    __fragment *data = __load_via_object(self->value, __pos___data__).seqvalue;
paul@354 60
    __fragment *newdata = __fragment_append(data, value);
paul@354 61
paul@354 62
    /* Replace the __data__ attribute if appropriate. */
paul@354 63
    if (newdata != data)
paul@569 64
        __store_via_object(self->value, __pos___data__, ((__attr) {.seqvalue=newdata}));
paul@354 65
    return __builtins___none_None;
paul@354 66
}
paul@354 67
paul@354 68
__attr __fn_native_list_list_concat(__attr __args[])
paul@354 69
{
paul@354 70
    __attr * const self = &__args[1];
paul@354 71
    __attr * const other = &__args[2];
paul@354 72
    /* self.__data__, other interpreted as list */
paul@354 73
    __fragment *data = __load_via_object(self->value, __pos___data__).seqvalue;
paul@354 74
    __fragment *other_data = other->seqvalue;
paul@354 75
    __fragment *newdata = data;
paul@354 76
    unsigned int size = data->size, capacity = data->capacity;
paul@354 77
    unsigned int other_size = other_data->size;
paul@354 78
    unsigned int i, j, n;
paul@354 79
paul@354 80
    /* Re-allocate the fragment if the capacity has been reached. */
paul@354 81
    if (size + other_size >= capacity)
paul@354 82
    {
paul@354 83
        n = size + other_size;
paul@354 84
        newdata = (__fragment *) __REALLOCATE(data, __FRAGMENT_SIZE(n));
paul@354 85
        newdata->capacity = n;
paul@354 86
    }
paul@354 87
paul@354 88
    /* Copy the elements from the other list and increment the list size. */
paul@354 89
    for (i = size, j = 0; j < other_size; i++, j++)
paul@354 90
        newdata->attrs[i] = other_data->attrs[j];
paul@354 91
    newdata->size = n;
paul@354 92
paul@354 93
    /* Replace the __data__ attribute if appropriate. */
paul@354 94
    if (newdata != data)
paul@569 95
        __store_via_object(self->value, __pos___data__, ((__attr) {.seqvalue=newdata}));
paul@354 96
    return __builtins___none_None;
paul@354 97
}
paul@354 98
paul@354 99
__attr __fn_native_list_list_len(__attr __args[])
paul@354 100
{
paul@354 101
    __attr * const _data = &__args[1];
paul@354 102
    /* _data interpreted as fragment */
paul@354 103
    unsigned int size = _data->seqvalue->size;
paul@354 104
paul@354 105
    /* Return the new integer. */
paul@354 106
    return __new_int(size);
paul@354 107
}
paul@354 108
paul@354 109
__attr __fn_native_list_list_nonempty(__attr __args[])
paul@354 110
{
paul@354 111
    __attr * const _data = &__args[1];
paul@354 112
paul@354 113
    return _data->seqvalue->size ? __builtins___boolean_True : __builtins___boolean_False;
paul@354 114
}
paul@354 115
paul@354 116
__attr __fn_native_list_list_element(__attr __args[])
paul@354 117
{
paul@354 118
    __attr * const _data = &__args[1];
paul@354 119
    __attr * const index = &__args[2];
paul@354 120
    /* _data interpreted as fragment */
paul@354 121
    __attr *elements = _data->seqvalue->attrs;
paul@354 122
    /* index.__data__ interpreted as int */
paul@354 123
    int i = __load_via_object(index->value, __pos___data__).intvalue;
paul@354 124
paul@354 125
    return elements[i];
paul@354 126
}
paul@354 127
paul@354 128
__attr __fn_native_list_list_setelement(__attr __args[])
paul@354 129
{
paul@354 130
    __attr * const _data = &__args[1];
paul@354 131
    __attr * const index = &__args[2];
paul@354 132
    __attr * const value = &__args[3];
paul@354 133
    /* _data interpreted as fragment */
paul@354 134
    __attr *elements = _data->seqvalue->attrs;
paul@354 135
    /* index.__data__ interpreted as int */
paul@354 136
    int i = __load_via_object(index->value, __pos___data__).intvalue;
paul@354 137
paul@354 138
    /* Set the element. */
paul@354 139
    elements[i] = *value;
paul@354 140
    return __builtins___none_None;
paul@354 141
}
paul@354 142
paul@354 143
/* Module initialisation. */
paul@354 144
paul@354 145
void __main_native_list()
paul@354 146
{
paul@354 147
}