From 4fdc0406bebac994c7559c0b1814804657f8285e Mon Sep 17 00:00:00 2001 From: Leonardo de Moura Date: Tue, 7 Jan 2014 17:35:34 -0800 Subject: [PATCH] feat(util/name): additional methods to name Lua API Signed-off-by: Leonardo de Moura --- src/util/name.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/src/util/name.cpp b/src/util/name.cpp index 9ccc093c0..b9a9127ad 100644 --- a/src/util/name.cpp +++ b/src/util/name.cpp @@ -490,12 +490,50 @@ static int name_hash(lua_State * L) { return 1; } +#define NAME_PRED(P) \ +static int name_ ## P(lua_State * L) { \ + lua_pushboolean(L, to_name(L, 1).P()); \ + return 1; \ +} + +NAME_PRED(is_atomic) +NAME_PRED(is_anonymous) +NAME_PRED(is_string) +NAME_PRED(is_numeral) + +static int name_get_prefix(lua_State * L) { + if (to_name(L, 1).is_atomic()) + throw exception("invalid get_prefix, non-atomic name expected"); + return push_name(L, to_name(L, 1).get_prefix()); +} + +static int name_get_numeral(lua_State * L) { + if (to_name(L, 1).is_numeral()) + throw exception("invalid get_numeral, hierarchical name with numeric head expected"); + lua_pushinteger(L, to_name(L, 1).get_numeral()); + return 1; +} + +static int name_get_string(lua_State * L) { + if (to_name(L, 1).is_string()) + throw exception("invalid get_string, hierarchical name with string head expected"); + lua_pushstring(L, to_name(L, 1).get_string()); + return 1; +} + static const struct luaL_Reg name_m[] = { - {"__gc", name_gc}, // never throws - {"__tostring", safe_function}, - {"__eq", safe_function}, - {"__lt", safe_function}, - {"hash", safe_function}, + {"__gc", name_gc}, // never throws + {"__tostring", safe_function}, + {"__eq", safe_function}, + {"__lt", safe_function}, + {"is_atomic", safe_function}, + {"is_anonymous", safe_function}, + {"is_numeral", safe_function}, + {"is_string", safe_function}, + {"get_prefix", safe_function}, + {"get_numeral", safe_function}, + {"get_string", safe_function}, + {"hash", safe_function}, {0, 0} };