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

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

View File

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

View File

@@ -1,5 +1,7 @@
#include "let.hpp"
#include <sstream>
namespace ast {
let_stmt::let_stmt(token::token token)
: token(std::move(token)),
@@ -14,4 +16,14 @@ namespace ast {
delete name;
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

View File

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

View File

@@ -1,5 +1,7 @@
#include "return.hpp"
#include <sstream>
namespace ast {
return_stmt::return_stmt(token::token token)
: token(std::move(token)),
@@ -12,4 +14,14 @@ namespace ast {
return_stmt::~return_stmt() {
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

View File

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