blog-static/code/compiler/13/env.hpp

40 lines
917 B
C++
Raw Permalink Normal View History

2020-09-08 18:38:05 -07:00
#pragma once
#include <memory>
#include <string>
2020-09-15 19:12:12 -07:00
class env {
public:
virtual ~env() = default;
2020-09-08 18:38:05 -07:00
2020-09-15 19:12:12 -07:00
virtual int get_offset(const std::string& name) const = 0;
virtual bool has_variable(const std::string& name) const = 0;
2020-09-08 18:38:05 -07:00
};
using env_ptr = std::shared_ptr<env>;
2020-09-15 19:12:12 -07:00
class env_var : public env {
private:
std::string name;
env_ptr parent;
2020-09-08 18:38:05 -07:00
2020-09-15 19:12:12 -07:00
public:
env_var(std::string n, env_ptr p)
: name(std::move(n)), parent(std::move(p)) {}
2020-09-08 18:38:05 -07:00
2020-09-15 19:12:12 -07:00
int get_offset(const std::string& name) const;
bool has_variable(const std::string& name) const;
2020-09-08 18:38:05 -07:00
};
2020-09-15 19:12:12 -07:00
class env_offset : public env {
private:
int offset;
env_ptr parent;
2020-09-08 18:38:05 -07:00
2020-09-15 19:12:12 -07:00
public:
env_offset(int o, env_ptr p)
: offset(o), parent(std::move(p)) {}
2020-09-08 18:38:05 -07:00
2020-09-15 19:12:12 -07:00
int get_offset(const std::string& name) const;
bool has_variable(const std::string& name) const;
2020-09-08 18:38:05 -07:00
};