From 6761fdd434a7e54551fe28398361f9eed5268406 Mon Sep 17 00:00:00 2001 From: Cori Barker Date: Sat, 7 Mar 2026 17:46:38 +0000 Subject: code is a complete mess, simplified some classes as they are way too complex will expand in future but too complicated for now since its the first version. --- include/scope.hpp | 20 ++++---------------- include/semantic_analyzer.hpp | 8 ++++++++ include/symbol.hpp | 37 ++++++------------------------------- include/symbol_table.hpp | 21 ++++----------------- 4 files changed, 22 insertions(+), 64 deletions(-) (limited to 'include') diff --git a/include/scope.hpp b/include/scope.hpp index ff20542..7d66f0f 100644 --- a/include/scope.hpp +++ b/include/scope.hpp @@ -1,23 +1,11 @@ #pragma once -#include -#include - class Scope { public: - explicit Scope(std::string name, int level, *Scope parent); - std::string getScopeName(); - int getScopeLevel(); - *Scope getParentScope(); - void define(Symbol symbol); - *Symbol lookup(std::string name); - bool isDeclared(std::string name); - std::unordered_map getAllSymbols(); - std::string toString(); + explicit Scope(int scope_level); private: - std::string scope_name; - int scope_level - *Scope parent_scope; - std::unordered_map symbols; + std::vector symbols; + int scope_level; + }; diff --git a/include/semantic_analyzer.hpp b/include/semantic_analyzer.hpp index 0d56599..630a267 100644 --- a/include/semantic_analyzer.hpp +++ b/include/semantic_analyzer.hpp @@ -1,5 +1,10 @@ #pragma once +#include "ast_node.hpp" + +#include +#include + class SemanticAnalyzer { public: explicit SemanticAnalyzer(); @@ -44,3 +49,6 @@ private: std::string current_function_return_type; bool has_main_function; }; + + +SemanticAnalyzer::SemanticAnalyzer() : symbol_table(), errors(), warnings(), current_function(nullptr), current_function_return_type(), has_main_functions(false) { } diff --git a/include/symbol.hpp b/include/symbol.hpp index 4bee45d..c3c7ef4 100644 --- a/include/symbol.hpp +++ b/include/symbol.hpp @@ -1,38 +1,13 @@ #pragma once -#include -#include - -#include "symbol_type.hpp" - class Symbol { public: - explicit Symbol(std::string name, SymbolType type, std::string data_type, int scope_level); - std::string getName(); - SymbolType getSymbolType(); - std::string getDataType(); - int getScopeLevel(); - bool isInitialized(); - void setInitialized(bool init); - bool isParameter(); - void setParameter(bool is_param); - std::vector getParameterTypes(); - void setParameterTypes(std::vector types); - std::string getReturnType(); - void setReturnType(std::string type); - int getLineDeclared(); - void setLineDeclared(int line); - std::string toString(); + explicit Symbol(); private: - std::string symbol_name; - SymbolType symbol_type; - std::string data_type; - int scope_level; - bool is_initialized; - bool is_parameter; - std::vector parameter_types; - std::string return_type; - int line_declared; - int column_declared; + std::string identifier; + Type type; + std::variant value; }; + +Symbol::Symbol(std::string identifier, Type type, std::variant value) : identifier(identifier), type(type), value(value) { } diff --git a/include/symbol_table.hpp b/include/symbol_table.hpp index b2b8270..55de65a 100644 --- a/include/symbol_table.hpp +++ b/include/symbol_table.hpp @@ -2,24 +2,11 @@ class SymbolTable { public: - explicit SymbolTable(); - ~SymbolTable() = default; - void enterScope(std::string scope_name); - void exitScope(); - Scope* getCurrentScope(); - Scope* getGlobalScope(); - int getScopeLevel(); - void insert(Symbol symbol); - Symbol* lookup(std::string name); - Symbol* lookupCurrentScope(std::string name); - bool isDeclared(std::string name); - bool isDeclaredInCurrentScope(std::string name); - void display(); - std::string toString(); + private: - std::vector> scopes; + std::vector scopes; Scope* current_scope; - int scope_level; - Scope* global_scope; + + }; -- cgit v1.2.3