lisp.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #include <ctype.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <locale.h>
  25. #include <limits.h>
  26. /*───────────────────────────────────────────────────────────────────────────│─╗
  27. │ The LISP Challenge § LISP Machine ─╬─│┼
  28. ╚────────────────────────────────────────────────────────────────────────────│*/
  29. #define kT 4
  30. #define kQuote 6
  31. #define kCond 12
  32. #define kRead 17
  33. #define kPrint 22
  34. #define kAtom 28
  35. #define kCar 33
  36. #define kCdr 37
  37. #define kCons 41
  38. #define kEq 46
  39. #define M (RAM + sizeof(RAM) / sizeof(RAM[0]) / 2)
  40. #define S "NIL\0T\0QUOTE\0COND\0READ\0PRINT\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. Read() {
  102. return GetObject(GetToken());
  103. }
  104. PrintAtom(x) {
  105. int c;
  106. for (;;) {
  107. if (!(c = M[x++])) break;
  108. PrintChar(c);
  109. }
  110. }
  111. PrintList(x) {
  112. PrintChar('(');
  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. PrintChar(')');
  125. }
  126. PrintObject(x) {
  127. if (x < 0) {
  128. PrintList(x);
  129. } else {
  130. PrintAtom(x);
  131. }
  132. }
  133. Print(e) {
  134. PrintObject(e);
  135. }
  136. PrintNewLine() {
  137. PrintChar('\n');
  138. }
  139. /*───────────────────────────────────────────────────────────────────────────│─╗
  140. │ The LISP Challenge § Bootstrap John McCarthy's Metacircular Evaluator ─╬─│┼
  141. ╚────────────────────────────────────────────────────────────────────────────│*/
  142. Car(x) {
  143. return M[x];
  144. }
  145. Cdr(x) {
  146. return M[x + 1];
  147. }
  148. Cons(car, cdr) {
  149. M[--cx] = cdr;
  150. M[--cx] = car;
  151. return cx;
  152. }
  153. Gc(x, m, k) {
  154. return x < m ? Cons(Gc(Car(x), m, k),
  155. Gc(Cdr(x), m, k)) + k : x;
  156. }
  157. Evlis(m, a) {
  158. if (m) {
  159. int x = Eval(Car(m), a);
  160. return Cons(x, Evlis(Cdr(m), a));
  161. } else {
  162. return 0;
  163. }
  164. }
  165. Pairlis(x, y, a) {
  166. return x ? Cons(Cons(Car(x), Car(y)),
  167. Pairlis(Cdr(x), Cdr(y), a)) : a;
  168. }
  169. Assoc(x, y) {
  170. if (!y) return 0;
  171. if (x == Car(Car(y))) return Cdr(Car(y));
  172. return Assoc(x, Cdr(y));
  173. }
  174. Evcon(c, a) {
  175. if (Eval(Car(Car(c)), a)) {
  176. return Eval(Car(Cdr(Car(c))), a);
  177. } else {
  178. return Evcon(Cdr(c), a);
  179. }
  180. }
  181. Apply(f, x, a) {
  182. if (f < 0) return Eval(Car(Cdr(Cdr(f))), Pairlis(Car(Cdr(f)), x, a));
  183. if (f > kEq) return Apply(Eval(f, a), x, a);
  184. if (f == kEq) return Car(x) == Car(Cdr(x)) ? kT : 0;
  185. if (f == kCons) return Cons(Car(x), Car(Cdr(x)));
  186. if (f == kAtom) return Car(x) < 0 ? 0 : kT;
  187. if (f == kCar) return Car(Car(x));
  188. if (f == kCdr) return Cdr(Car(x));
  189. if (f == kRead) return Read();
  190. if (f == kPrint) return (x ? Print(Car(x)) : PrintNewLine()), 0;
  191. }
  192. Eval(e, a) {
  193. int A, B, C;
  194. if (e >= 0)
  195. return Assoc(e, a);
  196. if (Car(e) == kQuote)
  197. return Car(Cdr(e));
  198. A = cx;
  199. if (Car(e) == kCond) {
  200. e = Evcon(Cdr(e), a);
  201. } else {
  202. e = Apply(Car(e), Evlis(Cdr(e), a), a);
  203. }
  204. B = cx;
  205. e = Gc(e, A, A - B);
  206. C = cx;
  207. while (C < B)
  208. M[--A] = M[--B];
  209. cx = A;
  210. return e;
  211. }
  212. /*───────────────────────────────────────────────────────────────────────────│─╗
  213. │ The LISP Challenge § User Interface ─╬─│┼
  214. ╚────────────────────────────────────────────────────────────────────────────│*/
  215. main() {
  216. int i;
  217. setlocale(LC_ALL, "");
  218. bestlineSetXlatCallback(bestlineUppercase);
  219. for(i = 0; i < sizeof(S); ++i) M[i] = S[i];
  220. for (;;) {
  221. cx = 0;
  222. Print(Eval(Read(), 0));
  223. PrintNewLine();
  224. }
  225. }