-
Notifications
You must be signed in to change notification settings - Fork 170
Add str() builtin function #1
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
Comments
I tried implementing
Reference code
def str_int(x: i32) -> str:
if x == 0:
return '0'
result: str
result = ''
if x < 0:
result += '-'
x = -x
rev_result: str
rev_result = ''
pos_to_str: list
pos_to_str = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
while x > 0:
rev_result += pos_to_str[x%10]
x = x//10
for pos in range(len(rev_result) - 1, -1, -1):
result += rev_result[pos]
return result AST
$ lfortran --show-python-ast --indent ser.txt
(Module [
(FunctionDef
str_int
([] [
(
x
(Name
i32
Load) ())] [] [] [] [] []) [
(If
(Compare
(Name
x
Load)
Eq [
(ConstantInt 0 ())]) [
(Return
(ConstantStr "0" ()))] [])
(AnnAssign
(Name
result
Store)
(Name
str
Load) () 1)
(Assign [
(Name
result
Store)]
(ConstantStr "" ()) ())
(If
(Compare
(Name
x
Load)
Lt [
(ConstantInt 0 ())]) [
(AugAssign
(Name
result
Store)
Add
(ConstantStr "-" ()))
(Assign [
(Name
x
Store)]
(UnaryOp
USub
(Name
x
Load)) ())] [])
(AnnAssign
(Name
rev_result
Store)
(Name
str
Load) () 1)
(Assign [
(Name
rev_result
Store)]
(ConstantStr "" ()) ())
(AnnAssign
(Name
pos_to_str
Store)
(Name
list
Load) () 1)
(Assign [
(Name
pos_to_str
Store)]
(List [
(ConstantStr "0" ())
(ConstantStr "1" ())
(ConstantStr "2" ())
(ConstantStr "3" ())
(ConstantStr "4" ())
(ConstantStr "5" ())
(ConstantStr "6" ())
(ConstantStr "7" ())
(ConstantStr "8" ())
(ConstantStr "9" ())]
Load) ())
(While
(Compare
(Name
x
Load)
Gt [
(ConstantInt 0 ())]) [
(AugAssign
(Name
rev_result
Store)
Add
(Subscript
(Name
pos_to_str
Load)
(BinOp
(Name
x
Load)
Mod
(ConstantInt 10 ()))
Load))
(Assign [
(Name
x
Store)]
(BinOp
(Name
x
Load)
FloorDiv
(ConstantInt 10 ())) ())] [])
(For
(Name
pos
Store)
(Call
(Name
range
Load) [
(BinOp
(Call
(Name
len
Load) [
(Name
rev_result
Load)] [])
Sub
(ConstantInt 1 ()))
(UnaryOp
USub
(ConstantInt 1 ()))
(UnaryOp
USub
(ConstantInt 1 ()))] []) [
(AugAssign
(Name
result
Store)
Add
(Subscript
(Name
rev_result
Load)
(Name
pos
Load)
Load))] [] ())
(Return
(Name
result
Load))] []
(Name
str
Load) ())] []) ASR
Traceback (most recent call last):
Binary file "/home/admin-pc/Smitlunagariya/lfortran/inst/bin/lfortran", in _start()
File "/build/glibc-S9d2JN/glibc-2.27/csu/../csu/libc-start.c", line 310, in __libc_start_main()
File "/home/admin-pc/Smitlunagariya/lfortran/src/bin/lfortran.cpp", line 1504, in main()
with_intrinsic_modules, compiler_options);
File "/home/admin-pc/Smitlunagariya/lfortran/src/bin/lfortran.cpp", line 616, in emit_python_asr()
r = LFortran::Python::python_ast_to_asr(al, *ast, diagnostics);
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/semantics/python_ast_to_asr.cpp", line 660, in LFortran::Python::python_ast_to_asr(Allocator&, LFortran::Python::AST::ast_t&, LFortran::diag::Diagnostics&)
auto res2 = body_visitor(al, *ast_m, diagnostics, unit);
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/semantics/python_ast_to_asr.cpp", line 616, in LFortran::Python::body_visitor(Allocator&, LFortran::Python::AST::Module_t&, LFortran::diag::Diagnostics&, LFortran::ASR::asr_t*)
b.visit_Module(ast);
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/semantics/python_ast_to_asr.cpp", line 299, in LFortran::Python::BodyVisitor::visit_Module(LFortran::Python::AST::Module_t const&)
visit_stmt(*x.m_body[i]);
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/python_ast.h", line 1805, in LFortran::Python::AST::BaseVisitor<LFortran::Python::BodyVisitor>::visit_stmt(LFortran::Python::AST::stmt_t const&)
void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/python_ast.h", line 1677, in visit_stmt_t<LFortran::Python::BodyVisitor>()
case stmtType::FunctionDef: { v.visit_FunctionDef((const FunctionDef_t &)x); return; }
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/semantics/python_ast_to_asr.cpp", line 312, in LFortran::Python::BodyVisitor::visit_FunctionDef(LFortran::Python::AST::FunctionDef_t const&)
transform_stmts(body, x.n_body, x.m_body);
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/semantics/python_ast_to_asr.cpp", line 283, in LFortran::Python::BodyVisitor::transform_stmts(LFortran::Vec<LFortran::ASR::stmt_t*>&, unsigned long, LFortran::Python::AST::stmt_t**)
this->visit_stmt(*m_body[i]);
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/python_ast.h", line 1805, in LFortran::Python::AST::BaseVisitor<LFortran::Python::BodyVisitor>::visit_stmt(LFortran::Python::AST::stmt_t const&)
void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/python_ast.h", line 1688, in visit_stmt_t<LFortran::Python::BodyVisitor>()
case stmtType::If: { v.visit_If((const If_t &)x); return; }
LFortranException: visit_If() not implemented
Reference code
def str_int(x: int) -> str:
if x == 0:
return '0'
result: str
result = ''
if x < 0:
result += '-'
x = -x
rev_result: str
rev_result = ''
pos_to_str: list
pos_to_str = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
while x > 0:
rev_result += pos_to_str[x%10]
x = x//10
for pos in range(len(rev_result) - 1, -1, -1):
result += rev_result[pos]
return result AST
(Module [
(FunctionDef
str_int
([] [
(
x
(Name
int
Load) ())] [] [] [] [] []) [
(If
(Compare
(Name
x
Load)
Eq [
(ConstantInt 0 ())]) [
(Return
(ConstantStr "0" ()))] [])
(AnnAssign
(Name
result
Store)
(Name
str
Load) () 1)
(Assign [
(Name
result
Store)]
(ConstantStr "" ()) ())
(If
(Compare
(Name
x
Load)
Lt [
(ConstantInt 0 ())]) [
(AugAssign
(Name
result
Store)
Add
(ConstantStr "-" ()))
(Assign [
(Name
x
Store)]
(UnaryOp
USub
(Name
x
Load)) ())] [])
(AnnAssign
(Name
rev_result
Store)
(Name
str
Load) () 1)
(Assign [
(Name
rev_result
Store)]
(ConstantStr "" ()) ())
(AnnAssign
(Name
pos_to_str
Store)
(Name
list
Load) () 1)
(Assign [
(Name
pos_to_str
Store)]
(List [
(ConstantStr "0" ())
(ConstantStr "1" ())
(ConstantStr "2" ())
(ConstantStr "3" ())
(ConstantStr "4" ())
(ConstantStr "5" ())
(ConstantStr "6" ())
(ConstantStr "7" ())
(ConstantStr "8" ())
(ConstantStr "9" ())]
Load) ())
(While
(Compare
(Name
x
Load)
Gt [
(ConstantInt 0 ())]) [
(AugAssign
(Name
rev_result
Store)
Add
(Subscript
(Name
pos_to_str
Load)
(BinOp
(Name
x
Load)
Mod
(ConstantInt 10 ()))
Load))
(Assign [
(Name
x
Store)]
(BinOp
(Name
x
Load)
FloorDiv
(ConstantInt 10 ())) ())] [])
(For
(Name
pos
Store)
(Call
(Name
range
Load) [
(BinOp
(Call
(Name
len
Load) [
(Name
rev_result
Load)] [])
Sub
(ConstantInt 1 ()))
(UnaryOp
USub
(ConstantInt 1 ()))
(UnaryOp
USub
(ConstantInt 1 ()))] []) [
(AugAssign
(Name
result
Store)
Add
(Subscript
(Name
rev_result
Load)
(Name
pos
Load)
Load))] [] ())
(Return
(Name
result
Load))] []
(Name
str
Load) ())] []) ASR
Reference code
def str_int(x: i32) -> str:
return x.__str__() AST
(Module [
(FunctionDef
str_int
([] [
(
x
(Name
i32
Load) ())] [] [] [] [] []) [
(Return
(Call
(Attribute
(Name
x
Load)
__str__
Load) [] []))] []
(Name
str
Load) ())] []) ASR
Traceback (most recent call last):
Binary file "/home/admin-pc/Smitlunagariya/lfortran/inst/bin/lfortran", in _start()
File "/build/glibc-S9d2JN/glibc-2.27/csu/../csu/libc-start.c", line 310, in __libc_start_main()
File "/home/admin-pc/Smitlunagariya/lfortran/src/bin/lfortran.cpp", line 1504, in main()
with_intrinsic_modules, compiler_options);
File "/home/admin-pc/Smitlunagariya/lfortran/src/bin/lfortran.cpp", line 616, in emit_python_asr()
r = LFortran::Python::python_ast_to_asr(al, *ast, diagnostics);
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/semantics/python_ast_to_asr.cpp", line 660, in LFortran::Python::python_ast_to_asr(Allocator&, LFortran::Python::AST::ast_t&, LFortran::diag::Diagnostics&)
auto res2 = body_visitor(al, *ast_m, diagnostics, unit);
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/semantics/python_ast_to_asr.cpp", line 616, in LFortran::Python::body_visitor(Allocator&, LFortran::Python::AST::Module_t&, LFortran::diag::Diagnostics&, LFortran::ASR::asr_t*)
b.visit_Module(ast);
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/semantics/python_ast_to_asr.cpp", line 299, in LFortran::Python::BodyVisitor::visit_Module(LFortran::Python::AST::Module_t const&)
visit_stmt(*x.m_body[i]);
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/python_ast.h", line 1805, in LFortran::Python::AST::BaseVisitor<LFortran::Python::BodyVisitor>::visit_stmt(LFortran::Python::AST::stmt_t const&)
void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/python_ast.h", line 1677, in visit_stmt_t<LFortran::Python::BodyVisitor>()
case stmtType::FunctionDef: { v.visit_FunctionDef((const FunctionDef_t &)x); return; }
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/semantics/python_ast_to_asr.cpp", line 312, in LFortran::Python::BodyVisitor::visit_FunctionDef(LFortran::Python::AST::FunctionDef_t const&)
transform_stmts(body, x.n_body, x.m_body);
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/semantics/python_ast_to_asr.cpp", line 283, in LFortran::Python::BodyVisitor::transform_stmts(LFortran::Vec<LFortran::ASR::stmt_t*>&, unsigned long, LFortran::Python::AST::stmt_t**)
this->visit_stmt(*m_body[i]);
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/python_ast.h", line 1805, in LFortran::Python::AST::BaseVisitor<LFortran::Python::BodyVisitor>::visit_stmt(LFortran::Python::AST::stmt_t const&)
void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
File "/home/admin-pc/Smitlunagariya/lfortran/src/lfortran/python_ast.h", line 1680, in visit_stmt_t<LFortran::Python::BodyVisitor>()
case stmtType::Return: { v.visit_Return((const Return_t &)x); return; }
LFortranException: visit_Return() not implemented
|
Perfect, thanks. Yes, we have to implement |
Well, |
Let's use |
Let's implement # Uncomment once we implement the `sys` module
#from sys import exit
def ord(s: str) -> i32:
if s == '0':
return 48
elif s == '1':
return 49
else:
exit(1)
def chr(i: i32) -> str:
if i == 48:
return '0'
elif i == 49:
return '1'
else:
exit(1) |
To implement some of the features that we need, isolate the given feature and try to implement it, such as:
|
The |
Shall I update the functions
Testing Code:
Testing Code Output:
To accomplish #281 , I think that we might need |
@Shaikh-Ubaid I think so. I think we might need to fix some bugs in LPython to make it work, but I think an implementation along your lines should work. Can you please submit it as a PR and add some tests? P.S. My apologies for late answer, I just noticed your comments now. |
Yes, I will submit the |
Once LPython can use ASR's generic function facilities, it should just be a generic function. In the meantime, let's implement:
str_int(x)
... converts an integer to a stringstr_float(x)
... converts a floating point to a stringThese will be implemented in pure Python, such as:
For now, let's put this into one file, and print the result at the end. Some things will not be implemented in LPython yet, so let's open up issues for things that are not implemented yet, and we'll fix them.
The text was updated successfully, but these errors were encountered: