C++ style of outputing in ATS

Today I figured out a way to do C++ style of outputing in ATS.

See:

https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/CA-HSR2/program-1-3.dats

val A = 1
val B = 2
val () = println! “(” A " + " B “) = " (A+B) // ATS style
val () = println! (”(", A, " + ", B, ") = ", A+B) // also ATS style
val () = cout << “(” << A << " + " << B << ") = " << (A+B) << endl // C++
style

Here is something I come up with:

val hundred = 100
//
val () = println! ("hundred = ", hundred)
//
val () =
{
fun print_int
(x: int): void = ignoret ($extfcall(int, “printf”, “%04X”, x))
overload print with print_int of 1000000
//
val () = println! ("hundred = ", hundred)
}
//
val () = println! ("hundred = ", hundred)

Here is the output:

hundred = 100
hundred = 0064
hundred = 100On Tuesday, February 25, 2014 11:32:12 PM UTC-5, Artyom Shalkhakov wrote:

On Tuesday, February 25, 2014 9:05:43 PM UTC+6, gmhwxi wrote:

You guessed right :slight_smile:

I did this experimental style of outputting when I was translating some
C++ code.

The macros ‘print!’ and ‘println!’ in ATS are loosely based on python’s
print, which are
pretty convenient. So I no longer plan to support C-style printf in ATS2.
If one need
printf, then one can always use $extfcall to call it directly. While this
is not type-safe,
it is really a very minor issue.

Is it possible to set formatting options with print! and println! ?

For instance, with printf one might do:

printf(“%04X”, 15) // prints 15 in hex, zero-padded to 4 digits

How can this be implemented with println! or print! ?

On Tuesday, February 25, 2014 6:16:22 AM UTC-5, Artyom Shalkhakov wrote:

I’m wondering on what grounds would one prefer one style to another?

For instance, I’d probably pick C++ style formatting if I were rewriting
some C++ code to ATS. Other reasons?

On Monday, February 24, 2014 11:56:11 AM UTC+6, gmhwxi wrote:

Today I figured out a way to do C++ style of outputing in ATS.

See:

https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/CA-HSR2/program-1-3.dats

val A = 1
val B = 2
val () = println! “(” A " + " B “) = " (A+B) // ATS style
val () = println! (”(", A, " + ", B, ") = ", A+B) // also ATS style
val () = cout << “(” << A << " + " << B << ") = " << (A+B) << endl //
C++ style

So the last line will desugar to something like:

fprint_endl (
fprint_int (
fprint_string (
fprint_string (
fprint_string (
fprint_int (
fprint_string (cout, “(”),
A),
" + "),
B),
" = "),
A+B),
endl)

(I did not run the above. It is likely that the code is messed up in
some way.)

You guessed right :slight_smile:

I did this experimental style of outputting when I was translating some C++
code.

The macros ‘print!’ and ‘println!’ in ATS are loosely based on python’s
print, which are
pretty convenient. So I no longer plan to support C-style printf in ATS2.
If one need
printf, then one can always use $extfcall to call it directly. While this
is not type-safe,
it is really a very minor issue.On Tuesday, February 25, 2014 6:16:22 AM UTC-5, Artyom Shalkhakov wrote:

I’m wondering on what grounds would one prefer one style to another?

For instance, I’d probably pick C++ style formatting if I were rewriting
some C++ code to ATS. Other reasons?

On Monday, February 24, 2014 11:56:11 AM UTC+6, gmhwxi wrote:

Today I figured out a way to do C++ style of outputing in ATS.

See:

https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/CA-HSR2/program-1-3.dats

val A = 1
val B = 2
val () = println! “(” A " + " B “) = " (A+B) // ATS style
val () = println! (”(", A, " + ", B, ") = ", A+B) // also ATS style
val () = cout << “(” << A << " + " << B << ") = " << (A+B) << endl // C++
style

So the last line will desugar to something like:

fprint_endl (
fprint_int (
fprint_string (
fprint_string (
fprint_string (
fprint_int (
fprint_string (cout, “(”),
A),
" + "),
B),
" = "),
A+B),
endl)

(I did not run the above. It is likely that the code is messed up in some
way.)

You guessed right :slight_smile:

I did this experimental style of outputting when I was translating some
C++ code.

The macros ‘print!’ and ‘println!’ in ATS are loosely based on python’s
print, which are
pretty convenient. So I no longer plan to support C-style printf in ATS2.
If one need
printf, then one can always use $extfcall to call it directly. While this
is not type-safe,
it is really a very minor issue.

Is it possible to set formatting options with print! and println! ?

For instance, with printf one might do:

printf(“%04X”, 15) // prints 15 in hex, zero-padded to 4 digits

How can this be implemented with println! or print! ?

I’m wondering on what grounds would one prefer one style to another?

For instance, I’d probably pick C++ style formatting if I were rewriting
some C++ code to ATS. Other reasons?On Monday, February 24, 2014 11:56:11 AM UTC+6, gmhwxi wrote:

Today I figured out a way to do C++ style of outputing in ATS.

See:

https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/CA-HSR2/program-1-3.dats

val A = 1
val B = 2
val () = println! “(” A " + " B “) = " (A+B) // ATS style
val () = println! (”(", A, " + ", B, ") = ", A+B) // also ATS style
val () = cout << “(” << A << " + " << B << ") = " << (A+B) << endl // C++
style

So the last line will desugar to something like:

fprint_endl (
fprint_int (
fprint_string (
fprint_string (
fprint_string (
fprint_int (
fprint_string (cout, “(”),
A),
" + "),
B),
" = "),
A+B),
endl)

(I did not run the above. It is likely that the code is messed up in some
way.)