unparse.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * unparse.c - Dump an expression tree into a string
  3. *
  4. * Written 2009, 2012 by Werner Almesberger
  5. * Copyright 2009, 2012 by Werner Almesberger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. /*
  13. * This is crazily inefficient but who cares :-)
  14. */
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include "util.h"
  18. #include "expr.h"
  19. #include "unparse.h"
  20. enum prec {
  21. prec_add,
  22. prec_mult,
  23. prec_unary,
  24. prec_primary,
  25. };
  26. static int precedence(op_type op)
  27. {
  28. if (op == op_add || op == op_sub)
  29. return prec_add;
  30. if (op == op_mult || op == op_div)
  31. return prec_mult;
  32. if (op == op_minus)
  33. return prec_unary;
  34. if (op == op_num || op == op_string || op == op_var ||
  35. op == op_sin || op == op_cos || op == op_sqrt || op == op_floor)
  36. return prec_primary;
  37. abort();
  38. }
  39. static char *merge3(char *a, const char *op, char *b)
  40. {
  41. char *buf;
  42. buf = alloc_size(strlen(op)+strlen(a)+strlen(b)+1);
  43. sprintf(buf, "%s%s%s", a, op, b);
  44. free(a);
  45. free(b);
  46. return buf;
  47. }
  48. static char *merge2(const char *op, char *a)
  49. {
  50. char *buf;
  51. buf = alloc_size(strlen(op)+strlen(a)+1);
  52. sprintf(buf, "%s%s", op, a);
  53. free(a);
  54. return buf;
  55. }
  56. static char *unparse_op(const struct expr *expr, enum prec prec);
  57. static char *unparse_fn(const char *name, const struct expr *expr)
  58. {
  59. char *buf, *tmp;
  60. tmp = unparse_op(expr->u.op.a, prec_add);
  61. buf = alloc_size(strlen(name)+strlen(tmp)+3);
  62. sprintf(buf, "%s(%s)", name, tmp);
  63. free(tmp);
  64. return buf;
  65. }
  66. static char *unparse_op(const struct expr *expr, enum prec prec)
  67. {
  68. char tmp[100];
  69. char *buf, *temp;
  70. if (prec > precedence(expr->op)) {
  71. temp = unparse_op(expr, prec_add);
  72. buf = alloc_size(strlen(temp)+3);
  73. sprintf(buf, "(%s)", temp);
  74. free(temp);
  75. return buf;
  76. }
  77. if (expr->op == op_num) {
  78. snprintf(tmp, sizeof(tmp), "%lg%s",
  79. expr->u.num.n, str_unit(expr->u.num));
  80. return stralloc(tmp);
  81. }
  82. if (expr->op == op_string)
  83. return stralloc_printf("\"%s\"", expr->u.str);
  84. if (expr->op == op_var)
  85. return stralloc(expr->u.var);
  86. if (expr->op == op_minus)
  87. return merge2("-", unparse_op(expr->u.op.a, prec_unary));
  88. if (expr->op == op_add)
  89. return merge3(unparse_op(expr->u.op.a, prec_add), "+",
  90. unparse_op(expr->u.op.b, prec_add));
  91. if (expr->op == op_sub)
  92. return merge3(unparse_op(expr->u.op.a, prec_add), "-",
  93. unparse_op(expr->u.op.b, prec_mult));
  94. if (expr->op == op_mult)
  95. return merge3(unparse_op(expr->u.op.a, prec_mult), "*",
  96. unparse_op(expr->u.op.b, prec_mult));
  97. if (expr->op == op_div)
  98. return merge3(unparse_op(expr->u.op.a, prec_mult), "/",
  99. unparse_op(expr->u.op.b, prec_primary));
  100. if (expr->op == op_sin)
  101. return unparse_fn("sin", expr);
  102. if (expr->op == op_cos)
  103. return unparse_fn("cos", expr);
  104. if (expr->op == op_sqrt)
  105. return unparse_fn("sqrt", expr);
  106. if (expr->op == op_floor)
  107. return unparse_fn("floor", expr);
  108. abort();
  109. }
  110. char *unparse(const struct expr *expr)
  111. {
  112. return expr ? unparse_op(expr, prec_add) : stralloc("");
  113. }