feat: lambda functions

This commit is contained in:
2026-07-24 23:52:09 +02:00
parent b8170c3c01
commit 729f07655a
2 changed files with 229 additions and 122 deletions
+122 -20
View File
@@ -100,6 +100,9 @@ void node_print(Node node) {
Nob_String_View func_name = node.as_function.name; Nob_String_View func_name = node.as_function.name;
printf(SV_Fmt, SV_Arg(func_name)); printf(SV_Fmt, SV_Arg(func_name));
} break; } break;
case NODE_KIND_LAMBDA: {
printf("<lambda>");
} break;
default: default:
UNREACHABLE("Unknown node type"); UNREACHABLE("Unknown node type");
} }
@@ -300,13 +303,9 @@ int eval_one(Node input, Scopes *scopes, Node *result) {
return 0; return 0;
} }
Node head = children->items[0]; Node head_node = children->items[0];
if (head.kind != NODE_KIND_SYMBOL) { if (head_node.kind == NODE_KIND_SYMBOL) {
*result = input; if (nob_sv_eq(head_node.as_symbol,
return 0;
}
if (nob_sv_eq(head.as_symbol,
(Nob_String_View){.count = 2, .data = "if"})) { (Nob_String_View){.count = 2, .data = "if"})) {
if (children->count != 3 + 1) { if (children->count != 3 + 1) {
printf("'if' expects three arguments!\n"); printf("'if' expects three arguments!\n");
@@ -327,7 +326,7 @@ int eval_one(Node input, Scopes *scopes, Node *result) {
return ret; return ret;
} }
if (nob_sv_eq(head.as_symbol, if (nob_sv_eq(head_node.as_symbol,
(Nob_String_View){.count = 6, .data = "define"})) { (Nob_String_View){.count = 6, .data = "define"})) {
if (children->count != 2 + 1) { if (children->count != 2 + 1) {
printf("'define' expects two arguments!\n"); printf("'define' expects two arguments!\n");
@@ -360,7 +359,7 @@ int eval_one(Node input, Scopes *scopes, Node *result) {
return 0; return 0;
} }
if (nob_sv_eq(head.as_symbol, if (nob_sv_eq(head_node.as_symbol,
(Nob_String_View){.count = 3, .data = "let"})) { (Nob_String_View){.count = 3, .data = "let"})) {
if (children->count != 2 + 1) { if (children->count != 2 + 1) {
printf("'let' expects two arguments!\n"); printf("'let' expects two arguments!\n");
@@ -379,13 +378,14 @@ int eval_one(Node input, Scopes *scopes, Node *result) {
return 1; return 1;
} }
// it's fine to allocate this on the stack because we will throw it // it's fine to allocate this on the stack because we will throw
// off the scope stack after parsing 'let' anyways therefore it'll // it off the scope stack after parsing 'let' anyways therefore
// outlive the recursive call // it'll outlive the recursive call
Scope scope = {0}; Scope scope = {0};
nob_da_foreach(Node, def, definitions.as_list) { nob_da_foreach(Node, def, definitions.as_list) {
if (def->kind != NODE_KIND_LIST || def->as_list->count != 2 || if (def->kind != NODE_KIND_LIST ||
def->as_list->count != 2 ||
def->as_list->items[0].kind != NODE_KIND_SYMBOL) { def->as_list->items[0].kind != NODE_KIND_SYMBOL) {
printf("'let' expects a list of key-value pairs, e.g. " printf("'let' expects a list of key-value pairs, e.g. "
"'(let ((x 42) (y 2)) (+ x y))'\n"); "'(let ((x 42) (y 2)) (+ x y))'\n");
@@ -421,6 +421,61 @@ int eval_one(Node input, Scopes *scopes, Node *result) {
return 0; return 0;
} }
if (nob_sv_eq(head_node.as_symbol,
(Nob_String_View){.count = 6, .data = "lambda"})) {
if (children->count != 2 + 1) {
printf("'lambda' expects two argument!\n");
return 1;
}
Node params = children->items[0 + 1];
if (params.kind != NODE_KIND_LIST) {
printf("'lambda' expects a list of parameters as the first "
"argument!\n");
return 1;
}
nob_da_foreach(Node, node, params.as_list) {
if (node->kind != NODE_KIND_SYMBOL) {
printf("'lambda' expects a list of symbols "
"(parameters) as "
"the "
"first argument!\n");
return 1;
}
}
result->kind = NODE_KIND_LAMBDA;
Nodes *params_list = malloc(sizeof(Nodes));
if (params_list == NULL) {
printf("Failed to allocate memory, something is really "
"wrong!\n");
return 1;
}
memcpy(params_list, params.as_list, sizeof(Nodes));
result->as_lambda.params = params_list;
Node *body = malloc(sizeof(Node));
if (body == NULL) {
printf("Failed to allocate memory, something is really "
"wrong!\n");
return 1;
}
memcpy(body, &children->items[1 + 1], sizeof(Node));
result->as_lambda.body = body;
return 0;
}
}
Node head = {0};
int ret = eval_one(head_node, scopes, &head);
if (ret != 0) {
return ret;
}
Nodes args = {0}; Nodes args = {0};
for (size_t i = 1; i < children->count; i++) { for (size_t i = 1; i < children->count; i++) {
Node n = {0}; Node n = {0};
@@ -431,22 +486,69 @@ int eval_one(Node input, Scopes *scopes, Node *result) {
nob_da_append(&args, n); nob_da_append(&args, n);
} }
Node func_node = {0}; switch (head.kind) {
int ret = eval_one(head, scopes, &func_node); case NODE_KIND_FUNCTION: {
head.as_function.apply(args, scopes, result);
return 0;
}
case NODE_KIND_LAMBDA: {
Nodes *lambda_params = head.as_lambda.params;
if (lambda_params == NULL) {
printf("Couldn't evaluate lambda, no params!\n");
return 1;
}
if (lambda_params->count != args.count) {
printf("Invalid amount of arguments to lambda: expected %zu, "
"found %zu\n!",
lambda_params->count, args.count);
return 1;
}
Node *lambda_body = head.as_lambda.body;
if (lambda_body == NULL) {
printf("Couldn't evaluate lambda, no body!\n");
return 1;
}
Scope scope = {0};
// params->count == args.count
for (size_t i = 0; i < lambda_params->count; i++) {
Node param = lambda_params->items[i];
// this should already be checked when we emit the lambda node
NOB_ASSERT(param.kind == NODE_KIND_SYMBOL);
Node value = args.items[i];
ScopeObject obj = {.name = param.as_symbol, .node = value};
nob_da_append(&scope, obj);
}
Node expression_value = {0};
{
nob_da_append(scopes, &scope);
int ret = eval_one(*lambda_body, scopes, &expression_value);
if (ret != 0) { if (ret != 0) {
return ret; return ret;
} }
if (func_node.kind == NODE_KIND_FUNCTION) { UNUSED(nob_da_pop(scopes));
func_node.as_function.apply(args, scopes, result);
return 0;
} }
printf("Not a function: '"); *result = expression_value;
node_print(func_node); return 0;
} break;
default: {
printf("Not a function-like object: '");
node_print(head);
printf("'\n"); printf("'\n");
return 1; return 1;
} }
}
}
case NODE_KIND_LAMBDA: {
return 0;
} break;
default: default:
return 1; return 1;
} }
+5
View File
@@ -26,6 +26,7 @@ typedef enum e_NodeKind {
NODE_KIND_LIST, NODE_KIND_LIST,
NODE_KIND_NUMBER, NODE_KIND_NUMBER,
NODE_KIND_FUNCTION, NODE_KIND_FUNCTION,
NODE_KIND_LAMBDA,
} NodeKind; } NodeKind;
typedef struct s_Node Node; typedef struct s_Node Node;
@@ -46,6 +47,10 @@ struct s_Node {
Nob_String_View name; Nob_String_View name;
LispFunction apply; LispFunction apply;
} as_function; } as_function;
struct {
Nodes *params;
Node *body;
} as_lambda;
}; };
}; };