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