api-overview.texi 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. @node API Overview
  7. @section Overview of the Guile API
  8. Guile's application programming interface (@dfn{API}) makes
  9. functionality available that an application developer can use in either
  10. C or Scheme programming. The interface consists of @dfn{elements} that
  11. may be macros, functions or variables in C, and procedures, variables,
  12. syntax or other types of object in Scheme.
  13. Many elements are available to both Scheme and C, in a form that is
  14. appropriate. For example, the @code{assq} Scheme procedure is also
  15. available as @code{scm_assq} to C code. These elements are documented
  16. only once, addressing both the Scheme and C aspects of them.
  17. The Scheme name of an element is related to its C name in a regular
  18. way. Also, a C function takes its parameters in a systematic way.
  19. Normally, the name of a C function can be derived given its Scheme name,
  20. using some simple textual transformations:
  21. @itemize @bullet
  22. @item
  23. Replace @code{-} (hyphen) with @code{_} (underscore).
  24. @item
  25. Replace @code{?} (question mark) with @code{_p}.
  26. @item
  27. Replace @code{!} (exclamation point) with @code{_x}.
  28. @item
  29. Replace internal @code{->} with @code{_to_}.
  30. @item
  31. Replace @code{<=} (less than or equal) with @code{_leq}.
  32. @item
  33. Replace @code{>=} (greater than or equal) with @code{_geq}.
  34. @item
  35. Replace @code{<} (less than) with @code{_less}.
  36. @item
  37. Replace @code{>} (greater than) with @code{_gr}.
  38. @item
  39. Prefix with @code{scm_}.
  40. @end itemize
  41. @c Here is an Emacs Lisp command that prompts for a Scheme function name and
  42. @c inserts the corresponding C function name into the buffer.
  43. @c @example
  44. @c (defun insert-scheme-to-C (name &optional use-gh)
  45. @c "Transforms Scheme NAME, a string, to its C counterpart, and inserts it.
  46. @c Prefix arg non-nil means use \"gh_\" prefix, otherwise use \"scm_\" prefix."
  47. @c (interactive "sScheme name: \nP")
  48. @c (let ((transforms '(("-" . "_")
  49. @c ("?" . "_p")
  50. @c ("!" . "_x")
  51. @c ("->" . "_to_")
  52. @c ("<=" . "_leq")
  53. @c (">=" . "_geq")
  54. @c ("<" . "_less")
  55. @c (">" . "_gr")
  56. @c ("@@" . "at"))))
  57. @c (while transforms
  58. @c (let ((trigger (concat "\\(.*\\)"
  59. @c (regexp-quote (caar transforms))
  60. @c "\\(.*\\)"))
  61. @c (sub (cdar transforms))
  62. @c (m nil))
  63. @c (while (setq m (string-match trigger name))
  64. @c (setq name (concat (match-string 1 name)
  65. @c sub
  66. @c (match-string 2 name)))))
  67. @c (setq transforms (cdr transforms))))
  68. @c (insert (if use-gh "gh_" "scm_") name))
  69. @c @end example
  70. A C function always takes a fixed number of arguments of type
  71. @code{SCM}, even when the corresponding Scheme function takes a
  72. variable number.
  73. For some Scheme functions, some last arguments are optional; the
  74. corresponding C function must always be invoked with all optional
  75. arguments specified. To get the effect as if an argument has not been
  76. specified, pass @code{SCM_UNDEFINED} as its value. You can not do
  77. this for an argument in the middle; when one argument is
  78. @code{SCM_UNDEFINED} all the ones following it must be
  79. @code{SCM_UNDEFINED} as well.
  80. Some Scheme functions take an arbitrary number of @emph{rest}
  81. arguments; the corresponding C function must be invoked with a list of
  82. all these arguments. This list is always the last argument of the C
  83. function.
  84. These two variants can also be combined.
  85. The type of the return value of a C function that corresponds to a
  86. Scheme function is always @code{SCM}. In the descriptions below,
  87. types are therefore often omitted but for the return value and for the
  88. arguments.