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