Add lots of utilties, initial builtins
This commit is contained in:
113
src/utility.c
Normal file
113
src/utility.c
Normal file
@@ -0,0 +1,113 @@
|
||||
#include "nihilispm.h"
|
||||
#include "nihilispm_internal.h"
|
||||
#include "nihilispm_utility.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#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
|
||||
}
|
||||
|
||||
struct nl_object *car = list->cell.car;
|
||||
if (car == NULL) {
|
||||
return nl_nil_create();
|
||||
}
|
||||
|
||||
return car;
|
||||
}
|
||||
|
||||
struct nl_object *nl_cdr(struct nl_object *list) {
|
||||
if (list == NULL || list->type != NL_TYPE_CELL) {
|
||||
return NULL; // TODO: Return an error type
|
||||
}
|
||||
|
||||
struct nl_object *cdr = list->cell.cdr;
|
||||
if (cdr == NULL) {
|
||||
return nl_nil_create();
|
||||
}
|
||||
|
||||
return cdr;
|
||||
}
|
||||
|
||||
struct nl_object *nl_nil_create() {
|
||||
return nl_cell_create(NULL, NULL);
|
||||
}
|
||||
|
||||
struct nl_object *nl_t_create() {
|
||||
return nl_symbol_create(strdup("t"));
|
||||
}
|
||||
|
||||
struct nl_object *nl_predicate(bool value) {
|
||||
if (value) {
|
||||
return nl_t_create();
|
||||
} else {
|
||||
return nl_nil_create();
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
struct nl_object *node = list;
|
||||
if (list->cell.car == NULL) {
|
||||
return nl_int_create(0);
|
||||
}
|
||||
|
||||
int length = 1;
|
||||
while (node->cell.cdr != NULL) {
|
||||
node = node->cell.cdr;
|
||||
length++;
|
||||
}
|
||||
|
||||
return nl_int_create(length);
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
int length = nl_list_length(list)->integer;
|
||||
if (length <= n) {
|
||||
return NULL; // TODO: Return an error type
|
||||
}
|
||||
|
||||
struct nl_object *node = list;
|
||||
for (int i = 0; i < n; i++) {
|
||||
node = node->cell.cdr;
|
||||
}
|
||||
|
||||
return node->cell.car;
|
||||
}
|
||||
|
||||
struct nl_object *nl_truthy(struct nl_object *obj) {
|
||||
// TODO: Implement me
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct nl_object *nl_tuple_create(struct nl_object *obj0, struct nl_object *obj1) {
|
||||
struct nl_object *tuple = nl_cell_create(obj0, NULL);
|
||||
nl_list_append(tuple, obj1);
|
||||
return tuple;
|
||||
}
|
||||
|
||||
struct nl_object *nl_list_append(struct nl_object *list, struct nl_object *obj) {
|
||||
struct nl_object *iter = list;
|
||||
|
||||
if (list->cell.car == NULL) {
|
||||
list->cell.car = obj;
|
||||
return list;
|
||||
}
|
||||
|
||||
while (iter->cell.cdr != NULL) {
|
||||
iter = iter->cell.cdr;
|
||||
}
|
||||
iter->cell.cdr = nl_cell_create(obj, NULL);
|
||||
|
||||
return list;
|
||||
}
|
||||
Reference in New Issue
Block a user