Added size() function.

pull/3/head
yhirose 9 years ago
parent 8039e65143
commit d800dd26f7
  1. 19
      language/interpreter.hpp

@ -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…
Cancel
Save