mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
fixes, builtin function fixes, deep copy, string access
This commit is contained in:
parent
3af0772da6
commit
9aaac90ef6
13 changed files with 450 additions and 152 deletions
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <optional>
|
||||
|
|
@ -32,16 +33,19 @@ public:
|
|||
|
||||
bool Same(const InternalValue& value) const;
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name) const;
|
||||
|
||||
utils::IdType DeepCopy(ValueManager* value_manager);
|
||||
|
||||
public:
|
||||
std::variant<double,
|
||||
long long,
|
||||
int64_t,
|
||||
std::string,
|
||||
char,
|
||||
bool,
|
||||
Unit> value;
|
||||
};
|
||||
|
||||
struct TupleValue {
|
||||
struct TupleValue { /// TODO: no need to store strings (only store associated type) ??
|
||||
public:
|
||||
TupleValue() = default;
|
||||
|
||||
|
|
@ -51,6 +55,9 @@ public:
|
|||
|
||||
bool Same(const TupleValue& other_value) const;
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name) const;
|
||||
|
||||
utils::IdType DeepCopy(ValueManager* value_manager); // TODO
|
||||
|
||||
public:
|
||||
std::vector<std::pair<std::optional<std::string>, utils::IdType>> fields;
|
||||
|
||||
|
|
@ -65,12 +72,14 @@ public:
|
|||
VariantValue(TupleValue&& value, size_t current_constructor)
|
||||
: value(std::move(value)), current_constructor(current_constructor) {}
|
||||
|
||||
bool Same(const VariantValue& other_value) const;
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name) const;
|
||||
|
||||
utils::IdType DeepCopy(ValueManager* value_manager);
|
||||
|
||||
public:
|
||||
TupleValue value;
|
||||
size_t current_constructor;
|
||||
|
||||
bool Same(const VariantValue& other_value) const;
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name) const;
|
||||
};
|
||||
|
||||
struct ReferenceToValue {
|
||||
|
|
@ -82,13 +91,15 @@ public:
|
|||
ValueManager* value_manager)
|
||||
: references(references), value(value), value_manager_(value_manager) {}
|
||||
|
||||
bool Same(const ReferenceToValue& other_value) const;
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name) const;
|
||||
|
||||
utils::IdType DeepCopy(ValueManager* value_manager);
|
||||
|
||||
public:
|
||||
std::vector<utils::ReferenceModifier> references;
|
||||
utils::IdType value;
|
||||
|
||||
bool Same(const ReferenceToValue& other_value) const;
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name) const;
|
||||
|
||||
private:
|
||||
ValueManager* value_manager_ = nullptr;
|
||||
};
|
||||
|
|
@ -101,13 +112,15 @@ public:
|
|||
FunctionValue(const T& function, ValueManager* value_manager)
|
||||
: function(function), value_manager_(value_manager) {}
|
||||
|
||||
bool Same(const FunctionValue& other_value) const;
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name) const;
|
||||
|
||||
utils::IdType DeepCopy(ValueManager* value_manager);
|
||||
|
||||
public:
|
||||
std::variant<interpreter::tokens::FunctionDeclaration*,
|
||||
interpreter::tokens::LambdaFunction*> function;
|
||||
|
||||
bool Same(const FunctionValue& other_value) const;
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name) const;
|
||||
|
||||
private:
|
||||
ValueManager* value_manager_ = nullptr;
|
||||
};
|
||||
|
|
@ -122,13 +135,15 @@ public:
|
|||
ValueManager* value_manager)
|
||||
: elements(std::move(elements)), is_constant_size(is_constant_size), value_manager_(value_manager) {}
|
||||
|
||||
bool Same(const ArrayValue& other_value) const;
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name) const;
|
||||
|
||||
utils::IdType DeepCopy(ValueManager* value_manager);
|
||||
|
||||
public:
|
||||
std::vector<utils::IdType> elements;
|
||||
bool is_constant_size = false;
|
||||
|
||||
bool Same(const ArrayValue& other_value) const;
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name) const;
|
||||
|
||||
private:
|
||||
ValueManager* value_manager_ = nullptr;
|
||||
};
|
||||
|
|
@ -143,6 +158,8 @@ public:
|
|||
bool Same(const OptionalValue& other_value) const;
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name) const;
|
||||
|
||||
utils::IdType DeepCopy(ValueManager* value_manager);
|
||||
|
||||
public:
|
||||
std::optional<utils::IdType> value;
|
||||
|
||||
|
|
@ -160,6 +177,8 @@ public:
|
|||
bool Same(const Value& other_value) const;
|
||||
std::optional<utils::IdType> GetFieldValue(const std::string& name) const;
|
||||
|
||||
utils::IdType DeepCopy(ValueManager* value_manager);
|
||||
|
||||
public:
|
||||
std::variant<InternalValue,
|
||||
TupleValue,
|
||||
|
|
@ -173,16 +192,25 @@ public:
|
|||
class ValueManager {
|
||||
public:
|
||||
template<typename T>
|
||||
utils::IdType AddValue(const T& value, utils::ValueType value_type) {
|
||||
utils::IdType ExplicitAddValue(const T& value, utils::ValueType value_type) {
|
||||
values_.push_back(std::pair<Value, utils::ValueType> {Value(value), value_type});
|
||||
return values_.size() - 1;
|
||||
}
|
||||
|
||||
utils::IdType AddAnyValue(Value&& value, utils::ValueType value_type) {
|
||||
utils::IdType ExplicitAddAnyValue(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>
|
||||
utils::IdType AddValue(const T& value, utils::ValueType value_type) {
|
||||
return ExplicitAddValue(std::move(value), value_type);
|
||||
}
|
||||
|
||||
utils::IdType AddAnyValue(Value&& value, utils::ValueType value_type) {
|
||||
return ExplicitAddAnyValue(std::move(value), value_type);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T*> GetValue(utils::IdType value_id) {
|
||||
if (!std::holds_alternative<T>(values_.at(value_id).first.value)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue