Conversation
|
I'm aware of all of the failing tests. They're compilation error details, it worked on my Ubuntu machine, but it's failing here. That's normal. I'll solve them. |
5e4e9bb to
615f09e
Compare
I think so.
The verify pass should have a check for this.
Yes. LPython will need to use StringFormat also. We can write a utility function that does it automatically for LPython.
I think so.
Print is a statement, so it doesn't have an evaluation. But StringFormat is an expr so it should have |
|
The replace symbolic is for symbolic (SymPy like) expressions. The Print replacement allowed printing the expressions. Those should be now handled in StringFormat. @anutosh491 can help with this later. For now it's fine, we can even disable this pass in LFortran if needed. We should add Fortran tests for symbolics also, to ensure things work. This is all unrelated. |
certik
left a comment
There was a problem hiding this comment.
I think that this is fine. Thanks for finishing this.
@Pranavchiku can you please also review this if you see any big problems? Small issues can be fixed after we merge.
|
I'll review this by tonight. |
Pranavchiku
left a comment
There was a problem hiding this comment.
Left a few comments above, please address those changes before merging. Reviewed references, looks good to me, except one which needs to be looked by Assem. Thanks for doing this!
|
@assem2002 let's finish this, I think it is almost done. |
615f09e to
480add4
Compare
|
It should be ready now. |
|
Thank you. I think this is great! Can you please resolve the conflicts? To summarize, this PR makes the @Pranavchiku, @gxyd can you please also review this PR if you see any issues? |
480add4 to
1cb7524
Compare
| bool global_sep_space = true; | ||
| end = builder->CreateGlobalStringPtr("\n"); | ||
| sep = builder->CreateGlobalStringPtr(" "); | ||
| if constexpr (x.class_type == ASR::stmtType::FileWrite){ // Remove after write node refactor. |
There was a problem hiding this comment.
How can this be constexpr if x is runtime?
There was a problem hiding this comment.
as I know, x should be either filewrite or print. CPP should evaluate that if condition at compile time which gives me the ability to use filewrite members freely without compile time errors as this function is generic.
There was a problem hiding this comment.
I have a question. Will we have similar refactors for filewrite node as we are doing now with print?
There was a problem hiding this comment.
Ah I see, x is compile time here. Clever!
Yes, we should refactor FileWrite in a similar way later.
| nullptr, nullptr, args.p, args.size(), nullptr, empty_string, nullptr); | ||
| } | ||
| return ASR::make_FileWrite_t(al, loc, 0, nullptr, nullptr, | ||
| nullptr, nullptr, args.p, args.size(), nullptr, empty_string, nullptr); |
There was a problem hiding this comment.
This is fine as long as the leading space still works for print, by adding the space using StringFormat or some other way.
There was a problem hiding this comment.
You can test it like this:
$ lfortran examples/expr2.f90
25
$ lfortran --print-leading-space examples/expr2.f90
25
$There was a problem hiding this comment.
Yeah it prints the same. We have test for this case btw (reference test print3.f90).
There was a problem hiding this comment.
I tried to use print_t but I couldn't tell the backend to use lfortran_printf() with no \n character as now print_t doesn't have end member (the standard way if no end is provided is to use \n character).
If you can suggest better approach, it would be super helpful.
There was a problem hiding this comment.
So lfortran --print-leading-space examples/expr2.f90 does not print the lead space now?
If so, then in AST->ASR you need to check for this flag, and if present, use StringFormat and concatenate " " and the other expression.
Regarding no end, that has to be implemented by having \n in the string itself. Print should not append \n. As a temporary solution you can reintroduce "end" into Print, to either append or not append \n. But this has to be done by the frontend to decide if \n is needed, and if so, append it via StringFormat. So unfortunately for now print("Hello") will turn into Print(StringFormat(["Hello", "\n"])). But later, we'll add compile time evaluation of StringFormat, that would provide a value "Hello\n" for this case, and in the backend you would use this value (if present), so in the end, the LLVM generated code would just print a simple string "Hello\n", which is the optimal way. If you can get this PR to work with having the backend to just append \n, that would be the easiest, and then once this is in, we can further refactor this based on this paragraph.
There was a problem hiding this comment.
Actullay that's much better to embed the " " in the stringformat args. it should work for both filewrite and print.
I think with your suggestion we won't need end member to help us with the leading space issue.
In the backend we print \n after each use of lfortran_printf() or lfortran_string_write() unless we set end in filewrite to something else.
I'll embed the " " character if leading space flag is true.
certik
left a comment
There was a problem hiding this comment.
I reviewed every commit, it looks good. I have just one concern:
As long as that works, then we can merge.
Pranavchiku
left a comment
There was a problem hiding this comment.
The changes look good to me, references are good too.
|
Looks good. I shall address this TODO commit here (3782c7a) once this goes in |
|
We need to print the empty space for |
|
(FileWrite
0
()
()
()
()
[(StringConstant
" "
(Character 1 1 () PointerString)
)]
()
(StringConstant
""
(Character 1 0 () PointerString)
)
()
)
(Print
(StringFormat
()
[(Var 2 x)]
FormatFortran
(Character -1 0 () PointerString)
()
)
)]
)With the suggestion you made here #4696 (comment) about adding just an arg into the stringFormat node instead of creating a whole (Print
(StringFormat
()
[(StringConstant
" "
(Character 1 1 () PointerString)
)
(Var 2 x)]
FormatFortran
(Character -1 0 () PointerString)
()
)
)]
)
but the final print would be --> Let me know what you think is better to use. |
- The refactor cleans up some uses of the removed members of the old node. - Handles generic function that operate on 'write' + 'print' - Created 'make_print_t_util()' to build a print node with stringformat as the text to hold the arguments to be printed
by not using a generic function for both and just handle string format or handle the single character in each one's standalone visitor
…his is the only node that supports 'advance=no' feature now, print doesn't support 'end' of value none, so we had to use FileWrite node
1cb7524 to
83cefd6
Compare
certik
left a comment
There was a problem hiding this comment.
Thanks! I think this is fine to merge. The rest we can improve upon later, such as using StringFormat for everything, including FileWrite, and handle leading spaces and \n in StringFormat.
This is a huge step forward, now Print is clean. Thank you!
towards #2543
Concerns
print_tonly member to be 'text', is that a good choice?printonly supporting single argument of character type only. Should we throw an error when we encounter atextmember that isn't of typecharacteror should we only assert or to not even care about that?endandsepfromprintgonna affect lPython?make_print_t_util()that creates a print node with stringformat wrapped in it if necessary, or should build such a thing on our own each time we want print bunch of arguments.replace_symbolicpass visitsprintand sees if it has some ready compile time values (In the oldprintnode definition, not the one in this PR), then it replaces the argument with value (as I understand) ; Do we benefit from this visitor?, if so we would need to addvaluesmember intoprintstructure