lib
demo
cfunc.c
Go to the documentation of this file.
1
/* Example for EusLisp foreign language interface
2
/* last maintained: 2004-Dec-16 by Toshihiro Matsui, DHRC, AIST
3
/* C program named cfunc.c
4
/*
5
/* Compile this program with the following command line on Linux
6
/* % cc -c -falign-functions=4 cfunc.c; ld -o cfunc.so -shared cfunc.o
7
*/
8
9
static
int (*
g
)();
/* variable to store Lisp function entry */
10
static
double (*
gf
)();
11
12
double
sync
(x)
13
double
x;
14
{
extern
double
sin
();
15
return
(
sin
(x)/x);}
16
17
char
*
upperstring
(
s
)
18
char
*
s
;
19
{
char
*ss=
s
;
20
while
(*
s
) {
if
(islower(*
s
)) *
s
=toupper(*
s
);
s
++;}
21
return
(ss);}
22
23
int
setlfunc
(f)
/* remember the argument in g just to see */
24
int (*f)();
/* how Lisp function can be called from C */
25
{
g
=
f
;}
26
27
int
setlffunc
(f)
/* remember the argument in g just to see */
28
double (*f)();
/* how Lisp function can be called from C */
29
{
gf
=
f
;}
30
31
int
callfunc
(x)
/* apply the Lisp function saved in g to the arg.*/
32
int
x;
33
{
return
((*
g
)(x));}
34
35
double
callfltfunc
(
double
dx)
36
{
return
((*
gf
)(dx)); }
37
38
39
/*************************
40
;;;; Example program for EusLisp's foreign language interface
41
;;;; make foreign-module
42
(setq m (load-foreign "cfunc.so"))
43
44
;; define foreign functions so that they can be callable from lisp
45
(defforeign sync m "sync" (:float) :float)
46
(defforeign toupper m "upperstring" (:string) :string)
47
(defforeign setlfunc m "setlfunc" (:integer) :integer)
48
(defforeign setlffunc m "setlffunc" (:integer) :integer)
49
(defforeign callfunc m "callfunc" (:integer) :integer)
50
(defforeign callfltfunc m "callfltfunc" (:float) :float)
51
52
;; call them
53
(sync 1.0) ;; --> 0.841471
54
(print (toupper "abc123")) ;;--> "ABC123"
55
56
;; define a test function which is callable from C.
57
(defun-c-callable LISP-INTFUNC ((a :integer)) :integer
58
(format t "LISP-INTFUNC is called, arg=~s~%" a)
59
(* a a)) ;; return the square of the arg
60
(defun-c-callable LISP-FLTFUNC ((f :float)) :float
61
(format t "LISP-FLTFUNC is called, arg=~s~%" f)
62
(sqrt f)) ;; return the square root of the arg
63
64
;; call it from C
65
;;setlfunc remembers the entry address of Lisp TEST function.
66
(setlfunc (pod-address 'LISP-INTFUNC))
67
(callfunc 12) ;; --> 144
68
(setlffunc (pod-address 'LISP-FLTFUNC))
69
(callfltfunc 2.0)
70
********************************/
71
s
short s
Definition:
structsize.c:2
gf
static double(* gf)()
Definition:
cfunc.c:10
g
static int(* g)()
Definition:
cfunc.c:9
upperstring
char * upperstring(char *s)
Definition:
cfunc.c:17
sin
double sin()
callfltfunc
double callfltfunc(double dx)
Definition:
cfunc.c:35
sync
double sync(double x)
Definition:
cfunc.c:12
f
f
callfunc
int callfunc(int x)
Definition:
cfunc.c:31
setlfunc
int setlfunc(int(*f)())
Definition:
cfunc.c:23
setlffunc
int setlffunc(double(*f)())
Definition:
cfunc.c:27
euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 15 2023 02:06:43