feat(api): add lean_ios API

This commit is contained in:
Leonardo de Moura 2015-08-23 08:19:25 -07:00
parent 98a27c53de
commit 5263ef4a74
4 changed files with 170 additions and 1 deletions

View file

@ -1,5 +1,5 @@
add_library(api OBJECT string.cpp exception.cpp name.cpp options.cpp univ.cpp
expr.cpp decl.cpp env.cpp)
expr.cpp decl.cpp env.cpp ios.cpp)
FILE(GLOB LEAN_API_INCLUDE_FILES lean*.h)
install(FILES ${LEAN_API_INCLUDE_FILES} DESTINATION include)

88
src/api/ios.cpp Normal file
View file

@ -0,0 +1,88 @@
/*
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include "frontends/lean/pp.h"
#include "api/string.h"
#include "api/exception.h"
#include "api/ios.h"
using namespace lean; // NOLINT
lean_bool lean_ios_mk_std(lean_options o, lean_ios * r, lean_exception * ex) {
LEAN_TRY;
check_nonnull(o);
io_state ios(mk_pretty_formatter_factory(), to_options_ref(o));
*r = of_io_state(new io_state(ios));
LEAN_CATCH;
}
lean_bool lean_ios_mk_buffered(lean_options o, lean_ios * r, lean_exception * ex) {
LEAN_TRY;
check_nonnull(o);
io_state ios(mk_pretty_formatter_factory(), to_options_ref(o));
ios.set_regular_channel(std::make_shared<string_output_channel>());
ios.set_diagnostic_channel(std::make_shared<string_output_channel>());
*r = of_io_state(new io_state(ios));
LEAN_CATCH;
}
void lean_ios_del(lean_ios ios) {
delete to_io_state(ios);
}
lean_bool lean_ios_is_std(lean_ios ios) {
if (!ios)
return lean_false;
return dynamic_cast<string_output_channel*>(&to_io_state_ref(ios).get_regular_channel()) != nullptr;
}
lean_bool lean_ios_set_options(lean_ios ios, lean_options o, lean_exception * ex) {
LEAN_TRY;
check_nonnull(ios);
check_nonnull(o);
to_io_state(ios)->set_options(to_options_ref(o));
LEAN_CATCH;
}
lean_bool lean_ios_get_options(lean_ios ios, lean_options * r, lean_exception * ex) {
LEAN_TRY;
check_nonnull(ios);
*r = of_options(new options(to_io_state_ref(ios).get_options()));
LEAN_CATCH;
}
static void check_nonstd(lean_ios ios) {
check_nonnull(ios);
if (lean_ios_is_std(ios))
throw exception("invalid argument, buffered IO state expected");
}
lean_bool lean_ios_get_regular(lean_ios ios, char const ** r, lean_exception * ex) {
LEAN_TRY;
check_nonstd(ios);
*r = mk_string(static_cast<string_output_channel&>(to_io_state_ref(ios).get_regular_channel()).str());
LEAN_CATCH;
}
lean_bool lean_ios_get_diagnostic(lean_ios ios, char const ** r, lean_exception * ex) {
LEAN_TRY;
check_nonstd(ios);
*r = mk_string(static_cast<string_output_channel&>(to_io_state_ref(ios).get_diagnostic_channel()).str());
LEAN_CATCH;
}
lean_bool lean_ios_reset_regular(lean_ios ios, lean_exception * ex) {
LEAN_TRY;
check_nonstd(ios);
to_io_state(ios)->set_regular_channel(std::make_shared<string_output_channel>());
LEAN_CATCH;
}
lean_bool lean_ios_reset_diagnostic(lean_ios ios, lean_exception * ex) {
LEAN_TRY;
check_nonstd(ios);
to_io_state(ios)->set_diagnostic_channel(std::make_shared<string_output_channel>());
LEAN_CATCH;
}

15
src/api/ios.h Normal file
View file

@ -0,0 +1,15 @@
/*
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#pragma once
#include "library/io_state.h"
#include "api/expr.h"
#include "api/lean_ios.h"
namespace lean {
inline io_state * to_io_state(lean_ios n) { return reinterpret_cast<io_state *>(n); }
inline io_state const & to_io_state_ref(lean_ios n) { return *reinterpret_cast<io_state *>(n); }
inline lean_ios of_io_state(io_state * n) { return reinterpret_cast<lean_ios>(n); }
}

66
src/api/lean_ios.h Normal file
View file

@ -0,0 +1,66 @@
/*
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#ifndef _LEAN_IOS_H
#define _LEAN_IOS_H
#ifdef __cplusplus
extern "C" {
#endif
/**
\defgroup capi C API
*/
/*@{*/
/**
@name IO state API
*/
/*@{*/
LEAN_DEFINE_TYPE(lean_ios);
/** \brief Create IO state object that sends the regular and diagnostic output to standard out and standard error */
lean_bool lean_ios_mk_std(lean_options o, lean_ios * r, lean_exception * ex);
/** \brief Create IO state object that sends the regular and diagnostic output to string buffers. */
lean_bool lean_ios_mk_buffered(lean_options o, lean_ios * r, lean_exception * ex);
/** \brief Delete IO state object. */
void lean_ios_del(lean_ios ios);
/** \brief Return true iff it is standard IO state (i.e., it was created using #lean_ios_mk_std */
lean_bool lean_ios_is_std(lean_ios ios);
/** \brief Update the set of configurations options in the given IO state object */
lean_bool lean_ios_set_options(lean_ios ios, lean_options o, lean_exception * ex);
/** \brief Store in \c r the set of configuration options associated with the given IO state object. */
lean_bool lean_ios_get_options(lean_ios ios, lean_options * r, lean_exception * ex);
/** \brief Store in \c r the content of the regular output stream.
\pre !lean_ios_is_std(ios) */
lean_bool lean_ios_get_regular(lean_ios ios, char const ** r, lean_exception * ex);
/** \brief Store in \c r the content of the diagnostic output stream.
\pre !lean_ios_is_std(ios) */
lean_bool lean_ios_get_diagnostic(lean_ios ios, char const ** r, lean_exception * ex);
/** \brief Reset the regular output stream.
\pre !lean_ios_is_std(ios) */
lean_bool lean_ios_reset_regular(lean_ios ios, lean_exception * ex);
/** \brief Reset the diagnostic output stream.
\pre !lean_ios_is_std(ios) */
lean_bool lean_ios_reset_diagnostic(lean_ios ios, lean_exception * ex);
/*@}*/
/*@}*/
#ifdef __cplusplus
};
#endif
#endif