mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
143 lines
3.3 KiB
C++
143 lines
3.3 KiB
C++
#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;
|
|
InternalValue(std::variant<double,
|
|
long long,
|
|
std::string,
|
|
char,
|
|
bool,
|
|
Unit>&& value) : value(std::move(value)) {}
|
|
|
|
public:
|
|
std::variant<double,
|
|
long long,
|
|
std::string,
|
|
char,
|
|
bool,
|
|
Unit> value;
|
|
};
|
|
|
|
struct TupleValue {
|
|
public:
|
|
TupleValue() = default;
|
|
TupleValue(std::unordered_map<std::string, utils::IdType>&& fields) : fields(fields) {}
|
|
|
|
public:
|
|
std::unordered_map<std::string, utils::IdType> fields;
|
|
};
|
|
|
|
struct VariantValue {
|
|
public:
|
|
VariantValue() = default;
|
|
VariantValue(size_t constructor, TupleValue value)
|
|
: constructor(constructor), value(value) {}
|
|
|
|
public:
|
|
size_t constructor;
|
|
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;
|
|
ArrayValue(const std::vector<utils::IdType>& elements)
|
|
: elements(elements) {}
|
|
|
|
public:
|
|
std::vector<utils::IdType> elements;
|
|
};
|
|
|
|
struct OptionalValue {
|
|
public:
|
|
OptionalValue() = default;
|
|
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>
|
|
utils::IdType AddType(const T& value, utils::ValueType value_type) {
|
|
values_.push_back(std::pair<Value, utils::ValueType> {value, value_type});
|
|
return values_.size() - 1;
|
|
}
|
|
|
|
utils::IdType AddAnyType(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>
|
|
std::optional<T*> GetType(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);
|
|
}
|
|
|
|
Value* GetAnyType(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
|