2014-07-04 21:25:44 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include <string>
|
2014-09-28 19:20:42 +00:00
|
|
|
#include "library/num.h"
|
2014-11-30 03:08:37 +00:00
|
|
|
#include "library/normalize.h"
|
2014-12-10 05:12:39 +00:00
|
|
|
#include "library/class.h"
|
2014-07-04 21:25:44 +00:00
|
|
|
#include "frontends/lean/parser.h"
|
2014-09-19 19:42:22 +00:00
|
|
|
#include "frontends/lean/util.h"
|
2014-09-28 19:20:42 +00:00
|
|
|
#include "frontends/lean/tokens.h"
|
|
|
|
|
2014-07-04 21:25:44 +00:00
|
|
|
namespace lean {
|
2014-09-28 19:20:42 +00:00
|
|
|
optional<unsigned> parse_instance_priority(parser & p) {
|
|
|
|
if (p.curr_is_token(get_priority_tk())) {
|
|
|
|
p.next();
|
|
|
|
auto pos = p.pos();
|
2014-11-30 03:08:37 +00:00
|
|
|
environment env = open_priority_aliases(open_num_notation(p.env()));
|
2014-11-30 02:29:48 +00:00
|
|
|
parser::local_scope scope(p, env);
|
2014-09-28 19:20:42 +00:00
|
|
|
expr val = p.parse_expr();
|
2014-11-30 03:08:37 +00:00
|
|
|
val = normalize(p.env(), val);
|
2014-09-28 19:20:42 +00:00
|
|
|
if (optional<mpz> mpz_val = to_num(val)) {
|
|
|
|
if (!mpz_val->is_unsigned_int())
|
|
|
|
throw parser_error("invalid 'priority', argument does not fit in a machine integer", pos);
|
|
|
|
p.check_token_next(get_rbracket_tk(), "invalid 'priority', ']' expected");
|
|
|
|
return optional<unsigned>(mpz_val->get_unsigned_int());
|
|
|
|
} else {
|
|
|
|
throw parser_error("invalid 'priority', argument does not evaluate to a numeral", pos);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return optional<unsigned>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 21:25:44 +00:00
|
|
|
environment add_instance_cmd(parser & p) {
|
2014-07-07 21:41:14 +00:00
|
|
|
bool found = false;
|
2014-09-19 19:42:22 +00:00
|
|
|
bool persistent = false;
|
|
|
|
parse_persistent(p, persistent);
|
2014-09-28 19:20:42 +00:00
|
|
|
optional<unsigned> priority = parse_instance_priority(p);
|
2014-07-07 21:41:14 +00:00
|
|
|
environment env = p.env();
|
|
|
|
while (p.curr_is_identifier()) {
|
|
|
|
found = true;
|
2014-07-15 00:19:47 +00:00
|
|
|
name c = p.check_constant_next("invalid 'class instance' declaration, constant expected");
|
2014-09-28 19:20:42 +00:00
|
|
|
if (priority)
|
|
|
|
env = add_instance(env, c, *priority, persistent);
|
|
|
|
else
|
|
|
|
env = add_instance(env, c, persistent);
|
2014-07-07 21:41:14 +00:00
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
throw parser_error("invalid 'class instance' declaration, at least one identifier expected", p.pos());
|
|
|
|
return env;
|
2014-07-04 21:25:44 +00:00
|
|
|
}
|
|
|
|
|
2014-09-19 19:42:22 +00:00
|
|
|
|
2014-09-02 22:03:33 +00:00
|
|
|
environment add_class_cmd(parser & p) {
|
|
|
|
bool found = false;
|
|
|
|
environment env = p.env();
|
2014-09-19 19:42:22 +00:00
|
|
|
bool persistent = false;
|
|
|
|
parse_persistent(p, persistent);
|
2014-09-02 22:03:33 +00:00
|
|
|
while (p.curr_is_identifier()) {
|
|
|
|
found = true;
|
|
|
|
name c = p.check_constant_next("invalid 'class' declaration, constant expected");
|
2014-09-19 19:42:22 +00:00
|
|
|
env = add_class(env, c, persistent);
|
2014-09-02 22:03:33 +00:00
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
throw parser_error("invalid 'class' declaration, at least one identifier expected", p.pos());
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2014-07-04 21:25:44 +00:00
|
|
|
void register_class_cmds(cmd_table & r) {
|
|
|
|
add_cmd(r, cmd_info("instance", "add a new instance", add_instance_cmd));
|
2014-09-02 22:03:33 +00:00
|
|
|
add_cmd(r, cmd_info("class", "add a new class", add_class_cmd));
|
2014-07-04 21:25:44 +00:00
|
|
|
}
|
|
|
|
}
|