【2025-11-16】现有代码上传
This commit is contained in:
38
semantic.h
Normal file
38
semantic.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef MINI_C_SEMANTIC_H
|
||||
#define MINI_C_SEMANTIC_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include "ast.h"
|
||||
#include "symbol.h"
|
||||
|
||||
struct Diagnostic {
|
||||
SourceLoc loc;
|
||||
std::string message;
|
||||
};
|
||||
|
||||
class SemanticAnalyzer {
|
||||
public:
|
||||
SemanticAnalyzer();
|
||||
void analyze(ASTNode* root);
|
||||
void visitForDeclStmt(ASTNode* n);
|
||||
const std::vector<Diagnostic>& diagnostics() const { return diags_; }
|
||||
std::shared_ptr<Scope> getGlobalScope() const { return rootScope_; }
|
||||
private:
|
||||
std::shared_ptr<Scope> rootScope_;
|
||||
std::shared_ptr<Scope> currentScope_;
|
||||
std::vector<Diagnostic> diags_;
|
||||
void visit(ASTNode* n);
|
||||
void visitFunctionDef(ASTNode* n);
|
||||
void visitCompoundStmt(ASTNode* n);
|
||||
void visitDeclaration(ASTNode* n);
|
||||
void visitInitDecl(ASTNode* n);
|
||||
void visitIdentifier(ASTNode* n);
|
||||
ASTNode* lastSpecList_{nullptr};
|
||||
void error(SourceLoc loc, const std::string& msg) {
|
||||
diags_.push_back({loc, msg});
|
||||
}
|
||||
};
|
||||
|
||||
#endif // MINI_C_SEMANTIC_H
|
||||
Reference in New Issue
Block a user