commit a7b36237d7c703c96b6a22c11cf37045d999fcc6
parent ddedc416185954051ff2a826c24fc1cc608204f1
Author: Cori Barker <coribarker2@gmail.com>
Date: Mon, 9 Feb 2026 11:05:11 +0000
Written some class declarations
Diffstat:
6 files changed, 195 insertions(+), 17 deletions(-)
diff --git a/include/semantic/scope.h b/include/semantic/scope.h
@@ -5,12 +5,12 @@
class Scope {
public:
- explicit Scope(std::string name, int level, std::unique_ptr<Scope> parent);
+ explicit Scope(std::string name, int level, *Scope parent);
std::string getScopeName();
int getScopeLevel();
- std::unique_ptr<Scope> getParentScope();
+ *Scope getParentScope();
void define(Symbol symbol);
- std::unique_ptr<Symbol> lookup(std::string name);
+ *Symbol lookup(std::string name);
bool isDeclared(std::string name);
std::unordered_map<std::string, Symbol> getAllSymbols();
std::string toString();
@@ -18,6 +18,6 @@ public:
private:
std::string scope_name;
int scope_level
- std::unique_ptr<Scope> parent_scope;
- std::unordered_map<std::string, Symbol>;
+ *Scope parent_scope;
+ std::unordered_map<std::string, Symbol> symbols;
};
diff --git a/include/semantic/symbol.h b/include/semantic/symbol.h
@@ -3,11 +3,11 @@
#include <string>
#include <vector>
-#include "types.h"
+#include "symbol_type.h"
class Symbol {
public:
- explicit Symbol(std::string name, SymbolType type, std::string data_type, int scope);
+ explicit Symbol(std::string name, SymbolType type, std::string data_type, int scope_level);
std::string getName();
SymbolType getSymbolType();
std::string getDataType();
@@ -25,14 +25,14 @@ public:
std::string toString();
private:
- std::string 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 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;
};
diff --git a/src/semantic/scope.cpp b/src/semantic/scope.cpp
@@ -0,0 +1,52 @@
+#include "semantic/scope.h"
+
+Scope::Scope(std::string name, int level, std::unique_ptr<Scope> 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> Scope::getParentScope() {
+ return parent_scope;
+}
+
+void Scope::define(Symbol symbol) {
+ if (isDeclared(symbol->getName())) {
+ // error
+ }
+
+ else {
+ symobls.add(symbol->getName(), symbol);
+ }
+}
+
+std::unique_ptr<Symbol> 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<std::string, Symbol> 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
diff --git a/src/semantic/symbol.cpp b/src/semantic/symbol.cpp
@@ -0,0 +1,126 @@
+#include "semantic/symbol.h"
+
+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<std::string> Symbol::getParameterTypes() {
+ return paremeter_types;
+}
+
+void Symbol::setParameterTypes(std::vector<std::string> 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