ready to build AST

This commit is contained in:
2024-08-08 13:05:56 +00:00
parent f1eb6abcc1
commit 9900d5aaf3
23 changed files with 683 additions and 110 deletions

View File

@ -3,15 +3,37 @@
#include "MXLexer.h"
#include "MXParser.h"
#include "antlr4-runtime.h"
#include "ast/ast.h"
#include "visitor.h"
int SemanticCheck(std::istream &fin) {
class MXErrorListener : public antlr4::BaseErrorListener {
bool no_problem;
public:
MXErrorListener() : no_problem(true) {}
void syntaxError(antlr4::Recognizer *recognizer, antlr4::Token *offendingSymbol, size_t line,
size_t charPositionInLine, const std::string &msg, std::exception_ptr e) override {
std::cout << "line " << line << ":" << charPositionInLine << " AT "
<< offendingSymbol->getText() << ": " << msg << std::endl;
no_problem = false;
}
bool IsOk() { return no_problem; }
};
std::shared_ptr<ASTNodeBase> BuildAST(Visitor *visitor, antlr4::tree::ParseTree *tree) { ; }
std::shared_ptr<ASTNodeBase> CheckAndDecorate(std::shared_ptr<ASTNodeBase> src) { ; }
void SemanticCheck(std::istream &fin, std::shared_ptr<ASTNodeBase> &ast_out) {
antlr4::ANTLRInputStream input(fin);
MXLexer lexer(&input);
antlr4::CommonTokenStream tokens(&lexer);
tokens.fill();
MXParser parser(&tokens);
parser.removeErrorListeners();
MXErrorListener error_listener;
parser.addErrorListener(&error_listener);
antlr4::tree::ParseTree *tree = parser.mxprog();
if (!error_listener.IsOk()) throw SemanticError("Fatal error: syntax error", 1);
Visitor visitor;
visitor.visit(tree);
return 0;
std::shared_ptr<ASTNodeBase> ast = BuildAST(&visitor, tree);
ast_out = CheckAndDecorate(ast);
}