set up semantic visitor
This commit is contained in:
@ -1 +1,39 @@
|
||||
#include "expr_astnode.h"
|
||||
#include "expr_astnode.h"
|
||||
#include <stdexcept>
|
||||
#include "astnode_visitor.h"
|
||||
#define SetAutoAccept(name) \
|
||||
void name::accept(class ASTNodeVisitorBase *visitor) { \
|
||||
if (auto v = dynamic_cast<ASTNodeVirturalVisitor *>(visitor)) { \
|
||||
v->ActuralVisit(this); \
|
||||
} else \
|
||||
throw std::runtime_error("Cannot automatically dispatch visitor"); \
|
||||
}
|
||||
SetAutoAccept(NewArrayExpr_ASTNode);
|
||||
SetAutoAccept(NewConstructExpr_ASTNode);
|
||||
SetAutoAccept(NewExpr_ASTNode);
|
||||
SetAutoAccept(AccessExpr_ASTNode);
|
||||
SetAutoAccept(IndexExpr_ASTNode);
|
||||
SetAutoAccept(SuffixExpr_ASTNode);
|
||||
SetAutoAccept(PrefixExpr_ASTNode);
|
||||
SetAutoAccept(OppositeExpr_ASTNode);
|
||||
SetAutoAccept(LNotExpr_ASTNode);
|
||||
SetAutoAccept(BNotExpr_ASTNode);
|
||||
SetAutoAccept(MDMExpr_ASTNode);
|
||||
SetAutoAccept(PMExpr_ASTNode);
|
||||
SetAutoAccept(RLExpr_ASTNode);
|
||||
SetAutoAccept(GGLLExpr_ASTNode);
|
||||
SetAutoAccept(NEExpr_ASTNode);
|
||||
SetAutoAccept(BAndExpr_ASTNode);
|
||||
SetAutoAccept(BXorExpr_ASTNode);
|
||||
SetAutoAccept(BOrExpr_ASTNode);
|
||||
SetAutoAccept(LAndExpr_ASTNode);
|
||||
SetAutoAccept(LOrExpr_ASTNode);
|
||||
SetAutoAccept(TernaryExpr_ASTNode);
|
||||
SetAutoAccept(AssignExpr_ASTNode);
|
||||
SetAutoAccept(ThisExpr_ASTNode);
|
||||
SetAutoAccept(ParenExpr_ASTNode);
|
||||
SetAutoAccept(IDExpr_ASTNode);
|
||||
SetAutoAccept(FunctionCallExpr_ASTNode);
|
||||
SetAutoAccept(FormattedStringExpr_ASTNode);
|
||||
SetAutoAccept(ConstantExpr_ASTNode);
|
||||
#undef SetAutoAccept
|
178
src/ast/semanticvisitor.cpp
Normal file
178
src/ast/semanticvisitor.cpp
Normal file
@ -0,0 +1,178 @@
|
||||
#include "astnode_visitor.h"
|
||||
#include "tools.h"
|
||||
|
||||
// Structural AST Nodes
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(FuncDef_ASTNode *node) { node->func_body->accept(this); }
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(ClassDef_ASTNode *node) {
|
||||
for (auto ch : node->sorted_children) {
|
||||
ch->accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(Program_ASTNode *node) {
|
||||
for (auto ch : node->sorted_children) {
|
||||
ch->accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
// Statement AST Nodes
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(EmptyStatement_ASTNode *node) {}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(DefinitionStatement_ASTNode *node) {
|
||||
auto cur_scope = node->current_scope;
|
||||
for (const auto &var : node->vars) {
|
||||
if (!cur_scope->add_variable(var.first, node->var_type)) {
|
||||
throw SemanticError("Variable redefinition for " + var.first, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(ExprStatement_ASTNode *node) { node->expr->accept(this); }
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(IfStatement_ASTNode *node) {
|
||||
node->condition->accept(this);
|
||||
node->if_clause->accept(this);
|
||||
if (node->has_else_clause) {
|
||||
node->else_clause->accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(WhileStatement_ASTNode *node) {
|
||||
node->condition->accept(this);
|
||||
node->loop_body->accept(this);
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(ForStatement_ASTNode *node) {
|
||||
if (node->initial) {
|
||||
node->initial->accept(this);
|
||||
}
|
||||
if (node->condition) {
|
||||
node->condition->accept(this);
|
||||
}
|
||||
if (node->update) {
|
||||
node->update->accept(this);
|
||||
}
|
||||
node->loop_body->accept(this);
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(JmpStatement_ASTNode *node) {}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(SuiteStatement_ASTNode *node) {
|
||||
for (auto ch : node->statements) {
|
||||
ch->accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
// Expression AST Nodes
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(NewArrayExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(NewConstructExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(NewExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(AccessExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(IndexExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(SuffixExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(PrefixExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(OppositeExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(LNotExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(BNotExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(MDMExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(PMExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(RLExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(GGLLExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(NEExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(BAndExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(BXorExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(BOrExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(LAndExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(LOrExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(TernaryExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(AssignExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(ThisExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(ParenExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(IDExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(FunctionCallExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(FormattedStringExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
||||
|
||||
void ASTSemanticCheckVisitor::ActuralVisit(ConstantExpr_ASTNode *node) {
|
||||
// TODO: Implement this method
|
||||
}
|
@ -1 +1,21 @@
|
||||
#include "statement_astnode.h"
|
||||
#include "statement_astnode.h"
|
||||
#include <stdexcept>
|
||||
#include "astnode_visitor.h"
|
||||
#define SetAutoAccept(name) \
|
||||
void name::accept(class ASTNodeVisitorBase *visitor) { \
|
||||
if (auto v = dynamic_cast<ASTNodeVirturalVisitor *>(visitor)) { \
|
||||
v->ActuralVisit(this); \
|
||||
} else \
|
||||
throw std::runtime_error("Cannot automatically dispatch visitor"); \
|
||||
}
|
||||
|
||||
SetAutoAccept(EmptyStatement_ASTNode);
|
||||
SetAutoAccept(DefinitionStatement_ASTNode);
|
||||
SetAutoAccept(ExprStatement_ASTNode);
|
||||
SetAutoAccept(IfStatement_ASTNode);
|
||||
SetAutoAccept(WhileStatement_ASTNode);
|
||||
SetAutoAccept(ForStatement_ASTNode);
|
||||
SetAutoAccept(JmpStatement_ASTNode);
|
||||
SetAutoAccept(SuiteStatement_ASTNode);
|
||||
|
||||
#undef SetAutoAccept
|
@ -1 +1,16 @@
|
||||
#include "structural_astnode.h"
|
||||
#include "structural_astnode.h"
|
||||
#include <stdexcept>
|
||||
#include "astnode_visitor.h"
|
||||
|
||||
#define SetAutoAccept(name) \
|
||||
void name::accept(class ASTNodeVisitorBase *visitor) { \
|
||||
if (auto v = dynamic_cast<ASTNodeVirturalVisitor *>(visitor)) { \
|
||||
v->ActuralVisit(this); \
|
||||
} else \
|
||||
throw std::runtime_error("Cannot automatically dispatch visitor"); \
|
||||
}
|
||||
|
||||
SetAutoAccept(FuncDef_ASTNode);
|
||||
SetAutoAccept(ClassDef_ASTNode);
|
||||
SetAutoAccept(Program_ASTNode);
|
||||
#undef SetAutoAccpet
|
@ -4,6 +4,7 @@
|
||||
#include "MXParser.h"
|
||||
#include "antlr4-runtime.h"
|
||||
#include "ast/ast.h"
|
||||
#include "ast/astnode_visitor.h"
|
||||
#include "visitor.h"
|
||||
|
||||
class MXErrorListener : public antlr4::BaseErrorListener {
|
||||
@ -33,6 +34,8 @@ std::shared_ptr<Program_ASTNode> CheckAndDecorate(std::shared_ptr<Program_ASTNod
|
||||
throw SemanticError("main() function should be int main()", 1);
|
||||
}
|
||||
}
|
||||
ASTSemanticCheckVisitor visitor;
|
||||
visitor.visit(src.get());
|
||||
return src;
|
||||
}
|
||||
|
||||
|
@ -22,13 +22,16 @@ std::any Visitor::visitMxprog(MXParser::MxprogContext *context) {
|
||||
if (auto classDefContext = dynamic_cast<MXParser::Class_defContext *>(def)) {
|
||||
auto classNode = std::any_cast<std::shared_ptr<ClassDef_ASTNode>>(visit(classDefContext));
|
||||
program->classes.push_back(classNode);
|
||||
program->sorted_children.push_back(classNode);
|
||||
} else if (auto defineStmtContext = dynamic_cast<MXParser::Define_statementContext *>(def)) {
|
||||
auto defineNode = std::dynamic_pointer_cast<DefinitionStatement_ASTNode>(
|
||||
std::any_cast<std::shared_ptr<Statement_ASTNode>>(visit(defineStmtContext)));
|
||||
program->global_variables.push_back(defineNode);
|
||||
program->sorted_children.push_back(defineNode);
|
||||
} else if (auto funcDefContext = dynamic_cast<MXParser::Function_defContext *>(def)) {
|
||||
auto funcNode = std::any_cast<std::shared_ptr<FuncDef_ASTNode>>(visit(funcDefContext));
|
||||
program->functions.push_back(funcNode);
|
||||
program->sorted_children.push_back(funcNode);
|
||||
} else if (auto EOFToken = dynamic_cast<antlr4::tree::TerminalNode *>(def)) {
|
||||
if (EOFToken == context->EOF()) break;
|
||||
throw std::runtime_error("unknown subnode occurred in visitMxprog");
|
||||
@ -160,20 +163,38 @@ std::any Visitor::visitClass_def(MXParser::Class_defContext *context) {
|
||||
class_def->class_name = context->ID()->getText();
|
||||
std::cerr << std::string(nodetype_stk.size() * 2, ' ') << "building a class named " << class_def->class_name
|
||||
<< std::endl;
|
||||
std::vector<MXParser::Class_constructorContext *> constructors = context->class_constructor();
|
||||
if (constructors.size() > 1) throw SemanticError("Multiple constructor found for class " + class_def->class_name, 2);
|
||||
if (constructors.size() > 0)
|
||||
class_def->constructor = std::any_cast<std::shared_ptr<FuncDef_ASTNode>>(visit(constructors[0]));
|
||||
std::vector<MXParser::Function_defContext *> functions = context->function_def();
|
||||
for (auto func : functions) {
|
||||
auto func_node = std::any_cast<std::shared_ptr<FuncDef_ASTNode>>(visit(func));
|
||||
class_def->member_functions.push_back(func_node);
|
||||
}
|
||||
std::vector<MXParser::Class_var_defContext *> vars = context->class_var_def();
|
||||
for (auto var : vars) {
|
||||
auto var_node = std::dynamic_pointer_cast<DefinitionStatement_ASTNode>(
|
||||
std::any_cast<std::shared_ptr<Statement_ASTNode>>(visit(var)));
|
||||
class_def->member_variables.push_back(var_node);
|
||||
// std::vector<MXParser::Class_constructorContext *> constructors = context->class_constructor();
|
||||
if (context->class_constructor().size() > 1)
|
||||
throw SemanticError("Multiple constructor found for class " + class_def->class_name, 2);
|
||||
// if (constructors.size() > 0)
|
||||
// class_def->constructor = std::any_cast<std::shared_ptr<FuncDef_ASTNode>>(visit(constructors[0]));
|
||||
// std::vector<MXParser::Function_defContext *> functions = context->function_def();
|
||||
// for (auto func : functions) {
|
||||
// auto func_node = std::any_cast<std::shared_ptr<FuncDef_ASTNode>>(visit(func));
|
||||
// class_def->member_functions.push_back(func_node);
|
||||
// }
|
||||
// std::vector<MXParser::Class_var_defContext *> vars = context->class_var_def();
|
||||
// for (auto var : vars) {
|
||||
// auto var_node = std::dynamic_pointer_cast<DefinitionStatement_ASTNode>(
|
||||
// std::any_cast<std::shared_ptr<Statement_ASTNode>>(visit(var)));
|
||||
// class_def->member_variables.push_back(var_node);
|
||||
// }
|
||||
for (size_t i = 3; i < context->children.size(); i++) {
|
||||
if (auto var_def_ctx = dynamic_cast<MXParser::Class_var_defContext *>(context->children[i])) {
|
||||
auto var_node = std::dynamic_pointer_cast<DefinitionStatement_ASTNode>(
|
||||
std::any_cast<std::shared_ptr<Statement_ASTNode>>(visit(context->children[i])));
|
||||
class_def->member_variables.push_back(var_node);
|
||||
class_def->sorted_children.push_back(var_node);
|
||||
} else if (auto constructor_ctx = dynamic_cast<MXParser::Class_constructorContext *>(context->children[i])) {
|
||||
auto constructor_node = std::any_cast<std::shared_ptr<FuncDef_ASTNode>>(visit(context->children[i]));
|
||||
class_def->constructor = constructor_node;
|
||||
class_def->sorted_children.push_back(constructor_node);
|
||||
} else if (auto func_def_ctx = dynamic_cast<MXParser::Function_defContext *>(context->children[i])) {
|
||||
auto func_node = std::any_cast<std::shared_ptr<FuncDef_ASTNode>>(visit(context->children[i]));
|
||||
class_def->member_functions.push_back(func_node);
|
||||
class_def->sorted_children.push_back(func_node);
|
||||
} else
|
||||
break;
|
||||
}
|
||||
|
||||
nodetype_stk.pop_back();
|
||||
@ -217,9 +238,9 @@ std::any Visitor::visitClass_var_def(MXParser::Class_var_defContext *context) {
|
||||
member_var_def->vars.push_back(std::make_pair(id->getText(), nullptr));
|
||||
std::cerr << std::string(nodetype_stk.size() * 2, ' ') << "recorded member variable name is " << id->getText()
|
||||
<< std::endl;
|
||||
if (!member_var_def->current_scope->add_variable(id->getText(), member_var_def->var_type)) {
|
||||
throw SemanticError("Variable name " + id->getText() + " is not available", 1);
|
||||
}
|
||||
// if (!member_var_def->current_scope->add_variable(id->getText(), member_var_def->var_type)) {
|
||||
// throw SemanticError("Variable name " + id->getText() + " is not available", 1);
|
||||
// }
|
||||
}
|
||||
|
||||
nodetype_stk.pop_back();
|
||||
@ -494,8 +515,8 @@ std::any Visitor::visitDefine_statement(MXParser::Define_statementContext *conte
|
||||
if (dynamic_cast<antlr4::tree::TerminalNode *>(context->children[i]) != nullptr &&
|
||||
dynamic_cast<antlr4::tree::TerminalNode *>(context->children[i])->getSymbol()->getType() == MXParser::ID) {
|
||||
def_stmt->vars.push_back(std::make_pair(context->children[i]->getText(), nullptr));
|
||||
if (!def_stmt->current_scope->add_variable(context->children[i]->getText(), def_stmt->var_type))
|
||||
throw SemanticError("Variable " + context->children[i]->getText() + " already defined", 1);
|
||||
// if (!def_stmt->current_scope->add_variable(context->children[i]->getText(), def_stmt->var_type))
|
||||
// throw SemanticError("Variable " + context->children[i]->getText() + " already defined", 1);
|
||||
} else
|
||||
throw std::runtime_error("unknown subnode occurred in visitDefine_statement");
|
||||
i++;
|
||||
|
Reference in New Issue
Block a user