feat(lua): add is_* predicates

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2013-11-08 12:40:28 -08:00
parent 5c35a9ad0a
commit cc17be1ef1
10 changed files with 67 additions and 0 deletions

View file

@ -111,6 +111,11 @@ static int format_highlight(lua_State * L) {
static int format_line(lua_State * L) { return push_format(L, line()); }
static int format_space(lua_State * L) { return push_format(L, space()); }
static int format_pred(lua_State * L) {
lua_pushboolean(L, is_format(L, 1));
return 1;
}
static const struct luaL_Reg format_m[] = {
{"__gc", format_gc}, // never throws
{"__tostring", safe_function<format_tostring>},
@ -130,5 +135,6 @@ void open_format(lua_State * L) {
set_global_function<mk_format>(L, "format");
set_global_function<format_line>(L, "line");
set_global_function<format_space>(L, "space");
set_global_function<format_pred>(L, "is_format");
}
}

View file

@ -72,6 +72,11 @@ static int name_lt(lua_State * L) {
return 1;
}
static int name_pred(lua_State * L) {
lua_pushboolean(L, is_name(L, 1));
return 1;
}
static const struct luaL_Reg name_m[] = {
{"__gc", name_gc}, // never throws
{"__tostring", safe_function<name_tostring>},
@ -85,5 +90,6 @@ void open_name(lua_State * L) {
setfuncs(L, name_m, 0);
set_global_function<mk_name>(L, "name");
set_global_function<name_pred>(L, "is_name");
}
}

View file

@ -102,6 +102,11 @@ static int mk_mpz(lua_State * L) {
return push_mpz(L, arg);
}
static int mpz_pred(lua_State * L) {
lua_pushboolean(L, is_mpz(L, 1));
return 1;
}
static const struct luaL_Reg mpz_m[] = {
{"__gc", mpz_gc}, // never throws
{"__tostring", safe_function<mpz_tostring>},
@ -121,6 +126,7 @@ void open_mpz(lua_State * L) {
setfuncs(L, mpz_m, 0);
set_global_function<mk_mpz>(L, "mpz");
set_global_function<mpz_pred>(L, "is_mpz");
}
constexpr char const * mpq_mt = "mpq.mt";
@ -213,6 +219,11 @@ static int mk_mpq(lua_State * L) {
return push_mpq(L, arg);
}
static int mpq_pred(lua_State * L) {
lua_pushboolean(L, is_mpq(L, 1));
return 1;
}
static const struct luaL_Reg mpq_m[] = {
{"__gc", mpq_gc}, // never throws
{"__tostring", safe_function<mpq_tostring>},
@ -232,5 +243,6 @@ void open_mpq(lua_State * L) {
setfuncs(L, mpq_m, 0);
set_global_function<mk_mpq>(L, "mpq");
set_global_function<mpq_pred>(L, "is_mpq");
}
}

View file

@ -165,6 +165,11 @@ static int options_update(lua_State * L) {
}
}
static int options_pred(lua_State * L) {
lua_pushboolean(L, is_options(L, 1));
return 1;
}
static const struct luaL_Reg options_m[] = {
{"__gc", options_gc}, // never throws
{"__tostring", safe_function<options_tostring>},
@ -195,5 +200,6 @@ void open_options(lua_State * L) {
setfuncs(L, options_m, 0);
set_global_function<mk_option>(L, "options");
set_global_function<options_pred>(L, "is_options");
}
}

View file

@ -169,6 +169,11 @@ static int sexpr_to_mpq(lua_State * L) {
return push_mpq(L, to_mpq(e));
}
static int sexpr_pred(lua_State * L) {
lua_pushboolean(L, is_sexpr(L, 1));
return 1;
}
static const struct luaL_Reg sexpr_m[] = {
{"__gc", sexpr_gc}, // never throws
{"__tostring", safe_function<sexpr_tostring>},
@ -205,5 +210,6 @@ void open_sexpr(lua_State * L) {
setfuncs(L, sexpr_m, 0);
set_global_function<mk_sexpr>(L, "sexpr");
set_global_function<sexpr_pred>(L, "is_sexpr");
}
}

4
tests/lua/format2.lua Normal file
View file

@ -0,0 +1,4 @@
f = format(format("hello"):highlight('red'), line(), 1):group() .. space() .. (line() .. format("world")):nest(4)
assert(is_format(f))
assert(not is_format("hello"))
assert(not is_format(sexpr(10, 20)))

6
tests/lua/mpz2.lua Normal file
View file

@ -0,0 +1,6 @@
assert(is_mpz(mpz(1000)))
assert(not is_mpz(10))
assert(not is_mpz(mpq(10)))
assert(is_mpq(mpq(1000)))
assert(not is_mpq(10))
assert(not is_mpq(mpz(10)))

6
tests/lua/n2.lua Normal file
View file

@ -0,0 +1,6 @@
assert(is_name(name("foo")))
assert(not is_name("foo"))
assert(not is_name(3))
a = name("foo", 1, 2)
print(a)
assert(is_name(a))

9
tests/lua/opt2.lua Normal file
View file

@ -0,0 +1,9 @@
opts = options()
assert(is_options(opts))
opts = opts:update(name('pp', 'colors'), false)
opts = opts:update(name('pp', 'colors'), true)
print(opts)
assert(is_options(opts))
assert(not is_options(name("foo")))
assert(not is_options("foo"))
assert(not is_options({}))

6
tests/lua/sexpr3.lua Normal file
View file

@ -0,0 +1,6 @@
l = sexpr(1, 2, 3)
print(l)
assert(is_sexpr(l))
assert(is_sexpr(sexpr(10)))
assert(not is_sexpr(10))
assert(not is_sexpr({}))