impl lots of arithmetic operations and if

This commit is contained in:
2026-07-21 17:00:50 +02:00
parent 0034fd79f5
commit 5b6c343ae3
3 changed files with 107 additions and 30 deletions
+50 -20
View File
@@ -1,27 +1,39 @@
#include "lisp.h"
Node *lisp_function_addition(Nodes *args, Scopes *scopes) {
UNUSED(scopes);
if (args->count != 2) {
printf("'+' expects two arguments!\n");
return NULL;
#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)
if (args->items[0].kind != NODE_KIND_NUMBER ||
args->items[1].kind != NODE_KIND_NUMBER) {
printf("'+' can only add numbers, you specified something else!\n");
return NULL;
}
Node *node = malloc(sizeof(Node));
node->kind = NODE_KIND_NUMBER;
intptr_t a = args->items[0].as_number;
intptr_t b = args->items[1].as_number;
node->as_number = a + b;
return node;
}
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) {
@@ -111,3 +123,21 @@ Node *lisp_function_isnull(Nodes *args, Scopes *scopes) {
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;
}