Skip to content

Commit bea2aaf

Browse files
committed
Merge branch 'master' of https://github.com/WerWolv/PatternLanguage into new_lexer
2 parents fe7828d + a81154b commit bea2aaf

File tree

9 files changed

+100
-101
lines changed

9 files changed

+100
-101
lines changed

lib/include/pl/core/evaluator.hpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,6 @@ namespace pl::core {
160160
void popSectionId();
161161
[[nodiscard]] u64 getSectionId() const;
162162
[[nodiscard]] u64 getUserSectionId() const;
163-
[[nodiscard]] u64 createSection(const std::string &name);
164-
void removeSection(u64 id);
165-
[[nodiscard]] std::vector<u8>& getSection(u64 id);
166-
[[nodiscard]] u64 getSectionSize(u64 id);
167-
[[nodiscard]] const std::map<u64, api::Section>& getSections() const;
168-
169-
[[nodiscard]] u64 getSectionCount() const;
170163

171164
void setInVariables(const std::map<std::string, Token::Literal> &inVariables) {
172165
this->m_inVariables = inVariables;
@@ -500,8 +493,6 @@ namespace pl::core {
500493
std::atomic<bool> m_aborted;
501494

502495
std::vector<u64> m_sectionIdStack;
503-
std::map<u64, api::Section> m_sections;
504-
u64 m_sectionId = 0;
505496

506497
std::vector<std::vector<u8>> m_heap;
507498
std::map<u32, PatternLocalData> m_patternLocalStorage;

lib/include/pl/pattern_language.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ namespace pl {
257257
* @param id ID of the section
258258
* @return Memory of the section
259259
*/
260-
[[nodiscard]] const std::vector<u8>& getSection(u64 id) const;
260+
[[nodiscard]] std::vector<u8>& getSection(u64 id);
261261

262262
/**
263263
* @brief Gets all custom sections that were created
@@ -411,9 +411,21 @@ namespace pl {
411411

412412
[[nodiscard]] const std::set<pl::ptrn::Pattern*>& getPatternsWithAttribute(const std::string &attribute) const;
413413

414+
[[nodiscard]] u64 getSectionSize(u64 id);
415+
[[nodiscard]] u64 getSectionCount() const;
416+
[[nodiscard]] u64 createSection(const std::string &name);
417+
void removeSection(u64 id);
418+
414419
private:
415420
void flattenPatterns();
416421

422+
friend class core::Evaluator;
423+
424+
struct SectionData {
425+
std::map<u64, api::Section> sections;
426+
u64 nextSectionId = 0;
427+
};
428+
417429
private:
418430
Internals m_internals;
419431
std::vector<core::err::CompileError> m_compileErrors;
@@ -450,6 +462,8 @@ namespace pl {
450462
std::function<bool()> m_dangerousFunctionCallCallback;
451463
core::LogConsole::Callback m_logCallback;
452464

465+
std::shared_ptr<SectionData> m_sectionData;
466+
453467
struct Function {
454468
api::Namespace nameSpace;
455469
std::string name;

lib/source/pl/core/ast/ast_node_array_variable_decl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ namespace pl::core::ast {
199199
if ((evaluator->getReadOffset() - evaluator->getDataBaseAddress()) > (evaluator->getDataSize() + 1))
200200
err::E0004.throwError("Array expanded past end of the data before a null-entry was found.", "Try using a while-sized array instead to limit the size of the array.", this->getLocation());
201201

202-
evaluator->readData(evaluator->getReadOffset(), buffer.data(), buffer.size(), templatePattern->getSection());
203-
evaluator->getReadOffsetAndIncrement(buffer.size());
202+
templatePattern->getEvaluator()->readData(evaluator->getReadOffset(), buffer.data(), buffer.size(), templatePattern->getSection());
203+
templatePattern->getEvaluator()->getReadOffsetAndIncrement(buffer.size());
204204

205205
entryCount++;
206206

@@ -213,7 +213,7 @@ namespace pl::core::ast {
213213
}
214214

215215
if (reachedEnd) break;
216-
evaluator->handleAbort();
216+
templatePattern->getEvaluator()->handleAbort();
217217
}
218218
}
219219

@@ -394,7 +394,7 @@ namespace pl::core::ast {
394394
err::E0004.throwError("Array expanded past end of the data before a null-entry was found.", "Try using a while-sized array instead to limit the size of the array.", this->getLocation());
395395

396396
const auto patternSize = pattern->getSize();
397-
evaluator->readData(evaluator->getReadOffset() - patternSize, buffer.data(), buffer.size(), pattern->getSection());
397+
pattern->getEvaluator()->readData(evaluator->getReadOffset() - patternSize, buffer.data(), buffer.size(), pattern->getSection());
398398

399399
addEntries(hlp::moveToVector(std::move(pattern)));
400400

lib/source/pl/core/ast/ast_node_cast.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ namespace pl::core::ast {
142142
auto type = dynamic_cast<ASTNodeBuiltinType *>(evaluatedType.get())->getType();
143143

144144
value = std::visit(wolv::util::overloaded {
145-
[&evaluator, &type](const std::shared_ptr<ptrn::Pattern> &value) -> Token::Literal {
145+
[&type](const std::shared_ptr<ptrn::Pattern> &value) -> Token::Literal {
146146
if (Token::isInteger(type) && value->getSize() <= Token::getTypeSize(type)) {
147147
u128 result = 0;
148-
evaluator->readData(value->getOffset(), &result, value->getSize(), value->getSection());
148+
value->getEvaluator()->readData(value->getOffset(), &result, value->getSize(), value->getSection());
149149

150150
return result;
151151
} else {

lib/source/pl/core/ast/ast_node_mathematical_expression.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ namespace pl::core::ast {
117117
[&, this](const std::shared_ptr<ptrn::Pattern> &left, const std::shared_ptr<ptrn::Pattern> &right) -> ASTNode * {
118118
std::vector<u8> leftBytes(left->getSize()), rightBytes(right->getSize());
119119

120-
evaluator->readData(left->getOffset(), leftBytes.data(), leftBytes.size(), left->getSection());
121-
evaluator->readData(right->getOffset(), rightBytes.data(), rightBytes.size(), right->getSection());
120+
left->getEvaluator()->readData(left->getOffset(), leftBytes.data(), leftBytes.size(), left->getSection());
121+
right->getEvaluator()->readData(right->getOffset(), rightBytes.data(), rightBytes.size(), right->getSection());
122122
switch (this->getOperator()) {
123123
case Token::Operator::BoolEqual:
124124
return new ASTNodeLiteral(leftBytes == rightBytes);

lib/source/pl/core/ast/ast_node_rvalue.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ namespace pl::core::ast {
3535
}
3636
}
3737

38-
static void readVariable(Evaluator *evaluator, auto &value, ptrn::Pattern *variablePattern) {
38+
static void readVariable(auto &value, ptrn::Pattern *variablePattern) {
3939
constexpr bool isString = std::same_as<std::remove_cvref_t<decltype(value)>, std::string>;
4040

4141
if constexpr (isString) {
4242
value.resize(variablePattern->getSize());
43-
evaluator->readData(variablePattern->getOffset(), value.data(), value.size(), variablePattern->getSection());
43+
variablePattern->getEvaluator()->readData(variablePattern->getOffset(), value.data(), value.size(), variablePattern->getSection());
4444
} else {
45-
evaluator->readData(variablePattern->getOffset(), &value, variablePattern->getSize(), variablePattern->getSection());
45+
variablePattern->getEvaluator()->readData(variablePattern->getOffset(), &value, variablePattern->getSize(), variablePattern->getSection());
4646
}
4747

4848
if constexpr (!isString)
@@ -91,39 +91,39 @@ namespace pl::core::ast {
9191
Token::Literal literal;
9292
if (dynamic_cast<ptrn::PatternUnsigned *>(pattern.get()) != nullptr) {
9393
u128 value = 0;
94-
readVariable(evaluator, value, pattern.get());
94+
readVariable(value, pattern.get());
9595
literal = value;
9696
} else if (dynamic_cast<ptrn::PatternSigned *>(pattern.get()) != nullptr) {
9797
i128 value = 0;
98-
readVariable(evaluator, value, pattern.get());
98+
readVariable(value, pattern.get());
9999
value = hlp::signExtend(pattern->getSize() * 8, value);
100100
literal = value;
101101
} else if (dynamic_cast<ptrn::PatternFloat *>(pattern.get()) != nullptr) {
102102
if (pattern->getSize() == sizeof(u16)) {
103103
u16 value = 0;
104-
readVariable(evaluator, value, pattern.get());
104+
readVariable(value, pattern.get());
105105
literal = double(hlp::float16ToFloat32(value));
106106
} else if (pattern->getSize() == sizeof(float)) {
107107
float value = 0;
108-
readVariable(evaluator, value, pattern.get());
108+
readVariable(value, pattern.get());
109109
literal = double(value);
110110
} else if (pattern->getSize() == sizeof(double)) {
111111
double value = 0;
112-
readVariable(evaluator, value, pattern.get());
112+
readVariable(value, pattern.get());
113113
literal = value;
114114
} else
115115
err::E0001.throwError("Invalid floating point type.");
116116
} else if (dynamic_cast<ptrn::PatternCharacter *>(pattern.get()) != nullptr) {
117117
char value = 0;
118-
readVariable(evaluator, value, pattern.get());
118+
readVariable(value, pattern.get());
119119
literal = value;
120120
} else if (dynamic_cast<ptrn::PatternBoolean *>(pattern.get()) != nullptr) {
121121
bool value = false;
122-
readVariable(evaluator, value, pattern.get());
122+
readVariable(value, pattern.get());
123123
literal = value;
124124
} else if (dynamic_cast<ptrn::PatternString *>(pattern.get()) != nullptr) {
125125
std::string value;
126-
readVariable(evaluator, value, pattern.get());
126+
readVariable(value, pattern.get());
127127
literal = value;
128128
} else if (auto bitfieldFieldPatternBoolean = dynamic_cast<ptrn::PatternBitfieldFieldBoolean *>(pattern.get()); bitfieldFieldPatternBoolean != nullptr) {
129129
literal = bool(bitfieldFieldPatternBoolean->readValue());

lib/source/pl/core/evaluator.cpp

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ namespace pl::core {
699699
else
700700
err::E0011.throwError(fmt::format("Tried accessing out of bounds pattern local cell {}. This is a bug.", pattern->getHeapAddress()));
701701
} else {
702-
return this->getSection(pattern->getSection());
702+
return this->m_patternLanguage->getSection(pattern->getSection());
703703
}
704704
};
705705

@@ -912,20 +912,16 @@ namespace pl::core {
912912
} else if (sectionId == ptrn::Pattern::InstantiationSectionId) {
913913
err::E0012.throwError("Cannot access data of type that hasn't been placed in memory.");
914914
} else {
915-
if (this->m_sections.contains(sectionId)) {
916-
auto &section = this->m_sections[sectionId];
917-
918-
if (!write) {
919-
if ((address + size) <= section.data.size())
920-
std::memmove(buffer, section.data.data() + address, size);
921-
else
922-
std::memset(buffer, 0x00, size);
923-
} else {
924-
if ((address + size) <= section.data.size())
925-
std::memmove(section.data.data() + address, buffer, size);
926-
}
927-
} else
928-
err::E0012.throwError(fmt::format("Tried accessing a non-existing section with id {}.", sectionId));
915+
auto &section = this->getRuntime().getSection(sectionId);
916+
if (!write) {
917+
if ((address + size) <= section.size())
918+
std::memmove(buffer, section.data() + address, size);
919+
else
920+
std::memset(buffer, 0x00, size);
921+
} else {
922+
if ((address + size) <= section.size())
923+
std::memmove(section.data() + address, buffer, size);
924+
}
929925
}
930926

931927
if (this->isDebugModeEnabled()) [[unlikely]]
@@ -959,54 +955,11 @@ namespace pl::core {
959955
return 0;
960956
}
961957

962-
963-
u64 Evaluator::createSection(const std::string &name) {
964-
auto id = this->m_sectionId;
965-
this->m_sectionId++;
966-
967-
this->m_sections.insert({ id, { name, { } } });
968-
return id;
969-
}
970-
971-
void Evaluator::removeSection(u64 id) {
972-
this->m_sections.erase(id);
973-
}
974-
975-
std::vector<u8>& Evaluator::getSection(u64 id) {
976-
if (id == ptrn::Pattern::MainSectionId)
977-
err::E0011.throwError("Cannot access main section.");
978-
else if (id == ptrn::Pattern::HeapSectionId)
979-
return this->m_heap.back();
980-
else if (this->m_sections.contains(id))
981-
return this->m_sections[id].data;
982-
else if (id == ptrn::Pattern::InstantiationSectionId)
983-
err::E0012.throwError("Cannot access data of type that hasn't been placed in memory.");
984-
else
985-
err::E0011.throwError(fmt::format("Tried accessing a non-existing section with id {}.", id));
986-
}
987-
988-
u64 Evaluator::getSectionSize(u64 id) {
989-
if (id == ptrn::Pattern::MainSectionId)
990-
return this->getDataSize();
991-
else
992-
return this->getSection(id).size();
993-
}
994-
995-
const std::map<u64, api::Section> &Evaluator::getSections() const {
996-
return this->m_sections;
997-
}
998-
999-
u64 Evaluator::getSectionCount() const {
1000-
return this->m_sections.size();
1001-
}
1002-
1003958
bool Evaluator::evaluate(const std::vector<std::shared_ptr<ast::ASTNode>> &ast) {
1004959
this->m_readOrderReversed = false;
1005960
this->m_currBitOffset = 0;
1006961

1007-
this->m_sections.clear();
1008962
this->m_sectionIdStack.clear();
1009-
this->m_sectionId = 1;
1010963
this->m_outVariables.clear();
1011964
this->m_outVariableValues.clear();
1012965

lib/source/pl/lib/std/mem.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ namespace pl::lib::libstd::mem {
7676
if (section == 0xFFFF'FFFF'FFFF'FFFF)
7777
section = ctx->getUserSectionId();
7878

79-
return u128(ctx->getSectionSize(section));
79+
return u128(ctx->getRuntime().getSectionSize(section));
8080
});
8181

8282
/* find_sequence_in_range(occurrence_index, start_offset, end_offset, bytes...) */
@@ -181,14 +181,14 @@ namespace pl::lib::libstd::mem {
181181
runtime.addFunction(nsStdMem, "create_section", FunctionParameterCount::exactly(1), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
182182
auto name = params[0].toString(false);
183183

184-
return u128(ctx->createSection(name));
184+
return u128(ctx->getRuntime().createSection(name));
185185
});
186186

187187
/* delete_section(id) */
188188
runtime.addFunction(nsStdMem, "delete_section", FunctionParameterCount::exactly(1), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
189189
auto id = u64(params[0].toUnsigned());
190190

191-
ctx->removeSection(id);
191+
ctx->getRuntime().removeSection(id);
192192

193193
return std::nullopt;
194194
});
@@ -197,15 +197,15 @@ namespace pl::lib::libstd::mem {
197197
runtime.addFunction(nsStdMem, "get_section_size", FunctionParameterCount::exactly(1), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
198198
auto id = u64(params[0].toUnsigned());
199199

200-
return u128(ctx->getSection(id).size());
200+
return u128(ctx->getRuntime().getSection(id).size());
201201
});
202202

203203
/* set_section_size(id, size) */
204204
runtime.addFunction(nsStdMem, "set_section_size", FunctionParameterCount::exactly(2), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
205205
auto id = u64(params[0].toUnsigned());
206206
auto size = size_t(params[1].toUnsigned());
207207

208-
ctx->getSection(id).resize(size);
208+
ctx->getRuntime().getSection(id).resize(size);
209209

210210
return std::nullopt;
211211
});
@@ -225,7 +225,7 @@ namespace pl::lib::libstd::mem {
225225
else if (toId == ptrn::Pattern::HeapSectionId)
226226
err::E0012.throwError("Invalid section id.");
227227

228-
auto& section = ctx->getSection(toId);
228+
auto& section = ctx->getRuntime().getSection(toId);
229229
if (section.size() < toAddr + size)
230230
section.resize(toAddr + size);
231231
std::memcpy(section.data() + toAddr, data.data(), size);
@@ -243,7 +243,7 @@ namespace pl::lib::libstd::mem {
243243
else if (toId == ptrn::Pattern::HeapSectionId)
244244
err::E0012.throwError("Invalid section id.");
245245

246-
auto& section = ctx->getSection(toId);
246+
auto& section = ctx->getRuntime().getSection(toId);
247247

248248
switch (params[0].getType()) {
249249
using enum Token::ValueType;
@@ -265,11 +265,11 @@ namespace pl::lib::libstd::mem {
265265
if (auto iterable = dynamic_cast<ptrn::IIterable*>(pattern.get())) {
266266
iterable->forEachEntry(0, iterable->getEntryCount(), [&](u64, ptrn::Pattern *entry) {
267267
auto entrySize = entry->getSize();
268-
ctx->readData(entry->getOffset(), section.data() + toAddr, entrySize, entry->getSection());
268+
pattern->getEvaluator()->readData(entry->getOffset(), section.data() + toAddr, entrySize, entry->getSection());
269269
toAddr += entrySize;
270270
});
271271
} else {
272-
ctx->readData(pattern->getOffset(), section.data() + toAddr, pattern->getSize(), pattern->getSection());
272+
pattern->getEvaluator()->readData(pattern->getOffset(), section.data() + toAddr, pattern->getSize(), pattern->getSection());
273273
}
274274
break;
275275
}

0 commit comments

Comments
 (0)