/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. }
OSZAR »