www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Feature Request: Label assignment / type

reply Joe Pusdesris <deformative0 gmail.com> writes:
I think it would be very useful to have a label type.  This way labels could
be assigned and passed around, opening up some flexability.  An example of
it's usefulness would be in constructing and manipulating a jump table or
navigating subroutines without calling a function. A goto is a lot faster
than a function call.

Possible syntax:
someLabel:
...
Label l = someLabel;
...
goto l;

Possible problems:
Difficulties would arise with limiting the variable to contain it within the
current scope.

I suppose some aspects of this are possible with -inline, but I think this
feature would be useful nonetheless.

Some feedback would be appreciated.
Nov 16 2007
next sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 11/17/07, Joe Pusdesris <deformative0 gmail.com> wrote:
 A goto is a lot faster
 than a function call.
You mean we haven't got rid of goto yet? Please let's not do anything which encourages it!
Nov 16 2007
prev sibling next sibling parent reply "Janice Caron" <caron800 googlemail.com> writes:
On 11/17/07, Joe Pusdesris <deformative0 gmail.com> wrote:
 A goto is a lot faster
 than a function call.
What's faster than what is for the compiler to figure out, not the programmer. The programmer should state intent. The compiler should figure out the fastest way to make it run.
Nov 16 2007
parent Leandro Lucarella <llucax gmail.com> writes:
Janice Caron, el 17 de noviembre a las 07:56 me escribiste:
 On 11/17/07, Joe Pusdesris <deformative0 gmail.com> wrote:
 A goto is a lot faster
 than a function call.
What's faster than what is for the compiler to figure out, not the programmer. The programmer should state intent. The compiler should figure out the fastest way to make it run.
Yeah! That's the kind of thoughts (this and the "goto is evil") that will lead D to be a real system-programming language =) D is beautiful because both Java and C programmers can be happy (the former using it allmost without knowing anything about pointer or low-level stuff and the latter because all kind of low level tricks can be done easily and elegantly). I think label type is a good idea, after all labels are just pointers. -- Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/ .------------------------------------------------------------------------, \ GPG: 5F5A8D05 // F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05 / '--------------------------------------------------------------------' DETIENEN A PADRE, MADRE, TIOS Y ABUELOS: TODOS DEPRAVADOS -- Crónica TV
Nov 17 2007
prev sibling next sibling parent TomD <t_demmer nospam.web.de> writes:
Joe Pusdesris Wrote:

 I think it would be very useful to have a label type.  This way labels could
 be assigned and passed around, opening up some flexability.  An example of
 it's usefulness would be in constructing and manipulating a jump table or
 navigating subroutines without calling a function. A goto is a lot faster
 than a function call.
 
 Possible syntax:
 someLabel:
 ...
 Label l = someLabel;
 ...
 goto l;
 
 Possible problems:
 Difficulties would arise with limiting the variable to contain it within the
 current scope.
 
 I suppose some aspects of this are possible with -inline, but I think this
 feature would be useful nonetheless.
 
 Some feedback would be appreciated.
Wow, assigned goto, alive in FORTRAN IV, buried in FORTRAN 77, coming back a zombie. Obfuscation is, as far as I can tell, not too high on Walter's list. Ciao Tom
Nov 17 2007
prev sibling parent reply Joe Pusdesris <deformative0 gmail.com> writes:
Joe Pusdesris wrote:

 I suppose some aspects of this are possible with -inline, but I think this
 feature would be useful nonetheless.
I just find it easier for things like jump tables and to specify a point to return to after finishing a block, but if no one else likes it that's cool too.
Nov 17 2007
parent reply "Janice Caron" <caron800 googlemail.com> writes:
On 11/17/07, Joe Pusdesris <deformative0 gmail.com> wrote:
 I just find it easier for things like jump tables and to specify a point to
 return to after finishing a block,  but if no one else likes it that's cool
 too.
switch statement often compile to jump tables. Or at least, they did in C - I've no idea what the D compiler does. So instead of Label a = label2; goto a; label1: /* stuff */ label2: /* stuff */ you do enum Label { label1, label2 }; Label a = label2; switch (a) { case label1: /* stuff */ case label2: /* stuff */ } It all boils down to the same thing.
Nov 17 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Janice Caron wrote:
 On 11/17/07, Joe Pusdesris <deformative0 gmail.com> wrote:
 I just find it easier for things like jump tables and to specify a point to
 return to after finishing a block,  but if no one else likes it that's cool
 too.
switch statement often compile to jump tables. Or at least, they did in C - I've no idea what the D compiler does. So instead of Label a = label2; goto a; label1: /* stuff */ label2: /* stuff */ you do enum Label { label1, label2 }; Label a = label2; switch (a) { case label1: /* stuff */ case label2: /* stuff */ } It all boils down to the same thing.
Perfect hashes can/are also be used for that, no? I'm curious how DMD handles string switches. switch(name) { case "bob": break; case "betty": break; case "barney": break; ... } I imagine something clever is possible to make that really efficient. But I wouldn't be surprised if it generated code similar to a regular if-then-else. --bb
Nov 17 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:fhnd3k$s31$1 digitalmars.com...
 Perfect hashes can/are also be used for that, no?
 I'm curious how DMD handles string switches.

 switch(name) {
    case "bob":
         break;
    case "betty":
         break;
    case "barney":
         break;
    ...
 }

 I imagine something clever is possible to make that really efficient. But 
 I wouldn't be surprised if it generated code similar to a regular 
 if-then-else.
You can see the code for it in dmd/src/phobos/internal/switch.d. It just does a binary search on a sorted array.
Nov 17 2007
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Jarrett Billingsley wrote:
 "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
 news:fhnd3k$s31$1 digitalmars.com...
 Perfect hashes can/are also be used for that, no?
 I'm curious how DMD handles string switches.

 switch(name) {
    case "bob":
         break;
    case "betty":
         break;
    case "barney":
         break;
    ...
 }

 I imagine something clever is possible to make that really efficient. But 
 I wouldn't be surprised if it generated code similar to a regular 
 if-then-else.
You can see the code for it in dmd/src/phobos/internal/switch.d. It just does a binary search on a sorted array.
Cool. Thanks for the info. Good to know it's at least smarter than a straight if-then-else. --bb
Nov 17 2007
prev sibling next sibling parent reply Joe Pusdesris <deformative0 gmail.com> writes:
Janice Caron wrote:

 On 11/17/07, Joe Pusdesris <deformative0 gmail.com> wrote:
 I just find it easier for things like jump tables and to specify a point
 to
 return to after finishing a block,  but if no one else likes it that's
 cool too.
switch statement often compile to jump tables. Or at least, they did in C - I've no idea what the D compiler does. So instead of Label a = label2; goto a; label1: /* stuff */ label2: /* stuff */ you do enum Label { label1, label2 }; Label a = label2; switch (a) { case label1: /* stuff */ case label2: /* stuff */ } It all boils down to the same thing.
Oh, wow, sticking a switch of a string in a while loop then changing the string did not even occur to me. That works great. Then to make additions to it at runtime, the default statement could lookup the string in an associative array and get a delegate to execute.
Nov 17 2007
parent Paul Findlay <r.lph50+d gmail.com> writes:
Joe Pusdesris wrote:
 Oh, wow,  sticking a switch of a string in a while loop then changing the
 string did not even occur to me.  That works great.  Then to make
 additions to it at runtime, the default statement could lookup the string
 in an associative array and get a delegate to execute.
So many cache misses... hope it won't run on a Pentium 4 - Paul
Nov 18 2007
prev sibling parent BCS <ao pathlink.com> writes:
Reply to Janice,

 On 11/17/07, Joe Pusdesris <deformative0 gmail.com> wrote:
 
 I just find it easier for things like jump tables and to specify a
 point to return to after finishing a block,  but if no one else likes
 it that's cool too.
 
switch statement often compile to jump tables. Or at least, they did in C - I've no idea what the D compiler does. So instead of Label a = label2; goto a; label1: /* stuff */ label2: /* stuff */ you do enum Label { label1, label2 }; Label a = label2; switch (a) { case label1: /* stuff */ case label2: /* stuff */ } It all boils down to the same thing.
in this case much of the benefit of the Label variable goto can be had if "goto case NonConst;" was allowed. The const version is.
Nov 17 2007