Description
One way to handle string formatting and printing in a language independent approach is to decouple string formatting form printing. The Print
ASR node would just do printing and printing related flags. I think it's pretty much there.
Then we need an expr node like StringFormat
, which will handle all string formatting. The string formatting is inherently runtime operation, so the StringFormat
would accept the format string (in various styles) and the variables to format, and it would produce formatted string. It is not clear if a single StringFormat
can handle all formatting styles. If not, we will introduce StringFormatFortran
, StringFormatC
, StringFormatPython1
(%s
style), StringFormatPython2
(the {}
style) etc. (there are also f-strings in Python, it would be nice to generate the original f-string from ASR if possible).
Then in Fortran print "(i0, i0)", a, b
will become Print(StringFormatFortran("(i0, i0)", a, b))
, and Python's print("%d %d", a, b)
would become Print(StringFormatPython1("%d %d", a, b))
.