Commit | Line | Data |
---|---|---|
a0e07ba4 | 1 | @page |
a7a7bb95 NJ |
2 | @node Guile API |
3 | @chapter Overview of the Guile API | |
4 | ||
5 | Guile's application programming interface (@dfn{API}) makes | |
6 | functionality available that an application developer can use in either | |
7 | C or Scheme programming. The interface consists of @dfn{elements} that | |
8 | may be macros, functions or variables in C, and procedures, variables, | |
9 | syntax or other types of object in Scheme. Broadly speaking, the | |
10 | interface as a whole can be divided into three groups. | |
11 | ||
12 | @enumerate | |
13 | @item | |
14 | Elements that are available equivalently as C functions or Scheme | |
15 | procedures. | |
16 | ||
17 | @item | |
18 | Elements that are only available as macros, functions or variables for C | |
19 | programming. | |
20 | ||
21 | @item | |
22 | Elements that are only available as procedures or other objects in | |
23 | Scheme. | |
24 | @end enumerate | |
25 | ||
26 | Functions/procedures in the first group are often known as | |
27 | @dfn{primitives}, @dfn{subrs} or @dfn{builtins}. An example is the | |
28 | @code{assq} Scheme procedure, which is also available as @code{scm_assq} | |
29 | in C. | |
30 | ||
31 | Elements in the second and third groups exist because they provide | |
32 | additional language-specific benefits in either Scheme or C. Examples | |
33 | are the C macro @code{SCM_CONSP}, which is faster and more convenient in | |
34 | C programming than the primitive @code{scm_pair_p}, and the | |
35 | procedure-with-setter @code{make-object-property}, which provides a | |
36 | more convenient property handling interface in Scheme than the | |
37 | primitives on which it is based. | |
38 | ||
39 | @menu | |
40 | * Primitives:: Identical function for Scheme and C. | |
41 | * C Only:: Elements only available in C. | |
42 | * Scheme Only:: Elements only available in Scheme. | |
43 | @end menu | |
44 | ||
45 | ||
46 | @node Primitives | |
47 | @section Identical Function in both Scheme and C | |
48 | ||
49 | They form the majority of the API, and allow both C and Scheme | |
50 | programmers to perform identical operations. | |
51 | ||
52 | @c @node Scheme Primitives | |
a0e07ba4 NJ |
53 | @c @chapter Writing Scheme primitives in C |
54 | @c - according to the menu in guile.texi - NJ 2001/1/26 | |
a7a7bb95 | 55 | @c @chapter Relationship between Scheme and C functions |
a0e07ba4 NJ |
56 | |
57 | @c Chapter contents contributed by Thien-Thi Nguyen <ttn@gnu.org>. | |
58 | ||
59 | Scheme procedures marked "primitive functions" have a regular interface | |
60 | when calling from C, reflected in two areas: the name of a C function, and | |
61 | the convention for passing non-required arguments to this function. | |
62 | ||
63 | @c Although the vast majority of functions support these relationships, | |
64 | @c there are some exceptions. | |
65 | ||
66 | @menu | |
67 | * Transforming Scheme name to C name:: | |
68 | * Structuring argument lists for C functions:: | |
69 | @c * Exceptions to the regularity:: | |
70 | @end menu | |
71 | ||
a7a7bb95 | 72 | |
a0e07ba4 | 73 | @node Transforming Scheme name to C name |
a7a7bb95 | 74 | @subsection Transforming Scheme name to C name |
a0e07ba4 NJ |
75 | |
76 | Normally, the name of a C function can be derived given its Scheme name, | |
77 | using some simple textual transformations: | |
78 | ||
79 | @itemize @bullet | |
80 | ||
81 | @item | |
82 | Replace @code{-} (hyphen) with @code{_} (underscore). | |
83 | ||
84 | @item | |
85 | Replace @code{?} (question mark) with "_p". | |
86 | ||
87 | @item | |
88 | Replace @code{!} (exclamation point) with "_x". | |
89 | ||
90 | @item | |
91 | Replace internal @code{->} with "_to_". | |
92 | ||
93 | @item | |
94 | Replace @code{<=} (less than or equal) with "_leq". | |
95 | ||
96 | @item | |
97 | Replace @code{>=} (greater than or equal) with "_geq". | |
98 | ||
99 | @item | |
100 | Replace @code{<} (less than) with "_less". | |
101 | ||
102 | @item | |
103 | Replace @code{>} (greater than) with "_gr". | |
104 | ||
105 | @item | |
106 | Replace @code{@@} with "at". [Omit?] | |
107 | ||
108 | @item | |
109 | Prefix with "gh_" (or "scm_" if you are ignoring the gh interface). | |
110 | ||
111 | @item | |
112 | [Anything else? --ttn, 2000/01/16 15:17:28] | |
113 | ||
114 | @end itemize | |
115 | ||
116 | Here is an Emacs Lisp command that prompts for a Scheme function name and | |
117 | inserts the corresponding C function name into the buffer. | |
118 | ||
119 | @example | |
120 | (defun insert-scheme-to-C (name &optional use-gh) | |
121 | "Transforms Scheme NAME, a string, to its C counterpart, and inserts it. | |
122 | Prefix arg non-nil means use \"gh_\" prefix, otherwise use \"scm_\" prefix." | |
123 | (interactive "sScheme name: \nP") | |
124 | (let ((transforms '(("-" . "_") | |
125 | ("?" . "_p") | |
126 | ("!" . "_x") | |
127 | ("->" . "_to_") | |
128 | ("<=" . "_leq") | |
129 | (">=" . "_geq") | |
130 | ("<" . "_less") | |
131 | (">" . "_gr") | |
6c997de2 | 132 | ("@@" . "at")))) |
a0e07ba4 NJ |
133 | (while transforms |
134 | (let ((trigger (concat "\\(.*\\)" | |
135 | (regexp-quote (caar transforms)) | |
136 | "\\(.*\\)")) | |
137 | (sub (cdar transforms)) | |
138 | (m nil)) | |
139 | (while (setq m (string-match trigger name)) | |
140 | (setq name (concat (match-string 1 name) | |
141 | sub | |
142 | (match-string 2 name))))) | |
143 | (setq transforms (cdr transforms)))) | |
144 | (insert (if use-gh "gh_" "scm_") name)) | |
145 | @end example | |
146 | ||
a7a7bb95 | 147 | |
a0e07ba4 | 148 | @node Structuring argument lists for C functions |
a7a7bb95 | 149 | @subsection Structuring argument lists for C functions |
a0e07ba4 NJ |
150 | |
151 | The C function's arguments will be all of the Scheme procedure's | |
85a9b4ed | 152 | arguments, both required and optional; if the Scheme procedure takes a |
a0e07ba4 NJ |
153 | ``rest'' argument, that will be a final argument to the C function. The |
154 | C function's arguments, as well as its return type, will be @code{SCM}. | |
155 | ||
a7a7bb95 NJ |
156 | |
157 | @node C Only | |
158 | @section Elements Available Only in C | |
159 | ||
160 | For C this is usually a matter of better performance (e.g. the | |
161 | @code{SCM_CONSP} macro) or of accepting C language types rather than the | |
162 | generic @code{SCM}. | |
163 | ||
164 | ||
165 | @node Scheme Only | |
166 | @section Elements Available Only in Scheme |