ready to restructure for more complecated check:

This commit is contained in:
2024-08-13 10:24:13 +00:00
parent 00501dae02
commit b010326e60
4 changed files with 47 additions and 1 deletions

View File

@ -16,6 +16,7 @@ class ASTNodeVisitorBase {
class ASTNodeBase {
friend Visitor;
friend std::shared_ptr<Program_ASTNode> CheckAndDecorate(std::shared_ptr<Program_ASTNode> src);
protected:
std::shared_ptr<ScopeBase> current_scope;

View File

@ -32,6 +32,13 @@ class LocalScope : public ScopeBase {
if (!VariableNameAvailable(name, 0)) {
return false;
}
if (std::holds_alternative<IdentifierType>(type) && std::get<IdentifierType>(type) == "void") {
throw SemanticError("Variable cannot be void", 1);
}
if (std::holds_alternative<ArrayType>(type) && std::get<ArrayType>(type).has_base_type &&
std::get<ArrayType>(type).basetype == "void") {
throw SemanticError("Variable cannot be void", 1);
}
local_variables[name] = type;
return true;
}
@ -53,6 +60,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;
FunctionSchema schema;
bool add_variable([[maybe_unused]] const std::string &name, [[maybe_unused]] const ExprTypeInfo &type) override {
@ -80,6 +88,13 @@ class ClassDefScope : public ScopeBase {
if (!VariableNameAvailable(name, 0)) {
return false;
}
if (std::holds_alternative<IdentifierType>(type) && std::get<IdentifierType>(type) == "void") {
throw SemanticError("Variable cannot be void", 1);
}
if (std::holds_alternative<ArrayType>(type) && std::get<ArrayType>(type).has_base_type &&
std::get<ArrayType>(type).basetype == "void") {
throw SemanticError("Variable cannot be void", 1);
}
member_variables[name] = type;
return true;
}
@ -111,6 +126,7 @@ class ClassDefScope : public ScopeBase {
};
class GlobalScope : public ScopeBase {
friend class Visitor;
friend std::shared_ptr<class Program_ASTNode> CheckAndDecorate(std::shared_ptr<class Program_ASTNode> src);
std::unordered_map<std::string, ExprTypeInfo> global_variables;
std::unordered_map<std::string, std::shared_ptr<FunctionScope>> global_functions;
std::unordered_map<std::string, std::shared_ptr<ClassDefScope>> classes;
@ -146,6 +162,13 @@ class GlobalScope : public ScopeBase {
if (!VariableNameAvailable(name, 0)) {
return false;
}
if (std::holds_alternative<IdentifierType>(type) && std::get<IdentifierType>(type) == "void") {
throw SemanticError("Variable cannot be void", 1);
}
if (std::holds_alternative<ArrayType>(type) && std::get<ArrayType>(type).has_base_type &&
std::get<ArrayType>(type).basetype == "void") {
throw SemanticError("Variable cannot be void", 1);
}
global_variables[name] = type;
return true;
}