2013-09-13 03:04:10 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2013-07-17 05:10:18 +00:00
|
|
|
#include <iostream>
|
2013-08-18 23:23:29 +00:00
|
|
|
#include <fstream>
|
2013-08-24 23:11:35 +00:00
|
|
|
#include <signal.h>
|
2013-12-04 17:57:33 +00:00
|
|
|
#include <cstdlib>
|
2013-11-20 01:04:52 +00:00
|
|
|
#include <getopt.h>
|
2013-12-04 19:21:26 +00:00
|
|
|
#include <string>
|
2013-12-01 20:42:32 +00:00
|
|
|
#include "util/stackinfo.h"
|
2013-11-20 03:30:39 +00:00
|
|
|
#include "util/debug.h"
|
2013-11-13 05:42:22 +00:00
|
|
|
#include "util/interrupt.h"
|
2013-11-27 22:57:33 +00:00
|
|
|
#include "util/script_state.h"
|
2013-12-10 01:33:47 +00:00
|
|
|
#include "util/thread.h"
|
2014-06-18 04:38:10 +00:00
|
|
|
#include "util/thread_script_state.h"
|
2013-12-23 01:56:53 +00:00
|
|
|
#include "util/lean_path.h"
|
2013-12-18 22:37:55 +00:00
|
|
|
#include "kernel/environment.h"
|
2013-12-29 10:44:49 +00:00
|
|
|
#include "kernel/kernel_exception.h"
|
|
|
|
#include "kernel/formatter.h"
|
2014-07-05 17:31:27 +00:00
|
|
|
#include "library/standard_kernel.h"
|
|
|
|
#include "library/hott_kernel.h"
|
2014-06-14 15:01:52 +00:00
|
|
|
#include "library/module.h"
|
2014-06-11 18:26:17 +00:00
|
|
|
#include "library/io_state_stream.h"
|
2014-04-29 18:52:09 +00:00
|
|
|
#include "library/error_handling/error_handling.h"
|
2014-06-11 18:26:17 +00:00
|
|
|
#include "frontends/lean/parser.h"
|
2014-07-09 08:12:36 +00:00
|
|
|
#include "frontends/lean/pp.h"
|
2014-06-14 17:38:42 +00:00
|
|
|
#include "frontends/lean/interactive.h"
|
2014-06-29 00:43:30 +00:00
|
|
|
#include "frontends/lean/dependencies.h"
|
2014-04-29 00:57:39 +00:00
|
|
|
#include "frontends/lua/register_modules.h"
|
2013-07-17 05:10:18 +00:00
|
|
|
#include "version.h"
|
2013-12-04 19:21:26 +00:00
|
|
|
#include "githash.h" // NOLINT
|
2013-09-13 23:14:24 +00:00
|
|
|
|
2013-11-27 22:57:33 +00:00
|
|
|
using lean::script_state;
|
2013-11-20 03:30:39 +00:00
|
|
|
using lean::unreachable_reached;
|
2014-04-30 17:28:07 +00:00
|
|
|
using lean::environment;
|
2014-04-29 18:52:09 +00:00
|
|
|
using lean::io_state;
|
2014-04-30 17:28:07 +00:00
|
|
|
using lean::io_state_stream;
|
|
|
|
using lean::regular;
|
2014-06-11 18:26:17 +00:00
|
|
|
using lean::mk_environment;
|
2014-06-16 22:50:27 +00:00
|
|
|
using lean::mk_hott_environment;
|
2014-06-18 05:36:47 +00:00
|
|
|
using lean::set_environment;
|
|
|
|
using lean::set_io_state;
|
2014-04-29 00:49:23 +00:00
|
|
|
|
2014-06-15 17:50:35 +00:00
|
|
|
enum class input_kind { Unspecified, Lean, Lua };
|
2013-11-20 00:47:47 +00:00
|
|
|
|
2013-09-20 04:26:01 +00:00
|
|
|
static void on_ctrl_c(int ) {
|
2013-11-13 05:42:22 +00:00
|
|
|
lean::request_interrupt();
|
2013-08-24 23:11:35 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 03:30:39 +00:00
|
|
|
static void display_header(std::ostream & out) {
|
2013-12-04 19:21:26 +00:00
|
|
|
out << "Lean (version " << LEAN_VERSION_MAJOR << "." << LEAN_VERSION_MINOR << ", commit " << std::string(g_githash).substr(0, 12) << ")\n";
|
2013-11-20 03:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void display_help(std::ostream & out) {
|
|
|
|
display_header(out);
|
|
|
|
std::cout << "Input format:\n";
|
2013-12-10 01:33:47 +00:00
|
|
|
std::cout << " --lean use parser for Lean default input format for files,\n";
|
|
|
|
std::cout << " with unknown extension (default)\n";
|
|
|
|
std::cout << " --lua use Lua parser for files with unknown extension\n";
|
2013-11-20 03:30:39 +00:00
|
|
|
std::cout << "Miscellaneous:\n";
|
2013-12-10 01:33:47 +00:00
|
|
|
std::cout << " --help -h display this message\n";
|
|
|
|
std::cout << " --version -v display version number\n";
|
|
|
|
std::cout << " --githash display the git commit hash number used to build this binary\n";
|
2013-12-23 01:56:53 +00:00
|
|
|
std::cout << " --path display the path used for finding Lean libraries and extensions\n";
|
2013-12-29 01:31:35 +00:00
|
|
|
std::cout << " --output=file -o save the final environment in binary format in the given file\n";
|
2013-12-10 01:33:47 +00:00
|
|
|
std::cout << " --luahook=num -c how often the Lua interpreter checks the interrupted flag,\n";
|
|
|
|
std::cout << " it is useful for interrupting non-terminating user scripts,\n";
|
|
|
|
std::cout << " 0 means 'do not check'.\n";
|
2014-06-11 18:26:17 +00:00
|
|
|
std::cout << " --trust=num -t trust level (default: 0) \n";
|
2014-01-09 23:31:58 +00:00
|
|
|
std::cout << " --quiet -q do not print verbose messages\n";
|
2014-06-14 17:38:42 +00:00
|
|
|
std::cout << " --interactive -i read blocks of commands from the input stream\n";
|
2014-06-16 22:50:27 +00:00
|
|
|
std::cout << " --hott use Homotopy Type Theory kernel and libraries\n";
|
2014-06-16 23:37:46 +00:00
|
|
|
std::cout << " --threads=num -r number of threads used to process lean files\n";
|
2014-06-29 00:43:30 +00:00
|
|
|
std::cout << " --deps just print dependencies of a Lean input\n";
|
2013-12-10 01:33:47 +00:00
|
|
|
#if defined(LEAN_USE_BOOST)
|
|
|
|
std::cout << " --tstack=num -s thread stack size in Kb\n";
|
|
|
|
#endif
|
2013-11-20 03:30:39 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 00:47:47 +00:00
|
|
|
static char const * get_file_extension(char const * fname) {
|
|
|
|
if (fname == 0)
|
|
|
|
return 0;
|
|
|
|
char const * last_dot = 0;
|
|
|
|
while (true) {
|
|
|
|
char const * tmp = strchr(fname, '.');
|
|
|
|
if (tmp == 0) {
|
|
|
|
return last_dot;
|
|
|
|
}
|
|
|
|
last_dot = tmp + 1;
|
|
|
|
fname = last_dot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 03:30:39 +00:00
|
|
|
static struct option g_long_options[] = {
|
2014-06-14 17:38:42 +00:00
|
|
|
{"version", no_argument, 0, 'v'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"lean", no_argument, 0, 'l'},
|
|
|
|
{"lua", no_argument, 0, 'u'},
|
|
|
|
{"path", no_argument, 0, 'p'},
|
|
|
|
{"luahook", required_argument, 0, 'c'},
|
|
|
|
{"githash", no_argument, 0, 'g'},
|
|
|
|
{"output", required_argument, 0, 'o'},
|
|
|
|
{"trust", required_argument, 0, 't'},
|
|
|
|
{"interactive", no_argument, 0, 'i'},
|
|
|
|
{"quiet", no_argument, 0, 'q'},
|
2014-06-16 22:50:27 +00:00
|
|
|
{"hott", no_argument, 0, 'H'},
|
2014-06-16 23:37:46 +00:00
|
|
|
{"threads", required_argument, 0, 'r'},
|
2014-06-29 00:43:30 +00:00
|
|
|
{"deps", no_argument, 0, 'D'},
|
2013-12-10 01:33:47 +00:00
|
|
|
#if defined(LEAN_USE_BOOST)
|
2014-06-14 17:38:42 +00:00
|
|
|
{"tstack", required_argument, 0, 's'},
|
2013-12-10 01:33:47 +00:00
|
|
|
#endif
|
2013-11-20 03:30:39 +00:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
2013-08-24 23:11:35 +00:00
|
|
|
|
2014-06-11 18:26:17 +00:00
|
|
|
#if defined(LEAN_USE_BOOST)
|
2014-06-29 00:43:30 +00:00
|
|
|
static char const * g_opt_str = "DHiqlupgvhr:012c:012s:012t:012o:";
|
2014-06-11 18:26:17 +00:00
|
|
|
#else
|
2014-06-29 00:43:30 +00:00
|
|
|
static char const * g_opt_str = "DHiqlupgvhr:012c:012t:012o:";
|
2014-06-11 18:26:17 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-16 22:50:27 +00:00
|
|
|
enum class lean_mode { Standard, HoTT };
|
|
|
|
|
2013-08-24 23:11:35 +00:00
|
|
|
int main(int argc, char ** argv) {
|
2014-01-14 00:54:21 +00:00
|
|
|
lean::save_stack_info();
|
2014-04-29 00:57:39 +00:00
|
|
|
lean::register_modules();
|
2014-06-16 23:37:46 +00:00
|
|
|
bool export_objects = false;
|
|
|
|
unsigned trust_lvl = 0;
|
|
|
|
bool quiet = false;
|
|
|
|
bool interactive = false;
|
2014-06-29 00:43:30 +00:00
|
|
|
bool only_deps = false;
|
2014-06-16 23:37:46 +00:00
|
|
|
lean_mode mode = lean_mode::Standard;
|
|
|
|
unsigned num_threads = 1;
|
2014-01-14 00:54:21 +00:00
|
|
|
std::string output;
|
|
|
|
input_kind default_k = input_kind::Lean; // default
|
|
|
|
while (true) {
|
2014-06-11 18:26:17 +00:00
|
|
|
int c = getopt_long(argc, argv, g_opt_str, g_long_options, NULL);
|
2014-01-14 00:54:21 +00:00
|
|
|
if (c == -1)
|
|
|
|
break; // end of command line
|
|
|
|
switch (c) {
|
2014-06-16 23:37:46 +00:00
|
|
|
case 'r':
|
|
|
|
num_threads = atoi(optarg);
|
|
|
|
break;
|
2014-06-16 22:50:27 +00:00
|
|
|
case 'H':
|
|
|
|
mode = lean_mode::HoTT;
|
|
|
|
lean::init_lean_path("hott");
|
|
|
|
break;
|
2014-06-14 17:38:42 +00:00
|
|
|
case 'i':
|
|
|
|
interactive = true;
|
|
|
|
break;
|
2014-01-14 00:54:21 +00:00
|
|
|
case 'v':
|
|
|
|
display_header(std::cout);
|
|
|
|
return 0;
|
|
|
|
case 'g':
|
|
|
|
std::cout << g_githash << "\n";
|
|
|
|
return 0;
|
|
|
|
case 'h':
|
|
|
|
display_help(std::cout);
|
|
|
|
return 0;
|
|
|
|
case 'l':
|
|
|
|
default_k = input_kind::Lean;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
default_k = input_kind::Lua;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
script_state::set_check_interrupt_freq(atoi(optarg));
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
std::cout << lean::get_lean_path() << "\n";
|
|
|
|
return 0;
|
|
|
|
case 's':
|
|
|
|
lean::set_thread_stack_size(atoi(optarg)*1024);
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
output = optarg;
|
2014-06-14 15:01:52 +00:00
|
|
|
export_objects = true;
|
2014-01-14 00:54:21 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2014-06-11 18:26:17 +00:00
|
|
|
trust_lvl = atoi(optarg);
|
2014-01-14 00:54:21 +00:00
|
|
|
break;
|
|
|
|
case 'q':
|
2014-06-11 18:26:17 +00:00
|
|
|
quiet = true;
|
2014-01-14 00:54:21 +00:00
|
|
|
break;
|
2014-06-29 00:43:30 +00:00
|
|
|
case 'D':
|
|
|
|
only_deps = true;
|
|
|
|
break;
|
2014-01-14 00:54:21 +00:00
|
|
|
default:
|
|
|
|
std::cerr << "Unknown command line option\n";
|
|
|
|
display_help(std::cerr);
|
|
|
|
return 1;
|
2013-11-20 03:30:39 +00:00
|
|
|
}
|
2014-01-14 00:54:21 +00:00
|
|
|
}
|
2014-04-29 00:49:23 +00:00
|
|
|
|
2014-06-16 22:50:27 +00:00
|
|
|
environment env = mode == lean_mode::Standard ? mk_environment(trust_lvl) : mk_hott_environment(trust_lvl);
|
2014-07-09 08:12:36 +00:00
|
|
|
io_state ios(lean::mk_pretty_formatter());
|
2014-06-11 18:26:17 +00:00
|
|
|
if (quiet)
|
|
|
|
ios.set_option("verbose", false);
|
2014-06-18 05:36:47 +00:00
|
|
|
script_state S = lean::get_thread_script_state();
|
|
|
|
set_environment set1(S, env);
|
|
|
|
set_io_state set2(S, ios);
|
2014-04-29 00:49:23 +00:00
|
|
|
|
2014-01-14 00:54:21 +00:00
|
|
|
try {
|
2014-06-14 17:38:42 +00:00
|
|
|
bool ok = true;
|
|
|
|
for (int i = optind; i < argc; i++) {
|
|
|
|
char const * ext = get_file_extension(argv[i]);
|
|
|
|
input_kind k = default_k;
|
|
|
|
if (ext) {
|
|
|
|
if (strcmp(ext, "lean") == 0) {
|
|
|
|
k = input_kind::Lean;
|
|
|
|
} else if (strcmp(ext, "lua") == 0) {
|
|
|
|
k = input_kind::Lua;
|
2013-11-20 00:47:47 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-14 17:38:42 +00:00
|
|
|
if (k == input_kind::Lean) {
|
2014-06-29 00:43:30 +00:00
|
|
|
if (only_deps) {
|
|
|
|
display_deps(env, std::cout, argv[i]);
|
|
|
|
} else if (!parse_commands(env, ios, argv[i], false, num_threads)) {
|
2014-06-14 17:38:42 +00:00
|
|
|
ok = false;
|
2014-06-29 00:43:30 +00:00
|
|
|
}
|
2014-06-14 17:38:42 +00:00
|
|
|
} else if (k == input_kind::Lua) {
|
|
|
|
try {
|
2014-06-18 05:04:09 +00:00
|
|
|
lean::system_import(argv[i]);
|
2014-06-14 17:38:42 +00:00
|
|
|
} catch (lean::exception & ex) {
|
|
|
|
::lean::display_error(regular(env, ios), nullptr, ex);
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
2014-06-14 15:01:52 +00:00
|
|
|
}
|
2013-08-18 23:23:29 +00:00
|
|
|
}
|
2014-06-14 17:38:42 +00:00
|
|
|
if (ok && interactive && default_k == input_kind::Lean) {
|
|
|
|
signal(SIGINT, on_ctrl_c);
|
2014-06-18 04:38:10 +00:00
|
|
|
lean::interactive in(env, ios, num_threads);
|
2014-06-14 17:38:42 +00:00
|
|
|
in(std::cin, "[stdin]");
|
|
|
|
}
|
2014-07-01 01:04:18 +00:00
|
|
|
if (export_objects && ok) {
|
2014-06-14 17:38:42 +00:00
|
|
|
std::ofstream out(output, std::ofstream::binary);
|
|
|
|
export_module(out, env);
|
|
|
|
}
|
|
|
|
return ok ? 0 : 1;
|
2013-12-29 10:44:49 +00:00
|
|
|
} catch (lean::exception & ex) {
|
2014-06-11 18:26:17 +00:00
|
|
|
::lean::display_error(regular(env, ios), nullptr, ex);
|
2013-08-18 23:23:29 +00:00
|
|
|
}
|
2014-01-09 19:01:27 +00:00
|
|
|
return 1;
|
2013-07-17 05:10:18 +00:00
|
|
|
}
|