lang_2023/include/values.hpp

152 lines
3.7 KiB
C++
Raw Normal View History

#pragma once
#include <string>
#include <variant>
#include <optional>
#include <unordered_map>
// for clangd
#include "interpreter_tree.hpp"
#include "utils.hpp"
namespace info::value {
struct Unit {};
struct InternalValue {
public:
InternalValue() = default;
explicit InternalValue(std::variant<double,
long long,
std::string,
char,
bool,
Unit>&& value) : value(std::move(value)) {}
template<typename T>
std::optional<T*> GetValue() {
if (!std::holds_alternative<T>(value)) {
return std::nullopt;
}
return std::get<T>(value);
}
public:
std::variant<double,
long long,
std::string,
char,
bool,
Unit> value;
};
struct TupleValue {
public:
TupleValue() = default;
explicit TupleValue(std::vector<std::pair<std::optional<std::string>, utils::IdType>>&& fields) : fields(std::move(fields)) {}
public:
std::vector<std::pair<std::optional<std::string>, utils::IdType>> fields;
};
struct VariantValue {
public:
VariantValue() = default;
VariantValue(TupleValue value) // TODO: add type & constructor??
: value(value) {}
public:
TupleValue value;
};
struct ReferenceToValue {
public:
ReferenceToValue() = default;
ReferenceToValue(const std::vector<utils::ReferenceModifier>& references,
utils::IdType value)
: references(references), value(value) {}
public:
std::vector<utils::ReferenceModifier> references;
utils::IdType value;
};
struct FunctionValue {
public:
FunctionValue() = default;
FunctionValue(std::variant<interpreter::tokens::FunctionDeclaration*,
interpreter::tokens::LambdaFunction*> function)
: function(function) {}
public:
std::variant<interpreter::tokens::FunctionDeclaration*,
interpreter::tokens::LambdaFunction*> function;
};
struct ArrayValue {
public:
ArrayValue() = default;
explicit ArrayValue(const std::vector<utils::IdType>& elements,
bool is_constant_size)
: elements(elements), is_constant_size(is_constant_size) {}
public:
std::vector<utils::IdType> elements;
bool is_constant_size = false;
};
struct OptionalValue {
public:
OptionalValue() = default;
explicit OptionalValue(utils::IdType value) : value(value) {}
public:
std::optional<utils::IdType> value;
};
struct Value { // DefinedValue ??
std::variant<InternalValue,
TupleValue,
VariantValue,
ReferenceToValue,
FunctionValue, // ??
ArrayValue,
OptionalValue> value;
};
class ValueManager {
public:
template<typename T>
2023-05-07 20:21:19 +03:00
utils::IdType AddValue(const T& value, utils::ValueType value_type) {
values_.push_back(std::pair<Value, utils::ValueType> {value, value_type});
return values_.size() - 1;
}
2023-05-07 20:21:19 +03:00
utils::IdType AddAnyValue(Value&& value, utils::ValueType value_type) {
values_.push_back(std::pair<Value, utils::ValueType> {std::move(value), value_type});
return values_.size() - 1;
}
template<typename T>
2023-05-07 20:21:19 +03:00
std::optional<T*> GetValue(utils::IdType value_id) {
if (!std::holds_alternative<T>(values_.at(value_id).first.value)) {
return std::nullopt;
}
return &std::get<T>(values_.at(value_id).first.value);
}
2023-05-07 20:21:19 +03:00
Value* GetAnyValue(utils::IdType value_id) {
return &values_.at(value_id).first;
}
utils::ValueType GetValueType(utils::IdType value_id) {
return values_.at(value_id).second;
}
private:
std::vector<std::pair<Value, utils::ValueType>> values_;
};
} // namespace info::value