commit 14adec8

uint  ·  2026-10-24 14:55:12 +0000 UTC
parent e674d40
Add small evaluator
2 files changed,  +61, -21
M expl.c
M expl.h
M expl.c
+49, -17
  1@@ -10,8 +10,20 @@
  2 #define pos(p) ((p)->pos)
  3 #define peek(p) ((p)->src[(p)->pos + 1])
  4 
  5-typedef struct parser parser;
  6+typedef struct function function;
  7 typedef struct environment environment;
  8+typedef struct parser parser;
  9+
 10+struct function {
 11+	expl_node* args;
 12+	expl_node* body;
 13+};
 14+
 15+struct environment {
 16+	char*        name;
 17+	expl_node*   value;
 18+	environment* next;
 19+};
 20 
 21 struct parser {
 22 	const char* src;
 23@@ -19,19 +31,15 @@ struct parser {
 24 	expl_error  err;
 25 };
 26 
 27-struct environment {
 28-	char*      name;
 29-	expl_node* value;
 30-	environment*   next;
 31-};
 32-
 33 static int define(environment** env, const char* name, expl_node* value);
 34 static expl_node* expression(parser* p);
 35-static expl_node* evaluate(expl_node* n, environment** env);
 36 static expl_node* list(parser* p);
 37 static expl_node* lookup(environment* env, const char* name);
 38 static expl_node* value(parser* p, expl_type type);
 39 
 40+static expl_node* eval(expl_node* n, environment** env);
 41+static expl_node* eval_let(expl_node* args, environment** env);
 42+
 43 static void skip_space(parser* p);
 44 static char* _strndup(const char* src, size_t len);
 45 static void unescape(char* src);
 46@@ -78,15 +86,6 @@ expression(parser* p)
 47 	}
 48 }
 49 
 50-static expl_node*
 51-evaluate(expl_node* n, environment** env)
 52-{
 53-	/* XXX */
 54-	(void) n;
 55-	(void) env;
 56-	return NULL;
 57-}
 58-
 59 static expl_node*
 60 list(parser* p)
 61 {
 62@@ -193,6 +192,39 @@ value(parser* p, expl_type type)
 63 	return node;
 64 }
 65 
 66+static expl_node*
 67+evaluate(expl_node* n, environment** env)
 68+{
 69+	char* end = NULL;
 70+
 71+	if (!n)
 72+		return NULL;
 73+
 74+	switch (n->type) {
 75+	case EXPL_TYPE_STRING:
 76+		return n;
 77+
 78+	case EXPL_TYPE_ATOM:
 79+		strtod(n->value, &end);
 80+		if (end != n->value && !*end)
 81+			return n;
 82+
 83+		return lookup(*env, n->value);
 84+
 85+	case EXPL_TYPE_LIST:
 86+		/* XXX */
 87+		return NULL;
 88+
 89+	default:
 90+		return NULL;
 91+	}
 92+}
 93+
 94+static expl_node*
 95+eval_let(expl_node* args, environment** env)
 96+{
 97+}
 98+
 99 static void
100 skip_space(parser* p)
101 {
M expl.h
+12, -4
 1@@ -15,14 +15,12 @@ typedef enum {
 2 
 3 typedef enum {
 4 	EXPL_VALUE_NIL,
 5-	EXPL_VALUE_INTGER,
 6-	EXPL_VALUE_FLOAT,
 7+	EXPL_VALUE_NUMBER,
 8 	EXPL_VALUE_STRING,
 9 	EXPL_VALUE_FUNCTION,
10-	EXPL_VALUE_NATIVE,
11 
12 	EXPL_VALUE_COUNT
13-} expl_value;
14+} expl_value_type;
15 
16 typedef enum {
17 	EXPL_ERR_NONE,
18@@ -36,6 +34,7 @@ typedef enum {
19 } expl_error;
20 
21 typedef struct expl_node expl_node;
22+typedef struct expl_value expl_value;
23 
24 struct expl_node {
25 	char*      value;
26@@ -44,6 +43,15 @@ struct expl_node {
27 	expl_node* next;
28 };
29 
30+struct expl_value {
31+	expl_value_type type;
32+	union {
33+		double number;
34+		char*  string;
35+		void*  function;
36+	} as;
37+};
38+
39 void expl_print(const expl_node* node, int depth);
40 expl_node* expl_parse(const char* source);
41