]> Untitled Git - axy/ft/c-cera.git/commitdiff
Simple expr checking of right shape
authorAxy <gilliardmarthey.axel@gmail.com>
Mon, 4 May 2026 02:13:24 +0000 (04:13 +0200)
committerAxy <gilliardmarthey.axel@gmail.com>
Mon, 4 May 2026 02:13:24 +0000 (04:13 +0200)
src/ccera.h
src/eval.c
src/value_lifetime.c
test.c

index 3445dd21441478f57b9540d4a61b4fd708c14491..9465c6eda34e9046770a105a5b1864fc2c37c7f6 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: agilliar <agilliar@student.42mulhouse.fr>  +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2026/04/29 16:08:57 by agilliar          #+#    #+#             */
-/*   Updated: 2026/05/04 02:35:51 by agilliar         ###   ########.fr       */
+/*   Updated: 2026/05/04 03:58:26 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -49,66 +49,59 @@ enum e_value_ptr: t_usize
        VALUE_PTR_BYTES,
 };
 
-struct s_value_builtin
+typedef struct s_value_builtin
 {
        enum e_value_ptr        discrim;
        t_builtin                       builtin;
-};
+}      t_value_builtin;
 
-struct s_value_list
+typedef struct s_value_list
 {
        t_usize                         len;
        enum e_value_ptr        discrim;
        t_value                         list[0];
-};
+}      t_value_list;
 
-struct s_value_bytes
+typedef struct s_value_bytes
 {
        t_usize                         len;
        enum e_value_ptr        discrim;
        char                            bytes[0];
-};
+}      t_value_bytes;
 
-t_value                                        value_builtin_new(t_builtin builtin);
-t_value                                        value_list_new(t_usize len);
-t_value                                        value_bytes_new(t_usize len);
-t_value                                        value_undefined_new(void);
-t_value                                        value_int_new(t_isize n);
+t_value                        value_builtin_new(t_builtin builtin);
+t_value                        value_list_new(t_usize len);
+t_value                        value_bytes_new(t_usize len);
+t_value                        value_undefined_new(void);
+t_value                        value_int_new(t_isize n);
 
-struct s_value_builtin *value_builtin_get(t_value value);
-struct s_value_list            *value_list_get(t_value value);
-struct s_value_bytes   *value_bytes_get(t_value value);
-t_isize                                        value_int_get(t_value value);
+t_value_builtin        *value_builtin_get(t_value value);
+t_value_list   *value_list_get(t_value value);
+t_value_bytes  *value_bytes_get(t_value value);
+t_isize                        value_int_get(t_value value);
 
-void                                   value_destroy(t_value value);
+void                   value_destroy(t_value value);
 
-t_value                                        value_copy(t_value value);
-struct s_value_list            *value_list_unique(t_value *value);
-struct s_value_bytes   *value_bytes_unique(t_value *value);
+t_value                        value_copy(t_value value);
+t_value_list   *value_list_unique(t_value *value);
+t_value_bytes  *value_bytes_unique(t_value *value);
 
-t_value                                        value_list_new(t_usize len);
-t_value                                        value_list_new(t_usize len);
+t_value                        value_list_new(t_usize len);
+t_value                        value_list_new(t_usize len);
 
 typedef void                   *t_arc;
 typedef void                   *(*t_unpack)(t_value);
 typedef void                   (*t_drop)(void *);
 
-t_arc                                  arc_create(t_usize size);
-t_arc                                  arc_copy(t_arc arc);
-void                                   arc_destroy(t_arc arc, void (*destructor)(void *));
-bool                                   arc_is_unique(t_arc arc);
+t_arc                  arc_create(t_usize size);
+t_arc                  arc_copy(t_arc arc);
+void                   arc_destroy(t_arc arc, void (*destructor)(void *));
+bool                   arc_is_unique(t_arc arc);
 
-void                                   noop(void *ptr);
+void                   noop(void *ptr);
 
 struct                                 s_exec_context;
 
-typedef struct s_eval_ctx
-{
-       struct s_eval_ctx       *parent;
-       struct s_value_list     *state;
-       t_usize                         pos;
-}      t_eval_ctx;
-
 // The expression value is a tuple of an evaluatable and the argument to give it
 //
 // Value-evaluatables are laid out as follows:
@@ -127,6 +120,6 @@ typedef struct s_eval_ctx
 // move is whether said value shall be moved out
 //
 // This function takes ownership
-t_value                                        expr_eval(t_value expr);
+t_value                        expr_eval(t_value expr);
 
 #endif
index 98e5dd914cfb13f0c6036d760ade6c3ca2a3c715..597e6ceda1fd791194edd42b8377f34536ce9cb7 100644 (file)
@@ -6,27 +6,32 @@
 /*   By: agilliar <agilliar@student.42mulhouse.fr>  +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2026/05/04 01:21:34 by agilliar          #+#    #+#             */
-/*   Updated: 2026/05/04 02:51:20 by agilliar         ###   ########.fr       */
+/*   Updated: 2026/05/04 04:12:32 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "ccera.h"
 #include <stdlib.h>
 
-static t_value ctx_eval(t_eval_ctx *ctx)
+static bool    to_state(t_value *value)
 {
-       (void) ctx;
-       return (value_undefined_new());
+       t_value_list    *expr;
+       t_value_list    *state;
+
+       expr = value_list_unique(value);
+       if (!expr || expr->len != 2)
+               return (false);
+       state = value_list_unique(&expr->list[0]);
+       if (!state || state->len != 2)
+               return (false);
+       if (!value_list_unique(&state->list[0]))
+               return (false);
+       return (true);
 }
 
 t_value        expr_eval(t_value expr)
 {
-       t_eval_ctx      *ctx;
-
-       (void) expr;
-       ctx = malloc(sizeof(t_eval_ctx));
-       if (!ctx)
-               return (value_undefined_new());
-       ctx->parent = NULL;
-       return (ctx_eval(ctx));
+       to_state(&expr);
+       value_destroy(expr);
+       return (value_undefined_new());
 }
index 77b872518e0817ceef18e258d31106248b2d8077..c5ad951280b47e404f073e2a1d8783f9a3d361f3 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: agilliar <agilliar@student.42mulhouse.fr>  +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2026/05/04 02:05:48 by agilliar          #+#    #+#             */
-/*   Updated: 2026/05/04 03:21:18 by agilliar         ###   ########.fr       */
+/*   Updated: 2026/05/04 03:27:11 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -39,12 +39,12 @@ t_value     value_copy(t_value value)
        return (value_int_new(value_int_get(value)));
 }
 
-struct s_value_list    *value_list_unique(t_value *value)
+t_value_list   *value_list_unique(t_value *value)
 {
-       struct s_value_list     *list;
-       t_value                         new;
-       struct s_value_list     *new_list;
-       t_usize                         i;
+       t_value_list    *list;
+       t_value                 new;
+       t_value_list    *new_list;
+       t_usize                 i;
 
        list = value_list_get(*value);
        if (!list)
@@ -66,11 +66,11 @@ struct s_value_list *value_list_unique(t_value *value)
        return (new_list);
 }
 
-struct s_value_bytes   *value_bytes_unique(t_value *value)
+t_value_bytes  *value_bytes_unique(t_value *value)
 {
-       struct s_value_bytes    *bytes;
-       t_value                                 new;
-       struct s_value_bytes    *new_bytes;
+       t_value_bytes   *bytes;
+       t_value                 new;
+       t_value_bytes   *new_bytes;
 
        bytes = value_bytes_get(*value);
        if (!bytes)
diff --git a/test.c b/test.c
index a45042f1f3affc5a864b99375cda38d4f5125dac..e15714d0ed9b2814f1524905bb9192f2030d82f3 100644 (file)
--- a/test.c
+++ b/test.c
@@ -6,7 +6,7 @@
 /*   By: agilliar <agilliar@student.42mulhouse.fr>  +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2026/04/30 17:19:58 by agilliar          #+#    #+#             */
-/*   Updated: 2026/05/04 03:20:35 by agilliar         ###   ########.fr       */
+/*   Updated: 2026/05/04 04:13:07 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 int    main(void)
 {
        char    *s = "Hello cera!";
-       t_value value = value_bytes_new(strlen(s) + 1);
-       strcpy(&value_bytes_get(value)->bytes[0], s);
-       printf("%s\n", &value_bytes_get(value)->bytes[0]);
-       printf("%p\n", value_bytes_get(value));
-       printf("%p\n", value_list_get(value));
-       char    c = value_bytes_get(value)->bytes[1];
-       (void)  c;
-       t_value value_2 = value_copy(value);
-       value_bytes_unique(&value_2)->bytes[1] = 'f';
-       printf("%s\n", &value_bytes_get(value_2)->bytes[0]);
-       printf("%s\n", &value_bytes_get(value)->bytes[0]);
-       value_destroy(value);
-       value_destroy(value_2);
-       value = value_int_new(128);
-       printf("%li\n", value_int_get(value));
+       t_value arg = value_bytes_new(strlen(s) + 1);
+       t_value state = value_list_new(0);
+       t_value idx = value_int_new(0);
+       t_value fn = value_list_new(2);
+       t_value expr = value_list_new(2);
+       value_list_get(fn)->list[0] = state;
+       value_list_get(fn)->list[1] = idx;
+       value_list_get(expr)->list[0] = fn;
+       value_list_get(expr)->list[1] = arg;
+       t_value val = value_copy(expr);
+       printf("%p\n", expr_eval(expr).ptr);
+       value_destroy(val);
 }