Skip to content

Commit 70819e2

Browse files
committed
Add CompilerValue class
1 parent 2219f8b commit 70819e2

File tree

7 files changed

+125
-0
lines changed

7 files changed

+125
-0
lines changed

CMakeLists.txt

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

src/dev/engine/compilervalue.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include <scratchcpp/dev/compilervalue.h>
4+
5+
#include "compilervalue_p.h"
6+
7+
using namespace libscratchcpp;
8+
9+
/*! Constructs CompilerValue. */
10+
CompilerValue::CompilerValue(Compiler::StaticType type) :
11+
impl(spimpl::make_unique_impl<CompilerValuePrivate>(type))
12+
{
13+
}
14+
15+
/*! Returns the type of this value. */
16+
Compiler::StaticType CompilerValue::type() const
17+
{
18+
return impl->type;
19+
}
20+
21+
/*! Sets the type of this value. */
22+
void CompilerValue::setType(Compiler::StaticType type)
23+
{
24+
impl->type = type;
25+
}

src/dev/engine/compilervalue_p.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "compilervalue_p.h"
4+
5+
using namespace libscratchcpp;
6+
7+
CompilerValuePrivate::CompilerValuePrivate(Compiler::StaticType type) :
8+
type(type)
9+
{
10+
}

src/dev/engine/compilervalue_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/dev/compiler.h>
6+
7+
namespace libscratchcpp
8+
{
9+
10+
struct CompilerValuePrivate
11+
{
12+
CompilerValuePrivate(Compiler::StaticType type);
13+
14+
Compiler::StaticType type = Compiler::StaticType::Unknown;
15+
};
16+
17+
} // namespace libscratchcpp

test/dev/compiler/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_executable(
22
compiler_test
33
compiler_test.cpp
4+
compilervalue_test.cpp
45
)
56

67
target_link_libraries(
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <scratchcpp/dev/compilervalue.h>
2+
#include <gtest/gtest.h>
3+
4+
using namespace libscratchcpp;
5+
6+
TEST(CompilerValueTest, Constructors)
7+
{
8+
{
9+
CompilerValue v(Compiler::StaticType::Number);
10+
ASSERT_EQ(v.type(), Compiler::StaticType::Number);
11+
}
12+
13+
{
14+
CompilerValue v(Compiler::StaticType::String);
15+
ASSERT_EQ(v.type(), Compiler::StaticType::String);
16+
}
17+
18+
{
19+
CompilerValue v(Compiler::StaticType::Unknown);
20+
ASSERT_EQ(v.type(), Compiler::StaticType::Unknown);
21+
}
22+
}
23+
24+
TEST(CompilerValueTest, Type)
25+
{
26+
CompilerValue v(Compiler::StaticType::Unknown);
27+
28+
v.setType(Compiler::StaticType::String);
29+
ASSERT_EQ(v.type(), Compiler::StaticType::String);
30+
31+
v.setType(Compiler::StaticType::Bool);
32+
ASSERT_EQ(v.type(), Compiler::StaticType::Bool);
33+
34+
v.setType(Compiler::StaticType::Unknown);
35+
ASSERT_EQ(v.type(), Compiler::StaticType::Unknown);
36+
}
37+
38+
TEST(CompilerValueTest, IsConst)
39+
{
40+
CompilerValue v(Compiler::StaticType::Unknown);
41+
ASSERT_FALSE(v.isConst());
42+
}

0 commit comments

Comments
 (0)