Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support different integers in chr() #2203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ RUN(NAME expr_17 LABELS cpython llvm c)
RUN(NAME expr_18 FAIL LABELS cpython llvm c)
RUN(NAME expr_19 LABELS cpython llvm c)
RUN(NAME expr_20 LABELS cpython llvm c)
RUN(NAME expr_21 LABELS cpython llvm c)

RUN(NAME expr_01u LABELS cpython llvm c NOFAST)
RUN(NAME expr_02u LABELS cpython llvm c NOFAST)
Expand Down
21 changes: 21 additions & 0 deletions integration_tests/expr_21.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from lpython import i8, i16, i32, i64

def main0():
x: i8
y: i16
z: i32
w: i64

x = i8(97)
y = i16(47)
z = 56
w = i64(67)

print(chr(x), chr(y), chr(z), chr(w))

assert chr(x) == 'a'
assert chr(y) == '/'
assert chr(z) == '8'
assert chr(w) == 'C'

main0()
3 changes: 2 additions & 1 deletion src/libasr/runtime/lfortran_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,8 @@ LFORTRAN_API int _lfortran_str_ord_c(char* s)
LFORTRAN_API char* _lfortran_str_chr(int val)
{
char* dest_char = (char*)malloc(2);
dest_char[0] = val;
uint8_t extended_ascii = (uint8_t)val;
dest_char[0] = extended_ascii;
Comment on lines -1343 to +1344
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dest_char[1] = '\0';
return dest_char;
}
Expand Down
13 changes: 9 additions & 4 deletions src/lpython/semantics/python_intrinsic_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,15 +448,20 @@ struct IntrinsicNodeHandler {
if (ASRUtils::is_integer(*type)) {
if (ASRUtils::expr_value(arg) != nullptr) {
int64_t c = ASR::down_cast<ASR::IntegerConstant_t>(arg)->m_n;
if (! (c >= 0 && c <= 127) ) {
throw SemanticError("The argument 'x' in chr(x) must be in the range 0 <= x <= 127.", loc);
c = (uint8_t) c;
if (! (c >= 0 && c <= 255) ) {
throw SemanticError("The argument 'x' in chr(x) must be in the range 0 <= x <= 255.", loc);
}
char cc = c;
std::string svalue;
svalue += cc;
svalue += char(c);
value = ASR::down_cast<ASR::expr_t>(
ASR::make_StringConstant_t(al, loc, s2c(al, svalue), str_type));
}
int kind = ASRUtils::extract_kind_from_ttype_t(type);
if (kind != 4) {
ASR::ttype_t* dest_type = ASRUtils::TYPE(ASR::make_Integer_t(al,loc, 4));
arg = ASRUtils::EXPR(ASR::make_Cast_t(al, loc, arg, ASR::cast_kindType::IntegerToInteger, dest_type, nullptr));
}
return ASR::make_StringChr_t(al, loc, arg, str_type, value);
} else {
throw SemanticError("'" + ASRUtils::type_to_str_python(type) + "' object cannot be interpreted as an integer",
Expand Down