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