very basic parser of let statements

This commit is contained in:
Karma Riuk
2025-07-03 13:30:56 +02:00
parent c091f7f021
commit de465b6122
9 changed files with 311 additions and 0 deletions

9
src/ast/ast.cpp Normal file
View File

@@ -0,0 +1,9 @@
#include "ast.hpp"
namespace ast {
std::string program ::token_literal() const {
if (statements.size() > 0)
return statements[0]->token_literal();
return "";
}
} // namespace ast

29
src/ast/ast.hpp Normal file
View File

@@ -0,0 +1,29 @@
#pragma once
#include <string>
#include <vector>
namespace ast {
struct node {
virtual std::string token_literal() const = 0;
virtual ~node() = default;
};
struct statement : node {
virtual std::string token_literal() const override = 0;
};
struct expression : node {
virtual std::string token_literal() const override = 0;
};
struct program : public node {
std::vector<statement*> statements;
std::string token_literal() const override;
~program() {
for (const auto& ref : statements)
delete ref;
};
};
} // namespace ast

View File

@@ -0,0 +1,11 @@
#include "identifier.hpp"
namespace ast {
identifier::identifier(token::token token, std::string value)
: token(std::move(token)),
value(std::move(value)) {}
std::string identifier::token_literal() const {
return token.literal;
}
} // namespace ast

View File

@@ -0,0 +1,16 @@
#pragma once
#include "ast/ast.hpp"
#include "token/token.hpp"
#include <string>
namespace ast {
struct identifier : expression {
identifier(token::token token, std::string value);
token::token token;
std::string value;
std::string token_literal() const override;
};
} // namespace ast

View File

@@ -0,0 +1,17 @@
#include "let.hpp"
namespace ast {
let::let(token::token token)
: token(std::move(token)),
name(nullptr),
value(nullptr) {}
std::string let::token_literal() const {
return token.literal;
}
let::~let() {
delete name;
delete value;
};
} // namespace ast

View File

@@ -0,0 +1,19 @@
#pragma once
#include "ast/ast.hpp"
#include "ast/expressions/identifier.hpp"
#include "token/token.hpp"
namespace ast {
struct let : statement {
let(token::token token);
token::token token;
identifier* name;
expression* value;
std::string token_literal() const override;
~let();
};
} // namespace ast