fixes, builtin function fixes, deep copy, string access

This commit is contained in:
ProgramSnail 2023-05-22 16:03:50 +03:00
parent 3af0772da6
commit 9aaac90ef6
13 changed files with 450 additions and 152 deletions

View file

@ -164,8 +164,8 @@ private:
bool HandleBuiltinFunctionCall(FunctionCallExpression* node);
bool HandleBuiltinTypeclassFunctionCall(FunctionCallExpression* node);
bool HandleBuiltinTypeFunctionCall(FunctionCallExpression* node,
info::type::InternalType type);
private:
info::GlobalInfo& global_info_;
info::TypeclassGraph& typeclass_graph_;

View file

@ -506,6 +506,8 @@ struct AccessExpression {
std::unique_ptr<NameExpression> name;
SubExpressionToken id;
bool is_string_access_ = false;
};
// Other Expressions -----------------

View file

@ -198,8 +198,8 @@ private:
utils::IdType abstract_type = context_manager_.AddValue(
info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
typeclass_graph_.GetVertex(graph_id).name,
requirement_graph_ids),
graph_id,
typeclass_graph_),
utils::ValueType::Tmp);
for (auto& requirement_graph_id : requirement_graph_ids) {

View file

@ -9,6 +9,7 @@
// for clangd
#include "error_handling.hpp"
#include "typeclass_graph.hpp"
#include "utils.hpp"
namespace info::type {
@ -19,23 +20,12 @@ class TypeManager;
class AbstractType { // latter will be found in context
public:
AbstractType() = default;
AbstractType(utils::AbstractTypeModifier modifier,
const std::string& name,
const std::vector<utils::IdType>& requirement_graph_ids)
utils::IdType graph_id,
info::TypeclassGraph& typeclass_graph)
: modifier_(modifier),
name_(name) {
for (auto& requirement_graph_id : requirement_graph_ids) {
requirement_graph_ids_.insert(requirement_graph_id);
}
}
AbstractType(utils::AbstractTypeModifier modifier,
const std::string& name,
const std::unordered_set<utils::IdType>& requirement_graph_ids)
: modifier_(modifier),
name_(name),
requirement_graph_ids_(requirement_graph_ids) {}
graph_id_(graph_id),
typeclass_graph_(typeclass_graph) {}
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
bool Same(const AbstractType& type) const;
@ -45,21 +35,21 @@ public:
std::optional<utils::IdType> GetFieldType(const std::string& name,
const std::unordered_set<utils::IdType>& type_namespaces) const;
const std::string& GetName() {
return name_;
utils::IdType GetGraphId() {
return graph_id_;
}
bool HasTypeclass(utils::IdType graph_id) {
return requirement_graph_ids_.count(graph_id) != 0;
bool HasTypeclass(utils::IdType graph_id) { // TODO: cache dependencies set
return graph_id == graph_id_ || typeclass_graph_.GetDependenciesSet(graph_id_).count(graph_id) != 0;
}
std::string ToString() {
return "Abstract " + name_;
return "Abstract " + std::to_string(graph_id_);
}
private:
utils::AbstractTypeModifier modifier_;
std::string name_;
std::unordered_set<utils::IdType> requirement_graph_ids_; // TODO: all typeclasses from tree
utils::IdType graph_id_;
info::TypeclassGraph& typeclass_graph_;
};
class DefinedType {
@ -324,8 +314,6 @@ private:
class Type {
public:
Type() = default;
template<typename T>
explicit Type(const T& type) : type_(type) {}

View file

@ -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)) {