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
.
lval* builtin_error(lenv* e, lval* a) {
LASSERT_NUM("error", a, 1);
LASSERT_TYPE("error", a, 0, LVAL_STR);
/* Construct Error from first argument */
lval* err = lval_err(a->cell[0]->str);
/* Delete arguments and return */
lval_del(a);
return err;
}
The final step is to register these as builtins. Now finally we can start building up libraries and writing them to files.
/* String Functions */
lenv_add_builtin(e, "load", builtin_load);
lenv_add_builtin(e, "error", builtin_error);
lenv_add_builtin(e, "print", builtin_print);
lispy> print "Hello World!"
"Hello World!"
()
lispy> error "This is an error"
Error: This is an error
lispy> load "hello.lspy"
"Hello World!"
()
lispy>
当前内容版权归 orangeduck 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 orangeduck .