hack.txt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. How to hack TinyScheme
  2. ----------------------
  3. TinyScheme is easy to learn and modify. It is structured like a
  4. meta-interpreter, only it is written in C. All data are Scheme
  5. objects, which facilitates both understanding/modifying the
  6. code and reifying the interpreter workings.
  7. In place of a dry description, we will pace through the addition
  8. of a useful new datatype: garbage-collected memory blocks.
  9. The interface will be:
  10. (make-block <n> [<fill>]) makes a new block of the specified size
  11. optionally filling it with a specified byte
  12. (block? <obj>)
  13. (block-length <block>)
  14. (block-ref <block> <index>) retrieves byte at location
  15. (block-set! <block> <index> <byte>) modifies byte at location
  16. In the sequel, lines that begin with '>' denote lines to add to the
  17. code. Lines that begin with '|' are just citations of existing code.
  18. First of all, we need to assign a typeid to our new type. Typeids
  19. in TinyScheme are small integers declared in an enum, very close to
  20. the top; it begins with T_STRING. Add a new one at the end, say
  21. T_MEMBLOCK. There can be at most 31 types, but you don't have to
  22. worry about that limit yet.
  23. | ...
  24. | T_PORT,
  25. | T_VECTOR, /* remember to add a comma to the preceding item! */
  26. | T_MEMBLOCK
  27. } };
  28. Then, some helper macros would be useful. Go to where isstring() and
  29. the rest are defined and define:
  30. > int ismemblock(pointer p) { return (type(p)==T_MEMBLOCK); }
  31. This actually is a function, because it is meant to be exported by
  32. scheme.h. If no foreign function will ever manipulate a memory block,
  33. you can instead define it as a macro
  34. > #define ismemblock(p) (type(p)==T_MEMBLOCK)
  35. Then we make space for the new type in the main data structure:
  36. struct cell. As it happens, the _string part of the union _object
  37. (that is used to hold character strings) has two fields that suit us:
  38. | struct {
  39. | char *_svalue;
  40. | int _keynum;
  41. | } _string;
  42. We can use _svalue to hold the actual pointer and _keynum to hold its
  43. length. If we couln't reuse existing fields, we could always add other
  44. alternatives in union _object.
  45. We then procede to write the function that actually makes a new block.
  46. For conformance reasons, we name it mk_memblock
  47. > static pointer mk_memblock(scheme *sc, int len, char fill) {
  48. > pointer x;
  49. > char *p=(char*)sc->malloc(len);
  50. >
  51. > if(p==0) {
  52. > return sc->NIL;
  53. > }
  54. > x = get_cell(sc, sc->NIL, sc->NIL);
  55. >
  56. > typeflag(x) = T_MEMBLOCK|T_ATOM;
  57. > strvalue(x)=p;
  58. > keynum(x)=len;
  59. > memset(p,fill,len);
  60. > return (x);
  61. > }
  62. The memory used by the MEMBLOCK will have to be freed when the cell
  63. is reclaimed during garbage collection. There is a placeholder for
  64. that staff, function finalize_cell(), currently handling strings only.
  65. | static void finalize_cell(scheme *sc, pointer a) {
  66. | if(isstring(a)) {
  67. | sc->free(strvalue(a));
  68. | }
  69. > else if(ismemblock(a)) {
  70. > sc->free(strvalue(x));
  71. > }
  72. | }
  73. There are no MEMBLOCK literals, so we don't concern ourselfs with
  74. the READER part (yet!). We must cater to the PRINTER, though. We
  75. add one case more in printatom().
  76. | } else if (iscontinuation(l)) {
  77. | p = "#<CONTINUATION>";
  78. > } else if (ismemblock(l)) {
  79. > p = "#<MEMORY BLOCK>";
  80. | }
  81. Whenever a MEMBLOCK is displayed, it will look like that.
  82. Now, we must add the interface functions: constructor, predicate,
  83. accessor, modifier. We must in fact create new op-codes for the virtual
  84. machine underlying TinyScheme. There is a huge enum with OP_XXX values.
  85. That's where the op-codes are declared. For reasons of cohesion, we add
  86. the new op-codes right after those for vectors:
  87. | OP_VECSET,
  88. > OP_MKBLOCK,
  89. > OP_MEMBLOCKP,
  90. > OP_BLOCKLEN,
  91. > OP_BLOCKREF,
  92. > OP_BLOCKSET,
  93. | OP_NOT,
  94. We add the predicate along the other predicates:
  95. | OP_VECTORP,
  96. > OP_BLOCKP,
  97. | OP_EQ,
  98. Op-codes are really just tags for a huge C switch, only this switch
  99. is broke up in a number of different opexe_X functions. The
  100. correspondence is made in table "dispatch_table". There, we assign
  101. the new op-codes to opexe_2, where the equivalent ones for vectors
  102. are situated. We also assign a name for them, and specify the minimum
  103. and maximum arity. INF_ARG as a maximum arity means "unlimited".
  104. | {opexe_2, "vector-set!", 3, 3}, /* OP_VECSET */
  105. > {opexe_2, "make-block", 1, 2}, /* OP_MKBLOCK */
  106. > {opexe_2, "block-length", 1, 1}, /* OP_BLOCKLEN */
  107. > {opexe_2, "block-ref", 2, 2}, /* OP_BLOCKREF */
  108. > {opexe_2, "block-set!",3 ,3}, /* OP_BLOCKSET */
  109. The predicate goes with the other predicates, in opexe_3.
  110. | {opexe_3, "vector?", 1, 1}, /* OP_VECTORP, */
  111. > {opexe_3, "block?", 1, 1}, /* OP_BLOCKP, */
  112. All that remains is to write the actual processing in opexe_2, right
  113. after OP_VECSET.
  114. > case OP_MKBLOCK: { /* make-block */
  115. > int fill=0;
  116. > int len;
  117. >
  118. > if(!isnumber(car(sc->args))) {
  119. > Error_1(sc,"make-block: not a number:",car(sc->args));
  120. > }
  121. > len=ivalue(car(sc->args));
  122. > if(len<=0) {
  123. > Error_1(sc,"make-block: not positive:",car(sc->args));
  124. > }
  125. >
  126. > if(cdr(sc->args)!=sc->NIL) {
  127. > if(!isnumber(cadr(sc->args)) || ivalue(cadr(sc->args))<0) {
  128. > Error_1(sc,"make-block: not a positive number:",cadr(sc->args));
  129. > }
  130. > fill=charvalue(cadr(sc->args))%255;
  131. > }
  132. > s_return(sc,mk_memblock(sc,len,(char)fill));
  133. > }
  134. >
  135. > case OP_BLOCKLEN: /* block-length */
  136. > if(!ismemblock(car(sc->args))) {
  137. > Error_1(sc,"block-length: not a memory block:",car(sc->args));
  138. > }
  139. > s_return(sc,mk_integer(sc,keynum(car(sc->args))));
  140. >
  141. > case OP_BLOCKREF: { /* block-ref */
  142. > char *str;
  143. > int index;
  144. >
  145. > if(!ismemblock(car(sc->args))) {
  146. > Error_1(sc,"block-ref: not a memory block:",car(sc->args));
  147. > }
  148. > str=strvalue(car(sc->args));
  149. >
  150. > if(cdr(sc->args)==sc->NIL) {
  151. > Error_0(sc,"block-ref: needs two arguments");
  152. > }
  153. > if(!isnumber(cadr(sc->args))) {
  154. > Error_1(sc,"block-ref: not a number:",cadr(sc->args));
  155. > }
  156. > index=ivalue(cadr(sc->args));
  157. >
  158. > if(index<0 || index>=keynum(car(sc->args))) {
  159. > Error_1(sc,"block-ref: out of bounds:",cadr(sc->args));
  160. > }
  161. >
  162. > s_return(sc,mk_integer(sc,str[index]));
  163. > }
  164. >
  165. > case OP_BLOCKSET: { /* block-set! */
  166. > char *str;
  167. > int index;
  168. > int c;
  169. >
  170. > if(!ismemblock(car(sc->args))) {
  171. > Error_1(sc,"block-set!: not a memory block:",car(sc->args));
  172. > }
  173. > if(isimmutable(car(sc->args))) {
  174. > Error_1(sc,"block-set!: unable to alter immutable memory block:",car(sc->args));
  175. > }
  176. > str=strvalue(car(sc->args));
  177. >
  178. > if(cdr(sc->args)==sc->NIL) {
  179. > Error_0(sc,"block-set!: needs three arguments");
  180. > }
  181. > if(!isnumber(cadr(sc->args))) {
  182. > Error_1(sc,"block-set!: not a number:",cadr(sc->args));
  183. > }
  184. > index=ivalue(cadr(sc->args));
  185. > if(index<0 || index>=keynum(car(sc->args))) {
  186. > Error_1(sc,"block-set!: out of bounds:",cadr(sc->args));
  187. > }
  188. >
  189. > if(cddr(sc->args)==sc->NIL) {
  190. > Error_0(sc,"block-set!: needs three arguments");
  191. > }
  192. > if(!isinteger(caddr(sc->args))) {
  193. > Error_1(sc,"block-set!: not an integer:",caddr(sc->args));
  194. > }
  195. > c=ivalue(caddr(sc->args))%255;
  196. >
  197. > str[index]=(char)c;
  198. > s_return(sc,car(sc->args));
  199. > }
  200. Same for the predicate in opexe_3.
  201. | case OP_VECTORP: /* vector? */
  202. | s_retbool(isvector(car(sc->args)));
  203. > case OP_BLOCKP: /* block? */
  204. > s_retbool(ismemblock(car(sc->args)));