Skip to content

Commit 8fadba1

Browse files
committed
Add CompilerConstant class
1 parent ddced86 commit 8fadba1

File tree

8 files changed

+109
-0
lines changed

8 files changed

+109
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ if (LIBSCRATCHCPP_USE_LLVM)
7373
PUBLIC
7474
include/scratchcpp/dev/compiler.h
7575
include/scratchcpp/dev/compilervalue.h
76+
include/scratchcpp/dev/compilerconstant.h
7677
include/scratchcpp/dev/executablecode.h
7778
include/scratchcpp/dev/executioncontext.h
7879
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "compilervalue.h"
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class CompilerConstantPrivate;
11+
12+
/*! \brief The CompilerConstant class represents a constant value in compiled code. */
13+
class LIBSCRATCHCPP_EXPORT CompilerConstant : public CompilerValue
14+
{
15+
public:
16+
CompilerConstant(Compiler::StaticType type, const Value &value);
17+
CompilerConstant(const CompilerConstant &) = delete;
18+
19+
bool isConst() const override final { return true; };
20+
21+
const Value &value() const;
22+
23+
private:
24+
spimpl::unique_impl_ptr<CompilerConstantPrivate> impl;
25+
};
26+
27+
} // namespace libscratchcpp

src/dev/engine/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ target_sources(scratchcpp
66
compilervalue.cpp
77
compilervalue_p.cpp
88
compilervalue_p.h
9+
compilerconstant.cpp
10+
compilerconstant_p.cpp
11+
compilerconstant_p.h
912
executioncontext.cpp
1013
executioncontext_p.cpp
1114
executioncontext_p.h

src/dev/engine/compilerconstant.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include <scratchcpp/dev/compilerconstant.h>
4+
5+
#include "compilerconstant_p.h"
6+
7+
using namespace libscratchcpp;
8+
9+
/*! Constructs CompilerConstant. */
10+
CompilerConstant::CompilerConstant(Compiler::StaticType type, const Value &value) :
11+
CompilerValue(type),
12+
impl(spimpl::make_unique_impl<CompilerConstantPrivate>(value))
13+
{
14+
}
15+
16+
/*! Returns the constant value. */
17+
const Value &CompilerConstant::value() const
18+
{
19+
return impl->value;
20+
}

src/dev/engine/compilerconstant_p.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "compilerconstant_p.h"
2+
3+
using namespace libscratchcpp;
4+
5+
CompilerConstantPrivate::CompilerConstantPrivate(const Value &value) :
6+
value(value)
7+
{
8+
}

src/dev/engine/compilerconstant_p.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <scratchcpp/value.h>
6+
7+
namespace libscratchcpp
8+
{
9+
10+
struct CompilerConstantPrivate
11+
{
12+
CompilerConstantPrivate(const Value &value);
13+
14+
Value value;
15+
};
16+
17+
} // namespace libscratchcpp

test/dev/compiler/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ add_executable(
22
compiler_test
33
compiler_test.cpp
44
compilervalue_test.cpp
5+
compilerconstant_test.cpp
56
)
67

78
target_link_libraries(
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <scratchcpp/dev/compilerconstant.h>
2+
#include <scratchcpp/value.h>
3+
#include <gtest/gtest.h>
4+
5+
using namespace libscratchcpp;
6+
7+
TEST(CompilerConstantTest, Constructors)
8+
{
9+
{
10+
CompilerConstant v(Compiler::StaticType::Number, 5);
11+
ASSERT_EQ(v.type(), Compiler::StaticType::Number);
12+
ASSERT_EQ(v.value(), 5);
13+
}
14+
15+
{
16+
CompilerConstant v(Compiler::StaticType::String, "test");
17+
ASSERT_EQ(v.type(), Compiler::StaticType::String);
18+
ASSERT_EQ(v.value(), "test");
19+
}
20+
21+
{
22+
CompilerConstant v(Compiler::StaticType::Unknown, true);
23+
ASSERT_EQ(v.type(), Compiler::StaticType::Unknown);
24+
ASSERT_EQ(v.value(), true);
25+
}
26+
}
27+
28+
TEST(CompilerConstantTest, IsConst)
29+
{
30+
CompilerConstant v(Compiler::StaticType::Unknown, Value());
31+
ASSERT_TRUE(v.isConst());
32+
}

0 commit comments

Comments
 (0)