diff --git a/builtins.c b/builtins.c index f70887e..ef1c667 100644 --- a/builtins.c +++ b/builtins.c @@ -26,17 +26,17 @@ #define func_arithmetic(name, operator) \ func_arithmetic_ex(name, operator, #operator) -func_arithmetic(lisp_function_add, +); -func_arithmetic(lisp_function_sub, -); -func_arithmetic(lisp_function_div, /); -func_arithmetic(lisp_function_mul, *); -func_arithmetic_ex(lisp_function_eq, ==, "="); -func_arithmetic(lisp_function_lt, <); -func_arithmetic(lisp_function_gt, >); -func_arithmetic(lisp_function_lt_eq, <=); -func_arithmetic(lisp_function_gt_eq, >=); +func_arithmetic(builtin_add, +); +func_arithmetic(builtin_sub, -); +func_arithmetic(builtin_div, /); +func_arithmetic(builtin_mul, *); +func_arithmetic_ex(builtin_eq, ==, "="); +func_arithmetic(builtin_lt, <); +func_arithmetic(builtin_gt, >); +func_arithmetic(builtin_lt_eq, <=); +func_arithmetic(builtin_gt_eq, >=); -int lisp_function_println(Nodes args, Scopes *scopes, Node *result) { +int builtin_println(Nodes args, Scopes *scopes, Node *result) { if (args.count != 1) { printf("'println' expects one argument!\n"); return 1; @@ -52,7 +52,7 @@ int lisp_function_println(Nodes args, Scopes *scopes, Node *result) { return 0; } -int lisp_function_list(Nodes args, Scopes *scopes, Node *result) { +int builtin_list(Nodes args, Scopes *scopes, Node *result) { UNUSED(scopes); Nodes *nodes = malloc(sizeof(Nodes)); @@ -68,7 +68,7 @@ int lisp_function_list(Nodes args, Scopes *scopes, Node *result) { return 0; } -int lisp_function_append(Nodes args, Scopes *scopes, Node *result) { +int builtin_append(Nodes args, Scopes *scopes, Node *result) { UNUSED(scopes); if (args.count != 2) { @@ -100,7 +100,7 @@ int lisp_function_append(Nodes args, Scopes *scopes, Node *result) { return 0; } -int lisp_function_length(Nodes args, Scopes *scopes, Node *result) { +int builtin_length(Nodes args, Scopes *scopes, Node *result) { UNUSED(scopes); if (args.count != 1) { @@ -118,7 +118,7 @@ int lisp_function_length(Nodes args, Scopes *scopes, Node *result) { return 0; } -int lisp_function_isnull(Nodes args, Scopes *scopes, Node *result) { +int builtin_isnull(Nodes args, Scopes *scopes, Node *result) { UNUSED(scopes); if (args.count != 1) { @@ -136,7 +136,7 @@ int lisp_function_isnull(Nodes args, Scopes *scopes, Node *result) { return 0; } -int lisp_function_builtins(Nodes args, Scopes *scopes, Node *result) { +int builtin_builtins(Nodes args, Scopes *scopes, Node *result) { if (args.count != 0) { printf("'builtins' expects no arguments!\n"); return 1; @@ -158,7 +158,7 @@ int lisp_function_builtins(Nodes args, Scopes *scopes, Node *result) { return 0; } -int lisp_function_floor(Nodes args, Scopes *scopes, Node *result) { +int builtin_floor(Nodes args, Scopes *scopes, Node *result) { UNUSED(scopes); if (args.count != 1) { @@ -176,7 +176,7 @@ int lisp_function_floor(Nodes args, Scopes *scopes, Node *result) { return 0; } -int lisp_function_ceil(Nodes args, Scopes *scopes, Node *result) { +int builtin_ceil(Nodes args, Scopes *scopes, Node *result) { UNUSED(scopes); if (args.count != 1) { diff --git a/lisp.c b/lisp.c index c4af421..1ee4edc 100644 --- a/lisp.c +++ b/lisp.c @@ -1,6 +1,19 @@ #include "lisp.h" static Scope global_scope = {0}; +static LispFunction special_forms[] = { + { + .name = (Nob_String_View){.count = 2, .data = "if"}, + .apply = special_form_if, + }, + {.name = (Nob_String_View){.count = 6, .data = "define"}, + .apply = special_form_define}, + {.name = (Nob_String_View){.count = 3, .data = "let"}, + .apply = special_form_let}, + { + .name = (Nob_String_View){.count = 6, .data = "lambda"}, + .apply = special_form_lambda, + }}; static int scope_lookup(Scopes *scopes, Nob_String_View name, Node *result) { nob_da_foreach(Scope *, scope, scopes) { @@ -17,7 +30,7 @@ static int scope_lookup(Scopes *scopes, Nob_String_View name, Node *result) { } static void scope_add_function(Scope *scope, const char *name, - LispFunction function) { + LispFunctionPtr function) { Nob_String_View obj_name = nob_sv_from_cstr(name); ScopeObject obj = (ScopeObject){ .name = obj_name, @@ -26,37 +39,26 @@ static void scope_add_function(Scope *scope, const char *name, 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) { - 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_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(&global_scope, "+", builtin_add); + scope_add_function(&global_scope, "-", builtin_sub); + scope_add_function(&global_scope, "/", builtin_div); + scope_add_function(&global_scope, "*", builtin_mul); + scope_add_function(&global_scope, "=", builtin_eq); + scope_add_function(&global_scope, "<", builtin_lt); + scope_add_function(&global_scope, ">", builtin_gt); + scope_add_function(&global_scope, "<=", builtin_lt_eq); + scope_add_function(&global_scope, ">=", builtin_gt_eq); - 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); + scope_add_function(&global_scope, "println", builtin_println); + scope_add_function(&global_scope, "list", builtin_list); + scope_add_function(&global_scope, "append", builtin_append); + scope_add_function(&global_scope, "length", builtin_length); + scope_add_function(&global_scope, "null?", builtin_isnull); + scope_add_function(&global_scope, "builtins", builtin_builtins); - scope_add_function(&global_scope, "floor", lisp_function_floor); - scope_add_function(&global_scope, "ceil", lisp_function_ceil); + scope_add_function(&global_scope, "floor", builtin_floor); + scope_add_function(&global_scope, "ceil", builtin_ceil); nob_da_append(scopes, &global_scope); } @@ -305,168 +307,16 @@ int eval_one(Node input, Scopes *scopes, Node *result) { Node head_node = children->items[0]; if (head_node.kind == NODE_KIND_SYMBOL) { - if (nob_sv_eq(head_node.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_node.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, &global_scope) { - 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(&global_scope, obj); - *result = children->items[0 + 1]; - return 0; - } - - if (nob_sv_eq(head_node.as_symbol, - (Nob_String_View){.count = 3, .data = "let"})) { - if (children->count != 2 + 1) { - printf("'let' expects two arguments!\n"); - return 1; - } - - Node definitions = children->items[0 + 1]; - if (definitions.kind != NODE_KIND_LIST) { - printf("First argument of 'let' must be a list!\n"); - return 1; - } - - Node expression = children->items[1 + 1]; - if (expression.kind != NODE_KIND_LIST) { - printf("Second argument of 'let' must be a list!\n"); - return 1; - } - - // it's fine to allocate this on the stack because we will throw - // it off the scope stack after parsing 'let' anyways therefore - // it'll outlive the recursive call - Scope scope = {0}; - - nob_da_foreach(Node, def, definitions.as_list) { - if (def->kind != NODE_KIND_LIST || - def->as_list->count != 2 || - def->as_list->items[0].kind != NODE_KIND_SYMBOL) { - printf("'let' expects a list of key-value pairs, e.g. " - "'(let ((x 42) (y 2)) (+ x y))'\n"); - return 1; - } - - Node key = def->as_list->items[0]; - Node value_node = def->as_list->items[1]; - - Node value = {0}; - int ret = eval_one(value_node, scopes, &value); + for (size_t i = 0; i < NOB_ARRAY_LEN(special_forms); i++) { + LispFunction special_form = special_forms[i]; + if (nob_sv_eq(head_node.as_symbol, special_form.name)) { + Nodes args = {.count = children->count - 1, + .items = children->items + 1}; + int ret = special_form.apply(args, scopes, result); if (ret != 0) - return ret; - - ScopeObject obj = - (ScopeObject){.name = key.as_symbol, .node = value}; - nob_da_append(&scope, obj); - } - - Node expression_value = {0}; - { - nob_da_append(scopes, &scope); - - int ret = eval_one(expression, scopes, &expression_value); - if (ret != 0) { - return ret; - } - - UNUSED(nob_da_pop(scopes)); - } - - *result = expression_value; - 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; - } + return 0; } - - 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; } } diff --git a/lisp.h b/lisp.h index 888268b..95c22e8 100644 --- a/lisp.h +++ b/lisp.h @@ -35,7 +35,12 @@ typedef struct s_Scopes Scopes; // args is specifically not a pointer, // functions shouldn't be able to mess with their arguments -typedef int (*LispFunction)(Nodes args, Scopes *scopes, Node *result); +typedef int (*LispFunctionPtr)(Nodes args, Scopes *scopes, Node *result); + +typedef struct s_LispFunction { + Nob_String_View name; + LispFunctionPtr apply; +} LispFunction; struct s_Node { NodeKind kind; @@ -43,10 +48,7 @@ struct s_Node { Nob_String_View as_symbol; Nodes *as_list; double as_number; - struct { - Nob_String_View name; - LispFunction apply; - } as_function; + LispFunction as_function; struct { Nodes *params; Node *body; @@ -90,26 +92,40 @@ int parse(Tokens *tokens, Nodes *nodes); int eval_one(Node input, Scopes *scopes, Node *result); int eval(Nodes inputs, Scopes *scopes, Nodes *results); +/* + special forms and builtins follow the function signature defined by + LispFunctionPtr + + The arguments for builtins will be evaluated before calling them, + that is not the case for special forms. + */ + +// special forms +int special_form_if(Nodes args, Scopes *scopes, Node *result); +int special_form_define(Nodes args, Scopes *scopes, Node *result); +int special_form_let(Nodes args, Scopes *scopes, Node *result); +int special_form_lambda(Nodes args, Scopes *scopes, Node *result); + // builtins -int lisp_function_add(Nodes args, Scopes *scopes, Node *result); -int lisp_function_sub(Nodes args, Scopes *scope, Node *result); -int lisp_function_div(Nodes args, Scopes *scope, Node *result); -int lisp_function_mul(Nodes args, Scopes *scope, Node *result); +int builtin_add(Nodes args, Scopes *scopes, Node *result); +int builtin_sub(Nodes args, Scopes *scope, Node *result); +int builtin_div(Nodes args, Scopes *scope, Node *result); +int builtin_mul(Nodes args, Scopes *scope, Node *result); -int lisp_function_eq(Nodes args, Scopes *scope, Node *result); -int lisp_function_lt(Nodes args, Scopes *scope, Node *result); -int lisp_function_gt(Nodes args, Scopes *scope, Node *result); -int lisp_function_lt_eq(Nodes args, Scopes *scope, Node *result); -int lisp_function_gt_eq(Nodes args, Scopes *scope, Node *result); +int builtin_eq(Nodes args, Scopes *scope, Node *result); +int builtin_lt(Nodes args, Scopes *scope, Node *result); +int builtin_gt(Nodes args, Scopes *scope, Node *result); +int builtin_lt_eq(Nodes args, Scopes *scope, Node *result); +int builtin_gt_eq(Nodes args, Scopes *scope, Node *result); -int lisp_function_println(Nodes args, Scopes *scopes, Node *result); -int lisp_function_list(Nodes args, Scopes *scopes, Node *result); -int lisp_function_append(Nodes args, Scopes *scope, Node *result); -int lisp_function_length(Nodes args, Scopes *scope, Node *result); -int lisp_function_isnull(Nodes args, Scopes *scope, Node *result); -int lisp_function_builtins(Nodes args, Scopes *scopes, Node *result); +int builtin_println(Nodes args, Scopes *scopes, Node *result); +int builtin_list(Nodes args, Scopes *scopes, Node *result); +int builtin_append(Nodes args, Scopes *scope, Node *result); +int builtin_length(Nodes args, Scopes *scope, Node *result); +int builtin_isnull(Nodes args, Scopes *scope, Node *result); +int builtin_builtins(Nodes args, Scopes *scopes, Node *result); -int lisp_function_floor(Nodes args, Scopes *scopes, Node *result); -int lisp_function_ceil(Nodes args, Scopes *scopes, Node *result); +int builtin_floor(Nodes args, Scopes *scopes, Node *result); +int builtin_ceil(Nodes args, Scopes *scopes, Node *result); #endif // LISP_H diff --git a/special_forms.c b/special_forms.c new file mode 100644 index 0000000..a761fd1 --- /dev/null +++ b/special_forms.c @@ -0,0 +1,170 @@ +#include "lisp.h" + +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; + } +} + +int special_form_if(Nodes args, Scopes *scopes, Node *result) { + if (args.count != 3) { + printf("'if' expects three arguments!\n"); + return 1; + } + + Node condition = {0}; + int ret = eval_one(args.items[0 + 1], scopes, &condition); + if (ret != 0) { + return ret; + } + + if (node_is_true(condition)) { + ret = eval_one(args.items[1], scopes, result); + } else { + ret = eval_one(args.items[2], scopes, result); + } + return ret; +} + +int special_form_define(Nodes args, Scopes *scopes, Node *result) { + if (args.count != 2) { + printf("'define' expects two arguments!\n"); + return 1; + } + Node name_node = args.items[0]; + 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(args.items[1], scopes, &value); + if (ret != 0) { + return ret; + } + + Scope *global_scope = nob_da_first(scopes); + nob_da_foreach(ScopeObject, scope_object, global_scope) { + if (nob_sv_eq(scope_object->name, name_node.as_symbol)) { + scope_object->node = value; + *result = args.items[0]; + return 0; + } + } + + ScopeObject obj = (ScopeObject){.name = name_node.as_symbol, .node = value}; + nob_da_append(global_scope, obj); + *result = args.items[0]; + return 0; +} + +int special_form_let(Nodes args, Scopes *scopes, Node *result) { + if (args.count != 2) { + printf("'let' expects two arguments!\n"); + return 1; + } + + Node definitions = args.items[0]; + if (definitions.kind != NODE_KIND_LIST) { + printf("First argument of 'let' must be a list!\n"); + return 1; + } + + Node expression = args.items[1]; + if (expression.kind != NODE_KIND_LIST) { + printf("Second argument of 'let' must be a list!\n"); + return 1; + } + + // it's fine to allocate this on the stack because we will throw + // it off the scope stack after parsing 'let' anyways therefore + // it'll outlive the recursive call + Scope scope = {0}; + + nob_da_foreach(Node, def, definitions.as_list) { + if (def->kind != NODE_KIND_LIST || def->as_list->count != 2 || + def->as_list->items[0].kind != NODE_KIND_SYMBOL) { + printf("'let' expects a list of key-value pairs, e.g. " + "'(let ((x 42) (y 2)) (+ x y))'\n"); + return 1; + } + + Node key = def->as_list->items[0]; + Node value_node = def->as_list->items[1]; + + Node value = {0}; + int ret = eval_one(value_node, scopes, &value); + if (ret != 0) + return ret; + + ScopeObject obj = (ScopeObject){.name = key.as_symbol, .node = value}; + nob_da_append(&scope, obj); + } + + Node expression_value = {0}; + { + nob_da_append(scopes, &scope); + + int ret = eval_one(expression, scopes, &expression_value); + if (ret != 0) { + return ret; + } + + UNUSED(nob_da_pop(scopes)); + } + + *result = expression_value; + return 0; +} + +int special_form_lambda(Nodes args, Scopes *scopes, Node *result) { + UNUSED(scopes); + + if (args.count != 2) { + printf("'lambda' expects two argument!\n"); + return 1; + } + + Node params = args.items[0]; + 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, &args.items[1], sizeof(Node)); + result->as_lambda.body = body; + return 0; +}