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 | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 150 | static char e_namespace[] = N_("E1075: Namespace not supported: %s"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 151 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 152 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 153 | 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] | 154 | |
| 155 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 156 | * Lookup variable "name" in the local scope and return it. |
| 157 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 158 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 159 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 160 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 161 | { |
| 162 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 163 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 164 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 165 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 166 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 167 | |
| 168 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 169 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 170 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 171 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 172 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 173 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 174 | { |
| 175 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 176 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 177 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 178 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 179 | |
| 180 | // Find local in outer function scope. |
| 181 | if (cctx->ctx_outer != NULL) |
| 182 | { |
| 183 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 184 | if (lvar != NULL) |
| 185 | { |
| 186 | // TODO: are there situations we should not mark the outer scope as |
| 187 | // used? |
| 188 | cctx->ctx_outer_used = TRUE; |
| 189 | lvar->lv_from_outer = TRUE; |
| 190 | return lvar; |
| 191 | } |
| 192 | } |
| 193 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 194 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 198 | * Lookup an argument in the current function and an enclosing function. |
| 199 | * Returns the argument index in "idxp" |
| 200 | * Returns the argument type in "type" |
| 201 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 202 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 203 | */ |
| 204 | static int |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 205 | lookup_arg( |
| 206 | char_u *name, |
| 207 | size_t len, |
| 208 | int *idxp, |
| 209 | type_T **type, |
| 210 | int *gen_load_outer, |
| 211 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 212 | { |
| 213 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 214 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 215 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 216 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 217 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 218 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 219 | { |
| 220 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 221 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 222 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 223 | { |
| 224 | if (idxp != NULL) |
| 225 | { |
| 226 | // Arguments are located above the frame pointer. One further |
| 227 | // if there is a vararg argument |
| 228 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 229 | + STACK_FRAME_SIZE) |
| 230 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 231 | |
| 232 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 233 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 234 | else |
| 235 | *type = &t_any; |
| 236 | } |
| 237 | return OK; |
| 238 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 239 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 240 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 241 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 242 | if (va_name != NULL |
| 243 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 244 | { |
| 245 | if (idxp != NULL) |
| 246 | { |
| 247 | // varargs is always the last argument |
| 248 | *idxp = -STACK_FRAME_SIZE - 1; |
| 249 | *type = cctx->ctx_ufunc->uf_va_type; |
| 250 | } |
| 251 | return OK; |
| 252 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 253 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 254 | if (cctx->ctx_outer != NULL) |
| 255 | { |
| 256 | // Lookup the name for an argument of the outer function. |
| 257 | if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
| 258 | == OK) |
| 259 | { |
| 260 | *gen_load_outer = TRUE; |
| 261 | return OK; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Lookup a variable in the current script. |
| 270 | * Returns OK or FAIL. |
| 271 | */ |
| 272 | static int |
| 273 | lookup_script(char_u *name, size_t len) |
| 274 | { |
| 275 | int cc; |
| 276 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 277 | dictitem_T *di; |
| 278 | |
| 279 | cc = name[len]; |
| 280 | name[len] = NUL; |
| 281 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 282 | name[len] = cc; |
| 283 | return di == NULL ? FAIL: OK; |
| 284 | } |
| 285 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 286 | /* |
| 287 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 288 | * compilation context "cctx". |
| 289 | * Return FAIL and give an error if it defined. |
| 290 | */ |
| 291 | int |
Bram Moolenaar | cbb6bdc | 2020-07-06 21:53:17 +0200 | [diff] [blame] | 292 | check_defined(char_u *p, size_t len, cctx_T *cctx) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 293 | { |
| 294 | if (lookup_script(p, len) == OK |
| 295 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 296 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 297 | || find_imported(p, len, cctx) != NULL))) |
| 298 | { |
| 299 | semsg("E1073: imported name already defined: %s", p); |
| 300 | return FAIL; |
| 301 | } |
| 302 | return OK; |
| 303 | } |
| 304 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 305 | /* |
| 306 | * Allocate memory for a type_T and add the pointer to type_gap, so that it can |
| 307 | * be freed later. |
| 308 | */ |
| 309 | static type_T * |
| 310 | alloc_type(garray_T *type_gap) |
| 311 | { |
| 312 | type_T *type; |
| 313 | |
| 314 | if (ga_grow(type_gap, 1) == FAIL) |
| 315 | return NULL; |
| 316 | type = ALLOC_CLEAR_ONE(type_T); |
| 317 | if (type != NULL) |
| 318 | { |
| 319 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type; |
| 320 | ++type_gap->ga_len; |
| 321 | } |
| 322 | return type; |
| 323 | } |
| 324 | |
Bram Moolenaar | 6110e79 | 2020-07-08 19:35:21 +0200 | [diff] [blame] | 325 | void |
| 326 | clear_type_list(garray_T *gap) |
| 327 | { |
| 328 | while (gap->ga_len > 0) |
| 329 | vim_free(((type_T **)gap->ga_data)[--gap->ga_len]); |
| 330 | ga_clear(gap); |
| 331 | } |
| 332 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 333 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 334 | get_list_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 335 | { |
| 336 | type_T *type; |
| 337 | |
| 338 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 339 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 340 | return &t_list_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 341 | if (member_type->tt_type == VAR_VOID |
| 342 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 343 | return &t_list_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 344 | if (member_type->tt_type == VAR_BOOL) |
| 345 | return &t_list_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 346 | if (member_type->tt_type == VAR_NUMBER) |
| 347 | return &t_list_number; |
| 348 | if (member_type->tt_type == VAR_STRING) |
| 349 | return &t_list_string; |
| 350 | |
| 351 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 352 | type = alloc_type(type_gap); |
| 353 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 354 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 355 | type->tt_type = VAR_LIST; |
| 356 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 357 | type->tt_argcount = 0; |
| 358 | type->tt_args = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 359 | return type; |
| 360 | } |
| 361 | |
| 362 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 363 | get_dict_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 364 | { |
| 365 | type_T *type; |
| 366 | |
| 367 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 368 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 369 | return &t_dict_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 370 | if (member_type->tt_type == VAR_VOID |
| 371 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 372 | return &t_dict_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 373 | if (member_type->tt_type == VAR_BOOL) |
| 374 | return &t_dict_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 375 | if (member_type->tt_type == VAR_NUMBER) |
| 376 | return &t_dict_number; |
| 377 | if (member_type->tt_type == VAR_STRING) |
| 378 | return &t_dict_string; |
| 379 | |
| 380 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 381 | type = alloc_type(type_gap); |
| 382 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 383 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 384 | type->tt_type = VAR_DICT; |
| 385 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 386 | type->tt_argcount = 0; |
| 387 | type->tt_args = NULL; |
| 388 | return type; |
| 389 | } |
| 390 | |
| 391 | /* |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 392 | * Allocate a new type for a function. |
| 393 | */ |
| 394 | static type_T * |
| 395 | alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 396 | { |
| 397 | type_T *type = alloc_type(type_gap); |
| 398 | |
| 399 | if (type == NULL) |
| 400 | return &t_any; |
| 401 | type->tt_type = VAR_FUNC; |
| 402 | type->tt_member = ret_type; |
| 403 | type->tt_argcount = argcount; |
| 404 | type->tt_args = NULL; |
| 405 | return type; |
| 406 | } |
| 407 | |
| 408 | /* |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 409 | * Get a function type, based on the return type "ret_type". |
| 410 | * If "argcount" is -1 or 0 a predefined type can be used. |
| 411 | * If "argcount" > 0 always create a new type, so that arguments can be added. |
| 412 | */ |
| 413 | static type_T * |
| 414 | get_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 415 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 416 | // recognize commonly used types |
| 417 | if (argcount <= 0) |
| 418 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 419 | if (ret_type == &t_unknown) |
| 420 | { |
| 421 | // (argcount == 0) is not possible |
| 422 | return &t_func_unknown; |
| 423 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 424 | if (ret_type == &t_void) |
| 425 | { |
| 426 | if (argcount == 0) |
| 427 | return &t_func_0_void; |
| 428 | else |
| 429 | return &t_func_void; |
| 430 | } |
| 431 | if (ret_type == &t_any) |
| 432 | { |
| 433 | if (argcount == 0) |
| 434 | return &t_func_0_any; |
| 435 | else |
| 436 | return &t_func_any; |
| 437 | } |
| 438 | if (ret_type == &t_number) |
| 439 | { |
| 440 | if (argcount == 0) |
| 441 | return &t_func_0_number; |
| 442 | else |
| 443 | return &t_func_number; |
| 444 | } |
| 445 | if (ret_type == &t_string) |
| 446 | { |
| 447 | if (argcount == 0) |
| 448 | return &t_func_0_string; |
| 449 | else |
| 450 | return &t_func_string; |
| 451 | } |
| 452 | } |
| 453 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 454 | return alloc_func_type(ret_type, argcount, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 455 | } |
| 456 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 457 | /* |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 458 | * For a function type, reserve space for "argcount" argument types (including |
| 459 | * vararg). |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 460 | */ |
| 461 | static int |
| 462 | func_type_add_arg_types( |
| 463 | type_T *functype, |
| 464 | int argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 465 | garray_T *type_gap) |
| 466 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 467 | // To make it easy to free the space needed for the argument types, add the |
| 468 | // pointer to type_gap. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 469 | if (ga_grow(type_gap, 1) == FAIL) |
| 470 | return FAIL; |
| 471 | functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); |
| 472 | if (functype->tt_args == NULL) |
| 473 | return FAIL; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 474 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = |
| 475 | (void *)functype->tt_args; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 476 | ++type_gap->ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 477 | return OK; |
| 478 | } |
| 479 | |
| 480 | /* |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 481 | * Return the type_T for a typval. Only for primitive types. |
| 482 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 483 | type_T * |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 484 | typval2type(typval_T *tv) |
| 485 | { |
| 486 | if (tv->v_type == VAR_NUMBER) |
| 487 | return &t_number; |
| 488 | if (tv->v_type == VAR_BOOL) |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 489 | return &t_bool; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 490 | if (tv->v_type == VAR_STRING) |
| 491 | return &t_string; |
| 492 | if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles |
| 493 | return &t_list_string; |
| 494 | if (tv->v_type == VAR_DICT) // e.g. for v:completed_item |
| 495 | return &t_dict_any; |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 496 | return &t_any; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 497 | } |
| 498 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 499 | static void |
| 500 | type_mismatch(type_T *expected, type_T *actual) |
| 501 | { |
| 502 | char *tofree1, *tofree2; |
| 503 | |
| 504 | semsg(_("E1013: type mismatch, expected %s but got %s"), |
| 505 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 506 | vim_free(tofree1); |
| 507 | vim_free(tofree2); |
| 508 | } |
| 509 | |
| 510 | static void |
| 511 | arg_type_mismatch(type_T *expected, type_T *actual, int argidx) |
| 512 | { |
| 513 | char *tofree1, *tofree2; |
| 514 | |
| 515 | semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"), |
| 516 | argidx, |
| 517 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 518 | vim_free(tofree1); |
| 519 | vim_free(tofree2); |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Check if the expected and actual types match. |
| 524 | * Does not allow for assigning "any" to a specific type. |
| 525 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 526 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 527 | check_type(type_T *expected, type_T *actual, int give_msg) |
| 528 | { |
| 529 | int ret = OK; |
| 530 | |
| 531 | // When expected is "unknown" we accept any actual type. |
| 532 | // When expected is "any" we accept any actual type except "void". |
| 533 | if (expected->tt_type != VAR_UNKNOWN |
| 534 | && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) |
| 535 | |
| 536 | { |
| 537 | if (expected->tt_type != actual->tt_type) |
| 538 | { |
| 539 | if (give_msg) |
| 540 | type_mismatch(expected, actual); |
| 541 | return FAIL; |
| 542 | } |
| 543 | if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) |
| 544 | { |
| 545 | // "unknown" is used for an empty list or dict |
| 546 | if (actual->tt_member != &t_unknown) |
| 547 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 548 | } |
| 549 | else if (expected->tt_type == VAR_FUNC) |
| 550 | { |
| 551 | if (expected->tt_member != &t_unknown) |
| 552 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 553 | if (ret == OK && expected->tt_argcount != -1 |
| 554 | && (actual->tt_argcount < expected->tt_min_argcount |
| 555 | || actual->tt_argcount > expected->tt_argcount)) |
| 556 | ret = FAIL; |
| 557 | } |
| 558 | if (ret == FAIL && give_msg) |
| 559 | type_mismatch(expected, actual); |
| 560 | } |
| 561 | return ret; |
| 562 | } |
| 563 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 564 | /* |
| 565 | * Return FAIl if "expected" and "actual" don't match. |
| 566 | * TODO: better type comparison |
| 567 | */ |
| 568 | int |
| 569 | check_argtype(type_T *expected, typval_T *actual_tv) |
| 570 | { |
| 571 | type_T actual; |
| 572 | type_T member; |
| 573 | |
| 574 | // TODO: should should be done with more levels |
| 575 | CLEAR_FIELD(actual); |
| 576 | actual.tt_type = actual_tv->v_type; |
| 577 | if (actual_tv->v_type == VAR_LIST |
| 578 | && actual_tv->vval.v_list != NULL |
| 579 | && actual_tv->vval.v_list->lv_first != NULL) |
| 580 | { |
| 581 | // Use the type of the first member, it is the most specific. |
| 582 | CLEAR_FIELD(member); |
| 583 | member.tt_type = actual_tv->vval.v_list->lv_first->li_tv.v_type; |
| 584 | member.tt_member = &t_any; |
| 585 | actual.tt_member = &member; |
| 586 | } |
| 587 | else if (actual_tv->v_type == VAR_DICT |
| 588 | && actual_tv->vval.v_dict != NULL |
| 589 | && actual_tv->vval.v_dict->dv_hashtab.ht_used > 0) |
| 590 | { |
| 591 | dict_iterator_T iter; |
| 592 | typval_T *value; |
| 593 | |
| 594 | // Use the type of the first value, it is the most specific. |
| 595 | dict_iterate_start(actual_tv, &iter); |
| 596 | dict_iterate_next(&iter, &value); |
| 597 | CLEAR_FIELD(member); |
| 598 | member.tt_type = value->v_type; |
| 599 | member.tt_member = &t_any; |
| 600 | actual.tt_member = &member; |
| 601 | } |
Bram Moolenaar | 0f60e80 | 2020-07-22 20:16:11 +0200 | [diff] [blame^] | 602 | else if (actual_tv->v_type == VAR_FUNC || actual_tv->v_type == VAR_PARTIAL) |
| 603 | { |
| 604 | char_u *name = NULL; |
| 605 | ufunc_T *ufunc = NULL; |
| 606 | |
| 607 | if (actual_tv->v_type == VAR_PARTIAL) |
| 608 | { |
| 609 | if (actual_tv->vval.v_partial->pt_func != NULL) |
| 610 | ufunc = actual_tv->vval.v_partial->pt_func; |
| 611 | else |
| 612 | name = actual_tv->vval.v_partial->pt_name; |
| 613 | } |
| 614 | else |
| 615 | name = actual_tv->vval.v_string; |
| 616 | if (name != NULL) |
| 617 | // TODO: how about a builtin function? |
| 618 | ufunc = find_func(name, FALSE, NULL); |
| 619 | if (ufunc != NULL && ufunc->uf_func_type != NULL) |
| 620 | actual = *ufunc->uf_func_type; |
| 621 | else |
| 622 | actual.tt_member = &t_any; |
| 623 | } |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 624 | else |
| 625 | actual.tt_member = &t_any; |
| 626 | return check_type(expected, &actual, TRUE); |
| 627 | } |
| 628 | |
| 629 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 630 | ///////////////////////////////////////////////////////////////////// |
| 631 | // Following generate_ functions expect the caller to call ga_grow(). |
| 632 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 633 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 634 | #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] | 635 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 636 | /* |
| 637 | * Generate an instruction without arguments. |
| 638 | * Returns a pointer to the new instruction, NULL if failed. |
| 639 | */ |
| 640 | static isn_T * |
| 641 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 642 | { |
| 643 | garray_T *instr = &cctx->ctx_instr; |
| 644 | isn_T *isn; |
| 645 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 646 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 647 | if (ga_grow(instr, 1) == FAIL) |
| 648 | return NULL; |
| 649 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 650 | isn->isn_type = isn_type; |
| 651 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 652 | ++instr->ga_len; |
| 653 | |
| 654 | return isn; |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * Generate an instruction without arguments. |
| 659 | * "drop" will be removed from the stack. |
| 660 | * Returns a pointer to the new instruction, NULL if failed. |
| 661 | */ |
| 662 | static isn_T * |
| 663 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 664 | { |
| 665 | garray_T *stack = &cctx->ctx_type_stack; |
| 666 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 667 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 668 | stack->ga_len -= drop; |
| 669 | return generate_instr(cctx, isn_type); |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 674 | */ |
| 675 | static isn_T * |
| 676 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 677 | { |
| 678 | isn_T *isn; |
| 679 | garray_T *stack = &cctx->ctx_type_stack; |
| 680 | |
| 681 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 682 | return NULL; |
| 683 | |
| 684 | if (ga_grow(stack, 1) == FAIL) |
| 685 | return NULL; |
| 686 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 687 | ++stack->ga_len; |
| 688 | |
| 689 | return isn; |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 694 | */ |
| 695 | static int |
| 696 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 697 | { |
| 698 | isn_T *isn; |
| 699 | garray_T *stack = &cctx->ctx_type_stack; |
| 700 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 701 | |
| 702 | if ((*type)->tt_type == VAR_STRING) |
| 703 | return OK; |
| 704 | *type = &t_string; |
| 705 | |
| 706 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 707 | return FAIL; |
| 708 | isn->isn_arg.number = offset; |
| 709 | |
| 710 | return OK; |
| 711 | } |
| 712 | |
| 713 | static int |
| 714 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 715 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 716 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 717 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 718 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 719 | { |
| 720 | if (*op == '+') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 721 | emsg(_("E1051: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 722 | else |
| 723 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 724 | return FAIL; |
| 725 | } |
| 726 | return OK; |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * Generate an instruction with two arguments. The instruction depends on the |
| 731 | * type of the arguments. |
| 732 | */ |
| 733 | static int |
| 734 | generate_two_op(cctx_T *cctx, char_u *op) |
| 735 | { |
| 736 | garray_T *stack = &cctx->ctx_type_stack; |
| 737 | type_T *type1; |
| 738 | type_T *type2; |
| 739 | vartype_T vartype; |
| 740 | isn_T *isn; |
| 741 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 742 | RETURN_OK_IF_SKIP(cctx); |
| 743 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 744 | // Get the known type of the two items on the stack. If they are matching |
| 745 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 746 | // checking. |
| 747 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 748 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 749 | vartype = VAR_ANY; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 750 | if (type1->tt_type == type2->tt_type |
| 751 | && (type1->tt_type == VAR_NUMBER |
| 752 | || type1->tt_type == VAR_LIST |
| 753 | #ifdef FEAT_FLOAT |
| 754 | || type1->tt_type == VAR_FLOAT |
| 755 | #endif |
| 756 | || type1->tt_type == VAR_BLOB)) |
| 757 | vartype = type1->tt_type; |
| 758 | |
| 759 | switch (*op) |
| 760 | { |
| 761 | case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 762 | && type1->tt_type != VAR_ANY |
| 763 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 764 | && check_number_or_float( |
| 765 | type1->tt_type, type2->tt_type, op) == FAIL) |
| 766 | return FAIL; |
| 767 | isn = generate_instr_drop(cctx, |
| 768 | vartype == VAR_NUMBER ? ISN_OPNR |
| 769 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 770 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 771 | #ifdef FEAT_FLOAT |
| 772 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 773 | #endif |
| 774 | : ISN_OPANY, 1); |
| 775 | if (isn != NULL) |
| 776 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 777 | break; |
| 778 | |
| 779 | case '-': |
| 780 | case '*': |
| 781 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 782 | op) == FAIL) |
| 783 | return FAIL; |
| 784 | if (vartype == VAR_NUMBER) |
| 785 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 786 | #ifdef FEAT_FLOAT |
| 787 | else if (vartype == VAR_FLOAT) |
| 788 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 789 | #endif |
| 790 | else |
| 791 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 792 | if (isn != NULL) |
| 793 | isn->isn_arg.op.op_type = *op == '*' |
| 794 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 795 | break; |
| 796 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 797 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 798 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 799 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 800 | && type2->tt_type != VAR_NUMBER)) |
| 801 | { |
| 802 | emsg(_("E1035: % requires number arguments")); |
| 803 | return FAIL; |
| 804 | } |
| 805 | isn = generate_instr_drop(cctx, |
| 806 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 807 | if (isn != NULL) |
| 808 | isn->isn_arg.op.op_type = EXPR_REM; |
| 809 | break; |
| 810 | } |
| 811 | |
| 812 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 813 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 814 | { |
| 815 | type_T *type = &t_any; |
| 816 | |
| 817 | #ifdef FEAT_FLOAT |
| 818 | // float+number and number+float results in float |
| 819 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 820 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 821 | type = &t_float; |
| 822 | #endif |
| 823 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 824 | } |
| 825 | |
| 826 | return OK; |
| 827 | } |
| 828 | |
| 829 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 830 | * Get the instruction to use for comparing "type1" with "type2" |
| 831 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 832 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 833 | static isntype_T |
| 834 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 835 | { |
| 836 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 837 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 838 | if (type1 == VAR_UNKNOWN) |
| 839 | type1 = VAR_ANY; |
| 840 | if (type2 == VAR_UNKNOWN) |
| 841 | type2 = VAR_ANY; |
| 842 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 843 | if (type1 == type2) |
| 844 | { |
| 845 | switch (type1) |
| 846 | { |
| 847 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 848 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 849 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 850 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 851 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 852 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 853 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 854 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 855 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 856 | default: isntype = ISN_COMPAREANY; break; |
| 857 | } |
| 858 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 859 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 860 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 861 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 862 | isntype = ISN_COMPAREANY; |
| 863 | |
| 864 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 865 | && (isntype == ISN_COMPAREBOOL |
| 866 | || isntype == ISN_COMPARESPECIAL |
| 867 | || isntype == ISN_COMPARENR |
| 868 | || isntype == ISN_COMPAREFLOAT)) |
| 869 | { |
| 870 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 871 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 872 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 873 | } |
| 874 | if (isntype == ISN_DROP |
| 875 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 876 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 877 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 878 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 879 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 880 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 881 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 882 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 883 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 884 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 885 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 886 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 887 | return isntype; |
| 888 | } |
| 889 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 890 | int |
| 891 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 892 | { |
| 893 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 894 | return FAIL; |
| 895 | return OK; |
| 896 | } |
| 897 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 898 | /* |
| 899 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 900 | */ |
| 901 | static int |
| 902 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 903 | { |
| 904 | isntype_T isntype; |
| 905 | isn_T *isn; |
| 906 | garray_T *stack = &cctx->ctx_type_stack; |
| 907 | vartype_T type1; |
| 908 | vartype_T type2; |
| 909 | |
| 910 | RETURN_OK_IF_SKIP(cctx); |
| 911 | |
| 912 | // Get the known type of the two items on the stack. If they are matching |
| 913 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 914 | // checking. |
| 915 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 916 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 917 | isntype = get_compare_isn(exptype, type1, type2); |
| 918 | if (isntype == ISN_DROP) |
| 919 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 920 | |
| 921 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 922 | return FAIL; |
| 923 | isn->isn_arg.op.op_type = exptype; |
| 924 | isn->isn_arg.op.op_ic = ic; |
| 925 | |
| 926 | // takes two arguments, puts one bool back |
| 927 | if (stack->ga_len >= 2) |
| 928 | { |
| 929 | --stack->ga_len; |
| 930 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 931 | } |
| 932 | |
| 933 | return OK; |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * Generate an ISN_2BOOL instruction. |
| 938 | */ |
| 939 | static int |
| 940 | generate_2BOOL(cctx_T *cctx, int invert) |
| 941 | { |
| 942 | isn_T *isn; |
| 943 | garray_T *stack = &cctx->ctx_type_stack; |
| 944 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 945 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 946 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 947 | return FAIL; |
| 948 | isn->isn_arg.number = invert; |
| 949 | |
| 950 | // type becomes bool |
| 951 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 952 | |
| 953 | return OK; |
| 954 | } |
| 955 | |
| 956 | static int |
| 957 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 958 | { |
| 959 | isn_T *isn; |
| 960 | garray_T *stack = &cctx->ctx_type_stack; |
| 961 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 962 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 963 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 964 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 965 | // TODO: whole type, e.g. for a function also arg and return types |
| 966 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 967 | isn->isn_arg.type.ct_off = offset; |
| 968 | |
| 969 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 970 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 971 | |
| 972 | return OK; |
| 973 | } |
| 974 | |
| 975 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 976 | * Check that |
| 977 | * - "actual" is "expected" type or |
| 978 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 979 | * - return FAIL. |
| 980 | */ |
| 981 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 982 | need_type( |
| 983 | type_T *actual, |
| 984 | type_T *expected, |
| 985 | int offset, |
| 986 | cctx_T *cctx, |
| 987 | int silent) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 988 | { |
| 989 | if (check_type(expected, actual, FALSE) == OK) |
| 990 | return OK; |
| 991 | if (actual->tt_type != VAR_ANY |
| 992 | && actual->tt_type != VAR_UNKNOWN |
| 993 | && !(actual->tt_type == VAR_FUNC |
| 994 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 995 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 996 | if (!silent) |
| 997 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 998 | return FAIL; |
| 999 | } |
| 1000 | generate_TYPECHECK(cctx, expected, offset); |
| 1001 | return OK; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1005 | * Generate an ISN_PUSHNR instruction. |
| 1006 | */ |
| 1007 | static int |
| 1008 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 1009 | { |
| 1010 | isn_T *isn; |
| 1011 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1012 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1013 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 1014 | return FAIL; |
| 1015 | isn->isn_arg.number = number; |
| 1016 | |
| 1017 | return OK; |
| 1018 | } |
| 1019 | |
| 1020 | /* |
| 1021 | * Generate an ISN_PUSHBOOL instruction. |
| 1022 | */ |
| 1023 | static int |
| 1024 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 1025 | { |
| 1026 | isn_T *isn; |
| 1027 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1028 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1029 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 1030 | return FAIL; |
| 1031 | isn->isn_arg.number = number; |
| 1032 | |
| 1033 | return OK; |
| 1034 | } |
| 1035 | |
| 1036 | /* |
| 1037 | * Generate an ISN_PUSHSPEC instruction. |
| 1038 | */ |
| 1039 | static int |
| 1040 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 1041 | { |
| 1042 | isn_T *isn; |
| 1043 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1044 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1045 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 1046 | return FAIL; |
| 1047 | isn->isn_arg.number = number; |
| 1048 | |
| 1049 | return OK; |
| 1050 | } |
| 1051 | |
| 1052 | #ifdef FEAT_FLOAT |
| 1053 | /* |
| 1054 | * Generate an ISN_PUSHF instruction. |
| 1055 | */ |
| 1056 | static int |
| 1057 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 1058 | { |
| 1059 | isn_T *isn; |
| 1060 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1061 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1062 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 1063 | return FAIL; |
| 1064 | isn->isn_arg.fnumber = fnumber; |
| 1065 | |
| 1066 | return OK; |
| 1067 | } |
| 1068 | #endif |
| 1069 | |
| 1070 | /* |
| 1071 | * Generate an ISN_PUSHS instruction. |
| 1072 | * Consumes "str". |
| 1073 | */ |
| 1074 | static int |
| 1075 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 1076 | { |
| 1077 | isn_T *isn; |
| 1078 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1079 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1080 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 1081 | return FAIL; |
| 1082 | isn->isn_arg.string = str; |
| 1083 | |
| 1084 | return OK; |
| 1085 | } |
| 1086 | |
| 1087 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1088 | * Generate an ISN_PUSHCHANNEL instruction. |
| 1089 | * Consumes "channel". |
| 1090 | */ |
| 1091 | static int |
| 1092 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 1093 | { |
| 1094 | isn_T *isn; |
| 1095 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1096 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1097 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 1098 | return FAIL; |
| 1099 | isn->isn_arg.channel = channel; |
| 1100 | |
| 1101 | return OK; |
| 1102 | } |
| 1103 | |
| 1104 | /* |
| 1105 | * Generate an ISN_PUSHJOB instruction. |
| 1106 | * Consumes "job". |
| 1107 | */ |
| 1108 | static int |
| 1109 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 1110 | { |
| 1111 | isn_T *isn; |
| 1112 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1113 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1114 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1115 | return FAIL; |
| 1116 | isn->isn_arg.job = job; |
| 1117 | |
| 1118 | return OK; |
| 1119 | } |
| 1120 | |
| 1121 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1122 | * Generate an ISN_PUSHBLOB instruction. |
| 1123 | * Consumes "blob". |
| 1124 | */ |
| 1125 | static int |
| 1126 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1127 | { |
| 1128 | isn_T *isn; |
| 1129 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1130 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1131 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1132 | return FAIL; |
| 1133 | isn->isn_arg.blob = blob; |
| 1134 | |
| 1135 | return OK; |
| 1136 | } |
| 1137 | |
| 1138 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1139 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1140 | * Consumes "name". |
| 1141 | */ |
| 1142 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1143 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1144 | { |
| 1145 | isn_T *isn; |
| 1146 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1147 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1148 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1149 | return FAIL; |
| 1150 | isn->isn_arg.string = name; |
| 1151 | |
| 1152 | return OK; |
| 1153 | } |
| 1154 | |
| 1155 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1156 | * Generate an ISN_GETITEM instruction with "index". |
| 1157 | */ |
| 1158 | static int |
| 1159 | generate_GETITEM(cctx_T *cctx, int index) |
| 1160 | { |
| 1161 | isn_T *isn; |
| 1162 | garray_T *stack = &cctx->ctx_type_stack; |
| 1163 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1164 | type_T *item_type = &t_any; |
| 1165 | |
| 1166 | RETURN_OK_IF_SKIP(cctx); |
| 1167 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1168 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1169 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1170 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1171 | emsg(_(e_listreq)); |
| 1172 | return FAIL; |
| 1173 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1174 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1175 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1176 | return FAIL; |
| 1177 | isn->isn_arg.number = index; |
| 1178 | |
| 1179 | // add the item type to the type stack |
| 1180 | if (ga_grow(stack, 1) == FAIL) |
| 1181 | return FAIL; |
| 1182 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 1183 | ++stack->ga_len; |
| 1184 | return OK; |
| 1185 | } |
| 1186 | |
| 1187 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 1188 | * Generate an ISN_SLICE instruction with "count". |
| 1189 | */ |
| 1190 | static int |
| 1191 | generate_SLICE(cctx_T *cctx, int count) |
| 1192 | { |
| 1193 | isn_T *isn; |
| 1194 | |
| 1195 | RETURN_OK_IF_SKIP(cctx); |
| 1196 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1197 | return FAIL; |
| 1198 | isn->isn_arg.number = count; |
| 1199 | return OK; |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1204 | */ |
| 1205 | static int |
| 1206 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1207 | { |
| 1208 | isn_T *isn; |
| 1209 | |
| 1210 | RETURN_OK_IF_SKIP(cctx); |
| 1211 | |
| 1212 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1213 | return FAIL; |
| 1214 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1215 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1216 | |
| 1217 | return OK; |
| 1218 | } |
| 1219 | |
| 1220 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1221 | * Generate an ISN_STORE instruction. |
| 1222 | */ |
| 1223 | static int |
| 1224 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1225 | { |
| 1226 | isn_T *isn; |
| 1227 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1228 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1229 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1230 | return FAIL; |
| 1231 | if (name != NULL) |
| 1232 | isn->isn_arg.string = vim_strsave(name); |
| 1233 | else |
| 1234 | isn->isn_arg.number = idx; |
| 1235 | |
| 1236 | return OK; |
| 1237 | } |
| 1238 | |
| 1239 | /* |
| 1240 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1241 | */ |
| 1242 | static int |
| 1243 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1244 | { |
| 1245 | isn_T *isn; |
| 1246 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1247 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1248 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1249 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1250 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1251 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1252 | |
| 1253 | return OK; |
| 1254 | } |
| 1255 | |
| 1256 | /* |
| 1257 | * Generate an ISN_STOREOPT instruction |
| 1258 | */ |
| 1259 | static int |
| 1260 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1261 | { |
| 1262 | isn_T *isn; |
| 1263 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1264 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1265 | if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL) |
| 1266 | return FAIL; |
| 1267 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1268 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1269 | |
| 1270 | return OK; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
| 1274 | * Generate an ISN_LOAD or similar instruction. |
| 1275 | */ |
| 1276 | static int |
| 1277 | generate_LOAD( |
| 1278 | cctx_T *cctx, |
| 1279 | isntype_T isn_type, |
| 1280 | int idx, |
| 1281 | char_u *name, |
| 1282 | type_T *type) |
| 1283 | { |
| 1284 | isn_T *isn; |
| 1285 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1286 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1287 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1288 | return FAIL; |
| 1289 | if (name != NULL) |
| 1290 | isn->isn_arg.string = vim_strsave(name); |
| 1291 | else |
| 1292 | isn->isn_arg.number = idx; |
| 1293 | |
| 1294 | return OK; |
| 1295 | } |
| 1296 | |
| 1297 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1298 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1299 | */ |
| 1300 | static int |
| 1301 | generate_LOADV( |
| 1302 | cctx_T *cctx, |
| 1303 | char_u *name, |
| 1304 | int error) |
| 1305 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1306 | int di_flags; |
| 1307 | int vidx = find_vim_var(name, &di_flags); |
| 1308 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1309 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1310 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1311 | if (vidx < 0) |
| 1312 | { |
| 1313 | if (error) |
| 1314 | semsg(_(e_var_notfound), name); |
| 1315 | return FAIL; |
| 1316 | } |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1317 | type = typval2type(get_vim_var_tv(vidx)); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1318 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1319 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1323 | * Generate an ISN_UNLET instruction. |
| 1324 | */ |
| 1325 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1326 | 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] | 1327 | { |
| 1328 | isn_T *isn; |
| 1329 | |
| 1330 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1331 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1332 | return FAIL; |
| 1333 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1334 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1335 | |
| 1336 | return OK; |
| 1337 | } |
| 1338 | |
| 1339 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1340 | * Generate an ISN_LOADS instruction. |
| 1341 | */ |
| 1342 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1343 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1344 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1345 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1346 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1347 | int sid, |
| 1348 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1349 | { |
| 1350 | isn_T *isn; |
| 1351 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1352 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1353 | if (isn_type == ISN_LOADS) |
| 1354 | isn = generate_instr_type(cctx, isn_type, type); |
| 1355 | else |
| 1356 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1357 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1358 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1359 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1360 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1361 | |
| 1362 | return OK; |
| 1363 | } |
| 1364 | |
| 1365 | /* |
| 1366 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1367 | */ |
| 1368 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1369 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1370 | cctx_T *cctx, |
| 1371 | isntype_T isn_type, |
| 1372 | int sid, |
| 1373 | int idx, |
| 1374 | type_T *type) |
| 1375 | { |
| 1376 | isn_T *isn; |
| 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_type == ISN_LOADSCRIPT) |
| 1380 | isn = generate_instr_type(cctx, isn_type, type); |
| 1381 | else |
| 1382 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1383 | if (isn == NULL) |
| 1384 | return FAIL; |
| 1385 | isn->isn_arg.script.script_sid = sid; |
| 1386 | isn->isn_arg.script.script_idx = idx; |
| 1387 | return OK; |
| 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * Generate an ISN_NEWLIST instruction. |
| 1392 | */ |
| 1393 | static int |
| 1394 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1395 | { |
| 1396 | isn_T *isn; |
| 1397 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1398 | type_T *type; |
| 1399 | type_T *member; |
| 1400 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1401 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1402 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1403 | return FAIL; |
| 1404 | isn->isn_arg.number = count; |
| 1405 | |
| 1406 | // drop the value types |
| 1407 | stack->ga_len -= count; |
| 1408 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1409 | // 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] | 1410 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1411 | if (count > 0) |
| 1412 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1413 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1414 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1415 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1416 | |
| 1417 | // add the list type to the type stack |
| 1418 | if (ga_grow(stack, 1) == FAIL) |
| 1419 | return FAIL; |
| 1420 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1421 | ++stack->ga_len; |
| 1422 | |
| 1423 | return OK; |
| 1424 | } |
| 1425 | |
| 1426 | /* |
| 1427 | * Generate an ISN_NEWDICT instruction. |
| 1428 | */ |
| 1429 | static int |
| 1430 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1431 | { |
| 1432 | isn_T *isn; |
| 1433 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1434 | type_T *type; |
| 1435 | type_T *member; |
| 1436 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1437 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1438 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1439 | return FAIL; |
| 1440 | isn->isn_arg.number = count; |
| 1441 | |
| 1442 | // drop the key and value types |
| 1443 | stack->ga_len -= 2 * count; |
| 1444 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1445 | // Use the first value type for the list member type. Use "void" for an |
| 1446 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1447 | if (count > 0) |
| 1448 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1449 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1450 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1451 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1452 | |
| 1453 | // add the dict type to the type stack |
| 1454 | if (ga_grow(stack, 1) == FAIL) |
| 1455 | return FAIL; |
| 1456 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1457 | ++stack->ga_len; |
| 1458 | |
| 1459 | return OK; |
| 1460 | } |
| 1461 | |
| 1462 | /* |
| 1463 | * Generate an ISN_FUNCREF instruction. |
| 1464 | */ |
| 1465 | static int |
| 1466 | generate_FUNCREF(cctx_T *cctx, int dfunc_idx) |
| 1467 | { |
| 1468 | isn_T *isn; |
| 1469 | garray_T *stack = &cctx->ctx_type_stack; |
| 1470 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1471 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1472 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1473 | return FAIL; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1474 | isn->isn_arg.funcref.fr_func = dfunc_idx; |
| 1475 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1476 | |
| 1477 | if (ga_grow(stack, 1) == FAIL) |
| 1478 | return FAIL; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1479 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1480 | // TODO: argument and return types |
| 1481 | ++stack->ga_len; |
| 1482 | |
| 1483 | return OK; |
| 1484 | } |
| 1485 | |
| 1486 | /* |
| 1487 | * Generate an ISN_JUMP instruction. |
| 1488 | */ |
| 1489 | static int |
| 1490 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1491 | { |
| 1492 | isn_T *isn; |
| 1493 | garray_T *stack = &cctx->ctx_type_stack; |
| 1494 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1495 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1496 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1497 | return FAIL; |
| 1498 | isn->isn_arg.jump.jump_when = when; |
| 1499 | isn->isn_arg.jump.jump_where = where; |
| 1500 | |
| 1501 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1502 | --stack->ga_len; |
| 1503 | |
| 1504 | return OK; |
| 1505 | } |
| 1506 | |
| 1507 | static int |
| 1508 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1509 | { |
| 1510 | isn_T *isn; |
| 1511 | garray_T *stack = &cctx->ctx_type_stack; |
| 1512 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1513 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1514 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1515 | return FAIL; |
| 1516 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1517 | |
| 1518 | if (ga_grow(stack, 1) == FAIL) |
| 1519 | return FAIL; |
| 1520 | // type doesn't matter, will be stored next |
| 1521 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1522 | ++stack->ga_len; |
| 1523 | |
| 1524 | return OK; |
| 1525 | } |
| 1526 | |
| 1527 | /* |
| 1528 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1529 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1530 | * Return FAIL if the number of arguments is wrong. |
| 1531 | */ |
| 1532 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1533 | 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] | 1534 | { |
| 1535 | isn_T *isn; |
| 1536 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1537 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1538 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1539 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1540 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1541 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1542 | argoff = check_internal_func(func_idx, argcount); |
| 1543 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1544 | return FAIL; |
| 1545 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1546 | if (method_call && argoff > 1) |
| 1547 | { |
| 1548 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1549 | return FAIL; |
| 1550 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1551 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1552 | } |
| 1553 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1554 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1555 | return FAIL; |
| 1556 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1557 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1558 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1559 | for (i = 0; i < argcount; ++i) |
| 1560 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1561 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1562 | stack->ga_len -= argcount; // drop the arguments |
| 1563 | if (ga_grow(stack, 1) == FAIL) |
| 1564 | return FAIL; |
| 1565 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1566 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1567 | ++stack->ga_len; // add return value |
| 1568 | |
| 1569 | return OK; |
| 1570 | } |
| 1571 | |
| 1572 | /* |
| 1573 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1574 | * Return FAIL if the number of arguments is wrong. |
| 1575 | */ |
| 1576 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1577 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1578 | { |
| 1579 | isn_T *isn; |
| 1580 | garray_T *stack = &cctx->ctx_type_stack; |
| 1581 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1582 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1583 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1584 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1585 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1586 | { |
| 1587 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1588 | return FAIL; |
| 1589 | } |
| 1590 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1591 | { |
| 1592 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1593 | return FAIL; |
| 1594 | } |
| 1595 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1596 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1597 | { |
| 1598 | int i; |
| 1599 | |
| 1600 | for (i = 0; i < argcount; ++i) |
| 1601 | { |
| 1602 | type_T *expected; |
| 1603 | type_T *actual; |
| 1604 | |
| 1605 | if (i < regular_args) |
| 1606 | { |
| 1607 | if (ufunc->uf_arg_types == NULL) |
| 1608 | continue; |
| 1609 | expected = ufunc->uf_arg_types[i]; |
| 1610 | } |
| 1611 | else |
| 1612 | expected = ufunc->uf_va_type->tt_member; |
| 1613 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1614 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1615 | { |
| 1616 | arg_type_mismatch(expected, actual, i + 1); |
| 1617 | return FAIL; |
| 1618 | } |
| 1619 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1620 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 1621 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1622 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1623 | } |
| 1624 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1625 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1626 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1627 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1628 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1629 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1630 | { |
| 1631 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1632 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1633 | } |
| 1634 | else |
| 1635 | { |
| 1636 | // A user function may be deleted and redefined later, can't use the |
| 1637 | // ufunc pointer, need to look it up again at runtime. |
| 1638 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1639 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1640 | } |
| 1641 | |
| 1642 | stack->ga_len -= argcount; // drop the arguments |
| 1643 | if (ga_grow(stack, 1) == FAIL) |
| 1644 | return FAIL; |
| 1645 | // add return value |
| 1646 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1647 | ++stack->ga_len; |
| 1648 | |
| 1649 | return OK; |
| 1650 | } |
| 1651 | |
| 1652 | /* |
| 1653 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1654 | */ |
| 1655 | static int |
| 1656 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1657 | { |
| 1658 | isn_T *isn; |
| 1659 | garray_T *stack = &cctx->ctx_type_stack; |
| 1660 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1661 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1662 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1663 | return FAIL; |
| 1664 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1665 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1666 | |
| 1667 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1668 | if (ga_grow(stack, 1) == FAIL) |
| 1669 | return FAIL; |
| 1670 | // add return value |
| 1671 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1672 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1673 | |
| 1674 | return OK; |
| 1675 | } |
| 1676 | |
| 1677 | /* |
| 1678 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1679 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1680 | */ |
| 1681 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1682 | generate_PCALL( |
| 1683 | cctx_T *cctx, |
| 1684 | int argcount, |
| 1685 | char_u *name, |
| 1686 | type_T *type, |
| 1687 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1688 | { |
| 1689 | isn_T *isn; |
| 1690 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1691 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1692 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1693 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1694 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1695 | if (type->tt_type == VAR_ANY) |
| 1696 | ret_type = &t_any; |
| 1697 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1698 | { |
| 1699 | if (type->tt_argcount != -1) |
| 1700 | { |
| 1701 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1702 | |
| 1703 | if (argcount < type->tt_min_argcount - varargs) |
| 1704 | { |
| 1705 | semsg(_(e_toofewarg), "[reference]"); |
| 1706 | return FAIL; |
| 1707 | } |
| 1708 | if (!varargs && argcount > type->tt_argcount) |
| 1709 | { |
| 1710 | semsg(_(e_toomanyarg), "[reference]"); |
| 1711 | return FAIL; |
| 1712 | } |
| 1713 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1714 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1715 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1716 | else |
| 1717 | { |
| 1718 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1719 | return FAIL; |
| 1720 | } |
| 1721 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1722 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1723 | return FAIL; |
| 1724 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1725 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1726 | |
| 1727 | stack->ga_len -= argcount; // drop the arguments |
| 1728 | |
| 1729 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1730 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1731 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1732 | // If partial is above the arguments it must be cleared and replaced with |
| 1733 | // the return value. |
| 1734 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1735 | return FAIL; |
| 1736 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1737 | return OK; |
| 1738 | } |
| 1739 | |
| 1740 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1741 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1742 | */ |
| 1743 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1744 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1745 | { |
| 1746 | isn_T *isn; |
| 1747 | garray_T *stack = &cctx->ctx_type_stack; |
| 1748 | type_T *type; |
| 1749 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1750 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1751 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1752 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1753 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1754 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1755 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1756 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1757 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1758 | { |
| 1759 | emsg(_(e_dictreq)); |
| 1760 | return FAIL; |
| 1761 | } |
| 1762 | // change dict type to dict member type |
| 1763 | if (type->tt_type == VAR_DICT) |
| 1764 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1765 | |
| 1766 | return OK; |
| 1767 | } |
| 1768 | |
| 1769 | /* |
| 1770 | * Generate an ISN_ECHO instruction. |
| 1771 | */ |
| 1772 | static int |
| 1773 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1774 | { |
| 1775 | isn_T *isn; |
| 1776 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1777 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1778 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1779 | return FAIL; |
| 1780 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1781 | isn->isn_arg.echo.echo_count = count; |
| 1782 | |
| 1783 | return OK; |
| 1784 | } |
| 1785 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1786 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1787 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1788 | */ |
| 1789 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1790 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1791 | { |
| 1792 | isn_T *isn; |
| 1793 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1794 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1795 | return FAIL; |
| 1796 | isn->isn_arg.number = count; |
| 1797 | |
| 1798 | return OK; |
| 1799 | } |
| 1800 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1801 | static int |
| 1802 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1803 | { |
| 1804 | isn_T *isn; |
| 1805 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1806 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1807 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1808 | return FAIL; |
| 1809 | isn->isn_arg.string = vim_strsave(line); |
| 1810 | return OK; |
| 1811 | } |
| 1812 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1813 | static int |
| 1814 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1815 | { |
| 1816 | isn_T *isn; |
| 1817 | |
| 1818 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1819 | return FAIL; |
| 1820 | isn->isn_arg.number = count; |
| 1821 | return OK; |
| 1822 | } |
| 1823 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1824 | /* |
| 1825 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1826 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1827 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1828 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1829 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1830 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1831 | lvar_T *lvar; |
| 1832 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1833 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1834 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1835 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1836 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1840 | return NULL; |
| 1841 | 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] | 1842 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1843 | // Every local variable uses the next entry on the stack. We could re-use |
| 1844 | // the last ones when leaving a scope, but then variables used in a closure |
| 1845 | // might get overwritten. To keep things simple do not re-use stack |
| 1846 | // entries. This is less efficient, but memory is cheap these days. |
| 1847 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1848 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1849 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1850 | lvar->lv_const = isConst; |
| 1851 | lvar->lv_type = type; |
| 1852 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1853 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1857 | * Remove local variables above "new_top". |
| 1858 | */ |
| 1859 | static void |
| 1860 | unwind_locals(cctx_T *cctx, int new_top) |
| 1861 | { |
| 1862 | if (cctx->ctx_locals.ga_len > new_top) |
| 1863 | { |
| 1864 | int idx; |
| 1865 | lvar_T *lvar; |
| 1866 | |
| 1867 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1868 | { |
| 1869 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1870 | vim_free(lvar->lv_name); |
| 1871 | } |
| 1872 | } |
| 1873 | cctx->ctx_locals.ga_len = new_top; |
| 1874 | } |
| 1875 | |
| 1876 | /* |
| 1877 | * Free all local variables. |
| 1878 | */ |
| 1879 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1880 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1881 | { |
| 1882 | unwind_locals(cctx, 0); |
| 1883 | ga_clear(&cctx->ctx_locals); |
| 1884 | } |
| 1885 | |
| 1886 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1887 | * Skip over a type definition and return a pointer to just after it. |
| 1888 | */ |
| 1889 | char_u * |
| 1890 | skip_type(char_u *start) |
| 1891 | { |
| 1892 | char_u *p = start; |
| 1893 | |
| 1894 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1895 | ++p; |
| 1896 | |
| 1897 | // Skip over "<type>"; this is permissive about white space. |
| 1898 | if (*skipwhite(p) == '<') |
| 1899 | { |
| 1900 | p = skipwhite(p); |
| 1901 | p = skip_type(skipwhite(p + 1)); |
| 1902 | p = skipwhite(p); |
| 1903 | if (*p == '>') |
| 1904 | ++p; |
| 1905 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1906 | else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1]))) |
| 1907 | && STRNCMP("func", start, 4) == 0) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1908 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1909 | if (*p == '(') |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1910 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1911 | // handle func(args): type |
| 1912 | ++p; |
| 1913 | while (*p != ')' && *p != NUL) |
| 1914 | { |
| 1915 | char_u *sp = p; |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1916 | |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1917 | p = skip_type(p); |
| 1918 | if (p == sp) |
| 1919 | return p; // syntax error |
| 1920 | if (*p == ',') |
| 1921 | p = skipwhite(p + 1); |
| 1922 | } |
| 1923 | if (*p == ')') |
| 1924 | { |
| 1925 | if (p[1] == ':') |
| 1926 | p = skip_type(skipwhite(p + 2)); |
| 1927 | else |
| 1928 | p = skipwhite(p + 1); |
| 1929 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1930 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1931 | else |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1932 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1933 | // handle func: return_type |
| 1934 | p = skip_type(skipwhite(p + 1)); |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1935 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1936 | } |
| 1937 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1938 | return p; |
| 1939 | } |
| 1940 | |
| 1941 | /* |
| 1942 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1943 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1944 | * Returns NULL in case of failure. |
| 1945 | */ |
| 1946 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1947 | 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] | 1948 | { |
| 1949 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1950 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1951 | |
| 1952 | if (**arg != '<') |
| 1953 | { |
| 1954 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1955 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1956 | else |
| 1957 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1958 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1959 | } |
| 1960 | *arg = skipwhite(*arg + 1); |
| 1961 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1962 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1963 | |
| 1964 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1965 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1966 | { |
| 1967 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1968 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1969 | } |
| 1970 | ++*arg; |
| 1971 | |
| 1972 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1973 | return get_list_type(member_type, type_gap); |
| 1974 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | /* |
| 1978 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 1979 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1980 | */ |
| 1981 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1982 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1983 | { |
| 1984 | char_u *p = *arg; |
| 1985 | size_t len; |
| 1986 | |
| 1987 | // skip over the first word |
| 1988 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1989 | ++p; |
| 1990 | len = p - *arg; |
| 1991 | |
| 1992 | switch (**arg) |
| 1993 | { |
| 1994 | case 'a': |
| 1995 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 1996 | { |
| 1997 | *arg += len; |
| 1998 | return &t_any; |
| 1999 | } |
| 2000 | break; |
| 2001 | case 'b': |
| 2002 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 2003 | { |
| 2004 | *arg += len; |
| 2005 | return &t_bool; |
| 2006 | } |
| 2007 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 2008 | { |
| 2009 | *arg += len; |
| 2010 | return &t_blob; |
| 2011 | } |
| 2012 | break; |
| 2013 | case 'c': |
| 2014 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 2015 | { |
| 2016 | *arg += len; |
| 2017 | return &t_channel; |
| 2018 | } |
| 2019 | break; |
| 2020 | case 'd': |
| 2021 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 2022 | { |
| 2023 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2024 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2025 | } |
| 2026 | break; |
| 2027 | case 'f': |
| 2028 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 2029 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2030 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2031 | *arg += len; |
| 2032 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2033 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2034 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2035 | return &t_any; |
| 2036 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2037 | } |
| 2038 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 2039 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2040 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2041 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2042 | int argcount = -1; |
| 2043 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2044 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2045 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 2046 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2047 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2048 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2049 | if (**arg == '(') |
| 2050 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2051 | // "func" may or may not return a value, "func()" does |
| 2052 | // not return a value. |
| 2053 | ret_type = &t_void; |
| 2054 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2055 | p = ++*arg; |
| 2056 | argcount = 0; |
| 2057 | while (*p != NUL && *p != ')') |
| 2058 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2059 | if (*p == '?') |
| 2060 | { |
| 2061 | if (first_optional == -1) |
| 2062 | first_optional = argcount; |
| 2063 | ++p; |
| 2064 | } |
| 2065 | else if (first_optional != -1) |
| 2066 | { |
| 2067 | emsg(_("E1007: mandatory argument after optional argument")); |
| 2068 | return &t_any; |
| 2069 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2070 | else if (STRNCMP(p, "...", 3) == 0) |
| 2071 | { |
| 2072 | flags |= TTFLAG_VARARGS; |
| 2073 | p += 3; |
| 2074 | } |
| 2075 | |
| 2076 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 2077 | |
| 2078 | // Nothing comes after "...{type}". |
| 2079 | if (flags & TTFLAG_VARARGS) |
| 2080 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2081 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2082 | if (*p != ',' && *skipwhite(p) == ',') |
| 2083 | { |
| 2084 | semsg(_(e_no_white_before), ","); |
| 2085 | return &t_any; |
| 2086 | } |
| 2087 | if (*p == ',') |
| 2088 | { |
| 2089 | ++p; |
| 2090 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2091 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2092 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2093 | return &t_any; |
| 2094 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2095 | } |
| 2096 | p = skipwhite(p); |
| 2097 | if (argcount == MAX_FUNC_ARGS) |
| 2098 | { |
| 2099 | emsg(_("E740: Too many argument types")); |
| 2100 | return &t_any; |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | p = skipwhite(p); |
| 2105 | if (*p != ')') |
| 2106 | { |
| 2107 | emsg(_(e_missing_close)); |
| 2108 | return &t_any; |
| 2109 | } |
| 2110 | *arg = p + 1; |
| 2111 | } |
| 2112 | if (**arg == ':') |
| 2113 | { |
| 2114 | // parse return type |
| 2115 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2116 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2117 | semsg(_(e_white_after), ":"); |
| 2118 | *arg = skipwhite(*arg); |
| 2119 | ret_type = parse_type(arg, type_gap); |
| 2120 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2121 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2122 | type = get_func_type(ret_type, argcount, type_gap); |
| 2123 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2124 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2125 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 2126 | type->tt_flags = flags; |
| 2127 | if (argcount > 0) |
| 2128 | { |
| 2129 | type->tt_argcount = argcount; |
| 2130 | type->tt_min_argcount = first_optional == -1 |
| 2131 | ? argcount : first_optional; |
| 2132 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2133 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2134 | return &t_any; |
| 2135 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2136 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2137 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2138 | } |
| 2139 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2140 | } |
| 2141 | break; |
| 2142 | case 'j': |
| 2143 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 2144 | { |
| 2145 | *arg += len; |
| 2146 | return &t_job; |
| 2147 | } |
| 2148 | break; |
| 2149 | case 'l': |
| 2150 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 2151 | { |
| 2152 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2153 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2154 | } |
| 2155 | break; |
| 2156 | case 'n': |
| 2157 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 2158 | { |
| 2159 | *arg += len; |
| 2160 | return &t_number; |
| 2161 | } |
| 2162 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2163 | case 's': |
| 2164 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 2165 | { |
| 2166 | *arg += len; |
| 2167 | return &t_string; |
| 2168 | } |
| 2169 | break; |
| 2170 | case 'v': |
| 2171 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 2172 | { |
| 2173 | *arg += len; |
| 2174 | return &t_void; |
| 2175 | } |
| 2176 | break; |
| 2177 | } |
| 2178 | |
| 2179 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 2180 | return &t_any; |
| 2181 | } |
| 2182 | |
| 2183 | /* |
| 2184 | * Check if "type1" and "type2" are exactly the same. |
| 2185 | */ |
| 2186 | static int |
| 2187 | equal_type(type_T *type1, type_T *type2) |
| 2188 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2189 | int i; |
| 2190 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2191 | if (type1->tt_type != type2->tt_type) |
| 2192 | return FALSE; |
| 2193 | switch (type1->tt_type) |
| 2194 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2195 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2196 | case VAR_ANY: |
| 2197 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2198 | case VAR_SPECIAL: |
| 2199 | case VAR_BOOL: |
| 2200 | case VAR_NUMBER: |
| 2201 | case VAR_FLOAT: |
| 2202 | case VAR_STRING: |
| 2203 | case VAR_BLOB: |
| 2204 | case VAR_JOB: |
| 2205 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2206 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2207 | case VAR_LIST: |
| 2208 | case VAR_DICT: |
| 2209 | return equal_type(type1->tt_member, type2->tt_member); |
| 2210 | case VAR_FUNC: |
| 2211 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2212 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2213 | || type1->tt_argcount != type2->tt_argcount) |
| 2214 | return FALSE; |
| 2215 | if (type1->tt_argcount < 0 |
| 2216 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2217 | return TRUE; |
| 2218 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2219 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2220 | return FALSE; |
| 2221 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2222 | } |
| 2223 | return TRUE; |
| 2224 | } |
| 2225 | |
| 2226 | /* |
| 2227 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2228 | * "type2" and "dest" may be the same. |
| 2229 | */ |
| 2230 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2231 | 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] | 2232 | { |
| 2233 | if (equal_type(type1, type2)) |
| 2234 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2235 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2236 | return; |
| 2237 | } |
| 2238 | |
| 2239 | if (type1->tt_type == type2->tt_type) |
| 2240 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2241 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2242 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2243 | type_T *common; |
| 2244 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2245 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2246 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2247 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2248 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2249 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2250 | return; |
| 2251 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2252 | if (type1->tt_type == VAR_FUNC) |
| 2253 | { |
| 2254 | type_T *common; |
| 2255 | |
| 2256 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2257 | if (type1->tt_argcount == type2->tt_argcount |
| 2258 | && type1->tt_argcount >= 0) |
| 2259 | { |
| 2260 | int argcount = type1->tt_argcount; |
| 2261 | int i; |
| 2262 | |
| 2263 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2264 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2265 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2266 | if (func_type_add_arg_types(*dest, argcount, |
| 2267 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2268 | for (i = 0; i < argcount; ++i) |
| 2269 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2270 | &(*dest)->tt_args[i], type_gap); |
| 2271 | } |
| 2272 | } |
| 2273 | else |
| 2274 | *dest = alloc_func_type(common, -1, type_gap); |
| 2275 | return; |
| 2276 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2277 | } |
| 2278 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2279 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | char * |
| 2283 | vartype_name(vartype_T type) |
| 2284 | { |
| 2285 | switch (type) |
| 2286 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2287 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2288 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2289 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2290 | case VAR_SPECIAL: return "special"; |
| 2291 | case VAR_BOOL: return "bool"; |
| 2292 | case VAR_NUMBER: return "number"; |
| 2293 | case VAR_FLOAT: return "float"; |
| 2294 | case VAR_STRING: return "string"; |
| 2295 | case VAR_BLOB: return "blob"; |
| 2296 | case VAR_JOB: return "job"; |
| 2297 | case VAR_CHANNEL: return "channel"; |
| 2298 | case VAR_LIST: return "list"; |
| 2299 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2300 | |
| 2301 | case VAR_FUNC: |
| 2302 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2303 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2304 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | /* |
| 2308 | * Return the name of a type. |
| 2309 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2310 | */ |
| 2311 | char * |
| 2312 | type_name(type_T *type, char **tofree) |
| 2313 | { |
| 2314 | char *name = vartype_name(type->tt_type); |
| 2315 | |
| 2316 | *tofree = NULL; |
| 2317 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2318 | { |
| 2319 | char *member_free; |
| 2320 | char *member_name = type_name(type->tt_member, &member_free); |
| 2321 | size_t len; |
| 2322 | |
| 2323 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2324 | *tofree = alloc(len); |
| 2325 | if (*tofree != NULL) |
| 2326 | { |
| 2327 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2328 | vim_free(member_free); |
| 2329 | return *tofree; |
| 2330 | } |
| 2331 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2332 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2333 | { |
| 2334 | garray_T ga; |
| 2335 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2336 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2337 | |
| 2338 | ga_init2(&ga, 1, 100); |
| 2339 | if (ga_grow(&ga, 20) == FAIL) |
| 2340 | return "[unknown]"; |
| 2341 | *tofree = ga.ga_data; |
| 2342 | STRCPY(ga.ga_data, "func("); |
| 2343 | ga.ga_len += 5; |
| 2344 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2345 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2346 | { |
| 2347 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2348 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2349 | int len; |
| 2350 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2351 | if (type->tt_args == NULL) |
| 2352 | arg_type = "[unknown]"; |
| 2353 | else |
| 2354 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2355 | if (i > 0) |
| 2356 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2357 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2358 | ga.ga_len += 2; |
| 2359 | } |
| 2360 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2361 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2362 | { |
| 2363 | vim_free(arg_free); |
| 2364 | return "[unknown]"; |
| 2365 | } |
| 2366 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2367 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2368 | { |
| 2369 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2370 | ga.ga_len += 3; |
| 2371 | } |
| 2372 | else if (i >= type->tt_min_argcount) |
| 2373 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2374 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2375 | ga.ga_len += len; |
| 2376 | vim_free(arg_free); |
| 2377 | } |
| 2378 | |
| 2379 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2380 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2381 | else |
| 2382 | { |
| 2383 | char *ret_free; |
| 2384 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2385 | int len; |
| 2386 | |
| 2387 | len = (int)STRLEN(ret_name) + 4; |
| 2388 | if (ga_grow(&ga, len) == FAIL) |
| 2389 | { |
| 2390 | vim_free(ret_free); |
| 2391 | return "[unknown]"; |
| 2392 | } |
| 2393 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2394 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2395 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2396 | vim_free(ret_free); |
| 2397 | } |
| 2398 | return ga.ga_data; |
| 2399 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2400 | |
| 2401 | return name; |
| 2402 | } |
| 2403 | |
| 2404 | /* |
| 2405 | * Find "name" in script-local items of script "sid". |
| 2406 | * Returns the index in "sn_var_vals" if found. |
| 2407 | * If found but not in "sn_var_vals" returns -1. |
| 2408 | * If not found returns -2. |
| 2409 | */ |
| 2410 | int |
| 2411 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2412 | { |
| 2413 | hashtab_T *ht; |
| 2414 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2415 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2416 | int idx; |
| 2417 | |
| 2418 | // First look the name up in the hashtable. |
| 2419 | if (sid <= 0 || sid > script_items.ga_len) |
| 2420 | return -1; |
| 2421 | ht = &SCRIPT_VARS(sid); |
| 2422 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2423 | if (di == NULL) |
| 2424 | return -2; |
| 2425 | |
| 2426 | // Now find the svar_T index in sn_var_vals. |
| 2427 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2428 | { |
| 2429 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2430 | |
| 2431 | if (sv->sv_tv == &di->di_tv) |
| 2432 | { |
| 2433 | if (check_writable && sv->sv_const) |
| 2434 | semsg(_(e_readonlyvar), name); |
| 2435 | return idx; |
| 2436 | } |
| 2437 | } |
| 2438 | return -1; |
| 2439 | } |
| 2440 | |
| 2441 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2442 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2443 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2444 | */ |
| 2445 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2446 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2447 | { |
Bram Moolenaar | 086eb18 | 2020-07-01 16:00:44 +0200 | [diff] [blame] | 2448 | scriptitem_T *si; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2449 | int idx; |
| 2450 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2451 | if (current_sctx.sc_sid <= 0) |
| 2452 | return NULL; |
| 2453 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2454 | if (cctx != NULL) |
| 2455 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2456 | { |
| 2457 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2458 | + idx; |
| 2459 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2460 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2461 | : STRLEN(import->imp_name) == len |
| 2462 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2463 | return import; |
| 2464 | } |
| 2465 | |
| 2466 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2467 | { |
| 2468 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2469 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2470 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2471 | : STRLEN(import->imp_name) == len |
| 2472 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2473 | return import; |
| 2474 | } |
| 2475 | return NULL; |
| 2476 | } |
| 2477 | |
| 2478 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2479 | * Free all imported variables. |
| 2480 | */ |
| 2481 | static void |
| 2482 | free_imported(cctx_T *cctx) |
| 2483 | { |
| 2484 | int idx; |
| 2485 | |
| 2486 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2487 | { |
| 2488 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2489 | |
| 2490 | vim_free(import->imp_name); |
| 2491 | } |
| 2492 | ga_clear(&cctx->ctx_imports); |
| 2493 | } |
| 2494 | |
| 2495 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2496 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2497 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 2498 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2499 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2500 | { |
| 2501 | return p[0] == '#' && p[1] != '{'; |
| 2502 | } |
| 2503 | |
| 2504 | /* |
| 2505 | * Return a pointer to the next line that isn't empty or only contains a |
| 2506 | * comment. Skips over white space. |
| 2507 | * Returns NULL if there is none. |
| 2508 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2509 | char_u * |
| 2510 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2511 | { |
| 2512 | int lnum = cctx->ctx_lnum; |
| 2513 | |
| 2514 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2515 | { |
| 2516 | 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] | 2517 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2518 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 2519 | if (line == NULL) |
| 2520 | break; |
| 2521 | p = skipwhite(line); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2522 | if (*p != NUL && !vim9_comment_start(p)) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2523 | return p; |
| 2524 | } |
| 2525 | return NULL; |
| 2526 | } |
| 2527 | |
| 2528 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2529 | * Called when checking for a following operator at "arg". When the rest of |
| 2530 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2531 | * line return a pointer to it and set "nextp". |
| 2532 | * Otherwise skip over white space. |
| 2533 | */ |
| 2534 | static char_u * |
| 2535 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2536 | { |
| 2537 | char_u *p = skipwhite(arg); |
| 2538 | |
| 2539 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2540 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2541 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2542 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2543 | if (*nextp != NULL) |
| 2544 | return *nextp; |
| 2545 | } |
| 2546 | return p; |
| 2547 | } |
| 2548 | |
| 2549 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2550 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2551 | * 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] | 2552 | * Returns NULL when at the end. |
| 2553 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2554 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2555 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2556 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2557 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2558 | |
| 2559 | do |
| 2560 | { |
| 2561 | ++cctx->ctx_lnum; |
| 2562 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2563 | { |
| 2564 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2565 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2566 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2567 | 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] | 2568 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2569 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2570 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2571 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2572 | return line; |
| 2573 | } |
| 2574 | |
| 2575 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2576 | * 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] | 2577 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2578 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2579 | */ |
| 2580 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2581 | 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] | 2582 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2583 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2584 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2585 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2586 | |
| 2587 | if (next == NULL) |
| 2588 | return FAIL; |
| 2589 | *arg = skipwhite(next); |
| 2590 | } |
| 2591 | return OK; |
| 2592 | } |
| 2593 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2594 | /* |
| 2595 | * Idem, and give an error when failed. |
| 2596 | */ |
| 2597 | static int |
| 2598 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2599 | { |
| 2600 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2601 | { |
| 2602 | emsg(_("E1097: line incomplete")); |
| 2603 | return FAIL; |
| 2604 | } |
| 2605 | return OK; |
| 2606 | } |
| 2607 | |
| 2608 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2609 | // Structure passed between the compile_expr* functions to keep track of |
| 2610 | // constants that have been parsed but for which no code was produced yet. If |
| 2611 | // possible expressions on these constants are applied at compile time. If |
| 2612 | // that is not possible, the code to push the constants needs to be generated |
| 2613 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2614 | // Using 50 should be more than enough of 5 levels of (). |
| 2615 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2616 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2617 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2618 | int pp_used; // active entries in pp_tv[] |
| 2619 | } ppconst_T; |
| 2620 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2621 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2622 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2623 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2624 | /* |
| 2625 | * Generate a PUSH instruction for "tv". |
| 2626 | * "tv" will be consumed or cleared. |
| 2627 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2628 | */ |
| 2629 | static int |
| 2630 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2631 | { |
| 2632 | if (tv != NULL) |
| 2633 | { |
| 2634 | switch (tv->v_type) |
| 2635 | { |
| 2636 | case VAR_UNKNOWN: |
| 2637 | break; |
| 2638 | case VAR_BOOL: |
| 2639 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2640 | break; |
| 2641 | case VAR_SPECIAL: |
| 2642 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2643 | break; |
| 2644 | case VAR_NUMBER: |
| 2645 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2646 | break; |
| 2647 | #ifdef FEAT_FLOAT |
| 2648 | case VAR_FLOAT: |
| 2649 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2650 | break; |
| 2651 | #endif |
| 2652 | case VAR_BLOB: |
| 2653 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2654 | tv->vval.v_blob = NULL; |
| 2655 | break; |
| 2656 | case VAR_STRING: |
| 2657 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2658 | tv->vval.v_string = NULL; |
| 2659 | break; |
| 2660 | default: |
| 2661 | iemsg("constant type not supported"); |
| 2662 | clear_tv(tv); |
| 2663 | return FAIL; |
| 2664 | } |
| 2665 | tv->v_type = VAR_UNKNOWN; |
| 2666 | } |
| 2667 | return OK; |
| 2668 | } |
| 2669 | |
| 2670 | /* |
| 2671 | * Generate code for any ppconst entries. |
| 2672 | */ |
| 2673 | static int |
| 2674 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2675 | { |
| 2676 | int i; |
| 2677 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2678 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2679 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2680 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2681 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2682 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2683 | ret = FAIL; |
| 2684 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2685 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2686 | return ret; |
| 2687 | } |
| 2688 | |
| 2689 | /* |
| 2690 | * Clear ppconst constants. Used when failing. |
| 2691 | */ |
| 2692 | static void |
| 2693 | clear_ppconst(ppconst_T *ppconst) |
| 2694 | { |
| 2695 | int i; |
| 2696 | |
| 2697 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2698 | clear_tv(&ppconst->pp_tv[i]); |
| 2699 | ppconst->pp_used = 0; |
| 2700 | } |
| 2701 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2702 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2703 | * Generate an instruction to load script-local variable "name", without the |
| 2704 | * leading "s:". |
| 2705 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2706 | */ |
| 2707 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2708 | compile_load_scriptvar( |
| 2709 | cctx_T *cctx, |
| 2710 | char_u *name, // variable NUL terminated |
| 2711 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2712 | char_u **end, // end of variable |
| 2713 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2714 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2715 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2716 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2717 | imported_T *import; |
| 2718 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2719 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2720 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2721 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2722 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2723 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2724 | } |
| 2725 | if (idx >= 0) |
| 2726 | { |
| 2727 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2728 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2729 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2730 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2731 | return OK; |
| 2732 | } |
| 2733 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2734 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2735 | if (import != NULL) |
| 2736 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2737 | if (import->imp_all) |
| 2738 | { |
| 2739 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2740 | char_u *exp_name; |
| 2741 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2742 | ufunc_T *ufunc; |
| 2743 | type_T *type; |
| 2744 | |
| 2745 | // Used "import * as Name", need to lookup the member. |
| 2746 | if (*p != '.') |
| 2747 | { |
| 2748 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2749 | return FAIL; |
| 2750 | } |
| 2751 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2752 | if (VIM_ISWHITE(*p)) |
| 2753 | { |
| 2754 | emsg(_("E1074: no white space allowed after dot")); |
| 2755 | return FAIL; |
| 2756 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2757 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2758 | // isolate one name |
| 2759 | exp_name = p; |
| 2760 | while (eval_isnamec(*p)) |
| 2761 | ++p; |
| 2762 | cc = *p; |
| 2763 | *p = NUL; |
| 2764 | |
| 2765 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2766 | *p = cc; |
| 2767 | p = skipwhite(p); |
| 2768 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2769 | // TODO: what if it is a function? |
| 2770 | if (idx < 0) |
| 2771 | return FAIL; |
| 2772 | *end = p; |
| 2773 | |
| 2774 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2775 | import->imp_sid, |
| 2776 | idx, |
| 2777 | type); |
| 2778 | } |
| 2779 | else |
| 2780 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2781 | // TODO: check this is a variable, not a function? |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2782 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2783 | import->imp_sid, |
| 2784 | import->imp_var_vals_idx, |
| 2785 | import->imp_type); |
| 2786 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2787 | return OK; |
| 2788 | } |
| 2789 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2790 | if (error) |
| 2791 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2792 | return FAIL; |
| 2793 | } |
| 2794 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2795 | static int |
| 2796 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2797 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2798 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2799 | |
| 2800 | if (ufunc == NULL) |
| 2801 | return FAIL; |
| 2802 | |
Bram Moolenaar | fe465a0 | 2020-07-07 22:50:12 +0200 | [diff] [blame] | 2803 | return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name), |
| 2804 | ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2805 | } |
| 2806 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2807 | /* |
| 2808 | * Compile a variable name into a load instruction. |
| 2809 | * "end" points to just after the name. |
| 2810 | * When "error" is FALSE do not give an error when not found. |
| 2811 | */ |
| 2812 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2813 | 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] | 2814 | { |
| 2815 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2816 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2817 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2818 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2819 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2820 | |
| 2821 | if (*(*arg + 1) == ':') |
| 2822 | { |
| 2823 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2824 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2825 | { |
| 2826 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2827 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2828 | switch (**arg) |
| 2829 | { |
| 2830 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2831 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2832 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2833 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2834 | default: |
| 2835 | semsg(_(e_namespace), *arg); |
| 2836 | goto theend; |
| 2837 | } |
| 2838 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2839 | goto theend; |
| 2840 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2841 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2842 | else |
| 2843 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2844 | isntype_T isn_type = ISN_DROP; |
| 2845 | |
| 2846 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2847 | if (name == NULL) |
| 2848 | return FAIL; |
| 2849 | |
| 2850 | switch (**arg) |
| 2851 | { |
| 2852 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2853 | break; |
| 2854 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2855 | NULL, NULL, error); |
| 2856 | break; |
| 2857 | case 'g': isn_type = ISN_LOADG; break; |
| 2858 | case 'w': isn_type = ISN_LOADW; break; |
| 2859 | case 't': isn_type = ISN_LOADT; break; |
| 2860 | case 'b': isn_type = ISN_LOADB; break; |
| 2861 | default: semsg(_(e_namespace), *arg); |
| 2862 | goto theend; |
| 2863 | } |
| 2864 | if (isn_type != ISN_DROP) |
| 2865 | { |
| 2866 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2867 | // variables can be defined later, thus we don't check if it |
| 2868 | // exists, give error at runtime. |
| 2869 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2870 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2871 | } |
| 2872 | } |
| 2873 | else |
| 2874 | { |
| 2875 | size_t len = end - *arg; |
| 2876 | int idx; |
| 2877 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2878 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2879 | |
| 2880 | name = vim_strnsave(*arg, end - *arg); |
| 2881 | if (name == NULL) |
| 2882 | return FAIL; |
| 2883 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2884 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2885 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2886 | if (!gen_load_outer) |
| 2887 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2888 | } |
| 2889 | else |
| 2890 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2891 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2892 | |
| 2893 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2894 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2895 | type = lvar->lv_type; |
| 2896 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2897 | if (lvar->lv_from_outer) |
| 2898 | gen_load_outer = TRUE; |
| 2899 | else |
| 2900 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2901 | } |
| 2902 | else |
| 2903 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2904 | // "var" can be script-local even without using "s:" if it |
| 2905 | // already exists. |
| 2906 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2907 | == SCRIPT_VERSION_VIM9 |
| 2908 | || lookup_script(*arg, len) == OK) |
| 2909 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2910 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2911 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2912 | // When the name starts with an uppercase letter or "x:" it |
| 2913 | // can be a user defined function. |
| 2914 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2915 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2916 | } |
| 2917 | } |
| 2918 | if (gen_load) |
| 2919 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2920 | if (gen_load_outer) |
| 2921 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | *arg = end; |
| 2925 | |
| 2926 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2927 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2928 | semsg(_(e_var_notfound), name); |
| 2929 | vim_free(name); |
| 2930 | return res; |
| 2931 | } |
| 2932 | |
| 2933 | /* |
| 2934 | * Compile the argument expressions. |
| 2935 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2936 | */ |
| 2937 | static int |
| 2938 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2939 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2940 | char_u *p = *arg; |
| 2941 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2942 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2943 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2944 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2945 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2946 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2947 | if (*p == ')') |
| 2948 | { |
| 2949 | *arg = p + 1; |
| 2950 | return OK; |
| 2951 | } |
| 2952 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2953 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2954 | return FAIL; |
| 2955 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2956 | |
| 2957 | if (*p != ',' && *skipwhite(p) == ',') |
| 2958 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2959 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2960 | p = skipwhite(p); |
| 2961 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2962 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2963 | { |
| 2964 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2965 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2966 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2967 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2968 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2969 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2970 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2971 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2972 | emsg(_(e_missing_close)); |
| 2973 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | /* |
| 2977 | * Compile a function call: name(arg1, arg2) |
| 2978 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2979 | * "argcount_init" is 1 for "value->method()" |
| 2980 | * Instructions: |
| 2981 | * EVAL arg1 |
| 2982 | * EVAL arg2 |
| 2983 | * BCALL / DCALL / UCALL |
| 2984 | */ |
| 2985 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2986 | compile_call( |
| 2987 | char_u **arg, |
| 2988 | size_t varlen, |
| 2989 | cctx_T *cctx, |
| 2990 | ppconst_T *ppconst, |
| 2991 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2992 | { |
| 2993 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2994 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2995 | int argcount = argcount_init; |
| 2996 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2997 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2998 | char_u *tofree = NULL; |
| 2999 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3000 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3001 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3002 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3003 | // we can evaluate "has('name')" at compile time |
| 3004 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 3005 | { |
| 3006 | char_u *s = skipwhite(*arg + varlen + 1); |
| 3007 | typval_T argvars[2]; |
| 3008 | |
| 3009 | argvars[0].v_type = VAR_UNKNOWN; |
| 3010 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3011 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3012 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3013 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3014 | s = skipwhite(s); |
| 3015 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 3016 | { |
| 3017 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 3018 | |
| 3019 | *arg = s + 1; |
| 3020 | argvars[1].v_type = VAR_UNKNOWN; |
| 3021 | tv->v_type = VAR_NUMBER; |
| 3022 | tv->vval.v_number = 0; |
| 3023 | f_has(argvars, tv); |
| 3024 | clear_tv(&argvars[0]); |
| 3025 | ++ppconst->pp_used; |
| 3026 | return OK; |
| 3027 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 3028 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3032 | return FAIL; |
| 3033 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3034 | if (varlen >= sizeof(namebuf)) |
| 3035 | { |
| 3036 | semsg(_("E1011: name too long: %s"), name); |
| 3037 | return FAIL; |
| 3038 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3039 | vim_strncpy(namebuf, *arg, varlen); |
| 3040 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3041 | |
| 3042 | *arg = skipwhite(*arg + varlen + 1); |
| 3043 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3044 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3045 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3046 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3047 | { |
| 3048 | int idx; |
| 3049 | |
| 3050 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3051 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3052 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 3053 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3054 | else |
| 3055 | semsg(_(e_unknownfunc), namebuf); |
| 3056 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3057 | } |
| 3058 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3059 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 3060 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3061 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3062 | { |
| 3063 | res = generate_CALL(cctx, ufunc, argcount); |
| 3064 | goto theend; |
| 3065 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3066 | |
| 3067 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3068 | // 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] | 3069 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3070 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 3071 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3072 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3073 | garray_T *stack = &cctx->ctx_type_stack; |
| 3074 | type_T *type; |
| 3075 | |
| 3076 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3077 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3078 | goto theend; |
| 3079 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3080 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3081 | // 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] | 3082 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3083 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 3084 | res = generate_UCALL(cctx, name, argcount); |
| 3085 | else |
| 3086 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3087 | |
| 3088 | theend: |
| 3089 | vim_free(tofree); |
| 3090 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 3094 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 3095 | |
| 3096 | /* |
| 3097 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 3098 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3099 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3100 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 3101 | * valid name. |
| 3102 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3103 | static char_u * |
| 3104 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3105 | { |
| 3106 | char_u *p; |
| 3107 | |
| 3108 | // Quick check for valid starting character. |
| 3109 | if (!eval_isnamec1(*arg)) |
| 3110 | return arg; |
| 3111 | |
| 3112 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 3113 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 3114 | // and can be used in slice "[n:]". |
| 3115 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3116 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3117 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 3118 | break; |
| 3119 | return p; |
| 3120 | } |
| 3121 | |
| 3122 | /* |
| 3123 | * 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] | 3124 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3125 | */ |
| 3126 | char_u * |
| 3127 | to_name_const_end(char_u *arg) |
| 3128 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3129 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3130 | typval_T rettv; |
| 3131 | |
| 3132 | if (p == arg && *arg == '[') |
| 3133 | { |
| 3134 | |
| 3135 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3136 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3137 | p = arg; |
| 3138 | } |
| 3139 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 3140 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3141 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3142 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3143 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3144 | p = arg; |
| 3145 | } |
| 3146 | else if (p == arg && *arg == '{') |
| 3147 | { |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3148 | int ret = get_lambda_tv(&p, &rettv, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3149 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3150 | // Can be "{x -> ret}()". |
| 3151 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3152 | if (ret == NOTDONE) |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3153 | ret = eval_dict(&p, &rettv, NULL, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3154 | if (ret != OK) |
| 3155 | p = arg; |
| 3156 | } |
| 3157 | |
| 3158 | return p; |
| 3159 | } |
| 3160 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3161 | /* |
| 3162 | * parse a list: [expr, expr] |
| 3163 | * "*arg" points to the '['. |
| 3164 | */ |
| 3165 | static int |
| 3166 | compile_list(char_u **arg, cctx_T *cctx) |
| 3167 | { |
| 3168 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3169 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3170 | int count = 0; |
| 3171 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3172 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3173 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3174 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3175 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3176 | semsg(_(e_list_end), *arg); |
| 3177 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3178 | } |
| 3179 | if (*p == ']') |
| 3180 | { |
| 3181 | ++p; |
| 3182 | // Allow for following comment, after at least one space. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3183 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '#') |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3184 | p += STRLEN(p); |
| 3185 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3186 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3187 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3188 | break; |
| 3189 | ++count; |
| 3190 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3191 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3192 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3193 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 3194 | { |
| 3195 | semsg(_(e_white_after), ","); |
| 3196 | return FAIL; |
| 3197 | } |
| 3198 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3199 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3200 | p = skipwhite(p); |
| 3201 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3202 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3203 | |
| 3204 | generate_NEWLIST(cctx, count); |
| 3205 | return OK; |
| 3206 | } |
| 3207 | |
| 3208 | /* |
| 3209 | * parse a lambda: {arg, arg -> expr} |
| 3210 | * "*arg" points to the '{'. |
| 3211 | */ |
| 3212 | static int |
| 3213 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 3214 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3215 | typval_T rettv; |
| 3216 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3217 | evalarg_T evalarg; |
| 3218 | |
| 3219 | CLEAR_FIELD(evalarg); |
| 3220 | evalarg.eval_flags = EVAL_EVALUATE; |
| 3221 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3222 | |
| 3223 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3224 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3225 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3226 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3227 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3228 | ++ufunc->uf_refcount; |
| 3229 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3230 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3231 | |
| 3232 | // The function will have one line: "return {expr}". |
| 3233 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3234 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3235 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 3236 | clear_evalarg(&evalarg, NULL); |
| 3237 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 3238 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 3239 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3240 | |
| 3241 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3242 | return FAIL; |
| 3243 | } |
| 3244 | |
| 3245 | /* |
| 3246 | * Compile a lamda call: expr->{lambda}(args) |
| 3247 | * "arg" points to the "{". |
| 3248 | */ |
| 3249 | static int |
| 3250 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 3251 | { |
| 3252 | ufunc_T *ufunc; |
| 3253 | typval_T rettv; |
| 3254 | int argcount = 1; |
| 3255 | int ret = FAIL; |
| 3256 | |
| 3257 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3258 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3259 | return FAIL; |
| 3260 | |
| 3261 | if (**arg != '(') |
| 3262 | { |
| 3263 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 3264 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3265 | else |
| 3266 | semsg(_(e_missing_paren), "lambda"); |
| 3267 | clear_tv(&rettv); |
| 3268 | return FAIL; |
| 3269 | } |
| 3270 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3271 | ufunc = rettv.vval.v_partial->pt_func; |
| 3272 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3273 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3274 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3275 | |
| 3276 | // The function will have one line: "return {expr}". |
| 3277 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3278 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3279 | |
| 3280 | // compile the arguments |
| 3281 | *arg = skipwhite(*arg + 1); |
| 3282 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 3283 | // call the compiled function |
| 3284 | ret = generate_CALL(cctx, ufunc, argcount); |
| 3285 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3286 | if (ret == FAIL) |
| 3287 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3288 | return ret; |
| 3289 | } |
| 3290 | |
| 3291 | /* |
| 3292 | * parse a dict: {'key': val} or #{key: val} |
| 3293 | * "*arg" points to the '{'. |
| 3294 | */ |
| 3295 | static int |
| 3296 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 3297 | { |
| 3298 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3299 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3300 | int count = 0; |
| 3301 | dict_T *d = dict_alloc(); |
| 3302 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3303 | char_u *whitep = *arg; |
| 3304 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3305 | |
| 3306 | if (d == NULL) |
| 3307 | return FAIL; |
| 3308 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3309 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3310 | { |
| 3311 | char_u *key = NULL; |
| 3312 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3313 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3314 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3315 | *arg = NULL; |
| 3316 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | if (**arg == '}') |
| 3320 | break; |
| 3321 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3322 | if (literal) |
| 3323 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3324 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3325 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3326 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3327 | { |
| 3328 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 3329 | return FAIL; |
| 3330 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3331 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3332 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3333 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3334 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3335 | } |
| 3336 | else |
| 3337 | { |
| 3338 | isn_T *isn; |
| 3339 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3340 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3341 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3342 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3343 | if (isn->isn_type == ISN_PUSHS) |
| 3344 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3345 | else |
| 3346 | { |
| 3347 | type_T *keytype = ((type_T **)stack->ga_data) |
| 3348 | [stack->ga_len - 1]; |
| 3349 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 3350 | return FAIL; |
| 3351 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | // Check for duplicate keys, if using string keys. |
| 3355 | if (key != NULL) |
| 3356 | { |
| 3357 | item = dict_find(d, key, -1); |
| 3358 | if (item != NULL) |
| 3359 | { |
| 3360 | semsg(_(e_duplicate_key), key); |
| 3361 | goto failret; |
| 3362 | } |
| 3363 | item = dictitem_alloc(key); |
| 3364 | if (item != NULL) |
| 3365 | { |
| 3366 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3367 | item->di_tv.v_lock = 0; |
| 3368 | if (dict_add(d, item) == FAIL) |
| 3369 | dictitem_free(item); |
| 3370 | } |
| 3371 | } |
| 3372 | |
| 3373 | *arg = skipwhite(*arg); |
| 3374 | if (**arg != ':') |
| 3375 | { |
| 3376 | semsg(_(e_missing_dict_colon), *arg); |
| 3377 | return FAIL; |
| 3378 | } |
| 3379 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3380 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3381 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3382 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3383 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3384 | *arg = NULL; |
| 3385 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3386 | } |
| 3387 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3388 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3389 | return FAIL; |
| 3390 | ++count; |
| 3391 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3392 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3393 | *arg = skipwhite(*arg); |
| 3394 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3395 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3396 | *arg = NULL; |
| 3397 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3398 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3399 | if (**arg == '}') |
| 3400 | break; |
| 3401 | if (**arg != ',') |
| 3402 | { |
| 3403 | semsg(_(e_missing_dict_comma), *arg); |
| 3404 | goto failret; |
| 3405 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3406 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3407 | *arg = skipwhite(*arg + 1); |
| 3408 | } |
| 3409 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3410 | *arg = *arg + 1; |
| 3411 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3412 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3413 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3414 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3415 | *arg += STRLEN(*arg); |
| 3416 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3417 | dict_unref(d); |
| 3418 | return generate_NEWDICT(cctx, count); |
| 3419 | |
| 3420 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3421 | if (*arg == NULL) |
| 3422 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3423 | dict_unref(d); |
| 3424 | return FAIL; |
| 3425 | } |
| 3426 | |
| 3427 | /* |
| 3428 | * Compile "&option". |
| 3429 | */ |
| 3430 | static int |
| 3431 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3432 | { |
| 3433 | typval_T rettv; |
| 3434 | char_u *start = *arg; |
| 3435 | int ret; |
| 3436 | |
| 3437 | // parse the option and get the current value to get the type. |
| 3438 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3439 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3440 | if (ret == OK) |
| 3441 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3442 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3443 | char_u *name = vim_strnsave(start, *arg - start); |
| 3444 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3445 | |
| 3446 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3447 | vim_free(name); |
| 3448 | } |
| 3449 | clear_tv(&rettv); |
| 3450 | |
| 3451 | return ret; |
| 3452 | } |
| 3453 | |
| 3454 | /* |
| 3455 | * Compile "$VAR". |
| 3456 | */ |
| 3457 | static int |
| 3458 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3459 | { |
| 3460 | char_u *start = *arg; |
| 3461 | int len; |
| 3462 | int ret; |
| 3463 | char_u *name; |
| 3464 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3465 | ++*arg; |
| 3466 | len = get_env_len(arg); |
| 3467 | if (len == 0) |
| 3468 | { |
| 3469 | semsg(_(e_syntax_at), start - 1); |
| 3470 | return FAIL; |
| 3471 | } |
| 3472 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3473 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3474 | name = vim_strnsave(start, len + 1); |
| 3475 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3476 | vim_free(name); |
| 3477 | return ret; |
| 3478 | } |
| 3479 | |
| 3480 | /* |
| 3481 | * Compile "@r". |
| 3482 | */ |
| 3483 | static int |
| 3484 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3485 | { |
| 3486 | int ret; |
| 3487 | |
| 3488 | ++*arg; |
| 3489 | if (**arg == NUL) |
| 3490 | { |
| 3491 | semsg(_(e_syntax_at), *arg - 1); |
| 3492 | return FAIL; |
| 3493 | } |
| 3494 | if (!valid_yank_reg(**arg, TRUE)) |
| 3495 | { |
| 3496 | emsg_invreg(**arg); |
| 3497 | return FAIL; |
| 3498 | } |
| 3499 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3500 | ++*arg; |
| 3501 | return ret; |
| 3502 | } |
| 3503 | |
| 3504 | /* |
| 3505 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3506 | */ |
| 3507 | static int |
| 3508 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3509 | { |
| 3510 | char_u *p = end; |
| 3511 | |
| 3512 | // this works from end to start |
| 3513 | while (p > start) |
| 3514 | { |
| 3515 | --p; |
| 3516 | if (*p == '-' || *p == '+') |
| 3517 | { |
| 3518 | // only '-' has an effect, for '+' we only check the type |
| 3519 | #ifdef FEAT_FLOAT |
| 3520 | if (rettv->v_type == VAR_FLOAT) |
| 3521 | { |
| 3522 | if (*p == '-') |
| 3523 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3524 | } |
| 3525 | else |
| 3526 | #endif |
| 3527 | { |
| 3528 | varnumber_T val; |
| 3529 | int error = FALSE; |
| 3530 | |
| 3531 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3532 | // here |
| 3533 | if (check_not_string(rettv) == FAIL) |
| 3534 | return FAIL; |
| 3535 | val = tv_get_number_chk(rettv, &error); |
| 3536 | clear_tv(rettv); |
| 3537 | if (error) |
| 3538 | return FAIL; |
| 3539 | if (*p == '-') |
| 3540 | val = -val; |
| 3541 | rettv->v_type = VAR_NUMBER; |
| 3542 | rettv->vval.v_number = val; |
| 3543 | } |
| 3544 | } |
| 3545 | else |
| 3546 | { |
| 3547 | int v = tv2bool(rettv); |
| 3548 | |
| 3549 | // '!' is permissive in the type. |
| 3550 | clear_tv(rettv); |
| 3551 | rettv->v_type = VAR_BOOL; |
| 3552 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3553 | } |
| 3554 | } |
| 3555 | return OK; |
| 3556 | } |
| 3557 | |
| 3558 | /* |
| 3559 | * Recognize v: variables that are constants and set "rettv". |
| 3560 | */ |
| 3561 | static void |
| 3562 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3563 | { |
| 3564 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3565 | { |
| 3566 | rettv->v_type = VAR_BOOL; |
| 3567 | rettv->vval.v_number = VVAL_TRUE; |
| 3568 | *arg += 6; |
| 3569 | } |
| 3570 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3571 | { |
| 3572 | rettv->v_type = VAR_BOOL; |
| 3573 | rettv->vval.v_number = VVAL_FALSE; |
| 3574 | *arg += 7; |
| 3575 | } |
| 3576 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3577 | { |
| 3578 | rettv->v_type = VAR_SPECIAL; |
| 3579 | rettv->vval.v_number = VVAL_NULL; |
| 3580 | *arg += 6; |
| 3581 | } |
| 3582 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3583 | { |
| 3584 | rettv->v_type = VAR_SPECIAL; |
| 3585 | rettv->vval.v_number = VVAL_NONE; |
| 3586 | *arg += 6; |
| 3587 | } |
| 3588 | } |
| 3589 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3590 | static exptype_T |
| 3591 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3592 | { |
| 3593 | exptype_T type = EXPR_UNKNOWN; |
| 3594 | int i; |
| 3595 | |
| 3596 | switch (p[0]) |
| 3597 | { |
| 3598 | case '=': if (p[1] == '=') |
| 3599 | type = EXPR_EQUAL; |
| 3600 | else if (p[1] == '~') |
| 3601 | type = EXPR_MATCH; |
| 3602 | break; |
| 3603 | case '!': if (p[1] == '=') |
| 3604 | type = EXPR_NEQUAL; |
| 3605 | else if (p[1] == '~') |
| 3606 | type = EXPR_NOMATCH; |
| 3607 | break; |
| 3608 | case '>': if (p[1] != '=') |
| 3609 | { |
| 3610 | type = EXPR_GREATER; |
| 3611 | *len = 1; |
| 3612 | } |
| 3613 | else |
| 3614 | type = EXPR_GEQUAL; |
| 3615 | break; |
| 3616 | case '<': if (p[1] != '=') |
| 3617 | { |
| 3618 | type = EXPR_SMALLER; |
| 3619 | *len = 1; |
| 3620 | } |
| 3621 | else |
| 3622 | type = EXPR_SEQUAL; |
| 3623 | break; |
| 3624 | case 'i': if (p[1] == 's') |
| 3625 | { |
| 3626 | // "is" and "isnot"; but not a prefix of a name |
| 3627 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3628 | *len = 5; |
| 3629 | i = p[*len]; |
| 3630 | if (!isalnum(i) && i != '_') |
| 3631 | { |
| 3632 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3633 | *type_is = TRUE; |
| 3634 | } |
| 3635 | } |
| 3636 | break; |
| 3637 | } |
| 3638 | return type; |
| 3639 | } |
| 3640 | |
| 3641 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3642 | * Compile code to apply '-', '+' and '!'. |
| 3643 | */ |
| 3644 | static int |
| 3645 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3646 | { |
| 3647 | char_u *p = end; |
| 3648 | |
| 3649 | // this works from end to start |
| 3650 | while (p > start) |
| 3651 | { |
| 3652 | --p; |
| 3653 | if (*p == '-' || *p == '+') |
| 3654 | { |
| 3655 | int negate = *p == '-'; |
| 3656 | isn_T *isn; |
| 3657 | |
| 3658 | // TODO: check type |
| 3659 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3660 | { |
| 3661 | --p; |
| 3662 | if (*p == '-') |
| 3663 | negate = !negate; |
| 3664 | } |
| 3665 | // only '-' has an effect, for '+' we only check the type |
| 3666 | if (negate) |
| 3667 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3668 | else |
| 3669 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3670 | if (isn == NULL) |
| 3671 | return FAIL; |
| 3672 | } |
| 3673 | else |
| 3674 | { |
| 3675 | int invert = TRUE; |
| 3676 | |
| 3677 | while (p > start && p[-1] == '!') |
| 3678 | { |
| 3679 | --p; |
| 3680 | invert = !invert; |
| 3681 | } |
| 3682 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3683 | return FAIL; |
| 3684 | } |
| 3685 | } |
| 3686 | return OK; |
| 3687 | } |
| 3688 | |
| 3689 | /* |
| 3690 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3691 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3692 | */ |
| 3693 | static int |
| 3694 | compile_subscript( |
| 3695 | char_u **arg, |
| 3696 | cctx_T *cctx, |
| 3697 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3698 | char_u *end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3699 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3700 | { |
| 3701 | for (;;) |
| 3702 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3703 | char_u *p = skipwhite(*arg); |
| 3704 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3705 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3706 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3707 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3708 | |
| 3709 | // If a following line starts with "->{" or "->X" advance to that |
| 3710 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3711 | // Also if a following line starts with ".x". |
| 3712 | if (next != NULL && |
| 3713 | ((next[0] == '-' && next[1] == '>' |
| 3714 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
| 3715 | || (next[0] == '.' && ASCII_ISALPHA(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3716 | { |
| 3717 | next = next_line_from_context(cctx, TRUE); |
| 3718 | if (next == NULL) |
| 3719 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3720 | *arg = next; |
| 3721 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3722 | } |
| 3723 | } |
| 3724 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3725 | if (*p == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3726 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3727 | garray_T *stack = &cctx->ctx_type_stack; |
| 3728 | type_T *type; |
| 3729 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3730 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3731 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3732 | return FAIL; |
| 3733 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3734 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3735 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3736 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3737 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3738 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3739 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3740 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3741 | return FAIL; |
| 3742 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3743 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3744 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3745 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3746 | return FAIL; |
| 3747 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3748 | // something->method() |
| 3749 | // Apply the '!', '-' and '+' first: |
| 3750 | // -1.0->func() works like (-1.0)->func() |
| 3751 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3752 | return FAIL; |
| 3753 | *start_leader = end_leader; // don't apply again later |
| 3754 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3755 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3756 | *arg = skipwhite(p); |
| 3757 | if (may_get_next_line(p, arg, cctx) == FAIL) |
| 3758 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3759 | if (**arg == '{') |
| 3760 | { |
| 3761 | // lambda call: list->{lambda} |
| 3762 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3763 | return FAIL; |
| 3764 | } |
| 3765 | else |
| 3766 | { |
| 3767 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3768 | p = *arg; |
| 3769 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3770 | p += 2; |
| 3771 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3772 | ; |
| 3773 | if (*p != '(') |
| 3774 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3775 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3776 | return FAIL; |
| 3777 | } |
| 3778 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3779 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3780 | return FAIL; |
| 3781 | } |
| 3782 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3783 | else if (*p == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3784 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3785 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3786 | type_T **typep; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3787 | vartype_T vtype; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3788 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3789 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3790 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3791 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3792 | // TODO: blob index |
| 3793 | // TODO: more arguments |
| 3794 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3795 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3796 | return FAIL; |
| 3797 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3798 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3799 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3800 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3801 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3802 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3803 | return FAIL; |
| 3804 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3805 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3806 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3807 | if (**arg != ']') |
| 3808 | { |
| 3809 | emsg(_(e_missbrac)); |
| 3810 | return FAIL; |
| 3811 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3812 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3813 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3814 | // We can index a list and a dict. If we don't know the type |
| 3815 | // we can use the index value type. |
| 3816 | // TODO: If we don't know use an instruction to figure it out at |
| 3817 | // runtime. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3818 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3819 | vtype = (*typep)->tt_type; |
| 3820 | if (*typep == &t_any) |
| 3821 | { |
| 3822 | type_T *valtype = ((type_T **)stack->ga_data) |
| 3823 | [stack->ga_len - 1]; |
| 3824 | if (valtype == &t_string) |
| 3825 | vtype = VAR_DICT; |
| 3826 | } |
| 3827 | if (vtype == VAR_DICT) |
| 3828 | { |
| 3829 | if ((*typep)->tt_type == VAR_DICT) |
| 3830 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3831 | else |
| 3832 | { |
| 3833 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3834 | return FAIL; |
| 3835 | *typep = &t_any; |
| 3836 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3837 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3838 | return FAIL; |
| 3839 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3840 | return FAIL; |
| 3841 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3842 | else if (vtype == VAR_STRING) |
| 3843 | { |
| 3844 | *typep = &t_number; |
| 3845 | if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL) |
| 3846 | return FAIL; |
| 3847 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3848 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3849 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3850 | if ((*typep)->tt_type == VAR_LIST) |
| 3851 | *typep = (*typep)->tt_member; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3852 | if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3853 | return FAIL; |
| 3854 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3855 | else |
| 3856 | { |
| 3857 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3858 | return FAIL; |
| 3859 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3860 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3861 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3862 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3863 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3864 | return FAIL; |
| 3865 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3866 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3867 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3868 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3869 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3870 | p = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3871 | if (eval_isnamec1(*p)) |
| 3872 | while (eval_isnamec(*p)) |
| 3873 | MB_PTR_ADV(p); |
| 3874 | if (p == *arg) |
| 3875 | { |
| 3876 | semsg(_(e_syntax_at), *arg); |
| 3877 | return FAIL; |
| 3878 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3879 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3880 | return FAIL; |
| 3881 | *arg = p; |
| 3882 | } |
| 3883 | else |
| 3884 | break; |
| 3885 | } |
| 3886 | |
| 3887 | // TODO - see handle_subscript(): |
| 3888 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3889 | // Don't do this when "Func" is already a partial that was bound |
| 3890 | // explicitly (pt_auto is FALSE). |
| 3891 | |
| 3892 | return OK; |
| 3893 | } |
| 3894 | |
| 3895 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3896 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3897 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3898 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3899 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3900 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3901 | * |
| 3902 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3903 | */ |
| 3904 | |
| 3905 | /* |
| 3906 | * number number constant |
| 3907 | * 0zFFFFFFFF Blob constant |
| 3908 | * "string" string constant |
| 3909 | * 'string' literal string constant |
| 3910 | * &option-name option value |
| 3911 | * @r register contents |
| 3912 | * identifier variable value |
| 3913 | * function() function call |
| 3914 | * $VAR environment variable |
| 3915 | * (expression) nested expression |
| 3916 | * [expr, expr] List |
| 3917 | * {key: val, key: val} Dictionary |
| 3918 | * #{key: val, key: val} Dictionary with literal keys |
| 3919 | * |
| 3920 | * Also handle: |
| 3921 | * ! in front logical NOT |
| 3922 | * - in front unary minus |
| 3923 | * + in front unary plus (ignored) |
| 3924 | * trailing (arg) funcref/partial call |
| 3925 | * trailing [] subscript in String or List |
| 3926 | * trailing .name entry in Dictionary |
| 3927 | * trailing ->name() method call |
| 3928 | */ |
| 3929 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3930 | compile_expr7( |
| 3931 | char_u **arg, |
| 3932 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3933 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3934 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3935 | char_u *start_leader, *end_leader; |
| 3936 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3937 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3938 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3939 | |
| 3940 | /* |
| 3941 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3942 | */ |
| 3943 | start_leader = *arg; |
| 3944 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3945 | *arg = skipwhite(*arg + 1); |
| 3946 | end_leader = *arg; |
| 3947 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3948 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3949 | switch (**arg) |
| 3950 | { |
| 3951 | /* |
| 3952 | * Number constant. |
| 3953 | */ |
| 3954 | case '0': // also for blob starting with 0z |
| 3955 | case '1': |
| 3956 | case '2': |
| 3957 | case '3': |
| 3958 | case '4': |
| 3959 | case '5': |
| 3960 | case '6': |
| 3961 | case '7': |
| 3962 | case '8': |
| 3963 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3964 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3965 | return FAIL; |
| 3966 | break; |
| 3967 | |
| 3968 | /* |
| 3969 | * String constant: "string". |
| 3970 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3971 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3972 | return FAIL; |
| 3973 | break; |
| 3974 | |
| 3975 | /* |
| 3976 | * Literal string constant: 'str''ing'. |
| 3977 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3978 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3979 | return FAIL; |
| 3980 | break; |
| 3981 | |
| 3982 | /* |
| 3983 | * Constant Vim variable. |
| 3984 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3985 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3986 | ret = NOTDONE; |
| 3987 | break; |
| 3988 | |
| 3989 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3990 | * "true" constant |
| 3991 | */ |
| 3992 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3993 | && !eval_isnamec((*arg)[4])) |
| 3994 | { |
| 3995 | *arg += 4; |
| 3996 | rettv->v_type = VAR_BOOL; |
| 3997 | rettv->vval.v_number = VVAL_TRUE; |
| 3998 | } |
| 3999 | else |
| 4000 | ret = NOTDONE; |
| 4001 | break; |
| 4002 | |
| 4003 | /* |
| 4004 | * "false" constant |
| 4005 | */ |
| 4006 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 4007 | && !eval_isnamec((*arg)[5])) |
| 4008 | { |
| 4009 | *arg += 5; |
| 4010 | rettv->v_type = VAR_BOOL; |
| 4011 | rettv->vval.v_number = VVAL_FALSE; |
| 4012 | } |
| 4013 | else |
| 4014 | ret = NOTDONE; |
| 4015 | break; |
| 4016 | |
| 4017 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4018 | * List: [expr, expr] |
| 4019 | */ |
| 4020 | case '[': ret = compile_list(arg, cctx); |
| 4021 | break; |
| 4022 | |
| 4023 | /* |
| 4024 | * Dictionary: #{key: val, key: val} |
| 4025 | */ |
| 4026 | case '#': if ((*arg)[1] == '{') |
| 4027 | { |
| 4028 | ++*arg; |
| 4029 | ret = compile_dict(arg, cctx, TRUE); |
| 4030 | } |
| 4031 | else |
| 4032 | ret = NOTDONE; |
| 4033 | break; |
| 4034 | |
| 4035 | /* |
| 4036 | * Lambda: {arg, arg -> expr} |
| 4037 | * Dictionary: {'key': val, 'key': val} |
| 4038 | */ |
| 4039 | case '{': { |
| 4040 | char_u *start = skipwhite(*arg + 1); |
| 4041 | |
| 4042 | // Find out what comes after the arguments. |
| 4043 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 4044 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4045 | if (ret != FAIL && *start == '>') |
| 4046 | ret = compile_lambda(arg, cctx); |
| 4047 | else |
| 4048 | ret = compile_dict(arg, cctx, FALSE); |
| 4049 | } |
| 4050 | break; |
| 4051 | |
| 4052 | /* |
| 4053 | * Option value: &name |
| 4054 | */ |
| 4055 | case '&': ret = compile_get_option(arg, cctx); |
| 4056 | break; |
| 4057 | |
| 4058 | /* |
| 4059 | * Environment variable: $VAR. |
| 4060 | */ |
| 4061 | case '$': ret = compile_get_env(arg, cctx); |
| 4062 | break; |
| 4063 | |
| 4064 | /* |
| 4065 | * Register contents: @r. |
| 4066 | */ |
| 4067 | case '@': ret = compile_get_register(arg, cctx); |
| 4068 | break; |
| 4069 | /* |
| 4070 | * nested expression: (expression). |
| 4071 | */ |
| 4072 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4073 | |
| 4074 | // recursive! |
| 4075 | if (ppconst->pp_used <= PPSIZE - 10) |
| 4076 | { |
| 4077 | ret = compile_expr1(arg, cctx, ppconst); |
| 4078 | } |
| 4079 | else |
| 4080 | { |
| 4081 | // Not enough space in ppconst, flush constants. |
| 4082 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4083 | return FAIL; |
| 4084 | ret = compile_expr0(arg, cctx); |
| 4085 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4086 | *arg = skipwhite(*arg); |
| 4087 | if (**arg == ')') |
| 4088 | ++*arg; |
| 4089 | else if (ret == OK) |
| 4090 | { |
| 4091 | emsg(_(e_missing_close)); |
| 4092 | ret = FAIL; |
| 4093 | } |
| 4094 | break; |
| 4095 | |
| 4096 | default: ret = NOTDONE; |
| 4097 | break; |
| 4098 | } |
| 4099 | if (ret == FAIL) |
| 4100 | return FAIL; |
| 4101 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4102 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4103 | { |
| 4104 | // apply the '!', '-' and '+' before the constant |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4105 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4106 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4107 | clear_tv(rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4108 | return FAIL; |
| 4109 | } |
| 4110 | start_leader = end_leader; // don't apply again below |
| 4111 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4112 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4113 | clear_tv(rettv); |
| 4114 | else |
| 4115 | // A constant expression can possibly be handled compile time, |
| 4116 | // return the value instead of generating code. |
| 4117 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4118 | } |
| 4119 | else if (ret == NOTDONE) |
| 4120 | { |
| 4121 | char_u *p; |
| 4122 | int r; |
| 4123 | |
| 4124 | if (!eval_isnamec1(**arg)) |
| 4125 | { |
| 4126 | semsg(_("E1015: Name expected: %s"), *arg); |
| 4127 | return FAIL; |
| 4128 | } |
| 4129 | |
| 4130 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4131 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4132 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4133 | { |
| 4134 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 4135 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4136 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4137 | { |
| 4138 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4139 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4140 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4141 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4142 | if (r == FAIL) |
| 4143 | return FAIL; |
| 4144 | } |
| 4145 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4146 | // Handle following "[]", ".member", etc. |
| 4147 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4148 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4149 | ppconst) == FAIL) |
| 4150 | return FAIL; |
| 4151 | if (ppconst->pp_used > 0) |
| 4152 | { |
| 4153 | // apply the '!', '-' and '+' before the constant |
| 4154 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4155 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
| 4156 | return FAIL; |
| 4157 | return OK; |
| 4158 | } |
| 4159 | if (compile_leader(cctx, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4160 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4161 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | /* |
| 4165 | * * number multiplication |
| 4166 | * / number division |
| 4167 | * % number modulo |
| 4168 | */ |
| 4169 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4170 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4171 | { |
| 4172 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4173 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4174 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4175 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4176 | // get the first expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4177 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4178 | return FAIL; |
| 4179 | |
| 4180 | /* |
| 4181 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4182 | */ |
| 4183 | for (;;) |
| 4184 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4185 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4186 | if (*op != '*' && *op != '/' && *op != '%') |
| 4187 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4188 | if (next != NULL) |
| 4189 | { |
| 4190 | *arg = next_line_from_context(cctx, TRUE); |
| 4191 | op = skipwhite(*arg); |
| 4192 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4193 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4194 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4195 | { |
| 4196 | char_u buf[3]; |
| 4197 | |
| 4198 | vim_strncpy(buf, op, 1); |
| 4199 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4200 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4201 | } |
| 4202 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4203 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4204 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4205 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4206 | // get the second expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4207 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4208 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4209 | |
| 4210 | if (ppconst->pp_used == ppconst_used + 2 |
| 4211 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4212 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4213 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4214 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4215 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4216 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4217 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4218 | // both are numbers: compute the result |
| 4219 | switch (*op) |
| 4220 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4221 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4222 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4223 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4224 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4225 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4226 | break; |
| 4227 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4228 | tv1->vval.v_number = res; |
| 4229 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4230 | } |
| 4231 | else |
| 4232 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4233 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4234 | generate_two_op(cctx, op); |
| 4235 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | return OK; |
| 4239 | } |
| 4240 | |
| 4241 | /* |
| 4242 | * + number addition |
| 4243 | * - number subtraction |
| 4244 | * .. string concatenation |
| 4245 | */ |
| 4246 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4247 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4248 | { |
| 4249 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4250 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4251 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4252 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4253 | |
| 4254 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4255 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4256 | return FAIL; |
| 4257 | |
| 4258 | /* |
| 4259 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4260 | */ |
| 4261 | for (;;) |
| 4262 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4263 | op = may_peek_next_line(cctx, *arg, &next); |
| 4264 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4265 | break; |
| 4266 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4267 | if (next != NULL) |
| 4268 | { |
| 4269 | *arg = next_line_from_context(cctx, TRUE); |
| 4270 | op = skipwhite(*arg); |
| 4271 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4272 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4273 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4274 | { |
| 4275 | char_u buf[3]; |
| 4276 | |
| 4277 | vim_strncpy(buf, op, oplen); |
| 4278 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4279 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4280 | } |
| 4281 | |
| 4282 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4283 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4284 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4285 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4286 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4287 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4288 | return FAIL; |
| 4289 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4290 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4291 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4292 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4293 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4294 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4295 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4296 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4297 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4298 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4299 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4300 | // concat/subtract/add constant numbers |
| 4301 | if (*op == '+') |
| 4302 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4303 | else if (*op == '-') |
| 4304 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4305 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4306 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4307 | // concatenate constant strings |
| 4308 | char_u *s1 = tv1->vval.v_string; |
| 4309 | char_u *s2 = tv2->vval.v_string; |
| 4310 | size_t len1 = STRLEN(s1); |
| 4311 | |
| 4312 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4313 | if (tv1->vval.v_string == NULL) |
| 4314 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4315 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4316 | return FAIL; |
| 4317 | } |
| 4318 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4319 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4320 | vim_free(s1); |
| 4321 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4322 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4323 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4324 | } |
| 4325 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4326 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4327 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4328 | if (*op == '.') |
| 4329 | { |
| 4330 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4331 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4332 | return FAIL; |
| 4333 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4334 | } |
| 4335 | else |
| 4336 | generate_two_op(cctx, op); |
| 4337 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | return OK; |
| 4341 | } |
| 4342 | |
| 4343 | /* |
| 4344 | * expr5a == expr5b |
| 4345 | * expr5a =~ expr5b |
| 4346 | * expr5a != expr5b |
| 4347 | * expr5a !~ expr5b |
| 4348 | * expr5a > expr5b |
| 4349 | * expr5a >= expr5b |
| 4350 | * expr5a < expr5b |
| 4351 | * expr5a <= expr5b |
| 4352 | * expr5a is expr5b |
| 4353 | * expr5a isnot expr5b |
| 4354 | * |
| 4355 | * Produces instructions: |
| 4356 | * EVAL expr5a Push result of "expr5a" |
| 4357 | * EVAL expr5b Push result of "expr5b" |
| 4358 | * COMPARE one of the compare instructions |
| 4359 | */ |
| 4360 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4361 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4362 | { |
| 4363 | exptype_T type = EXPR_UNKNOWN; |
| 4364 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4365 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4366 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4367 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4368 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4369 | |
| 4370 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4371 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4372 | return FAIL; |
| 4373 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4374 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4375 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4376 | |
| 4377 | /* |
| 4378 | * If there is a comparative operator, use it. |
| 4379 | */ |
| 4380 | if (type != EXPR_UNKNOWN) |
| 4381 | { |
| 4382 | int ic = FALSE; // Default: do not ignore case |
| 4383 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4384 | if (next != NULL) |
| 4385 | { |
| 4386 | *arg = next_line_from_context(cctx, TRUE); |
| 4387 | p = skipwhite(*arg); |
| 4388 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4389 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4390 | { |
| 4391 | semsg(_(e_invexpr2), *arg); |
| 4392 | return FAIL; |
| 4393 | } |
| 4394 | // extra question mark appended: ignore case |
| 4395 | if (p[len] == '?') |
| 4396 | { |
| 4397 | ic = TRUE; |
| 4398 | ++len; |
| 4399 | } |
| 4400 | // extra '#' appended: match case (ignored) |
| 4401 | else if (p[len] == '#') |
| 4402 | ++len; |
| 4403 | // nothing appended: match case |
| 4404 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4405 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4406 | { |
| 4407 | char_u buf[7]; |
| 4408 | |
| 4409 | vim_strncpy(buf, p, len); |
| 4410 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4411 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4412 | } |
| 4413 | |
| 4414 | // get the second variable |
| 4415 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4416 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4417 | return FAIL; |
| 4418 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4419 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4420 | return FAIL; |
| 4421 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4422 | if (ppconst->pp_used == ppconst_used + 2) |
| 4423 | { |
| 4424 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4425 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4426 | int ret; |
| 4427 | |
| 4428 | // Both sides are a constant, compute the result now. |
| 4429 | // First check for a valid combination of types, this is more |
| 4430 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4431 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4432 | ret = FAIL; |
| 4433 | else |
| 4434 | { |
| 4435 | ret = typval_compare(tv1, tv2, type, ic); |
| 4436 | tv1->v_type = VAR_BOOL; |
| 4437 | tv1->vval.v_number = tv1->vval.v_number |
| 4438 | ? VVAL_TRUE : VVAL_FALSE; |
| 4439 | clear_tv(tv2); |
| 4440 | --ppconst->pp_used; |
| 4441 | } |
| 4442 | return ret; |
| 4443 | } |
| 4444 | |
| 4445 | generate_ppconst(cctx, ppconst); |
| 4446 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4447 | } |
| 4448 | |
| 4449 | return OK; |
| 4450 | } |
| 4451 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4452 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4453 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4454 | /* |
| 4455 | * Compile || or &&. |
| 4456 | */ |
| 4457 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4458 | compile_and_or( |
| 4459 | char_u **arg, |
| 4460 | cctx_T *cctx, |
| 4461 | char *op, |
| 4462 | ppconst_T *ppconst, |
| 4463 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4464 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4465 | char_u *next; |
| 4466 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4467 | int opchar = *op; |
| 4468 | |
| 4469 | if (p[0] == opchar && p[1] == opchar) |
| 4470 | { |
| 4471 | garray_T *instr = &cctx->ctx_instr; |
| 4472 | garray_T end_ga; |
| 4473 | |
| 4474 | /* |
| 4475 | * Repeat until there is no following "||" or "&&" |
| 4476 | */ |
| 4477 | ga_init2(&end_ga, sizeof(int), 10); |
| 4478 | while (p[0] == opchar && p[1] == opchar) |
| 4479 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4480 | if (next != NULL) |
| 4481 | { |
| 4482 | *arg = next_line_from_context(cctx, TRUE); |
| 4483 | p = skipwhite(*arg); |
| 4484 | } |
| 4485 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4486 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4487 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4488 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4489 | return FAIL; |
| 4490 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4491 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4492 | // TODO: use ppconst if the value is a constant |
| 4493 | generate_ppconst(cctx, ppconst); |
| 4494 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4495 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4496 | { |
| 4497 | ga_clear(&end_ga); |
| 4498 | return FAIL; |
| 4499 | } |
| 4500 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4501 | ++end_ga.ga_len; |
| 4502 | generate_JUMP(cctx, opchar == '|' |
| 4503 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 4504 | |
| 4505 | // eval the next expression |
| 4506 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4507 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4508 | return FAIL; |
| 4509 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4510 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4511 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4512 | { |
| 4513 | ga_clear(&end_ga); |
| 4514 | return FAIL; |
| 4515 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4516 | |
| 4517 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4518 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4519 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4520 | |
| 4521 | // Fill in the end label in all jumps. |
| 4522 | while (end_ga.ga_len > 0) |
| 4523 | { |
| 4524 | isn_T *isn; |
| 4525 | |
| 4526 | --end_ga.ga_len; |
| 4527 | isn = ((isn_T *)instr->ga_data) |
| 4528 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4529 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4530 | } |
| 4531 | ga_clear(&end_ga); |
| 4532 | } |
| 4533 | |
| 4534 | return OK; |
| 4535 | } |
| 4536 | |
| 4537 | /* |
| 4538 | * expr4a && expr4a && expr4a logical AND |
| 4539 | * |
| 4540 | * Produces instructions: |
| 4541 | * EVAL expr4a Push result of "expr4a" |
| 4542 | * JUMP_AND_KEEP_IF_FALSE end |
| 4543 | * EVAL expr4b Push result of "expr4b" |
| 4544 | * JUMP_AND_KEEP_IF_FALSE end |
| 4545 | * EVAL expr4c Push result of "expr4c" |
| 4546 | * end: |
| 4547 | */ |
| 4548 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4549 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4550 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4551 | int ppconst_used = ppconst->pp_used; |
| 4552 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4553 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4554 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4555 | return FAIL; |
| 4556 | |
| 4557 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4558 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | /* |
| 4562 | * expr3a || expr3b || expr3c logical OR |
| 4563 | * |
| 4564 | * Produces instructions: |
| 4565 | * EVAL expr3a Push result of "expr3a" |
| 4566 | * JUMP_AND_KEEP_IF_TRUE end |
| 4567 | * EVAL expr3b Push result of "expr3b" |
| 4568 | * JUMP_AND_KEEP_IF_TRUE end |
| 4569 | * EVAL expr3c Push result of "expr3c" |
| 4570 | * end: |
| 4571 | */ |
| 4572 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4573 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4574 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4575 | int ppconst_used = ppconst->pp_used; |
| 4576 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4577 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4578 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4579 | return FAIL; |
| 4580 | |
| 4581 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4582 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | /* |
| 4586 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4587 | * |
| 4588 | * Produces instructions: |
| 4589 | * EVAL expr2 Push result of "expr" |
| 4590 | * JUMP_IF_FALSE alt jump if false |
| 4591 | * EVAL expr1a |
| 4592 | * JUMP_ALWAYS end |
| 4593 | * alt: EVAL expr1b |
| 4594 | * end: |
| 4595 | */ |
| 4596 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4597 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4598 | { |
| 4599 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4600 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4601 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4602 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4603 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4604 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4605 | return FAIL; |
| 4606 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4607 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4608 | if (*p == '?') |
| 4609 | { |
| 4610 | garray_T *instr = &cctx->ctx_instr; |
| 4611 | garray_T *stack = &cctx->ctx_type_stack; |
| 4612 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4613 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4614 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4615 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4616 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4617 | int has_const_expr = FALSE; |
| 4618 | int const_value = FALSE; |
| 4619 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4620 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4621 | if (next != NULL) |
| 4622 | { |
| 4623 | *arg = next_line_from_context(cctx, TRUE); |
| 4624 | p = skipwhite(*arg); |
| 4625 | } |
| 4626 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4627 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4628 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4629 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4630 | return FAIL; |
| 4631 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4632 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4633 | if (ppconst->pp_used == ppconst_used + 1) |
| 4634 | { |
| 4635 | // the condition is a constant, we know whether the ? or the : |
| 4636 | // expression is to be evaluated. |
| 4637 | has_const_expr = TRUE; |
| 4638 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4639 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4640 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4641 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4642 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4643 | } |
| 4644 | else |
| 4645 | { |
| 4646 | generate_ppconst(cctx, ppconst); |
| 4647 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4648 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4649 | |
| 4650 | // evaluate the second expression; any type is accepted |
| 4651 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4652 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4653 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4654 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4655 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4656 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4657 | if (!has_const_expr) |
| 4658 | { |
| 4659 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4660 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4661 | // remember the type and drop it |
| 4662 | --stack->ga_len; |
| 4663 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4664 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4665 | end_idx = instr->ga_len; |
| 4666 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4667 | |
| 4668 | // jump here from JUMP_IF_FALSE |
| 4669 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4670 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4671 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4672 | |
| 4673 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4674 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4675 | if (*p != ':') |
| 4676 | { |
| 4677 | emsg(_(e_missing_colon)); |
| 4678 | return FAIL; |
| 4679 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4680 | if (next != NULL) |
| 4681 | { |
| 4682 | *arg = next_line_from_context(cctx, TRUE); |
| 4683 | p = skipwhite(*arg); |
| 4684 | } |
| 4685 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4686 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4687 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4688 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4689 | return FAIL; |
| 4690 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4691 | |
| 4692 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4693 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4694 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4695 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4696 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4697 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4698 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4699 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4700 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4701 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4702 | if (!has_const_expr) |
| 4703 | { |
| 4704 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4705 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4706 | // If the types differ, the result has a more generic type. |
| 4707 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4708 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4709 | |
| 4710 | // jump here from JUMP_ALWAYS |
| 4711 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4712 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4713 | } |
| 4714 | |
| 4715 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4716 | } |
| 4717 | return OK; |
| 4718 | } |
| 4719 | |
| 4720 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4721 | * Toplevel expression. |
| 4722 | */ |
| 4723 | static int |
| 4724 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4725 | { |
| 4726 | ppconst_T ppconst; |
| 4727 | |
| 4728 | CLEAR_FIELD(ppconst); |
| 4729 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4730 | { |
| 4731 | clear_ppconst(&ppconst); |
| 4732 | return FAIL; |
| 4733 | } |
| 4734 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4735 | return FAIL; |
| 4736 | return OK; |
| 4737 | } |
| 4738 | |
| 4739 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4740 | * compile "return [expr]" |
| 4741 | */ |
| 4742 | static char_u * |
| 4743 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4744 | { |
| 4745 | char_u *p = arg; |
| 4746 | garray_T *stack = &cctx->ctx_type_stack; |
| 4747 | type_T *stack_type; |
| 4748 | |
| 4749 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4750 | { |
| 4751 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4752 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4753 | return NULL; |
| 4754 | |
| 4755 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4756 | if (set_return_type) |
| 4757 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4758 | else |
| 4759 | { |
| 4760 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4761 | && stack_type->tt_type != VAR_VOID |
| 4762 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4763 | { |
| 4764 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4765 | return NULL; |
| 4766 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4767 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4768 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4769 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4770 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4771 | } |
| 4772 | else |
| 4773 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4774 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4775 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4776 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4777 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4778 | { |
| 4779 | emsg(_("E1003: Missing return value")); |
| 4780 | return NULL; |
| 4781 | } |
| 4782 | |
| 4783 | // No argument, return zero. |
| 4784 | generate_PUSHNR(cctx, 0); |
| 4785 | } |
| 4786 | |
| 4787 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4788 | return NULL; |
| 4789 | |
| 4790 | // "return val | endif" is possible |
| 4791 | return skipwhite(p); |
| 4792 | } |
| 4793 | |
| 4794 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4795 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4796 | * Return a pointer to the line in allocated memory. |
| 4797 | * Return NULL for end-of-file or some error. |
| 4798 | */ |
| 4799 | static char_u * |
| 4800 | exarg_getline( |
| 4801 | int c UNUSED, |
| 4802 | void *cookie, |
| 4803 | int indent UNUSED, |
| 4804 | int do_concat UNUSED) |
| 4805 | { |
| 4806 | cctx_T *cctx = (cctx_T *)cookie; |
| 4807 | |
| 4808 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4809 | { |
| 4810 | iemsg("Heredoc got to end"); |
| 4811 | return NULL; |
| 4812 | } |
| 4813 | ++cctx->ctx_lnum; |
| 4814 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4815 | [cctx->ctx_lnum]); |
| 4816 | } |
| 4817 | |
| 4818 | /* |
| 4819 | * Compile a nested :def command. |
| 4820 | */ |
| 4821 | static char_u * |
| 4822 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4823 | { |
| 4824 | char_u *name_start = eap->arg; |
| 4825 | char_u *name_end = to_name_end(eap->arg, FALSE); |
| 4826 | char_u *name = get_lambda_name(); |
| 4827 | lvar_T *lvar; |
| 4828 | ufunc_T *ufunc; |
| 4829 | |
| 4830 | eap->arg = name_end; |
| 4831 | eap->getline = exarg_getline; |
| 4832 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4833 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4834 | eap->forceit = FALSE; |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4835 | ufunc = def_function(eap, name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4836 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4837 | if (ufunc == NULL) |
| 4838 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4839 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4840 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4841 | return NULL; |
| 4842 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4843 | // Define a local variable for the function reference. |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4844 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4845 | TRUE, ufunc->uf_func_type); |
| 4846 | |
| 4847 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL |
| 4848 | || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL) |
| 4849 | return NULL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4850 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4851 | // TODO: warning for trailing text? |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4852 | return (char_u *)""; |
| 4853 | } |
| 4854 | |
| 4855 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4856 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4857 | */ |
| 4858 | int |
| 4859 | assignment_len(char_u *p, int *heredoc) |
| 4860 | { |
| 4861 | if (*p == '=') |
| 4862 | { |
| 4863 | if (p[1] == '<' && p[2] == '<') |
| 4864 | { |
| 4865 | *heredoc = TRUE; |
| 4866 | return 3; |
| 4867 | } |
| 4868 | return 1; |
| 4869 | } |
| 4870 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4871 | return 2; |
| 4872 | if (STRNCMP(p, "..=", 3) == 0) |
| 4873 | return 3; |
| 4874 | return 0; |
| 4875 | } |
| 4876 | |
| 4877 | // words that cannot be used as a variable |
| 4878 | static char *reserved[] = { |
| 4879 | "true", |
| 4880 | "false", |
| 4881 | NULL |
| 4882 | }; |
| 4883 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4884 | typedef enum { |
| 4885 | dest_local, |
| 4886 | dest_option, |
| 4887 | dest_env, |
| 4888 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4889 | dest_buffer, |
| 4890 | dest_window, |
| 4891 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4892 | dest_vimvar, |
| 4893 | dest_script, |
| 4894 | dest_reg, |
| 4895 | } assign_dest_T; |
| 4896 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4897 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4898 | * Generate the load instruction for "name". |
| 4899 | */ |
| 4900 | static void |
| 4901 | generate_loadvar( |
| 4902 | cctx_T *cctx, |
| 4903 | assign_dest_T dest, |
| 4904 | char_u *name, |
| 4905 | lvar_T *lvar, |
| 4906 | type_T *type) |
| 4907 | { |
| 4908 | switch (dest) |
| 4909 | { |
| 4910 | case dest_option: |
| 4911 | // TODO: check the option exists |
| 4912 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4913 | break; |
| 4914 | case dest_global: |
| 4915 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4916 | break; |
| 4917 | case dest_buffer: |
| 4918 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4919 | break; |
| 4920 | case dest_window: |
| 4921 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4922 | break; |
| 4923 | case dest_tab: |
| 4924 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4925 | break; |
| 4926 | case dest_script: |
| 4927 | compile_load_scriptvar(cctx, |
| 4928 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4929 | break; |
| 4930 | case dest_env: |
| 4931 | // Include $ in the name here |
| 4932 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4933 | break; |
| 4934 | case dest_reg: |
| 4935 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4936 | break; |
| 4937 | case dest_vimvar: |
| 4938 | generate_LOADV(cctx, name + 2, TRUE); |
| 4939 | break; |
| 4940 | case dest_local: |
| 4941 | if (lvar->lv_from_outer) |
| 4942 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4943 | NULL, type); |
| 4944 | else |
| 4945 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4946 | break; |
| 4947 | } |
| 4948 | } |
| 4949 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4950 | void |
| 4951 | vim9_declare_error(char_u *name) |
| 4952 | { |
| 4953 | char *scope = ""; |
| 4954 | |
| 4955 | switch (*name) |
| 4956 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4957 | case 'g': scope = _("global"); break; |
| 4958 | case 'b': scope = _("buffer"); break; |
| 4959 | case 'w': scope = _("window"); break; |
| 4960 | case 't': scope = _("tab"); break; |
| 4961 | case 'v': scope = "v:"; break; |
| 4962 | case '$': semsg(_(e_declare_env_var), name); return; |
| 4963 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4964 | } |
| 4965 | semsg(_(e_declare_var), scope, name); |
| 4966 | } |
| 4967 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4968 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4969 | * Compile declaration and assignment: |
| 4970 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4971 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4972 | * Return NULL for an error. |
| 4973 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4974 | */ |
| 4975 | static char_u * |
| 4976 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4977 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4978 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4979 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4980 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4981 | char_u *ret = NULL; |
| 4982 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4983 | int var_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4984 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4985 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4986 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4987 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4988 | int oplen = 0; |
| 4989 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4990 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4991 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4992 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4993 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4994 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4995 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4996 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 4997 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 4998 | if (p == NULL) |
| 4999 | return *arg == '[' ? arg : NULL; |
| 5000 | |
| 5001 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5002 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 5003 | // TODO: should we allow this, and figure out type inference from list |
| 5004 | // members? |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5005 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5006 | return NULL; |
| 5007 | } |
| 5008 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5009 | sp = p; |
| 5010 | p = skipwhite(p); |
| 5011 | op = p; |
| 5012 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5013 | |
| 5014 | if (var_count > 0 && oplen == 0) |
| 5015 | // can be something like "[1, 2]->func()" |
| 5016 | return arg; |
| 5017 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5018 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 5019 | { |
| 5020 | char_u buf[4]; |
| 5021 | |
| 5022 | vim_strncpy(buf, op, oplen); |
| 5023 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5024 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 5025 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5026 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5027 | if (heredoc) |
| 5028 | { |
| 5029 | list_T *l; |
| 5030 | listitem_T *li; |
| 5031 | |
| 5032 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5033 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5034 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 5035 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5036 | |
| 5037 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 5038 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5039 | { |
| 5040 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 5041 | li->li_tv.vval.v_string = NULL; |
| 5042 | } |
| 5043 | generate_NEWLIST(cctx, l->lv_len); |
| 5044 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5045 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5046 | list_free(l); |
| 5047 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5048 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5049 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5050 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5051 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5052 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 5053 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5054 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5055 | p = skipwhite(op + oplen); |
| 5056 | if (compile_expr0(&p, cctx) == FAIL) |
| 5057 | return NULL; |
| 5058 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5059 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5060 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5061 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5062 | type_T *stacktype; |
| 5063 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5064 | stacktype = stack->ga_len == 0 ? &t_void |
| 5065 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5066 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5067 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5068 | emsg(_(e_cannot_use_void)); |
| 5069 | goto theend; |
| 5070 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5071 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5072 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5073 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5074 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 5075 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5076 | } |
| 5077 | } |
| 5078 | |
| 5079 | /* |
| 5080 | * Loop over variables in "[var, var] = expr". |
| 5081 | * For "var = expr" and "let var: type" this is done only once. |
| 5082 | */ |
| 5083 | if (var_count > 0) |
| 5084 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 5085 | else |
| 5086 | var_start = arg; |
| 5087 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 5088 | { |
| 5089 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 5090 | size_t varlen; |
| 5091 | int new_local = FALSE; |
| 5092 | int opt_type; |
| 5093 | int opt_flags = 0; |
| 5094 | assign_dest_T dest = dest_local; |
| 5095 | int vimvaridx = -1; |
| 5096 | lvar_T *lvar = NULL; |
| 5097 | lvar_T arg_lvar; |
| 5098 | int has_type = FALSE; |
| 5099 | int has_index = FALSE; |
| 5100 | int instr_count = -1; |
| 5101 | |
| 5102 | p = (*var_start == '&' || *var_start == '$' |
| 5103 | || *var_start == '@') ? var_start + 1 : var_start; |
| 5104 | p = to_name_end(p, TRUE); |
| 5105 | |
| 5106 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 5107 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 5108 | --var_end; |
| 5109 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 5110 | --p; |
| 5111 | |
| 5112 | varlen = p - var_start; |
| 5113 | vim_free(name); |
| 5114 | name = vim_strnsave(var_start, varlen); |
| 5115 | if (name == NULL) |
| 5116 | return NULL; |
| 5117 | if (!heredoc) |
| 5118 | type = &t_any; |
| 5119 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5120 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5121 | { |
| 5122 | if (*var_start == '&') |
| 5123 | { |
| 5124 | int cc; |
| 5125 | long numval; |
| 5126 | |
| 5127 | dest = dest_option; |
| 5128 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5129 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5130 | emsg(_(e_const_option)); |
| 5131 | goto theend; |
| 5132 | } |
| 5133 | if (is_decl) |
| 5134 | { |
| 5135 | semsg(_("E1052: Cannot declare an option: %s"), var_start); |
| 5136 | goto theend; |
| 5137 | } |
| 5138 | p = var_start; |
| 5139 | p = find_option_end(&p, &opt_flags); |
| 5140 | if (p == NULL) |
| 5141 | { |
| 5142 | // cannot happen? |
| 5143 | emsg(_(e_letunexp)); |
| 5144 | goto theend; |
| 5145 | } |
| 5146 | cc = *p; |
| 5147 | *p = NUL; |
| 5148 | opt_type = get_option_value(var_start + 1, &numval, |
| 5149 | NULL, opt_flags); |
| 5150 | *p = cc; |
| 5151 | if (opt_type == -3) |
| 5152 | { |
| 5153 | semsg(_(e_unknown_option), var_start); |
| 5154 | goto theend; |
| 5155 | } |
| 5156 | if (opt_type == -2 || opt_type == 0) |
| 5157 | type = &t_string; |
| 5158 | else |
| 5159 | type = &t_number; // both number and boolean option |
| 5160 | } |
| 5161 | else if (*var_start == '$') |
| 5162 | { |
| 5163 | dest = dest_env; |
| 5164 | type = &t_string; |
| 5165 | if (is_decl) |
| 5166 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5167 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5168 | goto theend; |
| 5169 | } |
| 5170 | } |
| 5171 | else if (*var_start == '@') |
| 5172 | { |
| 5173 | if (!valid_yank_reg(var_start[1], TRUE)) |
| 5174 | { |
| 5175 | emsg_invreg(var_start[1]); |
| 5176 | goto theend; |
| 5177 | } |
| 5178 | dest = dest_reg; |
| 5179 | type = &t_string; |
| 5180 | if (is_decl) |
| 5181 | { |
| 5182 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 5183 | goto theend; |
| 5184 | } |
| 5185 | } |
| 5186 | else if (STRNCMP(var_start, "g:", 2) == 0) |
| 5187 | { |
| 5188 | dest = dest_global; |
| 5189 | if (is_decl) |
| 5190 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5191 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5192 | goto theend; |
| 5193 | } |
| 5194 | } |
| 5195 | else if (STRNCMP(var_start, "b:", 2) == 0) |
| 5196 | { |
| 5197 | dest = dest_buffer; |
| 5198 | if (is_decl) |
| 5199 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5200 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5201 | goto theend; |
| 5202 | } |
| 5203 | } |
| 5204 | else if (STRNCMP(var_start, "w:", 2) == 0) |
| 5205 | { |
| 5206 | dest = dest_window; |
| 5207 | if (is_decl) |
| 5208 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5209 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5210 | goto theend; |
| 5211 | } |
| 5212 | } |
| 5213 | else if (STRNCMP(var_start, "t:", 2) == 0) |
| 5214 | { |
| 5215 | dest = dest_tab; |
| 5216 | if (is_decl) |
| 5217 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5218 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5219 | goto theend; |
| 5220 | } |
| 5221 | } |
| 5222 | else if (STRNCMP(var_start, "v:", 2) == 0) |
| 5223 | { |
| 5224 | typval_T *vtv; |
| 5225 | int di_flags; |
| 5226 | |
| 5227 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5228 | if (vimvaridx < 0) |
| 5229 | { |
| 5230 | semsg(_(e_var_notfound), var_start); |
| 5231 | goto theend; |
| 5232 | } |
| 5233 | // We use the current value of "sandbox" here, is that OK? |
| 5234 | if (var_check_ro(di_flags, name, FALSE)) |
| 5235 | goto theend; |
| 5236 | dest = dest_vimvar; |
| 5237 | vtv = get_vim_var_tv(vimvaridx); |
| 5238 | type = typval2type(vtv); |
| 5239 | if (is_decl) |
| 5240 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5241 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5242 | goto theend; |
| 5243 | } |
| 5244 | } |
| 5245 | else |
| 5246 | { |
| 5247 | int idx; |
| 5248 | |
| 5249 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5250 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5251 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5252 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5253 | goto theend; |
| 5254 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5255 | |
| 5256 | lvar = lookup_local(var_start, varlen, cctx); |
| 5257 | if (lvar == NULL) |
| 5258 | { |
| 5259 | CLEAR_FIELD(arg_lvar); |
| 5260 | if (lookup_arg(var_start, varlen, |
| 5261 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 5262 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5263 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5264 | if (is_decl) |
| 5265 | { |
| 5266 | semsg(_(e_used_as_arg), name); |
| 5267 | goto theend; |
| 5268 | } |
| 5269 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5270 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5271 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5272 | if (lvar != NULL) |
| 5273 | { |
| 5274 | if (is_decl) |
| 5275 | { |
| 5276 | semsg(_("E1017: Variable already declared: %s"), name); |
| 5277 | goto theend; |
| 5278 | } |
| 5279 | else if (lvar->lv_const) |
| 5280 | { |
| 5281 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 5282 | name); |
| 5283 | goto theend; |
| 5284 | } |
| 5285 | } |
| 5286 | else if (STRNCMP(var_start, "s:", 2) == 0 |
| 5287 | || lookup_script(var_start, varlen) == OK |
| 5288 | || find_imported(var_start, varlen, cctx) != NULL) |
| 5289 | { |
| 5290 | dest = dest_script; |
| 5291 | if (is_decl) |
| 5292 | { |
| 5293 | semsg(_("E1054: Variable already declared in the script: %s"), |
| 5294 | name); |
| 5295 | goto theend; |
| 5296 | } |
| 5297 | } |
| 5298 | else if (name[1] == ':' && name[2] != NUL) |
| 5299 | { |
| 5300 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
| 5301 | name); |
| 5302 | goto theend; |
| 5303 | } |
| 5304 | else if (!is_decl) |
| 5305 | { |
| 5306 | semsg(_("E1089: unknown variable: %s"), name); |
| 5307 | goto theend; |
| 5308 | } |
| 5309 | } |
| 5310 | } |
| 5311 | |
| 5312 | // handle "a:name" as a name, not index "name" on "a" |
| 5313 | if (varlen > 1 || var_start[varlen] != ':') |
| 5314 | p = var_end; |
| 5315 | |
| 5316 | if (dest != dest_option) |
| 5317 | { |
| 5318 | if (is_decl && *p == ':') |
| 5319 | { |
| 5320 | // parse optional type: "let var: type = expr" |
| 5321 | if (!VIM_ISWHITE(p[1])) |
| 5322 | { |
| 5323 | semsg(_(e_white_after), ":"); |
| 5324 | goto theend; |
| 5325 | } |
| 5326 | p = skipwhite(p + 1); |
| 5327 | type = parse_type(&p, cctx->ctx_type_list); |
| 5328 | has_type = TRUE; |
| 5329 | } |
| 5330 | else if (lvar != NULL) |
| 5331 | type = lvar->lv_type; |
| 5332 | } |
| 5333 | |
| 5334 | if (oplen == 3 && !heredoc && dest != dest_global |
| 5335 | && type->tt_type != VAR_STRING |
| 5336 | && type->tt_type != VAR_ANY) |
| 5337 | { |
| 5338 | emsg(_("E1019: Can only concatenate to string")); |
| 5339 | goto theend; |
| 5340 | } |
| 5341 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5342 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5343 | { |
| 5344 | if (oplen > 1 && !heredoc) |
| 5345 | { |
| 5346 | // +=, /=, etc. require an existing variable |
| 5347 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 5348 | name); |
| 5349 | goto theend; |
| 5350 | } |
| 5351 | |
| 5352 | // new local variable |
| 5353 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
| 5354 | goto theend; |
| 5355 | lvar = reserve_local(cctx, var_start, varlen, |
| 5356 | cmdidx == CMD_const, type); |
| 5357 | if (lvar == NULL) |
| 5358 | goto theend; |
| 5359 | new_local = TRUE; |
| 5360 | } |
| 5361 | |
| 5362 | member_type = type; |
| 5363 | if (var_end > var_start + varlen) |
| 5364 | { |
| 5365 | // Something follows after the variable: "var[idx]". |
| 5366 | if (is_decl) |
| 5367 | { |
| 5368 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 5369 | goto theend; |
| 5370 | } |
| 5371 | |
| 5372 | if (var_start[varlen] == '[') |
| 5373 | { |
| 5374 | has_index = TRUE; |
| 5375 | if (type->tt_member == NULL) |
| 5376 | { |
| 5377 | semsg(_("E1088: cannot use an index on %s"), name); |
| 5378 | goto theend; |
| 5379 | } |
| 5380 | member_type = type->tt_member; |
| 5381 | } |
| 5382 | else |
| 5383 | { |
| 5384 | semsg("Not supported yet: %s", var_start); |
| 5385 | goto theend; |
| 5386 | } |
| 5387 | } |
| 5388 | else if (lvar == &arg_lvar) |
| 5389 | { |
| 5390 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 5391 | goto theend; |
| 5392 | } |
| 5393 | |
| 5394 | if (!heredoc) |
| 5395 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5396 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5397 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5398 | if (oplen > 0 && var_count == 0) |
| 5399 | { |
| 5400 | // skip over the "=" and the expression |
| 5401 | p = skipwhite(op + oplen); |
| 5402 | compile_expr0(&p, cctx); |
| 5403 | } |
| 5404 | } |
| 5405 | else if (oplen > 0) |
| 5406 | { |
| 5407 | type_T *stacktype; |
| 5408 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5409 | // For "var = expr" evaluate the expression. |
| 5410 | if (var_count == 0) |
| 5411 | { |
| 5412 | int r; |
| 5413 | |
| 5414 | // for "+=", "*=", "..=" etc. first load the current value |
| 5415 | if (*op != '=') |
| 5416 | { |
| 5417 | generate_loadvar(cctx, dest, name, lvar, type); |
| 5418 | |
| 5419 | if (has_index) |
| 5420 | { |
| 5421 | // TODO: get member from list or dict |
| 5422 | emsg("Index with operation not supported yet"); |
| 5423 | goto theend; |
| 5424 | } |
| 5425 | } |
| 5426 | |
| 5427 | // Compile the expression. Temporarily hide the new local |
| 5428 | // variable here, it is not available to this expression. |
| 5429 | if (new_local) |
| 5430 | --cctx->ctx_locals.ga_len; |
| 5431 | instr_count = instr->ga_len; |
| 5432 | p = skipwhite(op + oplen); |
| 5433 | r = compile_expr0(&p, cctx); |
| 5434 | if (new_local) |
| 5435 | ++cctx->ctx_locals.ga_len; |
| 5436 | if (r == FAIL) |
| 5437 | goto theend; |
| 5438 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5439 | else if (semicolon && var_idx == var_count - 1) |
| 5440 | { |
| 5441 | // For "[var; var] = expr" get the rest of the list |
| 5442 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5443 | goto theend; |
| 5444 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5445 | else |
| 5446 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5447 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5448 | // list. |
| 5449 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 5450 | return FAIL; |
| 5451 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5452 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5453 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5454 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5455 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5456 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5457 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5458 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5459 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5460 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5461 | emsg(_(e_cannot_use_void)); |
| 5462 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5463 | } |
| 5464 | else |
| 5465 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5466 | // An empty list or dict has a &t_void member, |
| 5467 | // for a variable that implies &t_any. |
| 5468 | if (stacktype == &t_list_empty) |
| 5469 | lvar->lv_type = &t_list_any; |
| 5470 | else if (stacktype == &t_dict_empty) |
| 5471 | lvar->lv_type = &t_dict_any; |
| 5472 | else |
| 5473 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5474 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5475 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5476 | else |
| 5477 | { |
| 5478 | type_T *use_type = lvar->lv_type; |
| 5479 | |
| 5480 | if (has_index) |
| 5481 | { |
| 5482 | use_type = use_type->tt_member; |
| 5483 | if (use_type == NULL) |
| 5484 | use_type = &t_void; |
| 5485 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5486 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5487 | == FAIL) |
| 5488 | goto theend; |
| 5489 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5490 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5491 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5492 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5493 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5494 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5495 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5496 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5497 | emsg(_(e_const_req_value)); |
| 5498 | goto theend; |
| 5499 | } |
| 5500 | else if (!has_type || dest == dest_option) |
| 5501 | { |
| 5502 | emsg(_(e_type_req)); |
| 5503 | goto theend; |
| 5504 | } |
| 5505 | else |
| 5506 | { |
| 5507 | // variables are always initialized |
| 5508 | if (ga_grow(instr, 1) == FAIL) |
| 5509 | goto theend; |
| 5510 | switch (member_type->tt_type) |
| 5511 | { |
| 5512 | case VAR_BOOL: |
| 5513 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5514 | break; |
| 5515 | case VAR_FLOAT: |
| 5516 | #ifdef FEAT_FLOAT |
| 5517 | generate_PUSHF(cctx, 0.0); |
| 5518 | #endif |
| 5519 | break; |
| 5520 | case VAR_STRING: |
| 5521 | generate_PUSHS(cctx, NULL); |
| 5522 | break; |
| 5523 | case VAR_BLOB: |
| 5524 | generate_PUSHBLOB(cctx, NULL); |
| 5525 | break; |
| 5526 | case VAR_FUNC: |
| 5527 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5528 | break; |
| 5529 | case VAR_LIST: |
| 5530 | generate_NEWLIST(cctx, 0); |
| 5531 | break; |
| 5532 | case VAR_DICT: |
| 5533 | generate_NEWDICT(cctx, 0); |
| 5534 | break; |
| 5535 | case VAR_JOB: |
| 5536 | generate_PUSHJOB(cctx, NULL); |
| 5537 | break; |
| 5538 | case VAR_CHANNEL: |
| 5539 | generate_PUSHCHANNEL(cctx, NULL); |
| 5540 | break; |
| 5541 | case VAR_NUMBER: |
| 5542 | case VAR_UNKNOWN: |
| 5543 | case VAR_ANY: |
| 5544 | case VAR_PARTIAL: |
| 5545 | case VAR_VOID: |
| 5546 | case VAR_SPECIAL: // cannot happen |
| 5547 | generate_PUSHNR(cctx, 0); |
| 5548 | break; |
| 5549 | } |
| 5550 | } |
| 5551 | if (var_count == 0) |
| 5552 | end = p; |
| 5553 | } |
| 5554 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5555 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5556 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5557 | break; |
| 5558 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5559 | if (oplen > 0 && *op != '=') |
| 5560 | { |
| 5561 | type_T *expected = &t_number; |
| 5562 | type_T *stacktype; |
| 5563 | |
| 5564 | // TODO: if type is known use float or any operation |
| 5565 | |
| 5566 | if (*op == '.') |
| 5567 | expected = &t_string; |
| 5568 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5569 | if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5570 | goto theend; |
| 5571 | |
| 5572 | if (*op == '.') |
| 5573 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 5574 | else |
| 5575 | { |
| 5576 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 5577 | |
| 5578 | if (isn == NULL) |
| 5579 | goto theend; |
| 5580 | switch (*op) |
| 5581 | { |
| 5582 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 5583 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 5584 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 5585 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 5586 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 5587 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5588 | } |
| 5589 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5590 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5591 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5592 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5593 | int r; |
| 5594 | |
| 5595 | // Compile the "idx" in "var[idx]". |
| 5596 | if (new_local) |
| 5597 | --cctx->ctx_locals.ga_len; |
| 5598 | p = skipwhite(var_start + varlen + 1); |
| 5599 | r = compile_expr0(&p, cctx); |
| 5600 | if (new_local) |
| 5601 | ++cctx->ctx_locals.ga_len; |
| 5602 | if (r == FAIL) |
| 5603 | goto theend; |
| 5604 | if (*skipwhite(p) != ']') |
| 5605 | { |
| 5606 | emsg(_(e_missbrac)); |
| 5607 | goto theend; |
| 5608 | } |
| 5609 | if (type->tt_type == VAR_DICT |
| 5610 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5611 | goto theend; |
| 5612 | if (type->tt_type == VAR_LIST |
| 5613 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5614 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5615 | { |
| 5616 | emsg(_(e_number_exp)); |
| 5617 | goto theend; |
| 5618 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5619 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5620 | // Load the dict or list. On the stack we then have: |
| 5621 | // - value |
| 5622 | // - index |
| 5623 | // - variable |
| 5624 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5625 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5626 | if (type->tt_type == VAR_LIST) |
| 5627 | { |
| 5628 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5629 | return FAIL; |
| 5630 | } |
| 5631 | else if (type->tt_type == VAR_DICT) |
| 5632 | { |
| 5633 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5634 | return FAIL; |
| 5635 | } |
| 5636 | else |
| 5637 | { |
| 5638 | emsg(_(e_listreq)); |
| 5639 | goto theend; |
| 5640 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5641 | } |
| 5642 | else |
| 5643 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5644 | switch (dest) |
| 5645 | { |
| 5646 | case dest_option: |
| 5647 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5648 | break; |
| 5649 | case dest_global: |
| 5650 | // include g: with the name, easier to execute that way |
| 5651 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5652 | break; |
| 5653 | case dest_buffer: |
| 5654 | // include b: with the name, easier to execute that way |
| 5655 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5656 | break; |
| 5657 | case dest_window: |
| 5658 | // include w: with the name, easier to execute that way |
| 5659 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5660 | break; |
| 5661 | case dest_tab: |
| 5662 | // include t: with the name, easier to execute that way |
| 5663 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5664 | break; |
| 5665 | case dest_env: |
| 5666 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5667 | break; |
| 5668 | case dest_reg: |
| 5669 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5670 | break; |
| 5671 | case dest_vimvar: |
| 5672 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5673 | break; |
| 5674 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5675 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5676 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5677 | imported_T *import = NULL; |
| 5678 | int sid = current_sctx.sc_sid; |
| 5679 | int idx; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5680 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5681 | if (name[1] != ':') |
| 5682 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5683 | import = find_imported(name, 0, cctx); |
| 5684 | if (import != NULL) |
| 5685 | sid = import->imp_sid; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5686 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5687 | |
| 5688 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 5689 | // TODO: specific type |
| 5690 | if (idx < 0) |
| 5691 | { |
| 5692 | char_u *name_s = name; |
| 5693 | |
| 5694 | // Include s: in the name for store_var() |
| 5695 | if (name[1] != ':') |
| 5696 | { |
| 5697 | int len = (int)STRLEN(name) + 3; |
| 5698 | |
| 5699 | name_s = alloc(len); |
| 5700 | if (name_s == NULL) |
| 5701 | name_s = name; |
| 5702 | else |
| 5703 | vim_snprintf((char *)name_s, len, |
| 5704 | "s:%s", name); |
| 5705 | } |
| 5706 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5707 | &t_any); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5708 | if (name_s != name) |
| 5709 | vim_free(name_s); |
| 5710 | } |
| 5711 | else |
| 5712 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5713 | sid, idx, &t_any); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5714 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5715 | break; |
| 5716 | case dest_local: |
| 5717 | if (lvar != NULL) |
| 5718 | { |
| 5719 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5720 | + instr->ga_len - 1; |
| 5721 | |
| 5722 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5723 | // ISN_STORE into ISN_STORENR |
| 5724 | if (!lvar->lv_from_outer |
| 5725 | && instr->ga_len == instr_count + 1 |
| 5726 | && isn->isn_type == ISN_PUSHNR) |
| 5727 | { |
| 5728 | varnumber_T val = isn->isn_arg.number; |
| 5729 | |
| 5730 | isn->isn_type = ISN_STORENR; |
| 5731 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5732 | isn->isn_arg.storenr.stnr_val = val; |
| 5733 | if (stack->ga_len > 0) |
| 5734 | --stack->ga_len; |
| 5735 | } |
| 5736 | else if (lvar->lv_from_outer) |
| 5737 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5738 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5739 | else |
| 5740 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5741 | } |
| 5742 | break; |
| 5743 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5744 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5745 | |
| 5746 | if (var_idx + 1 < var_count) |
| 5747 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5748 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5749 | |
| 5750 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5751 | if (var_count > 0 && !semicolon) |
| 5752 | { |
| 5753 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5754 | goto theend; |
| 5755 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5756 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5757 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5758 | |
| 5759 | theend: |
| 5760 | vim_free(name); |
| 5761 | return ret; |
| 5762 | } |
| 5763 | |
| 5764 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5765 | * Check if "name" can be "unlet". |
| 5766 | */ |
| 5767 | int |
| 5768 | check_vim9_unlet(char_u *name) |
| 5769 | { |
| 5770 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5771 | { |
| 5772 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5773 | return FAIL; |
| 5774 | } |
| 5775 | return OK; |
| 5776 | } |
| 5777 | |
| 5778 | /* |
| 5779 | * Callback passed to ex_unletlock(). |
| 5780 | */ |
| 5781 | static int |
| 5782 | compile_unlet( |
| 5783 | lval_T *lvp, |
| 5784 | char_u *name_end, |
| 5785 | exarg_T *eap, |
| 5786 | int deep UNUSED, |
| 5787 | void *coookie) |
| 5788 | { |
| 5789 | cctx_T *cctx = coookie; |
| 5790 | |
| 5791 | if (lvp->ll_tv == NULL) |
| 5792 | { |
| 5793 | char_u *p = lvp->ll_name; |
| 5794 | int cc = *name_end; |
| 5795 | int ret = OK; |
| 5796 | |
| 5797 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5798 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5799 | if (*p == '$') |
| 5800 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5801 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5802 | ret = FAIL; |
| 5803 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5804 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5805 | |
| 5806 | *name_end = cc; |
| 5807 | return ret; |
| 5808 | } |
| 5809 | |
| 5810 | // TODO: unlet {list}[idx] |
| 5811 | // TODO: unlet {dict}[key] |
| 5812 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5813 | return FAIL; |
| 5814 | } |
| 5815 | |
| 5816 | /* |
| 5817 | * compile "unlet var", "lock var" and "unlock var" |
| 5818 | * "arg" points to "var". |
| 5819 | */ |
| 5820 | static char_u * |
| 5821 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5822 | { |
| 5823 | char_u *p = arg; |
| 5824 | |
| 5825 | if (eap->cmdidx != CMD_unlet) |
| 5826 | { |
| 5827 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5828 | return NULL; |
| 5829 | } |
| 5830 | |
| 5831 | if (*p == '!') |
| 5832 | { |
| 5833 | p = skipwhite(p + 1); |
| 5834 | eap->forceit = TRUE; |
| 5835 | } |
| 5836 | |
| 5837 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5838 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5839 | } |
| 5840 | |
| 5841 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5842 | * Compile an :import command. |
| 5843 | */ |
| 5844 | static char_u * |
| 5845 | compile_import(char_u *arg, cctx_T *cctx) |
| 5846 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5847 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5848 | } |
| 5849 | |
| 5850 | /* |
| 5851 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5852 | */ |
| 5853 | static int |
| 5854 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5855 | { |
| 5856 | garray_T *instr = &cctx->ctx_instr; |
| 5857 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5858 | |
| 5859 | if (endlabel == NULL) |
| 5860 | return FAIL; |
| 5861 | endlabel->el_next = *el; |
| 5862 | *el = endlabel; |
| 5863 | endlabel->el_end_label = instr->ga_len; |
| 5864 | |
| 5865 | generate_JUMP(cctx, when, 0); |
| 5866 | return OK; |
| 5867 | } |
| 5868 | |
| 5869 | static void |
| 5870 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5871 | { |
| 5872 | garray_T *instr = &cctx->ctx_instr; |
| 5873 | |
| 5874 | while (*el != NULL) |
| 5875 | { |
| 5876 | endlabel_T *cur = (*el); |
| 5877 | isn_T *isn; |
| 5878 | |
| 5879 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5880 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5881 | *el = cur->el_next; |
| 5882 | vim_free(cur); |
| 5883 | } |
| 5884 | } |
| 5885 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5886 | static void |
| 5887 | compile_free_jump_to_end(endlabel_T **el) |
| 5888 | { |
| 5889 | while (*el != NULL) |
| 5890 | { |
| 5891 | endlabel_T *cur = (*el); |
| 5892 | |
| 5893 | *el = cur->el_next; |
| 5894 | vim_free(cur); |
| 5895 | } |
| 5896 | } |
| 5897 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5898 | /* |
| 5899 | * Create a new scope and set up the generic items. |
| 5900 | */ |
| 5901 | static scope_T * |
| 5902 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5903 | { |
| 5904 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5905 | |
| 5906 | if (scope == NULL) |
| 5907 | return NULL; |
| 5908 | scope->se_outer = cctx->ctx_scope; |
| 5909 | cctx->ctx_scope = scope; |
| 5910 | scope->se_type = type; |
| 5911 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5912 | return scope; |
| 5913 | } |
| 5914 | |
| 5915 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5916 | * Free the current scope and go back to the outer scope. |
| 5917 | */ |
| 5918 | static void |
| 5919 | drop_scope(cctx_T *cctx) |
| 5920 | { |
| 5921 | scope_T *scope = cctx->ctx_scope; |
| 5922 | |
| 5923 | if (scope == NULL) |
| 5924 | { |
| 5925 | iemsg("calling drop_scope() without a scope"); |
| 5926 | return; |
| 5927 | } |
| 5928 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5929 | switch (scope->se_type) |
| 5930 | { |
| 5931 | case IF_SCOPE: |
| 5932 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5933 | case FOR_SCOPE: |
| 5934 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5935 | case WHILE_SCOPE: |
| 5936 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5937 | case TRY_SCOPE: |
| 5938 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5939 | case NO_SCOPE: |
| 5940 | case BLOCK_SCOPE: |
| 5941 | break; |
| 5942 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5943 | vim_free(scope); |
| 5944 | } |
| 5945 | |
| 5946 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5947 | * compile "if expr" |
| 5948 | * |
| 5949 | * "if expr" Produces instructions: |
| 5950 | * EVAL expr Push result of "expr" |
| 5951 | * JUMP_IF_FALSE end |
| 5952 | * ... body ... |
| 5953 | * end: |
| 5954 | * |
| 5955 | * "if expr | else" Produces instructions: |
| 5956 | * EVAL expr Push result of "expr" |
| 5957 | * JUMP_IF_FALSE else |
| 5958 | * ... body ... |
| 5959 | * JUMP_ALWAYS end |
| 5960 | * else: |
| 5961 | * ... body ... |
| 5962 | * end: |
| 5963 | * |
| 5964 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5965 | * EVAL expr Push result of "expr" |
| 5966 | * JUMP_IF_FALSE elseif |
| 5967 | * ... body ... |
| 5968 | * JUMP_ALWAYS end |
| 5969 | * elseif: |
| 5970 | * EVAL expr Push result of "expr" |
| 5971 | * JUMP_IF_FALSE else |
| 5972 | * ... body ... |
| 5973 | * JUMP_ALWAYS end |
| 5974 | * else: |
| 5975 | * ... body ... |
| 5976 | * end: |
| 5977 | */ |
| 5978 | static char_u * |
| 5979 | compile_if(char_u *arg, cctx_T *cctx) |
| 5980 | { |
| 5981 | char_u *p = arg; |
| 5982 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5983 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5984 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5985 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5986 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5987 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5988 | CLEAR_FIELD(ppconst); |
| 5989 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5990 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5991 | clear_ppconst(&ppconst); |
| 5992 | return NULL; |
| 5993 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5994 | if (cctx->ctx_skip == SKIP_YES) |
| 5995 | clear_ppconst(&ppconst); |
| 5996 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5997 | { |
| 5998 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5999 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6000 | clear_ppconst(&ppconst); |
| 6001 | } |
| 6002 | else |
| 6003 | { |
| 6004 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6005 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6006 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6007 | return NULL; |
| 6008 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6009 | |
| 6010 | scope = new_scope(cctx, IF_SCOPE); |
| 6011 | if (scope == NULL) |
| 6012 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6013 | scope->se_skip_save = skip_save; |
| 6014 | // "is_had_return" will be reset if any block does not end in :return |
| 6015 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6016 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6017 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6018 | { |
| 6019 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6020 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6021 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6022 | } |
| 6023 | else |
| 6024 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6025 | |
| 6026 | return p; |
| 6027 | } |
| 6028 | |
| 6029 | static char_u * |
| 6030 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 6031 | { |
| 6032 | char_u *p = arg; |
| 6033 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6034 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6035 | isn_T *isn; |
| 6036 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6037 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6038 | |
| 6039 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6040 | { |
| 6041 | emsg(_(e_elseif_without_if)); |
| 6042 | return NULL; |
| 6043 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6044 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6045 | if (!cctx->ctx_had_return) |
| 6046 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6047 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6048 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6049 | { |
| 6050 | 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] | 6051 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6052 | return NULL; |
| 6053 | // previous "if" or "elseif" jumps here |
| 6054 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6055 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6056 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6057 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6058 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6059 | CLEAR_FIELD(ppconst); |
| 6060 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6061 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6062 | clear_ppconst(&ppconst); |
| 6063 | return NULL; |
| 6064 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6065 | if (scope->se_skip_save == SKIP_YES) |
| 6066 | clear_ppconst(&ppconst); |
| 6067 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6068 | { |
| 6069 | // The expression results in a constant. |
| 6070 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6071 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6072 | clear_ppconst(&ppconst); |
| 6073 | scope->se_u.se_if.is_if_label = -1; |
| 6074 | } |
| 6075 | else |
| 6076 | { |
| 6077 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6078 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6079 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6080 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6081 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6082 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6083 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6084 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6085 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6086 | |
| 6087 | return p; |
| 6088 | } |
| 6089 | |
| 6090 | static char_u * |
| 6091 | compile_else(char_u *arg, cctx_T *cctx) |
| 6092 | { |
| 6093 | char_u *p = arg; |
| 6094 | garray_T *instr = &cctx->ctx_instr; |
| 6095 | isn_T *isn; |
| 6096 | scope_T *scope = cctx->ctx_scope; |
| 6097 | |
| 6098 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6099 | { |
| 6100 | emsg(_(e_else_without_if)); |
| 6101 | return NULL; |
| 6102 | } |
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 | scope->se_u.se_if.is_had_return = FALSE; |
| 6106 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6107 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6108 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6109 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6110 | // 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] | 6111 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6112 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6113 | if (!cctx->ctx_had_return |
| 6114 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6115 | JUMP_ALWAYS, cctx) == FAIL) |
| 6116 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6117 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6118 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6119 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6120 | { |
| 6121 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6122 | { |
| 6123 | // previous "if" or "elseif" jumps here |
| 6124 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6125 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6126 | scope->se_u.se_if.is_if_label = -1; |
| 6127 | } |
| 6128 | } |
| 6129 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6130 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6131 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6132 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6133 | |
| 6134 | return p; |
| 6135 | } |
| 6136 | |
| 6137 | static char_u * |
| 6138 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6139 | { |
| 6140 | scope_T *scope = cctx->ctx_scope; |
| 6141 | ifscope_T *ifscope; |
| 6142 | garray_T *instr = &cctx->ctx_instr; |
| 6143 | isn_T *isn; |
| 6144 | |
| 6145 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6146 | { |
| 6147 | emsg(_(e_endif_without_if)); |
| 6148 | return NULL; |
| 6149 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6150 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6151 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6152 | if (!cctx->ctx_had_return) |
| 6153 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6154 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6155 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6156 | { |
| 6157 | // previous "if" or "elseif" jumps here |
| 6158 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6159 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6160 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6161 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6162 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6163 | cctx->ctx_skip = scope->se_skip_save; |
| 6164 | |
| 6165 | // If all the blocks end in :return and there is an :else then the |
| 6166 | // had_return flag is set. |
| 6167 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6168 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6169 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6170 | return arg; |
| 6171 | } |
| 6172 | |
| 6173 | /* |
| 6174 | * compile "for var in expr" |
| 6175 | * |
| 6176 | * Produces instructions: |
| 6177 | * PUSHNR -1 |
| 6178 | * STORE loop-idx Set index to -1 |
| 6179 | * EVAL expr Push result of "expr" |
| 6180 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6181 | * - if beyond end, jump to "end" |
| 6182 | * - otherwise get item from list and push it |
| 6183 | * STORE var Store item in "var" |
| 6184 | * ... body ... |
| 6185 | * JUMP top Jump back to repeat |
| 6186 | * end: DROP Drop the result of "expr" |
| 6187 | * |
| 6188 | */ |
| 6189 | static char_u * |
| 6190 | compile_for(char_u *arg, cctx_T *cctx) |
| 6191 | { |
| 6192 | char_u *p; |
| 6193 | size_t varlen; |
| 6194 | garray_T *instr = &cctx->ctx_instr; |
| 6195 | garray_T *stack = &cctx->ctx_type_stack; |
| 6196 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6197 | lvar_T *loop_lvar; // loop iteration variable |
| 6198 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6199 | type_T *vartype; |
| 6200 | |
| 6201 | // TODO: list of variables: "for [key, value] in dict" |
| 6202 | // parse "var" |
| 6203 | for (p = arg; eval_isnamec1(*p); ++p) |
| 6204 | ; |
| 6205 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6206 | var_lvar = lookup_local(arg, varlen, cctx); |
| 6207 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6208 | { |
| 6209 | semsg(_("E1023: variable already defined: %s"), arg); |
| 6210 | return NULL; |
| 6211 | } |
| 6212 | |
| 6213 | // consume "in" |
| 6214 | p = skipwhite(p); |
| 6215 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 6216 | { |
| 6217 | emsg(_(e_missing_in)); |
| 6218 | return NULL; |
| 6219 | } |
| 6220 | p = skipwhite(p + 2); |
| 6221 | |
| 6222 | |
| 6223 | scope = new_scope(cctx, FOR_SCOPE); |
| 6224 | if (scope == NULL) |
| 6225 | return NULL; |
| 6226 | |
| 6227 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6228 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6229 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6230 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6231 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6232 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6233 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6234 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6235 | |
| 6236 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6237 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 6238 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6239 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6240 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6241 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6242 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6243 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6244 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6245 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6246 | |
| 6247 | // compile "expr", it remains on the stack until "endfor" |
| 6248 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6249 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6250 | { |
| 6251 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6252 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6253 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6254 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6255 | // Now that we know the type of "var", check that it is a list, now or at |
| 6256 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6257 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 6258 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6259 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6260 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6261 | return NULL; |
| 6262 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6263 | 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] | 6264 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6265 | |
| 6266 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6267 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6268 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6269 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 6270 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6271 | |
| 6272 | return arg; |
| 6273 | } |
| 6274 | |
| 6275 | /* |
| 6276 | * compile "endfor" |
| 6277 | */ |
| 6278 | static char_u * |
| 6279 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 6280 | { |
| 6281 | garray_T *instr = &cctx->ctx_instr; |
| 6282 | scope_T *scope = cctx->ctx_scope; |
| 6283 | forscope_T *forscope; |
| 6284 | isn_T *isn; |
| 6285 | |
| 6286 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 6287 | { |
| 6288 | emsg(_(e_for)); |
| 6289 | return NULL; |
| 6290 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6291 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6292 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6293 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6294 | |
| 6295 | // At end of ":for" scope jump back to the FOR instruction. |
| 6296 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 6297 | |
| 6298 | // Fill in the "end" label in the FOR statement so it can jump here |
| 6299 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 6300 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 6301 | |
| 6302 | // Fill in the "end" label any BREAK statements |
| 6303 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 6304 | |
| 6305 | // Below the ":for" scope drop the "expr" list from the stack. |
| 6306 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 6307 | return NULL; |
| 6308 | |
| 6309 | vim_free(scope); |
| 6310 | |
| 6311 | return arg; |
| 6312 | } |
| 6313 | |
| 6314 | /* |
| 6315 | * compile "while expr" |
| 6316 | * |
| 6317 | * Produces instructions: |
| 6318 | * top: EVAL expr Push result of "expr" |
| 6319 | * JUMP_IF_FALSE end jump if false |
| 6320 | * ... body ... |
| 6321 | * JUMP top Jump back to repeat |
| 6322 | * end: |
| 6323 | * |
| 6324 | */ |
| 6325 | static char_u * |
| 6326 | compile_while(char_u *arg, cctx_T *cctx) |
| 6327 | { |
| 6328 | char_u *p = arg; |
| 6329 | garray_T *instr = &cctx->ctx_instr; |
| 6330 | scope_T *scope; |
| 6331 | |
| 6332 | scope = new_scope(cctx, WHILE_SCOPE); |
| 6333 | if (scope == NULL) |
| 6334 | return NULL; |
| 6335 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6336 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6337 | |
| 6338 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6339 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6340 | return NULL; |
| 6341 | |
| 6342 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6343 | 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] | 6344 | JUMP_IF_FALSE, cctx) == FAIL) |
| 6345 | return FAIL; |
| 6346 | |
| 6347 | return p; |
| 6348 | } |
| 6349 | |
| 6350 | /* |
| 6351 | * compile "endwhile" |
| 6352 | */ |
| 6353 | static char_u * |
| 6354 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 6355 | { |
| 6356 | scope_T *scope = cctx->ctx_scope; |
| 6357 | |
| 6358 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 6359 | { |
| 6360 | emsg(_(e_while)); |
| 6361 | return NULL; |
| 6362 | } |
| 6363 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6364 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6365 | |
| 6366 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6367 | 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] | 6368 | |
| 6369 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 6370 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6371 | 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] | 6372 | |
| 6373 | vim_free(scope); |
| 6374 | |
| 6375 | return arg; |
| 6376 | } |
| 6377 | |
| 6378 | /* |
| 6379 | * compile "continue" |
| 6380 | */ |
| 6381 | static char_u * |
| 6382 | compile_continue(char_u *arg, cctx_T *cctx) |
| 6383 | { |
| 6384 | scope_T *scope = cctx->ctx_scope; |
| 6385 | |
| 6386 | for (;;) |
| 6387 | { |
| 6388 | if (scope == NULL) |
| 6389 | { |
| 6390 | emsg(_(e_continue)); |
| 6391 | return NULL; |
| 6392 | } |
| 6393 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6394 | break; |
| 6395 | scope = scope->se_outer; |
| 6396 | } |
| 6397 | |
| 6398 | // Jump back to the FOR or WHILE instruction. |
| 6399 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6400 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 6401 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6402 | return arg; |
| 6403 | } |
| 6404 | |
| 6405 | /* |
| 6406 | * compile "break" |
| 6407 | */ |
| 6408 | static char_u * |
| 6409 | compile_break(char_u *arg, cctx_T *cctx) |
| 6410 | { |
| 6411 | scope_T *scope = cctx->ctx_scope; |
| 6412 | endlabel_T **el; |
| 6413 | |
| 6414 | for (;;) |
| 6415 | { |
| 6416 | if (scope == NULL) |
| 6417 | { |
| 6418 | emsg(_(e_break)); |
| 6419 | return NULL; |
| 6420 | } |
| 6421 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6422 | break; |
| 6423 | scope = scope->se_outer; |
| 6424 | } |
| 6425 | |
| 6426 | // Jump to the end of the FOR or WHILE loop. |
| 6427 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6428 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6429 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6430 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6431 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 6432 | return FAIL; |
| 6433 | |
| 6434 | return arg; |
| 6435 | } |
| 6436 | |
| 6437 | /* |
| 6438 | * compile "{" start of block |
| 6439 | */ |
| 6440 | static char_u * |
| 6441 | compile_block(char_u *arg, cctx_T *cctx) |
| 6442 | { |
| 6443 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6444 | return NULL; |
| 6445 | return skipwhite(arg + 1); |
| 6446 | } |
| 6447 | |
| 6448 | /* |
| 6449 | * compile end of block: drop one scope |
| 6450 | */ |
| 6451 | static void |
| 6452 | compile_endblock(cctx_T *cctx) |
| 6453 | { |
| 6454 | scope_T *scope = cctx->ctx_scope; |
| 6455 | |
| 6456 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6457 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6458 | vim_free(scope); |
| 6459 | } |
| 6460 | |
| 6461 | /* |
| 6462 | * compile "try" |
| 6463 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6464 | * finally. |
| 6465 | * Creates another scope for the "try" block itself. |
| 6466 | * TRY instruction sets up exception handling at runtime. |
| 6467 | * |
| 6468 | * "try" |
| 6469 | * TRY -> catch1, -> finally push trystack entry |
| 6470 | * ... try block |
| 6471 | * "throw {exception}" |
| 6472 | * EVAL {exception} |
| 6473 | * THROW create exception |
| 6474 | * ... try block |
| 6475 | * " catch {expr}" |
| 6476 | * JUMP -> finally |
| 6477 | * catch1: PUSH exeception |
| 6478 | * EVAL {expr} |
| 6479 | * MATCH |
| 6480 | * JUMP nomatch -> catch2 |
| 6481 | * CATCH remove exception |
| 6482 | * ... catch block |
| 6483 | * " catch" |
| 6484 | * JUMP -> finally |
| 6485 | * catch2: CATCH remove exception |
| 6486 | * ... catch block |
| 6487 | * " finally" |
| 6488 | * finally: |
| 6489 | * ... finally block |
| 6490 | * " endtry" |
| 6491 | * ENDTRY pop trystack entry, may rethrow |
| 6492 | */ |
| 6493 | static char_u * |
| 6494 | compile_try(char_u *arg, cctx_T *cctx) |
| 6495 | { |
| 6496 | garray_T *instr = &cctx->ctx_instr; |
| 6497 | scope_T *try_scope; |
| 6498 | scope_T *scope; |
| 6499 | |
| 6500 | // scope that holds the jumps that go to catch/finally/endtry |
| 6501 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6502 | if (try_scope == NULL) |
| 6503 | return NULL; |
| 6504 | |
| 6505 | // "catch" is set when the first ":catch" is found. |
| 6506 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6507 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6508 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6509 | return NULL; |
| 6510 | |
| 6511 | // scope for the try block itself |
| 6512 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6513 | if (scope == NULL) |
| 6514 | return NULL; |
| 6515 | |
| 6516 | return arg; |
| 6517 | } |
| 6518 | |
| 6519 | /* |
| 6520 | * compile "catch {expr}" |
| 6521 | */ |
| 6522 | static char_u * |
| 6523 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6524 | { |
| 6525 | scope_T *scope = cctx->ctx_scope; |
| 6526 | garray_T *instr = &cctx->ctx_instr; |
| 6527 | char_u *p; |
| 6528 | isn_T *isn; |
| 6529 | |
| 6530 | // end block scope from :try or :catch |
| 6531 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6532 | compile_endblock(cctx); |
| 6533 | scope = cctx->ctx_scope; |
| 6534 | |
| 6535 | // Error if not in a :try scope |
| 6536 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6537 | { |
| 6538 | emsg(_(e_catch)); |
| 6539 | return NULL; |
| 6540 | } |
| 6541 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6542 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6543 | { |
| 6544 | emsg(_("E1033: catch unreachable after catch-all")); |
| 6545 | return NULL; |
| 6546 | } |
| 6547 | |
| 6548 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6549 | 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] | 6550 | JUMP_ALWAYS, cctx) == FAIL) |
| 6551 | return NULL; |
| 6552 | |
| 6553 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6554 | 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] | 6555 | if (isn->isn_arg.try.try_catch == 0) |
| 6556 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6557 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6558 | { |
| 6559 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6560 | 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] | 6561 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6562 | } |
| 6563 | |
| 6564 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6565 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6566 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6567 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6568 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6569 | } |
| 6570 | else |
| 6571 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6572 | char_u *end; |
| 6573 | char_u *pat; |
| 6574 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6575 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6576 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6577 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6578 | // Push v:exception, push {expr} and MATCH |
| 6579 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6580 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6581 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6582 | if (*end != *p) |
| 6583 | { |
| 6584 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 6585 | vim_free(tofree); |
| 6586 | return FAIL; |
| 6587 | } |
| 6588 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6589 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6590 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6591 | len = (int)(end - tofree); |
| 6592 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6593 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6594 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6595 | if (pat == NULL) |
| 6596 | return FAIL; |
| 6597 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6598 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6599 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6600 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6601 | return NULL; |
| 6602 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6603 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6604 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6605 | return NULL; |
| 6606 | } |
| 6607 | |
| 6608 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6609 | return NULL; |
| 6610 | |
| 6611 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6612 | return NULL; |
| 6613 | return p; |
| 6614 | } |
| 6615 | |
| 6616 | static char_u * |
| 6617 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6618 | { |
| 6619 | scope_T *scope = cctx->ctx_scope; |
| 6620 | garray_T *instr = &cctx->ctx_instr; |
| 6621 | isn_T *isn; |
| 6622 | |
| 6623 | // end block scope from :try or :catch |
| 6624 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6625 | compile_endblock(cctx); |
| 6626 | scope = cctx->ctx_scope; |
| 6627 | |
| 6628 | // Error if not in a :try scope |
| 6629 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6630 | { |
| 6631 | emsg(_(e_finally)); |
| 6632 | return NULL; |
| 6633 | } |
| 6634 | |
| 6635 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6636 | 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] | 6637 | if (isn->isn_arg.try.try_finally != 0) |
| 6638 | { |
| 6639 | emsg(_(e_finally_dup)); |
| 6640 | return NULL; |
| 6641 | } |
| 6642 | |
| 6643 | // 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] | 6644 | 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] | 6645 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6646 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6647 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6648 | { |
| 6649 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6650 | 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] | 6651 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6652 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6653 | } |
| 6654 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6655 | // TODO: set index in ts_finally_label jumps |
| 6656 | |
| 6657 | return arg; |
| 6658 | } |
| 6659 | |
| 6660 | static char_u * |
| 6661 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6662 | { |
| 6663 | scope_T *scope = cctx->ctx_scope; |
| 6664 | garray_T *instr = &cctx->ctx_instr; |
| 6665 | isn_T *isn; |
| 6666 | |
| 6667 | // end block scope from :catch or :finally |
| 6668 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6669 | compile_endblock(cctx); |
| 6670 | scope = cctx->ctx_scope; |
| 6671 | |
| 6672 | // Error if not in a :try scope |
| 6673 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6674 | { |
| 6675 | if (scope == NULL) |
| 6676 | emsg(_(e_no_endtry)); |
| 6677 | else if (scope->se_type == WHILE_SCOPE) |
| 6678 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6679 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6680 | emsg(_(e_endfor)); |
| 6681 | else |
| 6682 | emsg(_(e_endif)); |
| 6683 | return NULL; |
| 6684 | } |
| 6685 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6686 | 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] | 6687 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6688 | { |
| 6689 | emsg(_("E1032: missing :catch or :finally")); |
| 6690 | return NULL; |
| 6691 | } |
| 6692 | |
| 6693 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6694 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6695 | 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] | 6696 | |
| 6697 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6698 | if (isn->isn_arg.try.try_catch == 0) |
| 6699 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6700 | if (isn->isn_arg.try.try_finally == 0) |
| 6701 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6702 | |
| 6703 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6704 | { |
| 6705 | // Last catch without match jumps here |
| 6706 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6707 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6708 | } |
| 6709 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6710 | compile_endblock(cctx); |
| 6711 | |
| 6712 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6713 | return NULL; |
| 6714 | return arg; |
| 6715 | } |
| 6716 | |
| 6717 | /* |
| 6718 | * compile "throw {expr}" |
| 6719 | */ |
| 6720 | static char_u * |
| 6721 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6722 | { |
| 6723 | char_u *p = skipwhite(arg); |
| 6724 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6725 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6726 | return NULL; |
| 6727 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6728 | return NULL; |
| 6729 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6730 | return NULL; |
| 6731 | |
| 6732 | return p; |
| 6733 | } |
| 6734 | |
| 6735 | /* |
| 6736 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6737 | * compile "echomsg expr" |
| 6738 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6739 | * compile "execute expr" |
| 6740 | */ |
| 6741 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6742 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6743 | { |
| 6744 | char_u *p = arg; |
| 6745 | int count = 0; |
| 6746 | |
| 6747 | for (;;) |
| 6748 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6749 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6750 | return NULL; |
| 6751 | ++count; |
| 6752 | p = skipwhite(p); |
| 6753 | if (ends_excmd(*p)) |
| 6754 | break; |
| 6755 | } |
| 6756 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6757 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6758 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6759 | else if (cmdidx == CMD_execute) |
| 6760 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6761 | else if (cmdidx == CMD_echomsg) |
| 6762 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6763 | else |
| 6764 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6765 | return p; |
| 6766 | } |
| 6767 | |
| 6768 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6769 | * A command that is not compiled, execute with legacy code. |
| 6770 | */ |
| 6771 | static char_u * |
| 6772 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6773 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6774 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6775 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6776 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6777 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6778 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6779 | goto theend; |
| 6780 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6781 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6782 | { |
| 6783 | long argt = excmd_get_argt(eap->cmdidx); |
| 6784 | int usefilter = FALSE; |
| 6785 | |
| 6786 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6787 | |
| 6788 | // If the command can be followed by a bar, find the bar and truncate |
| 6789 | // it, so that the following command can be compiled. |
| 6790 | // The '|' is overwritten with a NUL, it is put back below. |
| 6791 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6792 | && *eap->arg == '!') |
| 6793 | // :w !filter or :r !filter or :r! filter |
| 6794 | usefilter = TRUE; |
| 6795 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6796 | { |
| 6797 | separate_nextcmd(eap); |
| 6798 | if (eap->nextcmd != NULL) |
| 6799 | nextcmd = eap->nextcmd; |
| 6800 | } |
| 6801 | } |
| 6802 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6803 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6804 | { |
| 6805 | // expand filename in "syntax include [@group] filename" |
| 6806 | has_expr = TRUE; |
| 6807 | eap->arg = skipwhite(eap->arg + 7); |
| 6808 | if (*eap->arg == '@') |
| 6809 | eap->arg = skiptowhite(eap->arg); |
| 6810 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6811 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6812 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6813 | { |
| 6814 | int count = 0; |
| 6815 | char_u *start = skipwhite(line); |
| 6816 | |
| 6817 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6818 | // PUSHS ":cmd xxx" |
| 6819 | // eval expr1 |
| 6820 | // PUSHS "yyy" |
| 6821 | // eval expr2 |
| 6822 | // PUSHS "zzz" |
| 6823 | // EXECCONCAT 5 |
| 6824 | for (;;) |
| 6825 | { |
| 6826 | if (p > start) |
| 6827 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6828 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6829 | ++count; |
| 6830 | } |
| 6831 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6832 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6833 | return NULL; |
| 6834 | may_generate_2STRING(-1, cctx); |
| 6835 | ++count; |
| 6836 | p = skipwhite(p); |
| 6837 | if (*p != '`') |
| 6838 | { |
| 6839 | emsg(_("E1083: missing backtick")); |
| 6840 | return NULL; |
| 6841 | } |
| 6842 | start = p + 1; |
| 6843 | |
| 6844 | p = (char_u *)strstr((char *)start, "`="); |
| 6845 | if (p == NULL) |
| 6846 | { |
| 6847 | if (*skipwhite(start) != NUL) |
| 6848 | { |
| 6849 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6850 | ++count; |
| 6851 | } |
| 6852 | break; |
| 6853 | } |
| 6854 | } |
| 6855 | generate_EXECCONCAT(cctx, count); |
| 6856 | } |
| 6857 | else |
| 6858 | generate_EXEC(cctx, line); |
| 6859 | |
| 6860 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6861 | if (*nextcmd != NUL) |
| 6862 | { |
| 6863 | // the parser expects a pointer to the bar, put it back |
| 6864 | --nextcmd; |
| 6865 | *nextcmd = '|'; |
| 6866 | } |
| 6867 | |
| 6868 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6869 | } |
| 6870 | |
| 6871 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6872 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6873 | * 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] | 6874 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6875 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6876 | add_def_function(ufunc_T *ufunc) |
| 6877 | { |
| 6878 | dfunc_T *dfunc; |
| 6879 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6880 | if (def_functions.ga_len == 0) |
| 6881 | { |
| 6882 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6883 | // wasn't set. |
| 6884 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6885 | return FAIL; |
| 6886 | ++def_functions.ga_len; |
| 6887 | } |
| 6888 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6889 | // Add the function to "def_functions". |
| 6890 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6891 | return FAIL; |
| 6892 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6893 | CLEAR_POINTER(dfunc); |
| 6894 | dfunc->df_idx = def_functions.ga_len; |
| 6895 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6896 | dfunc->df_ufunc = ufunc; |
| 6897 | ++def_functions.ga_len; |
| 6898 | return OK; |
| 6899 | } |
| 6900 | |
| 6901 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6902 | * After ex_function() has collected all the function lines: parse and compile |
| 6903 | * the lines into instructions. |
| 6904 | * Adds the function to "def_functions". |
| 6905 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6906 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6907 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6908 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6909 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6910 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6911 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6912 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6913 | 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] | 6914 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6915 | char_u *line = NULL; |
| 6916 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6917 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6918 | cctx_T cctx; |
| 6919 | garray_T *instr; |
| 6920 | int called_emsg_before = called_emsg; |
| 6921 | int ret = FAIL; |
| 6922 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6923 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6924 | int emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6925 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6926 | // When using a function that was compiled before: Free old instructions. |
| 6927 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6928 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6929 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6930 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6931 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6932 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6933 | } |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6934 | else if (add_def_function(ufunc) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6935 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6936 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6937 | ufunc->uf_def_status = UF_COMPILING; |
| 6938 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6939 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6940 | cctx.ctx_ufunc = ufunc; |
| 6941 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6942 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6943 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6944 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6945 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6946 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6947 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6948 | instr = &cctx.ctx_instr; |
| 6949 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6950 | // Set the context to the function, it may be compiled when called from |
| 6951 | // another script. Set the script version to the most modern one. |
| 6952 | // The line number will be set in next_line_from_context(). |
| 6953 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6954 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6955 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6956 | // Make sure error messages are OK. |
| 6957 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6958 | if (do_estack_push) |
| 6959 | estack_push_ufunc(ufunc, 1); |
| 6960 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6961 | if (ufunc->uf_def_args.ga_len > 0) |
| 6962 | { |
| 6963 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6964 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6965 | int i; |
| 6966 | char_u *arg; |
| 6967 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 6968 | |
| 6969 | // Produce instructions for the default values of optional arguments. |
| 6970 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6971 | // where to start when the function is called, depending on the number |
| 6972 | // of arguments. |
| 6973 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6974 | if (ufunc->uf_def_arg_idx == NULL) |
| 6975 | goto erret; |
| 6976 | for (i = 0; i < count; ++i) |
| 6977 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6978 | garray_T *stack = &cctx.ctx_type_stack; |
| 6979 | type_T *val_type; |
| 6980 | int arg_idx = first_def_arg + i; |
| 6981 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6982 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6983 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6984 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6985 | goto erret; |
| 6986 | |
| 6987 | // If no type specified use the type of the default value. |
| 6988 | // Otherwise check that the default value type matches the |
| 6989 | // specified type. |
| 6990 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6991 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
| 6992 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 6993 | 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] | 6994 | == FAIL) |
| 6995 | { |
| 6996 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 6997 | arg_idx + 1); |
| 6998 | goto erret; |
| 6999 | } |
| 7000 | |
| 7001 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7002 | goto erret; |
| 7003 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7004 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
| 7005 | } |
| 7006 | |
| 7007 | /* |
| 7008 | * Loop over all the lines of the function and generate instructions. |
| 7009 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7010 | for (;;) |
| 7011 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7012 | exarg_T ea; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7013 | int starts_with_colon = FALSE; |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7014 | char_u *cmd; |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7015 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7016 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7017 | // Bail out on the first error to avoid a flood of errors and report |
| 7018 | // the right line number when inside try/catch. |
| 7019 | if (emsg_before != called_emsg) |
| 7020 | goto erret; |
| 7021 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7022 | if (line != NULL && *line == '|') |
| 7023 | // the line continues after a '|' |
| 7024 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 7025 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7026 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 7027 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7028 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7029 | semsg(_("E488: Trailing characters: %s"), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7030 | goto erret; |
| 7031 | } |
| 7032 | else |
| 7033 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 7034 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 7035 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7036 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7037 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7038 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7039 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7040 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7041 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7042 | ea.cmdlinep = &line; |
| 7043 | ea.cmd = skipwhite(line); |
| 7044 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7045 | // Some things can be recognized by the first character. |
| 7046 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7047 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7048 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7049 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7050 | if (ea.cmd[1] != '{') |
| 7051 | { |
| 7052 | line = (char_u *)""; |
| 7053 | continue; |
| 7054 | } |
| 7055 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7056 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7057 | case '}': |
| 7058 | { |
| 7059 | // "}" ends a block scope |
| 7060 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7061 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7062 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7063 | if (stype == BLOCK_SCOPE) |
| 7064 | { |
| 7065 | compile_endblock(&cctx); |
| 7066 | line = ea.cmd; |
| 7067 | } |
| 7068 | else |
| 7069 | { |
| 7070 | emsg(_("E1025: using } outside of a block scope")); |
| 7071 | goto erret; |
| 7072 | } |
| 7073 | if (line != NULL) |
| 7074 | line = skipwhite(ea.cmd + 1); |
| 7075 | continue; |
| 7076 | } |
| 7077 | |
| 7078 | case '{': |
| 7079 | // "{" starts a block scope |
| 7080 | // "{'a': 1}->func() is something else |
| 7081 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7082 | { |
| 7083 | line = compile_block(ea.cmd, &cctx); |
| 7084 | continue; |
| 7085 | } |
| 7086 | break; |
| 7087 | |
| 7088 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7089 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7090 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7091 | } |
| 7092 | |
| 7093 | /* |
| 7094 | * COMMAND MODIFIERS |
| 7095 | */ |
| 7096 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 7097 | { |
| 7098 | if (errormsg != NULL) |
| 7099 | goto erret; |
| 7100 | // empty line or comment |
| 7101 | line = (char_u *)""; |
| 7102 | continue; |
| 7103 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7104 | // TODO: use modifiers in the command |
| 7105 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | b074e8b | 2020-07-11 13:40:45 +0200 | [diff] [blame] | 7106 | CLEAR_FIELD(cmdmod); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7107 | |
| 7108 | // Skip ":call" to get to the function name. |
| 7109 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 7110 | ea.cmd = skipwhite(ea.cmd); |
| 7111 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7112 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7113 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7114 | char_u *pskip; |
| 7115 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7116 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7117 | // find what follows. |
| 7118 | // Skip over "var.member", "var[idx]" and the like. |
| 7119 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 7120 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7121 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7122 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7123 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7124 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7125 | char_u *var_end; |
| 7126 | int oplen; |
| 7127 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7128 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7129 | var_end = find_name_end(pskip, NULL, NULL, |
| 7130 | FNE_CHECK_START | FNE_INCL_BR); |
| 7131 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7132 | if (oplen > 0) |
| 7133 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7134 | size_t len = p - ea.cmd; |
| 7135 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7136 | // Recognize an assignment if we recognize the variable |
| 7137 | // name: |
| 7138 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 7139 | // "local = expr" where "local" is a local var. |
| 7140 | // "script = expr" where "script" is a script-local var. |
| 7141 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7142 | // "&opt = expr" |
| 7143 | // "$ENV = expr" |
| 7144 | // "@r = expr" |
| 7145 | if (*ea.cmd == '&' |
| 7146 | || *ea.cmd == '$' |
| 7147 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7148 | || ((len) > 2 && ea.cmd[1] == ':') |
| 7149 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 7150 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 7151 | NULL, &cctx) == OK |
| 7152 | || lookup_script(ea.cmd, len) == OK |
| 7153 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7154 | { |
| 7155 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7156 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7157 | goto erret; |
| 7158 | continue; |
| 7159 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7160 | } |
| 7161 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7162 | |
| 7163 | if (*ea.cmd == '[') |
| 7164 | { |
| 7165 | // [var, var] = expr |
| 7166 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 7167 | if (line == NULL) |
| 7168 | goto erret; |
| 7169 | if (line != ea.cmd) |
| 7170 | continue; |
| 7171 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7172 | } |
| 7173 | |
| 7174 | /* |
| 7175 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7176 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7177 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7178 | cmd = ea.cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7179 | if (*cmd != '\'') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7180 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7181 | ea.cmd = skip_range(ea.cmd, NULL); |
| 7182 | if (ea.cmd > cmd && !starts_with_colon) |
| 7183 | { |
| 7184 | emsg(_(e_colon_required)); |
| 7185 | goto erret; |
| 7186 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7187 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7188 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7189 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7190 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7191 | |
| 7192 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 7193 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7194 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7195 | { |
| 7196 | line += STRLEN(line); |
| 7197 | continue; |
| 7198 | } |
| 7199 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7200 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7201 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7202 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7203 | // CMD_let cannot happen, compile_assignment() above is used |
| 7204 | iemsg("Command from find_ex_command() not handled"); |
| 7205 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7206 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7207 | } |
| 7208 | |
| 7209 | p = skipwhite(p); |
| 7210 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7211 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 7212 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7213 | && ea.cmdidx != CMD_elseif |
| 7214 | && ea.cmdidx != CMD_else |
| 7215 | && ea.cmdidx != CMD_endif) |
| 7216 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7217 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7218 | continue; |
| 7219 | } |
| 7220 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7221 | if (ea.cmdidx != CMD_elseif |
| 7222 | && ea.cmdidx != CMD_else |
| 7223 | && ea.cmdidx != CMD_endif |
| 7224 | && ea.cmdidx != CMD_endfor |
| 7225 | && ea.cmdidx != CMD_endwhile |
| 7226 | && ea.cmdidx != CMD_catch |
| 7227 | && ea.cmdidx != CMD_finally |
| 7228 | && ea.cmdidx != CMD_endtry) |
| 7229 | { |
| 7230 | if (cctx.ctx_had_return) |
| 7231 | { |
| 7232 | emsg(_("E1095: Unreachable code after :return")); |
| 7233 | goto erret; |
| 7234 | } |
| 7235 | } |
| 7236 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7237 | switch (ea.cmdidx) |
| 7238 | { |
| 7239 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7240 | ea.arg = p; |
| 7241 | line = compile_nested_function(&ea, &cctx); |
| 7242 | break; |
| 7243 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7244 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7245 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7246 | goto erret; |
| 7247 | |
| 7248 | case CMD_return: |
| 7249 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7250 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7251 | break; |
| 7252 | |
| 7253 | case CMD_let: |
| 7254 | case CMD_const: |
| 7255 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7256 | if (line == p) |
| 7257 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7258 | break; |
| 7259 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7260 | case CMD_unlet: |
| 7261 | case CMD_unlockvar: |
| 7262 | case CMD_lockvar: |
| 7263 | line = compile_unletlock(p, &ea, &cctx); |
| 7264 | break; |
| 7265 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7266 | case CMD_import: |
| 7267 | line = compile_import(p, &cctx); |
| 7268 | break; |
| 7269 | |
| 7270 | case CMD_if: |
| 7271 | line = compile_if(p, &cctx); |
| 7272 | break; |
| 7273 | case CMD_elseif: |
| 7274 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7275 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7276 | break; |
| 7277 | case CMD_else: |
| 7278 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7279 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7280 | break; |
| 7281 | case CMD_endif: |
| 7282 | line = compile_endif(p, &cctx); |
| 7283 | break; |
| 7284 | |
| 7285 | case CMD_while: |
| 7286 | line = compile_while(p, &cctx); |
| 7287 | break; |
| 7288 | case CMD_endwhile: |
| 7289 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7290 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7291 | break; |
| 7292 | |
| 7293 | case CMD_for: |
| 7294 | line = compile_for(p, &cctx); |
| 7295 | break; |
| 7296 | case CMD_endfor: |
| 7297 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7298 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7299 | break; |
| 7300 | case CMD_continue: |
| 7301 | line = compile_continue(p, &cctx); |
| 7302 | break; |
| 7303 | case CMD_break: |
| 7304 | line = compile_break(p, &cctx); |
| 7305 | break; |
| 7306 | |
| 7307 | case CMD_try: |
| 7308 | line = compile_try(p, &cctx); |
| 7309 | break; |
| 7310 | case CMD_catch: |
| 7311 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7312 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7313 | break; |
| 7314 | case CMD_finally: |
| 7315 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7316 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7317 | break; |
| 7318 | case CMD_endtry: |
| 7319 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7320 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7321 | break; |
| 7322 | case CMD_throw: |
| 7323 | line = compile_throw(p, &cctx); |
| 7324 | break; |
| 7325 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7326 | case CMD_eval: |
| 7327 | if (compile_expr0(&p, &cctx) == FAIL) |
| 7328 | goto erret; |
| 7329 | |
| 7330 | // drop the return value |
| 7331 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 7332 | |
| 7333 | line = skipwhite(p); |
| 7334 | break; |
| 7335 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7336 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7337 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7338 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7339 | case CMD_echomsg: |
| 7340 | case CMD_echoerr: |
| 7341 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7342 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7343 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7344 | // TODO: other commands with an expression argument |
| 7345 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7346 | case CMD_SIZE: |
| 7347 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 7348 | goto erret; |
| 7349 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7350 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7351 | // Not recognized, execute with do_cmdline_cmd(). |
| 7352 | ea.arg = p; |
| 7353 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7354 | break; |
| 7355 | } |
| 7356 | if (line == NULL) |
| 7357 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7358 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7359 | |
| 7360 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7361 | { |
| 7362 | iemsg("Type stack underflow"); |
| 7363 | goto erret; |
| 7364 | } |
| 7365 | } |
| 7366 | |
| 7367 | if (cctx.ctx_scope != NULL) |
| 7368 | { |
| 7369 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7370 | emsg(_(e_endif)); |
| 7371 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7372 | emsg(_(e_endwhile)); |
| 7373 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7374 | emsg(_(e_endfor)); |
| 7375 | else |
| 7376 | emsg(_("E1026: Missing }")); |
| 7377 | goto erret; |
| 7378 | } |
| 7379 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7380 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7381 | { |
| 7382 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7383 | { |
| 7384 | emsg(_("E1027: Missing return statement")); |
| 7385 | goto erret; |
| 7386 | } |
| 7387 | |
| 7388 | // Return zero if there is no return at the end. |
| 7389 | generate_PUSHNR(&cctx, 0); |
| 7390 | generate_instr(&cctx, ISN_RETURN); |
| 7391 | } |
| 7392 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7393 | { |
| 7394 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7395 | + ufunc->uf_dfunc_idx; |
| 7396 | dfunc->df_deleted = FALSE; |
| 7397 | dfunc->df_instr = instr->ga_data; |
| 7398 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7399 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 7400 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7401 | if (cctx.ctx_outer_used) |
| 7402 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7403 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7404 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7405 | |
| 7406 | ret = OK; |
| 7407 | |
| 7408 | erret: |
| 7409 | if (ret == FAIL) |
| 7410 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7411 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7412 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7413 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7414 | |
| 7415 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7416 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7417 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7418 | |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7419 | // if using the last entry in the table we might as well remove it |
| 7420 | if (!dfunc->df_deleted |
| 7421 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7422 | --def_functions.ga_len; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7423 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7424 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7425 | while (cctx.ctx_scope != NULL) |
| 7426 | drop_scope(&cctx); |
| 7427 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7428 | // Don't execute this function body. |
| 7429 | ga_clear_strings(&ufunc->uf_lines); |
| 7430 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7431 | if (errormsg != NULL) |
| 7432 | emsg(errormsg); |
| 7433 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 7434 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7435 | } |
| 7436 | |
| 7437 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7438 | if (do_estack_push) |
| 7439 | estack_pop(); |
| 7440 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7441 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7442 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7443 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7444 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7445 | } |
| 7446 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7447 | void |
| 7448 | set_function_type(ufunc_T *ufunc) |
| 7449 | { |
| 7450 | int varargs = ufunc->uf_va_name != NULL; |
| 7451 | int argcount = ufunc->uf_args.ga_len; |
| 7452 | |
| 7453 | // Create a type for the function, with the return type and any |
| 7454 | // argument types. |
| 7455 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7456 | // The type is included in "tt_args". |
| 7457 | if (argcount > 0 || varargs) |
| 7458 | { |
| 7459 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7460 | argcount, &ufunc->uf_type_list); |
| 7461 | // Add argument types to the function type. |
| 7462 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7463 | argcount + varargs, |
| 7464 | &ufunc->uf_type_list) == FAIL) |
| 7465 | return; |
| 7466 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7467 | ufunc->uf_func_type->tt_min_argcount = |
| 7468 | argcount - ufunc->uf_def_args.ga_len; |
| 7469 | if (ufunc->uf_arg_types == NULL) |
| 7470 | { |
| 7471 | int i; |
| 7472 | |
| 7473 | // lambda does not have argument types. |
| 7474 | for (i = 0; i < argcount; ++i) |
| 7475 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7476 | } |
| 7477 | else |
| 7478 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7479 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7480 | if (varargs) |
| 7481 | { |
| 7482 | ufunc->uf_func_type->tt_args[argcount] = |
| 7483 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7484 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7485 | } |
| 7486 | } |
| 7487 | else |
| 7488 | // No arguments, can use a predefined type. |
| 7489 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7490 | argcount, &ufunc->uf_type_list); |
| 7491 | } |
| 7492 | |
| 7493 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7494 | /* |
| 7495 | * Delete an instruction, free what it contains. |
| 7496 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7497 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7498 | delete_instr(isn_T *isn) |
| 7499 | { |
| 7500 | switch (isn->isn_type) |
| 7501 | { |
| 7502 | case ISN_EXEC: |
| 7503 | case ISN_LOADENV: |
| 7504 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7505 | case ISN_LOADB: |
| 7506 | case ISN_LOADW: |
| 7507 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7508 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7509 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7510 | case ISN_PUSHEXC: |
| 7511 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7512 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7513 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7514 | case ISN_STOREB: |
| 7515 | case ISN_STOREW: |
| 7516 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7517 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7518 | vim_free(isn->isn_arg.string); |
| 7519 | break; |
| 7520 | |
| 7521 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7522 | case ISN_STORES: |
| 7523 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7524 | break; |
| 7525 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7526 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7527 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7528 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7529 | break; |
| 7530 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7531 | case ISN_STOREOPT: |
| 7532 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7533 | break; |
| 7534 | |
| 7535 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7536 | blob_unref(isn->isn_arg.blob); |
| 7537 | break; |
| 7538 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7539 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7540 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7541 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7542 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7543 | break; |
| 7544 | |
| 7545 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7546 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7547 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7548 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7549 | break; |
| 7550 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7551 | case ISN_UCALL: |
| 7552 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7553 | break; |
| 7554 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7555 | case ISN_FUNCREF: |
| 7556 | { |
| 7557 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7558 | + isn->isn_arg.funcref.fr_func; |
| 7559 | func_ptr_unref(dfunc->df_ufunc); |
| 7560 | } |
| 7561 | break; |
| 7562 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7563 | case ISN_2BOOL: |
| 7564 | case ISN_2STRING: |
| 7565 | case ISN_ADDBLOB: |
| 7566 | case ISN_ADDLIST: |
| 7567 | case ISN_BCALL: |
| 7568 | case ISN_CATCH: |
| 7569 | case ISN_CHECKNR: |
| 7570 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7571 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7572 | case ISN_COMPAREANY: |
| 7573 | case ISN_COMPAREBLOB: |
| 7574 | case ISN_COMPAREBOOL: |
| 7575 | case ISN_COMPAREDICT: |
| 7576 | case ISN_COMPAREFLOAT: |
| 7577 | case ISN_COMPAREFUNC: |
| 7578 | case ISN_COMPARELIST: |
| 7579 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7580 | case ISN_COMPARESPECIAL: |
| 7581 | case ISN_COMPARESTRING: |
| 7582 | case ISN_CONCAT: |
| 7583 | case ISN_DCALL: |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 7584 | case ISN_SHUFFLE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7585 | case ISN_DROP: |
| 7586 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7587 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7588 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7589 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7590 | case ISN_EXECCONCAT: |
| 7591 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7592 | case ISN_FOR: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7593 | case ISN_LISTINDEX: |
| 7594 | case ISN_STRINDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7595 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7596 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7597 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7598 | case ISN_JUMP: |
| 7599 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7600 | case ISN_LOADBDICT: |
| 7601 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7602 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7603 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7604 | case ISN_LOADSCRIPT: |
| 7605 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7606 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7607 | case ISN_LOADWDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7608 | case ISN_NEGATENR: |
| 7609 | case ISN_NEWDICT: |
| 7610 | case ISN_NEWLIST: |
| 7611 | case ISN_OPNR: |
| 7612 | case ISN_OPFLOAT: |
| 7613 | case ISN_OPANY: |
| 7614 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7615 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7616 | case ISN_PUSHF: |
| 7617 | case ISN_PUSHNR: |
| 7618 | case ISN_PUSHBOOL: |
| 7619 | case ISN_PUSHSPEC: |
| 7620 | case ISN_RETURN: |
| 7621 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 7622 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7623 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7624 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7625 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7626 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7627 | case ISN_STOREDICT: |
| 7628 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7629 | case ISN_THROW: |
| 7630 | case ISN_TRY: |
| 7631 | // nothing allocated |
| 7632 | break; |
| 7633 | } |
| 7634 | } |
| 7635 | |
| 7636 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7637 | * Free all instructions for "dfunc". |
| 7638 | */ |
| 7639 | static void |
| 7640 | delete_def_function_contents(dfunc_T *dfunc) |
| 7641 | { |
| 7642 | int idx; |
| 7643 | |
| 7644 | ga_clear(&dfunc->df_def_args_isn); |
| 7645 | |
| 7646 | if (dfunc->df_instr != NULL) |
| 7647 | { |
| 7648 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7649 | delete_instr(dfunc->df_instr + idx); |
| 7650 | VIM_CLEAR(dfunc->df_instr); |
| 7651 | } |
| 7652 | |
| 7653 | dfunc->df_deleted = TRUE; |
| 7654 | } |
| 7655 | |
| 7656 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7657 | * When a user function is deleted, clear the contents of any associated def |
| 7658 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7659 | */ |
| 7660 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7661 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7662 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7663 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7664 | { |
| 7665 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7666 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7667 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7668 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7669 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7670 | } |
| 7671 | } |
| 7672 | |
| 7673 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7674 | /* |
| 7675 | * Free all functions defined with ":def". |
| 7676 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7677 | void |
| 7678 | free_def_functions(void) |
| 7679 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7680 | int idx; |
| 7681 | |
| 7682 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7683 | { |
| 7684 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7685 | |
| 7686 | delete_def_function_contents(dfunc); |
| 7687 | } |
| 7688 | |
| 7689 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7690 | } |
| 7691 | #endif |
| 7692 | |
| 7693 | |
| 7694 | #endif // FEAT_EVAL |