From 0b4c66c67a083f25816b9cdb8e76a41e02d9f560 Mon Sep 17 00:00:00 2001 From: Bram Moolenaar Date: Mon, 14 Sep 2020 21:39:44 +0200 Subject: patch 8.2.1685: Vim9: cannot declare a constant value Problem: Vim9: cannot declare a constant value. Solution: Introduce ":const!". --- src/vim9compile.c | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) (limited to 'src/vim9compile.c') diff --git a/src/vim9compile.c b/src/vim9compile.c index dd3eff49c..103e696bf 100644 --- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -1108,6 +1108,20 @@ generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit) return OK; } +/* + * Generate an ISN_LOCKCONST instruction. + */ + static int +generate_LOCKCONST(cctx_T *cctx) +{ + isn_T *isn; + + RETURN_OK_IF_SKIP(cctx); + if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL) + return FAIL; + return OK; +} + /* * Generate an ISN_LOADS instruction. */ @@ -4342,7 +4356,7 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx) ufunc_T *ufunc; int r; - if (*name_start == '!') + if (eap->forceit) { emsg(_(e_cannot_use_bang_with_nested_def)); return NULL; @@ -5232,6 +5246,11 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) } else { + if (is_decl && eap->forceit && cmdidx == CMD_const + && (dest == dest_script || dest == dest_local)) + // ":const! var": lock the value, but not referenced variables + generate_LOCKCONST(cctx); + switch (dest) { case dest_option: @@ -6362,13 +6381,8 @@ compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx) char_u *line = arg; linenr_T lnum; char *errormsg; - int above = FALSE; + int above = eap->forceit; - if (*arg == '!') - { - above = TRUE; - line = skipwhite(arg + 1); - } eap->regname = *line; if (eap->regname == '=') @@ -6411,7 +6425,7 @@ compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) { - long argt = excmd_get_argt(eap->cmdidx); + long argt = eap->argt; int usefilter = FALSE; has_expr = argt & (EX_XFILE | EX_EXPAND); @@ -6870,8 +6884,6 @@ compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx) } } - p = skipwhite(p); - if (cctx.ctx_had_return && ea.cmdidx != CMD_elseif && ea.cmdidx != CMD_else @@ -6886,6 +6898,18 @@ compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx) goto erret; } + p = skipwhite(p); + if (ea.cmdidx != CMD_SIZE + && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read) + { + ea.argt = excmd_get_argt(ea.cmdidx); + if ((ea.argt & EX_BANG) && *p == '!') + { + ea.forceit = TRUE; + p = skipwhite(p + 1); + } + } + switch (ea.cmdidx) { case CMD_def: @@ -7309,6 +7333,7 @@ delete_instr(isn_T *isn) case ISN_LOADTDICT: case ISN_LOADV: case ISN_LOADWDICT: + case ISN_LOCKCONST: case ISN_MEMBER: case ISN_NEGATENR: case ISN_NEWDICT: -- cgit v1.2.1