Page 1 of 1

Really dinky Lisp interpreter in C

Posted: Sat Dec 02, 2017 10:19 pm
by Schol-R-LEA
Just 200 lines. It isn't a particularly useful implementation, but dang, that's tiny.

The code itself is here.

Oh, and Tilde? Yeah, it still has to collect the input into tokens. It does so in an extremely simplistic way, by collecting the substring and doing a substring match inside the evaluator, but it does:

Code: Select all

#define SYMBOL_MAX  32
static char token[SYMBOL_MAX]; /* token */
int is_space(char x)  { return x == ' ' || x == '\n'; }
int is_parens(char x) { return x == '(' || x == ')'; }

static void gettoken() {
  int index = 0;
  while(is_space(look)) { look = getchar(); }
  if (is_parens(look)) {
    token[index++] = look;  look = getchar();
  } else {
    while(index < SYMBOL_MAX && look != EOF && !is_space(look) && !is_parens(look)) {
      token[index++] = look;  look = getchar();
    }
  }
  token[index] = '\0';
} 


further down...

Code: Select all

 List * eval(List *exp, List *env) {
  if (is_atom(exp) ) {
    for ( ; env != 0; env = cdr(env) )
      if (exp == car(car(env)))  return car(cdr(car(env)));
    return 0;
  } else if (is_atom( car (exp))) { /* special forms */
    if (car(exp) == intern("quote")) {
      return car(cdr(exp));
    } else if (car(exp) == intern("if")) {
      if (eval (car(cdr(exp)), env) != 0)
        return eval (car(cdr(cdr(exp))), env);
      else
        return eval (car(cdr(cdr(cdr(exp)))), env);
    } else if (car(exp) == intern("lambda")) {
      return exp; /* todo: create a closure and capture free vars */
    } else if (car(exp) == intern("apply")) { /* apply function to list */
      List *args = evlist (cdr(cdr(exp)), env);
      args = car(args); /* assumes one argument and that it is a list */
      return apply_primitive( eval(car(cdr(exp)), env), args);
    } else { /* function call */
      List *primop = eval (car(exp), env);
      if (is_pair(primop)) { /* user defined lambda, arg list eval happens in binding  below */
        return eval( cons(primop, cdr(exp)), env );
      } else if (primop) { /* built-in primitive */
        return apply_primitive(primop, evlist(cdr(exp), env));
      }
    }
  } else if (car(car(exp)) == intern("lambda")) { /* should be a lambda, bind names into env and eval body */
    List *extenv = env, *names = car(cdr(car(exp))), *vars = cdr(exp);
    for (  ; names ; names = cdr(names), vars = cdr(vars) )
      extenv = cons (cons(car(names),  cons(eval (car(vars), env), 0)), extenv);
    return eval (car(cdr(cdr(car(exp)))), extenv);
  }
  puts("cannot evaluate expression");
  return 0;
}