mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-06 06:58:45 +00:00
type_check_visitor first iteration, value, execution_visitor started
This commit is contained in:
parent
173d50672a
commit
890bd90eba
22 changed files with 481 additions and 452 deletions
143
include/values.hpp
Normal file
143
include/values.hpp
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
#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
|
||||
Loading…
Add table
Add a link
Reference in a new issue