226 lines
6.4 KiB
C
226 lines
6.4 KiB
C
#include "lisp.h"
|
|
|
|
#include <math.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; \
|
|
} \
|
|
double a = args.items[0].as_number; \
|
|
double 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(builtin_add, +);
|
|
func_arithmetic(builtin_sub, -);
|
|
func_arithmetic(builtin_div, /);
|
|
func_arithmetic(builtin_mul, *);
|
|
func_arithmetic_ex(builtin_eq, ==, "=");
|
|
func_arithmetic(builtin_lt, <);
|
|
func_arithmetic(builtin_gt, >);
|
|
func_arithmetic(builtin_lt_eq, <=);
|
|
func_arithmetic(builtin_gt_eq, >=);
|
|
|
|
int builtin_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 builtin_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 builtin_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 builtin_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 builtin_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 builtin_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 = malloc(sizeof(Nodes));
|
|
if (nodes == NULL) {
|
|
printf("Failed to allocated memory, something is very wrong!\n");
|
|
return 1;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
int builtin_floor(Nodes args, Scopes *scopes, Node *result) {
|
|
UNUSED(scopes);
|
|
|
|
if (args.count != 1) {
|
|
printf("'floor' expects one argument!\n");
|
|
return 1;
|
|
}
|
|
|
|
if (args.items[0].kind != NODE_KIND_NUMBER) {
|
|
printf("'floor' can only be used on numbers!\n");
|
|
return 1;
|
|
}
|
|
|
|
result->kind = NODE_KIND_NUMBER;
|
|
result->as_number = floor(args.items[0].as_number);
|
|
return 0;
|
|
}
|
|
|
|
int builtin_ceil(Nodes args, Scopes *scopes, Node *result) {
|
|
UNUSED(scopes);
|
|
|
|
if (args.count != 1) {
|
|
printf("'ceil' expects one argument!\n");
|
|
return 1;
|
|
}
|
|
|
|
if (args.items[0].kind != NODE_KIND_NUMBER) {
|
|
printf("'ceil' can only be used on numbers!\n");
|
|
return 1;
|
|
}
|
|
|
|
result->kind = NODE_KIND_NUMBER;
|
|
result->as_number = ceil(args.items[0].as_number);
|
|
return 0;
|
|
}
|
|
|
|
int builtin_nth(Nodes args, Scopes *scopes, Node *result) {
|
|
UNUSED(scopes);
|
|
|
|
if (args.count != 2) {
|
|
printf("'nth' expects two arguments!\n");
|
|
return 1;
|
|
}
|
|
|
|
Node node_idx = args.items[0];
|
|
if (node_idx.kind != NODE_KIND_NUMBER) {
|
|
printf("'nth' expects an index as the first argument!\n");
|
|
return 1;
|
|
}
|
|
|
|
Node node_list = args.items[1];
|
|
if (node_list.kind != NODE_KIND_LIST) {
|
|
printf("'nth' expects a list as the second argument!\n");
|
|
return 1;
|
|
}
|
|
|
|
uint64_t idx = node_idx.as_number;
|
|
if (idx >= node_list.as_list->count) {
|
|
printf("Index out of bounds!");
|
|
return 1;
|
|
}
|
|
|
|
*result = node_list.as_list->items[idx];
|
|
return 0;
|
|
}
|