cook.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2021
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * SPDX-License-Identifier: GPL-3.0+
  18. * License-Filename: LICENSE
  19. */
  20. #include <assert.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "arg.h"
  25. #include "cook.h"
  26. #include "error.h"
  27. #include "preen.h"
  28. #include "syntax.h"
  29. /* cook() prepares a grammar for feeding */
  30. /* perhaps we should use the walk() function from sugar.c, but that doesn't
  31. * support the "pre" variable that we use. */
  32. static void cook0(struct s_node *n) {
  33. static char *name;
  34. struct s_node *p;
  35. if (n->type == rule)
  36. name = n->text;
  37. /* Replace
  38. * ... _ foo
  39. * with
  40. * ... _ (foo / !.)
  41. * for any non-empty foo.
  42. */
  43. if (n->type == cafe) {
  44. struct s_node *t;
  45. //fprintf(stderr, "match at node %ld\n", n->id);
  46. if (!n->next)
  47. fatal3("invalid `$' at the end of rule `", name, "'");
  48. t = s_kid(seq, s_kid(not, s_new(any)));
  49. t = cons(s_kid(seq, n->next), t);
  50. t = s_kid(alt, t);
  51. // pre->next = t;
  52. // free(n);
  53. n->next = t;
  54. }
  55. if (s_has_children(n->type))
  56. for (p = n->first; p; p = p->next)
  57. cook0(p);
  58. }
  59. /* make all rules void, and remove all expr nodes */
  60. static void dexpr(struct s_node *n, struct s_node *par, struct s_node *sib) {
  61. struct s_node *p, *q;
  62. if (n->type == expr) {
  63. assert(par || sib);
  64. if (sib)
  65. sib->next = n->next;
  66. else
  67. par->first = 0;
  68. /* at this point we should free(n) and its children, but we can't do
  69. * that inside the loop */
  70. }
  71. /* XXX in principle, we could remove all bindings *except* those
  72. * used in guards. */
  73. if (s_has_children(n->type))
  74. for (p = 0, q = n->first; q; p = q, q = q->next)
  75. dexpr(q, n, p);
  76. }
  77. void cook(struct s_node *g) {
  78. char *newname;
  79. int l;
  80. dexpr(g, 0, 0);
  81. cook0(g);
  82. l = strlen(g->text) + strlen("_feed") + 1;
  83. newname = realloc(0, l);
  84. if (!newname) nomem();
  85. strcpy(newname, g->text);
  86. strcat(newname, "_feed");
  87. g->text = newname;
  88. check_recursion(g);
  89. }
  90. /* end. */