Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 11, 2006 Multiversion conditional compilation | ||||
---|---|---|---|---|
| ||||
Hello, I am testing D compiler with various things (in my spare time) and I was programming conditional compilation and I have found one thing that is not into the language: this is multiversion conditional compilation. See next source: //-------------------source-------------------- import std.stdio; alias char[] string; int main(string[] args) { version(win) { printf("Windows\n"); } version(lin) { printf("Linux"\n); } version(mac) { printf("Mac\n"); } /* version(win,lin) { printf("Windows & Linux\n"); } version(lin,mac) { printf("Linux & Mac\n"); } */ printf("All versions.\n"); return 0; } //-----------------end source--------------------- This portion of code compile witout problems but if I want to compile some piece of code for more than one version (commented source), I have to write it twice and, in the manner I show here, it can be done with less code. Could it be included into the language in next versions? Thanks for your attention, regards. -- Diego Lago beosman@gmail.com P.D.: Sorry, I can't speak English very well, so it can be ununderstandable. |
January 11, 2006 Re: Multiversion conditional compilation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Diego Lago | Diego Lago wrote:
> This portion of code compile witout problems but if I want to compile some piece
> of code for more than one version (commented source), I have to write it twice
> and, in the manner I show here, it can be done with less code. Could it be
> included into the language in next versions?
Walter doesn't want expressions in "version", so you need workarounds...
version(win)
version(lin)
version = win_and_lin;
version(win_and_lin) {
printf("Windows & Linux\n");
}
Just as an example of course, as the real ones are "Windows" and "linux"
--anders
|
January 12, 2006 Re: Multiversion conditional compilation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | Anders F Björklund wrote:
>
> Walter doesn't want expressions in "version", so you need workarounds...
>
Say it ain't so!
I would love to be able to use, at the very least, boolean operators inside of version. So like: version(D_InlineAsm_X86 && X86)
I ran into this last night and ended up with
version(D_InlineAsm_X86) { version(X86)
{
... // cpu feature detection code here
}}
It works I suppose, but it sucks when you might end up having to nest 10 versions into each other!
I'd much rather do something like
version( D_InlineAsm_X86
&& X86
&& ...
&& ...)
{
...
}
I also tried to do this:
version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } }
but it gives me the following compiler errors (at that line):
(condition) expected following version
found '=' instead of statement
I thought the spec allowed for that, and it seems to be the same kinda thing Anders just posted. I am using compiler version 0.142 (the latest AFAIK). So why the compiler errors?
|
January 12, 2006 Re: Multiversion conditional compilation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chad J | In article <dq4m1t$2per$1@digitaldaemon.com>, Chad J says... > >Anders F Björklund wrote: > >> >> Walter doesn't want expressions in "version", so you need workarounds... >> > >Say it ain't so! I'm afraid Anders is right. Many of us (myself included) have been asking for cooler version statements for years now. I've long since given up trying to get him to change his mind. :( >I would love to be able to use, at the very least, boolean operators inside of version. So like: version(D_InlineAsm_X86 && X86) > >I ran into this last night and ended up with > >version(D_InlineAsm_X86) { version(X86) >{ > ... // cpu feature detection code here >}} > >It works I suppose, but it sucks when you might end up having to nest 10 >versions into each other! >I'd much rather do something like >version( D_InlineAsm_X86 > && X86 > && ... > && ...) >{ > ... >} > >I also tried to do this: >version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } } That line works for me (at least it compiles). Here's a whole "program". It's trivial, but it does compiles: version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } } void main() {} >but it gives me the following compiler errors (at that line): >(condition) expected following version >found '=' instead of statement I suspect something else is generating that error. Sometimes the error messages seems to point to a particular line as the culprit, but the problem is really right after or right before. It's a good compiler, but it can't read minds. ;) >I thought the spec allowed for that, and it seems to be the same kinda thing Anders just posted. I am using compiler version 0.142 (the latest AFAIK). So why the compiler errors? Well, actually, the latest compiler is 0.143 (just released a few hours ago), and that's what I tested it with it. So I guess you could try upgrading. But I suspect you just have a pesky extra semi-colon or an extra brace in a troublesome place. Good luck. I wish version could do more in the way you suggested, but I think the clever tricks can get us pretty close. jcc7 |
January 12, 2006 Re: Multiversion conditional compilation | ||||
---|---|---|---|---|
| ||||
Posted in reply to J C Calvarese | J C Calvarese wrote:
>>
>>I also tried to do this:
>>version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } }
>
>
> That line works for me (at least it compiles). Here's a whole "program". It's
> trivial, but it does compiles:
>
> version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } }
> void main() {}
>
>
>>but it gives me the following compiler errors (at that line):
>>(condition) expected following version
>>found '=' instead of statement
>
>
> I suspect something else is generating that error. Sometimes the error messages
> seems to point to a particular line as the culprit, but the problem is really
> right after or right before. It's a good compiler, but it can't read minds. ;)
>
That line I wrote was in a static this() {} function, at the module scope. I moved it out of static this(), and it compiled just fine. So I think I figured out why it wasn't working - version = something; doesn't work within function bodies.
try this:
void main()
{
version = something;
}
Should fail to compile, same errors as previous post. I couldn't find this in the specs, is this a bug?
|
January 12, 2006 Re: Multiversion conditional compilation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chad J | Chad J wrote:
> J C Calvarese wrote:
>
>>>
>>> I also tried to do this:
>>> version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } }
>>
>>
>>
>> That line works for me (at least it compiles). Here's a whole "program". It's
>> trivial, but it does compiles:
>>
>> version(D_InlineAsm_X86) { version(X86) { version = UseX86Asm; } }
>> void main() {}
>>
>>
>>> but it gives me the following compiler errors (at that line):
>>> (condition) expected following version
>>> found '=' instead of statement
>>
>>
>>
>> I suspect something else is generating that error. Sometimes the error messages
>> seems to point to a particular line as the culprit, but the problem is really
>> right after or right before. It's a good compiler, but it can't read minds. ;)
>>
>
> That line I wrote was in a static this() {} function, at the module scope. I moved it out of static this(), and it compiled just fine. So I think I figured out why it wasn't working - version = something; doesn't work within function bodies.
> try this:
>
> void main()
> {
> version = something;
> }
>
> Should fail to compile, same errors as previous post. I couldn't find this in the specs, is this a bug?
Maybe the spec doesn't say it directly, but, version = something is called "VersionSpecification" and it's only allowed as a DeclDef; you can't have a DeclDef inside a FunctionBody.
|
January 12, 2006 Re: Multiversion conditional compilation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chad J | Chad J wrote: >> Walter doesn't want expressions in "version", so you need workarounds... > > Say it ain't so! Or at least: "it hasn't been deemed important enough to include yet"... > I would love to be able to use, at the very least, boolean operators inside of version. So like: version(D_InlineAsm_X86 && X86) > > I ran into this last night and ended up with > > version(D_InlineAsm_X86) { version(X86) > { > ... // cpu feature detection code here > }} Not that it affects the issue, but for *this* particular example the version(X86) is not needed. As Walter has already included that ? The new "D_InlineAsm_X86" is the same as the corresponding old code: version(X86) version (D_InlineAsm) { version = D_InlineAsm_X86; } There is no support for inline PPC asm in GDC yet (but there is in GCC), but I don't like adding certain arch to the language define like that... It makes sense in practice, but I would rather have both of them around. version (D_InlineAsm) { version(X86) version = D_InlineAsm_X86; version(PPC) version = D_InlineAsm_PPC; } --anders PS. Then again, "version(PPC)" is still missing from the list of versions ? (http://www.digitalmars.com/d/version.html) Along with: "version(Unix)" Both have been in "version(GNU)", i.e. GDC, for a rather long time now. |
January 12, 2006 Re: Multiversion conditional compilation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | Anders F Björklund escribió: > Diego Lago wrote: > >> This portion of code compile witout problems but if I want to compile some piece >> of code for more than one version (commented source), I have to write it twice >> and, in the manner I show here, it can be done with less code. Could it be >> included into the language in next versions? > > Walter doesn't want expressions in "version", so you need workarounds... > > version(win) > version(lin) > version = win_and_lin; > > version(win_and_lin) { > printf("Windows & Linux\n"); > } > > Just as an example of course, as the real ones are "Windows" and "linux" > > --anders Shouldn't it be: version(win) version = win_or_lin; version(lin) version = win_or_lin; version(win_or_lin) { printf("Windows & Linux\n"); } ? Otherwise, it doesn't make sense... -- Carlos Santander Bernal |
January 13, 2006 Re: Multiversion conditional compilation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander | Carlos Santander wrote:
> Shouldn't it be:
>
> version(win)
> version = win_or_lin;
>
> version(lin)
> version = win_or_lin;
>
> version(win_or_lin) {
> printf("Windows & Linux\n");
> }
>
> ? Otherwise, it doesn't make sense...
It was just an example, by the OP.
s/win/Apples/; s/lin/Oranges; # :-)
--anders
PS. In your "corrected" example,
printf("Windows | Linux\n");
|
January 13, 2006 Re: Multiversion conditional compilation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | Anders F Björklund escribió: > Carlos Santander wrote: > >> Shouldn't it be: >> >> version(win) >> version = win_or_lin; >> >> version(lin) >> version = win_or_lin; >> >> version(win_or_lin) { >> printf("Windows & Linux\n"); >> } >> >> ? Otherwise, it doesn't make sense... > > It was just an example, by the OP. > > s/win/Apples/; s/lin/Oranges; # :-) > > --anders > > PS. In your "corrected" example, > printf("Windows | Linux\n"); I know both things, but it just didn't make sense. It's like boolean operators mean different things in programming languages than in natural languages: "women and children first", "do you want chicken or fish?". I understood that printf as saying "this is common for Windows and Linux". Anyway, when the OP actually codes, he'll see what he really wanted. -- Carlos Santander Bernal |
Copyright © 1999-2021 by the D Language Foundation