-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of #25 - check left operand - check right operand - check calcul can be done between types Also add early return when type given by sub-expression is nullptr: it means there is an error, so no need to go further in this expression
- Loading branch information
Showing
8 changed files
with
219 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ coverage.info | |
.idea | ||
FilLexer.tokens | ||
node_modules | ||
vgcore.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* MIT License | ||
* | ||
* Copyright (c) 2024-Present Kevin Traini | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
#ifndef FILC_CALCULVALIDATOR_H | ||
#define FILC_CALCULVALIDATOR_H | ||
|
||
#include "filc/grammar/Type.h" | ||
#include <memory> | ||
#include <string> | ||
|
||
namespace filc { | ||
class CalculValidator { | ||
public: | ||
[[nodiscard]] static auto isCalculValid(const std::shared_ptr<AbstractType> &left_type, const std::string &op, | ||
const std::shared_ptr<AbstractType> &right_type) -> bool; | ||
|
||
private: | ||
[[nodiscard]] static auto isNumericOperatorValid(const std::string &op) -> bool; | ||
|
||
[[nodiscard]] static auto isBoolOperatorValid(const std::string &op) -> bool; | ||
|
||
[[nodiscard]] static auto isPointerOperatorValid(const std::string &op) -> bool; | ||
}; | ||
} | ||
|
||
#endif // FILC_CALCULVALIDATOR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* MIT License | ||
* | ||
* Copyright (c) 2024-Present Kevin Traini | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
#include "filc/validation/CalculValidator.h" | ||
#include <algorithm> | ||
#include <vector> | ||
|
||
using namespace filc; | ||
|
||
auto CalculValidator::isCalculValid(const std::shared_ptr<AbstractType> &left_type, const std::string &op, | ||
const std::shared_ptr<AbstractType> &right_type) -> bool { | ||
if (left_type != right_type) { | ||
return false; | ||
} | ||
const auto type = left_type->getName(); | ||
|
||
const std::vector<std::string> numeric_type = { | ||
"i8", "i16", "i32", "i64", "i128", "u8", "u16", "u32", "u64", "u128", "f32", "f64", | ||
}; | ||
if (std::find(numeric_type.begin(), numeric_type.end(), type) != numeric_type.end()) { | ||
return isNumericOperatorValid(op); | ||
} | ||
|
||
if (type == "bool") { | ||
return isBoolOperatorValid(op); | ||
} | ||
|
||
if (type[type.length() - 1] == '*') { // A pointer | ||
return isPointerOperatorValid(op); | ||
} | ||
|
||
// We don't know what it is, so we assert it cannot be done | ||
return false; | ||
} | ||
|
||
auto CalculValidator::isNumericOperatorValid(const std::string &op) -> bool { | ||
const std::vector<std::string> valid_op = { | ||
"%", "+", "-", "/", "*", "<", "<=", ">", ">=", "==", "!=", | ||
}; | ||
return std::find(valid_op.begin(), valid_op.end(), op) != valid_op.end(); | ||
} | ||
|
||
auto CalculValidator::isBoolOperatorValid(const std::string &op) -> bool { | ||
return op == "&&" || op == "||" || op == "==" || op == "!="; | ||
} | ||
|
||
auto CalculValidator::isPointerOperatorValid(const std::string &op) -> bool { return op == "==" || op == "!="; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* MIT License | ||
* | ||
* Copyright (c) 2024-Present Kevin Traini | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
#include <filc/grammar/Type.h> | ||
#include <filc/validation/CalculValidator.h> | ||
#include <gtest/gtest.h> | ||
#include <memory> | ||
|
||
TEST(CalculValidator, invalidDifferentType) { | ||
ASSERT_FALSE( | ||
filc::CalculValidator::isCalculValid(std::make_shared<filc::Type>("a"), "", std::make_shared<filc::Type>("b"))); | ||
} | ||
|
||
TEST(CalculValidator, validNumeric) { | ||
ASSERT_TRUE(filc::CalculValidator::isCalculValid(std::make_shared<filc::Type>("i32"), "+", | ||
std::make_shared<filc::Type>("i32"))); | ||
} | ||
|
||
TEST(CalculValidator, validBool) { | ||
ASSERT_TRUE(filc::CalculValidator::isCalculValid(std::make_shared<filc::Type>("bool"), "&&", | ||
std::make_shared<filc::Type>("bool"))); | ||
} | ||
|
||
TEST(CalculValidator, validPointer) { | ||
ASSERT_TRUE(filc::CalculValidator::isCalculValid(std::make_shared<filc::Type>("i32*"), | ||
"==", std::make_shared<filc::Type>("i32*"))); | ||
} | ||
|
||
TEST(CalculValidator, invalidUnknown) { | ||
ASSERT_FALSE(filc::CalculValidator::isCalculValid(std::make_shared<filc::Type>("foo"), "+", | ||
std::make_shared<filc::Type>("foo"))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters