Skip to content

Commit

Permalink
apply new format style
Browse files Browse the repository at this point in the history
  • Loading branch information
heckerpowered committed Jul 22, 2024
1 parent 858184b commit 857a397
Show file tree
Hide file tree
Showing 41 changed files with 411 additions and 194 deletions.
6 changes: 3 additions & 3 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html

AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignAfterOpenBracket: BlockIndent
AlignArrayOfStructures: Left
AlignConsecutiveAssignments:
Enabled: false
Expand Down Expand Up @@ -48,7 +48,7 @@ AlignTrailingComments:
OverEmptyLines: 0

AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowBreakBeforeNoexceptSpecifier: OnlyWithParen
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
Expand All @@ -60,7 +60,7 @@ AllowShortLambdasOnASingleLine: Inline
AllowShortLoopsOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: true
BinPackArguments: true
BinPackParameters: true
BinPackParameters: false
BitFieldColonSpacing: Both
BraceWrapping:
AfterCaseLabel: true
Expand Down
60 changes: 60 additions & 0 deletions src/Mamba/Code Analysis/Compilation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "Compilation.h"

#include <memory>

#include <fast_io.h>
#include <ranges>

#include "MambaCore.h"
#include "Parser.h"
#include "SourceText.h"
#include "SyntaxTree.h"

namespace Mamba
{
std::vector<std::shared_ptr<const SourceText>> TransformToSource(const std::vector<StringView>& Sources) noexcept
{
auto SourceTexts = std::vector<std::shared_ptr<const SourceText>>();
SourceTexts.reserve(Sources.size());
for (auto&& Source : Sources)
{
const auto SharedSource = std::make_shared<String>(Source);
SourceTexts.emplace_back(std::make_shared<SourceText>(SharedSource));
}
return SourceTexts;
}

void Compilation::Compile(const std::shared_ptr<class SyntaxTree> SyntaxTree) noexcept
{
auto Parser = ::Mamba::Parser(SyntaxTree);
SyntaxTree->PrivateRoot = Parser.ParseCompilationUnit();
SyntaxTree->PrivateDiagnostics = std::move(Parser.Diagnostics);
}

void Compilation::Compile() noexcept
{
for (auto&& SyntaxTree : SyntaxTrees)
{
Compilation::Compile(SyntaxTree);
}
}

std::vector<std::shared_ptr<const class Diagnostic>> Compilation::Diagnostics() const noexcept
{
return SyntaxTrees | std::views::transform([](auto&& SyntaxTree) { return SyntaxTree->PrivateDiagnostics; })
| std::views::join | std::ranges::to<std::vector>();
}

Compilation::Compilation(const std::vector<StringView>& Sources) noexcept : Compilation(TransformToSource(Sources))
{
}

Compilation::Compilation(const std::vector<std::shared_ptr<const SourceText>>& SourceTexts) noexcept
{
for (auto&& SourceText : SourceTexts)
{
const auto SyntaxTree = std::make_shared<::Mamba::SyntaxTree>(SourceText);
SyntaxTrees.emplace_back(SyntaxTree);
}
}
} // namespace Mamba
25 changes: 25 additions & 0 deletions src/Mamba/Code Analysis/Compilation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include "SourceText.h"
#include <memory>

namespace Mamba
{
//
class Compilation final
{
std::vector<std::shared_ptr<class SyntaxTree>> SyntaxTrees;

public:
[[nodiscard]] Compilation(const std::vector<StringView>& Sources) noexcept;
[[nodiscard]] Compilation(const std::vector<std::shared_ptr<const SourceText>>& Sources) noexcept;

private:
static void Compile(const std::shared_ptr<class SyntaxTree> SyntaxTree) noexcept;

public:
void Compile() noexcept;

std::vector<std::shared_ptr<const class Diagnostic>> Diagnostics() const noexcept;
};
} // namespace Mamba
11 changes: 7 additions & 4 deletions src/Mamba/Code Analysis/Diagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace Mamba
{
Diagnostic::Diagnostic(const DiagnosticSeverity Severity, const TextLocation Location,
const std::shared_ptr<const String> Message) noexcept :
Diagnostic::Diagnostic(
const DiagnosticSeverity Severity,
const TextLocation Location,
const std::shared_ptr<const String> Message
) noexcept :
Severity(Severity), Location(Location), Message(Message)
{
}
Expand All @@ -20,8 +23,8 @@ namespace Mamba
return Diagnostic(DiagnosticSeverity::Warning, Location, Message);
}

Diagnostic Diagnostic::Information(const TextLocation Location,
const std::shared_ptr<const String> Message) noexcept
Diagnostic
Diagnostic::Information(const TextLocation Location, const std::shared_ptr<const String> Message) noexcept
{
return Diagnostic(DiagnosticSeverity::Information, Location, Message);
}
Expand Down
11 changes: 7 additions & 4 deletions src/Mamba/Code Analysis/Diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ namespace Mamba
class Diagnostic final
{
public:
[[nodiscard]] Diagnostic(const DiagnosticSeverity Severity, const TextLocation Location,
const std::shared_ptr<const String> Message) noexcept;
[[nodiscard]] Diagnostic(
const DiagnosticSeverity Severity,
const TextLocation Location,
const std::shared_ptr<const String> Message
) noexcept;

const DiagnosticSeverity Severity;
const TextLocation Location;
const std::shared_ptr<const String> Message;

static Diagnostic Error(const TextLocation Location, const std::shared_ptr<const String> Message) noexcept;
static Diagnostic Warning(const TextLocation Location, const std::shared_ptr<const String> Message) noexcept;
static Diagnostic Information(const TextLocation Location,
const std::shared_ptr<const String> Message) noexcept;
static Diagnostic
Information(const TextLocation Location, const std::shared_ptr<const String> Message) noexcept;
};
} // namespace Mamba
34 changes: 22 additions & 12 deletions src/Mamba/Code Analysis/DiagnosticBag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,32 @@ namespace Mamba
void DiagnosticBag::ReportError(const TextLocation Location, const std::shared_ptr<const String> Message) noexcept
{
emplace_back(
Hatcher([&] { return std::make_shared<const Diagnostic>(DiagnosticSeverity::Error, Location, Message); }));
Hatcher([&] { return std::make_shared<const Diagnostic>(DiagnosticSeverity::Error, Location, Message); })
);
}

void DiagnosticBag::ReportWarning(const TextLocation Location, const std::shared_ptr<const String> Message) noexcept
{
emplace_back(Hatcher(
[&] { return std::make_shared<const Diagnostic>(DiagnosticSeverity::Warning, Location, Message); }));
emplace_back(
Hatcher([&] { return std::make_shared<const Diagnostic>(DiagnosticSeverity::Warning, Location, Message); })
);
}

void DiagnosticBag::ReportInformation(const TextLocation Location,
const std::shared_ptr<const String> Message) noexcept
void DiagnosticBag::ReportInformation(
const TextLocation Location,
const std::shared_ptr<const String> Message
) noexcept
{
emplace_back(Hatcher(
[&] { return std::make_shared<const Diagnostic>(DiagnosticSeverity::Information, Location, Message); }));
[&] { return std::make_shared<const Diagnostic>(DiagnosticSeverity::Information, Location, Message); }
));
}

void DiagnosticBag::ReportInvalidCharacter(const TextLocation Location, const Char Character) noexcept
{
const auto Message = std::make_shared<const String>(
Hatcher([&] { return Concat(TEXT("Invalid character '"), Character, TEXT("'.")); }));
Hatcher([&] { return Concat(TEXT("Invalid character '"), Character, TEXT("'.")); })
);
ReportError(Location, Message);
}

Expand Down Expand Up @@ -79,13 +85,17 @@ namespace Mamba
ReportError(Location, Message);
}

void DiagnosticBag::ReportUnexpectedToken(const TextLocation Location, const SyntaxKind Kind,
const SyntaxKind ExpectedKind) noexcept
void DiagnosticBag::ReportUnexpectedToken(
const TextLocation Location,
const SyntaxKind Kind,
const SyntaxKind ExpectedKind
) noexcept
{
// Unexpected token 'Kind', Expected: 'ExpectedKind'.
const auto Message = std::make_shared<const String>(
Concat(TEXT("Unexpected token '"), SyntaxFacts::GetText(Kind), TEXT("'"), TEXT("Expected: '"),
SyntaxFacts::ToString(ExpectedKind), TEXT("'.")));
const auto Message = std::make_shared<const String>(Concat(
TEXT("Unexpected token '"), SyntaxFacts::GetText(Kind), TEXT("'"), TEXT("Expected: '"),
SyntaxFacts::ToString(ExpectedKind), TEXT("'.")
));
ReportError(Location, Message);
}
} // namespace Mamba
7 changes: 5 additions & 2 deletions src/Mamba/Code Analysis/DiagnosticBag.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ namespace Mamba
void ReportInvalidBinary(const TextLocation Location, const StringView Literal) noexcept;
void ReportInvalidOctal(const TextLocation Location, const StringView Literal) noexcept;

void ReportUnexpectedToken(const TextLocation Location, const SyntaxKind Kind,
const SyntaxKind ExpectedKind) noexcept;
void ReportUnexpectedToken(
const TextLocation Location,
const SyntaxKind Kind,
const SyntaxKind ExpectedKind
) noexcept;
};

} // namespace Mamba
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace Mamba
const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> IdentifierToken,
const std::shared_ptr<const class SyntaxToken> AssignmentToken,
const std::shared_ptr<const ExpressionSyntax> Expression) noexcept :
const std::shared_ptr<const ExpressionSyntax> Expression
) noexcept :
Super(SyntaxTree), IdentifierToken(IdentifierToken), AssignmentToken(AssignmentToken), Expression(Expression)
{
}
Expand Down
10 changes: 6 additions & 4 deletions src/Mamba/Code Analysis/Syntax/AssignmentExpressionSyntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ namespace Mamba
public:
using Super = ExpressionSyntax;

[[nodiscard]] AssignmentExpressionSyntax(const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> IdentifierToken,
const std::shared_ptr<const class SyntaxToken> AssignmentToken,
const std::shared_ptr<const ExpressionSyntax> Expression) noexcept;
[[nodiscard]] AssignmentExpressionSyntax(
const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> IdentifierToken,
const std::shared_ptr<const class SyntaxToken> AssignmentToken,
const std::shared_ptr<const ExpressionSyntax> Expression
) noexcept;

std::vector<std::shared_ptr<const SyntaxNode>> Children() const noexcept override;
SyntaxKind Kind() const noexcept override;
Expand Down
10 changes: 6 additions & 4 deletions src/Mamba/Code Analysis/Syntax/BinaryExpressionSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Mamba
{
BinaryExpressionSyntax::BinaryExpressionSyntax(const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class ExpressionSyntax> Left,
const std::shared_ptr<const class SyntaxToken> OperatorToken,
const std::shared_ptr<const class ExpressionSyntax> Right) noexcept :
BinaryExpressionSyntax::BinaryExpressionSyntax(
const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class ExpressionSyntax> Left,
const std::shared_ptr<const class SyntaxToken> OperatorToken,
const std::shared_ptr<const class ExpressionSyntax> Right
) noexcept :
Super(SyntaxTree), Left(Left), OperatorToken(OperatorToken), Right(Right)
{
}
Expand Down
14 changes: 9 additions & 5 deletions src/Mamba/Code Analysis/Syntax/LiteralExpressionSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@

namespace Mamba
{
LiteralExpressionSyntax::LiteralExpressionSyntax(const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const SyntaxToken> LiteralToken) noexcept :
LiteralExpressionSyntax::LiteralExpressionSyntax(
const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const SyntaxToken> LiteralToken
) noexcept :
Super(SyntaxTree), LiteralToken(LiteralToken), Value(LiteralToken->Value)
{
}

LiteralExpressionSyntax::LiteralExpressionSyntax(const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const SyntaxToken> LiteralToken,
const std::shared_ptr<const Literal> Value) noexcept :
LiteralExpressionSyntax::LiteralExpressionSyntax(
const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const SyntaxToken> LiteralToken,
const std::shared_ptr<const Literal> Value
) noexcept :
Super(SyntaxTree), LiteralToken(LiteralToken), Value(Value)
{
}
Expand Down
20 changes: 12 additions & 8 deletions src/Mamba/Code Analysis/Syntax/LiteralExpressionSyntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ namespace Mamba
public:
using Super = ExpressionSyntax;

[[nodiscard]] LiteralExpressionSyntax(const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> LiteralToken) noexcept;
[[nodiscard]] LiteralExpressionSyntax(
const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> LiteralToken
) noexcept;

[[nodiscard]] LiteralExpressionSyntax(const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> LiteralToken,
// msvc bug, struct keyword is required here, class is not allowed
// https://eel.is/c++draft/dcl.dcl#dcl.type.elab-7
// https://zh.cppreference.com/w/cpp/language/elaborated_type_specifier
const std::shared_ptr<const struct Literal> Value) noexcept;
[[nodiscard]] LiteralExpressionSyntax(
const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> LiteralToken,
// use class in this elaborated-type-specifier is valid, but may cause linker error under Microsoft ABI
// https://eel.is/c++draft/dcl.dcl#dcl.type.elab-7
// https://zh.cppreference.com/w/cpp/language/elaborated_type_specifier
const std::shared_ptr<const struct Literal> Value
) noexcept;

SyntaxKind Kind() const noexcept override;
std::vector<std::shared_ptr<const class SyntaxNode>> Children() const noexcept override;
Expand Down
7 changes: 4 additions & 3 deletions src/Mamba/Code Analysis/Syntax/NameExpressionSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Mamba
{
NameExpressionSyntax::NameExpressionSyntax(const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> IdentifierToken) noexcept
:
NameExpressionSyntax::NameExpressionSyntax(
const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> IdentifierToken
) noexcept :
Super(SyntaxTree), IdentifierToken(IdentifierToken)
{
}
Expand Down
6 changes: 4 additions & 2 deletions src/Mamba/Code Analysis/Syntax/NameExpressionSyntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ namespace Mamba
public:
using Super = ExpressionSyntax;

[[nodiscard]] NameExpressionSyntax(const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> IdentifierToken) noexcept;
[[nodiscard]] NameExpressionSyntax(
const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> IdentifierToken
) noexcept;

SyntaxKind Kind() const noexcept override;
std::vector<std::shared_ptr<const class SyntaxNode>> Children() const noexcept override;
Expand Down
8 changes: 5 additions & 3 deletions src/Mamba/Code Analysis/Syntax/ParameterSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Mamba
{
ParameterSyntax::ParameterSyntax(const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> Identifier,
const std::shared_ptr<const class TypeClauseSyntax> Type) noexcept :
ParameterSyntax::ParameterSyntax(
const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> Identifier,
const std::shared_ptr<const class TypeClauseSyntax> Type
) noexcept :
Super(SyntaxTree), Identifier(Identifier), Type(Type)
{
}
Expand Down
8 changes: 5 additions & 3 deletions src/Mamba/Code Analysis/Syntax/ParameterSyntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ namespace Mamba
public:
using Super = SyntaxNode;

[[nodiscard]] ParameterSyntax(const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> Identifier,
const std::shared_ptr<const class TypeClauseSyntax> Type) noexcept;
[[nodiscard]] ParameterSyntax(
const std::shared_ptr<const class SyntaxTree> SyntaxTree,
const std::shared_ptr<const class SyntaxToken> Identifier,
const std::shared_ptr<const class TypeClauseSyntax> Type
) noexcept;

SyntaxKind Kind() const noexcept override;
std::vector<std::shared_ptr<const class SyntaxNode>> Children() const noexcept override;
Expand Down
Loading

0 comments on commit 857a397

Please sign in to comment.