Lisp Value


There are several ways to deal with errors in C, but in this context my preferred method is to make errors a possible result of evaluating an expression. Then we can say that, in Lispy, an expression will evaluate to either a number, or an error. For example + 1 2 will evaluate to a number, but / 10 0 will evaluate to an error.

For this we need a data structure that can act as either one thing or anything. For simplicity sake we are just going to use a struct with fields specific to each thing that can be represented, and a special field type to tell us exactly what fields are meaningful to access.

This we are going to call an lval, which stands for Lisp Value.

  1. /* Declare New lval Struct */
  2. typedef struct {
  3. int type;
  4. long num;
  5. int err;
  6. } lval;