回档之后的重写。部分问题修复。添加了什么我也忘了

This commit is contained in:
2026-02-01 15:52:28 +08:00
parent 61bffdc743
commit aea716ced2
50 changed files with 3676 additions and 2997 deletions

View File

@@ -1,9 +1,9 @@
#pragma once
#include "Value/value.hpp"
#include <Evaluator/Value/value.hpp>
#include <Core/fig_string.hpp>
#include <Value/value_forward.hpp>
#include <Value/Type.hpp>
#include <Evaluator/Value/value_forward.hpp>
#include <Evaluator/Value/Type.hpp>
#include <array>
#include <memory>
@@ -30,6 +30,11 @@ namespace Fig
if (val >= CACHE_MIN && val <= CACHE_MAX) { return cache[val - CACHE_MIN]; }
return std::make_shared<Object>(val);
}
Object createIntCopy(ValueType::IntClass val) const
{
if (val >= CACHE_MIN && val <= CACHE_MAX) { return *cache[val - CACHE_MIN]; }
return Object(val);
}
static const IntPool &getInstance()
{

View File

@@ -1,7 +1,7 @@
#pragma once
#include <Value/VariableSlot.hpp>
#include <Value/value.hpp>
#include <Evaluator/Value/VariableSlot.hpp>
#include <Evaluator/Value/value.hpp>
namespace Fig
{

View File

@@ -14,44 +14,32 @@ namespace Fig
private:
size_t id;
std::map<FString, size_t> &getTypeMap()
{
static std::map<FString, size_t> typeMap;
return typeMap;
}
public:
friend class TypeInfoHash;
FString name;
FString toString() const
{
return name;
}
FString toString() const { return name; }
static std::map<FString, size_t> typeMap;
static size_t getID(FString _name)
{
return typeMap.at(_name);
}
size_t getInstanceID() const
{
return id;
}
size_t getInstanceID() const { return id; }
TypeInfo();
explicit TypeInfo(const FString &_name, bool reg = false);
TypeInfo(const TypeInfo &other) = default;
bool operator==(const TypeInfo &other) const
{
return id == other.id;
}
bool operator==(const TypeInfo &other) const { return id == other.id; }
};
class TypeInfoHash
{
public:
std::size_t operator()(const TypeInfo &ti) const
{
return std::hash<size_t>{}(ti.id);
}
std::size_t operator()(const TypeInfo &ti) const { return std::hash<size_t>{}(ti.id); }
};
// class Value;
@@ -77,39 +65,31 @@ namespace Fig
using NullClass = std::monostate;
using StringClass = FString;
static const std::unordered_set<TypeInfo, TypeInfoHash> builtinTypes
{
Any,
Null,
Int,
String,
Bool,
Double,
Function,
StructType,
StructInstance,
List,
Map,
Module,
InterfaceType
};
inline bool isTypeBuiltin(const TypeInfo &type)
{
static const std::unordered_set<TypeInfo, TypeInfoHash> builtinTypes{Any,
Null,
Int,
String,
Bool,
Double,
Function,
StructType,
StructInstance,
List,
Map,
Module,
InterfaceType};
return builtinTypes.contains(type);
}
}; // namespace ValueType
}; // namespace Fig
namespace std
{
template <>
struct hash<Fig::TypeInfo>
{
size_t operator()(const Fig::TypeInfo &t)
{
return std::hash<size_t>{}(t.getInstanceID());
}
size_t operator()(const Fig::TypeInfo &t) { return std::hash<size_t>{}(t.getInstanceID()); }
};
};
}; // namespace std

View File

@@ -3,8 +3,8 @@
#include <Ast/AccessModifier.hpp>
#include <Core/fig_string.hpp>
#include <Value/Type.hpp>
#include <Value/value_forward.hpp>
#include <Evaluator/Value/Type.hpp>
#include <Evaluator/Value/value_forward.hpp>
#include <memory>
namespace Fig

View File

@@ -1,7 +1,7 @@
#pragma once
#include <Ast/functionParameters.hpp>
#include <Context/context_forward.hpp>
#include <Evaluator/Context/context_forward.hpp>
#include <atomic>
#include <functional>
@@ -11,51 +11,97 @@
namespace Fig
{
class Object;
class Function
{
public:
std::size_t id;
Ast::FunctionParameters paras;
TypeInfo retType;
Ast::BlockStatement body;
bool isBuiltin = false;
std::function<std::shared_ptr<Object>(const std::vector<std::shared_ptr<Object>> &)> builtin;
enum FnType
{
Normal,
Builtin,
MemberType
} type;
union
{
struct
{
Ast::FunctionParameters paras;
TypeInfo retType;
Ast::BlockStatement body;
};
std::function<std::shared_ptr<Object>(const std::vector<std::shared_ptr<Object>> &)> builtin;
std::function<std::shared_ptr<Object>(std::shared_ptr<Object>,
const std::vector<std::shared_ptr<Object>> &)>
mtFn;
};
int builtinParamCount = -1;
std::shared_ptr<Context> closureContext;
// ===== Constructors =====
Function() :
id(nextId()) {}
Function() : id(nextId()), type(Normal)
{
// 需要初始化 union
new (&paras) Ast::FunctionParameters();
new (&retType) TypeInfo();
new (&body) Ast::BlockStatement();
}
Function(Ast::FunctionParameters _paras, TypeInfo _retType, Ast::BlockStatement _body, ContextPtr _closureContext) :
Function(Ast::FunctionParameters _paras,
TypeInfo _retType,
Ast::BlockStatement _body,
ContextPtr _closureContext) :
id(nextId()), // 分配唯一 ID
paras(std::move(_paras)),
retType(std::move(_retType)),
body(std::move(_body)),
closureContext(std::move(_closureContext))
{
type = Normal;
}
Function(std::function<std::shared_ptr<Object>(const std::vector<std::shared_ptr<Object>> &)> fn, int argc) :
id(nextId()), isBuiltin(true), builtin(fn), builtinParamCount(argc) {}
id(nextId()), type(Builtin), builtin(fn), builtinParamCount(argc)
{
type = Builtin;
}
Function(std::function<std::shared_ptr<Object>(std::shared_ptr<Object>,
const std::vector<std::shared_ptr<Object>> &)> fn,
int argc) :
id(nextId()), type(MemberType), mtFn(fn), builtinParamCount(argc)
{
type = MemberType;
}
// ===== Copy / Move =====
Function(const Function &other) = default;
Function(Function &&) noexcept = default;
Function &operator=(const Function &) = default;
Function &operator=(Function &&) noexcept = default;
Function(const Function &other)
{
copyFrom(other);
}
Function &operator=(const Function &other)
{
if (this != &other)
{
destroy();
copyFrom(other);
}
return *this;
};
~Function()
{
destroy();
}
// ===== Comparison =====
bool operator==(const Function &other) const noexcept
{
return id == other.id;
}
bool operator!=(const Function &other) const noexcept
{
return !(*this == other);
}
bool operator==(const Function &other) const noexcept { return id == other.id; }
bool operator!=(const Function &other) const noexcept { return !(*this == other); }
private:
static std::size_t nextId()
@@ -63,5 +109,43 @@ namespace Fig
static std::atomic<std::size_t> counter{1};
return counter++;
}
void destroy()
{
switch (type)
{
case Normal:
paras.~FunctionParameters();
retType.~TypeInfo();
body.~shared_ptr();
break;
case Builtin: builtin.~function(); break;
case MemberType: mtFn.~function(); break;
}
}
void copyFrom(const Function &other)
{
type = other.type;
id = nextId(); // 每个复制都生成新的ID
builtinParamCount = other.builtinParamCount;
closureContext = other.closureContext;
switch (type)
{
case Normal:
new (&paras) Ast::FunctionParameters(other.paras);
new (&retType) TypeInfo(other.retType);
new (&body) Ast::BlockStatement(other.body);
break;
case Builtin:
new (&builtin) std::function<std::shared_ptr<Object>(const std::vector<std::shared_ptr<Object>> &)>(
other.builtin);
break;
case MemberType:
new (&mtFn) std::function<std::shared_ptr<Object>(
std::shared_ptr<Object>, const std::vector<std::shared_ptr<Object>> &)>(other.mtFn);
break;
}
}
};
} // namespace Fig

View File

@@ -1,7 +1,7 @@
#pragma once
#include <Ast/Statements/InterfaceDefSt.hpp>
#include <Value/Type.hpp>
#include <Evaluator/Value/Type.hpp>
#include <vector>

View File

@@ -2,7 +2,7 @@
#include <Core/fig_string.hpp>
#include <Context/context_forward.hpp>
#include <Evaluator/Context/context_forward.hpp>
namespace Fig
{

View File

@@ -1,7 +1,7 @@
#pragma once
#include <Context/context_forward.hpp>
#include <Value/Type.hpp>
#include <Evaluator/Context/context_forward.hpp>
#include <Evaluator/Value/Type.hpp>
namespace Fig
{

View File

@@ -3,9 +3,9 @@
#include <Core/fig_string.hpp>
#include <Ast/Statements/StructDefSt.hpp>
#include <Value/Type.hpp>
#include <Evaluator/Value/Type.hpp>
#include <Context/context_forward.hpp>
#include <Evaluator/Context/context_forward.hpp>
#include <atomic>
#include <vector>

View File

@@ -1,13 +1,13 @@
#include "Value/structType.hpp"
#include <Value/Type.hpp>
#include <Value/value.hpp>
#include <Context/context.hpp>
#include <Evaluator/Value/structType.hpp>
#include <Evaluator/Value/value_forward.hpp>
#include <Evaluator/Value/Type.hpp>
#include <Evaluator/Value/value.hpp>
#include <Evaluator/Context/context.hpp>
// #include <iostream>
namespace Fig
{
std::map<FString, size_t> TypeInfo::typeMap = {};
TypeInfo::TypeInfo() : // only allow use in evaluate time !! <---- dynamic type system requirement
id(1), name(FString(u8"Any"))
@@ -20,17 +20,17 @@ namespace Fig
// std::cerr << "TypeInfo constructor called for type name: " << name.toBasicString() << "\n";
if (reg)
{
typeMap[name] = ++id_count;
getTypeMap()[name] = ++id_count;
id = id_count;
}
else
{
if (!typeMap.contains(_name))
if (!getTypeMap().contains(_name))
{
throw RuntimeError(FString(std::format("No type named '{}'", _name.toBasicString())));
// *this = ValueType::String;
}
id = typeMap.at(name); // may throw
id = getTypeMap().at(name); // may throw
}
}

View File

@@ -1,13 +1,16 @@
#pragma once
#include <Value/function.hpp>
#include <Value/interface.hpp>
#include <Value/structType.hpp>
#include <Value/structInstance.hpp>
#include <Value/Type.hpp>
#include <Value/valueError.hpp>
#include <Value/module.hpp>
#include <Value/value_forward.hpp>
#include "Core/fig_string.hpp"
#include <Evaluator/Value/function.hpp>
#include <Evaluator/Value/interface.hpp>
#include <Evaluator/Value/structType.hpp>
#include <Evaluator/Value/structInstance.hpp>
#include <Evaluator/Value/Type.hpp>
#include <Evaluator/Value/valueError.hpp>
#include <Evaluator/Value/module.hpp>
#include <Evaluator/Value/value_forward.hpp>
#include <cassert>
#include <iostream>
#include <memory>
#include <variant>
#include <cmath>
@@ -41,26 +44,18 @@ namespace Fig
struct Element
{
ObjectPtr value;
Element(ObjectPtr _value) :
value(_value) {}
bool operator==(const Element &other) const
{
return *value == *other.value;
}
Element(ObjectPtr _value) : value(_value) {}
void deepCopy(const Element &e)
{
value = std::make_shared<Object>(*e.value);
}
bool operator==(const Element &other) const { return *value == *other.value; }
void deepCopy(const Element &e) { value = std::make_shared<Object>(*e.value); }
};
using List = std::vector<Element>;
struct ValueKey
{
ObjectPtr value;
ValueKey(ObjectPtr _value) :
value(_value) {}
ValueKey(ObjectPtr _value) : value(_value) {}
void deepCopy(const ValueKey &vk) { value = std::make_shared<Object>(*vk.value); }
};
@@ -74,231 +69,237 @@ namespace Fig
bool isTypeMatch(const TypeInfo &, ObjectPtr, ContextPtr);
bool implements(const TypeInfo &, const TypeInfo &, ContextPtr);
using BuiltinTypeMemberFn = std::function<ObjectPtr(ObjectPtr, std::vector<ObjectPtr>)>;
class Object : public std::enable_shared_from_this<Object>
{
public:
using VariantType = std::variant<
ValueType::NullClass,
ValueType::IntClass,
ValueType::DoubleClass,
ValueType::StringClass,
ValueType::BoolClass,
Function,
StructType,
StructInstance,
List,
Map,
Module,
InterfaceType>;
using VariantType = std::variant<ValueType::NullClass,
ValueType::IntClass,
ValueType::DoubleClass,
ValueType::StringClass,
ValueType::BoolClass,
Function,
StructType,
StructInstance,
List,
Map,
Module,
InterfaceType>;
static std::unordered_map<TypeInfo, std::unordered_map<FString, BuiltinTypeMemberFn>, TypeInfoHash> getMemberTypeFunctions()
{
static const std::unordered_map<TypeInfo, std::unordered_map<FString, BuiltinTypeMemberFn>, TypeInfoHash>
memberTypeFunctions{
{ValueType::Null, {}},
{ValueType::Int, {}},
{ValueType::Double, {}},
{ValueType::String,
{
{u8"length",
[](ObjectPtr object, std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 0)
throw RuntimeError(
FString(std::format("`length` expects 0 arguments, {} got", args.size())));
const FString &str = object->as<ValueType::StringClass>();
return std::make_shared<Object>(static_cast<ValueType::IntClass>(str.length()));
}},
{u8"replace",
[](ObjectPtr object, std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 2)
throw RuntimeError(
FString(std::format("`replace` expects 2 arguments, {} got", args.size())));
FString &str = object->as<ValueType::StringClass>();
ObjectPtr arg1 = args[0];
ObjectPtr arg2 = args[1];
if (!arg1->is<ValueType::IntClass>())
{
throw RuntimeError(FString("`replace` arg 1 expects type Int"));
}
if (!arg2->is<ValueType::StringClass>())
{
throw RuntimeError(FString("`replace` arg 2 expects type String"));
}
str.realReplace(arg1->as<ValueType::IntClass>(), arg2->as<ValueType::StringClass>());
return Object::getNullInstance();
}},
{u8"erase",
[](ObjectPtr object, std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 2)
throw RuntimeError(
FString(std::format("`erase` expects 2 arguments, {} got", args.size())));
FString &str = object->as<ValueType::StringClass>();
ObjectPtr arg1 = args[0];
ObjectPtr arg2 = args[1];
if (!arg1->is<ValueType::IntClass>())
{
throw RuntimeError(FString("`erase` arg 1 expects type Int"));
}
if (!arg2->is<ValueType::IntClass>())
{
throw RuntimeError(FString("`erase` arg 2 expects type Int"));
}
ValueType::IntClass index = arg1->as<ValueType::IntClass>();
ValueType::IntClass n = arg2->as<ValueType::IntClass>();
if (index < 0 || n < 0)
{
throw RuntimeError(FString("`erase`: index and n must greater or equal to 0"));
}
if (index + n > str.length())
{
throw RuntimeError(FString("`erase`: length is not long enough to erase"));
}
str.realErase(arg1->as<ValueType::IntClass>(), arg2->as<ValueType::IntClass>());
return Object::getNullInstance();
}},
{u8"insert",
[](ObjectPtr object, std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 2)
throw RuntimeError(
FString(std::format("`insert` expects 2 arguments, {} got", args.size())));
FString &str = object->as<ValueType::StringClass>();
ObjectPtr arg1 = args[0];
ObjectPtr arg2 = args[1];
if (!arg1->is<ValueType::IntClass>())
{
throw RuntimeError(FString("`insert` arg 1 expects type Int"));
}
if (!arg2->is<ValueType::StringClass>())
{
throw RuntimeError(FString("`insert` arg 2 expects type String"));
}
str.realInsert(arg1->as<ValueType::IntClass>(), arg2->as<ValueType::StringClass>());
return Object::getNullInstance();
}},
}},
{ValueType::Function, {}},
{ValueType::StructType, {}},
{ValueType::StructInstance, {}},
{ValueType::List,
{
{u8"length",
[](ObjectPtr object, std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 0)
throw RuntimeError(
FString(std::format("`length` expects 0 arguments, {} got", args.size())));
const List &list = object->as<List>();
return std::make_shared<Object>(static_cast<ValueType::IntClass>(list.size()));
}},
{u8"get",
[](ObjectPtr object, std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 1)
throw RuntimeError(
FString(std::format("`get` expects 1 arguments, {} got", args.size())));
ObjectPtr arg = args[0];
if (arg->getTypeInfo() != ValueType::Int)
throw RuntimeError(
FString(std::format("`get` argument 1 expects Int, {} got",
arg->getTypeInfo().toString().toBasicString())));
ValueType::IntClass i = arg->as<ValueType::IntClass>();
const List &list = object->as<List>();
if (i >= list.size()) return Object::getNullInstance();
return list[i].value;
}},
{u8"push",
[](ObjectPtr object, std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 1)
throw RuntimeError(
FString(std::format("`push` expects 1 arguments, {} got", args.size())));
ObjectPtr arg = args[0];
List &list = object->as<List>();
list.push_back(arg);
return Object::getNullInstance();
}},
}},
{ValueType::Map,
{
{u8"get",
[](ObjectPtr object, std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 1)
throw RuntimeError(
FString(std::format("`get` expects 1 arguments, {} got", args.size())));
ObjectPtr index = args[0];
const Map &map = object->as<Map>();
if (!map.contains(index)) return Object::getNullInstance();
return map.at(index);
}},
{u8"contains",
[](ObjectPtr object, std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 1)
throw RuntimeError(
FString(std::format("`contains` expects 1 arguments, {} got", args.size())));
ObjectPtr index = args[0];
const Map &map = object->as<Map>();
return std::make_shared<Object>(map.contains(index));
}},
}},
{ValueType::Module, {}},
{ValueType::InterfaceType, {}},
};
return memberTypeFunctions;
}
static std::unordered_map<TypeInfo, std::unordered_map<FString, int>, TypeInfoHash> getMemberTypeFunctionsParas()
{
static const std::unordered_map<TypeInfo, std::unordered_map<FString, int>, TypeInfoHash>
memberTypeFunctionsParas{
{ValueType::Null, {}},
{ValueType::Int, {}},
{ValueType::Double, {}},
{ValueType::String,
{
{u8"length", 0},
{u8"replace", 2},
{u8"erase", 2},
{u8"insert", 2},
}},
{ValueType::Function, {}},
{ValueType::StructType, {}},
{ValueType::StructInstance, {}},
{ValueType::List, {{u8"length", 0}, {u8"get", 1}, {u8"push", 1}}},
{ValueType::Map,
{
{u8"get", 1},
{u8"contains", 1},
}},
{ValueType::Module, {}},
{ValueType::InterfaceType, {}},
};
return memberTypeFunctionsParas;
}
std::unordered_map<TypeInfo,
std::unordered_map<FString,
std::function<ObjectPtr(std::vector<ObjectPtr>)>>,
TypeInfoHash>
memberTypeFunctions{
{ValueType::Null, {}},
{ValueType::Int, {}},
{ValueType::Double, {}},
{ValueType::String, {
{u8"length", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 0)
throw RuntimeError(FString(
std::format("`length` expects 0 arguments, {} got", args.size())));
const FString &str = as<ValueType::StringClass>();
return std::make_shared<Object>(static_cast<ValueType::IntClass>(str.length()));
}},
{u8"replace", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 2)
throw RuntimeError(FString(
std::format("`replace` expects 2 arguments, {} got", args.size())));
FString &str = as<ValueType::StringClass>();
ObjectPtr arg1 = args[0];
ObjectPtr arg2 = args[1];
if (!arg1->is<ValueType::IntClass>())
{
throw RuntimeError(FString(
"`replace` arg 1 expects type Int"));
}
if (!arg2->is<ValueType::StringClass>())
{
throw RuntimeError(FString(
"`replace` arg 2 expects type String"));
}
str.realReplace(arg1->as<ValueType::IntClass>(), arg2->as<ValueType::StringClass>());
return Object::getNullInstance();
}},
{u8"erase", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 2)
throw RuntimeError(FString(
std::format("`erase` expects 2 arguments, {} got", args.size())));
FString &str = as<ValueType::StringClass>();
ObjectPtr arg1 = args[0];
ObjectPtr arg2 = args[1];
if (!arg1->is<ValueType::IntClass>())
{
throw RuntimeError(FString(
"`erase` arg 1 expects type Int"));
}
if (!arg2->is<ValueType::IntClass>())
{
throw RuntimeError(FString(
"`erase` arg 2 expects type Int"));
}
ValueType::IntClass index = arg1->as<ValueType::IntClass>();
ValueType::IntClass n = arg2->as<ValueType::IntClass>();
if (index < 0 || n < 0)
{
throw RuntimeError(FString("`erase`: index and n must greater or equal to 0"));
}
if (index + n > str.length())
{
throw RuntimeError(FString("`erase`: length is not long enough to erase"));
}
str.realErase(arg1->as<ValueType::IntClass>(), arg2->as<ValueType::IntClass>());
return Object::getNullInstance();
}},
{u8"insert", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 2)
throw RuntimeError(FString(
std::format("`insert` expects 2 arguments, {} got", args.size())));
FString &str = as<ValueType::StringClass>();
ObjectPtr arg1 = args[0];
ObjectPtr arg2 = args[1];
if (!arg1->is<ValueType::IntClass>())
{
throw RuntimeError(FString(
"`insert` arg 1 expects type Int"));
}
if (!arg2->is<ValueType::StringClass>())
{
throw RuntimeError(FString(
"`insert` arg 2 expects type String"));
}
str.realInsert(arg1->as<ValueType::IntClass>(), arg2->as<ValueType::StringClass>());
return Object::getNullInstance();
}},
}},
{ValueType::Function, {}},
{ValueType::StructType, {}},
{ValueType::StructInstance, {}},
{ValueType::List, {
{u8"length", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 0)
throw RuntimeError(FString(
std::format("`length` expects 0 arguments, {} got", args.size())));
const List &list = as<List>();
return std::make_shared<Object>(static_cast<ValueType::IntClass>(list.size()));
}},
{u8"get", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 1)
throw RuntimeError(FString(
std::format("`get` expects 1 arguments, {} got", args.size())));
ObjectPtr arg = args[0];
if (arg->getTypeInfo() != ValueType::Int)
throw RuntimeError(FString(
std::format("`get` argument 1 expects Int, {} got", arg->getTypeInfo().toString().toBasicString())));
ValueType::IntClass i = arg->as<ValueType::IntClass>();
const List &list = as<List>();
if (i >= list.size())
return Object::getNullInstance();
return list[i].value;
}},
{u8"push", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 1)
throw RuntimeError(FString(
std::format("`push` expects 1 arguments, {} got", args.size())));
ObjectPtr arg = args[0];
List &list = as<List>();
list.push_back(arg);
return Object::getNullInstance();
}},
}},
{ValueType::Map, {
{u8"get", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 1)
throw RuntimeError(FString(
std::format("`get` expects 1 arguments, {} got", args.size())));
ObjectPtr index = args[0];
const Map &map = as<Map>();
if (!map.contains(index))
return Object::getNullInstance();
return map.at(index);
}},
{u8"contains", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
if (args.size() != 1)
throw RuntimeError(FString(
std::format("`contains` expects 1 arguments, {} got", args.size())));
ObjectPtr index = args[0];
const Map &map = as<Map>();
return std::make_shared<Object>(
map.contains(index));
}},
}},
{ValueType::Module, {}},
{ValueType::InterfaceType, {}},
};
std::unordered_map<TypeInfo, std::unordered_map<FString, int>, TypeInfoHash> memberTypeFunctionsParas{
{ValueType::Null, {}},
{ValueType::Int, {}},
{ValueType::Double, {}},
{ValueType::String, {
{u8"length", 0},
{u8"replace", 2},
{u8"erase", 2},
{u8"insert", 2},
}},
{ValueType::Function, {}},
{ValueType::StructType, {}},
{ValueType::StructInstance, {}},
{ValueType::List, {{u8"length", 0}, {u8"get", 1}, {u8"push", 1}}},
{ValueType::Map, {
{u8"get", 1},
{u8"contains", 1},
}},
{ValueType::Module, {}},
{ValueType::InterfaceType, {}},
};
bool hasMemberFunction(const FString &name) const
{
return memberTypeFunctions.at(getTypeInfo()).contains(name);
return getMemberTypeFunctions().at(getTypeInfo()).contains(name);
}
std::function<ObjectPtr(std::vector<ObjectPtr>)> getMemberFunction(const FString &name) const
BuiltinTypeMemberFn getMemberFunction(const FString &name) const
{
return memberTypeFunctions.at(getTypeInfo()).at(name);
return getMemberTypeFunctions().at(getTypeInfo()).at(name);
}
int getMemberFunctionParaCount(const FString &name) const
{
return memberTypeFunctionsParas.at(getTypeInfo()).at(name);
return getMemberTypeFunctionsParas().at(getTypeInfo()).at(name);
}
VariantType data;
Object() :
data(ValueType::NullClass{}) {}
Object(const ValueType::NullClass &n) :
data(n) {}
Object(const ValueType::IntClass &i) :
data(i) {}
explicit Object(const ValueType::DoubleClass &d) :
data(d) {}
Object(const ValueType::StringClass &s) :
data(s) {}
Object(const ValueType::BoolClass &b) :
data(b) {}
Object(const Function &f) :
data(f) {}
Object(const StructType &s) :
data(s) {}
Object(const StructInstance &s) :
data(s) {}
Object(const List &l) :
data(l) {}
Object(const Map &m) :
data(m) {}
Object(const Module &m) :
data(m) {}
Object(const InterfaceType &i) :
data(i) {}
Object() : data(ValueType::NullClass{}) {}
Object(const ValueType::NullClass &n) : data(n) {}
Object(const ValueType::IntClass &i) : data(i) {}
explicit Object(const ValueType::DoubleClass &d) : data(d) {}
Object(const ValueType::StringClass &s) : data(s) {}
Object(const ValueType::BoolClass &b) : data(b) {}
Object(const Function &f) : data(f) {}
Object(const StructType &s) : data(s) {}
Object(const StructInstance &s) : data(s) {}
Object(const List &l) : data(l) {}
Object(const Map &m) : data(m) {}
Object(const Module &m) : data(m) {}
Object(const InterfaceType &i) : data(i) {}
Object(const Object &) = default;
Object(Object &&) noexcept = default;
@@ -359,49 +360,50 @@ namespace Fig
TypeInfo getTypeInfo() const
{
return std::visit([](auto &&val) -> TypeInfo {
using T = std::decay_t<decltype(val)>;
return std::visit(
[](auto &&val) -> TypeInfo {
using T = std::decay_t<decltype(val)>;
if constexpr (std::is_same_v<T, ValueType::NullClass>)
return ValueType::Null;
if constexpr (std::is_same_v<T, ValueType::NullClass>)
return ValueType::Null;
else if constexpr (std::is_same_v<T, ValueType::IntClass>)
return ValueType::Int;
else if constexpr (std::is_same_v<T, ValueType::IntClass>)
return ValueType::Int;
else if constexpr (std::is_same_v<T, ValueType::DoubleClass>)
return ValueType::Double;
else if constexpr (std::is_same_v<T, ValueType::DoubleClass>)
return ValueType::Double;
else if constexpr (std::is_same_v<T, ValueType::StringClass>)
return ValueType::String;
else if constexpr (std::is_same_v<T, ValueType::StringClass>)
return ValueType::String;
else if constexpr (std::is_same_v<T, ValueType::BoolClass>)
return ValueType::Bool;
else if constexpr (std::is_same_v<T, ValueType::BoolClass>)
return ValueType::Bool;
else if constexpr (std::is_same_v<T, Function>)
return ValueType::Function;
else if constexpr (std::is_same_v<T, Function>)
return ValueType::Function;
else if constexpr (std::is_same_v<T, StructType>)
return ValueType::StructType;
else if constexpr (std::is_same_v<T, StructType>)
return ValueType::StructType;
else if constexpr (std::is_same_v<T, StructInstance>)
return ValueType::StructInstance;
else if constexpr (std::is_same_v<T, StructInstance>)
return ValueType::StructInstance;
else if constexpr (std::is_same_v<T, List>)
return ValueType::List;
else if constexpr (std::is_same_v<T, List>)
return ValueType::List;
else if constexpr (std::is_same_v<T, Map>)
return ValueType::Map;
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 if constexpr (std::is_same_v<T, Module>)
return ValueType::Module;
else if constexpr (std::is_same_v<T, InterfaceType>)
return ValueType::InterfaceType;
else if constexpr (std::is_same_v<T, InterfaceType>)
return ValueType::InterfaceType;
else
return ValueType::Any;
},
data);
else
return ValueType::Any;
},
data);
}
bool isNull() const { return is<ValueType::NullClass>(); }
@@ -427,13 +429,12 @@ namespace Fig
{
if (is<ValueType::NullClass>()) return FString(u8"null");
if (is<ValueType::IntClass>()) return FString(std::to_string(as<ValueType::IntClass>()));
if (is<ValueType::DoubleClass>()) return FString(std::format("{}", as<ValueType::DoubleClass>()));
if (is<ValueType::DoubleClass>()) return FString(std::format("{:g}", as<ValueType::DoubleClass>()));
if (is<ValueType::StringClass>()) return FString(u8"\"" + as<ValueType::StringClass>() + u8"\"");
if (is<ValueType::BoolClass>()) return as<ValueType::BoolClass>() ? FString(u8"true") : FString(u8"false");
if (is<Function>())
return FString(std::format("<Function '{}' at {:p}>",
as<Function>().id,
static_cast<const void *>(&as<Function>())));
return FString(std::format(
"<Function '{}' at {:p}>", as<Function>().id, static_cast<const void *>(&as<Function>())));
if (is<StructType>())
return FString(std::format("<StructType '{}' at {:p}>",
as<StructType>().type.toString().toBasicString(),
@@ -449,8 +450,7 @@ namespace Fig
bool first_flag = true;
for (auto &ele : list)
{
if (!first_flag)
output += u8", ";
if (!first_flag) output += u8", ";
output += ele.value->toString();
first_flag = false;
}
@@ -464,8 +464,7 @@ namespace Fig
bool first_flag = true;
for (auto &[key, value] : map)
{
if (!first_flag)
output += u8", ";
if (!first_flag) output += u8", ";
output += key.value->toString() + FString(u8" : ") + value->toString();
first_flag = false;
}
@@ -474,24 +473,22 @@ namespace Fig
}
if (is<Module>())
{
return FString(std::format(
"<Module '{}' at {:p}>",
as<Module>().name.toBasicString(),
static_cast<const void *>(&as<Module>())));
return FString(std::format("<Module '{}' at {:p}>",
as<Module>().name.toBasicString(),
static_cast<const void *>(&as<Module>())));
}
if (is<InterfaceType>())
{
return FString(std::format(
"<InterfaceType '{}' at {:p}>",
as<InterfaceType>().type.toString().toBasicString(),
static_cast<const void *>(&as<InterfaceType>())));
return FString(std::format("<InterfaceType '{}' at {:p}>",
as<InterfaceType>().type.toString().toBasicString(),
static_cast<const void *>(&as<InterfaceType>())));
}
return FString(u8"<error>");
}
private:
static std::string makeTypeErrorMessage(const char *prefix, const char *op,
const Object &lhs, const Object &rhs)
static std::string
makeTypeErrorMessage(const char *prefix, const char *op, const Object &lhs, const Object &rhs)
{
auto lhs_type = lhs.getTypeInfo().name.toBasicString();
auto rhs_type = rhs.getTypeInfo().name.toBasicString();
@@ -508,8 +505,7 @@ namespace Fig
{
bool bothInt = lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>();
auto result = lhs.getNumericValue() + rhs.getNumericValue();
if (bothInt)
return Object(static_cast<ValueType::IntClass>(result));
if (bothInt) return Object(static_cast<ValueType::IntClass>(result));
return Object(result);
}
if (lhs.is<ValueType::StringClass>() && rhs.is<ValueType::StringClass>())
@@ -525,8 +521,7 @@ namespace Fig
{
bool bothInt = lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>();
auto result = lhs.getNumericValue() - rhs.getNumericValue();
if (bothInt)
return Object(static_cast<ValueType::IntClass>(result));
if (bothInt) return Object(static_cast<ValueType::IntClass>(result));
return Object(result);
}
throw ValueError(FString(makeTypeErrorMessage("Unsupported operation", "-", lhs, rhs)));
@@ -540,18 +535,14 @@ namespace Fig
{
bool bothInt = lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>();
auto result = lhs.getNumericValue() * rhs.getNumericValue();
if (bothInt)
return Object(static_cast<ValueType::IntClass>(result));
if (bothInt) return Object(static_cast<ValueType::IntClass>(result));
return Object(result);
}
if (lhs.is<ValueType::StringClass>() && rhs.is<ValueType::IntClass>())
{
FString result;
const FString &l = lhs.as<ValueType::StringClass>();
for (size_t i=0; i < rhs.getNumericValue(); ++i)
{
result += l;
}
for (size_t i = 0; i < rhs.getNumericValue(); ++i) { result += l; }
return Object(result);
}
throw ValueError(FString(makeTypeErrorMessage("Unsupported operation", "*", lhs, rhs)));
@@ -564,8 +555,7 @@ namespace Fig
if (lhs.isNumeric() && rhs.isNumeric())
{
auto rnv = rhs.getNumericValue();
if (rnv == 0)
throw ValueError(FString(makeTypeErrorMessage("Division by zero", "/", lhs, rhs)));
if (rnv == 0) throw ValueError(FString(makeTypeErrorMessage("Division by zero", "/", lhs, rhs)));
// bool bothInt = lhs.is<ValueType::IntClass>() && rhs.is<ValueType::IntClass>();
auto result = lhs.getNumericValue() / rnv;
// if (bothInt)
@@ -593,12 +583,11 @@ namespace Fig
if (r != 0 && ((lv < 0) != (rv < 0))) { q -= 1; }
return q;
}
if (lhs.isNumeric() && rhs.isNumeric())
{
auto rnv = rhs.getNumericValue();
if (rnv == 0)
throw ValueError(FString(makeTypeErrorMessage("Modulo by zero", "/", lhs, rhs)));
if (rnv == 0) throw ValueError(FString(makeTypeErrorMessage("Modulo by zero", "/", lhs, rhs)));
auto result = std::fmod(lhs.getNumericValue(), rnv);
return Object(result);
}
@@ -623,25 +612,25 @@ namespace Fig
friend Object operator!(const Object &v)
{
if (!v.is<ValueType::BoolClass>())
throw ValueError(FString(std::format("Logical NOT requires bool: '{}'", v.getTypeInfo().name.toBasicString())));
throw ValueError(
FString(std::format("Logical NOT requires bool: '{}'", v.getTypeInfo().name.toBasicString())));
return Object(!v.as<ValueType::BoolClass>());
}
friend Object operator-(const Object &v)
{
if (v.isNull())
throw ValueError(FString(u8"Unary minus cannot be applied to null"));
if (v.is<ValueType::IntClass>())
return Object(-v.as<ValueType::IntClass>());
if (v.is<ValueType::DoubleClass>())
return Object(-v.as<ValueType::DoubleClass>());
throw ValueError(FString(std::format("Unary minus requires int or double: '{}'", v.getTypeInfo().name.toBasicString())));
if (v.isNull()) throw ValueError(FString(u8"Unary minus cannot be applied to null"));
if (v.is<ValueType::IntClass>()) return Object(-v.as<ValueType::IntClass>());
if (v.is<ValueType::DoubleClass>()) return Object(-v.as<ValueType::DoubleClass>());
throw ValueError(
FString(std::format("Unary minus requires int or double: '{}'", v.getTypeInfo().name.toBasicString())));
}
friend Object operator~(const Object &v)
{
if (!v.is<ValueType::IntClass>())
throw ValueError(FString(std::format("Bitwise NOT requires int: '{}'", v.getTypeInfo().name.toBasicString())));
throw ValueError(
FString(std::format("Bitwise NOT requires int: '{}'", v.getTypeInfo().name.toBasicString())));
return Object(~v.as<ValueType::IntClass>());
}
@@ -690,7 +679,8 @@ namespace Fig
friend Object bit_not(const Object &v)
{
if (!v.is<ValueType::IntClass>())
throw ValueError(FString(std::format("Bitwise NOT requires int: '{}'", v.getTypeInfo().name.toBasicString())));
throw ValueError(
FString(std::format("Bitwise NOT requires int: '{}'", v.getTypeInfo().name.toBasicString())));
return Object(~v.as<ValueType::IntClass>());
}
@@ -716,14 +706,19 @@ namespace Fig
{
bool bothInt = base.is<ValueType::IntClass>() && exp.is<ValueType::IntClass>();
auto result = std::pow(base.getNumericValue(), exp.getNumericValue());
if (bothInt)
return Object(static_cast<ValueType::IntClass>(result));
if (bothInt) return Object(static_cast<ValueType::IntClass>(result));
return Object(result);
}
throw ValueError(FString(makeTypeErrorMessage("Unsupported operation", "**", base, exp)));
}
};
inline bool isBoolObjectTruthy(ObjectPtr obj)
{
assert(obj->is<bool>());
return obj->as<bool>();
}
using RvObject = ObjectPtr;
inline bool operator==(const ValueKey &l, const ValueKey &r)