mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +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(ArrayValue&& a) : type(Array), v(a) {}
|
||||||
explicit Value(FunctionValue&& f) : type(Function), v(f) {}
|
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 {
|
bool to_bool() const {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Bool: return v.get<bool>();
|
case Bool: return v.get<bool>();
|
||||||
@ -286,6 +294,17 @@ struct Environment
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
false);
|
false);
|
||||||
|
|
||||||
|
initialize(
|
||||||
|
"size",
|
||||||
|
Value(Value::FunctionValue {
|
||||||
|
{ {"arg", true} },
|
||||||
|
[](std::shared_ptr<Environment> env) {
|
||||||
|
auto size = env->get("arg").size();
|
||||||
|
return Value(size);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Loading…
Reference in New Issue
Block a user