2
0

lisp.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
  2. │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
  3. ╞══════════════════════════════════════════════════════════════════════════════╡
  4. │ Copyright 2020 Justine Alexandra Roberts Tunney │
  5. │ │
  6. │ Permission to use, copy, modify, and/or distribute this software for │
  7. │ any purpose with or without fee is hereby granted, provided that the │
  8. │ above copyright notice and this permission notice appear in all copies. │
  9. │ │
  10. │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
  11. │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
  12. │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
  13. │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
  14. │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
  15. │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
  16. │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
  17. │ PERFORMANCE OF THIS SOFTWARE. │
  18. ╚─────────────────────────────────────────────────────────────────────────────*/
  19. #include "bestline.h"
  20. #ifndef __COSMOPOLITAN__
  21. #include <ctype.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <locale.h>
  26. #include <limits.h>
  27. #endif
  28. /*───────────────────────────────────────────────────────────────────────────│─╗
  29. │ The LISP Challenge § LISP Machine ─╬─│┼
  30. ╚────────────────────────────────────────────────────────────────────────────│*/
  31. #define kT 4
  32. #define kQuote 6
  33. #define kAtom 12
  34. #define kCar 17
  35. #define kCdr 21
  36. #define kCons 25
  37. #define kEq 30
  38. #define M (RAM + sizeof(RAM) / sizeof(RAM[0]) / 2)
  39. #define S "NIL\0T\0QUOTE\0ATOM\0CAR\0CDR\0CONS\0EQ"
  40. int cx; /* stores negative memory use */
  41. int dx; /* stores lookahead character */
  42. int RAM[0100000]; /* your own ibm7090 */
  43. Intern() {
  44. int i, j, x;
  45. for (i = 0; (x = M[i++]);) {
  46. for (j = 0;; ++j) {
  47. if (x != RAM[j]) break;
  48. if (!x) return i - j - 1;
  49. x = M[i++];
  50. }
  51. while (x)
  52. x = M[i++];
  53. }
  54. j = 0;
  55. x = --i;
  56. while ((M[i++] = RAM[j++]));
  57. return x;
  58. }
  59. GetChar() {
  60. int c, t;
  61. static char *l, *p;
  62. if (l || (l = p = bestlineWithHistory("* ", "sectorlisp"))) {
  63. if (*p) {
  64. c = *p++ & 255;
  65. } else {
  66. free(l);
  67. l = p = 0;
  68. c = '\n';
  69. }
  70. t = dx;
  71. dx = c;
  72. return t;
  73. } else {
  74. PrintChar('\n');
  75. exit(0);
  76. }
  77. }
  78. PrintChar(b) {
  79. fputwc(b, stdout);
  80. }
  81. GetToken() {
  82. int c, i = 0;
  83. do if ((c = GetChar()) > ' ') RAM[i++] = c;
  84. while (c <= ' ' || (c > ')' && dx > ')'));
  85. RAM[i] = 0;
  86. return c;
  87. }
  88. AddList(x) {
  89. return Cons(x, GetList());
  90. }
  91. GetList() {
  92. int c = GetToken();
  93. if (c == ')') return 0;
  94. return AddList(GetObject(c));
  95. }
  96. GetObject(c) {
  97. if (c == '(') return GetList();
  98. return Intern();
  99. }
  100. Read() {
  101. return GetObject(GetToken());
  102. }
  103. PrintAtom(x) {
  104. int c;
  105. for (;;) {
  106. if (!(c = M[x++])) break;
  107. PrintChar(c);
  108. }
  109. }
  110. PrintList(x) {
  111. PrintChar('(');
  112. if (x) {
  113. PrintObject(Car(x));
  114. while ((x = Cdr(x))) {
  115. if (x < 0) {
  116. PrintChar(' ');
  117. PrintObject(Car(x));
  118. } else {
  119. PrintChar(L'∙');
  120. PrintObject(x);
  121. break;
  122. }
  123. }
  124. }
  125. PrintChar(')');
  126. }
  127. PrintObject(x) {
  128. if (1./x < 0) {
  129. PrintList(x);
  130. } else {
  131. PrintAtom(x);
  132. }
  133. }
  134. Print(e) {
  135. PrintObject(e);
  136. PrintChar('\n');
  137. }
  138. /*───────────────────────────────────────────────────────────────────────────│─╗
  139. │ The LISP Challenge § Bootstrap John McCarthy's Metacircular Evaluator ─╬─│┼
  140. ╚────────────────────────────────────────────────────────────────────────────│*/
  141. Car(x) {
  142. return M[x];
  143. }
  144. Cdr(x) {
  145. return M[x + 1];
  146. }
  147. Cons(car, cdr) {
  148. M[--cx] = cdr;
  149. M[--cx] = car;
  150. return cx;
  151. }
  152. Gc(x, m, k) {
  153. return x < m ? Cons(Gc(Car(x), m, k),
  154. Gc(Cdr(x), m, k)) + k : x;
  155. }
  156. Evlis(m, a) {
  157. return m ? Cons(Eval(Car(m), a),
  158. Evlis(Cdr(m), a)) : 0;
  159. }
  160. Pairlis(x, y, a) {
  161. return x ? Cons(Cons(Car(x), Car(y)),
  162. Pairlis(Cdr(x), Cdr(y), a)) : a;
  163. }
  164. Assoc(x, y) {
  165. if (x == Car(Car(y))) return Cdr(Car(y));
  166. return Assoc(x, Cdr(y));
  167. }
  168. Evcon(c, a) {
  169. if (Eval(Car(Car(c)), a)) {
  170. return Eval(Car(Cdr(Car(c))), a);
  171. } else {
  172. return Evcon(Cdr(c), a);
  173. }
  174. }
  175. Apply(f, x, a) {
  176. if (f < 0) return Evcon(Cdr(f), Pairlis(Car(f), x, a));
  177. if (f > kEq) return Apply(Eval(f, a), x, a);
  178. if (f == kEq) return Car(x) == Car(Cdr(x)) ? kT : 0;
  179. if (f == kCons) return Cons(Car(x), Car(Cdr(x)));
  180. if (f == kAtom) return Car(x) < 0 ? 0 : kT;
  181. if (f == kCar) return Car(Car(x));
  182. if (f == kCdr) return Cdr(Car(x));
  183. }
  184. Eval(e, a) {
  185. int A, B, C;
  186. if (!e) return 0;
  187. if (e > 0) return Assoc(e, a);
  188. if (Car(e) == kQuote) return Car(Cdr(e));
  189. A = cx, e = Apply(Car(e), Evlis(Cdr(e), a), a);
  190. B = cx, e = Gc(e, A, A - B);
  191. C = cx;
  192. while (C < B) M[--A] = M[--B];
  193. cx = A;
  194. return e;
  195. }
  196. /*───────────────────────────────────────────────────────────────────────────│─╗
  197. │ The LISP Challenge § User Interface ─╬─│┼
  198. ╚────────────────────────────────────────────────────────────────────────────│*/
  199. main() {
  200. int i;
  201. setlocale(LC_ALL, "");
  202. bestlineSetXlatCallback(bestlineUppercase);
  203. for(i = 0; i < sizeof(S); ++i) M[i] = S[i];
  204. for (;;) {
  205. cx = 0;
  206. Print(Eval(Read(), 0));
  207. }
  208. }