more refactoring and error checking
This commit is contained in:
+42
-30
@@ -1,21 +1,21 @@
|
||||
#include "lisp.h"
|
||||
|
||||
#define func_arithmetic_ex(name, operator, operator_name) \
|
||||
int name(Nodes *args, Scopes *scopes, Node *result) { \
|
||||
int name(Nodes args, Scopes *scopes, Node *result) { \
|
||||
UNUSED(scopes); \
|
||||
if (args->count != 2) { \
|
||||
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) { \
|
||||
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; \
|
||||
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; \
|
||||
@@ -35,48 +35,60 @@ 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) {
|
||||
int lisp_function_println(Nodes args, Scopes *scopes, Node *result) {
|
||||
if (args.count != 1) {
|
||||
printf("'+' expects one argument!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
eval_one(args->items[0], scopes, result);
|
||||
eval_one(args.items[0], scopes, result);
|
||||
node_print(*result);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lisp_function_list(Nodes *args, Scopes *scopes, Node *result) {
|
||||
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 = args;
|
||||
result->as_list = nodes;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lisp_function_append(Nodes *args, Scopes *scopes, Node *result) {
|
||||
int lisp_function_append(Nodes args, Scopes *scopes, Node *result) {
|
||||
UNUSED(scopes);
|
||||
|
||||
if (args->count != 2) {
|
||||
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) {
|
||||
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;
|
||||
Nodes *a = args.items[0].as_list;
|
||||
nob_da_append_many(nodes, a->items, a->count);
|
||||
|
||||
Nodes *b = args->items[1].as_list;
|
||||
Nodes *b = args.items[1].as_list;
|
||||
nob_da_append_many(nodes, b->items, b->count);
|
||||
|
||||
result->as_list = nodes;
|
||||
@@ -84,55 +96,55 @@ int lisp_function_append(Nodes *args, Scopes *scopes, Node *result) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lisp_function_length(Nodes *args, Scopes *scopes, Node *result) {
|
||||
int lisp_function_length(Nodes args, Scopes *scopes, Node *result) {
|
||||
UNUSED(scopes);
|
||||
|
||||
if (args->count != 1) {
|
||||
if (args.count != 1) {
|
||||
printf("'length' expects one argument!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (args->items[0].kind != NODE_KIND_LIST) {
|
||||
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;
|
||||
result->as_number = args.items[0].as_list->count;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lisp_function_isnull(Nodes *args, Scopes *scopes, Node *result) {
|
||||
int lisp_function_isnull(Nodes args, Scopes *scopes, Node *result) {
|
||||
UNUSED(scopes);
|
||||
|
||||
if (args->count != 1) {
|
||||
if (args.count != 1) {
|
||||
printf("'null?' expects one argument!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (args->items[0].kind != NODE_KIND_LIST) {
|
||||
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;
|
||||
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) {
|
||||
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 = malloc(sizeof(Node));
|
||||
Nodes nodes = {0};
|
||||
nob_da_foreach(ScopeObject, scope_object, global_scope) {
|
||||
nob_da_append(nodes, scope_object->node);
|
||||
nob_da_append(&nodes, scope_object->node);
|
||||
}
|
||||
|
||||
result->as_list = nodes;
|
||||
*result->as_list = nodes;
|
||||
result->kind = NODE_KIND_LIST;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user