123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
- │ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
- ╞══════════════════════════════════════════════════════════════════════════════╡
- │ Copyright 2020 Justine Alexandra Roberts Tunney │
- │ │
- │ Permission to use, copy, modify, and/or distribute this software for │
- │ any purpose with or without fee is hereby granted, provided that the │
- │ above copyright notice and this permission notice appear in all copies. │
- │ │
- │ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
- │ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
- │ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
- │ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
- │ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
- │ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
- │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
- │ PERFORMANCE OF THIS SOFTWARE. │
- ╚─────────────────────────────────────────────────────────────────────────────*/
- #include "bestline.h"
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <locale.h>
- #include <limits.h>
- /*───────────────────────────────────────────────────────────────────────────│─╗
- │ The LISP Challenge § LISP Machine ─╬─│┼
- ╚────────────────────────────────────────────────────────────────────────────│*/
- #define kT 4
- #define kQuote 6
- #define kCond 12
- #define kRead 17
- #define kPrint 22
- #define kAtom 28
- #define kCar 33
- #define kCdr 37
- #define kCons 41
- #define kEq 46
- #define M (RAM + sizeof(RAM) / sizeof(RAM[0]) / 2)
- #define S "NIL\0T\0QUOTE\0COND\0READ\0PRINT\0ATOM\0CAR\0CDR\0CONS\0EQ"
- int cx; /* stores negative memory use */
- int dx; /* stores lookahead character */
- int RAM[0100000]; /* your own ibm7090 */
- Intern() {
- int i, j, x;
- for (i = 0; (x = M[i++]);) {
- for (j = 0;; ++j) {
- if (x != RAM[j]) break;
- if (!x) return i - j - 1;
- x = M[i++];
- }
- while (x)
- x = M[i++];
- }
- j = 0;
- x = --i;
- while ((M[i++] = RAM[j++]));
- return x;
- }
- GetChar() {
- int c, t;
- static char *l, *p;
- if (l || (l = p = bestlineWithHistory("* ", "sectorlisp"))) {
- if (*p) {
- c = *p++ & 255;
- } else {
- free(l);
- l = p = 0;
- c = '\n';
- }
- t = dx;
- dx = c;
- return t;
- } else {
- PrintChar('\n');
- exit(0);
- }
- }
- PrintChar(b) {
- fputwc(b, stdout);
- }
- GetToken() {
- int c, i = 0;
- do if ((c = GetChar()) > ' ') RAM[i++] = c;
- while (c <= ' ' || (c > ')' && dx > ')'));
- RAM[i] = 0;
- return c;
- }
- AddList(x) {
- return Cons(x, GetList());
- }
- GetList() {
- int c = GetToken();
- if (c == ')') return 0;
- return AddList(GetObject(c));
- }
- GetObject(c) {
- if (c == '(') return GetList();
- return Intern();
- }
- Read() {
- return GetObject(GetToken());
- }
- PrintAtom(x) {
- int c;
- for (;;) {
- if (!(c = M[x++])) break;
- PrintChar(c);
- }
- }
- PrintList(x) {
- PrintChar('(');
- PrintObject(Car(x));
- while ((x = Cdr(x))) {
- if (x < 0) {
- PrintChar(' ');
- PrintObject(Car(x));
- } else {
- PrintChar(L'∙');
- PrintObject(x);
- break;
- }
- }
- PrintChar(')');
- }
- PrintObject(x) {
- if (x < 0) {
- PrintList(x);
- } else {
- PrintAtom(x);
- }
- }
- Print(e) {
- PrintObject(e);
- }
- PrintNewLine() {
- PrintChar('\n');
- }
- /*───────────────────────────────────────────────────────────────────────────│─╗
- │ The LISP Challenge § Bootstrap John McCarthy's Metacircular Evaluator ─╬─│┼
- ╚────────────────────────────────────────────────────────────────────────────│*/
- Car(x) {
- return M[x];
- }
- Cdr(x) {
- return M[x + 1];
- }
- Cons(car, cdr) {
- M[--cx] = cdr;
- M[--cx] = car;
- return cx;
- }
- Gc(x, m, k) {
- return x < m ? Cons(Gc(Car(x), m, k),
- Gc(Cdr(x), m, k)) + k : x;
- }
- Evlis(m, a) {
- if (m) {
- int x = Eval(Car(m), a);
- return Cons(x, Evlis(Cdr(m), a));
- } else {
- return 0;
- }
- }
- Pairlis(x, y, a) {
- return x ? Cons(Cons(Car(x), Car(y)),
- Pairlis(Cdr(x), Cdr(y), a)) : a;
- }
- Assoc(x, y) {
- if (!y) return 0;
- if (x == Car(Car(y))) return Cdr(Car(y));
- return Assoc(x, Cdr(y));
- }
- Evcon(c, a) {
- if (Eval(Car(Car(c)), a)) {
- return Eval(Car(Cdr(Car(c))), a);
- } else {
- return Evcon(Cdr(c), a);
- }
- }
- Apply(f, x, a) {
- if (f < 0) return Eval(Car(Cdr(Cdr(f))), Pairlis(Car(Cdr(f)), x, a));
- if (f > kEq) return Apply(Eval(f, a), x, a);
- if (f == kEq) return Car(x) == Car(Cdr(x)) ? kT : 0;
- if (f == kCons) return Cons(Car(x), Car(Cdr(x)));
- if (f == kAtom) return Car(x) < 0 ? 0 : kT;
- if (f == kCar) return Car(Car(x));
- if (f == kCdr) return Cdr(Car(x));
- if (f == kRead) return Read();
- if (f == kPrint) return (x ? Print(Car(x)) : PrintNewLine()), 0;
- }
- Eval(e, a) {
- int A, B, C;
- if (e >= 0)
- return Assoc(e, a);
- if (Car(e) == kQuote)
- return Car(Cdr(e));
- A = cx;
- if (Car(e) == kCond) {
- e = Evcon(Cdr(e), a);
- } else {
- e = Apply(Car(e), Evlis(Cdr(e), a), a);
- }
- B = cx;
- e = Gc(e, A, A - B);
- C = cx;
- while (C < B)
- M[--A] = M[--B];
- cx = A;
- return e;
- }
- /*───────────────────────────────────────────────────────────────────────────│─╗
- │ The LISP Challenge § User Interface ─╬─│┼
- ╚────────────────────────────────────────────────────────────────────────────│*/
- main() {
- int i;
- setlocale(LC_ALL, "");
- bestlineSetXlatCallback(bestlineUppercase);
- for(i = 0; i < sizeof(S); ++i) M[i] = S[i];
- for (;;) {
- cx = 0;
- Print(Eval(Read(), 0));
- PrintNewLine();
- }
- }
|