Lichen

Changeset

945:16ecb996eb96
2021-11-04 Paul Boddie raw files shortlog changelog graph Introduced a dedicated sizevalue union member for __size__ attributes.
generator.py (file) lib/__builtins__/str.py (file) lib/native/__init__.py (file) lib/native/str.py (file) templates/native/buffer.c (file) templates/native/common.c (file) templates/native/io.c (file) templates/native/str.c (file) templates/native/str.h (file) templates/native/unicode.c (file) templates/types.h (file)
     1.1 --- a/generator.py	Tue Nov 02 00:04:48 2021 +0100
     1.2 +++ b/generator.py	Thu Nov 04 23:21:09 2021 +0100
     1.3 @@ -1018,7 +1018,7 @@
     1.4                  # Special internal size member.
     1.5  
     1.6                  elif attrname == "__size__":
     1.7 -                    structure.append("__INTVALUE(%d)" % attr)
     1.8 +                    structure.append("{.sizevalue=%d}" % attr)
     1.9                      continue
    1.10  
    1.11                  # Special internal key member.
     2.1 --- a/lib/__builtins__/str.py	Tue Nov 02 00:04:48 2021 +0100
     2.2 +++ b/lib/__builtins__/str.py	Thu Nov 04 23:21:09 2021 +0100
     2.3 @@ -24,7 +24,7 @@
     2.4  from __builtins__.types import check_int
     2.5  from native import isinstance as _isinstance, \
     2.6                     str_add, str_lt, str_gt, str_eq, str_ord, \
     2.7 -                   str_substr
     2.8 +                   str_size, str_substr
     2.9  
    2.10  WHITESPACE = (" ", "\f", "\n", "\r", "\t")
    2.11  
    2.12 @@ -54,7 +54,7 @@
    2.13          else:
    2.14              self.__data__ = None
    2.15              self.__key__ = None
    2.16 -            self.__size__ = 0
    2.17 +            self.__size__ = None
    2.18  
    2.19      # Internal methods.
    2.20  
    2.21 @@ -158,7 +158,7 @@
    2.22  
    2.23          "Return the number of bytes in this string."
    2.24  
    2.25 -        return self.__size__
    2.26 +        return str_size(self.__size__)
    2.27  
    2.28      # General type methods.
    2.29  
    2.30 @@ -166,7 +166,7 @@
    2.31  
    2.32          "Return whether the string provides any data."
    2.33  
    2.34 -        return self.__size__.__bool__()
    2.35 +        return str_size(self.__size__).__bool__()
    2.36  
    2.37      def __contains__(self, value):
    2.38  
     3.1 --- a/lib/native/__init__.py	Tue Nov 02 00:04:48 2021 +0100
     3.2 +++ b/lib/native/__init__.py	Thu Nov 04 23:21:09 2021 +0100
     3.3 @@ -3,7 +3,7 @@
     3.4  """
     3.5  Native library functions.
     3.6  
     3.7 -Copyright (C) 2011, 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
     3.8 +Copyright (C) 2011, 2015-2018, 2021 Paul Boddie <paul@boddie.org.uk>
     3.9  
    3.10  This program is free software; you can redistribute it and/or modify it under
    3.11  the terms of the GNU General Public License as published by the Free Software
    3.12 @@ -45,7 +45,7 @@
    3.13  from native.program import get_using
    3.14  
    3.15  from native.str import str_add, str_chr, str_eq, str_gt, str_lt, \
    3.16 -                       str_ord, str_substr
    3.17 +                       str_ord, str_size, str_substr
    3.18  
    3.19  from native.system import exit, get_argv, get_path
    3.20  
     4.1 --- a/lib/native/str.py	Tue Nov 02 00:04:48 2021 +0100
     4.2 +++ b/lib/native/str.py	Thu Nov 04 23:21:09 2021 +0100
     4.3 @@ -8,7 +8,7 @@
     4.4  non-core exceptions used by the native functions because they need to be
     4.5  identified as being needed by the program.
     4.6  
     4.7 -Copyright (C) 2011, 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
     4.8 +Copyright (C) 2011, 2015, 2016, 2017, 2021 Paul Boddie <paul@boddie.org.uk>
     4.9  
    4.10  This program is free software; you can redistribute it and/or modify it under
    4.11  the terms of the GNU General Public License as published by the Free Software
    4.12 @@ -32,6 +32,7 @@
    4.13  def str_gt(data, other_data): return True or False
    4.14  def str_lt(data, other_data): return True or False
    4.15  def str_ord(data): return 0
    4.16 +def str_size(size): return 0
    4.17  def str_substr(data, start, end, step): return ""
    4.18  
    4.19  # vim: tabstop=4 expandtab shiftwidth=4
     5.1 --- a/templates/native/buffer.c	Tue Nov 02 00:04:48 2021 +0100
     5.2 +++ b/templates/native/buffer.c	Thu Nov 04 23:21:09 2021 +0100
     5.3 @@ -36,7 +36,7 @@
     5.4  
     5.5      /* Calculate the size of the string. */
     5.6      for (i = 0; i < data->size; i++)
     5.7 -        size += __TOINT(__load_via_object(__VALUE(data->attrs[i]), __size__));
     5.8 +        size += __load_via_object(__VALUE(data->attrs[i]), __size__).sizevalue;
     5.9  
    5.10      /* Reserve space for a new string. */
    5.11      s = (char *) __ALLOCATE(size + 1, sizeof(char));
    5.12 @@ -45,7 +45,7 @@
    5.13      for (i = 0, j = 0; i < data->size; i++)
    5.14      {
    5.15          o = __load_via_object(__VALUE(data->attrs[i]), __data__);
    5.16 -        n = __TOINT(__load_via_object(__VALUE(data->attrs[i]), __size__));
    5.17 +        n = __load_via_object(__VALUE(data->attrs[i]), __size__).sizevalue;
    5.18          memcpy(s + j, o.strvalue, n); /* does not null terminate but final byte should be zero */
    5.19          j += n;
    5.20      }
     6.1 --- a/templates/native/common.c	Tue Nov 02 00:04:48 2021 +0100
     6.2 +++ b/templates/native/common.c	Thu Nov 04 23:21:09 2021 +0100
     6.3 @@ -31,7 +31,7 @@
     6.4      /* Create a new string and mutate the __data__, __size__ and __key__ attributes. */
     6.5      __attr attr = __NEWINSTANCE(__builtins___str_str);
     6.6      __store_via_object(__VALUE(attr), __data__, (__attr) {.strvalue=s});
     6.7 -    __store_via_object(__VALUE(attr), __size__, __INTVALUE(size));
     6.8 +    __store_via_object(__VALUE(attr), __size__, (__attr) {.sizevalue=size});
     6.9      __store_via_object(__VALUE(attr), __key__, __NULL);
    6.10      return attr;
    6.11  }
     7.1 --- a/templates/native/io.c	Tue Nov 02 00:04:48 2021 +0100
     7.2 +++ b/templates/native/io.c	Thu Nov 04 23:21:09 2021 +0100
     7.3 @@ -150,7 +150,7 @@
     7.4      /* str.__data__ interpreted as string */
     7.5      char *s = __load_via_object(__VALUE(str), __data__).strvalue;
     7.6      /* str.__size__ interpreted as int */
     7.7 -    size_t to_write = __TOINT(__load_via_object(__VALUE(str), __size__));
     7.8 +    size_t to_write = __load_via_object(__VALUE(str), __size__).sizevalue;
     7.9      size_t have_written = fwrite(s, sizeof(char), to_write, f);
    7.10      int error;
    7.11  
    7.12 @@ -207,7 +207,7 @@
    7.13      /* str.__data__ interpreted as string */
    7.14      char *s = __load_via_object(__VALUE(str), __data__).strvalue;
    7.15      /* str.__size__ interpreted as int */
    7.16 -    size_t size = __TOINT(__load_via_object(__VALUE(str), __size__));
    7.17 +    size_t size = __load_via_object(__VALUE(str), __size__).sizevalue;
    7.18      ssize_t have_written;
    7.19  
    7.20      errno = 0;
     8.1 --- a/templates/native/str.c	Tue Nov 02 00:04:48 2021 +0100
     8.2 +++ b/templates/native/str.c	Thu Nov 04 23:21:09 2021 +0100
     8.3 @@ -33,8 +33,8 @@
     8.4      /* _data, other interpreted as string.__data__ */
     8.5      char *s = _data.strvalue;
     8.6      char *o = other.strvalue;
     8.7 -    /* _size, othersize interpreted as int */
     8.8 -    __int ss = __TOINT(_size), os = __TOINT(othersize);
     8.9 +    /* _size, othersize interpreted as size */
    8.10 +    __int ss = _size.sizevalue, os = othersize.sizevalue;
    8.11      __int n = ss + os;
    8.12      char *r = (char *) __ALLOCATE(n + 1, sizeof(char));
    8.13  
    8.14 @@ -93,6 +93,11 @@
    8.15      return __new_int((__int) s[0]);
    8.16  }
    8.17  
    8.18 +__attr __fn_native_str_str_size(__attr __self, __attr _size)
    8.19 +{
    8.20 +    return __new_int(_size.sizevalue);
    8.21 +}
    8.22 +
    8.23  __attr __fn_native_str_str_substr(__attr __self, __attr _data, __attr start, __attr end, __attr step)
    8.24  {
    8.25      /* _data interpreted as string.__data__ */
     9.1 --- a/templates/native/str.h	Tue Nov 02 00:04:48 2021 +0100
     9.2 +++ b/templates/native/str.h	Thu Nov 04 23:21:09 2021 +0100
     9.3 @@ -1,6 +1,6 @@
     9.4  /* Native functions for string operations.
     9.5  
     9.6 -Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk>
     9.7 +Copyright (C) 2016, 2017, 2021 Paul Boddie <paul@boddie.org.uk>
     9.8  
     9.9  This program is free software; you can redistribute it and/or modify it under
    9.10  the terms of the GNU General Public License as published by the Free Software
    9.11 @@ -27,6 +27,7 @@
    9.12  __attr __fn_native_str_str_gt(__attr __self, __attr _data, __attr other);
    9.13  __attr __fn_native_str_str_eq(__attr __self, __attr _data, __attr other);
    9.14  __attr __fn_native_str_str_ord(__attr __self, __attr _data);
    9.15 +__attr __fn_native_str_str_size(__attr __self);
    9.16  __attr __fn_native_str_str_substr(__attr __self, __attr _data, __attr start, __attr end, __attr step);
    9.17  
    9.18  /* Module initialisation. */
    10.1 --- a/templates/native/unicode.c	Tue Nov 02 00:04:48 2021 +0100
    10.2 +++ b/templates/native/unicode.c	Thu Nov 04 23:21:09 2021 +0100
    10.3 @@ -73,8 +73,8 @@
    10.4  {
    10.5      /* _data interpreted as string.__data__ */
    10.6      char *s = _data.strvalue;
    10.7 -    /* _size interpreted as int */
    10.8 -    __int size = __TOINT(_size);
    10.9 +    /* _size interpreted as size */
   10.10 +    __int size = _size.sizevalue;
   10.11      __int i, c = 0;
   10.12  
   10.13      for (i = 0; i < size; i++)
   10.14 @@ -89,8 +89,8 @@
   10.15  {
   10.16      /* _data interpreted as string.__data__ */
   10.17      char *s = _data.strvalue;
   10.18 -    /* _size interpreted as int */
   10.19 -    __int size = __TOINT(_size);
   10.20 +    /* _size interpreted as size */
   10.21 +    __int size = _size.sizevalue;
   10.22      __int i, c = 0, v;
   10.23  
   10.24      for (i = 0; i < size; i++)
   10.25 @@ -123,8 +123,8 @@
   10.26  {
   10.27      /* _data interpreted as string.__data__ */
   10.28      char *s = _data.strvalue, *sub;
   10.29 -    /* _size interpreted as int */
   10.30 -    __int ss = __TOINT(_size);
   10.31 +    /* _size interpreted as size */
   10.32 +    __int ss = _size.sizevalue;
   10.33      /* start interpreted as int */
   10.34      __int istart = __TOINT(start);
   10.35      /* end interpreted as int */
    11.1 --- a/templates/types.h	Tue Nov 02 00:04:48 2021 +0100
    11.2 +++ b/templates/types.h	Thu Nov 04 23:21:09 2021 +0100
    11.3 @@ -1,7 +1,6 @@
    11.4  /* Runtime types.
    11.5  
    11.6 -Copyright (C) 2015, 2016, 2017, 2018, 2019,
    11.7 -              2021 Paul Boddie <paul@boddie.org.uk>
    11.8 +Copyright (C) 2015-2019, 2021 Paul Boddie <paul@boddie.org.uk>
    11.9  
   11.10  This program is free software; you can redistribute it and/or modify it under
   11.11  the terms of the GNU General Public License as published by the Free Software
   11.12 @@ -72,8 +71,7 @@
   11.13  typedef union __attr __attr;
   11.14  typedef __obj * __ref;
   11.15  
   11.16 -/* Introduce an integer type that should not exceed the size of the pointer
   11.17 -   type. */
   11.18 +/* Introduce an integer type that is interoperable with the size type. */
   11.19  
   11.20  typedef ssize_t __int;
   11.21  
   11.22 @@ -98,6 +96,7 @@
   11.23      char * strvalue;            /* string value */
   11.24      __fragment * seqvalue;      /* sequence data */
   11.25      void * datavalue;           /* object-specific data */
   11.26 +    __int sizevalue;            /* object-specific size */
   11.27  } __attr;
   11.28  
   11.29  typedef struct __obj