123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #ifndef QLOW_SEM_SCOPE_H
- #define QLOW_SEM_SCOPE_H
- #include <optional>
- #include <map>
- #include <memory>
- #include "Type.h"
- namespace qlow
- {
- namespace sem
- {
- /*!
- * \note contains owning pointers to elements
- */
- template<typename T>
- using SymbolTable = std::map<std::string, std::unique_ptr<T>>;
- struct Class;
- struct Method;
- struct Variable;
- class Scope;
- class GlobalScope;
- class ClassScope;
- class LocalScope;
- }
- }
- class qlow::sem::Scope
- {
- public:
- virtual ~Scope(void);
- virtual Variable* getVariable(const std::string& name) = 0;
- virtual Method* getMethod(const std::string& name) = 0;
- virtual std::optional<Type> getType(const std::string& name) = 0;
- virtual std::optional<Type> getReturnableType(void) = 0;
- virtual std::string toString(void) = 0;
- };
- class qlow::sem::GlobalScope : public Scope
- {
- public:
- SymbolTable<Class> classes;
- public:
- virtual Variable* getVariable(const std::string& name);
- virtual Method* getMethod(const std::string& name);
- virtual std::optional<Type> getType(const std::string& name);
- virtual std::optional<Type> getReturnableType(void);
- virtual std::string toString(void);
- };
- class qlow::sem::ClassScope : public Scope
- {
- Scope& parentScope;
- Class* class_ref;
- public:
- inline ClassScope(Scope& parentScope, Class* class_ref) :
- parentScope{ parentScope }, class_ref{ class_ref }
- {
- }
- virtual Variable* getVariable(const std::string& name);
- virtual Method* getMethod(const std::string& name);
- virtual std::optional<Type> getType(const std::string& name);
- virtual std::optional<Type> getReturnableType(void);
- virtual std::string toString(void);
- };
- class qlow::sem::LocalScope : public Scope
- {
- Scope& parentScope;
- SymbolTable<Variable> localVariables;
- Type returnType;
- public:
- inline LocalScope(Scope& parentScope, const Type& returnType) :
- parentScope{ parentScope }, returnType{ returnType }
- {}
- inline LocalScope(Scope& parentScope) :
- parentScope{ parentScope }
- {
- std::optional<Type> returnable = parentScope.getReturnableType();
- if (returnable) {
- returnType = returnable.value();
- }
- else {
- returnType = Type(Type::Kind::NULL_TYPE);
- }
- }
- void putVariable(const std::string& name, std::unique_ptr<Variable> v);
- SymbolTable<Variable>& getLocals(void);
- virtual Variable* getVariable(const std::string& name);
- virtual Method* getMethod(const std::string& name);
- virtual std::optional<Type> getType(const std::string& name);
- virtual std::optional<Type> getReturnableType(void);
- virtual std::string toString(void);
- };
- #endif // QLOW_SEM_SCOPE_H
|