Skip to content
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

Implement input(prompt) #2655

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Implement input(prompt) #2655

wants to merge 2 commits into from

Conversation

kmr-srbh
Copy link
Contributor

No description provided.

@kmr-srbh
Copy link
Contributor Author

@Shaikh-Ubaid , there is a small issue occurring here. Because we are directing calls to FileRead, input() itself is not having a return type and value. So, an expression like n: i32 = i32(int(input("Enter a number: ")))) leads to an exception. A simple program:

name: str = input("Your name: ")
print("Hello,", name)

gives the following output

(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
Your name: Segmentation fault (core dumped)

I request you to see if the implementation is correct. Instead of a separate function create_Input() with a call inside if (call_name == "input"), I am using the complete piece of code here.

@Shaikh-Ubaid
Copy link
Collaborator

@kmr-srbh the following works:

% cat examples/expr2.py 
name: str = "                "
name = input("Your name: ")
print("Hello,", name)
% python examples/expr2.py
Your name: John
Hello, John
% lpython examples/expr2.py
Your name: John
Hello,John

We just needed name to have initial length/space so that we could write to it the string read from stdin.

@Shaikh-Ubaid
Copy link
Collaborator

We need to see what other better alternative approaches can be for the workaround (for name length) in #2655 (comment).

@kmr-srbh
Copy link
Contributor Author

kmr-srbh commented Apr 17, 2024

We need to see what other better alternative approaches can be for the workaround (for name length) in #2655 (comment).

You are right @Shaikh-Ubaid. How do we go about working a fix for the return type issue? This is a real problem. I am new to this area of the code-base and learning.

@certik
Copy link
Contributor

certik commented Apr 19, 2024

Let's modify the semantics of the FileRead ASR node: if the "value" expr argument is an allocatable string, then the backend (LLVM/WASM, etc.) implementation of the FileRead node will allocate the string to hold the whole input, and assign it to it.

@kmr-srbh
Copy link
Contributor Author

Let's modify the semantics of the FileRead ASR node: if the "value" expr argument is an allocatable string, then the backend (LLVM/WASM, etc.) implementation of the FileRead node will allocate the string to hold the whole input, and assign it to it.

@certik Do you mean handling String_t here?

if (ASRUtils::is_array(type)) {
if (ASR::is_a<ASR::Allocatable_t>(*type)
|| ASR::is_a<ASR::Pointer_t>(*type)) {
tmp = CreateLoad(tmp);
}
tmp = arr_descr->get_pointer_to_data(tmp);
if (ASR::is_a<ASR::Allocatable_t>(*type)
|| ASR::is_a<ASR::Pointer_t>(*type)) {
tmp = CreateLoad(tmp);
}
llvm::Value *arr = tmp;
ASR::ttype_t *type32 = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, 4));
ASR::ArraySize_t* array_size = ASR::down_cast2<ASR::ArraySize_t>(ASR::make_ArraySize_t(al, x.base.base.loc,
x.m_values[i], nullptr, type32, nullptr));
visit_ArraySize(*array_size);
builder->CreateCall(fn, {arr, tmp, unit_val});
} else {
builder->CreateCall(fn, {tmp, unit_val});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants