api-overview.texi 3.9 KB

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