rdmd --main

spir denis.spir at gmail.com
Tue Nov 23 02:02:19 PST 2010


On Tue, 23 Nov 2010 04:44:51 +0800
KennyTM~ <kennytm at gmail.com> wrote:

> On Nov 23, 10 04:38, spir wrote:
> > On Mon, 22 Nov 2010 13:28:51 -0600
> > Andrei Alexandrescu<SeeWebsiteForEmail at erdani.org>  wrote:
> >
> >> On 11/22/10 12:53 PM, spir wrote:
> >>> Hello,
> >>>
> >>>
> >>> The rdmd option --main is really helpful to test modules independently (it adds an empty main() to prevent the linker from crying). An issue is that each time we switch between .d files that have no main() (modules in tended for export) and app files that have one, this option must be set or removed (else the linker complains for having 2 main()).
> >>> This is not very practicle for rapid edit-test cycles -- for "exploratory" programming in general. Would it be difficult for rdmd to detect whether a main is already present? It is not a big issue anyway -- just in case would be easy.
> >>
> >> That feature could be added in several ways.
> >>
> >> 1. Cooperate with the compiler - have the compiler detect whether or not
> >> main() exists.
> >>
> >> 2. Write a tokenizer (parser is not really needed) and have it identify
> >> the occurrence of main with one of the appropriate signatures at top level.
> >>
> >> 3. Use compiler's json output.
> >>
> >> 4. Use a low-tech search such as sed to find main.
> >>
> >> I fear that 2-4 would slow things down and add complexity. Probably 1
> >> would be the best choice. I'll ask Walter.
> >
> > Thank you for taking this into account. Indeed, asking the compiler is the best solution, but may require some work and testing, I guess. For a non-perfect solution (because would not work in 100% cases), just detecting "\nint main" or "\nvoid main" in file would nearly always work, don't you think? (Possibly adding some opt whitespace in the pattern to catch even more cases.) Or are there issues I'm not aware of. (Else, I would be happy with this, but it's only me.)
> >
> > Denis
> > -- -- -- -- -- -- --
> > vit esse estrany ☣
> >
> > spir.wikidot.com
> >
> 
> The issue is "not work in 100% cases" :)
> 
> class Tier {
>    private int mainValue;
>    private int secondaryValue;
>    int main(int factor) const { return mainValue * factor; }
>    // ^
>    int secondary(int factor) const { return secondaryValue * factor; }
>    ...
> }

You are right, but this case would correctly not be caught (as long as the search pattern does not allow free whitespace at start of main's header line), because you properly indented the class's definition.
Something like (untested -- and I don't know yet D's regexes):
	auto mainSearchPattern = r"^(int/void)[ \t]+main[ \t]*(";
Now, we cannot prevent anyone to write Hello, world as

// my first program in d
/////////////////////////////////////////////////////////////
			               void main (){
/////////////////////////////////////////////////////////////
	writeln("Hello, world!");}

What I imagine is (0) if no --main in cmdline, pass this phase, else (1) read the (top/passed) module into a string (2) search for main() using a paatern like this one (3) if not found, append "\nvoid main () {}\n".


Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the Digitalmars-d mailing list