refactor and add comment support

This commit is contained in:
2026-07-21 20:58:50 +02:00
parent 3f04adcd6b
commit 62dc0f30eb
5 changed files with 50 additions and 28 deletions
+5 -2
View File
@@ -37,11 +37,14 @@ func_arithmetic(lisp_function_gt_eq, >=);
int lisp_function_println(Nodes args, Scopes *scopes, Node *result) {
if (args.count != 1) {
printf("'+' expects one argument!\n");
printf("'println' expects one argument!\n");
return 1;
}
eval_one(args.items[0], scopes, result);
int ret = eval_one(args.items[0], scopes, result);
if (ret != 0)
return ret;
node_print(*result);
printf("\n");
+39 -24
View File
@@ -1,12 +1,12 @@
#include "lisp.h"
static Scope globalScope = {0};
static Scope global_scope = {0};
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)) {
*result = scopeObject->node;
nob_da_foreach(ScopeObject, scope_object, *scope) {
if (nob_sv_eq(name, scope_object->name)) {
*result = scope_object->node;
return 0;
}
}
@@ -39,26 +39,26 @@ static bool node_is_true(Node node) {
void build_scopes(Scopes *scopes) {
// clang-format off
scope_add_function(&globalScope, "+", lisp_function_add);
scope_add_function(&globalScope, "-", lisp_function_sub);
scope_add_function(&globalScope, "/", lisp_function_div);
scope_add_function(&globalScope, "*", lisp_function_mul);
scope_add_function(&globalScope, "%", lisp_function_mod);
scope_add_function(&globalScope, "=", lisp_function_eq);
scope_add_function(&globalScope, "<", lisp_function_lt);
scope_add_function(&globalScope, ">", lisp_function_gt);
scope_add_function(&globalScope, "<=", lisp_function_lt_eq);
scope_add_function(&globalScope, ">=", lisp_function_gt_eq);
scope_add_function(&global_scope, "+", lisp_function_add);
scope_add_function(&global_scope, "-", lisp_function_sub);
scope_add_function(&global_scope, "/", lisp_function_div);
scope_add_function(&global_scope, "*", lisp_function_mul);
scope_add_function(&global_scope, "%", lisp_function_mod);
scope_add_function(&global_scope, "=", lisp_function_eq);
scope_add_function(&global_scope, "<", lisp_function_lt);
scope_add_function(&global_scope, ">", lisp_function_gt);
scope_add_function(&global_scope, "<=", lisp_function_lt_eq);
scope_add_function(&global_scope, ">=", lisp_function_gt_eq);
scope_add_function(&globalScope, "println", lisp_function_println);
scope_add_function(&globalScope, "list", lisp_function_list);
scope_add_function(&globalScope, "append", lisp_function_append);
scope_add_function(&globalScope, "length", lisp_function_length);
scope_add_function(&globalScope, "null?", lisp_function_isnull);
scope_add_function(&globalScope, "builtins", lisp_function_builtins);
scope_add_function(&global_scope, "println", lisp_function_println);
scope_add_function(&global_scope, "list", lisp_function_list);
scope_add_function(&global_scope, "append", lisp_function_append);
scope_add_function(&global_scope, "length", lisp_function_length);
scope_add_function(&global_scope, "null?", lisp_function_isnull);
scope_add_function(&global_scope, "builtins", lisp_function_builtins);
// clang-format on
nob_da_append(scopes, &globalScope);
nob_da_append(scopes, &global_scope);
}
const char *token_kind_name(TokenKind kind) {
@@ -103,12 +103,26 @@ void node_print(Node node) {
}
}
void tokenize(Nob_String_View content, Tokens *tokens) {
int tokenize(Nob_String_View content, Tokens *tokens) {
bool ignored = false;
for (size_t i = 0; i < content.count; i++) {
char c = content.data[i];
if (ignored) {
if (c == '\n')
ignored = false;
continue;
}
int kind = -1;
switch (c) {
case '/': {
if (i + 1 < content.count && content.items[i + 1] == '/') {
i++;
ignored = true;
continue;
}
} break;
case '(': {
kind = TOKEN_KIND_PAR_LEFT;
} break;
@@ -156,6 +170,7 @@ void tokenize(Nob_String_View content, Tokens *tokens) {
nob_da_append(tokens, tok);
}
}
return 0;
}
int parse_one(Tokens *tokens, Node *node) {
@@ -325,7 +340,7 @@ int eval_one(Node input, Scopes *scopes, Node *result) {
return ret;
}
nob_da_foreach(ScopeObject, scope_object, &globalScope) {
nob_da_foreach(ScopeObject, scope_object, &global_scope) {
if (nob_sv_eq(scope_object->name, name_node.as_symbol)) {
scope_object->node = value;
*result = children->items[0 + 1];
@@ -335,7 +350,7 @@ int eval_one(Node input, Scopes *scopes, Node *result) {
ScopeObject obj =
(ScopeObject){.name = name_node.as_symbol, .node = value};
nob_da_append(&globalScope, obj);
nob_da_append(&global_scope, obj);
*result = children->items[0 + 1];
return 0;
}
+1 -1
View File
@@ -77,7 +77,7 @@ const char *token_kind_name(TokenKind kind);
void node_print(Node node);
void build_scopes(Scopes *scopes);
void tokenize(Nob_String_View content, Tokens *tokens);
int tokenize(Nob_String_View content, Tokens *tokens);
int parse_one(Tokens *tokens, Node *node);
int parse(Tokens *tokens, Nodes *nodes);
+2 -1
View File
@@ -27,7 +27,8 @@ int main(int argc, char **argv) {
}
Tokens tokens = {0};
tokenize(view, &tokens);
if (tokenize(view, &tokens) != 0)
return 1;
#if 0
for (size_t i = 0; i < tokens.count; i++) {
+3
View File
@@ -0,0 +1,3 @@
// wadadada
(list 1 2 3)