Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

s_expression.cpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 = talloc_strdup (this, tmp);
  32. assert(this->str != NULL);
  33. }
  34. s_list::s_list()
  35. {
  36. }
  37. unsigned
  38. s_list::length() const
  39. {
  40. unsigned i = 0;
  41. foreach_iter(exec_list_iterator, it, this->subexpressions) {
  42. i++;
  43. }
  44. return i;
  45. }
  46. static s_expression *
  47. read_atom(void *ctx, const char *& src)
  48. {
  49. char buf[101];
  50. int n;
  51. if (sscanf(src, " %100[^( \v\t\r\n)]%n", buf, &n) != 1)
  52. return NULL; // no atom
  53. src += n;
  54. // Check if the atom is a number.
  55. char *float_end = NULL;
  56. double f = strtod(buf, &float_end);
  57. if (float_end != buf) {
  58. char *int_end = NULL;
  59. int i = strtol(buf, &int_end, 10);
  60. // If strtod matched more characters, it must have a decimal part
  61. if (float_end > int_end)
  62. return new(ctx) s_float(f);
  63. return new(ctx) s_int(i);
  64. }
  65. // Not a number; return a symbol.
  66. return new(ctx) s_symbol(buf);
  67. }
  68. s_expression *
  69. s_expression::read_expression(void *ctx, const char *&src)
  70. {
  71. assert(src != NULL);
  72. s_expression *atom = read_atom(ctx, src);
  73. if (atom != NULL)
  74. return atom;
  75. char c;
  76. int n;
  77. if (sscanf(src, " %c%n", &c, &n) == 1 && c == '(') {
  78. src += n;
  79. s_list *list = new(ctx) s_list;
  80. s_expression *expr;
  81. while ((expr = read_expression(ctx, src)) != NULL) {
  82. list->subexpressions.push_tail(expr);
  83. }
  84. if (sscanf(src, " %c%n", &c, &n) != 1 || c != ')') {
  85. printf("Unclosed expression (check your parenthesis).\n");
  86. return NULL;
  87. }
  88. src += n;
  89. return list;
  90. }
  91. return NULL;
  92. }
  93. void s_int::print()
  94. {
  95. printf("%d", this->val);
  96. }
  97. void s_float::print()
  98. {
  99. printf("%f", this->val);
  100. }
  101. void s_symbol::print()
  102. {
  103. printf("%s", this->str);
  104. }
  105. void s_list::print()
  106. {
  107. printf("(");
  108. foreach_iter(exec_list_iterator, it, this->subexpressions) {
  109. s_expression *expr = (s_expression*) it.get();
  110. expr->print();
  111. printf(" ");
  112. }
  113. printf(")");
  114. }