From 5c1f937a7eb7a9cc9cd86cb69b3263f41f24408f Mon Sep 17 00:00:00 2001 From: Cori Barker Date: Sun, 22 Feb 2026 22:24:27 +0000 Subject: Partially completed lots of changes, refactoring most of the files --- src/semantic/scope.cpp | 52 --------------- src/semantic/semantic_analyzer.cpp | 33 ---------- src/semantic/symbol.cpp | 126 ------------------------------------- src/semantic/symbol_table.cpp | 89 -------------------------- 4 files changed, 300 deletions(-) delete mode 100644 src/semantic/scope.cpp delete mode 100644 src/semantic/semantic_analyzer.cpp delete mode 100644 src/semantic/symbol.cpp delete mode 100644 src/semantic/symbol_table.cpp (limited to 'src/semantic') diff --git a/src/semantic/scope.cpp b/src/semantic/scope.cpp deleted file mode 100644 index ba5aa41..0000000 --- a/src/semantic/scope.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "semantic/scope.hpp" - -Scope::Scope(std::string name, int level, std::unique_ptr parent) : scope_name(name), scope_level(level), parent_scope(parent) {} - -std::string Scope::getScopeName() { - return scope_name; -} - -int Scope::getScopeLevel() { - return scope_level; -} - -std::unique_ptr Scope::getParentScope() { - return parent_scope; -} - -void Scope::define(Symbol symbol) { - if (isDeclared(symbol->getName())) { - // error - } - - else { - symobls.add(symbol->getName(), symbol); - } -} - -std::unique_ptr Scope::lookup(std::string name) { - auto it = symbols.find(name); - if (it != symbols.end()) { - return &(it->second); - } - - return nullptr; -} - -bool Scope::isDeclared(std::string name) { - return lookup(name) != nullptr; -} - -std::unordered_map Scope::getAllSymbols() { - return symbols; -} - -std::string Scope::toString() { - std::string result = "Scope: " + scope_name + "(level " + std::to_string(scope_level) + ") \n"; - - for (auto& pair : symbols) { - result += " " + pair.first + " : " + pair.second.getDataType() + "\n"; - } - - return result; -} diff --git a/src/semantic/semantic_analyzer.cpp b/src/semantic/semantic_analyzer.cpp deleted file mode 100644 index 57f217e..0000000 --- a/src/semantic/semantic_analyzer.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "semantic/semantic_analyzer.hpp" - -SemanticAnalyzer::SemanticAnalyzer() : symobl_table(new SemanticTable), errors(std::vector), warnings(std::vector), current_function(nullptr), current_function_return_type(""), has_main_function(false) {} - -bool SemanticAnalyzer::analyze(ASTNode* ast) { - this->symbol_table.enterScope("global") - this->visit(ast); - this->validateMainFunction(); - - if (has_main_function == false) { - //error - return false; - } - - this->symbol_table.exitScope(); - return true; -} - -std::vector SemanticAnalyzer::getErrors() { - return this->errors; -} - -std::vector SemanticAnalyzer::getWarnings() { - return this->warnings; -} - -bool SemanticAnalyzer::hasErrors() { - return !this->errors.empty(); -} - -std::string SemanticAnalyzer::visit(ASTNode* ast) { - -} diff --git a/src/semantic/symbol.cpp b/src/semantic/symbol.cpp deleted file mode 100644 index 5142ea8..0000000 --- a/src/semantic/symbol.cpp +++ /dev/null @@ -1,126 +0,0 @@ -#include "semantic/symbol.hpp" - -Symbol::Symbol(std::string name, SymbolType type, std::string data_type, int scope) : symbol_name(name), symbol_type(type), data_type(data_type), scope_level(scope_level) {} - -std::string Symbool::getName() { - return name; -} - -SymbolType Symbol::getSymbolType() { - return symbol_type; -} - -std::string Symbol::getDataType() { - return symbol_data_type; -} - -int Symbol::getSymbolLevel() { - return scope_level; -} - -bool Symbol::isInitialized() { - return is_initialized; -} - -void Symbol::setInitialized(bool init) { - is_initialized = init; -} - -bool Symbol::isParameter() { - return is_parameter(); -} - -void Symbol::setParameter(bool is_param) { - is_parameter = is_param; -} - -std::vector Symbol::getParameterTypes() { - return paremeter_types; -} - -void Symbol::setParameterTypes(std::vector types) { - parameter_types = types; -} - -std::string Symbol::getReturnType() { - return return_type; -} - -void Symbol::setReturnType(std::string type) { - return_type = type; -} - -int Symbol::getLineDeclared() { - return line_declared; -} - -void Symbol::setLineDeclared(int line) { - line_declared = line; -} - -std::string Symbol::toString() const { - std::string result = ""; - - // Symbol name and basic info - result += "Symbol: " + name + "\n"; - - // Symbol type (VARIABLE, FUNCTION, PARAMETER) - result += " Type: "; - switch (symbol_type) { - case SymbolType::VARIABLE: - result += "VARIABLE\n"; - break; - case SymbolType::FUNCTION: - result += "FUNCTION\n"; - break; - case SymbolType::PARAMETER: - result += "PARAMETER\n"; - break; - default: - result += "UNKNOWN\n"; - break; - } - - // Data type - result += " Data Type: " + data_type + "\n"; - - // Scope level - result += " Scope Level: " + std::to_string(scope_level) + "\n"; - - // Initialization status (only relevant for variables) - if (symbol_type == SymbolType::VARIABLE || symbol_type == SymbolType::PARAMETER) { - result += " Initialized: " + std::string(is_initialized ? "true" : "false") + "\n"; - } - - // Parameter flag - if (is_parameter) { - result += " Is Parameter: true\n"; - } - - // Function-specific information - if (symbol_type == SymbolType::FUNCTION) { - result += " Return Type: " + return_type + "\n"; - - result += " Parameters: ["; - for (size_t i = 0; i < parameter_types.size(); i++) { - result += parameter_types[i]; - if (i < parameter_types.size() - 1) { - result += ", "; - } - } - result += "]\n"; - - result += " Parameter Count: " + std::to_string(parameter_types.size()) + "\n"; - } - - // Declaration location - if (line_declared > 0) { - result += " Declared at: line " + std::to_string(line_declared); - if (column_declared > 0) { - result += ", column " + std::to_string(column_declared); - } - result += "\n"; - } - - return result; -} diff --git a/src/semantic/symbol_table.cpp b/src/semantic/symbol_table.cpp deleted file mode 100644 index 548d6ee..0000000 --- a/src/semantic/symbol_table.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include "semantic/symbol_table.hpp" - -#include - -SymbolTable::SymbolTable() : scopes(std::vector), current_scope(nullptr), scope_level(0), global_scope(nullptr) {} - -SymbolTable::~SymbolTable() {} - -void SymbolTable::enterScope(std::string scope_name) { - Scope scope = Scope(scope_name, scope_level, current_scope); - scopes.push_back(scope*); - - this->current_scope = scope*; - this->scope_level ++; -} - -void SymbolTable::exitScope() { - parent_scope = getParentScope(); - this->current_scope = parent_scope - - this->scope_level --; -} - -Scope* SymbolTable::getCurrentScope() { - return this->current_scope; -} - -Scope* SymbolTable::getGlobalScope() { - return this->global_scope; -} - -int SymbolTable::getScopeLevel() { - return this->scope_level; -} - -void SymbolTable::insert(Symbol symbol) { - if (this->current_scope == nullptr) { - // error - } - - this->current_scope->define(symbol); -} - -Symbol* SymbolTable::lookup(std::string name) { - current_scope = getCurrentScope(); - symbol = current_scope->lookup(name); - - while (symbol == nullptr) { - current_scope = current_scope->getParentScope(); - if (current_scope == nullptr) { - return nullptr; - } - - symbol = current_scope->lookup(name); - } - - return symbol; -} - -Symbol* SymbolTable::lookupCurrentScope(std::string name) { - current_scope = getCurrentScope(); - return current_scope->lookup(name); -} - -bool SymbolTable::isDeclared(std::string name) { - symbol == this->lookup(name); - return (symbol != nullptr); -} - -bool SymbolTable::isDeclaredInCurrentScope(std::string name) { - symbol == this->lookupCurrentSceop(name); - return (symbol != nullptr); -} - -void display() { - std::cout << this->toString(); -} - -std::string toString() { - std::string indent; - std::string result; - - for (const Scope& i : this->scopes) { - indent += (" " * i->getScopeLevel()); - result += (indent + '\n' + i->toString()); - } - - return result; -} -- cgit v1.2.3