box.c 4.8 KB

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