144 lines
4.7 KiB
C
144 lines
4.7 KiB
C
#include "lisp.h"
|
|
|
|
#define func_arithmetic_ex(name, operator, operator_name) \
|
|
Node *name(Nodes *args, Scopes *scopes) { \
|
|
UNUSED(scopes); \
|
|
if (args->count != 2) { \
|
|
puts("'" operator_name "' expects two arguments!\n"); \
|
|
return NULL; \
|
|
} \
|
|
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 NULL; \
|
|
} \
|
|
Node *node = malloc(sizeof(Node)); \
|
|
node->kind = NODE_KIND_NUMBER; \
|
|
long long a = args->items[0].as_number; \
|
|
long long b = args->items[1].as_number; \
|
|
node->as_number = a operator b; \
|
|
return node; \
|
|
}
|
|
#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, >=);
|
|
|
|
Node *lisp_function_println(Nodes *args, Scopes *scopes) {
|
|
if (args->count != 1) {
|
|
printf("'+' expects one argument!\n");
|
|
return NULL;
|
|
}
|
|
|
|
Node *result = eval(&args->items[0], scopes);
|
|
node_print(*result);
|
|
printf("\n");
|
|
|
|
return result;
|
|
}
|
|
|
|
Node *lisp_function_list(Nodes *args, Scopes *scopes) {
|
|
UNUSED(scopes);
|
|
|
|
Node *node = malloc(sizeof(Node));
|
|
node->kind = NODE_KIND_LIST;
|
|
node->as_list = args;
|
|
return node;
|
|
}
|
|
|
|
Node *lisp_function_append(Nodes *args, Scopes *scopes) {
|
|
UNUSED(scopes);
|
|
|
|
if (args->count != 2) {
|
|
printf("'append' expects two arguments!\n");
|
|
return NULL;
|
|
}
|
|
|
|
Node *node = malloc(sizeof(Node));
|
|
node->kind = NODE_KIND_LIST;
|
|
|
|
if (args->items[0].kind != NODE_KIND_LIST ||
|
|
args->items[1].kind != NODE_KIND_LIST) {
|
|
printf("'append' can only append two lists!\n");
|
|
return NULL;
|
|
}
|
|
|
|
Nodes *nodes = malloc(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);
|
|
|
|
node->as_list = nodes;
|
|
return node;
|
|
}
|
|
|
|
Node *lisp_function_length(Nodes *args, Scopes *scopes) {
|
|
UNUSED(scopes);
|
|
|
|
if (args->count != 1) {
|
|
printf("'length' expects one argument!\n");
|
|
return NULL;
|
|
}
|
|
|
|
if (args->items[0].kind != NODE_KIND_LIST) {
|
|
printf("'length' can only be used on lists!\n");
|
|
return NULL;
|
|
}
|
|
|
|
Node *node = malloc(sizeof(Node));
|
|
node->kind = NODE_KIND_NUMBER;
|
|
node->as_number = args->items[0].as_list->count;
|
|
return node;
|
|
}
|
|
|
|
Node *lisp_function_isnull(Nodes *args, Scopes *scopes) {
|
|
UNUSED(scopes);
|
|
|
|
if (args->count != 1) {
|
|
printf("'null?' expects one argument!\n");
|
|
return NULL;
|
|
}
|
|
|
|
if (args->items[0].kind != NODE_KIND_LIST) {
|
|
printf("'null?' can only be used on lists!\n");
|
|
return NULL;
|
|
}
|
|
|
|
Node *node = malloc(sizeof(Node));
|
|
node->kind = NODE_KIND_NUMBER;
|
|
node->as_number = args->items[0].as_list->count == 0 ? 1 : 0;
|
|
return node;
|
|
}
|
|
|
|
Node *lisp_function_builtins(Nodes *args, Scopes *scopes) {
|
|
if (args->count != 0) {
|
|
printf("'builtins' expects no arguments!\n");
|
|
return NULL;
|
|
}
|
|
|
|
Node *node = malloc(sizeof(Node));
|
|
node->kind = NODE_KIND_LIST;
|
|
|
|
Scope global_scope = nob_da_first(scopes);
|
|
Nodes *nodes = malloc(sizeof(Node));
|
|
nob_da_foreach(ScopeObject, scope_object, &global_scope) {
|
|
nob_da_append(nodes, scope_object->node);
|
|
}
|
|
node->as_list = nodes;
|
|
return node;
|
|
}
|