lisp.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 ConsumeObject();
  181. if (*g_token == '\'') return AddList(GetQuote());
  182. #endif
  183. if (*g_token == ')') return NIL;
  184. return AddList(GetObject());
  185. }
  186. int GetObject(void) {
  187. #if QUOTES
  188. if (*g_token == '\'') return GetQuote();
  189. #endif
  190. if (*g_token == '(') return GetList();
  191. return Intern(g_token);
  192. }
  193. int ReadObject(void) {
  194. g_look = GetChar();
  195. GetToken();
  196. return GetObject();
  197. }
  198. int Read(void) {
  199. return ReadObject();
  200. }
  201. void PrintAtom(int x) {
  202. PrintString(g_str + VALUE(x));
  203. }
  204. void PrintList(int x) {
  205. #if QUOTES
  206. if (Car(x) == ATOM_QUOTE) {
  207. PrintChar('\'');
  208. PrintObject(Car(Cdr(x)));
  209. return;
  210. }
  211. #endif
  212. PrintChar('(');
  213. PrintObject(Car(x));
  214. while ((x = Cdr(x))) {
  215. if (!ISATOM(x)) {
  216. PrintChar(' ');
  217. PrintObject(Car(x));
  218. } else {
  219. PrintString("∙");
  220. PrintObject(x);
  221. break;
  222. }
  223. }
  224. PrintChar(')');
  225. }
  226. void PrintObject(int x) {
  227. if (ISATOM(x)) {
  228. PrintAtom(x);
  229. } else {
  230. PrintList(x);
  231. }
  232. }
  233. void Print(int i) {
  234. PrintObject(i);
  235. PrintString("\n");
  236. }
  237. /*───────────────────────────────────────────────────────────────────────────│─╗
  238. │ The LISP Challenge § Bootstrap John McCarthy's Metacircular Evaluator ─╬─│┼
  239. ╚────────────────────────────────────────────────────────────────────────────│*/
  240. int Assoc(int x, int y) {
  241. if (y == NIL) return NIL;
  242. if (x == Car(Car(y))) return Cdr(Car(y));
  243. return Assoc(x, Cdr(y));
  244. }
  245. int Evcon(int c, int a) {
  246. if (Eval(Car(Car(c)), a) != NIL) {
  247. return Eval(Car(Cdr(Car(c))), a);
  248. } else {
  249. return Evcon(Cdr(c), a);
  250. }
  251. }
  252. int Pairlis(int x, int y, int a) {
  253. int di, si; /* it's zip() basically */
  254. if (x == NIL) return a;
  255. di = Cons(Car(x), Car(y));
  256. si = Pairlis(Cdr(x), Cdr(y), a);
  257. return Cons(di, si); /* Tail-Modulo-Cons */
  258. }
  259. int Evlis(int m, int a) {
  260. int di, si;
  261. if (m == NIL) return NIL;
  262. di = Eval(Car(m), a);
  263. si = Evlis(Cdr(m), a);
  264. return Cons(di, si);
  265. }
  266. int Apply(int fn, int x, int a) {
  267. int t1, si, ax;
  268. if (ISATOM(fn)) {
  269. switch (fn) {
  270. #if FUNDEF
  271. case NIL:
  272. return UNDEFINED;
  273. #endif
  274. case ATOM_CAR:
  275. return Car(Car(x));
  276. case ATOM_CDR:
  277. return Cdr(Car(x));
  278. case ATOM_ATOM:
  279. return ISATOM(Car(x)) ? ATOM_T : NIL;
  280. case ATOM_CONS:
  281. return Cons(Car(x), Car(Cdr(x)));
  282. case ATOM_EQ:
  283. return Car(x) == Car(Cdr(x)) ? ATOM_T : NIL;
  284. default:
  285. return Apply(Eval(fn, a), x, a);
  286. }
  287. }
  288. if (Car(fn) == ATOM_LAMBDA) {
  289. t1 = Cdr(fn);
  290. si = Pairlis(Car(t1), x, a);
  291. ax = Car(Cdr(t1));
  292. return Eval(ax, si);
  293. }
  294. return UNDEFINED;
  295. }
  296. int Evaluate(int e, int a) {
  297. int ax;
  298. if (ISATOM(e))
  299. return Assoc(e, a);
  300. ax = Car(e);
  301. if (ISATOM(ax)) {
  302. if (ax == ATOM_QUOTE)
  303. return Car(Cdr(e));
  304. if (ax == ATOM_COND)
  305. return Evcon(Cdr(e), a);
  306. }
  307. return Apply(ax, Evlis(Cdr(e), a), a);
  308. }
  309. int Eval(int e, int a) {
  310. int ax;
  311. #if TRACE
  312. PrintString("> ");
  313. PrintObject(e);
  314. PrintString("\r\n ");
  315. PrintObject(a);
  316. PrintString("\r\n");
  317. #endif
  318. ax = Evaluate(e, a);
  319. #if TRACE
  320. PrintString("< ");
  321. PrintObject(ax);
  322. PrintString("\r\n");
  323. #endif
  324. return ax;
  325. }
  326. /*───────────────────────────────────────────────────────────────────────────│─╗
  327. │ The LISP Challenge § User Interface ─╬─│┼
  328. ╚────────────────────────────────────────────────────────────────────────────│*/
  329. void Repl(void) {
  330. for (;;) {
  331. Print(Eval(Read(), NIL));
  332. }
  333. }
  334. int main(int argc, char *argv[]) {
  335. SetupBuiltins();
  336. bestlineSetXlatCallback(bestlineUppercase);
  337. PrintString("THE LISP CHALLENGE V1\r\n"
  338. "VISIT GITHUB.COM/JART\r\n");
  339. Repl();
  340. }