c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Better prefix for $echo

Open tomaskallup opened this issue 11 months ago • 12 comments

Currently using $echo adds ] as a prefix, which is a bit confusing.

It might be better to use something like $ or c3c > .

tomaskallup avatar Jan 21 '25 18:01 tomaskallup

File name and line also could be useful, maybe.

alexveden avatar Jan 21 '25 19:01 alexveden

File-name can already be done with $$FILE (https://c3-lang.org/misc-advanced/builtins/#compiler-builtin-functions). Same for $$LINE and $$LINE_RAW (same url).

An example:

module main;
import std::io;

fn int main() {
	$echo "Compiler says hello in file " +++ $$FILE +++ " on line ";
	$echo $$LINE;
	io::printn("Hello, World!");
	return 0;
}

Produces:

$ c3c compile test.c3
] Compiler says hello in file test.c3 on line
] 8
Program linked to executable 'main'.

So I would not include filename and line into $echo, as it is already possible to do so now. The user could very well write their own macro to include that info without having to explicitly type it:

module main;

import std::io;

macro to_string($num) {
	char[] $res;
	$for (;$num != 0; $num = $num / 10)
		$res = { (char) ('0' + $num % 10) } +++ $res;
	$endfor
	return (String) $res;
}

macro compiler_message($msg) {
	$echo $msg +++ " in file " +++ $$FILE +++ " on line " +++ to_string($$LINE);
}

fn int main() {
	$echo "Compiler says hello in file " +++ $$FILE +++ " on line ";
	$echo $$LINE;
	io::printn("Hello, World!");
	compiler_message("Hello from compiler");
	return 0;
}

Correctly prints:

c3c compile test.c3
] Compiler says hello in file test.c3 on line
] 25
] Hello from compiler in file test.c3 on line 27
Program linked to executable 'main'.

Though I must admit that this to_string macro is a bit excessive, but I haven't found a shorter yet.

BWindey avatar Jan 21 '25 21:01 BWindey

ok, makes sense. I like macros in C3, one can solve any problem with them :)

re to_string($$LINE), does $stringify($$LINE) work?

alexveden avatar Jan 22 '25 06:01 alexveden

re to_string($$LINE), does $stringify($$LINE) work?

Nope, that just prints ] $$LINE

tomaskallup avatar Jan 22 '25 08:01 tomaskallup

No, that does not work. That's why I opened #1874, Christoffer told me in Discord that it would be a good addition. So in the future there will be more compile-time macros available by the compiler.

BWindey avatar Jan 22 '25 09:01 BWindey

So have you decided yet? 😄

lerno avatar Jan 23 '25 10:01 lerno

I would suggest $echo: as prefix, but I don't know what others think about it.

BWindey avatar Jan 23 '25 10:01 BWindey

No consensus yet?

lerno avatar Mar 19 '25 22:03 lerno

Ping

lerno avatar May 15 '25 20:05 lerno

Maybe a poll on Discord would be the way to close this issue?

BWindey avatar May 16 '25 05:05 BWindey

I agree with $echo, pretty clear where it comes from.

tomaskallup avatar May 16 '25 07:05 tomaskallup

Any consensus yet?

lerno avatar Jun 10 '25 20:06 lerno

This now has a --echo-prefix for setting it.

lerno avatar Jul 29 '25 21:07 lerno