forked from PuqiAR/Fig-TreeWalker
回档之后的重写。部分问题修复。添加了什么我也忘了
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user