Added commands for printing to console and error generation.

Errors now print to stderr
This commit is contained in:
2014-06-21 23:06:18 +01:00
parent f2c8dc167c
commit 437e1d21da
5 changed files with 31 additions and 11 deletions

View File

@@ -43,6 +43,7 @@ void lenv_add_builtin_funcs(lenv* env) {
lenv_add_builtin(env, "head", builtin_head);
lenv_add_builtin(env, "tail", builtin_tail);
lenv_add_builtin(env, "if", builtin_if);
lenv_add_builtin(env, "print", builtin_print);
//ENV Functions
lenv_add_builtin(env, "def", builtin_def);
@@ -53,6 +54,7 @@ void lenv_add_builtin_funcs(lenv* env) {
lenv_add_builtin(env, "\\", builtin_lambda);
lenv_add_builtin(env, "load", builtin_load);
lenv_add_builtin(env, "loadonce", builtin_loadonce);
lenv_add_builtin(env, "error", builtin_error);
}
char* builtin_op_strname(BUILTIN_OP_TYPE op) {
@@ -360,6 +362,14 @@ lval* builtin_if(lenv* env, lval* val) {
return result;
}
lval* builtin_print(lenv* env, lval* val) {
for(int i = 0; i < val->cell_count; i++) {
lval_print(val->cell_list[i]);
putchar(' ');
}
putchar('\n');
return lval_ok();
}
//End List/Util functions
//Start ENV Functions
@@ -512,4 +522,12 @@ lval* builtin_do_load(lenv* env, lval* val, BOOL loadonce) {
return err;
}
}
lval* builtin_error(lenv* env, lval* val) {
LASSERT_ARG_COUNT("error", val, val, 1);
LASSERT_TYPE("error", val, val->cell_list[0], LVAL_STR);
lval* errorVal = lval_err_detail(LERR_USER, "%s", val->cell_list[0]->data.str);
lval_delete(val);
return errorVal;
}
//End ENV Functions

View File

@@ -58,6 +58,7 @@ extern "C" {
lval* builtin_head(lenv* env, lval* val);
lval* builtin_tail(lenv* env, lval* val);
lval* builtin_if(lenv* env, lval* val);
lval* builtin_print(lenv* env, lval* val);
//ENV Functions
lval* builtin_envdef(lenv* env, lval* val, char* type);
@@ -69,6 +70,7 @@ extern "C" {
lval* builtin_load(lenv* env, lval* val);
lval* builtin_loadonce(lenv* env, lval* val);
lval* builtin_do_load(lenv* env, lval* val, BOOL loadonce);
lval* builtin_error(lenv* env, lval* val);
#ifdef __cplusplus
}

2
lang.c
View File

@@ -10,7 +10,7 @@ mpc_ast_t* tokenize(char *input) {
if (!mpc_parse("<stdin>", input,gLispy, &result)) {
success = 0;
mpc_err_print(result.error);
mpc_err_print_to(result.error, stderr);
mpc_err_delete(result.error);
}

2
lval.h
View File

@@ -26,7 +26,7 @@ typedef struct lval_func lval_func;
#define LVAL_IS_FALSE(val) (val->type == LVAL_NUM && fabs(val->data.num) <= DBL_EPSILON)
enum VAL_TYPE { LVAL_ERR, LVAL_NUM, LVAL_SYM, LVAL_FUNC, LVAL_S_EXPR, LVAL_Q_EXPR, LVAL_EXIT, LVAL_STR, LVAL_OK };
enum VAL_ERROR { LERR_DIV_ZERO, LERR_BAD_OP, LERR_BAD_NUM, LERR_BAD_SYM, LERR_OTHER, LERR_SYNTAX };
enum VAL_ERROR { LERR_DIV_ZERO, LERR_BAD_OP, LERR_BAD_NUM, LERR_BAD_SYM, LERR_OTHER, LERR_SYNTAX, LERR_USER };
typedef enum VAL_TYPE VAL_TYPE;
typedef enum VAL_ERROR VAL_ERROR;

18
main.c
View File

@@ -128,18 +128,18 @@ void lval_print(lval* val) {
}
break;
case LVAL_ERR:
printf("Error: ");
switch(val->data.err.num) {
case LERR_DIV_ZERO: printf("Divide By Zero"); break;
case LERR_BAD_NUM: printf("Bad Number"); break;
case LERR_BAD_OP: printf("Invalid Operator"); break;
case LERR_BAD_SYM: printf("Unknown/Invalid Symbol"); break;
case LERR_OTHER: printf("Unknown Error"); break;
case LERR_SYNTAX: printf("Syntax Error"); break;
default: printf("Unknown Error"); break;
case LERR_DIV_ZERO: fprintf(stderr, "Divide By Zero"); break;
case LERR_BAD_NUM: fprintf(stderr,"Bad Number"); break;
case LERR_BAD_OP: fprintf(stderr,"Invalid Operator"); break;
case LERR_BAD_SYM: fprintf(stderr,"Unknown/Invalid Symbol"); break;
case LERR_OTHER: fprintf(stderr,"Unknown/Other Error"); break;
case LERR_SYNTAX: fprintf(stderr,"Syntax Error"); break;
case LERR_USER: fprintf(stderr,"Runtime Error"); break;
default: fprintf(stderr,"Unknown Error"); break;
}
if (val->data.err.detail != NULL) {
printf(", %s", val->data.err.detail);
fprintf(stderr,": %s", val->data.err.detail);
}
break;
}