-
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.
feat: ValidationVisitor has now types
Part of #25 It set type of expressions (just literals for now) and check that last expression is an int (return type of program)
- Loading branch information
Showing
21 changed files
with
457 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* 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_TYPE_H | ||
#define FILC_TYPE_H | ||
|
||
#include <string> | ||
#include <memory> | ||
|
||
namespace filc { | ||
class AbstractType { | ||
public: | ||
[[nodiscard]] virtual auto getName() const noexcept -> std::string = 0; | ||
|
||
[[nodiscard]] virtual auto getDisplayName() const noexcept -> std::string = 0; | ||
|
||
protected: | ||
AbstractType() = default; | ||
}; | ||
|
||
class Type final: public AbstractType { | ||
public: | ||
explicit Type(std::string name); | ||
|
||
[[nodiscard]] auto getName() const noexcept -> std::string override; | ||
|
||
[[nodiscard]] auto getDisplayName() const noexcept -> std::string override; | ||
|
||
private: | ||
std::string _name; | ||
}; | ||
|
||
class PointerType final: public AbstractType { | ||
public: | ||
explicit PointerType(std::shared_ptr<AbstractType> pointed_type); | ||
|
||
[[nodiscard]] auto getName() const noexcept -> std::string override; | ||
|
||
[[nodiscard]] auto getDisplayName() const noexcept -> std::string override; | ||
|
||
private: | ||
std::shared_ptr<AbstractType> _pointed_type; | ||
}; | ||
|
||
class AliasType final: public AbstractType { | ||
public: | ||
AliasType(std::string name, std::shared_ptr<AbstractType> aliased_type); | ||
|
||
[[nodiscard]] auto getName() const noexcept -> std::string override; | ||
|
||
[[nodiscard]] auto getDisplayName() const noexcept -> std::string override; | ||
|
||
private: | ||
std::string _name; | ||
std::shared_ptr<AbstractType> _aliased_type; | ||
}; | ||
} | ||
|
||
auto operator==(const std::shared_ptr<filc::AbstractType> &a, const std::shared_ptr<filc::AbstractType> &b) -> bool; | ||
auto operator!=(const std::shared_ptr<filc::AbstractType> &a, const std::shared_ptr<filc::AbstractType> &b) -> bool; | ||
|
||
#endif // FILC_TYPE_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
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,47 @@ | ||
/** | ||
* 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_ENVIRONMENT_H | ||
#define FILC_ENVIRONMENT_H | ||
|
||
#include "filc/grammar/Type.h" | ||
#include <map> | ||
#include <string> | ||
|
||
namespace filc { | ||
class Environment { | ||
public: | ||
Environment(); | ||
|
||
[[nodiscard]] auto hasType(const std::string &name) const -> bool; | ||
|
||
[[nodiscard]] auto getType(const std::string &name) const -> const std::shared_ptr<AbstractType> &; | ||
|
||
auto addType(const std::shared_ptr<AbstractType> &type) -> void; | ||
|
||
private: | ||
std::map<std::string, std::shared_ptr<AbstractType>> _types; | ||
}; | ||
} | ||
|
||
#endif // FILC_ENVIRONMENT_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,54 @@ | ||
/** | ||
* 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 <utility> | ||
|
||
using namespace filc; | ||
|
||
Type::Type(std::string name) : _name(std::move(name)) {} | ||
|
||
std::string Type::getName() const noexcept { return _name; } | ||
|
||
std::string Type::getDisplayName() const noexcept { return getName(); } | ||
|
||
PointerType::PointerType(std::shared_ptr<AbstractType> pointed_type) : _pointed_type(std::move(pointed_type)) {} | ||
|
||
auto PointerType::getName() const noexcept -> std::string { return _pointed_type->getName() + "*"; } | ||
|
||
auto PointerType::getDisplayName() const noexcept -> std::string { return _pointed_type->getDisplayName() + "*"; } | ||
|
||
AliasType::AliasType(std::string name, std::shared_ptr<AbstractType> aliased_type) | ||
: _name(std::move(name)), _aliased_type(std::move(aliased_type)) {} | ||
|
||
auto AliasType::getName() const noexcept -> std::string { return _aliased_type->getName(); } | ||
|
||
auto AliasType::getDisplayName() const noexcept -> std::string { return _name; } | ||
|
||
auto operator==(const std::shared_ptr<AbstractType> &a, const std::shared_ptr<AbstractType> &b) -> bool { | ||
return a->getName() == b->getName(); | ||
} | ||
|
||
auto operator!=(const std::shared_ptr<AbstractType> &a, const std::shared_ptr<AbstractType> &b) -> bool { | ||
return !(a == b); | ||
} |
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/Environment.h" | ||
#include <stdexcept> | ||
|
||
using namespace filc; | ||
|
||
Environment::Environment() { | ||
addType(std::make_shared<Type>("i8")); | ||
addType(std::make_shared<Type>("i16")); | ||
addType(std::make_shared<Type>("i32")); | ||
addType(std::make_shared<Type>("i64")); | ||
addType(std::make_shared<Type>("i128")); | ||
addType(std::make_shared<AliasType>("int", getType("i32"))); | ||
|
||
addType(std::make_shared<Type>("u8")); | ||
addType(std::make_shared<Type>("u16")); | ||
addType(std::make_shared<Type>("u32")); | ||
addType(std::make_shared<Type>("u64")); | ||
addType(std::make_shared<Type>("u128")); | ||
addType(std::make_shared<AliasType>("uint", getType("u32"))); | ||
|
||
addType(std::make_shared<Type>("f32")); | ||
addType(std::make_shared<Type>("f64")); | ||
|
||
addType(std::make_shared<Type>("bool")); | ||
|
||
addType(std::make_shared<AliasType>("char", getType("u8"))); | ||
addType(std::make_shared<PointerType>(getType("char"))); | ||
} | ||
|
||
auto Environment::hasType(const std::string &name) const -> bool { return _types.find(name) != _types.end(); } | ||
|
||
auto Environment::getType(const std::string &name) const -> const std::shared_ptr<AbstractType> & { | ||
if (!hasType(name)) { | ||
throw std::logic_error("Environment doesn't have type " + name); | ||
} | ||
return _types.at(name); | ||
} | ||
|
||
auto Environment::addType(const std::shared_ptr<AbstractType> &type) -> void { | ||
if (hasType(type->getDisplayName())) { | ||
throw std::logic_error("Environment already have type " + type->getDisplayName() + " aka " + type->getName()); | ||
} | ||
_types[type->getDisplayName()] = type; | ||
} |
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
Oops, something went wrong.