refactor: special forms

This commit is contained in:
2026-07-25 00:25:21 +02:00
parent 729f07655a
commit 0e134957af
4 changed files with 263 additions and 227 deletions
+38 -22
View File
@@ -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