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