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"
|
2013-12-23 01:56:53 +00:00
|
|
|
#include "util/lean_path.h"
|
2013-10-01 01:16:13 +00:00
|
|
|
#include "kernel/printer.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"
|
2013-12-09 20:17:43 +00:00
|
|
|
#include "library/io_state.h"
|
|
|
|
#include "library/kernel_bindings.h"
|
2013-09-13 03:09:35 +00:00
|
|
|
#include "frontends/lean/parser.h"
|
2013-11-15 23:55:15 +00:00
|
|
|
#include "frontends/lean/frontend.h"
|
2013-11-27 22:57:33 +00:00
|
|
|
#include "frontends/lua/register_modules.h"
|
2013-12-09 20:17:43 +00:00
|
|
|
#include "shell/lua_repl.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
|
|
|
|
|
|
|
using lean::shell;
|
|
|
|
using lean::parser;
|
2013-11-27 22:57:33 +00:00
|
|
|
using lean::script_state;
|
2013-11-20 03:30:39 +00:00
|
|
|
using lean::unreachable_reached;
|
|
|
|
using lean::invoke_debugger;
|
|
|
|
using lean::notify_assertion_violation;
|
2013-12-18 22:37:55 +00:00
|
|
|
using lean::environment;
|
|
|
|
using lean::io_state;
|
2013-07-17 21:24:35 +00:00
|
|
|
|
2013-12-29 01:31:35 +00:00
|
|
|
enum class input_kind { Unspecified, Lean, OLean, 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";
|
2013-12-29 01:31:35 +00:00
|
|
|
std::cout << " --olean use parser for Lean binary input format for files\n";
|
|
|
|
std::cout << " with unknown extension\n";
|
2013-12-10 01:33:47 +00:00
|
|
|
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";
|
2013-12-29 11:43:45 +00:00
|
|
|
std::cout << " --trust -t trust imported modules\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[] = {
|
2013-12-04 17:57:33 +00:00
|
|
|
{"version", no_argument, 0, 'v'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"lean", no_argument, 0, 'l'},
|
2013-12-29 01:31:35 +00:00
|
|
|
{"olean", no_argument, 0, 'b'},
|
2013-12-04 17:57:33 +00:00
|
|
|
{"lua", no_argument, 0, 'u'},
|
2013-12-29 10:44:49 +00:00
|
|
|
{"kernel", no_argument, 0, 'k'},
|
2013-12-23 01:56:53 +00:00
|
|
|
{"path", no_argument, 0, 'p'},
|
2013-12-04 17:57:33 +00:00
|
|
|
{"luahook", required_argument, 0, 'c'},
|
2013-12-04 19:21:26 +00:00
|
|
|
{"githash", no_argument, 0, 'g'},
|
2013-12-29 01:31:35 +00:00
|
|
|
{"output", required_argument, 0, 'o'},
|
2013-12-29 11:43:45 +00:00
|
|
|
{"trust", no_argument, 0, 't'},
|
2013-12-10 01:33:47 +00:00
|
|
|
#if defined(LEAN_USE_BOOST)
|
|
|
|
{"tstack", required_argument, 0, 's'},
|
|
|
|
#endif
|
2013-11-20 03:30:39 +00:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
2013-08-24 23:11:35 +00:00
|
|
|
|
|
|
|
int main(int argc, char ** argv) {
|
2013-12-29 10:44:49 +00:00
|
|
|
try {
|
|
|
|
lean::save_stack_info();
|
|
|
|
lean::register_modules();
|
2013-12-29 11:43:45 +00:00
|
|
|
bool kernel_only = false;
|
2013-12-29 10:44:49 +00:00
|
|
|
bool export_objects = false;
|
2013-12-29 11:43:45 +00:00
|
|
|
bool trust_imported = false;
|
2013-12-29 10:44:49 +00:00
|
|
|
std::string output;
|
|
|
|
input_kind default_k = input_kind::Lean; // default
|
|
|
|
while (true) {
|
2013-12-29 11:43:45 +00:00
|
|
|
int c = getopt_long(argc, argv, "tklupgvhc:012s:012o:", g_long_options, NULL);
|
2013-12-29 10:44:49 +00:00
|
|
|
if (c == -1)
|
|
|
|
break; // end of command line
|
|
|
|
switch (c) {
|
|
|
|
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 'b':
|
|
|
|
default_k = input_kind::OLean;
|
|
|
|
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 'k':
|
|
|
|
kernel_only = true;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
output = optarg;
|
|
|
|
export_objects = true;
|
|
|
|
break;
|
2013-12-29 11:43:45 +00:00
|
|
|
case 't':
|
|
|
|
trust_imported = true;
|
|
|
|
break;
|
2013-12-29 10:44:49 +00:00
|
|
|
default:
|
|
|
|
std::cerr << "Unknown command line option\n";
|
|
|
|
display_help(std::cerr);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-11-20 03:30:39 +00:00
|
|
|
}
|
2013-12-29 10:44:49 +00:00
|
|
|
if (optind >= argc) {
|
|
|
|
display_header(std::cout);
|
|
|
|
signal(SIGINT, on_ctrl_c);
|
|
|
|
if (default_k == input_kind::Lean) {
|
|
|
|
#if defined(LEAN_WINDOWS)
|
|
|
|
std::cout << "Type 'Exit.' to exit or 'Help.' for help."<< std::endl;
|
|
|
|
#else
|
|
|
|
std::cout << "Type Ctrl-D or 'Exit.' to exit or 'Help.' for help."<< std::endl;
|
|
|
|
#endif
|
|
|
|
environment env;
|
2013-12-29 11:43:45 +00:00
|
|
|
env->set_trusted_imported(trust_imported);
|
2013-12-29 10:44:49 +00:00
|
|
|
io_state ios;
|
|
|
|
init_frontend(env, ios, kernel_only);
|
|
|
|
script_state S;
|
|
|
|
shell sh(env, &S);
|
|
|
|
int status = sh() ? 0 : 1;
|
|
|
|
if (export_objects)
|
|
|
|
env->export_objects(output);
|
|
|
|
return status;
|
|
|
|
} else {
|
|
|
|
lean_assert(default_k == input_kind::Lua);
|
|
|
|
script_state S;
|
|
|
|
S.dostring(g_lua_repl);
|
|
|
|
}
|
2013-12-09 20:17:43 +00:00
|
|
|
} else {
|
2013-12-29 10:44:49 +00:00
|
|
|
environment env;
|
2013-12-29 11:43:45 +00:00
|
|
|
env->set_trusted_imported(trust_imported);
|
2013-12-29 10:44:49 +00:00
|
|
|
io_state ios;
|
|
|
|
init_frontend(env, ios, kernel_only);
|
2013-12-09 20:17:43 +00:00
|
|
|
script_state S;
|
2013-12-29 10:44:49 +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, "olean") == 0) {
|
|
|
|
k = input_kind::OLean;
|
|
|
|
} else if (strcmp(ext, "lua") == 0) {
|
|
|
|
k = input_kind::Lua;
|
|
|
|
}
|
2013-12-29 01:31:35 +00:00
|
|
|
}
|
2013-12-29 10:44:49 +00:00
|
|
|
if (k == input_kind::Lean) {
|
|
|
|
std::ifstream in(argv[i]);
|
|
|
|
if (in.bad() || in.fail()) {
|
|
|
|
std::cerr << "Failed to open file '" << argv[i] << "'\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!parse_commands(env, ios, in, &S, false, false))
|
|
|
|
ok = false;
|
|
|
|
} else if (k == input_kind::OLean) {
|
|
|
|
try {
|
|
|
|
env->load(std::string(argv[i]), ios);
|
|
|
|
} catch (lean::exception & ex) {
|
|
|
|
std::cerr << "Failed to load binary file '" << argv[i] << "': " << ex.what() << "\n";
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
} else if (k == input_kind::Lua) {
|
|
|
|
try {
|
|
|
|
S.dofile(argv[i]);
|
|
|
|
} catch (lean::exception & ex) {
|
|
|
|
std::cerr << ex.what() << std::endl;
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
2013-11-20 00:47:47 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-29 10:44:49 +00:00
|
|
|
if (export_objects)
|
|
|
|
env->export_objects(output);
|
|
|
|
return ok ? 0 : 1;
|
2013-08-18 23:23:29 +00:00
|
|
|
}
|
2013-12-29 10:44:49 +00:00
|
|
|
} catch (lean::kernel_exception & ex) {
|
|
|
|
std::cerr << "Error: " << ex.pp(lean::mk_simple_formatter(), lean::options()) << "\n";
|
|
|
|
} catch (lean::exception & ex) {
|
|
|
|
std::cerr << "Error: " << ex.what() << "\n";
|
2013-08-18 23:23:29 +00:00
|
|
|
}
|
2013-07-17 05:10:18 +00:00
|
|
|
}
|