diff options
| author | Cori Barker <coribarker2@gmail.com> | 2026-03-07 17:46:38 +0000 |
|---|---|---|
| committer | Cori Barker <coribarker2@gmail.com> | 2026-03-07 17:46:38 +0000 |
| commit | 6761fdd434a7e54551fe28398361f9eed5268406 (patch) | |
| tree | 43f752cc165f6c13b1204dd0e82a219c0d2df4c3 | |
| parent | 32ee1c8be581e1967950e125551d672ff65cb04b (diff) | |
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.
| -rw-r--r-- | include/scope.hpp | 20 | ||||
| -rw-r--r-- | include/semantic_analyzer.hpp | 8 | ||||
| -rw-r--r-- | include/symbol.hpp | 37 | ||||
| -rw-r--r-- | include/symbol_table.hpp | 21 | ||||
| -rw-r--r-- | src/semantic_analyzer.cpp | 5 |
5 files changed, 25 insertions, 66 deletions
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 <string> -#include <memory> - 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<std::string, Symbol> getAllSymbols(); - std::string toString(); + explicit Scope(int scope_level); private: - std::string scope_name; - int scope_level - *Scope parent_scope; - std::unordered_map<std::string, Symbol> symbols; + std::vector<Symbol> 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 <string> +#include <vector> + 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 <string> -#include <vector> - -#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<std::string> getParameterTypes(); - void setParameterTypes(std::vector<std::string> 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<std::string> parameter_types; - std::string return_type; - int line_declared; - int column_declared; + std::string identifier; + Type type; + std::variant<int, bool, std::string> value; }; + +Symbol::Symbol(std::string identifier, Type type, std::variant<int, bool, std::string> 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<std::unique_ptr<Scope>> scopes; + std::vector<Scope> scopes; Scope* current_scope; - int scope_level; - Scope* global_scope; + + }; diff --git a/src/semantic_analyzer.cpp b/src/semantic_analyzer.cpp index f6d3d2a..6df7011 100644 --- a/src/semantic_analyzer.cpp +++ b/src/semantic_analyzer.cpp @@ -1,7 +1,5 @@ #include "semantic_analyzer.hpp" -SemanticAnalyzer::SemanticAnalyzer() : symobl_table(new SemanticTable), errors(std::vector<Error>), warnings(std::vector<Error>), current_function(nullptr), current_function_return_type(""), has_main_function(false) {} - bool SemanticAnalyzer::analyze(ASTNode* ast) { symbol_table.enterScope("global") visit(ast); @@ -99,6 +97,8 @@ std::string visitProgram(ProgramNode* node){ for (auto element : functions) { visitFunctionDeclaration(element); }; + + return; } std::string visitFunctionDeclaration(FunctionDeclarationNode* node){ @@ -112,6 +112,7 @@ std::string visitFunctionDeclaration(FunctionDeclarationNode* node){ // exit function scope and clear context // return + current_function = node; } |
