feat(library/app_builder): expose app_builder API in Lua

This commit is contained in:
Leonardo de Moura 2015-01-29 11:38:56 -08:00
parent 5efd91e435
commit f587cc3e1d
3 changed files with 57 additions and 0 deletions

View file

@ -9,6 +9,7 @@ Author: Leonardo de Moura
#include "kernel/instantiate.h"
#include "library/match.h"
#include "library/app_builder.h"
#include "library/kernel_bindings.h"
namespace lean {
@ -229,4 +230,56 @@ optional<expr> app_builder::mk_app(name const & n, expr const & a1, expr const &
}
void app_builder::push() { m_ptr->push(); }
void app_builder::pop() { m_ptr->pop(); }
struct lua_app_builder {
type_checker_ref m_tc;
app_builder m_builder;
lua_app_builder(type_checker_ref const & r):m_tc(r), m_builder(*r.get()) {}
};
typedef std::shared_ptr<lua_app_builder> app_builder_ref;
DECL_UDATA(app_builder_ref)
static int mk_app_builder(lua_State * L) {
return push_app_builder_ref(L, std::make_shared<lua_app_builder>(to_type_checker_ref(L, 1)));
}
static int app_builder_mk_app(lua_State * L) {
int nargs = lua_gettop(L);
buffer<expr> args;
app_builder & b = to_app_builder_ref(L, 1)->m_builder;
name n = to_name_ext(L, 2);
for (int i = 3; i <= nargs; i++)
args.push_back(to_expr(L, i));
return push_optional_expr(L, b.mk_app(n, args.size(), args.data()));
}
static int app_builder_push(lua_State * L) {
to_app_builder_ref(L, 1)->m_builder.push();
return 0;
}
static int app_builder_pop(lua_State * L) {
to_app_builder_ref(L, 1)->m_builder.pop();
return 0;
}
static const struct luaL_Reg app_builder_ref_m[] = {
{"__gc", app_builder_ref_gc},
{"mk_app", safe_function<app_builder_mk_app>},
{"push", safe_function<app_builder_push>},
{"pop", safe_function<app_builder_pop>},
{0, 0}
};
void open_app_builder(lua_State * L) {
luaL_newmetatable(L, app_builder_ref_mt);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
setfuncs(L, app_builder_ref_m, 0);
SET_GLOBAL_FUN(mk_app_builder, "app_builder");
SET_GLOBAL_FUN(app_builder_ref_pred, "is_app_builder");
}
}

View file

@ -54,4 +54,6 @@ public:
*/
void pop();
};
void open_app_builder(lua_State * L);
}

View file

@ -18,6 +18,7 @@ Author: Leonardo de Moura
#include "library/scoped_ext.h"
#include "library/match.h"
#include "library/reducible.h"
#include "library/app_builder.h"
namespace lean {
inline void open_core_module(lua_State * L) {
@ -33,6 +34,7 @@ inline void open_core_module(lua_State * L) {
open_explicit(L);
open_match(L);
open_reducible(L);
open_app_builder(L);
}
inline void register_core_module() {
script_state::register_module(open_core_module);