阶段性保存,全面修改Value为Object
This commit is contained in:
@@ -13,13 +13,13 @@ namespace Fig
|
||||
{
|
||||
namespace Builtins
|
||||
{
|
||||
const std::unordered_map<FString, Value> builtinValues = {
|
||||
{u8"null", Value::getNullInstance()},
|
||||
{u8"true", Value(true)},
|
||||
{u8"false", Value(false)},
|
||||
const std::unordered_map<FString, Object> builtinValues = {
|
||||
{u8"null", Object::getNullInstance()},
|
||||
{u8"true", Object(true)},
|
||||
{u8"false", Object(false)},
|
||||
};
|
||||
|
||||
using BuiltinFunction = std::function<Value(const std::vector<Value> &)>;
|
||||
using BuiltinFunction = std::function<Object(const std::vector<Object> &)>;
|
||||
|
||||
const std::unordered_map<FString, int> builtinFunctionArgCounts = {
|
||||
{u8"__fstdout_print", -1}, // variadic
|
||||
@@ -35,91 +35,91 @@ namespace Fig
|
||||
};
|
||||
|
||||
const std::unordered_map<FString, BuiltinFunction> builtinFunctions{
|
||||
{u8"__fstdout_print", [](const std::vector<Value> &args) -> Value {
|
||||
{u8"__fstdout_print", [](const std::vector<Object> &args) -> Object {
|
||||
for (auto arg : args)
|
||||
{
|
||||
std::print("{}", arg.toString().toBasicString());
|
||||
}
|
||||
return Value(Int(args.size()));
|
||||
return Object(Int(args.size()));
|
||||
}},
|
||||
{u8"__fstdout_println", [](const std::vector<Value> &args) -> Value {
|
||||
{u8"__fstdout_println", [](const std::vector<Object> &args) -> Object {
|
||||
for (auto arg : args)
|
||||
{
|
||||
std::print("{}", arg.toString().toBasicString());
|
||||
}
|
||||
std::print("\n");
|
||||
return Value(Int(args.size()));
|
||||
return Object(Int(args.size()));
|
||||
}},
|
||||
{u8"__fstdin_read", [](const std::vector<Value> &args) -> Value {
|
||||
{u8"__fstdin_read", [](const std::vector<Object> &args) -> Object {
|
||||
std::string input;
|
||||
std::cin >> input;
|
||||
return Value(FString::fromBasicString(input));
|
||||
return Object(FString::fromBasicString(input));
|
||||
}},
|
||||
{u8"__fstdin_readln", [](const std::vector<Value> &args) -> Value {
|
||||
{u8"__fstdin_readln", [](const std::vector<Object> &args) -> Object {
|
||||
std::string line;
|
||||
std::getline(std::cin, line);
|
||||
return Value(FString::fromBasicString(line));
|
||||
return Object(FString::fromBasicString(line));
|
||||
}},
|
||||
{u8"__fvalue_type", [](const std::vector<Value> &args) -> Value {
|
||||
return Value(args[0].getTypeInfo().toString());
|
||||
{u8"__fvalue_type", [](const std::vector<Object> &args) -> Object {
|
||||
return Object(args[0].getTypeInfo().toString());
|
||||
}},
|
||||
{u8"__fvalue_int_parse", [](const std::vector<Value> &args) -> Value {
|
||||
{u8"__fvalue_int_parse", [](const std::vector<Object> &args) -> Object {
|
||||
FString str = args[0].as<String>().getValue();
|
||||
try
|
||||
{
|
||||
ValueType::IntClass val = std::stoi(str.toBasicString());
|
||||
return Value(Int(val));
|
||||
return Object(Int(val));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw RuntimeError(FStringView(std::format("Invalid int string for parsing", str.toBasicString())));
|
||||
}
|
||||
}},
|
||||
{u8"__fvalue_int_from", [](const std::vector<Value> &args) -> Value {
|
||||
Value val = args[0];
|
||||
{u8"__fvalue_int_from", [](const std::vector<Object> &args) -> Object {
|
||||
Object val = args[0];
|
||||
if (val.is<Double>())
|
||||
{
|
||||
return Value(Int(static_cast<ValueType::IntClass>(val.as<Double>().getValue())));
|
||||
return Object(Int(static_cast<ValueType::IntClass>(val.as<Double>().getValue())));
|
||||
}
|
||||
else if (val.is<Bool>())
|
||||
{
|
||||
return Value(Int(val.as<Bool>().getValue() ? 1 : 0));
|
||||
return Object(Int(val.as<Bool>().getValue() ? 1 : 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw RuntimeError(FStringView(std::format("Type '{}' cannot be converted to int", val.getTypeInfo().toString().toBasicString())));
|
||||
}
|
||||
}},
|
||||
{u8"__fvalue_double_parse", [](const std::vector<Value> &args) -> Value {
|
||||
{u8"__fvalue_double_parse", [](const std::vector<Object> &args) -> Object {
|
||||
FString str = args[0].as<String>().getValue();
|
||||
try
|
||||
{
|
||||
ValueType::DoubleClass val = std::stod(str.toBasicString());
|
||||
return Value(Double(val));
|
||||
return Object(Double(val));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
throw RuntimeError(FStringView(std::format("Invalid double string for parsing", str.toBasicString())));
|
||||
}
|
||||
}},
|
||||
{u8"__fvalue_double_from", [](const std::vector<Value> &args) -> Value {
|
||||
Value val = args[0];
|
||||
{u8"__fvalue_double_from", [](const std::vector<Object> &args) -> Object {
|
||||
Object val = args[0];
|
||||
if (val.is<Int>())
|
||||
{
|
||||
return Value(Double(static_cast<ValueType::DoubleClass>(val.as<Int>().getValue())));
|
||||
return Object(Double(static_cast<ValueType::DoubleClass>(val.as<Int>().getValue())));
|
||||
}
|
||||
else if (val.is<Bool>())
|
||||
{
|
||||
return Value(Double(val.as<Bool>().getValue() ? 1.0 : 0.0));
|
||||
return Object(Double(val.as<Bool>().getValue() ? 1.0 : 0.0));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw RuntimeError(FStringView(std::format("Type '{}' cannot be converted to double", val.getTypeInfo().toString().toBasicString())));
|
||||
}
|
||||
}},
|
||||
{u8"__fvalue_string_from", [](const std::vector<Value> &args) -> Value {
|
||||
Value val = args[0];
|
||||
return Value(val.toString());
|
||||
{u8"__fvalue_string_from", [](const std::vector<Object> &args) -> Object {
|
||||
Object val = args[0];
|
||||
return Object(val.toString());
|
||||
}},
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user