Re: switch statement

new topic     » goto parent     » topic index » view thread      » older message » newer message
Critic said...

Well, firstly, "switch" is only standard for C-syntax based languages. EU is not one of them. Secondly, C's switch supports duff's device which I think EU's does not, so C's is still more powerful.

Actually, euphoria's switch can do Duff's device. Here's the Stroustroup version from Wikipedia:

dsend(to, from, count) 
char *to, *from; 
int count; 
{ 
    int n = (count + 7) / 8; 
    switch (count % 8) { 
    case 0: do { *to++ = *from++; 
    case 7:      *to++ = *from++; 
    case 6:      *to++ = *from++; 
    case 5:      *to++ = *from++; 
    case 4:      *to++ = *from++; 
    case 3:      *to++ = *from++; 
    case 2:      *to++ = *from++; 
    case 1:      *to++ = *from++; 
               } while (--n > 0); 
    } 
} 
Here's a euphoria version (used the print operator instead of poking to memory, but demonstrates the interesting part of the device):

rocedure dprint( atom To, atom from, integer count ) 
    integer n = floor( (count+7) / 8 ) 
    switch remainder( count, 8 ) do 
	case 0:  
	loop do 
		? To & from To += 1 from += 1 
	case 7: 
		? To & from To += 1 from += 1 
	case 6: 
		? To & from To += 1 from += 1 
	case 5: 
		? To & from To += 1 from += 1 
	case 4: 
		? To & from To += 1 from += 1 
	case 3: 
		? To & from To += 1 from += 1 
	case 2: 
		? To & from To += 1 from += 1 
	case 1: 
		? To & from To += 1 from += 1 
	 
	n -= 1 
	until n <= 0 
    end switch 
end procedure 
Critic said...

Fourthly, Pascal's case supports ranges, "standard switch" does not, so one could argue that Pascal's case is more powerful.

Yes, though it could be merely syntactic sugar. With a large enough range, though, you probably need to (want to?) change from a simple jump table. We've talked about doing such a thing for euphoria.

Matt

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu