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 3
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, see http://www.gnu.org/licenses/.
19:
20: require libcc.fs
21:
22: \c #include <string.h>
23: \c #include <stdlib.h>
24:
25: c-function strlen strlen a -- n
26: cr s\" fooo\0" 2dup dump drop .s strlen cr .s drop cr
27: c-function labs labs n -- n
28:
29: \c #define _FILE_OFFSET_BITS 64
30: \c #include <sys/types.h>
31: \c #include <unistd.h>
32: c-function dlseek lseek n d n -- d
33:
34: cr s\" fooo\0" 2dup dump drop .s strlen cr .s drop cr
35: -5 labs .s drop cr
36:
37: \c #include <stdio.h>
38: c-function printf-nr printf a n r -- n
39: c-function printf-rn printf a r n -- n
40: s\" n=%d r=%f\n\0" drop -5 -0.5e fp@ hex. cr printf-nr . cr
41: s\" r=%f n=%d\n\0" drop -0.5e -5 printf-rn . cr
42:
43: \c #define printfull(s,ull) printf(s,(unsigned long long)ull)
44: c-function printfull printfull a n -- n
45: s\" ull=%llu\n\0" drop -1 printfull . cr
46: s\" ull=%llu r=%f\n\0" drop -1 -0.5e printf-nr . cr
47:
48: \c #define printfll(s,ll) printf(s,(long long)ll)
49: c-function printfll printfll a n -- n
50: s\" ll=%lld\n\0" drop -1 printfll . cr
51: s\" ll=%lld r=%f\n\0" drop -1 -0.5e printf-nr . cr
52:
53: \ test calling a C function pointer from Forth
54:
55: \ first create a C function pointer:
56:
57: \c typedef long (* func1)(long);
58: \c #define labsptr(dummy) ((func1)labs)
59: c-function labsptr labsptr -- a
60:
61: \ now the call
62: \c #define call_func1(par1,fptr) ((func1)fptr)(par1)
63: c-function call_func1 call_func1 n func -- n
64:
65: -5 labsptr call_func1 . cr
66:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>