lisp.c 7.1 KB

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