aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCori Barker <coribarker2@gmail.com>2026-03-10 22:53:50 +0000
committerCori Barker <coribarker2@gmail.com>2026-03-10 22:53:50 +0000
commit32a1609b59fcbbfe24e223c078d51f8bfa08566f (patch)
tree0aafc1e1d19ab0a3e610ee227f92ab6a8a327813
parentc364efa08dff0abef9e043e2e28cf2d8dc6b95b3 (diff)
continued development of the semantic analyzer and symbol table generation
-rw-r--r--include/scope.hpp1
-rw-r--r--include/symbol.hpp26
-rw-r--r--include/type.hpp24
-rw-r--r--src/semantic_analyzer.cpp36
4 files changed, 37 insertions, 50 deletions
diff --git a/include/scope.hpp b/include/scope.hpp
index 69c70f6..4c90813 100644
--- a/include/scope.hpp
+++ b/include/scope.hpp
@@ -4,6 +4,7 @@ class Scope {
public:
explicit Scope(int scope_level);
+ add symbol
private:
std::vector<Symbol> symbols;
int scope_level;
diff --git a/include/symbol.hpp b/include/symbol.hpp
index d7be206..9b48a0a 100644
--- a/include/symbol.hpp
+++ b/include/symbol.hpp
@@ -1,30 +1,28 @@
#pragma once
-#include "symbol_type.hpp"
#include "type.hpp"
#include <variant>
#include <vector>
#include <string>
+enum SymbolType {
+ VARIABLE,
+ FUNCTION,
+};
+
class Symbol {
public:
std::string identifier;
+ SymbolType symbol_type;
+ Type data_type;
+ Type return_type;
+ std::vector<std::string> parameter_identifiers;
+ std::vector<Type> parameter_types;
int line;
int column;
- virtual ~Symbol() = default;
-};
-
-class FunctionDeclarationSymbol : public Symbol {
- Type return_type;
- std::vector<std::string> parameters;
-
- FunctionDeclarationSymbol(std::string identifier, Type return_type, std::vector<std::string> parameters, int line, int column) : identifier(identifier), return_type(return_type), parameters(parameters), line(line), column(column) { }
-};
-
-class VariableDeclarationSymbol : public Symbol {
- Type type;
+ Symbol(std::string identifier, Type data_type, int line, int column) : identifier(identifier), data_type(data_type), line(line), column(column), symbol_type(SymbolType::VARIABLE) { }
- VariableDeclarationSymbol
+ Symbol(std::string identifier, Type return_type, std::vector<std::string> parameter_identifiers, std::vector<Type> parameter_types, int line, int column) : identifier(identifier), data_type(data_type), return_type(return_type), parameter_identifiers(parameter_identifiers), parameter_types(parameter_types), line(line), column(column), symbol_type(SymbolType::FUNCTION) { }
};
diff --git a/include/type.hpp b/include/type.hpp
index 9b25d24..1726764 100644
--- a/include/type.hpp
+++ b/include/type.hpp
@@ -8,28 +8,4 @@ enum struct TypeKind {
FLOAT,
DOUBLE,
VOID,
- FUNCTION,
-};
-
-struct Type {
- TypeKind kind;
-
- Type* return_type = nullptr;
- std::vector<Type*> param_types;
-
- static Type makeInt() { return { TypeKind::INT }; }
- static Type makeChar() { return { TypeKind::CHAR }; }
- static Type makeBool() { return { TypeKind::BOOL }; }
- static Type makeString() { return { TypeKind::STRING }; }
- static Type makeFloat() { return { TypeKind::FLOAT }; }
- static Type makeDouble() { return { TypeKind::DOUBLE }; }
- static Type makeVoid() { return { TypeKind::VOID }; }
-
- static Type makeFunction(Type* return_type, std::vector<Type*> param_types) {
- Type t;
- t.kind = TypeKind::FUNCTION;
- t.return_type = return_type;
- t.param_types = std::move(param_types);
- return t;
- };
};
diff --git a/src/semantic_analyzer.cpp b/src/semantic_analyzer.cpp
index 48a9448..f9d051b 100644
--- a/src/semantic_analyzer.cpp
+++ b/src/semantic_analyzer.cpp
@@ -1,24 +1,36 @@
#include "semantic_analyzer.hpp"
-SemanticAnalyzer::SemanticAnalyzer() : symbol_table(), current_scope(nullptr), current_scope_level(-1) { }
+SemanticAnalyzer::SemanticAnalyzer() : symbol_table(SymbolTable();), current_scope(nullptr), current_scope_level(-1) { }
SymbolTable SemanticAnalyzer::analyze(ASTNode* node) {
- for (auto function : node->function_delcarations) {
- analyzeFunction(function);
- }
-}
+ Scope* global_scope = new Scope();
-ASTNode* SemanticAnalyzer::advance() {
- if (dynamic_cast<ProgramNode*>(node)) {
-
+ for (auto function : node->function_declarations) {
+ global_scope->addSymbol(analyzeFunction(function));
}
}
-ASTNode* SemanticAnalyzer::peek() { }
+Symbol* SemanticAnalyzer::analyzeVariable(ASTNode* node) {
+ std::string identifier = node->identifier;
+ Type data_type = node->type;
+ int line = node->line;
+ int column = node->column;
-Symbol* SemanticAnalyzer::analyzeVariable(ASTNode* node) { }
+ return new Symbol(identifier, data_type, line, column);
+}
-Symbol* SemanticAnalyzer::analyzeFunctionDeclaration(ASTNode* node) {
+Symbol* SemanticAnalyzer::analyzeFunction(ASTNode* node) {
std::string identifier = node->identifier;
- symbol_type = SymbolType::FUNCTION_DECLARATION;
+ Type return_type = node->return_type;
+ std::vector<std::string> parameter_identifiers;
+ std::vector<Type> parameter_types;
+ int line = node->line;
+ int column = node->column;
+
+ for (auto parameter : node->parameters) {
+ parameter_identifiers.push_back(parameter->identifier);
+ parameter_types.push_back(parameter->type);
+ }
+
+ return new Symbol(identifer, return_type, parameter_identifiers, parameter_types, line, column);
}