File:  [gforth] / gforth / test / libcc.fs
Revision 1.6: download - view: text, annotated - select for diffs
Mon Dec 31 16:43:57 2007 UTC (16 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Copyright comments updated
update-copyrigh now reports the author of "no copyright" files

    1: \ test libcc.fs C interface
    2: 
    3: \ Copyright (C) 2007 Free Software Foundation, Inc.
    4: 
    5: \ This file is part of Gforth.
    6: 
    7: \ Gforth is free software; you can redistribute it and/or
    8: \ modify it under the terms of the GNU General Public License
    9: \ as published by the Free Software Foundation; either version 2
   10: \ of the License, or (at your option) any later version.
   11: 
   12: \ This program is distributed in the hope that it will be useful,
   13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15: \ GNU General Public License for more details.
   16: 
   17: \ You should have received a copy of the GNU General Public License
   18: \ along with this program; if not, write to the Free Software
   19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: require libcc.fs
   22: 
   23: \c #include <string.h>
   24: \c #include <stdlib.h>
   25: 
   26: c-function strlen strlen a -- n
   27: cr s\" fooo\0" 2dup dump drop .s strlen cr .s drop cr 
   28: c-function labs labs n -- n
   29: 
   30: \c #define _FILE_OFFSET_BITS 64
   31: \c #include <sys/types.h>
   32: \c #include <unistd.h>
   33: c-function dlseek lseek n d n -- d
   34: 
   35: cr s\" fooo\0" 2dup dump drop .s strlen cr .s drop cr 
   36: -5 labs .s drop cr
   37: 
   38: \c #include <stdio.h>
   39: c-function printf-nr printf a n r -- n
   40: c-function printf-rn printf a r n -- n
   41: s\" n=%d r=%f\n\0" drop -5 -0.5e fp@ hex. cr printf-nr . cr
   42: s\" r=%f n=%d\n\0" drop -0.5e -5 printf-rn . cr
   43: 
   44: \c #define printfull(s,ull) printf(s,(unsigned long long)ull)
   45: c-function printfull printfull a n -- n
   46: s\" ull=%llu\n\0" drop -1 printfull . cr
   47: s\" ull=%llu r=%f\n\0" drop -1 -0.5e printf-nr . cr
   48: 
   49: \c #define printfll(s,ll) printf(s,(long long)ll)
   50: c-function printfll printfll a n -- n
   51: s\" ll=%lld\n\0" drop -1 printfll . cr
   52: s\" ll=%lld r=%f\n\0" drop -1 -0.5e printf-nr . cr
   53: 
   54: \ test calling a C function pointer from Forth
   55: 
   56: \ first create a C function pointer:
   57: 
   58: \c typedef long (* func1)(long);
   59: \c #define labsptr(dummy) ((func1)labs)
   60: c-function labsptr labsptr -- a
   61: 
   62: \ now the call
   63: \c #define call_func1(par1,fptr) ((func1)fptr)(par1)
   64: c-function call_func1 call_func1 n func -- n
   65: 
   66: -5 labsptr call_func1 . cr
   67: 

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