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 | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 481 | * Get a type_T for a typval_T. |
| 482 | * "type_list" is used to temporarily create types in. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 483 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 484 | type_T * |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 485 | typval2type(typval_T *tv, garray_T *type_gap) |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 486 | { |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 487 | type_T *actual; |
| 488 | type_T *member_type; |
| 489 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 490 | if (tv->v_type == VAR_NUMBER) |
| 491 | return &t_number; |
| 492 | if (tv->v_type == VAR_BOOL) |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 493 | return &t_bool; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 494 | if (tv->v_type == VAR_STRING) |
| 495 | return &t_string; |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 496 | |
| 497 | if (tv->v_type == VAR_LIST |
| 498 | && tv->vval.v_list != NULL |
| 499 | && tv->vval.v_list->lv_first != NULL) |
| 500 | { |
| 501 | // Use the type of the first member, it is the most specific. |
| 502 | member_type = typval2type(&tv->vval.v_list->lv_first->li_tv, type_gap); |
| 503 | return get_list_type(member_type, type_gap); |
| 504 | } |
| 505 | |
| 506 | if (tv->v_type == VAR_DICT |
| 507 | && tv->vval.v_dict != NULL |
| 508 | && tv->vval.v_dict->dv_hashtab.ht_used > 0) |
| 509 | { |
| 510 | dict_iterator_T iter; |
| 511 | typval_T *value; |
| 512 | |
| 513 | // Use the type of the first value, it is the most specific. |
| 514 | dict_iterate_start(tv, &iter); |
| 515 | dict_iterate_next(&iter, &value); |
| 516 | member_type = typval2type(value, type_gap); |
| 517 | return get_dict_type(member_type, type_gap); |
| 518 | } |
| 519 | |
| 520 | if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL) |
| 521 | { |
| 522 | char_u *name = NULL; |
| 523 | ufunc_T *ufunc = NULL; |
| 524 | |
| 525 | if (tv->v_type == VAR_PARTIAL) |
| 526 | { |
| 527 | if (tv->vval.v_partial->pt_func != NULL) |
| 528 | ufunc = tv->vval.v_partial->pt_func; |
| 529 | else |
| 530 | name = tv->vval.v_partial->pt_name; |
| 531 | } |
| 532 | else |
| 533 | name = tv->vval.v_string; |
| 534 | if (name != NULL) |
| 535 | // TODO: how about a builtin function? |
| 536 | ufunc = find_func(name, FALSE, NULL); |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 537 | if (ufunc != NULL) |
| 538 | { |
| 539 | // May need to get the argument types from default values by |
| 540 | // compiling the function. |
| 541 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
| 542 | && compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 543 | return NULL; |
| 544 | if (ufunc->uf_func_type != NULL) |
| 545 | return ufunc->uf_func_type; |
| 546 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | actual = alloc_type(type_gap); |
| 550 | if (actual == NULL) |
| 551 | return NULL; |
| 552 | actual->tt_type = tv->v_type; |
| 553 | actual->tt_member = &t_any; |
| 554 | |
| 555 | return actual; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Get a type_T for a typval_T, used for v: variables. |
| 560 | * "type_list" is used to temporarily create types in. |
| 561 | */ |
| 562 | type_T * |
| 563 | typval2type_vimvar(typval_T *tv, garray_T *type_gap) |
| 564 | { |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 565 | if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles |
| 566 | return &t_list_string; |
| 567 | if (tv->v_type == VAR_DICT) // e.g. for v:completed_item |
| 568 | return &t_dict_any; |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 569 | return typval2type(tv, type_gap); |
| 570 | } |
| 571 | |
| 572 | |
| 573 | /* |
| 574 | * Return FAIL if "expected" and "actual" don't match. |
| 575 | */ |
| 576 | int |
| 577 | check_typval_type(type_T *expected, typval_T *actual_tv) |
| 578 | { |
| 579 | garray_T type_list; |
| 580 | type_T *actual_type; |
| 581 | int res = FAIL; |
| 582 | |
| 583 | ga_init2(&type_list, sizeof(type_T *), 10); |
| 584 | actual_type = typval2type(actual_tv, &type_list); |
| 585 | if (actual_type != NULL) |
| 586 | res = check_type(expected, actual_type, TRUE); |
| 587 | clear_type_list(&type_list); |
| 588 | return res; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 589 | } |
| 590 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 591 | static void |
| 592 | type_mismatch(type_T *expected, type_T *actual) |
| 593 | { |
| 594 | char *tofree1, *tofree2; |
| 595 | |
| 596 | semsg(_("E1013: type mismatch, expected %s but got %s"), |
| 597 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 598 | vim_free(tofree1); |
| 599 | vim_free(tofree2); |
| 600 | } |
| 601 | |
| 602 | static void |
| 603 | arg_type_mismatch(type_T *expected, type_T *actual, int argidx) |
| 604 | { |
| 605 | char *tofree1, *tofree2; |
| 606 | |
| 607 | semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"), |
| 608 | argidx, |
| 609 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 610 | vim_free(tofree1); |
| 611 | vim_free(tofree2); |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * Check if the expected and actual types match. |
| 616 | * Does not allow for assigning "any" to a specific type. |
| 617 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 618 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 619 | check_type(type_T *expected, type_T *actual, int give_msg) |
| 620 | { |
| 621 | int ret = OK; |
| 622 | |
| 623 | // When expected is "unknown" we accept any actual type. |
| 624 | // When expected is "any" we accept any actual type except "void". |
| 625 | if (expected->tt_type != VAR_UNKNOWN |
| 626 | && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) |
| 627 | |
| 628 | { |
| 629 | if (expected->tt_type != actual->tt_type) |
| 630 | { |
| 631 | if (give_msg) |
| 632 | type_mismatch(expected, actual); |
| 633 | return FAIL; |
| 634 | } |
| 635 | if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) |
| 636 | { |
| 637 | // "unknown" is used for an empty list or dict |
| 638 | if (actual->tt_member != &t_unknown) |
| 639 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 640 | } |
| 641 | else if (expected->tt_type == VAR_FUNC) |
| 642 | { |
| 643 | if (expected->tt_member != &t_unknown) |
| 644 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 645 | if (ret == OK && expected->tt_argcount != -1 |
| 646 | && (actual->tt_argcount < expected->tt_min_argcount |
| 647 | || actual->tt_argcount > expected->tt_argcount)) |
| 648 | ret = FAIL; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 649 | if (expected->tt_args != NULL && actual->tt_args != NULL) |
| 650 | { |
| 651 | int i; |
| 652 | |
| 653 | for (i = 0; i < expected->tt_argcount; ++i) |
| 654 | if (check_type(expected->tt_args[i], actual->tt_args[i], |
| 655 | FALSE) == FAIL) |
| 656 | { |
| 657 | ret = FAIL; |
| 658 | break; |
| 659 | } |
| 660 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 661 | } |
| 662 | if (ret == FAIL && give_msg) |
| 663 | type_mismatch(expected, actual); |
| 664 | } |
| 665 | return ret; |
| 666 | } |
| 667 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 668 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 669 | ///////////////////////////////////////////////////////////////////// |
| 670 | // Following generate_ functions expect the caller to call ga_grow(). |
| 671 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 672 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 673 | #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] | 674 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 675 | /* |
| 676 | * Generate an instruction without arguments. |
| 677 | * Returns a pointer to the new instruction, NULL if failed. |
| 678 | */ |
| 679 | static isn_T * |
| 680 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 681 | { |
| 682 | garray_T *instr = &cctx->ctx_instr; |
| 683 | isn_T *isn; |
| 684 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 685 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 686 | if (ga_grow(instr, 1) == FAIL) |
| 687 | return NULL; |
| 688 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 689 | isn->isn_type = isn_type; |
| 690 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 691 | ++instr->ga_len; |
| 692 | |
| 693 | return isn; |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * Generate an instruction without arguments. |
| 698 | * "drop" will be removed from the stack. |
| 699 | * Returns a pointer to the new instruction, NULL if failed. |
| 700 | */ |
| 701 | static isn_T * |
| 702 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 703 | { |
| 704 | garray_T *stack = &cctx->ctx_type_stack; |
| 705 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 706 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 707 | stack->ga_len -= drop; |
| 708 | return generate_instr(cctx, isn_type); |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 713 | */ |
| 714 | static isn_T * |
| 715 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 716 | { |
| 717 | isn_T *isn; |
| 718 | garray_T *stack = &cctx->ctx_type_stack; |
| 719 | |
| 720 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 721 | return NULL; |
| 722 | |
| 723 | if (ga_grow(stack, 1) == FAIL) |
| 724 | return NULL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 725 | ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 726 | ++stack->ga_len; |
| 727 | |
| 728 | return isn; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 733 | */ |
| 734 | static int |
| 735 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 736 | { |
| 737 | isn_T *isn; |
| 738 | garray_T *stack = &cctx->ctx_type_stack; |
| 739 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 740 | |
| 741 | if ((*type)->tt_type == VAR_STRING) |
| 742 | return OK; |
| 743 | *type = &t_string; |
| 744 | |
| 745 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 746 | return FAIL; |
| 747 | isn->isn_arg.number = offset; |
| 748 | |
| 749 | return OK; |
| 750 | } |
| 751 | |
| 752 | static int |
| 753 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 754 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 755 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 756 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 757 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 758 | { |
| 759 | if (*op == '+') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 760 | emsg(_("E1051: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 761 | else |
| 762 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 763 | return FAIL; |
| 764 | } |
| 765 | return OK; |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * Generate an instruction with two arguments. The instruction depends on the |
| 770 | * type of the arguments. |
| 771 | */ |
| 772 | static int |
| 773 | generate_two_op(cctx_T *cctx, char_u *op) |
| 774 | { |
| 775 | garray_T *stack = &cctx->ctx_type_stack; |
| 776 | type_T *type1; |
| 777 | type_T *type2; |
| 778 | vartype_T vartype; |
| 779 | isn_T *isn; |
| 780 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 781 | RETURN_OK_IF_SKIP(cctx); |
| 782 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 783 | // Get the known type of the two items on the stack. If they are matching |
| 784 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 785 | // checking. |
| 786 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 787 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 788 | vartype = VAR_ANY; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 789 | if (type1->tt_type == type2->tt_type |
| 790 | && (type1->tt_type == VAR_NUMBER |
| 791 | || type1->tt_type == VAR_LIST |
| 792 | #ifdef FEAT_FLOAT |
| 793 | || type1->tt_type == VAR_FLOAT |
| 794 | #endif |
| 795 | || type1->tt_type == VAR_BLOB)) |
| 796 | vartype = type1->tt_type; |
| 797 | |
| 798 | switch (*op) |
| 799 | { |
| 800 | case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 801 | && type1->tt_type != VAR_ANY |
| 802 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 803 | && check_number_or_float( |
| 804 | type1->tt_type, type2->tt_type, op) == FAIL) |
| 805 | return FAIL; |
| 806 | isn = generate_instr_drop(cctx, |
| 807 | vartype == VAR_NUMBER ? ISN_OPNR |
| 808 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 809 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 810 | #ifdef FEAT_FLOAT |
| 811 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 812 | #endif |
| 813 | : ISN_OPANY, 1); |
| 814 | if (isn != NULL) |
| 815 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 816 | break; |
| 817 | |
| 818 | case '-': |
| 819 | case '*': |
| 820 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 821 | op) == FAIL) |
| 822 | return FAIL; |
| 823 | if (vartype == VAR_NUMBER) |
| 824 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 825 | #ifdef FEAT_FLOAT |
| 826 | else if (vartype == VAR_FLOAT) |
| 827 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 828 | #endif |
| 829 | else |
| 830 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 831 | if (isn != NULL) |
| 832 | isn->isn_arg.op.op_type = *op == '*' |
| 833 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 834 | break; |
| 835 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 836 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 837 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 838 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 839 | && type2->tt_type != VAR_NUMBER)) |
| 840 | { |
| 841 | emsg(_("E1035: % requires number arguments")); |
| 842 | return FAIL; |
| 843 | } |
| 844 | isn = generate_instr_drop(cctx, |
| 845 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 846 | if (isn != NULL) |
| 847 | isn->isn_arg.op.op_type = EXPR_REM; |
| 848 | break; |
| 849 | } |
| 850 | |
| 851 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 852 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 853 | { |
| 854 | type_T *type = &t_any; |
| 855 | |
| 856 | #ifdef FEAT_FLOAT |
| 857 | // float+number and number+float results in float |
| 858 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 859 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 860 | type = &t_float; |
| 861 | #endif |
| 862 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 863 | } |
| 864 | |
| 865 | return OK; |
| 866 | } |
| 867 | |
| 868 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 869 | * Get the instruction to use for comparing "type1" with "type2" |
| 870 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 871 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 872 | static isntype_T |
| 873 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 874 | { |
| 875 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 876 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 877 | if (type1 == VAR_UNKNOWN) |
| 878 | type1 = VAR_ANY; |
| 879 | if (type2 == VAR_UNKNOWN) |
| 880 | type2 = VAR_ANY; |
| 881 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 882 | if (type1 == type2) |
| 883 | { |
| 884 | switch (type1) |
| 885 | { |
| 886 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 887 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 888 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 889 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 890 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 891 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 892 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 893 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 894 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 895 | default: isntype = ISN_COMPAREANY; break; |
| 896 | } |
| 897 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 898 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 899 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 900 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 901 | isntype = ISN_COMPAREANY; |
| 902 | |
| 903 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 904 | && (isntype == ISN_COMPAREBOOL |
| 905 | || isntype == ISN_COMPARESPECIAL |
| 906 | || isntype == ISN_COMPARENR |
| 907 | || isntype == ISN_COMPAREFLOAT)) |
| 908 | { |
| 909 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 910 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 911 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 912 | } |
| 913 | if (isntype == ISN_DROP |
| 914 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 915 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 916 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 917 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 918 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 919 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 920 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 921 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 922 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 923 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 924 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 925 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 926 | return isntype; |
| 927 | } |
| 928 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 929 | int |
| 930 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 931 | { |
| 932 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 933 | return FAIL; |
| 934 | return OK; |
| 935 | } |
| 936 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 937 | /* |
| 938 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 939 | */ |
| 940 | static int |
| 941 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 942 | { |
| 943 | isntype_T isntype; |
| 944 | isn_T *isn; |
| 945 | garray_T *stack = &cctx->ctx_type_stack; |
| 946 | vartype_T type1; |
| 947 | vartype_T type2; |
| 948 | |
| 949 | RETURN_OK_IF_SKIP(cctx); |
| 950 | |
| 951 | // Get the known type of the two items on the stack. If they are matching |
| 952 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 953 | // checking. |
| 954 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 955 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 956 | isntype = get_compare_isn(exptype, type1, type2); |
| 957 | if (isntype == ISN_DROP) |
| 958 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 959 | |
| 960 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 961 | return FAIL; |
| 962 | isn->isn_arg.op.op_type = exptype; |
| 963 | isn->isn_arg.op.op_ic = ic; |
| 964 | |
| 965 | // takes two arguments, puts one bool back |
| 966 | if (stack->ga_len >= 2) |
| 967 | { |
| 968 | --stack->ga_len; |
| 969 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 970 | } |
| 971 | |
| 972 | return OK; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * Generate an ISN_2BOOL instruction. |
| 977 | */ |
| 978 | static int |
| 979 | generate_2BOOL(cctx_T *cctx, int invert) |
| 980 | { |
| 981 | isn_T *isn; |
| 982 | garray_T *stack = &cctx->ctx_type_stack; |
| 983 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 984 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 985 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 986 | return FAIL; |
| 987 | isn->isn_arg.number = invert; |
| 988 | |
| 989 | // type becomes bool |
| 990 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 991 | |
| 992 | return OK; |
| 993 | } |
| 994 | |
| 995 | static int |
| 996 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 997 | { |
| 998 | isn_T *isn; |
| 999 | garray_T *stack = &cctx->ctx_type_stack; |
| 1000 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1001 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1002 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 1003 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 1004 | // TODO: whole type, e.g. for a function also arg and return types |
| 1005 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1006 | isn->isn_arg.type.ct_off = offset; |
| 1007 | |
| 1008 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1009 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1010 | |
| 1011 | return OK; |
| 1012 | } |
| 1013 | |
| 1014 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1015 | * Check that |
| 1016 | * - "actual" is "expected" type or |
| 1017 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 1018 | * - return FAIL. |
| 1019 | */ |
| 1020 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1021 | need_type( |
| 1022 | type_T *actual, |
| 1023 | type_T *expected, |
| 1024 | int offset, |
| 1025 | cctx_T *cctx, |
| 1026 | int silent) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1027 | { |
| 1028 | if (check_type(expected, actual, FALSE) == OK) |
| 1029 | return OK; |
| 1030 | if (actual->tt_type != VAR_ANY |
| 1031 | && actual->tt_type != VAR_UNKNOWN |
| 1032 | && !(actual->tt_type == VAR_FUNC |
| 1033 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 1034 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1035 | if (!silent) |
| 1036 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1037 | return FAIL; |
| 1038 | } |
| 1039 | generate_TYPECHECK(cctx, expected, offset); |
| 1040 | return OK; |
| 1041 | } |
| 1042 | |
| 1043 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1044 | * Generate an ISN_PUSHNR instruction. |
| 1045 | */ |
| 1046 | static int |
| 1047 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 1048 | { |
| 1049 | isn_T *isn; |
| 1050 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1051 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1052 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 1053 | return FAIL; |
| 1054 | isn->isn_arg.number = number; |
| 1055 | |
| 1056 | return OK; |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | * Generate an ISN_PUSHBOOL instruction. |
| 1061 | */ |
| 1062 | static int |
| 1063 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 1064 | { |
| 1065 | isn_T *isn; |
| 1066 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1067 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1068 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 1069 | return FAIL; |
| 1070 | isn->isn_arg.number = number; |
| 1071 | |
| 1072 | return OK; |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Generate an ISN_PUSHSPEC instruction. |
| 1077 | */ |
| 1078 | static int |
| 1079 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 1080 | { |
| 1081 | isn_T *isn; |
| 1082 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1083 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1084 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 1085 | return FAIL; |
| 1086 | isn->isn_arg.number = number; |
| 1087 | |
| 1088 | return OK; |
| 1089 | } |
| 1090 | |
| 1091 | #ifdef FEAT_FLOAT |
| 1092 | /* |
| 1093 | * Generate an ISN_PUSHF instruction. |
| 1094 | */ |
| 1095 | static int |
| 1096 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 1097 | { |
| 1098 | isn_T *isn; |
| 1099 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1100 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1101 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 1102 | return FAIL; |
| 1103 | isn->isn_arg.fnumber = fnumber; |
| 1104 | |
| 1105 | return OK; |
| 1106 | } |
| 1107 | #endif |
| 1108 | |
| 1109 | /* |
| 1110 | * Generate an ISN_PUSHS instruction. |
| 1111 | * Consumes "str". |
| 1112 | */ |
| 1113 | static int |
| 1114 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 1115 | { |
| 1116 | isn_T *isn; |
| 1117 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1118 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1119 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 1120 | return FAIL; |
| 1121 | isn->isn_arg.string = str; |
| 1122 | |
| 1123 | return OK; |
| 1124 | } |
| 1125 | |
| 1126 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1127 | * Generate an ISN_PUSHCHANNEL instruction. |
| 1128 | * Consumes "channel". |
| 1129 | */ |
| 1130 | static int |
| 1131 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 1132 | { |
| 1133 | isn_T *isn; |
| 1134 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1135 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1136 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 1137 | return FAIL; |
| 1138 | isn->isn_arg.channel = channel; |
| 1139 | |
| 1140 | return OK; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * Generate an ISN_PUSHJOB instruction. |
| 1145 | * Consumes "job". |
| 1146 | */ |
| 1147 | static int |
| 1148 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 1149 | { |
| 1150 | isn_T *isn; |
| 1151 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1152 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1153 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1154 | return FAIL; |
| 1155 | isn->isn_arg.job = job; |
| 1156 | |
| 1157 | return OK; |
| 1158 | } |
| 1159 | |
| 1160 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1161 | * Generate an ISN_PUSHBLOB instruction. |
| 1162 | * Consumes "blob". |
| 1163 | */ |
| 1164 | static int |
| 1165 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1166 | { |
| 1167 | isn_T *isn; |
| 1168 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1169 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1170 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1171 | return FAIL; |
| 1172 | isn->isn_arg.blob = blob; |
| 1173 | |
| 1174 | return OK; |
| 1175 | } |
| 1176 | |
| 1177 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1178 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1179 | * Consumes "name". |
| 1180 | */ |
| 1181 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1182 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1183 | { |
| 1184 | isn_T *isn; |
| 1185 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1186 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1187 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1188 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 1189 | isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1190 | |
| 1191 | return OK; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1195 | * Generate an ISN_GETITEM instruction with "index". |
| 1196 | */ |
| 1197 | static int |
| 1198 | generate_GETITEM(cctx_T *cctx, int index) |
| 1199 | { |
| 1200 | isn_T *isn; |
| 1201 | garray_T *stack = &cctx->ctx_type_stack; |
| 1202 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1203 | type_T *item_type = &t_any; |
| 1204 | |
| 1205 | RETURN_OK_IF_SKIP(cctx); |
| 1206 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1207 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1208 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1209 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1210 | emsg(_(e_listreq)); |
| 1211 | return FAIL; |
| 1212 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1213 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1214 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1215 | return FAIL; |
| 1216 | isn->isn_arg.number = index; |
| 1217 | |
| 1218 | // add the item type to the type stack |
| 1219 | if (ga_grow(stack, 1) == FAIL) |
| 1220 | return FAIL; |
| 1221 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 1222 | ++stack->ga_len; |
| 1223 | return OK; |
| 1224 | } |
| 1225 | |
| 1226 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 1227 | * Generate an ISN_SLICE instruction with "count". |
| 1228 | */ |
| 1229 | static int |
| 1230 | generate_SLICE(cctx_T *cctx, int count) |
| 1231 | { |
| 1232 | isn_T *isn; |
| 1233 | |
| 1234 | RETURN_OK_IF_SKIP(cctx); |
| 1235 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1236 | return FAIL; |
| 1237 | isn->isn_arg.number = count; |
| 1238 | return OK; |
| 1239 | } |
| 1240 | |
| 1241 | /* |
| 1242 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1243 | */ |
| 1244 | static int |
| 1245 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1246 | { |
| 1247 | isn_T *isn; |
| 1248 | |
| 1249 | RETURN_OK_IF_SKIP(cctx); |
| 1250 | |
| 1251 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1252 | return FAIL; |
| 1253 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1254 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1255 | |
| 1256 | return OK; |
| 1257 | } |
| 1258 | |
| 1259 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1260 | * Generate an ISN_STORE instruction. |
| 1261 | */ |
| 1262 | static int |
| 1263 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1264 | { |
| 1265 | isn_T *isn; |
| 1266 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1267 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1268 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1269 | return FAIL; |
| 1270 | if (name != NULL) |
| 1271 | isn->isn_arg.string = vim_strsave(name); |
| 1272 | else |
| 1273 | isn->isn_arg.number = idx; |
| 1274 | |
| 1275 | return OK; |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1280 | */ |
| 1281 | static int |
| 1282 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 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(cctx, ISN_STORENR)) == NULL) |
| 1288 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1289 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1290 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1291 | |
| 1292 | return OK; |
| 1293 | } |
| 1294 | |
| 1295 | /* |
| 1296 | * Generate an ISN_STOREOPT instruction |
| 1297 | */ |
| 1298 | static int |
| 1299 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1300 | { |
| 1301 | isn_T *isn; |
| 1302 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1303 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1304 | if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL) |
| 1305 | return FAIL; |
| 1306 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1307 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1308 | |
| 1309 | return OK; |
| 1310 | } |
| 1311 | |
| 1312 | /* |
| 1313 | * Generate an ISN_LOAD or similar instruction. |
| 1314 | */ |
| 1315 | static int |
| 1316 | generate_LOAD( |
| 1317 | cctx_T *cctx, |
| 1318 | isntype_T isn_type, |
| 1319 | int idx, |
| 1320 | char_u *name, |
| 1321 | type_T *type) |
| 1322 | { |
| 1323 | isn_T *isn; |
| 1324 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1325 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1326 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1327 | return FAIL; |
| 1328 | if (name != NULL) |
| 1329 | isn->isn_arg.string = vim_strsave(name); |
| 1330 | else |
| 1331 | isn->isn_arg.number = idx; |
| 1332 | |
| 1333 | return OK; |
| 1334 | } |
| 1335 | |
| 1336 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1337 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1338 | */ |
| 1339 | static int |
| 1340 | generate_LOADV( |
| 1341 | cctx_T *cctx, |
| 1342 | char_u *name, |
| 1343 | int error) |
| 1344 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1345 | int di_flags; |
| 1346 | int vidx = find_vim_var(name, &di_flags); |
| 1347 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1348 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1349 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1350 | if (vidx < 0) |
| 1351 | { |
| 1352 | if (error) |
| 1353 | semsg(_(e_var_notfound), name); |
| 1354 | return FAIL; |
| 1355 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1356 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1357 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1358 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1362 | * Generate an ISN_UNLET instruction. |
| 1363 | */ |
| 1364 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1365 | 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] | 1366 | { |
| 1367 | isn_T *isn; |
| 1368 | |
| 1369 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1370 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1371 | return FAIL; |
| 1372 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1373 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1374 | |
| 1375 | return OK; |
| 1376 | } |
| 1377 | |
| 1378 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1379 | * Generate an ISN_LOADS instruction. |
| 1380 | */ |
| 1381 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1382 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1383 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1384 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1385 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1386 | int sid, |
| 1387 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1388 | { |
| 1389 | isn_T *isn; |
| 1390 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1391 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1392 | if (isn_type == ISN_LOADS) |
| 1393 | isn = generate_instr_type(cctx, isn_type, type); |
| 1394 | else |
| 1395 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1396 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1397 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1398 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1399 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1400 | |
| 1401 | return OK; |
| 1402 | } |
| 1403 | |
| 1404 | /* |
| 1405 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1406 | */ |
| 1407 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1408 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1409 | cctx_T *cctx, |
| 1410 | isntype_T isn_type, |
| 1411 | int sid, |
| 1412 | int idx, |
| 1413 | type_T *type) |
| 1414 | { |
| 1415 | isn_T *isn; |
| 1416 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1417 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1418 | if (isn_type == ISN_LOADSCRIPT) |
| 1419 | isn = generate_instr_type(cctx, isn_type, type); |
| 1420 | else |
| 1421 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1422 | if (isn == NULL) |
| 1423 | return FAIL; |
| 1424 | isn->isn_arg.script.script_sid = sid; |
| 1425 | isn->isn_arg.script.script_idx = idx; |
| 1426 | return OK; |
| 1427 | } |
| 1428 | |
| 1429 | /* |
| 1430 | * Generate an ISN_NEWLIST instruction. |
| 1431 | */ |
| 1432 | static int |
| 1433 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1434 | { |
| 1435 | isn_T *isn; |
| 1436 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1437 | type_T *type; |
| 1438 | type_T *member; |
| 1439 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1440 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1441 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1442 | return FAIL; |
| 1443 | isn->isn_arg.number = count; |
| 1444 | |
| 1445 | // drop the value types |
| 1446 | stack->ga_len -= count; |
| 1447 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1448 | // 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] | 1449 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1450 | if (count > 0) |
| 1451 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1452 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1453 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1454 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1455 | |
| 1456 | // add the list type to the type stack |
| 1457 | if (ga_grow(stack, 1) == FAIL) |
| 1458 | return FAIL; |
| 1459 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1460 | ++stack->ga_len; |
| 1461 | |
| 1462 | return OK; |
| 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * Generate an ISN_NEWDICT instruction. |
| 1467 | */ |
| 1468 | static int |
| 1469 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1470 | { |
| 1471 | isn_T *isn; |
| 1472 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1473 | type_T *type; |
| 1474 | type_T *member; |
| 1475 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1476 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1477 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1478 | return FAIL; |
| 1479 | isn->isn_arg.number = count; |
| 1480 | |
| 1481 | // drop the key and value types |
| 1482 | stack->ga_len -= 2 * count; |
| 1483 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1484 | // Use the first value type for the list member type. Use "void" for an |
| 1485 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1486 | if (count > 0) |
| 1487 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1488 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1489 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1490 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1491 | |
| 1492 | // add the dict type to the type stack |
| 1493 | if (ga_grow(stack, 1) == FAIL) |
| 1494 | return FAIL; |
| 1495 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1496 | ++stack->ga_len; |
| 1497 | |
| 1498 | return OK; |
| 1499 | } |
| 1500 | |
| 1501 | /* |
| 1502 | * Generate an ISN_FUNCREF instruction. |
| 1503 | */ |
| 1504 | static int |
| 1505 | generate_FUNCREF(cctx_T *cctx, int dfunc_idx) |
| 1506 | { |
| 1507 | isn_T *isn; |
| 1508 | garray_T *stack = &cctx->ctx_type_stack; |
| 1509 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1510 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1511 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1512 | return FAIL; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1513 | isn->isn_arg.funcref.fr_func = dfunc_idx; |
| 1514 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1515 | |
| 1516 | if (ga_grow(stack, 1) == FAIL) |
| 1517 | return FAIL; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1518 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1519 | // TODO: argument and return types |
| 1520 | ++stack->ga_len; |
| 1521 | |
| 1522 | return OK; |
| 1523 | } |
| 1524 | |
| 1525 | /* |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1526 | * Generate an ISN_NEWFUNC instruction. |
| 1527 | */ |
| 1528 | static int |
| 1529 | generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) |
| 1530 | { |
| 1531 | isn_T *isn; |
| 1532 | char_u *name; |
| 1533 | |
| 1534 | RETURN_OK_IF_SKIP(cctx); |
| 1535 | name = vim_strsave(lambda_name); |
| 1536 | if (name == NULL) |
| 1537 | return FAIL; |
| 1538 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
| 1539 | return FAIL; |
| 1540 | isn->isn_arg.newfunc.nf_lambda = name; |
| 1541 | isn->isn_arg.newfunc.nf_global = func_name; |
| 1542 | |
| 1543 | return OK; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1547 | * Generate an ISN_JUMP instruction. |
| 1548 | */ |
| 1549 | static int |
| 1550 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1551 | { |
| 1552 | isn_T *isn; |
| 1553 | garray_T *stack = &cctx->ctx_type_stack; |
| 1554 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1555 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1556 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1557 | return FAIL; |
| 1558 | isn->isn_arg.jump.jump_when = when; |
| 1559 | isn->isn_arg.jump.jump_where = where; |
| 1560 | |
| 1561 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1562 | --stack->ga_len; |
| 1563 | |
| 1564 | return OK; |
| 1565 | } |
| 1566 | |
| 1567 | static int |
| 1568 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1569 | { |
| 1570 | isn_T *isn; |
| 1571 | garray_T *stack = &cctx->ctx_type_stack; |
| 1572 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1573 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1574 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1575 | return FAIL; |
| 1576 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1577 | |
| 1578 | if (ga_grow(stack, 1) == FAIL) |
| 1579 | return FAIL; |
| 1580 | // type doesn't matter, will be stored next |
| 1581 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1582 | ++stack->ga_len; |
| 1583 | |
| 1584 | return OK; |
| 1585 | } |
| 1586 | |
| 1587 | /* |
| 1588 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1589 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1590 | * Return FAIL if the number of arguments is wrong. |
| 1591 | */ |
| 1592 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1593 | 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] | 1594 | { |
| 1595 | isn_T *isn; |
| 1596 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1597 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1598 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1599 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1600 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1601 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1602 | argoff = check_internal_func(func_idx, argcount); |
| 1603 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1604 | return FAIL; |
| 1605 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1606 | if (method_call && argoff > 1) |
| 1607 | { |
| 1608 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1609 | return FAIL; |
| 1610 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1611 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1612 | } |
| 1613 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1614 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1615 | return FAIL; |
| 1616 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1617 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1618 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1619 | for (i = 0; i < argcount; ++i) |
| 1620 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1621 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1622 | stack->ga_len -= argcount; // drop the arguments |
| 1623 | if (ga_grow(stack, 1) == FAIL) |
| 1624 | return FAIL; |
| 1625 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1626 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1627 | ++stack->ga_len; // add return value |
| 1628 | |
| 1629 | return OK; |
| 1630 | } |
| 1631 | |
| 1632 | /* |
| 1633 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1634 | * Return FAIL if the number of arguments is wrong. |
| 1635 | */ |
| 1636 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1637 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1638 | { |
| 1639 | isn_T *isn; |
| 1640 | garray_T *stack = &cctx->ctx_type_stack; |
| 1641 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1642 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1643 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1644 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1645 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1646 | { |
| 1647 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1648 | return FAIL; |
| 1649 | } |
| 1650 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1651 | { |
| 1652 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1653 | return FAIL; |
| 1654 | } |
| 1655 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1656 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1657 | { |
| 1658 | int i; |
| 1659 | |
| 1660 | for (i = 0; i < argcount; ++i) |
| 1661 | { |
| 1662 | type_T *expected; |
| 1663 | type_T *actual; |
| 1664 | |
| 1665 | if (i < regular_args) |
| 1666 | { |
| 1667 | if (ufunc->uf_arg_types == NULL) |
| 1668 | continue; |
| 1669 | expected = ufunc->uf_arg_types[i]; |
| 1670 | } |
| 1671 | else |
| 1672 | expected = ufunc->uf_va_type->tt_member; |
| 1673 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1674 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1675 | { |
| 1676 | arg_type_mismatch(expected, actual, i + 1); |
| 1677 | return FAIL; |
| 1678 | } |
| 1679 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1680 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 1681 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1682 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1683 | } |
| 1684 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1685 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1686 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1687 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1688 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1689 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1690 | { |
| 1691 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1692 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1693 | } |
| 1694 | else |
| 1695 | { |
| 1696 | // A user function may be deleted and redefined later, can't use the |
| 1697 | // ufunc pointer, need to look it up again at runtime. |
| 1698 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1699 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1700 | } |
| 1701 | |
| 1702 | stack->ga_len -= argcount; // drop the arguments |
| 1703 | if (ga_grow(stack, 1) == FAIL) |
| 1704 | return FAIL; |
| 1705 | // add return value |
| 1706 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1707 | ++stack->ga_len; |
| 1708 | |
| 1709 | return OK; |
| 1710 | } |
| 1711 | |
| 1712 | /* |
| 1713 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1714 | */ |
| 1715 | static int |
| 1716 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1717 | { |
| 1718 | isn_T *isn; |
| 1719 | garray_T *stack = &cctx->ctx_type_stack; |
| 1720 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1721 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1722 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1723 | return FAIL; |
| 1724 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1725 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1726 | |
| 1727 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1728 | if (ga_grow(stack, 1) == FAIL) |
| 1729 | return FAIL; |
| 1730 | // add return value |
| 1731 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1732 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1733 | |
| 1734 | return OK; |
| 1735 | } |
| 1736 | |
| 1737 | /* |
| 1738 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1739 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1740 | */ |
| 1741 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1742 | generate_PCALL( |
| 1743 | cctx_T *cctx, |
| 1744 | int argcount, |
| 1745 | char_u *name, |
| 1746 | type_T *type, |
| 1747 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1748 | { |
| 1749 | isn_T *isn; |
| 1750 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1751 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1752 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1753 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1754 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1755 | if (type->tt_type == VAR_ANY) |
| 1756 | ret_type = &t_any; |
| 1757 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1758 | { |
| 1759 | if (type->tt_argcount != -1) |
| 1760 | { |
| 1761 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1762 | |
| 1763 | if (argcount < type->tt_min_argcount - varargs) |
| 1764 | { |
| 1765 | semsg(_(e_toofewarg), "[reference]"); |
| 1766 | return FAIL; |
| 1767 | } |
| 1768 | if (!varargs && argcount > type->tt_argcount) |
| 1769 | { |
| 1770 | semsg(_(e_toomanyarg), "[reference]"); |
| 1771 | return FAIL; |
| 1772 | } |
| 1773 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1774 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1775 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1776 | else |
| 1777 | { |
| 1778 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1779 | return FAIL; |
| 1780 | } |
| 1781 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1782 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1783 | return FAIL; |
| 1784 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1785 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1786 | |
| 1787 | stack->ga_len -= argcount; // drop the arguments |
| 1788 | |
| 1789 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1790 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1791 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1792 | // If partial is above the arguments it must be cleared and replaced with |
| 1793 | // the return value. |
| 1794 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1795 | return FAIL; |
| 1796 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1797 | return OK; |
| 1798 | } |
| 1799 | |
| 1800 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1801 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1802 | */ |
| 1803 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1804 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1805 | { |
| 1806 | isn_T *isn; |
| 1807 | garray_T *stack = &cctx->ctx_type_stack; |
| 1808 | type_T *type; |
| 1809 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1810 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1811 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1812 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1813 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1814 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1815 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1816 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1817 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1818 | { |
| 1819 | emsg(_(e_dictreq)); |
| 1820 | return FAIL; |
| 1821 | } |
| 1822 | // change dict type to dict member type |
| 1823 | if (type->tt_type == VAR_DICT) |
| 1824 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1825 | |
| 1826 | return OK; |
| 1827 | } |
| 1828 | |
| 1829 | /* |
| 1830 | * Generate an ISN_ECHO instruction. |
| 1831 | */ |
| 1832 | static int |
| 1833 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1834 | { |
| 1835 | isn_T *isn; |
| 1836 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1837 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1838 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1839 | return FAIL; |
| 1840 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1841 | isn->isn_arg.echo.echo_count = count; |
| 1842 | |
| 1843 | return OK; |
| 1844 | } |
| 1845 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1846 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1847 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1848 | */ |
| 1849 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1850 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1851 | { |
| 1852 | isn_T *isn; |
| 1853 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1854 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1855 | return FAIL; |
| 1856 | isn->isn_arg.number = count; |
| 1857 | |
| 1858 | return OK; |
| 1859 | } |
| 1860 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1861 | static int |
| 1862 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1863 | { |
| 1864 | isn_T *isn; |
| 1865 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1866 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1867 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1868 | return FAIL; |
| 1869 | isn->isn_arg.string = vim_strsave(line); |
| 1870 | return OK; |
| 1871 | } |
| 1872 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1873 | static int |
| 1874 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1875 | { |
| 1876 | isn_T *isn; |
| 1877 | |
| 1878 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1879 | return FAIL; |
| 1880 | isn->isn_arg.number = count; |
| 1881 | return OK; |
| 1882 | } |
| 1883 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1884 | /* |
| 1885 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1886 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1887 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1888 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1889 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1890 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1891 | lvar_T *lvar; |
| 1892 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1893 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1894 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1895 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1896 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1900 | return NULL; |
| 1901 | 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] | 1902 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1903 | // Every local variable uses the next entry on the stack. We could re-use |
| 1904 | // the last ones when leaving a scope, but then variables used in a closure |
| 1905 | // might get overwritten. To keep things simple do not re-use stack |
| 1906 | // entries. This is less efficient, but memory is cheap these days. |
| 1907 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1908 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1909 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1910 | lvar->lv_const = isConst; |
| 1911 | lvar->lv_type = type; |
| 1912 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1913 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1917 | * Remove local variables above "new_top". |
| 1918 | */ |
| 1919 | static void |
| 1920 | unwind_locals(cctx_T *cctx, int new_top) |
| 1921 | { |
| 1922 | if (cctx->ctx_locals.ga_len > new_top) |
| 1923 | { |
| 1924 | int idx; |
| 1925 | lvar_T *lvar; |
| 1926 | |
| 1927 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1928 | { |
| 1929 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1930 | vim_free(lvar->lv_name); |
| 1931 | } |
| 1932 | } |
| 1933 | cctx->ctx_locals.ga_len = new_top; |
| 1934 | } |
| 1935 | |
| 1936 | /* |
| 1937 | * Free all local variables. |
| 1938 | */ |
| 1939 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1940 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1941 | { |
| 1942 | unwind_locals(cctx, 0); |
| 1943 | ga_clear(&cctx->ctx_locals); |
| 1944 | } |
| 1945 | |
| 1946 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1947 | * Skip over a type definition and return a pointer to just after it. |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 1948 | * When "optional" is TRUE then a leading "?" is accepted. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1949 | */ |
| 1950 | char_u * |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 1951 | skip_type(char_u *start, int optional) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1952 | { |
| 1953 | char_u *p = start; |
| 1954 | |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 1955 | if (optional && *p == '?') |
| 1956 | ++p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1957 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1958 | ++p; |
| 1959 | |
| 1960 | // Skip over "<type>"; this is permissive about white space. |
| 1961 | if (*skipwhite(p) == '<') |
| 1962 | { |
| 1963 | p = skipwhite(p); |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 1964 | p = skip_type(skipwhite(p + 1), FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1965 | p = skipwhite(p); |
| 1966 | if (*p == '>') |
| 1967 | ++p; |
| 1968 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1969 | else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1]))) |
| 1970 | && STRNCMP("func", start, 4) == 0) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1971 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1972 | if (*p == '(') |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1973 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1974 | // handle func(args): type |
| 1975 | ++p; |
| 1976 | while (*p != ')' && *p != NUL) |
| 1977 | { |
| 1978 | char_u *sp = p; |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1979 | |
Bram Moolenaar | ace6132 | 2020-07-26 18:16:58 +0200 | [diff] [blame] | 1980 | if (STRNCMP(p, "...", 3) == 0) |
| 1981 | p += 3; |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 1982 | p = skip_type(p, TRUE); |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1983 | if (p == sp) |
| 1984 | return p; // syntax error |
| 1985 | if (*p == ',') |
| 1986 | p = skipwhite(p + 1); |
| 1987 | } |
| 1988 | if (*p == ')') |
| 1989 | { |
| 1990 | if (p[1] == ':') |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 1991 | p = skip_type(skipwhite(p + 2), FALSE); |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1992 | else |
Bram Moolenaar | bfba865 | 2020-07-23 20:09:10 +0200 | [diff] [blame] | 1993 | ++p; |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1994 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1995 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1996 | else |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1997 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1998 | // handle func: return_type |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 1999 | p = skip_type(skipwhite(p + 1), FALSE); |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 2000 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2001 | } |
| 2002 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2003 | return p; |
| 2004 | } |
| 2005 | |
| 2006 | /* |
| 2007 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2008 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2009 | * Returns NULL in case of failure. |
| 2010 | */ |
| 2011 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2012 | 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] | 2013 | { |
| 2014 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2015 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2016 | |
| 2017 | if (**arg != '<') |
| 2018 | { |
| 2019 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2020 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2021 | else |
| 2022 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 2023 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2024 | } |
| 2025 | *arg = skipwhite(*arg + 1); |
| 2026 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2027 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2028 | |
| 2029 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2030 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2031 | { |
| 2032 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 2033 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2034 | } |
| 2035 | ++*arg; |
| 2036 | |
| 2037 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2038 | return get_list_type(member_type, type_gap); |
| 2039 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | /* |
| 2043 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 2044 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2045 | */ |
| 2046 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2047 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2048 | { |
| 2049 | char_u *p = *arg; |
| 2050 | size_t len; |
| 2051 | |
| 2052 | // skip over the first word |
| 2053 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 2054 | ++p; |
| 2055 | len = p - *arg; |
| 2056 | |
| 2057 | switch (**arg) |
| 2058 | { |
| 2059 | case 'a': |
| 2060 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 2061 | { |
| 2062 | *arg += len; |
| 2063 | return &t_any; |
| 2064 | } |
| 2065 | break; |
| 2066 | case 'b': |
| 2067 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 2068 | { |
| 2069 | *arg += len; |
| 2070 | return &t_bool; |
| 2071 | } |
| 2072 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 2073 | { |
| 2074 | *arg += len; |
| 2075 | return &t_blob; |
| 2076 | } |
| 2077 | break; |
| 2078 | case 'c': |
| 2079 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 2080 | { |
| 2081 | *arg += len; |
| 2082 | return &t_channel; |
| 2083 | } |
| 2084 | break; |
| 2085 | case 'd': |
| 2086 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 2087 | { |
| 2088 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2089 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2090 | } |
| 2091 | break; |
| 2092 | case 'f': |
| 2093 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 2094 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2095 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2096 | *arg += len; |
| 2097 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2098 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2099 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2100 | return &t_any; |
| 2101 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2102 | } |
| 2103 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 2104 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2105 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2106 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2107 | int argcount = -1; |
| 2108 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2109 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2110 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 2111 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2112 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2113 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2114 | if (**arg == '(') |
| 2115 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2116 | // "func" may or may not return a value, "func()" does |
| 2117 | // not return a value. |
| 2118 | ret_type = &t_void; |
| 2119 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2120 | p = ++*arg; |
| 2121 | argcount = 0; |
| 2122 | while (*p != NUL && *p != ')') |
| 2123 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2124 | if (*p == '?') |
| 2125 | { |
| 2126 | if (first_optional == -1) |
| 2127 | first_optional = argcount; |
| 2128 | ++p; |
| 2129 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2130 | else if (STRNCMP(p, "...", 3) == 0) |
| 2131 | { |
| 2132 | flags |= TTFLAG_VARARGS; |
| 2133 | p += 3; |
| 2134 | } |
Bram Moolenaar | 01865ad | 2020-07-26 18:33:09 +0200 | [diff] [blame] | 2135 | else if (first_optional != -1) |
| 2136 | { |
| 2137 | emsg(_("E1007: mandatory argument after optional argument")); |
| 2138 | return &t_any; |
| 2139 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2140 | |
| 2141 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 2142 | |
| 2143 | // Nothing comes after "...{type}". |
| 2144 | if (flags & TTFLAG_VARARGS) |
| 2145 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2146 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2147 | if (*p != ',' && *skipwhite(p) == ',') |
| 2148 | { |
| 2149 | semsg(_(e_no_white_before), ","); |
| 2150 | return &t_any; |
| 2151 | } |
| 2152 | if (*p == ',') |
| 2153 | { |
| 2154 | ++p; |
| 2155 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2156 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2157 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2158 | return &t_any; |
| 2159 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2160 | } |
| 2161 | p = skipwhite(p); |
| 2162 | if (argcount == MAX_FUNC_ARGS) |
| 2163 | { |
| 2164 | emsg(_("E740: Too many argument types")); |
| 2165 | return &t_any; |
| 2166 | } |
| 2167 | } |
| 2168 | |
| 2169 | p = skipwhite(p); |
| 2170 | if (*p != ')') |
| 2171 | { |
| 2172 | emsg(_(e_missing_close)); |
| 2173 | return &t_any; |
| 2174 | } |
| 2175 | *arg = p + 1; |
| 2176 | } |
| 2177 | if (**arg == ':') |
| 2178 | { |
| 2179 | // parse return type |
| 2180 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2181 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2182 | semsg(_(e_white_after), ":"); |
| 2183 | *arg = skipwhite(*arg); |
| 2184 | ret_type = parse_type(arg, type_gap); |
| 2185 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2186 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2187 | type = get_func_type(ret_type, argcount, type_gap); |
| 2188 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2189 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2190 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 2191 | type->tt_flags = flags; |
| 2192 | if (argcount > 0) |
| 2193 | { |
| 2194 | type->tt_argcount = argcount; |
| 2195 | type->tt_min_argcount = first_optional == -1 |
| 2196 | ? argcount : first_optional; |
| 2197 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2198 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2199 | return &t_any; |
| 2200 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2201 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2202 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2203 | } |
| 2204 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2205 | } |
| 2206 | break; |
| 2207 | case 'j': |
| 2208 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 2209 | { |
| 2210 | *arg += len; |
| 2211 | return &t_job; |
| 2212 | } |
| 2213 | break; |
| 2214 | case 'l': |
| 2215 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 2216 | { |
| 2217 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2218 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2219 | } |
| 2220 | break; |
| 2221 | case 'n': |
| 2222 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 2223 | { |
| 2224 | *arg += len; |
| 2225 | return &t_number; |
| 2226 | } |
| 2227 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2228 | case 's': |
| 2229 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 2230 | { |
| 2231 | *arg += len; |
| 2232 | return &t_string; |
| 2233 | } |
| 2234 | break; |
| 2235 | case 'v': |
| 2236 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 2237 | { |
| 2238 | *arg += len; |
| 2239 | return &t_void; |
| 2240 | } |
| 2241 | break; |
| 2242 | } |
| 2243 | |
| 2244 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 2245 | return &t_any; |
| 2246 | } |
| 2247 | |
| 2248 | /* |
| 2249 | * Check if "type1" and "type2" are exactly the same. |
| 2250 | */ |
| 2251 | static int |
| 2252 | equal_type(type_T *type1, type_T *type2) |
| 2253 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2254 | int i; |
| 2255 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2256 | if (type1->tt_type != type2->tt_type) |
| 2257 | return FALSE; |
| 2258 | switch (type1->tt_type) |
| 2259 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2260 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2261 | case VAR_ANY: |
| 2262 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2263 | case VAR_SPECIAL: |
| 2264 | case VAR_BOOL: |
| 2265 | case VAR_NUMBER: |
| 2266 | case VAR_FLOAT: |
| 2267 | case VAR_STRING: |
| 2268 | case VAR_BLOB: |
| 2269 | case VAR_JOB: |
| 2270 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2271 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2272 | case VAR_LIST: |
| 2273 | case VAR_DICT: |
| 2274 | return equal_type(type1->tt_member, type2->tt_member); |
| 2275 | case VAR_FUNC: |
| 2276 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2277 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2278 | || type1->tt_argcount != type2->tt_argcount) |
| 2279 | return FALSE; |
| 2280 | if (type1->tt_argcount < 0 |
| 2281 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2282 | return TRUE; |
| 2283 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2284 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2285 | return FALSE; |
| 2286 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2287 | } |
| 2288 | return TRUE; |
| 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2293 | * "type2" and "dest" may be the same. |
| 2294 | */ |
| 2295 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2296 | 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] | 2297 | { |
| 2298 | if (equal_type(type1, type2)) |
| 2299 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2300 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2301 | return; |
| 2302 | } |
| 2303 | |
| 2304 | if (type1->tt_type == type2->tt_type) |
| 2305 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2306 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2307 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2308 | type_T *common; |
| 2309 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2310 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2311 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2312 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2313 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2314 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2315 | return; |
| 2316 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2317 | if (type1->tt_type == VAR_FUNC) |
| 2318 | { |
| 2319 | type_T *common; |
| 2320 | |
| 2321 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2322 | if (type1->tt_argcount == type2->tt_argcount |
| 2323 | && type1->tt_argcount >= 0) |
| 2324 | { |
| 2325 | int argcount = type1->tt_argcount; |
| 2326 | int i; |
| 2327 | |
| 2328 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2329 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2330 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2331 | if (func_type_add_arg_types(*dest, argcount, |
| 2332 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2333 | for (i = 0; i < argcount; ++i) |
| 2334 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2335 | &(*dest)->tt_args[i], type_gap); |
| 2336 | } |
| 2337 | } |
| 2338 | else |
| 2339 | *dest = alloc_func_type(common, -1, type_gap); |
| 2340 | return; |
| 2341 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2342 | } |
| 2343 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2344 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | char * |
| 2348 | vartype_name(vartype_T type) |
| 2349 | { |
| 2350 | switch (type) |
| 2351 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2352 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2353 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2354 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2355 | case VAR_SPECIAL: return "special"; |
| 2356 | case VAR_BOOL: return "bool"; |
| 2357 | case VAR_NUMBER: return "number"; |
| 2358 | case VAR_FLOAT: return "float"; |
| 2359 | case VAR_STRING: return "string"; |
| 2360 | case VAR_BLOB: return "blob"; |
| 2361 | case VAR_JOB: return "job"; |
| 2362 | case VAR_CHANNEL: return "channel"; |
| 2363 | case VAR_LIST: return "list"; |
| 2364 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2365 | |
| 2366 | case VAR_FUNC: |
| 2367 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2368 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2369 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | /* |
| 2373 | * Return the name of a type. |
| 2374 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2375 | */ |
| 2376 | char * |
| 2377 | type_name(type_T *type, char **tofree) |
| 2378 | { |
| 2379 | char *name = vartype_name(type->tt_type); |
| 2380 | |
| 2381 | *tofree = NULL; |
| 2382 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2383 | { |
| 2384 | char *member_free; |
| 2385 | char *member_name = type_name(type->tt_member, &member_free); |
| 2386 | size_t len; |
| 2387 | |
| 2388 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2389 | *tofree = alloc(len); |
| 2390 | if (*tofree != NULL) |
| 2391 | { |
| 2392 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2393 | vim_free(member_free); |
| 2394 | return *tofree; |
| 2395 | } |
| 2396 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2397 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2398 | { |
| 2399 | garray_T ga; |
| 2400 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2401 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2402 | |
| 2403 | ga_init2(&ga, 1, 100); |
| 2404 | if (ga_grow(&ga, 20) == FAIL) |
| 2405 | return "[unknown]"; |
| 2406 | *tofree = ga.ga_data; |
| 2407 | STRCPY(ga.ga_data, "func("); |
| 2408 | ga.ga_len += 5; |
| 2409 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2410 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2411 | { |
| 2412 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2413 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2414 | int len; |
| 2415 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2416 | if (type->tt_args == NULL) |
| 2417 | arg_type = "[unknown]"; |
| 2418 | else |
| 2419 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2420 | if (i > 0) |
| 2421 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2422 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2423 | ga.ga_len += 2; |
| 2424 | } |
| 2425 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2426 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2427 | { |
| 2428 | vim_free(arg_free); |
| 2429 | return "[unknown]"; |
| 2430 | } |
| 2431 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2432 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2433 | { |
| 2434 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2435 | ga.ga_len += 3; |
| 2436 | } |
| 2437 | else if (i >= type->tt_min_argcount) |
| 2438 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2439 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2440 | ga.ga_len += len; |
| 2441 | vim_free(arg_free); |
| 2442 | } |
| 2443 | |
| 2444 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2445 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2446 | else |
| 2447 | { |
| 2448 | char *ret_free; |
| 2449 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2450 | int len; |
| 2451 | |
| 2452 | len = (int)STRLEN(ret_name) + 4; |
| 2453 | if (ga_grow(&ga, len) == FAIL) |
| 2454 | { |
| 2455 | vim_free(ret_free); |
| 2456 | return "[unknown]"; |
| 2457 | } |
| 2458 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2459 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2460 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2461 | vim_free(ret_free); |
| 2462 | } |
| 2463 | return ga.ga_data; |
| 2464 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2465 | |
| 2466 | return name; |
| 2467 | } |
| 2468 | |
| 2469 | /* |
| 2470 | * Find "name" in script-local items of script "sid". |
| 2471 | * Returns the index in "sn_var_vals" if found. |
| 2472 | * If found but not in "sn_var_vals" returns -1. |
| 2473 | * If not found returns -2. |
| 2474 | */ |
| 2475 | int |
| 2476 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2477 | { |
| 2478 | hashtab_T *ht; |
| 2479 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2480 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2481 | int idx; |
| 2482 | |
| 2483 | // First look the name up in the hashtable. |
| 2484 | if (sid <= 0 || sid > script_items.ga_len) |
| 2485 | return -1; |
| 2486 | ht = &SCRIPT_VARS(sid); |
| 2487 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2488 | if (di == NULL) |
| 2489 | return -2; |
| 2490 | |
| 2491 | // Now find the svar_T index in sn_var_vals. |
| 2492 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2493 | { |
| 2494 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2495 | |
| 2496 | if (sv->sv_tv == &di->di_tv) |
| 2497 | { |
| 2498 | if (check_writable && sv->sv_const) |
| 2499 | semsg(_(e_readonlyvar), name); |
| 2500 | return idx; |
| 2501 | } |
| 2502 | } |
| 2503 | return -1; |
| 2504 | } |
| 2505 | |
| 2506 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2507 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2508 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2509 | */ |
| 2510 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2511 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2512 | { |
Bram Moolenaar | 086eb18 | 2020-07-01 16:00:44 +0200 | [diff] [blame] | 2513 | scriptitem_T *si; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2514 | int idx; |
| 2515 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2516 | if (current_sctx.sc_sid <= 0) |
| 2517 | return NULL; |
| 2518 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2519 | if (cctx != NULL) |
| 2520 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2521 | { |
| 2522 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2523 | + idx; |
| 2524 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2525 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2526 | : STRLEN(import->imp_name) == len |
| 2527 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2528 | return import; |
| 2529 | } |
| 2530 | |
| 2531 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2532 | { |
| 2533 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2534 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2535 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2536 | : STRLEN(import->imp_name) == len |
| 2537 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2538 | return import; |
| 2539 | } |
| 2540 | return NULL; |
| 2541 | } |
| 2542 | |
| 2543 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2544 | * Free all imported variables. |
| 2545 | */ |
| 2546 | static void |
| 2547 | free_imported(cctx_T *cctx) |
| 2548 | { |
| 2549 | int idx; |
| 2550 | |
| 2551 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2552 | { |
| 2553 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2554 | |
| 2555 | vim_free(import->imp_name); |
| 2556 | } |
| 2557 | ga_clear(&cctx->ctx_imports); |
| 2558 | } |
| 2559 | |
| 2560 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2561 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2562 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 2563 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2564 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2565 | { |
| 2566 | return p[0] == '#' && p[1] != '{'; |
| 2567 | } |
| 2568 | |
| 2569 | /* |
| 2570 | * Return a pointer to the next line that isn't empty or only contains a |
| 2571 | * comment. Skips over white space. |
| 2572 | * Returns NULL if there is none. |
| 2573 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2574 | char_u * |
| 2575 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2576 | { |
| 2577 | int lnum = cctx->ctx_lnum; |
| 2578 | |
| 2579 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2580 | { |
| 2581 | 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] | 2582 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2583 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 2584 | if (line == NULL) |
| 2585 | break; |
| 2586 | p = skipwhite(line); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2587 | if (*p != NUL && !vim9_comment_start(p)) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2588 | return p; |
| 2589 | } |
| 2590 | return NULL; |
| 2591 | } |
| 2592 | |
| 2593 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2594 | * Called when checking for a following operator at "arg". When the rest of |
| 2595 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2596 | * line return a pointer to it and set "nextp". |
| 2597 | * Otherwise skip over white space. |
| 2598 | */ |
| 2599 | static char_u * |
| 2600 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2601 | { |
| 2602 | char_u *p = skipwhite(arg); |
| 2603 | |
| 2604 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2605 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2606 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2607 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2608 | if (*nextp != NULL) |
| 2609 | return *nextp; |
| 2610 | } |
| 2611 | return p; |
| 2612 | } |
| 2613 | |
| 2614 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2615 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2616 | * 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] | 2617 | * Returns NULL when at the end. |
| 2618 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2619 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2620 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2621 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2622 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2623 | |
| 2624 | do |
| 2625 | { |
| 2626 | ++cctx->ctx_lnum; |
| 2627 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2628 | { |
| 2629 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2630 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2631 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2632 | 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] | 2633 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2634 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2635 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2636 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2637 | return line; |
| 2638 | } |
| 2639 | |
| 2640 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2641 | * 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] | 2642 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2643 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2644 | */ |
| 2645 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2646 | 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] | 2647 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2648 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2649 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2650 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2651 | |
| 2652 | if (next == NULL) |
| 2653 | return FAIL; |
| 2654 | *arg = skipwhite(next); |
| 2655 | } |
| 2656 | return OK; |
| 2657 | } |
| 2658 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2659 | /* |
| 2660 | * Idem, and give an error when failed. |
| 2661 | */ |
| 2662 | static int |
| 2663 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2664 | { |
| 2665 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2666 | { |
| 2667 | emsg(_("E1097: line incomplete")); |
| 2668 | return FAIL; |
| 2669 | } |
| 2670 | return OK; |
| 2671 | } |
| 2672 | |
| 2673 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2674 | // Structure passed between the compile_expr* functions to keep track of |
| 2675 | // constants that have been parsed but for which no code was produced yet. If |
| 2676 | // possible expressions on these constants are applied at compile time. If |
| 2677 | // that is not possible, the code to push the constants needs to be generated |
| 2678 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2679 | // Using 50 should be more than enough of 5 levels of (). |
| 2680 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2681 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2682 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2683 | int pp_used; // active entries in pp_tv[] |
| 2684 | } ppconst_T; |
| 2685 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2686 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2687 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2688 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2689 | /* |
| 2690 | * Generate a PUSH instruction for "tv". |
| 2691 | * "tv" will be consumed or cleared. |
| 2692 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2693 | */ |
| 2694 | static int |
| 2695 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2696 | { |
| 2697 | if (tv != NULL) |
| 2698 | { |
| 2699 | switch (tv->v_type) |
| 2700 | { |
| 2701 | case VAR_UNKNOWN: |
| 2702 | break; |
| 2703 | case VAR_BOOL: |
| 2704 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2705 | break; |
| 2706 | case VAR_SPECIAL: |
| 2707 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2708 | break; |
| 2709 | case VAR_NUMBER: |
| 2710 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2711 | break; |
| 2712 | #ifdef FEAT_FLOAT |
| 2713 | case VAR_FLOAT: |
| 2714 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2715 | break; |
| 2716 | #endif |
| 2717 | case VAR_BLOB: |
| 2718 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2719 | tv->vval.v_blob = NULL; |
| 2720 | break; |
| 2721 | case VAR_STRING: |
| 2722 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2723 | tv->vval.v_string = NULL; |
| 2724 | break; |
| 2725 | default: |
| 2726 | iemsg("constant type not supported"); |
| 2727 | clear_tv(tv); |
| 2728 | return FAIL; |
| 2729 | } |
| 2730 | tv->v_type = VAR_UNKNOWN; |
| 2731 | } |
| 2732 | return OK; |
| 2733 | } |
| 2734 | |
| 2735 | /* |
| 2736 | * Generate code for any ppconst entries. |
| 2737 | */ |
| 2738 | static int |
| 2739 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2740 | { |
| 2741 | int i; |
| 2742 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2743 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2744 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2745 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2746 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2747 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2748 | ret = FAIL; |
| 2749 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2750 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2751 | return ret; |
| 2752 | } |
| 2753 | |
| 2754 | /* |
| 2755 | * Clear ppconst constants. Used when failing. |
| 2756 | */ |
| 2757 | static void |
| 2758 | clear_ppconst(ppconst_T *ppconst) |
| 2759 | { |
| 2760 | int i; |
| 2761 | |
| 2762 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2763 | clear_tv(&ppconst->pp_tv[i]); |
| 2764 | ppconst->pp_used = 0; |
| 2765 | } |
| 2766 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2767 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2768 | * Generate an instruction to load script-local variable "name", without the |
| 2769 | * leading "s:". |
| 2770 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2771 | */ |
| 2772 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2773 | compile_load_scriptvar( |
| 2774 | cctx_T *cctx, |
| 2775 | char_u *name, // variable NUL terminated |
| 2776 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2777 | char_u **end, // end of variable |
| 2778 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2779 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2780 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2781 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2782 | imported_T *import; |
| 2783 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2784 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2785 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2786 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2787 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2788 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2789 | } |
| 2790 | if (idx >= 0) |
| 2791 | { |
| 2792 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2793 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2794 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2795 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2796 | return OK; |
| 2797 | } |
| 2798 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2799 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2800 | if (import != NULL) |
| 2801 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2802 | if (import->imp_all) |
| 2803 | { |
| 2804 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2805 | char_u *exp_name; |
| 2806 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2807 | ufunc_T *ufunc; |
| 2808 | type_T *type; |
| 2809 | |
| 2810 | // Used "import * as Name", need to lookup the member. |
| 2811 | if (*p != '.') |
| 2812 | { |
| 2813 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2814 | return FAIL; |
| 2815 | } |
| 2816 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2817 | if (VIM_ISWHITE(*p)) |
| 2818 | { |
| 2819 | emsg(_("E1074: no white space allowed after dot")); |
| 2820 | return FAIL; |
| 2821 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2822 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2823 | // isolate one name |
| 2824 | exp_name = p; |
| 2825 | while (eval_isnamec(*p)) |
| 2826 | ++p; |
| 2827 | cc = *p; |
| 2828 | *p = NUL; |
| 2829 | |
| 2830 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2831 | *p = cc; |
| 2832 | p = skipwhite(p); |
| 2833 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2834 | // TODO: what if it is a function? |
| 2835 | if (idx < 0) |
| 2836 | return FAIL; |
| 2837 | *end = p; |
| 2838 | |
| 2839 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2840 | import->imp_sid, |
| 2841 | idx, |
| 2842 | type); |
| 2843 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2844 | else if (import->imp_funcname != NULL) |
| 2845 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2846 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2847 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2848 | import->imp_sid, |
| 2849 | import->imp_var_vals_idx, |
| 2850 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2851 | return OK; |
| 2852 | } |
| 2853 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2854 | if (error) |
| 2855 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2856 | return FAIL; |
| 2857 | } |
| 2858 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2859 | static int |
| 2860 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2861 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2862 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2863 | |
| 2864 | if (ufunc == NULL) |
| 2865 | return FAIL; |
| 2866 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2867 | // Need to compile any default values to get the argument types. |
| 2868 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2869 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2870 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2871 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2872 | } |
| 2873 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2874 | /* |
| 2875 | * Compile a variable name into a load instruction. |
| 2876 | * "end" points to just after the name. |
| 2877 | * When "error" is FALSE do not give an error when not found. |
| 2878 | */ |
| 2879 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2880 | 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] | 2881 | { |
| 2882 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2883 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2884 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2885 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2886 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2887 | |
| 2888 | if (*(*arg + 1) == ':') |
| 2889 | { |
| 2890 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2891 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2892 | { |
| 2893 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2894 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2895 | switch (**arg) |
| 2896 | { |
| 2897 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2898 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2899 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2900 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2901 | default: |
| 2902 | semsg(_(e_namespace), *arg); |
| 2903 | goto theend; |
| 2904 | } |
| 2905 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2906 | goto theend; |
| 2907 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2908 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2909 | else |
| 2910 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2911 | isntype_T isn_type = ISN_DROP; |
| 2912 | |
| 2913 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2914 | if (name == NULL) |
| 2915 | return FAIL; |
| 2916 | |
| 2917 | switch (**arg) |
| 2918 | { |
| 2919 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2920 | break; |
| 2921 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2922 | NULL, NULL, error); |
| 2923 | break; |
| 2924 | case 'g': isn_type = ISN_LOADG; break; |
| 2925 | case 'w': isn_type = ISN_LOADW; break; |
| 2926 | case 't': isn_type = ISN_LOADT; break; |
| 2927 | case 'b': isn_type = ISN_LOADB; break; |
| 2928 | default: semsg(_(e_namespace), *arg); |
| 2929 | goto theend; |
| 2930 | } |
| 2931 | if (isn_type != ISN_DROP) |
| 2932 | { |
| 2933 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2934 | // variables can be defined later, thus we don't check if it |
| 2935 | // exists, give error at runtime. |
| 2936 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2937 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2938 | } |
| 2939 | } |
| 2940 | else |
| 2941 | { |
| 2942 | size_t len = end - *arg; |
| 2943 | int idx; |
| 2944 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2945 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2946 | |
| 2947 | name = vim_strnsave(*arg, end - *arg); |
| 2948 | if (name == NULL) |
| 2949 | return FAIL; |
| 2950 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2951 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2952 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2953 | if (!gen_load_outer) |
| 2954 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2955 | } |
| 2956 | else |
| 2957 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2958 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2959 | |
| 2960 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2961 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2962 | type = lvar->lv_type; |
| 2963 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2964 | if (lvar->lv_from_outer) |
| 2965 | gen_load_outer = TRUE; |
| 2966 | else |
| 2967 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2968 | } |
| 2969 | else |
| 2970 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2971 | // "var" can be script-local even without using "s:" if it |
| 2972 | // already exists. |
| 2973 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2974 | == SCRIPT_VERSION_VIM9 |
| 2975 | || lookup_script(*arg, len) == OK) |
| 2976 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2977 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2978 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2979 | // When the name starts with an uppercase letter or "x:" it |
| 2980 | // can be a user defined function. |
| 2981 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2982 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2983 | } |
| 2984 | } |
| 2985 | if (gen_load) |
| 2986 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2987 | if (gen_load_outer) |
| 2988 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | *arg = end; |
| 2992 | |
| 2993 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2994 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2995 | semsg(_(e_var_notfound), name); |
| 2996 | vim_free(name); |
| 2997 | return res; |
| 2998 | } |
| 2999 | |
| 3000 | /* |
| 3001 | * Compile the argument expressions. |
| 3002 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 3003 | */ |
| 3004 | static int |
| 3005 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 3006 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3007 | char_u *p = *arg; |
| 3008 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3009 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 3010 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3011 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3012 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 3013 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 3014 | if (*p == ')') |
| 3015 | { |
| 3016 | *arg = p + 1; |
| 3017 | return OK; |
| 3018 | } |
| 3019 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3020 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3021 | return FAIL; |
| 3022 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3023 | |
| 3024 | if (*p != ',' && *skipwhite(p) == ',') |
| 3025 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 3026 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3027 | p = skipwhite(p); |
| 3028 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3029 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3030 | { |
| 3031 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 3032 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 3033 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3034 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3035 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3036 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3037 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3038 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 3039 | emsg(_(e_missing_close)); |
| 3040 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | /* |
| 3044 | * Compile a function call: name(arg1, arg2) |
| 3045 | * "arg" points to "name", "arg + varlen" to the "(". |
| 3046 | * "argcount_init" is 1 for "value->method()" |
| 3047 | * Instructions: |
| 3048 | * EVAL arg1 |
| 3049 | * EVAL arg2 |
| 3050 | * BCALL / DCALL / UCALL |
| 3051 | */ |
| 3052 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3053 | compile_call( |
| 3054 | char_u **arg, |
| 3055 | size_t varlen, |
| 3056 | cctx_T *cctx, |
| 3057 | ppconst_T *ppconst, |
| 3058 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3059 | { |
| 3060 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 3061 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3062 | int argcount = argcount_init; |
| 3063 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3064 | char_u fname_buf[FLEN_FIXED + 1]; |
| 3065 | char_u *tofree = NULL; |
| 3066 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3067 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3068 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3069 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3070 | // we can evaluate "has('name')" at compile time |
| 3071 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 3072 | { |
| 3073 | char_u *s = skipwhite(*arg + varlen + 1); |
| 3074 | typval_T argvars[2]; |
| 3075 | |
| 3076 | argvars[0].v_type = VAR_UNKNOWN; |
| 3077 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3078 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3079 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3080 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3081 | s = skipwhite(s); |
| 3082 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 3083 | { |
| 3084 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 3085 | |
| 3086 | *arg = s + 1; |
| 3087 | argvars[1].v_type = VAR_UNKNOWN; |
| 3088 | tv->v_type = VAR_NUMBER; |
| 3089 | tv->vval.v_number = 0; |
| 3090 | f_has(argvars, tv); |
| 3091 | clear_tv(&argvars[0]); |
| 3092 | ++ppconst->pp_used; |
| 3093 | return OK; |
| 3094 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 3095 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3099 | return FAIL; |
| 3100 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3101 | if (varlen >= sizeof(namebuf)) |
| 3102 | { |
| 3103 | semsg(_("E1011: name too long: %s"), name); |
| 3104 | return FAIL; |
| 3105 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3106 | vim_strncpy(namebuf, *arg, varlen); |
| 3107 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3108 | |
| 3109 | *arg = skipwhite(*arg + varlen + 1); |
| 3110 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3111 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3112 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3113 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3114 | { |
| 3115 | int idx; |
| 3116 | |
| 3117 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3118 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3119 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 3120 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3121 | else |
| 3122 | semsg(_(e_unknownfunc), namebuf); |
| 3123 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3124 | } |
| 3125 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3126 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 3127 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3128 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3129 | { |
| 3130 | res = generate_CALL(cctx, ufunc, argcount); |
| 3131 | goto theend; |
| 3132 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3133 | |
| 3134 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3135 | // 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] | 3136 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3137 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 3138 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3139 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3140 | garray_T *stack = &cctx->ctx_type_stack; |
| 3141 | type_T *type; |
| 3142 | |
| 3143 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3144 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3145 | goto theend; |
| 3146 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3147 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3148 | // 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] | 3149 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3150 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 3151 | res = generate_UCALL(cctx, name, argcount); |
| 3152 | else |
| 3153 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3154 | |
| 3155 | theend: |
| 3156 | vim_free(tofree); |
| 3157 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 3161 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 3162 | |
| 3163 | /* |
| 3164 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 3165 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3166 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3167 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 3168 | * valid name. |
| 3169 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3170 | static char_u * |
| 3171 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3172 | { |
| 3173 | char_u *p; |
| 3174 | |
| 3175 | // Quick check for valid starting character. |
| 3176 | if (!eval_isnamec1(*arg)) |
| 3177 | return arg; |
| 3178 | |
| 3179 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 3180 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 3181 | // and can be used in slice "[n:]". |
| 3182 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3183 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3184 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 3185 | break; |
| 3186 | return p; |
| 3187 | } |
| 3188 | |
| 3189 | /* |
| 3190 | * 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] | 3191 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3192 | */ |
| 3193 | char_u * |
| 3194 | to_name_const_end(char_u *arg) |
| 3195 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3196 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3197 | typval_T rettv; |
| 3198 | |
| 3199 | if (p == arg && *arg == '[') |
| 3200 | { |
| 3201 | |
| 3202 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3203 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3204 | p = arg; |
| 3205 | } |
| 3206 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 3207 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3208 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3209 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3210 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3211 | p = arg; |
| 3212 | } |
| 3213 | else if (p == arg && *arg == '{') |
| 3214 | { |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3215 | int ret = get_lambda_tv(&p, &rettv, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3216 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3217 | // Can be "{x -> ret}()". |
| 3218 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3219 | if (ret == NOTDONE) |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3220 | ret = eval_dict(&p, &rettv, NULL, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3221 | if (ret != OK) |
| 3222 | p = arg; |
| 3223 | } |
| 3224 | |
| 3225 | return p; |
| 3226 | } |
| 3227 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3228 | /* |
| 3229 | * parse a list: [expr, expr] |
| 3230 | * "*arg" points to the '['. |
| 3231 | */ |
| 3232 | static int |
| 3233 | compile_list(char_u **arg, cctx_T *cctx) |
| 3234 | { |
| 3235 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3236 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3237 | int count = 0; |
| 3238 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3239 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3240 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3241 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3242 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3243 | semsg(_(e_list_end), *arg); |
| 3244 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3245 | } |
| 3246 | if (*p == ']') |
| 3247 | { |
| 3248 | ++p; |
| 3249 | // Allow for following comment, after at least one space. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3250 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '#') |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3251 | p += STRLEN(p); |
| 3252 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3253 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3254 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3255 | break; |
| 3256 | ++count; |
| 3257 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3258 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3259 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3260 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 3261 | { |
| 3262 | semsg(_(e_white_after), ","); |
| 3263 | return FAIL; |
| 3264 | } |
| 3265 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3266 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3267 | p = skipwhite(p); |
| 3268 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3269 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3270 | |
| 3271 | generate_NEWLIST(cctx, count); |
| 3272 | return OK; |
| 3273 | } |
| 3274 | |
| 3275 | /* |
| 3276 | * parse a lambda: {arg, arg -> expr} |
| 3277 | * "*arg" points to the '{'. |
| 3278 | */ |
| 3279 | static int |
| 3280 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 3281 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3282 | typval_T rettv; |
| 3283 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3284 | evalarg_T evalarg; |
| 3285 | |
| 3286 | CLEAR_FIELD(evalarg); |
| 3287 | evalarg.eval_flags = EVAL_EVALUATE; |
| 3288 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3289 | |
| 3290 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3291 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3292 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3293 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3294 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3295 | ++ufunc->uf_refcount; |
| 3296 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3297 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3298 | |
| 3299 | // The function will have one line: "return {expr}". |
| 3300 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3301 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3302 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 3303 | clear_evalarg(&evalarg, NULL); |
| 3304 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 3305 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 3306 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3307 | |
| 3308 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3309 | return FAIL; |
| 3310 | } |
| 3311 | |
| 3312 | /* |
| 3313 | * Compile a lamda call: expr->{lambda}(args) |
| 3314 | * "arg" points to the "{". |
| 3315 | */ |
| 3316 | static int |
| 3317 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 3318 | { |
| 3319 | ufunc_T *ufunc; |
| 3320 | typval_T rettv; |
| 3321 | int argcount = 1; |
| 3322 | int ret = FAIL; |
| 3323 | |
| 3324 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3325 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3326 | return FAIL; |
| 3327 | |
| 3328 | if (**arg != '(') |
| 3329 | { |
| 3330 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 3331 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3332 | else |
| 3333 | semsg(_(e_missing_paren), "lambda"); |
| 3334 | clear_tv(&rettv); |
| 3335 | return FAIL; |
| 3336 | } |
| 3337 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3338 | ufunc = rettv.vval.v_partial->pt_func; |
| 3339 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3340 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3341 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3342 | |
| 3343 | // The function will have one line: "return {expr}". |
| 3344 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3345 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3346 | |
| 3347 | // compile the arguments |
| 3348 | *arg = skipwhite(*arg + 1); |
| 3349 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 3350 | // call the compiled function |
| 3351 | ret = generate_CALL(cctx, ufunc, argcount); |
| 3352 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3353 | if (ret == FAIL) |
| 3354 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3355 | return ret; |
| 3356 | } |
| 3357 | |
| 3358 | /* |
| 3359 | * parse a dict: {'key': val} or #{key: val} |
| 3360 | * "*arg" points to the '{'. |
| 3361 | */ |
| 3362 | static int |
| 3363 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 3364 | { |
| 3365 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3366 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3367 | int count = 0; |
| 3368 | dict_T *d = dict_alloc(); |
| 3369 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3370 | char_u *whitep = *arg; |
| 3371 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3372 | |
| 3373 | if (d == NULL) |
| 3374 | return FAIL; |
| 3375 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3376 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3377 | { |
| 3378 | char_u *key = NULL; |
| 3379 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3380 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3381 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3382 | *arg = NULL; |
| 3383 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | if (**arg == '}') |
| 3387 | break; |
| 3388 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3389 | if (literal) |
| 3390 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3391 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3392 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3393 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3394 | { |
| 3395 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 3396 | return FAIL; |
| 3397 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3398 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3399 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3400 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3401 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3402 | } |
| 3403 | else |
| 3404 | { |
| 3405 | isn_T *isn; |
| 3406 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3407 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3408 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3409 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3410 | if (isn->isn_type == ISN_PUSHS) |
| 3411 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3412 | else |
| 3413 | { |
| 3414 | type_T *keytype = ((type_T **)stack->ga_data) |
| 3415 | [stack->ga_len - 1]; |
| 3416 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 3417 | return FAIL; |
| 3418 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | // Check for duplicate keys, if using string keys. |
| 3422 | if (key != NULL) |
| 3423 | { |
| 3424 | item = dict_find(d, key, -1); |
| 3425 | if (item != NULL) |
| 3426 | { |
| 3427 | semsg(_(e_duplicate_key), key); |
| 3428 | goto failret; |
| 3429 | } |
| 3430 | item = dictitem_alloc(key); |
| 3431 | if (item != NULL) |
| 3432 | { |
| 3433 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3434 | item->di_tv.v_lock = 0; |
| 3435 | if (dict_add(d, item) == FAIL) |
| 3436 | dictitem_free(item); |
| 3437 | } |
| 3438 | } |
| 3439 | |
| 3440 | *arg = skipwhite(*arg); |
| 3441 | if (**arg != ':') |
| 3442 | { |
| 3443 | semsg(_(e_missing_dict_colon), *arg); |
| 3444 | return FAIL; |
| 3445 | } |
| 3446 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3447 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3448 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3449 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3450 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3451 | *arg = NULL; |
| 3452 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3453 | } |
| 3454 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3455 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3456 | return FAIL; |
| 3457 | ++count; |
| 3458 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3459 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3460 | *arg = skipwhite(*arg); |
| 3461 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3462 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3463 | *arg = NULL; |
| 3464 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3465 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3466 | if (**arg == '}') |
| 3467 | break; |
| 3468 | if (**arg != ',') |
| 3469 | { |
| 3470 | semsg(_(e_missing_dict_comma), *arg); |
| 3471 | goto failret; |
| 3472 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3473 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3474 | *arg = skipwhite(*arg + 1); |
| 3475 | } |
| 3476 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3477 | *arg = *arg + 1; |
| 3478 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3479 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3480 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3481 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3482 | *arg += STRLEN(*arg); |
| 3483 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3484 | dict_unref(d); |
| 3485 | return generate_NEWDICT(cctx, count); |
| 3486 | |
| 3487 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3488 | if (*arg == NULL) |
| 3489 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3490 | dict_unref(d); |
| 3491 | return FAIL; |
| 3492 | } |
| 3493 | |
| 3494 | /* |
| 3495 | * Compile "&option". |
| 3496 | */ |
| 3497 | static int |
| 3498 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3499 | { |
| 3500 | typval_T rettv; |
| 3501 | char_u *start = *arg; |
| 3502 | int ret; |
| 3503 | |
| 3504 | // parse the option and get the current value to get the type. |
| 3505 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3506 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3507 | if (ret == OK) |
| 3508 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3509 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3510 | char_u *name = vim_strnsave(start, *arg - start); |
| 3511 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3512 | |
| 3513 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3514 | vim_free(name); |
| 3515 | } |
| 3516 | clear_tv(&rettv); |
| 3517 | |
| 3518 | return ret; |
| 3519 | } |
| 3520 | |
| 3521 | /* |
| 3522 | * Compile "$VAR". |
| 3523 | */ |
| 3524 | static int |
| 3525 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3526 | { |
| 3527 | char_u *start = *arg; |
| 3528 | int len; |
| 3529 | int ret; |
| 3530 | char_u *name; |
| 3531 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3532 | ++*arg; |
| 3533 | len = get_env_len(arg); |
| 3534 | if (len == 0) |
| 3535 | { |
| 3536 | semsg(_(e_syntax_at), start - 1); |
| 3537 | return FAIL; |
| 3538 | } |
| 3539 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3540 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3541 | name = vim_strnsave(start, len + 1); |
| 3542 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3543 | vim_free(name); |
| 3544 | return ret; |
| 3545 | } |
| 3546 | |
| 3547 | /* |
| 3548 | * Compile "@r". |
| 3549 | */ |
| 3550 | static int |
| 3551 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3552 | { |
| 3553 | int ret; |
| 3554 | |
| 3555 | ++*arg; |
| 3556 | if (**arg == NUL) |
| 3557 | { |
| 3558 | semsg(_(e_syntax_at), *arg - 1); |
| 3559 | return FAIL; |
| 3560 | } |
| 3561 | if (!valid_yank_reg(**arg, TRUE)) |
| 3562 | { |
| 3563 | emsg_invreg(**arg); |
| 3564 | return FAIL; |
| 3565 | } |
| 3566 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3567 | ++*arg; |
| 3568 | return ret; |
| 3569 | } |
| 3570 | |
| 3571 | /* |
| 3572 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3573 | */ |
| 3574 | static int |
| 3575 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3576 | { |
| 3577 | char_u *p = end; |
| 3578 | |
| 3579 | // this works from end to start |
| 3580 | while (p > start) |
| 3581 | { |
| 3582 | --p; |
| 3583 | if (*p == '-' || *p == '+') |
| 3584 | { |
| 3585 | // only '-' has an effect, for '+' we only check the type |
| 3586 | #ifdef FEAT_FLOAT |
| 3587 | if (rettv->v_type == VAR_FLOAT) |
| 3588 | { |
| 3589 | if (*p == '-') |
| 3590 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3591 | } |
| 3592 | else |
| 3593 | #endif |
| 3594 | { |
| 3595 | varnumber_T val; |
| 3596 | int error = FALSE; |
| 3597 | |
| 3598 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3599 | // here |
| 3600 | if (check_not_string(rettv) == FAIL) |
| 3601 | return FAIL; |
| 3602 | val = tv_get_number_chk(rettv, &error); |
| 3603 | clear_tv(rettv); |
| 3604 | if (error) |
| 3605 | return FAIL; |
| 3606 | if (*p == '-') |
| 3607 | val = -val; |
| 3608 | rettv->v_type = VAR_NUMBER; |
| 3609 | rettv->vval.v_number = val; |
| 3610 | } |
| 3611 | } |
| 3612 | else |
| 3613 | { |
| 3614 | int v = tv2bool(rettv); |
| 3615 | |
| 3616 | // '!' is permissive in the type. |
| 3617 | clear_tv(rettv); |
| 3618 | rettv->v_type = VAR_BOOL; |
| 3619 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3620 | } |
| 3621 | } |
| 3622 | return OK; |
| 3623 | } |
| 3624 | |
| 3625 | /* |
| 3626 | * Recognize v: variables that are constants and set "rettv". |
| 3627 | */ |
| 3628 | static void |
| 3629 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3630 | { |
| 3631 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3632 | { |
| 3633 | rettv->v_type = VAR_BOOL; |
| 3634 | rettv->vval.v_number = VVAL_TRUE; |
| 3635 | *arg += 6; |
| 3636 | } |
| 3637 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3638 | { |
| 3639 | rettv->v_type = VAR_BOOL; |
| 3640 | rettv->vval.v_number = VVAL_FALSE; |
| 3641 | *arg += 7; |
| 3642 | } |
| 3643 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3644 | { |
| 3645 | rettv->v_type = VAR_SPECIAL; |
| 3646 | rettv->vval.v_number = VVAL_NULL; |
| 3647 | *arg += 6; |
| 3648 | } |
| 3649 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3650 | { |
| 3651 | rettv->v_type = VAR_SPECIAL; |
| 3652 | rettv->vval.v_number = VVAL_NONE; |
| 3653 | *arg += 6; |
| 3654 | } |
| 3655 | } |
| 3656 | |
Bram Moolenaar | 696ba23 | 2020-07-29 21:20:41 +0200 | [diff] [blame] | 3657 | exptype_T |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3658 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3659 | { |
| 3660 | exptype_T type = EXPR_UNKNOWN; |
| 3661 | int i; |
| 3662 | |
| 3663 | switch (p[0]) |
| 3664 | { |
| 3665 | case '=': if (p[1] == '=') |
| 3666 | type = EXPR_EQUAL; |
| 3667 | else if (p[1] == '~') |
| 3668 | type = EXPR_MATCH; |
| 3669 | break; |
| 3670 | case '!': if (p[1] == '=') |
| 3671 | type = EXPR_NEQUAL; |
| 3672 | else if (p[1] == '~') |
| 3673 | type = EXPR_NOMATCH; |
| 3674 | break; |
| 3675 | case '>': if (p[1] != '=') |
| 3676 | { |
| 3677 | type = EXPR_GREATER; |
| 3678 | *len = 1; |
| 3679 | } |
| 3680 | else |
| 3681 | type = EXPR_GEQUAL; |
| 3682 | break; |
| 3683 | case '<': if (p[1] != '=') |
| 3684 | { |
| 3685 | type = EXPR_SMALLER; |
| 3686 | *len = 1; |
| 3687 | } |
| 3688 | else |
| 3689 | type = EXPR_SEQUAL; |
| 3690 | break; |
| 3691 | case 'i': if (p[1] == 's') |
| 3692 | { |
| 3693 | // "is" and "isnot"; but not a prefix of a name |
| 3694 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3695 | *len = 5; |
| 3696 | i = p[*len]; |
| 3697 | if (!isalnum(i) && i != '_') |
| 3698 | { |
| 3699 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3700 | *type_is = TRUE; |
| 3701 | } |
| 3702 | } |
| 3703 | break; |
| 3704 | } |
| 3705 | return type; |
| 3706 | } |
| 3707 | |
| 3708 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3709 | * Compile code to apply '-', '+' and '!'. |
| 3710 | */ |
| 3711 | static int |
| 3712 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3713 | { |
| 3714 | char_u *p = end; |
| 3715 | |
| 3716 | // this works from end to start |
| 3717 | while (p > start) |
| 3718 | { |
| 3719 | --p; |
| 3720 | if (*p == '-' || *p == '+') |
| 3721 | { |
| 3722 | int negate = *p == '-'; |
| 3723 | isn_T *isn; |
| 3724 | |
| 3725 | // TODO: check type |
| 3726 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3727 | { |
| 3728 | --p; |
| 3729 | if (*p == '-') |
| 3730 | negate = !negate; |
| 3731 | } |
| 3732 | // only '-' has an effect, for '+' we only check the type |
| 3733 | if (negate) |
| 3734 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3735 | else |
| 3736 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3737 | if (isn == NULL) |
| 3738 | return FAIL; |
| 3739 | } |
| 3740 | else |
| 3741 | { |
| 3742 | int invert = TRUE; |
| 3743 | |
| 3744 | while (p > start && p[-1] == '!') |
| 3745 | { |
| 3746 | --p; |
| 3747 | invert = !invert; |
| 3748 | } |
| 3749 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3750 | return FAIL; |
| 3751 | } |
| 3752 | } |
| 3753 | return OK; |
| 3754 | } |
| 3755 | |
| 3756 | /* |
| 3757 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3758 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3759 | */ |
| 3760 | static int |
| 3761 | compile_subscript( |
| 3762 | char_u **arg, |
| 3763 | cctx_T *cctx, |
| 3764 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3765 | char_u *end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3766 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3767 | { |
| 3768 | for (;;) |
| 3769 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3770 | char_u *p = skipwhite(*arg); |
| 3771 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3772 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3773 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3774 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3775 | |
| 3776 | // If a following line starts with "->{" or "->X" advance to that |
| 3777 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3778 | // Also if a following line starts with ".x". |
| 3779 | if (next != NULL && |
| 3780 | ((next[0] == '-' && next[1] == '>' |
| 3781 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3782 | || (next[0] == '.' && eval_isdictc(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3783 | { |
| 3784 | next = next_line_from_context(cctx, TRUE); |
| 3785 | if (next == NULL) |
| 3786 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3787 | *arg = next; |
| 3788 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3789 | } |
| 3790 | } |
| 3791 | |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 3792 | // Do not skip over white space to find the "(", "exeucte 'x' ()" is |
| 3793 | // not a function call. |
| 3794 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3795 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3796 | garray_T *stack = &cctx->ctx_type_stack; |
| 3797 | type_T *type; |
| 3798 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3799 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3800 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3801 | return FAIL; |
| 3802 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3803 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3804 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3805 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3806 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3807 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3808 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3809 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3810 | return FAIL; |
| 3811 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3812 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3813 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3814 | char_u *pstart = p; |
| 3815 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3816 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3817 | return FAIL; |
| 3818 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3819 | // something->method() |
| 3820 | // Apply the '!', '-' and '+' first: |
| 3821 | // -1.0->func() works like (-1.0)->func() |
| 3822 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3823 | return FAIL; |
| 3824 | *start_leader = end_leader; // don't apply again later |
| 3825 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3826 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3827 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3828 | // No line break supported right after "->". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3829 | if (**arg == '{') |
| 3830 | { |
| 3831 | // lambda call: list->{lambda} |
| 3832 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3833 | return FAIL; |
| 3834 | } |
| 3835 | else |
| 3836 | { |
| 3837 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3838 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3839 | if (!eval_isnamec1(*p)) |
| 3840 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3841 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3842 | return FAIL; |
| 3843 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3844 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3845 | p += 2; |
| 3846 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3847 | ; |
| 3848 | if (*p != '(') |
| 3849 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3850 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3851 | return FAIL; |
| 3852 | } |
| 3853 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3854 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3855 | return FAIL; |
| 3856 | } |
| 3857 | } |
Bram Moolenaar | badd848 | 2020-07-31 22:38:17 +0200 | [diff] [blame] | 3858 | else if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3859 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3860 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3861 | type_T **typep; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3862 | vartype_T vtype; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3863 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3864 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3865 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3866 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3867 | // TODO: blob index |
| 3868 | // TODO: more arguments |
| 3869 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3870 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3871 | return FAIL; |
| 3872 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3873 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3874 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3875 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3876 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3877 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3878 | return FAIL; |
| 3879 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3880 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3881 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3882 | if (**arg != ']') |
| 3883 | { |
| 3884 | emsg(_(e_missbrac)); |
| 3885 | return FAIL; |
| 3886 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3887 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3888 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3889 | // We can index a list and a dict. If we don't know the type |
| 3890 | // we can use the index value type. |
| 3891 | // TODO: If we don't know use an instruction to figure it out at |
| 3892 | // runtime. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3893 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3894 | vtype = (*typep)->tt_type; |
| 3895 | if (*typep == &t_any) |
| 3896 | { |
| 3897 | type_T *valtype = ((type_T **)stack->ga_data) |
| 3898 | [stack->ga_len - 1]; |
| 3899 | if (valtype == &t_string) |
| 3900 | vtype = VAR_DICT; |
| 3901 | } |
| 3902 | if (vtype == VAR_DICT) |
| 3903 | { |
| 3904 | if ((*typep)->tt_type == VAR_DICT) |
| 3905 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3906 | else |
| 3907 | { |
| 3908 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3909 | return FAIL; |
| 3910 | *typep = &t_any; |
| 3911 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3912 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3913 | return FAIL; |
| 3914 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3915 | return FAIL; |
| 3916 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3917 | else if (vtype == VAR_STRING) |
| 3918 | { |
| 3919 | *typep = &t_number; |
| 3920 | if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL) |
| 3921 | return FAIL; |
| 3922 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3923 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3924 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3925 | if ((*typep)->tt_type == VAR_LIST) |
| 3926 | *typep = (*typep)->tt_member; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3927 | if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3928 | return FAIL; |
| 3929 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3930 | else |
| 3931 | { |
| 3932 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3933 | return FAIL; |
| 3934 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3935 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3936 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3937 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3938 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3939 | return FAIL; |
| 3940 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3941 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3942 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3943 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3944 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3945 | p = *arg; |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3946 | if (eval_isdictc(*p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3947 | while (eval_isnamec(*p)) |
| 3948 | MB_PTR_ADV(p); |
| 3949 | if (p == *arg) |
| 3950 | { |
| 3951 | semsg(_(e_syntax_at), *arg); |
| 3952 | return FAIL; |
| 3953 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3954 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3955 | return FAIL; |
| 3956 | *arg = p; |
| 3957 | } |
| 3958 | else |
| 3959 | break; |
| 3960 | } |
| 3961 | |
| 3962 | // TODO - see handle_subscript(): |
| 3963 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3964 | // Don't do this when "Func" is already a partial that was bound |
| 3965 | // explicitly (pt_auto is FALSE). |
| 3966 | |
| 3967 | return OK; |
| 3968 | } |
| 3969 | |
| 3970 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3971 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3972 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3973 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3974 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3975 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3976 | * |
| 3977 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3978 | */ |
| 3979 | |
| 3980 | /* |
| 3981 | * number number constant |
| 3982 | * 0zFFFFFFFF Blob constant |
| 3983 | * "string" string constant |
| 3984 | * 'string' literal string constant |
| 3985 | * &option-name option value |
| 3986 | * @r register contents |
| 3987 | * identifier variable value |
| 3988 | * function() function call |
| 3989 | * $VAR environment variable |
| 3990 | * (expression) nested expression |
| 3991 | * [expr, expr] List |
| 3992 | * {key: val, key: val} Dictionary |
| 3993 | * #{key: val, key: val} Dictionary with literal keys |
| 3994 | * |
| 3995 | * Also handle: |
| 3996 | * ! in front logical NOT |
| 3997 | * - in front unary minus |
| 3998 | * + in front unary plus (ignored) |
| 3999 | * trailing (arg) funcref/partial call |
| 4000 | * trailing [] subscript in String or List |
| 4001 | * trailing .name entry in Dictionary |
| 4002 | * trailing ->name() method call |
| 4003 | */ |
| 4004 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4005 | compile_expr7( |
| 4006 | char_u **arg, |
| 4007 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4008 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4009 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4010 | char_u *start_leader, *end_leader; |
| 4011 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4012 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4013 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4014 | |
| 4015 | /* |
| 4016 | * Skip '!', '-' and '+' characters. They are handled later. |
| 4017 | */ |
| 4018 | start_leader = *arg; |
| 4019 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 4020 | *arg = skipwhite(*arg + 1); |
| 4021 | end_leader = *arg; |
| 4022 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4023 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4024 | switch (**arg) |
| 4025 | { |
| 4026 | /* |
| 4027 | * Number constant. |
| 4028 | */ |
| 4029 | case '0': // also for blob starting with 0z |
| 4030 | case '1': |
| 4031 | case '2': |
| 4032 | case '3': |
| 4033 | case '4': |
| 4034 | case '5': |
| 4035 | case '6': |
| 4036 | case '7': |
| 4037 | case '8': |
| 4038 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4039 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4040 | return FAIL; |
| 4041 | break; |
| 4042 | |
| 4043 | /* |
| 4044 | * String constant: "string". |
| 4045 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4046 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4047 | return FAIL; |
| 4048 | break; |
| 4049 | |
| 4050 | /* |
| 4051 | * Literal string constant: 'str''ing'. |
| 4052 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4053 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4054 | return FAIL; |
| 4055 | break; |
| 4056 | |
| 4057 | /* |
| 4058 | * Constant Vim variable. |
| 4059 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4060 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4061 | ret = NOTDONE; |
| 4062 | break; |
| 4063 | |
| 4064 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4065 | * "true" constant |
| 4066 | */ |
| 4067 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 4068 | && !eval_isnamec((*arg)[4])) |
| 4069 | { |
| 4070 | *arg += 4; |
| 4071 | rettv->v_type = VAR_BOOL; |
| 4072 | rettv->vval.v_number = VVAL_TRUE; |
| 4073 | } |
| 4074 | else |
| 4075 | ret = NOTDONE; |
| 4076 | break; |
| 4077 | |
| 4078 | /* |
| 4079 | * "false" constant |
| 4080 | */ |
| 4081 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 4082 | && !eval_isnamec((*arg)[5])) |
| 4083 | { |
| 4084 | *arg += 5; |
| 4085 | rettv->v_type = VAR_BOOL; |
| 4086 | rettv->vval.v_number = VVAL_FALSE; |
| 4087 | } |
| 4088 | else |
| 4089 | ret = NOTDONE; |
| 4090 | break; |
| 4091 | |
| 4092 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4093 | * List: [expr, expr] |
| 4094 | */ |
| 4095 | case '[': ret = compile_list(arg, cctx); |
| 4096 | break; |
| 4097 | |
| 4098 | /* |
| 4099 | * Dictionary: #{key: val, key: val} |
| 4100 | */ |
| 4101 | case '#': if ((*arg)[1] == '{') |
| 4102 | { |
| 4103 | ++*arg; |
| 4104 | ret = compile_dict(arg, cctx, TRUE); |
| 4105 | } |
| 4106 | else |
| 4107 | ret = NOTDONE; |
| 4108 | break; |
| 4109 | |
| 4110 | /* |
| 4111 | * Lambda: {arg, arg -> expr} |
| 4112 | * Dictionary: {'key': val, 'key': val} |
| 4113 | */ |
| 4114 | case '{': { |
| 4115 | char_u *start = skipwhite(*arg + 1); |
| 4116 | |
| 4117 | // Find out what comes after the arguments. |
| 4118 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 4119 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4120 | if (ret != FAIL && *start == '>') |
| 4121 | ret = compile_lambda(arg, cctx); |
| 4122 | else |
| 4123 | ret = compile_dict(arg, cctx, FALSE); |
| 4124 | } |
| 4125 | break; |
| 4126 | |
| 4127 | /* |
| 4128 | * Option value: &name |
| 4129 | */ |
| 4130 | case '&': ret = compile_get_option(arg, cctx); |
| 4131 | break; |
| 4132 | |
| 4133 | /* |
| 4134 | * Environment variable: $VAR. |
| 4135 | */ |
| 4136 | case '$': ret = compile_get_env(arg, cctx); |
| 4137 | break; |
| 4138 | |
| 4139 | /* |
| 4140 | * Register contents: @r. |
| 4141 | */ |
| 4142 | case '@': ret = compile_get_register(arg, cctx); |
| 4143 | break; |
| 4144 | /* |
| 4145 | * nested expression: (expression). |
| 4146 | */ |
| 4147 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4148 | |
| 4149 | // recursive! |
| 4150 | if (ppconst->pp_used <= PPSIZE - 10) |
| 4151 | { |
| 4152 | ret = compile_expr1(arg, cctx, ppconst); |
| 4153 | } |
| 4154 | else |
| 4155 | { |
| 4156 | // Not enough space in ppconst, flush constants. |
| 4157 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4158 | return FAIL; |
| 4159 | ret = compile_expr0(arg, cctx); |
| 4160 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4161 | *arg = skipwhite(*arg); |
| 4162 | if (**arg == ')') |
| 4163 | ++*arg; |
| 4164 | else if (ret == OK) |
| 4165 | { |
| 4166 | emsg(_(e_missing_close)); |
| 4167 | ret = FAIL; |
| 4168 | } |
| 4169 | break; |
| 4170 | |
| 4171 | default: ret = NOTDONE; |
| 4172 | break; |
| 4173 | } |
| 4174 | if (ret == FAIL) |
| 4175 | return FAIL; |
| 4176 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4177 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4178 | { |
| 4179 | // apply the '!', '-' and '+' before the constant |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4180 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4181 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4182 | clear_tv(rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4183 | return FAIL; |
| 4184 | } |
| 4185 | start_leader = end_leader; // don't apply again below |
| 4186 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4187 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4188 | clear_tv(rettv); |
| 4189 | else |
| 4190 | // A constant expression can possibly be handled compile time, |
| 4191 | // return the value instead of generating code. |
| 4192 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4193 | } |
| 4194 | else if (ret == NOTDONE) |
| 4195 | { |
| 4196 | char_u *p; |
| 4197 | int r; |
| 4198 | |
| 4199 | if (!eval_isnamec1(**arg)) |
| 4200 | { |
| 4201 | semsg(_("E1015: Name expected: %s"), *arg); |
| 4202 | return FAIL; |
| 4203 | } |
| 4204 | |
| 4205 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4206 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4207 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4208 | { |
| 4209 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 4210 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4211 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4212 | { |
| 4213 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4214 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4215 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4216 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4217 | if (r == FAIL) |
| 4218 | return FAIL; |
| 4219 | } |
| 4220 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4221 | // Handle following "[]", ".member", etc. |
| 4222 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4223 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4224 | ppconst) == FAIL) |
| 4225 | return FAIL; |
| 4226 | if (ppconst->pp_used > 0) |
| 4227 | { |
| 4228 | // apply the '!', '-' and '+' before the constant |
| 4229 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4230 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
| 4231 | return FAIL; |
| 4232 | return OK; |
| 4233 | } |
| 4234 | if (compile_leader(cctx, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4235 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4236 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4237 | } |
| 4238 | |
| 4239 | /* |
| 4240 | * * number multiplication |
| 4241 | * / number division |
| 4242 | * % number modulo |
| 4243 | */ |
| 4244 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4245 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4246 | { |
| 4247 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4248 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4249 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4250 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4251 | // get the first expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4252 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4253 | return FAIL; |
| 4254 | |
| 4255 | /* |
| 4256 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4257 | */ |
| 4258 | for (;;) |
| 4259 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4260 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4261 | if (*op != '*' && *op != '/' && *op != '%') |
| 4262 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4263 | if (next != NULL) |
| 4264 | { |
| 4265 | *arg = next_line_from_context(cctx, TRUE); |
| 4266 | op = skipwhite(*arg); |
| 4267 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4268 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4269 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4270 | { |
| 4271 | char_u buf[3]; |
| 4272 | |
| 4273 | vim_strncpy(buf, op, 1); |
| 4274 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4275 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4276 | } |
| 4277 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4278 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
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 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4281 | // get the second expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4282 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4283 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4284 | |
| 4285 | if (ppconst->pp_used == ppconst_used + 2 |
| 4286 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4287 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4288 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4289 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4290 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4291 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4292 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4293 | // both are numbers: compute the result |
| 4294 | switch (*op) |
| 4295 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4296 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4297 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4298 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4299 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4300 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4301 | break; |
| 4302 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4303 | tv1->vval.v_number = res; |
| 4304 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4305 | } |
| 4306 | else |
| 4307 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4308 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4309 | generate_two_op(cctx, op); |
| 4310 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | return OK; |
| 4314 | } |
| 4315 | |
| 4316 | /* |
| 4317 | * + number addition |
| 4318 | * - number subtraction |
| 4319 | * .. string concatenation |
| 4320 | */ |
| 4321 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4322 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4323 | { |
| 4324 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4325 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4326 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4327 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4328 | |
| 4329 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4330 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4331 | return FAIL; |
| 4332 | |
| 4333 | /* |
| 4334 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4335 | */ |
| 4336 | for (;;) |
| 4337 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4338 | op = may_peek_next_line(cctx, *arg, &next); |
| 4339 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4340 | break; |
| 4341 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4342 | if (next != NULL) |
| 4343 | { |
| 4344 | *arg = next_line_from_context(cctx, TRUE); |
| 4345 | op = skipwhite(*arg); |
| 4346 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4347 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4348 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4349 | { |
| 4350 | char_u buf[3]; |
| 4351 | |
| 4352 | vim_strncpy(buf, op, oplen); |
| 4353 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4354 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4355 | } |
| 4356 | |
| 4357 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4358 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4359 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4360 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4361 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4362 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4363 | return FAIL; |
| 4364 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4365 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4366 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4367 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4368 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4369 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4370 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4371 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4372 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4373 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4374 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4375 | // concat/subtract/add constant numbers |
| 4376 | if (*op == '+') |
| 4377 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4378 | else if (*op == '-') |
| 4379 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4380 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4381 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4382 | // concatenate constant strings |
| 4383 | char_u *s1 = tv1->vval.v_string; |
| 4384 | char_u *s2 = tv2->vval.v_string; |
| 4385 | size_t len1 = STRLEN(s1); |
| 4386 | |
| 4387 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4388 | if (tv1->vval.v_string == NULL) |
| 4389 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4390 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4391 | return FAIL; |
| 4392 | } |
| 4393 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4394 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4395 | vim_free(s1); |
| 4396 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4397 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4398 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4399 | } |
| 4400 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4401 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4402 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4403 | if (*op == '.') |
| 4404 | { |
| 4405 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4406 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4407 | return FAIL; |
| 4408 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4409 | } |
| 4410 | else |
| 4411 | generate_two_op(cctx, op); |
| 4412 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | return OK; |
| 4416 | } |
| 4417 | |
| 4418 | /* |
| 4419 | * expr5a == expr5b |
| 4420 | * expr5a =~ expr5b |
| 4421 | * expr5a != expr5b |
| 4422 | * expr5a !~ expr5b |
| 4423 | * expr5a > expr5b |
| 4424 | * expr5a >= expr5b |
| 4425 | * expr5a < expr5b |
| 4426 | * expr5a <= expr5b |
| 4427 | * expr5a is expr5b |
| 4428 | * expr5a isnot expr5b |
| 4429 | * |
| 4430 | * Produces instructions: |
| 4431 | * EVAL expr5a Push result of "expr5a" |
| 4432 | * EVAL expr5b Push result of "expr5b" |
| 4433 | * COMPARE one of the compare instructions |
| 4434 | */ |
| 4435 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4436 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4437 | { |
| 4438 | exptype_T type = EXPR_UNKNOWN; |
| 4439 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4440 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4441 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4442 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4443 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4444 | |
| 4445 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4446 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4447 | return FAIL; |
| 4448 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4449 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4450 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4451 | |
| 4452 | /* |
| 4453 | * If there is a comparative operator, use it. |
| 4454 | */ |
| 4455 | if (type != EXPR_UNKNOWN) |
| 4456 | { |
| 4457 | int ic = FALSE; // Default: do not ignore case |
| 4458 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4459 | if (next != NULL) |
| 4460 | { |
| 4461 | *arg = next_line_from_context(cctx, TRUE); |
| 4462 | p = skipwhite(*arg); |
| 4463 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4464 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4465 | { |
| 4466 | semsg(_(e_invexpr2), *arg); |
| 4467 | return FAIL; |
| 4468 | } |
| 4469 | // extra question mark appended: ignore case |
| 4470 | if (p[len] == '?') |
| 4471 | { |
| 4472 | ic = TRUE; |
| 4473 | ++len; |
| 4474 | } |
| 4475 | // extra '#' appended: match case (ignored) |
| 4476 | else if (p[len] == '#') |
| 4477 | ++len; |
| 4478 | // nothing appended: match case |
| 4479 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4480 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4481 | { |
| 4482 | char_u buf[7]; |
| 4483 | |
| 4484 | vim_strncpy(buf, p, len); |
| 4485 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4486 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | // get the second variable |
| 4490 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4491 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4492 | return FAIL; |
| 4493 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4494 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4495 | return FAIL; |
| 4496 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4497 | if (ppconst->pp_used == ppconst_used + 2) |
| 4498 | { |
| 4499 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4500 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4501 | int ret; |
| 4502 | |
| 4503 | // Both sides are a constant, compute the result now. |
| 4504 | // First check for a valid combination of types, this is more |
| 4505 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4506 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4507 | ret = FAIL; |
| 4508 | else |
| 4509 | { |
| 4510 | ret = typval_compare(tv1, tv2, type, ic); |
| 4511 | tv1->v_type = VAR_BOOL; |
| 4512 | tv1->vval.v_number = tv1->vval.v_number |
| 4513 | ? VVAL_TRUE : VVAL_FALSE; |
| 4514 | clear_tv(tv2); |
| 4515 | --ppconst->pp_used; |
| 4516 | } |
| 4517 | return ret; |
| 4518 | } |
| 4519 | |
| 4520 | generate_ppconst(cctx, ppconst); |
| 4521 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4522 | } |
| 4523 | |
| 4524 | return OK; |
| 4525 | } |
| 4526 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4527 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4528 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4529 | /* |
| 4530 | * Compile || or &&. |
| 4531 | */ |
| 4532 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4533 | compile_and_or( |
| 4534 | char_u **arg, |
| 4535 | cctx_T *cctx, |
| 4536 | char *op, |
| 4537 | ppconst_T *ppconst, |
| 4538 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4539 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4540 | char_u *next; |
| 4541 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4542 | int opchar = *op; |
| 4543 | |
| 4544 | if (p[0] == opchar && p[1] == opchar) |
| 4545 | { |
| 4546 | garray_T *instr = &cctx->ctx_instr; |
| 4547 | garray_T end_ga; |
| 4548 | |
| 4549 | /* |
| 4550 | * Repeat until there is no following "||" or "&&" |
| 4551 | */ |
| 4552 | ga_init2(&end_ga, sizeof(int), 10); |
| 4553 | while (p[0] == opchar && p[1] == opchar) |
| 4554 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4555 | if (next != NULL) |
| 4556 | { |
| 4557 | *arg = next_line_from_context(cctx, TRUE); |
| 4558 | p = skipwhite(*arg); |
| 4559 | } |
| 4560 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4561 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4562 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4563 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4564 | return FAIL; |
| 4565 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4566 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4567 | // TODO: use ppconst if the value is a constant |
| 4568 | generate_ppconst(cctx, ppconst); |
| 4569 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4570 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4571 | { |
| 4572 | ga_clear(&end_ga); |
| 4573 | return FAIL; |
| 4574 | } |
| 4575 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4576 | ++end_ga.ga_len; |
| 4577 | generate_JUMP(cctx, opchar == '|' |
| 4578 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 4579 | |
| 4580 | // eval the next expression |
| 4581 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4582 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4583 | return FAIL; |
| 4584 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4585 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4586 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4587 | { |
| 4588 | ga_clear(&end_ga); |
| 4589 | return FAIL; |
| 4590 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4591 | |
| 4592 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4593 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4594 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4595 | |
| 4596 | // Fill in the end label in all jumps. |
| 4597 | while (end_ga.ga_len > 0) |
| 4598 | { |
| 4599 | isn_T *isn; |
| 4600 | |
| 4601 | --end_ga.ga_len; |
| 4602 | isn = ((isn_T *)instr->ga_data) |
| 4603 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4604 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4605 | } |
| 4606 | ga_clear(&end_ga); |
| 4607 | } |
| 4608 | |
| 4609 | return OK; |
| 4610 | } |
| 4611 | |
| 4612 | /* |
| 4613 | * expr4a && expr4a && expr4a logical AND |
| 4614 | * |
| 4615 | * Produces instructions: |
| 4616 | * EVAL expr4a Push result of "expr4a" |
| 4617 | * JUMP_AND_KEEP_IF_FALSE end |
| 4618 | * EVAL expr4b Push result of "expr4b" |
| 4619 | * JUMP_AND_KEEP_IF_FALSE end |
| 4620 | * EVAL expr4c Push result of "expr4c" |
| 4621 | * end: |
| 4622 | */ |
| 4623 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4624 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4625 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4626 | int ppconst_used = ppconst->pp_used; |
| 4627 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4628 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4629 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4630 | return FAIL; |
| 4631 | |
| 4632 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4633 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | /* |
| 4637 | * expr3a || expr3b || expr3c logical OR |
| 4638 | * |
| 4639 | * Produces instructions: |
| 4640 | * EVAL expr3a Push result of "expr3a" |
| 4641 | * JUMP_AND_KEEP_IF_TRUE end |
| 4642 | * EVAL expr3b Push result of "expr3b" |
| 4643 | * JUMP_AND_KEEP_IF_TRUE end |
| 4644 | * EVAL expr3c Push result of "expr3c" |
| 4645 | * end: |
| 4646 | */ |
| 4647 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4648 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4649 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4650 | int ppconst_used = ppconst->pp_used; |
| 4651 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4652 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4653 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4654 | return FAIL; |
| 4655 | |
| 4656 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4657 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | /* |
| 4661 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4662 | * |
| 4663 | * Produces instructions: |
| 4664 | * EVAL expr2 Push result of "expr" |
| 4665 | * JUMP_IF_FALSE alt jump if false |
| 4666 | * EVAL expr1a |
| 4667 | * JUMP_ALWAYS end |
| 4668 | * alt: EVAL expr1b |
| 4669 | * end: |
| 4670 | */ |
| 4671 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4672 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4673 | { |
| 4674 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4675 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4676 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4677 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4678 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4679 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4680 | return FAIL; |
| 4681 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4682 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4683 | if (*p == '?') |
| 4684 | { |
| 4685 | garray_T *instr = &cctx->ctx_instr; |
| 4686 | garray_T *stack = &cctx->ctx_type_stack; |
| 4687 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4688 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4689 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4690 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4691 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4692 | int has_const_expr = FALSE; |
| 4693 | int const_value = FALSE; |
| 4694 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4695 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4696 | if (next != NULL) |
| 4697 | { |
| 4698 | *arg = next_line_from_context(cctx, TRUE); |
| 4699 | p = skipwhite(*arg); |
| 4700 | } |
| 4701 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4702 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4703 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4704 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4705 | return FAIL; |
| 4706 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4707 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4708 | if (ppconst->pp_used == ppconst_used + 1) |
| 4709 | { |
| 4710 | // the condition is a constant, we know whether the ? or the : |
| 4711 | // expression is to be evaluated. |
| 4712 | has_const_expr = TRUE; |
| 4713 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4714 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4715 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4716 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4717 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4718 | } |
| 4719 | else |
| 4720 | { |
| 4721 | generate_ppconst(cctx, ppconst); |
| 4722 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4723 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4724 | |
| 4725 | // evaluate the second expression; any type is accepted |
| 4726 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4727 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4728 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4729 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4730 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4731 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4732 | if (!has_const_expr) |
| 4733 | { |
| 4734 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4735 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4736 | // remember the type and drop it |
| 4737 | --stack->ga_len; |
| 4738 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4739 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4740 | end_idx = instr->ga_len; |
| 4741 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4742 | |
| 4743 | // jump here from JUMP_IF_FALSE |
| 4744 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4745 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4746 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4747 | |
| 4748 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4749 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4750 | if (*p != ':') |
| 4751 | { |
| 4752 | emsg(_(e_missing_colon)); |
| 4753 | return FAIL; |
| 4754 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4755 | if (next != NULL) |
| 4756 | { |
| 4757 | *arg = next_line_from_context(cctx, TRUE); |
| 4758 | p = skipwhite(*arg); |
| 4759 | } |
| 4760 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4761 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4762 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4763 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4764 | return FAIL; |
| 4765 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4766 | |
| 4767 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4768 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4769 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4770 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4771 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4772 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4773 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4774 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4775 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4776 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4777 | if (!has_const_expr) |
| 4778 | { |
| 4779 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4780 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4781 | // If the types differ, the result has a more generic type. |
| 4782 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4783 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4784 | |
| 4785 | // jump here from JUMP_ALWAYS |
| 4786 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4787 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4788 | } |
| 4789 | |
| 4790 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4791 | } |
| 4792 | return OK; |
| 4793 | } |
| 4794 | |
| 4795 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4796 | * Toplevel expression. |
| 4797 | */ |
| 4798 | static int |
| 4799 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4800 | { |
| 4801 | ppconst_T ppconst; |
| 4802 | |
| 4803 | CLEAR_FIELD(ppconst); |
| 4804 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4805 | { |
| 4806 | clear_ppconst(&ppconst); |
| 4807 | return FAIL; |
| 4808 | } |
| 4809 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4810 | return FAIL; |
| 4811 | return OK; |
| 4812 | } |
| 4813 | |
| 4814 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4815 | * compile "return [expr]" |
| 4816 | */ |
| 4817 | static char_u * |
| 4818 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4819 | { |
| 4820 | char_u *p = arg; |
| 4821 | garray_T *stack = &cctx->ctx_type_stack; |
| 4822 | type_T *stack_type; |
| 4823 | |
| 4824 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4825 | { |
| 4826 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4827 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4828 | return NULL; |
| 4829 | |
| 4830 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4831 | if (set_return_type) |
| 4832 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4833 | else |
| 4834 | { |
| 4835 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4836 | && stack_type->tt_type != VAR_VOID |
| 4837 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4838 | { |
| 4839 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4840 | return NULL; |
| 4841 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4842 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4843 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4844 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4845 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4846 | } |
| 4847 | else |
| 4848 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4849 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4850 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4851 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4852 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4853 | { |
| 4854 | emsg(_("E1003: Missing return value")); |
| 4855 | return NULL; |
| 4856 | } |
| 4857 | |
| 4858 | // No argument, return zero. |
| 4859 | generate_PUSHNR(cctx, 0); |
| 4860 | } |
| 4861 | |
| 4862 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4863 | return NULL; |
| 4864 | |
| 4865 | // "return val | endif" is possible |
| 4866 | return skipwhite(p); |
| 4867 | } |
| 4868 | |
| 4869 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4870 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4871 | * Return a pointer to the line in allocated memory. |
| 4872 | * Return NULL for end-of-file or some error. |
| 4873 | */ |
| 4874 | static char_u * |
| 4875 | exarg_getline( |
| 4876 | int c UNUSED, |
| 4877 | void *cookie, |
| 4878 | int indent UNUSED, |
| 4879 | int do_concat UNUSED) |
| 4880 | { |
| 4881 | cctx_T *cctx = (cctx_T *)cookie; |
| 4882 | |
| 4883 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4884 | { |
| 4885 | iemsg("Heredoc got to end"); |
| 4886 | return NULL; |
| 4887 | } |
| 4888 | ++cctx->ctx_lnum; |
| 4889 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4890 | [cctx->ctx_lnum]); |
| 4891 | } |
| 4892 | |
| 4893 | /* |
| 4894 | * Compile a nested :def command. |
| 4895 | */ |
| 4896 | static char_u * |
| 4897 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4898 | { |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4899 | int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4900 | char_u *name_start = eap->arg; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4901 | char_u *name_end = to_name_end(eap->arg, is_global); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4902 | char_u *name = get_lambda_name(); |
| 4903 | lvar_T *lvar; |
| 4904 | ufunc_T *ufunc; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4905 | int r; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4906 | |
| 4907 | eap->arg = name_end; |
| 4908 | eap->getline = exarg_getline; |
| 4909 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4910 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4911 | eap->forceit = FALSE; |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4912 | ufunc = def_function(eap, name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4913 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4914 | if (ufunc == NULL) |
| 4915 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4916 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4917 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4918 | return NULL; |
| 4919 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4920 | if (is_global) |
| 4921 | { |
| 4922 | char_u *func_name = vim_strnsave(name_start + 2, |
| 4923 | name_end - name_start - 2); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4924 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4925 | if (func_name == NULL) |
| 4926 | r = FAIL; |
| 4927 | else |
| 4928 | r = generate_NEWFUNC(cctx, name, func_name); |
| 4929 | } |
| 4930 | else |
| 4931 | { |
| 4932 | // Define a local variable for the function reference. |
| 4933 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
| 4934 | TRUE, ufunc->uf_func_type); |
| 4935 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL) |
| 4936 | return NULL; |
| 4937 | r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 4938 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4939 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4940 | // TODO: warning for trailing text? |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4941 | return r == FAIL ? NULL : (char_u *)""; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4942 | } |
| 4943 | |
| 4944 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4945 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4946 | */ |
| 4947 | int |
| 4948 | assignment_len(char_u *p, int *heredoc) |
| 4949 | { |
| 4950 | if (*p == '=') |
| 4951 | { |
| 4952 | if (p[1] == '<' && p[2] == '<') |
| 4953 | { |
| 4954 | *heredoc = TRUE; |
| 4955 | return 3; |
| 4956 | } |
| 4957 | return 1; |
| 4958 | } |
| 4959 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4960 | return 2; |
| 4961 | if (STRNCMP(p, "..=", 3) == 0) |
| 4962 | return 3; |
| 4963 | return 0; |
| 4964 | } |
| 4965 | |
| 4966 | // words that cannot be used as a variable |
| 4967 | static char *reserved[] = { |
| 4968 | "true", |
| 4969 | "false", |
| 4970 | NULL |
| 4971 | }; |
| 4972 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4973 | typedef enum { |
| 4974 | dest_local, |
| 4975 | dest_option, |
| 4976 | dest_env, |
| 4977 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4978 | dest_buffer, |
| 4979 | dest_window, |
| 4980 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4981 | dest_vimvar, |
| 4982 | dest_script, |
| 4983 | dest_reg, |
| 4984 | } assign_dest_T; |
| 4985 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4986 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4987 | * Generate the load instruction for "name". |
| 4988 | */ |
| 4989 | static void |
| 4990 | generate_loadvar( |
| 4991 | cctx_T *cctx, |
| 4992 | assign_dest_T dest, |
| 4993 | char_u *name, |
| 4994 | lvar_T *lvar, |
| 4995 | type_T *type) |
| 4996 | { |
| 4997 | switch (dest) |
| 4998 | { |
| 4999 | case dest_option: |
| 5000 | // TODO: check the option exists |
| 5001 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 5002 | break; |
| 5003 | case dest_global: |
| 5004 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 5005 | break; |
| 5006 | case dest_buffer: |
| 5007 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 5008 | break; |
| 5009 | case dest_window: |
| 5010 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 5011 | break; |
| 5012 | case dest_tab: |
| 5013 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 5014 | break; |
| 5015 | case dest_script: |
| 5016 | compile_load_scriptvar(cctx, |
| 5017 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 5018 | break; |
| 5019 | case dest_env: |
| 5020 | // Include $ in the name here |
| 5021 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 5022 | break; |
| 5023 | case dest_reg: |
| 5024 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 5025 | break; |
| 5026 | case dest_vimvar: |
| 5027 | generate_LOADV(cctx, name + 2, TRUE); |
| 5028 | break; |
| 5029 | case dest_local: |
| 5030 | if (lvar->lv_from_outer) |
| 5031 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 5032 | NULL, type); |
| 5033 | else |
| 5034 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 5035 | break; |
| 5036 | } |
| 5037 | } |
| 5038 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5039 | void |
| 5040 | vim9_declare_error(char_u *name) |
| 5041 | { |
| 5042 | char *scope = ""; |
| 5043 | |
| 5044 | switch (*name) |
| 5045 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 5046 | case 'g': scope = _("global"); break; |
| 5047 | case 'b': scope = _("buffer"); break; |
| 5048 | case 'w': scope = _("window"); break; |
| 5049 | case 't': scope = _("tab"); break; |
| 5050 | case 'v': scope = "v:"; break; |
| 5051 | case '$': semsg(_(e_declare_env_var), name); return; |
| 5052 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5053 | } |
| 5054 | semsg(_(e_declare_var), scope, name); |
| 5055 | } |
| 5056 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5057 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5058 | * Compile declaration and assignment: |
| 5059 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5060 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5061 | * Return NULL for an error. |
| 5062 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5063 | */ |
| 5064 | static char_u * |
| 5065 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 5066 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5067 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5068 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5069 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5070 | char_u *ret = NULL; |
| 5071 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5072 | int var_idx; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5073 | int scriptvar_sid = 0; |
| 5074 | int scriptvar_idx = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5075 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5076 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5077 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5078 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5079 | int oplen = 0; |
| 5080 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 5081 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5082 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5083 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5084 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5085 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5086 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5087 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 5088 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 5089 | if (p == NULL) |
| 5090 | return *arg == '[' ? arg : NULL; |
| 5091 | |
| 5092 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5093 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 5094 | // TODO: should we allow this, and figure out type inference from list |
| 5095 | // members? |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5096 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5097 | return NULL; |
| 5098 | } |
| 5099 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5100 | sp = p; |
| 5101 | p = skipwhite(p); |
| 5102 | op = p; |
| 5103 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5104 | |
| 5105 | if (var_count > 0 && oplen == 0) |
| 5106 | // can be something like "[1, 2]->func()" |
| 5107 | return arg; |
| 5108 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5109 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 5110 | { |
| 5111 | char_u buf[4]; |
| 5112 | |
| 5113 | vim_strncpy(buf, op, oplen); |
| 5114 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5115 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 5116 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5117 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5118 | if (heredoc) |
| 5119 | { |
| 5120 | list_T *l; |
| 5121 | listitem_T *li; |
| 5122 | |
| 5123 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5124 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5125 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 5126 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5127 | |
| 5128 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 5129 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5130 | { |
| 5131 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 5132 | li->li_tv.vval.v_string = NULL; |
| 5133 | } |
| 5134 | generate_NEWLIST(cctx, l->lv_len); |
| 5135 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5136 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5137 | list_free(l); |
| 5138 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5139 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5140 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5141 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5142 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5143 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 5144 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5145 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5146 | p = skipwhite(op + oplen); |
| 5147 | if (compile_expr0(&p, cctx) == FAIL) |
| 5148 | return NULL; |
| 5149 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5150 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5151 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5152 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5153 | type_T *stacktype; |
| 5154 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5155 | stacktype = stack->ga_len == 0 ? &t_void |
| 5156 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5157 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5158 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5159 | emsg(_(e_cannot_use_void)); |
| 5160 | goto theend; |
| 5161 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5162 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5163 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5164 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5165 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 5166 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5167 | } |
| 5168 | } |
| 5169 | |
| 5170 | /* |
| 5171 | * Loop over variables in "[var, var] = expr". |
| 5172 | * For "var = expr" and "let var: type" this is done only once. |
| 5173 | */ |
| 5174 | if (var_count > 0) |
| 5175 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 5176 | else |
| 5177 | var_start = arg; |
| 5178 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 5179 | { |
| 5180 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 5181 | size_t varlen; |
| 5182 | int new_local = FALSE; |
| 5183 | int opt_type; |
| 5184 | int opt_flags = 0; |
| 5185 | assign_dest_T dest = dest_local; |
| 5186 | int vimvaridx = -1; |
| 5187 | lvar_T *lvar = NULL; |
| 5188 | lvar_T arg_lvar; |
| 5189 | int has_type = FALSE; |
| 5190 | int has_index = FALSE; |
| 5191 | int instr_count = -1; |
| 5192 | |
| 5193 | p = (*var_start == '&' || *var_start == '$' |
| 5194 | || *var_start == '@') ? var_start + 1 : var_start; |
| 5195 | p = to_name_end(p, TRUE); |
| 5196 | |
| 5197 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 5198 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 5199 | --var_end; |
| 5200 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 5201 | --p; |
| 5202 | |
| 5203 | varlen = p - var_start; |
| 5204 | vim_free(name); |
| 5205 | name = vim_strnsave(var_start, varlen); |
| 5206 | if (name == NULL) |
| 5207 | return NULL; |
| 5208 | if (!heredoc) |
| 5209 | type = &t_any; |
| 5210 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5211 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5212 | { |
| 5213 | if (*var_start == '&') |
| 5214 | { |
| 5215 | int cc; |
| 5216 | long numval; |
| 5217 | |
| 5218 | dest = dest_option; |
| 5219 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5220 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5221 | emsg(_(e_const_option)); |
| 5222 | goto theend; |
| 5223 | } |
| 5224 | if (is_decl) |
| 5225 | { |
| 5226 | semsg(_("E1052: Cannot declare an option: %s"), var_start); |
| 5227 | goto theend; |
| 5228 | } |
| 5229 | p = var_start; |
| 5230 | p = find_option_end(&p, &opt_flags); |
| 5231 | if (p == NULL) |
| 5232 | { |
| 5233 | // cannot happen? |
| 5234 | emsg(_(e_letunexp)); |
| 5235 | goto theend; |
| 5236 | } |
| 5237 | cc = *p; |
| 5238 | *p = NUL; |
| 5239 | opt_type = get_option_value(var_start + 1, &numval, |
| 5240 | NULL, opt_flags); |
| 5241 | *p = cc; |
| 5242 | if (opt_type == -3) |
| 5243 | { |
| 5244 | semsg(_(e_unknown_option), var_start); |
| 5245 | goto theend; |
| 5246 | } |
| 5247 | if (opt_type == -2 || opt_type == 0) |
| 5248 | type = &t_string; |
| 5249 | else |
| 5250 | type = &t_number; // both number and boolean option |
| 5251 | } |
| 5252 | else if (*var_start == '$') |
| 5253 | { |
| 5254 | dest = dest_env; |
| 5255 | type = &t_string; |
| 5256 | if (is_decl) |
| 5257 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5258 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5259 | goto theend; |
| 5260 | } |
| 5261 | } |
| 5262 | else if (*var_start == '@') |
| 5263 | { |
| 5264 | if (!valid_yank_reg(var_start[1], TRUE)) |
| 5265 | { |
| 5266 | emsg_invreg(var_start[1]); |
| 5267 | goto theend; |
| 5268 | } |
| 5269 | dest = dest_reg; |
| 5270 | type = &t_string; |
| 5271 | if (is_decl) |
| 5272 | { |
| 5273 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 5274 | goto theend; |
| 5275 | } |
| 5276 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 5277 | else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5278 | { |
| 5279 | dest = dest_global; |
| 5280 | if (is_decl) |
| 5281 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5282 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5283 | goto theend; |
| 5284 | } |
| 5285 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 5286 | else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5287 | { |
| 5288 | dest = dest_buffer; |
| 5289 | if (is_decl) |
| 5290 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5291 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5292 | goto theend; |
| 5293 | } |
| 5294 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 5295 | else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5296 | { |
| 5297 | dest = dest_window; |
| 5298 | if (is_decl) |
| 5299 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5300 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5301 | goto theend; |
| 5302 | } |
| 5303 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 5304 | else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5305 | { |
| 5306 | dest = dest_tab; |
| 5307 | if (is_decl) |
| 5308 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5309 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5310 | goto theend; |
| 5311 | } |
| 5312 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 5313 | else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5314 | { |
| 5315 | typval_T *vtv; |
| 5316 | int di_flags; |
| 5317 | |
| 5318 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5319 | if (vimvaridx < 0) |
| 5320 | { |
| 5321 | semsg(_(e_var_notfound), var_start); |
| 5322 | goto theend; |
| 5323 | } |
| 5324 | // We use the current value of "sandbox" here, is that OK? |
| 5325 | if (var_check_ro(di_flags, name, FALSE)) |
| 5326 | goto theend; |
| 5327 | dest = dest_vimvar; |
| 5328 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 5329 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5330 | if (is_decl) |
| 5331 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5332 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5333 | goto theend; |
| 5334 | } |
| 5335 | } |
| 5336 | else |
| 5337 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5338 | int idx; |
| 5339 | imported_T *import = NULL; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5340 | |
| 5341 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5342 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5343 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5344 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5345 | goto theend; |
| 5346 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5347 | |
| 5348 | lvar = lookup_local(var_start, varlen, cctx); |
| 5349 | if (lvar == NULL) |
| 5350 | { |
| 5351 | CLEAR_FIELD(arg_lvar); |
| 5352 | if (lookup_arg(var_start, varlen, |
| 5353 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 5354 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5355 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5356 | if (is_decl) |
| 5357 | { |
| 5358 | semsg(_(e_used_as_arg), name); |
| 5359 | goto theend; |
| 5360 | } |
| 5361 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5362 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5363 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5364 | if (lvar != NULL) |
| 5365 | { |
| 5366 | if (is_decl) |
| 5367 | { |
| 5368 | semsg(_("E1017: Variable already declared: %s"), name); |
| 5369 | goto theend; |
| 5370 | } |
| 5371 | else if (lvar->lv_const) |
| 5372 | { |
| 5373 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 5374 | name); |
| 5375 | goto theend; |
| 5376 | } |
| 5377 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 5378 | else if ((varlen > 1 && STRNCMP(var_start, "s:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5379 | || lookup_script(var_start, varlen) == OK |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5380 | || (import = find_imported(var_start, varlen, cctx)) |
| 5381 | != NULL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5382 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5383 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5384 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5385 | if (is_decl) |
| 5386 | { |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 5387 | if ((varlen > 1 && STRNCMP(var_start, "s:", 2) == 0)) |
| 5388 | semsg(_("E1101: Cannot declare a script variable in a function: %s"), |
| 5389 | name); |
| 5390 | else |
| 5391 | semsg(_("E1054: Variable already declared in the script: %s"), |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5392 | name); |
| 5393 | goto theend; |
| 5394 | } |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5395 | dest = dest_script; |
| 5396 | |
| 5397 | // existing script-local variables should have a type |
| 5398 | scriptvar_sid = current_sctx.sc_sid; |
| 5399 | if (import != NULL) |
| 5400 | scriptvar_sid = import->imp_sid; |
| 5401 | scriptvar_idx = get_script_item_idx(scriptvar_sid, |
| 5402 | rawname, TRUE); |
| 5403 | if (scriptvar_idx >= 0) |
| 5404 | { |
| 5405 | scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid); |
| 5406 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) |
| 5407 | + scriptvar_idx; |
| 5408 | type = sv->sv_type; |
| 5409 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5410 | } |
| 5411 | else if (name[1] == ':' && name[2] != NUL) |
| 5412 | { |
| 5413 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
| 5414 | name); |
| 5415 | goto theend; |
| 5416 | } |
| 5417 | else if (!is_decl) |
| 5418 | { |
| 5419 | semsg(_("E1089: unknown variable: %s"), name); |
| 5420 | goto theend; |
| 5421 | } |
| 5422 | } |
| 5423 | } |
| 5424 | |
| 5425 | // handle "a:name" as a name, not index "name" on "a" |
| 5426 | if (varlen > 1 || var_start[varlen] != ':') |
| 5427 | p = var_end; |
| 5428 | |
| 5429 | if (dest != dest_option) |
| 5430 | { |
| 5431 | if (is_decl && *p == ':') |
| 5432 | { |
| 5433 | // parse optional type: "let var: type = expr" |
| 5434 | if (!VIM_ISWHITE(p[1])) |
| 5435 | { |
| 5436 | semsg(_(e_white_after), ":"); |
| 5437 | goto theend; |
| 5438 | } |
| 5439 | p = skipwhite(p + 1); |
| 5440 | type = parse_type(&p, cctx->ctx_type_list); |
| 5441 | has_type = TRUE; |
| 5442 | } |
| 5443 | else if (lvar != NULL) |
| 5444 | type = lvar->lv_type; |
| 5445 | } |
| 5446 | |
| 5447 | if (oplen == 3 && !heredoc && dest != dest_global |
| 5448 | && type->tt_type != VAR_STRING |
| 5449 | && type->tt_type != VAR_ANY) |
| 5450 | { |
| 5451 | emsg(_("E1019: Can only concatenate to string")); |
| 5452 | goto theend; |
| 5453 | } |
| 5454 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5455 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5456 | { |
| 5457 | if (oplen > 1 && !heredoc) |
| 5458 | { |
| 5459 | // +=, /=, etc. require an existing variable |
| 5460 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 5461 | name); |
| 5462 | goto theend; |
| 5463 | } |
| 5464 | |
| 5465 | // new local variable |
| 5466 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
| 5467 | goto theend; |
| 5468 | lvar = reserve_local(cctx, var_start, varlen, |
| 5469 | cmdidx == CMD_const, type); |
| 5470 | if (lvar == NULL) |
| 5471 | goto theend; |
| 5472 | new_local = TRUE; |
| 5473 | } |
| 5474 | |
| 5475 | member_type = type; |
| 5476 | if (var_end > var_start + varlen) |
| 5477 | { |
| 5478 | // Something follows after the variable: "var[idx]". |
| 5479 | if (is_decl) |
| 5480 | { |
| 5481 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 5482 | goto theend; |
| 5483 | } |
| 5484 | |
| 5485 | if (var_start[varlen] == '[') |
| 5486 | { |
| 5487 | has_index = TRUE; |
| 5488 | if (type->tt_member == NULL) |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 5489 | member_type = &t_any; |
| 5490 | else |
| 5491 | member_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5492 | } |
| 5493 | else |
| 5494 | { |
| 5495 | semsg("Not supported yet: %s", var_start); |
| 5496 | goto theend; |
| 5497 | } |
| 5498 | } |
| 5499 | else if (lvar == &arg_lvar) |
| 5500 | { |
| 5501 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 5502 | goto theend; |
| 5503 | } |
| 5504 | |
| 5505 | if (!heredoc) |
| 5506 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5507 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5508 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5509 | if (oplen > 0 && var_count == 0) |
| 5510 | { |
| 5511 | // skip over the "=" and the expression |
| 5512 | p = skipwhite(op + oplen); |
| 5513 | compile_expr0(&p, cctx); |
| 5514 | } |
| 5515 | } |
| 5516 | else if (oplen > 0) |
| 5517 | { |
| 5518 | type_T *stacktype; |
| 5519 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5520 | // For "var = expr" evaluate the expression. |
| 5521 | if (var_count == 0) |
| 5522 | { |
| 5523 | int r; |
| 5524 | |
| 5525 | // for "+=", "*=", "..=" etc. first load the current value |
| 5526 | if (*op != '=') |
| 5527 | { |
| 5528 | generate_loadvar(cctx, dest, name, lvar, type); |
| 5529 | |
| 5530 | if (has_index) |
| 5531 | { |
| 5532 | // TODO: get member from list or dict |
| 5533 | emsg("Index with operation not supported yet"); |
| 5534 | goto theend; |
| 5535 | } |
| 5536 | } |
| 5537 | |
| 5538 | // Compile the expression. Temporarily hide the new local |
| 5539 | // variable here, it is not available to this expression. |
| 5540 | if (new_local) |
| 5541 | --cctx->ctx_locals.ga_len; |
| 5542 | instr_count = instr->ga_len; |
| 5543 | p = skipwhite(op + oplen); |
| 5544 | r = compile_expr0(&p, cctx); |
| 5545 | if (new_local) |
| 5546 | ++cctx->ctx_locals.ga_len; |
| 5547 | if (r == FAIL) |
| 5548 | goto theend; |
| 5549 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5550 | else if (semicolon && var_idx == var_count - 1) |
| 5551 | { |
| 5552 | // For "[var; var] = expr" get the rest of the list |
| 5553 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5554 | goto theend; |
| 5555 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5556 | else |
| 5557 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5558 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5559 | // list. |
| 5560 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 5561 | return FAIL; |
| 5562 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5563 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5564 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5565 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5566 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5567 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5568 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5569 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5570 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5571 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5572 | emsg(_(e_cannot_use_void)); |
| 5573 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5574 | } |
| 5575 | else |
| 5576 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5577 | // An empty list or dict has a &t_void member, |
| 5578 | // for a variable that implies &t_any. |
| 5579 | if (stacktype == &t_list_empty) |
| 5580 | lvar->lv_type = &t_list_any; |
| 5581 | else if (stacktype == &t_dict_empty) |
| 5582 | lvar->lv_type = &t_dict_any; |
| 5583 | else |
| 5584 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5585 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5586 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5587 | else |
| 5588 | { |
| 5589 | type_T *use_type = lvar->lv_type; |
| 5590 | |
| 5591 | if (has_index) |
| 5592 | { |
| 5593 | use_type = use_type->tt_member; |
| 5594 | if (use_type == NULL) |
| 5595 | use_type = &t_void; |
| 5596 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5597 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5598 | == FAIL) |
| 5599 | goto theend; |
| 5600 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5601 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5602 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5603 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5604 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5605 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5606 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5607 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5608 | emsg(_(e_const_req_value)); |
| 5609 | goto theend; |
| 5610 | } |
| 5611 | else if (!has_type || dest == dest_option) |
| 5612 | { |
| 5613 | emsg(_(e_type_req)); |
| 5614 | goto theend; |
| 5615 | } |
| 5616 | else |
| 5617 | { |
| 5618 | // variables are always initialized |
| 5619 | if (ga_grow(instr, 1) == FAIL) |
| 5620 | goto theend; |
| 5621 | switch (member_type->tt_type) |
| 5622 | { |
| 5623 | case VAR_BOOL: |
| 5624 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5625 | break; |
| 5626 | case VAR_FLOAT: |
| 5627 | #ifdef FEAT_FLOAT |
| 5628 | generate_PUSHF(cctx, 0.0); |
| 5629 | #endif |
| 5630 | break; |
| 5631 | case VAR_STRING: |
| 5632 | generate_PUSHS(cctx, NULL); |
| 5633 | break; |
| 5634 | case VAR_BLOB: |
| 5635 | generate_PUSHBLOB(cctx, NULL); |
| 5636 | break; |
| 5637 | case VAR_FUNC: |
| 5638 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5639 | break; |
| 5640 | case VAR_LIST: |
| 5641 | generate_NEWLIST(cctx, 0); |
| 5642 | break; |
| 5643 | case VAR_DICT: |
| 5644 | generate_NEWDICT(cctx, 0); |
| 5645 | break; |
| 5646 | case VAR_JOB: |
| 5647 | generate_PUSHJOB(cctx, NULL); |
| 5648 | break; |
| 5649 | case VAR_CHANNEL: |
| 5650 | generate_PUSHCHANNEL(cctx, NULL); |
| 5651 | break; |
| 5652 | case VAR_NUMBER: |
| 5653 | case VAR_UNKNOWN: |
| 5654 | case VAR_ANY: |
| 5655 | case VAR_PARTIAL: |
| 5656 | case VAR_VOID: |
| 5657 | case VAR_SPECIAL: // cannot happen |
| 5658 | generate_PUSHNR(cctx, 0); |
| 5659 | break; |
| 5660 | } |
| 5661 | } |
| 5662 | if (var_count == 0) |
| 5663 | end = p; |
| 5664 | } |
| 5665 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5666 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5667 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5668 | break; |
| 5669 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5670 | if (oplen > 0 && *op != '=') |
| 5671 | { |
| 5672 | type_T *expected = &t_number; |
| 5673 | type_T *stacktype; |
| 5674 | |
| 5675 | // TODO: if type is known use float or any operation |
| 5676 | |
| 5677 | if (*op == '.') |
| 5678 | expected = &t_string; |
| 5679 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5680 | if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5681 | goto theend; |
| 5682 | |
| 5683 | if (*op == '.') |
| 5684 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 5685 | else |
| 5686 | { |
| 5687 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 5688 | |
| 5689 | if (isn == NULL) |
| 5690 | goto theend; |
| 5691 | switch (*op) |
| 5692 | { |
| 5693 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 5694 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 5695 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 5696 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 5697 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 5698 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5699 | } |
| 5700 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5701 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5702 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5703 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5704 | int r; |
| 5705 | |
| 5706 | // Compile the "idx" in "var[idx]". |
| 5707 | if (new_local) |
| 5708 | --cctx->ctx_locals.ga_len; |
| 5709 | p = skipwhite(var_start + varlen + 1); |
| 5710 | r = compile_expr0(&p, cctx); |
| 5711 | if (new_local) |
| 5712 | ++cctx->ctx_locals.ga_len; |
| 5713 | if (r == FAIL) |
| 5714 | goto theend; |
| 5715 | if (*skipwhite(p) != ']') |
| 5716 | { |
| 5717 | emsg(_(e_missbrac)); |
| 5718 | goto theend; |
| 5719 | } |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 5720 | if (type == &t_any) |
| 5721 | { |
| 5722 | type_T *idx_type = ((type_T **)stack->ga_data)[ |
| 5723 | stack->ga_len - 1]; |
| 5724 | // Index on variable of unknown type: guess the type from the |
| 5725 | // index type: number is dict, otherwise dict. |
| 5726 | // TODO: should do the assignment at runtime |
| 5727 | if (idx_type->tt_type == VAR_NUMBER) |
| 5728 | type = &t_list_any; |
| 5729 | else |
| 5730 | type = &t_dict_any; |
| 5731 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5732 | if (type->tt_type == VAR_DICT |
| 5733 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5734 | goto theend; |
| 5735 | if (type->tt_type == VAR_LIST |
| 5736 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5737 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5738 | { |
| 5739 | emsg(_(e_number_exp)); |
| 5740 | goto theend; |
| 5741 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5742 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5743 | // Load the dict or list. On the stack we then have: |
| 5744 | // - value |
| 5745 | // - index |
| 5746 | // - variable |
| 5747 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5748 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5749 | if (type->tt_type == VAR_LIST) |
| 5750 | { |
| 5751 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5752 | return FAIL; |
| 5753 | } |
| 5754 | else if (type->tt_type == VAR_DICT) |
| 5755 | { |
| 5756 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5757 | return FAIL; |
| 5758 | } |
| 5759 | else |
| 5760 | { |
| 5761 | emsg(_(e_listreq)); |
| 5762 | goto theend; |
| 5763 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5764 | } |
| 5765 | else |
| 5766 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5767 | switch (dest) |
| 5768 | { |
| 5769 | case dest_option: |
| 5770 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5771 | break; |
| 5772 | case dest_global: |
| 5773 | // include g: with the name, easier to execute that way |
| 5774 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5775 | break; |
| 5776 | case dest_buffer: |
| 5777 | // include b: with the name, easier to execute that way |
| 5778 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5779 | break; |
| 5780 | case dest_window: |
| 5781 | // include w: with the name, easier to execute that way |
| 5782 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5783 | break; |
| 5784 | case dest_tab: |
| 5785 | // include t: with the name, easier to execute that way |
| 5786 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5787 | break; |
| 5788 | case dest_env: |
| 5789 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5790 | break; |
| 5791 | case dest_reg: |
| 5792 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5793 | break; |
| 5794 | case dest_vimvar: |
| 5795 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5796 | break; |
| 5797 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5798 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5799 | if (scriptvar_idx < 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5800 | { |
| 5801 | char_u *name_s = name; |
| 5802 | |
| 5803 | // Include s: in the name for store_var() |
| 5804 | if (name[1] != ':') |
| 5805 | { |
| 5806 | int len = (int)STRLEN(name) + 3; |
| 5807 | |
| 5808 | name_s = alloc(len); |
| 5809 | if (name_s == NULL) |
| 5810 | name_s = name; |
| 5811 | else |
| 5812 | vim_snprintf((char *)name_s, len, |
| 5813 | "s:%s", name); |
| 5814 | } |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5815 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, |
| 5816 | scriptvar_sid, type); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5817 | if (name_s != name) |
| 5818 | vim_free(name_s); |
| 5819 | } |
| 5820 | else |
| 5821 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5822 | scriptvar_sid, scriptvar_idx, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5823 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5824 | break; |
| 5825 | case dest_local: |
| 5826 | if (lvar != NULL) |
| 5827 | { |
| 5828 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5829 | + instr->ga_len - 1; |
| 5830 | |
| 5831 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5832 | // ISN_STORE into ISN_STORENR |
| 5833 | if (!lvar->lv_from_outer |
| 5834 | && instr->ga_len == instr_count + 1 |
| 5835 | && isn->isn_type == ISN_PUSHNR) |
| 5836 | { |
| 5837 | varnumber_T val = isn->isn_arg.number; |
| 5838 | |
| 5839 | isn->isn_type = ISN_STORENR; |
| 5840 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5841 | isn->isn_arg.storenr.stnr_val = val; |
| 5842 | if (stack->ga_len > 0) |
| 5843 | --stack->ga_len; |
| 5844 | } |
| 5845 | else if (lvar->lv_from_outer) |
| 5846 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5847 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5848 | else |
| 5849 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5850 | } |
| 5851 | break; |
| 5852 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5853 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5854 | |
| 5855 | if (var_idx + 1 < var_count) |
| 5856 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5857 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5858 | |
| 5859 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5860 | if (var_count > 0 && !semicolon) |
| 5861 | { |
| 5862 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5863 | goto theend; |
| 5864 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5865 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5866 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5867 | |
| 5868 | theend: |
| 5869 | vim_free(name); |
| 5870 | return ret; |
| 5871 | } |
| 5872 | |
| 5873 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5874 | * Check if "name" can be "unlet". |
| 5875 | */ |
| 5876 | int |
| 5877 | check_vim9_unlet(char_u *name) |
| 5878 | { |
| 5879 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5880 | { |
| 5881 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5882 | return FAIL; |
| 5883 | } |
| 5884 | return OK; |
| 5885 | } |
| 5886 | |
| 5887 | /* |
| 5888 | * Callback passed to ex_unletlock(). |
| 5889 | */ |
| 5890 | static int |
| 5891 | compile_unlet( |
| 5892 | lval_T *lvp, |
| 5893 | char_u *name_end, |
| 5894 | exarg_T *eap, |
| 5895 | int deep UNUSED, |
| 5896 | void *coookie) |
| 5897 | { |
| 5898 | cctx_T *cctx = coookie; |
| 5899 | |
| 5900 | if (lvp->ll_tv == NULL) |
| 5901 | { |
| 5902 | char_u *p = lvp->ll_name; |
| 5903 | int cc = *name_end; |
| 5904 | int ret = OK; |
| 5905 | |
| 5906 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5907 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5908 | if (*p == '$') |
| 5909 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5910 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5911 | ret = FAIL; |
| 5912 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5913 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5914 | |
| 5915 | *name_end = cc; |
| 5916 | return ret; |
| 5917 | } |
| 5918 | |
| 5919 | // TODO: unlet {list}[idx] |
| 5920 | // TODO: unlet {dict}[key] |
| 5921 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5922 | return FAIL; |
| 5923 | } |
| 5924 | |
| 5925 | /* |
| 5926 | * compile "unlet var", "lock var" and "unlock var" |
| 5927 | * "arg" points to "var". |
| 5928 | */ |
| 5929 | static char_u * |
| 5930 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5931 | { |
| 5932 | char_u *p = arg; |
| 5933 | |
| 5934 | if (eap->cmdidx != CMD_unlet) |
| 5935 | { |
| 5936 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5937 | return NULL; |
| 5938 | } |
| 5939 | |
| 5940 | if (*p == '!') |
| 5941 | { |
| 5942 | p = skipwhite(p + 1); |
| 5943 | eap->forceit = TRUE; |
| 5944 | } |
| 5945 | |
| 5946 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5947 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5948 | } |
| 5949 | |
| 5950 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5951 | * Compile an :import command. |
| 5952 | */ |
| 5953 | static char_u * |
| 5954 | compile_import(char_u *arg, cctx_T *cctx) |
| 5955 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5956 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5957 | } |
| 5958 | |
| 5959 | /* |
| 5960 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5961 | */ |
| 5962 | static int |
| 5963 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5964 | { |
| 5965 | garray_T *instr = &cctx->ctx_instr; |
| 5966 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5967 | |
| 5968 | if (endlabel == NULL) |
| 5969 | return FAIL; |
| 5970 | endlabel->el_next = *el; |
| 5971 | *el = endlabel; |
| 5972 | endlabel->el_end_label = instr->ga_len; |
| 5973 | |
| 5974 | generate_JUMP(cctx, when, 0); |
| 5975 | return OK; |
| 5976 | } |
| 5977 | |
| 5978 | static void |
| 5979 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5980 | { |
| 5981 | garray_T *instr = &cctx->ctx_instr; |
| 5982 | |
| 5983 | while (*el != NULL) |
| 5984 | { |
| 5985 | endlabel_T *cur = (*el); |
| 5986 | isn_T *isn; |
| 5987 | |
| 5988 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5989 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5990 | *el = cur->el_next; |
| 5991 | vim_free(cur); |
| 5992 | } |
| 5993 | } |
| 5994 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5995 | static void |
| 5996 | compile_free_jump_to_end(endlabel_T **el) |
| 5997 | { |
| 5998 | while (*el != NULL) |
| 5999 | { |
| 6000 | endlabel_T *cur = (*el); |
| 6001 | |
| 6002 | *el = cur->el_next; |
| 6003 | vim_free(cur); |
| 6004 | } |
| 6005 | } |
| 6006 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6007 | /* |
| 6008 | * Create a new scope and set up the generic items. |
| 6009 | */ |
| 6010 | static scope_T * |
| 6011 | new_scope(cctx_T *cctx, scopetype_T type) |
| 6012 | { |
| 6013 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 6014 | |
| 6015 | if (scope == NULL) |
| 6016 | return NULL; |
| 6017 | scope->se_outer = cctx->ctx_scope; |
| 6018 | cctx->ctx_scope = scope; |
| 6019 | scope->se_type = type; |
| 6020 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 6021 | return scope; |
| 6022 | } |
| 6023 | |
| 6024 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6025 | * Free the current scope and go back to the outer scope. |
| 6026 | */ |
| 6027 | static void |
| 6028 | drop_scope(cctx_T *cctx) |
| 6029 | { |
| 6030 | scope_T *scope = cctx->ctx_scope; |
| 6031 | |
| 6032 | if (scope == NULL) |
| 6033 | { |
| 6034 | iemsg("calling drop_scope() without a scope"); |
| 6035 | return; |
| 6036 | } |
| 6037 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6038 | switch (scope->se_type) |
| 6039 | { |
| 6040 | case IF_SCOPE: |
| 6041 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 6042 | case FOR_SCOPE: |
| 6043 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 6044 | case WHILE_SCOPE: |
| 6045 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 6046 | case TRY_SCOPE: |
| 6047 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 6048 | case NO_SCOPE: |
| 6049 | case BLOCK_SCOPE: |
| 6050 | break; |
| 6051 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6052 | vim_free(scope); |
| 6053 | } |
| 6054 | |
| 6055 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6056 | * compile "if expr" |
| 6057 | * |
| 6058 | * "if expr" Produces instructions: |
| 6059 | * EVAL expr Push result of "expr" |
| 6060 | * JUMP_IF_FALSE end |
| 6061 | * ... body ... |
| 6062 | * end: |
| 6063 | * |
| 6064 | * "if expr | else" Produces instructions: |
| 6065 | * EVAL expr Push result of "expr" |
| 6066 | * JUMP_IF_FALSE else |
| 6067 | * ... body ... |
| 6068 | * JUMP_ALWAYS end |
| 6069 | * else: |
| 6070 | * ... body ... |
| 6071 | * end: |
| 6072 | * |
| 6073 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 6074 | * EVAL expr Push result of "expr" |
| 6075 | * JUMP_IF_FALSE elseif |
| 6076 | * ... body ... |
| 6077 | * JUMP_ALWAYS end |
| 6078 | * elseif: |
| 6079 | * EVAL expr Push result of "expr" |
| 6080 | * JUMP_IF_FALSE else |
| 6081 | * ... body ... |
| 6082 | * JUMP_ALWAYS end |
| 6083 | * else: |
| 6084 | * ... body ... |
| 6085 | * end: |
| 6086 | */ |
| 6087 | static char_u * |
| 6088 | compile_if(char_u *arg, cctx_T *cctx) |
| 6089 | { |
| 6090 | char_u *p = arg; |
| 6091 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6092 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6093 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6094 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6095 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6096 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6097 | CLEAR_FIELD(ppconst); |
| 6098 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6099 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6100 | clear_ppconst(&ppconst); |
| 6101 | return NULL; |
| 6102 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6103 | if (cctx->ctx_skip == SKIP_YES) |
| 6104 | clear_ppconst(&ppconst); |
| 6105 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6106 | { |
| 6107 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6108 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6109 | clear_ppconst(&ppconst); |
| 6110 | } |
| 6111 | else |
| 6112 | { |
| 6113 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6114 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6115 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6116 | return NULL; |
| 6117 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6118 | |
| 6119 | scope = new_scope(cctx, IF_SCOPE); |
| 6120 | if (scope == NULL) |
| 6121 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6122 | scope->se_skip_save = skip_save; |
| 6123 | // "is_had_return" will be reset if any block does not end in :return |
| 6124 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6125 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6126 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6127 | { |
| 6128 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6129 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6130 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6131 | } |
| 6132 | else |
| 6133 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6134 | |
| 6135 | return p; |
| 6136 | } |
| 6137 | |
| 6138 | static char_u * |
| 6139 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 6140 | { |
| 6141 | char_u *p = arg; |
| 6142 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6143 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6144 | isn_T *isn; |
| 6145 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6146 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6147 | |
| 6148 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6149 | { |
| 6150 | emsg(_(e_elseif_without_if)); |
| 6151 | return NULL; |
| 6152 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6153 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6154 | if (!cctx->ctx_had_return) |
| 6155 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6156 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6157 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6158 | { |
| 6159 | 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] | 6160 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6161 | return NULL; |
| 6162 | // previous "if" or "elseif" jumps here |
| 6163 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6164 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6165 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6166 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6167 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6168 | CLEAR_FIELD(ppconst); |
| 6169 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6170 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6171 | clear_ppconst(&ppconst); |
| 6172 | return NULL; |
| 6173 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6174 | if (scope->se_skip_save == SKIP_YES) |
| 6175 | clear_ppconst(&ppconst); |
| 6176 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6177 | { |
| 6178 | // The expression results in a constant. |
| 6179 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6180 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6181 | clear_ppconst(&ppconst); |
| 6182 | scope->se_u.se_if.is_if_label = -1; |
| 6183 | } |
| 6184 | else |
| 6185 | { |
| 6186 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6187 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6188 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6189 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6190 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6191 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6192 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6193 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6194 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6195 | |
| 6196 | return p; |
| 6197 | } |
| 6198 | |
| 6199 | static char_u * |
| 6200 | compile_else(char_u *arg, cctx_T *cctx) |
| 6201 | { |
| 6202 | char_u *p = arg; |
| 6203 | garray_T *instr = &cctx->ctx_instr; |
| 6204 | isn_T *isn; |
| 6205 | scope_T *scope = cctx->ctx_scope; |
| 6206 | |
| 6207 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6208 | { |
| 6209 | emsg(_(e_else_without_if)); |
| 6210 | return NULL; |
| 6211 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6212 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6213 | if (!cctx->ctx_had_return) |
| 6214 | scope->se_u.se_if.is_had_return = FALSE; |
| 6215 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6216 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6217 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6218 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6219 | // 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] | 6220 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6221 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6222 | if (!cctx->ctx_had_return |
| 6223 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6224 | JUMP_ALWAYS, cctx) == FAIL) |
| 6225 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6226 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6227 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6228 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6229 | { |
| 6230 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6231 | { |
| 6232 | // previous "if" or "elseif" jumps here |
| 6233 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6234 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6235 | scope->se_u.se_if.is_if_label = -1; |
| 6236 | } |
| 6237 | } |
| 6238 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6239 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6240 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6241 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6242 | |
| 6243 | return p; |
| 6244 | } |
| 6245 | |
| 6246 | static char_u * |
| 6247 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6248 | { |
| 6249 | scope_T *scope = cctx->ctx_scope; |
| 6250 | ifscope_T *ifscope; |
| 6251 | garray_T *instr = &cctx->ctx_instr; |
| 6252 | isn_T *isn; |
| 6253 | |
| 6254 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6255 | { |
| 6256 | emsg(_(e_endif_without_if)); |
| 6257 | return NULL; |
| 6258 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6259 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6260 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6261 | if (!cctx->ctx_had_return) |
| 6262 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6263 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6264 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6265 | { |
| 6266 | // previous "if" or "elseif" jumps here |
| 6267 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6268 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6269 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6270 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6271 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6272 | cctx->ctx_skip = scope->se_skip_save; |
| 6273 | |
| 6274 | // If all the blocks end in :return and there is an :else then the |
| 6275 | // had_return flag is set. |
| 6276 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6277 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6278 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6279 | return arg; |
| 6280 | } |
| 6281 | |
| 6282 | /* |
| 6283 | * compile "for var in expr" |
| 6284 | * |
| 6285 | * Produces instructions: |
| 6286 | * PUSHNR -1 |
| 6287 | * STORE loop-idx Set index to -1 |
| 6288 | * EVAL expr Push result of "expr" |
| 6289 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6290 | * - if beyond end, jump to "end" |
| 6291 | * - otherwise get item from list and push it |
| 6292 | * STORE var Store item in "var" |
| 6293 | * ... body ... |
| 6294 | * JUMP top Jump back to repeat |
| 6295 | * end: DROP Drop the result of "expr" |
| 6296 | * |
| 6297 | */ |
| 6298 | static char_u * |
| 6299 | compile_for(char_u *arg, cctx_T *cctx) |
| 6300 | { |
| 6301 | char_u *p; |
| 6302 | size_t varlen; |
| 6303 | garray_T *instr = &cctx->ctx_instr; |
| 6304 | garray_T *stack = &cctx->ctx_type_stack; |
| 6305 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6306 | lvar_T *loop_lvar; // loop iteration variable |
| 6307 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6308 | type_T *vartype; |
| 6309 | |
| 6310 | // TODO: list of variables: "for [key, value] in dict" |
| 6311 | // parse "var" |
| 6312 | for (p = arg; eval_isnamec1(*p); ++p) |
| 6313 | ; |
| 6314 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6315 | var_lvar = lookup_local(arg, varlen, cctx); |
| 6316 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6317 | { |
| 6318 | semsg(_("E1023: variable already defined: %s"), arg); |
| 6319 | return NULL; |
| 6320 | } |
| 6321 | |
| 6322 | // consume "in" |
| 6323 | p = skipwhite(p); |
| 6324 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 6325 | { |
| 6326 | emsg(_(e_missing_in)); |
| 6327 | return NULL; |
| 6328 | } |
| 6329 | p = skipwhite(p + 2); |
| 6330 | |
| 6331 | |
| 6332 | scope = new_scope(cctx, FOR_SCOPE); |
| 6333 | if (scope == NULL) |
| 6334 | return NULL; |
| 6335 | |
| 6336 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6337 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6338 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6339 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6340 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6341 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6342 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6343 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6344 | |
| 6345 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6346 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 6347 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6348 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6349 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6350 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6351 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6352 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6353 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6354 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6355 | |
| 6356 | // compile "expr", it remains on the stack until "endfor" |
| 6357 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6358 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6359 | { |
| 6360 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6361 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6362 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6363 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6364 | // Now that we know the type of "var", check that it is a list, now or at |
| 6365 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6366 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 6367 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6368 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6369 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6370 | return NULL; |
| 6371 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6372 | 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] | 6373 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6374 | |
| 6375 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6376 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6377 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6378 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 6379 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6380 | |
| 6381 | return arg; |
| 6382 | } |
| 6383 | |
| 6384 | /* |
| 6385 | * compile "endfor" |
| 6386 | */ |
| 6387 | static char_u * |
| 6388 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 6389 | { |
| 6390 | garray_T *instr = &cctx->ctx_instr; |
| 6391 | scope_T *scope = cctx->ctx_scope; |
| 6392 | forscope_T *forscope; |
| 6393 | isn_T *isn; |
| 6394 | |
| 6395 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 6396 | { |
| 6397 | emsg(_(e_for)); |
| 6398 | return NULL; |
| 6399 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6400 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6401 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6402 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6403 | |
| 6404 | // At end of ":for" scope jump back to the FOR instruction. |
| 6405 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 6406 | |
| 6407 | // Fill in the "end" label in the FOR statement so it can jump here |
| 6408 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 6409 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 6410 | |
| 6411 | // Fill in the "end" label any BREAK statements |
| 6412 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 6413 | |
| 6414 | // Below the ":for" scope drop the "expr" list from the stack. |
| 6415 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 6416 | return NULL; |
| 6417 | |
| 6418 | vim_free(scope); |
| 6419 | |
| 6420 | return arg; |
| 6421 | } |
| 6422 | |
| 6423 | /* |
| 6424 | * compile "while expr" |
| 6425 | * |
| 6426 | * Produces instructions: |
| 6427 | * top: EVAL expr Push result of "expr" |
| 6428 | * JUMP_IF_FALSE end jump if false |
| 6429 | * ... body ... |
| 6430 | * JUMP top Jump back to repeat |
| 6431 | * end: |
| 6432 | * |
| 6433 | */ |
| 6434 | static char_u * |
| 6435 | compile_while(char_u *arg, cctx_T *cctx) |
| 6436 | { |
| 6437 | char_u *p = arg; |
| 6438 | garray_T *instr = &cctx->ctx_instr; |
| 6439 | scope_T *scope; |
| 6440 | |
| 6441 | scope = new_scope(cctx, WHILE_SCOPE); |
| 6442 | if (scope == NULL) |
| 6443 | return NULL; |
| 6444 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6445 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6446 | |
| 6447 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6448 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6449 | return NULL; |
| 6450 | |
| 6451 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6452 | 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] | 6453 | JUMP_IF_FALSE, cctx) == FAIL) |
| 6454 | return FAIL; |
| 6455 | |
| 6456 | return p; |
| 6457 | } |
| 6458 | |
| 6459 | /* |
| 6460 | * compile "endwhile" |
| 6461 | */ |
| 6462 | static char_u * |
| 6463 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 6464 | { |
| 6465 | scope_T *scope = cctx->ctx_scope; |
| 6466 | |
| 6467 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 6468 | { |
| 6469 | emsg(_(e_while)); |
| 6470 | return NULL; |
| 6471 | } |
| 6472 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6473 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6474 | |
| 6475 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6476 | 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] | 6477 | |
| 6478 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 6479 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6480 | 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] | 6481 | |
| 6482 | vim_free(scope); |
| 6483 | |
| 6484 | return arg; |
| 6485 | } |
| 6486 | |
| 6487 | /* |
| 6488 | * compile "continue" |
| 6489 | */ |
| 6490 | static char_u * |
| 6491 | compile_continue(char_u *arg, cctx_T *cctx) |
| 6492 | { |
| 6493 | scope_T *scope = cctx->ctx_scope; |
| 6494 | |
| 6495 | for (;;) |
| 6496 | { |
| 6497 | if (scope == NULL) |
| 6498 | { |
| 6499 | emsg(_(e_continue)); |
| 6500 | return NULL; |
| 6501 | } |
| 6502 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6503 | break; |
| 6504 | scope = scope->se_outer; |
| 6505 | } |
| 6506 | |
| 6507 | // Jump back to the FOR or WHILE instruction. |
| 6508 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6509 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 6510 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6511 | return arg; |
| 6512 | } |
| 6513 | |
| 6514 | /* |
| 6515 | * compile "break" |
| 6516 | */ |
| 6517 | static char_u * |
| 6518 | compile_break(char_u *arg, cctx_T *cctx) |
| 6519 | { |
| 6520 | scope_T *scope = cctx->ctx_scope; |
| 6521 | endlabel_T **el; |
| 6522 | |
| 6523 | for (;;) |
| 6524 | { |
| 6525 | if (scope == NULL) |
| 6526 | { |
| 6527 | emsg(_(e_break)); |
| 6528 | return NULL; |
| 6529 | } |
| 6530 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6531 | break; |
| 6532 | scope = scope->se_outer; |
| 6533 | } |
| 6534 | |
| 6535 | // Jump to the end of the FOR or WHILE loop. |
| 6536 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6537 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6538 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6539 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6540 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 6541 | return FAIL; |
| 6542 | |
| 6543 | return arg; |
| 6544 | } |
| 6545 | |
| 6546 | /* |
| 6547 | * compile "{" start of block |
| 6548 | */ |
| 6549 | static char_u * |
| 6550 | compile_block(char_u *arg, cctx_T *cctx) |
| 6551 | { |
| 6552 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6553 | return NULL; |
| 6554 | return skipwhite(arg + 1); |
| 6555 | } |
| 6556 | |
| 6557 | /* |
| 6558 | * compile end of block: drop one scope |
| 6559 | */ |
| 6560 | static void |
| 6561 | compile_endblock(cctx_T *cctx) |
| 6562 | { |
| 6563 | scope_T *scope = cctx->ctx_scope; |
| 6564 | |
| 6565 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6566 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6567 | vim_free(scope); |
| 6568 | } |
| 6569 | |
| 6570 | /* |
| 6571 | * compile "try" |
| 6572 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6573 | * finally. |
| 6574 | * Creates another scope for the "try" block itself. |
| 6575 | * TRY instruction sets up exception handling at runtime. |
| 6576 | * |
| 6577 | * "try" |
| 6578 | * TRY -> catch1, -> finally push trystack entry |
| 6579 | * ... try block |
| 6580 | * "throw {exception}" |
| 6581 | * EVAL {exception} |
| 6582 | * THROW create exception |
| 6583 | * ... try block |
| 6584 | * " catch {expr}" |
| 6585 | * JUMP -> finally |
| 6586 | * catch1: PUSH exeception |
| 6587 | * EVAL {expr} |
| 6588 | * MATCH |
| 6589 | * JUMP nomatch -> catch2 |
| 6590 | * CATCH remove exception |
| 6591 | * ... catch block |
| 6592 | * " catch" |
| 6593 | * JUMP -> finally |
| 6594 | * catch2: CATCH remove exception |
| 6595 | * ... catch block |
| 6596 | * " finally" |
| 6597 | * finally: |
| 6598 | * ... finally block |
| 6599 | * " endtry" |
| 6600 | * ENDTRY pop trystack entry, may rethrow |
| 6601 | */ |
| 6602 | static char_u * |
| 6603 | compile_try(char_u *arg, cctx_T *cctx) |
| 6604 | { |
| 6605 | garray_T *instr = &cctx->ctx_instr; |
| 6606 | scope_T *try_scope; |
| 6607 | scope_T *scope; |
| 6608 | |
| 6609 | // scope that holds the jumps that go to catch/finally/endtry |
| 6610 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6611 | if (try_scope == NULL) |
| 6612 | return NULL; |
| 6613 | |
| 6614 | // "catch" is set when the first ":catch" is found. |
| 6615 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6616 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6617 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6618 | return NULL; |
| 6619 | |
| 6620 | // scope for the try block itself |
| 6621 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6622 | if (scope == NULL) |
| 6623 | return NULL; |
| 6624 | |
| 6625 | return arg; |
| 6626 | } |
| 6627 | |
| 6628 | /* |
| 6629 | * compile "catch {expr}" |
| 6630 | */ |
| 6631 | static char_u * |
| 6632 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6633 | { |
| 6634 | scope_T *scope = cctx->ctx_scope; |
| 6635 | garray_T *instr = &cctx->ctx_instr; |
| 6636 | char_u *p; |
| 6637 | isn_T *isn; |
| 6638 | |
| 6639 | // end block scope from :try or :catch |
| 6640 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6641 | compile_endblock(cctx); |
| 6642 | scope = cctx->ctx_scope; |
| 6643 | |
| 6644 | // Error if not in a :try scope |
| 6645 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6646 | { |
| 6647 | emsg(_(e_catch)); |
| 6648 | return NULL; |
| 6649 | } |
| 6650 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6651 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6652 | { |
| 6653 | emsg(_("E1033: catch unreachable after catch-all")); |
| 6654 | return NULL; |
| 6655 | } |
| 6656 | |
| 6657 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6658 | 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] | 6659 | JUMP_ALWAYS, cctx) == FAIL) |
| 6660 | return NULL; |
| 6661 | |
| 6662 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6663 | 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] | 6664 | if (isn->isn_arg.try.try_catch == 0) |
| 6665 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6666 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6667 | { |
| 6668 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6669 | 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] | 6670 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6671 | } |
| 6672 | |
| 6673 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6674 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6675 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6676 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6677 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6678 | } |
| 6679 | else |
| 6680 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6681 | char_u *end; |
| 6682 | char_u *pat; |
| 6683 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6684 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6685 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6686 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6687 | // Push v:exception, push {expr} and MATCH |
| 6688 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6689 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6690 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6691 | if (*end != *p) |
| 6692 | { |
| 6693 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 6694 | vim_free(tofree); |
| 6695 | return FAIL; |
| 6696 | } |
| 6697 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6698 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6699 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6700 | len = (int)(end - tofree); |
| 6701 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6702 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6703 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6704 | if (pat == NULL) |
| 6705 | return FAIL; |
| 6706 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6707 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6708 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6709 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6710 | return NULL; |
| 6711 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6712 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6713 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6714 | return NULL; |
| 6715 | } |
| 6716 | |
| 6717 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6718 | return NULL; |
| 6719 | |
| 6720 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6721 | return NULL; |
| 6722 | return p; |
| 6723 | } |
| 6724 | |
| 6725 | static char_u * |
| 6726 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6727 | { |
| 6728 | scope_T *scope = cctx->ctx_scope; |
| 6729 | garray_T *instr = &cctx->ctx_instr; |
| 6730 | isn_T *isn; |
| 6731 | |
| 6732 | // end block scope from :try or :catch |
| 6733 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6734 | compile_endblock(cctx); |
| 6735 | scope = cctx->ctx_scope; |
| 6736 | |
| 6737 | // Error if not in a :try scope |
| 6738 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6739 | { |
| 6740 | emsg(_(e_finally)); |
| 6741 | return NULL; |
| 6742 | } |
| 6743 | |
| 6744 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6745 | 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] | 6746 | if (isn->isn_arg.try.try_finally != 0) |
| 6747 | { |
| 6748 | emsg(_(e_finally_dup)); |
| 6749 | return NULL; |
| 6750 | } |
| 6751 | |
| 6752 | // 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] | 6753 | 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] | 6754 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6755 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6756 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6757 | { |
| 6758 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6759 | 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] | 6760 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6761 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6762 | } |
| 6763 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6764 | // TODO: set index in ts_finally_label jumps |
| 6765 | |
| 6766 | return arg; |
| 6767 | } |
| 6768 | |
| 6769 | static char_u * |
| 6770 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6771 | { |
| 6772 | scope_T *scope = cctx->ctx_scope; |
| 6773 | garray_T *instr = &cctx->ctx_instr; |
| 6774 | isn_T *isn; |
| 6775 | |
| 6776 | // end block scope from :catch or :finally |
| 6777 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6778 | compile_endblock(cctx); |
| 6779 | scope = cctx->ctx_scope; |
| 6780 | |
| 6781 | // Error if not in a :try scope |
| 6782 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6783 | { |
| 6784 | if (scope == NULL) |
| 6785 | emsg(_(e_no_endtry)); |
| 6786 | else if (scope->se_type == WHILE_SCOPE) |
| 6787 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6788 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6789 | emsg(_(e_endfor)); |
| 6790 | else |
| 6791 | emsg(_(e_endif)); |
| 6792 | return NULL; |
| 6793 | } |
| 6794 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6795 | 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] | 6796 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6797 | { |
| 6798 | emsg(_("E1032: missing :catch or :finally")); |
| 6799 | return NULL; |
| 6800 | } |
| 6801 | |
| 6802 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6803 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6804 | 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] | 6805 | |
| 6806 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6807 | if (isn->isn_arg.try.try_catch == 0) |
| 6808 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6809 | if (isn->isn_arg.try.try_finally == 0) |
| 6810 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6811 | |
| 6812 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6813 | { |
| 6814 | // Last catch without match jumps here |
| 6815 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6816 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6817 | } |
| 6818 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6819 | compile_endblock(cctx); |
| 6820 | |
| 6821 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6822 | return NULL; |
| 6823 | return arg; |
| 6824 | } |
| 6825 | |
| 6826 | /* |
| 6827 | * compile "throw {expr}" |
| 6828 | */ |
| 6829 | static char_u * |
| 6830 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6831 | { |
| 6832 | char_u *p = skipwhite(arg); |
| 6833 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6834 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6835 | return NULL; |
| 6836 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6837 | return NULL; |
| 6838 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6839 | return NULL; |
| 6840 | |
| 6841 | return p; |
| 6842 | } |
| 6843 | |
| 6844 | /* |
| 6845 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6846 | * compile "echomsg expr" |
| 6847 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6848 | * compile "execute expr" |
| 6849 | */ |
| 6850 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6851 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6852 | { |
| 6853 | char_u *p = arg; |
| 6854 | int count = 0; |
| 6855 | |
| 6856 | for (;;) |
| 6857 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6858 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6859 | return NULL; |
| 6860 | ++count; |
| 6861 | p = skipwhite(p); |
| 6862 | if (ends_excmd(*p)) |
| 6863 | break; |
| 6864 | } |
| 6865 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6866 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6867 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6868 | else if (cmdidx == CMD_execute) |
| 6869 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6870 | else if (cmdidx == CMD_echomsg) |
| 6871 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6872 | else |
| 6873 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6874 | return p; |
| 6875 | } |
| 6876 | |
| 6877 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6878 | * A command that is not compiled, execute with legacy code. |
| 6879 | */ |
| 6880 | static char_u * |
| 6881 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6882 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6883 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6884 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6885 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6886 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6887 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6888 | goto theend; |
| 6889 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6890 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6891 | { |
| 6892 | long argt = excmd_get_argt(eap->cmdidx); |
| 6893 | int usefilter = FALSE; |
| 6894 | |
| 6895 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6896 | |
| 6897 | // If the command can be followed by a bar, find the bar and truncate |
| 6898 | // it, so that the following command can be compiled. |
| 6899 | // The '|' is overwritten with a NUL, it is put back below. |
| 6900 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6901 | && *eap->arg == '!') |
| 6902 | // :w !filter or :r !filter or :r! filter |
| 6903 | usefilter = TRUE; |
| 6904 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6905 | { |
| 6906 | separate_nextcmd(eap); |
| 6907 | if (eap->nextcmd != NULL) |
| 6908 | nextcmd = eap->nextcmd; |
| 6909 | } |
| 6910 | } |
| 6911 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6912 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6913 | { |
| 6914 | // expand filename in "syntax include [@group] filename" |
| 6915 | has_expr = TRUE; |
| 6916 | eap->arg = skipwhite(eap->arg + 7); |
| 6917 | if (*eap->arg == '@') |
| 6918 | eap->arg = skiptowhite(eap->arg); |
| 6919 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6920 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6921 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6922 | { |
| 6923 | int count = 0; |
| 6924 | char_u *start = skipwhite(line); |
| 6925 | |
| 6926 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6927 | // PUSHS ":cmd xxx" |
| 6928 | // eval expr1 |
| 6929 | // PUSHS "yyy" |
| 6930 | // eval expr2 |
| 6931 | // PUSHS "zzz" |
| 6932 | // EXECCONCAT 5 |
| 6933 | for (;;) |
| 6934 | { |
| 6935 | if (p > start) |
| 6936 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6937 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6938 | ++count; |
| 6939 | } |
| 6940 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6941 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6942 | return NULL; |
| 6943 | may_generate_2STRING(-1, cctx); |
| 6944 | ++count; |
| 6945 | p = skipwhite(p); |
| 6946 | if (*p != '`') |
| 6947 | { |
| 6948 | emsg(_("E1083: missing backtick")); |
| 6949 | return NULL; |
| 6950 | } |
| 6951 | start = p + 1; |
| 6952 | |
| 6953 | p = (char_u *)strstr((char *)start, "`="); |
| 6954 | if (p == NULL) |
| 6955 | { |
| 6956 | if (*skipwhite(start) != NUL) |
| 6957 | { |
| 6958 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6959 | ++count; |
| 6960 | } |
| 6961 | break; |
| 6962 | } |
| 6963 | } |
| 6964 | generate_EXECCONCAT(cctx, count); |
| 6965 | } |
| 6966 | else |
| 6967 | generate_EXEC(cctx, line); |
| 6968 | |
| 6969 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6970 | if (*nextcmd != NUL) |
| 6971 | { |
| 6972 | // the parser expects a pointer to the bar, put it back |
| 6973 | --nextcmd; |
| 6974 | *nextcmd = '|'; |
| 6975 | } |
| 6976 | |
| 6977 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6978 | } |
| 6979 | |
| 6980 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6981 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6982 | * 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] | 6983 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6984 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6985 | add_def_function(ufunc_T *ufunc) |
| 6986 | { |
| 6987 | dfunc_T *dfunc; |
| 6988 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6989 | if (def_functions.ga_len == 0) |
| 6990 | { |
| 6991 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6992 | // wasn't set. |
| 6993 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6994 | return FAIL; |
| 6995 | ++def_functions.ga_len; |
| 6996 | } |
| 6997 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6998 | // Add the function to "def_functions". |
| 6999 | if (ga_grow(&def_functions, 1) == FAIL) |
| 7000 | return FAIL; |
| 7001 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 7002 | CLEAR_POINTER(dfunc); |
| 7003 | dfunc->df_idx = def_functions.ga_len; |
| 7004 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 7005 | dfunc->df_ufunc = ufunc; |
| 7006 | ++def_functions.ga_len; |
| 7007 | return OK; |
| 7008 | } |
| 7009 | |
| 7010 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7011 | * After ex_function() has collected all the function lines: parse and compile |
| 7012 | * the lines into instructions. |
| 7013 | * Adds the function to "def_functions". |
| 7014 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 7015 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7016 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7017 | * This can be used recursively through compile_lambda(), which may reallocate |
| 7018 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7019 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7020 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7021 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7022 | 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] | 7023 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7024 | char_u *line = NULL; |
| 7025 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7026 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7027 | cctx_T cctx; |
| 7028 | garray_T *instr; |
| 7029 | int called_emsg_before = called_emsg; |
| 7030 | int ret = FAIL; |
| 7031 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7032 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7033 | int emsg_before = called_emsg; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7034 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7035 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7036 | // When using a function that was compiled before: Free old instructions. |
| 7037 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7038 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7039 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7040 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7041 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7042 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7043 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7044 | else |
| 7045 | { |
| 7046 | if (add_def_function(ufunc) == FAIL) |
| 7047 | return FAIL; |
| 7048 | new_def_function = TRUE; |
| 7049 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7050 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 7051 | ufunc->uf_def_status = UF_COMPILING; |
| 7052 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7053 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7054 | cctx.ctx_ufunc = ufunc; |
| 7055 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7056 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7057 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 7058 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 7059 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 7060 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 7061 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 7062 | instr = &cctx.ctx_instr; |
| 7063 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7064 | // Set the context to the function, it may be compiled when called from |
| 7065 | // another script. Set the script version to the most modern one. |
| 7066 | // The line number will be set in next_line_from_context(). |
| 7067 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7068 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 7069 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7070 | // Make sure error messages are OK. |
| 7071 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 7072 | if (do_estack_push) |
| 7073 | estack_push_ufunc(ufunc, 1); |
| 7074 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7075 | if (ufunc->uf_def_args.ga_len > 0) |
| 7076 | { |
| 7077 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7078 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7079 | int i; |
| 7080 | char_u *arg; |
| 7081 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7082 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7083 | |
| 7084 | // Produce instructions for the default values of optional arguments. |
| 7085 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 7086 | // where to start when the function is called, depending on the number |
| 7087 | // of arguments. |
| 7088 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 7089 | if (ufunc->uf_def_arg_idx == NULL) |
| 7090 | goto erret; |
| 7091 | for (i = 0; i < count; ++i) |
| 7092 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7093 | garray_T *stack = &cctx.ctx_type_stack; |
| 7094 | type_T *val_type; |
| 7095 | int arg_idx = first_def_arg + i; |
| 7096 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7097 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 7098 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7099 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7100 | goto erret; |
| 7101 | |
| 7102 | // If no type specified use the type of the default value. |
| 7103 | // Otherwise check that the default value type matches the |
| 7104 | // specified type. |
| 7105 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 7106 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7107 | { |
| 7108 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7109 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7110 | } |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 7111 | 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] | 7112 | == FAIL) |
| 7113 | { |
| 7114 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 7115 | arg_idx + 1); |
| 7116 | goto erret; |
| 7117 | } |
| 7118 | |
| 7119 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7120 | goto erret; |
| 7121 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7122 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7123 | |
| 7124 | if (did_set_arg_type) |
| 7125 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7126 | } |
| 7127 | |
| 7128 | /* |
| 7129 | * Loop over all the lines of the function and generate instructions. |
| 7130 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7131 | for (;;) |
| 7132 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7133 | exarg_T ea; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7134 | int starts_with_colon = FALSE; |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7135 | char_u *cmd; |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7136 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7137 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7138 | // Bail out on the first error to avoid a flood of errors and report |
| 7139 | // the right line number when inside try/catch. |
| 7140 | if (emsg_before != called_emsg) |
| 7141 | goto erret; |
| 7142 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7143 | if (line != NULL && *line == '|') |
| 7144 | // the line continues after a '|' |
| 7145 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 7146 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7147 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 7148 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7149 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 7150 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7151 | goto erret; |
| 7152 | } |
| 7153 | else |
| 7154 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 7155 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 7156 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7157 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7158 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7159 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7160 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7161 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7162 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7163 | ea.cmdlinep = &line; |
| 7164 | ea.cmd = skipwhite(line); |
| 7165 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7166 | // Some things can be recognized by the first character. |
| 7167 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7168 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7169 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7170 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7171 | if (ea.cmd[1] != '{') |
| 7172 | { |
| 7173 | line = (char_u *)""; |
| 7174 | continue; |
| 7175 | } |
| 7176 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7177 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7178 | case '}': |
| 7179 | { |
| 7180 | // "}" ends a block scope |
| 7181 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7182 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7183 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7184 | if (stype == BLOCK_SCOPE) |
| 7185 | { |
| 7186 | compile_endblock(&cctx); |
| 7187 | line = ea.cmd; |
| 7188 | } |
| 7189 | else |
| 7190 | { |
| 7191 | emsg(_("E1025: using } outside of a block scope")); |
| 7192 | goto erret; |
| 7193 | } |
| 7194 | if (line != NULL) |
| 7195 | line = skipwhite(ea.cmd + 1); |
| 7196 | continue; |
| 7197 | } |
| 7198 | |
| 7199 | case '{': |
| 7200 | // "{" starts a block scope |
| 7201 | // "{'a': 1}->func() is something else |
| 7202 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7203 | { |
| 7204 | line = compile_block(ea.cmd, &cctx); |
| 7205 | continue; |
| 7206 | } |
| 7207 | break; |
| 7208 | |
| 7209 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7210 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7211 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7212 | } |
| 7213 | |
| 7214 | /* |
| 7215 | * COMMAND MODIFIERS |
| 7216 | */ |
| 7217 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 7218 | { |
| 7219 | if (errormsg != NULL) |
| 7220 | goto erret; |
| 7221 | // empty line or comment |
| 7222 | line = (char_u *)""; |
| 7223 | continue; |
| 7224 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7225 | // TODO: use modifiers in the command |
| 7226 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | b074e8b | 2020-07-11 13:40:45 +0200 | [diff] [blame] | 7227 | CLEAR_FIELD(cmdmod); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7228 | |
| 7229 | // Skip ":call" to get to the function name. |
| 7230 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 7231 | ea.cmd = skipwhite(ea.cmd); |
| 7232 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7233 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7234 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7235 | char_u *pskip; |
| 7236 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7237 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7238 | // find what follows. |
| 7239 | // Skip over "var.member", "var[idx]" and the like. |
| 7240 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 7241 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7242 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7243 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7244 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7245 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7246 | char_u *var_end; |
| 7247 | int oplen; |
| 7248 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7249 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7250 | var_end = find_name_end(pskip, NULL, NULL, |
| 7251 | FNE_CHECK_START | FNE_INCL_BR); |
| 7252 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7253 | if (oplen > 0) |
| 7254 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7255 | size_t len = p - ea.cmd; |
| 7256 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7257 | // Recognize an assignment if we recognize the variable |
| 7258 | // name: |
| 7259 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 7260 | // "local = expr" where "local" is a local var. |
| 7261 | // "script = expr" where "script" is a script-local var. |
| 7262 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7263 | // "&opt = expr" |
| 7264 | // "$ENV = expr" |
| 7265 | // "@r = expr" |
| 7266 | if (*ea.cmd == '&' |
| 7267 | || *ea.cmd == '$' |
| 7268 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7269 | || ((len) > 2 && ea.cmd[1] == ':') |
| 7270 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 7271 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 7272 | NULL, &cctx) == OK |
| 7273 | || lookup_script(ea.cmd, len) == OK |
| 7274 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7275 | { |
| 7276 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7277 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7278 | goto erret; |
| 7279 | continue; |
| 7280 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7281 | } |
| 7282 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7283 | |
| 7284 | if (*ea.cmd == '[') |
| 7285 | { |
| 7286 | // [var, var] = expr |
| 7287 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 7288 | if (line == NULL) |
| 7289 | goto erret; |
| 7290 | if (line != ea.cmd) |
| 7291 | continue; |
| 7292 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7293 | } |
| 7294 | |
| 7295 | /* |
| 7296 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7297 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7298 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7299 | cmd = ea.cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7300 | if (*cmd != '\'') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7301 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7302 | ea.cmd = skip_range(ea.cmd, NULL); |
| 7303 | if (ea.cmd > cmd && !starts_with_colon) |
| 7304 | { |
| 7305 | emsg(_(e_colon_required)); |
| 7306 | goto erret; |
| 7307 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7308 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7309 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7310 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7311 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7312 | |
| 7313 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 7314 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7315 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7316 | { |
| 7317 | line += STRLEN(line); |
| 7318 | continue; |
| 7319 | } |
| 7320 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7321 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7322 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7323 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7324 | // CMD_let cannot happen, compile_assignment() above is used |
| 7325 | iemsg("Command from find_ex_command() not handled"); |
| 7326 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7327 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7328 | } |
| 7329 | |
| 7330 | p = skipwhite(p); |
| 7331 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7332 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 7333 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7334 | && ea.cmdidx != CMD_elseif |
| 7335 | && ea.cmdidx != CMD_else |
| 7336 | && ea.cmdidx != CMD_endif) |
| 7337 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7338 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7339 | continue; |
| 7340 | } |
| 7341 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7342 | if (ea.cmdidx != CMD_elseif |
| 7343 | && ea.cmdidx != CMD_else |
| 7344 | && ea.cmdidx != CMD_endif |
| 7345 | && ea.cmdidx != CMD_endfor |
| 7346 | && ea.cmdidx != CMD_endwhile |
| 7347 | && ea.cmdidx != CMD_catch |
| 7348 | && ea.cmdidx != CMD_finally |
| 7349 | && ea.cmdidx != CMD_endtry) |
| 7350 | { |
| 7351 | if (cctx.ctx_had_return) |
| 7352 | { |
| 7353 | emsg(_("E1095: Unreachable code after :return")); |
| 7354 | goto erret; |
| 7355 | } |
| 7356 | } |
| 7357 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7358 | switch (ea.cmdidx) |
| 7359 | { |
| 7360 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7361 | ea.arg = p; |
| 7362 | line = compile_nested_function(&ea, &cctx); |
| 7363 | break; |
| 7364 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7365 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7366 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7367 | goto erret; |
| 7368 | |
| 7369 | case CMD_return: |
| 7370 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7371 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7372 | break; |
| 7373 | |
| 7374 | case CMD_let: |
| 7375 | case CMD_const: |
| 7376 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7377 | if (line == p) |
| 7378 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7379 | break; |
| 7380 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7381 | case CMD_unlet: |
| 7382 | case CMD_unlockvar: |
| 7383 | case CMD_lockvar: |
| 7384 | line = compile_unletlock(p, &ea, &cctx); |
| 7385 | break; |
| 7386 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7387 | case CMD_import: |
| 7388 | line = compile_import(p, &cctx); |
| 7389 | break; |
| 7390 | |
| 7391 | case CMD_if: |
| 7392 | line = compile_if(p, &cctx); |
| 7393 | break; |
| 7394 | case CMD_elseif: |
| 7395 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7396 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7397 | break; |
| 7398 | case CMD_else: |
| 7399 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7400 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7401 | break; |
| 7402 | case CMD_endif: |
| 7403 | line = compile_endif(p, &cctx); |
| 7404 | break; |
| 7405 | |
| 7406 | case CMD_while: |
| 7407 | line = compile_while(p, &cctx); |
| 7408 | break; |
| 7409 | case CMD_endwhile: |
| 7410 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7411 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7412 | break; |
| 7413 | |
| 7414 | case CMD_for: |
| 7415 | line = compile_for(p, &cctx); |
| 7416 | break; |
| 7417 | case CMD_endfor: |
| 7418 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7419 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7420 | break; |
| 7421 | case CMD_continue: |
| 7422 | line = compile_continue(p, &cctx); |
| 7423 | break; |
| 7424 | case CMD_break: |
| 7425 | line = compile_break(p, &cctx); |
| 7426 | break; |
| 7427 | |
| 7428 | case CMD_try: |
| 7429 | line = compile_try(p, &cctx); |
| 7430 | break; |
| 7431 | case CMD_catch: |
| 7432 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7433 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7434 | break; |
| 7435 | case CMD_finally: |
| 7436 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7437 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7438 | break; |
| 7439 | case CMD_endtry: |
| 7440 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7441 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7442 | break; |
| 7443 | case CMD_throw: |
| 7444 | line = compile_throw(p, &cctx); |
| 7445 | break; |
| 7446 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7447 | case CMD_eval: |
| 7448 | if (compile_expr0(&p, &cctx) == FAIL) |
| 7449 | goto erret; |
| 7450 | |
| 7451 | // drop the return value |
| 7452 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 7453 | |
| 7454 | line = skipwhite(p); |
| 7455 | break; |
| 7456 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7457 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7458 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7459 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7460 | case CMD_echomsg: |
| 7461 | case CMD_echoerr: |
| 7462 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7463 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7464 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7465 | // TODO: other commands with an expression argument |
| 7466 | |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 7467 | case CMD_append: |
| 7468 | case CMD_change: |
| 7469 | case CMD_insert: |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 7470 | case CMD_t: |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 7471 | case CMD_xit: |
| 7472 | not_in_vim9(&ea); |
| 7473 | goto erret; |
| 7474 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7475 | case CMD_SIZE: |
| 7476 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 7477 | goto erret; |
| 7478 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7479 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7480 | // Not recognized, execute with do_cmdline_cmd(). |
| 7481 | ea.arg = p; |
| 7482 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7483 | break; |
| 7484 | } |
| 7485 | if (line == NULL) |
| 7486 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7487 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7488 | |
| 7489 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7490 | { |
| 7491 | iemsg("Type stack underflow"); |
| 7492 | goto erret; |
| 7493 | } |
| 7494 | } |
| 7495 | |
| 7496 | if (cctx.ctx_scope != NULL) |
| 7497 | { |
| 7498 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7499 | emsg(_(e_endif)); |
| 7500 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7501 | emsg(_(e_endwhile)); |
| 7502 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7503 | emsg(_(e_endfor)); |
| 7504 | else |
| 7505 | emsg(_("E1026: Missing }")); |
| 7506 | goto erret; |
| 7507 | } |
| 7508 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7509 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7510 | { |
| 7511 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7512 | { |
| 7513 | emsg(_("E1027: Missing return statement")); |
| 7514 | goto erret; |
| 7515 | } |
| 7516 | |
| 7517 | // Return zero if there is no return at the end. |
| 7518 | generate_PUSHNR(&cctx, 0); |
| 7519 | generate_instr(&cctx, ISN_RETURN); |
| 7520 | } |
| 7521 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7522 | { |
| 7523 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7524 | + ufunc->uf_dfunc_idx; |
| 7525 | dfunc->df_deleted = FALSE; |
| 7526 | dfunc->df_instr = instr->ga_data; |
| 7527 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7528 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 7529 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7530 | if (cctx.ctx_outer_used) |
| 7531 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7532 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7533 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7534 | |
| 7535 | ret = OK; |
| 7536 | |
| 7537 | erret: |
| 7538 | if (ret == FAIL) |
| 7539 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7540 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7541 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7542 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7543 | |
| 7544 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7545 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7546 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7547 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7548 | // If using the last entry in the table and it was added above, we |
| 7549 | // might as well remove it. |
| 7550 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7551 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7552 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7553 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7554 | ufunc->uf_dfunc_idx = 0; |
| 7555 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7556 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7557 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7558 | while (cctx.ctx_scope != NULL) |
| 7559 | drop_scope(&cctx); |
| 7560 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7561 | // Don't execute this function body. |
| 7562 | ga_clear_strings(&ufunc->uf_lines); |
| 7563 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7564 | if (errormsg != NULL) |
| 7565 | emsg(errormsg); |
| 7566 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 7567 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7568 | } |
| 7569 | |
| 7570 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7571 | if (do_estack_push) |
| 7572 | estack_pop(); |
| 7573 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7574 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7575 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7576 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7577 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7578 | } |
| 7579 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7580 | void |
| 7581 | set_function_type(ufunc_T *ufunc) |
| 7582 | { |
| 7583 | int varargs = ufunc->uf_va_name != NULL; |
| 7584 | int argcount = ufunc->uf_args.ga_len; |
| 7585 | |
| 7586 | // Create a type for the function, with the return type and any |
| 7587 | // argument types. |
| 7588 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7589 | // The type is included in "tt_args". |
| 7590 | if (argcount > 0 || varargs) |
| 7591 | { |
| 7592 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7593 | argcount, &ufunc->uf_type_list); |
| 7594 | // Add argument types to the function type. |
| 7595 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7596 | argcount + varargs, |
| 7597 | &ufunc->uf_type_list) == FAIL) |
| 7598 | return; |
| 7599 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7600 | ufunc->uf_func_type->tt_min_argcount = |
| 7601 | argcount - ufunc->uf_def_args.ga_len; |
| 7602 | if (ufunc->uf_arg_types == NULL) |
| 7603 | { |
| 7604 | int i; |
| 7605 | |
| 7606 | // lambda does not have argument types. |
| 7607 | for (i = 0; i < argcount; ++i) |
| 7608 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7609 | } |
| 7610 | else |
| 7611 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7612 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7613 | if (varargs) |
| 7614 | { |
| 7615 | ufunc->uf_func_type->tt_args[argcount] = |
| 7616 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7617 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7618 | } |
| 7619 | } |
| 7620 | else |
| 7621 | // No arguments, can use a predefined type. |
| 7622 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7623 | argcount, &ufunc->uf_type_list); |
| 7624 | } |
| 7625 | |
| 7626 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7627 | /* |
| 7628 | * Delete an instruction, free what it contains. |
| 7629 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7630 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7631 | delete_instr(isn_T *isn) |
| 7632 | { |
| 7633 | switch (isn->isn_type) |
| 7634 | { |
| 7635 | case ISN_EXEC: |
| 7636 | case ISN_LOADENV: |
| 7637 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7638 | case ISN_LOADB: |
| 7639 | case ISN_LOADW: |
| 7640 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7641 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7642 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7643 | case ISN_PUSHEXC: |
| 7644 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7645 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7646 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7647 | case ISN_STOREB: |
| 7648 | case ISN_STOREW: |
| 7649 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7650 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7651 | vim_free(isn->isn_arg.string); |
| 7652 | break; |
| 7653 | |
| 7654 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7655 | case ISN_STORES: |
| 7656 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7657 | break; |
| 7658 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7659 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7660 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7661 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7662 | break; |
| 7663 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7664 | case ISN_STOREOPT: |
| 7665 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7666 | break; |
| 7667 | |
| 7668 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7669 | blob_unref(isn->isn_arg.blob); |
| 7670 | break; |
| 7671 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7672 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7673 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7674 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7675 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7676 | break; |
| 7677 | |
| 7678 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7679 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7680 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7681 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7682 | break; |
| 7683 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7684 | case ISN_UCALL: |
| 7685 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7686 | break; |
| 7687 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7688 | case ISN_FUNCREF: |
| 7689 | { |
| 7690 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7691 | + isn->isn_arg.funcref.fr_func; |
| 7692 | func_ptr_unref(dfunc->df_ufunc); |
| 7693 | } |
| 7694 | break; |
| 7695 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7696 | case ISN_NEWFUNC: |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 7697 | { |
| 7698 | char_u *lambda = isn->isn_arg.newfunc.nf_lambda; |
| 7699 | ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL); |
| 7700 | |
| 7701 | if (ufunc != NULL) |
| 7702 | { |
| 7703 | // Clear uf_dfunc_idx so that the function is deleted. |
| 7704 | clear_def_function(ufunc); |
| 7705 | ufunc->uf_dfunc_idx = 0; |
| 7706 | func_ptr_unref(ufunc); |
| 7707 | } |
| 7708 | |
| 7709 | vim_free(lambda); |
| 7710 | vim_free(isn->isn_arg.newfunc.nf_global); |
| 7711 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7712 | break; |
| 7713 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7714 | case ISN_2BOOL: |
| 7715 | case ISN_2STRING: |
| 7716 | case ISN_ADDBLOB: |
| 7717 | case ISN_ADDLIST: |
| 7718 | case ISN_BCALL: |
| 7719 | case ISN_CATCH: |
| 7720 | case ISN_CHECKNR: |
| 7721 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7722 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7723 | case ISN_COMPAREANY: |
| 7724 | case ISN_COMPAREBLOB: |
| 7725 | case ISN_COMPAREBOOL: |
| 7726 | case ISN_COMPAREDICT: |
| 7727 | case ISN_COMPAREFLOAT: |
| 7728 | case ISN_COMPAREFUNC: |
| 7729 | case ISN_COMPARELIST: |
| 7730 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7731 | case ISN_COMPARESPECIAL: |
| 7732 | case ISN_COMPARESTRING: |
| 7733 | case ISN_CONCAT: |
| 7734 | case ISN_DCALL: |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 7735 | case ISN_SHUFFLE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7736 | case ISN_DROP: |
| 7737 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7738 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7739 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7740 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7741 | case ISN_EXECCONCAT: |
| 7742 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7743 | case ISN_FOR: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7744 | case ISN_LISTINDEX: |
| 7745 | case ISN_STRINDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7746 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7747 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7748 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7749 | case ISN_JUMP: |
| 7750 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7751 | case ISN_LOADBDICT: |
| 7752 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7753 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7754 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7755 | case ISN_LOADSCRIPT: |
| 7756 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7757 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7758 | case ISN_LOADWDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7759 | case ISN_NEGATENR: |
| 7760 | case ISN_NEWDICT: |
| 7761 | case ISN_NEWLIST: |
| 7762 | case ISN_OPNR: |
| 7763 | case ISN_OPFLOAT: |
| 7764 | case ISN_OPANY: |
| 7765 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7766 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7767 | case ISN_PUSHF: |
| 7768 | case ISN_PUSHNR: |
| 7769 | case ISN_PUSHBOOL: |
| 7770 | case ISN_PUSHSPEC: |
| 7771 | case ISN_RETURN: |
| 7772 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 7773 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7774 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7775 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7776 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7777 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7778 | case ISN_STOREDICT: |
| 7779 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7780 | case ISN_THROW: |
| 7781 | case ISN_TRY: |
| 7782 | // nothing allocated |
| 7783 | break; |
| 7784 | } |
| 7785 | } |
| 7786 | |
| 7787 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7788 | * Free all instructions for "dfunc". |
| 7789 | */ |
| 7790 | static void |
| 7791 | delete_def_function_contents(dfunc_T *dfunc) |
| 7792 | { |
| 7793 | int idx; |
| 7794 | |
| 7795 | ga_clear(&dfunc->df_def_args_isn); |
| 7796 | |
| 7797 | if (dfunc->df_instr != NULL) |
| 7798 | { |
| 7799 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7800 | delete_instr(dfunc->df_instr + idx); |
| 7801 | VIM_CLEAR(dfunc->df_instr); |
| 7802 | } |
| 7803 | |
| 7804 | dfunc->df_deleted = TRUE; |
| 7805 | } |
| 7806 | |
| 7807 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7808 | * When a user function is deleted, clear the contents of any associated def |
| 7809 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7810 | */ |
| 7811 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7812 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7813 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7814 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7815 | { |
| 7816 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7817 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7818 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7819 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7820 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7821 | } |
| 7822 | } |
| 7823 | |
| 7824 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7825 | /* |
| 7826 | * Free all functions defined with ":def". |
| 7827 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7828 | void |
| 7829 | free_def_functions(void) |
| 7830 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7831 | int idx; |
| 7832 | |
| 7833 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7834 | { |
| 7835 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7836 | |
| 7837 | delete_def_function_contents(dfunc); |
| 7838 | } |
| 7839 | |
| 7840 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7841 | } |
| 7842 | #endif |
| 7843 | |
| 7844 | |
| 7845 | #endif // FEAT_EVAL |