Commit a15d9159f4cc2c801ed62336fc9b67fa2c647e4e

Thomas de Grivel 2024-02-12T19:45:10

fix buf_parse

diff --git a/libc3/block.c b/libc3/block.c
index ec41aed..8a71554 100644
--- a/libc3/block.c
+++ b/libc3/block.c
@@ -103,6 +103,7 @@ s_block * block_init_copy (s_block *block, const s_block *src)
     tag_init_copy(block->tag + i, src->tag + i);
     i++;
   }
+  block->short_form = src->short_form;
   return block;
 }
 
diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index 8c019a8..ac15091 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -229,23 +229,48 @@ sw buf_inspect_block (s_buf *buf, const s_block *block)
   u64 i = 0;
   sw r;
   sw result = 0;
-  if (! block->count)
-    return buf_write_1(buf, "do end");
-  if ((r = buf_write_1(buf, "do")) < 0)
-    return r;
-  result += r;
-  while (i < block->count) {
-    if ((r = buf_write_1(buf, "\n  ")) < 0)
+  if (! block->count) {
+    if (block->short_form)
+      return buf_write_1(buf, "{}");
+    else
+      return buf_write_1(buf, "do end");
+  }
+  if (block->short_form) {    
+    if ((r = buf_write_1(buf, "{ ")) < 0)
       return r;
-    result += r;
+  }
+  else {
+    if ((r = buf_write_1(buf, "do\n  ")) < 0)
+      return r;
+  }
+  result += r;
+  while (i < block->count - 1) {
     if ((r = buf_inspect_tag(buf, block->tag + i)) < 0)
       return r;
     result += r;
+    if (block->short_form) {
+      if ((r = buf_write_1(buf, "; ")) < 0)
+        return r;
+    }
+    else {
+      if ((r = buf_write_1(buf, "\n  ")) < 0)
+        return r;
+    }
+    result += r;
     i++;
   }
-  if ((r = buf_write_1(buf, "\nend")) < 0)
+  if ((r = buf_inspect_tag(buf, block->tag + i)) < 0)
     return r;
   result += r;
+  if (block->short_form) {
+    if ((r = buf_write_1(buf, " }")) < 0)
+      return r;
+  }
+  else {
+    if ((r = buf_write_1(buf, "\nend")) < 0)
+      return r;
+  }
+  result += r;
   return result;
 }
 
diff --git a/libc3/buf_parse.c b/libc3/buf_parse.c
index 365fae1..f4ef45d 100644
--- a/libc3/buf_parse.c
+++ b/libc3/buf_parse.c
@@ -409,14 +409,18 @@ sw buf_parse_block (s_buf *buf, s_block *block)
   sw r;
   sw result = 0;
   s_buf_save save;
+  bool short_form = false;
   s_block tmp;
   assert(buf);
   assert(block);
   buf_save_init(buf, &save);
   if ((r = buf_read_sym(buf, &g_sym_do)) < 0)
     goto clean;
-  if (! r && (r = buf_read_1(buf, "{")) <= 0)
-    goto clean;
+  if (! r) {
+    short_form = true;
+    if ((r = buf_read_1(buf, "{")) <= 0)
+      goto clean;
+  }
   result += r;
   i = &list;
   *i = NULL;
@@ -484,6 +488,7 @@ sw buf_parse_block (s_buf *buf, s_block *block)
     k = list_next(k);
     l++;
   }
+  tmp.short_form = short_form;
   *block = tmp;
   r = result;
   goto clean;
@@ -3193,8 +3198,8 @@ sw buf_parse_tag_primary (s_buf *buf, s_tag *dest)
       (r = buf_parse_tag_character(buf, dest)) != 0 ||
       (r = buf_parse_tag_map(buf, dest)) != 0 ||
       (r = buf_parse_tag_str(buf, dest)) != 0 ||
-      (r = buf_parse_tag_block(buf, dest)) != 0 ||
       (r = buf_parse_tag_tuple(buf, dest)) != 0 ||
+      (r = buf_parse_tag_block(buf, dest)) != 0 ||
       (r = buf_parse_tag_quote(buf, dest)) != 0 ||
       (r = buf_parse_tag_unquote(buf, dest)) != 0 ||
       (r = buf_parse_tag_cfn(buf, dest)) != 0 ||
diff --git a/libc3/compare.c b/libc3/compare.c
index 1adf363..1027c7f 100644
--- a/libc3/compare.c
+++ b/libc3/compare.c
@@ -73,6 +73,10 @@ s8 compare_block (const s_block *a, const s_block *b)
       return r;
     i++;
   }
+  if (a->short_form && ! b->short_form)
+    return -1;
+  if (! a->short_form && b->short_form)
+    return 1;
   return 0;
 }
 
diff --git a/libc3/types.h b/libc3/types.h
index ef29c3f..682962e 100644
--- a/libc3/types.h
+++ b/libc3/types.h
@@ -207,6 +207,7 @@ struct array_dimension {
 struct block {
   uw count;
   s_tag *tag;
+  bool short_form;
 };
 
 struct buf_save {
diff --git a/test/ic3/fn.in b/test/ic3/fn.in
index f27a276..6d54c2d 100644
--- a/test/ic3/fn.in
+++ b/test/ic3/fn.in
@@ -6,10 +6,10 @@ quote fn {
   ([x | _y]) { x }
   (_) { :error }
 }
-quote fn (x) {
+quote fn (x) do
   "Hello, world !"
   x * 2
-}
+end
 a = fn (x) { x }
 a(1)
 b = fn (x, _y) { x }
@@ -22,8 +22,8 @@ d = fn {
   (_) { :error2 }
 }
 d([1, 2])
-e = fn (x) {
+e = fn (x) do
   "Hello, world !"
   x * 2
-}
+end
 e(2)
diff --git a/test/ic3/fn.out.expected b/test/ic3/fn.out.expected
index 1b75d91..b35f792 100644
--- a/test/ic3/fn.out.expected
+++ b/test/ic3/fn.out.expected
@@ -6,10 +6,10 @@ fn {
   ([x | _y]) { x }
   (_) { :error }
 }
-fn (x) {
+fn (x) do
   "Hello, world !"
   x * 2
-}
+end
 fn (x) { x }
 1
 fn (x, _y) { x }
@@ -22,8 +22,8 @@ fn {
   (_) { :error2 }
 }
 1
-fn (x) {
+fn (x) do
   "Hello, world !"
   x * 2
-}
+end
 4