Can this snippet be expressed in C? 
Author Message
 Can this snippet be expressed in C?

\ I haven't quite got my "C" legs yet.
\ How can the following be expressed in ANSI "C"?

: one   ." One" ;
: two   ." Two" ;
: three ." Three" ;

create exectable  3 cells allot
( This is in RAM, all else is in ROM )


: init  ['] one    exectable !
        ['] two    exectable cell+ !
        ['] three  exectable 2 cells + ! ;

: eins  0 myexec ;
: zwei  1 myexec ;
: drei  2 myexec ;

\ -- Brad Eckert

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.



Fri, 23 Nov 2001 03:00:00 GMT  
 Can this snippet be expressed in C?
\ I haven't quite got my "C" legs yet.
\ How can the following be expressed in ANSI "C"?

: one   ." One" ;
: two   ." Two" ;
: three ." Three" ;

create exectable  3 cells allot
( This is in RAM, all else is in ROM )


: init  ['] one    exectable !
        ['] two    exectable cell+ !
        ['] three  exectable 2 cells + ! ;

: eins  0 myexec ;
: zwei  1 myexec ;
: drei  2 myexec ;

\ -- Brad Eckert

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Fri, 23 Nov 2001 03:00:00 GMT  
 Can this snippet be expressed in C?
#include <stdio.h>

void one() { printf("One"); }
void two() { printf("two"); }
void three() { printf("three"); }

typedef void (*proc_ptr)();

proc_ptr exectable[3] = { one, two, three };

void myexec(short idx)
{
 exectable[idx]();

Quote:
}

void eins() { myexec(0); }
void zwei() { myexec(1); }
void drei() { myexec(2); }

Have fun!
Conrad Weyns,
Oslo, Norway.

----------

Quote:

>\ I haven't quite got my "C" legs yet.
>\ How can the following be expressed in ANSI "C"?

>: one   ." One" ;
>: two   ." Two" ;
>: three ." Three" ;

>create exectable  3 cells allot
>( This is in RAM, all else is in ROM )


>: init  ['] one    exectable !
>        ['] two    exectable cell+ !
>        ['] three  exectable 2 cells + ! ;

>: eins  0 myexec ;
>: zwei  1 myexec ;
>: drei  2 myexec ;

>\ -- Brad Eckert

>Sent via Deja.com http://www.deja.com/
>Share what you know. Learn what you don't.



Fri, 23 Nov 2001 03:00:00 GMT  
 Can this snippet be expressed in C?
#include <stdio.h>
void one() { printf("One") ; }
void two() { printf("Two") ; }
void three() { printf("Three") ; }
typedef void (*func)() ;
func exectable[] = {
    one, two, three
Quote:
} ;

void myexec(int n) { (*exectable[n])() ; }
void eins() { myexec(0) ; }
void zwei() { myexec(1) ; }
void drei() { myexec(2) ; }

int main(void) {
 eins() ;
 zwei() ;
 drei() ;
 return 0;

Quote:
}

/*
Output:
OneTwoThree
*/
--

http://www.sms.ndirect.co.uk/rants/

----------

Quote:

> \ I haven't quite got my "C" legs yet.
> \ How can the following be expressed in ANSI "C"?

> : one   ." One" ;
> : two   ." Two" ;
> : three ." Three" ;

> create exectable  3 cells allot
> ( This is in RAM, all else is in ROM )


> : init  ['] one    exectable !
>         ['] two    exectable cell+ !
>         ['] three  exectable 2 cells + ! ;

> : eins  0 myexec ;
> : zwei  1 myexec ;
> : drei  2 myexec ;

> \ -- Brad Eckert

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.



Fri, 23 Nov 2001 03:00:00 GMT  
 Can this snippet be expressed in C?
And in Java?  :-)


Sat, 24 Nov 2001 03:00:00 GMT  
 Can this snippet be expressed in C?


Quote:
>And in Java?  :-)

;-)

Then you'd do it like it would most probably be done in C, with a case
construct... (Which would work even if the original poster was
thinking of some kind of state machine etc.)

Stefan,
--
Stefan Axelsson                         Chalmers University of Technology

(Remove "rmovt.rply" to send mail.)



Sat, 24 Nov 2001 03:00:00 GMT  
 Can this snippet be expressed in C?


Quote:
> #include <stdio.h>

> void one() { printf("One"); }
> void two() { printf("two"); }
> void three() { printf("three"); }

> typedef void (*proc_ptr)();

> proc_ptr exectable[3] = { one, two, three };

> void myexec(short idx)
> {
>  exectable[idx]();
> }

> void eins() { myexec(0); }
> void zwei() { myexec(1); }
> void drei() { myexec(2); }

Thanks.  Now my question is, can I change elements of exectable at run
time?  For example, the C equivalent of ['] two exectable ! should make
eins print "two".  Will a cross compiler place the above exectable in
ROM, or will it be smart enough to allocate RAM and generate the
necessary initialization code.

-- Brad Eckert

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Sun, 25 Nov 2001 03:00:00 GMT  
 Can this snippet be expressed in C?

Quote:



>> #include <stdio.h>

>> void one() { printf("One"); }
>> void two() { printf("two"); }
>> void three() { printf("three"); }

>> typedef void (*proc_ptr)();

>> proc_ptr exectable[3] = { one, two, three };

>> void myexec(short idx)
>> {
>>  exectable[idx]();
>> }

>> void eins() { myexec(0); }
>> void zwei() { myexec(1); }
>> void drei() { myexec(2); }

>Thanks.  Now my question is, can I change elements of exectable at run
>time?  For example, the C equivalent of ['] two exectable ! should make
>eins print "two".

    executable[0] = two;

Quote:
>Will a cross compiler place the above exectable in
>ROM, or will it be smart enough to allocate RAM and generate the
>necessary initialization code.

Unless you define the array 'const' it will be in RAM and the startup code
will initialize it.

Andrew



Sun, 25 Nov 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. to CS: or not to CS: in F-PC assembler

2. Runs on MicroFocus Net Express but not on Unix server express

3. CA Cans VO ?

4. It's not bad canned meat...

5. It's not bad canned meat...

6. It's not bad canned meat...

7. Using CGI module with 'canned queries'

8. It's not bad canned meat...

9. I am not deaf, but am I mute?

10. Evaluating Snippet With Unmatched "]" Problem

11. SVC 11 (includes code snippet)

12. Need your snippet on reflection use

 

 
Powered by phpBB® Forum Software