Add lots of utilties, initial builtins

This commit is contained in:
2022-10-27 16:51:21 -04:00
parent 07a486cd16
commit d81d8c5156
11 changed files with 603 additions and 10 deletions

142
src/builtins.c Normal file
View File

@@ -0,0 +1,142 @@
#include "nihilispm.h"
#include "nihilispm_internal.h"
#include "nihilispm_utility.h"
#include "builtins.h"
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
LISP_FUNC_0(nl_builtin_hello_world) {
return nl_string_create(strdup("Hello, world!"));
}
LISP_FUNC_1(nl_builtin_type, arg) {
switch (arg->type) {
case NL_TYPE_CELL:
return nl_symbol_create(strdup("list"));
case NL_TYPE_SYMBOL:
return nl_symbol_create(strdup("symbol"));
case NL_TYPE_INT:
return nl_symbol_create(strdup("int"));
case NL_TYPE_STRING:
return nl_symbol_create(strdup("string"));
case NL_TYPE_COUNT:
assert(0);
return NULL;
}
}
LISP_FUNC_1(nl_builtin_symbol_p, arg) {
return nl_predicate(arg->type == NL_TYPE_SYMBOL);
}
LISP_FUNC_1(nl_builtin_string_p, arg) {
return nl_predicate(arg->type == NL_TYPE_STRING);
}
LISP_FUNC_1(nl_builtin_int_p, arg) {
return nl_predicate(arg->type == NL_TYPE_INT);
}
LISP_FUNC_1(nl_builtin_list_p, arg) {
return nl_predicate(arg->type == NL_TYPE_CELL);
}
LISP_FUNC_1(nl_builtin_car, arg) {
return nl_car(arg);
}
LISP_FUNC_1(nl_builtin_cdr, arg) {
return nl_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 NULL;
}
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 NULL;
}
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 NULL;
}
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 NULL;
}
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 NULL;
}
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 NULL;
}
int len = strlen(arg0->string) + strlen(arg1->string);
char *outstr = malloc(strlen(arg0->string) + strlen(arg1->string));
outstr[0] = '\0';
strcat(outstr, arg0->string);
strcat(outstr, arg1->string);
return nl_string_create(outstr);
}
LISP_FUNC_0(nl_builtin_now_millis_mono) {
// TODO: Implement and move to a 'platform' file
return NULL;
}

60
src/builtins.h Normal file
View File

@@ -0,0 +1,60 @@
#include "nihilispm_utility.h"
#define LISP_FUNC_0(func_name) \
static struct nl_object *func_name##_impl(); \
struct nl_object *func_name(struct nl_object *args) { \
if (args->cell.car != NULL) { \
return NULL; \
} \
return func_name##_impl(); \
} \
static struct nl_object *func_name##_impl()
#define LISP_FUNC_1(func_name, arg0_name) \
static struct nl_object *func_name##_impl(struct nl_object *arg0_name); \
struct nl_object *func_name(struct nl_object *args) { \
struct nl_object *len_obj = nl_list_length(args); \
if (len_obj->type != NL_TYPE_INT) { \
return NULL; \
} \
if (len_obj->integer != 1) { \
return NULL; \
} \
struct nl_object *arg0 = nl_car(args); \
return func_name##_impl(arg0); \
} \
static struct nl_object *func_name##_impl(struct nl_object *arg0_name)
// TODO: Unroll the args more efficiently, this is O(n^2)
#define LISP_FUNC_2(func_name, arg0_name, arg1_name) \
static struct nl_object *func_name##_impl(struct nl_object *arg0_name, struct nl_object *arg1_name); \
struct nl_object *func_name(struct nl_object *args) { \
struct nl_object *len_obj = nl_list_length(args); \
if (len_obj->type != NL_TYPE_INT) { \
return NULL; \
} \
if (len_obj->integer != 2) { \
return NULL; \
} \
struct nl_object *arg0 = nl_list_nth(args, 0); \
struct nl_object *arg1 = nl_list_nth(args, 1); \
return func_name##_impl(arg0, arg1); \
} \
static struct nl_object *func_name##_impl(struct nl_object *arg0_name, struct nl_object *arg1_name)
struct nl_object *nl_builtin_hello_world(struct nl_object *args);
struct nl_object *nl_builtin_type(struct nl_object *args);
struct nl_object *nl_builtin_symbol_p(struct nl_object *args);
struct nl_object *nl_builtin_string_p(struct nl_object *args);
struct nl_object *nl_builtin_int_p(struct nl_object *args);
struct nl_object *nl_builtin_list_p(struct nl_object *args);
struct nl_object *nl_builtin_add(struct nl_object *args);
struct nl_object *nl_builtin_sub(struct nl_object *args);
struct nl_object *nl_builtin_mul(struct nl_object *args);
struct nl_object *nl_builtin_div(struct nl_object *args);
struct nl_object *nl_builtin_mod(struct nl_object *args);
struct nl_object *nl_builtin_concat(struct nl_object *args);
struct nl_object *nl_builtin_now_millis_mono(struct nl_object *args);

29
src/evaluate.c Normal file
View File

@@ -0,0 +1,29 @@
#include <assert.h>
#include <stddef.h>
#include "nihilispm.h"
struct nl_object *nl_evaluate_list(struct nl_object *list) {
return NULL;
}
struct nl_object *nl_evaluate_symbol(struct nl_object *symbol) {
return NULL;
}
struct nl_object *nl_evaluate(struct nl_object *obj) {
assert(obj != NULL);
switch (obj->type) {
case NL_TYPE_CELL:
return nl_evaluate_list(obj);
case NL_TYPE_SYMBOL:
return NULL; //nl_evalute_symbol(obj);
case NL_TYPE_INT:
case NL_TYPE_STRING:
return obj;
case NL_TYPE_COUNT:
assert(0);
return NULL;
}
}

View File

@@ -6,7 +6,6 @@
static struct nl_object *nl_object_alloc();
static void nl_cell_delete(struct nl_cell *cell);
struct nl_object *nl_cell_create(struct nl_object *car, struct nl_object *cdr)
{
@@ -70,4 +69,3 @@ void nl_object_delete(struct nl_object *obj) {
}
free(obj);
}

View File

@@ -35,6 +35,6 @@ struct nl_state; // TODO
struct nl_object *nl_tokenize(const char *source);
struct nl_object *nl_parse(const char *sexp);
struct nl_cell *nl_evaluate(const struct nl_cell *sexp);
struct nl_object *nl_evaluate(struct nl_object *sexp);
#endif

View File

@@ -1,11 +1,13 @@
#include "nihilispm.h"
#define ARRAY_SIZE(x) sizeof(x) / sizeof(x[0])
#define FOREACH_LIST(list, iter, item) \
for (struct nl_object *iter = list, *item = iter->cell.car; \
iter != NULL; \
iter = token->cell.cdr, item = iter->cell.car)
#define FOREACH_LIST(list, iter_name, item_name) \
FOREACH_LIST_COND(list, iter_name, item_name, true)
#define FOREACH_LIST_COND(list, iter_name, item_name, cond) \
for (struct nl_object *iter_name = (list), *item_name = iter_name->cell.car; \
iter_name != NULL && (cond); \
iter_name = token->cell.cdr, item_name = iter_name->cell.car)
struct nl_object *nl_cell_create(struct nl_object *car, struct nl_object *cdr);

30
src/nihilispm_utility.h Normal file
View File

@@ -0,0 +1,30 @@
#include <stdbool.h>
#include "nihilispm.h"
struct nl_object *nl_nil_create();
struct nl_object *nl_t_create();
struct nl_object *nl_predicate(bool value);
struct nl_object *nl_truthy(struct nl_object *arg);
struct nl_object* nl_car(struct nl_object *list);
struct nl_object* nl_cdr(struct nl_object *list);
struct nl_object* nl_list_length(struct nl_object *list);
struct nl_object* nl_list_nth(struct nl_object *list, int n);
struct nl_object* nl_list_append(struct nl_object *list, struct nl_object *obj);
struct nl_object* nl_tuple_create(struct nl_object *obj0, struct nl_object *obj1);
/* #define NL_RET_IF_ERROR(obj) \ */
/* do { \ */
/* if ((obj)->type == NL_TYPE_ERROR) { \ */
/* return obj; \ */
/* } while(0) */
#define NL_COND_OR_RET_ERROR(cond, msg) \
do { \
if (!(cond)) { \
return nl_error_create(strdup(msg)); \
} \
} while(0)

113
src/utility.c Normal file
View 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;
}