Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented parsing (merge to dev) #24

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions include/dragon/parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#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 {
private:
std::vector<Token> tokens;
std::vector<Node> nodes;
size_t index;

std::optional<Token> peek() const;
std::optional<Token> peek_next() const;
std::optional<Token> advance(TokenType type);

public:
explicit Parser(std::vector<Token> tokens) : tokens(std::move(tokens)), index(0) {}
explicit Parser() : tokens(std::vector<Token>()), index(0) {}

void reset();

std::vector<Node> parse();
std::vector<Node> parse(const std::vector<Token>& tokens);

std::unique_ptr<VariableDeclarationNode> parse_variable_declaration();
std::unique_ptr<VariableAssignmentNode> parse_variable_assignment();
std::unique_ptr<WhileLoopNode> parse_while_loop();
std::unique_ptr<ForLoopNode> parse_for_loop();
std::unique_ptr<IfElseNode> parse_if_else();
std::unique_ptr<FunctionCallNode> parse_function_call();
std::unique_ptr<ExpressionNode> parse_expression();
};
4 changes: 3 additions & 1 deletion include/dragon/token.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <string>
#include <sstream>
#include <vector>

enum class TokenType {
// Keywords
Expand Down Expand Up @@ -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()) {
Expand Down
33 changes: 32 additions & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
#include "dragon/parser.h"
#include "dragon/parser.h"

std::optional<Token> Parser::peek() const {
if (index < tokens.size()) {
return tokens[index];
}

return std::nullopt;
}

std::optional<Token> Parser::peek_next() const {
if (index + 1 < tokens.size()) {
return tokens[index + 1];
}

return std::nullopt;
}

std::optional<Token> 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();
}