The complete source code of IdealIRC http://www.idealirc.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
idealirc/Script/SymbolScope.h

86 lines
2.1 KiB

/*
* IdealIRC Script Engine - Scripting tailored for IRC clients.
* Copyright (C) 2021 Tom-Andre Barstad.
* This software is licensed under the Software Attribution License.
* See LICENSE for more information.
*/
#ifndef SYMBOLSCOPE_H
#define SYMBOLSCOPE_H
#include "ValueHolder.h"
#include "Tokens.h"
#include <variant>
#include <unordered_map>
#include <optional>
#include <functional>
class Script;
using ArrayMap = std::unordered_map<std::string, ValueHolder>;
using ScopeCRef = std::reference_wrapper<const Scope>;
using BuiltinFn = std::function<ValueHolder(Script&, std::vector<ValueHolder>&)>;
class Symbol
{
public:
enum class Type {
Value,
Scope,
Builtin
};
Symbol() = delete;
virtual ~Symbol() = default;
Symbol(const Scope& scope);
Symbol(const ValueHolder& data);
Symbol(const BuiltinFn& fn);
Type getType() const;
ValueHolder& getValue();
const ValueHolder& getValue() const;
const Scope& getScope() const;
ValueHolder runBuiltin(Script& script, std::vector<ValueHolder>& params);
private:
std::variant<ValueHolder, ScopeCRef, BuiltinFn> m_data;
Type m_type;
};
using SymbolRef = std::reference_wrapper<Symbol>;
using SymbolCRef = std::reference_wrapper<const Symbol>;
class SymbolScope
{
public:
SymbolScope() = default;
~SymbolScope() = default;
SymbolScope(SymbolScope&& other);
SymbolScope(const SymbolScope&) = delete;
SymbolScope(SymbolScope* parent);
SymbolScope& operator=(SymbolScope&) = delete;
void setParent(SymbolScope& parent);
void set(const std::string& name, const ValueHolder& value);
void set(const std::string& name, ValueHolder&& value);
void set(const std::string& name, Scope& scope);
void set(const std::string& name, const BuiltinFn& fn);
std::optional<SymbolRef> get(const std::string& name);
std::optional<SymbolCRef> cget(const std::string& name) const;
void clear();
void dump(bool deep = false) const;
bool hasValues() const;
private:
std::unordered_map<std::string, Symbol> m_symbols;
SymbolScope* m_parent{ nullptr };
};
#endif // SYMBOLSCOPE_H