diff --git a/src/api/CMakeLists.txt b/src/api/CMakeLists.txt index eb1b10759..40b2a1105 100644 --- a/src/api/CMakeLists.txt +++ b/src/api/CMakeLists.txt @@ -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) diff --git a/src/api/ios.cpp b/src/api/ios.cpp new file mode 100644 index 000000000..ef47ee52f --- /dev/null +++ b/src/api/ios.cpp @@ -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()); + ios.set_diagnostic_channel(std::make_shared()); + *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(&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(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(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()); + 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()); + LEAN_CATCH; +} diff --git a/src/api/ios.h b/src/api/ios.h new file mode 100644 index 000000000..9519e874a --- /dev/null +++ b/src/api/ios.h @@ -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(n); } +inline io_state const & to_io_state_ref(lean_ios n) { return *reinterpret_cast(n); } +inline lean_ios of_io_state(io_state * n) { return reinterpret_cast(n); } +} diff --git a/src/api/lean_ios.h b/src/api/lean_ios.h new file mode 100644 index 000000000..79715ec23 --- /dev/null +++ b/src/api/lean_ios.h @@ -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