AstVisitor.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "AstVisitor.h"
  2. #include "Ast.h"
  3. #include "ErrorReporting.h"
  4. #include <typeinfo>
  5. #include "Util.h"
  6. using namespace qlow;
  7. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Class& ast, sem::Scope& scope)
  8. {
  9. //auto c = std::make_unique<sem::Class>();
  10. //c->name = ast.name;
  11. //return c;
  12. throw "shouldn't be called";
  13. }
  14. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureDeclaration& ast, sem::Scope& scope)
  15. {
  16. // not needed, because
  17. throw "shouldn't be called";
  18. }
  19. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FieldDeclaration& ast, sem::Scope& scope)
  20. {
  21. auto f = std::make_unique<sem::Field>();
  22. f->name = ast.name;
  23. auto* type = scope.getType(*ast.type);
  24. if (type) {
  25. f->type = type;
  26. }
  27. else {
  28. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  29. ast.type->asString(),
  30. ast.type->pos
  31. );
  32. }
  33. return f;
  34. }
  35. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::MethodDefinition& ast, sem::Scope& scope)
  36. {
  37. auto returnType = scope.getType(*ast.type);
  38. if (!returnType) {
  39. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  40. ast.type->asString(),
  41. ast.type->pos
  42. );
  43. }
  44. auto m = std::make_unique<sem::Method>(scope, returnType);
  45. m->name = ast.name;
  46. m->astNode = &ast;
  47. for (auto& arg : ast.arguments) {
  48. auto var = arg->accept(*this, scope);
  49. if (dynamic_cast<sem::Variable*>(var.get())) {
  50. std::unique_ptr<sem::Variable> variable =
  51. unique_dynamic_cast<sem::Variable>(std::move(var));
  52. m->arguments.push_back(variable.get());
  53. std::string varname = variable->name;
  54. m->scope.putVariable(varname, std::move(variable));
  55. }
  56. else {
  57. throw "internal error creating argument";
  58. }
  59. }
  60. return m;
  61. //throw " std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::MethodDefinition& ast, sem::Scope& scope) shouldn't be called";
  62. }
  63. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::VariableDeclaration& ast, sem::Scope& scope)
  64. {
  65. auto v = std::make_unique<sem::Variable>();
  66. v->name = ast.name;
  67. auto* type = scope.getType(*ast.type);
  68. if (type) {
  69. v->type = type;
  70. }
  71. else {
  72. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  73. ast.type->asString(),
  74. ast.type->pos
  75. );
  76. }
  77. return v;
  78. }
  79. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Statement& ast, sem::Scope& scope)
  80. {
  81. printf("at: %d:%d to %d:%d\n", ast.pos.first_line, ast.pos.first_column, ast.pos.last_line, ast.pos.last_column);
  82. printf("type: %s\n", typeid(ast).name());
  83. throw "visit(Statement) shouldn't be called";
  84. }
  85. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::DoEndBlock& ast, sem::Scope& scope)
  86. {
  87. auto body = std::make_unique<sem::DoEndBlock>(scope);
  88. for (auto& statement : ast.statements) {
  89. if (ast::LocalVariableStatement* nvs = dynamic_cast<ast::LocalVariableStatement*>(statement.get()); nvs) {
  90. auto* type = body->scope.getType(*nvs->type);
  91. if (!type)
  92. throw SemanticError(SemanticError::UNKNOWN_TYPE,
  93. nvs->type->asString(),
  94. nvs->type->pos);
  95. auto var = std::make_unique<sem::Variable>(type, nvs->name);
  96. body->scope.putVariable(nvs->name, std::move(var));
  97. continue;
  98. }
  99. auto v = statement->accept(*this, body->scope);
  100. if (dynamic_cast<sem::FeatureCallExpression*>(v.get()) != nullptr) {
  101. body->statements.push_back(
  102. std::make_unique<sem::FeatureCallStatement>(
  103. unique_dynamic_cast<sem::FeatureCallExpression>(std::move(v))));
  104. }
  105. else {
  106. body->statements.push_back(unique_dynamic_cast<sem::Statement>(std::move(v)));
  107. }
  108. }
  109. return body;
  110. }
  111. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::IfElseBlock& ast, sem::Scope& scope)
  112. {
  113. auto condition = ast.condition->accept(*this, scope);
  114. auto ifB = ast.ifBlock->accept(*this, scope);
  115. auto eB= ast.elseBlock->accept(*this, scope);
  116. if (!dynamic_cast<sem::DoEndBlock*>(ifB.get())
  117. || !dynamic_cast<sem::DoEndBlock*>(eB.get())
  118. || !dynamic_cast<sem::Expression*>(condition.get()))
  119. throw "internal error, invalid if block";
  120. auto condExpr = unique_dynamic_cast<sem::Expression>(std::move(condition));
  121. auto ifBBlock = unique_dynamic_cast<sem::DoEndBlock>(std::move(ifB));
  122. auto eBBlock= unique_dynamic_cast<sem::DoEndBlock>(std::move(eB));
  123. auto ieb = std::make_unique<sem::IfElseBlock>(std::move(condExpr), std::move(ifBBlock), std::move(eBBlock));
  124. return ieb;
  125. }
  126. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::WhileBlock& ast, sem::Scope& scope)
  127. {
  128. auto condition = ast.condition->accept(*this, scope);
  129. auto body = ast.body->accept(*this, scope);
  130. if (!dynamic_cast<sem::DoEndBlock*>(body.get()) ||
  131. !dynamic_cast<sem::Expression*>(condition.get()))
  132. throw "internal error, invalid while block";
  133. auto condExpr = unique_dynamic_cast<sem::Expression>(std::move(condition));
  134. auto bodyblock = unique_dynamic_cast<sem::DoEndBlock>(std::move(body));
  135. auto wb = std::make_unique<sem::WhileBlock>(std::move(condExpr), std::move(bodyblock));
  136. return wb;
  137. }
  138. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Expression& ast, sem::Scope& scope)
  139. {
  140. throw "visit(Expression) shouldn't be called";
  141. }
  142. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureCall& ast, sem::Scope& scope)
  143. {
  144. std::unique_ptr<sem::SemanticObject> target = nullptr;
  145. if (ast.target) {
  146. target = ast.target->accept(*this, scope);
  147. }
  148. auto* method = scope.getMethod(ast.name);
  149. auto* var = scope.getVariable(ast.name);
  150. if (var) {
  151. auto lve = std::make_unique<sem::LocalVariableExpression>();
  152. lve->var = var;
  153. return lve;
  154. }
  155. else if (method) {
  156. auto fce = std::make_unique<sem::FeatureCallExpression>();
  157. for (auto& arg : ast.arguments) {
  158. auto argument = arg->accept(*this, scope);
  159. if (dynamic_cast<sem::Expression*>(argument.get())) {
  160. fce->arguments.push_back(unique_dynamic_cast<sem::Expression>(std::move(argument)));
  161. }
  162. else {
  163. throw "internal error: non-expression passed as function parameter";
  164. }
  165. }
  166. fce->callee = method;
  167. return fce;
  168. }
  169. else {
  170. #ifdef DEBUGGING
  171. printf("var not found: %s\n", ast.name.c_str());
  172. printf("current scope: %s\n", scope.toString().c_str());
  173. #endif
  174. throw SemanticError(SemanticError::FEATURE_NOT_FOUND, ast.name, ast.pos);
  175. }
  176. }
  177. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::AssignmentStatement& ast, sem::Scope& scope)
  178. {
  179. auto as = std::make_unique<sem::AssignmentStatement>();
  180. // as->value = unique_dynamic_cast<sem::Expression>(visit(*ast.expr, classes));
  181. // as->target = unique_dynamic_cast<sem::Expression>(visit(*ast.target, classes));
  182. as->value = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  183. as->target = unique_dynamic_cast<sem::Expression>(ast.target->accept(*this, scope));
  184. return as;
  185. }
  186. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::ReturnStatement& ast, sem::Scope& scope)
  187. {
  188. auto as = std::make_unique<sem::ReturnStatement>();
  189. as->value = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  190. return as;
  191. }
  192. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::LocalVariableStatement& ast, sem::Scope& scope)
  193. {
  194. throw "shouldn't be called";
  195. }
  196. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::IntConst& ast, sem::Scope& scope)
  197. {
  198. return std::make_unique<sem::IntConst>(ast.value);
  199. }
  200. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::UnaryOperation& ast, sem::Scope& scope)
  201. {
  202. auto ret = std::make_unique<sem::UnaryOperation>();
  203. ret->op = ast.op;
  204. ret->side = ast.side;
  205. ret->arg = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  206. return ret;
  207. }
  208. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::BinaryOperation& ast, sem::Scope& scope)
  209. {
  210. auto ret = std::make_unique<sem::BinaryOperation>();
  211. ret->op = ast.op;
  212. ret->left = unique_dynamic_cast<sem::Expression>(ast.left->accept(*this, scope));
  213. ret->right = unique_dynamic_cast<sem::Expression>(ast.right->accept(*this, scope));
  214. return ret;
  215. }
  216. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::NewArrayExpression& ast, sem::Scope& scope)
  217. {
  218. auto ret = std::make_unique<sem::BinaryOperation>();
  219. return ret;
  220. }