slightly optimize and refactor
This commit is contained in:
@ -8,11 +8,7 @@
|
||||
#include "tools.h"
|
||||
|
||||
class ASTNodeBase {
|
||||
friend Visitor;
|
||||
friend std::shared_ptr<Program_ASTNode> CheckAndDecorate(std::shared_ptr<Program_ASTNode> src);
|
||||
friend std::shared_ptr<class ModuleItem> BuildIR(std::shared_ptr<Program_ASTNode> src);
|
||||
|
||||
protected:
|
||||
public:
|
||||
std::shared_ptr<ScopeBase> current_scope;
|
||||
ASTNodeType type;
|
||||
// std::vector<std::shared_ptr<ASTNodeBase>> children;
|
||||
|
@ -88,10 +88,7 @@ struct FunctionSchema {
|
||||
std::vector<std::pair<ExprTypeInfo, std::string>> arguments;
|
||||
};
|
||||
class FunctionScope : public ScopeBase {
|
||||
friend std::shared_ptr<class Program_ASTNode> CheckAndDecorate(std::shared_ptr<class Program_ASTNode> src);
|
||||
friend class Visitor;
|
||||
friend class ASTSemanticCheckVisitor;
|
||||
friend class GlobalScope;
|
||||
public:
|
||||
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");
|
||||
@ -132,9 +129,7 @@ class FunctionScope : public ScopeBase {
|
||||
}
|
||||
};
|
||||
class ClassDefScope : public ScopeBase {
|
||||
friend class Visitor;
|
||||
friend class GlobalScope;
|
||||
friend std::shared_ptr<Program_ASTNode> CheckAndDecorate(std::shared_ptr<Program_ASTNode> src);
|
||||
public:
|
||||
std::unordered_map<std::string, ExprTypeInfo> member_variables;
|
||||
std::unordered_map<std::string, std::shared_ptr<FunctionScope>> member_functions;
|
||||
IRClassInfo llvm_class_info;
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include "structural_astnode.h"
|
||||
|
||||
class ASTSemanticCheckVisitor : public ASTNodeVirturalVisitor {
|
||||
public:
|
||||
bool is_in_func_def;
|
||||
bool has_return;
|
||||
FunctionSchema cur_func_schema;
|
||||
@ -13,7 +14,6 @@ class ASTSemanticCheckVisitor : public ASTNodeVirturalVisitor {
|
||||
bool is_in_class_def;
|
||||
size_t loop_level;
|
||||
std::shared_ptr<GlobalScope> global_scope;
|
||||
friend std::shared_ptr<Program_ASTNode> CheckAndDecorate(std::shared_ptr<Program_ASTNode> src);
|
||||
|
||||
bool ClassExists(const std::string &name) {
|
||||
if (name == "int" || name == "bool") return true;
|
||||
|
@ -8,9 +8,7 @@
|
||||
#include "expr_astnode.h"
|
||||
#include "statement_astnode.h"
|
||||
class FuncDef_ASTNode : public ASTNodeBase {
|
||||
friend Visitor;
|
||||
friend class ASTSemanticCheckVisitor;
|
||||
friend class IRBuilder;
|
||||
public:
|
||||
bool is_constructor;
|
||||
IdentifierType func_name;
|
||||
ExprTypeInfo return_type;
|
||||
@ -22,11 +20,7 @@ class FuncDef_ASTNode : public ASTNodeBase {
|
||||
virtual void accept(class ASTNodeVisitorBase *visitor) override;
|
||||
};
|
||||
class ClassDef_ASTNode : public ASTNodeBase {
|
||||
friend Visitor;
|
||||
friend class ASTSemanticCheckVisitor;
|
||||
friend class IRBuilder;
|
||||
|
||||
private:
|
||||
public:
|
||||
std::string class_name;
|
||||
std::vector<std::shared_ptr<DefinitionStatement_ASTNode>> member_variables;
|
||||
std::vector<std::shared_ptr<FuncDef_ASTNode>> member_functions;
|
||||
@ -38,16 +32,12 @@ class ClassDef_ASTNode : public ASTNodeBase {
|
||||
virtual void accept(class ASTNodeVisitorBase *visitor) override;
|
||||
};
|
||||
class Program_ASTNode : public ASTNodeBase {
|
||||
friend std::shared_ptr<class ModuleItem> BuildIR(std::shared_ptr<Program_ASTNode> src);
|
||||
friend Visitor;
|
||||
friend class ASTSemanticCheckVisitor;
|
||||
friend class IRBuilder;
|
||||
public:
|
||||
std::vector<std::shared_ptr<DefinitionStatement_ASTNode>> global_variables;
|
||||
std::vector<std::shared_ptr<ClassDef_ASTNode>> classes;
|
||||
std::vector<std::shared_ptr<FuncDef_ASTNode>> functions;
|
||||
std::vector<std::shared_ptr<ASTNodeBase>> sorted_children;
|
||||
|
||||
public:
|
||||
Program_ASTNode() = default;
|
||||
virtual void accept(class ASTNodeVisitorBase *visitor) override;
|
||||
};
|
||||
|
Reference in New Issue
Block a user