Lichen

Changeset

929:bbcea70e1cde
5 months ago Paul Boddie raw files shortlog changelog graph Fixed int instantiation by special-casing the type in the translator. This also avoids allocating an instance which would never be used with the tagged value representation.
generator.py (file) lib/__builtins__/int.py (file) tests/int.py (file)
     1.1 --- a/generator.py	Sun Jun 27 22:10:02 2021 +0200
     1.2 +++ b/generator.py	Sun Jun 27 22:12:23 2021 +0200
     1.3 @@ -1200,18 +1200,36 @@
     1.4          for name in parameters:
     1.5              l.append("__attr %s" % name)
     1.6  
     1.7 -        print >>f_code, """\
     1.8 +        # Special-case the integer type.
     1.9 +
    1.10 +        if path == self.int_type:
    1.11 +            print >>f_code, """\
    1.12 +__attr %s(__attr __self, __attr number_or_string)
    1.13 +{
    1.14 +    if (!__BOOL(__fn_native_int_is_int(__self, number_or_string)))
    1.15 +        __raise_value_error(number_or_string);
    1.16 +
    1.17 +    return number_or_string;
    1.18 +}
    1.19 +""" % (
    1.20 +                encode_instantiator_pointer(path),
    1.21 +                )
    1.22 +
    1.23 +        # Generic instantiation support.
    1.24 +
    1.25 +        else:
    1.26 +            print >>f_code, """\
    1.27  __attr %s(__attr __self%s)
    1.28  {
    1.29      return %s(__NEWINSTANCE(%s)%s);
    1.30  }
    1.31  """ % (
    1.32 -            encode_instantiator_pointer(path),
    1.33 -            l and ", %s" % ",".join(l) or "",
    1.34 -            encode_function_pointer(initialiser),
    1.35 -            encode_path(path),
    1.36 -            parameters and ", %s" % ", ".join(parameters) or ""
    1.37 -            )
    1.38 +                encode_instantiator_pointer(path),
    1.39 +                l and ", %s" % ",".join(l) or "",
    1.40 +                encode_function_pointer(initialiser),
    1.41 +                encode_path(path),
    1.42 +                parameters and ", %s" % ", ".join(parameters) or ""
    1.43 +                )
    1.44  
    1.45          # Signature: __new_typename(__attr __self, ...)
    1.46  
     2.1 --- a/lib/__builtins__/int.py	Sun Jun 27 22:10:02 2021 +0200
     2.2 +++ b/lib/__builtins__/int.py	Sun Jun 27 22:12:23 2021 +0200
     2.3 @@ -3,7 +3,7 @@
     2.4  """
     2.5  Integer objects.
     2.6  
     2.7 -Copyright (C) 2015, 2016, 2017, 2018 Paul Boddie <paul@boddie.org.uk>
     2.8 +Copyright (C) 2015, 2016, 2017, 2018, 2021 Paul Boddie <paul@boddie.org.uk>
     2.9  
    2.10  This program is free software; you can redistribute it and/or modify it under
    2.11  the terms of the GNU General Public License as published by the Free Software
    2.12 @@ -34,7 +34,7 @@
    2.13  
    2.14          "Initialise the integer with the given 'number_or_string'."
    2.15  
    2.16 -        # NOTE: To be implemented.
    2.17 +        # Implemented in the translator.
    2.18  
    2.19          pass
    2.20  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tests/int.py	Sun Jun 27 22:12:23 2021 +0200
     3.3 @@ -0,0 +1,13 @@
     3.4 +i = int(123)
     3.5 +j = 123
     3.6 +print i, j, i == j      # 123 123 True
     3.7 +k = 456
     3.8 +print i, k, i == k      # 123 456 False
     3.9 +h = int(789)
    3.10 +print i, h, i == h      # 123 789 False
    3.11 +print j, h, j == h      # 123 789 False
    3.12 +
    3.13 +try:
    3.14 +    a = int("a")        # should raise an exception
    3.15 +except ValueError, exc:
    3.16 +    print 'int("a") failed:', exc.value