1.1 --- a/generator.py Tue Jun 22 23:04:00 2021 +0200
1.2 +++ b/generator.py Sun Jun 27 22:14:51 2021 +0200
1.3 @@ -1263,18 +1263,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 Tue Jun 22 23:04:00 2021 +0200
2.2 +++ b/lib/__builtins__/int.py Sun Jun 27 22:14:51 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 --- a/templates/progops.c Tue Jun 22 23:04:00 2021 +0200
3.2 +++ b/templates/progops.c Sun Jun 27 22:14:51 2021 +0200
3.3 @@ -1,6 +1,6 @@
3.4 /* Operations depending on program specifics.
3.5
3.6 -Copyright (C) 2015, 2016, 2017, 2018, 2019 Paul Boddie <paul@boddie.org.uk>
3.7 +Copyright (C) 2015-2019, 2021 Paul Boddie <paul@boddie.org.uk>
3.8
3.9 This program is free software; you can redistribute it and/or modify it under
3.10 the terms of the GNU General Public License as published by the Free Software
3.11 @@ -171,6 +171,13 @@
3.12 __Raise(__new___builtins___core_UnderflowError(__NULL));
3.13 }
3.14
3.15 +void __raise_value_error(__attr value)
3.16 +{
3.17 +#ifdef __HAVE___builtins___exception_base_ValueError
3.18 + __Raise(__new___builtins___exception_base_ValueError(__NULL, value));
3.19 +#endif /* __HAVE___builtins___exception_base_ValueError */
3.20 +}
3.21 +
3.22 void __raise_zero_division_error()
3.23 {
3.24 __Raise(__new___builtins___core_ZeroDivisionError(__NULL));
4.1 --- a/templates/progops.h Tue Jun 22 23:04:00 2021 +0200
4.2 +++ b/templates/progops.h Sun Jun 27 22:14:51 2021 +0200
4.3 @@ -1,6 +1,6 @@
4.4 /* Operations depending on program specifics.
4.5
4.6 -Copyright (C) 2015, 2016, 2017, 2018, 2019 Paul Boddie <paul@boddie.org.uk>
4.7 +Copyright (C) 2015-2019, 2021 Paul Boddie <paul@boddie.org.uk>
4.8
4.9 This program is free software; you can redistribute it and/or modify it under
4.10 the terms of the GNU General Public License as published by the Free Software
4.11 @@ -55,6 +55,7 @@
4.12 void __raise_overflow_error();
4.13 void __raise_unbound_method_error();
4.14 void __raise_underflow_error();
4.15 +void __raise_value_error(__attr value);
4.16 void __raise_zero_division_error();
4.17 void __raise_type_error();
4.18
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/tests/int.py Sun Jun 27 22:14:51 2021 +0200
5.3 @@ -0,0 +1,13 @@
5.4 +i = int(123)
5.5 +j = 123
5.6 +print i, j, i == j # 123 123 True
5.7 +k = 456
5.8 +print i, k, i == k # 123 456 False
5.9 +h = int(789)
5.10 +print i, h, i == h # 123 789 False
5.11 +print j, h, j == h # 123 789 False
5.12 +
5.13 +try:
5.14 + a = int("a") # should raise an exception
5.15 +except ValueError, exc:
5.16 + print 'int("a") failed:', exc.value