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-11-20 01:04:52 +00:00
|
|
|
#include <getopt.h>
|
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-10-01 01:16:13 +00:00
|
|
|
#include "kernel/printer.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-07-17 05:10:18 +00:00
|
|
|
#include "version.h"
|
2013-09-13 23:14:24 +00:00
|
|
|
|
|
|
|
using lean::shell;
|
|
|
|
using lean::frontend;
|
|
|
|
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-07-17 21:24:35 +00:00
|
|
|
|
2013-11-20 00:47:47 +00:00
|
|
|
enum class input_kind { Unspecified, Lean, Lua };
|
|
|
|
|
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) {
|
|
|
|
out << "Lean (version " << LEAN_VERSION_MAJOR << "." << LEAN_VERSION_MINOR << ")\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void display_help(std::ostream & out) {
|
|
|
|
display_header(out);
|
|
|
|
std::cout << "Input format:\n";
|
|
|
|
std::cout << " --lean use parser for Lean default input format for files with unknown extension (default)\n";
|
|
|
|
std::cout << " --lua use Lua parser for files with unknown extension\n";
|
|
|
|
std::cout << "Miscellaneous:\n";
|
|
|
|
std::cout << " --help -h display this message\n";
|
|
|
|
std::cout << " --version -v display version number\n";
|
|
|
|
}
|
|
|
|
|
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[] = {
|
|
|
|
{"version", no_argument, 0, 'v'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"lean", no_argument, 0, 'l'},
|
|
|
|
{"lua", no_argument, 0, 'u'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
2013-08-24 23:11:35 +00:00
|
|
|
|
|
|
|
int main(int argc, char ** argv) {
|
2013-12-01 20:42:32 +00:00
|
|
|
lean::save_stack_info();
|
2013-11-27 22:57:33 +00:00
|
|
|
lean::register_modules();
|
2013-11-20 03:30:39 +00:00
|
|
|
input_kind default_k = input_kind::Lean; // default
|
|
|
|
int optind = 1;
|
|
|
|
while (true) {
|
|
|
|
int c = getopt_long(argc, argv, "vh", g_long_options, &optind);
|
|
|
|
if (c == -1)
|
|
|
|
break; // end of command line
|
|
|
|
switch (c) {
|
|
|
|
case 'v':
|
|
|
|
display_header(std::cout);
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
std::cerr << "Unknown command line option\n";
|
|
|
|
display_help(std::cerr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
frontend f;
|
2013-11-27 22:57:33 +00:00
|
|
|
script_state S;
|
2013-11-20 03:30:39 +00:00
|
|
|
if (optind >= argc) {
|
|
|
|
display_header(std::cout);
|
|
|
|
std::cout << "Type Ctrl-D to exit or 'Help.' for help."<< std::endl;
|
|
|
|
shell sh(f, &S);
|
|
|
|
signal(SIGINT, on_ctrl_c);
|
|
|
|
return sh();
|
2013-08-18 23:23:29 +00:00
|
|
|
} else {
|
|
|
|
bool ok = true;
|
2013-11-20 03:30:39 +00:00
|
|
|
for (int i = optind; i < argc; i++) {
|
2013-11-20 00:47:47 +00:00
|
|
|
char const * ext = get_file_extension(argv[i]);
|
2013-11-20 03:30:39 +00:00
|
|
|
input_kind k = default_k;
|
2013-11-20 00:47:47 +00:00
|
|
|
if (ext) {
|
|
|
|
if (strcmp(ext, "lean") == 0) {
|
|
|
|
k = input_kind::Lean;
|
|
|
|
} else if (strcmp(ext, "lua") == 0) {
|
|
|
|
k = input_kind::Lua;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (k == input_kind::Lean) {
|
|
|
|
std::ifstream in(argv[i]);
|
|
|
|
parser p(f, in, &S, false, false);
|
|
|
|
if (!p())
|
|
|
|
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;
|
|
|
|
}
|
2013-11-20 03:30:39 +00:00
|
|
|
} else {
|
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
2013-11-20 00:47:47 +00:00
|
|
|
}
|
2013-08-18 23:23:29 +00:00
|
|
|
}
|
|
|
|
return ok ? 0 : 1;
|
|
|
|
}
|
2013-07-17 05:10:18 +00:00
|
|
|
}
|