diff --git a/language/interpreter.hpp b/language/interpreter.hpp index c2fb2ce..b7dc550 100644 --- a/language/interpreter.hpp +++ b/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(); @@ -286,6 +294,17 @@ struct Environment } }), false); + + initialize( + "size", + Value(Value::FunctionValue { + { {"arg", true} }, + [](std::shared_ptr env) { + auto size = env->get("arg").size(); + return Value(size); + } + }), + false); } private: