[Feat] 模块系统支持,使用 import x.x.x导入

[Fix] Context内部辅助函数修改, getStructName ....
[Feat] 增加字符串下标获取操作,和修改字符操作,实现使用了第四点的函数
[Impl] FString添加新方法 getRealChar, realReplace
[Fun] 在utf8_iterator中辱骂了C++
This commit is contained in:
2025-12-26 20:47:57 +08:00
parent 6e1df63507
commit 00240f1ed1
21 changed files with 578 additions and 140 deletions

View File

@@ -12,12 +12,13 @@ namespace Fig
{
Variable,
ListElement,
MapElement
MapElement,
StringElement,
} kind;
std::shared_ptr<VariableSlot> slot;
ObjectPtr listOrMap = nullptr;
size_t listIndex;
ObjectPtr value = nullptr;
size_t numIndex;
ObjectPtr mapIndex;
@@ -26,20 +27,18 @@ namespace Fig
{
kind = Kind::Variable;
}
LvObject(ObjectPtr _v, size_t _index) :
listOrMap(_v), listIndex(_index)
LvObject(ObjectPtr _v, size_t _index, Kind _kind) :
value(_v), numIndex(_index)
{
assert(_v->getTypeInfo() == ValueType::List);
kind = Kind::ListElement;
kind = _kind;
}
LvObject(ObjectPtr _v, ObjectPtr _index) :
listOrMap(_v), mapIndex(_index)
LvObject(ObjectPtr _v, ObjectPtr _index, Kind _kind) :
value(_v), mapIndex(_index)
{
assert(_v->getTypeInfo() == ValueType::Map);
kind = Kind::MapElement;
kind = _kind;
}
const ObjectPtr &get() const
ObjectPtr get() const
{
if (kind == Kind::Variable)
{
@@ -48,20 +47,30 @@ namespace Fig
}
else if (kind == Kind::ListElement)
{
List &list = listOrMap->as<List>();
if (listIndex >= list.size())
List &list = value->as<List>();
if (numIndex >= list.size())
throw RuntimeError(FString(
std::format("Index {} out of range", listIndex)));
return list.at(listIndex);
std::format("Index {} out of range {}", numIndex, value->toString().toBasicString())));
return list.at(numIndex);
}
else // map
else if (kind == Kind::MapElement) // map
{
Map &map = listOrMap->as<Map>();
Map &map = value->as<Map>();
if (!map.contains(mapIndex))
throw RuntimeError(FString(
std::format("Key {} not found", mapIndex->toString().toBasicString())));
return map.at(mapIndex);
}
else
{
// string
FString &string = value->as<ValueType::StringClass>();
if (numIndex >= string.length())
throw RuntimeError(FString(
std::format("Index {} out of range {}", numIndex, value->toString().toBasicString())));
return std::make_shared<Object>(string.getRealChar(numIndex));
}
}
void set(const ObjectPtr &v)
@@ -87,17 +96,35 @@ namespace Fig
}
else if (kind == Kind::ListElement)
{
List &list = listOrMap->as<List>();
if (listIndex >= list.size())
List &list = value->as<List>();
if (numIndex >= list.size())
throw RuntimeError(FString(
std::format("Index {} out of range", listIndex)));
list[listIndex] = v;
std::format("Index {} out of range", numIndex)));
list[numIndex] = v;
}
else // map
else if (kind == Kind::MapElement) // map
{
Map &map = listOrMap->as<Map>();
Map &map = value->as<Map>();
map[mapIndex] = v;
}
else if (kind == Kind::StringElement)
{
FString &string = value->as<ValueType::StringClass>();
if (numIndex >= string.length())
throw RuntimeError(FString(
std::format("Index {} out of range {}", numIndex, value->toString().toBasicString())));
if (v->getTypeInfo() != ValueType::String)
throw RuntimeError(FString(
std::format("Could not assign {} to sub string", v->toString().toBasicString())
));
const FString &strReplace = v->as<ValueType::StringClass>();
if (strReplace.length() > 1)
throw RuntimeError(FString(
std::format("Could not assign {} to sub string, expects length 1", v->toString().toBasicString())
));
string.realReplace(numIndex, strReplace);
}
}
FString name() const { return resolve(slot)->name; }
@@ -111,4 +138,4 @@ namespace Fig
return s;
}
};
}
} // namespace Fig

View File

@@ -67,6 +67,7 @@ namespace Fig
extern const TypeInfo StructInstance;
extern const TypeInfo List;
extern const TypeInfo Map;
extern const TypeInfo Module;
// extern const TypeInfo Tuple;
using IntClass = int64_t;

27
src/Value/module.hpp Normal file
View File

@@ -0,0 +1,27 @@
#pragma once
#include <Core/fig_string.hpp>
#include <Context/context_forward.hpp>
namespace Fig
{
struct Module
{
FString name;
ContextPtr ctx;
Module() = default;
Module(FString n, ContextPtr c) :
name(std::move(n)),
ctx(std::move(c))
{
}
bool operator==(const Module &o) const noexcept
{
return name == o.name;
}
};
};

View File

@@ -80,5 +80,5 @@ namespace Fig
const TypeInfo ValueType::StructInstance(FString(u8"StructInstance"), true); // id: 9
const TypeInfo ValueType::List(FString(u8"List"), true); // id: 10
const TypeInfo ValueType::Map(FString(u8"Map"), true); // id: 11
// const TypeInfo ValueType::Tuple(FString(u8"Tuple"), true); // id: 12
const TypeInfo ValueType::Module(FString(u8"Module"), true); // id: 12
} // namespace Fig

View File

@@ -4,6 +4,7 @@
#include <Value/structInstance.hpp>
#include <Value/Type.hpp>
#include <Value/valueError.hpp>
#include <Value/module.hpp>
#include <variant>
#include <cmath>
@@ -58,7 +59,8 @@ namespace Fig
StructType,
StructInstance,
List,
Map>;
Map,
Module>;
std::unordered_map<TypeInfo,
std::unordered_map<FString,
@@ -134,6 +136,7 @@ namespace Fig
map.contains(index));
}},
}},
{ValueType::Module, {}}
};
std::unordered_map<TypeInfo, std::unordered_map<FString, int>, TypeInfoHash> memberTypeFunctionsParas{
{ValueType::Null, {}},
@@ -151,6 +154,7 @@ namespace Fig
{u8"get", 1},
{u8"contains", 1},
}},
{ValueType::Module, {}}
};
bool hasMemberFunction(const FString &name) const
@@ -190,6 +194,8 @@ namespace Fig
data(l) {}
Object(const Map &m) :
data(m) {}
Object(const Module &m) :
data(m) {}
Object(const Object &) = default;
Object(Object &&) noexcept = default;
@@ -283,6 +289,9 @@ namespace Fig
else if constexpr (std::is_same_v<T, Map>)
return ValueType::Map;
else if constexpr (std::is_same_v<T, Module>)
return ValueType::Module;
else
return ValueType::Any;
},
@@ -357,6 +366,12 @@ namespace Fig
output += u8"}";
return output;
}
if (is<Module>())
{
return FString(std::format(
"<Module at {:p}>",
static_cast<const void *>(&as<Module>())));
}
return FString(u8"<error>");
}
@@ -576,7 +591,6 @@ namespace Fig
using ObjectPtr = std::shared_ptr<Object>;
using RvObject = ObjectPtr;
inline bool operator==(const ValueKey &l, const ValueKey &r)
{
return *l.value == *r.value;