diff --git a/builtins.c b/builtins.c index 0b8cded..f3afd8f 100644 --- a/builtins.c +++ b/builtins.c @@ -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"); diff --git a/lisp.c b/lisp.c index 1234bfd..13b3995 100644 --- a/lisp.c +++ b/lisp.c @@ -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; } diff --git a/lisp.h b/lisp.h index 78f71b2..4f13394 100644 --- a/lisp.h +++ b/lisp.h @@ -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); diff --git a/main.c b/main.c index 9d333ae..6a9a9c5 100644 --- a/main.c +++ b/main.c @@ -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++) { diff --git a/test/comments.lisp b/test/comments.lisp new file mode 100644 index 0000000..9d6ecdc --- /dev/null +++ b/test/comments.lisp @@ -0,0 +1,3 @@ +// wadadada + +(list 1 2 3)