changeset 22906:5ff7125e81fc v8.2.2000

patch 8.2.2000: Vim9: dict.key assignment not implemented yet Commit: https://github.com/vim/vim/commit/fc74d03e7694bac3b50d8d6b6b78b40a71818744 Author: Bram Moolenaar <Bram@vim.org> Date: Mon Nov 16 22:11:49 2020 +0100 patch 8.2.2000: Vim9: dict.key assignment not implemented yet Problem: Vim9: dict.key assignment not implemented yet. Solution: Implement dict.key assignment. (closes https://github.com/vim/vim/issues/7312)
author Bram Moolenaar <Bram@vim.org>
date Mon, 16 Nov 2020 22:15:03 +0100
parents 3e3b02c8ca00
children 6a8b703b33a3
files src/testdir/test_vim9_assign.vim src/version.c src/vim9compile.c
diffstat 3 files changed, 35 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/testdir/test_vim9_assign.vim
+++ b/src/testdir/test_vim9_assign.vim
@@ -408,6 +408,15 @@ def Test_assignment_dict()
 
   # overwrite
   dict3['key'] = 'another'
+  assert_equal(dict3, #{key: 'another'})
+  dict3.key = 'yet another'
+  assert_equal(dict3, #{key: 'yet another'})
+
+  var lines =<< trim END
+    var dd = #{one: 1}
+    dd.one) = 2
+  END
+  CheckDefFailure(lines, 'E15:', 2)
 
   # empty key can be used
   var dd = {}
@@ -418,7 +427,7 @@ def Test_assignment_dict()
   var somedict = rand() > 0 ? #{a: 1, b: 2} : #{a: 'a', b: 'b'}
 
   # assignment to script-local dict
-  var lines =<< trim END
+  lines =<< trim END
     vim9script
     var test: dict<any> = {}
     def FillDict(): dict<any>
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    2000,
+/**/
     1999,
 /**/
     1998,
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -5384,14 +5384,14 @@ compile_assignment(char_u *arg, exarg_T 
 	member_type = type;
 	if (var_end > var_start + varlen)
 	{
-	    // Something follows after the variable: "var[idx]".
+	    // Something follows after the variable: "var[idx]" or "var.key".
 	    if (is_decl)
 	    {
 		emsg(_(e_cannot_use_index_when_declaring_variable));
 		goto theend;
 	    }
 
-	    if (var_start[varlen] == '[')
+	    if (var_start[varlen] == '[' || var_start[varlen] == '.')
 	    {
 		has_index = TRUE;
 		if (type->tt_member == NULL)
@@ -5635,21 +5635,33 @@ compile_assignment(char_u *arg, exarg_T 
 	{
 	    int r;
 
-	    // Compile the "idx" in "var[idx]".
+	    // Compile the "idx" in "var[idx]" or "key" in "var.key".
 	    if (new_local)
 		--cctx->ctx_locals.ga_len;
-	    p = skipwhite(var_start + varlen + 1);
-	    r = compile_expr0(&p, cctx);
+	    p = var_start + varlen;
+	    if (*p == '[')
+	    {
+		p = skipwhite(p + 1);
+		r = compile_expr0(&p, cctx);
+		if (r == OK && *skipwhite(p) != ']')
+		{
+		    // this should not happen
+		    emsg(_(e_missbrac));
+		    r = FAIL;
+		}
+	    }
+	    else // if (*p == '.')
+	    {
+		char_u *key_end = to_name_end(p + 1, TRUE);
+		char_u *key = vim_strnsave(p + 1, key_end - p - 1);
+
+		r = generate_PUSHS(cctx, key);
+	    }
 	    if (new_local)
 		++cctx->ctx_locals.ga_len;
 	    if (r == FAIL)
 		goto theend;
-	    if (*skipwhite(p) != ']')
-	    {
-		// this should not happen
-		emsg(_(e_missbrac));
-		goto theend;
-	    }
+
 	    if (type == &t_any)
 	    {
 		type_T	    *idx_type = ((type_T **)stack->ga_data)[