tree.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #ifndef __TREE_H__
  2. #define __TREE_H__
  3. #include <vector>
  4. #include <string>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <stdexcept>
  8. #include <assert.h>
  9. class Namespace;
  10. class AtomicType {
  11. public:
  12. AtomicType() :
  13. name(),
  14. parent(0)
  15. { }
  16. virtual ~AtomicType()
  17. { }
  18. virtual void write_c(std::ostream& out)
  19. {
  20. out << name;
  21. }
  22. std::string name;
  23. Namespace* parent;
  24. private:
  25. AtomicType(const AtomicType&);
  26. AtomicType& operator=(const AtomicType&);
  27. };
  28. class BasicType : public AtomicType {
  29. public:
  30. static BasicType VOID;
  31. static BasicType BOOL;
  32. static BasicType CHAR;
  33. static BasicType SHORT;
  34. static BasicType INT;
  35. static BasicType LONG;
  36. static BasicType FLOAT;
  37. static BasicType DOUBLE;
  38. private:
  39. BasicType(const std::string& name)
  40. {
  41. this->name = name;
  42. }
  43. };
  44. class Type {
  45. public:
  46. Type()
  47. : atomic_type(0), _unsigned(false), _const(false), _static(false),
  48. pointer(0), ref(0)
  49. { }
  50. void write_c_type(std::ostream& out)
  51. {
  52. if(_static)
  53. out << "static ";
  54. if(_const)
  55. out << "const ";
  56. atomic_type->write_c(out);
  57. for(int i = 0; i < pointer; ++i)
  58. out << "*";
  59. for(int i = 0; i < ref; ++i)
  60. out << "&";
  61. }
  62. bool is_void() const
  63. {
  64. if(atomic_type == 0)
  65. return true;
  66. if(atomic_type == &BasicType::VOID && pointer == 0)
  67. return true;
  68. return false;
  69. }
  70. AtomicType* atomic_type;
  71. bool _unsigned;
  72. bool _const;
  73. bool _static;
  74. // number of '*' in the type declaration...
  75. int pointer;
  76. // number of '&' in the type declaration...
  77. int ref;
  78. };
  79. class SQIntegerType : public AtomicType {
  80. public:
  81. SQIntegerType()
  82. {
  83. this->name = "SQInteger";
  84. assert(_instance == 0);
  85. _instance = this;
  86. }
  87. virtual ~SQIntegerType()
  88. {
  89. assert(_instance == this);
  90. _instance = nullptr;
  91. }
  92. static SQIntegerType* instance()
  93. {
  94. return _instance;
  95. }
  96. private:
  97. static SQIntegerType* _instance;
  98. };
  99. class HSQUIRRELVMType : public AtomicType {
  100. public:
  101. HSQUIRRELVMType()
  102. {
  103. this->name = "HSQUIRRELVM";
  104. assert(_instance == 0);
  105. _instance = this;
  106. }
  107. virtual ~HSQUIRRELVMType()
  108. {
  109. assert(_instance == this);
  110. _instance = nullptr;
  111. }
  112. static HSQUIRRELVMType* instance()
  113. {
  114. return _instance;
  115. }
  116. private:
  117. static HSQUIRRELVMType* _instance;
  118. };
  119. class StringType : public AtomicType {
  120. public:
  121. StringType()
  122. {
  123. this->name = "string";
  124. assert(_instance == 0);
  125. _instance = this;
  126. }
  127. virtual ~StringType()
  128. {
  129. assert(_instance == this);
  130. _instance = 0;
  131. }
  132. static StringType* instance()
  133. {
  134. return _instance;
  135. }
  136. virtual void write_c(std::ostream& out)
  137. {
  138. out << "std::string";
  139. }
  140. private:
  141. static StringType* _instance;
  142. };
  143. class Parameter
  144. {
  145. public:
  146. Parameter() :
  147. name(),
  148. type()
  149. { }
  150. std::string name;
  151. Type type;
  152. };
  153. class ClassMember {
  154. public:
  155. ClassMember() :
  156. visibility()
  157. { }
  158. virtual ~ClassMember()
  159. { }
  160. enum Visibility {
  161. PUBLIC,
  162. PROTECTED,
  163. PRIVATE
  164. };
  165. Visibility visibility;
  166. };
  167. class Function : public ClassMember {
  168. public:
  169. Function() :
  170. type(),
  171. suspend(),
  172. custom(),
  173. parameter_spec(),
  174. docu_comment(),
  175. name(),
  176. return_type(),
  177. parameters()
  178. {
  179. type = FUNCTION;
  180. suspend = false;
  181. custom = false;
  182. }
  183. enum FuncType {
  184. FUNCTION,
  185. CONSTRUCTOR,
  186. DESTRUCTOR
  187. };
  188. FuncType type;
  189. /// function should suspend squirrel VM after execution
  190. bool suspend;
  191. /// a custom wrapper (just pass along HSQUIRRELVM)
  192. bool custom;
  193. std::string parameter_spec;
  194. std::string docu_comment;
  195. std::string name;
  196. Type return_type;
  197. std::vector<Parameter> parameters;
  198. };
  199. class Field : public ClassMember {
  200. public:
  201. Field() :
  202. type(),
  203. docu_comment(),
  204. name(),
  205. has_const_value(),
  206. const_float_value(),
  207. const_int_value(),
  208. const_string_value()
  209. {
  210. has_const_value = false;
  211. }
  212. Type* type;
  213. std::string docu_comment;
  214. std::string name;
  215. bool has_const_value;
  216. float const_float_value;
  217. int const_int_value;
  218. std::string const_string_value;
  219. private:
  220. Field(const Field&);
  221. Field& operator=(const Field&);
  222. };
  223. class Class : public AtomicType {
  224. public:
  225. Class() :
  226. members(),
  227. super_classes(),
  228. sub_classes(),
  229. docu_comment()
  230. { }
  231. ~Class() {
  232. for(std::vector<ClassMember*>::iterator i = members.begin(); i != members.end(); ++i)
  233. delete *i;
  234. }
  235. std::vector<ClassMember*> members;
  236. std::vector<Class*> super_classes;
  237. std::vector<Class*> sub_classes;
  238. std::string docu_comment;
  239. };
  240. class Namespace {
  241. public:
  242. Namespace() :
  243. functions(),
  244. fields(),
  245. types(),
  246. namespaces(),
  247. parent(),
  248. name()
  249. {
  250. parent = 0;
  251. }
  252. virtual ~Namespace() {
  253. for(std::vector<Function*>::iterator i = functions.begin();
  254. i != functions.end(); ++i)
  255. delete *i;
  256. for(std::vector<AtomicType*>::iterator i = types.begin();
  257. i != types.end(); ++i)
  258. delete *i;
  259. for(std::vector<Namespace*>::iterator i = namespaces.begin();
  260. i != namespaces.end(); ++i)
  261. delete *i;
  262. }
  263. void add_type(AtomicType* type)
  264. {
  265. types.push_back(type);
  266. type->parent = this;
  267. }
  268. void add_namespace(Namespace* ns)
  269. {
  270. namespaces.push_back(ns);
  271. ns->parent = this;
  272. }
  273. AtomicType* _findType(const std::string& name, bool godown = false) {
  274. for(std::vector<AtomicType*>::iterator i = types.begin();
  275. i != types.end(); ++i) {
  276. AtomicType* type = *i;
  277. if(type->name == name)
  278. return type;
  279. }
  280. if(godown && parent)
  281. return parent->_findType(name, true);
  282. return 0;
  283. }
  284. Namespace* _findNamespace(const std::string& name, bool godown = false) {
  285. for(std::vector<Namespace*>::iterator i = namespaces.begin();
  286. i != namespaces.end(); ++i) {
  287. Namespace* ns = *i;
  288. if(ns->name == name)
  289. return ns;
  290. }
  291. if(godown && parent)
  292. return parent->_findNamespace(name, true);
  293. return 0;
  294. }
  295. Namespace* findNamespace(const std::string& name, bool godown = false) {
  296. Namespace* ret = _findNamespace(name, godown);
  297. if(!ret) {
  298. std::ostringstream msg;
  299. msg << "Couldn't find namespace '" << name << "'.";
  300. throw std::runtime_error(msg.str());
  301. }
  302. return ret;
  303. }
  304. std::vector<Function*> functions;
  305. std::vector<Field*> fields;
  306. std::vector<AtomicType*> types;
  307. std::vector<Namespace*> namespaces;
  308. Namespace* parent;
  309. std::string name;
  310. private:
  311. Namespace(const Namespace&);
  312. Namespace& operator=(const Namespace&);
  313. };
  314. class CompilationUnit : public Namespace {
  315. public:
  316. };
  317. #endif