squirrel_util.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  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. #include "squirrel/squirrel_util.hpp"
  17. #include <simplesquirrel/array.hpp>
  18. #include <simplesquirrel/table.hpp>
  19. std::string squirrel_to_string(const ssq::Object& object)
  20. {
  21. std::ostringstream os;
  22. switch (object.getType())
  23. {
  24. case ssq::Type::NULLPTR:
  25. os << "<null>";
  26. break;
  27. case ssq::Type::BOOL:
  28. os << object.toBool();
  29. break;
  30. case ssq::Type::INTEGER:
  31. os << object.to<int>();
  32. break;
  33. case ssq::Type::FLOAT:
  34. os << object.toFloat();
  35. break;
  36. case ssq::Type::STRING:
  37. os << "\"" << object.toString() << "\"";
  38. break;
  39. case ssq::Type::TABLE:
  40. {
  41. const std::map<std::string, ssq::Object> table = object.toTable().convertRaw();
  42. bool first = true;
  43. os << "{";
  44. for (const auto& [key, value] : table)
  45. {
  46. if (!first)
  47. os << ", ";
  48. first = false;
  49. os << key << " => " << squirrel_to_string(value);
  50. }
  51. os << "}";
  52. break;
  53. }
  54. case ssq::Type::ARRAY:
  55. {
  56. const std::vector<ssq::Object> array = object.toArray().convertRaw();
  57. bool first = true;
  58. os << "[";
  59. for (const ssq::Object& value : array)
  60. {
  61. if (!first)
  62. os << ", ";
  63. first = false;
  64. os << squirrel_to_string(value);
  65. }
  66. os << "]";
  67. break;
  68. }
  69. case ssq::Type::USERDATA:
  70. os << "<userdata>";
  71. break;
  72. case ssq::Type::CLOSURE:
  73. os << "<closure>";
  74. break;
  75. case ssq::Type::NATIVECLOSURE:
  76. os << "<native closure>";
  77. break;
  78. case ssq::Type::GENERATOR:
  79. os << "<generator>";
  80. break;
  81. case ssq::Type::USERPOINTER:
  82. os << "userpointer";
  83. break;
  84. case ssq::Type::THREAD:
  85. os << "<thread>";
  86. break;
  87. case ssq::Type::CLASS:
  88. os << "<class>";
  89. break;
  90. case ssq::Type::INSTANCE:
  91. os << "<instance>";
  92. break;
  93. case ssq::Type::WEAKREF:
  94. os << "<weakref>";
  95. break;
  96. default:
  97. os << "<unknown>";
  98. break;
  99. }
  100. return os.str();
  101. }
  102. /* EOF */