Add error types and simple state

This commit is contained in:
2022-10-28 16:08:40 -04:00
parent fd91e66b8a
commit ed173bd17a
14 changed files with 302 additions and 89 deletions

View File

@@ -22,12 +22,22 @@ LISP_FUNC_1(nl_builtin_type, arg) {
return nl_symbol_create(strdup("int"));
case NL_TYPE_STRING:
return nl_symbol_create(strdup("string"));
case NL_TYPE_ERROR:
return nl_symbol_create(strdup("error"));
case NL_TYPE_COUNT:
assert(0);
return NULL;
}
}
LISP_FUNC_1(nl_builtin_error, arg) {
if (arg->type != NL_TYPE_STRING) {
return nl_error_create("Expected type string passed to 'error'");
}
return nl_error_create(strdup(arg->error));
}
LISP_FUNC_1(nl_builtin_symbol_p, arg) {
return nl_predicate(arg->type == NL_TYPE_SYMBOL);
}
@@ -44,6 +54,10 @@ LISP_FUNC_1(nl_builtin_list_p, arg) {
return nl_predicate(arg->type == NL_TYPE_CELL);
}
LISP_FUNC_1(nl_builtin_error_p, arg) {
return nl_predicate(arg->type == NL_TYPE_ERROR);
}
LISP_FUNC_1(nl_builtin_car, arg) {
return nl_car(arg);
}
@@ -53,78 +67,72 @@ LISP_FUNC_1(nl_builtin_cdr, arg) {
}
LISP_FUNC_2(nl_builtin_add, arg0, arg1) {
// TODO: Return an error type
if (arg0->type != arg1->type) {
return NULL;
if (arg0->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'add'");
}
if (arg0->type != NL_TYPE_INT) {
return NULL;
if (arg1->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'add'");
}
return nl_int_create(arg0->integer + arg1->integer);
}
LISP_FUNC_2(nl_builtin_sub, arg0, arg1) {
// TODO: Return an error type
if (arg0->type != arg1->type) {
return NULL;
if (arg0->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'sub'");
}
if (arg0->type != NL_TYPE_INT) {
return NULL;
if (arg1->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'sub'");
}
return nl_int_create(arg0->integer - arg1->integer);
}
LISP_FUNC_2(nl_builtin_mul, arg0, arg1) {
// TODO: Return an error type
if (arg0->type != arg1->type) {
return NULL;
if (arg0->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'mul'");
}
if (arg0->type != NL_TYPE_INT) {
return NULL;
if (arg1->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'mul'");
}
return nl_int_create(arg0->integer * arg1->integer);
}
LISP_FUNC_2(nl_builtin_div, arg0, arg1) {
// TODO: Return an error type
if (arg0->type != arg1->type) {
return NULL;
if (arg0->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'div'");
}
if (arg0->type != NL_TYPE_INT) {
return NULL;
if (arg1->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'div'");
}
return nl_int_create(arg0->integer / arg1->integer);
}
LISP_FUNC_2(nl_builtin_mod, arg0, arg1) {
// TODO: Return an error type
if (arg0->type != arg1->type) {
return NULL;
if (arg0->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 0 to 'mod'");
}
if (arg0->type != NL_TYPE_INT) {
return NULL;
if (arg1->type != NL_TYPE_INT) {
return nl_error_create("Invalid type of argument 1 to 'mod'");
}
return nl_int_create(arg0->integer % arg1->integer);
}
LISP_FUNC_2(nl_builtin_concat, arg0, arg1) {
// TODO: Return an error type
if (arg0->type != arg1->type) {
return NULL;
if (arg0->type != NL_TYPE_STRING) {
return nl_error_create("Invalid type of argument 0 to 'concat'");
}
if (arg0->type != NL_TYPE_STRING) {
return NULL;
if (arg1->type != NL_TYPE_STRING) {
return nl_error_create("Invalid type of argument 1 to 'concat'");
}
int len = strlen(arg0->string) + strlen(arg1->string);