# HG changeset patch # User Paul Boddie # Date 1486073828 -3600 # Node ID aab03ea7fc94741a3eb62ba1f5064ba98e2b2110 # Parent 7812a5a746be062ff38cb90ee608cd617abd6d41 Implemented the chr built-in function. diff -r 7812a5a746be -r aab03ea7fc94 lib/__builtins__/character.py --- a/lib/__builtins__/character.py Thu Feb 02 23:15:24 2017 +0100 +++ b/lib/__builtins__/character.py Thu Feb 02 23:17:08 2017 +0100 @@ -19,9 +19,19 @@ this program. If not, see . """ -from native import str_ord +from __builtins__.types import check_int, check_string +from native import str_chr, str_ord + +def chr(i): + + "Return a string containing a character having the value 'i'." -def chr(i): pass + check_int(i) + + if 0 <= i <= 255: + return str_chr(i.__data__) + else: + raise ValueError, i _hexdigits = "0123456789abcdef" @@ -75,10 +85,12 @@ "Return the value of the given character 'c'." - if isinstance(c, string) and len(c) == 1: + check_string(c) + + if c.__len__() == 1: return str_ord(c.__data__) else: - raise ValueError(c) + raise ValueError, c def unichr(i): pass diff -r 7812a5a746be -r aab03ea7fc94 lib/native/__init__.py --- a/lib/native/__init__.py Thu Feb 02 23:15:24 2017 +0100 +++ b/lib/native/__init__.py Thu Feb 02 23:17:08 2017 +0100 @@ -3,7 +3,7 @@ """ Native library functions. -Copyright (C) 2011, 2015, 2016 Paul Boddie +Copyright (C) 2011, 2015, 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 @@ -42,8 +42,8 @@ from native.program import get_using -from native.str import str_add, str_eq, str_gt, str_lt, str_len, str_nonempty, \ - str_ord, str_substr +from native.str import str_add, str_chr, str_eq, str_gt, str_lt, str_len, \ + str_nonempty, str_ord, str_substr from native.system import exit, get_argv, get_path diff -r 7812a5a746be -r aab03ea7fc94 lib/native/str.py --- a/lib/native/str.py Thu Feb 02 23:15:24 2017 +0100 +++ b/lib/native/str.py Thu Feb 02 23:17:08 2017 +0100 @@ -8,7 +8,7 @@ non-core exceptions used by the native functions because they need to be identified as being needed by the program. -Copyright (C) 2011, 2015, 2016 Paul Boddie +Copyright (C) 2011, 2015, 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 @@ -27,6 +27,7 @@ # String operations. def str_add(data, other_data): pass +def str_chr(data): pass def str_eq(data, other_data): pass def str_gt(data, other_data): pass def str_lt(data, other_data): pass diff -r 7812a5a746be -r aab03ea7fc94 templates/native/str.c --- a/templates/native/str.c Thu Feb 02 23:15:24 2017 +0100 +++ b/templates/native/str.c Thu Feb 02 23:17:08 2017 +0100 @@ -1,6 +1,6 @@ /* Native functions for string operations. -Copyright (C) 2016 Paul Boddie +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 @@ -45,6 +45,17 @@ return __new_str(r, n); } +__attr __fn_native_str_str_chr(__attr __args[]) +{ + __attr * const _data = &__args[1]; + /* _data interpreted as int */ + int n = _data->intvalue; + char *s = (char *) __ALLOCATE(2, sizeof(char)); + + s[0] = (char) n; + return __new_str(s, 1); +} + __attr __fn_native_str_str_lt(__attr __args[]) { __attr * const _data = &__args[1]; diff -r 7812a5a746be -r aab03ea7fc94 templates/native/str.h --- a/templates/native/str.h Thu Feb 02 23:15:24 2017 +0100 +++ b/templates/native/str.h Thu Feb 02 23:17:08 2017 +0100 @@ -1,6 +1,6 @@ /* Native functions for string operations. -Copyright (C) 2016 Paul Boddie +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 @@ -22,6 +22,7 @@ /* String operations. */ __attr __fn_native_str_str_add(__attr __args[]); +__attr __fn_native_str_str_chr(__attr __args[]); __attr __fn_native_str_str_lt(__attr __args[]); __attr __fn_native_str_str_gt(__attr __args[]); __attr __fn_native_str_str_eq(__attr __args[]); diff -r 7812a5a746be -r aab03ea7fc94 tests/string.py --- a/tests/string.py Thu Feb 02 23:15:24 2017 +0100 +++ b/tests/string.py Thu Feb 02 23:17:08 2017 +0100 @@ -49,6 +49,14 @@ except ValueError, exc: print "ord(s): value is not appropriate", repr(exc.value) +print chr(72) # H +print repr(chr(0)) # "\x00" + +try: + print repr(chr(-1)) # should raise an exception +except ValueError, exc: + print "chr(-1): value is not appropriate", exc.value + l = ["Hello", "world!"] s3 = " ".join(l) print s3 # Hello world!