ast.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // ast.h
  4. //
  5. //////////////////////////////////////////////////////////////////////////////
  6. #ifndef _ast_h_
  7. #define _ast_h_
  8. class Value;
  9. class Delay;
  10. class Environment;
  11. class Expression;
  12. //////////////////////////////////////////////////////////////////////////////
  13. //
  14. // Type
  15. //
  16. //////////////////////////////////////////////////////////////////////////////
  17. class PolymorphicType;
  18. class BaseType;
  19. class FunctionType;
  20. class ListType;
  21. class PairType;
  22. class Type : public IObject {
  23. public:
  24. virtual ZString GetString(int& id) = 0;
  25. virtual bool IsMatchNotPolymorphic(Type* ptype) = 0;
  26. virtual bool IsMatch(Type* ptype)
  27. {
  28. return ptype->IsMatchNotPolymorphic(this);
  29. }
  30. virtual BaseType* GetBaseType() { return NULL; };
  31. virtual FunctionType* GetFunctionType() { return NULL; };
  32. virtual ListType* GetListType() { return NULL; };
  33. virtual PairType* GetPairType() { return NULL; };
  34. virtual PolymorphicType* GetPolymorphicType() { return NULL; };
  35. };
  36. class PolymorphicType : public Type {
  37. private:
  38. TRef<Type> m_ptype;
  39. ZString m_str;
  40. public:
  41. PolymorphicType()
  42. {
  43. }
  44. ZString GetString(int& id)
  45. {
  46. if (m_ptype) {
  47. return m_ptype->GetString(id);
  48. } else {
  49. if (m_str.IsEmpty()) {
  50. char ch = id;
  51. m_str = "'" + ZString(&ch, 1);
  52. }
  53. return m_str;
  54. }
  55. }
  56. bool IsMatchNotPolymorphic(Type* ptype)
  57. {
  58. return IsMatch(ptype);
  59. }
  60. bool IsMatch(Type* ptypeArg)
  61. {
  62. if (m_ptype) {
  63. return m_ptype->IsMatch(ptypeArg);
  64. } else {
  65. Type* ptype = ptypeArg;
  66. while (ptype != NULL) {
  67. TRef<PolymorphicType> ppolymorphicType = ptype->GetPolymorphicType();
  68. if (ppolymorphicType != NULL) {
  69. if (ppolymorphicType == this) {
  70. return true;
  71. }
  72. } else {
  73. break;
  74. }
  75. ptype = ppolymorphicType->m_ptype;
  76. }
  77. m_ptype = ptypeArg;
  78. return true;
  79. }
  80. }
  81. BaseType* GetBaseType()
  82. {
  83. if (m_ptype) {
  84. return m_ptype->GetBaseType();
  85. } else {
  86. return NULL;
  87. }
  88. };
  89. FunctionType* GetFunctionType()
  90. {
  91. if (m_ptype) {
  92. return m_ptype->GetFunctionType();
  93. } else {
  94. return NULL;
  95. }
  96. };
  97. ListType* GetListType()
  98. {
  99. if (m_ptype) {
  100. return m_ptype->GetListType();
  101. } else {
  102. return NULL;
  103. }
  104. };
  105. PairType* GetPairType()
  106. {
  107. if (m_ptype) {
  108. return m_ptype->GetPairType();
  109. } else {
  110. return NULL;
  111. }
  112. };
  113. PolymorphicType* GetPolymorphicType()
  114. {
  115. return this;
  116. }
  117. };
  118. class BaseType : public Type {
  119. private:
  120. ZString m_str;
  121. public:
  122. BaseType(const ZString& str) :
  123. m_str(str)
  124. {
  125. }
  126. ZString GetString(int& id)
  127. {
  128. return m_str;
  129. }
  130. BaseType* GetBaseType()
  131. {
  132. return this;
  133. }
  134. bool IsMatchNotPolymorphic(Type* ptypeArg)
  135. {
  136. TRef<BaseType> ptype = ptypeArg->GetBaseType();
  137. if (ptype != NULL) {
  138. return m_str == ptype->m_str;
  139. }
  140. return false;
  141. }
  142. };
  143. class FunctionType : public Type {
  144. private:
  145. TRef<Type> m_ptypeArg;
  146. TRef<Type> m_ptypeResult;
  147. public:
  148. FunctionType(Type* ptypeArg, Type* ptypeResult) :
  149. m_ptypeArg(ptypeArg),
  150. m_ptypeResult(ptypeResult)
  151. {
  152. }
  153. Type* GetArgType()
  154. {
  155. return m_ptypeArg;
  156. }
  157. Type* GetResultType()
  158. {
  159. return m_ptypeResult;
  160. }
  161. ZString GetString(int& id)
  162. {
  163. return m_ptypeArg->GetString(id) + " -> " + m_ptypeResult->GetString(id);
  164. }
  165. FunctionType* GetFunctionType()
  166. {
  167. return this;
  168. }
  169. bool IsMatchNotPolymorphic(Type* ptypeArg)
  170. {
  171. TRef<FunctionType> ptype = ptypeArg->GetFunctionType();
  172. if (ptype != NULL) {
  173. return m_ptypeArg->IsMatch(ptype->m_ptypeArg) && m_ptypeResult->IsMatch(ptype->m_ptypeResult);
  174. }
  175. return false;
  176. }
  177. };
  178. class ListType : public Type {
  179. private:
  180. TRef<Type> m_ptype;
  181. public:
  182. ListType(Type* ptype) :
  183. m_ptype(ptype)
  184. {
  185. }
  186. Type* GetType()
  187. {
  188. return m_ptype;
  189. }
  190. ZString GetString(int& id)
  191. {
  192. return m_ptype->GetString(id) + " list";
  193. }
  194. ListType* GetListType()
  195. {
  196. return this;
  197. }
  198. bool IsMatchNotPolymorphic(Type* ptypeArg)
  199. {
  200. TRef<ListType> ptype = ptypeArg->GetListType();
  201. if (ptype != NULL) {
  202. return m_ptype->IsMatch(ptype->m_ptype);
  203. }
  204. return false;
  205. }
  206. };
  207. class PairType : public Type {
  208. private:
  209. TRef<Type> m_ptypeFirst;
  210. TRef<Type> m_ptypeSecond;
  211. public:
  212. PairType(Type* ptypeFirst, Type* ptypeSecond) :
  213. m_ptypeFirst(ptypeFirst),
  214. m_ptypeSecond(ptypeSecond)
  215. {
  216. }
  217. Type* GetFirstType()
  218. {
  219. return m_ptypeFirst;
  220. }
  221. Type* GetSecondType()
  222. {
  223. return m_ptypeSecond;
  224. }
  225. ZString GetString(int& id)
  226. {
  227. return
  228. m_ptypeFirst->GetString(id)
  229. + ZString(", " )
  230. + m_ptypeSecond->GetString(id);
  231. }
  232. PairType* GetPairType()
  233. {
  234. return this;
  235. }
  236. bool IsMatchNotPolymorphic(Type* ptypeArg)
  237. {
  238. TRef<PairType> ptype = ptypeArg->GetPairType();
  239. if (ptype != NULL) {
  240. return m_ptypeFirst->IsMatch(ptype->m_ptypeFirst) && m_ptypeSecond->IsMatch(ptype->m_ptypeSecond);
  241. }
  242. return false;
  243. }
  244. };
  245. //////////////////////////////////////////////////////////////////////////////
  246. //
  247. // Value
  248. //
  249. //////////////////////////////////////////////////////////////////////////////
  250. class Value : public IObject {
  251. public:
  252. virtual TRef<Type> GetType() { ZUnimplemented(); return NULL; }
  253. virtual ZString GetString() = 0;
  254. };
  255. //////////////////////////////////////////////////////////////////////////////
  256. //
  257. // Expression
  258. //
  259. //////////////////////////////////////////////////////////////////////////////
  260. class Expression : public IObject {
  261. public:
  262. virtual bool IsValue();
  263. virtual Value* GetValue();
  264. virtual TRef<Type> GetType(Environment* penv) = 0;
  265. virtual TRef<Delay> Evaluate(Environment* penv) = 0;
  266. };
  267. TRef<Expression> CreateSymbolExpression(const ZString& str);
  268. TRef<Expression> CreateApplyExpression(Expression* pexprFunc, Expression* pexprArg);
  269. TRef<Expression> CreateFunctionExpression(const ZString& str, Expression* pexpr);
  270. TRef<Expression> CreateLetExpression(Environment* penv, Expression* pexprIn);
  271. TRef<Expression> CreateValueExpression(Value* pvalue);
  272. //////////////////////////////////////////////////////////////////////////////
  273. //
  274. // Delay
  275. //
  276. //////////////////////////////////////////////////////////////////////////////
  277. class Delay : public IObject {
  278. public:
  279. virtual Expression* GetExpression() = 0;
  280. virtual TRef<Value> Evaluate() = 0;
  281. virtual TRef<Type> GetType() = 0;
  282. };
  283. TRef<Delay> CreateDelay(Environment* penv, Expression* pexpr);
  284. TRef<Delay> CreateTypeDelay(Type* ptype);
  285. //////////////////////////////////////////////////////////////////////////////
  286. //
  287. // Environment
  288. //
  289. //////////////////////////////////////////////////////////////////////////////
  290. class Environment : public IObject {
  291. private:
  292. ZString m_str;
  293. TRef<Delay> m_pdelay;
  294. TRef<Environment> m_penv;
  295. public:
  296. Environment(Environment* penv, const ZString& str, Delay* pdelay);
  297. const ZString& GetString()
  298. {
  299. return m_str;
  300. }
  301. Environment* GetNext()
  302. {
  303. return m_penv;
  304. }
  305. void SetNext(Environment* penv)
  306. {
  307. m_penv = penv;
  308. }
  309. Delay* GetDelay()
  310. {
  311. return m_pdelay;
  312. }
  313. Delay* Find(const ZString& str);
  314. Delay* GetArgument(int index);
  315. };
  316. #endif