PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/TeXmacs-1.0.7.11-src/src/Data/Tree/tree_modify.cpp

#
C++ | 84 lines | 61 code | 8 blank | 15 comment | 22 complexity | 3b401f4dab545732f036ce0ea20e1d56 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /******************************************************************************
  2. * MODULE : tree_modify.cpp
  3. * DESCRIPTION: high level tree modification subroutines
  4. * COPYRIGHT : (C) 2010 Joris van der Hoeven
  5. *******************************************************************************
  6. * This software falls under the GNU general public license version 3 or later.
  7. * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
  8. * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
  9. ******************************************************************************/
  10. #include "tree_modify.hpp"
  11. #include "drd_std.hpp"
  12. #include "path.hpp"
  13. extern tree the_et;
  14. /******************************************************************************
  15. * DRD-based correction of trees
  16. ******************************************************************************/
  17. void
  18. correct_concat_node (tree& t, int done) {
  19. //cout << "Correct " << t << ", " << done << "\n";
  20. int i, n= N(t);
  21. if (n == 0) {
  22. assign (t, "");
  23. return;
  24. }
  25. for (i=done; i<n; i++) {
  26. if (t[i] == "") {
  27. remove (t, i, 1);
  28. correct_concat_node (t, i);
  29. return;
  30. }
  31. if ((i<n-1) && is_atomic (t[i]) && is_atomic (t[i+1])) {
  32. join (t, i);
  33. correct_concat_node (t, i);
  34. return;
  35. }
  36. if (is_concat (t[i])) {
  37. insert_node (t, 0, CONCAT);
  38. split (t, 0, i);
  39. split (t, 1, 1);
  40. remove_node (t[1], 0);
  41. if (t[0] == tree (CONCAT)) remove (t, 0, 1);
  42. else join (t, 0);
  43. if (t[1] == tree (CONCAT)) remove (t, 1, 1);
  44. else join (t, 0);
  45. remove_node (t, 0);
  46. correct_concat_node (t, max (i-1, 0));
  47. return;
  48. }
  49. }
  50. }
  51. void
  52. correct_node (tree& t) {
  53. // NOTE: this routine should only modify t and its descendants,
  54. // but not any ancestors
  55. if (is_compound (t)) {
  56. if (the_drd->contains (as_string (L(t))) &&
  57. !the_drd->correct_arity (L(t), N(t)))
  58. assign (t, "");
  59. if (is_concat (t))
  60. correct_concat_node (t, 0);
  61. }
  62. }
  63. void
  64. correct_downwards (tree& t) {
  65. if (is_compound (t))
  66. for (int i=0; i<N(t); i++)
  67. correct_downwards (t[i]);
  68. correct_node (t);
  69. }
  70. void
  71. correct_upwards (tree& t) {
  72. correct_node (t);
  73. path ip= obtain_ip (t);
  74. if (ip_attached (ip) && !is_nil (ip))
  75. correct_upwards (subtree (the_et, reverse (ip->next)));
  76. }