feat(api): add APIs for parsing files, commands and expressions
This commit is contained in:
parent
3c1d6ec67a
commit
36d2c63ad0
5 changed files with 160 additions and 1 deletions
|
@ -1,5 +1,6 @@
|
|||
add_library(api OBJECT string.cpp exception.cpp name.cpp options.cpp univ.cpp
|
||||
expr.cpp decl.cpp env.cpp ios.cpp module.cpp type_checker.cpp inductive.cpp)
|
||||
expr.cpp decl.cpp env.cpp ios.cpp module.cpp type_checker.cpp inductive.cpp
|
||||
parser.cpp)
|
||||
|
||||
FILE(GLOB LEAN_API_INCLUDE_FILES lean*.h)
|
||||
install(FILES ${LEAN_API_INCLUDE_FILES} DESTINATION include)
|
||||
|
|
|
@ -21,5 +21,6 @@ Author: Leonardo de Moura
|
|||
#include "lean_module.h" // NOLINT
|
||||
#include "lean_type_checker.h" // NOLINT
|
||||
#include "lean_inductive.h" // NOLINT
|
||||
#include "lean_parser.h" // NOLINT
|
||||
|
||||
#endif
|
||||
|
|
44
src/api/lean_parser.h
Normal file
44
src/api/lean_parser.h
Normal file
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
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_PARSER_H
|
||||
#define _LEAN_PARSER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
\defgroup capi C API
|
||||
*/
|
||||
/*@{*/
|
||||
|
||||
/**
|
||||
@name Parser API
|
||||
*/
|
||||
/*@{*/
|
||||
|
||||
/** \brief Parse the file \c fname using \c env and \c ios.
|
||||
Store the updated environment and ios at \c new_env and \c new_ios.
|
||||
\remark We return a new ios object because Lean has commands for updating configuration options. */
|
||||
lean_bool lean_parse_file(lean_env env, lean_ios ios, char const * fname, lean_env * new_env, lean_ios * new_ios, lean_exception * ex);
|
||||
|
||||
/** \brief Parse the commands in the string \c str using \c env and \c ios.
|
||||
Store the updated environment and ios at \c new_env and \c new_ios.
|
||||
\remark We return a new ios object because Lean has commands for updating configuration options. */
|
||||
lean_bool lean_parse_commands(lean_env env, lean_ios ios, char const * str, lean_env * new_env, lean_ios * new_ios, lean_exception * ex);
|
||||
|
||||
/** \brief Parse (and elaborate) the expression in the string \c str using \c env and \c ios.
|
||||
Store the elaborated expression in \c new_expr, and automatically generated universe parameters in \c new_ps. */
|
||||
lean_bool lean_parse_expr(lean_env env, lean_ios ios, char const * str, lean_expr * new_expr, lean_list_name * new_ps, lean_exception * ex);
|
||||
|
||||
/*@}*/
|
||||
/*@}*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif
|
||||
#endif
|
65
src/api/parser.cpp
Normal file
65
src/api/parser.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
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/parser.h"
|
||||
#include "api/string.h"
|
||||
#include "api/exception.h"
|
||||
#include "api/decl.h"
|
||||
#include "api/ios.h"
|
||||
#include "api/lean_parser.h"
|
||||
using namespace lean; // NOLINT
|
||||
|
||||
lean_bool lean_parse_file(lean_env env, lean_ios ios, char const * fname, lean_env * new_env, lean_ios * new_ios, lean_exception * ex) {
|
||||
LEAN_TRY;
|
||||
check_nonnull(env);
|
||||
check_nonnull(ios);
|
||||
check_nonnull(fname);
|
||||
environment _env = to_env_ref(env);
|
||||
io_state _ios = to_io_state_ref(ios);
|
||||
bool use_exceptions = true;
|
||||
unsigned num_threads = 1;
|
||||
parse_commands(_env, _ios, fname, use_exceptions, num_threads);
|
||||
*new_env = of_env(new environment(_env));
|
||||
*new_ios = of_io_state(new io_state(_ios));
|
||||
LEAN_CATCH;
|
||||
}
|
||||
|
||||
lean_bool lean_parse_commands(lean_env env, lean_ios ios, char const * str, lean_env * new_env, lean_ios * new_ios, lean_exception * ex) {
|
||||
LEAN_TRY;
|
||||
check_nonnull(env);
|
||||
check_nonnull(ios);
|
||||
check_nonnull(str);
|
||||
std::istringstream in(str);
|
||||
char const * strname = "[string]";
|
||||
environment _env = to_env_ref(env);
|
||||
io_state _ios = to_io_state_ref(ios);
|
||||
bool use_exceptions = true;
|
||||
unsigned num_threads = 1;
|
||||
parse_commands(_env, _ios, in, strname, use_exceptions, num_threads);
|
||||
*new_env = of_env(new environment(_env));
|
||||
*new_ios = of_io_state(new io_state(_ios));
|
||||
LEAN_CATCH;
|
||||
}
|
||||
|
||||
lean_bool lean_parse_expr(lean_env env, lean_ios ios, char const * str, lean_expr * new_expr, lean_list_name * new_ps, lean_exception * ex) {
|
||||
LEAN_TRY;
|
||||
check_nonnull(env);
|
||||
check_nonnull(ios);
|
||||
check_nonnull(str);
|
||||
std::istringstream in(str);
|
||||
char const * strname = "[string]";
|
||||
environment _env = to_env_ref(env);
|
||||
io_state _ios = to_io_state_ref(ios);
|
||||
bool use_exceptions = true;
|
||||
unsigned num_threads = 1;
|
||||
parser p(_env, _ios, in, strname, use_exceptions, num_threads);
|
||||
expr e = p.parse_expr();
|
||||
expr _e; level_param_names _ps;
|
||||
std::tie(_e, _ps) = p.elaborate(e, list<expr>());
|
||||
*new_expr = of_expr(new expr(_e));
|
||||
*new_ps = of_list_name(new list<name>(_ps));
|
||||
LEAN_CATCH;
|
||||
}
|
|
@ -464,11 +464,59 @@ void test_inductive() {
|
|||
lean_env_del(new_env);
|
||||
}
|
||||
|
||||
void test_parser() {
|
||||
lean_exception ex = 0;
|
||||
lean_env env = mk_env();
|
||||
lean_ios ios;
|
||||
lean_env new_env;
|
||||
lean_ios new_ios;
|
||||
lean_options o;
|
||||
check(lean_options_mk_empty(&o, &ex));
|
||||
check(lean_ios_mk_std(o, &ios, &ex));
|
||||
check(lean_parse_commands(env, ios, "import standard open nat definition double (a : nat) := a + a check double 4 eval double 4",
|
||||
&new_env, &new_ios, &ex));
|
||||
{
|
||||
lean_name double_name = mk_name("double");
|
||||
lean_decl double_decl;
|
||||
lean_expr double_decl_value;
|
||||
char const * s;
|
||||
check(lean_env_get_decl(new_env, double_name, &double_decl, &ex));
|
||||
check(lean_decl_get_value(double_decl, &double_decl_value, &ex));
|
||||
check(lean_expr_to_pp_string(new_env, new_ios, double_decl_value, &s, &ex));
|
||||
printf("definition of double\n%s\n", s);
|
||||
lean_name_del(double_name);
|
||||
lean_decl_del(double_decl);
|
||||
lean_expr_del(double_decl_value);
|
||||
lean_string_del(s);
|
||||
}
|
||||
|
||||
{
|
||||
lean_expr e;
|
||||
lean_list_name ps;
|
||||
// remark: we can use notation from the namespace nat because we have executed 'open nat'
|
||||
// when we created new_env
|
||||
check(lean_parse_expr(new_env, new_ios, "double (2 + 3)", &e, &ps, &ex));
|
||||
char const * s;
|
||||
check(lean_expr_to_pp_string(new_env, new_ios, e, &s, &ex));
|
||||
printf("parsed expression: %s\n", s);
|
||||
lean_string_del(s);
|
||||
lean_expr_del(e);
|
||||
lean_list_name_del(ps);
|
||||
}
|
||||
|
||||
lean_options_del(o);
|
||||
lean_env_del(env);
|
||||
lean_env_del(new_env);
|
||||
lean_ios_del(ios);
|
||||
lean_ios_del(new_ios);
|
||||
}
|
||||
|
||||
int main() {
|
||||
test_add_univ();
|
||||
test_id();
|
||||
test_path();
|
||||
test_import();
|
||||
test_inductive();
|
||||
test_parser();
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue