Commit 7fadaf12c5a350af9d526be7fe6af26491c14d78

Thomas de Grivel 2024-10-31T18:17:06

fix let and Map in Struct

diff --git a/.ikc3_history b/.ikc3_history
index 69ca913..ea22d00 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,5 +1,3 @@
-quote %HTTP.Request{url: "/plop"}
-quote %HTTP.Request{method: GET, url: "/plop"}
 quote %HTTP.Request{method: GET, url: "/plop", body: void}
 quote [a: 1, b: 2]
 [a: 1, b: 2]
@@ -97,3 +95,5 @@ Crypt.sha512_hash_password("Plop")
 quote if trrue
 quote if true do if false do 1 else %KC3.Operator{sym: :-, symbol_value: 3} end end
 Crypt.sha512("Plop", "$6$rounds=123456$abc0123456789$")
+1 + 1
+1 + 10000000000000000000000000000000000000000000
diff --git a/lib/kc3/0.1/http/request.kc3 b/lib/kc3/0.1/http/request.kc3
index e0cfe64..e840be9 100644
--- a/lib/kc3/0.1/http/request.kc3
+++ b/lib/kc3/0.1/http/request.kc3
@@ -4,7 +4,8 @@ defmodule HTTP.Request do
              body: ?,
              url: "/",
              protocol: "HTTP/1.1",
-             headers: []]
+             headers: [],
+             params: %{}]
 
   dlopen(__DIR__ + "../http.so")
 
diff --git a/lib/kc3/0.1/httpd.kc3 b/lib/kc3/0.1/httpd.kc3
index 40391ad..4c7b78a 100644
--- a/lib/kc3/0.1/httpd.kc3
+++ b/lib/kc3/0.1/httpd.kc3
@@ -25,6 +25,7 @@ defmodule HTTPd do
     if (File.exists?("config/routes.kc3")) do
       load("config/routes.kc3")
     end
+    load_directory("app/plugs/")
   }
 
   def http_client = fn (socket, events, client_ev, client) do
@@ -35,6 +36,7 @@ defmodule HTTPd do
         if (type(request.method) == Str) do
           response = error_405_page(request)
         else
+          request = plug_request(request)
           response = route_and_render_request(request)
         end
         response = HTTP.Response.set_header(response,
@@ -203,6 +205,19 @@ defmodule HTTPd do
     %HTTP.Response{code: 405, body: body}
   }
 
+  def plugs = []
+
+  def def_plug = fn (plug) {
+    tmp = List.append(HTTPd.plugs, plug)
+    def HTTPd.plugs = tmp
+  }
+
+  def plug_request = fn {
+    (request) { plug_request(request, plugs) }
+    (request, []) { request }
+    (request, [plug | rest]) { plug_request(plug(request), rest) }
+  }
+
   def routes = []
 
   def def_route = fn (path, controller) {
@@ -221,7 +236,9 @@ defmodule HTTPd do
     }
     (request, [route | next_routes]) {
       if ((controller = HTTPd.Route.match(route, request)) &&
-          (response = controller(request)))
+          (response = let(request.params, do
+             controller(request)
+           end)))
         response
       else
         route_and_render_request(request, next_routes)
diff --git a/lib/kc3/0.1/kc3.facts b/lib/kc3/0.1/kc3.facts
index c321f50..e965be7 100644
--- a/lib/kc3/0.1/kc3.facts
+++ b/lib/kc3/0.1/kc3.facts
@@ -277,7 +277,7 @@ replace {KC3.access, :symbol_value, cfn Tag "kc3_access" (Tag, List, Result)}
 add {KC3, :symbol, KC3.let}
 replace {KC3.let, :arity, 2}
 replace {KC3.let, :is_a, :special_operator}
-replace {KC3.let, :symbol_value, cfn Tag "kc3_let" (Tag, Block, Result)}
+replace {KC3.let, :symbol_value, cfn Tag "kc3_let" (Tag, Tag, Result)}
 add {KC3, :symbol, KC3.puts}
 replace {KC3.puts, :symbol_value, cfn Sw "kc3_puts" (Tag)}
 add {KC3, :symbol, KC3.load}
diff --git a/libkc3/data.c b/libkc3/data.c
index 6e37efe..78a16d7 100644
--- a/libkc3/data.c
+++ b/libkc3/data.c
@@ -629,6 +629,8 @@ void * data_init_copy (const s_sym *type, void *data, const void *src)
     return integer_init_copy(data, src);
   if (type == &g_sym_List)
     return list_init_copy(data, src);
+  if (type == &g_sym_Map)
+    return map_init_copy(data, src);
   if (type == &g_sym_Ptag)
     return ptag_init_copy(data, src);
   if (type == &g_sym_Ptr)
diff --git a/libkc3/env.c b/libkc3/env.c
index 6601ae5..29e41e3 100644
--- a/libkc3/env.c
+++ b/libkc3/env.c
@@ -2615,7 +2615,7 @@ s_tag * env_kc3_def (s_env *env, const s_call *call, s_tag *dest)
   return dest;
 }
 
-s_tag * env_let (s_env *env, const s_tag *tag, const s_block *block,
+s_tag * env_let (s_env *env, const s_tag *vars, const s_tag *tag,
                  s_tag *dest)
 {
   s_frame frame;
@@ -2623,22 +2623,22 @@ s_tag * env_let (s_env *env, const s_tag *tag, const s_block *block,
   const s_map *map;
   s_tag tmp = {0};
   assert(env);
+  assert(vars);
   assert(tag);
-  assert(block);
   assert(dest);
   if (! frame_init(&frame, env->frame, NULL))
     return NULL;
   env->frame = &frame;
-  if (! env_eval_tag(env, tag, &tmp)) {
+  if (! env_eval_tag(env, vars, &tmp)) {
     env->frame = frame_clean(&frame);
     return NULL;
   }
-  switch(tag->type) {
+  switch(tmp.type) {
   case TAG_MAP:
-    map = &tag->data.map;
+    map = &tmp.data.map;
     break;
   case TAG_STRUCT:
-    map = &tag->data.struct_.type->map;
+    map = &tmp.data.struct_.type->map;
     break;
   default:
     tag_clean(&tmp);
@@ -2671,7 +2671,7 @@ s_tag * env_let (s_env *env, const s_tag *tag, const s_block *block,
     }
     i++;
   }
-  if (! env_eval_block(env, block, dest)) {
+  if (! env_eval_tag(env, tag, dest)) {
     tag_clean(&tmp);
     env->frame = frame_clean(&frame);
     return NULL;
diff --git a/libkc3/env.h b/libkc3/env.h
index 5c98308..8757ef3 100644
--- a/libkc3/env.h
+++ b/libkc3/env.h
@@ -86,8 +86,8 @@ bool *         env_ident_is_special_operator (s_env *env,
                                               bool *dest);
 s_tag *        env_kc3_def (s_env *env, const s_call *call,
                             s_tag *dest);
-s_tag *        env_let (s_env *env, const s_tag *tag,
-                        const s_block *block, s_tag *dest);
+s_tag *        env_let (s_env *env, const s_tag *vars, const s_tag *tag,
+                        s_tag *dest);
 bool           env_load (s_env *env, const s_str *path);
 bool *         env_module_is_loading (s_env *env, const s_sym *module,
                                       bool *dest);
diff --git a/libkc3/kc3.c b/libkc3/kc3.c
index 7334ff9..776c8f1 100644
--- a/libkc3/kc3.c
+++ b/libkc3/kc3.c
@@ -393,9 +393,9 @@ s_tag * kc3_integer_reduce (const s_tag *tag, s_tag *dest)
   return dest;
 }
 
-s_tag * kc3_let (const s_tag *tag, const s_block *block, s_tag *dest)
+s_tag * kc3_let (const s_tag *vars, const s_tag *tag, s_tag *dest)
 {
-  return env_let(&g_kc3_env, tag, block, dest);
+  return env_let(&g_kc3_env, vars, tag, dest);
 }
 
 void kc3_license (void)
diff --git a/libkc3/kc3_main.h b/libkc3/kc3_main.h
index 0cd419f..a3269aa 100644
--- a/libkc3/kc3_main.h
+++ b/libkc3/kc3_main.h
@@ -100,6 +100,7 @@ s_tag *      kc3_facts_with_tags (s_facts *facts, s_tag *subject,
                                   s_fn *callback, s_tag *dest);
 s_tag *      kc3_identity (const s_tag *tag, s_tag *dest);
 s_tag *      kc3_integer_reduce (const s_tag *tag, s_tag *dest);
+s_tag *      kc3_let (const s_tag *vars, const s_tag *tag, s_tag *dest);
 bool         kc3_load (const s_str *path);
 s_tag *      kc3_operator_find_by_sym (const s_sym * const *sym,
                                        s_tag *dest);
diff --git a/libkc3/sym.c b/libkc3/sym.c
index 5b3b44f..ab696f1 100644
--- a/libkc3/sym.c
+++ b/libkc3/sym.c
@@ -1134,6 +1134,10 @@ uw * sym_type_size (const s_sym * const *type, uw *dest)
     *dest = sizeof(s_list *);
     return dest;
   }
+  if (*type == &g_sym_Map) {
+    *dest = sizeof(s_map);
+    return dest;
+  }
   if (*type == &g_sym_Ptag) {
     *dest = sizeof(p_tag);
     return dest;