Commit 40e9a91163bc395bdbf91c961eb24b66438d9c3a

Thomas de Grivel 2024-09-06T16:42:03

wip doc

diff --git a/lib/kc3/0.1/list.kc3 b/lib/kc3/0.1/list.kc3
index b4d5d53..303d350 100644
--- a/lib/kc3/0.1/list.kc3
+++ b/lib/kc3/0.1/list.kc3
@@ -11,6 +11,15 @@ defmodule List do
 
   def has? = cfn Bool "list_has" (List, Tag, Result)
 
+  def join = fn {
+    (list, separator) { join(list, separator, []) }
+    ([], sep, acc) { str(reverse(acc)) }
+    ([first, second | rest], sep, acc) {
+      join([second | rest], sep, [sep, first | acc])
+    }
+    ([first], sep, acc) { str(reverse([first | acc])) }
+  }
+
   def map = fn {
     ([], _) do
       []
diff --git a/lib/kc3/0.1/str.facts b/lib/kc3/0.1/str.facts
index 5edba7d..8dfba05 100644
--- a/lib/kc3/0.1/str.facts
+++ b/lib/kc3/0.1/str.facts
@@ -10,9 +10,13 @@ replace {Str.ends_with?, :symbol_value,
 add {Str, :symbol, Str.rindex_character}
 replace {Str.rindex_character, :symbol_value,
          cfn Sw "str_rindex_character" (Str, Character)}
-add {Str, :symbol, Str.slice}
-replace {Str.slice, :symbol_value,
+add {Str, :symbol, Str.slice_cfn}
+replace {Str.slice_cfn, :symbol_value,
          cfn Str "str_init_slice" (Result, Str, Sw, Sw)}
+add {Str, :symbol, Str.slice}
+replace {Str.slice, :symbol_value, fn (str, start, end_) {
+  Str.slice_cfn((Str) str, (Sw) start, (Sw) end_)
+}}
 add {Str, :symbol, Str.starts_with?}
 replace {Str.starts_with?, :symbol_value,
          cfn Bool "str_starts_with" (Str, Str, Result)}
diff --git a/libkc3/cfn.c b/libkc3/cfn.c
index 0d7cd45..843ebe3 100644
--- a/libkc3/cfn.c
+++ b/libkc3/cfn.c
@@ -111,7 +111,11 @@ s_tag * cfn_apply (s_cfn *cfn, s_list *args, s_tag *dest)
         else
           if (! tag_to_ffi_pointer(&a->tag, cfn_arg_types->tag.data.sym,
                                    arg_values + i)) {
-            err_puts("cfn_apply: tag_to_ffi_pointer 5");
+            err_write_1("cfn_apply: ");
+            err_inspect_str(&cfn->name->str);
+            err_write_1(" ");
+            err_inspect_list((const s_list * const *) &args);
+            err_puts(": tag_to_ffi_pointer 5");
             assert(! "cfn_apply: tag_to_ffi_pointer 5");
             goto ko;
           }
diff --git a/libkc3/env.c b/libkc3/env.c
index ad6e653..a8681b6 100644
--- a/libkc3/env.c
+++ b/libkc3/env.c
@@ -695,12 +695,12 @@ bool env_eval_call_fn_args (s_env *env, const s_fn *fn,
       clause = fn->clauses;
       while (clause) {
         err_inspect_fn_pattern(clause->pattern);
-        err_puts("\n");
+        err_write_1("\n");
         clause = clause->next_clause;
       }
       err_puts("\nArguments :\n");
       err_inspect_fn_pattern(args);
-      err_puts("\n");
+      err_write_1("\n");
       list_delete_all(args);
       list_delete_all(env->search_modules);
       env->search_modules = search_modules;
diff --git a/test/httpd/app/controllers/doc_controller.kc3 b/test/httpd/app/controllers/doc_controller.kc3
index e17214f..3bd03cc 100644
--- a/test/httpd/app/controllers/doc_controller.kc3
+++ b/test/httpd/app/controllers/doc_controller.kc3
@@ -1,12 +1,47 @@
 defmodule DocController do
 
+  require File
+
+  def doc_index = fn {
+    (path, path_md) { doc_index(path, path_md, path, []) }
+    ([], path_md, dir, acc) { List.reverse(acc) }
+    ([file | rest], path_md, dir, acc) {
+      if (Str.starts_with?(file, ".")) do
+        doc_index(rest, path_md, dir, acc)
+      else
+        path = dir + file
+        if (File.is_directory?(path)) do
+          items = doc_index(File.list(path), path_md, path + "/", [])
+          item = %{type: :dir,
+                   url: Str.slice(path, 1, -1),
+                   name: file,
+                   items: items}
+          doc_index(rest, path_md, dir, [item | acc])
+        else
+          item = %{type: :file,
+                   url: Str.slice(path, 1, -1),
+                   name: file}
+          doc_index(rest, path_md, dir, [item | acc])
+        end
+      end
+    }
+    (path, path_md, dir, acc) {
+      if (type(path) == Str) do
+        doc_index(File.list(path), path_md, dir, acc)
+      end
+    }
+  }
+
   def route = fn (request) {
     if (request.method == :get) do
       path_md = ".#{request.url}.md"
       if File.exists?(path_md) do
-        md = File.read(path_md)
-        md_html = Markdown.to_html_str(md)
+        index = doc_index("./doc/", path_md)
+        menu = EKC3.render_file("app/templates/doc/menu.html.ekc3")
         title = "kc3-lang.org"
+        md = File.read(path_md)
+        html = Markdown.to_html_str(md)
+        page = EKC3.render_file("app/templates/doc/show.html.ekc3")
         body = EKC3.render_file("app/templates/layout.html.ekc3")
         %HTTP.Response{body: body}
       else
diff --git a/test/httpd/app/templates/doc/menu.html.ekc3 b/test/httpd/app/templates/doc/menu.html.ekc3
new file mode 100644
index 0000000..53126a9
--- /dev/null
+++ b/test/httpd/app/templates/doc/menu.html.ekc3
@@ -0,0 +1,14 @@
+<ul class="bg-gray">
+  <%= raw List.join(List.map(index, fn (file) {
+    items = if (file.type == :dir) do
+              let %{index: file.items} do
+                EKC3.render_file(__FILE__)
+              end
+            end
+    """
+  <li>
+    <a href="#{URL.escape(file.url)}">#{HTML.escape(file.name)}</a>
+    #{items}
+  </li>
+"""}), "\n") %>
+</ul>
diff --git a/test/httpd/app/templates/doc/show.html.ekc3 b/test/httpd/app/templates/doc/show.html.ekc3
new file mode 100644
index 0000000..3c8d8b1
--- /dev/null
+++ b/test/httpd/app/templates/doc/show.html.ekc3
@@ -0,0 +1,6 @@
+<div class="col-3">
+  <%= raw menu %>
+</div>
+<div class="col-9">
+  <%= raw html %>
+</div>
diff --git a/test/httpd/app/templates/layout.html.ekc3 b/test/httpd/app/templates/layout.html.ekc3
index b3ec33e..01ecf1f 100644
--- a/test/httpd/app/templates/layout.html.ekc3
+++ b/test/httpd/app/templates/layout.html.ekc3
@@ -14,10 +14,6 @@
   </head>
   <body>
     <%= raw EKC3.render_file("app/templates/nav.html.ekc3") %>
-    <div class="col-3">
-    </div>
-    <div class="col-9">
-      <%= raw md_html %>
-    </div>
+    <%= raw page %>
   </body>
 </html>
diff --git a/test/ikc3/if.kc3 b/test/ikc3/if.kc3
index 0579d19..4fdfbcb 100644
--- a/test/ikc3/if.kc3
+++ b/test/ikc3/if.kc3
@@ -84,3 +84,7 @@ else
     4
   end
 end
+quote if true do puts(1); 2 else puts(3); 4 end
+if true do puts(1); 2 else puts(3); 4 end
+quote if false do puts(5); 6 else puts(7); 8 end
+if false do puts(5); 6 else puts(7); 8 end
diff --git a/test/ikc3/if.out.expected b/test/ikc3/if.out.expected
index f26a753..f218f25 100644
--- a/test/ikc3/if.out.expected
+++ b/test/ikc3/if.out.expected
@@ -48,3 +48,17 @@ else
   end
 end
 4
+if true do
+  puts(1) ; 2
+else
+  puts(3) ; 4
+end
+1
+2
+if false do
+  puts(5) ; 6
+else
+  puts(7) ; 8
+end
+7
+8