# HG changeset patch # User Paul Boddie # Date 1490461119 -3600 # Node ID ed4b4531dd288efb699247d2d8c8abe79d05f6db # Parent af05b2a6697a91c678c4a1a8be66fad3d8450cc1 Added specific support for initialising tuples, introducing a common empty fragment for empty tuples. diff -r af05b2a6697a -r ed4b4531dd28 lib/__builtins__/tuple.py --- a/lib/__builtins__/tuple.py Sat Mar 25 17:24:11 2017 +0100 +++ b/lib/__builtins__/tuple.py Sat Mar 25 17:58:39 2017 +0100 @@ -21,8 +21,8 @@ from __builtins__.iteration.iterator import itemiterator from __builtins__.sequence import hashable, sequence -from native import list_element, list_init, list_len, list_setsize, \ - list_setelement +from native import tuple_init, \ + list_element, list_len, list_setsize, list_setelement class tuple(sequence, hashable): @@ -35,16 +35,19 @@ # Reserve an attribute for a fragment reference along with some space # for elements. - size = args is not None and len(args) or 0 - self.__data__ = list_init(size) - list_setsize(self.__data__, size) + self.__data__ = tuple_init(size) - # Populate the tuple. + if args is None: + size = 0 + else: + size = args.__len__() + list_setsize(self.__data__, size) - if args is not None: + # Populate the tuple. + i = 0 - for arg in args: - list_setelement(self.__data__, i, arg) + while i < size: + list_setelement(self.__data__, i, args[i]) i += 1 def __hash__(self): diff -r af05b2a6697a -r ed4b4531dd28 lib/native/__init__.py --- a/lib/native/__init__.py Sat Mar 25 17:24:11 2017 +0100 +++ b/lib/native/__init__.py Sat Mar 25 17:58:39 2017 +0100 @@ -48,6 +48,8 @@ from native.system import exit, get_argv, get_path +from native.tuple import tuple_init + from native.unicode import unicode_len, unicode_ord, unicode_substr, unicode_unichr # vim: tabstop=4 expandtab shiftwidth=4 diff -r af05b2a6697a -r ed4b4531dd28 lib/native/tuple.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/native/tuple.py Sat Mar 25 17:58:39 2017 +0100 @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +""" +Native library functions for tuples. + +None of these are actually defined here. Instead, native implementations are +substituted when each program is built. It is, however, important to declare +non-core exceptions used by the native functions because they need to be +identified as being needed by the program. + +Copyright (C) 2017 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +def tuple_init(size): pass + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r af05b2a6697a -r ed4b4531dd28 templates/native/tuple.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/native/tuple.c Sat Mar 25 17:58:39 2017 +0100 @@ -0,0 +1,46 @@ +/* Native functions for tuple operations. + +Copyright (C) 2016, 2017 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +*/ + +#include "native/common.h" +#include "types.h" +#include "exceptions.h" +#include "ops.h" +#include "progconsts.h" +#include "progops.h" +#include "progtypes.h" +#include "main.h" + +/* Tuple operations. */ + +static __fragment __empty_fragment = {.size=0, .capacity=0}; + +__attr __fn_native_tuple_tuple_init(__attr __self, __attr size) +{ + /* size interpreted as int */ + unsigned int n = __TOINT(size); + + /* Return the __data__ attribute. */ + if (n) return (__attr) {.seqvalue=__new_fragment(n)}; + else return (__attr) {.seqvalue=&__empty_fragment}; +} + +/* Module initialisation. */ + +void __main_native_tuple() +{ +} diff -r af05b2a6697a -r ed4b4531dd28 templates/native/tuple.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/native/tuple.h Sat Mar 25 17:58:39 2017 +0100 @@ -0,0 +1,32 @@ +/* Native functions for tuple operations. + +Copyright (C) 2016, 2017 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +*/ + +#ifndef __NATIVE_TUPLE_H__ +#define __NATIVE_TUPLE_H__ + +#include "types.h" + +/* List operations. */ + +__attr __fn_native_tuple_tuple_init(__attr __self, __attr size); + +/* Module initialisation. */ + +void __main_native_tuple(); + +#endif /* __NATIVE_TUPLE_H__ */