Print Function


If we are running programs from the command line we might want them to output some data, rather than just define functions and other values. We can add a print function to our Lisp which makes use of our existing lval_print function.

This function prints each argument separated by a space and then prints a newline character to finish. It returns the empty expression.

  1. lval* builtin_print(lenv* e, lval* a) {
  2. /* Print each argument followed by a space */
  3. for (int i = 0; i < a->count; i++) {
  4. lval_print(a->cell[i]); putchar(' ');
  5. }
  6. /* Print a newline and delete arguments */
  7. putchar('\n');
  8. lval_del(a);
  9. return lval_sexpr();
  10. }