Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

s_expression.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* -*- c++ -*- */
  2. /*
  3. * Copyright © 2010 Intel Corporation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <cstdio>
  25. #include <cstdlib>
  26. #include <cstring>
  27. #include <assert.h>
  28. #include "s_expression.h"
  29. s_symbol::s_symbol(const char *tmp)
  30. {
  31. this->str = new char [strlen(tmp) + 1];
  32. strcpy(this->str, tmp);
  33. }
  34. s_symbol::~s_symbol()
  35. {
  36. delete [] this->str;
  37. this->str = NULL;
  38. }
  39. s_list::s_list()
  40. {
  41. }
  42. s_list::~s_list()
  43. {
  44. exec_list_iterator it(this->subexpressions.iterator());
  45. while (it.has_next())
  46. it.remove();
  47. assert(this->subexpressions.is_empty());
  48. }
  49. unsigned
  50. s_list::length() const
  51. {
  52. unsigned i = 0;
  53. foreach_iter(exec_list_iterator, it, this->subexpressions) {
  54. i++;
  55. }
  56. return i;
  57. }
  58. static s_expression *
  59. read_atom(const char *& src)
  60. {
  61. char buf[101];
  62. int n;
  63. if (sscanf(src, " %100[^( \v\t\r\n)]%n", buf, &n) != 1)
  64. return NULL; // no atom
  65. src += n;
  66. // Check if the atom is a number.
  67. char *float_end = NULL;
  68. double f = strtod(buf, &float_end);
  69. if (float_end != buf) {
  70. char *int_end = NULL;
  71. int i = strtol(buf, &int_end, 10);
  72. // If strtod matched more characters, it must have a decimal part
  73. if (float_end > int_end)
  74. return new s_float(f);
  75. return new s_int(i);
  76. }
  77. // Not a number; return a symbol.
  78. return new s_symbol(buf);
  79. }
  80. s_expression *
  81. s_expression::read_expression(const char *&src)
  82. {
  83. assert(src != NULL);
  84. s_expression *atom = read_atom(src);
  85. if (atom != NULL)
  86. return atom;
  87. char c;
  88. int n;
  89. if (sscanf(src, " %c%n", &c, &n) == 1 && c == '(') {
  90. src += n;
  91. s_list *list = new s_list;
  92. s_expression *expr;
  93. while ((expr = read_expression(src)) != NULL) {
  94. list->subexpressions.push_tail(expr);
  95. }
  96. if (sscanf(src, " %c%n", &c, &n) != 1 || c != ')') {
  97. printf("Unclosed expression (check your parenthesis).\n");
  98. return NULL;
  99. }
  100. src += n;
  101. return list;
  102. }
  103. return NULL;
  104. }
  105. void s_int::print()
  106. {
  107. printf("%d", this->val);
  108. }
  109. void s_float::print()
  110. {
  111. printf("%f", this->val);
  112. }
  113. void s_symbol::print()
  114. {
  115. printf("%s", this->str);
  116. }
  117. void s_list::print()
  118. {
  119. printf("(");
  120. foreach_iter(exec_list_iterator, it, this->subexpressions) {
  121. s_expression *expr = (s_expression*) it.get();
  122. expr->print();
  123. printf(" ");
  124. }
  125. printf(")");
  126. }