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