Builtin Functions


We can read in Q-Expressions but they are still useless. We need some way to manipulate them.

For this we can define some built-in operators to work on our list type. Choosing a concise set of these is important. If we implement a few fundamental operations then we can use these to define new operations without adding extra C code. There are a few ways to pick these fundamental operators but I’ve chosen a set that will allow us to do everything we need. They are defined as follows.

listTakes one or more arguments and returns a new Q-Expression containing the arguments
headTakes a Q-Expression and returns a Q-Expression with only of the first element
tailTakes a Q-Expression and returns a Q-Expression with the first element removed
joinTakes one or more Q-Expressions and returns a Q-Expression of them conjoined together
evalTakes a Q-Expression and evaluates it as if it were a S-Expression

Like with our mathematical operators we should add these functions as possible valid symbols. Afterward we can go about trying to define their behaviour in a similar way to builtin_op.

  1. mpca_lang(MPCA_LANG_DEFAULT,
  2. " \
  3. number : /-?[0-9]+/ ; \
  4. symbol : \"list\" | \"head\" | \"tail\" \
  5. | \"join\" | \"eval\" | '+' | '-' | '*' | '/' ; \
  6. sexpr : '(' <expr>* ')' ; \
  7. qexpr : '{' <expr>* '}' ; \
  8. expr : <number> | <symbol> | <sexpr> | <qexpr> ; \
  9. lispy : /^/ <expr>* /$/ ; \
  10. ",
  11. Number, Symbol, Sexpr, Qexpr, Expr, Lispy)