Fixes several valgrind spotted issues.

This commit is contained in:
Deukhoofd 2020-10-10 20:02:47 +02:00
parent ce9ad2c9ba
commit 6a0ec63a7e
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 55 additions and 31 deletions

View File

@ -49,7 +49,6 @@ namespace MalachScript::Parser {
const auto* current = _currentToken; const auto* current = _currentToken;
auto start = current->GetSpan().GetStart(); auto start = current->GetSpan().GetStart();
bool lookingForClass = true; bool lookingForClass = true;
bool encounteredError = false;
while (lookingForClass) { while (lookingForClass) {
switch (current->GetKind()) { switch (current->GetKind()) {
case LexTokenKind::SharedKeyword: break; case LexTokenKind::SharedKeyword: break;
@ -91,8 +90,7 @@ namespace MalachScript::Parser {
inherits.push_back(id); inherits.push_back(id);
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
} }
if (!encounteredError && current->GetKind() != LexTokenKind::OpenCurlyParenthesisSymbol) { if (current->GetKind() != LexTokenKind::OpenCurlyParenthesisSymbol) {
encounteredError = true;
LogError(Diagnostics::DiagnosticType::UnexpectedToken, current->GetSpan()); LogError(Diagnostics::DiagnosticType::UnexpectedToken, current->GetSpan());
} }
// Intentionally don't break so we continue into the inner body statement. // Intentionally don't break so we continue into the inner body statement.
@ -105,7 +103,7 @@ namespace MalachScript::Parser {
PROGRESS_TOKEN(current); PROGRESS_TOKEN(current);
break; break;
} }
const ParsedStatement* statement; const ParsedStatement* statement = nullptr;
// TODO: Sort by // TODO: Sort by
if (!ParseVirtProp(statement) && !ParseFunc(statement) && !ParseVar(statement) && if (!ParseVirtProp(statement) && !ParseFunc(statement) && !ParseVar(statement) &&
!ParseFuncDef(statement)) { !ParseFuncDef(statement)) {
@ -154,7 +152,7 @@ namespace MalachScript::Parser {
if (!ParseIdentifier(identifier, _currentToken)) { if (!ParseIdentifier(identifier, _currentToken)) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, _currentToken->GetSpan()); LogError(Diagnostics::DiagnosticType::UnexpectedToken, _currentToken->GetSpan());
} }
auto script = ParseScript(); const auto *script = ParseScript();
auto end = _currentToken->GetSpan().GetEnd(); auto end = _currentToken->GetSpan().GetEnd();
PROGRESS_TOKEN(_currentToken); PROGRESS_TOKEN(_currentToken);
out = new ParsedNamespaceStatement(TextSpan(start, end), identifier, script); out = new ParsedNamespaceStatement(TextSpan(start, end), identifier, script);
@ -214,7 +212,7 @@ namespace MalachScript::Parser {
PROGRESS_TOKEN(_currentToken); PROGRESS_TOKEN(_currentToken);
} }
bool lookingForFuncAttr = true; bool lookingForFuncAttr = true;
FuncAttr funcAttr; FuncAttr funcAttr = FuncAttr::None;
while (lookingForFuncAttr) { while (lookingForFuncAttr) {
switch (_currentToken->GetKind()) { switch (_currentToken->GetKind()) {
case LexTokenKind::OverrideKeyword: case LexTokenKind::OverrideKeyword:
@ -325,7 +323,8 @@ namespace MalachScript::Parser {
} }
auto start = currentToken->GetSpan().GetStart(); auto start = currentToken->GetSpan().GetStart();
PROGRESS_TOKEN(currentToken); PROGRESS_TOKEN(currentToken);
std::vector<ParsedParamListStatement::ParsedParameter> parameters; std::vector<const ParsedParamListStatement::ParsedParameter*> parameters;
if (currentToken->GetKind() == LexTokenKind::VoidKeyword) { if (currentToken->GetKind() == LexTokenKind::VoidKeyword) {
PROGRESS_TOKEN(currentToken); PROGRESS_TOKEN(currentToken);
if (currentToken->GetKind() != LexTokenKind::CloseParenthesisSymbol) { if (currentToken->GetKind() != LexTokenKind::CloseParenthesisSymbol) {
@ -334,20 +333,28 @@ namespace MalachScript::Parser {
PROGRESS_TOKEN(currentToken); PROGRESS_TOKEN(currentToken);
out = new ParsedParamListStatement(TextSpan(start, currentToken->GetSpan().GetEnd()), parameters); out = new ParsedParamListStatement(TextSpan(start, currentToken->GetSpan().GetEnd()), parameters);
return true; return true;
} else if (currentToken->GetKind() == LexTokenKind::CloseParenthesisSymbol) { } if (currentToken->GetKind() == LexTokenKind::CloseParenthesisSymbol) {
out = new ParsedParamListStatement(TextSpan(start, currentToken->GetSpan().GetEnd()), parameters); out = new ParsedParamListStatement(TextSpan(start, currentToken->GetSpan().GetEnd()), parameters);
PROGRESS_TOKEN(currentToken); PROGRESS_TOKEN(currentToken);
return true; return true;
} }
while (true) { while (true) {
parameters.emplace_back(); const ParsedStatement* typeStatement = nullptr;
auto& parameter = parameters.at(parameters.size() - 1); TypeMod typeMod = TypeMod::None;
if (!ParseType((const ParsedStatement*&)parameter.GetTypeStatement(), currentToken)) { Identifier identifier;
const ParsedExpression* defaultExpression = nullptr;
if (!ParseType(typeStatement, currentToken)) {
LogError(Diagnostics::DiagnosticType::UnexpectedToken, currentToken->GetSpan()); LogError(Diagnostics::DiagnosticType::UnexpectedToken, currentToken->GetSpan());
} }
ParseTypeMod(parameter.GetTypeMod(), currentToken); ParseTypeMod(typeMod, currentToken);
ParseIdentifier(parameter.GetIdentifier(), currentToken); ParseIdentifier(identifier, currentToken);
PROGRESS_TOKEN(currentToken); PROGRESS_TOKEN(currentToken);
// TODO: Default expression
parameters.push_back(new ParsedParamListStatement::ParsedParameter(
dynamic_cast<const ParsedTypeStatement*>(typeStatement), typeMod, identifier, defaultExpression));
if (currentToken->GetKind() != LexTokenKind::CommaSymbol) { if (currentToken->GetKind() != LexTokenKind::CommaSymbol) {
break; break;
} }

View File

@ -105,16 +105,23 @@ namespace MalachScript::Parser {
public: public:
class ParsedParameter { class ParsedParameter {
private: private:
const ParsedTypeStatement* _typeStatement = nullptr; std::unique_ptr<const ParsedTypeStatement> _typeStatement = nullptr;
TypeMod _typeMod = TypeMod::None; TypeMod _typeMod = TypeMod::None;
Identifier _identifier; Identifier _identifier;
const ParsedExpression* _defaultExpression = nullptr; std::unique_ptr<const ParsedExpression> _defaultExpression = nullptr;
public: public:
ParsedParameter(){}; ParsedParameter(const ParsedTypeStatement* typeStatement, TypeMod typeMod, const Identifier& identifier,
const ParsedExpression* defaultExpression)
: _typeStatement(typeStatement), _typeMod(typeMod), _identifier(identifier),
_defaultExpression(defaultExpression){};
[[nodiscard]] const ParsedTypeStatement*& GetTypeStatement() noexcept { return _typeStatement; } [[nodiscard]] std::unique_ptr<const ParsedTypeStatement>& GetTypeStatement() noexcept {
[[nodiscard]] const ParsedTypeStatement* GetTypeStatement() const noexcept { return _typeStatement; } return _typeStatement;
}
[[nodiscard]] const std::unique_ptr<const ParsedTypeStatement>& GetTypeStatement() const noexcept {
return _typeStatement;
}
[[nodiscard]] TypeMod& GetTypeMod() noexcept { return _typeMod; } [[nodiscard]] TypeMod& GetTypeMod() noexcept { return _typeMod; }
[[nodiscard]] const TypeMod& GetTypeMod() const noexcept { return _typeMod; } [[nodiscard]] const TypeMod& GetTypeMod() const noexcept { return _typeMod; }
@ -122,18 +129,28 @@ namespace MalachScript::Parser {
[[nodiscard]] Identifier& GetIdentifier() noexcept { return _identifier; } [[nodiscard]] Identifier& GetIdentifier() noexcept { return _identifier; }
[[nodiscard]] const Identifier& GetIdentifier() const noexcept { return _identifier; } [[nodiscard]] const Identifier& GetIdentifier() const noexcept { return _identifier; }
[[nodiscard]] const ParsedExpression*& GetDefaultExpression() noexcept { return _defaultExpression; } [[nodiscard]] std::unique_ptr<const ParsedExpression>& GetDefaultExpression() noexcept {
[[nodiscard]] const ParsedExpression* GetDefaultExpression() const noexcept { return _defaultExpression; } return _defaultExpression;
}
[[nodiscard]] const std::unique_ptr<const ParsedExpression>& GetDefaultExpression() const noexcept {
return _defaultExpression;
}
}; };
private: private:
std::vector<ParsedParameter> _parameters; std::vector<std::unique_ptr<const ParsedParameter>> _parameters;
public: public:
ParsedParamListStatement(TextSpan span, std::vector<ParsedParameter> parameters) ParsedParamListStatement(TextSpan span, const std::vector<const ParsedParameter*>& parameters)
: ParsedStatementImpl<ParsedStatementKind::ParamList>(span), _parameters(std::move(parameters)){}; : ParsedStatementImpl<ParsedStatementKind::ParamList>(span), _parameters(parameters.size()) {
for (size_t i = 0; i < parameters.size(); i++) {
_parameters[i] = std::unique_ptr<const ParsedParameter>(parameters[i]);
}
};
[[nodiscard]] const std::vector<ParsedParameter>& GetParameters() const noexcept { return _parameters; } [[nodiscard]] const std::vector<std::unique_ptr<const ParsedParameter>>& GetParameters() const noexcept {
return _parameters;
}
}; };
class ParsedFuncStatement : public ParsedStatementImpl<ParsedStatementKind::Func> { class ParsedFuncStatement : public ParsedStatementImpl<ParsedStatementKind::Func> {
@ -145,7 +162,7 @@ namespace MalachScript::Parser {
bool _returnsReference; bool _returnsReference;
Identifier _identifier; Identifier _identifier;
std::unique_ptr<const ParsedStatement> _paramList; std::unique_ptr<const ParsedStatement> _paramList;
bool isConst; bool _isConst;
FuncAttr _funcAttr; FuncAttr _funcAttr;
std::unique_ptr<const ParsedStatement> _statBlock; std::unique_ptr<const ParsedStatement> _statBlock;
@ -156,7 +173,7 @@ namespace MalachScript::Parser {
const ParsedStatement* statBlock) const ParsedStatement* statBlock)
: ParsedStatementImpl<ParsedStatementKind::Func>(span), _isShared(isShared), _isExternal(isExternal), : ParsedStatementImpl<ParsedStatementKind::Func>(span), _isShared(isShared), _isExternal(isExternal),
_access(access), _type(type), _returnsReference(returnsReference), _identifier(identifier), _access(access), _type(type), _returnsReference(returnsReference), _identifier(identifier),
_paramList(paramList), isConst(isConst), _funcAttr(funcAttr), _statBlock(statBlock) {} _paramList(paramList), _isConst(isConst), _funcAttr(funcAttr), _statBlock(statBlock) {}
[[nodiscard]] inline bool IsShared() const noexcept { return _isShared; } [[nodiscard]] inline bool IsShared() const noexcept { return _isShared; }
[[nodiscard]] inline bool IsExternal() const noexcept { return _isExternal; } [[nodiscard]] inline bool IsExternal() const noexcept { return _isExternal; }
@ -169,7 +186,7 @@ namespace MalachScript::Parser {
[[nodiscard]] inline const std::unique_ptr<const ParsedStatement>& GetParamList() const noexcept { [[nodiscard]] inline const std::unique_ptr<const ParsedStatement>& GetParamList() const noexcept {
return _paramList; return _paramList;
} }
[[nodiscard]] inline bool IsConst() const noexcept { return isConst; } [[nodiscard]] inline bool IsConst() const noexcept { return _isConst; }
[[nodiscard]] inline FuncAttr GetFuncAttr() const noexcept { return _funcAttr; } [[nodiscard]] inline FuncAttr GetFuncAttr() const noexcept { return _funcAttr; }
[[nodiscard]] inline const std::unique_ptr<const ParsedStatement>& GetStatBlock() const noexcept { [[nodiscard]] inline const std::unique_ptr<const ParsedStatement>& GetStatBlock() const noexcept {
return _statBlock; return _statBlock;

View File

@ -129,7 +129,7 @@ PARSER_TEST("Parse scoped function with parameters without body.",
auto paramList = (const MalachScript::Parser::ParsedParamListStatement*)funcStat->GetParamList().get(); auto paramList = (const MalachScript::Parser::ParsedParamListStatement*)funcStat->GetParamList().get();
CHECK(paramList->GetParameters().size() == 2); CHECK(paramList->GetParameters().size() == 2);
auto& par1 = paramList->GetParameters()[0]; auto& par1 = *paramList->GetParameters()[0];
CHECK_FALSE(par1.GetTypeStatement()->IsConst()); CHECK_FALSE(par1.GetTypeStatement()->IsConst());
CHECK_FALSE(par1.GetTypeStatement()->IsArray()); CHECK_FALSE(par1.GetTypeStatement()->IsArray());
CHECK_FALSE(par1.GetTypeStatement()->IsHandle()); CHECK_FALSE(par1.GetTypeStatement()->IsHandle());
@ -139,7 +139,7 @@ PARSER_TEST("Parse scoped function with parameters without body.",
CHECK(par1.GetIdentifier().GetString() == u8"par1"); CHECK(par1.GetIdentifier().GetString() == u8"par1");
CHECK(par1.GetDefaultExpression() == nullptr); CHECK(par1.GetDefaultExpression() == nullptr);
auto& par2 = paramList->GetParameters()[1]; auto& par2 = *paramList->GetParameters()[1];
CHECK_FALSE(par2.GetTypeStatement()->IsConst()); CHECK_FALSE(par2.GetTypeStatement()->IsConst());
CHECK_FALSE(par2.GetTypeStatement()->IsArray()); CHECK_FALSE(par2.GetTypeStatement()->IsArray());
CHECK_FALSE(par2.GetTypeStatement()->IsHandle()); CHECK_FALSE(par2.GetTypeStatement()->IsHandle());
@ -200,7 +200,7 @@ PARSER_TEST("Parse scoped function with reference parameters without body.",
auto paramList = (const MalachScript::Parser::ParsedParamListStatement*)funcStat->GetParamList().get(); auto paramList = (const MalachScript::Parser::ParsedParamListStatement*)funcStat->GetParamList().get();
CHECK(paramList->GetParameters().size() == 2); CHECK(paramList->GetParameters().size() == 2);
auto& par1 = paramList->GetParameters()[0]; auto& par1 = *paramList->GetParameters()[0];
CHECK_FALSE(par1.GetTypeStatement()->IsConst()); CHECK_FALSE(par1.GetTypeStatement()->IsConst());
CHECK_FALSE(par1.GetTypeStatement()->IsArray()); CHECK_FALSE(par1.GetTypeStatement()->IsArray());
CHECK_FALSE(par1.GetTypeStatement()->IsHandle()); CHECK_FALSE(par1.GetTypeStatement()->IsHandle());
@ -210,7 +210,7 @@ PARSER_TEST("Parse scoped function with reference parameters without body.",
CHECK(par1.GetIdentifier().GetString() == u8"par1"); CHECK(par1.GetIdentifier().GetString() == u8"par1");
CHECK(par1.GetDefaultExpression() == nullptr); CHECK(par1.GetDefaultExpression() == nullptr);
auto& par2 = paramList->GetParameters()[1]; auto& par2 = *paramList->GetParameters()[1];
CHECK_FALSE(par2.GetTypeStatement()->IsConst()); CHECK_FALSE(par2.GetTypeStatement()->IsConst());
CHECK_FALSE(par2.GetTypeStatement()->IsArray()); CHECK_FALSE(par2.GetTypeStatement()->IsArray());
CHECK_FALSE(par2.GetTypeStatement()->IsHandle()); CHECK_FALSE(par2.GetTypeStatement()->IsHandle());