Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9compile.c: :def and dealing with instructions |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | #ifdef VMS |
| 20 | # include <float.h> |
| 21 | #endif |
| 22 | |
| 23 | #define DEFINE_VIM9_GLOBALS |
| 24 | #include "vim9.h" |
| 25 | |
| 26 | /* |
| 27 | * Chain of jump instructions where the end label needs to be set. |
| 28 | */ |
| 29 | typedef struct endlabel_S endlabel_T; |
| 30 | struct endlabel_S { |
| 31 | endlabel_T *el_next; // chain end_label locations |
| 32 | int el_end_label; // instruction idx where to set end |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * info specific for the scope of :if / elseif / else |
| 37 | */ |
| 38 | typedef struct { |
| 39 | int is_if_label; // instruction idx at IF or ELSEIF |
| 40 | endlabel_T *is_end_label; // instructions to set end label |
| 41 | } ifscope_T; |
| 42 | |
| 43 | /* |
| 44 | * info specific for the scope of :while |
| 45 | */ |
| 46 | typedef struct { |
| 47 | int ws_top_label; // instruction idx at WHILE |
| 48 | endlabel_T *ws_end_label; // instructions to set end |
| 49 | } whilescope_T; |
| 50 | |
| 51 | /* |
| 52 | * info specific for the scope of :for |
| 53 | */ |
| 54 | typedef struct { |
| 55 | int fs_top_label; // instruction idx at FOR |
| 56 | endlabel_T *fs_end_label; // break instructions |
| 57 | } forscope_T; |
| 58 | |
| 59 | /* |
| 60 | * info specific for the scope of :try |
| 61 | */ |
| 62 | typedef struct { |
| 63 | int ts_try_label; // instruction idx at TRY |
| 64 | endlabel_T *ts_end_label; // jump to :finally or :endtry |
| 65 | int ts_catch_label; // instruction idx of last CATCH |
| 66 | int ts_caught_all; // "catch" without argument encountered |
| 67 | } tryscope_T; |
| 68 | |
| 69 | typedef enum { |
| 70 | NO_SCOPE, |
| 71 | IF_SCOPE, |
| 72 | WHILE_SCOPE, |
| 73 | FOR_SCOPE, |
| 74 | TRY_SCOPE, |
| 75 | BLOCK_SCOPE |
| 76 | } scopetype_T; |
| 77 | |
| 78 | /* |
| 79 | * Info for one scope, pointed to by "ctx_scope". |
| 80 | */ |
| 81 | typedef struct scope_S scope_T; |
| 82 | struct scope_S { |
| 83 | scope_T *se_outer; // scope containing this one |
| 84 | scopetype_T se_type; |
| 85 | int se_local_count; // ctx_locals.ga_len before scope |
| 86 | union { |
| 87 | ifscope_T se_if; |
| 88 | whilescope_T se_while; |
| 89 | forscope_T se_for; |
| 90 | tryscope_T se_try; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 91 | } se_u; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /* |
| 95 | * Entry for "ctx_locals". Used for arguments and local variables. |
| 96 | */ |
| 97 | typedef struct { |
| 98 | char_u *lv_name; |
| 99 | type_T *lv_type; |
| 100 | int lv_const; // when TRUE cannot be assigned to |
| 101 | int lv_arg; // when TRUE this is an argument |
| 102 | } lvar_T; |
| 103 | |
| 104 | /* |
| 105 | * Context for compiling lines of Vim script. |
| 106 | * Stores info about the local variables and condition stack. |
| 107 | */ |
| 108 | struct cctx_S { |
| 109 | ufunc_T *ctx_ufunc; // current function |
| 110 | int ctx_lnum; // line number in current function |
| 111 | garray_T ctx_instr; // generated instructions |
| 112 | |
| 113 | garray_T ctx_locals; // currently visible local variables |
| 114 | int ctx_max_local; // maximum number of locals at one time |
| 115 | |
| 116 | garray_T ctx_imports; // imported items |
| 117 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 118 | int ctx_skip; // when TRUE skip commands, when FALSE skip |
| 119 | // commands after "else" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 120 | scope_T *ctx_scope; // current scope, NULL at toplevel |
| 121 | |
| 122 | garray_T ctx_type_stack; // type of each item on the stack |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 123 | garray_T *ctx_type_list; // list of pointers to allocated types |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | static char e_var_notfound[] = N_("E1001: variable not found: %s"); |
| 127 | static char e_syntax_at[] = N_("E1002: Syntax error at %s"); |
| 128 | |
| 129 | static int compile_expr1(char_u **arg, cctx_T *cctx); |
| 130 | static int compile_expr2(char_u **arg, cctx_T *cctx); |
| 131 | static int compile_expr3(char_u **arg, cctx_T *cctx); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 132 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 133 | static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx); |
| 134 | static int check_type(type_T *expected, type_T *actual, int give_msg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * Lookup variable "name" in the local scope and return the index. |
| 138 | */ |
| 139 | static int |
| 140 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 141 | { |
| 142 | int idx; |
| 143 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 144 | if (len == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 145 | return -1; |
| 146 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 147 | { |
| 148 | lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 149 | |
| 150 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 151 | && STRLEN(lvar->lv_name) == len) |
| 152 | return idx; |
| 153 | } |
| 154 | return -1; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Lookup an argument in the current function. |
| 159 | * Returns the argument index or -1 if not found. |
| 160 | */ |
| 161 | static int |
| 162 | lookup_arg(char_u *name, size_t len, cctx_T *cctx) |
| 163 | { |
| 164 | int idx; |
| 165 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 166 | if (len == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 167 | return -1; |
| 168 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 169 | { |
| 170 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 171 | |
| 172 | if (STRNCMP(name, arg, len) == 0 && STRLEN(arg) == len) |
| 173 | return idx; |
| 174 | } |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Lookup a vararg argument in the current function. |
| 180 | * Returns TRUE if there is a match. |
| 181 | */ |
| 182 | static int |
| 183 | lookup_vararg(char_u *name, size_t len, cctx_T *cctx) |
| 184 | { |
| 185 | char_u *va_name = cctx->ctx_ufunc->uf_va_name; |
| 186 | |
| 187 | return len > 0 && va_name != NULL |
| 188 | && STRNCMP(name, va_name, len) == 0 && STRLEN(va_name) == len; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Lookup a variable in the current script. |
| 193 | * Returns OK or FAIL. |
| 194 | */ |
| 195 | static int |
| 196 | lookup_script(char_u *name, size_t len) |
| 197 | { |
| 198 | int cc; |
| 199 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 200 | dictitem_T *di; |
| 201 | |
| 202 | cc = name[len]; |
| 203 | name[len] = NUL; |
| 204 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 205 | name[len] = cc; |
| 206 | return di == NULL ? FAIL: OK; |
| 207 | } |
| 208 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 209 | /* |
| 210 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 211 | * compilation context "cctx". |
| 212 | * Return FAIL and give an error if it defined. |
| 213 | */ |
| 214 | int |
| 215 | check_defined(char_u *p, int len, cctx_T *cctx) |
| 216 | { |
| 217 | if (lookup_script(p, len) == OK |
| 218 | || (cctx != NULL |
| 219 | && (lookup_local(p, len, cctx) >= 0 |
| 220 | || find_imported(p, len, cctx) != NULL))) |
| 221 | { |
| 222 | semsg("E1073: imported name already defined: %s", p); |
| 223 | return FAIL; |
| 224 | } |
| 225 | return OK; |
| 226 | } |
| 227 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 228 | /* |
| 229 | * Allocate memory for a type_T and add the pointer to type_gap, so that it can |
| 230 | * be freed later. |
| 231 | */ |
| 232 | static type_T * |
| 233 | alloc_type(garray_T *type_gap) |
| 234 | { |
| 235 | type_T *type; |
| 236 | |
| 237 | if (ga_grow(type_gap, 1) == FAIL) |
| 238 | return NULL; |
| 239 | type = ALLOC_CLEAR_ONE(type_T); |
| 240 | if (type != NULL) |
| 241 | { |
| 242 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type; |
| 243 | ++type_gap->ga_len; |
| 244 | } |
| 245 | return type; |
| 246 | } |
| 247 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 248 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 249 | get_list_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 250 | { |
| 251 | type_T *type; |
| 252 | |
| 253 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 254 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 255 | return &t_list_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 256 | if (member_type->tt_type == VAR_VOID |
| 257 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 258 | return &t_list_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 259 | if (member_type->tt_type == VAR_BOOL) |
| 260 | return &t_list_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 261 | if (member_type->tt_type == VAR_NUMBER) |
| 262 | return &t_list_number; |
| 263 | if (member_type->tt_type == VAR_STRING) |
| 264 | return &t_list_string; |
| 265 | |
| 266 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 267 | type = alloc_type(type_gap); |
| 268 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 269 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 270 | type->tt_type = VAR_LIST; |
| 271 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 272 | type->tt_argcount = 0; |
| 273 | type->tt_args = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 274 | return type; |
| 275 | } |
| 276 | |
| 277 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 278 | get_dict_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 279 | { |
| 280 | type_T *type; |
| 281 | |
| 282 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 283 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 284 | return &t_dict_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 285 | if (member_type->tt_type == VAR_VOID |
| 286 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 287 | return &t_dict_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 288 | if (member_type->tt_type == VAR_BOOL) |
| 289 | return &t_dict_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 290 | if (member_type->tt_type == VAR_NUMBER) |
| 291 | return &t_dict_number; |
| 292 | if (member_type->tt_type == VAR_STRING) |
| 293 | return &t_dict_string; |
| 294 | |
| 295 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 296 | type = alloc_type(type_gap); |
| 297 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 298 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 299 | type->tt_type = VAR_DICT; |
| 300 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 301 | type->tt_argcount = 0; |
| 302 | type->tt_args = NULL; |
| 303 | return type; |
| 304 | } |
| 305 | |
| 306 | /* |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 307 | * Allocate a new type for a function. |
| 308 | */ |
| 309 | static type_T * |
| 310 | alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 311 | { |
| 312 | type_T *type = alloc_type(type_gap); |
| 313 | |
| 314 | if (type == NULL) |
| 315 | return &t_any; |
| 316 | type->tt_type = VAR_FUNC; |
| 317 | type->tt_member = ret_type; |
| 318 | type->tt_argcount = argcount; |
| 319 | type->tt_args = NULL; |
| 320 | return type; |
| 321 | } |
| 322 | |
| 323 | /* |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 324 | * Get a function type, based on the return type "ret_type". |
| 325 | * If "argcount" is -1 or 0 a predefined type can be used. |
| 326 | * If "argcount" > 0 always create a new type, so that arguments can be added. |
| 327 | */ |
| 328 | static type_T * |
| 329 | get_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 330 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 331 | // recognize commonly used types |
| 332 | if (argcount <= 0) |
| 333 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 334 | if (ret_type == &t_unknown) |
| 335 | { |
| 336 | // (argcount == 0) is not possible |
| 337 | return &t_func_unknown; |
| 338 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 339 | if (ret_type == &t_void) |
| 340 | { |
| 341 | if (argcount == 0) |
| 342 | return &t_func_0_void; |
| 343 | else |
| 344 | return &t_func_void; |
| 345 | } |
| 346 | if (ret_type == &t_any) |
| 347 | { |
| 348 | if (argcount == 0) |
| 349 | return &t_func_0_any; |
| 350 | else |
| 351 | return &t_func_any; |
| 352 | } |
| 353 | if (ret_type == &t_number) |
| 354 | { |
| 355 | if (argcount == 0) |
| 356 | return &t_func_0_number; |
| 357 | else |
| 358 | return &t_func_number; |
| 359 | } |
| 360 | if (ret_type == &t_string) |
| 361 | { |
| 362 | if (argcount == 0) |
| 363 | return &t_func_0_string; |
| 364 | else |
| 365 | return &t_func_string; |
| 366 | } |
| 367 | } |
| 368 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 369 | return alloc_func_type(ret_type, argcount, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 372 | /* |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 373 | * For a function type, reserve space for "argcount" argument types (including |
| 374 | * vararg). |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 375 | */ |
| 376 | static int |
| 377 | func_type_add_arg_types( |
| 378 | type_T *functype, |
| 379 | int argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 380 | garray_T *type_gap) |
| 381 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 382 | // To make it easy to free the space needed for the argument types, add the |
| 383 | // pointer to type_gap. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 384 | if (ga_grow(type_gap, 1) == FAIL) |
| 385 | return FAIL; |
| 386 | functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); |
| 387 | if (functype->tt_args == NULL) |
| 388 | return FAIL; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 389 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = |
| 390 | (void *)functype->tt_args; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 391 | ++type_gap->ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 392 | return OK; |
| 393 | } |
| 394 | |
| 395 | /* |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 396 | * Return the type_T for a typval. Only for primitive types. |
| 397 | */ |
| 398 | static type_T * |
| 399 | typval2type(typval_T *tv) |
| 400 | { |
| 401 | if (tv->v_type == VAR_NUMBER) |
| 402 | return &t_number; |
| 403 | if (tv->v_type == VAR_BOOL) |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 404 | return &t_bool; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 405 | if (tv->v_type == VAR_STRING) |
| 406 | return &t_string; |
| 407 | if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles |
| 408 | return &t_list_string; |
| 409 | if (tv->v_type == VAR_DICT) // e.g. for v:completed_item |
| 410 | return &t_dict_any; |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 411 | return &t_any; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 414 | ///////////////////////////////////////////////////////////////////// |
| 415 | // Following generate_ functions expect the caller to call ga_grow(). |
| 416 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 417 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return NULL |
| 418 | #define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == TRUE) return OK |
| 419 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 420 | /* |
| 421 | * Generate an instruction without arguments. |
| 422 | * Returns a pointer to the new instruction, NULL if failed. |
| 423 | */ |
| 424 | static isn_T * |
| 425 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 426 | { |
| 427 | garray_T *instr = &cctx->ctx_instr; |
| 428 | isn_T *isn; |
| 429 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 430 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 431 | if (ga_grow(instr, 1) == FAIL) |
| 432 | return NULL; |
| 433 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 434 | isn->isn_type = isn_type; |
| 435 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 436 | ++instr->ga_len; |
| 437 | |
| 438 | return isn; |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Generate an instruction without arguments. |
| 443 | * "drop" will be removed from the stack. |
| 444 | * Returns a pointer to the new instruction, NULL if failed. |
| 445 | */ |
| 446 | static isn_T * |
| 447 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 448 | { |
| 449 | garray_T *stack = &cctx->ctx_type_stack; |
| 450 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 451 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 452 | stack->ga_len -= drop; |
| 453 | return generate_instr(cctx, isn_type); |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 458 | */ |
| 459 | static isn_T * |
| 460 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 461 | { |
| 462 | isn_T *isn; |
| 463 | garray_T *stack = &cctx->ctx_type_stack; |
| 464 | |
| 465 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 466 | return NULL; |
| 467 | |
| 468 | if (ga_grow(stack, 1) == FAIL) |
| 469 | return NULL; |
| 470 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 471 | ++stack->ga_len; |
| 472 | |
| 473 | return isn; |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 478 | */ |
| 479 | static int |
| 480 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 481 | { |
| 482 | isn_T *isn; |
| 483 | garray_T *stack = &cctx->ctx_type_stack; |
| 484 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 485 | |
| 486 | if ((*type)->tt_type == VAR_STRING) |
| 487 | return OK; |
| 488 | *type = &t_string; |
| 489 | |
| 490 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 491 | return FAIL; |
| 492 | isn->isn_arg.number = offset; |
| 493 | |
| 494 | return OK; |
| 495 | } |
| 496 | |
| 497 | static int |
| 498 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 499 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 500 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 501 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 502 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 503 | { |
| 504 | if (*op == '+') |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 505 | emsg(_("E1035: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 506 | else |
| 507 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 508 | return FAIL; |
| 509 | } |
| 510 | return OK; |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Generate an instruction with two arguments. The instruction depends on the |
| 515 | * type of the arguments. |
| 516 | */ |
| 517 | static int |
| 518 | generate_two_op(cctx_T *cctx, char_u *op) |
| 519 | { |
| 520 | garray_T *stack = &cctx->ctx_type_stack; |
| 521 | type_T *type1; |
| 522 | type_T *type2; |
| 523 | vartype_T vartype; |
| 524 | isn_T *isn; |
| 525 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 526 | RETURN_OK_IF_SKIP(cctx); |
| 527 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 528 | // Get the known type of the two items on the stack. If they are matching |
| 529 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 530 | // checking. |
| 531 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 532 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 533 | vartype = VAR_ANY; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 534 | if (type1->tt_type == type2->tt_type |
| 535 | && (type1->tt_type == VAR_NUMBER |
| 536 | || type1->tt_type == VAR_LIST |
| 537 | #ifdef FEAT_FLOAT |
| 538 | || type1->tt_type == VAR_FLOAT |
| 539 | #endif |
| 540 | || type1->tt_type == VAR_BLOB)) |
| 541 | vartype = type1->tt_type; |
| 542 | |
| 543 | switch (*op) |
| 544 | { |
| 545 | case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 546 | && type1->tt_type != VAR_ANY |
| 547 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 548 | && check_number_or_float( |
| 549 | type1->tt_type, type2->tt_type, op) == FAIL) |
| 550 | return FAIL; |
| 551 | isn = generate_instr_drop(cctx, |
| 552 | vartype == VAR_NUMBER ? ISN_OPNR |
| 553 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 554 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 555 | #ifdef FEAT_FLOAT |
| 556 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 557 | #endif |
| 558 | : ISN_OPANY, 1); |
| 559 | if (isn != NULL) |
| 560 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 561 | break; |
| 562 | |
| 563 | case '-': |
| 564 | case '*': |
| 565 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 566 | op) == FAIL) |
| 567 | return FAIL; |
| 568 | if (vartype == VAR_NUMBER) |
| 569 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 570 | #ifdef FEAT_FLOAT |
| 571 | else if (vartype == VAR_FLOAT) |
| 572 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 573 | #endif |
| 574 | else |
| 575 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 576 | if (isn != NULL) |
| 577 | isn->isn_arg.op.op_type = *op == '*' |
| 578 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 579 | break; |
| 580 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 581 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 582 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 583 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 584 | && type2->tt_type != VAR_NUMBER)) |
| 585 | { |
| 586 | emsg(_("E1035: % requires number arguments")); |
| 587 | return FAIL; |
| 588 | } |
| 589 | isn = generate_instr_drop(cctx, |
| 590 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 591 | if (isn != NULL) |
| 592 | isn->isn_arg.op.op_type = EXPR_REM; |
| 593 | break; |
| 594 | } |
| 595 | |
| 596 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 597 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 598 | { |
| 599 | type_T *type = &t_any; |
| 600 | |
| 601 | #ifdef FEAT_FLOAT |
| 602 | // float+number and number+float results in float |
| 603 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 604 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 605 | type = &t_float; |
| 606 | #endif |
| 607 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 608 | } |
| 609 | |
| 610 | return OK; |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 615 | */ |
| 616 | static int |
| 617 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 618 | { |
| 619 | isntype_T isntype = ISN_DROP; |
| 620 | isn_T *isn; |
| 621 | garray_T *stack = &cctx->ctx_type_stack; |
| 622 | vartype_T type1; |
| 623 | vartype_T type2; |
| 624 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 625 | RETURN_OK_IF_SKIP(cctx); |
| 626 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 627 | // Get the known type of the two items on the stack. If they are matching |
| 628 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 629 | // checking. |
| 630 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 631 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 632 | if (type1 == VAR_UNKNOWN) |
| 633 | type1 = VAR_ANY; |
| 634 | if (type2 == VAR_UNKNOWN) |
| 635 | type2 = VAR_ANY; |
| 636 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 637 | if (type1 == type2) |
| 638 | { |
| 639 | switch (type1) |
| 640 | { |
| 641 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 642 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 643 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 644 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 645 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 646 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 647 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 648 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 649 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 650 | default: isntype = ISN_COMPAREANY; break; |
| 651 | } |
| 652 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 653 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 654 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 655 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 656 | isntype = ISN_COMPAREANY; |
| 657 | |
| 658 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 659 | && (isntype == ISN_COMPAREBOOL |
| 660 | || isntype == ISN_COMPARESPECIAL |
| 661 | || isntype == ISN_COMPARENR |
| 662 | || isntype == ISN_COMPAREFLOAT)) |
| 663 | { |
| 664 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 665 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
| 666 | return FAIL; |
| 667 | } |
| 668 | if (isntype == ISN_DROP |
| 669 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 670 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 671 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 672 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 673 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 674 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 675 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 676 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 677 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 678 | vartype_name(type1), vartype_name(type2)); |
| 679 | return FAIL; |
| 680 | } |
| 681 | |
| 682 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 683 | return FAIL; |
| 684 | isn->isn_arg.op.op_type = exptype; |
| 685 | isn->isn_arg.op.op_ic = ic; |
| 686 | |
| 687 | // takes two arguments, puts one bool back |
| 688 | if (stack->ga_len >= 2) |
| 689 | { |
| 690 | --stack->ga_len; |
| 691 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 692 | } |
| 693 | |
| 694 | return OK; |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * Generate an ISN_2BOOL instruction. |
| 699 | */ |
| 700 | static int |
| 701 | generate_2BOOL(cctx_T *cctx, int invert) |
| 702 | { |
| 703 | isn_T *isn; |
| 704 | garray_T *stack = &cctx->ctx_type_stack; |
| 705 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 706 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 707 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 708 | return FAIL; |
| 709 | isn->isn_arg.number = invert; |
| 710 | |
| 711 | // type becomes bool |
| 712 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 713 | |
| 714 | return OK; |
| 715 | } |
| 716 | |
| 717 | static int |
| 718 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 719 | { |
| 720 | isn_T *isn; |
| 721 | garray_T *stack = &cctx->ctx_type_stack; |
| 722 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 723 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 724 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 725 | return FAIL; |
| 726 | isn->isn_arg.type.ct_type = vartype->tt_type; // TODO: whole type |
| 727 | isn->isn_arg.type.ct_off = offset; |
| 728 | |
| 729 | // type becomes vartype |
| 730 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = vartype; |
| 731 | |
| 732 | return OK; |
| 733 | } |
| 734 | |
| 735 | /* |
| 736 | * Generate an ISN_PUSHNR instruction. |
| 737 | */ |
| 738 | static int |
| 739 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 740 | { |
| 741 | isn_T *isn; |
| 742 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 743 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 744 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 745 | return FAIL; |
| 746 | isn->isn_arg.number = number; |
| 747 | |
| 748 | return OK; |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | * Generate an ISN_PUSHBOOL instruction. |
| 753 | */ |
| 754 | static int |
| 755 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 756 | { |
| 757 | isn_T *isn; |
| 758 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 759 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 760 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 761 | return FAIL; |
| 762 | isn->isn_arg.number = number; |
| 763 | |
| 764 | return OK; |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * Generate an ISN_PUSHSPEC instruction. |
| 769 | */ |
| 770 | static int |
| 771 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 772 | { |
| 773 | isn_T *isn; |
| 774 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 775 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 776 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 777 | return FAIL; |
| 778 | isn->isn_arg.number = number; |
| 779 | |
| 780 | return OK; |
| 781 | } |
| 782 | |
| 783 | #ifdef FEAT_FLOAT |
| 784 | /* |
| 785 | * Generate an ISN_PUSHF instruction. |
| 786 | */ |
| 787 | static int |
| 788 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 789 | { |
| 790 | isn_T *isn; |
| 791 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 792 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 793 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 794 | return FAIL; |
| 795 | isn->isn_arg.fnumber = fnumber; |
| 796 | |
| 797 | return OK; |
| 798 | } |
| 799 | #endif |
| 800 | |
| 801 | /* |
| 802 | * Generate an ISN_PUSHS instruction. |
| 803 | * Consumes "str". |
| 804 | */ |
| 805 | static int |
| 806 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 807 | { |
| 808 | isn_T *isn; |
| 809 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 810 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 811 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 812 | return FAIL; |
| 813 | isn->isn_arg.string = str; |
| 814 | |
| 815 | return OK; |
| 816 | } |
| 817 | |
| 818 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 819 | * Generate an ISN_PUSHCHANNEL instruction. |
| 820 | * Consumes "channel". |
| 821 | */ |
| 822 | static int |
| 823 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 824 | { |
| 825 | isn_T *isn; |
| 826 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 827 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 828 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 829 | return FAIL; |
| 830 | isn->isn_arg.channel = channel; |
| 831 | |
| 832 | return OK; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * Generate an ISN_PUSHJOB instruction. |
| 837 | * Consumes "job". |
| 838 | */ |
| 839 | static int |
| 840 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 841 | { |
| 842 | isn_T *isn; |
| 843 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 844 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 845 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 846 | return FAIL; |
| 847 | isn->isn_arg.job = job; |
| 848 | |
| 849 | return OK; |
| 850 | } |
| 851 | |
| 852 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 853 | * Generate an ISN_PUSHBLOB instruction. |
| 854 | * Consumes "blob". |
| 855 | */ |
| 856 | static int |
| 857 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 858 | { |
| 859 | isn_T *isn; |
| 860 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 861 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 862 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 863 | return FAIL; |
| 864 | isn->isn_arg.blob = blob; |
| 865 | |
| 866 | return OK; |
| 867 | } |
| 868 | |
| 869 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 870 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 871 | * Consumes "name". |
| 872 | */ |
| 873 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 874 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 875 | { |
| 876 | isn_T *isn; |
| 877 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 878 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 879 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 880 | return FAIL; |
| 881 | isn->isn_arg.string = name; |
| 882 | |
| 883 | return OK; |
| 884 | } |
| 885 | |
| 886 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 887 | * Generate an ISN_STORE instruction. |
| 888 | */ |
| 889 | static int |
| 890 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 891 | { |
| 892 | isn_T *isn; |
| 893 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 894 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 895 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 896 | return FAIL; |
| 897 | if (name != NULL) |
| 898 | isn->isn_arg.string = vim_strsave(name); |
| 899 | else |
| 900 | isn->isn_arg.number = idx; |
| 901 | |
| 902 | return OK; |
| 903 | } |
| 904 | |
| 905 | /* |
| 906 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 907 | */ |
| 908 | static int |
| 909 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 910 | { |
| 911 | isn_T *isn; |
| 912 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 913 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 914 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 915 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 916 | isn->isn_arg.storenr.stnr_idx = idx; |
| 917 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 918 | |
| 919 | return OK; |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Generate an ISN_STOREOPT instruction |
| 924 | */ |
| 925 | static int |
| 926 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 927 | { |
| 928 | isn_T *isn; |
| 929 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 930 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 931 | if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL) |
| 932 | return FAIL; |
| 933 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 934 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 935 | |
| 936 | return OK; |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * Generate an ISN_LOAD or similar instruction. |
| 941 | */ |
| 942 | static int |
| 943 | generate_LOAD( |
| 944 | cctx_T *cctx, |
| 945 | isntype_T isn_type, |
| 946 | int idx, |
| 947 | char_u *name, |
| 948 | type_T *type) |
| 949 | { |
| 950 | isn_T *isn; |
| 951 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 952 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 953 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 954 | return FAIL; |
| 955 | if (name != NULL) |
| 956 | isn->isn_arg.string = vim_strsave(name); |
| 957 | else |
| 958 | isn->isn_arg.number = idx; |
| 959 | |
| 960 | return OK; |
| 961 | } |
| 962 | |
| 963 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 964 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 965 | */ |
| 966 | static int |
| 967 | generate_LOADV( |
| 968 | cctx_T *cctx, |
| 969 | char_u *name, |
| 970 | int error) |
| 971 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 972 | int di_flags; |
| 973 | int vidx = find_vim_var(name, &di_flags); |
| 974 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 975 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 976 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 977 | if (vidx < 0) |
| 978 | { |
| 979 | if (error) |
| 980 | semsg(_(e_var_notfound), name); |
| 981 | return FAIL; |
| 982 | } |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 983 | type = typval2type(get_vim_var_tv(vidx)); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 984 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 985 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 989 | * Generate an ISN_LOADS instruction. |
| 990 | */ |
| 991 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 992 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 993 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 994 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 995 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 996 | int sid, |
| 997 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 998 | { |
| 999 | isn_T *isn; |
| 1000 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1001 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1002 | if (isn_type == ISN_LOADS) |
| 1003 | isn = generate_instr_type(cctx, isn_type, type); |
| 1004 | else |
| 1005 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1006 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1007 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1008 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1009 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1010 | |
| 1011 | return OK; |
| 1012 | } |
| 1013 | |
| 1014 | /* |
| 1015 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1016 | */ |
| 1017 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1018 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1019 | cctx_T *cctx, |
| 1020 | isntype_T isn_type, |
| 1021 | int sid, |
| 1022 | int idx, |
| 1023 | type_T *type) |
| 1024 | { |
| 1025 | isn_T *isn; |
| 1026 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1027 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1028 | if (isn_type == ISN_LOADSCRIPT) |
| 1029 | isn = generate_instr_type(cctx, isn_type, type); |
| 1030 | else |
| 1031 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1032 | if (isn == NULL) |
| 1033 | return FAIL; |
| 1034 | isn->isn_arg.script.script_sid = sid; |
| 1035 | isn->isn_arg.script.script_idx = idx; |
| 1036 | return OK; |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * Generate an ISN_NEWLIST instruction. |
| 1041 | */ |
| 1042 | static int |
| 1043 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1044 | { |
| 1045 | isn_T *isn; |
| 1046 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1047 | type_T *type; |
| 1048 | type_T *member; |
| 1049 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1050 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1051 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1052 | return FAIL; |
| 1053 | isn->isn_arg.number = count; |
| 1054 | |
| 1055 | // drop the value types |
| 1056 | stack->ga_len -= count; |
| 1057 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1058 | // Use the first value type for the list member type. Use "any" for an |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1059 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1060 | if (count > 0) |
| 1061 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1062 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1063 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1064 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1065 | |
| 1066 | // add the list type to the type stack |
| 1067 | if (ga_grow(stack, 1) == FAIL) |
| 1068 | return FAIL; |
| 1069 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1070 | ++stack->ga_len; |
| 1071 | |
| 1072 | return OK; |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Generate an ISN_NEWDICT instruction. |
| 1077 | */ |
| 1078 | static int |
| 1079 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1080 | { |
| 1081 | isn_T *isn; |
| 1082 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1083 | type_T *type; |
| 1084 | type_T *member; |
| 1085 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1086 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1087 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1088 | return FAIL; |
| 1089 | isn->isn_arg.number = count; |
| 1090 | |
| 1091 | // drop the key and value types |
| 1092 | stack->ga_len -= 2 * count; |
| 1093 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1094 | // Use the first value type for the list member type. Use "void" for an |
| 1095 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1096 | if (count > 0) |
| 1097 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1098 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1099 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1100 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1101 | |
| 1102 | // add the dict type to the type stack |
| 1103 | if (ga_grow(stack, 1) == FAIL) |
| 1104 | return FAIL; |
| 1105 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1106 | ++stack->ga_len; |
| 1107 | |
| 1108 | return OK; |
| 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | * Generate an ISN_FUNCREF instruction. |
| 1113 | */ |
| 1114 | static int |
| 1115 | generate_FUNCREF(cctx_T *cctx, int dfunc_idx) |
| 1116 | { |
| 1117 | isn_T *isn; |
| 1118 | garray_T *stack = &cctx->ctx_type_stack; |
| 1119 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1120 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1121 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1122 | return FAIL; |
| 1123 | isn->isn_arg.number = dfunc_idx; |
| 1124 | |
| 1125 | if (ga_grow(stack, 1) == FAIL) |
| 1126 | return FAIL; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1127 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1128 | // TODO: argument and return types |
| 1129 | ++stack->ga_len; |
| 1130 | |
| 1131 | return OK; |
| 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * Generate an ISN_JUMP instruction. |
| 1136 | */ |
| 1137 | static int |
| 1138 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1139 | { |
| 1140 | isn_T *isn; |
| 1141 | garray_T *stack = &cctx->ctx_type_stack; |
| 1142 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1143 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1144 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1145 | return FAIL; |
| 1146 | isn->isn_arg.jump.jump_when = when; |
| 1147 | isn->isn_arg.jump.jump_where = where; |
| 1148 | |
| 1149 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1150 | --stack->ga_len; |
| 1151 | |
| 1152 | return OK; |
| 1153 | } |
| 1154 | |
| 1155 | static int |
| 1156 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1157 | { |
| 1158 | isn_T *isn; |
| 1159 | garray_T *stack = &cctx->ctx_type_stack; |
| 1160 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1161 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1162 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1163 | return FAIL; |
| 1164 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1165 | |
| 1166 | if (ga_grow(stack, 1) == FAIL) |
| 1167 | return FAIL; |
| 1168 | // type doesn't matter, will be stored next |
| 1169 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1170 | ++stack->ga_len; |
| 1171 | |
| 1172 | return OK; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * Generate an ISN_BCALL instruction. |
| 1177 | * Return FAIL if the number of arguments is wrong. |
| 1178 | */ |
| 1179 | static int |
| 1180 | generate_BCALL(cctx_T *cctx, int func_idx, int argcount) |
| 1181 | { |
| 1182 | isn_T *isn; |
| 1183 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1184 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1185 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1186 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1187 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1188 | if (check_internal_func(func_idx, argcount) == FAIL) |
| 1189 | return FAIL; |
| 1190 | |
| 1191 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1192 | return FAIL; |
| 1193 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1194 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1195 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1196 | for (i = 0; i < argcount; ++i) |
| 1197 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1198 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1199 | stack->ga_len -= argcount; // drop the arguments |
| 1200 | if (ga_grow(stack, 1) == FAIL) |
| 1201 | return FAIL; |
| 1202 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1203 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1204 | ++stack->ga_len; // add return value |
| 1205 | |
| 1206 | return OK; |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1211 | * Return FAIL if the number of arguments is wrong. |
| 1212 | */ |
| 1213 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1214 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1215 | { |
| 1216 | isn_T *isn; |
| 1217 | garray_T *stack = &cctx->ctx_type_stack; |
| 1218 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1219 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1220 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1221 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1222 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1223 | { |
| 1224 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1225 | return FAIL; |
| 1226 | } |
| 1227 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1228 | { |
| 1229 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1230 | return FAIL; |
| 1231 | } |
| 1232 | |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1233 | if (ufunc->uf_dfunc_idx >= 0) |
| 1234 | { |
| 1235 | int i; |
| 1236 | |
| 1237 | for (i = 0; i < argcount; ++i) |
| 1238 | { |
| 1239 | type_T *expected; |
| 1240 | type_T *actual; |
| 1241 | |
| 1242 | if (i < regular_args) |
| 1243 | { |
| 1244 | if (ufunc->uf_arg_types == NULL) |
| 1245 | continue; |
| 1246 | expected = ufunc->uf_arg_types[i]; |
| 1247 | } |
| 1248 | else |
| 1249 | expected = ufunc->uf_va_type->tt_member; |
| 1250 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1251 | if (check_type(expected, actual, FALSE) == FAIL) |
| 1252 | { |
| 1253 | arg_type_mismatch(expected, actual, i + 1); |
| 1254 | return FAIL; |
| 1255 | } |
| 1256 | } |
| 1257 | } |
| 1258 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1259 | if ((isn = generate_instr(cctx, |
| 1260 | ufunc->uf_dfunc_idx >= 0 ? ISN_DCALL : ISN_UCALL)) == NULL) |
| 1261 | return FAIL; |
| 1262 | if (ufunc->uf_dfunc_idx >= 0) |
| 1263 | { |
| 1264 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1265 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1266 | } |
| 1267 | else |
| 1268 | { |
| 1269 | // A user function may be deleted and redefined later, can't use the |
| 1270 | // ufunc pointer, need to look it up again at runtime. |
| 1271 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1272 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1273 | } |
| 1274 | |
| 1275 | stack->ga_len -= argcount; // drop the arguments |
| 1276 | if (ga_grow(stack, 1) == FAIL) |
| 1277 | return FAIL; |
| 1278 | // add return value |
| 1279 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1280 | ++stack->ga_len; |
| 1281 | |
| 1282 | return OK; |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1287 | */ |
| 1288 | static int |
| 1289 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1290 | { |
| 1291 | isn_T *isn; |
| 1292 | garray_T *stack = &cctx->ctx_type_stack; |
| 1293 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1294 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1295 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1296 | return FAIL; |
| 1297 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1298 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1299 | |
| 1300 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1301 | if (ga_grow(stack, 1) == FAIL) |
| 1302 | return FAIL; |
| 1303 | // add return value |
| 1304 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1305 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1306 | |
| 1307 | return OK; |
| 1308 | } |
| 1309 | |
| 1310 | /* |
| 1311 | * Generate an ISN_PCALL instruction. |
| 1312 | */ |
| 1313 | static int |
| 1314 | generate_PCALL(cctx_T *cctx, int argcount, int at_top) |
| 1315 | { |
| 1316 | isn_T *isn; |
| 1317 | garray_T *stack = &cctx->ctx_type_stack; |
| 1318 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1319 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1320 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1321 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1322 | return FAIL; |
| 1323 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1324 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1325 | |
| 1326 | stack->ga_len -= argcount; // drop the arguments |
| 1327 | |
| 1328 | // drop the funcref/partial, get back the return value |
| 1329 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_any; |
| 1330 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1331 | // If partial is above the arguments it must be cleared and replaced with |
| 1332 | // the return value. |
| 1333 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1334 | return FAIL; |
| 1335 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1336 | return OK; |
| 1337 | } |
| 1338 | |
| 1339 | /* |
| 1340 | * Generate an ISN_MEMBER instruction. |
| 1341 | */ |
| 1342 | static int |
| 1343 | generate_MEMBER(cctx_T *cctx, char_u *name, size_t len) |
| 1344 | { |
| 1345 | isn_T *isn; |
| 1346 | garray_T *stack = &cctx->ctx_type_stack; |
| 1347 | type_T *type; |
| 1348 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1349 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1350 | if ((isn = generate_instr(cctx, ISN_MEMBER)) == NULL) |
| 1351 | return FAIL; |
| 1352 | isn->isn_arg.string = vim_strnsave(name, (int)len); |
| 1353 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1354 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1355 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1356 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1357 | { |
| 1358 | emsg(_(e_dictreq)); |
| 1359 | return FAIL; |
| 1360 | } |
| 1361 | // change dict type to dict member type |
| 1362 | if (type->tt_type == VAR_DICT) |
| 1363 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1364 | |
| 1365 | return OK; |
| 1366 | } |
| 1367 | |
| 1368 | /* |
| 1369 | * Generate an ISN_ECHO instruction. |
| 1370 | */ |
| 1371 | static int |
| 1372 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1373 | { |
| 1374 | isn_T *isn; |
| 1375 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1376 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1377 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1378 | return FAIL; |
| 1379 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1380 | isn->isn_arg.echo.echo_count = count; |
| 1381 | |
| 1382 | return OK; |
| 1383 | } |
| 1384 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1385 | /* |
| 1386 | * Generate an ISN_EXECUTE instruction. |
| 1387 | */ |
| 1388 | static int |
| 1389 | generate_EXECUTE(cctx_T *cctx, int count) |
| 1390 | { |
| 1391 | isn_T *isn; |
| 1392 | |
| 1393 | if ((isn = generate_instr_drop(cctx, ISN_EXECUTE, count)) == NULL) |
| 1394 | return FAIL; |
| 1395 | isn->isn_arg.number = count; |
| 1396 | |
| 1397 | return OK; |
| 1398 | } |
| 1399 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1400 | static int |
| 1401 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1402 | { |
| 1403 | isn_T *isn; |
| 1404 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1405 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1406 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1407 | return FAIL; |
| 1408 | isn->isn_arg.string = vim_strsave(line); |
| 1409 | return OK; |
| 1410 | } |
| 1411 | |
| 1412 | static char e_white_both[] = |
| 1413 | N_("E1004: white space required before and after '%s'"); |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1414 | static char e_white_after[] = N_("E1069: white space required after '%s'"); |
| 1415 | static char e_no_white_before[] = N_("E1068: No white space allowed before '%s'"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1416 | |
| 1417 | /* |
| 1418 | * Reserve space for a local variable. |
| 1419 | * Return the index or -1 if it failed. |
| 1420 | */ |
| 1421 | static int |
| 1422 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1423 | { |
| 1424 | int idx; |
| 1425 | lvar_T *lvar; |
| 1426 | |
| 1427 | if (lookup_arg(name, len, cctx) >= 0 || lookup_vararg(name, len, cctx)) |
| 1428 | { |
| 1429 | emsg_namelen(_("E1006: %s is used as an argument"), name, (int)len); |
| 1430 | return -1; |
| 1431 | } |
| 1432 | |
| 1433 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
| 1434 | return -1; |
| 1435 | idx = cctx->ctx_locals.ga_len; |
| 1436 | if (cctx->ctx_max_local < idx + 1) |
| 1437 | cctx->ctx_max_local = idx + 1; |
| 1438 | ++cctx->ctx_locals.ga_len; |
| 1439 | |
| 1440 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1441 | lvar->lv_name = vim_strnsave(name, (int)(len == 0 ? STRLEN(name) : len)); |
| 1442 | lvar->lv_const = isConst; |
| 1443 | lvar->lv_type = type; |
| 1444 | |
| 1445 | return idx; |
| 1446 | } |
| 1447 | |
| 1448 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1449 | * Remove local variables above "new_top". |
| 1450 | */ |
| 1451 | static void |
| 1452 | unwind_locals(cctx_T *cctx, int new_top) |
| 1453 | { |
| 1454 | if (cctx->ctx_locals.ga_len > new_top) |
| 1455 | { |
| 1456 | int idx; |
| 1457 | lvar_T *lvar; |
| 1458 | |
| 1459 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1460 | { |
| 1461 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1462 | vim_free(lvar->lv_name); |
| 1463 | } |
| 1464 | } |
| 1465 | cctx->ctx_locals.ga_len = new_top; |
| 1466 | } |
| 1467 | |
| 1468 | /* |
| 1469 | * Free all local variables. |
| 1470 | */ |
| 1471 | static void |
| 1472 | free_local(cctx_T *cctx) |
| 1473 | { |
| 1474 | unwind_locals(cctx, 0); |
| 1475 | ga_clear(&cctx->ctx_locals); |
| 1476 | } |
| 1477 | |
| 1478 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1479 | * Skip over a type definition and return a pointer to just after it. |
| 1480 | */ |
| 1481 | char_u * |
| 1482 | skip_type(char_u *start) |
| 1483 | { |
| 1484 | char_u *p = start; |
| 1485 | |
| 1486 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1487 | ++p; |
| 1488 | |
| 1489 | // Skip over "<type>"; this is permissive about white space. |
| 1490 | if (*skipwhite(p) == '<') |
| 1491 | { |
| 1492 | p = skipwhite(p); |
| 1493 | p = skip_type(skipwhite(p + 1)); |
| 1494 | p = skipwhite(p); |
| 1495 | if (*p == '>') |
| 1496 | ++p; |
| 1497 | } |
| 1498 | return p; |
| 1499 | } |
| 1500 | |
| 1501 | /* |
| 1502 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1503 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1504 | * Returns NULL in case of failure. |
| 1505 | */ |
| 1506 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1507 | parse_type_member(char_u **arg, type_T *type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1508 | { |
| 1509 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1510 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1511 | |
| 1512 | if (**arg != '<') |
| 1513 | { |
| 1514 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1515 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1516 | else |
| 1517 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1518 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1519 | } |
| 1520 | *arg = skipwhite(*arg + 1); |
| 1521 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1522 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1523 | |
| 1524 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1525 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1526 | { |
| 1527 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1528 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1529 | } |
| 1530 | ++*arg; |
| 1531 | |
| 1532 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1533 | return get_list_type(member_type, type_gap); |
| 1534 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | /* |
| 1538 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 1539 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1540 | */ |
| 1541 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1542 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1543 | { |
| 1544 | char_u *p = *arg; |
| 1545 | size_t len; |
| 1546 | |
| 1547 | // skip over the first word |
| 1548 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1549 | ++p; |
| 1550 | len = p - *arg; |
| 1551 | |
| 1552 | switch (**arg) |
| 1553 | { |
| 1554 | case 'a': |
| 1555 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 1556 | { |
| 1557 | *arg += len; |
| 1558 | return &t_any; |
| 1559 | } |
| 1560 | break; |
| 1561 | case 'b': |
| 1562 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 1563 | { |
| 1564 | *arg += len; |
| 1565 | return &t_bool; |
| 1566 | } |
| 1567 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 1568 | { |
| 1569 | *arg += len; |
| 1570 | return &t_blob; |
| 1571 | } |
| 1572 | break; |
| 1573 | case 'c': |
| 1574 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 1575 | { |
| 1576 | *arg += len; |
| 1577 | return &t_channel; |
| 1578 | } |
| 1579 | break; |
| 1580 | case 'd': |
| 1581 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 1582 | { |
| 1583 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1584 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1585 | } |
| 1586 | break; |
| 1587 | case 'f': |
| 1588 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 1589 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1590 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1591 | *arg += len; |
| 1592 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1593 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1594 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1595 | return &t_any; |
| 1596 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1597 | } |
| 1598 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 1599 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1600 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 1601 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1602 | int argcount = -1; |
| 1603 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1604 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1605 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 1606 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1607 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1608 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1609 | if (**arg == '(') |
| 1610 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1611 | // "func" may or may not return a value, "func()" does |
| 1612 | // not return a value. |
| 1613 | ret_type = &t_void; |
| 1614 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1615 | p = ++*arg; |
| 1616 | argcount = 0; |
| 1617 | while (*p != NUL && *p != ')') |
| 1618 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1619 | if (*p == '?') |
| 1620 | { |
| 1621 | if (first_optional == -1) |
| 1622 | first_optional = argcount; |
| 1623 | ++p; |
| 1624 | } |
| 1625 | else if (first_optional != -1) |
| 1626 | { |
| 1627 | emsg(_("E1007: mandatory argument after optional argument")); |
| 1628 | return &t_any; |
| 1629 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1630 | else if (STRNCMP(p, "...", 3) == 0) |
| 1631 | { |
| 1632 | flags |= TTFLAG_VARARGS; |
| 1633 | p += 3; |
| 1634 | } |
| 1635 | |
| 1636 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 1637 | |
| 1638 | // Nothing comes after "...{type}". |
| 1639 | if (flags & TTFLAG_VARARGS) |
| 1640 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1641 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1642 | if (*p != ',' && *skipwhite(p) == ',') |
| 1643 | { |
| 1644 | semsg(_(e_no_white_before), ","); |
| 1645 | return &t_any; |
| 1646 | } |
| 1647 | if (*p == ',') |
| 1648 | { |
| 1649 | ++p; |
| 1650 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1651 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1652 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1653 | return &t_any; |
| 1654 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1655 | } |
| 1656 | p = skipwhite(p); |
| 1657 | if (argcount == MAX_FUNC_ARGS) |
| 1658 | { |
| 1659 | emsg(_("E740: Too many argument types")); |
| 1660 | return &t_any; |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | p = skipwhite(p); |
| 1665 | if (*p != ')') |
| 1666 | { |
| 1667 | emsg(_(e_missing_close)); |
| 1668 | return &t_any; |
| 1669 | } |
| 1670 | *arg = p + 1; |
| 1671 | } |
| 1672 | if (**arg == ':') |
| 1673 | { |
| 1674 | // parse return type |
| 1675 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 1676 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1677 | semsg(_(e_white_after), ":"); |
| 1678 | *arg = skipwhite(*arg); |
| 1679 | ret_type = parse_type(arg, type_gap); |
| 1680 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1681 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1682 | type = get_func_type(ret_type, argcount, type_gap); |
| 1683 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1684 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1685 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 1686 | type->tt_flags = flags; |
| 1687 | if (argcount > 0) |
| 1688 | { |
| 1689 | type->tt_argcount = argcount; |
| 1690 | type->tt_min_argcount = first_optional == -1 |
| 1691 | ? argcount : first_optional; |
| 1692 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1693 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1694 | return &t_any; |
| 1695 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1696 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1697 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1698 | } |
| 1699 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1700 | } |
| 1701 | break; |
| 1702 | case 'j': |
| 1703 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 1704 | { |
| 1705 | *arg += len; |
| 1706 | return &t_job; |
| 1707 | } |
| 1708 | break; |
| 1709 | case 'l': |
| 1710 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 1711 | { |
| 1712 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1713 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1714 | } |
| 1715 | break; |
| 1716 | case 'n': |
| 1717 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 1718 | { |
| 1719 | *arg += len; |
| 1720 | return &t_number; |
| 1721 | } |
| 1722 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1723 | case 's': |
| 1724 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 1725 | { |
| 1726 | *arg += len; |
| 1727 | return &t_string; |
| 1728 | } |
| 1729 | break; |
| 1730 | case 'v': |
| 1731 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 1732 | { |
| 1733 | *arg += len; |
| 1734 | return &t_void; |
| 1735 | } |
| 1736 | break; |
| 1737 | } |
| 1738 | |
| 1739 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 1740 | return &t_any; |
| 1741 | } |
| 1742 | |
| 1743 | /* |
| 1744 | * Check if "type1" and "type2" are exactly the same. |
| 1745 | */ |
| 1746 | static int |
| 1747 | equal_type(type_T *type1, type_T *type2) |
| 1748 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 1749 | int i; |
| 1750 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1751 | if (type1->tt_type != type2->tt_type) |
| 1752 | return FALSE; |
| 1753 | switch (type1->tt_type) |
| 1754 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1755 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 1756 | case VAR_ANY: |
| 1757 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1758 | case VAR_SPECIAL: |
| 1759 | case VAR_BOOL: |
| 1760 | case VAR_NUMBER: |
| 1761 | case VAR_FLOAT: |
| 1762 | case VAR_STRING: |
| 1763 | case VAR_BLOB: |
| 1764 | case VAR_JOB: |
| 1765 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 1766 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1767 | case VAR_LIST: |
| 1768 | case VAR_DICT: |
| 1769 | return equal_type(type1->tt_member, type2->tt_member); |
| 1770 | case VAR_FUNC: |
| 1771 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 1772 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 1773 | || type1->tt_argcount != type2->tt_argcount) |
| 1774 | return FALSE; |
| 1775 | if (type1->tt_argcount < 0 |
| 1776 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 1777 | return TRUE; |
| 1778 | for (i = 0; i < type1->tt_argcount; ++i) |
| 1779 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 1780 | return FALSE; |
| 1781 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1782 | } |
| 1783 | return TRUE; |
| 1784 | } |
| 1785 | |
| 1786 | /* |
| 1787 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 1788 | * "type2" and "dest" may be the same. |
| 1789 | */ |
| 1790 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1791 | common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1792 | { |
| 1793 | if (equal_type(type1, type2)) |
| 1794 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 1795 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1796 | return; |
| 1797 | } |
| 1798 | |
| 1799 | if (type1->tt_type == type2->tt_type) |
| 1800 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1801 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 1802 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 1803 | type_T *common; |
| 1804 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1805 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 1806 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1807 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 1808 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1809 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1810 | return; |
| 1811 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 1812 | if (type1->tt_type == VAR_FUNC) |
| 1813 | { |
| 1814 | type_T *common; |
| 1815 | |
| 1816 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 1817 | if (type1->tt_argcount == type2->tt_argcount |
| 1818 | && type1->tt_argcount >= 0) |
| 1819 | { |
| 1820 | int argcount = type1->tt_argcount; |
| 1821 | int i; |
| 1822 | |
| 1823 | *dest = alloc_func_type(common, argcount, type_gap); |
| 1824 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 1825 | { |
| 1826 | (*dest)->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); |
| 1827 | if ((*dest)->tt_args != NULL) |
| 1828 | for (i = 0; i < argcount; ++i) |
| 1829 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 1830 | &(*dest)->tt_args[i], type_gap); |
| 1831 | } |
| 1832 | } |
| 1833 | else |
| 1834 | *dest = alloc_func_type(common, -1, type_gap); |
| 1835 | return; |
| 1836 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1837 | } |
| 1838 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 1839 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | char * |
| 1843 | vartype_name(vartype_T type) |
| 1844 | { |
| 1845 | switch (type) |
| 1846 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 1847 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 1848 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1849 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1850 | case VAR_SPECIAL: return "special"; |
| 1851 | case VAR_BOOL: return "bool"; |
| 1852 | case VAR_NUMBER: return "number"; |
| 1853 | case VAR_FLOAT: return "float"; |
| 1854 | case VAR_STRING: return "string"; |
| 1855 | case VAR_BLOB: return "blob"; |
| 1856 | case VAR_JOB: return "job"; |
| 1857 | case VAR_CHANNEL: return "channel"; |
| 1858 | case VAR_LIST: return "list"; |
| 1859 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1860 | |
| 1861 | case VAR_FUNC: |
| 1862 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1863 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 1864 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | /* |
| 1868 | * Return the name of a type. |
| 1869 | * The result may be in allocated memory, in which case "tofree" is set. |
| 1870 | */ |
| 1871 | char * |
| 1872 | type_name(type_T *type, char **tofree) |
| 1873 | { |
| 1874 | char *name = vartype_name(type->tt_type); |
| 1875 | |
| 1876 | *tofree = NULL; |
| 1877 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 1878 | { |
| 1879 | char *member_free; |
| 1880 | char *member_name = type_name(type->tt_member, &member_free); |
| 1881 | size_t len; |
| 1882 | |
| 1883 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 1884 | *tofree = alloc(len); |
| 1885 | if (*tofree != NULL) |
| 1886 | { |
| 1887 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 1888 | vim_free(member_free); |
| 1889 | return *tofree; |
| 1890 | } |
| 1891 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1892 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1893 | { |
| 1894 | garray_T ga; |
| 1895 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1896 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1897 | |
| 1898 | ga_init2(&ga, 1, 100); |
| 1899 | if (ga_grow(&ga, 20) == FAIL) |
| 1900 | return "[unknown]"; |
| 1901 | *tofree = ga.ga_data; |
| 1902 | STRCPY(ga.ga_data, "func("); |
| 1903 | ga.ga_len += 5; |
| 1904 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1905 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1906 | { |
| 1907 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1908 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1909 | int len; |
| 1910 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1911 | if (type->tt_args == NULL) |
| 1912 | arg_type = "[unknown]"; |
| 1913 | else |
| 1914 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1915 | if (i > 0) |
| 1916 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 1917 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1918 | ga.ga_len += 2; |
| 1919 | } |
| 1920 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1921 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1922 | { |
| 1923 | vim_free(arg_free); |
| 1924 | return "[unknown]"; |
| 1925 | } |
| 1926 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1927 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1928 | { |
| 1929 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 1930 | ga.ga_len += 3; |
| 1931 | } |
| 1932 | else if (i >= type->tt_min_argcount) |
| 1933 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 1934 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1935 | ga.ga_len += len; |
| 1936 | vim_free(arg_free); |
| 1937 | } |
| 1938 | |
| 1939 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 1940 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1941 | else |
| 1942 | { |
| 1943 | char *ret_free; |
| 1944 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 1945 | int len; |
| 1946 | |
| 1947 | len = (int)STRLEN(ret_name) + 4; |
| 1948 | if (ga_grow(&ga, len) == FAIL) |
| 1949 | { |
| 1950 | vim_free(ret_free); |
| 1951 | return "[unknown]"; |
| 1952 | } |
| 1953 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 1954 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 1955 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1956 | vim_free(ret_free); |
| 1957 | } |
| 1958 | return ga.ga_data; |
| 1959 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1960 | |
| 1961 | return name; |
| 1962 | } |
| 1963 | |
| 1964 | /* |
| 1965 | * Find "name" in script-local items of script "sid". |
| 1966 | * Returns the index in "sn_var_vals" if found. |
| 1967 | * If found but not in "sn_var_vals" returns -1. |
| 1968 | * If not found returns -2. |
| 1969 | */ |
| 1970 | int |
| 1971 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 1972 | { |
| 1973 | hashtab_T *ht; |
| 1974 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1975 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1976 | int idx; |
| 1977 | |
| 1978 | // First look the name up in the hashtable. |
| 1979 | if (sid <= 0 || sid > script_items.ga_len) |
| 1980 | return -1; |
| 1981 | ht = &SCRIPT_VARS(sid); |
| 1982 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 1983 | if (di == NULL) |
| 1984 | return -2; |
| 1985 | |
| 1986 | // Now find the svar_T index in sn_var_vals. |
| 1987 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 1988 | { |
| 1989 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1990 | |
| 1991 | if (sv->sv_tv == &di->di_tv) |
| 1992 | { |
| 1993 | if (check_writable && sv->sv_const) |
| 1994 | semsg(_(e_readonlyvar), name); |
| 1995 | return idx; |
| 1996 | } |
| 1997 | } |
| 1998 | return -1; |
| 1999 | } |
| 2000 | |
| 2001 | /* |
| 2002 | * Find "name" in imported items of the current script/ |
| 2003 | */ |
| 2004 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2005 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2006 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2007 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2008 | int idx; |
| 2009 | |
| 2010 | if (cctx != NULL) |
| 2011 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2012 | { |
| 2013 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2014 | + idx; |
| 2015 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2016 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2017 | : STRLEN(import->imp_name) == len |
| 2018 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2019 | return import; |
| 2020 | } |
| 2021 | |
| 2022 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2023 | { |
| 2024 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2025 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2026 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2027 | : STRLEN(import->imp_name) == len |
| 2028 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2029 | return import; |
| 2030 | } |
| 2031 | return NULL; |
| 2032 | } |
| 2033 | |
| 2034 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2035 | * Free all imported variables. |
| 2036 | */ |
| 2037 | static void |
| 2038 | free_imported(cctx_T *cctx) |
| 2039 | { |
| 2040 | int idx; |
| 2041 | |
| 2042 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2043 | { |
| 2044 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2045 | |
| 2046 | vim_free(import->imp_name); |
| 2047 | } |
| 2048 | ga_clear(&cctx->ctx_imports); |
| 2049 | } |
| 2050 | |
| 2051 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2052 | * Generate an instruction to load script-local variable "name", without the |
| 2053 | * leading "s:". |
| 2054 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2055 | */ |
| 2056 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2057 | compile_load_scriptvar( |
| 2058 | cctx_T *cctx, |
| 2059 | char_u *name, // variable NUL terminated |
| 2060 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2061 | char_u **end, // end of variable |
| 2062 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2063 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2064 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2065 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2066 | imported_T *import; |
| 2067 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2068 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2069 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2070 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2071 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2072 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2073 | } |
| 2074 | if (idx >= 0) |
| 2075 | { |
| 2076 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2077 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2078 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2079 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2080 | return OK; |
| 2081 | } |
| 2082 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2083 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2084 | if (import != NULL) |
| 2085 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2086 | if (import->imp_all) |
| 2087 | { |
| 2088 | char_u *p = skipwhite(*end); |
| 2089 | int name_len; |
| 2090 | ufunc_T *ufunc; |
| 2091 | type_T *type; |
| 2092 | |
| 2093 | // Used "import * as Name", need to lookup the member. |
| 2094 | if (*p != '.') |
| 2095 | { |
| 2096 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2097 | return FAIL; |
| 2098 | } |
| 2099 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2100 | if (VIM_ISWHITE(*p)) |
| 2101 | { |
| 2102 | emsg(_("E1074: no white space allowed after dot")); |
| 2103 | return FAIL; |
| 2104 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2105 | |
| 2106 | idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type); |
| 2107 | // TODO: what if it is a function? |
| 2108 | if (idx < 0) |
| 2109 | return FAIL; |
| 2110 | *end = p; |
| 2111 | |
| 2112 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2113 | import->imp_sid, |
| 2114 | idx, |
| 2115 | type); |
| 2116 | } |
| 2117 | else |
| 2118 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2119 | // TODO: check this is a variable, not a function? |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2120 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2121 | import->imp_sid, |
| 2122 | import->imp_var_vals_idx, |
| 2123 | import->imp_type); |
| 2124 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2125 | return OK; |
| 2126 | } |
| 2127 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2128 | if (error) |
| 2129 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2130 | return FAIL; |
| 2131 | } |
| 2132 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2133 | static int |
| 2134 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2135 | { |
| 2136 | ufunc_T *ufunc = find_func(name, cctx); |
| 2137 | |
| 2138 | if (ufunc == NULL) |
| 2139 | return FAIL; |
| 2140 | |
| 2141 | return generate_PUSHFUNC(cctx, vim_strsave(name), ufunc->uf_func_type); |
| 2142 | } |
| 2143 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2144 | /* |
| 2145 | * Compile a variable name into a load instruction. |
| 2146 | * "end" points to just after the name. |
| 2147 | * When "error" is FALSE do not give an error when not found. |
| 2148 | */ |
| 2149 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2150 | compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2151 | { |
| 2152 | type_T *type; |
| 2153 | char_u *name; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2154 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2155 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2156 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2157 | |
| 2158 | if (*(*arg + 1) == ':') |
| 2159 | { |
| 2160 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2161 | if (end <= *arg + 2) |
| 2162 | name = vim_strsave((char_u *)"[empty]"); |
| 2163 | else |
| 2164 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2165 | if (name == NULL) |
| 2166 | return FAIL; |
| 2167 | |
| 2168 | if (**arg == 'v') |
| 2169 | { |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2170 | res = generate_LOADV(cctx, name, error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2171 | } |
| 2172 | else if (**arg == 'g') |
| 2173 | { |
| 2174 | // Global variables can be defined later, thus we don't check if it |
| 2175 | // exists, give error at runtime. |
| 2176 | res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any); |
| 2177 | } |
| 2178 | else if (**arg == 's') |
| 2179 | { |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2180 | res = compile_load_scriptvar(cctx, name, NULL, NULL, error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2181 | } |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2182 | else if (**arg == 'b') |
| 2183 | { |
| 2184 | semsg("Namespace b: not supported yet: %s", *arg); |
| 2185 | goto theend; |
| 2186 | } |
| 2187 | else if (**arg == 'w') |
| 2188 | { |
| 2189 | semsg("Namespace w: not supported yet: %s", *arg); |
| 2190 | goto theend; |
| 2191 | } |
| 2192 | else if (**arg == 't') |
| 2193 | { |
| 2194 | semsg("Namespace t: not supported yet: %s", *arg); |
| 2195 | goto theend; |
| 2196 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2197 | else |
| 2198 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2199 | semsg("E1075: Namespace not supported: %s", *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2200 | goto theend; |
| 2201 | } |
| 2202 | } |
| 2203 | else |
| 2204 | { |
| 2205 | size_t len = end - *arg; |
| 2206 | int idx; |
| 2207 | int gen_load = FALSE; |
| 2208 | |
| 2209 | name = vim_strnsave(*arg, end - *arg); |
| 2210 | if (name == NULL) |
| 2211 | return FAIL; |
| 2212 | |
| 2213 | idx = lookup_arg(*arg, len, cctx); |
| 2214 | if (idx >= 0) |
| 2215 | { |
| 2216 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 2217 | type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 2218 | else |
| 2219 | type = &t_any; |
| 2220 | |
| 2221 | // Arguments are located above the frame pointer. |
| 2222 | idx -= cctx->ctx_ufunc->uf_args.ga_len + STACK_FRAME_SIZE; |
| 2223 | if (cctx->ctx_ufunc->uf_va_name != NULL) |
| 2224 | --idx; |
| 2225 | gen_load = TRUE; |
| 2226 | } |
| 2227 | else if (lookup_vararg(*arg, len, cctx)) |
| 2228 | { |
| 2229 | // varargs is always the last argument |
| 2230 | idx = -STACK_FRAME_SIZE - 1; |
| 2231 | type = cctx->ctx_ufunc->uf_va_type; |
| 2232 | gen_load = TRUE; |
| 2233 | } |
| 2234 | else |
| 2235 | { |
| 2236 | idx = lookup_local(*arg, len, cctx); |
| 2237 | if (idx >= 0) |
| 2238 | { |
| 2239 | type = (((lvar_T *)cctx->ctx_locals.ga_data) + idx)->lv_type; |
| 2240 | gen_load = TRUE; |
| 2241 | } |
| 2242 | else |
| 2243 | { |
| 2244 | if ((len == 4 && STRNCMP("true", *arg, 4) == 0) |
| 2245 | || (len == 5 && STRNCMP("false", *arg, 5) == 0)) |
| 2246 | res = generate_PUSHBOOL(cctx, **arg == 't' |
| 2247 | ? VVAL_TRUE : VVAL_FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2248 | else |
| 2249 | { |
| 2250 | // "var" can be script-local even without using "s:" if it |
| 2251 | // already exists. |
| 2252 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2253 | == SCRIPT_VERSION_VIM9 |
| 2254 | || lookup_script(*arg, len) == OK) |
| 2255 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2256 | FALSE); |
| 2257 | |
| 2258 | // When the name starts with an uppercase letter or "x:" it |
| 2259 | // can be a user defined function. |
| 2260 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2261 | res = generate_funcref(cctx, name); |
| 2262 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2263 | } |
| 2264 | } |
| 2265 | if (gen_load) |
| 2266 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
| 2267 | } |
| 2268 | |
| 2269 | *arg = end; |
| 2270 | |
| 2271 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2272 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2273 | semsg(_(e_var_notfound), name); |
| 2274 | vim_free(name); |
| 2275 | return res; |
| 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * Compile the argument expressions. |
| 2280 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2281 | */ |
| 2282 | static int |
| 2283 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2284 | { |
| 2285 | char_u *p = *arg; |
| 2286 | |
| 2287 | while (*p != NUL && *p != ')') |
| 2288 | { |
| 2289 | if (compile_expr1(&p, cctx) == FAIL) |
| 2290 | return FAIL; |
| 2291 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2292 | |
| 2293 | if (*p != ',' && *skipwhite(p) == ',') |
| 2294 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2295 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2296 | p = skipwhite(p); |
| 2297 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2298 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2299 | { |
| 2300 | ++p; |
| 2301 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2302 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2303 | } |
| 2304 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2305 | } |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2306 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2307 | if (*p != ')') |
| 2308 | { |
| 2309 | emsg(_(e_missing_close)); |
| 2310 | return FAIL; |
| 2311 | } |
| 2312 | *arg = p + 1; |
| 2313 | return OK; |
| 2314 | } |
| 2315 | |
| 2316 | /* |
| 2317 | * Compile a function call: name(arg1, arg2) |
| 2318 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2319 | * "argcount_init" is 1 for "value->method()" |
| 2320 | * Instructions: |
| 2321 | * EVAL arg1 |
| 2322 | * EVAL arg2 |
| 2323 | * BCALL / DCALL / UCALL |
| 2324 | */ |
| 2325 | static int |
| 2326 | compile_call(char_u **arg, size_t varlen, cctx_T *cctx, int argcount_init) |
| 2327 | { |
| 2328 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2329 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2330 | int argcount = argcount_init; |
| 2331 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2332 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2333 | char_u *tofree = NULL; |
| 2334 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2335 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2336 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2337 | |
| 2338 | if (varlen >= sizeof(namebuf)) |
| 2339 | { |
| 2340 | semsg(_("E1011: name too long: %s"), name); |
| 2341 | return FAIL; |
| 2342 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2343 | vim_strncpy(namebuf, *arg, varlen); |
| 2344 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2345 | |
| 2346 | *arg = skipwhite(*arg + varlen + 1); |
| 2347 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2348 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2349 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2350 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2351 | { |
| 2352 | int idx; |
| 2353 | |
| 2354 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2355 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2356 | if (idx >= 0) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2357 | res = generate_BCALL(cctx, idx, argcount); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2358 | else |
| 2359 | semsg(_(e_unknownfunc), namebuf); |
| 2360 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2361 | } |
| 2362 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2363 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2364 | ufunc = find_func(name, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2365 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2366 | { |
| 2367 | res = generate_CALL(cctx, ufunc, argcount); |
| 2368 | goto theend; |
| 2369 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2370 | |
| 2371 | // If the name is a variable, load it and use PCALL. |
| 2372 | p = namebuf; |
| 2373 | if (compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2374 | { |
| 2375 | res = generate_PCALL(cctx, argcount, FALSE); |
| 2376 | goto theend; |
| 2377 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2378 | |
| 2379 | // The function may be defined only later. Need to figure out at runtime. |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2380 | res = generate_UCALL(cctx, name, argcount); |
| 2381 | |
| 2382 | theend: |
| 2383 | vim_free(tofree); |
| 2384 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2388 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2389 | |
| 2390 | /* |
| 2391 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2392 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2393 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2394 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2395 | * valid name. |
| 2396 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2397 | static char_u * |
| 2398 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2399 | { |
| 2400 | char_u *p; |
| 2401 | |
| 2402 | // Quick check for valid starting character. |
| 2403 | if (!eval_isnamec1(*arg)) |
| 2404 | return arg; |
| 2405 | |
| 2406 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2407 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2408 | // and can be used in slice "[n:]". |
| 2409 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2410 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2411 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2412 | break; |
| 2413 | return p; |
| 2414 | } |
| 2415 | |
| 2416 | /* |
| 2417 | * Like to_name_end() but also skip over a list or dict constant. |
| 2418 | */ |
| 2419 | char_u * |
| 2420 | to_name_const_end(char_u *arg) |
| 2421 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2422 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2423 | typval_T rettv; |
| 2424 | |
| 2425 | if (p == arg && *arg == '[') |
| 2426 | { |
| 2427 | |
| 2428 | // Can be "[1, 2, 3]->Func()". |
| 2429 | if (get_list_tv(&p, &rettv, FALSE, FALSE) == FAIL) |
| 2430 | p = arg; |
| 2431 | } |
| 2432 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 2433 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2434 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2435 | ++p; |
| 2436 | if (eval_dict(&p, &rettv, FALSE, TRUE) == FAIL) |
| 2437 | p = arg; |
| 2438 | } |
| 2439 | else if (p == arg && *arg == '{') |
| 2440 | { |
| 2441 | int ret = get_lambda_tv(&p, &rettv, FALSE); |
| 2442 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2443 | // Can be "{x -> ret}()". |
| 2444 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2445 | if (ret == NOTDONE) |
| 2446 | ret = eval_dict(&p, &rettv, FALSE, FALSE); |
| 2447 | if (ret != OK) |
| 2448 | p = arg; |
| 2449 | } |
| 2450 | |
| 2451 | return p; |
| 2452 | } |
| 2453 | |
| 2454 | static void |
| 2455 | type_mismatch(type_T *expected, type_T *actual) |
| 2456 | { |
| 2457 | char *tofree1, *tofree2; |
| 2458 | |
| 2459 | semsg(_("E1013: type mismatch, expected %s but got %s"), |
| 2460 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 2461 | vim_free(tofree1); |
| 2462 | vim_free(tofree2); |
| 2463 | } |
| 2464 | |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 2465 | static void |
| 2466 | arg_type_mismatch(type_T *expected, type_T *actual, int argidx) |
| 2467 | { |
| 2468 | char *tofree1, *tofree2; |
| 2469 | |
| 2470 | semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"), |
| 2471 | argidx, |
| 2472 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 2473 | vim_free(tofree1); |
| 2474 | vim_free(tofree2); |
| 2475 | } |
| 2476 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2477 | /* |
| 2478 | * Check if the expected and actual types match. |
| 2479 | */ |
| 2480 | static int |
| 2481 | check_type(type_T *expected, type_T *actual, int give_msg) |
| 2482 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2483 | int ret = OK; |
| 2484 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2485 | // When expected is "unknown" we accept any actual type. |
| 2486 | // When expected is "any" we accept any actual type except "void". |
| 2487 | if (expected->tt_type != VAR_UNKNOWN |
| 2488 | && (expected->tt_type != VAR_ANY || actual->tt_type == VAR_VOID)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2489 | { |
| 2490 | if (expected->tt_type != actual->tt_type) |
| 2491 | { |
| 2492 | if (give_msg) |
| 2493 | type_mismatch(expected, actual); |
| 2494 | return FAIL; |
| 2495 | } |
| 2496 | if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) |
| 2497 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2498 | // "unknown" is used for an empty list or dict |
| 2499 | if (actual->tt_member != &t_unknown) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 2500 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2501 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2502 | else if (expected->tt_type == VAR_FUNC) |
| 2503 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2504 | if (expected->tt_member != &t_unknown) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2505 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 2506 | if (ret == OK && expected->tt_argcount != -1 |
| 2507 | && (actual->tt_argcount < expected->tt_min_argcount |
| 2508 | || actual->tt_argcount > expected->tt_argcount)) |
| 2509 | ret = FAIL; |
| 2510 | } |
| 2511 | if (ret == FAIL && give_msg) |
| 2512 | type_mismatch(expected, actual); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2513 | } |
Bram Moolenaar | 8922860 | 2020-04-05 22:14:54 +0200 | [diff] [blame] | 2514 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | /* |
| 2518 | * Check that |
| 2519 | * - "actual" is "expected" type or |
| 2520 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 2521 | * - return FAIL. |
| 2522 | */ |
| 2523 | static int |
| 2524 | need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx) |
| 2525 | { |
Bram Moolenaar | 8922860 | 2020-04-05 22:14:54 +0200 | [diff] [blame] | 2526 | if (check_type(expected, actual, FALSE) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2527 | return OK; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2528 | if (actual->tt_type != VAR_ANY && actual->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2529 | { |
| 2530 | type_mismatch(expected, actual); |
| 2531 | return FAIL; |
| 2532 | } |
| 2533 | generate_TYPECHECK(cctx, expected, offset); |
| 2534 | return OK; |
| 2535 | } |
| 2536 | |
| 2537 | /* |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame^] | 2538 | * Get the next line of the function from "cctx". |
| 2539 | * Returns NULL when at the end. |
| 2540 | */ |
| 2541 | static char_u * |
| 2542 | next_line_from_context(cctx_T *cctx) |
| 2543 | { |
| 2544 | char_u *line = NULL; |
| 2545 | |
| 2546 | do |
| 2547 | { |
| 2548 | ++cctx->ctx_lnum; |
| 2549 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
| 2550 | break; |
| 2551 | line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]; |
| 2552 | SOURCING_LNUM = cctx->ctx_ufunc->uf_script_ctx.sc_lnum |
| 2553 | + cctx->ctx_lnum + 1; |
| 2554 | } while (line == NULL); |
| 2555 | return line; |
| 2556 | } |
| 2557 | |
| 2558 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2559 | * parse a list: [expr, expr] |
| 2560 | * "*arg" points to the '['. |
| 2561 | */ |
| 2562 | static int |
| 2563 | compile_list(char_u **arg, cctx_T *cctx) |
| 2564 | { |
| 2565 | char_u *p = skipwhite(*arg + 1); |
| 2566 | int count = 0; |
| 2567 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame^] | 2568 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2569 | { |
| 2570 | if (*p == NUL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2571 | { |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame^] | 2572 | p = next_line_from_context(cctx); |
| 2573 | if (p == NULL) |
| 2574 | { |
| 2575 | semsg(_(e_list_end), *arg); |
| 2576 | return FAIL; |
| 2577 | } |
| 2578 | p = skipwhite(p); |
| 2579 | } |
| 2580 | if (*p == ']') |
| 2581 | { |
| 2582 | ++p; |
| 2583 | // Allow for following comment, after at least one space. |
| 2584 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '"') |
| 2585 | p += STRLEN(p); |
| 2586 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2587 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2588 | if (compile_expr1(&p, cctx) == FAIL) |
| 2589 | break; |
| 2590 | ++count; |
| 2591 | if (*p == ',') |
| 2592 | ++p; |
| 2593 | p = skipwhite(p); |
| 2594 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame^] | 2595 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2596 | |
| 2597 | generate_NEWLIST(cctx, count); |
| 2598 | return OK; |
| 2599 | } |
| 2600 | |
| 2601 | /* |
| 2602 | * parse a lambda: {arg, arg -> expr} |
| 2603 | * "*arg" points to the '{'. |
| 2604 | */ |
| 2605 | static int |
| 2606 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 2607 | { |
| 2608 | garray_T *instr = &cctx->ctx_instr; |
| 2609 | typval_T rettv; |
| 2610 | ufunc_T *ufunc; |
| 2611 | |
| 2612 | // Get the funcref in "rettv". |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2613 | if (get_lambda_tv(arg, &rettv, TRUE) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2614 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2615 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2616 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2617 | ++ufunc->uf_refcount; |
| 2618 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2619 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2620 | |
| 2621 | // The function will have one line: "return {expr}". |
| 2622 | // Compile it into instructions. |
| 2623 | compile_def_function(ufunc, TRUE); |
| 2624 | |
| 2625 | if (ufunc->uf_dfunc_idx >= 0) |
| 2626 | { |
| 2627 | if (ga_grow(instr, 1) == FAIL) |
| 2628 | return FAIL; |
| 2629 | generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
| 2630 | return OK; |
| 2631 | } |
| 2632 | return FAIL; |
| 2633 | } |
| 2634 | |
| 2635 | /* |
| 2636 | * Compile a lamda call: expr->{lambda}(args) |
| 2637 | * "arg" points to the "{". |
| 2638 | */ |
| 2639 | static int |
| 2640 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 2641 | { |
| 2642 | ufunc_T *ufunc; |
| 2643 | typval_T rettv; |
| 2644 | int argcount = 1; |
| 2645 | int ret = FAIL; |
| 2646 | |
| 2647 | // Get the funcref in "rettv". |
| 2648 | if (get_lambda_tv(arg, &rettv, TRUE) == FAIL) |
| 2649 | return FAIL; |
| 2650 | |
| 2651 | if (**arg != '(') |
| 2652 | { |
| 2653 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 2654 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2655 | else |
| 2656 | semsg(_(e_missing_paren), "lambda"); |
| 2657 | clear_tv(&rettv); |
| 2658 | return FAIL; |
| 2659 | } |
| 2660 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2661 | ufunc = rettv.vval.v_partial->pt_func; |
| 2662 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2663 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2664 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2665 | |
| 2666 | // The function will have one line: "return {expr}". |
| 2667 | // Compile it into instructions. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2668 | compile_def_function(ufunc, TRUE); |
| 2669 | |
| 2670 | // compile the arguments |
| 2671 | *arg = skipwhite(*arg + 1); |
| 2672 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 2673 | // call the compiled function |
| 2674 | ret = generate_CALL(cctx, ufunc, argcount); |
| 2675 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2676 | return ret; |
| 2677 | } |
| 2678 | |
| 2679 | /* |
| 2680 | * parse a dict: {'key': val} or #{key: val} |
| 2681 | * "*arg" points to the '{'. |
| 2682 | */ |
| 2683 | static int |
| 2684 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 2685 | { |
| 2686 | garray_T *instr = &cctx->ctx_instr; |
| 2687 | int count = 0; |
| 2688 | dict_T *d = dict_alloc(); |
| 2689 | dictitem_T *item; |
| 2690 | |
| 2691 | if (d == NULL) |
| 2692 | return FAIL; |
| 2693 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame^] | 2694 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2695 | { |
| 2696 | char_u *key = NULL; |
| 2697 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame^] | 2698 | if (**arg == NUL || (literal && **arg == '"')) |
| 2699 | { |
| 2700 | *arg = next_line_from_context(cctx); |
| 2701 | if (*arg == NULL) |
| 2702 | goto failret; |
| 2703 | *arg = skipwhite(*arg); |
| 2704 | } |
| 2705 | |
| 2706 | if (**arg == '}') |
| 2707 | break; |
| 2708 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2709 | if (literal) |
| 2710 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2711 | char_u *p = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2712 | |
| 2713 | if (p == *arg) |
| 2714 | { |
| 2715 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 2716 | return FAIL; |
| 2717 | } |
| 2718 | key = vim_strnsave(*arg, p - *arg); |
| 2719 | if (generate_PUSHS(cctx, key) == FAIL) |
| 2720 | return FAIL; |
| 2721 | *arg = p; |
| 2722 | } |
| 2723 | else |
| 2724 | { |
| 2725 | isn_T *isn; |
| 2726 | |
| 2727 | if (compile_expr1(arg, cctx) == FAIL) |
| 2728 | return FAIL; |
| 2729 | // TODO: check type is string |
| 2730 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 2731 | if (isn->isn_type == ISN_PUSHS) |
| 2732 | key = isn->isn_arg.string; |
| 2733 | } |
| 2734 | |
| 2735 | // Check for duplicate keys, if using string keys. |
| 2736 | if (key != NULL) |
| 2737 | { |
| 2738 | item = dict_find(d, key, -1); |
| 2739 | if (item != NULL) |
| 2740 | { |
| 2741 | semsg(_(e_duplicate_key), key); |
| 2742 | goto failret; |
| 2743 | } |
| 2744 | item = dictitem_alloc(key); |
| 2745 | if (item != NULL) |
| 2746 | { |
| 2747 | item->di_tv.v_type = VAR_UNKNOWN; |
| 2748 | item->di_tv.v_lock = 0; |
| 2749 | if (dict_add(d, item) == FAIL) |
| 2750 | dictitem_free(item); |
| 2751 | } |
| 2752 | } |
| 2753 | |
| 2754 | *arg = skipwhite(*arg); |
| 2755 | if (**arg != ':') |
| 2756 | { |
| 2757 | semsg(_(e_missing_dict_colon), *arg); |
| 2758 | return FAIL; |
| 2759 | } |
| 2760 | |
| 2761 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame^] | 2762 | if (**arg == NUL) |
| 2763 | { |
| 2764 | *arg = next_line_from_context(cctx); |
| 2765 | if (*arg == NULL) |
| 2766 | goto failret; |
| 2767 | *arg = skipwhite(*arg); |
| 2768 | } |
| 2769 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2770 | if (compile_expr1(arg, cctx) == FAIL) |
| 2771 | return FAIL; |
| 2772 | ++count; |
| 2773 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame^] | 2774 | if (**arg == NUL || *skipwhite(*arg) == '"') |
| 2775 | { |
| 2776 | *arg = next_line_from_context(cctx); |
| 2777 | if (*arg == NULL) |
| 2778 | goto failret; |
| 2779 | *arg = skipwhite(*arg); |
| 2780 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2781 | if (**arg == '}') |
| 2782 | break; |
| 2783 | if (**arg != ',') |
| 2784 | { |
| 2785 | semsg(_(e_missing_dict_comma), *arg); |
| 2786 | goto failret; |
| 2787 | } |
| 2788 | *arg = skipwhite(*arg + 1); |
| 2789 | } |
| 2790 | |
| 2791 | if (**arg != '}') |
| 2792 | { |
| 2793 | semsg(_(e_missing_dict_end), *arg); |
| 2794 | goto failret; |
| 2795 | } |
| 2796 | *arg = *arg + 1; |
| 2797 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame^] | 2798 | // Allow for following comment, after at least one space. |
| 2799 | if (VIM_ISWHITE(**arg) && *skipwhite(*arg) == '"') |
| 2800 | *arg += STRLEN(*arg); |
| 2801 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2802 | dict_unref(d); |
| 2803 | return generate_NEWDICT(cctx, count); |
| 2804 | |
| 2805 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame^] | 2806 | if (*arg == NULL) |
| 2807 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2808 | dict_unref(d); |
| 2809 | return FAIL; |
| 2810 | } |
| 2811 | |
| 2812 | /* |
| 2813 | * Compile "&option". |
| 2814 | */ |
| 2815 | static int |
| 2816 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 2817 | { |
| 2818 | typval_T rettv; |
| 2819 | char_u *start = *arg; |
| 2820 | int ret; |
| 2821 | |
| 2822 | // parse the option and get the current value to get the type. |
| 2823 | rettv.v_type = VAR_UNKNOWN; |
| 2824 | ret = get_option_tv(arg, &rettv, TRUE); |
| 2825 | if (ret == OK) |
| 2826 | { |
| 2827 | // include the '&' in the name, get_option_tv() expects it. |
| 2828 | char_u *name = vim_strnsave(start, *arg - start); |
| 2829 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 2830 | |
| 2831 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 2832 | vim_free(name); |
| 2833 | } |
| 2834 | clear_tv(&rettv); |
| 2835 | |
| 2836 | return ret; |
| 2837 | } |
| 2838 | |
| 2839 | /* |
| 2840 | * Compile "$VAR". |
| 2841 | */ |
| 2842 | static int |
| 2843 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 2844 | { |
| 2845 | char_u *start = *arg; |
| 2846 | int len; |
| 2847 | int ret; |
| 2848 | char_u *name; |
| 2849 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2850 | ++*arg; |
| 2851 | len = get_env_len(arg); |
| 2852 | if (len == 0) |
| 2853 | { |
| 2854 | semsg(_(e_syntax_at), start - 1); |
| 2855 | return FAIL; |
| 2856 | } |
| 2857 | |
| 2858 | // include the '$' in the name, get_env_tv() expects it. |
| 2859 | name = vim_strnsave(start, len + 1); |
| 2860 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 2861 | vim_free(name); |
| 2862 | return ret; |
| 2863 | } |
| 2864 | |
| 2865 | /* |
| 2866 | * Compile "@r". |
| 2867 | */ |
| 2868 | static int |
| 2869 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 2870 | { |
| 2871 | int ret; |
| 2872 | |
| 2873 | ++*arg; |
| 2874 | if (**arg == NUL) |
| 2875 | { |
| 2876 | semsg(_(e_syntax_at), *arg - 1); |
| 2877 | return FAIL; |
| 2878 | } |
| 2879 | if (!valid_yank_reg(**arg, TRUE)) |
| 2880 | { |
| 2881 | emsg_invreg(**arg); |
| 2882 | return FAIL; |
| 2883 | } |
| 2884 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 2885 | ++*arg; |
| 2886 | return ret; |
| 2887 | } |
| 2888 | |
| 2889 | /* |
| 2890 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 2891 | */ |
| 2892 | static int |
| 2893 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 2894 | { |
| 2895 | char_u *p = end; |
| 2896 | |
| 2897 | // this works from end to start |
| 2898 | while (p > start) |
| 2899 | { |
| 2900 | --p; |
| 2901 | if (*p == '-' || *p == '+') |
| 2902 | { |
| 2903 | // only '-' has an effect, for '+' we only check the type |
| 2904 | #ifdef FEAT_FLOAT |
| 2905 | if (rettv->v_type == VAR_FLOAT) |
| 2906 | { |
| 2907 | if (*p == '-') |
| 2908 | rettv->vval.v_float = -rettv->vval.v_float; |
| 2909 | } |
| 2910 | else |
| 2911 | #endif |
| 2912 | { |
| 2913 | varnumber_T val; |
| 2914 | int error = FALSE; |
| 2915 | |
| 2916 | // tv_get_number_chk() accepts a string, but we don't want that |
| 2917 | // here |
| 2918 | if (check_not_string(rettv) == FAIL) |
| 2919 | return FAIL; |
| 2920 | val = tv_get_number_chk(rettv, &error); |
| 2921 | clear_tv(rettv); |
| 2922 | if (error) |
| 2923 | return FAIL; |
| 2924 | if (*p == '-') |
| 2925 | val = -val; |
| 2926 | rettv->v_type = VAR_NUMBER; |
| 2927 | rettv->vval.v_number = val; |
| 2928 | } |
| 2929 | } |
| 2930 | else |
| 2931 | { |
| 2932 | int v = tv2bool(rettv); |
| 2933 | |
| 2934 | // '!' is permissive in the type. |
| 2935 | clear_tv(rettv); |
| 2936 | rettv->v_type = VAR_BOOL; |
| 2937 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 2938 | } |
| 2939 | } |
| 2940 | return OK; |
| 2941 | } |
| 2942 | |
| 2943 | /* |
| 2944 | * Recognize v: variables that are constants and set "rettv". |
| 2945 | */ |
| 2946 | static void |
| 2947 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 2948 | { |
| 2949 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 2950 | { |
| 2951 | rettv->v_type = VAR_BOOL; |
| 2952 | rettv->vval.v_number = VVAL_TRUE; |
| 2953 | *arg += 6; |
| 2954 | } |
| 2955 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 2956 | { |
| 2957 | rettv->v_type = VAR_BOOL; |
| 2958 | rettv->vval.v_number = VVAL_FALSE; |
| 2959 | *arg += 7; |
| 2960 | } |
| 2961 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 2962 | { |
| 2963 | rettv->v_type = VAR_SPECIAL; |
| 2964 | rettv->vval.v_number = VVAL_NULL; |
| 2965 | *arg += 6; |
| 2966 | } |
| 2967 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 2968 | { |
| 2969 | rettv->v_type = VAR_SPECIAL; |
| 2970 | rettv->vval.v_number = VVAL_NONE; |
| 2971 | *arg += 6; |
| 2972 | } |
| 2973 | } |
| 2974 | |
| 2975 | /* |
| 2976 | * Compile code to apply '-', '+' and '!'. |
| 2977 | */ |
| 2978 | static int |
| 2979 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 2980 | { |
| 2981 | char_u *p = end; |
| 2982 | |
| 2983 | // this works from end to start |
| 2984 | while (p > start) |
| 2985 | { |
| 2986 | --p; |
| 2987 | if (*p == '-' || *p == '+') |
| 2988 | { |
| 2989 | int negate = *p == '-'; |
| 2990 | isn_T *isn; |
| 2991 | |
| 2992 | // TODO: check type |
| 2993 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 2994 | { |
| 2995 | --p; |
| 2996 | if (*p == '-') |
| 2997 | negate = !negate; |
| 2998 | } |
| 2999 | // only '-' has an effect, for '+' we only check the type |
| 3000 | if (negate) |
| 3001 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3002 | else |
| 3003 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3004 | if (isn == NULL) |
| 3005 | return FAIL; |
| 3006 | } |
| 3007 | else |
| 3008 | { |
| 3009 | int invert = TRUE; |
| 3010 | |
| 3011 | while (p > start && p[-1] == '!') |
| 3012 | { |
| 3013 | --p; |
| 3014 | invert = !invert; |
| 3015 | } |
| 3016 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3017 | return FAIL; |
| 3018 | } |
| 3019 | } |
| 3020 | return OK; |
| 3021 | } |
| 3022 | |
| 3023 | /* |
| 3024 | * Compile whatever comes after "name" or "name()". |
| 3025 | */ |
| 3026 | static int |
| 3027 | compile_subscript( |
| 3028 | char_u **arg, |
| 3029 | cctx_T *cctx, |
| 3030 | char_u **start_leader, |
| 3031 | char_u *end_leader) |
| 3032 | { |
| 3033 | for (;;) |
| 3034 | { |
| 3035 | if (**arg == '(') |
| 3036 | { |
| 3037 | int argcount = 0; |
| 3038 | |
| 3039 | // funcref(arg) |
| 3040 | *arg = skipwhite(*arg + 1); |
| 3041 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3042 | return FAIL; |
| 3043 | if (generate_PCALL(cctx, argcount, TRUE) == FAIL) |
| 3044 | return FAIL; |
| 3045 | } |
| 3046 | else if (**arg == '-' && (*arg)[1] == '>') |
| 3047 | { |
| 3048 | char_u *p; |
| 3049 | |
| 3050 | // something->method() |
| 3051 | // Apply the '!', '-' and '+' first: |
| 3052 | // -1.0->func() works like (-1.0)->func() |
| 3053 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3054 | return FAIL; |
| 3055 | *start_leader = end_leader; // don't apply again later |
| 3056 | |
| 3057 | *arg = skipwhite(*arg + 2); |
| 3058 | if (**arg == '{') |
| 3059 | { |
| 3060 | // lambda call: list->{lambda} |
| 3061 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3062 | return FAIL; |
| 3063 | } |
| 3064 | else |
| 3065 | { |
| 3066 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3067 | p = *arg; |
| 3068 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3069 | p += 2; |
| 3070 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3071 | ; |
| 3072 | if (*p != '(') |
| 3073 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3074 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3075 | return FAIL; |
| 3076 | } |
| 3077 | // TODO: base value may not be the first argument |
| 3078 | if (compile_call(arg, p - *arg, cctx, 1) == FAIL) |
| 3079 | return FAIL; |
| 3080 | } |
| 3081 | } |
| 3082 | else if (**arg == '[') |
| 3083 | { |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3084 | garray_T *stack; |
| 3085 | type_T **typep; |
| 3086 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3087 | // list index: list[123] |
| 3088 | // TODO: more arguments |
| 3089 | // TODO: dict member dict['name'] |
| 3090 | *arg = skipwhite(*arg + 1); |
| 3091 | if (compile_expr1(arg, cctx) == FAIL) |
| 3092 | return FAIL; |
| 3093 | |
| 3094 | if (**arg != ']') |
| 3095 | { |
| 3096 | emsg(_(e_missbrac)); |
| 3097 | return FAIL; |
| 3098 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3099 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3100 | |
| 3101 | if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL) |
| 3102 | return FAIL; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3103 | stack = &cctx->ctx_type_stack; |
| 3104 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 1; |
| 3105 | if ((*typep)->tt_type != VAR_LIST && *typep != &t_any) |
| 3106 | { |
| 3107 | emsg(_(e_listreq)); |
| 3108 | return FAIL; |
| 3109 | } |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 3110 | if ((*typep)->tt_type == VAR_LIST) |
| 3111 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3112 | } |
| 3113 | else if (**arg == '.' && (*arg)[1] != '.') |
| 3114 | { |
| 3115 | char_u *p; |
| 3116 | |
| 3117 | ++*arg; |
| 3118 | p = *arg; |
| 3119 | // dictionary member: dict.name |
| 3120 | if (eval_isnamec1(*p)) |
| 3121 | while (eval_isnamec(*p)) |
| 3122 | MB_PTR_ADV(p); |
| 3123 | if (p == *arg) |
| 3124 | { |
| 3125 | semsg(_(e_syntax_at), *arg); |
| 3126 | return FAIL; |
| 3127 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3128 | if (generate_MEMBER(cctx, *arg, p - *arg) == FAIL) |
| 3129 | return FAIL; |
| 3130 | *arg = p; |
| 3131 | } |
| 3132 | else |
| 3133 | break; |
| 3134 | } |
| 3135 | |
| 3136 | // TODO - see handle_subscript(): |
| 3137 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3138 | // Don't do this when "Func" is already a partial that was bound |
| 3139 | // explicitly (pt_auto is FALSE). |
| 3140 | |
| 3141 | return OK; |
| 3142 | } |
| 3143 | |
| 3144 | /* |
| 3145 | * Compile an expression at "*p" and add instructions to "instr". |
| 3146 | * "p" is advanced until after the expression, skipping white space. |
| 3147 | * |
| 3148 | * This is the equivalent of eval1(), eval2(), etc. |
| 3149 | */ |
| 3150 | |
| 3151 | /* |
| 3152 | * number number constant |
| 3153 | * 0zFFFFFFFF Blob constant |
| 3154 | * "string" string constant |
| 3155 | * 'string' literal string constant |
| 3156 | * &option-name option value |
| 3157 | * @r register contents |
| 3158 | * identifier variable value |
| 3159 | * function() function call |
| 3160 | * $VAR environment variable |
| 3161 | * (expression) nested expression |
| 3162 | * [expr, expr] List |
| 3163 | * {key: val, key: val} Dictionary |
| 3164 | * #{key: val, key: val} Dictionary with literal keys |
| 3165 | * |
| 3166 | * Also handle: |
| 3167 | * ! in front logical NOT |
| 3168 | * - in front unary minus |
| 3169 | * + in front unary plus (ignored) |
| 3170 | * trailing (arg) funcref/partial call |
| 3171 | * trailing [] subscript in String or List |
| 3172 | * trailing .name entry in Dictionary |
| 3173 | * trailing ->name() method call |
| 3174 | */ |
| 3175 | static int |
| 3176 | compile_expr7(char_u **arg, cctx_T *cctx) |
| 3177 | { |
| 3178 | typval_T rettv; |
| 3179 | char_u *start_leader, *end_leader; |
| 3180 | int ret = OK; |
| 3181 | |
| 3182 | /* |
| 3183 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3184 | */ |
| 3185 | start_leader = *arg; |
| 3186 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3187 | *arg = skipwhite(*arg + 1); |
| 3188 | end_leader = *arg; |
| 3189 | |
| 3190 | rettv.v_type = VAR_UNKNOWN; |
| 3191 | switch (**arg) |
| 3192 | { |
| 3193 | /* |
| 3194 | * Number constant. |
| 3195 | */ |
| 3196 | case '0': // also for blob starting with 0z |
| 3197 | case '1': |
| 3198 | case '2': |
| 3199 | case '3': |
| 3200 | case '4': |
| 3201 | case '5': |
| 3202 | case '6': |
| 3203 | case '7': |
| 3204 | case '8': |
| 3205 | case '9': |
| 3206 | case '.': if (get_number_tv(arg, &rettv, TRUE, FALSE) == FAIL) |
| 3207 | return FAIL; |
| 3208 | break; |
| 3209 | |
| 3210 | /* |
| 3211 | * String constant: "string". |
| 3212 | */ |
| 3213 | case '"': if (get_string_tv(arg, &rettv, TRUE) == FAIL) |
| 3214 | return FAIL; |
| 3215 | break; |
| 3216 | |
| 3217 | /* |
| 3218 | * Literal string constant: 'str''ing'. |
| 3219 | */ |
| 3220 | case '\'': if (get_lit_string_tv(arg, &rettv, TRUE) == FAIL) |
| 3221 | return FAIL; |
| 3222 | break; |
| 3223 | |
| 3224 | /* |
| 3225 | * Constant Vim variable. |
| 3226 | */ |
| 3227 | case 'v': get_vim_constant(arg, &rettv); |
| 3228 | ret = NOTDONE; |
| 3229 | break; |
| 3230 | |
| 3231 | /* |
| 3232 | * List: [expr, expr] |
| 3233 | */ |
| 3234 | case '[': ret = compile_list(arg, cctx); |
| 3235 | break; |
| 3236 | |
| 3237 | /* |
| 3238 | * Dictionary: #{key: val, key: val} |
| 3239 | */ |
| 3240 | case '#': if ((*arg)[1] == '{') |
| 3241 | { |
| 3242 | ++*arg; |
| 3243 | ret = compile_dict(arg, cctx, TRUE); |
| 3244 | } |
| 3245 | else |
| 3246 | ret = NOTDONE; |
| 3247 | break; |
| 3248 | |
| 3249 | /* |
| 3250 | * Lambda: {arg, arg -> expr} |
| 3251 | * Dictionary: {'key': val, 'key': val} |
| 3252 | */ |
| 3253 | case '{': { |
| 3254 | char_u *start = skipwhite(*arg + 1); |
| 3255 | |
| 3256 | // Find out what comes after the arguments. |
| 3257 | ret = get_function_args(&start, '-', NULL, |
| 3258 | NULL, NULL, NULL, TRUE); |
| 3259 | if (ret != FAIL && *start == '>') |
| 3260 | ret = compile_lambda(arg, cctx); |
| 3261 | else |
| 3262 | ret = compile_dict(arg, cctx, FALSE); |
| 3263 | } |
| 3264 | break; |
| 3265 | |
| 3266 | /* |
| 3267 | * Option value: &name |
| 3268 | */ |
| 3269 | case '&': ret = compile_get_option(arg, cctx); |
| 3270 | break; |
| 3271 | |
| 3272 | /* |
| 3273 | * Environment variable: $VAR. |
| 3274 | */ |
| 3275 | case '$': ret = compile_get_env(arg, cctx); |
| 3276 | break; |
| 3277 | |
| 3278 | /* |
| 3279 | * Register contents: @r. |
| 3280 | */ |
| 3281 | case '@': ret = compile_get_register(arg, cctx); |
| 3282 | break; |
| 3283 | /* |
| 3284 | * nested expression: (expression). |
| 3285 | */ |
| 3286 | case '(': *arg = skipwhite(*arg + 1); |
| 3287 | ret = compile_expr1(arg, cctx); // recursive! |
| 3288 | *arg = skipwhite(*arg); |
| 3289 | if (**arg == ')') |
| 3290 | ++*arg; |
| 3291 | else if (ret == OK) |
| 3292 | { |
| 3293 | emsg(_(e_missing_close)); |
| 3294 | ret = FAIL; |
| 3295 | } |
| 3296 | break; |
| 3297 | |
| 3298 | default: ret = NOTDONE; |
| 3299 | break; |
| 3300 | } |
| 3301 | if (ret == FAIL) |
| 3302 | return FAIL; |
| 3303 | |
| 3304 | if (rettv.v_type != VAR_UNKNOWN) |
| 3305 | { |
| 3306 | // apply the '!', '-' and '+' before the constant |
| 3307 | if (apply_leader(&rettv, start_leader, end_leader) == FAIL) |
| 3308 | { |
| 3309 | clear_tv(&rettv); |
| 3310 | return FAIL; |
| 3311 | } |
| 3312 | start_leader = end_leader; // don't apply again below |
| 3313 | |
| 3314 | // push constant |
| 3315 | switch (rettv.v_type) |
| 3316 | { |
| 3317 | case VAR_BOOL: |
| 3318 | generate_PUSHBOOL(cctx, rettv.vval.v_number); |
| 3319 | break; |
| 3320 | case VAR_SPECIAL: |
| 3321 | generate_PUSHSPEC(cctx, rettv.vval.v_number); |
| 3322 | break; |
| 3323 | case VAR_NUMBER: |
| 3324 | generate_PUSHNR(cctx, rettv.vval.v_number); |
| 3325 | break; |
| 3326 | #ifdef FEAT_FLOAT |
| 3327 | case VAR_FLOAT: |
| 3328 | generate_PUSHF(cctx, rettv.vval.v_float); |
| 3329 | break; |
| 3330 | #endif |
| 3331 | case VAR_BLOB: |
| 3332 | generate_PUSHBLOB(cctx, rettv.vval.v_blob); |
| 3333 | rettv.vval.v_blob = NULL; |
| 3334 | break; |
| 3335 | case VAR_STRING: |
| 3336 | generate_PUSHS(cctx, rettv.vval.v_string); |
| 3337 | rettv.vval.v_string = NULL; |
| 3338 | break; |
| 3339 | default: |
| 3340 | iemsg("constant type missing"); |
| 3341 | return FAIL; |
| 3342 | } |
| 3343 | } |
| 3344 | else if (ret == NOTDONE) |
| 3345 | { |
| 3346 | char_u *p; |
| 3347 | int r; |
| 3348 | |
| 3349 | if (!eval_isnamec1(**arg)) |
| 3350 | { |
| 3351 | semsg(_("E1015: Name expected: %s"), *arg); |
| 3352 | return FAIL; |
| 3353 | } |
| 3354 | |
| 3355 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3356 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3357 | if (*p == '(') |
| 3358 | r = compile_call(arg, p - *arg, cctx, 0); |
| 3359 | else |
| 3360 | r = compile_load(arg, p, cctx, TRUE); |
| 3361 | if (r == FAIL) |
| 3362 | return FAIL; |
| 3363 | } |
| 3364 | |
| 3365 | if (compile_subscript(arg, cctx, &start_leader, end_leader) == FAIL) |
| 3366 | return FAIL; |
| 3367 | |
| 3368 | // Now deal with prefixed '-', '+' and '!', if not done already. |
| 3369 | return compile_leader(cctx, start_leader, end_leader); |
| 3370 | } |
| 3371 | |
| 3372 | /* |
| 3373 | * * number multiplication |
| 3374 | * / number division |
| 3375 | * % number modulo |
| 3376 | */ |
| 3377 | static int |
| 3378 | compile_expr6(char_u **arg, cctx_T *cctx) |
| 3379 | { |
| 3380 | char_u *op; |
| 3381 | |
| 3382 | // get the first variable |
| 3383 | if (compile_expr7(arg, cctx) == FAIL) |
| 3384 | return FAIL; |
| 3385 | |
| 3386 | /* |
| 3387 | * Repeat computing, until no "*", "/" or "%" is following. |
| 3388 | */ |
| 3389 | for (;;) |
| 3390 | { |
| 3391 | op = skipwhite(*arg); |
| 3392 | if (*op != '*' && *op != '/' && *op != '%') |
| 3393 | break; |
| 3394 | if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[1])) |
| 3395 | { |
| 3396 | char_u buf[3]; |
| 3397 | |
| 3398 | vim_strncpy(buf, op, 1); |
| 3399 | semsg(_(e_white_both), buf); |
| 3400 | } |
| 3401 | *arg = skipwhite(op + 1); |
| 3402 | |
| 3403 | // get the second variable |
| 3404 | if (compile_expr7(arg, cctx) == FAIL) |
| 3405 | return FAIL; |
| 3406 | |
| 3407 | generate_two_op(cctx, op); |
| 3408 | } |
| 3409 | |
| 3410 | return OK; |
| 3411 | } |
| 3412 | |
| 3413 | /* |
| 3414 | * + number addition |
| 3415 | * - number subtraction |
| 3416 | * .. string concatenation |
| 3417 | */ |
| 3418 | static int |
| 3419 | compile_expr5(char_u **arg, cctx_T *cctx) |
| 3420 | { |
| 3421 | char_u *op; |
| 3422 | int oplen; |
| 3423 | |
| 3424 | // get the first variable |
| 3425 | if (compile_expr6(arg, cctx) == FAIL) |
| 3426 | return FAIL; |
| 3427 | |
| 3428 | /* |
| 3429 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3430 | */ |
| 3431 | for (;;) |
| 3432 | { |
| 3433 | op = skipwhite(*arg); |
| 3434 | if (*op != '+' && *op != '-' && !(*op == '.' && (*(*arg + 1) == '.'))) |
| 3435 | break; |
| 3436 | oplen = (*op == '.' ? 2 : 1); |
| 3437 | |
| 3438 | if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(op[oplen])) |
| 3439 | { |
| 3440 | char_u buf[3]; |
| 3441 | |
| 3442 | vim_strncpy(buf, op, oplen); |
| 3443 | semsg(_(e_white_both), buf); |
| 3444 | } |
| 3445 | |
| 3446 | *arg = skipwhite(op + oplen); |
| 3447 | |
| 3448 | // get the second variable |
| 3449 | if (compile_expr6(arg, cctx) == FAIL) |
| 3450 | return FAIL; |
| 3451 | |
| 3452 | if (*op == '.') |
| 3453 | { |
| 3454 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 3455 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 3456 | return FAIL; |
| 3457 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 3458 | } |
| 3459 | else |
| 3460 | generate_two_op(cctx, op); |
| 3461 | } |
| 3462 | |
| 3463 | return OK; |
| 3464 | } |
| 3465 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3466 | static exptype_T |
| 3467 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3468 | { |
| 3469 | exptype_T type = EXPR_UNKNOWN; |
| 3470 | int i; |
| 3471 | |
| 3472 | switch (p[0]) |
| 3473 | { |
| 3474 | case '=': if (p[1] == '=') |
| 3475 | type = EXPR_EQUAL; |
| 3476 | else if (p[1] == '~') |
| 3477 | type = EXPR_MATCH; |
| 3478 | break; |
| 3479 | case '!': if (p[1] == '=') |
| 3480 | type = EXPR_NEQUAL; |
| 3481 | else if (p[1] == '~') |
| 3482 | type = EXPR_NOMATCH; |
| 3483 | break; |
| 3484 | case '>': if (p[1] != '=') |
| 3485 | { |
| 3486 | type = EXPR_GREATER; |
| 3487 | *len = 1; |
| 3488 | } |
| 3489 | else |
| 3490 | type = EXPR_GEQUAL; |
| 3491 | break; |
| 3492 | case '<': if (p[1] != '=') |
| 3493 | { |
| 3494 | type = EXPR_SMALLER; |
| 3495 | *len = 1; |
| 3496 | } |
| 3497 | else |
| 3498 | type = EXPR_SEQUAL; |
| 3499 | break; |
| 3500 | case 'i': if (p[1] == 's') |
| 3501 | { |
| 3502 | // "is" and "isnot"; but not a prefix of a name |
| 3503 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3504 | *len = 5; |
| 3505 | i = p[*len]; |
| 3506 | if (!isalnum(i) && i != '_') |
| 3507 | { |
| 3508 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3509 | *type_is = TRUE; |
| 3510 | } |
| 3511 | } |
| 3512 | break; |
| 3513 | } |
| 3514 | return type; |
| 3515 | } |
| 3516 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3517 | /* |
| 3518 | * expr5a == expr5b |
| 3519 | * expr5a =~ expr5b |
| 3520 | * expr5a != expr5b |
| 3521 | * expr5a !~ expr5b |
| 3522 | * expr5a > expr5b |
| 3523 | * expr5a >= expr5b |
| 3524 | * expr5a < expr5b |
| 3525 | * expr5a <= expr5b |
| 3526 | * expr5a is expr5b |
| 3527 | * expr5a isnot expr5b |
| 3528 | * |
| 3529 | * Produces instructions: |
| 3530 | * EVAL expr5a Push result of "expr5a" |
| 3531 | * EVAL expr5b Push result of "expr5b" |
| 3532 | * COMPARE one of the compare instructions |
| 3533 | */ |
| 3534 | static int |
| 3535 | compile_expr4(char_u **arg, cctx_T *cctx) |
| 3536 | { |
| 3537 | exptype_T type = EXPR_UNKNOWN; |
| 3538 | char_u *p; |
| 3539 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3540 | int type_is = FALSE; |
| 3541 | |
| 3542 | // get the first variable |
| 3543 | if (compile_expr5(arg, cctx) == FAIL) |
| 3544 | return FAIL; |
| 3545 | |
| 3546 | p = skipwhite(*arg); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3547 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3548 | |
| 3549 | /* |
| 3550 | * If there is a comparative operator, use it. |
| 3551 | */ |
| 3552 | if (type != EXPR_UNKNOWN) |
| 3553 | { |
| 3554 | int ic = FALSE; // Default: do not ignore case |
| 3555 | |
| 3556 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 3557 | { |
| 3558 | semsg(_(e_invexpr2), *arg); |
| 3559 | return FAIL; |
| 3560 | } |
| 3561 | // extra question mark appended: ignore case |
| 3562 | if (p[len] == '?') |
| 3563 | { |
| 3564 | ic = TRUE; |
| 3565 | ++len; |
| 3566 | } |
| 3567 | // extra '#' appended: match case (ignored) |
| 3568 | else if (p[len] == '#') |
| 3569 | ++len; |
| 3570 | // nothing appended: match case |
| 3571 | |
| 3572 | if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[len])) |
| 3573 | { |
| 3574 | char_u buf[7]; |
| 3575 | |
| 3576 | vim_strncpy(buf, p, len); |
| 3577 | semsg(_(e_white_both), buf); |
| 3578 | } |
| 3579 | |
| 3580 | // get the second variable |
| 3581 | *arg = skipwhite(p + len); |
| 3582 | if (compile_expr5(arg, cctx) == FAIL) |
| 3583 | return FAIL; |
| 3584 | |
| 3585 | generate_COMPARE(cctx, type, ic); |
| 3586 | } |
| 3587 | |
| 3588 | return OK; |
| 3589 | } |
| 3590 | |
| 3591 | /* |
| 3592 | * Compile || or &&. |
| 3593 | */ |
| 3594 | static int |
| 3595 | compile_and_or(char_u **arg, cctx_T *cctx, char *op) |
| 3596 | { |
| 3597 | char_u *p = skipwhite(*arg); |
| 3598 | int opchar = *op; |
| 3599 | |
| 3600 | if (p[0] == opchar && p[1] == opchar) |
| 3601 | { |
| 3602 | garray_T *instr = &cctx->ctx_instr; |
| 3603 | garray_T end_ga; |
| 3604 | |
| 3605 | /* |
| 3606 | * Repeat until there is no following "||" or "&&" |
| 3607 | */ |
| 3608 | ga_init2(&end_ga, sizeof(int), 10); |
| 3609 | while (p[0] == opchar && p[1] == opchar) |
| 3610 | { |
| 3611 | if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2])) |
| 3612 | semsg(_(e_white_both), op); |
| 3613 | |
| 3614 | if (ga_grow(&end_ga, 1) == FAIL) |
| 3615 | { |
| 3616 | ga_clear(&end_ga); |
| 3617 | return FAIL; |
| 3618 | } |
| 3619 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 3620 | ++end_ga.ga_len; |
| 3621 | generate_JUMP(cctx, opchar == '|' |
| 3622 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 3623 | |
| 3624 | // eval the next expression |
| 3625 | *arg = skipwhite(p + 2); |
| 3626 | if ((opchar == '|' ? compile_expr3(arg, cctx) |
| 3627 | : compile_expr4(arg, cctx)) == FAIL) |
| 3628 | { |
| 3629 | ga_clear(&end_ga); |
| 3630 | return FAIL; |
| 3631 | } |
| 3632 | p = skipwhite(*arg); |
| 3633 | } |
| 3634 | |
| 3635 | // Fill in the end label in all jumps. |
| 3636 | while (end_ga.ga_len > 0) |
| 3637 | { |
| 3638 | isn_T *isn; |
| 3639 | |
| 3640 | --end_ga.ga_len; |
| 3641 | isn = ((isn_T *)instr->ga_data) |
| 3642 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 3643 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3644 | } |
| 3645 | ga_clear(&end_ga); |
| 3646 | } |
| 3647 | |
| 3648 | return OK; |
| 3649 | } |
| 3650 | |
| 3651 | /* |
| 3652 | * expr4a && expr4a && expr4a logical AND |
| 3653 | * |
| 3654 | * Produces instructions: |
| 3655 | * EVAL expr4a Push result of "expr4a" |
| 3656 | * JUMP_AND_KEEP_IF_FALSE end |
| 3657 | * EVAL expr4b Push result of "expr4b" |
| 3658 | * JUMP_AND_KEEP_IF_FALSE end |
| 3659 | * EVAL expr4c Push result of "expr4c" |
| 3660 | * end: |
| 3661 | */ |
| 3662 | static int |
| 3663 | compile_expr3(char_u **arg, cctx_T *cctx) |
| 3664 | { |
| 3665 | // get the first variable |
| 3666 | if (compile_expr4(arg, cctx) == FAIL) |
| 3667 | return FAIL; |
| 3668 | |
| 3669 | // || and && work almost the same |
| 3670 | return compile_and_or(arg, cctx, "&&"); |
| 3671 | } |
| 3672 | |
| 3673 | /* |
| 3674 | * expr3a || expr3b || expr3c logical OR |
| 3675 | * |
| 3676 | * Produces instructions: |
| 3677 | * EVAL expr3a Push result of "expr3a" |
| 3678 | * JUMP_AND_KEEP_IF_TRUE end |
| 3679 | * EVAL expr3b Push result of "expr3b" |
| 3680 | * JUMP_AND_KEEP_IF_TRUE end |
| 3681 | * EVAL expr3c Push result of "expr3c" |
| 3682 | * end: |
| 3683 | */ |
| 3684 | static int |
| 3685 | compile_expr2(char_u **arg, cctx_T *cctx) |
| 3686 | { |
| 3687 | // eval the first expression |
| 3688 | if (compile_expr3(arg, cctx) == FAIL) |
| 3689 | return FAIL; |
| 3690 | |
| 3691 | // || and && work almost the same |
| 3692 | return compile_and_or(arg, cctx, "||"); |
| 3693 | } |
| 3694 | |
| 3695 | /* |
| 3696 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 3697 | * |
| 3698 | * Produces instructions: |
| 3699 | * EVAL expr2 Push result of "expr" |
| 3700 | * JUMP_IF_FALSE alt jump if false |
| 3701 | * EVAL expr1a |
| 3702 | * JUMP_ALWAYS end |
| 3703 | * alt: EVAL expr1b |
| 3704 | * end: |
| 3705 | */ |
| 3706 | static int |
| 3707 | compile_expr1(char_u **arg, cctx_T *cctx) |
| 3708 | { |
| 3709 | char_u *p; |
| 3710 | |
| 3711 | // evaluate the first expression |
| 3712 | if (compile_expr2(arg, cctx) == FAIL) |
| 3713 | return FAIL; |
| 3714 | |
| 3715 | p = skipwhite(*arg); |
| 3716 | if (*p == '?') |
| 3717 | { |
| 3718 | garray_T *instr = &cctx->ctx_instr; |
| 3719 | garray_T *stack = &cctx->ctx_type_stack; |
| 3720 | int alt_idx = instr->ga_len; |
| 3721 | int end_idx; |
| 3722 | isn_T *isn; |
| 3723 | type_T *type1; |
| 3724 | type_T *type2; |
| 3725 | |
| 3726 | if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1])) |
| 3727 | semsg(_(e_white_both), "?"); |
| 3728 | |
| 3729 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 3730 | |
| 3731 | // evaluate the second expression; any type is accepted |
| 3732 | *arg = skipwhite(p + 1); |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 3733 | if (compile_expr1(arg, cctx) == FAIL) |
| 3734 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3735 | |
| 3736 | // remember the type and drop it |
| 3737 | --stack->ga_len; |
| 3738 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 3739 | |
| 3740 | end_idx = instr->ga_len; |
| 3741 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 3742 | |
| 3743 | // jump here from JUMP_IF_FALSE |
| 3744 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 3745 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3746 | |
| 3747 | // Check for the ":". |
| 3748 | p = skipwhite(*arg); |
| 3749 | if (*p != ':') |
| 3750 | { |
| 3751 | emsg(_(e_missing_colon)); |
| 3752 | return FAIL; |
| 3753 | } |
| 3754 | if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1])) |
| 3755 | semsg(_(e_white_both), ":"); |
| 3756 | |
| 3757 | // evaluate the third expression |
| 3758 | *arg = skipwhite(p + 1); |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 3759 | if (compile_expr1(arg, cctx) == FAIL) |
| 3760 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3761 | |
| 3762 | // If the types differ, the result has a more generic type. |
| 3763 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 3764 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3765 | |
| 3766 | // jump here from JUMP_ALWAYS |
| 3767 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 3768 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3769 | } |
| 3770 | return OK; |
| 3771 | } |
| 3772 | |
| 3773 | /* |
| 3774 | * compile "return [expr]" |
| 3775 | */ |
| 3776 | static char_u * |
| 3777 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 3778 | { |
| 3779 | char_u *p = arg; |
| 3780 | garray_T *stack = &cctx->ctx_type_stack; |
| 3781 | type_T *stack_type; |
| 3782 | |
| 3783 | if (*p != NUL && *p != '|' && *p != '\n') |
| 3784 | { |
| 3785 | // compile return argument into instructions |
| 3786 | if (compile_expr1(&p, cctx) == FAIL) |
| 3787 | return NULL; |
| 3788 | |
| 3789 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3790 | if (set_return_type) |
| 3791 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
| 3792 | else if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx) |
| 3793 | == FAIL) |
| 3794 | return NULL; |
| 3795 | } |
| 3796 | else |
| 3797 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 3798 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 3799 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 3800 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 3801 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3802 | { |
| 3803 | emsg(_("E1003: Missing return value")); |
| 3804 | return NULL; |
| 3805 | } |
| 3806 | |
| 3807 | // No argument, return zero. |
| 3808 | generate_PUSHNR(cctx, 0); |
| 3809 | } |
| 3810 | |
| 3811 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 3812 | return NULL; |
| 3813 | |
| 3814 | // "return val | endif" is possible |
| 3815 | return skipwhite(p); |
| 3816 | } |
| 3817 | |
| 3818 | /* |
| 3819 | * Return the length of an assignment operator, or zero if there isn't one. |
| 3820 | */ |
| 3821 | int |
| 3822 | assignment_len(char_u *p, int *heredoc) |
| 3823 | { |
| 3824 | if (*p == '=') |
| 3825 | { |
| 3826 | if (p[1] == '<' && p[2] == '<') |
| 3827 | { |
| 3828 | *heredoc = TRUE; |
| 3829 | return 3; |
| 3830 | } |
| 3831 | return 1; |
| 3832 | } |
| 3833 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 3834 | return 2; |
| 3835 | if (STRNCMP(p, "..=", 3) == 0) |
| 3836 | return 3; |
| 3837 | return 0; |
| 3838 | } |
| 3839 | |
| 3840 | // words that cannot be used as a variable |
| 3841 | static char *reserved[] = { |
| 3842 | "true", |
| 3843 | "false", |
| 3844 | NULL |
| 3845 | }; |
| 3846 | |
| 3847 | /* |
| 3848 | * Get a line for "=<<". |
| 3849 | * Return a pointer to the line in allocated memory. |
| 3850 | * Return NULL for end-of-file or some error. |
| 3851 | */ |
| 3852 | static char_u * |
| 3853 | heredoc_getline( |
| 3854 | int c UNUSED, |
| 3855 | void *cookie, |
| 3856 | int indent UNUSED, |
| 3857 | int do_concat UNUSED) |
| 3858 | { |
| 3859 | cctx_T *cctx = (cctx_T *)cookie; |
| 3860 | |
| 3861 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 3862 | { |
| 3863 | iemsg("Heredoc got to end"); |
| 3864 | return NULL; |
| 3865 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3866 | ++cctx->ctx_lnum; |
| 3867 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 3868 | [cctx->ctx_lnum]); |
| 3869 | } |
| 3870 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 3871 | typedef enum { |
| 3872 | dest_local, |
| 3873 | dest_option, |
| 3874 | dest_env, |
| 3875 | dest_global, |
| 3876 | dest_vimvar, |
| 3877 | dest_script, |
| 3878 | dest_reg, |
| 3879 | } assign_dest_T; |
| 3880 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3881 | /* |
| 3882 | * compile "let var [= expr]", "const var = expr" and "var = expr" |
| 3883 | * "arg" points to "var". |
| 3884 | */ |
| 3885 | static char_u * |
| 3886 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 3887 | { |
| 3888 | char_u *p; |
| 3889 | char_u *ret = NULL; |
| 3890 | int var_count = 0; |
| 3891 | int semicolon = 0; |
| 3892 | size_t varlen; |
| 3893 | garray_T *instr = &cctx->ctx_instr; |
| 3894 | int idx = -1; |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 3895 | int new_local = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3896 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3897 | int opt_type; |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 3898 | assign_dest_T dest = dest_local; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3899 | int opt_flags = 0; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 3900 | int vimvaridx = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3901 | int oplen = 0; |
| 3902 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 3903 | type_T *type = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3904 | lvar_T *lvar; |
| 3905 | char_u *name; |
| 3906 | char_u *sp; |
| 3907 | int has_type = FALSE; |
| 3908 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
| 3909 | int instr_count = -1; |
| 3910 | |
| 3911 | p = skip_var_list(arg, FALSE, &var_count, &semicolon); |
| 3912 | if (p == NULL) |
| 3913 | return NULL; |
| 3914 | if (var_count > 0) |
| 3915 | { |
| 3916 | // TODO: let [var, var] = list |
| 3917 | emsg("Cannot handle a list yet"); |
| 3918 | return NULL; |
| 3919 | } |
| 3920 | |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 3921 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 3922 | if (is_decl && p == arg + 2 && p[-1] == ':') |
| 3923 | --p; |
| 3924 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3925 | varlen = p - arg; |
| 3926 | name = vim_strnsave(arg, (int)varlen); |
| 3927 | if (name == NULL) |
| 3928 | return NULL; |
| 3929 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3930 | if (cctx->ctx_skip != TRUE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3931 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3932 | if (*arg == '&') |
| 3933 | { |
| 3934 | int cc; |
| 3935 | long numval; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3936 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3937 | dest = dest_option; |
| 3938 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3939 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3940 | emsg(_(e_const_option)); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 3941 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3942 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3943 | if (is_decl) |
| 3944 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3945 | semsg(_("E1052: Cannot declare an option: %s"), arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3946 | goto theend; |
| 3947 | } |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3948 | p = arg; |
| 3949 | p = find_option_end(&p, &opt_flags); |
| 3950 | if (p == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3951 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 3952 | // cannot happen? |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3953 | emsg(_(e_letunexp)); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 3954 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3955 | } |
| 3956 | cc = *p; |
| 3957 | *p = NUL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3958 | opt_type = get_option_value(arg + 1, &numval, NULL, opt_flags); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3959 | *p = cc; |
| 3960 | if (opt_type == -3) |
| 3961 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 3962 | semsg(_(e_unknown_option), arg); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 3963 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3964 | } |
| 3965 | if (opt_type == -2 || opt_type == 0) |
| 3966 | type = &t_string; |
| 3967 | else |
| 3968 | type = &t_number; // both number and boolean option |
| 3969 | } |
| 3970 | else if (*arg == '$') |
| 3971 | { |
| 3972 | dest = dest_env; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 3973 | type = &t_string; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3974 | if (is_decl) |
| 3975 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3976 | semsg(_("E1065: Cannot declare an environment variable: %s"), |
| 3977 | name); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3978 | goto theend; |
| 3979 | } |
| 3980 | } |
| 3981 | else if (*arg == '@') |
| 3982 | { |
| 3983 | if (!valid_yank_reg(arg[1], TRUE)) |
| 3984 | { |
| 3985 | emsg_invreg(arg[1]); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 3986 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3987 | } |
| 3988 | dest = dest_reg; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 3989 | type = &t_string; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3990 | if (is_decl) |
| 3991 | { |
| 3992 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 3993 | goto theend; |
| 3994 | } |
| 3995 | } |
| 3996 | else if (STRNCMP(arg, "g:", 2) == 0) |
| 3997 | { |
| 3998 | dest = dest_global; |
| 3999 | if (is_decl) |
| 4000 | { |
| 4001 | semsg(_("E1016: Cannot declare a global variable: %s"), name); |
| 4002 | goto theend; |
| 4003 | } |
| 4004 | } |
| 4005 | else if (STRNCMP(arg, "v:", 2) == 0) |
| 4006 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 4007 | typval_T *vtv; |
| 4008 | int di_flags; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4009 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 4010 | vimvaridx = find_vim_var(name + 2, &di_flags); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4011 | if (vimvaridx < 0) |
| 4012 | { |
| 4013 | semsg(_(e_var_notfound), arg); |
| 4014 | goto theend; |
| 4015 | } |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 4016 | // We use the current value of "sandbox" here, is that OK? |
| 4017 | if (var_check_ro(di_flags, name, FALSE)) |
| 4018 | goto theend; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4019 | dest = dest_vimvar; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4020 | vtv = get_vim_var_tv(vimvaridx); |
| 4021 | type = typval2type(vtv); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4022 | if (is_decl) |
| 4023 | { |
| 4024 | semsg(_("E1064: Cannot declare a v: variable: %s"), name); |
| 4025 | goto theend; |
| 4026 | } |
| 4027 | } |
| 4028 | else |
| 4029 | { |
| 4030 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 4031 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4032 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4033 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4034 | goto theend; |
| 4035 | } |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4036 | |
| 4037 | idx = lookup_local(arg, varlen, cctx); |
| 4038 | if (idx >= 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4039 | { |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4040 | if (is_decl) |
| 4041 | { |
| 4042 | semsg(_("E1017: Variable already declared: %s"), name); |
| 4043 | goto theend; |
| 4044 | } |
| 4045 | else |
| 4046 | { |
| 4047 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 4048 | if (lvar->lv_const) |
| 4049 | { |
| 4050 | semsg(_("E1018: Cannot assign to a constant: %s"), name); |
| 4051 | goto theend; |
| 4052 | } |
| 4053 | } |
| 4054 | } |
| 4055 | else if (STRNCMP(arg, "s:", 2) == 0 |
| 4056 | || lookup_script(arg, varlen) == OK |
| 4057 | || find_imported(arg, varlen, cctx) != NULL) |
| 4058 | { |
| 4059 | dest = dest_script; |
| 4060 | if (is_decl) |
| 4061 | { |
| 4062 | semsg(_("E1054: Variable already declared in the script: %s"), |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4063 | name); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4064 | goto theend; |
| 4065 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4066 | } |
| 4067 | } |
| 4068 | } |
| 4069 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4070 | if (dest != dest_option) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4071 | { |
| 4072 | if (is_decl && *p == ':') |
| 4073 | { |
| 4074 | // parse optional type: "let var: type = expr" |
| 4075 | p = skipwhite(p + 1); |
| 4076 | type = parse_type(&p, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4077 | has_type = TRUE; |
| 4078 | } |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4079 | else if (idx >= 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4080 | { |
| 4081 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 4082 | type = lvar->lv_type; |
| 4083 | } |
| 4084 | } |
| 4085 | |
| 4086 | sp = p; |
| 4087 | p = skipwhite(p); |
| 4088 | op = p; |
| 4089 | oplen = assignment_len(p, &heredoc); |
| 4090 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4091 | { |
| 4092 | char_u buf[4]; |
| 4093 | |
| 4094 | vim_strncpy(buf, op, oplen); |
| 4095 | semsg(_(e_white_both), buf); |
| 4096 | } |
| 4097 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4098 | if (oplen == 3 && !heredoc && dest != dest_global |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4099 | && type->tt_type != VAR_STRING && type->tt_type != VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4100 | { |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 4101 | emsg(_("E1019: Can only concatenate to string")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4102 | goto theend; |
| 4103 | } |
| 4104 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4105 | if (idx < 0 && dest == dest_local && cctx->ctx_skip != TRUE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4106 | { |
| 4107 | if (oplen > 1 && !heredoc) |
| 4108 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4109 | // +=, /=, etc. require an existing variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4110 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 4111 | name); |
| 4112 | goto theend; |
| 4113 | } |
| 4114 | |
| 4115 | // new local variable |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 4116 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4117 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4118 | idx = reserve_local(cctx, arg, varlen, cmdidx == CMD_const, type); |
| 4119 | if (idx < 0) |
| 4120 | goto theend; |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 4121 | new_local = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | if (heredoc) |
| 4125 | { |
| 4126 | list_T *l; |
| 4127 | listitem_T *li; |
| 4128 | |
| 4129 | // [let] varname =<< [trim] {end} |
| 4130 | eap->getline = heredoc_getline; |
| 4131 | eap->cookie = cctx; |
| 4132 | l = heredoc_get(eap, op + 3); |
| 4133 | |
| 4134 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 4135 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4136 | { |
| 4137 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4138 | li->li_tv.vval.v_string = NULL; |
| 4139 | } |
| 4140 | generate_NEWLIST(cctx, l->lv_len); |
| 4141 | type = &t_list_string; |
| 4142 | list_free(l); |
| 4143 | p += STRLEN(p); |
| 4144 | } |
| 4145 | else if (oplen > 0) |
| 4146 | { |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4147 | int r; |
| 4148 | type_T *stacktype; |
| 4149 | garray_T *stack; |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4150 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4151 | // for "+=", "*=", "..=" etc. first load the current value |
| 4152 | if (*op != '=') |
| 4153 | { |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4154 | switch (dest) |
| 4155 | { |
| 4156 | case dest_option: |
| 4157 | // TODO: check the option exists |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4158 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4159 | break; |
| 4160 | case dest_global: |
| 4161 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4162 | break; |
| 4163 | case dest_script: |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 4164 | compile_load_scriptvar(cctx, |
| 4165 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4166 | break; |
| 4167 | case dest_env: |
| 4168 | // Include $ in the name here |
| 4169 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4170 | break; |
| 4171 | case dest_reg: |
| 4172 | generate_LOAD(cctx, ISN_LOADREG, arg[1], NULL, &t_string); |
| 4173 | break; |
| 4174 | case dest_vimvar: |
| 4175 | generate_LOADV(cctx, name + 2, TRUE); |
| 4176 | break; |
| 4177 | case dest_local: |
| 4178 | generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
| 4179 | break; |
| 4180 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4181 | } |
| 4182 | |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4183 | // Compile the expression. Temporarily hide the new local variable |
| 4184 | // here, it is not available to this expression. |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 4185 | if (new_local) |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4186 | --cctx->ctx_locals.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4187 | instr_count = instr->ga_len; |
| 4188 | p = skipwhite(p + oplen); |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4189 | r = compile_expr1(&p, cctx); |
Bram Moolenaar | 01b3862 | 2020-03-30 21:28:39 +0200 | [diff] [blame] | 4190 | if (new_local) |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4191 | ++cctx->ctx_locals.ga_len; |
| 4192 | if (r == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4193 | goto theend; |
| 4194 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4195 | if (cctx->ctx_skip != TRUE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4196 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4197 | stack = &cctx->ctx_type_stack; |
| 4198 | stacktype = stack->ga_len == 0 ? &t_void |
| 4199 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4200 | if (idx >= 0 && (is_decl || !has_type)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4201 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4202 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 4203 | if (new_local && !has_type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4204 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4205 | if (stacktype->tt_type == VAR_VOID) |
| 4206 | { |
| 4207 | emsg(_("E1031: Cannot use void value")); |
| 4208 | goto theend; |
| 4209 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4210 | else |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4211 | { |
| 4212 | // An empty list or dict has a &t_void member, for a |
| 4213 | // variable that implies &t_any. |
| 4214 | if (stacktype == &t_list_empty) |
| 4215 | lvar->lv_type = &t_list_any; |
| 4216 | else if (stacktype == &t_dict_empty) |
| 4217 | lvar->lv_type = &t_dict_any; |
| 4218 | else |
| 4219 | lvar->lv_type = stacktype; |
| 4220 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4221 | } |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4222 | else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL) |
| 4223 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4224 | } |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4225 | else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4226 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4227 | } |
| 4228 | } |
| 4229 | else if (cmdidx == CMD_const) |
| 4230 | { |
| 4231 | emsg(_("E1021: const requires a value")); |
| 4232 | goto theend; |
| 4233 | } |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4234 | else if (!has_type || dest == dest_option) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4235 | { |
| 4236 | emsg(_("E1022: type or initialization required")); |
| 4237 | goto theend; |
| 4238 | } |
| 4239 | else |
| 4240 | { |
| 4241 | // variables are always initialized |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4242 | if (ga_grow(instr, 1) == FAIL) |
| 4243 | goto theend; |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4244 | switch (type->tt_type) |
| 4245 | { |
| 4246 | case VAR_BOOL: |
| 4247 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 4248 | break; |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4249 | case VAR_FLOAT: |
| 4250 | #ifdef FEAT_FLOAT |
| 4251 | generate_PUSHF(cctx, 0.0); |
| 4252 | #endif |
| 4253 | break; |
| 4254 | case VAR_STRING: |
| 4255 | generate_PUSHS(cctx, NULL); |
| 4256 | break; |
| 4257 | case VAR_BLOB: |
| 4258 | generate_PUSHBLOB(cctx, NULL); |
| 4259 | break; |
| 4260 | case VAR_FUNC: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4261 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4262 | break; |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4263 | case VAR_LIST: |
| 4264 | generate_NEWLIST(cctx, 0); |
| 4265 | break; |
| 4266 | case VAR_DICT: |
| 4267 | generate_NEWDICT(cctx, 0); |
| 4268 | break; |
| 4269 | case VAR_JOB: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 4270 | generate_PUSHJOB(cctx, NULL); |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4271 | break; |
| 4272 | case VAR_CHANNEL: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 4273 | generate_PUSHCHANNEL(cctx, NULL); |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4274 | break; |
| 4275 | case VAR_NUMBER: |
| 4276 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4277 | case VAR_ANY: |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 4278 | case VAR_PARTIAL: |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4279 | case VAR_VOID: |
Bram Moolenaar | e69f6d0 | 2020-04-01 22:11:01 +0200 | [diff] [blame] | 4280 | case VAR_SPECIAL: // cannot happen |
Bram Moolenaar | 04d0522 | 2020-02-06 22:06:54 +0100 | [diff] [blame] | 4281 | generate_PUSHNR(cctx, 0); |
| 4282 | break; |
| 4283 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4284 | } |
| 4285 | |
| 4286 | if (oplen > 0 && *op != '=') |
| 4287 | { |
| 4288 | type_T *expected = &t_number; |
| 4289 | garray_T *stack = &cctx->ctx_type_stack; |
| 4290 | type_T *stacktype; |
| 4291 | |
| 4292 | // TODO: if type is known use float or any operation |
| 4293 | |
| 4294 | if (*op == '.') |
| 4295 | expected = &t_string; |
| 4296 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4297 | if (need_type(stacktype, expected, -1, cctx) == FAIL) |
| 4298 | goto theend; |
| 4299 | |
| 4300 | if (*op == '.') |
| 4301 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4302 | else |
| 4303 | { |
| 4304 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 4305 | |
| 4306 | if (isn == NULL) |
| 4307 | goto theend; |
| 4308 | switch (*op) |
| 4309 | { |
| 4310 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 4311 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 4312 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 4313 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 4314 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 4315 | } |
| 4316 | } |
| 4317 | } |
| 4318 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4319 | switch (dest) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4320 | { |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4321 | case dest_option: |
| 4322 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 4323 | break; |
| 4324 | case dest_global: |
| 4325 | // include g: with the name, easier to execute that way |
| 4326 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 4327 | break; |
| 4328 | case dest_env: |
| 4329 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 4330 | break; |
| 4331 | case dest_reg: |
| 4332 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 4333 | break; |
| 4334 | case dest_vimvar: |
| 4335 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 4336 | break; |
| 4337 | case dest_script: |
| 4338 | { |
| 4339 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 4340 | imported_T *import = NULL; |
| 4341 | int sid = current_sctx.sc_sid; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 4342 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4343 | if (name[1] != ':') |
| 4344 | { |
| 4345 | import = find_imported(name, 0, cctx); |
| 4346 | if (import != NULL) |
| 4347 | sid = import->imp_sid; |
| 4348 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4349 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4350 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 4351 | // TODO: specific type |
| 4352 | if (idx < 0) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4353 | { |
| 4354 | char_u *name_s = name; |
| 4355 | |
| 4356 | // Include s: in the name for store_var() |
| 4357 | if (name[1] != ':') |
| 4358 | { |
| 4359 | int len = (int)STRLEN(name) + 3; |
| 4360 | |
| 4361 | name_s = alloc(len); |
| 4362 | if (name_s == NULL) |
| 4363 | name_s = name; |
| 4364 | else |
| 4365 | vim_snprintf((char *)name_s, len, "s:%s", name); |
| 4366 | } |
| 4367 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, &t_any); |
| 4368 | if (name_s != name) |
| 4369 | vim_free(name_s); |
| 4370 | } |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4371 | else |
| 4372 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
| 4373 | sid, idx, &t_any); |
| 4374 | } |
| 4375 | break; |
| 4376 | case dest_local: |
| 4377 | { |
| 4378 | isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4379 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4380 | // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE |
| 4381 | // into ISN_STORENR |
| 4382 | if (instr->ga_len == instr_count + 1 |
| 4383 | && isn->isn_type == ISN_PUSHNR) |
| 4384 | { |
| 4385 | varnumber_T val = isn->isn_arg.number; |
| 4386 | garray_T *stack = &cctx->ctx_type_stack; |
| 4387 | |
| 4388 | isn->isn_type = ISN_STORENR; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 4389 | isn->isn_arg.storenr.stnr_idx = idx; |
| 4390 | isn->isn_arg.storenr.stnr_val = val; |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4391 | if (stack->ga_len > 0) |
| 4392 | --stack->ga_len; |
| 4393 | } |
| 4394 | else |
| 4395 | generate_STORE(cctx, ISN_STORE, idx, NULL); |
| 4396 | } |
| 4397 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4398 | } |
| 4399 | ret = p; |
| 4400 | |
| 4401 | theend: |
| 4402 | vim_free(name); |
| 4403 | return ret; |
| 4404 | } |
| 4405 | |
| 4406 | /* |
| 4407 | * Compile an :import command. |
| 4408 | */ |
| 4409 | static char_u * |
| 4410 | compile_import(char_u *arg, cctx_T *cctx) |
| 4411 | { |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 4412 | return handle_import(arg, &cctx->ctx_imports, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | /* |
| 4416 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 4417 | */ |
| 4418 | static int |
| 4419 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 4420 | { |
| 4421 | garray_T *instr = &cctx->ctx_instr; |
| 4422 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 4423 | |
| 4424 | if (endlabel == NULL) |
| 4425 | return FAIL; |
| 4426 | endlabel->el_next = *el; |
| 4427 | *el = endlabel; |
| 4428 | endlabel->el_end_label = instr->ga_len; |
| 4429 | |
| 4430 | generate_JUMP(cctx, when, 0); |
| 4431 | return OK; |
| 4432 | } |
| 4433 | |
| 4434 | static void |
| 4435 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 4436 | { |
| 4437 | garray_T *instr = &cctx->ctx_instr; |
| 4438 | |
| 4439 | while (*el != NULL) |
| 4440 | { |
| 4441 | endlabel_T *cur = (*el); |
| 4442 | isn_T *isn; |
| 4443 | |
| 4444 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 4445 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4446 | *el = cur->el_next; |
| 4447 | vim_free(cur); |
| 4448 | } |
| 4449 | } |
| 4450 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 4451 | static void |
| 4452 | compile_free_jump_to_end(endlabel_T **el) |
| 4453 | { |
| 4454 | while (*el != NULL) |
| 4455 | { |
| 4456 | endlabel_T *cur = (*el); |
| 4457 | |
| 4458 | *el = cur->el_next; |
| 4459 | vim_free(cur); |
| 4460 | } |
| 4461 | } |
| 4462 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4463 | /* |
| 4464 | * Create a new scope and set up the generic items. |
| 4465 | */ |
| 4466 | static scope_T * |
| 4467 | new_scope(cctx_T *cctx, scopetype_T type) |
| 4468 | { |
| 4469 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 4470 | |
| 4471 | if (scope == NULL) |
| 4472 | return NULL; |
| 4473 | scope->se_outer = cctx->ctx_scope; |
| 4474 | cctx->ctx_scope = scope; |
| 4475 | scope->se_type = type; |
| 4476 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 4477 | return scope; |
| 4478 | } |
| 4479 | |
| 4480 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4481 | * Free the current scope and go back to the outer scope. |
| 4482 | */ |
| 4483 | static void |
| 4484 | drop_scope(cctx_T *cctx) |
| 4485 | { |
| 4486 | scope_T *scope = cctx->ctx_scope; |
| 4487 | |
| 4488 | if (scope == NULL) |
| 4489 | { |
| 4490 | iemsg("calling drop_scope() without a scope"); |
| 4491 | return; |
| 4492 | } |
| 4493 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 4494 | switch (scope->se_type) |
| 4495 | { |
| 4496 | case IF_SCOPE: |
| 4497 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 4498 | case FOR_SCOPE: |
| 4499 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 4500 | case WHILE_SCOPE: |
| 4501 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 4502 | case TRY_SCOPE: |
| 4503 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 4504 | case NO_SCOPE: |
| 4505 | case BLOCK_SCOPE: |
| 4506 | break; |
| 4507 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4508 | vim_free(scope); |
| 4509 | } |
| 4510 | |
| 4511 | /* |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 4512 | * Evaluate an expression that is a constant: |
| 4513 | * has(arg) |
| 4514 | * |
| 4515 | * Also handle: |
| 4516 | * ! in front logical NOT |
| 4517 | * |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4518 | * Return FAIL if the expression is not a constant. |
| 4519 | */ |
| 4520 | static int |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 4521 | evaluate_const_expr7(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4522 | { |
| 4523 | typval_T argvars[2]; |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 4524 | char_u *start_leader, *end_leader; |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 4525 | int has_call = FALSE; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4526 | |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 4527 | /* |
| 4528 | * Skip '!' characters. They are handled later. |
| 4529 | */ |
| 4530 | start_leader = *arg; |
| 4531 | while (**arg == '!') |
| 4532 | *arg = skipwhite(*arg + 1); |
| 4533 | end_leader = *arg; |
| 4534 | |
| 4535 | /* |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4536 | * Recognize only a few types of constants for now. |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 4537 | */ |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4538 | if (STRNCMP("true", *arg, 4) == 0 && !ASCII_ISALNUM((*arg)[4])) |
| 4539 | { |
| 4540 | tv->v_type = VAR_SPECIAL; |
| 4541 | tv->vval.v_number = VVAL_TRUE; |
| 4542 | *arg += 4; |
| 4543 | return OK; |
| 4544 | } |
| 4545 | if (STRNCMP("false", *arg, 5) == 0 && !ASCII_ISALNUM((*arg)[5])) |
| 4546 | { |
| 4547 | tv->v_type = VAR_SPECIAL; |
| 4548 | tv->vval.v_number = VVAL_FALSE; |
| 4549 | *arg += 5; |
| 4550 | return OK; |
| 4551 | } |
| 4552 | |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 4553 | if (STRNCMP("has(", *arg, 4) == 0) |
| 4554 | { |
| 4555 | has_call = TRUE; |
| 4556 | *arg = skipwhite(*arg + 4); |
| 4557 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4558 | |
| 4559 | if (**arg == '"') |
| 4560 | { |
| 4561 | if (get_string_tv(arg, tv, TRUE) == FAIL) |
| 4562 | return FAIL; |
| 4563 | } |
| 4564 | else if (**arg == '\'') |
| 4565 | { |
| 4566 | if (get_lit_string_tv(arg, tv, TRUE) == FAIL) |
| 4567 | return FAIL; |
| 4568 | } |
| 4569 | else |
| 4570 | return FAIL; |
| 4571 | |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 4572 | if (has_call) |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 4573 | { |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 4574 | *arg = skipwhite(*arg); |
| 4575 | if (**arg != ')') |
| 4576 | return FAIL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 4577 | *arg = *arg + 1; |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 4578 | |
| 4579 | argvars[0] = *tv; |
| 4580 | argvars[1].v_type = VAR_UNKNOWN; |
| 4581 | tv->v_type = VAR_NUMBER; |
| 4582 | tv->vval.v_number = 0; |
| 4583 | f_has(argvars, tv); |
| 4584 | clear_tv(&argvars[0]); |
| 4585 | |
| 4586 | while (start_leader < end_leader) |
| 4587 | { |
| 4588 | if (*start_leader == '!') |
| 4589 | tv->vval.v_number = !tv->vval.v_number; |
| 4590 | ++start_leader; |
| 4591 | } |
Bram Moolenaar | 7f829ca | 2020-01-31 22:12:41 +0100 | [diff] [blame] | 4592 | } |
| 4593 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4594 | return OK; |
| 4595 | } |
| 4596 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4597 | static int |
| 4598 | evaluate_const_expr4(char_u **arg, cctx_T *cctx UNUSED, typval_T *tv) |
| 4599 | { |
| 4600 | exptype_T type = EXPR_UNKNOWN; |
| 4601 | char_u *p; |
| 4602 | int len = 2; |
| 4603 | int type_is = FALSE; |
| 4604 | |
| 4605 | // get the first variable |
| 4606 | if (evaluate_const_expr7(arg, cctx, tv) == FAIL) |
| 4607 | return FAIL; |
| 4608 | |
| 4609 | p = skipwhite(*arg); |
| 4610 | type = get_compare_type(p, &len, &type_is); |
| 4611 | |
| 4612 | /* |
| 4613 | * If there is a comparative operator, use it. |
| 4614 | */ |
| 4615 | if (type != EXPR_UNKNOWN) |
| 4616 | { |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 4617 | typval_T tv2; |
| 4618 | char_u *s1, *s2; |
| 4619 | char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; |
| 4620 | int n; |
| 4621 | |
| 4622 | // TODO: Only string == string is supported now |
| 4623 | if (tv->v_type != VAR_STRING) |
| 4624 | return FAIL; |
| 4625 | if (type != EXPR_EQUAL) |
| 4626 | return FAIL; |
| 4627 | |
| 4628 | // get the second variable |
Bram Moolenaar | 4227c78 | 2020-04-02 16:00:04 +0200 | [diff] [blame] | 4629 | init_tv(&tv2); |
Bram Moolenaar | 80c34ca | 2020-04-01 23:05:18 +0200 | [diff] [blame] | 4630 | *arg = skipwhite(p + len); |
| 4631 | if (evaluate_const_expr7(arg, cctx, &tv2) == FAIL |
| 4632 | || tv2.v_type != VAR_STRING) |
| 4633 | { |
| 4634 | clear_tv(&tv2); |
| 4635 | return FAIL; |
| 4636 | } |
| 4637 | s1 = tv_get_string_buf(tv, buf1); |
| 4638 | s2 = tv_get_string_buf(&tv2, buf2); |
| 4639 | n = STRCMP(s1, s2); |
| 4640 | clear_tv(tv); |
| 4641 | clear_tv(&tv2); |
| 4642 | tv->v_type = VAR_BOOL; |
| 4643 | tv->vval.v_number = n == 0 ? VVAL_TRUE : VVAL_FALSE; |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | return OK; |
| 4647 | } |
| 4648 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4649 | static int evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv); |
| 4650 | |
| 4651 | /* |
| 4652 | * Compile constant || or &&. |
| 4653 | */ |
| 4654 | static int |
| 4655 | evaluate_const_and_or(char_u **arg, cctx_T *cctx, char *op, typval_T *tv) |
| 4656 | { |
| 4657 | char_u *p = skipwhite(*arg); |
| 4658 | int opchar = *op; |
| 4659 | |
| 4660 | if (p[0] == opchar && p[1] == opchar) |
| 4661 | { |
| 4662 | int val = tv2bool(tv); |
| 4663 | |
| 4664 | /* |
| 4665 | * Repeat until there is no following "||" or "&&" |
| 4666 | */ |
| 4667 | while (p[0] == opchar && p[1] == opchar) |
| 4668 | { |
| 4669 | typval_T tv2; |
| 4670 | |
| 4671 | if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[2])) |
| 4672 | return FAIL; |
| 4673 | |
| 4674 | // eval the next expression |
| 4675 | *arg = skipwhite(p + 2); |
| 4676 | tv2.v_type = VAR_UNKNOWN; |
Bram Moolenaar | eed3571 | 2020-02-04 23:08:14 +0100 | [diff] [blame] | 4677 | tv2.v_lock = 0; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4678 | if ((opchar == '|' ? evaluate_const_expr3(arg, cctx, &tv2) |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4679 | : evaluate_const_expr4(arg, cctx, &tv2)) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4680 | { |
| 4681 | clear_tv(&tv2); |
| 4682 | return FAIL; |
| 4683 | } |
| 4684 | if ((opchar == '&') == val) |
| 4685 | { |
| 4686 | // false || tv2 or true && tv2: use tv2 |
| 4687 | clear_tv(tv); |
| 4688 | *tv = tv2; |
| 4689 | val = tv2bool(tv); |
| 4690 | } |
| 4691 | else |
| 4692 | clear_tv(&tv2); |
| 4693 | p = skipwhite(*arg); |
| 4694 | } |
| 4695 | } |
| 4696 | |
| 4697 | return OK; |
| 4698 | } |
| 4699 | |
| 4700 | /* |
| 4701 | * Evaluate an expression that is a constant: expr4 && expr4 && expr4 |
| 4702 | * Return FAIL if the expression is not a constant. |
| 4703 | */ |
| 4704 | static int |
| 4705 | evaluate_const_expr3(char_u **arg, cctx_T *cctx, typval_T *tv) |
| 4706 | { |
| 4707 | // evaluate the first expression |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4708 | if (evaluate_const_expr4(arg, cctx, tv) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4709 | return FAIL; |
| 4710 | |
| 4711 | // || and && work almost the same |
| 4712 | return evaluate_const_and_or(arg, cctx, "&&", tv); |
| 4713 | } |
| 4714 | |
| 4715 | /* |
| 4716 | * Evaluate an expression that is a constant: expr3 || expr3 || expr3 |
| 4717 | * Return FAIL if the expression is not a constant. |
| 4718 | */ |
| 4719 | static int |
| 4720 | evaluate_const_expr2(char_u **arg, cctx_T *cctx, typval_T *tv) |
| 4721 | { |
| 4722 | // evaluate the first expression |
| 4723 | if (evaluate_const_expr3(arg, cctx, tv) == FAIL) |
| 4724 | return FAIL; |
| 4725 | |
| 4726 | // || and && work almost the same |
| 4727 | return evaluate_const_and_or(arg, cctx, "||", tv); |
| 4728 | } |
| 4729 | |
| 4730 | /* |
| 4731 | * Evaluate an expression that is a constant: expr2 ? expr1 : expr1 |
| 4732 | * E.g. for "has('feature')". |
| 4733 | * This does not produce error messages. "tv" should be cleared afterwards. |
| 4734 | * Return FAIL if the expression is not a constant. |
| 4735 | */ |
| 4736 | static int |
| 4737 | evaluate_const_expr1(char_u **arg, cctx_T *cctx, typval_T *tv) |
| 4738 | { |
| 4739 | char_u *p; |
| 4740 | |
| 4741 | // evaluate the first expression |
| 4742 | if (evaluate_const_expr2(arg, cctx, tv) == FAIL) |
| 4743 | return FAIL; |
| 4744 | |
| 4745 | p = skipwhite(*arg); |
| 4746 | if (*p == '?') |
| 4747 | { |
| 4748 | int val = tv2bool(tv); |
| 4749 | typval_T tv2; |
| 4750 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 4751 | // require space before and after the ? |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4752 | if (!VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1])) |
| 4753 | return FAIL; |
| 4754 | |
| 4755 | // evaluate the second expression; any type is accepted |
| 4756 | clear_tv(tv); |
| 4757 | *arg = skipwhite(p + 1); |
| 4758 | if (evaluate_const_expr1(arg, cctx, tv) == FAIL) |
| 4759 | return FAIL; |
| 4760 | |
| 4761 | // Check for the ":". |
| 4762 | p = skipwhite(*arg); |
| 4763 | if (*p != ':' || !VIM_ISWHITE(**arg) || !VIM_ISWHITE(p[1])) |
| 4764 | return FAIL; |
| 4765 | |
| 4766 | // evaluate the third expression |
| 4767 | *arg = skipwhite(p + 1); |
| 4768 | tv2.v_type = VAR_UNKNOWN; |
| 4769 | if (evaluate_const_expr1(arg, cctx, &tv2) == FAIL) |
| 4770 | { |
| 4771 | clear_tv(&tv2); |
| 4772 | return FAIL; |
| 4773 | } |
| 4774 | if (val) |
| 4775 | { |
| 4776 | // use the expr after "?" |
| 4777 | clear_tv(&tv2); |
| 4778 | } |
| 4779 | else |
| 4780 | { |
| 4781 | // use the expr after ":" |
| 4782 | clear_tv(tv); |
| 4783 | *tv = tv2; |
| 4784 | } |
| 4785 | } |
| 4786 | return OK; |
| 4787 | } |
| 4788 | |
| 4789 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4790 | * compile "if expr" |
| 4791 | * |
| 4792 | * "if expr" Produces instructions: |
| 4793 | * EVAL expr Push result of "expr" |
| 4794 | * JUMP_IF_FALSE end |
| 4795 | * ... body ... |
| 4796 | * end: |
| 4797 | * |
| 4798 | * "if expr | else" Produces instructions: |
| 4799 | * EVAL expr Push result of "expr" |
| 4800 | * JUMP_IF_FALSE else |
| 4801 | * ... body ... |
| 4802 | * JUMP_ALWAYS end |
| 4803 | * else: |
| 4804 | * ... body ... |
| 4805 | * end: |
| 4806 | * |
| 4807 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 4808 | * EVAL expr Push result of "expr" |
| 4809 | * JUMP_IF_FALSE elseif |
| 4810 | * ... body ... |
| 4811 | * JUMP_ALWAYS end |
| 4812 | * elseif: |
| 4813 | * EVAL expr Push result of "expr" |
| 4814 | * JUMP_IF_FALSE else |
| 4815 | * ... body ... |
| 4816 | * JUMP_ALWAYS end |
| 4817 | * else: |
| 4818 | * ... body ... |
| 4819 | * end: |
| 4820 | */ |
| 4821 | static char_u * |
| 4822 | compile_if(char_u *arg, cctx_T *cctx) |
| 4823 | { |
| 4824 | char_u *p = arg; |
| 4825 | garray_T *instr = &cctx->ctx_instr; |
| 4826 | scope_T *scope; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4827 | typval_T tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4828 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4829 | // compile "expr"; if we know it evaluates to FALSE skip the block |
| 4830 | tv.v_type = VAR_UNKNOWN; |
| 4831 | if (evaluate_const_expr1(&p, cctx, &tv) == OK) |
| 4832 | cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE; |
| 4833 | else |
| 4834 | cctx->ctx_skip = MAYBE; |
| 4835 | clear_tv(&tv); |
| 4836 | if (cctx->ctx_skip == MAYBE) |
| 4837 | { |
| 4838 | p = arg; |
| 4839 | if (compile_expr1(&p, cctx) == FAIL) |
| 4840 | return NULL; |
| 4841 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4842 | |
| 4843 | scope = new_scope(cctx, IF_SCOPE); |
| 4844 | if (scope == NULL) |
| 4845 | return NULL; |
| 4846 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4847 | if (cctx->ctx_skip == MAYBE) |
| 4848 | { |
| 4849 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 4850 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 4851 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4852 | } |
| 4853 | else |
| 4854 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4855 | |
| 4856 | return p; |
| 4857 | } |
| 4858 | |
| 4859 | static char_u * |
| 4860 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 4861 | { |
| 4862 | char_u *p = arg; |
| 4863 | garray_T *instr = &cctx->ctx_instr; |
| 4864 | isn_T *isn; |
| 4865 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4866 | typval_T tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4867 | |
| 4868 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 4869 | { |
| 4870 | emsg(_(e_elseif_without_if)); |
| 4871 | return NULL; |
| 4872 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 4873 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4874 | |
Bram Moolenaar | 158906c | 2020-02-06 20:39:45 +0100 | [diff] [blame] | 4875 | if (cctx->ctx_skip == MAYBE) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4876 | { |
| 4877 | if (compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4878 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4879 | return NULL; |
| 4880 | // previous "if" or "elseif" jumps here |
| 4881 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 4882 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4883 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4884 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4885 | // compile "expr"; if we know it evaluates to FALSE skip the block |
| 4886 | tv.v_type = VAR_UNKNOWN; |
| 4887 | if (evaluate_const_expr1(&p, cctx, &tv) == OK) |
| 4888 | cctx->ctx_skip = tv2bool(&tv) ? FALSE : TRUE; |
| 4889 | else |
| 4890 | cctx->ctx_skip = MAYBE; |
| 4891 | clear_tv(&tv); |
| 4892 | if (cctx->ctx_skip == MAYBE) |
| 4893 | { |
| 4894 | p = arg; |
| 4895 | if (compile_expr1(&p, cctx) == FAIL) |
| 4896 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4897 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4898 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 4899 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 4900 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4901 | } |
| 4902 | else |
| 4903 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4904 | |
| 4905 | return p; |
| 4906 | } |
| 4907 | |
| 4908 | static char_u * |
| 4909 | compile_else(char_u *arg, cctx_T *cctx) |
| 4910 | { |
| 4911 | char_u *p = arg; |
| 4912 | garray_T *instr = &cctx->ctx_instr; |
| 4913 | isn_T *isn; |
| 4914 | scope_T *scope = cctx->ctx_scope; |
| 4915 | |
| 4916 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 4917 | { |
| 4918 | emsg(_(e_else_without_if)); |
| 4919 | return NULL; |
| 4920 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 4921 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4922 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4923 | // jump from previous block to the end, unless the else block is empty |
| 4924 | if (cctx->ctx_skip == MAYBE) |
| 4925 | { |
| 4926 | if (compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4927 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4928 | return NULL; |
| 4929 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4930 | |
Bram Moolenaar | 158906c | 2020-02-06 20:39:45 +0100 | [diff] [blame] | 4931 | if (cctx->ctx_skip == MAYBE) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4932 | { |
| 4933 | if (scope->se_u.se_if.is_if_label >= 0) |
| 4934 | { |
| 4935 | // previous "if" or "elseif" jumps here |
| 4936 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 4937 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | 158906c | 2020-02-06 20:39:45 +0100 | [diff] [blame] | 4938 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4939 | } |
| 4940 | } |
| 4941 | |
| 4942 | if (cctx->ctx_skip != MAYBE) |
| 4943 | cctx->ctx_skip = !cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4944 | |
| 4945 | return p; |
| 4946 | } |
| 4947 | |
| 4948 | static char_u * |
| 4949 | compile_endif(char_u *arg, cctx_T *cctx) |
| 4950 | { |
| 4951 | scope_T *scope = cctx->ctx_scope; |
| 4952 | ifscope_T *ifscope; |
| 4953 | garray_T *instr = &cctx->ctx_instr; |
| 4954 | isn_T *isn; |
| 4955 | |
| 4956 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 4957 | { |
| 4958 | emsg(_(e_endif_without_if)); |
| 4959 | return NULL; |
| 4960 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 4961 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 4962 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4963 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4964 | if (scope->se_u.se_if.is_if_label >= 0) |
| 4965 | { |
| 4966 | // previous "if" or "elseif" jumps here |
| 4967 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 4968 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4969 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4970 | // Fill in the "end" label in jumps at the end of the blocks. |
| 4971 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 4972 | cctx->ctx_skip = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4973 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 4974 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4975 | return arg; |
| 4976 | } |
| 4977 | |
| 4978 | /* |
| 4979 | * compile "for var in expr" |
| 4980 | * |
| 4981 | * Produces instructions: |
| 4982 | * PUSHNR -1 |
| 4983 | * STORE loop-idx Set index to -1 |
| 4984 | * EVAL expr Push result of "expr" |
| 4985 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 4986 | * - if beyond end, jump to "end" |
| 4987 | * - otherwise get item from list and push it |
| 4988 | * STORE var Store item in "var" |
| 4989 | * ... body ... |
| 4990 | * JUMP top Jump back to repeat |
| 4991 | * end: DROP Drop the result of "expr" |
| 4992 | * |
| 4993 | */ |
| 4994 | static char_u * |
| 4995 | compile_for(char_u *arg, cctx_T *cctx) |
| 4996 | { |
| 4997 | char_u *p; |
| 4998 | size_t varlen; |
| 4999 | garray_T *instr = &cctx->ctx_instr; |
| 5000 | garray_T *stack = &cctx->ctx_type_stack; |
| 5001 | scope_T *scope; |
| 5002 | int loop_idx; // index of loop iteration variable |
| 5003 | int var_idx; // index of "var" |
| 5004 | type_T *vartype; |
| 5005 | |
| 5006 | // TODO: list of variables: "for [key, value] in dict" |
| 5007 | // parse "var" |
| 5008 | for (p = arg; eval_isnamec1(*p); ++p) |
| 5009 | ; |
| 5010 | varlen = p - arg; |
| 5011 | var_idx = lookup_local(arg, varlen, cctx); |
| 5012 | if (var_idx >= 0) |
| 5013 | { |
| 5014 | semsg(_("E1023: variable already defined: %s"), arg); |
| 5015 | return NULL; |
| 5016 | } |
| 5017 | |
| 5018 | // consume "in" |
| 5019 | p = skipwhite(p); |
| 5020 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 5021 | { |
| 5022 | emsg(_(e_missing_in)); |
| 5023 | return NULL; |
| 5024 | } |
| 5025 | p = skipwhite(p + 2); |
| 5026 | |
| 5027 | |
| 5028 | scope = new_scope(cctx, FOR_SCOPE); |
| 5029 | if (scope == NULL) |
| 5030 | return NULL; |
| 5031 | |
| 5032 | // Reserve a variable to store the loop iteration counter. |
| 5033 | loop_idx = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 5034 | if (loop_idx < 0) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5035 | { |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5036 | // only happens when out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5037 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5038 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5039 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5040 | |
| 5041 | // Reserve a variable to store "var" |
| 5042 | var_idx = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 5043 | if (var_idx < 0) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5044 | { |
| 5045 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5046 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5047 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5048 | |
| 5049 | generate_STORENR(cctx, loop_idx, -1); |
| 5050 | |
| 5051 | // compile "expr", it remains on the stack until "endfor" |
| 5052 | arg = p; |
| 5053 | if (compile_expr1(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5054 | { |
| 5055 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5056 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5057 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5058 | |
| 5059 | // now we know the type of "var" |
| 5060 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 5061 | if (vartype->tt_type != VAR_LIST) |
| 5062 | { |
| 5063 | emsg(_("E1024: need a List to iterate over")); |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5064 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5065 | return NULL; |
| 5066 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 5067 | if (vartype->tt_member->tt_type != VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5068 | { |
| 5069 | lvar_T *lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + var_idx; |
| 5070 | |
| 5071 | lvar->lv_type = vartype->tt_member; |
| 5072 | } |
| 5073 | |
| 5074 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5075 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5076 | |
| 5077 | generate_FOR(cctx, loop_idx); |
| 5078 | generate_STORE(cctx, ISN_STORE, var_idx, NULL); |
| 5079 | |
| 5080 | return arg; |
| 5081 | } |
| 5082 | |
| 5083 | /* |
| 5084 | * compile "endfor" |
| 5085 | */ |
| 5086 | static char_u * |
| 5087 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 5088 | { |
| 5089 | garray_T *instr = &cctx->ctx_instr; |
| 5090 | scope_T *scope = cctx->ctx_scope; |
| 5091 | forscope_T *forscope; |
| 5092 | isn_T *isn; |
| 5093 | |
| 5094 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 5095 | { |
| 5096 | emsg(_(e_for)); |
| 5097 | return NULL; |
| 5098 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5099 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5100 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5101 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5102 | |
| 5103 | // At end of ":for" scope jump back to the FOR instruction. |
| 5104 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 5105 | |
| 5106 | // Fill in the "end" label in the FOR statement so it can jump here |
| 5107 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 5108 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 5109 | |
| 5110 | // Fill in the "end" label any BREAK statements |
| 5111 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 5112 | |
| 5113 | // Below the ":for" scope drop the "expr" list from the stack. |
| 5114 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5115 | return NULL; |
| 5116 | |
| 5117 | vim_free(scope); |
| 5118 | |
| 5119 | return arg; |
| 5120 | } |
| 5121 | |
| 5122 | /* |
| 5123 | * compile "while expr" |
| 5124 | * |
| 5125 | * Produces instructions: |
| 5126 | * top: EVAL expr Push result of "expr" |
| 5127 | * JUMP_IF_FALSE end jump if false |
| 5128 | * ... body ... |
| 5129 | * JUMP top Jump back to repeat |
| 5130 | * end: |
| 5131 | * |
| 5132 | */ |
| 5133 | static char_u * |
| 5134 | compile_while(char_u *arg, cctx_T *cctx) |
| 5135 | { |
| 5136 | char_u *p = arg; |
| 5137 | garray_T *instr = &cctx->ctx_instr; |
| 5138 | scope_T *scope; |
| 5139 | |
| 5140 | scope = new_scope(cctx, WHILE_SCOPE); |
| 5141 | if (scope == NULL) |
| 5142 | return NULL; |
| 5143 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5144 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5145 | |
| 5146 | // compile "expr" |
| 5147 | if (compile_expr1(&p, cctx) == FAIL) |
| 5148 | return NULL; |
| 5149 | |
| 5150 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5151 | if (compile_jump_to_end(&scope->se_u.se_while.ws_end_label, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5152 | JUMP_IF_FALSE, cctx) == FAIL) |
| 5153 | return FAIL; |
| 5154 | |
| 5155 | return p; |
| 5156 | } |
| 5157 | |
| 5158 | /* |
| 5159 | * compile "endwhile" |
| 5160 | */ |
| 5161 | static char_u * |
| 5162 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 5163 | { |
| 5164 | scope_T *scope = cctx->ctx_scope; |
| 5165 | |
| 5166 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 5167 | { |
| 5168 | emsg(_(e_while)); |
| 5169 | return NULL; |
| 5170 | } |
| 5171 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5172 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5173 | |
| 5174 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5175 | generate_JUMP(cctx, JUMP_ALWAYS, scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5176 | |
| 5177 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 5178 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5179 | compile_fill_jump_to_end(&scope->se_u.se_while.ws_end_label, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5180 | |
| 5181 | vim_free(scope); |
| 5182 | |
| 5183 | return arg; |
| 5184 | } |
| 5185 | |
| 5186 | /* |
| 5187 | * compile "continue" |
| 5188 | */ |
| 5189 | static char_u * |
| 5190 | compile_continue(char_u *arg, cctx_T *cctx) |
| 5191 | { |
| 5192 | scope_T *scope = cctx->ctx_scope; |
| 5193 | |
| 5194 | for (;;) |
| 5195 | { |
| 5196 | if (scope == NULL) |
| 5197 | { |
| 5198 | emsg(_(e_continue)); |
| 5199 | return NULL; |
| 5200 | } |
| 5201 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5202 | break; |
| 5203 | scope = scope->se_outer; |
| 5204 | } |
| 5205 | |
| 5206 | // Jump back to the FOR or WHILE instruction. |
| 5207 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5208 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 5209 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5210 | return arg; |
| 5211 | } |
| 5212 | |
| 5213 | /* |
| 5214 | * compile "break" |
| 5215 | */ |
| 5216 | static char_u * |
| 5217 | compile_break(char_u *arg, cctx_T *cctx) |
| 5218 | { |
| 5219 | scope_T *scope = cctx->ctx_scope; |
| 5220 | endlabel_T **el; |
| 5221 | |
| 5222 | for (;;) |
| 5223 | { |
| 5224 | if (scope == NULL) |
| 5225 | { |
| 5226 | emsg(_(e_break)); |
| 5227 | return NULL; |
| 5228 | } |
| 5229 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5230 | break; |
| 5231 | scope = scope->se_outer; |
| 5232 | } |
| 5233 | |
| 5234 | // Jump to the end of the FOR or WHILE loop. |
| 5235 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5236 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5237 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5238 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5239 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 5240 | return FAIL; |
| 5241 | |
| 5242 | return arg; |
| 5243 | } |
| 5244 | |
| 5245 | /* |
| 5246 | * compile "{" start of block |
| 5247 | */ |
| 5248 | static char_u * |
| 5249 | compile_block(char_u *arg, cctx_T *cctx) |
| 5250 | { |
| 5251 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 5252 | return NULL; |
| 5253 | return skipwhite(arg + 1); |
| 5254 | } |
| 5255 | |
| 5256 | /* |
| 5257 | * compile end of block: drop one scope |
| 5258 | */ |
| 5259 | static void |
| 5260 | compile_endblock(cctx_T *cctx) |
| 5261 | { |
| 5262 | scope_T *scope = cctx->ctx_scope; |
| 5263 | |
| 5264 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5265 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5266 | vim_free(scope); |
| 5267 | } |
| 5268 | |
| 5269 | /* |
| 5270 | * compile "try" |
| 5271 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 5272 | * finally. |
| 5273 | * Creates another scope for the "try" block itself. |
| 5274 | * TRY instruction sets up exception handling at runtime. |
| 5275 | * |
| 5276 | * "try" |
| 5277 | * TRY -> catch1, -> finally push trystack entry |
| 5278 | * ... try block |
| 5279 | * "throw {exception}" |
| 5280 | * EVAL {exception} |
| 5281 | * THROW create exception |
| 5282 | * ... try block |
| 5283 | * " catch {expr}" |
| 5284 | * JUMP -> finally |
| 5285 | * catch1: PUSH exeception |
| 5286 | * EVAL {expr} |
| 5287 | * MATCH |
| 5288 | * JUMP nomatch -> catch2 |
| 5289 | * CATCH remove exception |
| 5290 | * ... catch block |
| 5291 | * " catch" |
| 5292 | * JUMP -> finally |
| 5293 | * catch2: CATCH remove exception |
| 5294 | * ... catch block |
| 5295 | * " finally" |
| 5296 | * finally: |
| 5297 | * ... finally block |
| 5298 | * " endtry" |
| 5299 | * ENDTRY pop trystack entry, may rethrow |
| 5300 | */ |
| 5301 | static char_u * |
| 5302 | compile_try(char_u *arg, cctx_T *cctx) |
| 5303 | { |
| 5304 | garray_T *instr = &cctx->ctx_instr; |
| 5305 | scope_T *try_scope; |
| 5306 | scope_T *scope; |
| 5307 | |
| 5308 | // scope that holds the jumps that go to catch/finally/endtry |
| 5309 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 5310 | if (try_scope == NULL) |
| 5311 | return NULL; |
| 5312 | |
| 5313 | // "catch" is set when the first ":catch" is found. |
| 5314 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5315 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5316 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 5317 | return NULL; |
| 5318 | |
| 5319 | // scope for the try block itself |
| 5320 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 5321 | if (scope == NULL) |
| 5322 | return NULL; |
| 5323 | |
| 5324 | return arg; |
| 5325 | } |
| 5326 | |
| 5327 | /* |
| 5328 | * compile "catch {expr}" |
| 5329 | */ |
| 5330 | static char_u * |
| 5331 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 5332 | { |
| 5333 | scope_T *scope = cctx->ctx_scope; |
| 5334 | garray_T *instr = &cctx->ctx_instr; |
| 5335 | char_u *p; |
| 5336 | isn_T *isn; |
| 5337 | |
| 5338 | // end block scope from :try or :catch |
| 5339 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5340 | compile_endblock(cctx); |
| 5341 | scope = cctx->ctx_scope; |
| 5342 | |
| 5343 | // Error if not in a :try scope |
| 5344 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 5345 | { |
| 5346 | emsg(_(e_catch)); |
| 5347 | return NULL; |
| 5348 | } |
| 5349 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5350 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5351 | { |
| 5352 | emsg(_("E1033: catch unreachable after catch-all")); |
| 5353 | return NULL; |
| 5354 | } |
| 5355 | |
| 5356 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5357 | if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5358 | JUMP_ALWAYS, cctx) == FAIL) |
| 5359 | return NULL; |
| 5360 | |
| 5361 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5362 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5363 | if (isn->isn_arg.try.try_catch == 0) |
| 5364 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5365 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5366 | { |
| 5367 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5368 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5369 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5370 | } |
| 5371 | |
| 5372 | p = skipwhite(arg); |
| 5373 | if (ends_excmd(*p)) |
| 5374 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5375 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 5376 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5377 | } |
| 5378 | else |
| 5379 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5380 | char_u *end; |
| 5381 | char_u *pat; |
| 5382 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5383 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 5384 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5385 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5386 | // Push v:exception, push {expr} and MATCH |
| 5387 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 5388 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5389 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5390 | if (*end != *p) |
| 5391 | { |
| 5392 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 5393 | vim_free(tofree); |
| 5394 | return FAIL; |
| 5395 | } |
| 5396 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 5397 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5398 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5399 | len = (int)(end - tofree); |
| 5400 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5401 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5402 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5403 | if (pat == NULL) |
| 5404 | return FAIL; |
| 5405 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 5406 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5407 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5408 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 5409 | return NULL; |
| 5410 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5411 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5412 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 5413 | return NULL; |
| 5414 | } |
| 5415 | |
| 5416 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 5417 | return NULL; |
| 5418 | |
| 5419 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 5420 | return NULL; |
| 5421 | return p; |
| 5422 | } |
| 5423 | |
| 5424 | static char_u * |
| 5425 | compile_finally(char_u *arg, cctx_T *cctx) |
| 5426 | { |
| 5427 | scope_T *scope = cctx->ctx_scope; |
| 5428 | garray_T *instr = &cctx->ctx_instr; |
| 5429 | isn_T *isn; |
| 5430 | |
| 5431 | // end block scope from :try or :catch |
| 5432 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5433 | compile_endblock(cctx); |
| 5434 | scope = cctx->ctx_scope; |
| 5435 | |
| 5436 | // Error if not in a :try scope |
| 5437 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 5438 | { |
| 5439 | emsg(_(e_finally)); |
| 5440 | return NULL; |
| 5441 | } |
| 5442 | |
| 5443 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5444 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5445 | if (isn->isn_arg.try.try_finally != 0) |
| 5446 | { |
| 5447 | emsg(_(e_finally_dup)); |
| 5448 | return NULL; |
| 5449 | } |
| 5450 | |
| 5451 | // Fill in the "end" label in jumps at the end of the blocks. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5452 | compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5453 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 5454 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5455 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5456 | { |
| 5457 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5458 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5459 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5460 | } |
| 5461 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5462 | // TODO: set index in ts_finally_label jumps |
| 5463 | |
| 5464 | return arg; |
| 5465 | } |
| 5466 | |
| 5467 | static char_u * |
| 5468 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 5469 | { |
| 5470 | scope_T *scope = cctx->ctx_scope; |
| 5471 | garray_T *instr = &cctx->ctx_instr; |
| 5472 | isn_T *isn; |
| 5473 | |
| 5474 | // end block scope from :catch or :finally |
| 5475 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5476 | compile_endblock(cctx); |
| 5477 | scope = cctx->ctx_scope; |
| 5478 | |
| 5479 | // Error if not in a :try scope |
| 5480 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 5481 | { |
| 5482 | if (scope == NULL) |
| 5483 | emsg(_(e_no_endtry)); |
| 5484 | else if (scope->se_type == WHILE_SCOPE) |
| 5485 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 5486 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5487 | emsg(_(e_endfor)); |
| 5488 | else |
| 5489 | emsg(_(e_endif)); |
| 5490 | return NULL; |
| 5491 | } |
| 5492 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5493 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5494 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 5495 | { |
| 5496 | emsg(_("E1032: missing :catch or :finally")); |
| 5497 | return NULL; |
| 5498 | } |
| 5499 | |
| 5500 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 5501 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5502 | compile_fill_jump_to_end(&scope->se_u.se_try.ts_end_label, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5503 | |
| 5504 | // End :catch or :finally scope: set value in ISN_TRY instruction |
| 5505 | if (isn->isn_arg.try.try_finally == 0) |
| 5506 | isn->isn_arg.try.try_finally = instr->ga_len; |
| 5507 | compile_endblock(cctx); |
| 5508 | |
| 5509 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 5510 | return NULL; |
| 5511 | return arg; |
| 5512 | } |
| 5513 | |
| 5514 | /* |
| 5515 | * compile "throw {expr}" |
| 5516 | */ |
| 5517 | static char_u * |
| 5518 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 5519 | { |
| 5520 | char_u *p = skipwhite(arg); |
| 5521 | |
| 5522 | if (ends_excmd(*p)) |
| 5523 | { |
| 5524 | emsg(_(e_argreq)); |
| 5525 | return NULL; |
| 5526 | } |
| 5527 | if (compile_expr1(&p, cctx) == FAIL) |
| 5528 | return NULL; |
| 5529 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 5530 | return NULL; |
| 5531 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 5532 | return NULL; |
| 5533 | |
| 5534 | return p; |
| 5535 | } |
| 5536 | |
| 5537 | /* |
| 5538 | * compile "echo expr" |
| 5539 | */ |
| 5540 | static char_u * |
| 5541 | compile_echo(char_u *arg, int with_white, cctx_T *cctx) |
| 5542 | { |
| 5543 | char_u *p = arg; |
| 5544 | int count = 0; |
| 5545 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 5546 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5547 | { |
| 5548 | if (compile_expr1(&p, cctx) == FAIL) |
| 5549 | return NULL; |
| 5550 | ++count; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 5551 | p = skipwhite(p); |
| 5552 | if (ends_excmd(*p)) |
| 5553 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | generate_ECHO(cctx, with_white, count); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 5557 | return p; |
| 5558 | } |
| 5559 | |
| 5560 | /* |
| 5561 | * compile "execute expr" |
| 5562 | */ |
| 5563 | static char_u * |
| 5564 | compile_execute(char_u *arg, cctx_T *cctx) |
| 5565 | { |
| 5566 | char_u *p = arg; |
| 5567 | int count = 0; |
| 5568 | |
| 5569 | for (;;) |
| 5570 | { |
| 5571 | if (compile_expr1(&p, cctx) == FAIL) |
| 5572 | return NULL; |
| 5573 | ++count; |
| 5574 | p = skipwhite(p); |
| 5575 | if (ends_excmd(*p)) |
| 5576 | break; |
| 5577 | } |
| 5578 | |
| 5579 | generate_EXECUTE(cctx, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5580 | |
| 5581 | return p; |
| 5582 | } |
| 5583 | |
| 5584 | /* |
| 5585 | * After ex_function() has collected all the function lines: parse and compile |
| 5586 | * the lines into instructions. |
| 5587 | * Adds the function to "def_functions". |
| 5588 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 5589 | * return statement (used for lambda). |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 5590 | * This can be used recursively through compile_lambda(), which may reallocate |
| 5591 | * "def_functions". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5592 | */ |
| 5593 | void |
| 5594 | compile_def_function(ufunc_T *ufunc, int set_return_type) |
| 5595 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5596 | char_u *line = NULL; |
| 5597 | char_u *p; |
| 5598 | exarg_T ea; |
| 5599 | char *errormsg = NULL; // error message |
| 5600 | int had_return = FALSE; |
| 5601 | cctx_T cctx; |
| 5602 | garray_T *instr; |
| 5603 | int called_emsg_before = called_emsg; |
| 5604 | int ret = FAIL; |
| 5605 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 5606 | int emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5607 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5608 | { |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 5609 | dfunc_T *dfunc; // may be invalidated by compile_lambda() |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5610 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 5611 | if (ufunc->uf_dfunc_idx >= 0) |
| 5612 | { |
| 5613 | // Redefining a function that was compiled before. |
| 5614 | dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; |
| 5615 | |
| 5616 | // Free old instructions. |
| 5617 | delete_def_function_contents(dfunc); |
| 5618 | } |
| 5619 | else |
| 5620 | { |
| 5621 | // Add the function to "def_functions". |
| 5622 | if (ga_grow(&def_functions, 1) == FAIL) |
| 5623 | return; |
| 5624 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 5625 | vim_memset(dfunc, 0, sizeof(dfunc_T)); |
| 5626 | dfunc->df_idx = def_functions.ga_len; |
| 5627 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 5628 | dfunc->df_ufunc = ufunc; |
| 5629 | ++def_functions.ga_len; |
| 5630 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | vim_memset(&cctx, 0, sizeof(cctx)); |
| 5634 | cctx.ctx_ufunc = ufunc; |
| 5635 | cctx.ctx_lnum = -1; |
| 5636 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 5637 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 5638 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 5639 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 5640 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 5641 | instr = &cctx.ctx_instr; |
| 5642 | |
| 5643 | // Most modern script version. |
| 5644 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 5645 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 5646 | if (ufunc->uf_def_args.ga_len > 0) |
| 5647 | { |
| 5648 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 5649 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 5650 | int i; |
| 5651 | char_u *arg; |
| 5652 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 5653 | |
| 5654 | // Produce instructions for the default values of optional arguments. |
| 5655 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 5656 | // where to start when the function is called, depending on the number |
| 5657 | // of arguments. |
| 5658 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 5659 | if (ufunc->uf_def_arg_idx == NULL) |
| 5660 | goto erret; |
| 5661 | for (i = 0; i < count; ++i) |
| 5662 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 5663 | garray_T *stack = &cctx.ctx_type_stack; |
| 5664 | type_T *val_type; |
| 5665 | int arg_idx = first_def_arg + i; |
| 5666 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 5667 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 5668 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 5669 | if (compile_expr1(&arg, &cctx) == FAIL) |
| 5670 | goto erret; |
| 5671 | |
| 5672 | // If no type specified use the type of the default value. |
| 5673 | // Otherwise check that the default value type matches the |
| 5674 | // specified type. |
| 5675 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 5676 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
| 5677 | ufunc->uf_arg_types[arg_idx] = val_type; |
| 5678 | else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE) |
| 5679 | == FAIL) |
| 5680 | { |
| 5681 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 5682 | arg_idx + 1); |
| 5683 | goto erret; |
| 5684 | } |
| 5685 | |
| 5686 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 5687 | goto erret; |
| 5688 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 5689 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
| 5690 | } |
| 5691 | |
| 5692 | /* |
| 5693 | * Loop over all the lines of the function and generate instructions. |
| 5694 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5695 | for (;;) |
| 5696 | { |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 5697 | int is_ex_command; |
| 5698 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5699 | // Bail out on the first error to avoid a flood of errors and report |
| 5700 | // the right line number when inside try/catch. |
| 5701 | if (emsg_before != called_emsg) |
| 5702 | goto erret; |
| 5703 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5704 | if (line != NULL && *line == '|') |
| 5705 | // the line continues after a '|' |
| 5706 | ++line; |
| 5707 | else if (line != NULL && *line != NUL) |
| 5708 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5709 | semsg(_("E488: Trailing characters: %s"), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5710 | goto erret; |
| 5711 | } |
| 5712 | else |
| 5713 | { |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame^] | 5714 | line = next_line_from_context(&cctx); |
| 5715 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5716 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5717 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 5718 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5719 | |
| 5720 | had_return = FALSE; |
| 5721 | vim_memset(&ea, 0, sizeof(ea)); |
| 5722 | ea.cmdlinep = &line; |
| 5723 | ea.cmd = skipwhite(line); |
| 5724 | |
| 5725 | // "}" ends a block scope |
| 5726 | if (*ea.cmd == '}') |
| 5727 | { |
| 5728 | scopetype_T stype = cctx.ctx_scope == NULL |
| 5729 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
| 5730 | |
| 5731 | if (stype == BLOCK_SCOPE) |
| 5732 | { |
| 5733 | compile_endblock(&cctx); |
| 5734 | line = ea.cmd; |
| 5735 | } |
| 5736 | else |
| 5737 | { |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 5738 | emsg(_("E1025: using } outside of a block scope")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5739 | goto erret; |
| 5740 | } |
| 5741 | if (line != NULL) |
| 5742 | line = skipwhite(ea.cmd + 1); |
| 5743 | continue; |
| 5744 | } |
| 5745 | |
| 5746 | // "{" starts a block scope |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 5747 | // "{'a': 1}->func() is something else |
| 5748 | if (*ea.cmd == '{' && ends_excmd(*skipwhite(ea.cmd + 1))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5749 | { |
| 5750 | line = compile_block(ea.cmd, &cctx); |
| 5751 | continue; |
| 5752 | } |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 5753 | is_ex_command = *ea.cmd == ':'; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5754 | |
| 5755 | /* |
| 5756 | * COMMAND MODIFIERS |
| 5757 | */ |
| 5758 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 5759 | { |
| 5760 | if (errormsg != NULL) |
| 5761 | goto erret; |
| 5762 | // empty line or comment |
| 5763 | line = (char_u *)""; |
| 5764 | continue; |
| 5765 | } |
| 5766 | |
| 5767 | // Skip ":call" to get to the function name. |
| 5768 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 5769 | ea.cmd = skipwhite(ea.cmd); |
| 5770 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 5771 | if (!is_ex_command) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5772 | { |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 5773 | // Assuming the command starts with a variable or function name, |
| 5774 | // find what follows. Also "&opt = val", "$ENV = val" and "@r = |
| 5775 | // val". |
| 5776 | p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
| 5777 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 5778 | p = to_name_end(p, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 5779 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5780 | { |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 5781 | int oplen; |
| 5782 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5783 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 5784 | oplen = assignment_len(skipwhite(p), &heredoc); |
| 5785 | if (oplen > 0) |
| 5786 | { |
| 5787 | // Recognize an assignment if we recognize the variable |
| 5788 | // name: |
| 5789 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 5790 | // "local = expr" where "local" is a local var. |
| 5791 | // "script = expr" where "script" is a script-local var. |
| 5792 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 5793 | // "&opt = expr" |
| 5794 | // "$ENV = expr" |
| 5795 | // "@r = expr" |
| 5796 | if (*ea.cmd == '&' |
| 5797 | || *ea.cmd == '$' |
| 5798 | || *ea.cmd == '@' |
| 5799 | || ((p - ea.cmd) > 2 && ea.cmd[1] == ':') |
| 5800 | || lookup_local(ea.cmd, p - ea.cmd, &cctx) >= 0 |
| 5801 | || lookup_script(ea.cmd, p - ea.cmd) == OK |
| 5802 | || find_imported(ea.cmd, p - ea.cmd, &cctx) != NULL) |
| 5803 | { |
| 5804 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 5805 | if (line == NULL) |
| 5806 | goto erret; |
| 5807 | continue; |
| 5808 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5809 | } |
| 5810 | } |
| 5811 | } |
| 5812 | |
| 5813 | /* |
| 5814 | * COMMAND after range |
| 5815 | */ |
| 5816 | ea.cmd = skip_range(ea.cmd, NULL); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 5817 | p = find_ex_command(&ea, NULL, is_ex_command ? NULL : lookup_local, |
| 5818 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5819 | |
| 5820 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 5821 | { |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5822 | if (cctx.ctx_skip == TRUE) |
| 5823 | { |
| 5824 | line += STRLEN(line); |
| 5825 | continue; |
| 5826 | } |
| 5827 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5828 | // Expression or function call. |
| 5829 | if (ea.cmdidx == CMD_eval) |
| 5830 | { |
| 5831 | p = ea.cmd; |
| 5832 | if (compile_expr1(&p, &cctx) == FAIL) |
| 5833 | goto erret; |
| 5834 | |
| 5835 | // drop the return value |
| 5836 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 5837 | line = p; |
| 5838 | continue; |
| 5839 | } |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 5840 | // CMD_let cannot happen, compile_assignment() above is used |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5841 | iemsg("Command from find_ex_command() not handled"); |
| 5842 | goto erret; |
| 5843 | } |
| 5844 | |
| 5845 | p = skipwhite(p); |
| 5846 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5847 | if (cctx.ctx_skip == TRUE |
| 5848 | && ea.cmdidx != CMD_elseif |
| 5849 | && ea.cmdidx != CMD_else |
| 5850 | && ea.cmdidx != CMD_endif) |
| 5851 | { |
| 5852 | line += STRLEN(line); |
| 5853 | continue; |
| 5854 | } |
| 5855 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5856 | switch (ea.cmdidx) |
| 5857 | { |
| 5858 | case CMD_def: |
| 5859 | case CMD_function: |
| 5860 | // TODO: Nested function |
| 5861 | emsg("Nested function not implemented yet"); |
| 5862 | goto erret; |
| 5863 | |
| 5864 | case CMD_return: |
| 5865 | line = compile_return(p, set_return_type, &cctx); |
| 5866 | had_return = TRUE; |
| 5867 | break; |
| 5868 | |
| 5869 | case CMD_let: |
| 5870 | case CMD_const: |
| 5871 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
| 5872 | break; |
| 5873 | |
| 5874 | case CMD_import: |
| 5875 | line = compile_import(p, &cctx); |
| 5876 | break; |
| 5877 | |
| 5878 | case CMD_if: |
| 5879 | line = compile_if(p, &cctx); |
| 5880 | break; |
| 5881 | case CMD_elseif: |
| 5882 | line = compile_elseif(p, &cctx); |
| 5883 | break; |
| 5884 | case CMD_else: |
| 5885 | line = compile_else(p, &cctx); |
| 5886 | break; |
| 5887 | case CMD_endif: |
| 5888 | line = compile_endif(p, &cctx); |
| 5889 | break; |
| 5890 | |
| 5891 | case CMD_while: |
| 5892 | line = compile_while(p, &cctx); |
| 5893 | break; |
| 5894 | case CMD_endwhile: |
| 5895 | line = compile_endwhile(p, &cctx); |
| 5896 | break; |
| 5897 | |
| 5898 | case CMD_for: |
| 5899 | line = compile_for(p, &cctx); |
| 5900 | break; |
| 5901 | case CMD_endfor: |
| 5902 | line = compile_endfor(p, &cctx); |
| 5903 | break; |
| 5904 | case CMD_continue: |
| 5905 | line = compile_continue(p, &cctx); |
| 5906 | break; |
| 5907 | case CMD_break: |
| 5908 | line = compile_break(p, &cctx); |
| 5909 | break; |
| 5910 | |
| 5911 | case CMD_try: |
| 5912 | line = compile_try(p, &cctx); |
| 5913 | break; |
| 5914 | case CMD_catch: |
| 5915 | line = compile_catch(p, &cctx); |
| 5916 | break; |
| 5917 | case CMD_finally: |
| 5918 | line = compile_finally(p, &cctx); |
| 5919 | break; |
| 5920 | case CMD_endtry: |
| 5921 | line = compile_endtry(p, &cctx); |
| 5922 | break; |
| 5923 | case CMD_throw: |
| 5924 | line = compile_throw(p, &cctx); |
| 5925 | break; |
| 5926 | |
| 5927 | case CMD_echo: |
| 5928 | line = compile_echo(p, TRUE, &cctx); |
| 5929 | break; |
| 5930 | case CMD_echon: |
| 5931 | line = compile_echo(p, FALSE, &cctx); |
| 5932 | break; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 5933 | case CMD_execute: |
| 5934 | line = compile_execute(p, &cctx); |
| 5935 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5936 | |
| 5937 | default: |
| 5938 | // Not recognized, execute with do_cmdline_cmd(). |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 5939 | // TODO: |
| 5940 | // CMD_echomsg |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 5941 | // etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5942 | generate_EXEC(&cctx, line); |
| 5943 | line = (char_u *)""; |
| 5944 | break; |
| 5945 | } |
| 5946 | if (line == NULL) |
| 5947 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 5948 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5949 | |
| 5950 | if (cctx.ctx_type_stack.ga_len < 0) |
| 5951 | { |
| 5952 | iemsg("Type stack underflow"); |
| 5953 | goto erret; |
| 5954 | } |
| 5955 | } |
| 5956 | |
| 5957 | if (cctx.ctx_scope != NULL) |
| 5958 | { |
| 5959 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 5960 | emsg(_(e_endif)); |
| 5961 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 5962 | emsg(_(e_endwhile)); |
| 5963 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 5964 | emsg(_(e_endfor)); |
| 5965 | else |
| 5966 | emsg(_("E1026: Missing }")); |
| 5967 | goto erret; |
| 5968 | } |
| 5969 | |
| 5970 | if (!had_return) |
| 5971 | { |
| 5972 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 5973 | { |
| 5974 | emsg(_("E1027: Missing return statement")); |
| 5975 | goto erret; |
| 5976 | } |
| 5977 | |
| 5978 | // Return zero if there is no return at the end. |
| 5979 | generate_PUSHNR(&cctx, 0); |
| 5980 | generate_instr(&cctx, ISN_RETURN); |
| 5981 | } |
| 5982 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 5983 | { |
| 5984 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 5985 | + ufunc->uf_dfunc_idx; |
| 5986 | dfunc->df_deleted = FALSE; |
| 5987 | dfunc->df_instr = instr->ga_data; |
| 5988 | dfunc->df_instr_count = instr->ga_len; |
| 5989 | dfunc->df_varcount = cctx.ctx_max_local; |
| 5990 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5991 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5992 | { |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 5993 | int varargs = ufunc->uf_va_name != NULL; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 5994 | int argcount = ufunc->uf_args.ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5995 | |
| 5996 | // Create a type for the function, with the return type and any |
| 5997 | // argument types. |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 5998 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 5999 | // The type is included in "tt_args". |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6000 | if (argcount > 0 || varargs) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6001 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6002 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 6003 | argcount, &ufunc->uf_type_list); |
| 6004 | // Add argument types to the function type. |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6005 | if (func_type_add_arg_types(ufunc->uf_func_type, |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6006 | argcount + varargs, |
| 6007 | &ufunc->uf_type_list) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6008 | { |
| 6009 | ret = FAIL; |
| 6010 | goto erret; |
| 6011 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6012 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 6013 | ufunc->uf_func_type->tt_min_argcount = |
| 6014 | argcount - ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6015 | if (ufunc->uf_arg_types == NULL) |
| 6016 | { |
| 6017 | int i; |
| 6018 | |
| 6019 | // lambda does not have argument types. |
| 6020 | for (i = 0; i < argcount; ++i) |
| 6021 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 6022 | } |
| 6023 | else |
| 6024 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 6025 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6026 | if (varargs) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6027 | { |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 6028 | ufunc->uf_func_type->tt_args[argcount] = |
| 6029 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6030 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 6031 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6032 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 6033 | else |
| 6034 | // No arguments, can use a predefined type. |
| 6035 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 6036 | argcount, &ufunc->uf_type_list); |
| 6037 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6038 | } |
| 6039 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6040 | ret = OK; |
| 6041 | |
| 6042 | erret: |
| 6043 | if (ret == FAIL) |
| 6044 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6045 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6046 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6047 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6048 | |
| 6049 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 6050 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6051 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6052 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6053 | ufunc->uf_dfunc_idx = -1; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6054 | if (!dfunc->df_deleted) |
| 6055 | --def_functions.ga_len; |
| 6056 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6057 | while (cctx.ctx_scope != NULL) |
| 6058 | drop_scope(&cctx); |
| 6059 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6060 | // Don't execute this function body. |
| 6061 | ga_clear_strings(&ufunc->uf_lines); |
| 6062 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6063 | if (errormsg != NULL) |
| 6064 | emsg(errormsg); |
| 6065 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 6066 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6067 | } |
| 6068 | |
| 6069 | current_sctx = save_current_sctx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6070 | free_imported(&cctx); |
| 6071 | free_local(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6072 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6073 | } |
| 6074 | |
| 6075 | /* |
| 6076 | * Delete an instruction, free what it contains. |
| 6077 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6078 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6079 | delete_instr(isn_T *isn) |
| 6080 | { |
| 6081 | switch (isn->isn_type) |
| 6082 | { |
| 6083 | case ISN_EXEC: |
| 6084 | case ISN_LOADENV: |
| 6085 | case ISN_LOADG: |
| 6086 | case ISN_LOADOPT: |
| 6087 | case ISN_MEMBER: |
| 6088 | case ISN_PUSHEXC: |
| 6089 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6090 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6091 | case ISN_STOREG: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6092 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6093 | vim_free(isn->isn_arg.string); |
| 6094 | break; |
| 6095 | |
| 6096 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6097 | case ISN_STORES: |
| 6098 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6099 | break; |
| 6100 | |
| 6101 | case ISN_STOREOPT: |
| 6102 | vim_free(isn->isn_arg.storeopt.so_name); |
| 6103 | break; |
| 6104 | |
| 6105 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 6106 | blob_unref(isn->isn_arg.blob); |
| 6107 | break; |
| 6108 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6109 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6110 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6111 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6112 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6113 | break; |
| 6114 | |
| 6115 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6116 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6117 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6118 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6119 | break; |
| 6120 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6121 | case ISN_UCALL: |
| 6122 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 6123 | break; |
| 6124 | |
| 6125 | case ISN_2BOOL: |
| 6126 | case ISN_2STRING: |
| 6127 | case ISN_ADDBLOB: |
| 6128 | case ISN_ADDLIST: |
| 6129 | case ISN_BCALL: |
| 6130 | case ISN_CATCH: |
| 6131 | case ISN_CHECKNR: |
| 6132 | case ISN_CHECKTYPE: |
| 6133 | case ISN_COMPAREANY: |
| 6134 | case ISN_COMPAREBLOB: |
| 6135 | case ISN_COMPAREBOOL: |
| 6136 | case ISN_COMPAREDICT: |
| 6137 | case ISN_COMPAREFLOAT: |
| 6138 | case ISN_COMPAREFUNC: |
| 6139 | case ISN_COMPARELIST: |
| 6140 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6141 | case ISN_COMPARESPECIAL: |
| 6142 | case ISN_COMPARESTRING: |
| 6143 | case ISN_CONCAT: |
| 6144 | case ISN_DCALL: |
| 6145 | case ISN_DROP: |
| 6146 | case ISN_ECHO: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6147 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6148 | case ISN_ENDTRY: |
| 6149 | case ISN_FOR: |
| 6150 | case ISN_FUNCREF: |
| 6151 | case ISN_INDEX: |
| 6152 | case ISN_JUMP: |
| 6153 | case ISN_LOAD: |
| 6154 | case ISN_LOADSCRIPT: |
| 6155 | case ISN_LOADREG: |
| 6156 | case ISN_LOADV: |
| 6157 | case ISN_NEGATENR: |
| 6158 | case ISN_NEWDICT: |
| 6159 | case ISN_NEWLIST: |
| 6160 | case ISN_OPNR: |
| 6161 | case ISN_OPFLOAT: |
| 6162 | case ISN_OPANY: |
| 6163 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6164 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6165 | case ISN_PUSHF: |
| 6166 | case ISN_PUSHNR: |
| 6167 | case ISN_PUSHBOOL: |
| 6168 | case ISN_PUSHSPEC: |
| 6169 | case ISN_RETURN: |
| 6170 | case ISN_STORE: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6171 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6172 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6173 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6174 | case ISN_STORESCRIPT: |
| 6175 | case ISN_THROW: |
| 6176 | case ISN_TRY: |
| 6177 | // nothing allocated |
| 6178 | break; |
| 6179 | } |
| 6180 | } |
| 6181 | |
| 6182 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6183 | * Free all instructions for "dfunc". |
| 6184 | */ |
| 6185 | static void |
| 6186 | delete_def_function_contents(dfunc_T *dfunc) |
| 6187 | { |
| 6188 | int idx; |
| 6189 | |
| 6190 | ga_clear(&dfunc->df_def_args_isn); |
| 6191 | |
| 6192 | if (dfunc->df_instr != NULL) |
| 6193 | { |
| 6194 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 6195 | delete_instr(dfunc->df_instr + idx); |
| 6196 | VIM_CLEAR(dfunc->df_instr); |
| 6197 | } |
| 6198 | |
| 6199 | dfunc->df_deleted = TRUE; |
| 6200 | } |
| 6201 | |
| 6202 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6203 | * When a user function is deleted, delete any associated def function. |
| 6204 | */ |
| 6205 | void |
| 6206 | delete_def_function(ufunc_T *ufunc) |
| 6207 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6208 | if (ufunc->uf_dfunc_idx >= 0) |
| 6209 | { |
| 6210 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6211 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6212 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6213 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6214 | } |
| 6215 | } |
| 6216 | |
| 6217 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6218 | /* |
| 6219 | * Free all functions defined with ":def". |
| 6220 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6221 | void |
| 6222 | free_def_functions(void) |
| 6223 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6224 | int idx; |
| 6225 | |
| 6226 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 6227 | { |
| 6228 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 6229 | |
| 6230 | delete_def_function_contents(dfunc); |
| 6231 | } |
| 6232 | |
| 6233 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6234 | } |
| 6235 | #endif |
| 6236 | |
| 6237 | |
| 6238 | #endif // FEAT_EVAL |