(Fly Evaluation): Add scm_call_4, suggested by Bruce Korb.
[bpt/guile.git] / doc / ref / api-overview.texi
CommitLineData
07d83abe
MV
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
7@page
8@node API Overview
9@section Overview of the Guile API
10
11Guile's application programming interface (@dfn{API}) makes
12functionality available that an application developer can use in either
13C or Scheme programming. The interface consists of @dfn{elements} that
14may be macros, functions or variables in C, and procedures, variables,
15syntax or other types of object in Scheme.
16
17Many elements are available to both Scheme and C, in a form that is
18appropriate. For example, the @code{assq} Scheme procedure is also
19available as @code{scm_assq} to C code. These elements are documented
20only once, addressing both the Scheme and C aspects of them.
21
22The Scheme name of an element is related to its C name in a regular
23way. Also, a C function takes its parameters in a systematic way.
24
25Normally, the name of a C function can be derived given its Scheme name,
26using some simple textual transformations:
27
28@itemize @bullet
29
30@item
31Replace @code{-} (hyphen) with @code{_} (underscore).
32
33@item
34Replace @code{?} (question mark) with @code{_p}.
35
36@item
37Replace @code{!} (exclamation point) with @code{_x}.
38
39@item
40Replace internal @code{->} with @code{_to_}.
41
42@item
43Replace @code{<=} (less than or equal) with @code{_leq}.
44
45@item
46Replace @code{>=} (greater than or equal) with @code{_geq}.
47
48@item
49Replace @code{<} (less than) with @code{_less}.
50
51@item
52Replace @code{>} (greater than) with @code{_gr}.
53
54@item
55Prefix with @code{scm_}.
56
57@end itemize
58
59@c Here is an Emacs Lisp command that prompts for a Scheme function name and
60@c inserts the corresponding C function name into the buffer.
61
62@c @example
63@c (defun insert-scheme-to-C (name &optional use-gh)
64@c "Transforms Scheme NAME, a string, to its C counterpart, and inserts it.
65@c Prefix arg non-nil means use \"gh_\" prefix, otherwise use \"scm_\" prefix."
66@c (interactive "sScheme name: \nP")
67@c (let ((transforms '(("-" . "_")
68@c ("?" . "_p")
69@c ("!" . "_x")
70@c ("->" . "_to_")
71@c ("<=" . "_leq")
72@c (">=" . "_geq")
73@c ("<" . "_less")
74@c (">" . "_gr")
75@c ("@@" . "at"))))
76@c (while transforms
77@c (let ((trigger (concat "\\(.*\\)"
78@c (regexp-quote (caar transforms))
79@c "\\(.*\\)"))
80@c (sub (cdar transforms))
81@c (m nil))
82@c (while (setq m (string-match trigger name))
83@c (setq name (concat (match-string 1 name)
84@c sub
85@c (match-string 2 name)))))
86@c (setq transforms (cdr transforms))))
87@c (insert (if use-gh "gh_" "scm_") name))
88@c @end example
89
90A C function always takes a fixed number of arguments of type
91@code{SCM}, even when the corresponding Scheme function takes a
92variable number.
93
94For some Scheme functions, some last arguments are optional; the
95corresponding C function must always be invoked with all optional
96arguments specified. To get the effect as if an argument has not been
97specified, pass @code{SCM_UNDEFINED} as its value. You can not do
98this for an argument in the middle; when one argument is
99@code{SCM_UNDEFINED} all the ones following it must be
100@code{SCM_UNDEFINED} as well.
101
102Some Scheme functions take an arbitrary number of @emph{rest}
103arguments; the corresponding C function must be invoked with a list of
104all these arguments. This list is always the last argument of the C
105function.
106
107These two variants can also be combined.
108
109The type of the return value of a C function that corresponds to a
110Scheme function is always @code{SCM}. In the descriptions below,
111types are therefore often omitted bot for the return value and for the
112arguments.