- CAMLparam1(grammar);
- CAMLlocal1(res);
- const char * s = (GRAMMAR(grammar)->getTagName(Long_val(symbol) >> 2)).c_str();
- res = caml_copy_string(s);
- CAMLreturn(res);
+ CAMLparam2(b, idx);
+ CAMLreturn (Val_int(bp_preorder_rank(Obj_val<bp*>(b), Int_val(idx)) - 1));
+}
+
+
+extern "C" value caml_bp_load(value file)
+{
+ CAMLparam1(file);
+ CAMLlocal1(result);
+ bp *B;
+ int f1 = Int_val(file);
+ int f2 = dup(f1);
+ FILE * fd = fdopen(f2, "r");
+ if (fd == NULL)
+ CAMLRAISEMSG("Error opening bp file");
+ B = loadTree(fd);
+ fclose(fd);
+ result = sxsi_alloc_custom<bp*>(caml_bp_delete);
+ Obj_val<bp*>(result) = B;
+ CAMLreturn(result);
+}
+
+extern "C" value caml_bp_save(value b, value file)
+{
+ CAMLparam2(b, file);
+ bp *B = Obj_val<bp*>(b);
+ int f1 = Int_val(file);
+ int f2 = dup(f1);
+ FILE * fd = fdopen(f2, "a");
+ fflush(stderr);
+ if (fd == NULL)
+ CAMLRAISEMSG("Error saving bp file");
+ saveTree(B, fd);
+ fclose(fd);
+ CAMLreturn(Val_unit);
+}
+
+extern "C" value caml_bp_alloc_stats(value unit)
+{
+ CAMLparam1(unit);
+ CAMLreturn (Val_long(bp_get_alloc_stats()));