Skip to content

Commit 0bd2ab2

Browse files
committed
Retain custom build types as they are global and have lifetimes beyond a single evaluation.
1 parent b2c957a commit 0bd2ab2

File tree

7 files changed

+69
-13
lines changed

7 files changed

+69
-13
lines changed

lib/include/pl/core/parser_manager.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ namespace pl::core {
3838
this->m_onceIncluded.clear();
3939
for(const auto &[_, types] : this->m_parsedTypes) {
4040
for(const auto &[_, type] : types) {
41-
type->setType(nullptr);
41+
if(type != nullptr && type->isValid()) {
42+
if(auto builtinType = dynamic_cast<ast::ASTNodeBuiltinType*>(type->getType().get()); builtinType != nullptr) {
43+
if(builtinType->getType() != Token::ValueType::CustomType) {
44+
type->setType(nullptr);
45+
}
46+
}
47+
} else {
48+
type->setType(nullptr);
49+
}
4250
}
4351
}
4452
this->m_parsedTypes.clear();

lib/source/pl/core/parser.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2814,8 +2814,14 @@ namespace pl::core {
28142814

28152815
void Parser::reset() {
28162816
for(const auto &[_, type] : this->m_types) {
2817-
if(type != nullptr) {
2818-
type->setType(nullptr);
2817+
if(type != nullptr && type->isValid()) {
2818+
if(auto builtinType = dynamic_cast<ast::ASTNodeBuiltinType*>(type->getType().get()); builtinType != nullptr) {
2819+
if(builtinType->getType() != Token::ValueType::CustomType) {
2820+
type->setType(nullptr);
2821+
}
2822+
} else {
2823+
type->setType(nullptr);
2824+
}
28192825
}
28202826
}
28212827
this->m_types.clear();

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ set(AVAILABLE_TESTS
3434
RValuesAssignmentInStruct
3535
TemplateParametersScope
3636
TypeNameOf
37+
CustomBuiltInType
3738
)
3839

3940

tests/include/test_patterns/test_pattern.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ namespace pl::test {
5959
return true;
6060
}
6161

62+
[[nodiscard]] virtual size_t repeatTimes() const {
63+
return 1;
64+
}
65+
6266
private:
6367
std::vector<std::shared_ptr<ptrn::Pattern>> m_patterns;
6468
core::Evaluator *m_evaluator;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include "test_pattern.hpp"
4+
5+
namespace pl::test {
6+
7+
class TestPatternCustomBuiltinType : public TestPattern {
8+
public:
9+
TestPatternCustomBuiltinType(core::Evaluator *evaluator) : TestPattern(evaluator, "CustomBuiltInType") {
10+
}
11+
~TestPatternCustomBuiltinType() override = default;
12+
13+
virtual void setup() override{
14+
const pl::api::Namespace ns = { "custom_type" };
15+
m_runtime->addType(ns, "custom_type", pl::api::FunctionParameterCount::exactly(1), [](core::Evaluator *evaluator, auto params) -> std::unique_ptr<pl::ptrn::Pattern> {
16+
auto pattern = std::make_unique<pl::ptrn::PatternUnsigned>(evaluator, 0, sizeof(u8), 0);
17+
return pattern;
18+
});
19+
};
20+
21+
[[nodiscard]] std::string getSourceCode() const override {
22+
return R"(
23+
custom_type::custom_type<1> test @ 0;
24+
std::assert(test == 137, "test should be 137");
25+
)";
26+
}
27+
28+
[[nodiscard]] size_t repeatTimes() const override {
29+
return 2;
30+
}
31+
};
32+
}

tests/source/main.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,23 @@ int runTests(int argc, char **argv) {
108108
auto &test = testPatterns[testName];
109109
test->m_runtime = &runtime;
110110
test->setup();
111-
auto result = runtime.executeString(test->getSourceCode());
112111

113-
// Check if compilation succeeded
114-
if (!result) {
115-
fmt::print("Error during test!\n");
112+
for(size_t i = 0; i < test->repeatTimes(); i++) {
113+
auto result = runtime.executeString(test->getSourceCode());
116114

117-
if (auto error = runtime.getEvalError(); error.has_value())
118-
fmt::print("Error: {}:{} : {}\n", error->line, error->column, error->message);
115+
// Check if compilation succeeded
116+
if (!result) {
117+
fmt::print("Error during test!\n");
119118

120-
for (const auto& error : runtime.getCompileErrors()) {
121-
fmt::print("{}", error.format());
122-
}
119+
if (auto error = runtime.getEvalError(); error.has_value())
120+
fmt::print("Error: {}:{} : {}\n", error->line, error->column, error->message);
123121

124-
return failing ? EXIT_SUCCESS : EXIT_FAILURE;
122+
for (const auto& error : runtime.getCompileErrors()) {
123+
fmt::print("{}", error.format());
124+
}
125+
126+
return failing ? EXIT_SUCCESS : EXIT_FAILURE;
127+
}
125128
}
126129

127130
const auto &evaluatedPatterns = runtime.getPatterns();

tests/source/tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "test_patterns/test_pattern_rvalues_assignment_in_struct.hpp"
3030
#include "test_patterns/test_pattern_template_parameters_scope.hpp"
3131
#include "test_patterns/test_pattern_typenameof.hpp"
32+
#include "test_patterns/test_pattern_custom_builtin_type.hpp"
3233

3334
static pl::core::Evaluator s_evaluator;
3435

@@ -63,4 +64,5 @@ std::array Tests = {
6364
TEST(RValuesAssignmentInStruct),
6465
TEST(TemplateParametersScope),
6566
TEST(TypeNameOf),
67+
TEST(CustomBuiltinType),
6668
};

0 commit comments

Comments
 (0)