WIP: refactor api

This commit is contained in:
2026-07-21 19:18:53 +02:00
parent 5b6c343ae3
commit 8ec82a66e0
4 changed files with 214 additions and 131 deletions
+133 -57
View File
@@ -2,17 +2,18 @@
static Scope globalScope = {0};
static Node *scope_lookup(Scopes *scopes, Nob_String_View name) {
nob_da_foreach(Scope, scope, scopes) {
nob_da_foreach(ScopeObject, scopeObject, scope) {
static int scope_lookup(Scopes *scopes, Nob_String_View name, Node *result) {
nob_da_foreach(Scope *, scope, scopes) {
nob_da_foreach(ScopeObject, scopeObject, *scope) {
if (nob_sv_eq(name, scopeObject->name)) {
return &scopeObject->node;
*result = scopeObject->node;
return 0;
}
}
}
printf("Undefined symbol: '" SV_Fmt "'!\n", SV_Arg(name));
return NULL;
return 1;
}
static void scope_add_function(Scope *scope, const char *name,
@@ -57,7 +58,7 @@ void build_scopes(Scopes *scopes) {
scope_add_function(&globalScope, "builtins", lisp_function_builtins);
// clang-format on
nob_da_append(scopes, globalScope);
nob_da_append(scopes, &globalScope);
}
const char *token_kind_name(TokenKind kind) {
@@ -157,51 +158,47 @@ void tokenize(Nob_String_View content, Tokens *tokens) {
}
}
Node *parse(Tokens *tokens) {
if (tokens->count == 0) {
int parse_one(Tokens *tokens, Node *node) {
if (tokens == NULL || node == NULL || tokens->count == 0) {
printf("unexpected end of input");
return NULL;
return 1;
}
Token tok = tokens->items[0];
tokens->items++;
tokens->count--;
Node *node = malloc(sizeof(Node));
if (tok.kind == TOKEN_KIND_PAR_LEFT) {
switch (tok.kind) {
case TOKEN_KIND_PAR_LEFT: {
Nodes *nodes = malloc(sizeof(Nodes));
memset(nodes, 0, sizeof(Nodes));
while (true) {
if (tokens->count == 0) {
printf("expected closing ')'");
return NULL;
return 1;
}
if (tokens->items[0].kind == TOKEN_KIND_PAR_RIGHT) {
tokens->items++;
tokens->count--;
if (tokens->count > 0) {
}
break;
}
Node *node = parse(tokens);
if (node == NULL)
return NULL;
parse_one(tokens, node);
nob_da_append(nodes, *node);
}
node->kind = NODE_KIND_LIST;
node->as_list = nodes;
return node;
}
if (tok.kind == TOKEN_KIND_PAR_RIGHT) {
} break;
case TOKEN_KIND_PAR_RIGHT: {
printf("unexpected ')'");
return NULL;
return 1;
}
switch (tok.kind) {
case TOKEN_KIND_NUMBER: {
long long num = strtoll(tok.data.data, NULL, 10);
// TODO:
@@ -219,56 +216,135 @@ Node *parse(Tokens *tokens) {
UNREACHABLE("");
} break;
}
return node;
return 0;
}
Node *eval(Node *node, Scopes *scopes) {
if (node == NULL || scopes == NULL)
return NULL;
void parse(Tokens *tokens, Nodes *nodes) {
if (nodes == NULL)
return;
switch (node->kind) {
for (size_t i = 0; i < tokens->count; i++) {
if (tokens->items[i].kind == TOKEN_KIND_PAR_LEFT) {
size_t sub_size = tokens->count - i;
Tokens toks = {.count = sub_size,
.capacity = sub_size,
.items = tokens->items + i};
Node result = {0};
parse_one(&toks, &result);
nob_da_append(nodes, result);
}
}
}
int eval_one(Node input, Scopes *scopes, Node *result) {
if (scopes == NULL || result == NULL)
return 1;
switch (input.kind) {
case NODE_KIND_NUMBER:
return node;
*result = input;
return 0;
case NODE_KIND_SYMBOL: {
return scope_lookup(scopes, node->as_symbol);
return scope_lookup(scopes, input.as_symbol, result);
}
case NODE_KIND_LIST: {
Nodes *children = node->as_list;
if (children->count == 0)
return node;
Nodes *children = input.as_list;
if (children->count == 0) {
*result = input;
return 0;
}
Node *head = &children->items[0];
if (head->kind != NODE_KIND_SYMBOL)
return node;
Node head = children->items[0];
if (head.kind != NODE_KIND_SYMBOL) {
*result = input;
return 0;
}
if (nob_sv_eq(head.as_symbol,
(Nob_String_View){.count = 2, .data = "if"})) {
if (children->count != 3 + 1) {
printf("'if' expects three arguments!\n");
return 1;
}
Node condition = {0};
eval_one(children->items[0 + 1], scopes, &condition);
if (node_is_true(condition)) {
eval_one(children->items[1 + 1], scopes, result);
} else {
eval_one(children->items[2 + 1], scopes, result);
}
return 0;
}
if (nob_sv_eq(head.as_symbol,
(Nob_String_View){.count = 6, .data = "define"})) {
if (children->count != 2 + 1) {
printf("'define' expects two arguments!\n");
return 1;
}
Node name_node = children->items[0 + 1];
if (name_node.kind != NODE_KIND_SYMBOL) {
printf("First argument of 'define' must be a symbol!\n");
return 1;
}
Node value = {0};
eval_one(children->items[1 + 1], scopes, &value);
nob_da_foreach(ScopeObject, scope_object, &globalScope) {
if (nob_sv_eq(scope_object->name, name_node.as_symbol)) {
scope_object->node = value;
result = &children->items[0 + 1];
return 0;
}
}
ScopeObject obj =
(ScopeObject){.name = name_node.as_symbol, .node = value};
nob_da_append(&globalScope, obj);
result = &children->items[0 + 1];
return 0;
}
Nodes *args = malloc(sizeof(Nodes));
for (size_t i = 1; i < children->count; i++) {
nob_da_append(args, *eval(&children->items[i], scopes));
Node n = {0};
eval_one(children->items[i], scopes, &n);
nob_da_append(args, n);
}
if (nob_sv_eq(head->as_symbol,
(Nob_String_View){.count = 2, .data = "if"})) {
if (args->count != 3) {
printf("'if' expects three arguments!\n");
return NULL;
}
Node *condition = eval(&args->items[0], scopes);
return node_is_true(*condition) ? eval(&args->items[1], scopes)
: eval(&args->items[2], scopes);
} else {
Node *func_node = eval(head, scopes);
if (func_node->kind == NODE_KIND_FUNCTION) {
return func_node->as_function.apply(args, scopes);
}
printf("Not a function: '");
node_print(*func_node);
printf("'\n");
return NULL;
Node func_node = {0};
int ret = eval_one(head, scopes, &func_node);
if (ret != 0) {
return ret;
}
if (func_node.kind == NODE_KIND_FUNCTION) {
func_node.as_function.apply(args, scopes, result);
return 0;
}
printf("Not a function: '");
node_print(func_node);
printf("'\n");
return 1;
}
default:
UNREACHABLE("");
}
return NULL;
return 0;
}
int eval(Nodes *inputs, Scopes *scopes, Nodes *results) {
nob_da_foreach(Node, input, inputs) {
Node result = {0};
int ret = 0;
if ((ret = eval_one(*input, scopes, &result)) != 0) {
return ret;
}
nob_da_append(results, result);
}
return 0;
}