diff --git a/include/ast/astnode_visitor.h b/include/ast/astnode_visitor.h index 4a8d225..c0c30ae 100644 --- a/include/ast/astnode_visitor.h +++ b/include/ast/astnode_visitor.h @@ -55,8 +55,19 @@ class ASTNodeVirturalVisitor : public ASTNodeVisitorBase { }; class ASTSemanticCheckVisitor : public ASTNodeVirturalVisitor { + bool is_in_func; + FunctionSchema cur_func_schema; + size_t loop_level; + std::shared_ptr global_scope; + friend std::shared_ptr CheckAndDecorate(std::shared_ptr src); + + bool ClassExists(const std::string &name) { + if (name == "int" || name == "bool") return true; + return global_scope->classes.find(name) != global_scope->classes.end(); + } + public: - ASTSemanticCheckVisitor() = default; + ASTSemanticCheckVisitor() : is_in_func(false), loop_level(0) {} // Structural AST Nodes void ActuralVisit(FuncDef_ASTNode *node) override; void ActuralVisit(ClassDef_ASTNode *node) override; diff --git a/include/ast/scope.hpp b/include/ast/scope.hpp index ff56895..71ecc4e 100644 --- a/include/ast/scope.hpp +++ b/include/ast/scope.hpp @@ -19,6 +19,7 @@ class ScopeBase { ScopeBase *parent; // cannot use std::shared_ptr because of circular dependency virtual bool VariableNameAvailable(const std::string &name, int ttl) = 0; virtual bool add_variable(const std::string &name, const ExprTypeInfo &type) = 0; + virtual ExprTypeInfo fetch_varaible(const std::string &name) = 0; static inline bool IsKeyWord(const std::string &name) { static const std::unordered_set keywords = {"void", "bool", "int", "string", "new", "class", "null", "true", "false", "this", "if", "else", @@ -43,6 +44,12 @@ class LocalScope : public ScopeBase { local_variables[name] = type; return true; } + virtual ExprTypeInfo fetch_varaible(const std::string &name) override { + if (local_variables.find(name) != local_variables.end()) { + return local_variables[name]; + } + return parent->fetch_varaible(name); + } bool VariableNameAvailable(const std::string &name, int ttl) override { if (ttl == 0 && IsKeyWord(name)) { return false; @@ -63,10 +70,19 @@ struct FunctionSchema { class FunctionScope : public ScopeBase { friend std::shared_ptr CheckAndDecorate(std::shared_ptr src); friend class Visitor; + friend class ASTSemanticCheckVisitor; FunctionSchema schema; bool add_variable([[maybe_unused]] const std::string &name, [[maybe_unused]] const ExprTypeInfo &type) override { throw std::runtime_error("FunctionScope does not support add_variable"); } + virtual ExprTypeInfo fetch_varaible(const std::string &name) override { + for (const auto &arg : schema.arguments) { + if (arg.second == name) { + return arg.first; + } + } + return parent->fetch_varaible(name); + } bool VariableNameAvailable(const std::string &name, int ttl) override { if (ttl == 0 && IsKeyWord(name)) { return false; @@ -99,6 +115,12 @@ class ClassDefScope : public ScopeBase { member_variables[name] = type; return true; } + virtual ExprTypeInfo fetch_varaible(const std::string &name) override { + if (member_variables.find(name) != member_variables.end()) { + return member_variables[name]; + } + return parent->fetch_varaible(name); + } bool add_function(const std::string &name, std::shared_ptr ptr) { if (IsKeyWord(name)) return false; if (member_variables.find(name) != member_variables.end()) { @@ -129,6 +151,7 @@ class ClassDefScope : public ScopeBase { }; class GlobalScope : public ScopeBase { friend class Visitor; + friend class ASTSemanticCheckVisitor; friend std::shared_ptr CheckAndDecorate(std::shared_ptr src); std::unordered_map global_variables; std::unordered_map> global_functions; @@ -192,6 +215,12 @@ class GlobalScope : public ScopeBase { } return true; } + virtual ExprTypeInfo fetch_varaible(const std::string &name) override { + if (global_variables.find(name) != global_variables.end()) { + return global_variables[name]; + } + throw SemanticError("Variable " + name + " not found", 1); + } public: GlobalScope() { parent = nullptr; } diff --git a/src/ast/semanticvisitor.cpp b/src/ast/semanticvisitor.cpp index cda8d13..2e770c4 100644 --- a/src/ast/semanticvisitor.cpp +++ b/src/ast/semanticvisitor.cpp @@ -1,12 +1,26 @@ +#include #include "astnode_visitor.h" +#include "scope.hpp" #include "tools.h" // Structural AST Nodes -void ASTSemanticCheckVisitor::ActuralVisit(FuncDef_ASTNode *node) { node->func_body->accept(this); } +void ASTSemanticCheckVisitor::ActuralVisit(FuncDef_ASTNode *node) { + is_in_func = true; + cur_func_schema = std::dynamic_pointer_cast(node->current_scope)->schema; + std::cerr << "enter function " << node->func_name << std::endl; + node->func_body->accept(this); + std::cerr << "leave function " << node->func_name << std::endl; + is_in_func = false; +} void ASTSemanticCheckVisitor::ActuralVisit(ClassDef_ASTNode *node) { + for (auto var : node->member_variables) { + var->accept(this); + } for (auto ch : node->sorted_children) { - ch->accept(this); + if (std::dynamic_pointer_cast(ch) == nullptr) { + ch->accept(this); + } } } @@ -22,9 +36,22 @@ void ASTSemanticCheckVisitor::ActuralVisit(EmptyStatement_ASTNode *node) {} void ASTSemanticCheckVisitor::ActuralVisit(DefinitionStatement_ASTNode *node) { auto cur_scope = node->current_scope; for (const auto &var : node->vars) { + std::string base_type; + if (std::holds_alternative(node->var_type)) { + base_type = std::get(node->var_type); + } else { + base_type = std::get(node->var_type).basetype; + } + if (!ClassExists(base_type)) { + throw SemanticError("Undefined class " + base_type, 1); + } if (!cur_scope->add_variable(var.first, node->var_type)) { throw SemanticError("Variable redefinition for " + var.first, 1); } + if (var.second) { + var.second->accept(this); + // TODO type check + } } } @@ -32,6 +59,7 @@ void ASTSemanticCheckVisitor::ActuralVisit(ExprStatement_ASTNode *node) { node-> void ASTSemanticCheckVisitor::ActuralVisit(IfStatement_ASTNode *node) { node->condition->accept(this); + // TODO type check node->if_clause->accept(this); if (node->has_else_clause) { node->else_clause->accept(this); @@ -40,7 +68,10 @@ void ASTSemanticCheckVisitor::ActuralVisit(IfStatement_ASTNode *node) { void ASTSemanticCheckVisitor::ActuralVisit(WhileStatement_ASTNode *node) { node->condition->accept(this); + // TODO type check + loop_level++; node->loop_body->accept(this); + loop_level--; } void ASTSemanticCheckVisitor::ActuralVisit(ForStatement_ASTNode *node) { @@ -49,14 +80,22 @@ void ASTSemanticCheckVisitor::ActuralVisit(ForStatement_ASTNode *node) { } if (node->condition) { node->condition->accept(this); + // TODO type check } if (node->update) { node->update->accept(this); } + loop_level++; node->loop_body->accept(this); + loop_level--; } -void ASTSemanticCheckVisitor::ActuralVisit(JmpStatement_ASTNode *node) {} +void ASTSemanticCheckVisitor::ActuralVisit(JmpStatement_ASTNode *node) { + if (loop_level == 0 && node->jmp_type > 0) throw SemanticError("Jump statement outside loop", 1); + if (node->jmp_type == 0) { + // TODO : return type check + } +} void ASTSemanticCheckVisitor::ActuralVisit(SuiteStatement_ASTNode *node) { for (auto ch : node->statements) { @@ -67,112 +106,198 @@ void ASTSemanticCheckVisitor::ActuralVisit(SuiteStatement_ASTNode *node) { // Expression AST Nodes void ASTSemanticCheckVisitor::ActuralVisit(NewArrayExpr_ASTNode *node) { // TODO: Implement this method + for (size_t i = 0; i < node->dim_size.size(); i++) { + if (node->dim_size[i]) { + node->dim_size[i]->accept(this); + // TODO type check + } + } + if (node->has_initial_value) { + node->initial_value->accept(this); + // TODO type check + } } -void ASTSemanticCheckVisitor::ActuralVisit(NewConstructExpr_ASTNode *node) { - // TODO: Implement this method -} +void ASTSemanticCheckVisitor::ActuralVisit(NewConstructExpr_ASTNode *node) {} -void ASTSemanticCheckVisitor::ActuralVisit(NewExpr_ASTNode *node) { - // TODO: Implement this method -} +void ASTSemanticCheckVisitor::ActuralVisit(NewExpr_ASTNode *node) {} void ASTSemanticCheckVisitor::ActuralVisit(AccessExpr_ASTNode *node) { // TODO: Implement this method + node->base->accept(this); + // TODO: member check + if (node->is_function) { + // TODO arg number check + for (auto arg : node->arguments) { + arg->accept(this); + // TODO type check + } + } } void ASTSemanticCheckVisitor::ActuralVisit(IndexExpr_ASTNode *node) { // TODO: Implement this method + node->base->accept(this); + // TODO: dimension check + for (auto idx : node->indices) { + idx->accept(this); + // TODO type check + } } void ASTSemanticCheckVisitor::ActuralVisit(SuffixExpr_ASTNode *node) { // TODO: Implement this method + node->base->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(PrefixExpr_ASTNode *node) { // TODO: Implement this method + node->base->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(OppositeExpr_ASTNode *node) { // TODO: Implement this method + node->base->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(LNotExpr_ASTNode *node) { // TODO: Implement this method + node->base->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(BNotExpr_ASTNode *node) { // TODO: Implement this method + node->base->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(MDMExpr_ASTNode *node) { // TODO: Implement this method + node->left->accept(this); + node->right->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(PMExpr_ASTNode *node) { // TODO: Implement this method + node->left->accept(this); + node->right->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(RLExpr_ASTNode *node) { // TODO: Implement this method + node->left->accept(this); + node->right->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(GGLLExpr_ASTNode *node) { // TODO: Implement this method + node->left->accept(this); + node->right->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(NEExpr_ASTNode *node) { // TODO: Implement this method + node->left->accept(this); + node->right->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(BAndExpr_ASTNode *node) { // TODO: Implement this method + node->left->accept(this); + node->right->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(BXorExpr_ASTNode *node) { // TODO: Implement this method + node->left->accept(this); + node->right->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(BOrExpr_ASTNode *node) { // TODO: Implement this method + node->left->accept(this); + node->right->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(LAndExpr_ASTNode *node) { // TODO: Implement this method + node->left->accept(this); + node->right->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(LOrExpr_ASTNode *node) { // TODO: Implement this method + node->left->accept(this); + node->right->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(TernaryExpr_ASTNode *node) { // TODO: Implement this method + node->condition->accept(this); + // TODO: type check + node->src1->accept(this); + node->src2->accept(this); + // TODO: type check } void ASTSemanticCheckVisitor::ActuralVisit(AssignExpr_ASTNode *node) { // TODO: Implement this method + node->dest->accept(this); + node->src->accept(this); + // TODO: check type and assignability } -void ASTSemanticCheckVisitor::ActuralVisit(ThisExpr_ASTNode *node) { - // TODO: Implement this method -} +void ASTSemanticCheckVisitor::ActuralVisit(ThisExpr_ASTNode *node) {} void ASTSemanticCheckVisitor::ActuralVisit(ParenExpr_ASTNode *node) { // TODO: Implement this method + node->expr->accept(this); + node->expr_type_info = node->expr->expr_type_info; } void ASTSemanticCheckVisitor::ActuralVisit(IDExpr_ASTNode *node) { // TODO: Implement this method + // TODO: process type + node->expr_type_info = node->current_scope->fetch_varaible(node->id); } void ASTSemanticCheckVisitor::ActuralVisit(FunctionCallExpr_ASTNode *node) { // TODO: Implement this method + // TODO: check function existence and arg number + for (auto arg : node->arguments) { + arg->accept(this); + // TODO: type check + } } void ASTSemanticCheckVisitor::ActuralVisit(FormattedStringExpr_ASTNode *node) { // TODO: Implement this method + for (auto arg : node->exprs) { + arg->accept(this); + // TODO: type check + } } void ASTSemanticCheckVisitor::ActuralVisit(ConstantExpr_ASTNode *node) { // TODO: Implement this method + // type process + if (std::holds_alternative(node->expr_type_info)) { + return; + } else { + ; + } } diff --git a/src/semantic/semantic.cpp b/src/semantic/semantic.cpp index 87f40dd..2a199bf 100644 --- a/src/semantic/semantic.cpp +++ b/src/semantic/semantic.cpp @@ -35,6 +35,8 @@ std::shared_ptr CheckAndDecorate(std::shared_ptrclasses["string"] = nullptr; // TODO: add string class visitor.visit(src.get()); return src; } diff --git a/src/semantic/visitor.cpp b/src/semantic/visitor.cpp index 9b223bb..2cc3766 100644 --- a/src/semantic/visitor.cpp +++ b/src/semantic/visitor.cpp @@ -675,7 +675,8 @@ std::any Visitor::visitNew_array_expression(MXParser::New_array_expressionContex new_array->has_initial_value = true; new_array->initial_value = std::dynamic_pointer_cast( std::any_cast>(visit(context->constant()))); - } + } else + new_array->has_initial_value = false; nodetype_stk.pop_back(); return std::static_pointer_cast(new_array);