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

Set up ast and nodes for the feature set #23

Merged
merged 11 commits into from
Jan 6, 2025
40 changes: 40 additions & 0 deletions include/dragon/ast.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <string>
#include <memory>
#include <vector>
#include <variant>
#include "token.h"

class Node {
public:
virtual ~Node() = default;
virtual std::string to_string() const = 0;
};

class ExpressionNode : public Node {
public:
Token op;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;

ExpressionNode(Token token, std::unique_ptr<Node> left, std::unique_ptr<Node> right)
: op(std::move(token)), left(std::move(left)), right(std::move(right)) {}

~ExpressionNode() override = default;

std::string to_string() const override {
return "ExpressionNode(op: " + op.to_string() + ")";
}

virtual std::variant<int, std::string, bool> eval() const = 0;
virtual size_t size() const = 0;
};

class StatementNode : public Node {
public:
virtual ~StatementNode() override = default;
virtual std::string to_string() const override = 0;
};


5 changes: 5 additions & 0 deletions include/dragon/ast/for_loop_node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "../ast.h"

class ForLoopNode : public StatementNode {};
5 changes: 5 additions & 0 deletions include/dragon/ast/function_call_node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "../ast.h"

class FunctionCallNode : public ExpressionNode {};
5 changes: 5 additions & 0 deletions include/dragon/ast/identifier_node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "../ast.h"

class IdentifierNode : public ExpressionNode {};
5 changes: 5 additions & 0 deletions include/dragon/ast/if_else_node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "../ast.h"

class IfElseNode : public StatementNode {};
5 changes: 5 additions & 0 deletions include/dragon/ast/literal_node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "../ast.h"

class LiteralNode : public ExpressionNode {};
5 changes: 5 additions & 0 deletions include/dragon/ast/variable_assignment_node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "../ast.h"

class VariableAssignmentNode : public StatementNode {};
23 changes: 23 additions & 0 deletions include/dragon/ast/variable_declaration_node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <memory>
#include "../ast.h"

class VariableDeclarationNode : public StatementNode {
public:
Token identifier;
Token type;
std::unique_ptr<ExpressionNode> value;
bool is_const;

explicit VariableDeclarationNode(Token identifier, Token type, std::unique_ptr<ExpressionNode> value, bool is_const)
: identifier(std::move(identifier)), type(std::move(type)), value(std::move(value)), is_const(is_const) {}

~VariableDeclarationNode() override = default;

std::string to_string() const override {
return "VariableDeclarationNode(identifier: " + identifier.to_string()
+ ", type: " + type.to_string()
+ ", is_const: " + (is_const ? "true" : "false") + ")";
}
};
5 changes: 5 additions & 0 deletions include/dragon/ast/while_loop_node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "../ast.h"

class WhileLoopNode : public StatementNode {};
Loading