Ast.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // =============================================================================
  2. //
  3. // This file is part of the qlow compiler.
  4. //
  5. // Copyright (C) 2014-2015 Nicolas Winkler
  6. //
  7. // This program is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. //
  20. // =============================================================================
  21. #ifndef QLOW_AST_H
  22. #define QLOW_AST_H
  23. #include <string>
  24. #include <vector>
  25. #include <memory>
  26. #include <utility>
  27. #include "Visitor.h"
  28. namespace qlow
  29. {
  30. class AstVisitor;
  31. namespace ast
  32. {
  33. template<typename T>
  34. using List = std::vector<std::unique_ptr<T>>;
  35. // base class
  36. struct AstObject;
  37. struct Class;
  38. struct FeatureDeclaration;
  39. struct FieldDeclaration;
  40. struct MethodDefinition;
  41. struct VariableDeclaration;
  42. struct ArgumentDeclaration;
  43. struct DoEndBlock;
  44. struct Statement;
  45. struct Expression;
  46. struct FeatureCall;
  47. struct AssignmentStatement;
  48. struct NewVariableStatement;
  49. struct Operation;
  50. struct UnaryOperation;
  51. struct BinaryOperation;
  52. }
  53. namespace sem
  54. {
  55. struct SemanticObject;
  56. }
  57. }
  58. struct qlow::ast::AstObject :
  59. public Visitable<std::unique_ptr<sem::SemanticObject>, AstVisitor>
  60. {
  61. virtual ~AstObject(void);
  62. };
  63. struct qlow::ast::Class : public AstObject
  64. {
  65. std::string name;
  66. List<FeatureDeclaration> features;
  67. inline Class(const std::string& name, List<FeatureDeclaration>& features) :
  68. name(name), features(std::move(features))
  69. {
  70. }
  71. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  72. };
  73. struct qlow::ast::FeatureDeclaration : public AstObject
  74. {
  75. std::string name;
  76. std::string type;
  77. inline FeatureDeclaration(const std::string& type, const std::string& name) :
  78. name(name), type(type)
  79. {
  80. }
  81. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  82. };
  83. struct qlow::ast::FieldDeclaration : public FeatureDeclaration
  84. {
  85. inline FieldDeclaration(const std::string& type, const std::string& name) :
  86. FeatureDeclaration(type, name)
  87. {
  88. }
  89. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  90. };
  91. struct qlow::ast::MethodDefinition : public FeatureDeclaration
  92. {
  93. List<ArgumentDeclaration> arguments;
  94. std::unique_ptr<DoEndBlock> body;
  95. inline MethodDefinition(const std::string& type, const std::string& name,
  96. std::unique_ptr<DoEndBlock> body) :
  97. FeatureDeclaration(type, name),
  98. body(std::move(body))
  99. {
  100. }
  101. inline MethodDefinition(const std::string& type, const std::string& name,
  102. List<ArgumentDeclaration>&& arguments, std::unique_ptr<DoEndBlock> body) :
  103. FeatureDeclaration(type, name),
  104. arguments(std::move(arguments)),
  105. body(std::move(body))
  106. {
  107. }
  108. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  109. };
  110. struct qlow::ast::VariableDeclaration : public AstObject
  111. {
  112. std::string type;
  113. std::string name;
  114. inline VariableDeclaration(const std::string& type, const std::string& name) :
  115. type(type), name(name)
  116. {
  117. }
  118. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  119. };
  120. struct qlow::ast::ArgumentDeclaration :
  121. public VariableDeclaration
  122. {
  123. inline ArgumentDeclaration(const std::string& type, const std::string& name) :
  124. VariableDeclaration(type, name)
  125. {
  126. }
  127. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  128. };
  129. struct qlow::ast::DoEndBlock : public AstObject
  130. {
  131. List<Statement> statements;
  132. inline DoEndBlock(List<Statement>&& statements) :
  133. statements(std::move(statements))
  134. {
  135. }
  136. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  137. };
  138. struct qlow::ast::Statement : public virtual AstObject
  139. {
  140. virtual ~Statement(void);
  141. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  142. };
  143. struct qlow::ast::Expression : public virtual AstObject
  144. {
  145. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  146. };
  147. struct qlow::ast::FeatureCall : public Expression, public Statement
  148. {
  149. std::unique_ptr<Expression> target;
  150. std::string name;
  151. List<Expression> arguments;
  152. inline FeatureCall(std::unique_ptr<Expression> target, const std::string& name) :
  153. target(std::move(target)), name(name)
  154. {
  155. }
  156. inline FeatureCall(std::unique_ptr<Expression> target, const std::string& name,
  157. List<Expression>&& arguments) :
  158. target(std::move(target)), name(name), arguments(std::move(arguments))
  159. {
  160. }
  161. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  162. };
  163. struct qlow::ast::AssignmentStatement : public Statement
  164. {
  165. std::string target;
  166. std::unique_ptr<Expression> expr;
  167. inline AssignmentStatement(const std::string& target, std::unique_ptr<Expression> expr) :
  168. target(target), expr(std::move(expr))
  169. {
  170. }
  171. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  172. };
  173. struct qlow::ast::NewVariableStatement : public Statement
  174. {
  175. std::string name;
  176. std::string type;
  177. inline NewVariableStatement(const std::string& name, const std::string& type) :
  178. name(name), type(type)
  179. {
  180. }
  181. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  182. };
  183. struct qlow::ast::Operation : public Expression
  184. {
  185. enum Operator {
  186. PLUS, MINUS, ASTERISK, SLASH
  187. };
  188. Operator op;
  189. inline Operation(Operator op) :
  190. op(op)
  191. {
  192. }
  193. };
  194. struct qlow::ast::UnaryOperation : public Operation
  195. {
  196. enum Side
  197. {
  198. PREFIX,
  199. SUFFIX,
  200. };
  201. Side side;
  202. std::unique_ptr<Expression> expr;
  203. inline UnaryOperation(std::unique_ptr<Expression> expr, Side side, Operator op) :
  204. Operation(op),
  205. side(side),
  206. expr(std::move(expr))
  207. {
  208. }
  209. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  210. };
  211. struct qlow::ast::BinaryOperation : public Operation
  212. {
  213. std::unique_ptr<Expression> left;
  214. std::unique_ptr<Expression> right;
  215. inline BinaryOperation(std::unique_ptr<Expression> left, std::unique_ptr<Expression> right, Operator op) :
  216. Operation(op),
  217. left(std::move(left)), right(std::move(right))
  218. {
  219. }
  220. std::unique_ptr<sem::SemanticObject> accept(AstVisitor& v);
  221. };
  222. #endif // QLOW_AST_H