added string function to all nodes of ast
This commit is contained in:
@@ -5,14 +5,17 @@
|
||||
namespace ast {
|
||||
struct node {
|
||||
virtual std::string token_literal() const = 0;
|
||||
virtual std::string str() const = 0;
|
||||
virtual ~node() = default;
|
||||
};
|
||||
|
||||
struct statement : node {
|
||||
virtual std::string token_literal() const override = 0;
|
||||
virtual std::string str() const override = 0;
|
||||
};
|
||||
|
||||
struct expression : node {
|
||||
virtual std::string token_literal() const override = 0;
|
||||
virtual std::string str() const override = 0;
|
||||
};
|
||||
} // namespace ast
|
||||
|
@@ -8,4 +8,8 @@ namespace ast {
|
||||
std::string identifier::token_literal() const {
|
||||
return token.literal;
|
||||
}
|
||||
|
||||
std::string identifier::str() const {
|
||||
return value;
|
||||
};
|
||||
} // namespace ast
|
||||
|
@@ -12,5 +12,6 @@ namespace ast {
|
||||
std::string value;
|
||||
|
||||
std::string token_literal() const override;
|
||||
std::string str() const override;
|
||||
};
|
||||
} // namespace ast
|
||||
|
@@ -1,5 +1,7 @@
|
||||
#include "program.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace ast {
|
||||
std::string program ::token_literal() const {
|
||||
if (statements.size() > 0)
|
||||
@@ -11,4 +13,12 @@ namespace ast {
|
||||
for (const auto& ref : statements)
|
||||
delete ref;
|
||||
};
|
||||
|
||||
std::string program::str() const {
|
||||
std::stringstream ss;
|
||||
for (const auto& stmt : statements)
|
||||
ss << stmt->str();
|
||||
|
||||
return ss.str();
|
||||
};
|
||||
} // namespace ast
|
||||
|
@@ -9,7 +9,8 @@ namespace ast {
|
||||
struct program : public node {
|
||||
std::vector<statement*> statements;
|
||||
std::string token_literal() const override;
|
||||
virtual std::string str() const override = 0;
|
||||
|
||||
virtual std::string str() const override;
|
||||
|
||||
~program();
|
||||
};
|
||||
|
@@ -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
|
||||
|
@@ -11,6 +11,7 @@ namespace ast {
|
||||
ast::expression* expression;
|
||||
|
||||
std::string token_literal() const override;
|
||||
std::string str() const override;
|
||||
|
||||
~expression_stmt();
|
||||
};
|
||||
|
@@ -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
|
||||
|
@@ -13,6 +13,7 @@ namespace ast {
|
||||
expression* value;
|
||||
|
||||
std::string token_literal() const override;
|
||||
std::string str() const override;
|
||||
|
||||
~let_stmt();
|
||||
};
|
||||
|
@@ -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
|
||||
|
@@ -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();
|
||||
};
|
||||
|
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "token/token.hpp"
|
||||
|
||||
#include <istream>
|
||||
|
Reference in New Issue
Block a user