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 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 26 | // values for ctx_skip |
| 27 | typedef enum { |
| 28 | SKIP_NOT, // condition is a constant, produce code |
| 29 | SKIP_YES, // condition is a constant, do NOT produce code |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 30 | SKIP_UNKNOWN // condition is not a constant, produce code |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 31 | } skip_T; |
| 32 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Chain of jump instructions where the end label needs to be set. |
| 35 | */ |
| 36 | typedef struct endlabel_S endlabel_T; |
| 37 | struct endlabel_S { |
| 38 | endlabel_T *el_next; // chain end_label locations |
| 39 | int el_end_label; // instruction idx where to set end |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * info specific for the scope of :if / elseif / else |
| 44 | */ |
| 45 | typedef struct { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 46 | int is_seen_else; |
| 47 | int is_had_return; // every block ends in :return |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 48 | int is_if_label; // instruction idx at IF or ELSEIF |
| 49 | endlabel_T *is_end_label; // instructions to set end label |
| 50 | } ifscope_T; |
| 51 | |
| 52 | /* |
| 53 | * info specific for the scope of :while |
| 54 | */ |
| 55 | typedef struct { |
| 56 | int ws_top_label; // instruction idx at WHILE |
| 57 | endlabel_T *ws_end_label; // instructions to set end |
| 58 | } whilescope_T; |
| 59 | |
| 60 | /* |
| 61 | * info specific for the scope of :for |
| 62 | */ |
| 63 | typedef struct { |
| 64 | int fs_top_label; // instruction idx at FOR |
| 65 | endlabel_T *fs_end_label; // break instructions |
| 66 | } forscope_T; |
| 67 | |
| 68 | /* |
| 69 | * info specific for the scope of :try |
| 70 | */ |
| 71 | typedef struct { |
| 72 | int ts_try_label; // instruction idx at TRY |
| 73 | endlabel_T *ts_end_label; // jump to :finally or :endtry |
| 74 | int ts_catch_label; // instruction idx of last CATCH |
| 75 | int ts_caught_all; // "catch" without argument encountered |
| 76 | } tryscope_T; |
| 77 | |
| 78 | typedef enum { |
| 79 | NO_SCOPE, |
| 80 | IF_SCOPE, |
| 81 | WHILE_SCOPE, |
| 82 | FOR_SCOPE, |
| 83 | TRY_SCOPE, |
| 84 | BLOCK_SCOPE |
| 85 | } scopetype_T; |
| 86 | |
| 87 | /* |
| 88 | * Info for one scope, pointed to by "ctx_scope". |
| 89 | */ |
| 90 | typedef struct scope_S scope_T; |
| 91 | struct scope_S { |
| 92 | scope_T *se_outer; // scope containing this one |
| 93 | scopetype_T se_type; |
| 94 | int se_local_count; // ctx_locals.ga_len before scope |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 95 | skip_T se_skip_save; // ctx_skip before the block |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 96 | union { |
| 97 | ifscope_T se_if; |
| 98 | whilescope_T se_while; |
| 99 | forscope_T se_for; |
| 100 | tryscope_T se_try; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 101 | } se_u; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Entry for "ctx_locals". Used for arguments and local variables. |
| 106 | */ |
| 107 | typedef struct { |
| 108 | char_u *lv_name; |
| 109 | type_T *lv_type; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 110 | int lv_idx; // index of the variable on the stack |
| 111 | int lv_from_outer; // when TRUE using ctx_outer scope |
| 112 | int lv_const; // when TRUE cannot be assigned to |
| 113 | int lv_arg; // when TRUE this is an argument |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 114 | } lvar_T; |
| 115 | |
| 116 | /* |
| 117 | * Context for compiling lines of Vim script. |
| 118 | * Stores info about the local variables and condition stack. |
| 119 | */ |
| 120 | struct cctx_S { |
| 121 | ufunc_T *ctx_ufunc; // current function |
| 122 | int ctx_lnum; // line number in current function |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 123 | char_u *ctx_line_start; // start of current line or NULL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 124 | garray_T ctx_instr; // generated instructions |
| 125 | |
| 126 | garray_T ctx_locals; // currently visible local variables |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 127 | int ctx_locals_count; // total number of local variables |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 128 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 129 | int ctx_closure_count; // number of closures created in the |
| 130 | // function |
| 131 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 132 | garray_T ctx_imports; // imported items |
| 133 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 134 | skip_T ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 135 | scope_T *ctx_scope; // current scope, NULL at toplevel |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 136 | int ctx_had_return; // last seen statement was "return" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 137 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 138 | cctx_T *ctx_outer; // outer scope for lambda or nested |
| 139 | // function |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 140 | int ctx_outer_used; // var in ctx_outer was used |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 141 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 142 | garray_T ctx_type_stack; // type of each item on the stack |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 143 | garray_T *ctx_type_list; // list of pointers to allocated types |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static char e_var_notfound[] = N_("E1001: variable not found: %s"); |
| 147 | static char e_syntax_at[] = N_("E1002: Syntax error at %s"); |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 148 | static char e_used_as_arg[] = N_("E1006: %s is used as an argument"); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 149 | static char e_cannot_use_void[] = N_("E1031: Cannot use void value"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 150 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 151 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 152 | static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 153 | |
| 154 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 155 | * Lookup variable "name" in the local scope and return it. |
| 156 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 157 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 158 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 159 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 160 | { |
| 161 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 162 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 163 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 164 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 165 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 166 | |
| 167 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 168 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 169 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 170 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 171 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 172 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 173 | { |
| 174 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 175 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 176 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 177 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 178 | |
| 179 | // Find local in outer function scope. |
| 180 | if (cctx->ctx_outer != NULL) |
| 181 | { |
| 182 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 183 | if (lvar != NULL) |
| 184 | { |
| 185 | // TODO: are there situations we should not mark the outer scope as |
| 186 | // used? |
| 187 | cctx->ctx_outer_used = TRUE; |
| 188 | lvar->lv_from_outer = TRUE; |
| 189 | return lvar; |
| 190 | } |
| 191 | } |
| 192 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 193 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 197 | * Lookup an argument in the current function and an enclosing function. |
| 198 | * Returns the argument index in "idxp" |
| 199 | * Returns the argument type in "type" |
| 200 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 201 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 202 | */ |
| 203 | static int |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 204 | lookup_arg( |
| 205 | char_u *name, |
| 206 | size_t len, |
| 207 | int *idxp, |
| 208 | type_T **type, |
| 209 | int *gen_load_outer, |
| 210 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 211 | { |
| 212 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 213 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 214 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 215 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 216 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 217 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 218 | { |
| 219 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 220 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 221 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 222 | { |
| 223 | if (idxp != NULL) |
| 224 | { |
| 225 | // Arguments are located above the frame pointer. One further |
| 226 | // if there is a vararg argument |
| 227 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 228 | + STACK_FRAME_SIZE) |
| 229 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 230 | |
| 231 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 232 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 233 | else |
| 234 | *type = &t_any; |
| 235 | } |
| 236 | return OK; |
| 237 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 238 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 239 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 240 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 241 | if (va_name != NULL |
| 242 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 243 | { |
| 244 | if (idxp != NULL) |
| 245 | { |
| 246 | // varargs is always the last argument |
| 247 | *idxp = -STACK_FRAME_SIZE - 1; |
| 248 | *type = cctx->ctx_ufunc->uf_va_type; |
| 249 | } |
| 250 | return OK; |
| 251 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 252 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 253 | if (cctx->ctx_outer != NULL) |
| 254 | { |
| 255 | // Lookup the name for an argument of the outer function. |
| 256 | if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
| 257 | == OK) |
| 258 | { |
| 259 | *gen_load_outer = TRUE; |
| 260 | return OK; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Lookup a variable in the current script. |
| 269 | * Returns OK or FAIL. |
| 270 | */ |
| 271 | static int |
| 272 | lookup_script(char_u *name, size_t len) |
| 273 | { |
| 274 | int cc; |
| 275 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 276 | dictitem_T *di; |
| 277 | |
| 278 | cc = name[len]; |
| 279 | name[len] = NUL; |
| 280 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 281 | name[len] = cc; |
| 282 | return di == NULL ? FAIL: OK; |
| 283 | } |
| 284 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 285 | /* |
| 286 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 287 | * compilation context "cctx". |
| 288 | * Return FAIL and give an error if it defined. |
| 289 | */ |
| 290 | int |
Bram Moolenaar | cbb6bdc | 2020-07-06 21:53:17 +0200 | [diff] [blame] | 291 | check_defined(char_u *p, size_t len, cctx_T *cctx) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 292 | { |
| 293 | if (lookup_script(p, len) == OK |
| 294 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 295 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 296 | || find_imported(p, len, cctx) != NULL))) |
| 297 | { |
| 298 | semsg("E1073: imported name already defined: %s", p); |
| 299 | return FAIL; |
| 300 | } |
| 301 | return OK; |
| 302 | } |
| 303 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 304 | /* |
| 305 | * Allocate memory for a type_T and add the pointer to type_gap, so that it can |
| 306 | * be freed later. |
| 307 | */ |
| 308 | static type_T * |
| 309 | alloc_type(garray_T *type_gap) |
| 310 | { |
| 311 | type_T *type; |
| 312 | |
| 313 | if (ga_grow(type_gap, 1) == FAIL) |
| 314 | return NULL; |
| 315 | type = ALLOC_CLEAR_ONE(type_T); |
| 316 | if (type != NULL) |
| 317 | { |
| 318 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type; |
| 319 | ++type_gap->ga_len; |
| 320 | } |
| 321 | return type; |
| 322 | } |
| 323 | |
Bram Moolenaar | 6110e79 | 2020-07-08 19:35:21 +0200 | [diff] [blame] | 324 | void |
| 325 | clear_type_list(garray_T *gap) |
| 326 | { |
| 327 | while (gap->ga_len > 0) |
| 328 | vim_free(((type_T **)gap->ga_data)[--gap->ga_len]); |
| 329 | ga_clear(gap); |
| 330 | } |
| 331 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 332 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 333 | get_list_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 334 | { |
| 335 | type_T *type; |
| 336 | |
| 337 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 338 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 339 | return &t_list_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 340 | if (member_type->tt_type == VAR_VOID |
| 341 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 342 | return &t_list_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 343 | if (member_type->tt_type == VAR_BOOL) |
| 344 | return &t_list_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 345 | if (member_type->tt_type == VAR_NUMBER) |
| 346 | return &t_list_number; |
| 347 | if (member_type->tt_type == VAR_STRING) |
| 348 | return &t_list_string; |
| 349 | |
| 350 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 351 | type = alloc_type(type_gap); |
| 352 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 353 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 354 | type->tt_type = VAR_LIST; |
| 355 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 356 | type->tt_argcount = 0; |
| 357 | type->tt_args = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 358 | return type; |
| 359 | } |
| 360 | |
| 361 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 362 | get_dict_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 363 | { |
| 364 | type_T *type; |
| 365 | |
| 366 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 367 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 368 | return &t_dict_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 369 | if (member_type->tt_type == VAR_VOID |
| 370 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 371 | return &t_dict_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 372 | if (member_type->tt_type == VAR_BOOL) |
| 373 | return &t_dict_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 374 | if (member_type->tt_type == VAR_NUMBER) |
| 375 | return &t_dict_number; |
| 376 | if (member_type->tt_type == VAR_STRING) |
| 377 | return &t_dict_string; |
| 378 | |
| 379 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 380 | type = alloc_type(type_gap); |
| 381 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 382 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 383 | type->tt_type = VAR_DICT; |
| 384 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 385 | type->tt_argcount = 0; |
| 386 | type->tt_args = NULL; |
| 387 | return type; |
| 388 | } |
| 389 | |
| 390 | /* |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 391 | * Allocate a new type for a function. |
| 392 | */ |
| 393 | static type_T * |
| 394 | alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 395 | { |
| 396 | type_T *type = alloc_type(type_gap); |
| 397 | |
| 398 | if (type == NULL) |
| 399 | return &t_any; |
| 400 | type->tt_type = VAR_FUNC; |
| 401 | type->tt_member = ret_type; |
| 402 | type->tt_argcount = argcount; |
| 403 | type->tt_args = NULL; |
| 404 | return type; |
| 405 | } |
| 406 | |
| 407 | /* |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 408 | * Get a function type, based on the return type "ret_type". |
| 409 | * If "argcount" is -1 or 0 a predefined type can be used. |
| 410 | * If "argcount" > 0 always create a new type, so that arguments can be added. |
| 411 | */ |
| 412 | static type_T * |
| 413 | get_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 414 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 415 | // recognize commonly used types |
| 416 | if (argcount <= 0) |
| 417 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 418 | if (ret_type == &t_unknown) |
| 419 | { |
| 420 | // (argcount == 0) is not possible |
| 421 | return &t_func_unknown; |
| 422 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 423 | if (ret_type == &t_void) |
| 424 | { |
| 425 | if (argcount == 0) |
| 426 | return &t_func_0_void; |
| 427 | else |
| 428 | return &t_func_void; |
| 429 | } |
| 430 | if (ret_type == &t_any) |
| 431 | { |
| 432 | if (argcount == 0) |
| 433 | return &t_func_0_any; |
| 434 | else |
| 435 | return &t_func_any; |
| 436 | } |
| 437 | if (ret_type == &t_number) |
| 438 | { |
| 439 | if (argcount == 0) |
| 440 | return &t_func_0_number; |
| 441 | else |
| 442 | return &t_func_number; |
| 443 | } |
| 444 | if (ret_type == &t_string) |
| 445 | { |
| 446 | if (argcount == 0) |
| 447 | return &t_func_0_string; |
| 448 | else |
| 449 | return &t_func_string; |
| 450 | } |
| 451 | } |
| 452 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 453 | return alloc_func_type(ret_type, argcount, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 456 | /* |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 457 | * For a function type, reserve space for "argcount" argument types (including |
| 458 | * vararg). |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 459 | */ |
| 460 | static int |
| 461 | func_type_add_arg_types( |
| 462 | type_T *functype, |
| 463 | int argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 464 | garray_T *type_gap) |
| 465 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 466 | // To make it easy to free the space needed for the argument types, add the |
| 467 | // pointer to type_gap. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 468 | if (ga_grow(type_gap, 1) == FAIL) |
| 469 | return FAIL; |
| 470 | functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); |
| 471 | if (functype->tt_args == NULL) |
| 472 | return FAIL; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 473 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = |
| 474 | (void *)functype->tt_args; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 475 | ++type_gap->ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 476 | return OK; |
| 477 | } |
| 478 | |
| 479 | /* |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 480 | * Return the type_T for a typval. Only for primitive types. |
| 481 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 482 | type_T * |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 483 | typval2type(typval_T *tv) |
| 484 | { |
| 485 | if (tv->v_type == VAR_NUMBER) |
| 486 | return &t_number; |
| 487 | if (tv->v_type == VAR_BOOL) |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 488 | return &t_bool; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 489 | if (tv->v_type == VAR_STRING) |
| 490 | return &t_string; |
| 491 | if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles |
| 492 | return &t_list_string; |
| 493 | if (tv->v_type == VAR_DICT) // e.g. for v:completed_item |
| 494 | return &t_dict_any; |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 495 | return &t_any; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 496 | } |
| 497 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 498 | static void |
| 499 | type_mismatch(type_T *expected, type_T *actual) |
| 500 | { |
| 501 | char *tofree1, *tofree2; |
| 502 | |
| 503 | semsg(_("E1013: type mismatch, expected %s but got %s"), |
| 504 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 505 | vim_free(tofree1); |
| 506 | vim_free(tofree2); |
| 507 | } |
| 508 | |
| 509 | static void |
| 510 | arg_type_mismatch(type_T *expected, type_T *actual, int argidx) |
| 511 | { |
| 512 | char *tofree1, *tofree2; |
| 513 | |
| 514 | semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"), |
| 515 | argidx, |
| 516 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 517 | vim_free(tofree1); |
| 518 | vim_free(tofree2); |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Check if the expected and actual types match. |
| 523 | * Does not allow for assigning "any" to a specific type. |
| 524 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 525 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 526 | check_type(type_T *expected, type_T *actual, int give_msg) |
| 527 | { |
| 528 | int ret = OK; |
| 529 | |
| 530 | // When expected is "unknown" we accept any actual type. |
| 531 | // When expected is "any" we accept any actual type except "void". |
| 532 | if (expected->tt_type != VAR_UNKNOWN |
| 533 | && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) |
| 534 | |
| 535 | { |
| 536 | if (expected->tt_type != actual->tt_type) |
| 537 | { |
| 538 | if (give_msg) |
| 539 | type_mismatch(expected, actual); |
| 540 | return FAIL; |
| 541 | } |
| 542 | if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) |
| 543 | { |
| 544 | // "unknown" is used for an empty list or dict |
| 545 | if (actual->tt_member != &t_unknown) |
| 546 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 547 | } |
| 548 | else if (expected->tt_type == VAR_FUNC) |
| 549 | { |
| 550 | if (expected->tt_member != &t_unknown) |
| 551 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 552 | if (ret == OK && expected->tt_argcount != -1 |
| 553 | && (actual->tt_argcount < expected->tt_min_argcount |
| 554 | || actual->tt_argcount > expected->tt_argcount)) |
| 555 | ret = FAIL; |
| 556 | } |
| 557 | if (ret == FAIL && give_msg) |
| 558 | type_mismatch(expected, actual); |
| 559 | } |
| 560 | return ret; |
| 561 | } |
| 562 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 563 | /* |
| 564 | * Return FAIl if "expected" and "actual" don't match. |
| 565 | * TODO: better type comparison |
| 566 | */ |
| 567 | int |
| 568 | check_argtype(type_T *expected, typval_T *actual_tv) |
| 569 | { |
| 570 | type_T actual; |
| 571 | type_T member; |
| 572 | |
| 573 | // TODO: should should be done with more levels |
| 574 | CLEAR_FIELD(actual); |
| 575 | actual.tt_type = actual_tv->v_type; |
| 576 | if (actual_tv->v_type == VAR_LIST |
| 577 | && actual_tv->vval.v_list != NULL |
| 578 | && actual_tv->vval.v_list->lv_first != NULL) |
| 579 | { |
| 580 | // Use the type of the first member, it is the most specific. |
| 581 | CLEAR_FIELD(member); |
| 582 | member.tt_type = actual_tv->vval.v_list->lv_first->li_tv.v_type; |
| 583 | member.tt_member = &t_any; |
| 584 | actual.tt_member = &member; |
| 585 | } |
| 586 | else if (actual_tv->v_type == VAR_DICT |
| 587 | && actual_tv->vval.v_dict != NULL |
| 588 | && actual_tv->vval.v_dict->dv_hashtab.ht_used > 0) |
| 589 | { |
| 590 | dict_iterator_T iter; |
| 591 | typval_T *value; |
| 592 | |
| 593 | // Use the type of the first value, it is the most specific. |
| 594 | dict_iterate_start(actual_tv, &iter); |
| 595 | dict_iterate_next(&iter, &value); |
| 596 | CLEAR_FIELD(member); |
| 597 | member.tt_type = value->v_type; |
| 598 | member.tt_member = &t_any; |
| 599 | actual.tt_member = &member; |
| 600 | } |
| 601 | else |
| 602 | actual.tt_member = &t_any; |
| 603 | return check_type(expected, &actual, TRUE); |
| 604 | } |
| 605 | |
| 606 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 607 | ///////////////////////////////////////////////////////////////////// |
| 608 | // Following generate_ functions expect the caller to call ga_grow(). |
| 609 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 610 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 611 | #define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 612 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 613 | /* |
| 614 | * Generate an instruction without arguments. |
| 615 | * Returns a pointer to the new instruction, NULL if failed. |
| 616 | */ |
| 617 | static isn_T * |
| 618 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 619 | { |
| 620 | garray_T *instr = &cctx->ctx_instr; |
| 621 | isn_T *isn; |
| 622 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 623 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 624 | if (ga_grow(instr, 1) == FAIL) |
| 625 | return NULL; |
| 626 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 627 | isn->isn_type = isn_type; |
| 628 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 629 | ++instr->ga_len; |
| 630 | |
| 631 | return isn; |
| 632 | } |
| 633 | |
| 634 | /* |
| 635 | * Generate an instruction without arguments. |
| 636 | * "drop" will be removed from the stack. |
| 637 | * Returns a pointer to the new instruction, NULL if failed. |
| 638 | */ |
| 639 | static isn_T * |
| 640 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 641 | { |
| 642 | garray_T *stack = &cctx->ctx_type_stack; |
| 643 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 644 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 645 | stack->ga_len -= drop; |
| 646 | return generate_instr(cctx, isn_type); |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 651 | */ |
| 652 | static isn_T * |
| 653 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 654 | { |
| 655 | isn_T *isn; |
| 656 | garray_T *stack = &cctx->ctx_type_stack; |
| 657 | |
| 658 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 659 | return NULL; |
| 660 | |
| 661 | if (ga_grow(stack, 1) == FAIL) |
| 662 | return NULL; |
| 663 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 664 | ++stack->ga_len; |
| 665 | |
| 666 | return isn; |
| 667 | } |
| 668 | |
| 669 | /* |
| 670 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 671 | */ |
| 672 | static int |
| 673 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 674 | { |
| 675 | isn_T *isn; |
| 676 | garray_T *stack = &cctx->ctx_type_stack; |
| 677 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 678 | |
| 679 | if ((*type)->tt_type == VAR_STRING) |
| 680 | return OK; |
| 681 | *type = &t_string; |
| 682 | |
| 683 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 684 | return FAIL; |
| 685 | isn->isn_arg.number = offset; |
| 686 | |
| 687 | return OK; |
| 688 | } |
| 689 | |
| 690 | static int |
| 691 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 692 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 693 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 694 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 695 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 696 | { |
| 697 | if (*op == '+') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 698 | emsg(_("E1051: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 699 | else |
| 700 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 701 | return FAIL; |
| 702 | } |
| 703 | return OK; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | * Generate an instruction with two arguments. The instruction depends on the |
| 708 | * type of the arguments. |
| 709 | */ |
| 710 | static int |
| 711 | generate_two_op(cctx_T *cctx, char_u *op) |
| 712 | { |
| 713 | garray_T *stack = &cctx->ctx_type_stack; |
| 714 | type_T *type1; |
| 715 | type_T *type2; |
| 716 | vartype_T vartype; |
| 717 | isn_T *isn; |
| 718 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 719 | RETURN_OK_IF_SKIP(cctx); |
| 720 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 721 | // Get the known type of the two items on the stack. If they are matching |
| 722 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 723 | // checking. |
| 724 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 725 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 726 | vartype = VAR_ANY; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 727 | if (type1->tt_type == type2->tt_type |
| 728 | && (type1->tt_type == VAR_NUMBER |
| 729 | || type1->tt_type == VAR_LIST |
| 730 | #ifdef FEAT_FLOAT |
| 731 | || type1->tt_type == VAR_FLOAT |
| 732 | #endif |
| 733 | || type1->tt_type == VAR_BLOB)) |
| 734 | vartype = type1->tt_type; |
| 735 | |
| 736 | switch (*op) |
| 737 | { |
| 738 | case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 739 | && type1->tt_type != VAR_ANY |
| 740 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 741 | && check_number_or_float( |
| 742 | type1->tt_type, type2->tt_type, op) == FAIL) |
| 743 | return FAIL; |
| 744 | isn = generate_instr_drop(cctx, |
| 745 | vartype == VAR_NUMBER ? ISN_OPNR |
| 746 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 747 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 748 | #ifdef FEAT_FLOAT |
| 749 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 750 | #endif |
| 751 | : ISN_OPANY, 1); |
| 752 | if (isn != NULL) |
| 753 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 754 | break; |
| 755 | |
| 756 | case '-': |
| 757 | case '*': |
| 758 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 759 | op) == FAIL) |
| 760 | return FAIL; |
| 761 | if (vartype == VAR_NUMBER) |
| 762 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 763 | #ifdef FEAT_FLOAT |
| 764 | else if (vartype == VAR_FLOAT) |
| 765 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 766 | #endif |
| 767 | else |
| 768 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 769 | if (isn != NULL) |
| 770 | isn->isn_arg.op.op_type = *op == '*' |
| 771 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 772 | break; |
| 773 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 774 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 775 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 776 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 777 | && type2->tt_type != VAR_NUMBER)) |
| 778 | { |
| 779 | emsg(_("E1035: % requires number arguments")); |
| 780 | return FAIL; |
| 781 | } |
| 782 | isn = generate_instr_drop(cctx, |
| 783 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 784 | if (isn != NULL) |
| 785 | isn->isn_arg.op.op_type = EXPR_REM; |
| 786 | break; |
| 787 | } |
| 788 | |
| 789 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 790 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 791 | { |
| 792 | type_T *type = &t_any; |
| 793 | |
| 794 | #ifdef FEAT_FLOAT |
| 795 | // float+number and number+float results in float |
| 796 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 797 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 798 | type = &t_float; |
| 799 | #endif |
| 800 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 801 | } |
| 802 | |
| 803 | return OK; |
| 804 | } |
| 805 | |
| 806 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 807 | * Get the instruction to use for comparing "type1" with "type2" |
| 808 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 809 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 810 | static isntype_T |
| 811 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 812 | { |
| 813 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 814 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 815 | if (type1 == VAR_UNKNOWN) |
| 816 | type1 = VAR_ANY; |
| 817 | if (type2 == VAR_UNKNOWN) |
| 818 | type2 = VAR_ANY; |
| 819 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 820 | if (type1 == type2) |
| 821 | { |
| 822 | switch (type1) |
| 823 | { |
| 824 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 825 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 826 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 827 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 828 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 829 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 830 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 831 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 832 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 833 | default: isntype = ISN_COMPAREANY; break; |
| 834 | } |
| 835 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 836 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 837 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 838 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 839 | isntype = ISN_COMPAREANY; |
| 840 | |
| 841 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 842 | && (isntype == ISN_COMPAREBOOL |
| 843 | || isntype == ISN_COMPARESPECIAL |
| 844 | || isntype == ISN_COMPARENR |
| 845 | || isntype == ISN_COMPAREFLOAT)) |
| 846 | { |
| 847 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 848 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 849 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 850 | } |
| 851 | if (isntype == ISN_DROP |
| 852 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 853 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 854 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 855 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 856 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 857 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 858 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 859 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 860 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 861 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 862 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 863 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 864 | return isntype; |
| 865 | } |
| 866 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 867 | int |
| 868 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 869 | { |
| 870 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 871 | return FAIL; |
| 872 | return OK; |
| 873 | } |
| 874 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 875 | /* |
| 876 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 877 | */ |
| 878 | static int |
| 879 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 880 | { |
| 881 | isntype_T isntype; |
| 882 | isn_T *isn; |
| 883 | garray_T *stack = &cctx->ctx_type_stack; |
| 884 | vartype_T type1; |
| 885 | vartype_T type2; |
| 886 | |
| 887 | RETURN_OK_IF_SKIP(cctx); |
| 888 | |
| 889 | // Get the known type of the two items on the stack. If they are matching |
| 890 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 891 | // checking. |
| 892 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 893 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 894 | isntype = get_compare_isn(exptype, type1, type2); |
| 895 | if (isntype == ISN_DROP) |
| 896 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 897 | |
| 898 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 899 | return FAIL; |
| 900 | isn->isn_arg.op.op_type = exptype; |
| 901 | isn->isn_arg.op.op_ic = ic; |
| 902 | |
| 903 | // takes two arguments, puts one bool back |
| 904 | if (stack->ga_len >= 2) |
| 905 | { |
| 906 | --stack->ga_len; |
| 907 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 908 | } |
| 909 | |
| 910 | return OK; |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | * Generate an ISN_2BOOL instruction. |
| 915 | */ |
| 916 | static int |
| 917 | generate_2BOOL(cctx_T *cctx, int invert) |
| 918 | { |
| 919 | isn_T *isn; |
| 920 | garray_T *stack = &cctx->ctx_type_stack; |
| 921 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 922 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 923 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 924 | return FAIL; |
| 925 | isn->isn_arg.number = invert; |
| 926 | |
| 927 | // type becomes bool |
| 928 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 929 | |
| 930 | return OK; |
| 931 | } |
| 932 | |
| 933 | static int |
| 934 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 935 | { |
| 936 | isn_T *isn; |
| 937 | garray_T *stack = &cctx->ctx_type_stack; |
| 938 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 939 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 940 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 941 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 942 | // TODO: whole type, e.g. for a function also arg and return types |
| 943 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 944 | isn->isn_arg.type.ct_off = offset; |
| 945 | |
| 946 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 947 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 948 | |
| 949 | return OK; |
| 950 | } |
| 951 | |
| 952 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 953 | * Check that |
| 954 | * - "actual" is "expected" type or |
| 955 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 956 | * - return FAIL. |
| 957 | */ |
| 958 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 959 | need_type( |
| 960 | type_T *actual, |
| 961 | type_T *expected, |
| 962 | int offset, |
| 963 | cctx_T *cctx, |
| 964 | int silent) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 965 | { |
| 966 | if (check_type(expected, actual, FALSE) == OK) |
| 967 | return OK; |
| 968 | if (actual->tt_type != VAR_ANY |
| 969 | && actual->tt_type != VAR_UNKNOWN |
| 970 | && !(actual->tt_type == VAR_FUNC |
| 971 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 972 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 973 | if (!silent) |
| 974 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 975 | return FAIL; |
| 976 | } |
| 977 | generate_TYPECHECK(cctx, expected, offset); |
| 978 | return OK; |
| 979 | } |
| 980 | |
| 981 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 982 | * Generate an ISN_PUSHNR instruction. |
| 983 | */ |
| 984 | static int |
| 985 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 986 | { |
| 987 | isn_T *isn; |
| 988 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 989 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 990 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 991 | return FAIL; |
| 992 | isn->isn_arg.number = number; |
| 993 | |
| 994 | return OK; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * Generate an ISN_PUSHBOOL instruction. |
| 999 | */ |
| 1000 | static int |
| 1001 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 1002 | { |
| 1003 | isn_T *isn; |
| 1004 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1005 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1006 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 1007 | return FAIL; |
| 1008 | isn->isn_arg.number = number; |
| 1009 | |
| 1010 | return OK; |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * Generate an ISN_PUSHSPEC instruction. |
| 1015 | */ |
| 1016 | static int |
| 1017 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 1018 | { |
| 1019 | isn_T *isn; |
| 1020 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1021 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1022 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 1023 | return FAIL; |
| 1024 | isn->isn_arg.number = number; |
| 1025 | |
| 1026 | return OK; |
| 1027 | } |
| 1028 | |
| 1029 | #ifdef FEAT_FLOAT |
| 1030 | /* |
| 1031 | * Generate an ISN_PUSHF instruction. |
| 1032 | */ |
| 1033 | static int |
| 1034 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 1035 | { |
| 1036 | isn_T *isn; |
| 1037 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1038 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1039 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 1040 | return FAIL; |
| 1041 | isn->isn_arg.fnumber = fnumber; |
| 1042 | |
| 1043 | return OK; |
| 1044 | } |
| 1045 | #endif |
| 1046 | |
| 1047 | /* |
| 1048 | * Generate an ISN_PUSHS instruction. |
| 1049 | * Consumes "str". |
| 1050 | */ |
| 1051 | static int |
| 1052 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 1053 | { |
| 1054 | isn_T *isn; |
| 1055 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1056 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1057 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 1058 | return FAIL; |
| 1059 | isn->isn_arg.string = str; |
| 1060 | |
| 1061 | return OK; |
| 1062 | } |
| 1063 | |
| 1064 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1065 | * Generate an ISN_PUSHCHANNEL instruction. |
| 1066 | * Consumes "channel". |
| 1067 | */ |
| 1068 | static int |
| 1069 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 1070 | { |
| 1071 | isn_T *isn; |
| 1072 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1073 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1074 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 1075 | return FAIL; |
| 1076 | isn->isn_arg.channel = channel; |
| 1077 | |
| 1078 | return OK; |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * Generate an ISN_PUSHJOB instruction. |
| 1083 | * Consumes "job". |
| 1084 | */ |
| 1085 | static int |
| 1086 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 1087 | { |
| 1088 | isn_T *isn; |
| 1089 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1090 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1091 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1092 | return FAIL; |
| 1093 | isn->isn_arg.job = job; |
| 1094 | |
| 1095 | return OK; |
| 1096 | } |
| 1097 | |
| 1098 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1099 | * Generate an ISN_PUSHBLOB instruction. |
| 1100 | * Consumes "blob". |
| 1101 | */ |
| 1102 | static int |
| 1103 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1104 | { |
| 1105 | isn_T *isn; |
| 1106 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1107 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1108 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1109 | return FAIL; |
| 1110 | isn->isn_arg.blob = blob; |
| 1111 | |
| 1112 | return OK; |
| 1113 | } |
| 1114 | |
| 1115 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1116 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1117 | * Consumes "name". |
| 1118 | */ |
| 1119 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1120 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1121 | { |
| 1122 | isn_T *isn; |
| 1123 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1124 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1125 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1126 | return FAIL; |
| 1127 | isn->isn_arg.string = name; |
| 1128 | |
| 1129 | return OK; |
| 1130 | } |
| 1131 | |
| 1132 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1133 | * Generate an ISN_GETITEM instruction with "index". |
| 1134 | */ |
| 1135 | static int |
| 1136 | generate_GETITEM(cctx_T *cctx, int index) |
| 1137 | { |
| 1138 | isn_T *isn; |
| 1139 | garray_T *stack = &cctx->ctx_type_stack; |
| 1140 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1141 | type_T *item_type = &t_any; |
| 1142 | |
| 1143 | RETURN_OK_IF_SKIP(cctx); |
| 1144 | |
| 1145 | if (type->tt_type == VAR_LIST) |
| 1146 | item_type = type->tt_member; |
| 1147 | else if (type->tt_type != VAR_ANY) |
| 1148 | { |
| 1149 | emsg(_(e_listreq)); |
| 1150 | return FAIL; |
| 1151 | } |
| 1152 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1153 | return FAIL; |
| 1154 | isn->isn_arg.number = index; |
| 1155 | |
| 1156 | // add the item type to the type stack |
| 1157 | if (ga_grow(stack, 1) == FAIL) |
| 1158 | return FAIL; |
| 1159 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 1160 | ++stack->ga_len; |
| 1161 | return OK; |
| 1162 | } |
| 1163 | |
| 1164 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 1165 | * Generate an ISN_SLICE instruction with "count". |
| 1166 | */ |
| 1167 | static int |
| 1168 | generate_SLICE(cctx_T *cctx, int count) |
| 1169 | { |
| 1170 | isn_T *isn; |
| 1171 | |
| 1172 | RETURN_OK_IF_SKIP(cctx); |
| 1173 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1174 | return FAIL; |
| 1175 | isn->isn_arg.number = count; |
| 1176 | return OK; |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1181 | */ |
| 1182 | static int |
| 1183 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1184 | { |
| 1185 | isn_T *isn; |
| 1186 | |
| 1187 | RETURN_OK_IF_SKIP(cctx); |
| 1188 | |
| 1189 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1190 | return FAIL; |
| 1191 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1192 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1193 | |
| 1194 | return OK; |
| 1195 | } |
| 1196 | |
| 1197 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1198 | * Generate an ISN_STORE instruction. |
| 1199 | */ |
| 1200 | static int |
| 1201 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1202 | { |
| 1203 | isn_T *isn; |
| 1204 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1205 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1206 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1207 | return FAIL; |
| 1208 | if (name != NULL) |
| 1209 | isn->isn_arg.string = vim_strsave(name); |
| 1210 | else |
| 1211 | isn->isn_arg.number = idx; |
| 1212 | |
| 1213 | return OK; |
| 1214 | } |
| 1215 | |
| 1216 | /* |
| 1217 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1218 | */ |
| 1219 | static int |
| 1220 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1221 | { |
| 1222 | isn_T *isn; |
| 1223 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1224 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1225 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1226 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1227 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1228 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1229 | |
| 1230 | return OK; |
| 1231 | } |
| 1232 | |
| 1233 | /* |
| 1234 | * Generate an ISN_STOREOPT instruction |
| 1235 | */ |
| 1236 | static int |
| 1237 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1238 | { |
| 1239 | isn_T *isn; |
| 1240 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1241 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1242 | if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL) |
| 1243 | return FAIL; |
| 1244 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1245 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1246 | |
| 1247 | return OK; |
| 1248 | } |
| 1249 | |
| 1250 | /* |
| 1251 | * Generate an ISN_LOAD or similar instruction. |
| 1252 | */ |
| 1253 | static int |
| 1254 | generate_LOAD( |
| 1255 | cctx_T *cctx, |
| 1256 | isntype_T isn_type, |
| 1257 | int idx, |
| 1258 | char_u *name, |
| 1259 | type_T *type) |
| 1260 | { |
| 1261 | isn_T *isn; |
| 1262 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1263 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1264 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1265 | return FAIL; |
| 1266 | if (name != NULL) |
| 1267 | isn->isn_arg.string = vim_strsave(name); |
| 1268 | else |
| 1269 | isn->isn_arg.number = idx; |
| 1270 | |
| 1271 | return OK; |
| 1272 | } |
| 1273 | |
| 1274 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1275 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1276 | */ |
| 1277 | static int |
| 1278 | generate_LOADV( |
| 1279 | cctx_T *cctx, |
| 1280 | char_u *name, |
| 1281 | int error) |
| 1282 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1283 | int di_flags; |
| 1284 | int vidx = find_vim_var(name, &di_flags); |
| 1285 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1286 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1287 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1288 | if (vidx < 0) |
| 1289 | { |
| 1290 | if (error) |
| 1291 | semsg(_(e_var_notfound), name); |
| 1292 | return FAIL; |
| 1293 | } |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1294 | type = typval2type(get_vim_var_tv(vidx)); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1295 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1296 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1300 | * Generate an ISN_UNLET instruction. |
| 1301 | */ |
| 1302 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1303 | generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1304 | { |
| 1305 | isn_T *isn; |
| 1306 | |
| 1307 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1308 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1309 | return FAIL; |
| 1310 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1311 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1312 | |
| 1313 | return OK; |
| 1314 | } |
| 1315 | |
| 1316 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1317 | * Generate an ISN_LOADS instruction. |
| 1318 | */ |
| 1319 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1320 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1321 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1322 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1323 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1324 | int sid, |
| 1325 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1326 | { |
| 1327 | isn_T *isn; |
| 1328 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1329 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1330 | if (isn_type == ISN_LOADS) |
| 1331 | isn = generate_instr_type(cctx, isn_type, type); |
| 1332 | else |
| 1333 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1334 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1335 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1336 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1337 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1338 | |
| 1339 | return OK; |
| 1340 | } |
| 1341 | |
| 1342 | /* |
| 1343 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1344 | */ |
| 1345 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1346 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1347 | cctx_T *cctx, |
| 1348 | isntype_T isn_type, |
| 1349 | int sid, |
| 1350 | int idx, |
| 1351 | type_T *type) |
| 1352 | { |
| 1353 | isn_T *isn; |
| 1354 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1355 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1356 | if (isn_type == ISN_LOADSCRIPT) |
| 1357 | isn = generate_instr_type(cctx, isn_type, type); |
| 1358 | else |
| 1359 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1360 | if (isn == NULL) |
| 1361 | return FAIL; |
| 1362 | isn->isn_arg.script.script_sid = sid; |
| 1363 | isn->isn_arg.script.script_idx = idx; |
| 1364 | return OK; |
| 1365 | } |
| 1366 | |
| 1367 | /* |
| 1368 | * Generate an ISN_NEWLIST instruction. |
| 1369 | */ |
| 1370 | static int |
| 1371 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1372 | { |
| 1373 | isn_T *isn; |
| 1374 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1375 | type_T *type; |
| 1376 | type_T *member; |
| 1377 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1378 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1379 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1380 | return FAIL; |
| 1381 | isn->isn_arg.number = count; |
| 1382 | |
| 1383 | // drop the value types |
| 1384 | stack->ga_len -= count; |
| 1385 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1386 | // 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] | 1387 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1388 | if (count > 0) |
| 1389 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1390 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1391 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1392 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1393 | |
| 1394 | // add the list type to the type stack |
| 1395 | if (ga_grow(stack, 1) == FAIL) |
| 1396 | return FAIL; |
| 1397 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1398 | ++stack->ga_len; |
| 1399 | |
| 1400 | return OK; |
| 1401 | } |
| 1402 | |
| 1403 | /* |
| 1404 | * Generate an ISN_NEWDICT instruction. |
| 1405 | */ |
| 1406 | static int |
| 1407 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1408 | { |
| 1409 | isn_T *isn; |
| 1410 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1411 | type_T *type; |
| 1412 | type_T *member; |
| 1413 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1414 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1415 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1416 | return FAIL; |
| 1417 | isn->isn_arg.number = count; |
| 1418 | |
| 1419 | // drop the key and value types |
| 1420 | stack->ga_len -= 2 * count; |
| 1421 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1422 | // Use the first value type for the list member type. Use "void" for an |
| 1423 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1424 | if (count > 0) |
| 1425 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1426 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1427 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1428 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1429 | |
| 1430 | // add the dict type to the type stack |
| 1431 | if (ga_grow(stack, 1) == FAIL) |
| 1432 | return FAIL; |
| 1433 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1434 | ++stack->ga_len; |
| 1435 | |
| 1436 | return OK; |
| 1437 | } |
| 1438 | |
| 1439 | /* |
| 1440 | * Generate an ISN_FUNCREF instruction. |
| 1441 | */ |
| 1442 | static int |
| 1443 | generate_FUNCREF(cctx_T *cctx, int dfunc_idx) |
| 1444 | { |
| 1445 | isn_T *isn; |
| 1446 | garray_T *stack = &cctx->ctx_type_stack; |
| 1447 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1448 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1449 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1450 | return FAIL; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1451 | isn->isn_arg.funcref.fr_func = dfunc_idx; |
| 1452 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1453 | |
| 1454 | if (ga_grow(stack, 1) == FAIL) |
| 1455 | return FAIL; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1456 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1457 | // TODO: argument and return types |
| 1458 | ++stack->ga_len; |
| 1459 | |
| 1460 | return OK; |
| 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | * Generate an ISN_JUMP instruction. |
| 1465 | */ |
| 1466 | static int |
| 1467 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1468 | { |
| 1469 | isn_T *isn; |
| 1470 | garray_T *stack = &cctx->ctx_type_stack; |
| 1471 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1472 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1473 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1474 | return FAIL; |
| 1475 | isn->isn_arg.jump.jump_when = when; |
| 1476 | isn->isn_arg.jump.jump_where = where; |
| 1477 | |
| 1478 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1479 | --stack->ga_len; |
| 1480 | |
| 1481 | return OK; |
| 1482 | } |
| 1483 | |
| 1484 | static int |
| 1485 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1486 | { |
| 1487 | isn_T *isn; |
| 1488 | garray_T *stack = &cctx->ctx_type_stack; |
| 1489 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1490 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1491 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1492 | return FAIL; |
| 1493 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1494 | |
| 1495 | if (ga_grow(stack, 1) == FAIL) |
| 1496 | return FAIL; |
| 1497 | // type doesn't matter, will be stored next |
| 1498 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1499 | ++stack->ga_len; |
| 1500 | |
| 1501 | return OK; |
| 1502 | } |
| 1503 | |
| 1504 | /* |
| 1505 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1506 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1507 | * Return FAIL if the number of arguments is wrong. |
| 1508 | */ |
| 1509 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1510 | generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1511 | { |
| 1512 | isn_T *isn; |
| 1513 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1514 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1515 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1516 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1517 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1518 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1519 | argoff = check_internal_func(func_idx, argcount); |
| 1520 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1521 | return FAIL; |
| 1522 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1523 | if (method_call && argoff > 1) |
| 1524 | { |
| 1525 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1526 | return FAIL; |
| 1527 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1528 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1529 | } |
| 1530 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1531 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1532 | return FAIL; |
| 1533 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1534 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1535 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1536 | for (i = 0; i < argcount; ++i) |
| 1537 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1538 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1539 | stack->ga_len -= argcount; // drop the arguments |
| 1540 | if (ga_grow(stack, 1) == FAIL) |
| 1541 | return FAIL; |
| 1542 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1543 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1544 | ++stack->ga_len; // add return value |
| 1545 | |
| 1546 | return OK; |
| 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1551 | * Return FAIL if the number of arguments is wrong. |
| 1552 | */ |
| 1553 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1554 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1555 | { |
| 1556 | isn_T *isn; |
| 1557 | garray_T *stack = &cctx->ctx_type_stack; |
| 1558 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1559 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1560 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1561 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1562 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1563 | { |
| 1564 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1565 | return FAIL; |
| 1566 | } |
| 1567 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1568 | { |
| 1569 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1570 | return FAIL; |
| 1571 | } |
| 1572 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1573 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1574 | { |
| 1575 | int i; |
| 1576 | |
| 1577 | for (i = 0; i < argcount; ++i) |
| 1578 | { |
| 1579 | type_T *expected; |
| 1580 | type_T *actual; |
| 1581 | |
| 1582 | if (i < regular_args) |
| 1583 | { |
| 1584 | if (ufunc->uf_arg_types == NULL) |
| 1585 | continue; |
| 1586 | expected = ufunc->uf_arg_types[i]; |
| 1587 | } |
| 1588 | else |
| 1589 | expected = ufunc->uf_va_type->tt_member; |
| 1590 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1591 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1592 | { |
| 1593 | arg_type_mismatch(expected, actual, i + 1); |
| 1594 | return FAIL; |
| 1595 | } |
| 1596 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1597 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 1598 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1599 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1600 | } |
| 1601 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1602 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1603 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1604 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1605 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1606 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1607 | { |
| 1608 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1609 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1610 | } |
| 1611 | else |
| 1612 | { |
| 1613 | // A user function may be deleted and redefined later, can't use the |
| 1614 | // ufunc pointer, need to look it up again at runtime. |
| 1615 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1616 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1617 | } |
| 1618 | |
| 1619 | stack->ga_len -= argcount; // drop the arguments |
| 1620 | if (ga_grow(stack, 1) == FAIL) |
| 1621 | return FAIL; |
| 1622 | // add return value |
| 1623 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1624 | ++stack->ga_len; |
| 1625 | |
| 1626 | return OK; |
| 1627 | } |
| 1628 | |
| 1629 | /* |
| 1630 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1631 | */ |
| 1632 | static int |
| 1633 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1634 | { |
| 1635 | isn_T *isn; |
| 1636 | garray_T *stack = &cctx->ctx_type_stack; |
| 1637 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1638 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1639 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1640 | return FAIL; |
| 1641 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1642 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1643 | |
| 1644 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1645 | if (ga_grow(stack, 1) == FAIL) |
| 1646 | return FAIL; |
| 1647 | // add return value |
| 1648 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1649 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1650 | |
| 1651 | return OK; |
| 1652 | } |
| 1653 | |
| 1654 | /* |
| 1655 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1656 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1657 | */ |
| 1658 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1659 | generate_PCALL( |
| 1660 | cctx_T *cctx, |
| 1661 | int argcount, |
| 1662 | char_u *name, |
| 1663 | type_T *type, |
| 1664 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1665 | { |
| 1666 | isn_T *isn; |
| 1667 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1668 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1669 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1670 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1671 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1672 | if (type->tt_type == VAR_ANY) |
| 1673 | ret_type = &t_any; |
| 1674 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1675 | { |
| 1676 | if (type->tt_argcount != -1) |
| 1677 | { |
| 1678 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1679 | |
| 1680 | if (argcount < type->tt_min_argcount - varargs) |
| 1681 | { |
| 1682 | semsg(_(e_toofewarg), "[reference]"); |
| 1683 | return FAIL; |
| 1684 | } |
| 1685 | if (!varargs && argcount > type->tt_argcount) |
| 1686 | { |
| 1687 | semsg(_(e_toomanyarg), "[reference]"); |
| 1688 | return FAIL; |
| 1689 | } |
| 1690 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1691 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1692 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1693 | else |
| 1694 | { |
| 1695 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1696 | return FAIL; |
| 1697 | } |
| 1698 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1699 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1700 | return FAIL; |
| 1701 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1702 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1703 | |
| 1704 | stack->ga_len -= argcount; // drop the arguments |
| 1705 | |
| 1706 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1707 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1708 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1709 | // If partial is above the arguments it must be cleared and replaced with |
| 1710 | // the return value. |
| 1711 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1712 | return FAIL; |
| 1713 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1714 | return OK; |
| 1715 | } |
| 1716 | |
| 1717 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1718 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1719 | */ |
| 1720 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1721 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1722 | { |
| 1723 | isn_T *isn; |
| 1724 | garray_T *stack = &cctx->ctx_type_stack; |
| 1725 | type_T *type; |
| 1726 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1727 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1728 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1729 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1730 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1731 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1732 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1733 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1734 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1735 | { |
| 1736 | emsg(_(e_dictreq)); |
| 1737 | return FAIL; |
| 1738 | } |
| 1739 | // change dict type to dict member type |
| 1740 | if (type->tt_type == VAR_DICT) |
| 1741 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1742 | |
| 1743 | return OK; |
| 1744 | } |
| 1745 | |
| 1746 | /* |
| 1747 | * Generate an ISN_ECHO instruction. |
| 1748 | */ |
| 1749 | static int |
| 1750 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1751 | { |
| 1752 | isn_T *isn; |
| 1753 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1754 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1755 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1756 | return FAIL; |
| 1757 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1758 | isn->isn_arg.echo.echo_count = count; |
| 1759 | |
| 1760 | return OK; |
| 1761 | } |
| 1762 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1763 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1764 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1765 | */ |
| 1766 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1767 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1768 | { |
| 1769 | isn_T *isn; |
| 1770 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1771 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1772 | return FAIL; |
| 1773 | isn->isn_arg.number = count; |
| 1774 | |
| 1775 | return OK; |
| 1776 | } |
| 1777 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1778 | static int |
| 1779 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1780 | { |
| 1781 | isn_T *isn; |
| 1782 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1783 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1784 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1785 | return FAIL; |
| 1786 | isn->isn_arg.string = vim_strsave(line); |
| 1787 | return OK; |
| 1788 | } |
| 1789 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1790 | static int |
| 1791 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1792 | { |
| 1793 | isn_T *isn; |
| 1794 | |
| 1795 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1796 | return FAIL; |
| 1797 | isn->isn_arg.number = count; |
| 1798 | return OK; |
| 1799 | } |
| 1800 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1801 | /* |
| 1802 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1803 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1804 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1805 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1806 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1807 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1808 | lvar_T *lvar; |
| 1809 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1810 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1811 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1812 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1813 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1817 | return NULL; |
| 1818 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1819 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1820 | // Every local variable uses the next entry on the stack. We could re-use |
| 1821 | // the last ones when leaving a scope, but then variables used in a closure |
| 1822 | // might get overwritten. To keep things simple do not re-use stack |
| 1823 | // entries. This is less efficient, but memory is cheap these days. |
| 1824 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1825 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1826 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1827 | lvar->lv_const = isConst; |
| 1828 | lvar->lv_type = type; |
| 1829 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1830 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1834 | * Remove local variables above "new_top". |
| 1835 | */ |
| 1836 | static void |
| 1837 | unwind_locals(cctx_T *cctx, int new_top) |
| 1838 | { |
| 1839 | if (cctx->ctx_locals.ga_len > new_top) |
| 1840 | { |
| 1841 | int idx; |
| 1842 | lvar_T *lvar; |
| 1843 | |
| 1844 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1845 | { |
| 1846 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1847 | vim_free(lvar->lv_name); |
| 1848 | } |
| 1849 | } |
| 1850 | cctx->ctx_locals.ga_len = new_top; |
| 1851 | } |
| 1852 | |
| 1853 | /* |
| 1854 | * Free all local variables. |
| 1855 | */ |
| 1856 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1857 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1858 | { |
| 1859 | unwind_locals(cctx, 0); |
| 1860 | ga_clear(&cctx->ctx_locals); |
| 1861 | } |
| 1862 | |
| 1863 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1864 | * Skip over a type definition and return a pointer to just after it. |
| 1865 | */ |
| 1866 | char_u * |
| 1867 | skip_type(char_u *start) |
| 1868 | { |
| 1869 | char_u *p = start; |
| 1870 | |
| 1871 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1872 | ++p; |
| 1873 | |
| 1874 | // Skip over "<type>"; this is permissive about white space. |
| 1875 | if (*skipwhite(p) == '<') |
| 1876 | { |
| 1877 | p = skipwhite(p); |
| 1878 | p = skip_type(skipwhite(p + 1)); |
| 1879 | p = skipwhite(p); |
| 1880 | if (*p == '>') |
| 1881 | ++p; |
| 1882 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1883 | else if (*p == '(' && STRNCMP("func", start, 4) == 0) |
| 1884 | { |
| 1885 | // handle func(args): type |
| 1886 | ++p; |
| 1887 | while (*p != ')' && *p != NUL) |
| 1888 | { |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1889 | char_u *sp = p; |
| 1890 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1891 | p = skip_type(p); |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1892 | if (p == sp) |
| 1893 | return p; // syntax error |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1894 | if (*p == ',') |
| 1895 | p = skipwhite(p + 1); |
| 1896 | } |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1897 | if (*p == ')') |
| 1898 | { |
| 1899 | if (p[1] == ':') |
| 1900 | p = skip_type(skipwhite(p + 2)); |
| 1901 | else |
| 1902 | p = skipwhite(p + 1); |
| 1903 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1904 | } |
| 1905 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1906 | return p; |
| 1907 | } |
| 1908 | |
| 1909 | /* |
| 1910 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1911 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1912 | * Returns NULL in case of failure. |
| 1913 | */ |
| 1914 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1915 | 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] | 1916 | { |
| 1917 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1918 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1919 | |
| 1920 | if (**arg != '<') |
| 1921 | { |
| 1922 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1923 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1924 | else |
| 1925 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1926 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1927 | } |
| 1928 | *arg = skipwhite(*arg + 1); |
| 1929 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1930 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1931 | |
| 1932 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1933 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1934 | { |
| 1935 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1936 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1937 | } |
| 1938 | ++*arg; |
| 1939 | |
| 1940 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1941 | return get_list_type(member_type, type_gap); |
| 1942 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 1947 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1948 | */ |
| 1949 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1950 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1951 | { |
| 1952 | char_u *p = *arg; |
| 1953 | size_t len; |
| 1954 | |
| 1955 | // skip over the first word |
| 1956 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1957 | ++p; |
| 1958 | len = p - *arg; |
| 1959 | |
| 1960 | switch (**arg) |
| 1961 | { |
| 1962 | case 'a': |
| 1963 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 1964 | { |
| 1965 | *arg += len; |
| 1966 | return &t_any; |
| 1967 | } |
| 1968 | break; |
| 1969 | case 'b': |
| 1970 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 1971 | { |
| 1972 | *arg += len; |
| 1973 | return &t_bool; |
| 1974 | } |
| 1975 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 1976 | { |
| 1977 | *arg += len; |
| 1978 | return &t_blob; |
| 1979 | } |
| 1980 | break; |
| 1981 | case 'c': |
| 1982 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 1983 | { |
| 1984 | *arg += len; |
| 1985 | return &t_channel; |
| 1986 | } |
| 1987 | break; |
| 1988 | case 'd': |
| 1989 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 1990 | { |
| 1991 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1992 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1993 | } |
| 1994 | break; |
| 1995 | case 'f': |
| 1996 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 1997 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1998 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1999 | *arg += len; |
| 2000 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2001 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2002 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2003 | return &t_any; |
| 2004 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2005 | } |
| 2006 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 2007 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2008 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2009 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2010 | int argcount = -1; |
| 2011 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2012 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2013 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 2014 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2015 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2016 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2017 | if (**arg == '(') |
| 2018 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2019 | // "func" may or may not return a value, "func()" does |
| 2020 | // not return a value. |
| 2021 | ret_type = &t_void; |
| 2022 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2023 | p = ++*arg; |
| 2024 | argcount = 0; |
| 2025 | while (*p != NUL && *p != ')') |
| 2026 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2027 | if (*p == '?') |
| 2028 | { |
| 2029 | if (first_optional == -1) |
| 2030 | first_optional = argcount; |
| 2031 | ++p; |
| 2032 | } |
| 2033 | else if (first_optional != -1) |
| 2034 | { |
| 2035 | emsg(_("E1007: mandatory argument after optional argument")); |
| 2036 | return &t_any; |
| 2037 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2038 | else if (STRNCMP(p, "...", 3) == 0) |
| 2039 | { |
| 2040 | flags |= TTFLAG_VARARGS; |
| 2041 | p += 3; |
| 2042 | } |
| 2043 | |
| 2044 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 2045 | |
| 2046 | // Nothing comes after "...{type}". |
| 2047 | if (flags & TTFLAG_VARARGS) |
| 2048 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2049 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2050 | if (*p != ',' && *skipwhite(p) == ',') |
| 2051 | { |
| 2052 | semsg(_(e_no_white_before), ","); |
| 2053 | return &t_any; |
| 2054 | } |
| 2055 | if (*p == ',') |
| 2056 | { |
| 2057 | ++p; |
| 2058 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2059 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2060 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2061 | return &t_any; |
| 2062 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2063 | } |
| 2064 | p = skipwhite(p); |
| 2065 | if (argcount == MAX_FUNC_ARGS) |
| 2066 | { |
| 2067 | emsg(_("E740: Too many argument types")); |
| 2068 | return &t_any; |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | p = skipwhite(p); |
| 2073 | if (*p != ')') |
| 2074 | { |
| 2075 | emsg(_(e_missing_close)); |
| 2076 | return &t_any; |
| 2077 | } |
| 2078 | *arg = p + 1; |
| 2079 | } |
| 2080 | if (**arg == ':') |
| 2081 | { |
| 2082 | // parse return type |
| 2083 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2084 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2085 | semsg(_(e_white_after), ":"); |
| 2086 | *arg = skipwhite(*arg); |
| 2087 | ret_type = parse_type(arg, type_gap); |
| 2088 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2089 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2090 | type = get_func_type(ret_type, argcount, type_gap); |
| 2091 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2092 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2093 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 2094 | type->tt_flags = flags; |
| 2095 | if (argcount > 0) |
| 2096 | { |
| 2097 | type->tt_argcount = argcount; |
| 2098 | type->tt_min_argcount = first_optional == -1 |
| 2099 | ? argcount : first_optional; |
| 2100 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2101 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2102 | return &t_any; |
| 2103 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2104 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2105 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2106 | } |
| 2107 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2108 | } |
| 2109 | break; |
| 2110 | case 'j': |
| 2111 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 2112 | { |
| 2113 | *arg += len; |
| 2114 | return &t_job; |
| 2115 | } |
| 2116 | break; |
| 2117 | case 'l': |
| 2118 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 2119 | { |
| 2120 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2121 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2122 | } |
| 2123 | break; |
| 2124 | case 'n': |
| 2125 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 2126 | { |
| 2127 | *arg += len; |
| 2128 | return &t_number; |
| 2129 | } |
| 2130 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2131 | case 's': |
| 2132 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 2133 | { |
| 2134 | *arg += len; |
| 2135 | return &t_string; |
| 2136 | } |
| 2137 | break; |
| 2138 | case 'v': |
| 2139 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 2140 | { |
| 2141 | *arg += len; |
| 2142 | return &t_void; |
| 2143 | } |
| 2144 | break; |
| 2145 | } |
| 2146 | |
| 2147 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 2148 | return &t_any; |
| 2149 | } |
| 2150 | |
| 2151 | /* |
| 2152 | * Check if "type1" and "type2" are exactly the same. |
| 2153 | */ |
| 2154 | static int |
| 2155 | equal_type(type_T *type1, type_T *type2) |
| 2156 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2157 | int i; |
| 2158 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2159 | if (type1->tt_type != type2->tt_type) |
| 2160 | return FALSE; |
| 2161 | switch (type1->tt_type) |
| 2162 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2163 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2164 | case VAR_ANY: |
| 2165 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2166 | case VAR_SPECIAL: |
| 2167 | case VAR_BOOL: |
| 2168 | case VAR_NUMBER: |
| 2169 | case VAR_FLOAT: |
| 2170 | case VAR_STRING: |
| 2171 | case VAR_BLOB: |
| 2172 | case VAR_JOB: |
| 2173 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2174 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2175 | case VAR_LIST: |
| 2176 | case VAR_DICT: |
| 2177 | return equal_type(type1->tt_member, type2->tt_member); |
| 2178 | case VAR_FUNC: |
| 2179 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2180 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2181 | || type1->tt_argcount != type2->tt_argcount) |
| 2182 | return FALSE; |
| 2183 | if (type1->tt_argcount < 0 |
| 2184 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2185 | return TRUE; |
| 2186 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2187 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2188 | return FALSE; |
| 2189 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2190 | } |
| 2191 | return TRUE; |
| 2192 | } |
| 2193 | |
| 2194 | /* |
| 2195 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2196 | * "type2" and "dest" may be the same. |
| 2197 | */ |
| 2198 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2199 | 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] | 2200 | { |
| 2201 | if (equal_type(type1, type2)) |
| 2202 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2203 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2204 | return; |
| 2205 | } |
| 2206 | |
| 2207 | if (type1->tt_type == type2->tt_type) |
| 2208 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2209 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2210 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2211 | type_T *common; |
| 2212 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2213 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2214 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2215 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2216 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2217 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2218 | return; |
| 2219 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2220 | if (type1->tt_type == VAR_FUNC) |
| 2221 | { |
| 2222 | type_T *common; |
| 2223 | |
| 2224 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2225 | if (type1->tt_argcount == type2->tt_argcount |
| 2226 | && type1->tt_argcount >= 0) |
| 2227 | { |
| 2228 | int argcount = type1->tt_argcount; |
| 2229 | int i; |
| 2230 | |
| 2231 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2232 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2233 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2234 | if (func_type_add_arg_types(*dest, argcount, |
| 2235 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2236 | for (i = 0; i < argcount; ++i) |
| 2237 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2238 | &(*dest)->tt_args[i], type_gap); |
| 2239 | } |
| 2240 | } |
| 2241 | else |
| 2242 | *dest = alloc_func_type(common, -1, type_gap); |
| 2243 | return; |
| 2244 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2245 | } |
| 2246 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2247 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | char * |
| 2251 | vartype_name(vartype_T type) |
| 2252 | { |
| 2253 | switch (type) |
| 2254 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2255 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2256 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2257 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2258 | case VAR_SPECIAL: return "special"; |
| 2259 | case VAR_BOOL: return "bool"; |
| 2260 | case VAR_NUMBER: return "number"; |
| 2261 | case VAR_FLOAT: return "float"; |
| 2262 | case VAR_STRING: return "string"; |
| 2263 | case VAR_BLOB: return "blob"; |
| 2264 | case VAR_JOB: return "job"; |
| 2265 | case VAR_CHANNEL: return "channel"; |
| 2266 | case VAR_LIST: return "list"; |
| 2267 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2268 | |
| 2269 | case VAR_FUNC: |
| 2270 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2271 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2272 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | /* |
| 2276 | * Return the name of a type. |
| 2277 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2278 | */ |
| 2279 | char * |
| 2280 | type_name(type_T *type, char **tofree) |
| 2281 | { |
| 2282 | char *name = vartype_name(type->tt_type); |
| 2283 | |
| 2284 | *tofree = NULL; |
| 2285 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2286 | { |
| 2287 | char *member_free; |
| 2288 | char *member_name = type_name(type->tt_member, &member_free); |
| 2289 | size_t len; |
| 2290 | |
| 2291 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2292 | *tofree = alloc(len); |
| 2293 | if (*tofree != NULL) |
| 2294 | { |
| 2295 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2296 | vim_free(member_free); |
| 2297 | return *tofree; |
| 2298 | } |
| 2299 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2300 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2301 | { |
| 2302 | garray_T ga; |
| 2303 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2304 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2305 | |
| 2306 | ga_init2(&ga, 1, 100); |
| 2307 | if (ga_grow(&ga, 20) == FAIL) |
| 2308 | return "[unknown]"; |
| 2309 | *tofree = ga.ga_data; |
| 2310 | STRCPY(ga.ga_data, "func("); |
| 2311 | ga.ga_len += 5; |
| 2312 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2313 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2314 | { |
| 2315 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2316 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2317 | int len; |
| 2318 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2319 | if (type->tt_args == NULL) |
| 2320 | arg_type = "[unknown]"; |
| 2321 | else |
| 2322 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2323 | if (i > 0) |
| 2324 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2325 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2326 | ga.ga_len += 2; |
| 2327 | } |
| 2328 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2329 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2330 | { |
| 2331 | vim_free(arg_free); |
| 2332 | return "[unknown]"; |
| 2333 | } |
| 2334 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2335 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2336 | { |
| 2337 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2338 | ga.ga_len += 3; |
| 2339 | } |
| 2340 | else if (i >= type->tt_min_argcount) |
| 2341 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2342 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2343 | ga.ga_len += len; |
| 2344 | vim_free(arg_free); |
| 2345 | } |
| 2346 | |
| 2347 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2348 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2349 | else |
| 2350 | { |
| 2351 | char *ret_free; |
| 2352 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2353 | int len; |
| 2354 | |
| 2355 | len = (int)STRLEN(ret_name) + 4; |
| 2356 | if (ga_grow(&ga, len) == FAIL) |
| 2357 | { |
| 2358 | vim_free(ret_free); |
| 2359 | return "[unknown]"; |
| 2360 | } |
| 2361 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2362 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2363 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2364 | vim_free(ret_free); |
| 2365 | } |
| 2366 | return ga.ga_data; |
| 2367 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2368 | |
| 2369 | return name; |
| 2370 | } |
| 2371 | |
| 2372 | /* |
| 2373 | * Find "name" in script-local items of script "sid". |
| 2374 | * Returns the index in "sn_var_vals" if found. |
| 2375 | * If found but not in "sn_var_vals" returns -1. |
| 2376 | * If not found returns -2. |
| 2377 | */ |
| 2378 | int |
| 2379 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2380 | { |
| 2381 | hashtab_T *ht; |
| 2382 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2383 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2384 | int idx; |
| 2385 | |
| 2386 | // First look the name up in the hashtable. |
| 2387 | if (sid <= 0 || sid > script_items.ga_len) |
| 2388 | return -1; |
| 2389 | ht = &SCRIPT_VARS(sid); |
| 2390 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2391 | if (di == NULL) |
| 2392 | return -2; |
| 2393 | |
| 2394 | // Now find the svar_T index in sn_var_vals. |
| 2395 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2396 | { |
| 2397 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2398 | |
| 2399 | if (sv->sv_tv == &di->di_tv) |
| 2400 | { |
| 2401 | if (check_writable && sv->sv_const) |
| 2402 | semsg(_(e_readonlyvar), name); |
| 2403 | return idx; |
| 2404 | } |
| 2405 | } |
| 2406 | return -1; |
| 2407 | } |
| 2408 | |
| 2409 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2410 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2411 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2412 | */ |
| 2413 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2414 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2415 | { |
Bram Moolenaar | 086eb18 | 2020-07-01 16:00:44 +0200 | [diff] [blame] | 2416 | scriptitem_T *si; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2417 | int idx; |
| 2418 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2419 | if (current_sctx.sc_sid <= 0) |
| 2420 | return NULL; |
| 2421 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2422 | if (cctx != NULL) |
| 2423 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2424 | { |
| 2425 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2426 | + idx; |
| 2427 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2428 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2429 | : STRLEN(import->imp_name) == len |
| 2430 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2431 | return import; |
| 2432 | } |
| 2433 | |
| 2434 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2435 | { |
| 2436 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2437 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2438 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2439 | : STRLEN(import->imp_name) == len |
| 2440 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2441 | return import; |
| 2442 | } |
| 2443 | return NULL; |
| 2444 | } |
| 2445 | |
| 2446 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2447 | * Free all imported variables. |
| 2448 | */ |
| 2449 | static void |
| 2450 | free_imported(cctx_T *cctx) |
| 2451 | { |
| 2452 | int idx; |
| 2453 | |
| 2454 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2455 | { |
| 2456 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2457 | |
| 2458 | vim_free(import->imp_name); |
| 2459 | } |
| 2460 | ga_clear(&cctx->ctx_imports); |
| 2461 | } |
| 2462 | |
| 2463 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2464 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2465 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 2466 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2467 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2468 | { |
| 2469 | return p[0] == '#' && p[1] != '{'; |
| 2470 | } |
| 2471 | |
| 2472 | /* |
| 2473 | * Return a pointer to the next line that isn't empty or only contains a |
| 2474 | * comment. Skips over white space. |
| 2475 | * Returns NULL if there is none. |
| 2476 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2477 | char_u * |
| 2478 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2479 | { |
| 2480 | int lnum = cctx->ctx_lnum; |
| 2481 | |
| 2482 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2483 | { |
| 2484 | char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum]; |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 2485 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2486 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 2487 | if (line == NULL) |
| 2488 | break; |
| 2489 | p = skipwhite(line); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2490 | if (*p != NUL && !vim9_comment_start(p)) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2491 | return p; |
| 2492 | } |
| 2493 | return NULL; |
| 2494 | } |
| 2495 | |
| 2496 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2497 | * Called when checking for a following operator at "arg". When the rest of |
| 2498 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2499 | * line return a pointer to it and set "nextp". |
| 2500 | * Otherwise skip over white space. |
| 2501 | */ |
| 2502 | static char_u * |
| 2503 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2504 | { |
| 2505 | char_u *p = skipwhite(arg); |
| 2506 | |
| 2507 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2508 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2509 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2510 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2511 | if (*nextp != NULL) |
| 2512 | return *nextp; |
| 2513 | } |
| 2514 | return p; |
| 2515 | } |
| 2516 | |
| 2517 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2518 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2519 | * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE. |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2520 | * Returns NULL when at the end. |
| 2521 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2522 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2523 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2524 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2525 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2526 | |
| 2527 | do |
| 2528 | { |
| 2529 | ++cctx->ctx_lnum; |
| 2530 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2531 | { |
| 2532 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2533 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2534 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2535 | line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2536 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2537 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2538 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2539 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2540 | return line; |
| 2541 | } |
| 2542 | |
| 2543 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2544 | * If "*arg" is at the end of the line, advance to the next line. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2545 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2546 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2547 | */ |
| 2548 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2549 | may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2550 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2551 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2552 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2553 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2554 | |
| 2555 | if (next == NULL) |
| 2556 | return FAIL; |
| 2557 | *arg = skipwhite(next); |
| 2558 | } |
| 2559 | return OK; |
| 2560 | } |
| 2561 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2562 | /* |
| 2563 | * Idem, and give an error when failed. |
| 2564 | */ |
| 2565 | static int |
| 2566 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2567 | { |
| 2568 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2569 | { |
| 2570 | emsg(_("E1097: line incomplete")); |
| 2571 | return FAIL; |
| 2572 | } |
| 2573 | return OK; |
| 2574 | } |
| 2575 | |
| 2576 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2577 | // Structure passed between the compile_expr* functions to keep track of |
| 2578 | // constants that have been parsed but for which no code was produced yet. If |
| 2579 | // possible expressions on these constants are applied at compile time. If |
| 2580 | // that is not possible, the code to push the constants needs to be generated |
| 2581 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2582 | // Using 50 should be more than enough of 5 levels of (). |
| 2583 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2584 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2585 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2586 | int pp_used; // active entries in pp_tv[] |
| 2587 | } ppconst_T; |
| 2588 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2589 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2590 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2591 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2592 | /* |
| 2593 | * Generate a PUSH instruction for "tv". |
| 2594 | * "tv" will be consumed or cleared. |
| 2595 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2596 | */ |
| 2597 | static int |
| 2598 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2599 | { |
| 2600 | if (tv != NULL) |
| 2601 | { |
| 2602 | switch (tv->v_type) |
| 2603 | { |
| 2604 | case VAR_UNKNOWN: |
| 2605 | break; |
| 2606 | case VAR_BOOL: |
| 2607 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2608 | break; |
| 2609 | case VAR_SPECIAL: |
| 2610 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2611 | break; |
| 2612 | case VAR_NUMBER: |
| 2613 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2614 | break; |
| 2615 | #ifdef FEAT_FLOAT |
| 2616 | case VAR_FLOAT: |
| 2617 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2618 | break; |
| 2619 | #endif |
| 2620 | case VAR_BLOB: |
| 2621 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2622 | tv->vval.v_blob = NULL; |
| 2623 | break; |
| 2624 | case VAR_STRING: |
| 2625 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2626 | tv->vval.v_string = NULL; |
| 2627 | break; |
| 2628 | default: |
| 2629 | iemsg("constant type not supported"); |
| 2630 | clear_tv(tv); |
| 2631 | return FAIL; |
| 2632 | } |
| 2633 | tv->v_type = VAR_UNKNOWN; |
| 2634 | } |
| 2635 | return OK; |
| 2636 | } |
| 2637 | |
| 2638 | /* |
| 2639 | * Generate code for any ppconst entries. |
| 2640 | */ |
| 2641 | static int |
| 2642 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2643 | { |
| 2644 | int i; |
| 2645 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2646 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2647 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2648 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2649 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2650 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2651 | ret = FAIL; |
| 2652 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2653 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2654 | return ret; |
| 2655 | } |
| 2656 | |
| 2657 | /* |
| 2658 | * Clear ppconst constants. Used when failing. |
| 2659 | */ |
| 2660 | static void |
| 2661 | clear_ppconst(ppconst_T *ppconst) |
| 2662 | { |
| 2663 | int i; |
| 2664 | |
| 2665 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2666 | clear_tv(&ppconst->pp_tv[i]); |
| 2667 | ppconst->pp_used = 0; |
| 2668 | } |
| 2669 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2670 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2671 | * Generate an instruction to load script-local variable "name", without the |
| 2672 | * leading "s:". |
| 2673 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2674 | */ |
| 2675 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2676 | compile_load_scriptvar( |
| 2677 | cctx_T *cctx, |
| 2678 | char_u *name, // variable NUL terminated |
| 2679 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2680 | char_u **end, // end of variable |
| 2681 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2682 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2683 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2684 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2685 | imported_T *import; |
| 2686 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2687 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2688 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2689 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2690 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2691 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2692 | } |
| 2693 | if (idx >= 0) |
| 2694 | { |
| 2695 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2696 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2697 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2698 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2699 | return OK; |
| 2700 | } |
| 2701 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2702 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2703 | if (import != NULL) |
| 2704 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2705 | if (import->imp_all) |
| 2706 | { |
| 2707 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2708 | char_u *exp_name; |
| 2709 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2710 | ufunc_T *ufunc; |
| 2711 | type_T *type; |
| 2712 | |
| 2713 | // Used "import * as Name", need to lookup the member. |
| 2714 | if (*p != '.') |
| 2715 | { |
| 2716 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2717 | return FAIL; |
| 2718 | } |
| 2719 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2720 | if (VIM_ISWHITE(*p)) |
| 2721 | { |
| 2722 | emsg(_("E1074: no white space allowed after dot")); |
| 2723 | return FAIL; |
| 2724 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2725 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2726 | // isolate one name |
| 2727 | exp_name = p; |
| 2728 | while (eval_isnamec(*p)) |
| 2729 | ++p; |
| 2730 | cc = *p; |
| 2731 | *p = NUL; |
| 2732 | |
| 2733 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2734 | *p = cc; |
| 2735 | p = skipwhite(p); |
| 2736 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2737 | // TODO: what if it is a function? |
| 2738 | if (idx < 0) |
| 2739 | return FAIL; |
| 2740 | *end = p; |
| 2741 | |
| 2742 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2743 | import->imp_sid, |
| 2744 | idx, |
| 2745 | type); |
| 2746 | } |
| 2747 | else |
| 2748 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2749 | // TODO: check this is a variable, not a function? |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2750 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2751 | import->imp_sid, |
| 2752 | import->imp_var_vals_idx, |
| 2753 | import->imp_type); |
| 2754 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2755 | return OK; |
| 2756 | } |
| 2757 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2758 | if (error) |
| 2759 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2760 | return FAIL; |
| 2761 | } |
| 2762 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2763 | static int |
| 2764 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2765 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2766 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2767 | |
| 2768 | if (ufunc == NULL) |
| 2769 | return FAIL; |
| 2770 | |
Bram Moolenaar | fe465a0 | 2020-07-07 22:50:12 +0200 | [diff] [blame] | 2771 | return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name), |
| 2772 | ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2773 | } |
| 2774 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2775 | /* |
| 2776 | * Compile a variable name into a load instruction. |
| 2777 | * "end" points to just after the name. |
| 2778 | * When "error" is FALSE do not give an error when not found. |
| 2779 | */ |
| 2780 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2781 | 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] | 2782 | { |
| 2783 | type_T *type; |
| 2784 | char_u *name; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2785 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2786 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2787 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2788 | |
| 2789 | if (*(*arg + 1) == ':') |
| 2790 | { |
| 2791 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2792 | if (end <= *arg + 2) |
| 2793 | name = vim_strsave((char_u *)"[empty]"); |
| 2794 | else |
| 2795 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2796 | if (name == NULL) |
| 2797 | return FAIL; |
| 2798 | |
| 2799 | if (**arg == 'v') |
| 2800 | { |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2801 | res = generate_LOADV(cctx, name, error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2802 | } |
| 2803 | else if (**arg == 'g') |
| 2804 | { |
| 2805 | // Global variables can be defined later, thus we don't check if it |
| 2806 | // exists, give error at runtime. |
| 2807 | res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any); |
| 2808 | } |
| 2809 | else if (**arg == 's') |
| 2810 | { |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2811 | res = compile_load_scriptvar(cctx, name, NULL, NULL, error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2812 | } |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2813 | else if (**arg == 'b') |
| 2814 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2815 | // Buffer-local variables can be defined later, thus we don't check |
| 2816 | // if it exists, give error at runtime. |
| 2817 | res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2818 | } |
| 2819 | else if (**arg == 'w') |
| 2820 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2821 | // Window-local variables can be defined later, thus we don't check |
| 2822 | // if it exists, give error at runtime. |
| 2823 | res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2824 | } |
| 2825 | else if (**arg == 't') |
| 2826 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2827 | // Tabpage-local variables can be defined later, thus we don't |
| 2828 | // check if it exists, give error at runtime. |
| 2829 | res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2830 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2831 | else |
| 2832 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2833 | semsg("E1075: Namespace not supported: %s", *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2834 | goto theend; |
| 2835 | } |
| 2836 | } |
| 2837 | else |
| 2838 | { |
| 2839 | size_t len = end - *arg; |
| 2840 | int idx; |
| 2841 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2842 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2843 | |
| 2844 | name = vim_strnsave(*arg, end - *arg); |
| 2845 | if (name == NULL) |
| 2846 | return FAIL; |
| 2847 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2848 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2849 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2850 | if (!gen_load_outer) |
| 2851 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2852 | } |
| 2853 | else |
| 2854 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2855 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2856 | |
| 2857 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2858 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2859 | type = lvar->lv_type; |
| 2860 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2861 | if (lvar->lv_from_outer) |
| 2862 | gen_load_outer = TRUE; |
| 2863 | else |
| 2864 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2865 | } |
| 2866 | else |
| 2867 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2868 | // "var" can be script-local even without using "s:" if it |
| 2869 | // already exists. |
| 2870 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2871 | == SCRIPT_VERSION_VIM9 |
| 2872 | || lookup_script(*arg, len) == OK) |
| 2873 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2874 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2875 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2876 | // When the name starts with an uppercase letter or "x:" it |
| 2877 | // can be a user defined function. |
| 2878 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2879 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2880 | } |
| 2881 | } |
| 2882 | if (gen_load) |
| 2883 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2884 | if (gen_load_outer) |
| 2885 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | *arg = end; |
| 2889 | |
| 2890 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2891 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2892 | semsg(_(e_var_notfound), name); |
| 2893 | vim_free(name); |
| 2894 | return res; |
| 2895 | } |
| 2896 | |
| 2897 | /* |
| 2898 | * Compile the argument expressions. |
| 2899 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2900 | */ |
| 2901 | static int |
| 2902 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2903 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2904 | char_u *p = *arg; |
| 2905 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2906 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2907 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2908 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2909 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2910 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2911 | if (*p == ')') |
| 2912 | { |
| 2913 | *arg = p + 1; |
| 2914 | return OK; |
| 2915 | } |
| 2916 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2917 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2918 | return FAIL; |
| 2919 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2920 | |
| 2921 | if (*p != ',' && *skipwhite(p) == ',') |
| 2922 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2923 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2924 | p = skipwhite(p); |
| 2925 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2926 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2927 | { |
| 2928 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2929 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2930 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2931 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2932 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2933 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2934 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2935 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2936 | emsg(_(e_missing_close)); |
| 2937 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | /* |
| 2941 | * Compile a function call: name(arg1, arg2) |
| 2942 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2943 | * "argcount_init" is 1 for "value->method()" |
| 2944 | * Instructions: |
| 2945 | * EVAL arg1 |
| 2946 | * EVAL arg2 |
| 2947 | * BCALL / DCALL / UCALL |
| 2948 | */ |
| 2949 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2950 | compile_call( |
| 2951 | char_u **arg, |
| 2952 | size_t varlen, |
| 2953 | cctx_T *cctx, |
| 2954 | ppconst_T *ppconst, |
| 2955 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2956 | { |
| 2957 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2958 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2959 | int argcount = argcount_init; |
| 2960 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2961 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2962 | char_u *tofree = NULL; |
| 2963 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2964 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2965 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2966 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2967 | // we can evaluate "has('name')" at compile time |
| 2968 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2969 | { |
| 2970 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2971 | typval_T argvars[2]; |
| 2972 | |
| 2973 | argvars[0].v_type = VAR_UNKNOWN; |
| 2974 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2975 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2976 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2977 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2978 | s = skipwhite(s); |
| 2979 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 2980 | { |
| 2981 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2982 | |
| 2983 | *arg = s + 1; |
| 2984 | argvars[1].v_type = VAR_UNKNOWN; |
| 2985 | tv->v_type = VAR_NUMBER; |
| 2986 | tv->vval.v_number = 0; |
| 2987 | f_has(argvars, tv); |
| 2988 | clear_tv(&argvars[0]); |
| 2989 | ++ppconst->pp_used; |
| 2990 | return OK; |
| 2991 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2992 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2996 | return FAIL; |
| 2997 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2998 | if (varlen >= sizeof(namebuf)) |
| 2999 | { |
| 3000 | semsg(_("E1011: name too long: %s"), name); |
| 3001 | return FAIL; |
| 3002 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3003 | vim_strncpy(namebuf, *arg, varlen); |
| 3004 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3005 | |
| 3006 | *arg = skipwhite(*arg + varlen + 1); |
| 3007 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3008 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3009 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3010 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3011 | { |
| 3012 | int idx; |
| 3013 | |
| 3014 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3015 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3016 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 3017 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3018 | else |
| 3019 | semsg(_(e_unknownfunc), namebuf); |
| 3020 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3021 | } |
| 3022 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3023 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 3024 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3025 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3026 | { |
| 3027 | res = generate_CALL(cctx, ufunc, argcount); |
| 3028 | goto theend; |
| 3029 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3030 | |
| 3031 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3032 | // Not for g:Func(), we don't know if it is a variable or not. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3033 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3034 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 3035 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3036 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3037 | garray_T *stack = &cctx->ctx_type_stack; |
| 3038 | type_T *type; |
| 3039 | |
| 3040 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3041 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3042 | goto theend; |
| 3043 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3044 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3045 | // A global function may be defined only later. Need to figure out at |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3046 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3047 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 3048 | res = generate_UCALL(cctx, name, argcount); |
| 3049 | else |
| 3050 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3051 | |
| 3052 | theend: |
| 3053 | vim_free(tofree); |
| 3054 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3055 | } |
| 3056 | |
| 3057 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 3058 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 3059 | |
| 3060 | /* |
| 3061 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 3062 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3063 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3064 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 3065 | * valid name. |
| 3066 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3067 | static char_u * |
| 3068 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3069 | { |
| 3070 | char_u *p; |
| 3071 | |
| 3072 | // Quick check for valid starting character. |
| 3073 | if (!eval_isnamec1(*arg)) |
| 3074 | return arg; |
| 3075 | |
| 3076 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 3077 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 3078 | // and can be used in slice "[n:]". |
| 3079 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3080 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3081 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 3082 | break; |
| 3083 | return p; |
| 3084 | } |
| 3085 | |
| 3086 | /* |
| 3087 | * Like to_name_end() but also skip over a list or dict constant. |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 3088 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3089 | */ |
| 3090 | char_u * |
| 3091 | to_name_const_end(char_u *arg) |
| 3092 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3093 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3094 | typval_T rettv; |
| 3095 | |
| 3096 | if (p == arg && *arg == '[') |
| 3097 | { |
| 3098 | |
| 3099 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3100 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3101 | p = arg; |
| 3102 | } |
| 3103 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 3104 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3105 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3106 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3107 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3108 | p = arg; |
| 3109 | } |
| 3110 | else if (p == arg && *arg == '{') |
| 3111 | { |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3112 | int ret = get_lambda_tv(&p, &rettv, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3113 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3114 | // Can be "{x -> ret}()". |
| 3115 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3116 | if (ret == NOTDONE) |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3117 | ret = eval_dict(&p, &rettv, NULL, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3118 | if (ret != OK) |
| 3119 | p = arg; |
| 3120 | } |
| 3121 | |
| 3122 | return p; |
| 3123 | } |
| 3124 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3125 | /* |
| 3126 | * parse a list: [expr, expr] |
| 3127 | * "*arg" points to the '['. |
| 3128 | */ |
| 3129 | static int |
| 3130 | compile_list(char_u **arg, cctx_T *cctx) |
| 3131 | { |
| 3132 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3133 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3134 | int count = 0; |
| 3135 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3136 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3137 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3138 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3139 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3140 | semsg(_(e_list_end), *arg); |
| 3141 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3142 | } |
| 3143 | if (*p == ']') |
| 3144 | { |
| 3145 | ++p; |
| 3146 | // Allow for following comment, after at least one space. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3147 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '#') |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3148 | p += STRLEN(p); |
| 3149 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3150 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3151 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3152 | break; |
| 3153 | ++count; |
| 3154 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3155 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3156 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3157 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 3158 | { |
| 3159 | semsg(_(e_white_after), ","); |
| 3160 | return FAIL; |
| 3161 | } |
| 3162 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3163 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3164 | p = skipwhite(p); |
| 3165 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3166 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3167 | |
| 3168 | generate_NEWLIST(cctx, count); |
| 3169 | return OK; |
| 3170 | } |
| 3171 | |
| 3172 | /* |
| 3173 | * parse a lambda: {arg, arg -> expr} |
| 3174 | * "*arg" points to the '{'. |
| 3175 | */ |
| 3176 | static int |
| 3177 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 3178 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3179 | typval_T rettv; |
| 3180 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3181 | evalarg_T evalarg; |
| 3182 | |
| 3183 | CLEAR_FIELD(evalarg); |
| 3184 | evalarg.eval_flags = EVAL_EVALUATE; |
| 3185 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3186 | |
| 3187 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3188 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3189 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3190 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3191 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3192 | ++ufunc->uf_refcount; |
| 3193 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3194 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3195 | |
| 3196 | // The function will have one line: "return {expr}". |
| 3197 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3198 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3199 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 3200 | clear_evalarg(&evalarg, NULL); |
| 3201 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 3202 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 3203 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3204 | |
| 3205 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3206 | return FAIL; |
| 3207 | } |
| 3208 | |
| 3209 | /* |
| 3210 | * Compile a lamda call: expr->{lambda}(args) |
| 3211 | * "arg" points to the "{". |
| 3212 | */ |
| 3213 | static int |
| 3214 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 3215 | { |
| 3216 | ufunc_T *ufunc; |
| 3217 | typval_T rettv; |
| 3218 | int argcount = 1; |
| 3219 | int ret = FAIL; |
| 3220 | |
| 3221 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3222 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3223 | return FAIL; |
| 3224 | |
| 3225 | if (**arg != '(') |
| 3226 | { |
| 3227 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 3228 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3229 | else |
| 3230 | semsg(_(e_missing_paren), "lambda"); |
| 3231 | clear_tv(&rettv); |
| 3232 | return FAIL; |
| 3233 | } |
| 3234 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3235 | ufunc = rettv.vval.v_partial->pt_func; |
| 3236 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3237 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3238 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3239 | |
| 3240 | // The function will have one line: "return {expr}". |
| 3241 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3242 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3243 | |
| 3244 | // compile the arguments |
| 3245 | *arg = skipwhite(*arg + 1); |
| 3246 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 3247 | // call the compiled function |
| 3248 | ret = generate_CALL(cctx, ufunc, argcount); |
| 3249 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3250 | if (ret == FAIL) |
| 3251 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3252 | return ret; |
| 3253 | } |
| 3254 | |
| 3255 | /* |
| 3256 | * parse a dict: {'key': val} or #{key: val} |
| 3257 | * "*arg" points to the '{'. |
| 3258 | */ |
| 3259 | static int |
| 3260 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 3261 | { |
| 3262 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3263 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3264 | int count = 0; |
| 3265 | dict_T *d = dict_alloc(); |
| 3266 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3267 | char_u *whitep = *arg; |
| 3268 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3269 | |
| 3270 | if (d == NULL) |
| 3271 | return FAIL; |
| 3272 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3273 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3274 | { |
| 3275 | char_u *key = NULL; |
| 3276 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3277 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3278 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3279 | *arg = NULL; |
| 3280 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | if (**arg == '}') |
| 3284 | break; |
| 3285 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3286 | if (literal) |
| 3287 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3288 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3289 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3290 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3291 | { |
| 3292 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 3293 | return FAIL; |
| 3294 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3295 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3296 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3297 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3298 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3299 | } |
| 3300 | else |
| 3301 | { |
| 3302 | isn_T *isn; |
| 3303 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3304 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3305 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3306 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3307 | if (isn->isn_type == ISN_PUSHS) |
| 3308 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3309 | else |
| 3310 | { |
| 3311 | type_T *keytype = ((type_T **)stack->ga_data) |
| 3312 | [stack->ga_len - 1]; |
| 3313 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 3314 | return FAIL; |
| 3315 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | // Check for duplicate keys, if using string keys. |
| 3319 | if (key != NULL) |
| 3320 | { |
| 3321 | item = dict_find(d, key, -1); |
| 3322 | if (item != NULL) |
| 3323 | { |
| 3324 | semsg(_(e_duplicate_key), key); |
| 3325 | goto failret; |
| 3326 | } |
| 3327 | item = dictitem_alloc(key); |
| 3328 | if (item != NULL) |
| 3329 | { |
| 3330 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3331 | item->di_tv.v_lock = 0; |
| 3332 | if (dict_add(d, item) == FAIL) |
| 3333 | dictitem_free(item); |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | *arg = skipwhite(*arg); |
| 3338 | if (**arg != ':') |
| 3339 | { |
| 3340 | semsg(_(e_missing_dict_colon), *arg); |
| 3341 | return FAIL; |
| 3342 | } |
| 3343 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3344 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3345 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3346 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3347 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3348 | *arg = NULL; |
| 3349 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3350 | } |
| 3351 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3352 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3353 | return FAIL; |
| 3354 | ++count; |
| 3355 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3356 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3357 | *arg = skipwhite(*arg); |
| 3358 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3359 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3360 | *arg = NULL; |
| 3361 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3362 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3363 | if (**arg == '}') |
| 3364 | break; |
| 3365 | if (**arg != ',') |
| 3366 | { |
| 3367 | semsg(_(e_missing_dict_comma), *arg); |
| 3368 | goto failret; |
| 3369 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3370 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3371 | *arg = skipwhite(*arg + 1); |
| 3372 | } |
| 3373 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3374 | *arg = *arg + 1; |
| 3375 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3376 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3377 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3378 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3379 | *arg += STRLEN(*arg); |
| 3380 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3381 | dict_unref(d); |
| 3382 | return generate_NEWDICT(cctx, count); |
| 3383 | |
| 3384 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3385 | if (*arg == NULL) |
| 3386 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3387 | dict_unref(d); |
| 3388 | return FAIL; |
| 3389 | } |
| 3390 | |
| 3391 | /* |
| 3392 | * Compile "&option". |
| 3393 | */ |
| 3394 | static int |
| 3395 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3396 | { |
| 3397 | typval_T rettv; |
| 3398 | char_u *start = *arg; |
| 3399 | int ret; |
| 3400 | |
| 3401 | // parse the option and get the current value to get the type. |
| 3402 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3403 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3404 | if (ret == OK) |
| 3405 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3406 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3407 | char_u *name = vim_strnsave(start, *arg - start); |
| 3408 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3409 | |
| 3410 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3411 | vim_free(name); |
| 3412 | } |
| 3413 | clear_tv(&rettv); |
| 3414 | |
| 3415 | return ret; |
| 3416 | } |
| 3417 | |
| 3418 | /* |
| 3419 | * Compile "$VAR". |
| 3420 | */ |
| 3421 | static int |
| 3422 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3423 | { |
| 3424 | char_u *start = *arg; |
| 3425 | int len; |
| 3426 | int ret; |
| 3427 | char_u *name; |
| 3428 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3429 | ++*arg; |
| 3430 | len = get_env_len(arg); |
| 3431 | if (len == 0) |
| 3432 | { |
| 3433 | semsg(_(e_syntax_at), start - 1); |
| 3434 | return FAIL; |
| 3435 | } |
| 3436 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3437 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3438 | name = vim_strnsave(start, len + 1); |
| 3439 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3440 | vim_free(name); |
| 3441 | return ret; |
| 3442 | } |
| 3443 | |
| 3444 | /* |
| 3445 | * Compile "@r". |
| 3446 | */ |
| 3447 | static int |
| 3448 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3449 | { |
| 3450 | int ret; |
| 3451 | |
| 3452 | ++*arg; |
| 3453 | if (**arg == NUL) |
| 3454 | { |
| 3455 | semsg(_(e_syntax_at), *arg - 1); |
| 3456 | return FAIL; |
| 3457 | } |
| 3458 | if (!valid_yank_reg(**arg, TRUE)) |
| 3459 | { |
| 3460 | emsg_invreg(**arg); |
| 3461 | return FAIL; |
| 3462 | } |
| 3463 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3464 | ++*arg; |
| 3465 | return ret; |
| 3466 | } |
| 3467 | |
| 3468 | /* |
| 3469 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3470 | */ |
| 3471 | static int |
| 3472 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3473 | { |
| 3474 | char_u *p = end; |
| 3475 | |
| 3476 | // this works from end to start |
| 3477 | while (p > start) |
| 3478 | { |
| 3479 | --p; |
| 3480 | if (*p == '-' || *p == '+') |
| 3481 | { |
| 3482 | // only '-' has an effect, for '+' we only check the type |
| 3483 | #ifdef FEAT_FLOAT |
| 3484 | if (rettv->v_type == VAR_FLOAT) |
| 3485 | { |
| 3486 | if (*p == '-') |
| 3487 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3488 | } |
| 3489 | else |
| 3490 | #endif |
| 3491 | { |
| 3492 | varnumber_T val; |
| 3493 | int error = FALSE; |
| 3494 | |
| 3495 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3496 | // here |
| 3497 | if (check_not_string(rettv) == FAIL) |
| 3498 | return FAIL; |
| 3499 | val = tv_get_number_chk(rettv, &error); |
| 3500 | clear_tv(rettv); |
| 3501 | if (error) |
| 3502 | return FAIL; |
| 3503 | if (*p == '-') |
| 3504 | val = -val; |
| 3505 | rettv->v_type = VAR_NUMBER; |
| 3506 | rettv->vval.v_number = val; |
| 3507 | } |
| 3508 | } |
| 3509 | else |
| 3510 | { |
| 3511 | int v = tv2bool(rettv); |
| 3512 | |
| 3513 | // '!' is permissive in the type. |
| 3514 | clear_tv(rettv); |
| 3515 | rettv->v_type = VAR_BOOL; |
| 3516 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3517 | } |
| 3518 | } |
| 3519 | return OK; |
| 3520 | } |
| 3521 | |
| 3522 | /* |
| 3523 | * Recognize v: variables that are constants and set "rettv". |
| 3524 | */ |
| 3525 | static void |
| 3526 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3527 | { |
| 3528 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3529 | { |
| 3530 | rettv->v_type = VAR_BOOL; |
| 3531 | rettv->vval.v_number = VVAL_TRUE; |
| 3532 | *arg += 6; |
| 3533 | } |
| 3534 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3535 | { |
| 3536 | rettv->v_type = VAR_BOOL; |
| 3537 | rettv->vval.v_number = VVAL_FALSE; |
| 3538 | *arg += 7; |
| 3539 | } |
| 3540 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3541 | { |
| 3542 | rettv->v_type = VAR_SPECIAL; |
| 3543 | rettv->vval.v_number = VVAL_NULL; |
| 3544 | *arg += 6; |
| 3545 | } |
| 3546 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3547 | { |
| 3548 | rettv->v_type = VAR_SPECIAL; |
| 3549 | rettv->vval.v_number = VVAL_NONE; |
| 3550 | *arg += 6; |
| 3551 | } |
| 3552 | } |
| 3553 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3554 | static exptype_T |
| 3555 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3556 | { |
| 3557 | exptype_T type = EXPR_UNKNOWN; |
| 3558 | int i; |
| 3559 | |
| 3560 | switch (p[0]) |
| 3561 | { |
| 3562 | case '=': if (p[1] == '=') |
| 3563 | type = EXPR_EQUAL; |
| 3564 | else if (p[1] == '~') |
| 3565 | type = EXPR_MATCH; |
| 3566 | break; |
| 3567 | case '!': if (p[1] == '=') |
| 3568 | type = EXPR_NEQUAL; |
| 3569 | else if (p[1] == '~') |
| 3570 | type = EXPR_NOMATCH; |
| 3571 | break; |
| 3572 | case '>': if (p[1] != '=') |
| 3573 | { |
| 3574 | type = EXPR_GREATER; |
| 3575 | *len = 1; |
| 3576 | } |
| 3577 | else |
| 3578 | type = EXPR_GEQUAL; |
| 3579 | break; |
| 3580 | case '<': if (p[1] != '=') |
| 3581 | { |
| 3582 | type = EXPR_SMALLER; |
| 3583 | *len = 1; |
| 3584 | } |
| 3585 | else |
| 3586 | type = EXPR_SEQUAL; |
| 3587 | break; |
| 3588 | case 'i': if (p[1] == 's') |
| 3589 | { |
| 3590 | // "is" and "isnot"; but not a prefix of a name |
| 3591 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3592 | *len = 5; |
| 3593 | i = p[*len]; |
| 3594 | if (!isalnum(i) && i != '_') |
| 3595 | { |
| 3596 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3597 | *type_is = TRUE; |
| 3598 | } |
| 3599 | } |
| 3600 | break; |
| 3601 | } |
| 3602 | return type; |
| 3603 | } |
| 3604 | |
| 3605 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3606 | * Compile code to apply '-', '+' and '!'. |
| 3607 | */ |
| 3608 | static int |
| 3609 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3610 | { |
| 3611 | char_u *p = end; |
| 3612 | |
| 3613 | // this works from end to start |
| 3614 | while (p > start) |
| 3615 | { |
| 3616 | --p; |
| 3617 | if (*p == '-' || *p == '+') |
| 3618 | { |
| 3619 | int negate = *p == '-'; |
| 3620 | isn_T *isn; |
| 3621 | |
| 3622 | // TODO: check type |
| 3623 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3624 | { |
| 3625 | --p; |
| 3626 | if (*p == '-') |
| 3627 | negate = !negate; |
| 3628 | } |
| 3629 | // only '-' has an effect, for '+' we only check the type |
| 3630 | if (negate) |
| 3631 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3632 | else |
| 3633 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3634 | if (isn == NULL) |
| 3635 | return FAIL; |
| 3636 | } |
| 3637 | else |
| 3638 | { |
| 3639 | int invert = TRUE; |
| 3640 | |
| 3641 | while (p > start && p[-1] == '!') |
| 3642 | { |
| 3643 | --p; |
| 3644 | invert = !invert; |
| 3645 | } |
| 3646 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3647 | return FAIL; |
| 3648 | } |
| 3649 | } |
| 3650 | return OK; |
| 3651 | } |
| 3652 | |
| 3653 | /* |
| 3654 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3655 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3656 | */ |
| 3657 | static int |
| 3658 | compile_subscript( |
| 3659 | char_u **arg, |
| 3660 | cctx_T *cctx, |
| 3661 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3662 | char_u *end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3663 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3664 | { |
| 3665 | for (;;) |
| 3666 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3667 | char_u *p = skipwhite(*arg); |
| 3668 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3669 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3670 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3671 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3672 | |
| 3673 | // If a following line starts with "->{" or "->X" advance to that |
| 3674 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3675 | // Also if a following line starts with ".x". |
| 3676 | if (next != NULL && |
| 3677 | ((next[0] == '-' && next[1] == '>' |
| 3678 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
| 3679 | || (next[0] == '.' && ASCII_ISALPHA(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3680 | { |
| 3681 | next = next_line_from_context(cctx, TRUE); |
| 3682 | if (next == NULL) |
| 3683 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3684 | *arg = next; |
| 3685 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3686 | } |
| 3687 | } |
| 3688 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3689 | if (*p == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3690 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3691 | garray_T *stack = &cctx->ctx_type_stack; |
| 3692 | type_T *type; |
| 3693 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3694 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3695 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3696 | return FAIL; |
| 3697 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3698 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3699 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3700 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3701 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3702 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3703 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3704 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3705 | return FAIL; |
| 3706 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3707 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3708 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3709 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3710 | return FAIL; |
| 3711 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3712 | // something->method() |
| 3713 | // Apply the '!', '-' and '+' first: |
| 3714 | // -1.0->func() works like (-1.0)->func() |
| 3715 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3716 | return FAIL; |
| 3717 | *start_leader = end_leader; // don't apply again later |
| 3718 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3719 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3720 | *arg = skipwhite(p); |
| 3721 | if (may_get_next_line(p, arg, cctx) == FAIL) |
| 3722 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3723 | if (**arg == '{') |
| 3724 | { |
| 3725 | // lambda call: list->{lambda} |
| 3726 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3727 | return FAIL; |
| 3728 | } |
| 3729 | else |
| 3730 | { |
| 3731 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3732 | p = *arg; |
| 3733 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3734 | p += 2; |
| 3735 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3736 | ; |
| 3737 | if (*p != '(') |
| 3738 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3739 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3740 | return FAIL; |
| 3741 | } |
| 3742 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3743 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3744 | return FAIL; |
| 3745 | } |
| 3746 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3747 | else if (*p == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3748 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3749 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3750 | type_T **typep; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3751 | vartype_T vtype; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3752 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3753 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3754 | // dict member: dict[key] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3755 | // TODO: blob index |
| 3756 | // TODO: more arguments |
| 3757 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3758 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3759 | return FAIL; |
| 3760 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3761 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3762 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3763 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3764 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3765 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3766 | return FAIL; |
| 3767 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3768 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3769 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3770 | if (**arg != ']') |
| 3771 | { |
| 3772 | emsg(_(e_missbrac)); |
| 3773 | return FAIL; |
| 3774 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3775 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3776 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3777 | // We can index a list and a dict. If we don't know the type |
| 3778 | // we can use the index value type. |
| 3779 | // TODO: If we don't know use an instruction to figure it out at |
| 3780 | // runtime. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3781 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3782 | vtype = (*typep)->tt_type; |
| 3783 | if (*typep == &t_any) |
| 3784 | { |
| 3785 | type_T *valtype = ((type_T **)stack->ga_data) |
| 3786 | [stack->ga_len - 1]; |
| 3787 | if (valtype == &t_string) |
| 3788 | vtype = VAR_DICT; |
| 3789 | } |
| 3790 | if (vtype == VAR_DICT) |
| 3791 | { |
| 3792 | if ((*typep)->tt_type == VAR_DICT) |
| 3793 | *typep = (*typep)->tt_member; |
| 3794 | else if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) |
| 3795 | == FAIL) |
| 3796 | return FAIL; |
| 3797 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3798 | return FAIL; |
| 3799 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3800 | return FAIL; |
| 3801 | } |
| 3802 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3803 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3804 | if ((*typep)->tt_type == VAR_LIST) |
| 3805 | *typep = (*typep)->tt_member; |
| 3806 | if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL) |
| 3807 | return FAIL; |
| 3808 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3809 | else |
| 3810 | { |
| 3811 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3812 | return FAIL; |
| 3813 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3814 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3815 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3816 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3817 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3818 | return FAIL; |
| 3819 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3820 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3821 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3822 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3823 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3824 | p = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3825 | if (eval_isnamec1(*p)) |
| 3826 | while (eval_isnamec(*p)) |
| 3827 | MB_PTR_ADV(p); |
| 3828 | if (p == *arg) |
| 3829 | { |
| 3830 | semsg(_(e_syntax_at), *arg); |
| 3831 | return FAIL; |
| 3832 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3833 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3834 | return FAIL; |
| 3835 | *arg = p; |
| 3836 | } |
| 3837 | else |
| 3838 | break; |
| 3839 | } |
| 3840 | |
| 3841 | // TODO - see handle_subscript(): |
| 3842 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3843 | // Don't do this when "Func" is already a partial that was bound |
| 3844 | // explicitly (pt_auto is FALSE). |
| 3845 | |
| 3846 | return OK; |
| 3847 | } |
| 3848 | |
| 3849 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3850 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3851 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3852 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3853 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3854 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3855 | * |
| 3856 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3857 | */ |
| 3858 | |
| 3859 | /* |
| 3860 | * number number constant |
| 3861 | * 0zFFFFFFFF Blob constant |
| 3862 | * "string" string constant |
| 3863 | * 'string' literal string constant |
| 3864 | * &option-name option value |
| 3865 | * @r register contents |
| 3866 | * identifier variable value |
| 3867 | * function() function call |
| 3868 | * $VAR environment variable |
| 3869 | * (expression) nested expression |
| 3870 | * [expr, expr] List |
| 3871 | * {key: val, key: val} Dictionary |
| 3872 | * #{key: val, key: val} Dictionary with literal keys |
| 3873 | * |
| 3874 | * Also handle: |
| 3875 | * ! in front logical NOT |
| 3876 | * - in front unary minus |
| 3877 | * + in front unary plus (ignored) |
| 3878 | * trailing (arg) funcref/partial call |
| 3879 | * trailing [] subscript in String or List |
| 3880 | * trailing .name entry in Dictionary |
| 3881 | * trailing ->name() method call |
| 3882 | */ |
| 3883 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3884 | compile_expr7( |
| 3885 | char_u **arg, |
| 3886 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3887 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3888 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3889 | char_u *start_leader, *end_leader; |
| 3890 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3891 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3892 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3893 | |
| 3894 | /* |
| 3895 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3896 | */ |
| 3897 | start_leader = *arg; |
| 3898 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3899 | *arg = skipwhite(*arg + 1); |
| 3900 | end_leader = *arg; |
| 3901 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3902 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3903 | switch (**arg) |
| 3904 | { |
| 3905 | /* |
| 3906 | * Number constant. |
| 3907 | */ |
| 3908 | case '0': // also for blob starting with 0z |
| 3909 | case '1': |
| 3910 | case '2': |
| 3911 | case '3': |
| 3912 | case '4': |
| 3913 | case '5': |
| 3914 | case '6': |
| 3915 | case '7': |
| 3916 | case '8': |
| 3917 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3918 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3919 | return FAIL; |
| 3920 | break; |
| 3921 | |
| 3922 | /* |
| 3923 | * String constant: "string". |
| 3924 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3925 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3926 | return FAIL; |
| 3927 | break; |
| 3928 | |
| 3929 | /* |
| 3930 | * Literal string constant: 'str''ing'. |
| 3931 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3932 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3933 | return FAIL; |
| 3934 | break; |
| 3935 | |
| 3936 | /* |
| 3937 | * Constant Vim variable. |
| 3938 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3939 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3940 | ret = NOTDONE; |
| 3941 | break; |
| 3942 | |
| 3943 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3944 | * "true" constant |
| 3945 | */ |
| 3946 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3947 | && !eval_isnamec((*arg)[4])) |
| 3948 | { |
| 3949 | *arg += 4; |
| 3950 | rettv->v_type = VAR_BOOL; |
| 3951 | rettv->vval.v_number = VVAL_TRUE; |
| 3952 | } |
| 3953 | else |
| 3954 | ret = NOTDONE; |
| 3955 | break; |
| 3956 | |
| 3957 | /* |
| 3958 | * "false" constant |
| 3959 | */ |
| 3960 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3961 | && !eval_isnamec((*arg)[5])) |
| 3962 | { |
| 3963 | *arg += 5; |
| 3964 | rettv->v_type = VAR_BOOL; |
| 3965 | rettv->vval.v_number = VVAL_FALSE; |
| 3966 | } |
| 3967 | else |
| 3968 | ret = NOTDONE; |
| 3969 | break; |
| 3970 | |
| 3971 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3972 | * List: [expr, expr] |
| 3973 | */ |
| 3974 | case '[': ret = compile_list(arg, cctx); |
| 3975 | break; |
| 3976 | |
| 3977 | /* |
| 3978 | * Dictionary: #{key: val, key: val} |
| 3979 | */ |
| 3980 | case '#': if ((*arg)[1] == '{') |
| 3981 | { |
| 3982 | ++*arg; |
| 3983 | ret = compile_dict(arg, cctx, TRUE); |
| 3984 | } |
| 3985 | else |
| 3986 | ret = NOTDONE; |
| 3987 | break; |
| 3988 | |
| 3989 | /* |
| 3990 | * Lambda: {arg, arg -> expr} |
| 3991 | * Dictionary: {'key': val, 'key': val} |
| 3992 | */ |
| 3993 | case '{': { |
| 3994 | char_u *start = skipwhite(*arg + 1); |
| 3995 | |
| 3996 | // Find out what comes after the arguments. |
| 3997 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3998 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3999 | if (ret != FAIL && *start == '>') |
| 4000 | ret = compile_lambda(arg, cctx); |
| 4001 | else |
| 4002 | ret = compile_dict(arg, cctx, FALSE); |
| 4003 | } |
| 4004 | break; |
| 4005 | |
| 4006 | /* |
| 4007 | * Option value: &name |
| 4008 | */ |
| 4009 | case '&': ret = compile_get_option(arg, cctx); |
| 4010 | break; |
| 4011 | |
| 4012 | /* |
| 4013 | * Environment variable: $VAR. |
| 4014 | */ |
| 4015 | case '$': ret = compile_get_env(arg, cctx); |
| 4016 | break; |
| 4017 | |
| 4018 | /* |
| 4019 | * Register contents: @r. |
| 4020 | */ |
| 4021 | case '@': ret = compile_get_register(arg, cctx); |
| 4022 | break; |
| 4023 | /* |
| 4024 | * nested expression: (expression). |
| 4025 | */ |
| 4026 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4027 | |
| 4028 | // recursive! |
| 4029 | if (ppconst->pp_used <= PPSIZE - 10) |
| 4030 | { |
| 4031 | ret = compile_expr1(arg, cctx, ppconst); |
| 4032 | } |
| 4033 | else |
| 4034 | { |
| 4035 | // Not enough space in ppconst, flush constants. |
| 4036 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4037 | return FAIL; |
| 4038 | ret = compile_expr0(arg, cctx); |
| 4039 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4040 | *arg = skipwhite(*arg); |
| 4041 | if (**arg == ')') |
| 4042 | ++*arg; |
| 4043 | else if (ret == OK) |
| 4044 | { |
| 4045 | emsg(_(e_missing_close)); |
| 4046 | ret = FAIL; |
| 4047 | } |
| 4048 | break; |
| 4049 | |
| 4050 | default: ret = NOTDONE; |
| 4051 | break; |
| 4052 | } |
| 4053 | if (ret == FAIL) |
| 4054 | return FAIL; |
| 4055 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4056 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4057 | { |
| 4058 | // apply the '!', '-' and '+' before the constant |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4059 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4060 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4061 | clear_tv(rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4062 | return FAIL; |
| 4063 | } |
| 4064 | start_leader = end_leader; // don't apply again below |
| 4065 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4066 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4067 | clear_tv(rettv); |
| 4068 | else |
| 4069 | // A constant expression can possibly be handled compile time, |
| 4070 | // return the value instead of generating code. |
| 4071 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4072 | } |
| 4073 | else if (ret == NOTDONE) |
| 4074 | { |
| 4075 | char_u *p; |
| 4076 | int r; |
| 4077 | |
| 4078 | if (!eval_isnamec1(**arg)) |
| 4079 | { |
| 4080 | semsg(_("E1015: Name expected: %s"), *arg); |
| 4081 | return FAIL; |
| 4082 | } |
| 4083 | |
| 4084 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4085 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4086 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4087 | { |
| 4088 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 4089 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4090 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4091 | { |
| 4092 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4093 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4094 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4095 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4096 | if (r == FAIL) |
| 4097 | return FAIL; |
| 4098 | } |
| 4099 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4100 | // Handle following "[]", ".member", etc. |
| 4101 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4102 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4103 | ppconst) == FAIL) |
| 4104 | return FAIL; |
| 4105 | if (ppconst->pp_used > 0) |
| 4106 | { |
| 4107 | // apply the '!', '-' and '+' before the constant |
| 4108 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4109 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
| 4110 | return FAIL; |
| 4111 | return OK; |
| 4112 | } |
| 4113 | if (compile_leader(cctx, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4114 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4115 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | /* |
| 4119 | * * number multiplication |
| 4120 | * / number division |
| 4121 | * % number modulo |
| 4122 | */ |
| 4123 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4124 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4125 | { |
| 4126 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4127 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4128 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4129 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4130 | // get the first expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4131 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4132 | return FAIL; |
| 4133 | |
| 4134 | /* |
| 4135 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4136 | */ |
| 4137 | for (;;) |
| 4138 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4139 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4140 | if (*op != '*' && *op != '/' && *op != '%') |
| 4141 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4142 | if (next != NULL) |
| 4143 | { |
| 4144 | *arg = next_line_from_context(cctx, TRUE); |
| 4145 | op = skipwhite(*arg); |
| 4146 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4147 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4148 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4149 | { |
| 4150 | char_u buf[3]; |
| 4151 | |
| 4152 | vim_strncpy(buf, op, 1); |
| 4153 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4154 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4155 | } |
| 4156 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4157 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4158 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4159 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4160 | // get the second expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4161 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4162 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4163 | |
| 4164 | if (ppconst->pp_used == ppconst_used + 2 |
| 4165 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4166 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4167 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4168 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4169 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4170 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4171 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4172 | // both are numbers: compute the result |
| 4173 | switch (*op) |
| 4174 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4175 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4176 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4177 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4178 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4179 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4180 | break; |
| 4181 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4182 | tv1->vval.v_number = res; |
| 4183 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4184 | } |
| 4185 | else |
| 4186 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4187 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4188 | generate_two_op(cctx, op); |
| 4189 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4190 | } |
| 4191 | |
| 4192 | return OK; |
| 4193 | } |
| 4194 | |
| 4195 | /* |
| 4196 | * + number addition |
| 4197 | * - number subtraction |
| 4198 | * .. string concatenation |
| 4199 | */ |
| 4200 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4201 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4202 | { |
| 4203 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4204 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4205 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4206 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4207 | |
| 4208 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4209 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4210 | return FAIL; |
| 4211 | |
| 4212 | /* |
| 4213 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4214 | */ |
| 4215 | for (;;) |
| 4216 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4217 | op = may_peek_next_line(cctx, *arg, &next); |
| 4218 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4219 | break; |
| 4220 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4221 | if (next != NULL) |
| 4222 | { |
| 4223 | *arg = next_line_from_context(cctx, TRUE); |
| 4224 | op = skipwhite(*arg); |
| 4225 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4226 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4227 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4228 | { |
| 4229 | char_u buf[3]; |
| 4230 | |
| 4231 | vim_strncpy(buf, op, oplen); |
| 4232 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4233 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4234 | } |
| 4235 | |
| 4236 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4237 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4238 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4239 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4240 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4241 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4242 | return FAIL; |
| 4243 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4244 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4245 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4246 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4247 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4248 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4249 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4250 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4251 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4252 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4253 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4254 | // concat/subtract/add constant numbers |
| 4255 | if (*op == '+') |
| 4256 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4257 | else if (*op == '-') |
| 4258 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4259 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4260 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4261 | // concatenate constant strings |
| 4262 | char_u *s1 = tv1->vval.v_string; |
| 4263 | char_u *s2 = tv2->vval.v_string; |
| 4264 | size_t len1 = STRLEN(s1); |
| 4265 | |
| 4266 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4267 | if (tv1->vval.v_string == NULL) |
| 4268 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4269 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4270 | return FAIL; |
| 4271 | } |
| 4272 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4273 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4274 | vim_free(s1); |
| 4275 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4276 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4277 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4278 | } |
| 4279 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4280 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4281 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4282 | if (*op == '.') |
| 4283 | { |
| 4284 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4285 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4286 | return FAIL; |
| 4287 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4288 | } |
| 4289 | else |
| 4290 | generate_two_op(cctx, op); |
| 4291 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4292 | } |
| 4293 | |
| 4294 | return OK; |
| 4295 | } |
| 4296 | |
| 4297 | /* |
| 4298 | * expr5a == expr5b |
| 4299 | * expr5a =~ expr5b |
| 4300 | * expr5a != expr5b |
| 4301 | * expr5a !~ expr5b |
| 4302 | * expr5a > expr5b |
| 4303 | * expr5a >= expr5b |
| 4304 | * expr5a < expr5b |
| 4305 | * expr5a <= expr5b |
| 4306 | * expr5a is expr5b |
| 4307 | * expr5a isnot expr5b |
| 4308 | * |
| 4309 | * Produces instructions: |
| 4310 | * EVAL expr5a Push result of "expr5a" |
| 4311 | * EVAL expr5b Push result of "expr5b" |
| 4312 | * COMPARE one of the compare instructions |
| 4313 | */ |
| 4314 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4315 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4316 | { |
| 4317 | exptype_T type = EXPR_UNKNOWN; |
| 4318 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4319 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4320 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4321 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4322 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4323 | |
| 4324 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4325 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4326 | return FAIL; |
| 4327 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4328 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4329 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4330 | |
| 4331 | /* |
| 4332 | * If there is a comparative operator, use it. |
| 4333 | */ |
| 4334 | if (type != EXPR_UNKNOWN) |
| 4335 | { |
| 4336 | int ic = FALSE; // Default: do not ignore case |
| 4337 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4338 | if (next != NULL) |
| 4339 | { |
| 4340 | *arg = next_line_from_context(cctx, TRUE); |
| 4341 | p = skipwhite(*arg); |
| 4342 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4343 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4344 | { |
| 4345 | semsg(_(e_invexpr2), *arg); |
| 4346 | return FAIL; |
| 4347 | } |
| 4348 | // extra question mark appended: ignore case |
| 4349 | if (p[len] == '?') |
| 4350 | { |
| 4351 | ic = TRUE; |
| 4352 | ++len; |
| 4353 | } |
| 4354 | // extra '#' appended: match case (ignored) |
| 4355 | else if (p[len] == '#') |
| 4356 | ++len; |
| 4357 | // nothing appended: match case |
| 4358 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4359 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4360 | { |
| 4361 | char_u buf[7]; |
| 4362 | |
| 4363 | vim_strncpy(buf, p, len); |
| 4364 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4365 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | // get the second variable |
| 4369 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4370 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4371 | return FAIL; |
| 4372 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4373 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4374 | return FAIL; |
| 4375 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4376 | if (ppconst->pp_used == ppconst_used + 2) |
| 4377 | { |
| 4378 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4379 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4380 | int ret; |
| 4381 | |
| 4382 | // Both sides are a constant, compute the result now. |
| 4383 | // First check for a valid combination of types, this is more |
| 4384 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4385 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4386 | ret = FAIL; |
| 4387 | else |
| 4388 | { |
| 4389 | ret = typval_compare(tv1, tv2, type, ic); |
| 4390 | tv1->v_type = VAR_BOOL; |
| 4391 | tv1->vval.v_number = tv1->vval.v_number |
| 4392 | ? VVAL_TRUE : VVAL_FALSE; |
| 4393 | clear_tv(tv2); |
| 4394 | --ppconst->pp_used; |
| 4395 | } |
| 4396 | return ret; |
| 4397 | } |
| 4398 | |
| 4399 | generate_ppconst(cctx, ppconst); |
| 4400 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4401 | } |
| 4402 | |
| 4403 | return OK; |
| 4404 | } |
| 4405 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4406 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4407 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4408 | /* |
| 4409 | * Compile || or &&. |
| 4410 | */ |
| 4411 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4412 | compile_and_or( |
| 4413 | char_u **arg, |
| 4414 | cctx_T *cctx, |
| 4415 | char *op, |
| 4416 | ppconst_T *ppconst, |
| 4417 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4418 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4419 | char_u *next; |
| 4420 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4421 | int opchar = *op; |
| 4422 | |
| 4423 | if (p[0] == opchar && p[1] == opchar) |
| 4424 | { |
| 4425 | garray_T *instr = &cctx->ctx_instr; |
| 4426 | garray_T end_ga; |
| 4427 | |
| 4428 | /* |
| 4429 | * Repeat until there is no following "||" or "&&" |
| 4430 | */ |
| 4431 | ga_init2(&end_ga, sizeof(int), 10); |
| 4432 | while (p[0] == opchar && p[1] == opchar) |
| 4433 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4434 | if (next != NULL) |
| 4435 | { |
| 4436 | *arg = next_line_from_context(cctx, TRUE); |
| 4437 | p = skipwhite(*arg); |
| 4438 | } |
| 4439 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4440 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4441 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4442 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4443 | return FAIL; |
| 4444 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4445 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4446 | // TODO: use ppconst if the value is a constant |
| 4447 | generate_ppconst(cctx, ppconst); |
| 4448 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4449 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4450 | { |
| 4451 | ga_clear(&end_ga); |
| 4452 | return FAIL; |
| 4453 | } |
| 4454 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4455 | ++end_ga.ga_len; |
| 4456 | generate_JUMP(cctx, opchar == '|' |
| 4457 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 4458 | |
| 4459 | // eval the next expression |
| 4460 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4461 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4462 | return FAIL; |
| 4463 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4464 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4465 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4466 | { |
| 4467 | ga_clear(&end_ga); |
| 4468 | return FAIL; |
| 4469 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4470 | |
| 4471 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4472 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4473 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4474 | |
| 4475 | // Fill in the end label in all jumps. |
| 4476 | while (end_ga.ga_len > 0) |
| 4477 | { |
| 4478 | isn_T *isn; |
| 4479 | |
| 4480 | --end_ga.ga_len; |
| 4481 | isn = ((isn_T *)instr->ga_data) |
| 4482 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4483 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4484 | } |
| 4485 | ga_clear(&end_ga); |
| 4486 | } |
| 4487 | |
| 4488 | return OK; |
| 4489 | } |
| 4490 | |
| 4491 | /* |
| 4492 | * expr4a && expr4a && expr4a logical AND |
| 4493 | * |
| 4494 | * Produces instructions: |
| 4495 | * EVAL expr4a Push result of "expr4a" |
| 4496 | * JUMP_AND_KEEP_IF_FALSE end |
| 4497 | * EVAL expr4b Push result of "expr4b" |
| 4498 | * JUMP_AND_KEEP_IF_FALSE end |
| 4499 | * EVAL expr4c Push result of "expr4c" |
| 4500 | * end: |
| 4501 | */ |
| 4502 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4503 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4504 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4505 | int ppconst_used = ppconst->pp_used; |
| 4506 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4507 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4508 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4509 | return FAIL; |
| 4510 | |
| 4511 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4512 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4513 | } |
| 4514 | |
| 4515 | /* |
| 4516 | * expr3a || expr3b || expr3c logical OR |
| 4517 | * |
| 4518 | * Produces instructions: |
| 4519 | * EVAL expr3a Push result of "expr3a" |
| 4520 | * JUMP_AND_KEEP_IF_TRUE end |
| 4521 | * EVAL expr3b Push result of "expr3b" |
| 4522 | * JUMP_AND_KEEP_IF_TRUE end |
| 4523 | * EVAL expr3c Push result of "expr3c" |
| 4524 | * end: |
| 4525 | */ |
| 4526 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4527 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4528 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4529 | int ppconst_used = ppconst->pp_used; |
| 4530 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4531 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4532 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4533 | return FAIL; |
| 4534 | |
| 4535 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4536 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4537 | } |
| 4538 | |
| 4539 | /* |
| 4540 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4541 | * |
| 4542 | * Produces instructions: |
| 4543 | * EVAL expr2 Push result of "expr" |
| 4544 | * JUMP_IF_FALSE alt jump if false |
| 4545 | * EVAL expr1a |
| 4546 | * JUMP_ALWAYS end |
| 4547 | * alt: EVAL expr1b |
| 4548 | * end: |
| 4549 | */ |
| 4550 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4551 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4552 | { |
| 4553 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4554 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4555 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4556 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4557 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4558 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4559 | return FAIL; |
| 4560 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4561 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4562 | if (*p == '?') |
| 4563 | { |
| 4564 | garray_T *instr = &cctx->ctx_instr; |
| 4565 | garray_T *stack = &cctx->ctx_type_stack; |
| 4566 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4567 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4568 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4569 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4570 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4571 | int has_const_expr = FALSE; |
| 4572 | int const_value = FALSE; |
| 4573 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4574 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4575 | if (next != NULL) |
| 4576 | { |
| 4577 | *arg = next_line_from_context(cctx, TRUE); |
| 4578 | p = skipwhite(*arg); |
| 4579 | } |
| 4580 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4581 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4582 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4583 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4584 | return FAIL; |
| 4585 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4586 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4587 | if (ppconst->pp_used == ppconst_used + 1) |
| 4588 | { |
| 4589 | // the condition is a constant, we know whether the ? or the : |
| 4590 | // expression is to be evaluated. |
| 4591 | has_const_expr = TRUE; |
| 4592 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4593 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4594 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4595 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4596 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4597 | } |
| 4598 | else |
| 4599 | { |
| 4600 | generate_ppconst(cctx, ppconst); |
| 4601 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4602 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4603 | |
| 4604 | // evaluate the second expression; any type is accepted |
| 4605 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4606 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4607 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4608 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4609 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4610 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4611 | if (!has_const_expr) |
| 4612 | { |
| 4613 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4614 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4615 | // remember the type and drop it |
| 4616 | --stack->ga_len; |
| 4617 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4618 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4619 | end_idx = instr->ga_len; |
| 4620 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4621 | |
| 4622 | // jump here from JUMP_IF_FALSE |
| 4623 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4624 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4625 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4626 | |
| 4627 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4628 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4629 | if (*p != ':') |
| 4630 | { |
| 4631 | emsg(_(e_missing_colon)); |
| 4632 | return FAIL; |
| 4633 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4634 | if (next != NULL) |
| 4635 | { |
| 4636 | *arg = next_line_from_context(cctx, TRUE); |
| 4637 | p = skipwhite(*arg); |
| 4638 | } |
| 4639 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4640 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4641 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4642 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4643 | return FAIL; |
| 4644 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4645 | |
| 4646 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4647 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4648 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4649 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4650 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4651 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4652 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4653 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4654 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4655 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4656 | if (!has_const_expr) |
| 4657 | { |
| 4658 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4659 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4660 | // If the types differ, the result has a more generic type. |
| 4661 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4662 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4663 | |
| 4664 | // jump here from JUMP_ALWAYS |
| 4665 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4666 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4667 | } |
| 4668 | |
| 4669 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4670 | } |
| 4671 | return OK; |
| 4672 | } |
| 4673 | |
| 4674 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4675 | * Toplevel expression. |
| 4676 | */ |
| 4677 | static int |
| 4678 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4679 | { |
| 4680 | ppconst_T ppconst; |
| 4681 | |
| 4682 | CLEAR_FIELD(ppconst); |
| 4683 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4684 | { |
| 4685 | clear_ppconst(&ppconst); |
| 4686 | return FAIL; |
| 4687 | } |
| 4688 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4689 | return FAIL; |
| 4690 | return OK; |
| 4691 | } |
| 4692 | |
| 4693 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4694 | * compile "return [expr]" |
| 4695 | */ |
| 4696 | static char_u * |
| 4697 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4698 | { |
| 4699 | char_u *p = arg; |
| 4700 | garray_T *stack = &cctx->ctx_type_stack; |
| 4701 | type_T *stack_type; |
| 4702 | |
| 4703 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4704 | { |
| 4705 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4706 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4707 | return NULL; |
| 4708 | |
| 4709 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4710 | if (set_return_type) |
| 4711 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4712 | else |
| 4713 | { |
| 4714 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4715 | && stack_type->tt_type != VAR_VOID |
| 4716 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4717 | { |
| 4718 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4719 | return NULL; |
| 4720 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4721 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4722 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4723 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4724 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4725 | } |
| 4726 | else |
| 4727 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4728 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4729 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4730 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4731 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4732 | { |
| 4733 | emsg(_("E1003: Missing return value")); |
| 4734 | return NULL; |
| 4735 | } |
| 4736 | |
| 4737 | // No argument, return zero. |
| 4738 | generate_PUSHNR(cctx, 0); |
| 4739 | } |
| 4740 | |
| 4741 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4742 | return NULL; |
| 4743 | |
| 4744 | // "return val | endif" is possible |
| 4745 | return skipwhite(p); |
| 4746 | } |
| 4747 | |
| 4748 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4749 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4750 | * Return a pointer to the line in allocated memory. |
| 4751 | * Return NULL for end-of-file or some error. |
| 4752 | */ |
| 4753 | static char_u * |
| 4754 | exarg_getline( |
| 4755 | int c UNUSED, |
| 4756 | void *cookie, |
| 4757 | int indent UNUSED, |
| 4758 | int do_concat UNUSED) |
| 4759 | { |
| 4760 | cctx_T *cctx = (cctx_T *)cookie; |
| 4761 | |
| 4762 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4763 | { |
| 4764 | iemsg("Heredoc got to end"); |
| 4765 | return NULL; |
| 4766 | } |
| 4767 | ++cctx->ctx_lnum; |
| 4768 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4769 | [cctx->ctx_lnum]); |
| 4770 | } |
| 4771 | |
| 4772 | /* |
| 4773 | * Compile a nested :def command. |
| 4774 | */ |
| 4775 | static char_u * |
| 4776 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4777 | { |
| 4778 | char_u *name_start = eap->arg; |
| 4779 | char_u *name_end = to_name_end(eap->arg, FALSE); |
| 4780 | char_u *name = get_lambda_name(); |
| 4781 | lvar_T *lvar; |
| 4782 | ufunc_T *ufunc; |
| 4783 | |
| 4784 | eap->arg = name_end; |
| 4785 | eap->getline = exarg_getline; |
| 4786 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4787 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4788 | eap->forceit = FALSE; |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4789 | ufunc = def_function(eap, name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4790 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4791 | if (ufunc == NULL) |
| 4792 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4793 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4794 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4795 | return NULL; |
| 4796 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4797 | // Define a local variable for the function reference. |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4798 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4799 | TRUE, ufunc->uf_func_type); |
| 4800 | |
| 4801 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL |
| 4802 | || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL) |
| 4803 | return NULL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4804 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4805 | // TODO: warning for trailing text? |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4806 | return (char_u *)""; |
| 4807 | } |
| 4808 | |
| 4809 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4810 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4811 | */ |
| 4812 | int |
| 4813 | assignment_len(char_u *p, int *heredoc) |
| 4814 | { |
| 4815 | if (*p == '=') |
| 4816 | { |
| 4817 | if (p[1] == '<' && p[2] == '<') |
| 4818 | { |
| 4819 | *heredoc = TRUE; |
| 4820 | return 3; |
| 4821 | } |
| 4822 | return 1; |
| 4823 | } |
| 4824 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4825 | return 2; |
| 4826 | if (STRNCMP(p, "..=", 3) == 0) |
| 4827 | return 3; |
| 4828 | return 0; |
| 4829 | } |
| 4830 | |
| 4831 | // words that cannot be used as a variable |
| 4832 | static char *reserved[] = { |
| 4833 | "true", |
| 4834 | "false", |
| 4835 | NULL |
| 4836 | }; |
| 4837 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4838 | typedef enum { |
| 4839 | dest_local, |
| 4840 | dest_option, |
| 4841 | dest_env, |
| 4842 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4843 | dest_buffer, |
| 4844 | dest_window, |
| 4845 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4846 | dest_vimvar, |
| 4847 | dest_script, |
| 4848 | dest_reg, |
| 4849 | } assign_dest_T; |
| 4850 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4851 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4852 | * Generate the load instruction for "name". |
| 4853 | */ |
| 4854 | static void |
| 4855 | generate_loadvar( |
| 4856 | cctx_T *cctx, |
| 4857 | assign_dest_T dest, |
| 4858 | char_u *name, |
| 4859 | lvar_T *lvar, |
| 4860 | type_T *type) |
| 4861 | { |
| 4862 | switch (dest) |
| 4863 | { |
| 4864 | case dest_option: |
| 4865 | // TODO: check the option exists |
| 4866 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4867 | break; |
| 4868 | case dest_global: |
| 4869 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4870 | break; |
| 4871 | case dest_buffer: |
| 4872 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4873 | break; |
| 4874 | case dest_window: |
| 4875 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4876 | break; |
| 4877 | case dest_tab: |
| 4878 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4879 | break; |
| 4880 | case dest_script: |
| 4881 | compile_load_scriptvar(cctx, |
| 4882 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4883 | break; |
| 4884 | case dest_env: |
| 4885 | // Include $ in the name here |
| 4886 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4887 | break; |
| 4888 | case dest_reg: |
| 4889 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4890 | break; |
| 4891 | case dest_vimvar: |
| 4892 | generate_LOADV(cctx, name + 2, TRUE); |
| 4893 | break; |
| 4894 | case dest_local: |
| 4895 | if (lvar->lv_from_outer) |
| 4896 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4897 | NULL, type); |
| 4898 | else |
| 4899 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4900 | break; |
| 4901 | } |
| 4902 | } |
| 4903 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4904 | void |
| 4905 | vim9_declare_error(char_u *name) |
| 4906 | { |
| 4907 | char *scope = ""; |
| 4908 | |
| 4909 | switch (*name) |
| 4910 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4911 | case 'g': scope = _("global"); break; |
| 4912 | case 'b': scope = _("buffer"); break; |
| 4913 | case 'w': scope = _("window"); break; |
| 4914 | case 't': scope = _("tab"); break; |
| 4915 | case 'v': scope = "v:"; break; |
| 4916 | case '$': semsg(_(e_declare_env_var), name); return; |
| 4917 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4918 | } |
| 4919 | semsg(_(e_declare_var), scope, name); |
| 4920 | } |
| 4921 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4922 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4923 | * Compile declaration and assignment: |
| 4924 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4925 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4926 | * Return NULL for an error. |
| 4927 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4928 | */ |
| 4929 | static char_u * |
| 4930 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4931 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4932 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4933 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4934 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4935 | char_u *ret = NULL; |
| 4936 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4937 | int var_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4938 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4939 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4940 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4941 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4942 | int oplen = 0; |
| 4943 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4944 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4945 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4946 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4947 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4948 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4949 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4950 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 4951 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 4952 | if (p == NULL) |
| 4953 | return *arg == '[' ? arg : NULL; |
| 4954 | |
| 4955 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4956 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4957 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4958 | return NULL; |
| 4959 | } |
| 4960 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4961 | sp = p; |
| 4962 | p = skipwhite(p); |
| 4963 | op = p; |
| 4964 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4965 | |
| 4966 | if (var_count > 0 && oplen == 0) |
| 4967 | // can be something like "[1, 2]->func()" |
| 4968 | return arg; |
| 4969 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4970 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4971 | { |
| 4972 | char_u buf[4]; |
| 4973 | |
| 4974 | vim_strncpy(buf, op, oplen); |
| 4975 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4976 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4977 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4978 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4979 | if (heredoc) |
| 4980 | { |
| 4981 | list_T *l; |
| 4982 | listitem_T *li; |
| 4983 | |
| 4984 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4985 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4986 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 4987 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4988 | |
| 4989 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 4990 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4991 | { |
| 4992 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4993 | li->li_tv.vval.v_string = NULL; |
| 4994 | } |
| 4995 | generate_NEWLIST(cctx, l->lv_len); |
| 4996 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4997 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4998 | list_free(l); |
| 4999 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5000 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5001 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5002 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5003 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5004 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 5005 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5006 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5007 | p = skipwhite(op + oplen); |
| 5008 | if (compile_expr0(&p, cctx) == FAIL) |
| 5009 | return NULL; |
| 5010 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5011 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5012 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5013 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5014 | type_T *stacktype; |
| 5015 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5016 | stacktype = stack->ga_len == 0 ? &t_void |
| 5017 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5018 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5019 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5020 | emsg(_(e_cannot_use_void)); |
| 5021 | goto theend; |
| 5022 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5023 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5024 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5025 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5026 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 5027 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5028 | } |
| 5029 | } |
| 5030 | |
| 5031 | /* |
| 5032 | * Loop over variables in "[var, var] = expr". |
| 5033 | * For "var = expr" and "let var: type" this is done only once. |
| 5034 | */ |
| 5035 | if (var_count > 0) |
| 5036 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 5037 | else |
| 5038 | var_start = arg; |
| 5039 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 5040 | { |
| 5041 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 5042 | size_t varlen; |
| 5043 | int new_local = FALSE; |
| 5044 | int opt_type; |
| 5045 | int opt_flags = 0; |
| 5046 | assign_dest_T dest = dest_local; |
| 5047 | int vimvaridx = -1; |
| 5048 | lvar_T *lvar = NULL; |
| 5049 | lvar_T arg_lvar; |
| 5050 | int has_type = FALSE; |
| 5051 | int has_index = FALSE; |
| 5052 | int instr_count = -1; |
| 5053 | |
| 5054 | p = (*var_start == '&' || *var_start == '$' |
| 5055 | || *var_start == '@') ? var_start + 1 : var_start; |
| 5056 | p = to_name_end(p, TRUE); |
| 5057 | |
| 5058 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 5059 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 5060 | --var_end; |
| 5061 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 5062 | --p; |
| 5063 | |
| 5064 | varlen = p - var_start; |
| 5065 | vim_free(name); |
| 5066 | name = vim_strnsave(var_start, varlen); |
| 5067 | if (name == NULL) |
| 5068 | return NULL; |
| 5069 | if (!heredoc) |
| 5070 | type = &t_any; |
| 5071 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5072 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5073 | { |
| 5074 | if (*var_start == '&') |
| 5075 | { |
| 5076 | int cc; |
| 5077 | long numval; |
| 5078 | |
| 5079 | dest = dest_option; |
| 5080 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5081 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5082 | emsg(_(e_const_option)); |
| 5083 | goto theend; |
| 5084 | } |
| 5085 | if (is_decl) |
| 5086 | { |
| 5087 | semsg(_("E1052: Cannot declare an option: %s"), var_start); |
| 5088 | goto theend; |
| 5089 | } |
| 5090 | p = var_start; |
| 5091 | p = find_option_end(&p, &opt_flags); |
| 5092 | if (p == NULL) |
| 5093 | { |
| 5094 | // cannot happen? |
| 5095 | emsg(_(e_letunexp)); |
| 5096 | goto theend; |
| 5097 | } |
| 5098 | cc = *p; |
| 5099 | *p = NUL; |
| 5100 | opt_type = get_option_value(var_start + 1, &numval, |
| 5101 | NULL, opt_flags); |
| 5102 | *p = cc; |
| 5103 | if (opt_type == -3) |
| 5104 | { |
| 5105 | semsg(_(e_unknown_option), var_start); |
| 5106 | goto theend; |
| 5107 | } |
| 5108 | if (opt_type == -2 || opt_type == 0) |
| 5109 | type = &t_string; |
| 5110 | else |
| 5111 | type = &t_number; // both number and boolean option |
| 5112 | } |
| 5113 | else if (*var_start == '$') |
| 5114 | { |
| 5115 | dest = dest_env; |
| 5116 | type = &t_string; |
| 5117 | if (is_decl) |
| 5118 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5119 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5120 | goto theend; |
| 5121 | } |
| 5122 | } |
| 5123 | else if (*var_start == '@') |
| 5124 | { |
| 5125 | if (!valid_yank_reg(var_start[1], TRUE)) |
| 5126 | { |
| 5127 | emsg_invreg(var_start[1]); |
| 5128 | goto theend; |
| 5129 | } |
| 5130 | dest = dest_reg; |
| 5131 | type = &t_string; |
| 5132 | if (is_decl) |
| 5133 | { |
| 5134 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 5135 | goto theend; |
| 5136 | } |
| 5137 | } |
| 5138 | else if (STRNCMP(var_start, "g:", 2) == 0) |
| 5139 | { |
| 5140 | dest = dest_global; |
| 5141 | if (is_decl) |
| 5142 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5143 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5144 | goto theend; |
| 5145 | } |
| 5146 | } |
| 5147 | else if (STRNCMP(var_start, "b:", 2) == 0) |
| 5148 | { |
| 5149 | dest = dest_buffer; |
| 5150 | if (is_decl) |
| 5151 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5152 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5153 | goto theend; |
| 5154 | } |
| 5155 | } |
| 5156 | else if (STRNCMP(var_start, "w:", 2) == 0) |
| 5157 | { |
| 5158 | dest = dest_window; |
| 5159 | if (is_decl) |
| 5160 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5161 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5162 | goto theend; |
| 5163 | } |
| 5164 | } |
| 5165 | else if (STRNCMP(var_start, "t:", 2) == 0) |
| 5166 | { |
| 5167 | dest = dest_tab; |
| 5168 | if (is_decl) |
| 5169 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5170 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5171 | goto theend; |
| 5172 | } |
| 5173 | } |
| 5174 | else if (STRNCMP(var_start, "v:", 2) == 0) |
| 5175 | { |
| 5176 | typval_T *vtv; |
| 5177 | int di_flags; |
| 5178 | |
| 5179 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5180 | if (vimvaridx < 0) |
| 5181 | { |
| 5182 | semsg(_(e_var_notfound), var_start); |
| 5183 | goto theend; |
| 5184 | } |
| 5185 | // We use the current value of "sandbox" here, is that OK? |
| 5186 | if (var_check_ro(di_flags, name, FALSE)) |
| 5187 | goto theend; |
| 5188 | dest = dest_vimvar; |
| 5189 | vtv = get_vim_var_tv(vimvaridx); |
| 5190 | type = typval2type(vtv); |
| 5191 | if (is_decl) |
| 5192 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5193 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5194 | goto theend; |
| 5195 | } |
| 5196 | } |
| 5197 | else |
| 5198 | { |
| 5199 | int idx; |
| 5200 | |
| 5201 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5202 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5203 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5204 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5205 | goto theend; |
| 5206 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5207 | |
| 5208 | lvar = lookup_local(var_start, varlen, cctx); |
| 5209 | if (lvar == NULL) |
| 5210 | { |
| 5211 | CLEAR_FIELD(arg_lvar); |
| 5212 | if (lookup_arg(var_start, varlen, |
| 5213 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 5214 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5215 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5216 | if (is_decl) |
| 5217 | { |
| 5218 | semsg(_(e_used_as_arg), name); |
| 5219 | goto theend; |
| 5220 | } |
| 5221 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5222 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5223 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5224 | if (lvar != NULL) |
| 5225 | { |
| 5226 | if (is_decl) |
| 5227 | { |
| 5228 | semsg(_("E1017: Variable already declared: %s"), name); |
| 5229 | goto theend; |
| 5230 | } |
| 5231 | else if (lvar->lv_const) |
| 5232 | { |
| 5233 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 5234 | name); |
| 5235 | goto theend; |
| 5236 | } |
| 5237 | } |
| 5238 | else if (STRNCMP(var_start, "s:", 2) == 0 |
| 5239 | || lookup_script(var_start, varlen) == OK |
| 5240 | || find_imported(var_start, varlen, cctx) != NULL) |
| 5241 | { |
| 5242 | dest = dest_script; |
| 5243 | if (is_decl) |
| 5244 | { |
| 5245 | semsg(_("E1054: Variable already declared in the script: %s"), |
| 5246 | name); |
| 5247 | goto theend; |
| 5248 | } |
| 5249 | } |
| 5250 | else if (name[1] == ':' && name[2] != NUL) |
| 5251 | { |
| 5252 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
| 5253 | name); |
| 5254 | goto theend; |
| 5255 | } |
| 5256 | else if (!is_decl) |
| 5257 | { |
| 5258 | semsg(_("E1089: unknown variable: %s"), name); |
| 5259 | goto theend; |
| 5260 | } |
| 5261 | } |
| 5262 | } |
| 5263 | |
| 5264 | // handle "a:name" as a name, not index "name" on "a" |
| 5265 | if (varlen > 1 || var_start[varlen] != ':') |
| 5266 | p = var_end; |
| 5267 | |
| 5268 | if (dest != dest_option) |
| 5269 | { |
| 5270 | if (is_decl && *p == ':') |
| 5271 | { |
| 5272 | // parse optional type: "let var: type = expr" |
| 5273 | if (!VIM_ISWHITE(p[1])) |
| 5274 | { |
| 5275 | semsg(_(e_white_after), ":"); |
| 5276 | goto theend; |
| 5277 | } |
| 5278 | p = skipwhite(p + 1); |
| 5279 | type = parse_type(&p, cctx->ctx_type_list); |
| 5280 | has_type = TRUE; |
| 5281 | } |
| 5282 | else if (lvar != NULL) |
| 5283 | type = lvar->lv_type; |
| 5284 | } |
| 5285 | |
| 5286 | if (oplen == 3 && !heredoc && dest != dest_global |
| 5287 | && type->tt_type != VAR_STRING |
| 5288 | && type->tt_type != VAR_ANY) |
| 5289 | { |
| 5290 | emsg(_("E1019: Can only concatenate to string")); |
| 5291 | goto theend; |
| 5292 | } |
| 5293 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5294 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5295 | { |
| 5296 | if (oplen > 1 && !heredoc) |
| 5297 | { |
| 5298 | // +=, /=, etc. require an existing variable |
| 5299 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 5300 | name); |
| 5301 | goto theend; |
| 5302 | } |
| 5303 | |
| 5304 | // new local variable |
| 5305 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
| 5306 | goto theend; |
| 5307 | lvar = reserve_local(cctx, var_start, varlen, |
| 5308 | cmdidx == CMD_const, type); |
| 5309 | if (lvar == NULL) |
| 5310 | goto theend; |
| 5311 | new_local = TRUE; |
| 5312 | } |
| 5313 | |
| 5314 | member_type = type; |
| 5315 | if (var_end > var_start + varlen) |
| 5316 | { |
| 5317 | // Something follows after the variable: "var[idx]". |
| 5318 | if (is_decl) |
| 5319 | { |
| 5320 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 5321 | goto theend; |
| 5322 | } |
| 5323 | |
| 5324 | if (var_start[varlen] == '[') |
| 5325 | { |
| 5326 | has_index = TRUE; |
| 5327 | if (type->tt_member == NULL) |
| 5328 | { |
| 5329 | semsg(_("E1088: cannot use an index on %s"), name); |
| 5330 | goto theend; |
| 5331 | } |
| 5332 | member_type = type->tt_member; |
| 5333 | } |
| 5334 | else |
| 5335 | { |
| 5336 | semsg("Not supported yet: %s", var_start); |
| 5337 | goto theend; |
| 5338 | } |
| 5339 | } |
| 5340 | else if (lvar == &arg_lvar) |
| 5341 | { |
| 5342 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 5343 | goto theend; |
| 5344 | } |
| 5345 | |
| 5346 | if (!heredoc) |
| 5347 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5348 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5349 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5350 | if (oplen > 0 && var_count == 0) |
| 5351 | { |
| 5352 | // skip over the "=" and the expression |
| 5353 | p = skipwhite(op + oplen); |
| 5354 | compile_expr0(&p, cctx); |
| 5355 | } |
| 5356 | } |
| 5357 | else if (oplen > 0) |
| 5358 | { |
| 5359 | type_T *stacktype; |
| 5360 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5361 | // For "var = expr" evaluate the expression. |
| 5362 | if (var_count == 0) |
| 5363 | { |
| 5364 | int r; |
| 5365 | |
| 5366 | // for "+=", "*=", "..=" etc. first load the current value |
| 5367 | if (*op != '=') |
| 5368 | { |
| 5369 | generate_loadvar(cctx, dest, name, lvar, type); |
| 5370 | |
| 5371 | if (has_index) |
| 5372 | { |
| 5373 | // TODO: get member from list or dict |
| 5374 | emsg("Index with operation not supported yet"); |
| 5375 | goto theend; |
| 5376 | } |
| 5377 | } |
| 5378 | |
| 5379 | // Compile the expression. Temporarily hide the new local |
| 5380 | // variable here, it is not available to this expression. |
| 5381 | if (new_local) |
| 5382 | --cctx->ctx_locals.ga_len; |
| 5383 | instr_count = instr->ga_len; |
| 5384 | p = skipwhite(op + oplen); |
| 5385 | r = compile_expr0(&p, cctx); |
| 5386 | if (new_local) |
| 5387 | ++cctx->ctx_locals.ga_len; |
| 5388 | if (r == FAIL) |
| 5389 | goto theend; |
| 5390 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5391 | else if (semicolon && var_idx == var_count - 1) |
| 5392 | { |
| 5393 | // For "[var; var] = expr" get the rest of the list |
| 5394 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5395 | goto theend; |
| 5396 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5397 | else |
| 5398 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5399 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5400 | // list. |
| 5401 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 5402 | return FAIL; |
| 5403 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5404 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5405 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5406 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5407 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5408 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5409 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5410 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5411 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5412 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5413 | emsg(_(e_cannot_use_void)); |
| 5414 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5415 | } |
| 5416 | else |
| 5417 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5418 | // An empty list or dict has a &t_void member, |
| 5419 | // for a variable that implies &t_any. |
| 5420 | if (stacktype == &t_list_empty) |
| 5421 | lvar->lv_type = &t_list_any; |
| 5422 | else if (stacktype == &t_dict_empty) |
| 5423 | lvar->lv_type = &t_dict_any; |
| 5424 | else |
| 5425 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5426 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5427 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5428 | else |
| 5429 | { |
| 5430 | type_T *use_type = lvar->lv_type; |
| 5431 | |
| 5432 | if (has_index) |
| 5433 | { |
| 5434 | use_type = use_type->tt_member; |
| 5435 | if (use_type == NULL) |
| 5436 | use_type = &t_void; |
| 5437 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5438 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5439 | == FAIL) |
| 5440 | goto theend; |
| 5441 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5442 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5443 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5444 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5445 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5446 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5447 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5448 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5449 | emsg(_(e_const_req_value)); |
| 5450 | goto theend; |
| 5451 | } |
| 5452 | else if (!has_type || dest == dest_option) |
| 5453 | { |
| 5454 | emsg(_(e_type_req)); |
| 5455 | goto theend; |
| 5456 | } |
| 5457 | else |
| 5458 | { |
| 5459 | // variables are always initialized |
| 5460 | if (ga_grow(instr, 1) == FAIL) |
| 5461 | goto theend; |
| 5462 | switch (member_type->tt_type) |
| 5463 | { |
| 5464 | case VAR_BOOL: |
| 5465 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5466 | break; |
| 5467 | case VAR_FLOAT: |
| 5468 | #ifdef FEAT_FLOAT |
| 5469 | generate_PUSHF(cctx, 0.0); |
| 5470 | #endif |
| 5471 | break; |
| 5472 | case VAR_STRING: |
| 5473 | generate_PUSHS(cctx, NULL); |
| 5474 | break; |
| 5475 | case VAR_BLOB: |
| 5476 | generate_PUSHBLOB(cctx, NULL); |
| 5477 | break; |
| 5478 | case VAR_FUNC: |
| 5479 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5480 | break; |
| 5481 | case VAR_LIST: |
| 5482 | generate_NEWLIST(cctx, 0); |
| 5483 | break; |
| 5484 | case VAR_DICT: |
| 5485 | generate_NEWDICT(cctx, 0); |
| 5486 | break; |
| 5487 | case VAR_JOB: |
| 5488 | generate_PUSHJOB(cctx, NULL); |
| 5489 | break; |
| 5490 | case VAR_CHANNEL: |
| 5491 | generate_PUSHCHANNEL(cctx, NULL); |
| 5492 | break; |
| 5493 | case VAR_NUMBER: |
| 5494 | case VAR_UNKNOWN: |
| 5495 | case VAR_ANY: |
| 5496 | case VAR_PARTIAL: |
| 5497 | case VAR_VOID: |
| 5498 | case VAR_SPECIAL: // cannot happen |
| 5499 | generate_PUSHNR(cctx, 0); |
| 5500 | break; |
| 5501 | } |
| 5502 | } |
| 5503 | if (var_count == 0) |
| 5504 | end = p; |
| 5505 | } |
| 5506 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5507 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5508 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5509 | break; |
| 5510 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5511 | if (oplen > 0 && *op != '=') |
| 5512 | { |
| 5513 | type_T *expected = &t_number; |
| 5514 | type_T *stacktype; |
| 5515 | |
| 5516 | // TODO: if type is known use float or any operation |
| 5517 | |
| 5518 | if (*op == '.') |
| 5519 | expected = &t_string; |
| 5520 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5521 | if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5522 | goto theend; |
| 5523 | |
| 5524 | if (*op == '.') |
| 5525 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 5526 | else |
| 5527 | { |
| 5528 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 5529 | |
| 5530 | if (isn == NULL) |
| 5531 | goto theend; |
| 5532 | switch (*op) |
| 5533 | { |
| 5534 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 5535 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 5536 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 5537 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 5538 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 5539 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5540 | } |
| 5541 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5542 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5543 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5544 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5545 | int r; |
| 5546 | |
| 5547 | // Compile the "idx" in "var[idx]". |
| 5548 | if (new_local) |
| 5549 | --cctx->ctx_locals.ga_len; |
| 5550 | p = skipwhite(var_start + varlen + 1); |
| 5551 | r = compile_expr0(&p, cctx); |
| 5552 | if (new_local) |
| 5553 | ++cctx->ctx_locals.ga_len; |
| 5554 | if (r == FAIL) |
| 5555 | goto theend; |
| 5556 | if (*skipwhite(p) != ']') |
| 5557 | { |
| 5558 | emsg(_(e_missbrac)); |
| 5559 | goto theend; |
| 5560 | } |
| 5561 | if (type->tt_type == VAR_DICT |
| 5562 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5563 | goto theend; |
| 5564 | if (type->tt_type == VAR_LIST |
| 5565 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5566 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5567 | { |
| 5568 | emsg(_(e_number_exp)); |
| 5569 | goto theend; |
| 5570 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5571 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5572 | // Load the dict or list. On the stack we then have: |
| 5573 | // - value |
| 5574 | // - index |
| 5575 | // - variable |
| 5576 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5577 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5578 | if (type->tt_type == VAR_LIST) |
| 5579 | { |
| 5580 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5581 | return FAIL; |
| 5582 | } |
| 5583 | else if (type->tt_type == VAR_DICT) |
| 5584 | { |
| 5585 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5586 | return FAIL; |
| 5587 | } |
| 5588 | else |
| 5589 | { |
| 5590 | emsg(_(e_listreq)); |
| 5591 | goto theend; |
| 5592 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5593 | } |
| 5594 | else |
| 5595 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5596 | switch (dest) |
| 5597 | { |
| 5598 | case dest_option: |
| 5599 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5600 | break; |
| 5601 | case dest_global: |
| 5602 | // include g: with the name, easier to execute that way |
| 5603 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5604 | break; |
| 5605 | case dest_buffer: |
| 5606 | // include b: with the name, easier to execute that way |
| 5607 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5608 | break; |
| 5609 | case dest_window: |
| 5610 | // include w: with the name, easier to execute that way |
| 5611 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5612 | break; |
| 5613 | case dest_tab: |
| 5614 | // include t: with the name, easier to execute that way |
| 5615 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5616 | break; |
| 5617 | case dest_env: |
| 5618 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5619 | break; |
| 5620 | case dest_reg: |
| 5621 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5622 | break; |
| 5623 | case dest_vimvar: |
| 5624 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5625 | break; |
| 5626 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5627 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5628 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5629 | imported_T *import = NULL; |
| 5630 | int sid = current_sctx.sc_sid; |
| 5631 | int idx; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5632 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5633 | if (name[1] != ':') |
| 5634 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5635 | import = find_imported(name, 0, cctx); |
| 5636 | if (import != NULL) |
| 5637 | sid = import->imp_sid; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5638 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5639 | |
| 5640 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 5641 | // TODO: specific type |
| 5642 | if (idx < 0) |
| 5643 | { |
| 5644 | char_u *name_s = name; |
| 5645 | |
| 5646 | // Include s: in the name for store_var() |
| 5647 | if (name[1] != ':') |
| 5648 | { |
| 5649 | int len = (int)STRLEN(name) + 3; |
| 5650 | |
| 5651 | name_s = alloc(len); |
| 5652 | if (name_s == NULL) |
| 5653 | name_s = name; |
| 5654 | else |
| 5655 | vim_snprintf((char *)name_s, len, |
| 5656 | "s:%s", name); |
| 5657 | } |
| 5658 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5659 | &t_any); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5660 | if (name_s != name) |
| 5661 | vim_free(name_s); |
| 5662 | } |
| 5663 | else |
| 5664 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5665 | sid, idx, &t_any); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5666 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5667 | break; |
| 5668 | case dest_local: |
| 5669 | if (lvar != NULL) |
| 5670 | { |
| 5671 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5672 | + instr->ga_len - 1; |
| 5673 | |
| 5674 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5675 | // ISN_STORE into ISN_STORENR |
| 5676 | if (!lvar->lv_from_outer |
| 5677 | && instr->ga_len == instr_count + 1 |
| 5678 | && isn->isn_type == ISN_PUSHNR) |
| 5679 | { |
| 5680 | varnumber_T val = isn->isn_arg.number; |
| 5681 | |
| 5682 | isn->isn_type = ISN_STORENR; |
| 5683 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5684 | isn->isn_arg.storenr.stnr_val = val; |
| 5685 | if (stack->ga_len > 0) |
| 5686 | --stack->ga_len; |
| 5687 | } |
| 5688 | else if (lvar->lv_from_outer) |
| 5689 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5690 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5691 | else |
| 5692 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5693 | } |
| 5694 | break; |
| 5695 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5696 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5697 | |
| 5698 | if (var_idx + 1 < var_count) |
| 5699 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5700 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5701 | |
| 5702 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5703 | if (var_count > 0 && !semicolon) |
| 5704 | { |
| 5705 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5706 | goto theend; |
| 5707 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5708 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame^] | 5709 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5710 | |
| 5711 | theend: |
| 5712 | vim_free(name); |
| 5713 | return ret; |
| 5714 | } |
| 5715 | |
| 5716 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5717 | * Check if "name" can be "unlet". |
| 5718 | */ |
| 5719 | int |
| 5720 | check_vim9_unlet(char_u *name) |
| 5721 | { |
| 5722 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5723 | { |
| 5724 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5725 | return FAIL; |
| 5726 | } |
| 5727 | return OK; |
| 5728 | } |
| 5729 | |
| 5730 | /* |
| 5731 | * Callback passed to ex_unletlock(). |
| 5732 | */ |
| 5733 | static int |
| 5734 | compile_unlet( |
| 5735 | lval_T *lvp, |
| 5736 | char_u *name_end, |
| 5737 | exarg_T *eap, |
| 5738 | int deep UNUSED, |
| 5739 | void *coookie) |
| 5740 | { |
| 5741 | cctx_T *cctx = coookie; |
| 5742 | |
| 5743 | if (lvp->ll_tv == NULL) |
| 5744 | { |
| 5745 | char_u *p = lvp->ll_name; |
| 5746 | int cc = *name_end; |
| 5747 | int ret = OK; |
| 5748 | |
| 5749 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5750 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5751 | if (*p == '$') |
| 5752 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5753 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5754 | ret = FAIL; |
| 5755 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5756 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5757 | |
| 5758 | *name_end = cc; |
| 5759 | return ret; |
| 5760 | } |
| 5761 | |
| 5762 | // TODO: unlet {list}[idx] |
| 5763 | // TODO: unlet {dict}[key] |
| 5764 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5765 | return FAIL; |
| 5766 | } |
| 5767 | |
| 5768 | /* |
| 5769 | * compile "unlet var", "lock var" and "unlock var" |
| 5770 | * "arg" points to "var". |
| 5771 | */ |
| 5772 | static char_u * |
| 5773 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5774 | { |
| 5775 | char_u *p = arg; |
| 5776 | |
| 5777 | if (eap->cmdidx != CMD_unlet) |
| 5778 | { |
| 5779 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5780 | return NULL; |
| 5781 | } |
| 5782 | |
| 5783 | if (*p == '!') |
| 5784 | { |
| 5785 | p = skipwhite(p + 1); |
| 5786 | eap->forceit = TRUE; |
| 5787 | } |
| 5788 | |
| 5789 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5790 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5791 | } |
| 5792 | |
| 5793 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5794 | * Compile an :import command. |
| 5795 | */ |
| 5796 | static char_u * |
| 5797 | compile_import(char_u *arg, cctx_T *cctx) |
| 5798 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5799 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5800 | } |
| 5801 | |
| 5802 | /* |
| 5803 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5804 | */ |
| 5805 | static int |
| 5806 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5807 | { |
| 5808 | garray_T *instr = &cctx->ctx_instr; |
| 5809 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5810 | |
| 5811 | if (endlabel == NULL) |
| 5812 | return FAIL; |
| 5813 | endlabel->el_next = *el; |
| 5814 | *el = endlabel; |
| 5815 | endlabel->el_end_label = instr->ga_len; |
| 5816 | |
| 5817 | generate_JUMP(cctx, when, 0); |
| 5818 | return OK; |
| 5819 | } |
| 5820 | |
| 5821 | static void |
| 5822 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5823 | { |
| 5824 | garray_T *instr = &cctx->ctx_instr; |
| 5825 | |
| 5826 | while (*el != NULL) |
| 5827 | { |
| 5828 | endlabel_T *cur = (*el); |
| 5829 | isn_T *isn; |
| 5830 | |
| 5831 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5832 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5833 | *el = cur->el_next; |
| 5834 | vim_free(cur); |
| 5835 | } |
| 5836 | } |
| 5837 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5838 | static void |
| 5839 | compile_free_jump_to_end(endlabel_T **el) |
| 5840 | { |
| 5841 | while (*el != NULL) |
| 5842 | { |
| 5843 | endlabel_T *cur = (*el); |
| 5844 | |
| 5845 | *el = cur->el_next; |
| 5846 | vim_free(cur); |
| 5847 | } |
| 5848 | } |
| 5849 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5850 | /* |
| 5851 | * Create a new scope and set up the generic items. |
| 5852 | */ |
| 5853 | static scope_T * |
| 5854 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5855 | { |
| 5856 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5857 | |
| 5858 | if (scope == NULL) |
| 5859 | return NULL; |
| 5860 | scope->se_outer = cctx->ctx_scope; |
| 5861 | cctx->ctx_scope = scope; |
| 5862 | scope->se_type = type; |
| 5863 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5864 | return scope; |
| 5865 | } |
| 5866 | |
| 5867 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5868 | * Free the current scope and go back to the outer scope. |
| 5869 | */ |
| 5870 | static void |
| 5871 | drop_scope(cctx_T *cctx) |
| 5872 | { |
| 5873 | scope_T *scope = cctx->ctx_scope; |
| 5874 | |
| 5875 | if (scope == NULL) |
| 5876 | { |
| 5877 | iemsg("calling drop_scope() without a scope"); |
| 5878 | return; |
| 5879 | } |
| 5880 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5881 | switch (scope->se_type) |
| 5882 | { |
| 5883 | case IF_SCOPE: |
| 5884 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5885 | case FOR_SCOPE: |
| 5886 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5887 | case WHILE_SCOPE: |
| 5888 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5889 | case TRY_SCOPE: |
| 5890 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5891 | case NO_SCOPE: |
| 5892 | case BLOCK_SCOPE: |
| 5893 | break; |
| 5894 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5895 | vim_free(scope); |
| 5896 | } |
| 5897 | |
| 5898 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5899 | * compile "if expr" |
| 5900 | * |
| 5901 | * "if expr" Produces instructions: |
| 5902 | * EVAL expr Push result of "expr" |
| 5903 | * JUMP_IF_FALSE end |
| 5904 | * ... body ... |
| 5905 | * end: |
| 5906 | * |
| 5907 | * "if expr | else" Produces instructions: |
| 5908 | * EVAL expr Push result of "expr" |
| 5909 | * JUMP_IF_FALSE else |
| 5910 | * ... body ... |
| 5911 | * JUMP_ALWAYS end |
| 5912 | * else: |
| 5913 | * ... body ... |
| 5914 | * end: |
| 5915 | * |
| 5916 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5917 | * EVAL expr Push result of "expr" |
| 5918 | * JUMP_IF_FALSE elseif |
| 5919 | * ... body ... |
| 5920 | * JUMP_ALWAYS end |
| 5921 | * elseif: |
| 5922 | * EVAL expr Push result of "expr" |
| 5923 | * JUMP_IF_FALSE else |
| 5924 | * ... body ... |
| 5925 | * JUMP_ALWAYS end |
| 5926 | * else: |
| 5927 | * ... body ... |
| 5928 | * end: |
| 5929 | */ |
| 5930 | static char_u * |
| 5931 | compile_if(char_u *arg, cctx_T *cctx) |
| 5932 | { |
| 5933 | char_u *p = arg; |
| 5934 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5935 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5936 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5937 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5938 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5939 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5940 | CLEAR_FIELD(ppconst); |
| 5941 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5942 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5943 | clear_ppconst(&ppconst); |
| 5944 | return NULL; |
| 5945 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5946 | if (cctx->ctx_skip == SKIP_YES) |
| 5947 | clear_ppconst(&ppconst); |
| 5948 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5949 | { |
| 5950 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5951 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5952 | clear_ppconst(&ppconst); |
| 5953 | } |
| 5954 | else |
| 5955 | { |
| 5956 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5957 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5958 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5959 | return NULL; |
| 5960 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5961 | |
| 5962 | scope = new_scope(cctx, IF_SCOPE); |
| 5963 | if (scope == NULL) |
| 5964 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5965 | scope->se_skip_save = skip_save; |
| 5966 | // "is_had_return" will be reset if any block does not end in :return |
| 5967 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5968 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5969 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5970 | { |
| 5971 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5972 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5973 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5974 | } |
| 5975 | else |
| 5976 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5977 | |
| 5978 | return p; |
| 5979 | } |
| 5980 | |
| 5981 | static char_u * |
| 5982 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 5983 | { |
| 5984 | char_u *p = arg; |
| 5985 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5986 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5987 | isn_T *isn; |
| 5988 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5989 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5990 | |
| 5991 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5992 | { |
| 5993 | emsg(_(e_elseif_without_if)); |
| 5994 | return NULL; |
| 5995 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5996 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5997 | if (!cctx->ctx_had_return) |
| 5998 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5999 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6000 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6001 | { |
| 6002 | 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] | 6003 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6004 | return NULL; |
| 6005 | // previous "if" or "elseif" jumps here |
| 6006 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6007 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6008 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6009 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6010 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6011 | CLEAR_FIELD(ppconst); |
| 6012 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6013 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6014 | clear_ppconst(&ppconst); |
| 6015 | return NULL; |
| 6016 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6017 | if (scope->se_skip_save == SKIP_YES) |
| 6018 | clear_ppconst(&ppconst); |
| 6019 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6020 | { |
| 6021 | // The expression results in a constant. |
| 6022 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6023 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6024 | clear_ppconst(&ppconst); |
| 6025 | scope->se_u.se_if.is_if_label = -1; |
| 6026 | } |
| 6027 | else |
| 6028 | { |
| 6029 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6030 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6031 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6032 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6033 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6034 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6035 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6036 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6037 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6038 | |
| 6039 | return p; |
| 6040 | } |
| 6041 | |
| 6042 | static char_u * |
| 6043 | compile_else(char_u *arg, cctx_T *cctx) |
| 6044 | { |
| 6045 | char_u *p = arg; |
| 6046 | garray_T *instr = &cctx->ctx_instr; |
| 6047 | isn_T *isn; |
| 6048 | scope_T *scope = cctx->ctx_scope; |
| 6049 | |
| 6050 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6051 | { |
| 6052 | emsg(_(e_else_without_if)); |
| 6053 | return NULL; |
| 6054 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6055 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6056 | if (!cctx->ctx_had_return) |
| 6057 | scope->se_u.se_if.is_had_return = FALSE; |
| 6058 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6059 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6060 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6061 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6062 | // jump from previous block to the end, unless the else block is empty |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6063 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6064 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6065 | if (!cctx->ctx_had_return |
| 6066 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6067 | JUMP_ALWAYS, cctx) == FAIL) |
| 6068 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6069 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6070 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6071 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6072 | { |
| 6073 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6074 | { |
| 6075 | // previous "if" or "elseif" jumps here |
| 6076 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6077 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6078 | scope->se_u.se_if.is_if_label = -1; |
| 6079 | } |
| 6080 | } |
| 6081 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6082 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6083 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6084 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6085 | |
| 6086 | return p; |
| 6087 | } |
| 6088 | |
| 6089 | static char_u * |
| 6090 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6091 | { |
| 6092 | scope_T *scope = cctx->ctx_scope; |
| 6093 | ifscope_T *ifscope; |
| 6094 | garray_T *instr = &cctx->ctx_instr; |
| 6095 | isn_T *isn; |
| 6096 | |
| 6097 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6098 | { |
| 6099 | emsg(_(e_endif_without_if)); |
| 6100 | return NULL; |
| 6101 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6102 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6103 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6104 | if (!cctx->ctx_had_return) |
| 6105 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6106 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6107 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6108 | { |
| 6109 | // previous "if" or "elseif" jumps here |
| 6110 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6111 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6112 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6113 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6114 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6115 | cctx->ctx_skip = scope->se_skip_save; |
| 6116 | |
| 6117 | // If all the blocks end in :return and there is an :else then the |
| 6118 | // had_return flag is set. |
| 6119 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6120 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6121 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6122 | return arg; |
| 6123 | } |
| 6124 | |
| 6125 | /* |
| 6126 | * compile "for var in expr" |
| 6127 | * |
| 6128 | * Produces instructions: |
| 6129 | * PUSHNR -1 |
| 6130 | * STORE loop-idx Set index to -1 |
| 6131 | * EVAL expr Push result of "expr" |
| 6132 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6133 | * - if beyond end, jump to "end" |
| 6134 | * - otherwise get item from list and push it |
| 6135 | * STORE var Store item in "var" |
| 6136 | * ... body ... |
| 6137 | * JUMP top Jump back to repeat |
| 6138 | * end: DROP Drop the result of "expr" |
| 6139 | * |
| 6140 | */ |
| 6141 | static char_u * |
| 6142 | compile_for(char_u *arg, cctx_T *cctx) |
| 6143 | { |
| 6144 | char_u *p; |
| 6145 | size_t varlen; |
| 6146 | garray_T *instr = &cctx->ctx_instr; |
| 6147 | garray_T *stack = &cctx->ctx_type_stack; |
| 6148 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6149 | lvar_T *loop_lvar; // loop iteration variable |
| 6150 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6151 | type_T *vartype; |
| 6152 | |
| 6153 | // TODO: list of variables: "for [key, value] in dict" |
| 6154 | // parse "var" |
| 6155 | for (p = arg; eval_isnamec1(*p); ++p) |
| 6156 | ; |
| 6157 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6158 | var_lvar = lookup_local(arg, varlen, cctx); |
| 6159 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6160 | { |
| 6161 | semsg(_("E1023: variable already defined: %s"), arg); |
| 6162 | return NULL; |
| 6163 | } |
| 6164 | |
| 6165 | // consume "in" |
| 6166 | p = skipwhite(p); |
| 6167 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 6168 | { |
| 6169 | emsg(_(e_missing_in)); |
| 6170 | return NULL; |
| 6171 | } |
| 6172 | p = skipwhite(p + 2); |
| 6173 | |
| 6174 | |
| 6175 | scope = new_scope(cctx, FOR_SCOPE); |
| 6176 | if (scope == NULL) |
| 6177 | return NULL; |
| 6178 | |
| 6179 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6180 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6181 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6182 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6183 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6184 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6185 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6186 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6187 | |
| 6188 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6189 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 6190 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6191 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6192 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6193 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6194 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6195 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6196 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6197 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6198 | |
| 6199 | // compile "expr", it remains on the stack until "endfor" |
| 6200 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6201 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6202 | { |
| 6203 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6204 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6205 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6206 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6207 | // Now that we know the type of "var", check that it is a list, now or at |
| 6208 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6209 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 6210 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6211 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6212 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6213 | return NULL; |
| 6214 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6215 | if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6216 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6217 | |
| 6218 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6219 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6220 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6221 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 6222 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6223 | |
| 6224 | return arg; |
| 6225 | } |
| 6226 | |
| 6227 | /* |
| 6228 | * compile "endfor" |
| 6229 | */ |
| 6230 | static char_u * |
| 6231 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 6232 | { |
| 6233 | garray_T *instr = &cctx->ctx_instr; |
| 6234 | scope_T *scope = cctx->ctx_scope; |
| 6235 | forscope_T *forscope; |
| 6236 | isn_T *isn; |
| 6237 | |
| 6238 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 6239 | { |
| 6240 | emsg(_(e_for)); |
| 6241 | return NULL; |
| 6242 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6243 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6244 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6245 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6246 | |
| 6247 | // At end of ":for" scope jump back to the FOR instruction. |
| 6248 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 6249 | |
| 6250 | // Fill in the "end" label in the FOR statement so it can jump here |
| 6251 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 6252 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 6253 | |
| 6254 | // Fill in the "end" label any BREAK statements |
| 6255 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 6256 | |
| 6257 | // Below the ":for" scope drop the "expr" list from the stack. |
| 6258 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 6259 | return NULL; |
| 6260 | |
| 6261 | vim_free(scope); |
| 6262 | |
| 6263 | return arg; |
| 6264 | } |
| 6265 | |
| 6266 | /* |
| 6267 | * compile "while expr" |
| 6268 | * |
| 6269 | * Produces instructions: |
| 6270 | * top: EVAL expr Push result of "expr" |
| 6271 | * JUMP_IF_FALSE end jump if false |
| 6272 | * ... body ... |
| 6273 | * JUMP top Jump back to repeat |
| 6274 | * end: |
| 6275 | * |
| 6276 | */ |
| 6277 | static char_u * |
| 6278 | compile_while(char_u *arg, cctx_T *cctx) |
| 6279 | { |
| 6280 | char_u *p = arg; |
| 6281 | garray_T *instr = &cctx->ctx_instr; |
| 6282 | scope_T *scope; |
| 6283 | |
| 6284 | scope = new_scope(cctx, WHILE_SCOPE); |
| 6285 | if (scope == NULL) |
| 6286 | return NULL; |
| 6287 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6288 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6289 | |
| 6290 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6291 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6292 | return NULL; |
| 6293 | |
| 6294 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6295 | 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] | 6296 | JUMP_IF_FALSE, cctx) == FAIL) |
| 6297 | return FAIL; |
| 6298 | |
| 6299 | return p; |
| 6300 | } |
| 6301 | |
| 6302 | /* |
| 6303 | * compile "endwhile" |
| 6304 | */ |
| 6305 | static char_u * |
| 6306 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 6307 | { |
| 6308 | scope_T *scope = cctx->ctx_scope; |
| 6309 | |
| 6310 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 6311 | { |
| 6312 | emsg(_(e_while)); |
| 6313 | return NULL; |
| 6314 | } |
| 6315 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6316 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6317 | |
| 6318 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6319 | 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] | 6320 | |
| 6321 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 6322 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6323 | 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] | 6324 | |
| 6325 | vim_free(scope); |
| 6326 | |
| 6327 | return arg; |
| 6328 | } |
| 6329 | |
| 6330 | /* |
| 6331 | * compile "continue" |
| 6332 | */ |
| 6333 | static char_u * |
| 6334 | compile_continue(char_u *arg, cctx_T *cctx) |
| 6335 | { |
| 6336 | scope_T *scope = cctx->ctx_scope; |
| 6337 | |
| 6338 | for (;;) |
| 6339 | { |
| 6340 | if (scope == NULL) |
| 6341 | { |
| 6342 | emsg(_(e_continue)); |
| 6343 | return NULL; |
| 6344 | } |
| 6345 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6346 | break; |
| 6347 | scope = scope->se_outer; |
| 6348 | } |
| 6349 | |
| 6350 | // Jump back to the FOR or WHILE instruction. |
| 6351 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6352 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 6353 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6354 | return arg; |
| 6355 | } |
| 6356 | |
| 6357 | /* |
| 6358 | * compile "break" |
| 6359 | */ |
| 6360 | static char_u * |
| 6361 | compile_break(char_u *arg, cctx_T *cctx) |
| 6362 | { |
| 6363 | scope_T *scope = cctx->ctx_scope; |
| 6364 | endlabel_T **el; |
| 6365 | |
| 6366 | for (;;) |
| 6367 | { |
| 6368 | if (scope == NULL) |
| 6369 | { |
| 6370 | emsg(_(e_break)); |
| 6371 | return NULL; |
| 6372 | } |
| 6373 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6374 | break; |
| 6375 | scope = scope->se_outer; |
| 6376 | } |
| 6377 | |
| 6378 | // Jump to the end of the FOR or WHILE loop. |
| 6379 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6380 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6381 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6382 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6383 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 6384 | return FAIL; |
| 6385 | |
| 6386 | return arg; |
| 6387 | } |
| 6388 | |
| 6389 | /* |
| 6390 | * compile "{" start of block |
| 6391 | */ |
| 6392 | static char_u * |
| 6393 | compile_block(char_u *arg, cctx_T *cctx) |
| 6394 | { |
| 6395 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6396 | return NULL; |
| 6397 | return skipwhite(arg + 1); |
| 6398 | } |
| 6399 | |
| 6400 | /* |
| 6401 | * compile end of block: drop one scope |
| 6402 | */ |
| 6403 | static void |
| 6404 | compile_endblock(cctx_T *cctx) |
| 6405 | { |
| 6406 | scope_T *scope = cctx->ctx_scope; |
| 6407 | |
| 6408 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6409 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6410 | vim_free(scope); |
| 6411 | } |
| 6412 | |
| 6413 | /* |
| 6414 | * compile "try" |
| 6415 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6416 | * finally. |
| 6417 | * Creates another scope for the "try" block itself. |
| 6418 | * TRY instruction sets up exception handling at runtime. |
| 6419 | * |
| 6420 | * "try" |
| 6421 | * TRY -> catch1, -> finally push trystack entry |
| 6422 | * ... try block |
| 6423 | * "throw {exception}" |
| 6424 | * EVAL {exception} |
| 6425 | * THROW create exception |
| 6426 | * ... try block |
| 6427 | * " catch {expr}" |
| 6428 | * JUMP -> finally |
| 6429 | * catch1: PUSH exeception |
| 6430 | * EVAL {expr} |
| 6431 | * MATCH |
| 6432 | * JUMP nomatch -> catch2 |
| 6433 | * CATCH remove exception |
| 6434 | * ... catch block |
| 6435 | * " catch" |
| 6436 | * JUMP -> finally |
| 6437 | * catch2: CATCH remove exception |
| 6438 | * ... catch block |
| 6439 | * " finally" |
| 6440 | * finally: |
| 6441 | * ... finally block |
| 6442 | * " endtry" |
| 6443 | * ENDTRY pop trystack entry, may rethrow |
| 6444 | */ |
| 6445 | static char_u * |
| 6446 | compile_try(char_u *arg, cctx_T *cctx) |
| 6447 | { |
| 6448 | garray_T *instr = &cctx->ctx_instr; |
| 6449 | scope_T *try_scope; |
| 6450 | scope_T *scope; |
| 6451 | |
| 6452 | // scope that holds the jumps that go to catch/finally/endtry |
| 6453 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6454 | if (try_scope == NULL) |
| 6455 | return NULL; |
| 6456 | |
| 6457 | // "catch" is set when the first ":catch" is found. |
| 6458 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6459 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6460 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6461 | return NULL; |
| 6462 | |
| 6463 | // scope for the try block itself |
| 6464 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6465 | if (scope == NULL) |
| 6466 | return NULL; |
| 6467 | |
| 6468 | return arg; |
| 6469 | } |
| 6470 | |
| 6471 | /* |
| 6472 | * compile "catch {expr}" |
| 6473 | */ |
| 6474 | static char_u * |
| 6475 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6476 | { |
| 6477 | scope_T *scope = cctx->ctx_scope; |
| 6478 | garray_T *instr = &cctx->ctx_instr; |
| 6479 | char_u *p; |
| 6480 | isn_T *isn; |
| 6481 | |
| 6482 | // end block scope from :try or :catch |
| 6483 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6484 | compile_endblock(cctx); |
| 6485 | scope = cctx->ctx_scope; |
| 6486 | |
| 6487 | // Error if not in a :try scope |
| 6488 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6489 | { |
| 6490 | emsg(_(e_catch)); |
| 6491 | return NULL; |
| 6492 | } |
| 6493 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6494 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6495 | { |
| 6496 | emsg(_("E1033: catch unreachable after catch-all")); |
| 6497 | return NULL; |
| 6498 | } |
| 6499 | |
| 6500 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6501 | 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] | 6502 | JUMP_ALWAYS, cctx) == FAIL) |
| 6503 | return NULL; |
| 6504 | |
| 6505 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6506 | 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] | 6507 | if (isn->isn_arg.try.try_catch == 0) |
| 6508 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6509 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6510 | { |
| 6511 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6512 | 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] | 6513 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6514 | } |
| 6515 | |
| 6516 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6517 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6518 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6519 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6520 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6521 | } |
| 6522 | else |
| 6523 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6524 | char_u *end; |
| 6525 | char_u *pat; |
| 6526 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6527 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6528 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6529 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6530 | // Push v:exception, push {expr} and MATCH |
| 6531 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6532 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6533 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6534 | if (*end != *p) |
| 6535 | { |
| 6536 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 6537 | vim_free(tofree); |
| 6538 | return FAIL; |
| 6539 | } |
| 6540 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6541 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6542 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6543 | len = (int)(end - tofree); |
| 6544 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6545 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6546 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6547 | if (pat == NULL) |
| 6548 | return FAIL; |
| 6549 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6550 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6551 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6552 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6553 | return NULL; |
| 6554 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6555 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6556 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6557 | return NULL; |
| 6558 | } |
| 6559 | |
| 6560 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6561 | return NULL; |
| 6562 | |
| 6563 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6564 | return NULL; |
| 6565 | return p; |
| 6566 | } |
| 6567 | |
| 6568 | static char_u * |
| 6569 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6570 | { |
| 6571 | scope_T *scope = cctx->ctx_scope; |
| 6572 | garray_T *instr = &cctx->ctx_instr; |
| 6573 | isn_T *isn; |
| 6574 | |
| 6575 | // end block scope from :try or :catch |
| 6576 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6577 | compile_endblock(cctx); |
| 6578 | scope = cctx->ctx_scope; |
| 6579 | |
| 6580 | // Error if not in a :try scope |
| 6581 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6582 | { |
| 6583 | emsg(_(e_finally)); |
| 6584 | return NULL; |
| 6585 | } |
| 6586 | |
| 6587 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6588 | 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] | 6589 | if (isn->isn_arg.try.try_finally != 0) |
| 6590 | { |
| 6591 | emsg(_(e_finally_dup)); |
| 6592 | return NULL; |
| 6593 | } |
| 6594 | |
| 6595 | // 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] | 6596 | 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] | 6597 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6598 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6599 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6600 | { |
| 6601 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6602 | 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] | 6603 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6604 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6605 | } |
| 6606 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6607 | // TODO: set index in ts_finally_label jumps |
| 6608 | |
| 6609 | return arg; |
| 6610 | } |
| 6611 | |
| 6612 | static char_u * |
| 6613 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6614 | { |
| 6615 | scope_T *scope = cctx->ctx_scope; |
| 6616 | garray_T *instr = &cctx->ctx_instr; |
| 6617 | isn_T *isn; |
| 6618 | |
| 6619 | // end block scope from :catch or :finally |
| 6620 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6621 | compile_endblock(cctx); |
| 6622 | scope = cctx->ctx_scope; |
| 6623 | |
| 6624 | // Error if not in a :try scope |
| 6625 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6626 | { |
| 6627 | if (scope == NULL) |
| 6628 | emsg(_(e_no_endtry)); |
| 6629 | else if (scope->se_type == WHILE_SCOPE) |
| 6630 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6631 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6632 | emsg(_(e_endfor)); |
| 6633 | else |
| 6634 | emsg(_(e_endif)); |
| 6635 | return NULL; |
| 6636 | } |
| 6637 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6638 | 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] | 6639 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6640 | { |
| 6641 | emsg(_("E1032: missing :catch or :finally")); |
| 6642 | return NULL; |
| 6643 | } |
| 6644 | |
| 6645 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6646 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6647 | 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] | 6648 | |
| 6649 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6650 | if (isn->isn_arg.try.try_catch == 0) |
| 6651 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6652 | if (isn->isn_arg.try.try_finally == 0) |
| 6653 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6654 | |
| 6655 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6656 | { |
| 6657 | // Last catch without match jumps here |
| 6658 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6659 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6660 | } |
| 6661 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6662 | compile_endblock(cctx); |
| 6663 | |
| 6664 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6665 | return NULL; |
| 6666 | return arg; |
| 6667 | } |
| 6668 | |
| 6669 | /* |
| 6670 | * compile "throw {expr}" |
| 6671 | */ |
| 6672 | static char_u * |
| 6673 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6674 | { |
| 6675 | char_u *p = skipwhite(arg); |
| 6676 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6677 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6678 | return NULL; |
| 6679 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6680 | return NULL; |
| 6681 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6682 | return NULL; |
| 6683 | |
| 6684 | return p; |
| 6685 | } |
| 6686 | |
| 6687 | /* |
| 6688 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6689 | * compile "echomsg expr" |
| 6690 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6691 | * compile "execute expr" |
| 6692 | */ |
| 6693 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6694 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6695 | { |
| 6696 | char_u *p = arg; |
| 6697 | int count = 0; |
| 6698 | |
| 6699 | for (;;) |
| 6700 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6701 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6702 | return NULL; |
| 6703 | ++count; |
| 6704 | p = skipwhite(p); |
| 6705 | if (ends_excmd(*p)) |
| 6706 | break; |
| 6707 | } |
| 6708 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6709 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6710 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6711 | else if (cmdidx == CMD_execute) |
| 6712 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6713 | else if (cmdidx == CMD_echomsg) |
| 6714 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6715 | else |
| 6716 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6717 | return p; |
| 6718 | } |
| 6719 | |
| 6720 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6721 | * A command that is not compiled, execute with legacy code. |
| 6722 | */ |
| 6723 | static char_u * |
| 6724 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6725 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6726 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6727 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6728 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6729 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6730 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6731 | goto theend; |
| 6732 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6733 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6734 | { |
| 6735 | long argt = excmd_get_argt(eap->cmdidx); |
| 6736 | int usefilter = FALSE; |
| 6737 | |
| 6738 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6739 | |
| 6740 | // If the command can be followed by a bar, find the bar and truncate |
| 6741 | // it, so that the following command can be compiled. |
| 6742 | // The '|' is overwritten with a NUL, it is put back below. |
| 6743 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6744 | && *eap->arg == '!') |
| 6745 | // :w !filter or :r !filter or :r! filter |
| 6746 | usefilter = TRUE; |
| 6747 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6748 | { |
| 6749 | separate_nextcmd(eap); |
| 6750 | if (eap->nextcmd != NULL) |
| 6751 | nextcmd = eap->nextcmd; |
| 6752 | } |
| 6753 | } |
| 6754 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6755 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6756 | { |
| 6757 | // expand filename in "syntax include [@group] filename" |
| 6758 | has_expr = TRUE; |
| 6759 | eap->arg = skipwhite(eap->arg + 7); |
| 6760 | if (*eap->arg == '@') |
| 6761 | eap->arg = skiptowhite(eap->arg); |
| 6762 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6763 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6764 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6765 | { |
| 6766 | int count = 0; |
| 6767 | char_u *start = skipwhite(line); |
| 6768 | |
| 6769 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6770 | // PUSHS ":cmd xxx" |
| 6771 | // eval expr1 |
| 6772 | // PUSHS "yyy" |
| 6773 | // eval expr2 |
| 6774 | // PUSHS "zzz" |
| 6775 | // EXECCONCAT 5 |
| 6776 | for (;;) |
| 6777 | { |
| 6778 | if (p > start) |
| 6779 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6780 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6781 | ++count; |
| 6782 | } |
| 6783 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6784 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6785 | return NULL; |
| 6786 | may_generate_2STRING(-1, cctx); |
| 6787 | ++count; |
| 6788 | p = skipwhite(p); |
| 6789 | if (*p != '`') |
| 6790 | { |
| 6791 | emsg(_("E1083: missing backtick")); |
| 6792 | return NULL; |
| 6793 | } |
| 6794 | start = p + 1; |
| 6795 | |
| 6796 | p = (char_u *)strstr((char *)start, "`="); |
| 6797 | if (p == NULL) |
| 6798 | { |
| 6799 | if (*skipwhite(start) != NUL) |
| 6800 | { |
| 6801 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6802 | ++count; |
| 6803 | } |
| 6804 | break; |
| 6805 | } |
| 6806 | } |
| 6807 | generate_EXECCONCAT(cctx, count); |
| 6808 | } |
| 6809 | else |
| 6810 | generate_EXEC(cctx, line); |
| 6811 | |
| 6812 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6813 | if (*nextcmd != NUL) |
| 6814 | { |
| 6815 | // the parser expects a pointer to the bar, put it back |
| 6816 | --nextcmd; |
| 6817 | *nextcmd = '|'; |
| 6818 | } |
| 6819 | |
| 6820 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6821 | } |
| 6822 | |
| 6823 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6824 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6825 | * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet. |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6826 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6827 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6828 | add_def_function(ufunc_T *ufunc) |
| 6829 | { |
| 6830 | dfunc_T *dfunc; |
| 6831 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6832 | if (def_functions.ga_len == 0) |
| 6833 | { |
| 6834 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6835 | // wasn't set. |
| 6836 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6837 | return FAIL; |
| 6838 | ++def_functions.ga_len; |
| 6839 | } |
| 6840 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6841 | // Add the function to "def_functions". |
| 6842 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6843 | return FAIL; |
| 6844 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6845 | CLEAR_POINTER(dfunc); |
| 6846 | dfunc->df_idx = def_functions.ga_len; |
| 6847 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6848 | dfunc->df_ufunc = ufunc; |
| 6849 | ++def_functions.ga_len; |
| 6850 | return OK; |
| 6851 | } |
| 6852 | |
| 6853 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6854 | * After ex_function() has collected all the function lines: parse and compile |
| 6855 | * the lines into instructions. |
| 6856 | * Adds the function to "def_functions". |
| 6857 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6858 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6859 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6860 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6861 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6862 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6863 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6864 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6865 | compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6866 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6867 | char_u *line = NULL; |
| 6868 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6869 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6870 | cctx_T cctx; |
| 6871 | garray_T *instr; |
| 6872 | int called_emsg_before = called_emsg; |
| 6873 | int ret = FAIL; |
| 6874 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6875 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6876 | int emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6877 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6878 | // When using a function that was compiled before: Free old instructions. |
| 6879 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6880 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6881 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6882 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6883 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6884 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6885 | } |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6886 | else if (add_def_function(ufunc) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6887 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6888 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6889 | ufunc->uf_def_status = UF_COMPILING; |
| 6890 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6891 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6892 | cctx.ctx_ufunc = ufunc; |
| 6893 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6894 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6895 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6896 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6897 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6898 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6899 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6900 | instr = &cctx.ctx_instr; |
| 6901 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6902 | // Set the context to the function, it may be compiled when called from |
| 6903 | // another script. Set the script version to the most modern one. |
| 6904 | // The line number will be set in next_line_from_context(). |
| 6905 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6906 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6907 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6908 | // Make sure error messages are OK. |
| 6909 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6910 | if (do_estack_push) |
| 6911 | estack_push_ufunc(ufunc, 1); |
| 6912 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6913 | if (ufunc->uf_def_args.ga_len > 0) |
| 6914 | { |
| 6915 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6916 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6917 | int i; |
| 6918 | char_u *arg; |
| 6919 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 6920 | |
| 6921 | // Produce instructions for the default values of optional arguments. |
| 6922 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6923 | // where to start when the function is called, depending on the number |
| 6924 | // of arguments. |
| 6925 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6926 | if (ufunc->uf_def_arg_idx == NULL) |
| 6927 | goto erret; |
| 6928 | for (i = 0; i < count; ++i) |
| 6929 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6930 | garray_T *stack = &cctx.ctx_type_stack; |
| 6931 | type_T *val_type; |
| 6932 | int arg_idx = first_def_arg + i; |
| 6933 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6934 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6935 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6936 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6937 | goto erret; |
| 6938 | |
| 6939 | // If no type specified use the type of the default value. |
| 6940 | // Otherwise check that the default value type matches the |
| 6941 | // specified type. |
| 6942 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6943 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
| 6944 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 6945 | else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6946 | == FAIL) |
| 6947 | { |
| 6948 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 6949 | arg_idx + 1); |
| 6950 | goto erret; |
| 6951 | } |
| 6952 | |
| 6953 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6954 | goto erret; |
| 6955 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6956 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
| 6957 | } |
| 6958 | |
| 6959 | /* |
| 6960 | * Loop over all the lines of the function and generate instructions. |
| 6961 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6962 | for (;;) |
| 6963 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6964 | exarg_T ea; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6965 | int starts_with_colon = FALSE; |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6966 | char_u *cmd; |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 6967 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6968 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6969 | // Bail out on the first error to avoid a flood of errors and report |
| 6970 | // the right line number when inside try/catch. |
| 6971 | if (emsg_before != called_emsg) |
| 6972 | goto erret; |
| 6973 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6974 | if (line != NULL && *line == '|') |
| 6975 | // the line continues after a '|' |
| 6976 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 6977 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6978 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 6979 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6980 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6981 | semsg(_("E488: Trailing characters: %s"), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6982 | goto erret; |
| 6983 | } |
| 6984 | else |
| 6985 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 6986 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 6987 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6988 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6989 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6990 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6991 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6992 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6993 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6994 | ea.cmdlinep = &line; |
| 6995 | ea.cmd = skipwhite(line); |
| 6996 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6997 | // Some things can be recognized by the first character. |
| 6998 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6999 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7000 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7001 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7002 | if (ea.cmd[1] != '{') |
| 7003 | { |
| 7004 | line = (char_u *)""; |
| 7005 | continue; |
| 7006 | } |
| 7007 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7008 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7009 | case '}': |
| 7010 | { |
| 7011 | // "}" ends a block scope |
| 7012 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7013 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7014 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7015 | if (stype == BLOCK_SCOPE) |
| 7016 | { |
| 7017 | compile_endblock(&cctx); |
| 7018 | line = ea.cmd; |
| 7019 | } |
| 7020 | else |
| 7021 | { |
| 7022 | emsg(_("E1025: using } outside of a block scope")); |
| 7023 | goto erret; |
| 7024 | } |
| 7025 | if (line != NULL) |
| 7026 | line = skipwhite(ea.cmd + 1); |
| 7027 | continue; |
| 7028 | } |
| 7029 | |
| 7030 | case '{': |
| 7031 | // "{" starts a block scope |
| 7032 | // "{'a': 1}->func() is something else |
| 7033 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7034 | { |
| 7035 | line = compile_block(ea.cmd, &cctx); |
| 7036 | continue; |
| 7037 | } |
| 7038 | break; |
| 7039 | |
| 7040 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7041 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7042 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7043 | } |
| 7044 | |
| 7045 | /* |
| 7046 | * COMMAND MODIFIERS |
| 7047 | */ |
| 7048 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 7049 | { |
| 7050 | if (errormsg != NULL) |
| 7051 | goto erret; |
| 7052 | // empty line or comment |
| 7053 | line = (char_u *)""; |
| 7054 | continue; |
| 7055 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7056 | // TODO: use modifiers in the command |
| 7057 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | b074e8b | 2020-07-11 13:40:45 +0200 | [diff] [blame] | 7058 | CLEAR_FIELD(cmdmod); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7059 | |
| 7060 | // Skip ":call" to get to the function name. |
| 7061 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 7062 | ea.cmd = skipwhite(ea.cmd); |
| 7063 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7064 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7065 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7066 | char_u *pskip; |
| 7067 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7068 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7069 | // find what follows. |
| 7070 | // Skip over "var.member", "var[idx]" and the like. |
| 7071 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 7072 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7073 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7074 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7075 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7076 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7077 | char_u *var_end; |
| 7078 | int oplen; |
| 7079 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7080 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7081 | var_end = find_name_end(pskip, NULL, NULL, |
| 7082 | FNE_CHECK_START | FNE_INCL_BR); |
| 7083 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7084 | if (oplen > 0) |
| 7085 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7086 | size_t len = p - ea.cmd; |
| 7087 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7088 | // Recognize an assignment if we recognize the variable |
| 7089 | // name: |
| 7090 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 7091 | // "local = expr" where "local" is a local var. |
| 7092 | // "script = expr" where "script" is a script-local var. |
| 7093 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7094 | // "&opt = expr" |
| 7095 | // "$ENV = expr" |
| 7096 | // "@r = expr" |
| 7097 | if (*ea.cmd == '&' |
| 7098 | || *ea.cmd == '$' |
| 7099 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7100 | || ((len) > 2 && ea.cmd[1] == ':') |
| 7101 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 7102 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 7103 | NULL, &cctx) == OK |
| 7104 | || lookup_script(ea.cmd, len) == OK |
| 7105 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7106 | { |
| 7107 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7108 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7109 | goto erret; |
| 7110 | continue; |
| 7111 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7112 | } |
| 7113 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7114 | |
| 7115 | if (*ea.cmd == '[') |
| 7116 | { |
| 7117 | // [var, var] = expr |
| 7118 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 7119 | if (line == NULL) |
| 7120 | goto erret; |
| 7121 | if (line != ea.cmd) |
| 7122 | continue; |
| 7123 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7124 | } |
| 7125 | |
| 7126 | /* |
| 7127 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7128 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7129 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7130 | cmd = ea.cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7131 | if (*cmd != '\'') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7132 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7133 | ea.cmd = skip_range(ea.cmd, NULL); |
| 7134 | if (ea.cmd > cmd && !starts_with_colon) |
| 7135 | { |
| 7136 | emsg(_(e_colon_required)); |
| 7137 | goto erret; |
| 7138 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7139 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7140 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7141 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7142 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7143 | |
| 7144 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 7145 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7146 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7147 | { |
| 7148 | line += STRLEN(line); |
| 7149 | continue; |
| 7150 | } |
| 7151 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7152 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7153 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7154 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7155 | // CMD_let cannot happen, compile_assignment() above is used |
| 7156 | iemsg("Command from find_ex_command() not handled"); |
| 7157 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7158 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7159 | } |
| 7160 | |
| 7161 | p = skipwhite(p); |
| 7162 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7163 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 7164 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7165 | && ea.cmdidx != CMD_elseif |
| 7166 | && ea.cmdidx != CMD_else |
| 7167 | && ea.cmdidx != CMD_endif) |
| 7168 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7169 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7170 | continue; |
| 7171 | } |
| 7172 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7173 | if (ea.cmdidx != CMD_elseif |
| 7174 | && ea.cmdidx != CMD_else |
| 7175 | && ea.cmdidx != CMD_endif |
| 7176 | && ea.cmdidx != CMD_endfor |
| 7177 | && ea.cmdidx != CMD_endwhile |
| 7178 | && ea.cmdidx != CMD_catch |
| 7179 | && ea.cmdidx != CMD_finally |
| 7180 | && ea.cmdidx != CMD_endtry) |
| 7181 | { |
| 7182 | if (cctx.ctx_had_return) |
| 7183 | { |
| 7184 | emsg(_("E1095: Unreachable code after :return")); |
| 7185 | goto erret; |
| 7186 | } |
| 7187 | } |
| 7188 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7189 | switch (ea.cmdidx) |
| 7190 | { |
| 7191 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7192 | ea.arg = p; |
| 7193 | line = compile_nested_function(&ea, &cctx); |
| 7194 | break; |
| 7195 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7196 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7197 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7198 | goto erret; |
| 7199 | |
| 7200 | case CMD_return: |
| 7201 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7202 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7203 | break; |
| 7204 | |
| 7205 | case CMD_let: |
| 7206 | case CMD_const: |
| 7207 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7208 | if (line == p) |
| 7209 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7210 | break; |
| 7211 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7212 | case CMD_unlet: |
| 7213 | case CMD_unlockvar: |
| 7214 | case CMD_lockvar: |
| 7215 | line = compile_unletlock(p, &ea, &cctx); |
| 7216 | break; |
| 7217 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7218 | case CMD_import: |
| 7219 | line = compile_import(p, &cctx); |
| 7220 | break; |
| 7221 | |
| 7222 | case CMD_if: |
| 7223 | line = compile_if(p, &cctx); |
| 7224 | break; |
| 7225 | case CMD_elseif: |
| 7226 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7227 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7228 | break; |
| 7229 | case CMD_else: |
| 7230 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7231 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7232 | break; |
| 7233 | case CMD_endif: |
| 7234 | line = compile_endif(p, &cctx); |
| 7235 | break; |
| 7236 | |
| 7237 | case CMD_while: |
| 7238 | line = compile_while(p, &cctx); |
| 7239 | break; |
| 7240 | case CMD_endwhile: |
| 7241 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7242 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7243 | break; |
| 7244 | |
| 7245 | case CMD_for: |
| 7246 | line = compile_for(p, &cctx); |
| 7247 | break; |
| 7248 | case CMD_endfor: |
| 7249 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7250 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7251 | break; |
| 7252 | case CMD_continue: |
| 7253 | line = compile_continue(p, &cctx); |
| 7254 | break; |
| 7255 | case CMD_break: |
| 7256 | line = compile_break(p, &cctx); |
| 7257 | break; |
| 7258 | |
| 7259 | case CMD_try: |
| 7260 | line = compile_try(p, &cctx); |
| 7261 | break; |
| 7262 | case CMD_catch: |
| 7263 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7264 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7265 | break; |
| 7266 | case CMD_finally: |
| 7267 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7268 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7269 | break; |
| 7270 | case CMD_endtry: |
| 7271 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7272 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7273 | break; |
| 7274 | case CMD_throw: |
| 7275 | line = compile_throw(p, &cctx); |
| 7276 | break; |
| 7277 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7278 | case CMD_eval: |
| 7279 | if (compile_expr0(&p, &cctx) == FAIL) |
| 7280 | goto erret; |
| 7281 | |
| 7282 | // drop the return value |
| 7283 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 7284 | |
| 7285 | line = skipwhite(p); |
| 7286 | break; |
| 7287 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7288 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7289 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7290 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7291 | case CMD_echomsg: |
| 7292 | case CMD_echoerr: |
| 7293 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7294 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7295 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7296 | // TODO: other commands with an expression argument |
| 7297 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7298 | case CMD_SIZE: |
| 7299 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 7300 | goto erret; |
| 7301 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7302 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7303 | // Not recognized, execute with do_cmdline_cmd(). |
| 7304 | ea.arg = p; |
| 7305 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7306 | break; |
| 7307 | } |
| 7308 | if (line == NULL) |
| 7309 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7310 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7311 | |
| 7312 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7313 | { |
| 7314 | iemsg("Type stack underflow"); |
| 7315 | goto erret; |
| 7316 | } |
| 7317 | } |
| 7318 | |
| 7319 | if (cctx.ctx_scope != NULL) |
| 7320 | { |
| 7321 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7322 | emsg(_(e_endif)); |
| 7323 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7324 | emsg(_(e_endwhile)); |
| 7325 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7326 | emsg(_(e_endfor)); |
| 7327 | else |
| 7328 | emsg(_("E1026: Missing }")); |
| 7329 | goto erret; |
| 7330 | } |
| 7331 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7332 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7333 | { |
| 7334 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7335 | { |
| 7336 | emsg(_("E1027: Missing return statement")); |
| 7337 | goto erret; |
| 7338 | } |
| 7339 | |
| 7340 | // Return zero if there is no return at the end. |
| 7341 | generate_PUSHNR(&cctx, 0); |
| 7342 | generate_instr(&cctx, ISN_RETURN); |
| 7343 | } |
| 7344 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7345 | { |
| 7346 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7347 | + ufunc->uf_dfunc_idx; |
| 7348 | dfunc->df_deleted = FALSE; |
| 7349 | dfunc->df_instr = instr->ga_data; |
| 7350 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7351 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 7352 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7353 | if (cctx.ctx_outer_used) |
| 7354 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7355 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7356 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7357 | |
| 7358 | ret = OK; |
| 7359 | |
| 7360 | erret: |
| 7361 | if (ret == FAIL) |
| 7362 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7363 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7364 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7365 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7366 | |
| 7367 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7368 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7369 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7370 | |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7371 | // if using the last entry in the table we might as well remove it |
| 7372 | if (!dfunc->df_deleted |
| 7373 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7374 | --def_functions.ga_len; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7375 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7376 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7377 | while (cctx.ctx_scope != NULL) |
| 7378 | drop_scope(&cctx); |
| 7379 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7380 | // Don't execute this function body. |
| 7381 | ga_clear_strings(&ufunc->uf_lines); |
| 7382 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7383 | if (errormsg != NULL) |
| 7384 | emsg(errormsg); |
| 7385 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 7386 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7387 | } |
| 7388 | |
| 7389 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7390 | if (do_estack_push) |
| 7391 | estack_pop(); |
| 7392 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7393 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7394 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7395 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7396 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7397 | } |
| 7398 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7399 | void |
| 7400 | set_function_type(ufunc_T *ufunc) |
| 7401 | { |
| 7402 | int varargs = ufunc->uf_va_name != NULL; |
| 7403 | int argcount = ufunc->uf_args.ga_len; |
| 7404 | |
| 7405 | // Create a type for the function, with the return type and any |
| 7406 | // argument types. |
| 7407 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7408 | // The type is included in "tt_args". |
| 7409 | if (argcount > 0 || varargs) |
| 7410 | { |
| 7411 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7412 | argcount, &ufunc->uf_type_list); |
| 7413 | // Add argument types to the function type. |
| 7414 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7415 | argcount + varargs, |
| 7416 | &ufunc->uf_type_list) == FAIL) |
| 7417 | return; |
| 7418 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7419 | ufunc->uf_func_type->tt_min_argcount = |
| 7420 | argcount - ufunc->uf_def_args.ga_len; |
| 7421 | if (ufunc->uf_arg_types == NULL) |
| 7422 | { |
| 7423 | int i; |
| 7424 | |
| 7425 | // lambda does not have argument types. |
| 7426 | for (i = 0; i < argcount; ++i) |
| 7427 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7428 | } |
| 7429 | else |
| 7430 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7431 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7432 | if (varargs) |
| 7433 | { |
| 7434 | ufunc->uf_func_type->tt_args[argcount] = |
| 7435 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7436 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7437 | } |
| 7438 | } |
| 7439 | else |
| 7440 | // No arguments, can use a predefined type. |
| 7441 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7442 | argcount, &ufunc->uf_type_list); |
| 7443 | } |
| 7444 | |
| 7445 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7446 | /* |
| 7447 | * Delete an instruction, free what it contains. |
| 7448 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7449 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7450 | delete_instr(isn_T *isn) |
| 7451 | { |
| 7452 | switch (isn->isn_type) |
| 7453 | { |
| 7454 | case ISN_EXEC: |
| 7455 | case ISN_LOADENV: |
| 7456 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7457 | case ISN_LOADB: |
| 7458 | case ISN_LOADW: |
| 7459 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7460 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7461 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7462 | case ISN_PUSHEXC: |
| 7463 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7464 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7465 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7466 | case ISN_STOREB: |
| 7467 | case ISN_STOREW: |
| 7468 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7469 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7470 | vim_free(isn->isn_arg.string); |
| 7471 | break; |
| 7472 | |
| 7473 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7474 | case ISN_STORES: |
| 7475 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7476 | break; |
| 7477 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7478 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7479 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7480 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7481 | break; |
| 7482 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7483 | case ISN_STOREOPT: |
| 7484 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7485 | break; |
| 7486 | |
| 7487 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7488 | blob_unref(isn->isn_arg.blob); |
| 7489 | break; |
| 7490 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7491 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7492 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7493 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7494 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7495 | break; |
| 7496 | |
| 7497 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7498 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7499 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7500 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7501 | break; |
| 7502 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7503 | case ISN_UCALL: |
| 7504 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7505 | break; |
| 7506 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7507 | case ISN_FUNCREF: |
| 7508 | { |
| 7509 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7510 | + isn->isn_arg.funcref.fr_func; |
| 7511 | func_ptr_unref(dfunc->df_ufunc); |
| 7512 | } |
| 7513 | break; |
| 7514 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7515 | case ISN_2BOOL: |
| 7516 | case ISN_2STRING: |
| 7517 | case ISN_ADDBLOB: |
| 7518 | case ISN_ADDLIST: |
| 7519 | case ISN_BCALL: |
| 7520 | case ISN_CATCH: |
| 7521 | case ISN_CHECKNR: |
| 7522 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7523 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7524 | case ISN_COMPAREANY: |
| 7525 | case ISN_COMPAREBLOB: |
| 7526 | case ISN_COMPAREBOOL: |
| 7527 | case ISN_COMPAREDICT: |
| 7528 | case ISN_COMPAREFLOAT: |
| 7529 | case ISN_COMPAREFUNC: |
| 7530 | case ISN_COMPARELIST: |
| 7531 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7532 | case ISN_COMPARESPECIAL: |
| 7533 | case ISN_COMPARESTRING: |
| 7534 | case ISN_CONCAT: |
| 7535 | case ISN_DCALL: |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 7536 | case ISN_SHUFFLE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7537 | case ISN_DROP: |
| 7538 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7539 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7540 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7541 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7542 | case ISN_EXECCONCAT: |
| 7543 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7544 | case ISN_FOR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7545 | case ISN_INDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7546 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7547 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7548 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7549 | case ISN_JUMP: |
| 7550 | case ISN_LOAD: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7551 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7552 | case ISN_LOADSCRIPT: |
| 7553 | case ISN_LOADREG: |
| 7554 | case ISN_LOADV: |
| 7555 | case ISN_NEGATENR: |
| 7556 | case ISN_NEWDICT: |
| 7557 | case ISN_NEWLIST: |
| 7558 | case ISN_OPNR: |
| 7559 | case ISN_OPFLOAT: |
| 7560 | case ISN_OPANY: |
| 7561 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7562 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7563 | case ISN_PUSHF: |
| 7564 | case ISN_PUSHNR: |
| 7565 | case ISN_PUSHBOOL: |
| 7566 | case ISN_PUSHSPEC: |
| 7567 | case ISN_RETURN: |
| 7568 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 7569 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7570 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7571 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7572 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7573 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7574 | case ISN_STOREDICT: |
| 7575 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7576 | case ISN_THROW: |
| 7577 | case ISN_TRY: |
| 7578 | // nothing allocated |
| 7579 | break; |
| 7580 | } |
| 7581 | } |
| 7582 | |
| 7583 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7584 | * Free all instructions for "dfunc". |
| 7585 | */ |
| 7586 | static void |
| 7587 | delete_def_function_contents(dfunc_T *dfunc) |
| 7588 | { |
| 7589 | int idx; |
| 7590 | |
| 7591 | ga_clear(&dfunc->df_def_args_isn); |
| 7592 | |
| 7593 | if (dfunc->df_instr != NULL) |
| 7594 | { |
| 7595 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7596 | delete_instr(dfunc->df_instr + idx); |
| 7597 | VIM_CLEAR(dfunc->df_instr); |
| 7598 | } |
| 7599 | |
| 7600 | dfunc->df_deleted = TRUE; |
| 7601 | } |
| 7602 | |
| 7603 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7604 | * When a user function is deleted, clear the contents of any associated def |
| 7605 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7606 | */ |
| 7607 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7608 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7609 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7610 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7611 | { |
| 7612 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7613 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7614 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7615 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7616 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7617 | } |
| 7618 | } |
| 7619 | |
| 7620 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7621 | /* |
| 7622 | * Free all functions defined with ":def". |
| 7623 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7624 | void |
| 7625 | free_def_functions(void) |
| 7626 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7627 | int idx; |
| 7628 | |
| 7629 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7630 | { |
| 7631 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7632 | |
| 7633 | delete_def_function_contents(dfunc); |
| 7634 | } |
| 7635 | |
| 7636 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7637 | } |
| 7638 | #endif |
| 7639 | |
| 7640 | |
| 7641 | #endif // FEAT_EVAL |