commit b3f83360282bfe327c0ccbeecab706e0e7d2c050 parent 26738e6c932038ea309a5dbaa9e941fc1ce144b8 Author: Cori Barker <coribarker2@gmail.com> Date: Mon, 9 Feb 2026 11:59:14 +0000 Changed .h to .hpp Diffstat:
22 files changed, 193 insertions(+), 193 deletions(-)
diff --git a/README b/README @@ -1,15 +1,15 @@ -blc -=============== +blc - bl-compiler +================= Written as A-Level project, compiler for the bl-language. Requirements --------------- +------------ - make Building ---------------- +-------- mkdir build cd build ../configure diff --git a/include/lexer/lexer.h b/include/lexer/lexer.h @@ -1,27 +0,0 @@ -#ifndef LEXER_H -#define LEXER_H - -#include "token.h" - -#include <vector> -#include <string> - -class Lexer { -public: - explicit Lexer (const std::string& src); - - std::vector<Token> tokenise(); - -private: - int line; - int column; - int position; - std::string src; - std::vector<Token> tokens; - - char advance(); - void skipWhitespace(); - void skipComment(); -}; - -#endif diff --git a/include/lexer/lexer.hpp b/include/lexer/lexer.hpp @@ -0,0 +1,27 @@ +#ifndef LEXER_H +#define LEXER_H + +#include "token.hpp" + +#include <vector> +#include <string> + +class Lexer { +public: + explicit Lexer (const std::string& src); + + std::vector<Token> tokenise(); + +private: + int line; + int column; + int position; + std::string src; + std::vector<Token> tokens; + + char advance(); + void skipWhitespace(); + void skipComment(); +}; + +#endif diff --git a/include/lexer/token.h b/include/lexer/token.h @@ -1,17 +0,0 @@ -#ifndef TOKEN_H -#define TOKEN_H - -#include "token_type.h" - -#include <string> - -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.hpp b/include/lexer/token.hpp @@ -0,0 +1,17 @@ +#ifndef TOKEN_H +#define TOKEN_H + +#include "token_type.hpp" + +#include <string> + +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.h b/include/lexer/token_type.hpp diff --git a/include/parser/ast_node.h b/include/parser/ast_node.h @@ -1,70 +0,0 @@ -#ifndef AST_NODE_H -#define AST_NODE_H - -#include "parser/node_type.h" - -#include <string> -#include <vector> -#include <memory> - -class ASTNode { -public: - int line; - int column; - - virtual ~ASTNode() = default; -}; - -class Program : public ASTNode { -public: - std::vector<std::unique_ptr<ASTNode>> declarations; -}; - -class Declaration : public ASTNode { -public: - std::string type; - std::string var_name; - std::unique_ptr<ASTNode> value; - - Declaration(std::string type, std::string var_name, std::unique_ptr<ASTNode> value = nullptr) : type(type), var_name(var_name), value(std::move(value)) {} -}; - -class Assignment : public ASTNode { -public: - std::string variable_name; - std::unique_ptr<ASTNode> value; - - Assignment(std::string var, std::unique_ptr<ASTNode> 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<ASTNode> left; - std::string value; - std::unique_ptr<ASTNode> right; - - BinaryOp(std::unique_ptr<ASTNode> left, std::string value, std::unique_ptr<ASTNode> right) : left(std::move(left)), value(std::move(value)), right(std::move(right)) {} -}; - -#endif diff --git a/include/parser/ast_node.hpp b/include/parser/ast_node.hpp @@ -0,0 +1,70 @@ +#ifndef AST_NODE_H +#define AST_NODE_H + +#include "node_type.hpp" + +#include <string> +#include <vector> +#include <memory> + +class ASTNode { +public: + int line; + int column; + + virtual ~ASTNode() = default; +}; + +class Program : public ASTNode { +public: + std::vector<std::unique_ptr<ASTNode>> declarations; +}; + +class Declaration : public ASTNode { +public: + std::string type; + std::string var_name; + std::unique_ptr<ASTNode> value; + + Declaration(std::string type, std::string var_name, std::unique_ptr<ASTNode> value = nullptr) : type(type), var_name(var_name), value(std::move(value)) {} +}; + +class Assignment : public ASTNode { +public: + std::string variable_name; + std::unique_ptr<ASTNode> value; + + Assignment(std::string var, std::unique_ptr<ASTNode> 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<ASTNode> left; + std::string value; + std::unique_ptr<ASTNode> right; + + BinaryOp(std::unique_ptr<ASTNode> left, std::string value, std::unique_ptr<ASTNode> right) : left(std::move(left)), value(std::move(value)), right(std::move(right)) {} +}; + +#endif diff --git a/include/parser/node_type.h b/include/parser/node_type.hpp diff --git a/include/parser/parser.h b/include/parser/parser.h @@ -1,31 +0,0 @@ -#ifndef PARSER_H -#define PARSER_H - -#include "lexer/token.h" -#include "parser/ast_node.h" - -#include <vector> - -class Parser { -public: - explicit Parser(const std::vector<Token>& tokens); - std::unique_ptr<Program> parse(); - -private: - std::vector<Token> tokens_; - int position_; - - std::unique_ptr<ASTNode> parseStatement(); - std::unique_ptr<ASTNode> parseExpression(); - std::unique_ptr<ASTNode> parseAddSub(); - std::unique_ptr<ASTNode> parseMulDiv(); - std::unique_ptr<ASTNode> parsePrimary(); - std::unique_ptr<ASTNode> parseDeclaration(); - std::unique_ptr<ASTNode> parseAssignment(); - Token advance(); - Token peek(); - bool isAtEnd(); - void error(std::string s); -}; - -#endif diff --git a/include/parser/parser.hpp b/include/parser/parser.hpp @@ -0,0 +1,31 @@ +#ifndef PARSER_H +#define PARSER_H + +#include "lexer/token.hpp" +#include "parser/ast_node.hpp" + +#include <vector> + +class Parser { +public: + explicit Parser(const std::vector<Token>& tokens); + std::unique_ptr<Program> parse(); + +private: + std::vector<Token> tokens_; + int position_; + + std::unique_ptr<ASTNode> parseStatement(); + std::unique_ptr<ASTNode> parseExpression(); + std::unique_ptr<ASTNode> parseAddSub(); + std::unique_ptr<ASTNode> parseMulDiv(); + std::unique_ptr<ASTNode> parsePrimary(); + std::unique_ptr<ASTNode> parseDeclaration(); + std::unique_ptr<ASTNode> parseAssignment(); + Token advance(); + Token peek(); + bool isAtEnd(); + void error(std::string s); +}; + +#endif diff --git a/include/semantic/scope.h b/include/semantic/scope.hpp diff --git a/include/semantic/semantic_analyzer.h b/include/semantic/semantic_analyzer.hpp diff --git a/include/semantic/symbol.h b/include/semantic/symbol.h @@ -1,38 +0,0 @@ -#pragma once - -#include <string> -#include <vector> - -#include "symbol_type.h" - -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<std::string> getParameterTypes(); - void setParameterTypes(std::vector<std::string> 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<std::string> parameter_types; - std::string return_type; - int line_declared; - int column_declared; -}; diff --git a/include/semantic/symbol.hpp b/include/semantic/symbol.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include <string> +#include <vector> + +#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<std::string> getParameterTypes(); + void setParameterTypes(std::vector<std::string> 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<std::string> parameter_types; + std::string return_type; + int line_declared; + int column_declared; +}; diff --git a/include/semantic/symbol_table.h b/include/semantic/symbol_table.hpp diff --git a/include/semantic/symbol_type.h b/include/semantic/symbol_type.hpp diff --git a/src/lexer/lexer.cpp b/src/lexer/lexer.cpp @@ -1,4 +1,4 @@ -#include "lexer/lexer.h" +#include "lexer/lexer.hpp" Lexer::Lexer(const std::string& src) : src(src), position(0), line(1), column(1) {} diff --git a/src/main.cpp b/src/main.cpp @@ -2,8 +2,8 @@ #include <sstream> #include <fstream> -#include "lexer/lexer.h" -#include "parser/parser.h" +#include "lexer/lexer.hpp" +#include "parser/parser.hpp" std::string tokenTypeToString(TokenType type) { switch (type) { diff --git a/src/parser/parser.cpp b/src/parser/parser.cpp @@ -1,6 +1,6 @@ #include <iostream> -#include "parser/parser.h" +#include "parser/parser.hpp" Parser::Parser(const std::vector<Token>& tokens) : tokens_(tokens), position_(0) {} diff --git a/src/semantic/scope.cpp b/src/semantic/scope.cpp @@ -1,4 +1,4 @@ -#include "semantic/scope.h" +#include "semantic/scope.hpp" Scope::Scope(std::string name, int level, std::unique_ptr<Scope> parent) : scope_name(name), scope_level(level), parent_scope(parent) {} diff --git a/src/semantic/symbol.cpp b/src/semantic/symbol.cpp @@ -1,4 +1,4 @@ -#include "semantic/symbol.h" +#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) {}