commit 2cd0deb

uint  ·  2026-10-24 18:23:28 +0000 UTC
parent 3496656
Remove expl_print, rename expl_node->expression
3 files changed,  +52, -84
M expl.c
M expl.h
+1, -1
1@@ -26,7 +26,7 @@ birds_who_hit_my_window(int argc, const expl_value* argv, void* data)
2 }
3 
4 #define FIND_THE_BIRDS \
5-"(fn foo (x) (birds_who_hit_window x))\n" \
6+"(fn foo (x) (birds_who_hit_my_window x))\n" \
7 "(foo 134)\n"
8 
9 int
M expl.c
+50, -70
  1@@ -10,27 +10,35 @@
  2 #define pos(p) ((p)->pos)
  3 #define peek(p) ((p)->src[(p)->pos + 1])
  4 
  5-typedef struct function function;
  6 typedef struct environment environment;
  7+typedef struct function function;
  8 typedef struct parser parser;
  9 
 10 struct expl {
 11 	environment* env;
 12-	expl_node*   programs;
 13+	expression*  programs;
 14 };
 15 
 16-struct function {
 17-	expl_native native;
 18-	expl_node*  args;
 19-	expl_node*  body;
 20-	void*       data;
 21-};
 22 struct environment {
 23 	char*        name;
 24 	expl_value   value;
 25 	environment* next;
 26 };
 27 
 28+struct function {
 29+	expl_native native;
 30+	expression*  args;
 31+	expression*  body;
 32+	void*       data;
 33+};
 34+
 35+struct expression {
 36+	char*      value;
 37+	expl_type  type;
 38+	expression* child;
 39+	expression* next;
 40+};
 41+
 42 struct parser {
 43 	const char* src;
 44 	size_t      pos;
 45@@ -38,15 +46,15 @@ struct parser {
 46 };
 47 
 48 static int define(environment** env, const char* name, expl_value value);
 49-static expl_node* expression(parser* p);
 50-static expl_node* list(parser* p);
 51+static expression* parse_expression(parser* p);
 52+static expression* list(parser* p);
 53 static expl_value* lookup(environment* env, const char* name);
 54-expl_node* parse(const char* source);
 55-static expl_node* value(parser* p, expl_type type);
 56+expression* parse(const char* source);
 57+static expression* value(parser* p, expl_type type);
 58 
 59-static expl_value eval(expl_node* n, environment** env);
 60-static expl_value eval_call(expl_node* args, environment** env);
 61-static expl_value eval_fn(expl_node* args, environment** env);
 62+static expl_value eval(expression* n, environment** env);
 63+static expl_value eval_call(expression* args, environment** env);
 64+static expl_value eval_fn(expression* args, environment** env);
 65 static expl_value eval_user(function* f, int argc, expl_value* argv, environment** env);
 66 static int register_native(environment** env, const char* name, expl_native fn, void* data);
 67 
 68@@ -54,8 +62,8 @@ static void skip_space(parser* p);
 69 static char* _strndup(const char* src, size_t len);
 70 static void unescape(char* src);
 71 
 72-static expl_node* new_node(expl_type type);
 73-static void free_node(expl_node* node);
 74+static expression* new_node(expl_type type);
 75+static void free_node(expression* node);
 76 static void free_environment(environment* env);
 77 
 78 static int
 79@@ -78,8 +86,8 @@ define(environment** env, const char* name, expl_value value)
 80 	return 1;
 81 }
 82 
 83-static expl_node*
 84-expression(parser* p)
 85+static expression*
 86+parse_expression(parser* p)
 87 {
 88 	skip_space(p);
 89 	switch (p->src[p->pos]) {
 90@@ -96,11 +104,11 @@ expression(parser* p)
 91 	}
 92 }
 93 
 94-static expl_node*
 95+static expression*
 96 list(parser* p)
 97 {
 98-	expl_node* node = NULL;
 99-	expl_node** next = NULL;
100+	expression* node = NULL;
101+	expression** next = NULL;
102 
103 	advance(p);
104 
105@@ -114,7 +122,7 @@ list(parser* p)
106 
107 	skip_space(p);
108 	while (!current_char(p, ')') && !current_char(p, '\0')) {
109-		*next = expression(p);
110+		*next = parse_expression(p);
111 		if (!*next) {
112 			free_node(node);
113 			return NULL;
114@@ -146,12 +154,12 @@ lookup(environment* env, const char* name)
115 	return NULL;
116 }
117 
118-expl_node*
119+expression*
120 parse(const char* source)
121 {
122 	parser p = { 0 };
123-	expl_node* head = NULL;
124-	expl_node** next = &head;
125+	expression* head = NULL;
126+	expression** next = &head;
127 
128 	if (!source)
129 		return NULL;
130@@ -159,7 +167,7 @@ parse(const char* source)
131 
132 	skip_space(&p);
133 	while (!current_char(&p, '\0')) {
134-		*next = expression(&p);
135+		*next = parse_expression(&p);
136 		if (!*next) {
137 			free_node(head);
138 			return NULL;
139@@ -172,10 +180,10 @@ parse(const char* source)
140 	return head;
141 }
142 
143-static expl_node*
144+static expression*
145 value(parser* p, expl_type type)
146 {
147-	expl_node* node = NULL;
148+	expression* node = NULL;
149 	size_t start = 0;
150 
151 	if (type == EXPL_TYPE_STRING)
152@@ -229,7 +237,7 @@ value(parser* p, expl_type type)
153 }
154 
155 static expl_value
156-eval(expl_node* n, environment** env)
157+eval(expression* n, environment** env)
158 {
159 	expl_value result = { 0 };
160 	expl_value* v;
161@@ -265,12 +273,12 @@ eval(expl_node* n, environment** env)
162 }
163 
164 static expl_value
165-eval_call(expl_node* n, environment** env)
166+eval_call(expression* n, environment** env)
167 {
168 	expl_value result = { 0 };
169 	expl_value* fn = NULL;
170 	expl_value* argv = NULL;
171-	expl_node* arg = NULL;
172+	expression* arg = NULL;
173 	function* f = NULL;
174 	int argc = 0;
175 	int i = 0;
176@@ -312,10 +320,10 @@ eval_call(expl_node* n, environment** env)
177 }
178 
179 static expl_value
180-eval_fn(expl_node* args, environment** env)
181+eval_fn(expression* args, environment** env)
182 {
183 	expl_value result = { 0 };
184-	expl_node* name = NULL;
185+	expression* name = NULL;
186 	function* f = NULL;
187 
188 	name = args;
189@@ -347,9 +355,9 @@ static expl_value
190 eval_user(function* f, int argc, expl_value* argv, environment** env)
191 {
192 	environment* local = *env;
193-	expl_node* param = f->args;
194+	expression* param = f->args;
195 	environment* next = NULL;
196-	expl_node* body = NULL;
197+	expression* body = NULL;
198 	expl_value result = { 0 };
199 	int i;
200 
201@@ -457,10 +465,10 @@ unescape(char* src)
202 	*dst = '\0';
203 }
204 
205-static expl_node*
206+static expression*
207 new_node(expl_type type)
208 {
209-	expl_node* node = malloc(sizeof(*node));
210+	expression* node = malloc(sizeof(*node));
211 	if (!node)
212 		return NULL;
213 
214@@ -473,7 +481,7 @@ new_node(expl_type type)
215 }
216 
217 static void
218-free_node(expl_node* node)
219+free_node(expression* node)
220 {
221 	if (!node)
222 		return;
223@@ -530,8 +538,8 @@ expl_value
224 expl_run(expl* ctx, const char* source)
225 {
226 	expl_value result = { 0 };
227-	expl_node* program = NULL;
228-	expl_node* n = NULL;
229+	expression* program = NULL;
230+	expression* n = NULL;
231 
232 	if (!ctx || !source)
233 		return result;
234@@ -555,34 +563,6 @@ expl_run(expl* ctx, const char* source)
235 	return result;
236 }
237 
238-void
239-expl_print(const expl_node* node, int depth)
240-{
241-	int i;
242-
243-	while (node) {
244-		for (i = 0; i < depth; i++)
245-			printf(EXPL_SPACES);
246-
247-		switch (node->type) {
248-		case EXPL_TYPE_ATOM:
249-			printf("ATOM: %s\n", node->value);
250-			break;
251-		case EXPL_TYPE_STRING:
252-			printf("STRING: %s\n", node->value);
253-			break;
254-		case EXPL_TYPE_LIST:
255-			printf("LIST:\n");
256-			expl_print(node->child, depth + 1);
257-			break;
258-		default:
259-			printf("UNKNOWN TYPE\n");
260-		}
261-
262-		node = node->next;
263-	}
264-}
265-
266 #if 1
267 static expl_value
268 birds_who_hit_my_window(int argc, const expl_value* argv, void* data)
269@@ -600,7 +580,7 @@ birds_who_hit_my_window(int argc, const expl_value* argv, void* data)
270 }
271 
272 #define FIND_THE_BIRDS \
273-"(fn foo (x) (birds_who_hit_window x))\n" \
274+"(fn foo (x) (birds_who_hit_my_window x))\n" \
275 "(foo 134)\n"
276 
277 int
M expl.h
+1, -13
 1@@ -1,10 +1,6 @@
 2 #ifndef EXPL_H
 3 #define EXPL_H
 4 
 5-#ifndef EXPL_SPACES
 6-#define EXPL_SPACES "  "
 7-#endif /* EXPL_SPACES */
 8-
 9 typedef enum {
10 	EXPL_TYPE_ATOM,
11 	EXPL_TYPE_STRING,
12@@ -33,19 +29,12 @@ typedef enum {
13 	EXPL_ERR_COUNT
14 } expl_error;
15 
16-typedef struct expl_node expl_node;
17+typedef struct expression expression;
18 typedef struct expl_value expl_value;
19 typedef struct expl expl;
20 
21 typedef expl_value (*expl_native)(int argc, const expl_value* argv, void* data);
22 
23-struct expl_node {
24-	char*      value;
25-	expl_type  type;
26-	expl_node* child;
27-	expl_node* next;
28-};
29-
30 struct expl_value {
31 	expl_value_type type;
32 	union {
33@@ -59,7 +48,6 @@ expl* expl_init(void);
34 void expl_close(expl* ctx);
35 int expl_register(expl* ctx, const char* name, expl_native fn, void* data);
36 expl_value expl_run(expl* ctx, const char* source);
37-void expl_print(const expl_node* node, int depth);
38 
39 #endif /* EXPL_H */
40