diff src/vim9compile.c @ 21717:ef3b31d510d2 v8.2.1408

patch 8.2.1408: Vim9: type casting not supported Commit: https://github.com/vim/vim/commit/64d662d5fc2ff8af4dbf399ff02aa9d711cc9312 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Aug 9 19:02:50 2020 +0200 patch 8.2.1408: Vim9: type casting not supported Problem: Vim9: type casting not supported. Solution: Introduce type casting.
author Bram Moolenaar <Bram@vim.org>
date Sun, 09 Aug 2020 19:15:03 +0200
parents 571832713efa
children caf0286cf02b
line wrap: on
line diff
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -3402,6 +3402,56 @@ error_white_both(char_u *op, int len)
 }
 
 /*
+ * <type>expr7: runtime type check / conversion
+ */
+    static int
+compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
+{
+    type_T *want_type = NULL;
+
+    // Recognize <type>
+    if (**arg == '<' && eval_isnamec1((*arg)[1]))
+    {
+	int		called_emsg_before = called_emsg;
+
+	++*arg;
+	want_type = parse_type(arg, cctx->ctx_type_list);
+	if (called_emsg != called_emsg_before)
+	    return FAIL;
+
+	if (**arg != '>')
+	{
+	    if (*skipwhite(*arg) == '>')
+		semsg(_(e_no_white_before), ">");
+	    else
+		emsg(_("E1104: Missing >"));
+	    return FAIL;
+	}
+	++*arg;
+	if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL)
+	    return FAIL;
+    }
+
+    if (compile_expr7(arg, cctx, ppconst) == FAIL)
+	return FAIL;
+
+    if (want_type != NULL)
+    {
+	garray_T    *stack = &cctx->ctx_type_stack;
+	type_T	    *actual = ((type_T **)stack->ga_data)[stack->ga_len - 1];
+
+	if (check_type(want_type, actual, FALSE) == FAIL)
+	{
+	    generate_ppconst(cctx, ppconst);
+	    if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL)
+		return FAIL;
+	}
+    }
+
+    return OK;
+}
+
+/*
  *	*	number multiplication
  *	/	number division
  *	%	number modulo
@@ -3414,7 +3464,7 @@ compile_expr6(char_u **arg, cctx_T *cctx
     int		ppconst_used = ppconst->pp_used;
 
     // get the first expression
-    if (compile_expr7(arg, cctx, ppconst) == FAIL)
+    if (compile_expr7t(arg, cctx, ppconst) == FAIL)
 	return FAIL;
 
     /*
@@ -3441,7 +3491,7 @@ compile_expr6(char_u **arg, cctx_T *cctx
 	    return FAIL;
 
 	// get the second expression
-	if (compile_expr7(arg, cctx, ppconst) == FAIL)
+	if (compile_expr7t(arg, cctx, ppconst) == FAIL)
 	    return FAIL;
 
 	if (ppconst->pp_used == ppconst_used + 2