Ast.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 <map>
  28. #include "Visitor.h"
  29. #include "Util.h"
  30. #include "ErrorReporting.h"
  31. namespace qlow
  32. {
  33. struct CodePosition;
  34. class StructureVisitor;
  35. namespace ast
  36. {
  37. // base class
  38. struct AstObject;
  39. struct Class;
  40. struct Type;
  41. struct ClassType;
  42. struct ArrayType;
  43. struct PointerType;
  44. struct FeatureDeclaration;
  45. struct FieldDeclaration;
  46. struct MethodDefinition;
  47. struct VariableDeclaration;
  48. struct ArgumentDeclaration;
  49. struct Statement;
  50. struct DoEndBlock;
  51. struct IfElseBlock;
  52. struct WhileBlock;
  53. struct Expression;
  54. struct FeatureCall;
  55. struct AssignmentStatement;
  56. struct ReturnStatement;
  57. struct LocalVariableStatement;
  58. struct AddressExpression;
  59. struct IntConst;
  60. struct Operation;
  61. struct UnaryOperation;
  62. struct BinaryOperation;
  63. struct NewArrayExpression;
  64. struct CastExpression;
  65. }
  66. namespace sem
  67. {
  68. struct SemanticObject;
  69. struct Class;
  70. class Scope;
  71. // template<typename T>
  72. // using SymbolTable = std::map<std::string, std::unique_ptr<T>>;
  73. }
  74. }
  75. struct qlow::ast::AstObject :
  76. public Visitable<std::unique_ptr<sem::SemanticObject>, sem::Scope&, StructureVisitor>
  77. {
  78. CodePosition pos;
  79. inline AstObject(const CodePosition& cp) :
  80. pos{ cp } {}
  81. virtual ~AstObject(void);
  82. };
  83. struct qlow::ast::Class : public AstObject
  84. {
  85. std::string name;
  86. OwningList<FeatureDeclaration> features;
  87. inline Class(const std::string& name, OwningList<FeatureDeclaration>& features, const CodePosition& cp) :
  88. AstObject{ cp },
  89. name{ name }, features(std::move(features))
  90. {
  91. }
  92. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&) override;
  93. };
  94. struct qlow::ast::Type : public AstObject
  95. {
  96. inline Type(const CodePosition& cp) :
  97. AstObject{ cp }
  98. {
  99. }
  100. virtual std::string asString(void) const = 0;
  101. virtual inline std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&) override {}
  102. };
  103. struct qlow::ast::ClassType : public ast::Type
  104. {
  105. std::string typeName;
  106. inline ClassType(const std::string& typeName, const CodePosition& cp) :
  107. Type{ cp },
  108. typeName{ typeName }
  109. {
  110. }
  111. inline ClassType(std::string&& typeName, const CodePosition& cp) :
  112. Type{ cp },
  113. typeName{ std::move(typeName) }
  114. {
  115. }
  116. inline std::string asString(void) const override { return typeName; }
  117. };
  118. struct qlow::ast::ArrayType : public ast::Type
  119. {
  120. std::unique_ptr<ast::Type> arrayType;
  121. inline ArrayType(std::unique_ptr<ast::Type> arrayType, const CodePosition& cp) :
  122. Type{ cp },
  123. arrayType{ std::move(arrayType) }
  124. {
  125. }
  126. inline std::string asString(void) const override {
  127. return std::string("[") + arrayType->asString() + "]";
  128. }
  129. };
  130. struct qlow::ast::PointerType : public ast::Type
  131. {
  132. std::unique_ptr<ast::Type> derefType;
  133. inline PointerType(std::unique_ptr<ast::Type> derefType, const CodePosition& cp) :
  134. Type{ cp },
  135. derefType{ std::move(derefType) }
  136. {
  137. }
  138. inline std::string asString(void) const override {
  139. return derefType->asString() + "*";
  140. }
  141. };
  142. struct qlow::ast::FeatureDeclaration : public AstObject
  143. {
  144. std::string name;
  145. std::unique_ptr<ast::Type> type;
  146. inline FeatureDeclaration(std::unique_ptr<ast::Type> type, const std::string& name, const CodePosition& cp) :
  147. AstObject{ cp },
  148. name{ name },
  149. type{ std::move(type) }
  150. {
  151. }
  152. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  153. };
  154. struct qlow::ast::FieldDeclaration : public FeatureDeclaration
  155. {
  156. inline FieldDeclaration(std::unique_ptr<ast::Type> type, const std::string& name, const CodePosition& cp) :
  157. FeatureDeclaration{ std::move(type), name, cp }
  158. {
  159. }
  160. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  161. };
  162. struct qlow::ast::MethodDefinition : public FeatureDeclaration
  163. {
  164. OwningList<ArgumentDeclaration> arguments;
  165. /// pointer to method body. If this is a null pointer, the method has only
  166. /// been declared and not defined (with extern)
  167. std::unique_ptr<DoEndBlock> body;
  168. inline MethodDefinition(std::unique_ptr<ast::Type> type, const std::string& name,
  169. std::unique_ptr<DoEndBlock> body, const CodePosition& cp) :
  170. FeatureDeclaration{ std::move(type), name, cp },
  171. body{ std::move(body) }
  172. {
  173. }
  174. inline MethodDefinition(std::unique_ptr<ast::Type> type, const std::string& name,
  175. OwningList<ArgumentDeclaration>&& arguments, std::unique_ptr<DoEndBlock> body, const CodePosition& cp) :
  176. FeatureDeclaration{ std::move(type), name, cp },
  177. arguments(std::move(arguments)),
  178. body{ std::move(body) }
  179. {
  180. }
  181. inline MethodDefinition(std::unique_ptr<ast::Type> type, const std::string& name,
  182. const CodePosition& cp) :
  183. FeatureDeclaration{ std::move(type), name, cp },
  184. body{ nullptr }
  185. {
  186. }
  187. inline MethodDefinition(std::unique_ptr<ast::Type> type, const std::string& name,
  188. OwningList<ArgumentDeclaration>&& arguments, const CodePosition& cp) :
  189. FeatureDeclaration{ std::move(type), name, cp },
  190. arguments(std::move(arguments)),
  191. body{ nullptr }
  192. {
  193. }
  194. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  195. };
  196. struct qlow::ast::VariableDeclaration : public AstObject
  197. {
  198. std::unique_ptr<ast::Type> type;
  199. std::string name;
  200. inline VariableDeclaration(std::unique_ptr<ast::Type> type, std::string&& name, const CodePosition& cp) :
  201. AstObject{ cp },
  202. type{ std::move(type) },
  203. name{ std::move(name) }
  204. {
  205. }
  206. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  207. };
  208. struct qlow::ast::ArgumentDeclaration :
  209. public VariableDeclaration
  210. {
  211. inline ArgumentDeclaration(std::unique_ptr<ast::Type> type, std::string&& name, const CodePosition& cp) :
  212. VariableDeclaration{ std::move(type), std::move(name), cp }
  213. {
  214. }
  215. //virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  216. };
  217. struct qlow::ast::Statement : public virtual AstObject
  218. {
  219. inline Statement(const CodePosition& cp) :
  220. AstObject{ cp } {}
  221. virtual ~Statement(void);
  222. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  223. };
  224. struct qlow::ast::DoEndBlock : public Statement
  225. {
  226. OwningList<Statement> statements;
  227. inline DoEndBlock(OwningList<Statement>&& statements, const CodePosition& cp) :
  228. AstObject{ cp },
  229. Statement{ cp },
  230. statements(std::move(statements))
  231. {
  232. }
  233. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  234. };
  235. struct qlow::ast::IfElseBlock : public Statement
  236. {
  237. std::unique_ptr<Expression> condition;
  238. std::unique_ptr<DoEndBlock> ifBlock;
  239. std::unique_ptr<DoEndBlock> elseBlock;
  240. inline IfElseBlock(std::unique_ptr<Expression> condition,
  241. std::unique_ptr<DoEndBlock> ifBlock,
  242. std::unique_ptr<DoEndBlock> elseBlock,
  243. const CodePosition& cp) :
  244. AstObject{ cp },
  245. Statement{ cp },
  246. condition{ std::move(condition) },
  247. ifBlock{ std::move(ifBlock) },
  248. elseBlock{ std::move(elseBlock) }
  249. {
  250. }
  251. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  252. };
  253. struct qlow::ast::WhileBlock : public Statement
  254. {
  255. std::unique_ptr<Expression> condition;
  256. std::unique_ptr<DoEndBlock> body;
  257. inline WhileBlock(std::unique_ptr<Expression> condition,
  258. std::unique_ptr<DoEndBlock> body,
  259. const CodePosition& cp) :
  260. AstObject{ cp },
  261. Statement{ cp },
  262. condition{ std::move(condition) },
  263. body{ std::move(body) }
  264. {
  265. }
  266. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  267. };
  268. struct qlow::ast::Expression : public virtual AstObject
  269. {
  270. inline Expression(const CodePosition& cp) :
  271. AstObject{ cp } {}
  272. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  273. };
  274. struct qlow::ast::FeatureCall : public Expression, public Statement
  275. {
  276. std::unique_ptr<Expression> target;
  277. std::string name;
  278. OwningList<Expression> arguments;
  279. inline FeatureCall(std::unique_ptr<Expression> target, const std::string& name, const CodePosition& cp) :
  280. AstObject{ cp },
  281. Expression{ cp }, Statement{ cp },
  282. target(std::move(target)), name(name)
  283. {
  284. }
  285. inline FeatureCall(std::unique_ptr<Expression> target, const std::string& name,
  286. OwningList<Expression>&& arguments, const CodePosition& cp) :
  287. AstObject{ cp },
  288. Expression{ cp }, Statement{ cp },
  289. target(std::move(target)), name(name), arguments(std::move(arguments))
  290. {
  291. }
  292. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  293. };
  294. struct qlow::ast::ReturnStatement : public Statement
  295. {
  296. std::unique_ptr<Expression> expr;
  297. inline ReturnStatement(std::unique_ptr<Expression>&& expr, const CodePosition& cp) :
  298. AstObject{ cp },
  299. Statement{ cp },
  300. expr{ std::move(expr) }
  301. {
  302. }
  303. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  304. };
  305. struct qlow::ast::AssignmentStatement : public Statement
  306. {
  307. std::unique_ptr<Expression> target;
  308. std::unique_ptr<Expression> expr;
  309. inline AssignmentStatement(std::unique_ptr<Expression>&& target, std::unique_ptr<Expression>&& expr, const CodePosition& cp) :
  310. AstObject{ cp },
  311. Statement{ cp },
  312. target{ std::move(target) }, expr{ std::move(expr) }
  313. {
  314. }
  315. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  316. };
  317. struct qlow::ast::LocalVariableStatement : public Statement
  318. {
  319. std::string name;
  320. std::unique_ptr<ast::Type> type;
  321. inline LocalVariableStatement(std::string&& name, std::unique_ptr<Type> type, const CodePosition& cp) :
  322. AstObject{ cp },
  323. Statement{ cp },
  324. name{ name },
  325. type{ std::move(type) }
  326. {
  327. }
  328. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  329. };
  330. struct qlow::ast::AddressExpression : public Expression
  331. {
  332. std::unique_ptr<Expression> target;
  333. inline AddressExpression(std::unique_ptr<Expression> target,
  334. const CodePosition& cp) :
  335. AstObject{ cp },
  336. Expression{ cp },
  337. target{ std::move(target) }
  338. {
  339. }
  340. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  341. };
  342. struct qlow::ast::IntConst : public Expression
  343. {
  344. unsigned long long value;
  345. IntConst(unsigned long long v, const CodePosition& p) :
  346. AstObject(p),
  347. Expression(p),
  348. value{ v } {}
  349. IntConst(std::string&& val, const CodePosition& p);
  350. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  351. };
  352. struct qlow::ast::Operation : public Expression
  353. {
  354. std::string opString;
  355. CodePosition opPos;
  356. inline Operation(const std::string& opString, const CodePosition& cp,
  357. const CodePosition& opPos) :
  358. AstObject{ cp },
  359. Expression{ cp },
  360. opString{ opString },
  361. opPos{ opPos }
  362. {
  363. }
  364. };
  365. struct qlow::ast::UnaryOperation : public Operation
  366. {
  367. enum Side
  368. {
  369. PREFIX,
  370. SUFFIX,
  371. };
  372. Side side;
  373. std::unique_ptr<Expression> expr;
  374. inline UnaryOperation(std::unique_ptr<Expression> expr, Side side,
  375. const std::string& op, const CodePosition& cp,
  376. const CodePosition& opPos
  377. ) :
  378. AstObject{ cp },
  379. Operation{ op, cp, opPos },
  380. side{ side },
  381. expr{ std::move(expr) }
  382. {
  383. }
  384. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  385. };
  386. struct qlow::ast::BinaryOperation : public Operation
  387. {
  388. std::unique_ptr<Expression> left;
  389. std::unique_ptr<Expression> right;
  390. inline BinaryOperation(std::unique_ptr<Expression> left,
  391. std::unique_ptr<Expression> right,
  392. const std::string& op,
  393. const CodePosition& cp,
  394. const CodePosition& opPos
  395. ) :
  396. AstObject{ cp },
  397. Operation{ op, cp, opPos },
  398. left{ std::move(left) },
  399. right{ std::move(right) }
  400. {
  401. }
  402. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  403. };
  404. struct qlow::ast::NewArrayExpression : public Expression
  405. {
  406. std::unique_ptr<ast::Type> type;
  407. std::unique_ptr<Expression> length;
  408. inline NewArrayExpression(std::unique_ptr<ast::Type> type,
  409. std::unique_ptr<Expression> length,
  410. const CodePosition& cp) :
  411. AstObject{ cp },
  412. Expression{ cp },
  413. type{ std::move(type) },
  414. length{ std::move(length) }
  415. {
  416. }
  417. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  418. };
  419. struct qlow::ast::CastExpression : public Expression
  420. {
  421. std::unique_ptr<ast::Expression> expression;
  422. std::unique_ptr<ast::Type> targetType;
  423. inline CastExpression(std::unique_ptr<ast::Expression> expression,
  424. std::unique_ptr<ast::Type> targetType,
  425. const CodePosition& cp) :
  426. AstObject{ cp },
  427. Expression{ cp },
  428. expression{ std::move(expression) },
  429. targetType{ std::move(targetType) }
  430. {
  431. }
  432. virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
  433. };
  434. #endif // QLOW_AST_H