#include "lisp.h" static Scope globalScope = {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; return 0; } } } printf("Undefined symbol: '" SV_Fmt "'!\n", SV_Arg(name)); return 1; } static void scope_add_function(Scope *scope, const char *name, LispFunction function) { Nob_String_View obj_name = nob_sv_from_cstr(name); ScopeObject obj = (ScopeObject){ .name = obj_name, .node = (Node){.kind = NODE_KIND_FUNCTION, .as_function = {.name = obj_name, .apply = function}}}; nob_da_append(scope, obj); } static bool node_is_true(Node node) { switch (node.kind) { case NODE_KIND_NUMBER: return node.as_number != 0; case NODE_KIND_LIST: return node.as_list->count > 0; default: return true; } } 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(&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); // clang-format on nob_da_append(scopes, &globalScope); } const char *token_kind_name(TokenKind kind) { switch (kind) { case TOKEN_KIND_PAR_LEFT: return "PAR_LEFT"; case TOKEN_KIND_PAR_RIGHT: return "PAR_RIGHT"; case TOKEN_KIND_NUMBER: return "NUMBER"; case TOKEN_KIND_SYMBOL: return "SYMBOL"; default: UNREACHABLE("Unknown token type"); } } void node_print(Node node) { switch (node.kind) { case NODE_KIND_NUMBER: { printf("%lld", node.as_number); } break; case NODE_KIND_SYMBOL: { printf(SV_Fmt, SV_Arg(node.as_symbol)); } break; case NODE_KIND_LIST: { Nodes *children = node.as_list; printf("("); for (size_t i = 0; i < children->count; i++) { Node node = children->items[i]; node_print(node); if (i != children->count - 1) { printf(" "); } } printf(")"); } break; case NODE_KIND_FUNCTION: { Nob_String_View func_name = node.as_function.name; printf(SV_Fmt, SV_Arg(func_name)); } break; } } void tokenize(Nob_String_View content, Tokens *tokens) { for (size_t i = 0; i < content.count; i++) { char c = content.data[i]; int kind = -1; switch (c) { case '(': { kind = TOKEN_KIND_PAR_LEFT; } break; case ')': { kind = TOKEN_KIND_PAR_RIGHT; } break; } if (kind != -1) { Token tok = (Token){.kind = kind, .data = {0}}; nob_da_append(tokens, tok); continue; } Nob_String_Builder sb = {0}; while (isdigit(c)) { nob_sb_appendf(&sb, "%c", c); c = content.data[++i]; } if (sb.count > 0) { i--; Token tok = (Token){.kind = TOKEN_KIND_NUMBER, .data = nob_sb_to_sv(sb)}; nob_da_append(tokens, tok); continue; } // symbols shouldn't start with a digit bool condition = isascii(c) && !isdigit(c) && !isblank(c) && !iscntrl(c) && c != '(' && c != ')'; while (condition) { nob_sb_appendf(&sb, "%c", c); c = content.data[++i]; condition = isascii(c) && !isblank(c) && !iscntrl(c) && c != '(' && c != ')'; } if (sb.count > 0) { i--; Token tok = (Token){.kind = TOKEN_KIND_SYMBOL, .data = nob_sb_to_sv(sb)}; nob_da_append(tokens, tok); } } } int parse_one(Tokens *tokens, Node *node) { if (tokens == NULL || node == NULL || tokens->count == 0) { printf("unexpected end of input"); return 1; } Token tok = tokens->items[0]; tokens->items++; tokens->count--; 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 1; } if (tokens->items[0].kind == TOKEN_KIND_PAR_RIGHT) { tokens->items++; tokens->count--; if (tokens->count > 0) { } break; } parse_one(tokens, node); nob_da_append(nodes, *node); } node->kind = NODE_KIND_LIST; node->as_list = nodes; } break; case TOKEN_KIND_PAR_RIGHT: { printf("unexpected ')'"); return 1; } case TOKEN_KIND_NUMBER: { long long num = strtoll(tok.data.data, NULL, 10); // TODO: // - error checking // - double support node->kind = NODE_KIND_NUMBER; node->as_number = num; } break; case TOKEN_KIND_SYMBOL: { node->kind = NODE_KIND_SYMBOL; node->as_symbol = tok.data; } break; default: { return 1; } } return 0; } int parse(Tokens *tokens, Nodes *nodes) { if (tokens == NULL || nodes == NULL) return 1; struct { size_t *items; size_t count; size_t capacity; } stack = {0}; size_t last_idx = 0; for (size_t i = 0; i < tokens->count; i++) { TokenKind kind = tokens->items[i].kind; if (kind == TOKEN_KIND_PAR_LEFT) nob_da_append(&stack, i); else if (kind == TOKEN_KIND_PAR_RIGHT) last_idx = nob_da_pop(&stack); if (stack.count == 0) { size_t sub_size = tokens->count - last_idx; Tokens toks = {.count = sub_size, .capacity = sub_size, .items = tokens->items + last_idx}; printf("Parsing:\n"); nob_da_foreach(Token, token, &toks) { printf(" %s\n", token_kind_name(token->kind)); } Node result = {0}; int ret = parse_one(&toks, &result); if (ret != 0) { return ret; } nob_da_append(nodes, result); } } return 0; } int eval_one(Node input, Scopes *scopes, Node *result) { if (scopes == NULL || result == NULL) return 1; switch (input.kind) { case NODE_KIND_NUMBER: *result = input; return 0; case NODE_KIND_SYMBOL: { return scope_lookup(scopes, input.as_symbol, result); } case NODE_KIND_LIST: { Nodes *children = input.as_list; if (children->count == 0) { *result = input; return 0; } 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}; int ret = eval_one(children->items[0 + 1], scopes, &condition); if (ret != 0) { return ret; } if (node_is_true(condition)) { ret = eval_one(children->items[1 + 1], scopes, result); } else { ret = eval_one(children->items[2 + 1], scopes, result); } return ret; } 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}; int ret = eval_one(children->items[1 + 1], scopes, &value); if (ret != 0) { return ret; } 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++) { Node n = {0}; int ret = eval_one(children->items[i], scopes, &n); if (ret != 0) { return ret; } nob_da_append(args, n); } 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: return 1; } } int eval(Nodes inputs, Scopes *scopes, Nodes *results) { nob_da_foreach(Node, input, &inputs) { Node result = {0}; int ret = eval_one(*input, scopes, &result); if (ret != 0) { return ret; } nob_da_append(results, result); } return 0; }