lisp.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #endif
  26. #define QUOTES 1 /* allow 'X shorthand for (QUOTE X) */
  27. #define FUNDEF 1 /* be friendly w/undefined behavior */
  28. #define TRACE 0 /* prints Eval() arguments / result */
  29. /*───────────────────────────────────────────────────────────────────────────│─╗
  30. │ The LISP Challenge § LISP Machine ─╬─│┼
  31. ╚────────────────────────────────────────────────────────────────────────────│*/
  32. #define ATOM 1
  33. #define CONS 0
  34. #define ISATOM(x) ((x)&1)
  35. #define VALUE(x) ((x)>>1)
  36. #define OBJECT(t,v) ((v)<<1|(t))
  37. #define NIL OBJECT(ATOM,0)
  38. #define ATOM_T OBJECT(ATOM,4)
  39. #define ATOM_QUOTE OBJECT(ATOM,6)
  40. #define ATOM_COND OBJECT(ATOM,12)
  41. #define ATOM_ATOM OBJECT(ATOM,17)
  42. #define ATOM_CAR OBJECT(ATOM,22)
  43. #define ATOM_CDR OBJECT(ATOM,26)
  44. #define ATOM_CONS OBJECT(ATOM,30)
  45. #define ATOM_EQ OBJECT(ATOM,35)
  46. #define ATOM_LAMBDA OBJECT(ATOM,38)
  47. #define UNDEFINED OBJECT(ATOM,45)
  48. const char kSymbols[] =
  49. "NIL\0"
  50. "T\0"
  51. "QUOTE\0"
  52. "COND\0"
  53. "ATOM\0"
  54. "CAR\0"
  55. "CDR\0"
  56. "CONS\0"
  57. "EQ\0"
  58. "LAMBDA\0"
  59. #if FUNDEF
  60. "*UNDEFINED"
  61. #endif
  62. ;
  63. int g_look;
  64. int g_index;
  65. char g_token[128];
  66. int g_mem[8192];
  67. char g_str[8192];
  68. int GetList(void);
  69. int GetObject(void);
  70. void PrintObject(int);
  71. int Eval(int, int);
  72. void SetupBuiltins(void) {
  73. memmove(g_str, kSymbols, sizeof(kSymbols));
  74. }
  75. int Car(int x) {
  76. return g_mem[VALUE(x) + 0];
  77. }
  78. int Cdr(int x) {
  79. return g_mem[VALUE(x) + 1];
  80. }
  81. int Cons(int car, int cdr) {
  82. int i, cell;
  83. i = g_index;
  84. g_mem[i + 0] = car;
  85. g_mem[i + 1] = cdr;
  86. g_index = i + 2;
  87. cell = OBJECT(CONS, i);
  88. return cell;
  89. }
  90. char *StpCpy(char *d, char *s) {
  91. char c;
  92. do {
  93. c = *s++;
  94. *d++ = c;
  95. } while (c);
  96. return d;
  97. }
  98. int Intern(char *s) {
  99. int j, cx;
  100. char c, *z, *t;
  101. z = g_str;
  102. c = *z++;
  103. while (c) {
  104. for (j = 0;; ++j) {
  105. if (c != s[j]) {
  106. break;
  107. }
  108. if (!c) {
  109. return OBJECT(ATOM, z - g_str - j - 1);
  110. }
  111. c = *z++;
  112. }
  113. while (c) c = *z++;
  114. c = *z++;
  115. }
  116. --z;
  117. StpCpy(z, s);
  118. return OBJECT(ATOM, z - g_str);
  119. }
  120. void PrintChar(unsigned char b) {
  121. if (write(1, &b, 1) == -1) exit(1);
  122. }
  123. void PrintString(const char *s) {
  124. char c;
  125. for (;;) {
  126. if (!(c = s[0])) break;
  127. PrintChar(c);
  128. ++s;
  129. }
  130. }
  131. int GetChar(void) {
  132. int b;
  133. static char *l, *p;
  134. if (l || (l = p = bestlineWithHistory("* ", "sectorlisp"))) {
  135. if (*p) {
  136. b = *p++ & 255;
  137. } else {
  138. free(l);
  139. l = p = 0;
  140. b = '\n';
  141. }
  142. return b;
  143. } else {
  144. PrintString("\n");
  145. exit(0);
  146. }
  147. }
  148. void GetToken(void) {
  149. int al;
  150. char *di;
  151. di = g_token;
  152. do {
  153. if (g_look > ' ') {
  154. *di++ = g_look;
  155. }
  156. al = g_look;
  157. g_look = GetChar();
  158. } while (al <= ' ' || (al > ')' && g_look > ')'));
  159. *di++ = 0;
  160. }
  161. int ConsumeObject(void) {
  162. GetToken();
  163. return GetObject();
  164. }
  165. int List(int x, int y) {
  166. return Cons(x, Cons(y, NIL));
  167. }
  168. int Quote(int x) {
  169. return List(ATOM_QUOTE, x);
  170. }
  171. int GetQuote(void) {
  172. return Quote(ConsumeObject());
  173. }
  174. int AddList(int x) {
  175. return Cons(x, GetList());
  176. }
  177. int GetList(void) {
  178. GetToken();
  179. #if QUOTES
  180. if (*g_token == '\'') return AddList(GetQuote());
  181. #endif
  182. if (*g_token == ')') return NIL;
  183. return AddList(GetObject());
  184. }
  185. int GetObject(void) {
  186. #if QUOTES
  187. if (*g_token == '\'') return GetQuote();
  188. #endif
  189. if (*g_token == '(') return GetList();
  190. return Intern(g_token);
  191. }
  192. int ReadObject(void) {
  193. g_look = GetChar();
  194. GetToken();
  195. return GetObject();
  196. }
  197. int Read(void) {
  198. return ReadObject();
  199. }
  200. void PrintAtom(int x) {
  201. PrintString(g_str + VALUE(x));
  202. }
  203. void PrintList(int x) {
  204. #if QUOTES
  205. if (Car(x) == ATOM_QUOTE) {
  206. PrintChar('\'');
  207. PrintObject(Car(Cdr(x)));
  208. return;
  209. }
  210. #endif
  211. PrintChar('(');
  212. PrintObject(Car(x));
  213. while ((x = Cdr(x)) != NIL) {
  214. if (!ISATOM(x)) {
  215. PrintChar(' ');
  216. PrintObject(Car(x));
  217. } else {
  218. PrintString("∙");
  219. PrintObject(x);
  220. break;
  221. }
  222. }
  223. PrintChar(')');
  224. }
  225. void PrintObject(int x) {
  226. if (ISATOM(x)) {
  227. PrintAtom(x);
  228. } else {
  229. PrintList(x);
  230. }
  231. }
  232. void Print(int i) {
  233. PrintObject(i);
  234. PrintString("\n");
  235. }
  236. /*───────────────────────────────────────────────────────────────────────────│─╗
  237. │ The LISP Challenge § Bootstrap John McCarthy's Metacircular Evaluator ─╬─│┼
  238. ╚────────────────────────────────────────────────────────────────────────────│*/
  239. int Assoc(int x, int y) {
  240. if (y == NIL) return NIL;
  241. if (x == Car(Car(y))) return Cdr(Car(y));
  242. return Assoc(x, Cdr(y));
  243. }
  244. int Evcon(int c, int a) {
  245. if (Eval(Car(Car(c)), a) != NIL) {
  246. return Eval(Car(Cdr(Car(c))), a);
  247. } else {
  248. return Evcon(Cdr(c), a);
  249. }
  250. }
  251. int Pairlis(int x, int y, int a) {
  252. int di, si; /* it's zip() basically */
  253. if (x == NIL) return a;
  254. di = Cons(Car(x), Car(y));
  255. si = Pairlis(Cdr(x), Cdr(y), a);
  256. return Cons(di, si); /* Tail-Modulo-Cons */
  257. }
  258. int Evlis(int m, int a) {
  259. int di, si;
  260. if (m == NIL) return NIL;
  261. di = Eval(Car(m), a);
  262. si = Evlis(Cdr(m), a);
  263. return Cons(di, si);
  264. }
  265. int Apply(int fn, int x, int a) {
  266. int t1, si, ax;
  267. if (ISATOM(fn)) {
  268. switch (fn) {
  269. #if FUNDEF
  270. case NIL:
  271. return UNDEFINED;
  272. #endif
  273. case ATOM_CAR:
  274. return Car(Car(x));
  275. case ATOM_CDR:
  276. return Cdr(Car(x));
  277. case ATOM_ATOM:
  278. return ISATOM(Car(x)) ? ATOM_T : NIL;
  279. case ATOM_CONS:
  280. return Cons(Car(x), Car(Cdr(x)));
  281. case ATOM_EQ:
  282. return Car(x) == Car(Cdr(x)) ? ATOM_T : NIL;
  283. default:
  284. return Apply(Eval(fn, a), x, a);
  285. }
  286. }
  287. if (Car(fn) == ATOM_LAMBDA) {
  288. t1 = Cdr(fn);
  289. si = Pairlis(Car(t1), x, a);
  290. ax = Car(Cdr(t1));
  291. return Eval(ax, si);
  292. }
  293. return UNDEFINED;
  294. }
  295. int Evaluate(int e, int a) {
  296. int ax;
  297. if (ISATOM(e))
  298. return Assoc(e, a);
  299. ax = Car(e);
  300. if (ISATOM(ax)) {
  301. if (ax == ATOM_QUOTE)
  302. return Car(Cdr(e));
  303. if (ax == ATOM_COND)
  304. return Evcon(Cdr(e), a);
  305. }
  306. return Apply(ax, Evlis(Cdr(e), a), a);
  307. }
  308. int Eval(int e, int a) {
  309. int ax;
  310. #if TRACE
  311. PrintString("> ");
  312. PrintObject(e);
  313. PrintString("\r\n ");
  314. PrintObject(a);
  315. PrintString("\r\n");
  316. #endif
  317. ax = Evaluate(e, a);
  318. #if TRACE
  319. PrintString("< ");
  320. PrintObject(ax);
  321. PrintString("\r\n");
  322. #endif
  323. return ax;
  324. }
  325. /*───────────────────────────────────────────────────────────────────────────│─╗
  326. │ The LISP Challenge § User Interface ─╬─│┼
  327. ╚────────────────────────────────────────────────────────────────────────────│*/
  328. void Repl(void) {
  329. for (;;) {
  330. Print(Eval(Read(), NIL));
  331. }
  332. }
  333. int main(int argc, char *argv[]) {
  334. SetupBuiltins();
  335. bestlineSetXlatCallback(bestlineUppercase);
  336. PrintString("THE LISP CHALLENGE V1\r\n"
  337. "VISIT GITHUB.COM/JART\r\n");
  338. Repl();
  339. }