aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCori Barker <coribarker2@gmail.com>2026-03-07 18:33:21 +0000
committerCori Barker <coribarker2@gmail.com>2026-03-07 18:33:21 +0000
commit50a51df0404ee4f057fbc19657672d1c7d3eef68 (patch)
tree9cb9ed4572a357448f2b78c4ac0add7eaf6a1e7e
parent6761fdd434a7e54551fe28398361f9eed5268406 (diff)
removed implementations of symbol table related classes, need to fix the parser to use the new AST node classes then i can write the symbol table classes and refactor the lexer and parser to use the symbol table
-rw-r--r--include/ast_node.hpp1
-rw-r--r--include/parser.hpp4
-rw-r--r--include/semantic_analyzer.hpp47
-rw-r--r--include/symbol.hpp11
-rw-r--r--include/symbol_table.hpp12
-rw-r--r--src/Makefile.am4
-rw-r--r--src/Makefile.in50
-rw-r--r--src/parser.cpp4
-rw-r--r--src/scope.cpp52
-rw-r--r--src/semantic_analyzer.cpp241
-rw-r--r--src/symbol.cpp126
-rw-r--r--src/symbol_table.cpp89
12 files changed, 37 insertions, 604 deletions
diff --git a/include/ast_node.hpp b/include/ast_node.hpp
index 96300c2..4484959 100644
--- a/include/ast_node.hpp
+++ b/include/ast_node.hpp
@@ -1,6 +1,5 @@
#pragma once
-#include "node_type.hpp"
#include "type.hpp"
#include <string>
diff --git a/include/parser.hpp b/include/parser.hpp
index f986ce2..6ec505e 100644
--- a/include/parser.hpp
+++ b/include/parser.hpp
@@ -1,7 +1,7 @@
#pragma once
-#include "lexer/token.hpp"
-#include "parser/ast_node.hpp"
+#include "token.hpp"
+#include "ast_node.hpp"
#include <vector>
diff --git a/include/semantic_analyzer.hpp b/include/semantic_analyzer.hpp
index 630a267..ec1e4a1 100644
--- a/include/semantic_analyzer.hpp
+++ b/include/semantic_analyzer.hpp
@@ -1,54 +1,13 @@
#pragma once
-#include "ast_node.hpp"
-
-#include <string>
-#include <vector>
+#include "symbol_table.hpp"
class SemanticAnalyzer {
public:
explicit SemanticAnalyzer();
- bool analyze(ASTNode* ast);
- std::vector<Error> getErrors();
- std::vector<Error> getWarnings();
- bool hasErrors();
-
- // Main visitor method
- std::string visit(ASTNode* ast);
-
- // Visitor methods for program structure
- std::string visitProgram(ProgramNode* node);
- std::string visitFunctionDecl(FunctionDeclNode* node);
- std::string visitParameter(ParameterNode*);
-
- // Visitor methods for statements
- std::string visitVarDeclaration(VarDeclNode* node);
- std::string visitAssignment(AssignmentNode* node);
- std::string visitIfStatement(IfStatementNode* node);
- std::string visitWhileStatement(WhileStatementNode* node);
- std::string visitForStatement(ForStatementNode* node);
- std::string visitReturnStatement(ReturnStatementNode* node);
- std::String visitExpressionStatement(ExpressionStatementNode* node);
-
- // Visitor methods for expressions
- std::string visitBinaryExpression(BinaryExprNode* node);
- std::string visitUnaryExpression(UnaryExprNode* node);
- std::string visitFunctionCall(FunctionCallNode* node);
- std::string visitIdentifier(IdentifierNode* node);
- std::string visitLiteral(LiteralNode* node);
-
- // Type checking helper methods
- bool checkTypeCompatibility(std::string target, std::string source);
- std::string inferBinaryOpType(std::string op, std::string left, std::string right);
private:
SymbolTable symbol_table;
- std::vector<Error> errors;
- std::vector<Error> warnings;
- FunctionDeclNode* current_function;
- std::string current_function_return_type;
- bool has_main_function;
+ Scope* current_scope;
+ int current_scope_level;
};
-
-
-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 c3c7ef4..a388f04 100644
--- a/include/symbol.hpp
+++ b/include/symbol.hpp
@@ -1,13 +1,18 @@
#pragma once
+#include "symbol_type.hpp"
+#include "type.hpp"
+
+#include <variant>
+#include <string>
+
class Symbol {
public:
explicit Symbol();
private:
std::string identifier;
- Type type;
+
+ SymbolType symbol_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 55de65a..42c6cd7 100644
--- a/include/symbol_table.hpp
+++ b/include/symbol_table.hpp
@@ -1,12 +1,14 @@
#pragma once
+#include "scope.hpp"
+
+#include <vector>
+
class SymbolTable {
public:
-
+ explicit SymbolTable();
+ void addScope(Scope* scope);
private:
- std::vector<Scope> scopes;
- Scope* current_scope;
-
-
+ std::vector<Scope*> scopes;
};
diff --git a/src/Makefile.am b/src/Makefile.am
index 40b1655..b78467c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2,8 +2,8 @@ bin_PROGRAMS = blc
blc_SOURCES = \
main.cpp \
- lexer/lexer.cpp \
- parser/parser.cpp
+ lexer.cpp \
+ parser.cpp
AM_CXXFLAGS = \
-std=c++17 \
diff --git a/src/Makefile.in b/src/Makefile.in
index ad84e73..06881b3 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -99,9 +99,7 @@ CONFIG_CLEAN_FILES =
CONFIG_CLEAN_VPATH_FILES =
am__installdirs = "$(DESTDIR)$(bindir)"
PROGRAMS = $(bin_PROGRAMS)
-am__dirstamp = $(am__leading_dot)dirstamp
-am_blc_OBJECTS = main.$(OBJEXT) lexer/lexer.$(OBJEXT) \
- parser/parser.$(OBJEXT)
+am_blc_OBJECTS = main.$(OBJEXT) lexer.$(OBJEXT) parser.$(OBJEXT)
blc_OBJECTS = $(am_blc_OBJECTS)
blc_LDADD = $(LDADD)
AM_V_P = $(am__v_P_@AM_V@)
@@ -119,8 +117,8 @@ am__v_at_1 =
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
depcomp = $(SHELL) $(top_srcdir)/depcomp
am__maybe_remake_depfiles = depfiles
-am__depfiles_remade = ./$(DEPDIR)/main.Po lexer/$(DEPDIR)/lexer.Po \
- parser/$(DEPDIR)/parser.Po
+am__depfiles_remade = ./$(DEPDIR)/lexer.Po ./$(DEPDIR)/main.Po \
+ ./$(DEPDIR)/parser.Po
am__mv = mv -f
CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
@@ -251,8 +249,8 @@ top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
blc_SOURCES = \
main.cpp \
- lexer/lexer.cpp \
- parser/parser.cpp
+ lexer.cpp \
+ parser.cpp
AM_CXXFLAGS = \
-std=c++17 \
@@ -333,22 +331,6 @@ uninstall-binPROGRAMS:
clean-binPROGRAMS:
-test -z "$(bin_PROGRAMS)" || rm -f $(bin_PROGRAMS)
-lexer/$(am__dirstamp):
- @$(MKDIR_P) lexer
- @: > lexer/$(am__dirstamp)
-lexer/$(DEPDIR)/$(am__dirstamp):
- @$(MKDIR_P) lexer/$(DEPDIR)
- @: > lexer/$(DEPDIR)/$(am__dirstamp)
-lexer/lexer.$(OBJEXT): lexer/$(am__dirstamp) \
- lexer/$(DEPDIR)/$(am__dirstamp)
-parser/$(am__dirstamp):
- @$(MKDIR_P) parser
- @: > parser/$(am__dirstamp)
-parser/$(DEPDIR)/$(am__dirstamp):
- @$(MKDIR_P) parser/$(DEPDIR)
- @: > parser/$(DEPDIR)/$(am__dirstamp)
-parser/parser.$(OBJEXT): parser/$(am__dirstamp) \
- parser/$(DEPDIR)/$(am__dirstamp)
blc$(EXEEXT): $(blc_OBJECTS) $(blc_DEPENDENCIES) $(EXTRA_blc_DEPENDENCIES)
@rm -f blc$(EXEEXT)
@@ -356,15 +338,13 @@ blc$(EXEEXT): $(blc_OBJECTS) $(blc_DEPENDENCIES) $(EXTRA_blc_DEPENDENCIES)
mostlyclean-compile:
-rm -f *.$(OBJEXT)
- -rm -f lexer/*.$(OBJEXT)
- -rm -f parser/*.$(OBJEXT)
distclean-compile:
-rm -f *.tab.c
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lexer.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ # am--include-marker
-@AMDEP_TRUE@@am__include@ @am__quote@lexer/$(DEPDIR)/lexer.Po@am__quote@ # am--include-marker
-@AMDEP_TRUE@@am__include@ @am__quote@parser/$(DEPDIR)/parser.Po@am__quote@ # am--include-marker
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/parser.Po@am__quote@ # am--include-marker
$(am__depfiles_remade):
@$(MKDIR_P) $(@D)
@@ -505,10 +485,6 @@ clean-generic:
distclean-generic:
-test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES)
-test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES)
- -rm -f lexer/$(DEPDIR)/$(am__dirstamp)
- -rm -f lexer/$(am__dirstamp)
- -rm -f parser/$(DEPDIR)/$(am__dirstamp)
- -rm -f parser/$(am__dirstamp)
maintainer-clean-generic:
@echo "This command is intended for maintainers to use"
@@ -518,9 +494,9 @@ clean: clean-am
clean-am: clean-binPROGRAMS clean-generic mostlyclean-am
distclean: distclean-am
- -rm -f ./$(DEPDIR)/main.Po
- -rm -f lexer/$(DEPDIR)/lexer.Po
- -rm -f parser/$(DEPDIR)/parser.Po
+ -rm -f ./$(DEPDIR)/lexer.Po
+ -rm -f ./$(DEPDIR)/main.Po
+ -rm -f ./$(DEPDIR)/parser.Po
-rm -f Makefile
distclean-am: clean-am distclean-compile distclean-generic \
distclean-tags
@@ -566,9 +542,9 @@ install-ps-am:
installcheck-am:
maintainer-clean: maintainer-clean-am
- -rm -f ./$(DEPDIR)/main.Po
- -rm -f lexer/$(DEPDIR)/lexer.Po
- -rm -f parser/$(DEPDIR)/parser.Po
+ -rm -f ./$(DEPDIR)/lexer.Po
+ -rm -f ./$(DEPDIR)/main.Po
+ -rm -f ./$(DEPDIR)/parser.Po
-rm -f Makefile
maintainer-clean-am: distclean-am maintainer-clean-generic
diff --git a/src/parser.cpp b/src/parser.cpp
index b697254..fe86397 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -4,8 +4,8 @@
Parser::Parser(const std::vector<Token>& tokens) : tokens_(tokens), position_(0) {}
-std::unique_ptr<Program> Parser::parse() {
- auto program = std::make_unique<Program>();
+std::unique_ptr<ProgramNode> Parser::parse() {
+ auto program = std::make_unique<ProgramNode>();
while (peek().type != TokenType::END_OF_FILE) {
try {
diff --git a/src/scope.cpp b/src/scope.cpp
deleted file mode 100644
index 86e2668..0000000
--- a/src/scope.cpp
+++ /dev/null
@@ -1,52 +0,0 @@
-#include "scope.hpp"
-
-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_analyzer.cpp b/src/semantic_analyzer.cpp
deleted file mode 100644
index 6df7011..0000000
--- a/src/semantic_analyzer.cpp
+++ /dev/null
@@ -1,241 +0,0 @@
-#include "semantic_analyzer.hpp"
-
-bool SemanticAnalyzer::analyze(ASTNode* ast) {
- symbol_table.enterScope("global")
- visit(ast);
- validateMainFunction();
-
- if (has_main_function == false) {
- //error
- return false;
- }
-
- symbol_table.exitScope();
- return true;
-}
-
-std::vector<Error> SemanticAnalyzer::getErrors() {
- return errors;
-}
-
-std::vector<Error> SemanticAnalyzer::getWarnings() {
- return warnings;
-}
-
-bool SemanticAnalyzer::hasErrors() {
- return !errors.empty();
-}
-
-std::string SemanticAnalyzer::visit(ASTNode* node) {
- if (ProgramNode* prog = dynamic_cast<ProgramNode*>(node)) {
- return visitProgram(prog);
- }
-
- else if (FunctionDeclNode* func = dynamic_cast<FunctionDeclNode*>(node)) {
- return visitFunctionDecl(func);
- }
-
- else if (ParameterNode* param = dynamic_cast<ParameterNode*>(node)) {
- return visitParameter(param);
- }
-
- else if (VarDeclNode* var = dynamic_cast<VarDeclNode*>(node)) {
- return visitVarDeclaration(var);
- }
-
- else if (AssignmentNode as = dynamic_cast<AssignmentNode>(node)) {
- return visitAssignment(as);
- }
-
- else if (IfStatementNode is = dynamic_cast<IfStatementNode>(node)) {
- return visitIfStatement(is);
- }
-
- else if (WhileStatementNode ws = dynamic_cast<WhileStatementNode>(node)) {
- return visitWhileStatement(ws);
- }
-
- else if (ForStatementNode fs = dynamic_cast<ForStatementNode>(node)) {
- return visitForStatement(fs);
- }
-
- else if (ReturnStatementNode rs = dynamic_cast<ReturnStatementNode>(node)) {
- return visitReturnStatement(rs);
- }
-
- else if (ExpressionStatementNode es = dynamic_cast<ExpressionStatementNode>(node)) {
- return visitExpressionStatement(es);
- }
-
- else if (BinaryExprNode bin = dynamic_cast<BinaryExprNode>(node)) {
- return visitBinaryExpression(bin);
- }
-
- else if (UnaryExprNode un = dynamic_cast<UnaryExprNode>(node)) {
- return visitUnaryExpression(un);
- }
-
- else if (FunctionCallNode func_call = dynamic_cast<FunctionCallNode>(node)) {
- return visitFunctionCall(func_call);
- }
-
- else if (IdentifierNode ident = dynamic_cast<IdentifierNode>(node)) {
- return visitIdentifier(ident);
- }
-
- else if (LiteralNode lit = dynamic_cast<LiteralNode>(node)) {
- return visitLiteral(lit);
- }
-}
-
-std::string visitProgram(ProgramNode* node){
- // get list of functions
- // visit each function
- // return
-
- std::vector<FunctionDeclarationNode*> functions = node->function_declarations;
- for (auto element : functions) {
- visitFunctionDeclaration(element);
- };
-
- return;
-}
-
-std::string visitFunctionDeclaration(FunctionDeclarationNode* node){
- // check for dupe name
- // create function symbol and insert into global scope
- // check if main function
- // enter function scope
- // process parameters
- // process function body
- // verify non-void functions have return statements
- // exit function scope and clear context
- // return
-
- current_function = node;
-
-}
-
-std::string visitParameter(ParameterNode*){
- // get parameter info from node
- // check for dupes
- // create parameter symbol
- // insert into current scope
- // return parameter type
-
-}
-
-std::string visitVariableDeclaration(VariableDeclarationNode* node){
- // get variable information
- // check for dupes
- // if initializer exists, check type
- // create variable symbol
- // insert into current scope
- // return
-
-}
-
-std::string visitAssignment(AssignmentNode* node){
- // get left side info
- // look up target variable
- // verify variable
- // analyze right side info
- // check type compatability
- // make var as initialized
- // return
-
-}
-
-std::string visitIfStatement(IfStatementNode* node){
- // analyze condition
- // verify condition is bool
- // analyze then branch
- // analyze else branch
- // return
-
-}
-
-std::string visitWhileStatement(WhileStatementNode* node){
- // analyze condition
- // verify condition is bool
- // analyze loop body
- // return
-
-}
-
-std::string visitForStatement(ForStatementNode* node){
- // enter new scope for loop
- // analyze initialization
- // analyze condition
- // analyze update statement
- // analyze loop body
- // exit for loop scope
- // return
-
-}
-
-std::string visitReturnStatement(ReturnStatementNode* node){
- // check if inside function
- // get return expression
- // analyze return expression
- // validate return type matches function
- // return
-
-}
-
-std::string visitExpressionStatement(ExpressionStatementNode* node){
- // get the expression
- // analyze expression
- // return
-
-}
-
-std::string visitBinaryExpression(BinaryExprNode* node){
- // get operator and operands
- // analyze operands
- // infer result type based on operator
- // return result type
-
-}
-
-std::string visitUnaryExpression(UnaryExprNode* node){
- // get operator and operand
- // analyze operand
- // infer result type
- // return result type
-
-}
-
-std::string visitFunctionCall(FunctionCallNode* node){
- // get function name and args
- // look up function
- // verify its a function
- // get expected parameter information
- // check argument count
- // check each argument type
- // return functions return type
-
-}
-
-std::string visitIdentifier(IdentifierNode* node){
- // get identifier name
- // look up in symbol table
- // check if initialized
- // return symbol type
-
-}
-
-std::string visitLiteral(LiteralNode* node){
- // determine literal type
- // return appropriate type
-
-}
-
-bool checkTypeCompatibility(std::string target, std::string source){
- // determines if a value of source type can be used where target type is expected
-
-}
-
-std::string inferBinaryOpType(std::string op, std::string left, std::string right){
-
-}
diff --git a/src/symbol.cpp b/src/symbol.cpp
deleted file mode 100644
index ad2041d..0000000
--- a/src/symbol.cpp
+++ /dev/null
@@ -1,126 +0,0 @@
-#include "symbol.hpp"
-
-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/symbol_table.cpp b/src/symbol_table.cpp
deleted file mode 100644
index 81ff474..0000000
--- a/src/symbol_table.cpp
+++ /dev/null
@@ -1,89 +0,0 @@
-#include "symbol_table.hpp"
-
-#include <iostream>
-
-SymbolTable::SymbolTable() : scopes(std::vector<Scope>), current_scope(nullptr), scope_level(0), global_scope(nullptr) {}
-
-SymbolTable::~SymbolTable() {}
-
-void SymbolTable::enterScope(std::string scope_name) {
- Scope scope = Scope(scope_name, scope_level, current_scope);
- scopes.push_back(scope*);
-
- this->current_scope = scope*;
- this->scope_level ++;
-}
-
-void SymbolTable::exitScope() {
- parent_scope = getParentScope();
- this->current_scope = parent_scope
-
- this->scope_level --;
-}
-
-Scope* SymbolTable::getCurrentScope() {
- return this->current_scope;
-}
-
-Scope* SymbolTable::getGlobalScope() {
- return this->global_scope;
-}
-
-int SymbolTable::getScopeLevel() {
- return this->scope_level;
-}
-
-void SymbolTable::insert(Symbol symbol) {
- if (this->current_scope == nullptr) {
- // error
- }
-
- this->current_scope->define(symbol);
-}
-
-Symbol* SymbolTable::lookup(std::string name) {
- current_scope = getCurrentScope();
- symbol = current_scope->lookup(name);
-
- while (symbol == nullptr) {
- current_scope = current_scope->getParentScope();
- if (current_scope == nullptr) {
- return nullptr;
- }
-
- symbol = current_scope->lookup(name);
- }
-
- return symbol;
-}
-
-Symbol* SymbolTable::lookupCurrentScope(std::string name) {
- current_scope = getCurrentScope();
- return current_scope->lookup(name);
-}
-
-bool SymbolTable::isDeclared(std::string name) {
- symbol == this->lookup(name);
- return (symbol != nullptr);
-}
-
-bool SymbolTable::isDeclaredInCurrentScope(std::string name) {
- symbol == this->lookupCurrentSceop(name);
- return (symbol != nullptr);
-}
-
-void display() {
- std::cout << this->toString();
-}
-
-std::string toString() {
- std::string indent;
- std::string result;
-
- for (const Scope& i : this->scopes) {
- indent += (" " * i->getScopeLevel());
- result += (indent + '\n' + i->toString());
- }
-
- return result;
-}