Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
51a6d74
Add code from dev branch
shewitt-au Jul 10, 2025
f9e9304
Fix auto code-formatter disaster
shewitt-au Jul 11, 2025
95e0ccf
Fix auto code-formatter disaster
shewitt-au Jul 11, 2025
f52b404
Fix auto code-formatter disaster
shewitt-au Jul 11, 2025
4ecaf94
Fix auto code-formatter disaster
shewitt-au Jul 11, 2025
09bdf7a
Fix auto code-formatter disaster
shewitt-au Jul 11, 2025
35a70d9
Fix build error.
shewitt-au Jul 11, 2025
8c37d1d
Make whole longest line visible
shewitt-au Jul 11, 2025
dd10d49
Fix grammar in comment
shewitt-au Jul 11, 2025
c021818
Remove unwanted formatting changes
shewitt-au Jul 12, 2025
155dcbe
Remove unwanted formatting changes
shewitt-au Jul 12, 2025
0c71626
Merge branch 'accurate-column' of https://github.com/shewitt-au/Steve…
shewitt-au Jul 12, 2025
b1872f6
Remove unwanted formatting changes
shewitt-au Jul 12, 2025
b4fab84
Remove unwanted formatting changes
shewitt-au Jul 12, 2025
6dc69fa
Fixed potential buffer overrun
shewitt-au Jul 12, 2025
fefa424
Fixed bug while investigating feedback from @paxcut
shewitt-au Jul 12, 2025
c4373e7
Longest line length
shewitt-au Jul 13, 2025
2b95a3b
Comment
shewitt-au Jul 14, 2025
e97d5d8
Kind of works. Arrarys loc is off
shewitt-au Jul 16, 2025
7994201
Arrays seem to work now
shewitt-au Jul 17, 2025
59008b9
Clean up a a little
shewitt-au Jul 18, 2025
eb224fc
Work on unit tests
shewitt-au Jul 18, 2025
4a7f1c7
Remove a 'this->next()'
shewitt-au Jul 19, 2025
fa7ff0b
Various fixes
shewitt-au Jul 19, 2025
278a49a
Various fixes
shewitt-au Jul 19, 2025
3ceb74b
Initial
shewitt-au Jul 20, 2025
9277174
Merge branch 'master' into goto-in-pattern-editor
shewitt-au Jul 20, 2025
5e022d7
Oops
shewitt-au Jul 20, 2025
80ee28b
Strange stuff
shewitt-au Jul 20, 2025
3fd3748
Reluctantly remove lexer mods
shewitt-au Jul 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/include/pl/core/evaluator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ namespace pl::core {
void createParameterPack(const std::string &name, const std::vector<Token::Literal> &values);

void createArrayVariable(const std::string &name, const ast::ASTNode *type, size_t entryCount, u64 section, bool constant = false);
std::shared_ptr<ptrn::Pattern> createVariable(const std::string &name, const ast::ASTNodeTypeDecl *type, const std::optional<Token::Literal> &value = std::nullopt, bool outVariable = false, bool reference = false, bool templateVariable = false, bool constant = false);
std::shared_ptr<ptrn::Pattern> createVariable(const std::string &name, const pl::core::Location &loc, const ast::ASTNodeTypeDecl *type, const std::optional<Token::Literal> &value = std::nullopt, bool outVariable = false, bool reference = false, bool templateVariable = false, bool constant = false);
std::shared_ptr<ptrn::Pattern>& getVariableByName(const std::string &name);
void setVariable(const std::string &name, const Token::Literal &value);
void setVariable(std::shared_ptr<ptrn::Pattern> &pattern, const Token::Literal &value);
Expand Down
8 changes: 7 additions & 1 deletion lib/include/pl/core/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ namespace pl::core {
return temp;
}

template<typename T, typename...Ts>
hlp::safe_unique_ptr<T> createWithLocation(const Location &loc, Ts&&... ts) {
auto temp = std::make_unique<T>(std::forward<Ts>(ts)...);
temp->setLocation(loc);
return temp;
}

template<typename T, typename...Ts>
hlp::safe_shared_ptr<T> createShared(Ts&&... ts) {
auto temp = std::make_shared<T>(std::forward<Ts>(ts)...);
Expand Down Expand Up @@ -287,7 +294,6 @@ namespace pl::core {
if (!peek(token))
return true;

this->next();
partReset();
return false;
} else
Expand Down
14 changes: 11 additions & 3 deletions lib/include/pl/patterns/pattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <pl/core/errors/error.hpp>
#include <pl/core/evaluator.hpp>
#include <pl/core/location.hpp>
#include <pl/pattern_visitor.hpp>
#include <pl/helpers/types.hpp>
#include <pl/helpers/utils.hpp>
Expand Down Expand Up @@ -69,8 +70,8 @@ namespace pl::ptrn {
constexpr static u64 InstantiationSectionId = 0xFFFF'FFFF'FFFF'FFFD;

Pattern(core::Evaluator *evaluator, u64 offset, size_t size, u32 line)
: m_evaluator(evaluator), m_line(line), m_offset(offset), m_size(size) {

: m_evaluator(evaluator), m_line(line), m_variableLocation(pl::core::Location::Empty()),
m_offset(offset), m_size(size) {
if (evaluator != nullptr) {
this->m_color = evaluator->getNextPatternColor();
this->m_manualColor = false;
Expand All @@ -93,6 +94,7 @@ namespace pl::ptrn {
this->m_initialized = other.m_initialized;
this->m_constant = other.m_constant;
this->m_variableName = other.m_variableName;
this->m_variableLocation = other.m_variableLocation;
this->m_typeName = other.m_typeName;
this->m_reference = other.m_reference;
this->m_parent = other.m_parent;
Expand Down Expand Up @@ -154,14 +156,19 @@ namespace pl::ptrn {
return *this->m_variableName;
}

pl::core::Location getVariableLocation() const {
return m_variableLocation;
}

[[nodiscard]] bool hasVariableName() const {
return getEvaluator()->isStringPoolEntryValid(this->m_variableName);
}

void setVariableName(const std::string &name) {
void setVariableName(const std::string &name, pl::core::Location loc = pl::core::Location()) {
if (!name.empty()) {
auto [it, inserted] = m_evaluator->getStringPool().emplace(name);
this->m_variableName = it;
this->m_variableLocation = loc;
}
}

Expand Down Expand Up @@ -633,6 +640,7 @@ namespace pl::ptrn {
u32 m_line = 0;

std::set<std::string>::const_iterator m_variableName;
pl::core::Location m_variableLocation;
std::set<std::string>::const_iterator m_typeName;
std::optional<u64> m_arrayIndex;

Expand Down
2 changes: 1 addition & 1 deletion lib/include/pl/patterns/pattern_array_static.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace pl::ptrn {

std::shared_ptr<Pattern> highlightTemplate = this->m_template->clone();

highlightTemplate->setVariableName(this->getVariableName());
highlightTemplate->setVariableName(this->getVariableName(), this->getVariableLocation());
highlightTemplate->setOffset(this->getOffset());

const auto &children = this->m_highlightTemplates.emplace_back(std::move(highlightTemplate))->getChildren();
Expand Down
2 changes: 1 addition & 1 deletion lib/include/pl/patterns/pattern_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace pl::ptrn {

void setPointedAtPattern(std::shared_ptr<Pattern> &&pattern) {
this->m_pointedAt = std::move(pattern);
this->m_pointedAt->setVariableName(fmt::format("*({})", this->getVariableName()));
this->m_pointedAt->setVariableName(fmt::format("*({})", this->getVariableName()), this->getVariableLocation());
this->m_pointedAt->setOffset(u64(this->m_pointedAtAddress));

if (this->hasOverriddenColor())
Expand Down
7 changes: 4 additions & 3 deletions lib/source/pl/core/ast/ast_node_array_variable_decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
namespace pl::core::ast {

ASTNodeArrayVariableDecl::ASTNodeArrayVariableDecl(std::string name, std::shared_ptr<ASTNodeTypeDecl> type, std::unique_ptr<ASTNode> &&size, std::unique_ptr<ASTNode> &&placementOffset, std::unique_ptr<ASTNode> &&placementSection, bool constant)
:m_name(std::move(name)), m_type(std::move(type)), m_size(std::move(size)), m_placementOffset(std::move(placementOffset)), m_placementSection(std::move(placementSection)), m_constant(constant) { }
:m_name(std::move(name)), m_type(std::move(type)), m_size(std::move(size)), m_placementOffset(std::move(placementOffset)), m_placementSection(std::move(placementSection)), m_constant(constant) {
}

ASTNodeArrayVariableDecl::ASTNodeArrayVariableDecl(const ASTNodeArrayVariableDecl &other) : ASTNode(other), Attributable(other) {
this->m_name = other.m_name;
Expand Down Expand Up @@ -230,7 +231,7 @@ namespace pl::core::ast {
outputPattern = std::move(arrayPattern);
}

outputPattern->setVariableName(this->m_name);
outputPattern->setVariableName(this->m_name, this->getLocation());
if (templatePattern->hasOverriddenEndian())
outputPattern->setEndian(templatePattern->getEndian());
outputPattern->setTypeName(templatePattern->getTypeName());
Expand All @@ -257,7 +258,7 @@ namespace pl::core::ast {

evaluator->alignToByte();
auto arrayPattern = std::make_unique<ptrn::PatternArrayDynamic>(evaluator, evaluator->getReadOffset(), 0, getLocation().line);
arrayPattern->setVariableName(this->m_name);
arrayPattern->setVariableName(this->m_name, this->getLocation());
arrayPattern->setSection(evaluator->getSectionId());

std::vector<std::shared_ptr<ptrn::Pattern>> entries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace pl::core::ast {

auto position = evaluator->getBitwiseReadOffset();
auto arrayPattern = std::make_shared<ptrn::PatternBitfieldArray>(evaluator, position.byteOffset, position.bitOffset, 0, getLocation().line);
arrayPattern->setVariableName(this->m_name);
arrayPattern->setVariableName(this->m_name, this->getLocation());
arrayPattern->setSection(evaluator->getSectionId());
arrayPattern->setReversed(evaluator->isReadOrderReversed());

Expand All @@ -65,7 +65,7 @@ namespace pl::core::ast {

auto addEntries = [&](std::vector<std::shared_ptr<ptrn::Pattern>> &&patterns) {
for (auto &pattern : patterns) {
pattern->setVariableName(fmt::format("[{}]", entryIndex));
pattern->setVariableName(fmt::format("[{}]", entryIndex), pattern->getVariableLocation());
pattern->setEndian(arrayPattern->getEndian());
if (pattern->getSection() == ptrn::Pattern::MainSectionId)
pattern->setSection(arrayPattern->getSection());
Expand Down
2 changes: 1 addition & 1 deletion lib/source/pl/core/ast/ast_node_bitfield_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace pl::core::ast {
auto position = evaluator->getBitwiseReadOffsetAndIncrement(bitSize);
auto pattern = this->createBitfield(evaluator, position.byteOffset, position.bitOffset, bitSize);
pattern->setPadding(this->isPadding());
pattern->setVariableName(this->getName());
pattern->setVariableName(this->getName(), this->getLocation());

pattern->setEndian(evaluator->getDefaultEndian());
pattern->setSection(evaluator->getSectionId());
Expand Down
2 changes: 1 addition & 1 deletion lib/source/pl/core/ast/ast_node_function_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace pl::core::ast {
for (size_t i = 0; i < variables.size(); i++) {
auto &[pattern, name] = variables[i];

pattern->setVariableName(name);
pattern->setVariableName(name, pattern->getVariableLocation());
}
};

Expand Down
7 changes: 4 additions & 3 deletions lib/source/pl/core/ast/ast_node_function_definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace pl::core::ast {
std::vector<std::pair<std::shared_ptr<ptrn::Pattern>, std::string>> originalNames;
ON_SCOPE_EXIT {
for (auto &[variable, name] : originalNames) {
variable->setVariableName(name);
variable->setVariableName(name, variable->getVariableLocation());
}
};
for (u32 paramIndex = 0; paramIndex < this->m_params.size() && paramIndex < params.size(); paramIndex++) {
Expand All @@ -89,7 +89,7 @@ namespace pl::core::ast {
if (params[paramIndex].isString())
reference = false;

auto variable = ctx->createVariable(name, typeNode, params[paramIndex], false, reference);
auto variable = ctx->createVariable(name, type->getLocation(), typeNode, params[paramIndex], false, reference);

if (reference && params[paramIndex].isPattern()) {
auto pattern = params[paramIndex].toPattern();
Expand All @@ -100,7 +100,8 @@ namespace pl::core::ast {

ctx->setVariable(name, params[paramIndex]);
originalNames.emplace_back(variable, name);
variable->setVariableName(name);

variable->setVariableName(name, type->getLocation());

ctx->setCurrentControlFlowStatement(ControlFlowStatement::None);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/source/pl/core/ast/ast_node_multi_variable_decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace pl::core::ast {
auto variableDecl = dynamic_cast<ASTNodeVariableDecl *>(variable.get());
auto variableType = variableDecl->getType();

evaluator->createVariable(variableDecl->getName(), variableType.get());
evaluator->createVariable(variableDecl->getName(), this->getLocation(), variableType.get());
}

return std::nullopt;
Expand Down
2 changes: 1 addition & 1 deletion lib/source/pl/core/ast/ast_node_pointer_variable_decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace pl::core::ast {
sizePattern->setSection(evaluator->getSectionId());

auto pattern = std::make_shared<ptrn::PatternPointer>(evaluator, pointerStartOffset, sizePattern->getSize(), getLocation().line);
pattern->setVariableName(this->m_name);
pattern->setVariableName(this->m_name, this->getLocation());
pattern->setPointerTypePattern(std::move(sizePattern));

ON_SCOPE_EXIT {
Expand Down
2 changes: 1 addition & 1 deletion lib/source/pl/core/ast/ast_node_rvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ namespace pl::core::ast {
if (auto transformFunc = evaluator->findFunction(pattern->getTransformFunction()); transformFunc.has_value()) {
auto oldPatternName = pattern->getVariableName();
auto result = transformFunc->func(evaluator, { std::move(literal) });
pattern->setVariableName(oldPatternName);
pattern->setVariableName(oldPatternName, pattern->getVariableLocation());

if (!result.has_value())
err::E0009.throwError("Transform function did not return a value.", "Try adding a 'return <value>;' statement in all code paths.", this->getLocation());
Expand Down
2 changes: 1 addition & 1 deletion lib/source/pl/core/ast/ast_node_type_decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ namespace pl::core::ast {

this->m_currTemplateParameterType->setLocation(lvalue->getLocation());

auto variable = evaluator->createVariable(lvalue->getLValueName(), this->m_currTemplateParameterType.get(), value, false, value.isPattern(), true, true);
auto variable = evaluator->createVariable(lvalue->getLValueName(), lvalue->getLocation(), this->m_currTemplateParameterType.get(), value, false, value.isPattern(), true, true);
if (variable != nullptr) {
variable->setInitialized(false);
evaluator->setVariable(variable, value);
Expand Down
5 changes: 3 additions & 2 deletions lib/source/pl/core/ast/ast_node_variable_decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace pl::core::ast {
if (this->m_placementOffset != nullptr && dynamic_cast<ptrn::PatternString*>(pattern.get()) != nullptr)
err::E0005.throwError(fmt::format("Variables of type 'str' cannot be placed in memory.", this->m_name), { }, this->getLocation());

pattern->setVariableName(this->m_name);
pattern->setVariableName(this->m_name, this->getLocation());

if (this->m_placementSection != nullptr)
pattern->setSection(evaluator->getSectionId());
Expand Down Expand Up @@ -126,7 +126,8 @@ namespace pl::core::ast {

auto startOffset = evaluator->getBitwiseReadOffset();

evaluator->createVariable(this->getName(), this->getType().get(), { }, this->m_outVariable, false, false, this->m_constant);
// TODO: debug-menu: can get location here.
evaluator->createVariable(this->getName(), this->getLocation(), this->getType().get(), { }, this->m_outVariable, false, false, this->m_constant);
auto &variable = evaluator->getScope(0).scope->back();

std::vector<std::shared_ptr<ptrn::Pattern>> initValues;
Expand Down
11 changes: 6 additions & 5 deletions lib/source/pl/core/evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ namespace pl::core {
pattern->setSection(section);
}

pattern->setVariableName(name);
pattern->setVariableName(name, pattern->getVariableLocation());

if (this->isDebugModeEnabled())
this->getConsole().log(LogConsole::Level::Debug, fmt::format("Creating local array variable '{} {}[{}]' at heap address 0x{:X}.", pattern->getTypeName(), pattern->getVariableName(), entryCount, pattern->getOffset()));
Expand Down Expand Up @@ -352,7 +352,7 @@ namespace pl::core {
}
}

std::shared_ptr<ptrn::Pattern> Evaluator::createVariable(const std::string &name, const ast::ASTNodeTypeDecl *type, const std::optional<Token::Literal> &value, bool outVariable, bool reference, bool templateVariable, bool constant) {
std::shared_ptr<ptrn::Pattern> Evaluator::createVariable(const std::string &name, const pl::core::Location &loc, const ast::ASTNodeTypeDecl *type, const std::optional<Token::Literal> &value, bool outVariable, bool reference, bool templateVariable, bool constant) {
auto startPos = this->getBitwiseReadOffset();
ON_SCOPE_EXIT { this->setBitwiseReadOffset(startPos); };

Expand Down Expand Up @@ -433,7 +433,7 @@ namespace pl::core {
}
}

pattern->setVariableName(name);
pattern->setVariableName(name, loc);

if (!reference) {
pattern->setSection(sectionId);
Expand Down Expand Up @@ -599,7 +599,7 @@ namespace pl::core {
// Keep the old variable until the new one is ref-counted, so we don't delete storage.
auto oldVariable = std::exchange(variablePattern, value->clone());

variablePattern->setVariableName(name);
variablePattern->setVariableName(name, variablePattern->getVariableLocation());
variablePattern->setReference(reference);

if (!reference) {
Expand Down Expand Up @@ -647,12 +647,13 @@ namespace pl::core {
auto section = pattern->getSection();
auto offset = pattern->getOffset();
auto variableName = pattern->getVariableName();
auto variableLocation = pattern->getVariableLocation();

pattern = std::move(newPattern);

pattern->setSection(section);
pattern->setOffset(offset);
pattern->setVariableName(variableName);
pattern->setVariableName(variableName, variableLocation);
}

void Evaluator::setVariable(std::shared_ptr<ptrn::Pattern> &pattern, const Token::Literal &variableValue) {
Expand Down
8 changes: 5 additions & 3 deletions lib/source/pl/core/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1700,8 +1700,9 @@ namespace pl::core {

// (parseType) Identifier[(parseMathematicalExpression)]
hlp::safe_unique_ptr<ast::ASTNode> Parser::parseMemberArrayVariable(const hlp::safe_shared_ptr<ast::ASTNodeTypeDecl> &type, bool constant) {
auto &nameToken = m_curr[-2];
auto name = getValue<Token::Identifier>(-2).get();
auto memberIdentifier = std::get_if<Token::Identifier>(&((m_curr[-2]).value));
auto memberIdentifier = std::get_if<Token::Identifier>(&nameToken.value);

hlp::safe_unique_ptr<ast::ASTNode> size;

Expand Down Expand Up @@ -1778,7 +1779,7 @@ namespace pl::core {
else
memberIdentifier->setType(Token::Identifier::IdentifierType::PatternVariable);
}
return create<ast::ASTNodeArrayVariableDecl>(name, type.unwrapUnchecked(), std::move(size.unwrapUnchecked()), nullptr, nullptr, constant);
return createWithLocation<ast::ASTNodeArrayVariableDecl>(nameToken.location, name, type.unwrapUnchecked(), std::move(size.unwrapUnchecked()), nullptr, nullptr, constant);
}

// (parseType) *Identifier : (parseType)
Expand Down Expand Up @@ -2147,6 +2148,7 @@ namespace pl::core {
} else if (const auto identifierOffset = parseCompoundAssignment(tkn::Literal::Identifier); identifierOffset.has_value())
member = parseFunctionVariableCompoundAssignment(getValue<Token::Identifier>(*identifierOffset).get());
else if (MATCHES(optional(tkn::Keyword::Unsigned) && sequence(tkn::Literal::Identifier, tkn::Operator::Colon))) {
auto identToken = m_curr[-2];
auto fieldName = getValue<Token::Identifier>(-2).get();
auto identifier = std::get_if<Token::Identifier>(&((m_curr[-2]).value));
if (identifier != nullptr)
Expand All @@ -2156,7 +2158,7 @@ namespace pl::core {
if (bitfieldSize == nullptr)
return nullptr;

member = create<ast::ASTNodeBitfieldField>(fieldName, std::move(bitfieldSize));
member = createWithLocation<ast::ASTNodeBitfieldField>(identToken.location, fieldName, std::move(bitfieldSize));
} else if (sequence(tkn::Keyword::Signed, tkn::Literal::Identifier, tkn::Operator::Colon)) {
auto fieldName = getValue<Token::Identifier>(-2).get();
auto identifier = std::get_if<Token::Identifier>(&((m_curr[-2]).value));
Expand Down
Loading