Răsfoiți Sursa

Fix known bugs and finally switch to binary search trees for maps.

Despite the name, they are not actual rebalancing AVL trees yet.
Just plain binary search trees.
Giovanni Mascellani 5 ani în urmă
părinte
comite
f59f1c2246
6 a modificat fișierele cu 55 adăugiri și 7 ștergeri
  1. 26 1
      asmg/avl_map.g
  2. 2 2
      asmg/c_preproc.g
  3. 2 2
      asmg/main.g
  4. 7 0
      asmg/map.g
  5. 16 0
      asmg/utils.g
  6. 2 2
      asmg/vector.g

+ 26 - 1
asmg/avl_map.g

@@ -125,6 +125,13 @@ fun map_at 2 {
   $avl
   @avl map key 0 _map_find = ;
   avl 0 != "map_at: key does not exist" assert_msg ;
+
+  # "map_at(" 1 platform_log ;
+  # key 1 platform_log ;
+  # ") = " 1 platform_log ;
+  # avl AVL_VALUE take itoa 1 platform_log ;
+  # "\n" 1 platform_log ;
+
   avl AVL_VALUE take ret ;
 }
 
@@ -136,6 +143,13 @@ fun map_has 2 {
 
   $avl
   @avl map key 0 _map_find = ;
+
+  # "map_has(" 1 platform_log ;
+  # key 1 platform_log ;
+  # ") = " 1 platform_log ;
+  # avl 0 != itoa 1 platform_log ;
+  # "\n" 1 platform_log ;
+
   avl 0 != ret ;
 }
 
@@ -150,6 +164,13 @@ fun map_set 3 {
   $avl
   @avl map key 1 _map_find = ;
   avl 0 != "map_set: error 1" assert_msg ;
+
+  # "map_set(" 1 platform_log ;
+  # key 1 platform_log ;
+  # ", " 1 platform_log ;
+  # value itoa 1 platform_log ;
+  # ")\n" 1 platform_log ;
+
   avl AVL_VALUE take_addr value = ;
 }
 
@@ -238,11 +259,15 @@ fun map_erase 2 {
   $avl
   @avl map key 0 _map_find = ;
   map avl _map_erase ;
+
+  # "map_erase(" 1 platform_log ;
+  # key 1 platform_log ;
+  # ")\n" 1 platform_log ;
 }
 
 fun map_size 1 {
   $map
-  @map 0 param 0 ;
+  @map 0 param = ;
 
   map MAP_SIZE take ret ;
 }

+ 2 - 2
asmg/c_preproc.g

@@ -501,7 +501,7 @@ fun process_token_function 5 {
   # First parse the inputs
   $args
   @args map_init = ;
-  while args vector_size subst SUBST_PARAMETERS take vector_size < {
+  while args map_size subst SUBST_PARAMETERS take vector_size < {
     $depth
     @depth 0 = ;
     $cont
@@ -528,7 +528,7 @@ fun process_token_function 5 {
     $ident
     @ident subst SUBST_PARAMETERS take args map_size vector_at = ;
     args ident arg map_set ;
-    if args vector_size subst SUBST_PARAMETERS take vector_size == {
+    if args map_size subst SUBST_PARAMETERS take vector_size == {
       tok ")" strcmp 0 == "process_token_function: ) expected" assert_msg ;
     } else {
       tok "," strcmp 0 == "process_token_function: , expected" assert_msg ;

+ 2 - 2
asmg/main.g

@@ -28,8 +28,8 @@ const USE_SIMPLE_MALLOC 0
 const USE_CHECKED_MALLOC 0
 const USE_KMALLOC 1
 
-const USE_SIMPLE_MAP 1
-const USE_AVL_MAP 0
+const USE_SIMPLE_MAP 0
+const USE_AVL_MAP 1
 
 fun main 0 {
   "Hello, G!\n" 1 platform_log ;

+ 7 - 0
asmg/map.g

@@ -63,6 +63,13 @@ fun map_at 2 {
   $addr
   @addr map idx vector_at_addr = ;
   addr MAP_ELEM_PRESENT take "map_at: element is not present" assert_msg ;
+
+  # "map_at(" 1 platform_log ;
+  # key 1 platform_log ;
+  # ") = " 1 platform_log ;
+  # addr MAP_ELEM_VALUE take itoa 1 platform_log ;
+  # "\n" 1 platform_log ;
+
   addr MAP_ELEM_VALUE take ret ;
 }
 

+ 16 - 0
asmg/utils.g

@@ -122,6 +122,22 @@ fun assert_msg_str 3 {
   }
 }
 
+fun assert_msg_int_int 4 {
+  if 3 param ! {
+    "\nASSERTION FAILED at line " 1 platform_log ;
+    assert_pos itoa 1 platform_log ;
+    "\n" 1 platform_log ;
+    2 param 1 platform_log ;
+    "\n" 1 platform_log ;
+    1 param itoa 1 platform_log ;
+    " " 1 platform_log ;
+    0 param itoa 1 platform_log ;
+    "\n" 1 platform_log ;
+    dump_stacktrace ;
+    platform_panic ;
+  }
+}
+
 fun min 2 {
   $x
   $y

+ 2 - 2
asmg/vector.g

@@ -57,7 +57,7 @@ fun vector_at_addr 2 {
   $idx
   @vptr 1 param = ;
   @idx 0 param = ;
-  idx vptr VECTOR_SIZE take < "Vector access out of bounds" assert_msg ;
+  idx vptr VECTOR_SIZE take < "vector_at_addr: vector access out of bounds" idx vptr VECTOR_SIZE take assert_msg_int_int ;
   vptr VECTOR_DATA take idx vptr VECTOR_SIZEOF_ELEM take * + ret ;
 }
 
@@ -66,7 +66,7 @@ fun vector_at 2 {
   $idx
   @vptr 1 param = ;
   @idx 0 param = ;
-  idx vptr VECTOR_SIZE take < "Vector access out of bounds" assert_msg ;
+  idx vptr VECTOR_SIZE take < "vector_at: vector access out of bounds" idx vptr VECTOR_SIZE take assert_msg_int_int ;
   vptr VECTOR_DATA take idx vptr VECTOR_SIZEOF_ELEM take * + ** ret ;
 }