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 ++++++ 22 files changed, 274 insertions(+), 319 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 (limited to 'include') 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, +}; -- cgit v1.2.3