exprnode.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #ifndef EXPRNODE_H
  2. #define EXPRNODE_H
  3. #include "node.h"
  4. struct ConstNode; //is constant int,float or string
  5. struct ExprNode : public Node{
  6. Type *sem_type;
  7. ExprNode():sem_type(0){}
  8. ExprNode( Type *t ):sem_type( t ){}
  9. ExprNode *castTo( Type *ty,Environ *e );
  10. ExprNode *semant( Environ *e,Type *ty );
  11. virtual ExprNode *semant( Environ *e )=0;
  12. virtual TNode *translate( Codegen *g )=0;
  13. virtual ConstNode *constNode(){ return 0; }
  14. };
  15. struct ExprSeqNode : public Node{
  16. vector<ExprNode*> exprs;
  17. ~ExprSeqNode(){ for( ;exprs.size();exprs.pop_back() ) delete exprs.back(); }
  18. void push_back( ExprNode *e ){ exprs.push_back( e ); }
  19. int size(){ return exprs.size(); }
  20. void semant( Environ *e );
  21. TNode *translate( Codegen *g,bool userlib );
  22. void castTo( DeclSeq *ds,Environ *e,bool userlib );
  23. void castTo( Type *t,Environ *e );
  24. };
  25. #include "varnode.h"
  26. struct CastNode : public ExprNode{
  27. ExprNode *expr;
  28. Type *type;
  29. CastNode( ExprNode *ex,Type *ty ):expr( ex ),type( ty ){}
  30. ~CastNode(){ delete expr; }
  31. ExprNode *semant( Environ *e );
  32. TNode *translate( Codegen *g );
  33. };
  34. struct CallNode : public ExprNode{
  35. string ident,tag;
  36. ExprSeqNode *exprs;
  37. Decl *sem_decl;
  38. CallNode( const string &i,const string &t,ExprSeqNode *e ):ident(i),tag(t),exprs(e){}
  39. ~CallNode(){ delete exprs; }
  40. ExprNode *semant( Environ *e );
  41. TNode *translate( Codegen *g );
  42. };
  43. struct VarExprNode : public ExprNode{
  44. VarNode *var;
  45. VarExprNode( VarNode *v ):var(v){}
  46. ~VarExprNode(){ delete var; }
  47. ExprNode *semant( Environ *e );
  48. TNode *translate( Codegen *g );
  49. };
  50. struct ConstNode : public ExprNode{
  51. ExprNode *semant( Environ *e ){ return this; }
  52. ConstNode *constNode(){ return this; }
  53. virtual int intValue()=0;
  54. virtual float floatValue()=0;
  55. virtual string stringValue()=0;
  56. };
  57. struct IntConstNode : public ConstNode{
  58. int value;
  59. IntConstNode( int n );
  60. TNode *translate( Codegen *g );
  61. int intValue();
  62. float floatValue();
  63. string stringValue();
  64. };
  65. struct FloatConstNode : public ConstNode{
  66. float value;
  67. FloatConstNode( float f );
  68. TNode *translate( Codegen *g );
  69. int intValue();
  70. float floatValue();
  71. string stringValue();
  72. };
  73. struct StringConstNode : public ConstNode{
  74. string value;
  75. StringConstNode( const string &s );
  76. TNode *translate( Codegen *g );
  77. int intValue();
  78. float floatValue();
  79. string stringValue();
  80. };
  81. struct UniExprNode : public ExprNode{
  82. int op;ExprNode *expr;
  83. UniExprNode( int op,ExprNode *expr ):op( op ),expr( expr ){}
  84. ~UniExprNode(){ delete expr; }
  85. ExprNode *constize();
  86. ExprNode *semant( Environ *e );
  87. TNode *translate( Codegen *g );
  88. };
  89. // and, or, eor, lsl, lsr, asr
  90. struct BinExprNode : public ExprNode{
  91. int op;ExprNode *lhs,*rhs;
  92. BinExprNode( int op,ExprNode *lhs,ExprNode *rhs ):op( op ),lhs( lhs ),rhs( rhs ){}
  93. ~BinExprNode(){ delete lhs;delete rhs; }
  94. ExprNode *semant( Environ *e );
  95. TNode *translate( Codegen *g );
  96. };
  97. // *,/,Mod,+,-
  98. struct ArithExprNode : public ExprNode{
  99. int op;ExprNode *lhs,*rhs;
  100. ArithExprNode( int op,ExprNode *lhs,ExprNode *rhs ):op( op ),lhs( lhs ),rhs( rhs ){}
  101. ~ArithExprNode(){ delete lhs;delete rhs; }
  102. ExprNode *semant( Environ *e );
  103. TNode *translate( Codegen *g );
  104. };
  105. //<,=,>,<=,<>,>=
  106. struct RelExprNode : public ExprNode{
  107. int op;ExprNode *lhs,*rhs;
  108. Type *opType;
  109. RelExprNode( int op,ExprNode *lhs,ExprNode *rhs ):op( op ),lhs( lhs ),rhs( rhs ){}
  110. ~RelExprNode(){ delete lhs;delete rhs; }
  111. ExprNode *semant( Environ *e );
  112. TNode *translate( Codegen *g );
  113. };
  114. struct NewNode : public ExprNode{
  115. string ident;
  116. NewNode( const string &i ):ident(i){}
  117. ExprNode *semant( Environ *e );
  118. TNode *translate( Codegen *g );
  119. };
  120. struct FirstNode : public ExprNode{
  121. string ident;
  122. FirstNode( const string &i ):ident(i){}
  123. ExprNode *semant( Environ *e );
  124. TNode *translate( Codegen *g );
  125. };
  126. struct LastNode : public ExprNode{
  127. string ident;
  128. LastNode( const string &i ):ident(i){}
  129. ExprNode *semant( Environ *e );
  130. TNode *translate( Codegen *g );
  131. };
  132. struct AfterNode : public ExprNode{
  133. ExprNode *expr;
  134. AfterNode( ExprNode *e ):expr(e){}
  135. ~AfterNode(){ delete expr; }
  136. ExprNode *semant( Environ *e );
  137. TNode *translate( Codegen *g );
  138. };
  139. struct BeforeNode : public ExprNode{
  140. ExprNode *expr;
  141. BeforeNode( ExprNode *e ):expr(e){}
  142. ~BeforeNode(){ delete expr; }
  143. ExprNode *semant( Environ *e );
  144. TNode *translate( Codegen *g );
  145. };
  146. struct NullNode : public ExprNode{
  147. ExprNode *semant( Environ *e );
  148. TNode *translate( Codegen *g );
  149. };
  150. struct ObjectCastNode : public ExprNode{
  151. ExprNode *expr;
  152. string type_ident;
  153. ObjectCastNode( ExprNode *e,const string &t ):expr(e),type_ident(t){}
  154. ~ObjectCastNode(){ delete expr; }
  155. ExprNode *semant( Environ *e );
  156. TNode *translate( Codegen *g );
  157. };
  158. struct ObjectHandleNode : public ExprNode{
  159. ExprNode *expr;
  160. ObjectHandleNode( ExprNode *e ):expr(e){}
  161. ~ObjectHandleNode(){ delete expr; }
  162. ExprNode *semant( Environ *e );
  163. TNode *translate( Codegen *g );
  164. };
  165. #endif