Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Validation of AST #26

Merged
merged 14 commits into from
Sep 22, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ coverage.info
.idea
FilLexer.tokens
node_modules
vgcore.*
4 changes: 3 additions & 1 deletion include/filc/filc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@
#include "filc/grammar/Parser.h"
#include "filc/options/OptionsParser.h"
#include "filc/grammar/DumpVisitor.h"
#include "filc/validation/ValidationVisitor.h"

namespace filc {
class FilCompiler final {
public:
FilCompiler(OptionsParser options_parser, DumpVisitor ast_dump_visitor);
FilCompiler(OptionsParser options_parser, DumpVisitor ast_dump_visitor, ValidationVisitor validation_visitor);

auto run(int argc, char **argv) -> int;

private:
OptionsParser _options_parser;
DumpVisitor _ast_dump_visitor;
ValidationVisitor _validation_visitor;
};
} // namespace filc

Expand Down
55 changes: 55 additions & 0 deletions include/filc/grammar/Position.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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_POSITION_H
#define FILC_POSITION_H

#include "antlr4-runtime.h"
#include <utility>
#include <vector>

namespace filc {
class Position {
public:
Position();

Position(const antlr4::Token* start_token, const antlr4::Token* end_token);

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

[[nodiscard]] auto getStartPosition() const -> std::pair<unsigned int, unsigned int>;

[[nodiscard]] auto getEndPosition() const -> std::pair<unsigned int, unsigned int>;

[[nodiscard]] auto getContent() const -> std::vector<std::string>;

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

private:
std::string _filename;
std::pair<unsigned int, unsigned int> _start_position;
std::pair<unsigned int, unsigned int> _end_position;
};
}

#endif // FILC_POSITION_H
90 changes: 90 additions & 0 deletions include/filc/grammar/Type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* 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;

[[nodiscard]] virtual auto toDisplay() 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;

[[nodiscard]] auto toDisplay() 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;

[[nodiscard]] auto toDisplay() 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;

[[nodiscard]] auto toDisplay() 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
14 changes: 14 additions & 0 deletions include/filc/grammar/expression/Expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,29 @@

#include "filc/grammar/ast.h"
#include "filc/grammar/Visitor.h"
#include "filc/grammar/Position.h"
#include "filc/grammar/Type.h"
#include <string>

namespace filc {
class Expression: public Visitable {
public:
virtual ~Expression() = default;

auto setPosition(const Position& position) -> void;

[[nodiscard]] auto getPosition() const -> const Position&;

auto setType(const std::shared_ptr<AbstractType> &type) -> void;

[[nodiscard]] auto getType() const -> const std::shared_ptr<AbstractType>&;

protected:
Expression();

private:
Position _position;
std::shared_ptr<AbstractType> _type;
};
}

Expand Down
52 changes: 52 additions & 0 deletions include/filc/utils/Message.h
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.
*/
#ifndef FILC_MESSAGE_H
#define FILC_MESSAGE_H

#include "filc/grammar/Position.h"
#include <string>

#define WARNING "WARNING"
#define WARNING_COLOR "\033[33m"
#define ERROR "ERROR"
#define ERROR_COLOR "\033[31m"

namespace filc {
class Message final {
public:
Message(std::string tag, std::string message, Position position, std::string color);

auto write(std::ostream &out) const -> std::ostream &;

private:
std::string _tag;
std::string _message;
Position _position;
std::string _color;
};
}

auto operator<<(std::ostream &out, const filc::Message &message) -> std::ostream&;

#endif // FILC_MESSAGE_H
46 changes: 46 additions & 0 deletions include/filc/validation/CalculValidator.h
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
55 changes: 55 additions & 0 deletions include/filc/validation/Environment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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 "filc/validation/Name.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;

[[nodiscard]] auto hasName(const std::string &name) const -> bool;

[[nodiscard]] auto getName(const std::string &name) const -> const Name&;

auto addName(const Name &name) -> void;

private:
std::map<std::string, std::shared_ptr<AbstractType>> _types;
std::map<std::string, Name> _names;
};
}

#endif // FILC_ENVIRONMENT_H
Loading