feat(library/kernel_bindings): justification Lua API
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
340c0e0945
commit
9f5122b4c7
1 changed files with 53 additions and 112 deletions
|
@ -830,135 +830,74 @@ io_state * get_io_state(lua_State * L) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Justification
|
||||||
DECL_UDATA(justification)
|
DECL_UDATA(justification)
|
||||||
|
|
||||||
int push_optional_justification(lua_State * L, optional<justification> const & j) {
|
int push_optional_justification(lua_State * L, optional<justification> const & j) { return j ? push_justification(L, *j) : pushnil(L); }
|
||||||
if (j)
|
|
||||||
push_justification(L, *j);
|
|
||||||
else
|
|
||||||
lua_pushnil(L);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if 0
|
|
||||||
static int justification_tostring(lua_State * L) {
|
static int justification_tostring(lua_State * L) {
|
||||||
std::ostringstream out;
|
std::ostringstream out;
|
||||||
justification & jst = to_justification(L, 1);
|
justification & jst = to_justification(L, 1);
|
||||||
if (jst) {
|
out << jst;
|
||||||
formatter fmt = get_global_formatter(L);
|
|
||||||
options opts = get_global_options(L);
|
|
||||||
out << mk_pair(jst.pp(fmt, opts), opts);
|
|
||||||
} else {
|
|
||||||
out << "<null-justification>";
|
|
||||||
}
|
|
||||||
lua_pushstring(L, out.str().c_str());
|
lua_pushstring(L, out.str().c_str());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int justification_has_children(lua_State * L) {
|
#define JST_PRED(P) static int justification_ ## P(lua_State * L) { return pushboolean(L, to_justification(L, 1).P()); }
|
||||||
lua_pushboolean(L, to_justification(L, 1).has_children());
|
JST_PRED(is_none)
|
||||||
return 1;
|
JST_PRED(is_asserted)
|
||||||
}
|
JST_PRED(is_assumption)
|
||||||
|
JST_PRED(is_composite)
|
||||||
static int justification_is_null(lua_State * L) {
|
static int justification_get_main_expr(lua_State * L) { return push_optional_expr(L, to_justification(L, 1).get_main_expr()); }
|
||||||
lua_pushboolean(L, !to_justification(L, 1));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
\brief Iterator (closure base function) for justification children. See \c justification_children
|
|
||||||
*/
|
|
||||||
static int justification_next_child(lua_State * L) {
|
|
||||||
unsigned i = lua_tointeger(L, lua_upvalueindex(2));
|
|
||||||
unsigned num = objlen(L, lua_upvalueindex(1));
|
|
||||||
if (i > num) {
|
|
||||||
lua_pushnil(L);
|
|
||||||
} else {
|
|
||||||
lua_pushinteger(L, i + 1);
|
|
||||||
lua_replace(L, lua_upvalueindex(2)); // update i
|
|
||||||
lua_rawgeti(L, lua_upvalueindex(1), i); // read children[i]
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int justification_children(lua_State * L) {
|
|
||||||
buffer<justification_cell*> children;
|
|
||||||
to_justification(L, 1).get_children(children);
|
|
||||||
lua_newtable(L);
|
|
||||||
int i = 1;
|
|
||||||
for (auto jcell : children) {
|
|
||||||
push_justification(L, justification(jcell));
|
|
||||||
lua_rawseti(L, -2, i);
|
|
||||||
i = i + 1;
|
|
||||||
}
|
|
||||||
lua_pushinteger(L, 1);
|
|
||||||
lua_pushcclosure(L, &safe_function<justification_next_child>, 2); // create closure with 2 upvalues
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int justification_get_main_expr(lua_State * L) {
|
|
||||||
optional<expr> r = to_justification(L, 1).get_main_expr();
|
|
||||||
if (r)
|
|
||||||
push_expr(L, *r);
|
|
||||||
else
|
|
||||||
lua_pushnil(L);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int justification_pp(lua_State * L) {
|
static int justification_pp(lua_State * L) {
|
||||||
int nargs = lua_gettop(L);
|
int nargs = lua_gettop(L);
|
||||||
justification & jst = to_justification(L, 1);
|
justification & j = to_justification(L, 1);
|
||||||
formatter fmt = get_global_formatter(L);
|
if (nargs == 1)
|
||||||
options opts = get_global_options(L);
|
return push_format(L, j.pp(get_global_formatter(L), get_global_options(L), nullptr, substitution()));
|
||||||
bool display_children = true;
|
else if (nargs == 2 && is_substitution(L, 2))
|
||||||
|
return push_format(L, j.pp(get_global_formatter(L), get_global_options(L), nullptr, to_substitution(L, 2)));
|
||||||
if (nargs == 2) {
|
else if (nargs == 2)
|
||||||
if (lua_isboolean(L, 2)) {
|
return push_format(L, j.pp(to_formatter(L, 2), get_global_options(L), nullptr, substitution()));
|
||||||
display_children = lua_toboolean(L, 2);
|
else if (nargs == 3 && is_substitution(L, 3))
|
||||||
} else {
|
return push_format(L, j.pp(to_formatter(L, 2), get_global_options(L), nullptr, to_substitution(L, 3)));
|
||||||
luaL_checktype(L, 2, LUA_TTABLE);
|
else if (nargs == 3)
|
||||||
|
return push_format(L, j.pp(to_formatter(L, 2), to_options(L, 3), nullptr, substitution()));
|
||||||
lua_pushstring(L, "formatter");
|
else
|
||||||
lua_gettable(L, 2);
|
return push_format(L, j.pp(to_formatter(L, 2), to_options(L, 3), nullptr, to_substitution(L, 4)));
|
||||||
if (is_formatter(L, -1))
|
|
||||||
fmt = to_formatter(L, -1);
|
|
||||||
lua_pop(L, 1);
|
|
||||||
|
|
||||||
lua_pushstring(L, "options");
|
|
||||||
lua_gettable(L, 2);
|
|
||||||
if (is_options(L, -1))
|
|
||||||
opts = to_options(L, -1);
|
|
||||||
lua_pop(L, 1);
|
|
||||||
|
|
||||||
lua_pushstring(L, "display_children");
|
|
||||||
lua_gettable(L, 2);
|
|
||||||
if (lua_isboolean(L, -1))
|
|
||||||
display_children = lua_toboolean(L, -1);
|
|
||||||
lua_pop(L, 1);
|
|
||||||
}
|
}
|
||||||
|
static int justification_assumption_idx(lua_State * L) {
|
||||||
|
if (!to_justification(L, 1).is_assumption())
|
||||||
|
throw exception("arg #1 must be an assumption justification");
|
||||||
|
return pushinteger(L, assumption_idx(to_justification(L, 1)));
|
||||||
}
|
}
|
||||||
return push_format(L, jst.pp(fmt, opts, nullptr, display_children));
|
static int justification_child1(lua_State * L) {
|
||||||
|
if (!to_justification(L, 1).is_composite())
|
||||||
|
throw exception("arg #1 must be a composite justification");
|
||||||
|
return push_justification(L, composite_child1(to_justification(L, 1)));
|
||||||
}
|
}
|
||||||
|
static int justification_child2(lua_State * L) {
|
||||||
static int justification_depends_on(lua_State * L) {
|
if (!to_justification(L, 1).is_composite())
|
||||||
lua_pushboolean(L, depends_on(to_justification(L, 1), to_justification(L, 2)));
|
throw exception("arg #1 must be a composite justification");
|
||||||
return 1;
|
return push_justification(L, composite_child2(to_justification(L, 1)));
|
||||||
}
|
}
|
||||||
|
static int justification_depends_on(lua_State * L) { return pushboolean(L, depends_on(to_justification(L, 1), pushinteger(L, 2))); }
|
||||||
static int mk_assumption_justification(lua_State * L) {
|
static int mk_assumption_justification(lua_State * L) { return push_justification(L, mk_assumption_justification(pushinteger(L, 1))); }
|
||||||
return push_justification(L, mk_assumption_justification(luaL_checkinteger(L, 1)));
|
static int mk_justification(lua_State * L) { return push_justification(L, justification()); }
|
||||||
}
|
static int mk_composite1(lua_State * L) { return push_justification(L, mk_composite1(to_justification(L, 1), to_justification(L, 2))); }
|
||||||
#endif
|
|
||||||
|
|
||||||
static const struct luaL_Reg justification_m[] = {
|
static const struct luaL_Reg justification_m[] = {
|
||||||
{"__gc", justification_gc}, // never throws
|
{"__gc", justification_gc}, // never throws
|
||||||
// {"__tostring", safe_function<justification_tostring>},
|
{"__tostring", safe_function<justification_tostring>},
|
||||||
// {"is_null", safe_function<justification_is_null>},
|
{"is_none", safe_function<justification_is_none>},
|
||||||
// {"has_children", safe_function<justification_has_children>},
|
{"is_asserted", safe_function<justification_is_asserted>},
|
||||||
// {"children", safe_function<justification_children>},
|
{"is_assumption", safe_function<justification_is_assumption>},
|
||||||
// {"get_main_expr", safe_function<justification_get_main_expr>},
|
{"is_composite", safe_function<justification_is_composite>},
|
||||||
// {"pp", safe_function<justification_pp>},
|
{"get_main_expr", safe_function<justification_get_main_expr>},
|
||||||
// {"depends_on", safe_function<justification_depends_on>},
|
{"pp", safe_function<justification_pp>},
|
||||||
|
{"depends_on", safe_function<justification_depends_on>},
|
||||||
|
{"assumption_idx", safe_function<justification_assumption_idx>},
|
||||||
|
{"child1", safe_function<justification_child1>},
|
||||||
|
{"child2", safe_function<justification_child2>},
|
||||||
{0, 0}
|
{0, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -968,7 +907,9 @@ static void open_justification(lua_State * L) {
|
||||||
lua_setfield(L, -2, "__index");
|
lua_setfield(L, -2, "__index");
|
||||||
setfuncs(L, justification_m, 0);
|
setfuncs(L, justification_m, 0);
|
||||||
|
|
||||||
// SET_GLOBAL_FUN(mk_assumption_justification, "mk_assumption_justification");
|
SET_GLOBAL_FUN(mk_justification, "justification");
|
||||||
|
SET_GLOBAL_FUN(mk_assumption_justification, "assumption_justification");
|
||||||
|
SET_GLOBAL_FUN(mk_composite1, "mk_composite_justification");
|
||||||
SET_GLOBAL_FUN(justification_pred, "is_justification");
|
SET_GLOBAL_FUN(justification_pred, "is_justification");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue