[Feat] 为List增加push函数
This commit is contained in:
@@ -81,27 +81,38 @@ namespace Fig
|
|||||||
{ValueType::Function, {}},
|
{ValueType::Function, {}},
|
||||||
{ValueType::StructType, {}},
|
{ValueType::StructType, {}},
|
||||||
{ValueType::StructInstance, {}},
|
{ValueType::StructInstance, {}},
|
||||||
{ValueType::List, {{u8"length", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
{ValueType::List, {
|
||||||
if (args.size() != 0)
|
{u8"length", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||||
throw RuntimeError(FString(
|
if (args.size() != 0)
|
||||||
std::format("`length` expects 0 arguments, {} got", args.size())));
|
throw RuntimeError(FString(
|
||||||
const List &list = as<List>();
|
std::format("`length` expects 0 arguments, {} got", args.size())));
|
||||||
return std::make_shared<Object>(static_cast<ValueType::IntClass>(list.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)
|
{u8"get", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||||
throw RuntimeError(FString(
|
if (args.size() != 1)
|
||||||
std::format("`get` expects 1 arguments, {} got", args.size())));
|
throw RuntimeError(FString(
|
||||||
ObjectPtr arg = args[0];
|
std::format("`get` expects 1 arguments, {} got", args.size())));
|
||||||
if (arg->getTypeInfo() != ValueType::Int)
|
ObjectPtr arg = args[0];
|
||||||
throw RuntimeError(FString(
|
if (arg->getTypeInfo() != ValueType::Int)
|
||||||
std::format("`get` argument 1 expects Int, {} got", arg->getTypeInfo().toString().toBasicString())));
|
throw RuntimeError(FString(
|
||||||
ValueType::IntClass i = arg->as<ValueType::IntClass>();
|
std::format("`get` argument 1 expects Int, {} got", arg->getTypeInfo().toString().toBasicString())));
|
||||||
const List &list = as<List>();
|
ValueType::IntClass i = arg->as<ValueType::IntClass>();
|
||||||
if (i >= list.size())
|
const List &list = as<List>();
|
||||||
return Object::getNullInstance();
|
if (i >= list.size())
|
||||||
return list[i];
|
return Object::getNullInstance();
|
||||||
}}}},
|
return list[i];
|
||||||
|
}},
|
||||||
|
{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, {
|
{ValueType::Map, {
|
||||||
{u8"get", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
{u8"get", [this](std::vector<ObjectPtr> args) -> ObjectPtr {
|
||||||
if (args.size() != 1)
|
if (args.size() != 1)
|
||||||
@@ -135,7 +146,7 @@ namespace Fig
|
|||||||
{ValueType::Function, {}},
|
{ValueType::Function, {}},
|
||||||
{ValueType::StructType, {}},
|
{ValueType::StructType, {}},
|
||||||
{ValueType::StructInstance, {}},
|
{ValueType::StructInstance, {}},
|
||||||
{ValueType::List, {{u8"length", 0}, {u8"get", 1}}},
|
{ValueType::List, {{u8"length", 0}, {u8"get", 1}, {u8"push", 1}}},
|
||||||
{ValueType::Map, {
|
{ValueType::Map, {
|
||||||
{u8"get", 1},
|
{u8"get", 1},
|
||||||
{u8"contains", 1},
|
{u8"contains", 1},
|
||||||
|
|||||||
Reference in New Issue
Block a user