Skip to content
This repository has been archived by the owner on Jun 29, 2024. It is now read-only.

Commit

Permalink
Add & fix type comparaison
Browse files Browse the repository at this point in the history
  • Loading branch information
Gashmob committed Aug 26, 2023
1 parent 98cf3fe commit fba3664
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ add_library(compiler_lib
ast/ForIter.cpp
ast/While.cpp
ast/AssignationOperator.cpp
ast/AbstractType.cpp
# === Environment ===
environment/Environment.cpp
environment/Name.cpp
Expand Down
20 changes: 17 additions & 3 deletions src/lib/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,18 @@ namespace filc::ast {

AbstractType(AbstractType &&other) = default;

auto operator=(const AbstractType &other) -> AbstractType & = default;
auto operator=(const AbstractType &other) -> AbstractType & = delete;

auto operator=(AbstractType &&other) -> AbstractType & = default;
auto operator=(AbstractType &&other) -> AbstractType & = delete;

[[nodiscard]] virtual auto dump() const -> std::string = 0;

[[nodiscard]] virtual auto getInnerType() const -> AbstractType * = 0;

[[nodiscard]] virtual auto equals(const AbstractType &other) const -> bool = 0;

protected:
AbstractType() = default;
explicit AbstractType() = default;
};

class Type : public AbstractType {
Expand All @@ -224,6 +226,8 @@ namespace filc::ast {

[[nodiscard]] auto getInnerType() const -> AbstractType * override;

[[nodiscard]] auto equals(const AbstractType &other) const -> bool override;

private:
Identifier *_name;
};
Expand All @@ -240,6 +244,8 @@ namespace filc::ast {

[[nodiscard]] auto dump() const -> std::string override;

auto equals(const AbstractType &other) const -> bool override;

private:
AbstractType *_inner_type;
unsigned int _size;
Expand All @@ -255,6 +261,8 @@ namespace filc::ast {

[[nodiscard]] auto dump() const -> std::string override;

auto equals(const AbstractType &other) const -> bool override;

private:
AbstractType *_inner_type;
};
Expand All @@ -276,6 +284,8 @@ namespace filc::ast {

[[nodiscard]] auto getCalledOn() const -> AbstractType *;

auto equals(const AbstractType &other) const -> bool override;

private:
std::vector<AbstractType *> _argument_types;
AbstractType *_return_type;
Expand Down Expand Up @@ -632,4 +642,8 @@ namespace filc::ast {
};
}

auto operator==(const filc::ast::AbstractType &type1, const filc::ast::AbstractType &type2) -> bool;

auto operator!=(const filc::ast::AbstractType &type1, const filc::ast::AbstractType &type2) -> bool;

#endif //FILC_AST_H
32 changes: 32 additions & 0 deletions src/lib/ast/AbstractType.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* MIT License
*
* Copyright (c) 2023-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 "AST.h"

auto operator==(const filc::ast::AbstractType &type1, const filc::ast::AbstractType &type2) -> bool {
return type1.equals(type2);
}

auto operator!=(const filc::ast::AbstractType &type1, const filc::ast::AbstractType &type2) -> bool {
return !(type1 == type2);
}
15 changes: 14 additions & 1 deletion src/lib/ast/ArrayType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ namespace filc::ast {
}

ArrayType::~ArrayType() {
delete _inner_type;
// delete _inner_type;
}

auto ArrayType::equals(const AbstractType &other) const -> bool {
if (dynamic_cast<const ArrayType *>(&other) == nullptr) {
return false;
}
auto other_type = dynamic_cast<const ArrayType &>(other);

if (_size != other_type._size) {
return false;
}

return *_inner_type == *other_type._inner_type;
}
}
2 changes: 1 addition & 1 deletion src/lib/ast/ClassicOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace filc::ast {
filc::ast::AbstractType *called_on,
filc::environment::Environment *environment,
filc::message::MessageCollector *collector) const -> LambdaType * {
return new LambdaType({}, return_type);
return new LambdaType({}, return_type, called_on);
}

auto ClassicOperator::dumpPostLambdaType(filc::ast::AbstractType *return_type,
Expand Down
27 changes: 23 additions & 4 deletions src/lib/ast/LambdaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ namespace filc::ast {
}

LambdaType::~LambdaType() {
for (const auto &argument_type: _argument_types) {
delete argument_type;
}
delete _return_type;
// for (const auto &argument_type: _argument_types) {
// delete argument_type;
// }
// delete _return_type;
}

auto LambdaType::getInnerType() const -> AbstractType * {
Expand All @@ -67,4 +67,23 @@ namespace filc::ast {
auto LambdaType::getCalledOn() const -> AbstractType * {
return _called_on;
}

auto LambdaType::equals(const AbstractType &other) const -> bool {
if (dynamic_cast<const LambdaType *>(&other) == nullptr) {
return false;
}
auto other_type = dynamic_cast<const LambdaType &>(other);

if (_argument_types.size() != other_type._argument_types.size()) {
return false;
}

for (unsigned int i = 0; i < _argument_types.size(); i++) {
if (*_argument_types[i] != *other_type._argument_types[i]) {
return false;
}
}

return *_return_type == *other_type._return_type && *_called_on == *other_type._called_on;
}
}
11 changes: 10 additions & 1 deletion src/lib/ast/PointerType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ namespace filc::ast {
}

PointerType::~PointerType() {
delete _inner_type;
// delete _inner_type;
}

auto PointerType::equals(const AbstractType &other) const -> bool {
if (dynamic_cast<const PointerType *>(&other) == nullptr) {
return false;
}
auto other_type = dynamic_cast<const PointerType &>(other);

return *_inner_type == *other_type._inner_type;
}
}
11 changes: 10 additions & 1 deletion src/lib/ast/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace filc::ast {
: _name(name) {}

Type::~Type() {
delete _name;
// delete _name;
}

auto Type::getName() const -> Identifier * {
Expand All @@ -42,4 +42,13 @@ namespace filc::ast {
auto Type::getInnerType() const -> AbstractType * {
return (AbstractType *) this;
}

auto Type::equals(const AbstractType &other) const -> bool {
if (dynamic_cast<const Type *>(&other) == nullptr) {
return false;
}
auto other_type = dynamic_cast<const Type &>(other);

return _name->getName() == other_type._name->getName();
}
}
3 changes: 2 additions & 1 deletion src/lib/ast/VariableDeclaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ namespace filc::ast {
return;
}

if (environment->getName("operator=", new filc::ast::LambdaType({_type, assignation_type}, _type)) == nullptr) {
if (!environment->hasName("operator=", new filc::ast::LambdaType({_type, assignation_type},
environment->getType("void")))) {
collector->addError(
new filc::message::Error(filc::message::ERROR,
"Cannot assign " + assignation_type->dump() + " to " + _type->dump(),
Expand Down
4 changes: 2 additions & 2 deletions src/lib/environment/Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace filc::environment {

auto Environment::hasName(const std::string &name, filc::ast::AbstractType *type) const -> bool {
return std::any_of(_names.begin(), _names.end(), [name, type](auto *item) -> bool {
return item->getName() == name && (type == nullptr || item->getType()->dump() == type->dump());
return item->getName() == name && (type == nullptr || *item->getType() == *type);
}) || (_parent != nullptr && _parent->hasName(name));
}

Expand All @@ -52,7 +52,7 @@ namespace filc::environment {
auto Environment::getName(const std::string &name, filc::ast::AbstractType *type) const -> Name * {
if (hasName(name, type)) {
auto result = std::find_if(_names.begin(), _names.end(), [name, type](auto *item) -> bool {
return item->getName() == name && (type == nullptr || item->getType()->dump() == type->dump());
return item->getName() == name && (type == nullptr || *item->getType() == *type);
});

if (result == _names.end()) {
Expand Down

0 comments on commit fba3664

Please sign in to comment.