From 7cdd2de6e48a108fd5654c32c72117b04d368284 Mon Sep 17 00:00:00 2001 From: Gaith Hallak Date: Mon, 2 Jan 2023 23:15:48 +0300 Subject: [PATCH] Add hole expressions --- src/aeso_ast_infer_types.erl | 11 +++++++++++ src/aeso_parser.erl | 3 +++ 2 files changed, 14 insertions(+) diff --git a/src/aeso_ast_infer_types.erl b/src/aeso_ast_infer_types.erl index 55ba29e..6bc6cbd 100644 --- a/src/aeso_ast_infer_types.erl +++ b/src/aeso_ast_infer_types.erl @@ -1843,6 +1843,8 @@ infer_expr(_Env, Body={contract_pubkey, As, _}) -> {typed, As, Body, Con}; infer_expr(_Env, Body={id, As, "_"}) -> {typed, As, Body, fresh_uvar(As)}; +infer_expr(_Env, Body={id, As, "???"}) -> + {typed, As, Body, {id, As, "hole"}}; infer_expr(Env, Id = {Tag, As, _}) when Tag == id; Tag == qid -> {QName, Type} = lookup_name(Env, As, Id), {typed, As, QName, Type}; @@ -2939,6 +2941,12 @@ unify1(_Env, _A, {id, _, "void"}, Variance, _When) unify1(_Env, {id, _, "void"}, _B, Variance, _When) when Variance == contravariant orelse Variance == bivariant -> true; +unify1(_Env, {id, Ann, "hole"}, B, _Variance, _When) -> + type_error({hole_found, Ann, B}), + true; +unify1(_Env, A, {id, Ann, "hole"}, _Variance, _When) -> + type_error({hole_found, Ann, A}), + true; unify1(_Env, {id, _, Name}, {id, _, Name}, _Variance, _When) -> true; unify1(Env, A = {con, _, NameA}, B = {con, _, NameB}, Variance, When) -> @@ -3396,6 +3404,9 @@ mk_error({cannot_unify, A, B, Cxt, When}) -> [pp(instantiate(A)), pp(instantiate(B))]), {Pos, Ctxt} = pp_when(When), mk_t_err(Pos, Msg, Ctxt); +mk_error({hole_found, Ann, Type}) -> + Msg = io_lib:format("Found a hole of type `~s`", [pp(instantiate(Type))]), + mk_t_err(pos(Ann), Msg); mk_error({unbound_variable, Id}) -> Msg = io_lib:format("Unbound variable `~s`", [pp(Id)]), case Id of diff --git a/src/aeso_parser.erl b/src/aeso_parser.erl index f844942..d1d92cb 100644 --- a/src/aeso_parser.erl +++ b/src/aeso_parser.erl @@ -359,9 +359,12 @@ exprAtom() -> , ?RULE(tok('['), Expr, binop('..'), Expr, tok(']'), _3(_2, _4)) , ?RULE(keyword('('), comma_sep(Expr), tok(')'), tuple_e(_1, _2)) , letpat() + , hole() ]) end). +hole() -> ?RULE(token('???'), {id, get_ann(_1), "???"}). + comprehension_exp() -> ?LAZY_P(choice( [ comprehension_bind()