c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Add an optional 'Prefix' parameter to $echo

Open NotsoanoNimus opened this issue 6 months ago • 6 comments

Addresses Issue #1873 and attempts to resolve the $echo "better prefix" discussion by letting developers choose their own.

Code written against older versions of C3 which do not support prefixes will not be affected/broken because the prefix is the second (optional) param, i.e. $echo becomes $echo MESSAGE[, PREFIX] ;. Thus, messages without a PREFIX will still output the typical ] prefix.

The only notable constraint at the time of submission is that surrounding the two constant expressions with parentheses (see below) will not compile properly.

module main;

import std::io;


macro beacon($msg)
{
    $echo $msg, @sprintf("[%s:%s]>  ", $$FILE, $$LINE);
}


fn void main()
{
    $echo "Old-school, backwards-compatible";
    $echo("simple, still works");

    $echo "example,test", "PFX>>> ";
    // DOES NOT WORK:  $echo("parens", "<<< WITH >>>  ");

    beacon("It's this location!");

    io::printn("Hello, World!");
}
$ ./build/c3c compile-run /tmp/tmp.YHrcPSCpdl/hw.c3
] Old-school, backwards-compatible
] simple, still works
PFX>>> example,test
[hw.c3:18]>  It's this location!
Program linked to executable 'hw'.
Launching ./hw
Hello, World!
Program completed with exit code 0.

NotsoanoNimus avatar Jun 13 '25 19:06 NotsoanoNimus

Not sure about what I think of this solution, but the parens not working is because the $echo never needs parens anyway, just like the $for and $if, the parens are only used for operator precedence.

So something like $echo ("Hello"), ("prefix> "); works, I think.

BWindey avatar Jun 13 '25 20:06 BWindey

@BWindey Thanks for the feedback, and makes sense about the parentheses.

macro @echo($message)
{
    $echo $message, "$echo: ";
}

// ...
    @echo(@sprintf("Test message on line %s", $$LINE));
$echo: Test message on line 12

Something like this for those who want to customize a prefix is an easy solution, without settling on one option. It only adds a slight overhead to the builtin but resolves the "what should it be" problem for everyone who cares. Those who don't care can just keep $echoing as they do now. :smiley:

Anything else is too complex IMHO.

NotsoanoNimus avatar Jun 13 '25 21:06 NotsoanoNimus

An alternative is to make it configurable as a build parameter: --echo-prefix myecho

lerno avatar Jun 14 '25 22:06 lerno

An alternative is to make it configurable as a build parameter: --echo-prefix myecho

I think that's a great idea. Would this still work if someone wanted to use a prefix that includes tokens like $$FILE?

NotsoanoNimus avatar Jun 16 '25 13:06 NotsoanoNimus

It could I guess, although that is more work.

lerno avatar Jun 16 '25 20:06 lerno

It could I guess, although that is more work.

I suppose the original proposal is perhaps a more flexible option (whatever that gains C3).

But I'm willing to help with an implementation for either option/both; as no matter what is chosen, something extra on top of $echo would need to be configured. Whether that's a cmdline param or an extra macro.

NotsoanoNimus avatar Jun 18 '25 00:06 NotsoanoNimus

I added a simple --echo-prefix build option now. It can definitely be improved on. Currently it supports {FILE} and {LINE}

lerno avatar Jul 03 '25 23:07 lerno

I added a simple --echo-prefix build option now. It can definitely be improved on. Currently it supports {FILE} and {LINE}

Nice! We can close this in favor of your changes if the branch should be axed. Thank you.

NotsoanoNimus avatar Jul 04 '25 14:07 NotsoanoNimus

Thank you!

lerno avatar Jul 05 '25 00:07 lerno