feat: add #[elem1, elem2, ..] notation for creating arrays

@kha @dselsam: I added this notation because I am tired of writing
`[elem1, elem2, ...].toArray`. BTW, the new notation is based on the
one available in SML.
This commit is contained in:
Leonardo de Moura 2019-10-07 15:36:44 -07:00
parent 719fb3c962
commit 4793cbfa9a
5 changed files with 13 additions and 1 deletions

View file

@ -719,6 +719,11 @@ static expr parse_list(parser & p, unsigned, expr const *, pos_info const & pos)
return r;
}
static expr parse_array(parser & p, unsigned n, expr const * l, pos_info const & pos) {
expr xs = parse_list(p, n, l, pos);
return p.save_pos(mk_app(mk_constant(get_list_to_array_name()), xs), pos);
}
static void consume_rparen(parser & p) {
p.check_token_next(get_rparen_tk(), "invalid expression, `)` expected");
}
@ -819,6 +824,7 @@ parse_table init_nud_table() {
r = r.add({transition("if", mk_ext_action(parse_if_then_else))}, x0);
r = r.add({transition("(", mk_ext_action(parse_lparen))}, x0);
r = r.add({transition("[", mk_ext_action(parse_list))}, x0);
r = r.add({transition("#[", mk_ext_action(parse_array))}, x0);
r = r.add({transition("", mk_ext_action(parse_constructor))}, x0);
r = r.add({transition("{", mk_ext_action(parse_curly_bracket))}, x0);
r = r.add({transition(".(", mk_ext_action(parse_inaccessible))}, x0);

View file

@ -85,7 +85,7 @@ void init_token_table(token_table & t) {
{"```(", g_max_prec}, {"`[", g_max_prec}, {"`", g_max_prec},
{"%%", g_max_prec}, {"()", g_max_prec}, {"(::)", g_max_prec}, {")", 0}, {"'", 0},
{"{", g_max_prec}, {"}", 0}, {"_", g_max_prec},
{"[", g_max_prec}, {"]", 0}, {"", g_max_prec}, {"", 0}, {".{", 0},
{"[", g_max_prec}, {"#[", g_max_prec}, {"]", 0}, {"", g_max_prec}, {"", 0}, {".{", 0},
{"{!", g_max_prec}, {"!}", 0},
{"Type", g_max_prec}, {"Type*", g_max_prec}, {"Sort", g_max_prec}, {"Sort*", g_max_prec},
{"(:", g_max_prec}, {":)", 0}, {".(", g_max_prec}, {"._", g_max_prec},

View file

@ -110,6 +110,7 @@ name const * g_lc_unreachable = nullptr;
name const * g_list = nullptr;
name const * g_list_nil = nullptr;
name const * g_list_cons = nullptr;
name const * g_list_to_array = nullptr;
name const * g_match_failed = nullptr;
name const * g_monad = nullptr;
name const * g_monad_fail = nullptr;
@ -295,6 +296,7 @@ void initialize_constants() {
g_list = new name{"List"};
g_list_nil = new name{"List", "nil"};
g_list_cons = new name{"List", "cons"};
g_list_to_array = new name{"List", "toArray"};
g_match_failed = new name{"matchFailed"};
g_monad = new name{"Monad"};
g_monad_fail = new name{"MonadFail"};
@ -481,6 +483,7 @@ void finalize_constants() {
delete g_list;
delete g_list_nil;
delete g_list_cons;
delete g_list_to_array;
delete g_match_failed;
delete g_monad;
delete g_monad_fail;
@ -666,6 +669,7 @@ name const & get_lc_unreachable_name() { return *g_lc_unreachable; }
name const & get_list_name() { return *g_list; }
name const & get_list_nil_name() { return *g_list_nil; }
name const & get_list_cons_name() { return *g_list_cons; }
name const & get_list_to_array_name() { return *g_list_to_array; }
name const & get_match_failed_name() { return *g_match_failed; }
name const & get_monad_name() { return *g_monad; }
name const & get_monad_fail_name() { return *g_monad_fail; }

View file

@ -112,6 +112,7 @@ name const & get_lc_unreachable_name();
name const & get_list_name();
name const & get_list_nil_name();
name const & get_list_cons_name();
name const & get_list_to_array_name();
name const & get_match_failed_name();
name const & get_monad_name();
name const & get_monad_fail_name();

View file

@ -105,6 +105,7 @@ lcUnreachable
List
List.nil
List.cons
List.toArray
matchFailed
Monad
MonadFail