Files
lisp/builtins.c
T

154 lines
4.8 KiB
C

#include "lisp.h"
#define func_arithmetic_ex(name, operator, operator_name) \
int name(Nodes args, Scopes *scopes, Node *result) { \
UNUSED(scopes); \
if (args.count != 2) { \
puts("'" operator_name "' expects two arguments!\n"); \
return 1; \
} \
if (args.items[0].kind != NODE_KIND_NUMBER || \
args.items[1].kind != NODE_KIND_NUMBER) { \
puts("'" operator_name \
"' only works on numbers, you specified something " \
"else!\n"); \
return 1; \
} \
long long a = args.items[0].as_number; \
long long b = args.items[1].as_number; \
result->as_number = a operator b; \
result->kind = NODE_KIND_NUMBER; \
return 0; \
}
#define func_arithmetic(name, operator) \
func_arithmetic_ex(name, operator, #operator)
func_arithmetic(lisp_function_add, +);
func_arithmetic(lisp_function_sub, -);
func_arithmetic(lisp_function_div, /);
func_arithmetic(lisp_function_mul, *);
func_arithmetic(lisp_function_mod, %);
func_arithmetic_ex(lisp_function_eq, ==, "=");
func_arithmetic(lisp_function_lt, <);
func_arithmetic(lisp_function_gt, >);
func_arithmetic(lisp_function_lt_eq, <=);
func_arithmetic(lisp_function_gt_eq, >=);
int lisp_function_println(Nodes args, Scopes *scopes, Node *result) {
if (args.count != 1) {
printf("'println' expects one argument!\n");
return 1;
}
int ret = eval_one(args.items[0], scopes, result);
if (ret != 0)
return ret;
node_print(*result);
printf("\n");
return 0;
}
int lisp_function_list(Nodes args, Scopes *scopes, Node *result) {
UNUSED(scopes);
Nodes *nodes = malloc(sizeof(Nodes));
if (nodes == NULL) {
printf("Failed to allocate memory, something is very wrong!\n");
return 1;
}
memcpy(nodes, &args, sizeof(Nodes));
result->kind = NODE_KIND_LIST;
result->as_list = nodes;
return 0;
}
int lisp_function_append(Nodes args, Scopes *scopes, Node *result) {
UNUSED(scopes);
if (args.count != 2) {
printf("'append' expects two arguments!\n");
return 1;
}
if (args.items[0].kind != NODE_KIND_LIST ||
args.items[1].kind != NODE_KIND_LIST) {
printf("'append' can only append two lists!\n");
return 1;
}
Nodes *nodes = malloc(sizeof(Nodes));
if (nodes == NULL) {
printf("Failed to allocate memory, something is very wrong!\n");
return 1;
}
memset(nodes, 0, sizeof(Nodes));
Nodes *a = args.items[0].as_list;
nob_da_append_many(nodes, a->items, a->count);
Nodes *b = args.items[1].as_list;
nob_da_append_many(nodes, b->items, b->count);
result->as_list = nodes;
result->kind = NODE_KIND_LIST;
return 0;
}
int lisp_function_length(Nodes args, Scopes *scopes, Node *result) {
UNUSED(scopes);
if (args.count != 1) {
printf("'length' expects one argument!\n");
return 1;
}
if (args.items[0].kind != NODE_KIND_LIST) {
printf("'length' can only be used on lists!\n");
return 1;
}
result->kind = NODE_KIND_NUMBER;
result->as_number = args.items[0].as_list->count;
return 0;
}
int lisp_function_isnull(Nodes args, Scopes *scopes, Node *result) {
UNUSED(scopes);
if (args.count != 1) {
printf("'null?' expects one argument!\n");
return 1;
}
if (args.items[0].kind != NODE_KIND_LIST) {
printf("'null?' can only be used on lists!\n");
return 1;
}
result->kind = NODE_KIND_NUMBER;
result->as_number = args.items[0].as_list->count == 0 ? 1 : 0;
return 0;
}
int lisp_function_builtins(Nodes args, Scopes *scopes, Node *result) {
if (args.count != 0) {
printf("'builtins' expects no arguments!\n");
return 1;
}
Scope *global_scope = nob_da_first(scopes);
Nodes nodes = {0};
nob_da_foreach(ScopeObject, scope_object, global_scope) {
nob_da_append(&nodes, scope_object->node);
}
*result->as_list = nodes;
result->kind = NODE_KIND_LIST;
return 0;
}