aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCori Barker <coribarker2@gmail.com>2026-02-09 11:05:11 +0000
committerCori Barker <coribarker2@gmail.com>2026-02-09 11:05:11 +0000
commita7b36237d7c703c96b6a22c11cf37045d999fcc6 (patch)
tree2f5fec8d71345e39285364e2695717cea4023b12 /src
parentddedc416185954051ff2a826c24fc1cc608204f1 (diff)
Written some class declarations
Diffstat (limited to 'src')
-rw-r--r--src/semantic/scope.cpp52
-rw-r--r--src/semantic/semantic_analyzer.cpp0
-rw-r--r--src/semantic/symbol.cpp126
-rw-r--r--src/semantic/symbol_table.cpp0
4 files changed, 178 insertions, 0 deletions
diff --git a/src/semantic/scope.cpp b/src/semantic/scope.cpp
new file mode 100644
index 0000000..75c4e61
--- /dev/null
+++ 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
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/semantic/semantic_analyzer.cpp
diff --git a/src/semantic/symbol.cpp b/src/semantic/symbol.cpp
new file mode 100644
index 0000000..26913f1
--- /dev/null
+++ 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
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/semantic/symbol_table.cpp