2
0

lisp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 0
  33. #define CONS 1
  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. struct Lisp {
  49. int mem[8192];
  50. unsigned char syntax[256];
  51. int look;
  52. int globals;
  53. int index;
  54. char token[128];
  55. char str[8192];
  56. };
  57. static const char kSymbols[] =
  58. "NIL\0"
  59. "T\0"
  60. "QUOTE\0"
  61. "COND\0"
  62. "ATOM\0"
  63. "CAR\0"
  64. "CDR\0"
  65. "CONS\0"
  66. "EQ\0"
  67. "LAMBDA\0"
  68. #if FUNDEF
  69. "*UNDEFINED"
  70. #endif
  71. ;
  72. static struct Lisp q[1];
  73. static void Print(int);
  74. static int GetList(void);
  75. static int GetObject(void);
  76. static void PrintObject(int);
  77. static int Eval(int, int);
  78. static void SetupSyntax(void) {
  79. q->syntax[' '] = ' ';
  80. q->syntax['\r'] = ' ';
  81. q->syntax['\n'] = ' ';
  82. q->syntax['('] = '(';
  83. q->syntax[')'] = ')';
  84. q->syntax['.'] = '.';
  85. q->syntax['\''] = '\'';
  86. }
  87. static void SetupBuiltins(void) {
  88. memmove(q->str, kSymbols, sizeof(kSymbols));
  89. }
  90. static inline int Car(int x) {
  91. return q->mem[VALUE(x) + 0];
  92. }
  93. static inline int Cdr(int x) {
  94. return q->mem[VALUE(x) + 1];
  95. }
  96. static int Set(int i, int k, int v) {
  97. q->mem[VALUE(i) + 0] = k;
  98. q->mem[VALUE(i) + 1] = v;
  99. return i;
  100. }
  101. static int Cons(int car, int cdr) {
  102. int i, cell;
  103. i = q->index;
  104. q->mem[i + 0] = car;
  105. q->mem[i + 1] = cdr;
  106. q->index = i + 2;
  107. cell = OBJECT(CONS, i);
  108. return cell;
  109. }
  110. static char *StpCpy(char *d, char *s) {
  111. char c;
  112. do {
  113. c = *s++;
  114. *d++ = c;
  115. } while (c);
  116. return d;
  117. }
  118. static int Intern(char *s) {
  119. int j, cx;
  120. char c, *z, *t;
  121. z = q->str;
  122. c = *z++;
  123. while (c) {
  124. for (j = 0;; ++j) {
  125. if (c != s[j]) {
  126. break;
  127. }
  128. if (!c) {
  129. return OBJECT(ATOM, z - q->str - j - 1);
  130. }
  131. c = *z++;
  132. }
  133. while (c) c = *z++;
  134. c = *z++;
  135. }
  136. --z;
  137. StpCpy(z, s);
  138. return OBJECT(ATOM, z - q->str);
  139. }
  140. static void PrintChar(unsigned char b) {
  141. if (write(1, &b, 1) == -1) exit(1);
  142. }
  143. static void PrintString(char *s) {
  144. char c;
  145. for (;;) {
  146. if (!(c = s[0])) break;
  147. PrintChar(c);
  148. ++s;
  149. }
  150. }
  151. static int GetChar(void) {
  152. unsigned char b;
  153. static char *l, *p;
  154. if (l || (l = p = bestlineWithHistory("* ", "sectorlisp"))) {
  155. if (*p) {
  156. b = *p++;
  157. } else {
  158. free(l);
  159. l = p = 0;
  160. b = '\n';
  161. }
  162. return b;
  163. } else {
  164. PrintChar('\n');
  165. exit(0);
  166. }
  167. }
  168. static void GetToken(void) {
  169. char *t;
  170. int b, x;
  171. b = q->look;
  172. t = q->token;
  173. for (;;) {
  174. x = q->syntax[b];
  175. if (x != ' ') break;
  176. b = GetChar();
  177. }
  178. if (x) {
  179. *t++ = b;
  180. b = GetChar();
  181. } else {
  182. while (b && !x) {
  183. *t++ = b;
  184. b = GetChar();
  185. x = q->syntax[b];
  186. }
  187. }
  188. *t++ = 0;
  189. q->look = b;
  190. }
  191. static int ConsumeObject(void) {
  192. GetToken();
  193. return GetObject();
  194. }
  195. static int Cadr(int x) {
  196. return Car(Cdr(x)); /* ((A B C D) (E F G) H I) → (E F G) */
  197. }
  198. static int List(int x, int y) {
  199. return Cons(x, Cons(y, NIL));
  200. }
  201. static int Quote(int x) {
  202. return List(ATOM_QUOTE, x);
  203. }
  204. static int GetQuote(void) {
  205. return Quote(ConsumeObject());
  206. }
  207. static int AddList(int x) {
  208. return Cons(x, GetList());
  209. }
  210. static int GetList(void) {
  211. GetToken();
  212. switch (*q->token & 0xFF) {
  213. default:
  214. return AddList(GetObject());
  215. case ')':
  216. return NIL;
  217. case '.':
  218. return ConsumeObject();
  219. #if QUOTES
  220. case '\'':
  221. return AddList(GetQuote());
  222. #endif
  223. }
  224. }
  225. static int GetObject(void) {
  226. switch (*q->token & 0xFF) {
  227. default:
  228. return Intern(q->token);
  229. case '(':
  230. return GetList();
  231. #if QUOTES
  232. case '\'':
  233. return GetQuote();
  234. #endif
  235. }
  236. }
  237. static int ReadObject(void) {
  238. q->look = GetChar();
  239. GetToken();
  240. return GetObject();
  241. }
  242. static int Read(void) {
  243. return ReadObject();
  244. }
  245. static void PrintAtom(int x) {
  246. PrintString(q->str + VALUE(x));
  247. }
  248. static void PrintList(int x) {
  249. #if QUOTES
  250. if (Car(x) == ATOM_QUOTE) {
  251. PrintChar('\'');
  252. PrintObject(Cadr(x));
  253. return;
  254. }
  255. #endif
  256. PrintChar('(');
  257. PrintObject(Car(x));
  258. while ((x = Cdr(x))) {
  259. if (!ISATOM(x)) {
  260. PrintChar(' ');
  261. PrintObject(Car(x));
  262. } else {
  263. PrintString(" . ");
  264. PrintObject(x);
  265. break;
  266. }
  267. }
  268. PrintChar(')');
  269. }
  270. static void PrintObject(int x) {
  271. if (ISATOM(x)) {
  272. PrintAtom(x);
  273. } else {
  274. PrintList(x);
  275. }
  276. }
  277. static void Print(int i) {
  278. PrintObject(i);
  279. PrintString("\r\n");
  280. }
  281. /*───────────────────────────────────────────────────────────────────────────│─╗
  282. │ The LISP Challenge § Bootstrap John McCarthy's Metacircular Evaluator ─╬─│┼
  283. ╚────────────────────────────────────────────────────────────────────────────│*/
  284. static int Caar(int x) {
  285. return Car(Car(x)); /* ((A B C D) (E F G) H I) → A */
  286. }
  287. static int Cdar(int x) {
  288. return Cdr(Car(x)); /* ((A B C D) (E F G) H I) → (B C D) */
  289. }
  290. static int Cadar(int x) {
  291. return Cadr(Car(x)); /* ((A B C D) (E F G) H I) → B */
  292. }
  293. static int Caddr(int x) {
  294. return Cadr(Cdr(x)); /* ((A B C D) (E F G) H I) → H */
  295. }
  296. static int Caddar(int x) {
  297. return Caddr(Car(x)); /* ((A B C D) (E F G) H I) → C */
  298. }
  299. static int Evcon(int c, int a) {
  300. return Eval(Caar(c), a) != NIL ? Eval(Cadar(c), a) : Evcon(Cdr(c), a);
  301. }
  302. static int Assoc(int x, int a) {
  303. return a ? Caar(a) == x ? Cdar(a) : Assoc(x, Cdr(a)) : NIL;
  304. }
  305. static int Pairlis(int x, int y, int a) { /* it's zip() basically */
  306. int di, si;
  307. if (!x) return a;
  308. di = Cons(Car(x), Car(y));
  309. si = Pairlis(Cdr(x), Cdr(y), a);
  310. return Cons(di, si); /* Tail-Modulo-Cons */
  311. }
  312. static int Evlis(int m, int a) {
  313. int di, si;
  314. if (!m) return NIL;
  315. di = Eval(Car(m), a);
  316. si = Evlis(Cdr(m), a);
  317. return Cons(di, si);
  318. }
  319. static int Apply(int fn, int x, int a) {
  320. int t1, si, ax;
  321. if (ISATOM(fn)) {
  322. switch (fn) {
  323. #if FUNDEF
  324. case NIL:
  325. return UNDEFINED;
  326. #endif
  327. case ATOM_CAR:
  328. return Caar(x);
  329. case ATOM_CDR:
  330. return Cdar(x);
  331. case ATOM_ATOM:
  332. return ISATOM(Car(x)) ? ATOM_T : NIL;
  333. case ATOM_CONS:
  334. return Cons(Car(x), Cadr(x));
  335. case ATOM_EQ:
  336. return Car(x) == Cadr(x) ? ATOM_T : NIL;
  337. default:
  338. return Apply(Eval(fn, a), x, a);
  339. }
  340. }
  341. if (Car(fn) == ATOM_LAMBDA) {
  342. t1 = Cdr(fn);
  343. si = Pairlis(Car(t1), x, a);
  344. ax = Cadr(t1);
  345. return Eval(ax, si);
  346. }
  347. return UNDEFINED;
  348. }
  349. static int Evaluate(int e, int a) {
  350. int ax;
  351. if (ISATOM(e))
  352. return Assoc(e, a);
  353. ax = Car(e);
  354. if (ISATOM(ax)) {
  355. if (ax == ATOM_QUOTE)
  356. return Cadr(e);
  357. if (ax == ATOM_COND)
  358. return Evcon(Cdr(e), a);
  359. if (ax == ATOM_LAMBDA)
  360. return e;
  361. }
  362. return Apply(ax, Evlis(Cdr(e), a), a);
  363. }
  364. static int Eval(int e, int a) {
  365. int ax;
  366. #if TRACE
  367. PrintString("> ");
  368. PrintObject(e);
  369. PrintString("\r\n ");
  370. PrintObject(a);
  371. PrintString("\r\n");
  372. #endif
  373. ax = Evaluate(e, a);
  374. #if TRACE
  375. PrintString("< ");
  376. PrintObject(ax);
  377. PrintString("\r\n");
  378. #endif
  379. return ax;
  380. }
  381. /*───────────────────────────────────────────────────────────────────────────│─╗
  382. │ The LISP Challenge § User Interface ─╬─│┼
  383. ╚────────────────────────────────────────────────────────────────────────────│*/
  384. void Repl(void) {
  385. for (;;) {
  386. Print(Eval(Read(), q->globals));
  387. }
  388. }
  389. int main(int argc, char *argv[]) {
  390. SetupSyntax();
  391. SetupBuiltins();
  392. bestlineSetXlatCallback(bestlineUppercase);
  393. PrintString("THE LISP CHALLENGE V1\r\n"
  394. "VISIT GITHUB.COM/JART\r\n");
  395. Repl();
  396. }