Skip to content
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

Better prefix for $echo #1873

Open
tomaskallup opened this issue Jan 21, 2025 · 7 comments
Open

Better prefix for $echo #1873

tomaskallup opened this issue Jan 21, 2025 · 7 comments

Comments

@tomaskallup
Copy link
Contributor

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

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

@alexveden
Copy link
Contributor

File name and line also could be useful, maybe.

@BWindey
Copy link
Contributor

BWindey commented Jan 21, 2025

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.

@alexveden
Copy link
Contributor

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

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

@tomaskallup
Copy link
Contributor Author

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

Nope, that just prints ] $$LINE

@BWindey
Copy link
Contributor

BWindey commented Jan 22, 2025

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.

@lerno
Copy link
Collaborator

lerno commented Jan 23, 2025

So have you decided yet? 😄

@BWindey
Copy link
Contributor

BWindey commented Jan 23, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants