File:  [gforth] / gforth / test / libcc.fs
Revision 1.5: download - view: text, annotated - select for diffs
Sat Jun 9 15:55:45 2007 UTC (16 years, 10 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Documented and tested calling C function pointers

    1: require libcc.fs
    2: 
    3: \c #include <string.h>
    4: \c #include <stdlib.h>
    5: 
    6: c-function strlen strlen a -- n
    7: cr s\" fooo\0" 2dup dump drop .s strlen cr .s drop cr 
    8: c-function labs labs n -- n
    9: 
   10: \c #define _FILE_OFFSET_BITS 64
   11: \c #include <sys/types.h>
   12: \c #include <unistd.h>
   13: c-function dlseek lseek n d n -- d
   14: 
   15: cr s\" fooo\0" 2dup dump drop .s strlen cr .s drop cr 
   16: -5 labs .s drop cr
   17: 
   18: \c #include <stdio.h>
   19: c-function printf-nr printf a n r -- n
   20: c-function printf-rn printf a r n -- n
   21: s\" n=%d r=%f\n\0" drop -5 -0.5e fp@ hex. cr printf-nr . cr
   22: s\" r=%f n=%d\n\0" drop -0.5e -5 printf-rn . cr
   23: 
   24: \c #define printfull(s,ull) printf(s,(unsigned long long)ull)
   25: c-function printfull printfull a n -- n
   26: s\" ull=%llu\n\0" drop -1 printfull . cr
   27: s\" ull=%llu r=%f\n\0" drop -1 -0.5e printf-nr . cr
   28: 
   29: \c #define printfll(s,ll) printf(s,(long long)ll)
   30: c-function printfll printfll a n -- n
   31: s\" ll=%lld\n\0" drop -1 printfll . cr
   32: s\" ll=%lld r=%f\n\0" drop -1 -0.5e printf-nr . cr
   33: 
   34: \ test calling a C function pointer from Forth
   35: 
   36: \ first create a C function pointer:
   37: 
   38: \c typedef long (* func1)(long);
   39: \c #define labsptr(dummy) ((func1)labs)
   40: c-function labsptr labsptr -- a
   41: 
   42: \ now the call
   43: \c #define call_func1(par1,fptr) ((func1)fptr)(par1)
   44: c-function call_func1 call_func1 n func -- n
   45: 
   46: -5 labsptr call_func1 . cr
   47: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>