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

View File

@ -105,16 +105,23 @@ namespace MalachScript::Parser {
public:
class ParsedParameter {
private:
const ParsedTypeStatement* _typeStatement = nullptr;
std::unique_ptr<const ParsedTypeStatement> _typeStatement = nullptr;
TypeMod _typeMod = TypeMod::None;
Identifier _identifier;
const ParsedExpression* _defaultExpression = nullptr;
std::unique_ptr<const ParsedExpression> _defaultExpression = nullptr;
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]] const ParsedTypeStatement* GetTypeStatement() const noexcept { return _typeStatement; }
[[nodiscard]] std::unique_ptr<const ParsedTypeStatement>& GetTypeStatement() noexcept {
return _typeStatement;
}
[[nodiscard]] const std::unique_ptr<const ParsedTypeStatement>& GetTypeStatement() const noexcept {
return _typeStatement;
}
[[nodiscard]] TypeMod& GetTypeMod() 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]] const Identifier& GetIdentifier() const noexcept { return _identifier; }
[[nodiscard]] const ParsedExpression*& GetDefaultExpression() noexcept { return _defaultExpression; }
[[nodiscard]] const ParsedExpression* GetDefaultExpression() const noexcept { return _defaultExpression; }
[[nodiscard]] std::unique_ptr<const ParsedExpression>& GetDefaultExpression() noexcept {
return _defaultExpression;
}
[[nodiscard]] const std::unique_ptr<const ParsedExpression>& GetDefaultExpression() const noexcept {
return _defaultExpression;
}
};
private:
std::vector<ParsedParameter> _parameters;
std::vector<std::unique_ptr<const ParsedParameter>> _parameters;
public:
ParsedParamListStatement(TextSpan span, std::vector<ParsedParameter> parameters)
: ParsedStatementImpl<ParsedStatementKind::ParamList>(span), _parameters(std::move(parameters)){};
ParsedParamListStatement(TextSpan span, const std::vector<const ParsedParameter*>& 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> {
@ -145,7 +162,7 @@ namespace MalachScript::Parser {
bool _returnsReference;
Identifier _identifier;
std::unique_ptr<const ParsedStatement> _paramList;
bool isConst;
bool _isConst;
FuncAttr _funcAttr;
std::unique_ptr<const ParsedStatement> _statBlock;
@ -156,7 +173,7 @@ namespace MalachScript::Parser {
const ParsedStatement* statBlock)
: ParsedStatementImpl<ParsedStatementKind::Func>(span), _isShared(isShared), _isExternal(isExternal),
_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 IsExternal() const noexcept { return _isExternal; }
@ -169,7 +186,7 @@ namespace MalachScript::Parser {
[[nodiscard]] inline const std::unique_ptr<const ParsedStatement>& GetParamList() const noexcept {
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 const std::unique_ptr<const ParsedStatement>& GetStatBlock() const noexcept {
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();
CHECK(paramList->GetParameters().size() == 2);
auto& par1 = paramList->GetParameters()[0];
auto& par1 = *paramList->GetParameters()[0];
CHECK_FALSE(par1.GetTypeStatement()->IsConst());
CHECK_FALSE(par1.GetTypeStatement()->IsArray());
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.GetDefaultExpression() == nullptr);
auto& par2 = paramList->GetParameters()[1];
auto& par2 = *paramList->GetParameters()[1];
CHECK_FALSE(par2.GetTypeStatement()->IsConst());
CHECK_FALSE(par2.GetTypeStatement()->IsArray());
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();
CHECK(paramList->GetParameters().size() == 2);
auto& par1 = paramList->GetParameters()[0];
auto& par1 = *paramList->GetParameters()[0];
CHECK_FALSE(par1.GetTypeStatement()->IsConst());
CHECK_FALSE(par1.GetTypeStatement()->IsArray());
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.GetDefaultExpression() == nullptr);
auto& par2 = paramList->GetParameters()[1];
auto& par2 = *paramList->GetParameters()[1];
CHECK_FALSE(par2.GetTypeStatement()->IsConst());
CHECK_FALSE(par2.GetTypeStatement()->IsArray());
CHECK_FALSE(par2.GetTypeStatement()->IsHandle());