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.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #pragma once
  25. #ifndef S_EXPRESSION_H
  26. #define S_EXPRESSION_H
  27. #include "list.h"
  28. #define SX_AS_(t,x) ((x) && ((s_expression*) x)->is_##t()) ? ((s_##t*) (x)) \
  29. : NULL
  30. #define SX_AS_LIST(x) SX_AS_(list, x)
  31. #define SX_AS_SYMBOL(x) SX_AS_(symbol, x)
  32. #define SX_AS_NUMBER(x) SX_AS_(number, x)
  33. #define SX_AS_INT(x) SX_AS_(int, x)
  34. /* For our purposes, S-Expressions are:
  35. * - <int>
  36. * - <float>
  37. * - symbol
  38. * - (expr1 expr2 ... exprN) where exprN is an S-Expression
  39. *
  40. * Unlike LISP/Scheme, we do not support (foo . bar) pairs.
  41. */
  42. class s_expression : public exec_node
  43. {
  44. public:
  45. virtual ~s_expression() { }
  46. /**
  47. * Read an S-Expression from the given string.
  48. * Advances the supplied pointer to just after the expression read.
  49. */
  50. static s_expression *read_expression(const char *&src);
  51. /**
  52. * Print out an S-Expression. Useful for debugging.
  53. */
  54. virtual void print() = 0;
  55. virtual bool is_list() const { return false; }
  56. virtual bool is_symbol() const { return false; }
  57. virtual bool is_number() const { return false; }
  58. virtual bool is_int() const { return false; }
  59. protected:
  60. s_expression() { }
  61. };
  62. /* Atoms */
  63. class s_number : public s_expression
  64. {
  65. public:
  66. virtual ~s_number() { }
  67. bool is_number() const { return true; }
  68. virtual float fvalue() = 0;
  69. protected:
  70. s_number() { }
  71. };
  72. class s_int : public s_number
  73. {
  74. public:
  75. s_int(int x) : val(x) { }
  76. virtual ~s_int() { }
  77. bool is_int() const { return true; }
  78. float fvalue() { return float(this->val); }
  79. int value() { return this->val; }
  80. void print();
  81. private:
  82. int val;
  83. };
  84. class s_float : public s_number
  85. {
  86. public:
  87. s_float(float x) : val(x) { }
  88. virtual ~s_float() { }
  89. float fvalue() { return this->val; }
  90. void print();
  91. private:
  92. float val;
  93. };
  94. class s_symbol : public s_expression
  95. {
  96. public:
  97. s_symbol(const char *);
  98. virtual ~s_symbol();
  99. bool is_symbol() const { return true; }
  100. const char *value() { return this->str; }
  101. void print();
  102. private:
  103. char *str;
  104. };
  105. /* Lists of expressions: (expr1 ... exprN) */
  106. class s_list : public s_expression
  107. {
  108. public:
  109. s_list();
  110. virtual ~s_list();
  111. virtual bool is_list() const { return true; }
  112. unsigned length() const;
  113. void print();
  114. exec_list subexpressions;
  115. };
  116. #endif /* S_EXPRESSION_H */