Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 13 additions & 18 deletions cli/source/subcommands/docs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace pl::cli::sub {

namespace {

std::string getTypeEndian(const core::ast::ASTNodeTypeDecl *typeDecl) {
auto endian = typeDecl->getEndian();
std::string getTypeEndian(const core::ast::ASTNodeTypeApplication *typeApp) {
auto endian = typeApp->getEndian();

if (!endian.has_value())
return "";
Expand All @@ -35,15 +35,12 @@ namespace pl::cli::sub {
}

std::string getTypeName(const core::ast::ASTNode *type) {
if (auto builtinType = dynamic_cast<const core::ast::ASTNodeBuiltinType*>(type))
return core::Token::getTypeName(builtinType->getType());
else if (auto typeDecl = dynamic_cast<const core::ast::ASTNodeTypeDecl*>(type)) {
if (typeDecl->getName().empty())
return getTypeEndian(typeDecl) + getTypeName(typeDecl->getType().get());
else
return getTypeEndian(typeDecl) + typeDecl->getName();
if (auto typeApp = dynamic_cast<const core::ast::ASTNodeTypeApplication*>(type); typeApp != nullptr) {
return fmt::format("{}{}", getTypeEndian(typeApp), getTypeName(typeApp->getType().get()));
} else if (auto typeDecl = dynamic_cast<const core::ast::ASTNodeTypeDecl*>(type); typeDecl != nullptr) {
return typeDecl->getName();
} else {
return "???";
return "";
}
}

Expand Down Expand Up @@ -72,12 +69,10 @@ namespace pl::cli::sub {

std::string result = "<";
for (const auto &templateParam : templateParams) {
if (auto typeDecl = dynamic_cast<const core::ast::ASTNodeTypeDecl*>(templateParam.get()); typeDecl != nullptr)
result += typeDecl->getName();
else if (auto lvalue = dynamic_cast<const core::ast::ASTNodeLValueAssignment*>(templateParam.get()); lvalue != nullptr)
result += fmt::format("auto {}", lvalue->getLValueName());
else
continue;
if (templateParam->isType())
result += templateParam->getName().get();
else
result += fmt::format("auto {}", templateParam->getName().get());

result += ", ";
}
Expand All @@ -89,8 +84,8 @@ namespace pl::cli::sub {
}

std::string generateTypeDocumentation(const std::string &name, const core::ast::ASTNodeTypeDecl *type) {
if (auto typeDecl = dynamic_cast<core::ast::ASTNodeTypeDecl*>(type->getType().get())) {
return fmt::format("```rust\nusing {}{} = {}{};\n```", name, generateTemplateParams(type), getTypeName(typeDecl), generateAttributes(typeDecl));
if (auto typeApp = dynamic_cast<core::ast::ASTNodeTypeApplication*>(type->getType().get())) {
return fmt::format("```rust\nusing {}{} = {}{};\n```", name, generateTemplateParams(type), getTypeName(typeApp), generateAttributes(type));
} else if (dynamic_cast<core::ast::ASTNodeStruct*>(type->getType().get())) {
return fmt::format("```rust\nstruct {}{} {{ ... }}{};\n```", name, generateTemplateParams(type), generateAttributes(type));
} else if (dynamic_cast<core::ast::ASTNodeUnion*>(type->getType().get())) {
Expand Down
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ add_library(libpl ${LIBRARY_TYPE}
source/pl/core/ast/ast_node_struct.cpp
source/pl/core/ast/ast_node_ternary_expression.cpp
source/pl/core/ast/ast_node_try_catch_statement.cpp
source/pl/core/ast/ast_node_type_application.cpp
source/pl/core/ast/ast_node_type_decl.cpp
source/pl/core/ast/ast_node_type_operator.cpp
source/pl/core/ast/ast_node_union.cpp
Expand Down
7 changes: 4 additions & 3 deletions lib/include/pl/core/ast/ast_node_array_variable_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <pl/core/ast/ast_node.hpp>
#include <pl/core/ast/ast_node_attribute.hpp>
#include <pl/core/ast/ast_node_type_appilication.hpp>

namespace pl::core::ast {

Expand All @@ -10,7 +11,7 @@ namespace pl::core::ast {
class ASTNodeArrayVariableDecl : public ASTNode,
public Attributable {
public:
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 = false);
ASTNodeArrayVariableDecl(std::string name, std::shared_ptr<ASTNodeTypeApplication> type, std::unique_ptr<ASTNode> &&size, std::unique_ptr<ASTNode> &&placementOffset = {}, std::unique_ptr<ASTNode> &&placementSection = {}, bool constant = false);
ASTNodeArrayVariableDecl(const ASTNodeArrayVariableDecl &other);

[[nodiscard]] std::unique_ptr<ASTNode> clone() const override {
Expand All @@ -24,7 +25,7 @@ namespace pl::core::ast {
return this->m_name;
}

[[nodiscard]] const std::shared_ptr<ASTNodeTypeDecl> &getType() const {
[[nodiscard]] const std::shared_ptr<ASTNodeTypeApplication> &getType() const {
return this->m_type;
}

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

private:
std::string m_name;
std::shared_ptr<ASTNodeTypeDecl> m_type;
std::shared_ptr<ASTNodeTypeApplication> m_type;
std::unique_ptr<ASTNode> m_size;
std::unique_ptr<ASTNode> m_placementOffset, m_placementSection;
bool m_constant;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <pl/core/ast/ast_node_bitfield.hpp>
#include <pl/core/ast/ast_node_bitfield_field.hpp>
#include <pl/core/ast/ast_node_literal.hpp>
#include <pl/core/ast/ast_node_type_decl.hpp>
#include <pl/core/ast/ast_node_type_appilication.hpp>
#include <pl/core/ast/ast_node_while_statement.hpp>

#include <pl/patterns/pattern_array_dynamic.hpp>
Expand All @@ -15,7 +15,7 @@ namespace pl::core::ast {
class ASTNodeBitfieldArrayVariableDecl : public ASTNode,
public Attributable {
public:
ASTNodeBitfieldArrayVariableDecl(std::string name, std::shared_ptr<ASTNodeTypeDecl> type, std::unique_ptr<ASTNode> &&size);
ASTNodeBitfieldArrayVariableDecl(std::string name, std::shared_ptr<ASTNodeTypeApplication> type, std::unique_ptr<ASTNode> &&size);
ASTNodeBitfieldArrayVariableDecl(const ASTNodeBitfieldArrayVariableDecl &other);

[[nodiscard]] std::unique_ptr<ASTNode> clone() const override {
Expand All @@ -28,7 +28,7 @@ namespace pl::core::ast {
return this->m_name;
}

[[nodiscard]] const std::shared_ptr<ASTNodeTypeDecl> &getType() const {
[[nodiscard]] const std::shared_ptr<ASTNodeTypeApplication> &getType() const {
return this->m_type;
}

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

private:
std::string m_name;
std::shared_ptr<ASTNodeTypeDecl> m_type;
std::shared_ptr<ASTNodeTypeApplication> m_type;
std::unique_ptr<ASTNode> m_size;

void createArray(Evaluator *evaluator, std::shared_ptr<ptrn::Pattern> &resultPattern) const;
Expand Down
6 changes: 3 additions & 3 deletions lib/include/pl/core/ast/ast_node_bitfield_field.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <pl/core/ast/ast_node.hpp>
#include <pl/core/ast/ast_node_type_decl.hpp>
#include <pl/core/ast/ast_node_type_appilication.hpp>
#include <pl/core/ast/ast_node_attribute.hpp>

#include <pl/patterns/pattern_bitfield.hpp>
Expand Down Expand Up @@ -42,7 +42,7 @@ namespace pl::core::ast {

class ASTNodeBitfieldFieldSizedType : public ASTNodeBitfieldField {
public:
ASTNodeBitfieldFieldSizedType(std::string name, std::unique_ptr<ASTNodeTypeDecl> &&type, std::unique_ptr<ASTNode> &&size);
ASTNodeBitfieldFieldSizedType(std::string name, std::unique_ptr<ASTNodeTypeApplication> &&type, std::unique_ptr<ASTNode> &&size);
ASTNodeBitfieldFieldSizedType(const ASTNodeBitfieldFieldSizedType &other);

[[nodiscard]] std::unique_ptr<ASTNode> clone() const override {
Expand All @@ -52,7 +52,7 @@ namespace pl::core::ast {
[[nodiscard]] std::shared_ptr<ptrn::PatternBitfieldField> createBitfield(Evaluator *evaluator, u64 byteOffset, u8 bitOffset, u8 bitSize) const override;

private:
std::unique_ptr<ASTNodeTypeDecl> m_type;
std::unique_ptr<ASTNodeTypeApplication> m_type;
};

}
5 changes: 3 additions & 2 deletions lib/include/pl/core/ast/ast_node_cast.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#pragma once

#include <pl/core/ast/ast_node.hpp>
#include <pl/core/ast/ast_node_type_appilication.hpp>

namespace pl::core::ast {

class ASTNodeCast : public ASTNode {
public:
ASTNodeCast(std::unique_ptr<ASTNode> &&value, std::unique_ptr<ASTNode> &&type, bool reinterpret);
ASTNodeCast(std::unique_ptr<ASTNode> &&value, std::unique_ptr<ASTNodeTypeApplication> &&type, bool reinterpret);
ASTNodeCast(const ASTNodeCast &other);

[[nodiscard]] std::unique_ptr<ASTNode> clone() const override {
Expand All @@ -20,7 +21,7 @@ namespace pl::core::ast {

private:
std::unique_ptr<ASTNode> m_value;
std::unique_ptr<ASTNode> m_type;
std::unique_ptr<ASTNodeTypeApplication> m_type;
bool m_reinterpret;
};

Expand Down
8 changes: 4 additions & 4 deletions lib/include/pl/core/ast/ast_node_pointer_variable_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <pl/core/ast/ast_node.hpp>
#include <pl/core/ast/ast_node_attribute.hpp>

#include <pl/core/ast/ast_node_type_appilication.hpp>

namespace pl::core::ast {

Expand All @@ -11,7 +11,7 @@ namespace pl::core::ast {
class ASTNodePointerVariableDecl : public ASTNode,
public Attributable {
public:
ASTNodePointerVariableDecl(std::string name, std::shared_ptr<ASTNode> type, std::shared_ptr<ASTNodeTypeDecl> sizeType, std::unique_ptr<ASTNode> &&placementOffset = nullptr, std::unique_ptr<ASTNode> &&placementSection = nullptr);
ASTNodePointerVariableDecl(std::string name, std::shared_ptr<ASTNode> type, std::shared_ptr<ASTNodeTypeApplication> sizeType, std::unique_ptr<ASTNode> &&placementOffset = nullptr, std::unique_ptr<ASTNode> &&placementSection = nullptr);
ASTNodePointerVariableDecl(const ASTNodePointerVariableDecl &other);

[[nodiscard]] std::unique_ptr<ASTNode> clone() const override {
Expand All @@ -20,15 +20,15 @@ namespace pl::core::ast {

[[nodiscard]] const std::string &getName() const { return this->m_name; }
[[nodiscard]] constexpr const std::shared_ptr<ASTNode> &getType() const { return this->m_type; }
[[nodiscard]] constexpr const std::shared_ptr<ASTNodeTypeDecl> &getSizeType() const { return this->m_sizeType; }
[[nodiscard]] constexpr const std::shared_ptr<ASTNodeTypeApplication> &getSizeType() const { return this->m_sizeType; }
[[nodiscard]] constexpr const std::unique_ptr<ASTNode> &getPlacementOffset() const { return this->m_placementOffset; }

void createPatterns(Evaluator *evaluator, std::vector<std::shared_ptr<ptrn::Pattern>> &resultPatterns) const override;

private:
std::string m_name;
std::shared_ptr<ASTNode> m_type;
std::shared_ptr<ASTNodeTypeDecl> m_sizeType;
std::shared_ptr<ASTNodeTypeApplication> m_sizeType;
std::unique_ptr<ASTNode> m_placementOffset, m_placementSection;
};

Expand Down
5 changes: 3 additions & 2 deletions lib/include/pl/core/ast/ast_node_scope_resolution.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#pragma once

#include <pl/core/ast/ast_node.hpp>
#include <pl/core/ast/ast_node_type_decl.hpp>

namespace pl::core::ast {

class ASTNodeScopeResolution : public ASTNode {
public:
explicit ASTNodeScopeResolution(std::shared_ptr<ASTNode> &&type, std::string name);
explicit ASTNodeScopeResolution(std::shared_ptr<ASTNodeTypeDecl> &&type, std::string name);
ASTNodeScopeResolution(const ASTNodeScopeResolution &other);

[[nodiscard]] std::unique_ptr<ASTNode> clone() const override {
Expand All @@ -16,7 +17,7 @@ namespace pl::core::ast {
[[nodiscard]] std::unique_ptr<ASTNode> evaluate(Evaluator *evaluator) const override;

private:
std::shared_ptr<ASTNode> m_type;
std::shared_ptr<ASTNodeTypeDecl> m_type;
std::string m_name;
};

Expand Down
31 changes: 31 additions & 0 deletions lib/include/pl/core/ast/ast_node_template_parameter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <pl/core/ast/ast_node.hpp>

namespace pl::core::ast {

class ASTNodeTemplateParameter : public ASTNode {
public:
explicit ASTNodeTemplateParameter(Token::Identifier name, bool isType)
: m_name(std::move(name)), m_isType(isType) {}

ASTNodeTemplateParameter(const ASTNodeTemplateParameter &) = default;

[[nodiscard]] std::unique_ptr<ASTNode> clone() const override {
return std::unique_ptr<ASTNode>(new ASTNodeTemplateParameter(*this));
}

[[nodiscard]] const auto &getName() const {
return this->m_name;
}

[[nodiscard]] bool isType() const {
return this->m_isType;
}

private:
Token::Identifier m_name;
bool m_isType;
};

}
65 changes: 65 additions & 0 deletions lib/include/pl/core/ast/ast_node_type_appilication.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once

#include <pl/core/ast/ast_node.hpp>
#include <pl/core/ast/ast_node_type_decl.hpp>

namespace pl::core::ast {

class ASTNodeTypeApplication : public ASTNode {
public:
explicit ASTNodeTypeApplication(std::shared_ptr<ASTNode> type);

ASTNodeTypeApplication(const ASTNodeTypeApplication &);

[[nodiscard]] std::unique_ptr<ASTNode> clone() const override {
return std::unique_ptr<ASTNode>(new ASTNodeTypeApplication(*this));
}

void setTemplateArguments(std::vector<std::unique_ptr<ASTNode>> &&arguments) {
this->m_templateArguments = std::move(arguments);
}

std::vector<std::unique_ptr<ASTNode>> evaluateTemplateArguments(Evaluator *evaluator) const;

[[nodiscard]] std::unique_ptr<ASTNode> evaluate(Evaluator *evaluator) const override;
void createPatterns(Evaluator *evaluator, std::vector<std::shared_ptr<ptrn::Pattern>> &resultPatterns) const override;

[[nodiscard]] const std::shared_ptr<ASTNode> &getType() const {
return this->m_type;
}

void setReference(bool reference) {
this->m_reference = reference;
}

[[nodiscard]] bool isReference() const {
return this->m_reference;
}

const ast::ASTNode* getTypeDefinition(Evaluator *evaluator) const;

[[nodiscard]] const std::string getTypeName() const;

void setEndian(std::endian endian) {
this->m_endian = endian;
}

[[nodiscard]] std::optional<std::endian> getEndian() const { return this->m_endian; }

void setTemplateParameterIndex(int index) {
this->m_templateParameterIndex = index;
}

[[nodiscard]] int getTemplateParameterIndex() const {
return this->m_templateParameterIndex;
}

private:
std::shared_ptr<ASTNode> m_type;
std::vector<std::unique_ptr<ASTNode>> m_templateArguments;
bool m_reference = false;
std::optional<std::endian> m_endian;
size_t m_templateParameterIndex = 0;
};

}
Loading
Loading