Skip to content

Commit

Permalink
[compiler]: G-Machine environment implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
muqiuhan committed Feb 3, 2024
1 parent e9312f9 commit 7c00b5e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
50 changes: 49 additions & 1 deletion compiler/g-machine/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,54 @@
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "environment.hpp"
#include "optional/optional.hpp"
#include "panic/panic.hpp"

namespace swallow::compiler::gmachine
{} // namespace swallow::compiler::gmachine
{

[[nodiscard]] auto Variable::getOffset(const std::string &name) const noexcept
-> tl::optional<int>
{
if (name == Name)
return tl::make_optional(0);

if (Parent != nullptr)
return Parent->getOffset(name).map(
[](const auto &offset) { return offset + 1; });

utils::panic("Get variable {} offset failed", name);
}

[[nodiscard]] auto
Variable::hasVariable(const std::string &name) const noexcept -> bool
{
if (name == Name)
return true;

if (Parent != nullptr)
return Parent->hasVariable(name);

return false;
}

[[nodiscard]] auto Offset::hasVariable(const std::string &name) const noexcept
-> bool
{
if (Parent != nullptr)
return Parent->hasVariable(name);

return false;
}

[[nodiscard]] auto Offset::getOffset(const std::string &name) const noexcept
-> tl::optional<int>
{
if (Parent != nullptr)
return Parent->getOffset(name).map(
[&](const auto &offset) { return offset + Value; });

utils::panic("Get variable {} offset failed, the Parent == nullptr", name);
}

} // namespace swallow::compiler::gmachine
19 changes: 10 additions & 9 deletions compiler/g-machine/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#ifndef SWALLOW_COMPILER_G_MACHINE_ENVIRONMENT_HPP
#define SWALLOW_COMPILER_G_MACHINE_ENVIRONMENT_HPP

#include "optional/optional.hpp"
#include <memory>
#include <string>

Expand All @@ -43,11 +44,11 @@ namespace swallow::compiler::gmachine

virtual ~Environment() = default;

[[nodiscard]] virtual auto
get_offset(const std::string &name) const noexcept -> int = 0;
[[nodiscard]] virtual auto getOffset(const std::string &name) const noexcept
-> tl::optional<int> = 0;

[[nodiscard]] virtual auto
has_variable(const std::string &name) const noexcept -> bool = 0;
hasVariable(const std::string &name) const noexcept -> bool = 0;
};

class Variable : public Environment
Expand All @@ -60,10 +61,10 @@ namespace swallow::compiler::gmachine
: Name(Name), Parent(std::move(Parent))
{}

[[nodiscard]] auto get_offset(const std::string &name) const noexcept
-> int override;
[[nodiscard]] auto getOffset(const std::string &name) const noexcept
-> tl::optional<int> override;

[[nodiscard]] auto has_variable(const std::string &name) const noexcept
[[nodiscard]] auto hasVariable(const std::string &name) const noexcept
-> bool override;
};

Expand All @@ -77,10 +78,10 @@ namespace swallow::compiler::gmachine
: Value(Value), Parent(std::move(Parent))
{}

[[nodiscard]] auto get_offset(const std::string &name) const noexcept
-> int override;
[[nodiscard]] auto getOffset(const std::string &name) const noexcept
-> tl::optional<int> override;

[[nodiscard]] auto has_variable(const std::string &name) const noexcept
[[nodiscard]] auto hasVariable(const std::string &name) const noexcept
-> bool override;
};

Expand Down

0 comments on commit 7c00b5e

Please sign in to comment.