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