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

@@ -7,9 +7,9 @@
#include <string.h>
struct nl_object *nl_car(struct nl_object *list) {
if (list == NULL || list->type != NL_TYPE_CELL) {
return NULL; // TODO: Return an error type
}
NL_COND_OR_RET_ERROR(
list != NULL && list->type == NL_TYPE_CELL,
"Invalid type of argument 0 to 'nl_car'");
struct nl_object *car = list->cell.car;
if (car == NULL) {
@@ -20,9 +20,9 @@ struct nl_object *nl_car(struct nl_object *list) {
}
struct nl_object *nl_cdr(struct nl_object *list) {
if (list == NULL || list->type != NL_TYPE_CELL) {
return NULL; // TODO: Return an error type
}
NL_COND_OR_RET_ERROR(
list != NULL && list->type == NL_TYPE_CELL,
"Invalid type of argument 0 to 'nl_cdr'");
struct nl_object *cdr = list->cell.cdr;
if (cdr == NULL) {
@@ -49,9 +49,9 @@ struct nl_object *nl_predicate(bool value) {
}
struct nl_object *nl_list_length(struct nl_object *list) {
if (list == NULL || list->type != NL_TYPE_CELL) {
return NULL; // TODO: Return an error type
}
NL_COND_OR_RET_ERROR(
list != NULL && list->type == NL_TYPE_CELL,
"Invalid type of argument 0 to 'nl_list_length'");
struct nl_object *node = list;
if (list->cell.car == NULL) {
@@ -68,14 +68,12 @@ struct nl_object *nl_list_length(struct nl_object *list) {
}
struct nl_object *nl_list_nth(struct nl_object *list, int n) {
if (list == NULL || list->type != NL_TYPE_CELL) {
return NULL; // TODO: Return an error type
}
NL_COND_OR_RET_ERROR(
list != NULL && list->type == NL_TYPE_CELL,
"Invalid type of argument 0 to 'nl_list_'");
int length = nl_list_length(list)->integer;
if (length <= n) {
return NULL; // TODO: Return an error type
}
NL_COND_OR_RET_ERROR(length > n, "Position n >= list length in nl_list_nth");
struct nl_object *node = list;
for (int i = 0; i < n; i++) {
@@ -87,7 +85,7 @@ struct nl_object *nl_list_nth(struct nl_object *list, int n) {
struct nl_object *nl_truthy(struct nl_object *obj) {
// TODO: Implement me
return NULL;
return nl_error_create("Unimplemented function 'nl_truthy'");
}
struct nl_object *nl_tuple_create(struct nl_object *obj0, struct nl_object *obj1) {