box.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* examples/box-module/box.c
  2. *
  3. * Copyright (C) 1998,2001, 2006 Free Software Foundation, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; see the file COPYING. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301 USA
  19. */
  20. /* Include all needed declarations. */
  21. #include <libguile.h>
  22. /* The type code for the newly created smob type will be stored into
  23. this variable. It has the prefix `scm_tc16_' to make it usable
  24. with the SCM_VALIDATE_SMOB macro below. */
  25. static scm_t_bits scm_tc16_box;
  26. /* This function is responsible for marking all SCM objects included
  27. in the smob. */
  28. static SCM
  29. mark_box (SCM b)
  30. {
  31. /* Since we have only one SCM object to protect, we simply return it
  32. and the caller will mark it. */
  33. return SCM_CELL_OBJECT_1 (b);
  34. }
  35. /* Print a textual represenation of the smob to a given port. */
  36. static int
  37. print_box (SCM b, SCM port, scm_print_state *pstate)
  38. {
  39. SCM value = SCM_CELL_OBJECT_1 (b);
  40. scm_puts ("#<box ", port);
  41. scm_write (value, port);
  42. scm_puts (">", port);
  43. /* Non-zero means success. */
  44. return 1;
  45. }
  46. /* This defines the primitve `make-box', which returns a new smob of
  47. type `box', initialized to `#f'. */
  48. static SCM
  49. #define FUNC_NAME "make-box"
  50. make_box (void)
  51. {
  52. /* This macro creates the new objects, stores the value `#f' into it
  53. and returns it to the caller. */
  54. SCM_RETURN_NEWSMOB (scm_tc16_box, SCM_BOOL_F);
  55. }
  56. #undef FUNC_NAME
  57. /* This is the primitive `box-ref' which returns the object stored in
  58. the box. */
  59. static SCM
  60. box_ref (SCM b)
  61. #define FUNC_NAME "box-ref"
  62. {
  63. /* First, we have to ensure that the user really gave us a box
  64. objects. The macro SCM_VALIDATE_SMOB will do all what is needed.
  65. The parameters are interpreted as follows:
  66. 1: The position of the checked variable in the parameter list.
  67. b: The passed parameter.
  68. box: Concatenated with the fixed prefix scm_tc16_, names the type
  69. code for the expected smob type. */
  70. SCM_VALIDATE_SMOB (1, b, box);
  71. /* Fetch the object from the box and return it. */
  72. return SCM_CELL_OBJECT_1 (b);
  73. }
  74. #undef FUNC_NAME
  75. /* Primitive which stores an arbitrary value into a box. */
  76. static SCM
  77. box_set_x (SCM b, SCM value)
  78. #define FUNC_NAME "box-set!"
  79. {
  80. SCM_VALIDATE_SMOB (1, b, box);
  81. /* Set the cell number 1 of the smob to the given value. */
  82. SCM_SET_CELL_OBJECT_1 (b, value);
  83. /* When this constant is returned, the REPL will not print the
  84. returned value. All procedures in Guile which are documented as
  85. returning `and unspecified value' actually return this value. */
  86. return SCM_UNSPECIFIED;
  87. }
  88. #undef FUNC_NAME
  89. /* Create and initialize the new smob type, and register the
  90. primitives withe the interpreter library.
  91. This function must be declared a bit different from the example in
  92. the ../box directory, because it will be called by
  93. `scm_c_define_module', called from below. */
  94. static void
  95. init_box_type (void * unused)
  96. {
  97. scm_tc16_box = scm_make_smob_type ("box", 0);
  98. scm_set_smob_mark (scm_tc16_box, mark_box);
  99. scm_set_smob_print (scm_tc16_box, print_box);
  100. scm_c_define_gsubr ("make-box", 0, 0, 0, make_box);
  101. scm_c_define_gsubr ("box-set!", 2, 0, 0, box_set_x);
  102. scm_c_define_gsubr ("box-ref", 1, 0, 0, box_ref);
  103. /* This is new too: Since the procedures are now in a module, we
  104. have to explicitly export them before they can be used. */
  105. scm_c_export ("make-box", "box-set!", "box-ref", NULL);
  106. }
  107. /* This is the function which gets called by scm_boot_guile after the
  108. Guile library is completely initialized. */
  109. static void
  110. inner_main (void *closure, int argc, char **argv)
  111. {
  112. /* Unlike the example in ../box, init_box_type is not called
  113. directly, but by scm_c_define_module, which will create a module
  114. named (box-module) and make this module current while called
  115. init_box_type, thus placing the definitions into that module. */
  116. scm_c_define_module ("box-module", init_box_type, NULL);
  117. /* ... then we start a shell, in which the box data type can be
  118. used (after using the module (box-module)). */
  119. scm_shell (argc, argv);
  120. }
  121. /* Main program. */
  122. int
  123. main (int argc, char **argv)
  124. {
  125. /* Initialize Guile, then call `inner_main' with the arguments 0,
  126. argc and argv. */
  127. scm_boot_guile (argc, argv, inner_main, 0);
  128. return 0; /* Never reached. */
  129. }
  130. /* End of file. */