Commit c5e08ece3a758dd226ff276ab24267d661f724aa

Thomas de Grivel 2022-11-01T08:03:48

buf_parse_fact

diff --git a/libc3/buf_parse.c b/libc3/buf_parse.c
index 047fcb2..66bb7bd 100644
--- a/libc3/buf_parse.c
+++ b/libc3/buf_parse.c
@@ -345,6 +345,88 @@ sw buf_parse_digit_dec (s_buf *buf, u8 *dest)
   return r;
 }
 
+sw buf_parse_fact (s_buf *buf, s_fact *dest)
+{
+  s_tag *object;
+  s_tag *predicate;
+  sw r;
+  sw result = 0;
+  s_buf_save save;
+  s_tag *subject;
+  assert(buf);
+  assert(dest);
+  buf_save_init(buf, &save);
+  if ((r = buf_read_1(buf, "{")) <= 0)
+    goto clean;
+  result += r;
+  if ((r = buf_parse_comments(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_ignore_spaces(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_parse_new_tag(buf, &subject)) <= 0)
+    goto restore;
+  result += r;
+  if ((r = buf_parse_comments(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_ignore_spaces(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_read_1(buf, ",")) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_parse_comments(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_ignore_spaces(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_parse_new_tag(buf, &predicate)) <= 0)
+    goto restore;
+  result += r;
+  if ((r = buf_parse_comments(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_ignore_spaces(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_read_1(buf, ",")) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_parse_comments(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_ignore_spaces(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_parse_new_tag(buf, &object)) <= 0)
+    goto restore;
+  result += r;
+  if ((r = buf_parse_comments(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_ignore_spaces(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_read_1(buf, "}")) < 0)
+    goto restore;
+  dest->subject = subject;
+  dest->predicate = predicate;
+  dest->object = object;
+  r = result;
+  goto clean;
+ restore:
+  buf_save_restore_rpos(buf, &save);
+  tag_delete(subject);
+  tag_delete(predicate);
+  tag_delete(object);
+ clean:
+  buf_save_clean(buf, &save);
+  return r;
+}
+
 sw buf_parse_ident (s_buf *buf, s_ident *dest)
 {
   character c;
@@ -740,6 +822,19 @@ sw buf_parse_module (s_buf *buf, const s_sym **dest)
   return r;
 }
 
+sw buf_parse_new_tag (s_buf *buf, s_tag **dest)
+{
+  sw r;
+  s_tag *tag = tag_new();
+  if ((r = buf_parse_tag(buf, tag)) < 0) {
+    tag_delete(tag);
+    *dest = NULL;
+    return r;
+  }
+  *dest = tag;
+  return r;
+}
+
 sw buf_parse_str (s_buf *buf, s_str *dest)
 {
   u8 b;
diff --git a/libc3/buf_parse.h b/libc3/buf_parse.h
index 2801c4a..ffa2a36 100644
--- a/libc3/buf_parse.h
+++ b/libc3/buf_parse.h
@@ -52,6 +52,7 @@ sw buf_parse_ident (s_buf *buf, s_ident *dest);
 sw buf_parse_integer (s_buf *buf, s_integer *dest);
 sw buf_parse_list (s_buf *buf, s_list **dest);
 sw buf_parse_module (s_buf *buf, const s_sym **dest);
+sw buf_parse_new_tag (s_buf *buf, s_tag **dest);
 sw buf_parse_str (s_buf *buf, s_str *dest);
 sw buf_parse_str_character (s_buf *buf, character *dest);
 sw buf_parse_str_character_unicode (s_buf *buf, character *dest);
diff --git a/libc3/tag.c b/libc3/tag.c
index a3aca63..01b3667 100644
--- a/libc3/tag.c
+++ b/libc3/tag.c
@@ -643,6 +643,13 @@ s_tag * tag_list_1 (s_tag *tag, const s8 *p)
   return tag_init_list_1(tag, p);
 }
 
+s_tag * tag_new ()
+{
+  s_tag *tag;
+  tag = calloc(1, sizeof(s_tag));
+  return tag;
+}
+
 s_tag * tag_new_1 (const s8 *p)
 {
   s_tag *tag;
diff --git a/libc3/tag.h b/libc3/tag.h
index 7a79721..a29514b 100644
--- a/libc3/tag.h
+++ b/libc3/tag.h
@@ -63,6 +63,7 @@ s_tag * tag_init_void (s_tag *tag);
 void    tag_clean (s_tag *tag);
 
 /* Constructors, call tag_delete after use */
+s_tag * tag_new ();
 s_tag * tag_new_1 (const s8 *p);
 s_tag * tag_new_str (s8 *free, uw size, const s8 *p);
 s_tag * tag_new_bool (bool p);
diff --git a/libtommath b/libtommath
index 6d26011..3e5e647 160000
--- a/libtommath
+++ b/libtommath
@@ -1 +1 @@
-Subproject commit 6d26011da204b0deb6dab8e2e899e43dd3993c33
+Subproject commit 3e5e647c9b870f2d366d0066e1760322e75a98aa
diff --git a/test/facts_test.c b/test/facts_test.c
index fc5d8e2..dacbba7 100644
--- a/test/facts_test.c
+++ b/test/facts_test.c
@@ -19,8 +19,10 @@
 
 void facts_test ();
 void facts_test_add ();
+void facts_test_dump ();
 void facts_test_find ();
 void facts_test_init_clean ();
+void facts_test_load ();
 void facts_test_log_add ();
 void facts_test_log_remove ();
 void facts_test_new_delete ();
@@ -90,6 +92,49 @@ void facts_test_add ()
   facts_clean(&facts);
 }
 
+void facts_test_dump ()
+{
+  uw i = 0;
+  s8 *p[24] = {
+    "\"a\"",
+    ":a",
+    "A",
+    "a",
+    "[]",
+    "[[], []]",
+    "{:a, :b}",
+    "{{:a, :b}, {:c, :d}}",
+    "{a, b}",
+    "{{a, b}, {c, d}}",
+    "0",
+    "1",
+    "10",
+    "0x100",
+    "0x10000",
+    "0x100000000",
+    "0x10000000000000000",
+    "-1",
+    "-10",
+    "-0x100",
+    "-0x10000",
+    "-0x100000000",
+    "-0x10000000000000000",
+    NULL
+  };
+  s_fact fact[24];
+  s_facts facts;
+  facts_init(&facts, NULL);
+  while (p[i]) {
+    fact_test_init_1(fact + i, p[i]);
+    facts_add_fact(&facts, fact + i);
+    i++;
+  }
+  facts_dump(&facts, "facts_test_dump.facts");
+  test_file_compare("facts_test_dump.facts",
+                    "facts_test_dump.facts.expected");
+  facts_clean(&facts);
+}
+
 void facts_test_find ()
 {
   uw i = 0;
@@ -162,6 +207,10 @@ void facts_test_init_clean ()
   test_ok();
 }
 
+void facts_test_load ()
+{
+}
+
 void facts_test_log_add ()
 {
   uw i = 0;