/src/core/type.h

https://github.com/jojoin/Def · C Header · 360 lines · 281 code · 54 blank · 25 comment · 28 complexity · 3e7a4f6ae2567b03ecbc470361097b3c MD5 · raw file

  1. #pragma once
  2. /**
  3. * Def ¶ÔÏóÀàÐÍ
  4. */
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <map>
  9. #include "../global.h"
  10. #include "./splitfix.h"
  11. #include "../util/str.h"
  12. // def µÄÔ­×ÓÀàÐÍÁбí
  13. #define DEF_AOTM_TYPE_LIST(T) \
  14. T(Nil) \
  15. T(Bool) \
  16. T(Int) \
  17. T(Float) \
  18. T(Char) \
  19. T(String) \
  20. // T(Quote) /* ÒýÓÃ */ \
  21. namespace def {
  22. namespace core {
  23. using namespace std;
  24. using namespace def::util;
  25. /**
  26. * Def ¶ÔÏóÀàÐÍ
  27. */
  28. struct Type
  29. {
  30. virtual string str() = 0; // ×Ö·û´®±íʾ
  31. virtual void set(Type*) {}; // ÉèÖÃ type
  32. virtual void add(Type*) {}; // Ôö¼Ó type
  33. virtual size_t len() { return 1; }; // type ³¤¶È
  34. virtual Type* copy(const string& n="") { // ¿½±´ type
  35. return nullptr;
  36. };
  37. virtual bool is(Type*t) = 0; // ÅжÏÁ½¸ö¶ÔÏóÊÇ·ñÏàµÈ
  38. virtual bool isAtomType() = 0; // ÊÇ·ñΪԭ×ÓÀàÐÍ
  39. virtual string getIdentify(bool strict=true) { // »ñµÃΨһ±êʶ
  40. return str();
  41. }
  42. // static function
  43. static map<string, Type*> types; // »º´æµÄÔ­×ÓÀàÐÍ
  44. static Type* get(const string &);
  45. };
  46. // Ô­×ÓÀàÐÍ
  47. #define ATOM_TYPE(N) \
  48. struct Type##N : Type \
  49. { \
  50. virtual bool isAtomType(){ return true; } \
  51. virtual string str() { \
  52. return #N; \
  53. } \
  54. virtual bool is(Type*t){ /* Ô­×ÓÀàÐͲÉÓÃÔËÐÐʱÅÐ¶Ï */ \
  55. return dynamic_cast<Type##N*>(t); \
  56. } \
  57. };
  58. #define AT(T) ATOM_TYPE(T)
  59. DEF_AOTM_TYPE_LIST(AT)
  60. #undef AT
  61. #undef AOTM_TYPE
  62. // À©Õ¹ÀàÐÍ
  63. #define NOATOM_TYPE(N) \
  64. struct Type##N : Type \
  65. { \
  66. virtual bool isAtomType() { return false; };
  67. #define TYPE_POND(T) \
  68. /* Ö¸ÕëÀàÐÍ³Ø */ \
  69. static map<int, Type##T*> typtrs; \
  70. static Type##T* get(Type*t) { \
  71. int adr = (int)t; \
  72. auto it = typtrs.find(adr); \
  73. if (it != typtrs.end()) { \
  74. return it->second; \
  75. } else { \
  76. auto tar = new Type##T(t); \
  77. typtrs.insert(pair<int,Type##T*>(adr,tar)); \
  78. return tar; \
  79. } \
  80. } \
  81. // Ö¸ÕëÀàÐÍ
  82. // ½ö¹©ÄÚ²¿±àÒëÆ÷ʵÏÖʹÓÃ
  83. // ±íÏÖ´Ó¶ÑÉÏ·ÖÅäµÄ¶ÔÏó
  84. NOATOM_TYPE(Pointer)
  85. Type* type = nullptr; // ÒýÓÃÖµµÄÀàÐÍ
  86. TypePointer(Type*t)
  87. : type(t)
  88. {}
  89. virtual string str() {
  90. return getIdentify();
  91. }
  92. virtual string getIdentify(bool strict=true) { // »ñµÃΨһ±êʶ
  93. return "*" + type->getIdentify(strict);
  94. }
  95. virtual bool is(Type*t){
  96. if (auto *ty = dynamic_cast<TypePointer*>(t)) {
  97. return type->is(ty->type); // Ö¸ÏòÀàÐÍÒ»ÖÂ
  98. }
  99. return false;
  100. }
  101. // ÀàÐͳØ
  102. TYPE_POND(Pointer)
  103. };
  104. // ÒýÓÃÀàÐÍ
  105. NOATOM_TYPE(Refer)
  106. // size_t len;
  107. Type* type = nullptr; // ÒýÓÃÖµµÄÀàÐÍ
  108. TypeRefer(Type*t)
  109. : type(t)
  110. {}
  111. virtual bool is(Type*t){
  112. if (auto *ty = dynamic_cast<TypeRefer*>(t)) {
  113. return ty->type->is(type); // ÒýÓÃÀàÐÍÒ»ÖÂ
  114. }
  115. return false;
  116. }
  117. virtual void set(Type* t) { // Ôö¼Ó type
  118. type = t;
  119. }
  120. virtual string str() {
  121. return getIdentify();
  122. }
  123. virtual string getIdentify(bool strict=true) { // »ñµÃΨһ±êʶ
  124. return "~" + type->getIdentify(strict);
  125. }
  126. // ÀàÐͳØ
  127. TYPE_POND(Refer)
  128. };
  129. // Êý×éÀàÐÍ
  130. NOATOM_TYPE(Array)
  131. size_t len;
  132. Type* type;
  133. TypeArray(Type*t, size_t l=0)
  134. : type(t)
  135. , len(l)
  136. {}
  137. virtual string str() {
  138. return getIdentify();
  139. }
  140. virtual string getIdentify(bool strict=true) {
  141. // »ñµÃΨһ±êʶ£¬strict ±íʾ´óСҲ±ØÐëÏàµÈ
  142. return "["
  143. + (strict ? Str::l2s(len) + "x" : "")
  144. + type->getIdentify(strict)
  145. + "]";
  146. }
  147. virtual void set(Type* t) { // Ôö¼Ó type
  148. type = t;
  149. }
  150. virtual bool is(Type*t){
  151. if (auto *ty = dynamic_cast<TypeArray*>(t)) {
  152. return ty->type->is(type); // Êý×éÀàÐÍÒ»ÖÂ
  153. }
  154. return false;
  155. }
  156. // ÀàÐͳØ
  157. TYPE_POND(Array)
  158. };
  159. // À©Õ¹ÀàÐÍ
  160. #define EXTEND_TYPE(N,P) \
  161. struct Type##N : P \
  162. { \
  163. virtual bool isAtomType() { return false; };
  164. // ½á¹¹ÀàÐÍ
  165. EXTEND_TYPE(Struct, Type)
  166. long idx = 0;
  167. static long auto_idx; // ×ÔÔöΨһ±êʶ£¡£¡£¡
  168. string name; // ÀàÃû³Æ
  169. vector<string> tabs; // ×ÓÀà±êʶ·û
  170. vector<Type*> types; // ×ÓÀàÁбí
  171. TypeStruct(const string&n)
  172. : name(n) {
  173. // cout << "TypeStruct(const string&n)"<< n << endl;
  174. }
  175. void increment() {
  176. idx = ++auto_idx;
  177. }
  178. virtual bool is(Type*t){ // À©Õ¹ÀàÐÍÖ±½Ó¶Ô±ÈµØÖ·
  179. if(name!=""){
  180. return !!( ((int)this)==((int)t) );
  181. }
  182. if(auto*sctty=dynamic_cast<TypeStruct*>(t)){
  183. size_t len = types.size();
  184. size_t len1 = sctty->types.size();
  185. if(len!=len1){
  186. return false;
  187. }
  188. while(len--){
  189. if(!types[len]->is(sctty->types[len])){
  190. return false;
  191. }
  192. }
  193. return true; // ÔªËظöÊýÏàµÈ£¬Ã¿¸öÔªËØÏàµÈ
  194. }
  195. return false;
  196. }
  197. virtual string str() {
  198. string s = name+"{";
  199. bool f = true;
  200. for(auto& it : types) {
  201. if (f) {
  202. f = false;
  203. } else {
  204. s += ",";
  205. }
  206. s += it->str();
  207. }
  208. return s+"}";
  209. }
  210. virtual void add(Type* t, const string&n="") { // Ôö¼Ó type
  211. tabs.push_back(n);
  212. types.push_back(t);
  213. }
  214. virtual void front(Type* t, const string&n="") { // Ôö¼Ó type
  215. tabs.insert(tabs.begin(), n);
  216. types.insert(types.begin(), t);
  217. }
  218. virtual size_t len() { // type ³¤¶È
  219. return types.size();
  220. };
  221. virtual Type* copy(const string&n="") { // ¿½±´ type
  222. TypeStruct* nts = new TypeStruct(n);
  223. nts->types = types; // ¿½±´½á¹¹
  224. return nts; // ·µ»Øн¨µÄÀàÐÍ
  225. };
  226. int elmpos(const string&n) { // ×ÓÔªËØÆ«ÒÆ
  227. int i = 0;
  228. for(auto &it : tabs) {
  229. if (it==n) {
  230. return i;
  231. }
  232. i++;
  233. }
  234. return -1;
  235. };
  236. Type* elmget(const string&n) { // ×ÓÔªËØÆ«ÒÆ
  237. int i = 0;
  238. for(auto &it : tabs) {
  239. if (it==n) {
  240. return types[i];
  241. }
  242. i++;
  243. }
  244. return nullptr;
  245. };
  246. virtual string getIdentify(bool strict=true) { // »ñµÃΨһ±êʶ
  247. if(name == ""){ // ÄäÃû½á¹¹
  248. string idf = "";
  249. for(auto t : types){
  250. idf += "." + t->getIdentify();
  251. }
  252. return idf;
  253. }
  254. if(idx == 0){
  255. return name;
  256. }
  257. return name + "." + Str::l2s(idx);
  258. }
  259. };
  260. // º¯ÊýÀàÐÍ
  261. EXTEND_TYPE(Function,TypeStruct)
  262. Type* ret; // ·µ»ØÖµÀàÐÍ
  263. TypeFunction(const string&n, Type*t=nullptr)
  264. : TypeStruct(n)
  265. , ret(t)
  266. {}
  267. virtual bool is(Type*t){ // À©Õ¹ÀàÐÍÖ±½Ó¶Ô±ÈµØÖ·
  268. return !!( ((int)this)==((int)t) );
  269. }
  270. virtual void set(Type* t) { // ÉèÖ÷µ»ØÖµ type
  271. ret = t;
  272. }
  273. virtual string str() {
  274. string s(name);
  275. if (ret) {
  276. s += ": " + ret->getIdentify();
  277. }
  278. s += "(";
  279. bool fx = false;
  280. for(auto& it : types) {
  281. if (fx) s += ",";
  282. fx = true;
  283. s += it->getIdentify();
  284. }
  285. return s+")";
  286. }
  287. virtual string getIdentify(bool strict=true) { // »ñµÃΨһ±êʶ
  288. string identify(name);
  289. for(auto& it : types) {
  290. identify += DEF_SPLITFIX_FUNCARGV+it->getIdentify(strict);
  291. }
  292. return identify;
  293. }
  294. TypeFunction* clone() { // ¿Ë¡
  295. auto *clnew = new TypeFunction("", ret);
  296. size_t len = types.size();
  297. for(int i = 0; i < len; i++){
  298. clnew->tabs.push_back(tabs[i]);
  299. clnew->types.push_back(types[i]);
  300. }
  301. return clnew;
  302. };
  303. };
  304. #undef EXTEND_TYPE
  305. }
  306. }