From 5c1f937a7eb7a9cc9cd86cb69b3263f41f24408f Mon Sep 17 00:00:00 2001 From: Cori Barker Date: Sun, 22 Feb 2026 22:24:27 +0000 Subject: Partially completed lots of changes, refactoring most of the files --- include/ast_node.hpp | 36 +++++ include/lexer.hpp | 24 ++++ include/lexer/lexer.hpp | 27 ---- include/lexer/token.hpp | 17 --- include/lexer/token_type.hpp | 25 ---- include/parser.hpp | 28 ++++ include/parser/ast_node.hpp | 70 ---------- include/parser/node_type.hpp | 10 -- include/parser/parser.hpp | 31 ----- include/scope.hpp | 23 ++++ include/semantic/scope.hpp | 23 ---- include/semantic/semantic_analyzer.hpp | 46 ------- include/semantic/symbol.hpp | 38 ------ include/semantic/symbol_table.hpp | 25 ---- include/semantic/symbol_type.hpp | 7 - include/semantic_analyzer.hpp | 46 +++++++ include/symbol.hpp | 38 ++++++ include/symbol_table.hpp | 25 ++++ include/symbol_type.hpp | 7 + include/token.hpp | 14 ++ include/token_type.hpp | 22 +++ include/type.hpp | 11 ++ src/lexer.cpp | 103 +++++++++++++++ src/lexer/lexer.cpp | 103 --------------- src/main.cpp | 4 +- src/parser.cpp | 171 ++++++++++++++++++++++++ src/parser/parser.cpp | 171 ------------------------ src/scope.cpp | 52 ++++++++ src/semantic/scope.cpp | 52 -------- src/semantic/semantic_analyzer.cpp | 33 ----- src/semantic/symbol.cpp | 126 ------------------ src/semantic/symbol_table.cpp | 89 ------------- src/semantic_analyzer.cpp | 235 +++++++++++++++++++++++++++++++++ src/symbol.cpp | 126 ++++++++++++++++++ src/symbol_table.cpp | 89 +++++++++++++ 35 files changed, 1052 insertions(+), 895 deletions(-) create mode 100644 include/ast_node.hpp create mode 100644 include/lexer.hpp delete mode 100644 include/lexer/lexer.hpp delete mode 100644 include/lexer/token.hpp delete mode 100644 include/lexer/token_type.hpp create mode 100644 include/parser.hpp delete mode 100644 include/parser/ast_node.hpp delete mode 100644 include/parser/node_type.hpp delete mode 100644 include/parser/parser.hpp create mode 100644 include/scope.hpp delete mode 100644 include/semantic/scope.hpp delete mode 100644 include/semantic/semantic_analyzer.hpp delete mode 100644 include/semantic/symbol.hpp delete mode 100644 include/semantic/symbol_table.hpp delete mode 100644 include/semantic/symbol_type.hpp create mode 100644 include/semantic_analyzer.hpp create mode 100644 include/symbol.hpp create mode 100644 include/symbol_table.hpp create mode 100644 include/symbol_type.hpp create mode 100644 include/token.hpp create mode 100644 include/token_type.hpp create mode 100644 include/type.hpp create mode 100644 src/lexer.cpp delete mode 100644 src/lexer/lexer.cpp create mode 100644 src/parser.cpp delete mode 100644 src/parser/parser.cpp create mode 100644 src/scope.cpp delete mode 100644 src/semantic/scope.cpp delete mode 100644 src/semantic/semantic_analyzer.cpp delete mode 100644 src/semantic/symbol.cpp delete mode 100644 src/semantic/symbol_table.cpp create mode 100644 src/semantic_analyzer.cpp create mode 100644 src/symbol.cpp create mode 100644 src/symbol_table.cpp diff --git a/include/ast_node.hpp b/include/ast_node.hpp new file mode 100644 index 0000000..caffb35 --- /dev/null +++ b/include/ast_node.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "node_type.hpp" +#include "type.hpp" + +#include +#include +#include + +class ASTNode { +public: + int line; + int column; + + virtual ~ASTNode() = default; +}; + +class ProgramNode : ASTNode { +public: + std::vector function_declarations; + + explicit ProgramNode(std::vector f) : function_declarations(f) { } +}; + +class FunctionDeclNode : ASTNode { + Type type; + std::vector parameters; + std::vector body; + + FunctionDeclNode(std::string t,std::vector p, std::vector b) : type(t), parameters(p), body(b) { } +}; + +class ParameterNode : ASTNode { + std::string name; + std::string type; +}; diff --git a/include/lexer.hpp b/include/lexer.hpp new file mode 100644 index 0000000..2ef9700 --- /dev/null +++ b/include/lexer.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "token.hpp" + +#include +#include + +class Lexer { +public: + explicit Lexer (const std::string& src); + + std::vector tokenise(); + +private: + int line; + int column; + int position; + std::string src; + std::vector tokens; + + char advance(); + void skipWhitespace(); + void skipComment(); +}; diff --git a/include/lexer/lexer.hpp b/include/lexer/lexer.hpp deleted file mode 100644 index 2c165b6..0000000 --- a/include/lexer/lexer.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef LEXER_H -#define LEXER_H - -#include "token.hpp" - -#include -#include - -class Lexer { -public: - explicit Lexer (const std::string& src); - - std::vector tokenise(); - -private: - int line; - int column; - int position; - std::string src; - std::vector tokens; - - char advance(); - void skipWhitespace(); - void skipComment(); -}; - -#endif diff --git a/include/lexer/token.hpp b/include/lexer/token.hpp deleted file mode 100644 index 86a41f6..0000000 --- a/include/lexer/token.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef TOKEN_H -#define TOKEN_H - -#include "token_type.hpp" - -#include - -struct Token { - TokenType type; - std::string value; - int line; - int column; - - Token(TokenType t, const std::string& val, int line, int col) : type{t}, value{val}, line{line}, column{col} {}; -}; - -#endif diff --git a/include/lexer/token_type.hpp b/include/lexer/token_type.hpp deleted file mode 100644 index f83c6d6..0000000 --- a/include/lexer/token_type.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef TOKEN_TYPE_H -#define TOKEN_TYPE_H - -enum class TokenType { - INT, - STRING, - - NUMBER, - IDENTIFIER, - - PLUS, - MINUS, - MULTIPLY, - DIVIDE, - - ASSIGN, - - SEMICOLON, - - END_OF_FILE, - INVALID - -}; - -#endif diff --git a/include/parser.hpp b/include/parser.hpp new file mode 100644 index 0000000..f986ce2 --- /dev/null +++ b/include/parser.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "lexer/token.hpp" +#include "parser/ast_node.hpp" + +#include + +class Parser { +public: + explicit Parser(const std::vector& tokens); + std::unique_ptr parse(); + +private: + std::vector tokens_; + int position_; + + std::unique_ptr parseStatement(); + std::unique_ptr parseExpression(); + std::unique_ptr parseAddSub(); + std::unique_ptr parseMulDiv(); + std::unique_ptr parsePrimary(); + std::unique_ptr parseDeclaration(); + std::unique_ptr parseAssignment(); + Token advance(); + Token peek(); + bool isAtEnd(); + void error(std::string s); +}; diff --git a/include/parser/ast_node.hpp b/include/parser/ast_node.hpp deleted file mode 100644 index 6539cf6..0000000 --- a/include/parser/ast_node.hpp +++ /dev/null @@ -1,70 +0,0 @@ -#ifndef AST_NODE_H -#define AST_NODE_H - -#include "node_type.hpp" - -#include -#include -#include - -class ASTNode { -public: - int line; - int column; - - virtual ~ASTNode() = default; -}; - -class Program : public ASTNode { -public: - std::vector> declarations; -}; - -class Declaration : public ASTNode { -public: - std::string type; - std::string var_name; - std::unique_ptr value; - - Declaration(std::string type, std::string var_name, std::unique_ptr value = nullptr) : type(type), var_name(var_name), value(std::move(value)) {} -}; - -class Assignment : public ASTNode { -public: - std::string variable_name; - std::unique_ptr value; - - Assignment(std::string var, std::unique_ptr val) : variable_name(var), value(std::move(val)) {} -}; - -class NumberLiteral : public ASTNode { -public: - double value; - - NumberLiteral(double val) : value(val) {} -}; - -class StringLiteral : public ASTNode { -public: - std::string value; - - StringLiteral(std::string val) : value(val) {} -}; - -class Identifier : public ASTNode { -public: - std::string name; - - Identifier(std::string name) : name(name) {} -}; - -class BinaryOp : public ASTNode { -public: - std::unique_ptr left; - std::string value; - std::unique_ptr right; - - BinaryOp(std::unique_ptr left, std::string value, std::unique_ptr right) : left(std::move(left)), value(std::move(value)), right(std::move(right)) {} -}; - -#endif diff --git a/include/parser/node_type.hpp b/include/parser/node_type.hpp deleted file mode 100644 index 1766b19..0000000 --- a/include/parser/node_type.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef NODE_TYPE_H -#define NODE_TYPE_H - -enum class NodeType { - PROGRAM, - VARIABLE_DECLARATION, - ASSIGNMENT, -}; - -#endif diff --git a/include/parser/parser.hpp b/include/parser/parser.hpp deleted file mode 100644 index 438b8fe..0000000 --- a/include/parser/parser.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef PARSER_H -#define PARSER_H - -#include "lexer/token.hpp" -#include "parser/ast_node.hpp" - -#include - -class Parser { -public: - explicit Parser(const std::vector& tokens); - std::unique_ptr parse(); - -private: - std::vector tokens_; - int position_; - - std::unique_ptr parseStatement(); - std::unique_ptr parseExpression(); - std::unique_ptr parseAddSub(); - std::unique_ptr parseMulDiv(); - std::unique_ptr parsePrimary(); - std::unique_ptr parseDeclaration(); - std::unique_ptr parseAssignment(); - Token advance(); - Token peek(); - bool isAtEnd(); - void error(std::string s); -}; - -#endif diff --git a/include/scope.hpp b/include/scope.hpp new file mode 100644 index 0000000..ff20542 --- /dev/null +++ b/include/scope.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +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 getAllSymbols(); + std::string toString(); + +private: + std::string scope_name; + int scope_level + *Scope parent_scope; + std::unordered_map symbols; +}; diff --git a/include/semantic/scope.hpp b/include/semantic/scope.hpp deleted file mode 100644 index ff20542..0000000 --- a/include/semantic/scope.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include -#include - -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 getAllSymbols(); - std::string toString(); - -private: - std::string scope_name; - int scope_level - *Scope parent_scope; - std::unordered_map symbols; -}; diff --git a/include/semantic/semantic_analyzer.hpp b/include/semantic/semantic_analyzer.hpp deleted file mode 100644 index 0d56599..0000000 --- a/include/semantic/semantic_analyzer.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -class SemanticAnalyzer { -public: - explicit SemanticAnalyzer(); - bool analyze(ASTNode* ast); - std::vector getErrors(); - std::vector 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 errors; - std::vector warnings; - FunctionDeclNode* current_function; - std::string current_function_return_type; - bool has_main_function; -}; diff --git a/include/semantic/symbol.hpp b/include/semantic/symbol.hpp deleted file mode 100644 index 4bee45d..0000000 --- a/include/semantic/symbol.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include -#include - -#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 getParameterTypes(); - void setParameterTypes(std::vector types); - std::string getReturnType(); - void setReturnType(std::string type); - int getLineDeclared(); - void setLineDeclared(int line); - std::string toString(); - -private: - std::string symbol_name; - SymbolType symbol_type; - std::string data_type; - int scope_level; - bool is_initialized; - bool is_parameter; - std::vector parameter_types; - std::string return_type; - int line_declared; - int column_declared; -}; diff --git a/include/semantic/symbol_table.hpp b/include/semantic/symbol_table.hpp deleted file mode 100644 index b2b8270..0000000 --- a/include/semantic/symbol_table.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -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> scopes; - Scope* current_scope; - int scope_level; - Scope* global_scope; -}; diff --git a/include/semantic/symbol_type.hpp b/include/semantic/symbol_type.hpp deleted file mode 100644 index a792c09..0000000 --- a/include/semantic/symbol_type.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -enum class SymbolType { - VARIABLE, - FUNCTION, - PARAMETER, -}; diff --git a/include/semantic_analyzer.hpp b/include/semantic_analyzer.hpp new file mode 100644 index 0000000..0d56599 --- /dev/null +++ b/include/semantic_analyzer.hpp @@ -0,0 +1,46 @@ +#pragma once + +class SemanticAnalyzer { +public: + explicit SemanticAnalyzer(); + bool analyze(ASTNode* ast); + std::vector getErrors(); + std::vector 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 errors; + std::vector warnings; + FunctionDeclNode* current_function; + std::string current_function_return_type; + bool has_main_function; +}; diff --git a/include/symbol.hpp b/include/symbol.hpp new file mode 100644 index 0000000..4bee45d --- /dev/null +++ b/include/symbol.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +#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 getParameterTypes(); + void setParameterTypes(std::vector types); + std::string getReturnType(); + void setReturnType(std::string type); + int getLineDeclared(); + void setLineDeclared(int line); + std::string toString(); + +private: + std::string symbol_name; + SymbolType symbol_type; + std::string data_type; + int scope_level; + bool is_initialized; + bool is_parameter; + std::vector parameter_types; + std::string return_type; + int line_declared; + int column_declared; +}; diff --git a/include/symbol_table.hpp b/include/symbol_table.hpp new file mode 100644 index 0000000..b2b8270 --- /dev/null +++ b/include/symbol_table.hpp @@ -0,0 +1,25 @@ +#pragma once + +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> scopes; + Scope* current_scope; + int scope_level; + Scope* global_scope; +}; diff --git a/include/symbol_type.hpp b/include/symbol_type.hpp new file mode 100644 index 0000000..a792c09 --- /dev/null +++ b/include/symbol_type.hpp @@ -0,0 +1,7 @@ +#pragma once + +enum class SymbolType { + VARIABLE, + FUNCTION, + PARAMETER, +}; diff --git a/include/token.hpp b/include/token.hpp new file mode 100644 index 0000000..d85ba94 --- /dev/null +++ b/include/token.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include "token_type.hpp" + +#include + +struct Token { + TokenType type; + std::string value; + int line; + int column; + + Token(TokenType t, const std::string& val, int line, int col) : type{t}, value{val}, line{line}, column{col} {}; +}; diff --git a/include/token_type.hpp b/include/token_type.hpp new file mode 100644 index 0000000..d123aaa --- /dev/null +++ b/include/token_type.hpp @@ -0,0 +1,22 @@ +#pragma once + +enum class TokenType { + INT, + STRING, + + NUMBER, + IDENTIFIER, + + PLUS, + MINUS, + MULTIPLY, + DIVIDE, + + ASSIGN, + + SEMICOLON, + + END_OF_FILE, + INVALID + +}; diff --git a/include/type.hpp b/include/type.hpp new file mode 100644 index 0000000..c46ff34 --- /dev/null +++ b/include/type.hpp @@ -0,0 +1,11 @@ +#pragma once + +enum struct Type { + INT, + CHAR, + BOOL, + STRING, + FLOAT, + DOUBLE, + VOID, +}; diff --git a/src/lexer.cpp b/src/lexer.cpp new file mode 100644 index 0000000..3b134eb --- /dev/null +++ b/src/lexer.cpp @@ -0,0 +1,103 @@ +#include "lexer.hpp" + +Lexer::Lexer(const std::string& src) : src(src), position(0), line(1), column(1) {} + +std::vector Lexer::tokenise() { + while (position < src.size()) { + if (std::isdigit(src[position])) { + std::string digit; + int original_column = column; + + while (std::isdigit(src[position])) { + digit.push_back(src[position]); + advance(); + } + + tokens.push_back(Token(TokenType::NUMBER, digit, line, original_column)); + + } else if (std::isalpha(src[position])) { + if (src.substr(position, 3) == "int") { + tokens.push_back(Token(TokenType::INT, "int", line, column)); + advance(); + advance(); + advance(); + + } else { + std::string identifier; + int original_column = column; + + while (std::isalnum(src[position])) { + identifier.push_back(src[position]); + advance(); + } + + tokens.push_back(Token(TokenType::IDENTIFIER, identifier, line, original_column)); + } + + } else if (src[position] == '+') { + tokens.push_back(Token(TokenType::PLUS, std::string(1, src[position]), line, column)); + advance(); + + } else if (src[position] == '-') { + tokens.push_back(Token(TokenType::MINUS, std::string(1, src[position]), line, column)); + advance(); + + } else if (src[position] == '*') { + tokens.push_back(Token(TokenType::MULTIPLY, std::string(1, src[position]), line, column)); + advance(); + + } else if (src[position] == '/') { + tokens.push_back(Token(TokenType::DIVIDE, std::string(1, src[position]), line, column)); + advance(); + + } else if (src[position] == '=') { + tokens.push_back(Token(TokenType::ASSIGN, std::string(1, src[position]), line, column)); + advance(); + + } else if (src[position] == ';') { + tokens.push_back(Token(TokenType::SEMICOLON, std::string(1, src[position]), line, column)); + advance(); + + } else { + tokens.push_back(Token(TokenType::INVALID, std::string(1, src[position]), line, column)); + advance(); + + } + skipComment(); + skipWhitespace(); + + } + tokens.push_back(Token(TokenType::END_OF_FILE, std::string(), line, column)); + return tokens; +} + +char Lexer::advance() { + column++; + return src[position++]; +} + +void Lexer::skipWhitespace() { + while (src[position] == ' ' || src[position] == '\n') { + if (src[position] == ' ') { + position++; + column++; + + } else if (src[position] == '\n') { + position++; + column = 1; + line++; + } + } +} + +void Lexer::skipComment() { + if (src[position] == '/' && src[++position] == '/') { + while (src[position] != '\n') { + position++; + column++; + } + position++; + line += 1; + column = 1; + } +} diff --git a/src/lexer/lexer.cpp b/src/lexer/lexer.cpp deleted file mode 100644 index 70af4b5..0000000 --- a/src/lexer/lexer.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include "lexer/lexer.hpp" - -Lexer::Lexer(const std::string& src) : src(src), position(0), line(1), column(1) {} - -std::vector Lexer::tokenise() { - while (position < src.size()) { - if (std::isdigit(src[position])) { - std::string digit; - int original_column = column; - - while (std::isdigit(src[position])) { - digit.push_back(src[position]); - advance(); - } - - tokens.push_back(Token(TokenType::NUMBER, digit, line, original_column)); - - } else if (std::isalpha(src[position])) { - if (src.substr(position, 3) == "int") { - tokens.push_back(Token(TokenType::INT, "int", line, column)); - advance(); - advance(); - advance(); - - } else { - std::string identifier; - int original_column = column; - - while (std::isalnum(src[position])) { - identifier.push_back(src[position]); - advance(); - } - - tokens.push_back(Token(TokenType::IDENTIFIER, identifier, line, original_column)); - } - - } else if (src[position] == '+') { - tokens.push_back(Token(TokenType::PLUS, std::string(1, src[position]), line, column)); - advance(); - - } else if (src[position] == '-') { - tokens.push_back(Token(TokenType::MINUS, std::string(1, src[position]), line, column)); - advance(); - - } else if (src[position] == '*') { - tokens.push_back(Token(TokenType::MULTIPLY, std::string(1, src[position]), line, column)); - advance(); - - } else if (src[position] == '/') { - tokens.push_back(Token(TokenType::DIVIDE, std::string(1, src[position]), line, column)); - advance(); - - } else if (src[position] == '=') { - tokens.push_back(Token(TokenType::ASSIGN, std::string(1, src[position]), line, column)); - advance(); - - } else if (src[position] == ';') { - tokens.push_back(Token(TokenType::SEMICOLON, std::string(1, src[position]), line, column)); - advance(); - - } else { - tokens.push_back(Token(TokenType::INVALID, std::string(1, src[position]), line, column)); - advance(); - - } - skipComment(); - skipWhitespace(); - - } - tokens.push_back(Token(TokenType::END_OF_FILE, std::string(), line, column)); - return tokens; -} - -char Lexer::advance() { - column++; - return src[position++]; -} - -void Lexer::skipWhitespace() { - while (src[position] == ' ' || src[position] == '\n') { - if (src[position] == ' ') { - position++; - column++; - - } else if (src[position] == '\n') { - position++; - column = 1; - line++; - } - } -} - -void Lexer::skipComment() { - if (src[position] == '/' && src[++position] == '/') { - while (src[position] != '\n') { - position++; - column++; - } - position++; - line += 1; - column = 1; - } -} diff --git a/src/main.cpp b/src/main.cpp index 9a11516..24a748d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,8 +2,8 @@ #include #include -#include "lexer/lexer.hpp" -#include "parser/parser.hpp" +#include "lexer.hpp" +#include "parser.hpp" std::string tokenTypeToString(TokenType type) { switch (type) { diff --git a/src/parser.cpp b/src/parser.cpp new file mode 100644 index 0000000..6f47890 --- /dev/null +++ b/src/parser.cpp @@ -0,0 +1,171 @@ +#include + +#include "parser.hpp" + +Parser::Parser(const std::vector& tokens) : tokens_(tokens), position_(0) {} + +std::unique_ptr Parser::parse() { + auto program = std::make_unique(); + + while (peek().type != TokenType::END_OF_FILE) { + try { + auto statement = parseStatement(); + program->declarations.push_back(std::move(statement)); + + if (peek().type == TokenType::SEMICOLON) { + advance(); + } + } + + catch (const std::exception& e) { + std::cerr << " Parser error: " << e.what() << std::endl; + break; + } + } + + return program; +} + +std::unique_ptr Parser::parseStatement() { + switch(peek().type) { + case TokenType::INT: + case TokenType::STRING: + { + return parseDeclaration(); + break; + } + + default: + { + error("Unexpected statement"); + return nullptr; + } + } +} + +std::unique_ptr Parser::parseExpression() { + return parseAddSub(); +} + +std::unique_ptr Parser::parseDeclaration() { + std::string type; + + switch(peek().type) { + case TokenType::INT: + { + type = "int"; + break; + } + + case TokenType::STRING: + { + type = "string"; + break; + } + + default: + { + error("Unexpected declaration: Invalid type"); + return nullptr; + } + } + + advance(); + std::string name = peek().value; + advance(); + if (peek().type == TokenType::ASSIGN) { + advance(); + auto value = parseExpression(); + return std::make_unique(type, name, std::move(value)); + } + + else if (peek().type == TokenType::SEMICOLON) { + return std::make_unique(type, name); + } + + else { + error("Invalid declaration"); + return nullptr; + } +} + +std::unique_ptr Parser::parseAssignment() { + std::string name = peek().value; + advance(); + advance(); // consume = + auto value = parseExpression(); + return std::make_unique(name, std::move(value)); +} + +std::unique_ptr Parser::parseAddSub() { + auto left = parseMulDiv(); + + while (peek().type == TokenType::PLUS || peek().type == TokenType::MINUS) { + std::string op = peek().value; + advance(); + auto right = parseMulDiv(); + left = std::make_unique(std::move(left), op, std::move(right)); + } + + return left; +} + +std::unique_ptr Parser::parseMulDiv() { + auto left = parsePrimary(); + + while (peek().type == TokenType::MULTIPLY || peek().type == TokenType::DIVIDE) { + std::string op = peek().value; + advance(); + auto right = parsePrimary(); + left = std::make_unique(std::move(left), op, std::move(right)); + } + + return left; +} + +std::unique_ptr Parser::parsePrimary() { + switch(peek().type) { + case TokenType::NUMBER: + { + double value = std::stod(peek().value); + advance(); + return std::make_unique(value); + } + + case TokenType::IDENTIFIER: + { + std::string name = peek().value; + advance(); + return std::make_unique(name); + } + + case TokenType::STRING: + { + std::string value = peek().value; + advance(); + return std::make_unique(value); + } + + default: + { + error("Unknown keyword"); + return nullptr; + } + } +} + +Token Parser::advance() { + return tokens_[position_++]; +} + +Token Parser::peek() { + return tokens_[position_]; +} + +bool Parser::isAtEnd() { + return position_ >= tokens_.size(); +} + +void Parser::error(std::string s) { + +} diff --git a/src/parser/parser.cpp b/src/parser/parser.cpp deleted file mode 100644 index 97a1f27..0000000 --- a/src/parser/parser.cpp +++ /dev/null @@ -1,171 +0,0 @@ -#include - -#include "parser/parser.hpp" - -Parser::Parser(const std::vector& tokens) : tokens_(tokens), position_(0) {} - -std::unique_ptr Parser::parse() { - auto program = std::make_unique(); - - while (peek().type != TokenType::END_OF_FILE) { - try { - auto statement = parseStatement(); - program->declarations.push_back(std::move(statement)); - - if (peek().type == TokenType::SEMICOLON) { - advance(); - } - } - - catch (const std::exception& e) { - std::cerr << " Parser error: " << e.what() << std::endl; - break; - } - } - - return program; -} - -std::unique_ptr Parser::parseStatement() { - switch(peek().type) { - case TokenType::INT: - case TokenType::STRING: - { - return parseDeclaration(); - break; - } - - default: - { - error("Unexpected statement"); - return nullptr; - } - } -} - -std::unique_ptr Parser::parseExpression() { - return parseAddSub(); -} - -std::unique_ptr Parser::parseDeclaration() { - std::string type; - - switch(peek().type) { - case TokenType::INT: - { - type = "int"; - break; - } - - case TokenType::STRING: - { - type = "string"; - break; - } - - default: - { - error("Unexpected declaration: Invalid type"); - return nullptr; - } - } - - advance(); - std::string name = peek().value; - advance(); - if (peek().type == TokenType::ASSIGN) { - advance(); - auto value = parseExpression(); - return std::make_unique(type, name, std::move(value)); - } - - else if (peek().type == TokenType::SEMICOLON) { - return std::make_unique(type, name); - } - - else { - error("Invalid declaration"); - return nullptr; - } -} - -std::unique_ptr Parser::parseAssignment() { - std::string name = peek().value; - advance(); - advance(); // consume = - auto value = parseExpression(); - return std::make_unique(name, std::move(value)); -} - -std::unique_ptr Parser::parseAddSub() { - auto left = parseMulDiv(); - - while (peek().type == TokenType::PLUS || peek().type == TokenType::MINUS) { - std::string op = peek().value; - advance(); - auto right = parseMulDiv(); - left = std::make_unique(std::move(left), op, std::move(right)); - } - - return left; -} - -std::unique_ptr Parser::parseMulDiv() { - auto left = parsePrimary(); - - while (peek().type == TokenType::MULTIPLY || peek().type == TokenType::DIVIDE) { - std::string op = peek().value; - advance(); - auto right = parsePrimary(); - left = std::make_unique(std::move(left), op, std::move(right)); - } - - return left; -} - -std::unique_ptr Parser::parsePrimary() { - switch(peek().type) { - case TokenType::NUMBER: - { - double value = std::stod(peek().value); - advance(); - return std::make_unique(value); - } - - case TokenType::IDENTIFIER: - { - std::string name = peek().value; - advance(); - return std::make_unique(name); - } - - case TokenType::STRING: - { - std::string value = peek().value; - advance(); - return std::make_unique(value); - } - - default: - { - error("Unknown keyword"); - return nullptr; - } - } -} - -Token Parser::advance() { - return tokens_[position_++]; -} - -Token Parser::peek() { - return tokens_[position_]; -} - -bool Parser::isAtEnd() { - return position_ >= tokens_.size(); -} - -void Parser::error(std::string s) { - -} diff --git a/src/scope.cpp b/src/scope.cpp new file mode 100644 index 0000000..86e2668 --- /dev/null +++ b/src/scope.cpp @@ -0,0 +1,52 @@ +#include "scope.hpp" + +Scope::Scope(std::string name, int level, std::unique_ptr 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::getParentScope() { + return parent_scope; +} + +void Scope::define(Symbol symbol) { + if (isDeclared(symbol->getName())) { + // error + } + + else { + symobls.add(symbol->getName(), symbol); + } +} + +std::unique_ptr 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 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/scope.cpp b/src/semantic/scope.cpp deleted file mode 100644 index ba5aa41..0000000 --- a/src/semantic/scope.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "semantic/scope.hpp" - -Scope::Scope(std::string name, int level, std::unique_ptr 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::getParentScope() { - return parent_scope; -} - -void Scope::define(Symbol symbol) { - if (isDeclared(symbol->getName())) { - // error - } - - else { - symobls.add(symbol->getName(), symbol); - } -} - -std::unique_ptr 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 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 deleted file mode 100644 index 57f217e..0000000 --- a/src/semantic/semantic_analyzer.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "semantic/semantic_analyzer.hpp" - -SemanticAnalyzer::SemanticAnalyzer() : symobl_table(new SemanticTable), errors(std::vector), warnings(std::vector), current_function(nullptr), current_function_return_type(""), has_main_function(false) {} - -bool SemanticAnalyzer::analyze(ASTNode* ast) { - this->symbol_table.enterScope("global") - this->visit(ast); - this->validateMainFunction(); - - if (has_main_function == false) { - //error - return false; - } - - this->symbol_table.exitScope(); - return true; -} - -std::vector SemanticAnalyzer::getErrors() { - return this->errors; -} - -std::vector SemanticAnalyzer::getWarnings() { - return this->warnings; -} - -bool SemanticAnalyzer::hasErrors() { - return !this->errors.empty(); -} - -std::string SemanticAnalyzer::visit(ASTNode* ast) { - -} diff --git a/src/semantic/symbol.cpp b/src/semantic/symbol.cpp deleted file mode 100644 index 5142ea8..0000000 --- a/src/semantic/symbol.cpp +++ /dev/null @@ -1,126 +0,0 @@ -#include "semantic/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 Symbol::getParameterTypes() { - return paremeter_types; -} - -void Symbol::setParameterTypes(std::vector 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 deleted file mode 100644 index 548d6ee..0000000 --- a/src/semantic/symbol_table.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include "semantic/symbol_table.hpp" - -#include - -SymbolTable::SymbolTable() : scopes(std::vector), 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; -} diff --git a/src/semantic_analyzer.cpp b/src/semantic_analyzer.cpp new file mode 100644 index 0000000..e097e4f --- /dev/null +++ b/src/semantic_analyzer.cpp @@ -0,0 +1,235 @@ +#include "semantic_analyzer.hpp" + +SemanticAnalyzer::SemanticAnalyzer() : symobl_table(new SemanticTable), errors(std::vector), warnings(std::vector), current_function(nullptr), current_function_return_type(""), has_main_function(false) {} + +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 SemanticAnalyzer::getErrors() { + return errors; +} + +std::vector SemanticAnalyzer::getWarnings() { + return warnings; +} + +bool SemanticAnalyzer::hasErrors() { + return !errors.empty(); +} + +std::string SemanticAnalyzer::visit(ASTNode* node) { + if (ProgramNode* prog = dynamic_cast(node)) { + return visitProgram(prog); + } + + else if (FunctionDeclNode* func = dynamic_cast(node)) { + return visitFunctionDecl(func); + } + + else if (ParameterNode* param = dynamic_cast(node)) { + return visitParameter(param); + } + + else if (VarDeclNode* var = dynamic_cast(node)) { + return visitVarDeclaration(var); + } + + else if (AssignmentNode as = dynamic_cast(node)) { + return visitAssignment(as); + } + + else if (IfStatementNode is = dynamic_cast(node)) { + return visitIfStatement(is); + } + + else if (WhileStatementNode ws = dynamic_cast(node)) { + return visitWhileStatement(ws); + } + + else if (ForStatementNode fs = dynamic_cast(node)) { + return visitForStatement(fs); + } + + else if (ReturnStatementNode rs = dynamic_cast(node)) { + return visitReturnStatement(rs); + } + + else if (ExpressionStatementNode es = dynamic_cast(node)) { + return visitExpressionStatement(es); + } + + else if (BinaryExprNode bin = dynamic_cast(node)) { + return visitBinaryExpression(bin); + } + + else if (UnaryExprNode un = dynamic_cast(node)) { + return visitUnaryExpression(un); + } + + else if (FunctionCallNode func_call = dynamic_cast(node)) { + return visitFunctionCall(func_call); + } + + else if (IdentifierNode ident = dynamic_cast(node)) { + return visitIdentifier(ident); + } + + else if (LiteralNode lit = dynamic_cast(node)) { + return visitLiteral(lit); + } +} + +std::string visitProgram(ProgramNode* node){ + // get list of functions + // visit each function + // return + +} + +std::string visitFunctionDecl(FunctionDeclNode* 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 + +} + +std::string visitParameter(ParameterNode*){ + // get parameter info from node + // check for dupes + // create parameter symbol + // insert into current scope + // return parameter type + +} + +std::string visitVarDeclaration(VarDeclNode* 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 new file mode 100644 index 0000000..ad2041d --- /dev/null +++ b/src/symbol.cpp @@ -0,0 +1,126 @@ +#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 Symbol::getParameterTypes() { + return paremeter_types; +} + +void Symbol::setParameterTypes(std::vector 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 new file mode 100644 index 0000000..81ff474 --- /dev/null +++ b/src/symbol_table.cpp @@ -0,0 +1,89 @@ +#include "symbol_table.hpp" + +#include + +SymbolTable::SymbolTable() : scopes(std::vector), 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; +} -- cgit v1.2.3