2
0

lisp.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
  2. │ vi: set et 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 kCond 12
  34. #define kRead 17
  35. #define kPrint 22
  36. #define kAtom 28
  37. #define kCar 33
  38. #define kCdr 37
  39. #define kCons 41
  40. #define kEq 46
  41. #define M (RAM + sizeof(RAM) / sizeof(RAM[0]) / 2)
  42. #define S "NIL\0T\0QUOTE\0COND\0READ\0PRINT\0ATOM\0CAR\0CDR\0CONS\0EQ"
  43. int cx; /* stores negative memory use */
  44. int dx; /* stores lookahead character */
  45. int RAM[0100000]; /* your own ibm7090 */
  46. Intern() {
  47. int i, j, x;
  48. for (i = 0; (x = M[i++]);) {
  49. for (j = 0;; ++j) {
  50. if (x != RAM[j]) break;
  51. if (!x) return i - j - 1;
  52. x = M[i++];
  53. }
  54. while (x)
  55. x = M[i++];
  56. }
  57. j = 0;
  58. x = --i;
  59. while ((M[i++] = RAM[j++]));
  60. return x;
  61. }
  62. GetChar() {
  63. int c, t;
  64. static char *l, *p;
  65. if (l || (l = p = bestlineWithHistory("* ", "sectorlisp"))) {
  66. if (*p) {
  67. c = *p++ & 255;
  68. } else {
  69. free(l);
  70. l = p = 0;
  71. c = '\n';
  72. }
  73. t = dx;
  74. dx = c;
  75. return t;
  76. } else {
  77. PrintChar('\n');
  78. exit(0);
  79. }
  80. }
  81. PrintChar(b) {
  82. fputwc(b, stdout);
  83. }
  84. GetToken() {
  85. int c, i = 0;
  86. do if ((c = GetChar()) > ' ') RAM[i++] = c;
  87. while (c <= ' ' || (c > ')' && dx > ')'));
  88. RAM[i] = 0;
  89. return c;
  90. }
  91. AddList(x) {
  92. return Cons(x, GetList());
  93. }
  94. GetList() {
  95. int c = GetToken();
  96. if (c == ')') return 0;
  97. return AddList(GetObject(c));
  98. }
  99. GetObject(c) {
  100. if (c == '(') return GetList();
  101. return Intern();
  102. }
  103. Read() {
  104. return GetObject(GetToken());
  105. }
  106. PrintAtom(x) {
  107. int c;
  108. for (;;) {
  109. if (!(c = M[x++])) break;
  110. PrintChar(c);
  111. }
  112. }
  113. PrintList(x) {
  114. PrintChar('(');
  115. PrintObject(Car(x));
  116. while ((x = Cdr(x))) {
  117. if (x < 0) {
  118. PrintChar(' ');
  119. PrintObject(Car(x));
  120. } else {
  121. PrintChar(L'∙');
  122. PrintObject(x);
  123. break;
  124. }
  125. }
  126. PrintChar(')');
  127. }
  128. PrintObject(x) {
  129. if (x < 0) {
  130. PrintList(x);
  131. } else {
  132. PrintAtom(x);
  133. }
  134. }
  135. Print(e) {
  136. PrintObject(e);
  137. }
  138. PrintNewLine() {
  139. PrintChar('\n');
  140. }
  141. /*───────────────────────────────────────────────────────────────────────────│─╗
  142. │ The LISP Challenge § Bootstrap John McCarthy's Metacircular Evaluator ─╬─│┼
  143. ╚────────────────────────────────────────────────────────────────────────────│*/
  144. Car(x) {
  145. return M[x];
  146. }
  147. Cdr(x) {
  148. return M[x + 1];
  149. }
  150. Cons(car, cdr) {
  151. M[--cx] = cdr;
  152. M[--cx] = car;
  153. return cx;
  154. }
  155. Gc(x, m, k) {
  156. return x < m ? Cons(Gc(Car(x), m, k),
  157. Gc(Cdr(x), m, k)) + k : x;
  158. }
  159. Evlis(m, a) {
  160. if (m) {
  161. int x = Eval(Car(m), a);
  162. return Cons(x, Evlis(Cdr(m), a));
  163. } else {
  164. return 0;
  165. }
  166. }
  167. Pairlis(x, y, a) {
  168. return x ? Cons(Cons(Car(x), Car(y)),
  169. Pairlis(Cdr(x), Cdr(y), a)) : a;
  170. }
  171. Assoc(x, y) {
  172. if (!y) return 0;
  173. if (x == Car(Car(y))) return Cdr(Car(y));
  174. return Assoc(x, Cdr(y));
  175. }
  176. Evcon(c, a) {
  177. if (Eval(Car(Car(c)), a)) {
  178. return Eval(Car(Cdr(Car(c))), a);
  179. } else {
  180. return Evcon(Cdr(c), a);
  181. }
  182. }
  183. Apply(f, x, a) {
  184. if (f < 0) return Eval(Car(Cdr(Cdr(f))), Pairlis(Car(Cdr(f)), x, a));
  185. if (f > kEq) return Apply(Eval(f, a), x, a);
  186. if (f == kEq) return Car(x) == Car(Cdr(x)) ? kT : 0;
  187. if (f == kCons) return Cons(Car(x), Car(Cdr(x)));
  188. if (f == kAtom) return Car(x) < 0 ? 0 : kT;
  189. if (f == kCar) return Car(Car(x));
  190. if (f == kCdr) return Cdr(Car(x));
  191. if (f == kRead) return Read();
  192. if (f == kPrint) return (x ? Print(Car(x)) : PrintNewLine()), 0;
  193. }
  194. Eval(e, a) {
  195. int A, B, C;
  196. if (e >= 0)
  197. return Assoc(e, a);
  198. if (Car(e) == kQuote)
  199. return Car(Cdr(e));
  200. A = cx;
  201. if (Car(e) == kCond) {
  202. e = Evcon(Cdr(e), a);
  203. } else {
  204. e = Apply(Car(e), Evlis(Cdr(e), a), a);
  205. }
  206. B = cx;
  207. e = Gc(e, A, A - B);
  208. C = cx;
  209. while (C < B)
  210. M[--A] = M[--B];
  211. cx = A;
  212. return e;
  213. }
  214. /*───────────────────────────────────────────────────────────────────────────│─╗
  215. │ The LISP Challenge § User Interface ─╬─│┼
  216. ╚────────────────────────────────────────────────────────────────────────────│*/
  217. main() {
  218. int i;
  219. setlocale(LC_ALL, "");
  220. bestlineSetXlatCallback(bestlineUppercase);
  221. for(i = 0; i < sizeof(S); ++i) M[i] = S[i];
  222. for (;;) {
  223. cx = 0;
  224. Print(Eval(Read(), 0));
  225. PrintNewLine();
  226. }
  227. }