Error Function


We can also make use of strings to add in an error reporting function. This can take as input a user supplied string and provide it as an error message for lval_err.

  1. lval* builtin_error(lenv* e, lval* a) {
  2. LASSERT_NUM("error", a, 1);
  3. LASSERT_TYPE("error", a, 0, LVAL_STR);
  4. /* Construct Error from first argument */
  5. lval* err = lval_err(a->cell[0]->str);
  6. /* Delete arguments and return */
  7. lval_del(a);
  8. return err;
  9. }

The final step is to register these as builtins. Now finally we can start building up libraries and writing them to files.

  1. /* String Functions */
  2. lenv_add_builtin(e, "load", builtin_load);
  3. lenv_add_builtin(e, "error", builtin_error);
  4. lenv_add_builtin(e, "print", builtin_print);
  1. lispy> print "Hello World!"
  2. "Hello World!"
  3. ()
  4. lispy> error "This is an error"
  5. Error: This is an error
  6. lispy> load "hello.lspy"
  7. "Hello World!"
  8. ()
  9. lispy>