commit cbe9c07
uint
·
2026-10-24 15:17:48 +0000 UTC
parent 14adec8
Add natives
M
expl.c
+36,
-12
1@@ -15,13 +15,14 @@ typedef struct environment environment;
2 typedef struct parser parser;
3
4 struct function {
5- expl_node* args;
6- expl_node* body;
7+ expl_native native;
8+ expl_node* args;
9+ expl_node* body;
10 };
11
12 struct environment {
13 char* name;
14- expl_node* value;
15+ expl_value value;
16 environment* next;
17 };
18
19@@ -31,14 +32,15 @@ struct parser {
20 expl_error err;
21 };
22
23-static int define(environment** env, const char* name, expl_node* value);
24+static int define(environment** env, const char* name, expl_value value);
25 static expl_node* expression(parser* p);
26 static expl_node* list(parser* p);
27-static expl_node* lookup(environment* env, const char* name);
28+static expl_value* lookup(environment* env, const char* name);
29 static expl_node* value(parser* p, expl_type type);
30
31-static expl_node* eval(expl_node* n, environment** env);
32-static expl_node* eval_let(expl_node* args, environment** env);
33+static expl_value eval(expl_node* n, environment** env);
34+static expl_value eval_call(expl_node* args, environment** env);
35+static int register_native(environment** env, const char* name, expl_native fn);
36
37 static void skip_space(parser* p);
38 static char* _strndup(const char* src, size_t len);
39@@ -49,7 +51,7 @@ static void free_node(expl_node* node);
40 static void free_environment(environment* env);
41
42 static int
43-define(environment** env, const char* name, expl_node* value)
44+define(environment** env, const char* name, expl_value value)
45 {
46 environment* b = malloc(sizeof(*b));
47 if (!b)
48@@ -124,12 +126,12 @@ list(parser* p)
49 return node;
50 }
51
52-static expl_node*
53+static expl_value*
54 lookup(environment* env, const char* name)
55 {
56 while (env) {
57 if (strcmp(env->name, name) == 0)
58- return env->value;
59+ return &env->value;
60 env = env->next;
61 }
62
63@@ -192,8 +194,8 @@ value(parser* p, expl_type type)
64 return node;
65 }
66
67-static expl_node*
68-evaluate(expl_node* n, environment** env)
69+static expl_value
70+eval(expl_node* n, environment** env)
71 {
72 char* end = NULL;
73
74@@ -223,6 +225,28 @@ evaluate(expl_node* n, environment** env)
75 static expl_node*
76 eval_let(expl_node* args, environment** env)
77 {
78+static int
79+register_native(environment** env, const char* name, expl_native fn)
80+{
81+ function* f = malloc(sizeof(*f));
82+ expl_value value = { 0 };
83+
84+ if (!f)
85+ return 0;
86+
87+ f->native = fn;
88+ f->args = NULL;
89+ f->body = NULL;
90+
91+ value.type = EXPL_VALUE_FUNCTION;
92+ value.as.function = f;
93+
94+ if (!define(env, name, value)) {
95+ free(f);
96+ return 0;
97+ }
98+
99+ return 1;
100 }
101
102 static void
M
expl.h
+3,
-1
1@@ -3,7 +3,7 @@
2
3 #ifndef EXPL_SPACES
4 #define EXPL_SPACES " "
5-#endif
6+#endif /* EXPL_SPACES */
7
8 typedef enum {
9 EXPL_TYPE_ATOM,
10@@ -36,6 +36,8 @@ typedef enum {
11 typedef struct expl_node expl_node;
12 typedef struct expl_value expl_value;
13
14+typedef expl_value (*expl_native)(int argc, const expl_value* argv);
15+
16 struct expl_node {
17 char* value;
18 expl_type type;