From 40ac2eeabec5df2ecdd26527d8ad492d1ade656c Mon Sep 17 00:00:00 2001 From: hrszpuk <107559570+hrszpuk@users.noreply.github.com> Date: Mon, 6 Jan 2025 09:31:51 +0000 Subject: [PATCH 1/6] Fixed bug with sstream not being included in token.h --- include/dragon/token.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/dragon/token.h b/include/dragon/token.h index 36c4b92..7bdac53 100644 --- a/include/dragon/token.h +++ b/include/dragon/token.h @@ -1,6 +1,8 @@ #pragma once #include +#include +#include enum class TokenType { // Keywords @@ -136,7 +138,7 @@ class Token { return this->type != other.type || this->value != other.value; } - inline std::string to_string() { + inline std::string to_string() const { std::stringstream ss; ss << "Token(" << token_type_to_string(this->type); if (!this->value.empty()) { From 4df475b5ab45a12154f8e04562ad0c53f2c0d211 Mon Sep 17 00:00:00 2001 From: hrszpuk <107559570+hrszpuk@users.noreply.github.com> Date: Mon, 6 Jan 2025 10:06:22 +0000 Subject: [PATCH 2/6] Setting up the parser --- include/dragon/parser.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/dragon/parser.h b/include/dragon/parser.h index e69de29..a96867a 100644 --- a/include/dragon/parser.h +++ b/include/dragon/parser.h @@ -0,0 +1,9 @@ +#pragma once + +#include "token.h" +#include "ast.h" + + + +class Parser { +}; \ No newline at end of file From 6be46cc74aeb0d31e0fdc42f6776bcc44d11ef82 Mon Sep 17 00:00:00 2001 From: hrszpuk <107559570+hrszpuk@users.noreply.github.com> Date: Mon, 6 Jan 2025 10:06:57 +0000 Subject: [PATCH 3/6] Added internal methods, attributes, and public constructors --- include/dragon/parser.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/dragon/parser.h b/include/dragon/parser.h index a96867a..2933c6c 100644 --- a/include/dragon/parser.h +++ b/include/dragon/parser.h @@ -6,4 +6,16 @@ class Parser { +private: + std::vector tokens; + std::vector nodes; + size_t index; + + std::optional peek() const; + std::optional peek_next() const; + std::optional advance(TokenType type); + +public: + explicit Parser(std::vector tokens) : tokens(std::move(tokens)), index(0) {} + explicit Parser() : tokens(std::vector()), index(0) {} }; \ No newline at end of file From a231a5848b01df705c381d11a51eb56d5a74ae17 Mon Sep 17 00:00:00 2001 From: hrszpuk <107559570+hrszpuk@users.noreply.github.com> Date: Mon, 6 Jan 2025 10:07:20 +0000 Subject: [PATCH 4/6] Added methods to parse all AST nodes --- include/dragon/parser.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/include/dragon/parser.h b/include/dragon/parser.h index 2933c6c..74a6b89 100644 --- a/include/dragon/parser.h +++ b/include/dragon/parser.h @@ -3,6 +3,12 @@ #include "token.h" #include "ast.h" +#include "ast/variable_declaration_node.h" +#include "ast/variable_assignment_node.h" +#include "ast/while_loop_node.h" +#include "ast/for_loop_node.h" +#include "ast/if_else_node.h" +#include "ast/function_call_node.h" class Parser { @@ -18,4 +24,17 @@ class Parser { public: explicit Parser(std::vector tokens) : tokens(std::move(tokens)), index(0) {} explicit Parser() : tokens(std::vector()), index(0) {} + + void reset(); + + std::vector parse(); + std::vector parse(const std::vector& tokens); + + std::unique_ptr parse_variable_declaration(); + std::unique_ptr parse_variable_assignment(); + std::unique_ptr parse_while_loop(); + std::unique_ptr parse_for_loop(); + std::unique_ptr parse_if_else(); + std::unique_ptr parse_function_call(); + std::unique_ptr parse_expression(); }; \ No newline at end of file From bad13a5b9f137ad7cdde7502a4107ae0bf7d303f Mon Sep 17 00:00:00 2001 From: hrszpuk <107559570+hrszpuk@users.noreply.github.com> Date: Mon, 6 Jan 2025 10:07:48 +0000 Subject: [PATCH 5/6] Implemented peek() and peek_next() --- src/parser.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/parser.cpp b/src/parser.cpp index 3150749..15097f6 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1 +1,17 @@ -#include "dragon/parser.h" \ No newline at end of file +#include "dragon/parser.h" + +std::optional Parser::peek() const { + if (index < tokens.size()) { + return tokens[index]; + } + + return std::nullopt; +} + +std::optional Parser::peek_next() const { + if (index + 1 < tokens.size()) { + return tokens[index + 1]; + } + + return std::nullopt; +} From db81a5a65309b041ebe995a5f807563bbbc8c388 Mon Sep 17 00:00:00 2001 From: hrszpuk <107559570+hrszpuk@users.noreply.github.com> Date: Mon, 6 Jan 2025 10:08:14 +0000 Subject: [PATCH 6/6] Implemented advance() and reset() [for parser] --- src/parser.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/parser.cpp b/src/parser.cpp index 15097f6..7ace9e9 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -15,3 +15,18 @@ std::optional Parser::peek_next() const { return std::nullopt; } + +std::optional Parser::advance(TokenType type) { + if (index < tokens.size() && tokens[index].type == type) { + return tokens[index++]; + } + + return std::nullopt; +} + +void Parser::reset() { + index = 0; + tokens.clear(); + nodes.clear(); +} +