added tests for the stringification of the program
This commit is contained in:
@@ -8,6 +8,12 @@
|
|||||||
namespace ast {
|
namespace ast {
|
||||||
struct program : public node {
|
struct program : public node {
|
||||||
std::vector<statement*> statements;
|
std::vector<statement*> statements;
|
||||||
|
|
||||||
|
program() {}
|
||||||
|
|
||||||
|
program(std::vector<statement*> statements)
|
||||||
|
: statements(std::move(statements)) {};
|
||||||
|
|
||||||
std::string token_literal() const override;
|
std::string token_literal() const override;
|
||||||
|
|
||||||
virtual std::string str() const override;
|
virtual std::string str() const override;
|
||||||
|
@@ -22,6 +22,8 @@ namespace ast {
|
|||||||
if (expression != nullptr)
|
if (expression != nullptr)
|
||||||
ss << expression->str();
|
ss << expression->str();
|
||||||
|
|
||||||
|
ss << ';';
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
};
|
};
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
|
@@ -8,6 +8,11 @@ namespace ast {
|
|||||||
name(nullptr),
|
name(nullptr),
|
||||||
value(nullptr) {}
|
value(nullptr) {}
|
||||||
|
|
||||||
|
let_stmt::let_stmt(token::token token, identifier* name, expression* value)
|
||||||
|
: token(std::move(token)),
|
||||||
|
name(name),
|
||||||
|
value(value) {}
|
||||||
|
|
||||||
std::string let_stmt::token_literal() const {
|
std::string let_stmt::token_literal() const {
|
||||||
return token.literal;
|
return token.literal;
|
||||||
}
|
}
|
||||||
@@ -24,6 +29,8 @@ namespace ast {
|
|||||||
if (value != nullptr)
|
if (value != nullptr)
|
||||||
ss << value->str();
|
ss << value->str();
|
||||||
|
|
||||||
|
ss << ';';
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
};
|
};
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
|
@@ -7,6 +7,7 @@
|
|||||||
namespace ast {
|
namespace ast {
|
||||||
struct let_stmt : statement {
|
struct let_stmt : statement {
|
||||||
let_stmt(token::token token);
|
let_stmt(token::token token);
|
||||||
|
let_stmt(token::token token, identifier* name, expression* value);
|
||||||
|
|
||||||
token::token token;
|
token::token token;
|
||||||
identifier* name;
|
identifier* name;
|
||||||
|
@@ -7,6 +7,10 @@ namespace ast {
|
|||||||
: token(std::move(token)),
|
: token(std::move(token)),
|
||||||
value(nullptr) {}
|
value(nullptr) {}
|
||||||
|
|
||||||
|
return_stmt::return_stmt(token::token token, expression* value)
|
||||||
|
: token(std::move(token)),
|
||||||
|
value(value) {}
|
||||||
|
|
||||||
std::string return_stmt::token_literal() const {
|
std::string return_stmt::token_literal() const {
|
||||||
return token.literal;
|
return token.literal;
|
||||||
}
|
}
|
||||||
@@ -22,6 +26,8 @@ namespace ast {
|
|||||||
if (value != nullptr)
|
if (value != nullptr)
|
||||||
ss << value->str();
|
ss << value->str();
|
||||||
|
|
||||||
|
ss << ';';
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
};
|
};
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
|
@@ -6,6 +6,7 @@
|
|||||||
namespace ast {
|
namespace ast {
|
||||||
struct return_stmt : statement {
|
struct return_stmt : statement {
|
||||||
return_stmt(token::token token);
|
return_stmt(token::token token);
|
||||||
|
return_stmt(token::token token, expression* value);
|
||||||
|
|
||||||
token::token token;
|
token::token token;
|
||||||
expression* value;
|
expression* value;
|
||||||
|
38
test/program.cpp
Normal file
38
test/program.cpp
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#include "ast/program.hpp"
|
||||||
|
|
||||||
|
#include "ast/ast.hpp"
|
||||||
|
#include "ast/statements/let.hpp"
|
||||||
|
#include "ast/statements/return.hpp"
|
||||||
|
#include "token/type.hpp"
|
||||||
|
|
||||||
|
#include <doctest.h>
|
||||||
|
|
||||||
|
TEST_SUITE("Program") {
|
||||||
|
TEST_CASE("Let string") {
|
||||||
|
ast::program program({new ast::let_stmt(
|
||||||
|
token::token(token::type::LET, "let"),
|
||||||
|
new ast::identifier(
|
||||||
|
token::token(token::type::IDENTIFIER, "myVar"),
|
||||||
|
"myVar"
|
||||||
|
),
|
||||||
|
new ast::identifier(
|
||||||
|
token::token(token::type::IDENTIFIER, "anotherVar"),
|
||||||
|
"anotherVar"
|
||||||
|
)
|
||||||
|
)});
|
||||||
|
|
||||||
|
CHECK(program.str() == "let myVar = anotherVar;");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Return string") {
|
||||||
|
ast::program program({new ast::return_stmt(
|
||||||
|
token::token(token::type::RETURN, "return"),
|
||||||
|
new ast::identifier(
|
||||||
|
token::token(token::type::IDENTIFIER, "myVar"),
|
||||||
|
"myVar"
|
||||||
|
)
|
||||||
|
)});
|
||||||
|
|
||||||
|
CHECK(program.str() == "return myVar;");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user