set structure for IR

This commit is contained in:
2024-08-21 04:10:36 +00:00
parent 03e33d0a0f
commit ed1ba4b59a
17 changed files with 1423 additions and 66 deletions

View File

@ -17,6 +17,7 @@ class ScopeBase {
protected:
ScopeBase *parent; // cannot use std::shared_ptr<ScopeBase> because of circular dependency
size_t scope_id;
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;
@ -26,6 +27,11 @@ class ScopeBase {
"for", "while", "break", "continue", "return"};
return keywords.find(name) != keywords.end();
}
public:
ScopeBase() {
static size_t scope_counter=0;
scope_id = scope_counter++;
}
};
class LocalScope : public ScopeBase {
friend class Visitor;
@ -104,6 +110,7 @@ class ClassDefScope : public ScopeBase {
friend std::shared_ptr<Program_ASTNode> CheckAndDecorate(std::shared_ptr<Program_ASTNode> src);
std::unordered_map<std::string, ExprTypeInfo> member_variables;
std::unordered_map<std::string, std::shared_ptr<FunctionScope>> member_functions;
IRClassInfo llvm_class_info;
bool add_variable(const std::string &name, const ExprTypeInfo &type) override {
if (!VariableNameAvailable(name, 0)) {
return false;