mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 11:55:30 +00:00
Added size() function.
This commit is contained in:
parent
8039e65143
commit
d800dd26f7
@ -55,6 +55,14 @@ struct Value
|
||||
explicit Value(ArrayValue&& a) : type(Array), v(a) {}
|
||||
explicit Value(FunctionValue&& f) : type(Function), v(f) {}
|
||||
|
||||
long size() const {
|
||||
switch (type) {
|
||||
case String: return to_string().size();
|
||||
case Array: return to_array().values.size();
|
||||
}
|
||||
throw std::runtime_error("type error.");
|
||||
}
|
||||
|
||||
bool to_bool() const {
|
||||
switch (type) {
|
||||
case Bool: return v.get<bool>();
|
||||
@ -286,6 +294,17 @@ struct Environment
|
||||
}
|
||||
}),
|
||||
false);
|
||||
|
||||
initialize(
|
||||
"size",
|
||||
Value(Value::FunctionValue {
|
||||
{ {"arg", true} },
|
||||
[](std::shared_ptr<Environment> env) {
|
||||
auto size = env->get("arg").size();
|
||||
return Value(size);
|
||||
}
|
||||
}),
|
||||
false);
|
||||
}
|
||||
|
||||
private:
|
||||
|
Loading…
Reference in New Issue
Block a user