commit 2d54b5b80877040cf62707c3eda5ead300291ada
parent 3ec3ea75d12a06c41b0115e26d3303edf803a032
Author: Mohammad-Reza Nabipoor <m.nabipoor@yahoo.com>
Date: Sun, 16 Aug 2020 08:44:43 +0430
Add default ctor to ASTNode; Update tests
Now `ASTNode` is default constructible; and it acts like a default
constructible pointer: you cannot use it unless assign a valid
value to it.
Diffstat:
2 files changed, 46 insertions(+), 1 deletion(-)
diff --git a/kaleidoscope_ast.hpp b/kaleidoscope_ast.hpp
@@ -3,6 +3,7 @@
#include "kaleidoscope_codegen.hpp"
+#include <cassert>
#include <memory>
#include <string>
#include <vector>
@@ -35,6 +36,9 @@ to_string(const Function&);
class ASTNode
{
public:
+ ASTNode() = default;
+ ~ASTNode() = default;
+
template<typename T>
ASTNode(T value)
: self_{ std::make_shared<Model<T>>(std::move(value)) }
@@ -42,13 +46,22 @@ public:
friend std::string to_string(const ASTNode& t)
{
+ assert(t.self_ != nullptr);
+
return t.self_->to_string();
}
- friend llvm::Value* codegen(const ASTNode& t) { return t.self_->codegen(); }
+ friend llvm::Value* codegen(const ASTNode& t)
+ {
+ assert(t.self_ != nullptr);
+
+ return t.self_->codegen();
+ }
friend bool operator==(const ASTNode& x, const ASTNode& y)
{
+ assert(x.self_ != nullptr && y.self_ != nullptr);
+
return x.self_->to_string() == y.self_->to_string(); // FIXME
}
diff --git a/tests/kaleidoscope_ast.test.cpp b/tests/kaleidoscope_ast.test.cpp
@@ -29,17 +29,27 @@ TEST_CASE("to_string", "[str]")
REQUIRE(kal::to_string(n[2]) == "(number -1)");
REQUIRE(kal::to_string(n[3]) == "(number 3.14159)"); // FIXME str trunc
REQUIRE(kal::to_string(n[4]) == "(number -3.14159)"); // FIXME str trunc
+
+ kal::Number nDefault;
+
+ (void)nDefault;
}
SECTION("Variable")
{
+ kal::Variable vDefault;
kal::Variable v{ "var" };
+ (void)vDefault;
REQUIRE(v.name == "var");
}
SECTION("BinaryOp")
{
+ kal::BinaryOp opDefault;
+
+ (void)opDefault;
+
kal::Number n0{ 9.8 };
kal::Number n1{ -2.72 };
kal::BinaryOp op01{ '+', n0, n1 };
@@ -70,12 +80,19 @@ TEST_CASE("to_string", "[str]")
REQUIRE(kal::to_string(c0) == "(call pi)");
// FIXME stod truncation
REQUIRE(kal::to_string(c1) == "(call tan2 (number 0.523333) (variable y))");
+
+ kal::Call cDefault;
+
+ (void)cDefault;
}
SECTION("Prototype")
{
+ kal::Prototype pDefault;
kal::Prototype p{ "memcmp", { "dest", "src", "n" } };
+ (void)pDefault;
+
REQUIRE(p.name == "memcmp");
REQUIRE(p.params.size() == 3);
REQUIRE(p.params.at(0) == "dest");
@@ -85,6 +102,7 @@ TEST_CASE("to_string", "[str]")
SECTION("Function")
{
+ kal::Function fDefault;
kal::Function fun{
kal::Prototype{ "fun", { "x", "y", "z" } },
// (- (* b b) (* (* 4 a) c))
@@ -98,9 +116,23 @@ TEST_CASE("to_string", "[str]")
} }
};
+ (void)fDefault;
+
REQUIRE(kal::to_string(fun) ==
"(function (prototype fun x y z) (binop '-' (binop '*' (variable "
"b) (variable b)) (binop '*' (binop '*' (number 4) (variable a)) "
"(variable c))))");
}
+
+ SECTION("ASTNode")
+ {
+ kal::ASTNode nDefault;
+ kal::ASTNode n{ kal::Number{ 10 } };
+
+ (void)nDefault;
+
+ using kal::to_string;
+
+ REQUIRE(to_string(n) == "(number 10)");
+ }
}