added string function to all nodes of ast

This commit is contained in:
Karma Riuk
2025-07-08 10:17:38 +02:00
parent da2b6716b1
commit 31cb483602
12 changed files with 60 additions and 1 deletions

View File

@@ -5,14 +5,17 @@
namespace ast { namespace ast {
struct node { struct node {
virtual std::string token_literal() const = 0; virtual std::string token_literal() const = 0;
virtual std::string str() const = 0;
virtual ~node() = default; virtual ~node() = default;
}; };
struct statement : node { struct statement : node {
virtual std::string token_literal() const override = 0; virtual std::string token_literal() const override = 0;
virtual std::string str() const override = 0;
}; };
struct expression : node { struct expression : node {
virtual std::string token_literal() const override = 0; virtual std::string token_literal() const override = 0;
virtual std::string str() const override = 0;
}; };
} // namespace ast } // namespace ast

View File

@@ -8,4 +8,8 @@ namespace ast {
std::string identifier::token_literal() const { std::string identifier::token_literal() const {
return token.literal; return token.literal;
} }
std::string identifier::str() const {
return value;
};
} // namespace ast } // namespace ast

View File

@@ -12,5 +12,6 @@ namespace ast {
std::string value; std::string value;
std::string token_literal() const override; std::string token_literal() const override;
std::string str() const override;
}; };
} // namespace ast } // namespace ast

View File

@@ -1,5 +1,7 @@
#include "program.hpp" #include "program.hpp"
#include <sstream>
namespace ast { namespace ast {
std::string program ::token_literal() const { std::string program ::token_literal() const {
if (statements.size() > 0) if (statements.size() > 0)
@@ -11,4 +13,12 @@ namespace ast {
for (const auto& ref : statements) for (const auto& ref : statements)
delete ref; delete ref;
}; };
std::string program::str() const {
std::stringstream ss;
for (const auto& stmt : statements)
ss << stmt->str();
return ss.str();
};
} // namespace ast } // namespace ast

View File

@@ -9,7 +9,8 @@ namespace ast {
struct program : public node { struct program : public node {
std::vector<statement*> statements; std::vector<statement*> statements;
std::string token_literal() const override; std::string token_literal() const override;
virtual std::string str() const override = 0;
virtual std::string str() const override;
~program(); ~program();
}; };

View File

@@ -1,5 +1,7 @@
#include "expression.hpp" #include "expression.hpp"
#include <sstream>
namespace ast { namespace ast {
expression_stmt::expression_stmt(token::token token) expression_stmt::expression_stmt(token::token token)
@@ -13,4 +15,13 @@ namespace ast {
expression_stmt::~expression_stmt() { expression_stmt::~expression_stmt() {
delete expression; delete expression;
} }
std::string expression_stmt::str() const {
std::stringstream ss;
if (expression != nullptr)
ss << expression->str();
return ss.str();
};
} // namespace ast } // namespace ast

View File

@@ -11,6 +11,7 @@ namespace ast {
ast::expression* expression; ast::expression* expression;
std::string token_literal() const override; std::string token_literal() const override;
std::string str() const override;
~expression_stmt(); ~expression_stmt();
}; };

View File

@@ -1,5 +1,7 @@
#include "let.hpp" #include "let.hpp"
#include <sstream>
namespace ast { namespace ast {
let_stmt::let_stmt(token::token token) let_stmt::let_stmt(token::token token)
: token(std::move(token)), : token(std::move(token)),
@@ -14,4 +16,14 @@ namespace ast {
delete name; delete name;
delete value; delete value;
}; };
std::string let_stmt::str() const {
std::stringstream ss;
ss << token_literal() << ' ' << name->str() << " = ";
if (value != nullptr)
ss << value->str();
return ss.str();
};
} // namespace ast } // namespace ast

View File

@@ -13,6 +13,7 @@ namespace ast {
expression* value; expression* value;
std::string token_literal() const override; std::string token_literal() const override;
std::string str() const override;
~let_stmt(); ~let_stmt();
}; };

View File

@@ -1,5 +1,7 @@
#include "return.hpp" #include "return.hpp"
#include <sstream>
namespace ast { namespace ast {
return_stmt::return_stmt(token::token token) return_stmt::return_stmt(token::token token)
: token(std::move(token)), : token(std::move(token)),
@@ -12,4 +14,14 @@ namespace ast {
return_stmt::~return_stmt() { return_stmt::~return_stmt() {
delete value; delete value;
}; };
std::string return_stmt::str() const {
std::stringstream ss;
ss << token_literal() << " ";
if (value != nullptr)
ss << value->str();
return ss.str();
};
} // namespace ast } // namespace ast

View File

@@ -1,3 +1,4 @@
#pragma once
#include "ast/ast.hpp" #include "ast/ast.hpp"
#include "token/token.hpp" #include "token/token.hpp"
@@ -10,6 +11,7 @@ namespace ast {
expression* value; expression* value;
std::string token_literal() const override; std::string token_literal() const override;
std::string str() const override;
~return_stmt(); ~return_stmt();
}; };

View File

@@ -1,4 +1,5 @@
#pragma once #pragma once
#include "token/token.hpp" #include "token/token.hpp"
#include <istream> #include <istream>