AstVisitor.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "AstVisitor.h"
  2. #include "Ast.h"
  3. #include <typeinfo>
  4. #include "Util.h"
  5. using namespace qlow;
  6. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Class& ast, sem::Scope& scope)
  7. {
  8. //auto c = std::make_unique<sem::Class>();
  9. //c->name = ast.name;
  10. //return c;
  11. throw "shouldn't be called";
  12. }
  13. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureDeclaration& ast, sem::Scope& scope)
  14. {
  15. // not needed, because
  16. throw "shouldn't be called";
  17. }
  18. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FieldDeclaration& ast, sem::Scope& scope)
  19. {
  20. auto f = std::make_unique<sem::Field>();
  21. f->name = ast.name;
  22. auto type = scope.getType(ast.type);
  23. if (type) {
  24. f->type = type.value();
  25. }
  26. else {
  27. throw sem::SemanticException(sem::SemanticException::UNKNOWN_TYPE,
  28. ast.type,
  29. ast.pos
  30. );
  31. }
  32. return f;
  33. }
  34. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::MethodDefinition& ast, sem::Scope& scope)
  35. {
  36. auto returnType = scope.getType(ast.type);
  37. if (!returnType) {
  38. throw sem::SemanticException(sem::SemanticException::UNKNOWN_TYPE,
  39. ast.type,
  40. ast.pos
  41. );
  42. }
  43. auto m = std::make_unique<sem::Method>(scope, returnType.value());
  44. m->name = ast.name;
  45. m->astNode = &ast;
  46. return m;
  47. //throw " std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::MethodDefinition& ast, sem::Scope& scope) shouldn't be called";
  48. }
  49. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::VariableDeclaration& ast, sem::Scope& scope)
  50. {
  51. auto v = std::make_unique<sem::Variable>();
  52. v->name = ast.name;
  53. auto type = scope.getType(ast.type);
  54. if (type) {
  55. v->type = type.value();
  56. }
  57. else {
  58. throw sem::SemanticException(sem::SemanticException::UNKNOWN_TYPE,
  59. ast.type,
  60. ast.pos
  61. );
  62. }
  63. return v;
  64. }
  65. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::ArgumentDeclaration& ast, sem::Scope& scope)
  66. {
  67. throw "shouldn't be called";
  68. }
  69. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::DoEndBlock& ast, sem::Scope& scope)
  70. {
  71. auto body = std::make_unique<sem::DoEndBlock>(scope);
  72. for (auto& statement : ast.statements) {
  73. if (ast::NewVariableStatement* nvs = dynamic_cast<ast::NewVariableStatement*>(statement.get()); nvs) {
  74. auto type = body->scope.getType(nvs->type);
  75. if (!type)
  76. throw sem::SemanticException(sem::SemanticException::UNKNOWN_TYPE, nvs->type, nvs->pos);
  77. auto var = std::make_unique<sem::Variable>(type.value(), nvs->name);
  78. body->scope.putVariable(nvs->name, std::move(var));
  79. continue;
  80. }
  81. auto v = statement->accept(*this, body->scope);
  82. if (dynamic_cast<sem::FeatureCallExpression*>(v.get()) != nullptr) {
  83. body->statements.push_back(std::make_unique<sem::FeatureCallStatement>(unique_dynamic_cast<sem::FeatureCallExpression>(std::move(v))));
  84. }
  85. else {
  86. body->statements.push_back(unique_dynamic_cast<sem::Statement>(std::move(v)));
  87. }
  88. }
  89. return body;
  90. }
  91. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Statement& ast, sem::Scope& scope)
  92. {
  93. printf("at: %d:%d to %d:%d\n", ast.pos.first_line, ast.pos.first_column, ast.pos.last_line, ast.pos.last_column);
  94. printf("type: %s\n", typeid(ast).name());
  95. throw "visit(Statement) shouldn't be called";
  96. }
  97. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::Expression& ast, sem::Scope& scope)
  98. {
  99. throw "visit(Expression) shouldn't be called";
  100. }
  101. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FeatureCall& ast, sem::Scope& scope)
  102. {
  103. auto* method = scope.getMethod(ast.name);
  104. auto* var = scope.getVariable(ast.name);
  105. if (var) {
  106. auto lve = std::make_unique<sem::LocalVariableExpression>();
  107. lve->var = var;
  108. return lve;
  109. }
  110. else if (method) {
  111. auto fce = std::make_unique<sem::FeatureCallExpression>();
  112. fce->callee = method;
  113. return fce;
  114. }
  115. else {
  116. #ifdef DEBUGGING
  117. printf("var not found: %s\n", ast.name.c_str());
  118. printf("current scope: %s\n", scope.toString().c_str());
  119. #endif
  120. throw sem::SemanticException(sem::SemanticException::FEATURE_NOT_FOUND, ast.name, ast.pos);
  121. }
  122. }
  123. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::AssignmentStatement& ast, sem::Scope& scope)
  124. {
  125. auto as = std::make_unique<sem::AssignmentStatement>();
  126. // as->value = unique_dynamic_cast<sem::Expression>(visit(*ast.expr, classes));
  127. // as->target = unique_dynamic_cast<sem::Expression>(visit(*ast.target, classes));
  128. as->value = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  129. as->target = unique_dynamic_cast<sem::Expression>(ast.target->accept(*this, scope));
  130. return as;
  131. }
  132. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::NewVariableStatement& ast, sem::Scope& scope)
  133. {
  134. throw "shouldn't be called";
  135. }
  136. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::IntConst& ast, sem::Scope& scope)
  137. {
  138. return std::make_unique<sem::IntConst>(ast.value);
  139. }
  140. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::UnaryOperation& ast, sem::Scope& scope)
  141. {
  142. auto ret = std::make_unique<sem::UnaryOperation>();
  143. ret->op = ast.op;
  144. ret->side = ast.side;
  145. ret->arg = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));
  146. return ret;
  147. }
  148. std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::BinaryOperation& ast, sem::Scope& scope)
  149. {
  150. auto ret = std::make_unique<sem::BinaryOperation>();
  151. ret->op = ast.op;
  152. ret->left = unique_dynamic_cast<sem::Expression>(ast.left->accept(*this, scope));
  153. ret->right = unique_dynamic_cast<sem::Expression>(ast.right->accept(*this, scope));
  154. return ret;
  155. }