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 | /* |
| 1526 | * Generate an ISN_JUMP instruction. |
| 1527 | */ |
| 1528 | static int |
| 1529 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1530 | { |
| 1531 | isn_T *isn; |
| 1532 | garray_T *stack = &cctx->ctx_type_stack; |
| 1533 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1534 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1535 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1536 | return FAIL; |
| 1537 | isn->isn_arg.jump.jump_when = when; |
| 1538 | isn->isn_arg.jump.jump_where = where; |
| 1539 | |
| 1540 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1541 | --stack->ga_len; |
| 1542 | |
| 1543 | return OK; |
| 1544 | } |
| 1545 | |
| 1546 | static int |
| 1547 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1548 | { |
| 1549 | isn_T *isn; |
| 1550 | garray_T *stack = &cctx->ctx_type_stack; |
| 1551 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1552 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1553 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1554 | return FAIL; |
| 1555 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1556 | |
| 1557 | if (ga_grow(stack, 1) == FAIL) |
| 1558 | return FAIL; |
| 1559 | // type doesn't matter, will be stored next |
| 1560 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1561 | ++stack->ga_len; |
| 1562 | |
| 1563 | return OK; |
| 1564 | } |
| 1565 | |
| 1566 | /* |
| 1567 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1568 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1569 | * Return FAIL if the number of arguments is wrong. |
| 1570 | */ |
| 1571 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1572 | 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] | 1573 | { |
| 1574 | isn_T *isn; |
| 1575 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1576 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1577 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1578 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1579 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1580 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1581 | argoff = check_internal_func(func_idx, argcount); |
| 1582 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1583 | return FAIL; |
| 1584 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1585 | if (method_call && argoff > 1) |
| 1586 | { |
| 1587 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1588 | return FAIL; |
| 1589 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1590 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1591 | } |
| 1592 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1593 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1594 | return FAIL; |
| 1595 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1596 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1597 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1598 | for (i = 0; i < argcount; ++i) |
| 1599 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1600 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1601 | stack->ga_len -= argcount; // drop the arguments |
| 1602 | if (ga_grow(stack, 1) == FAIL) |
| 1603 | return FAIL; |
| 1604 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1605 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1606 | ++stack->ga_len; // add return value |
| 1607 | |
| 1608 | return OK; |
| 1609 | } |
| 1610 | |
| 1611 | /* |
| 1612 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1613 | * Return FAIL if the number of arguments is wrong. |
| 1614 | */ |
| 1615 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1616 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1617 | { |
| 1618 | isn_T *isn; |
| 1619 | garray_T *stack = &cctx->ctx_type_stack; |
| 1620 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1621 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1622 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1623 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1624 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1625 | { |
| 1626 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1627 | return FAIL; |
| 1628 | } |
| 1629 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1630 | { |
| 1631 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1632 | return FAIL; |
| 1633 | } |
| 1634 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1635 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1636 | { |
| 1637 | int i; |
| 1638 | |
| 1639 | for (i = 0; i < argcount; ++i) |
| 1640 | { |
| 1641 | type_T *expected; |
| 1642 | type_T *actual; |
| 1643 | |
| 1644 | if (i < regular_args) |
| 1645 | { |
| 1646 | if (ufunc->uf_arg_types == NULL) |
| 1647 | continue; |
| 1648 | expected = ufunc->uf_arg_types[i]; |
| 1649 | } |
| 1650 | else |
| 1651 | expected = ufunc->uf_va_type->tt_member; |
| 1652 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1653 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1654 | { |
| 1655 | arg_type_mismatch(expected, actual, i + 1); |
| 1656 | return FAIL; |
| 1657 | } |
| 1658 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1659 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 1660 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1661 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1662 | } |
| 1663 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1664 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1665 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1666 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1667 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1668 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1669 | { |
| 1670 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1671 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1672 | } |
| 1673 | else |
| 1674 | { |
| 1675 | // A user function may be deleted and redefined later, can't use the |
| 1676 | // ufunc pointer, need to look it up again at runtime. |
| 1677 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1678 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1679 | } |
| 1680 | |
| 1681 | stack->ga_len -= argcount; // drop the arguments |
| 1682 | if (ga_grow(stack, 1) == FAIL) |
| 1683 | return FAIL; |
| 1684 | // add return value |
| 1685 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1686 | ++stack->ga_len; |
| 1687 | |
| 1688 | return OK; |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1693 | */ |
| 1694 | static int |
| 1695 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1696 | { |
| 1697 | isn_T *isn; |
| 1698 | garray_T *stack = &cctx->ctx_type_stack; |
| 1699 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1700 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1701 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1702 | return FAIL; |
| 1703 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1704 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1705 | |
| 1706 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1707 | if (ga_grow(stack, 1) == FAIL) |
| 1708 | return FAIL; |
| 1709 | // add return value |
| 1710 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1711 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1712 | |
| 1713 | return OK; |
| 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1718 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1719 | */ |
| 1720 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1721 | generate_PCALL( |
| 1722 | cctx_T *cctx, |
| 1723 | int argcount, |
| 1724 | char_u *name, |
| 1725 | type_T *type, |
| 1726 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1727 | { |
| 1728 | isn_T *isn; |
| 1729 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1730 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1731 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1732 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1733 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1734 | if (type->tt_type == VAR_ANY) |
| 1735 | ret_type = &t_any; |
| 1736 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1737 | { |
| 1738 | if (type->tt_argcount != -1) |
| 1739 | { |
| 1740 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1741 | |
| 1742 | if (argcount < type->tt_min_argcount - varargs) |
| 1743 | { |
| 1744 | semsg(_(e_toofewarg), "[reference]"); |
| 1745 | return FAIL; |
| 1746 | } |
| 1747 | if (!varargs && argcount > type->tt_argcount) |
| 1748 | { |
| 1749 | semsg(_(e_toomanyarg), "[reference]"); |
| 1750 | return FAIL; |
| 1751 | } |
| 1752 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1753 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1754 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1755 | else |
| 1756 | { |
| 1757 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1758 | return FAIL; |
| 1759 | } |
| 1760 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1761 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1762 | return FAIL; |
| 1763 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1764 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1765 | |
| 1766 | stack->ga_len -= argcount; // drop the arguments |
| 1767 | |
| 1768 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1769 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1770 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1771 | // If partial is above the arguments it must be cleared and replaced with |
| 1772 | // the return value. |
| 1773 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1774 | return FAIL; |
| 1775 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1776 | return OK; |
| 1777 | } |
| 1778 | |
| 1779 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1780 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1781 | */ |
| 1782 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1783 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1784 | { |
| 1785 | isn_T *isn; |
| 1786 | garray_T *stack = &cctx->ctx_type_stack; |
| 1787 | type_T *type; |
| 1788 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1789 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1790 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1791 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1792 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1793 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1794 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1795 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1796 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1797 | { |
| 1798 | emsg(_(e_dictreq)); |
| 1799 | return FAIL; |
| 1800 | } |
| 1801 | // change dict type to dict member type |
| 1802 | if (type->tt_type == VAR_DICT) |
| 1803 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1804 | |
| 1805 | return OK; |
| 1806 | } |
| 1807 | |
| 1808 | /* |
| 1809 | * Generate an ISN_ECHO instruction. |
| 1810 | */ |
| 1811 | static int |
| 1812 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1813 | { |
| 1814 | isn_T *isn; |
| 1815 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1816 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1817 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1818 | return FAIL; |
| 1819 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1820 | isn->isn_arg.echo.echo_count = count; |
| 1821 | |
| 1822 | return OK; |
| 1823 | } |
| 1824 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1825 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1826 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1827 | */ |
| 1828 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1829 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1830 | { |
| 1831 | isn_T *isn; |
| 1832 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1833 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1834 | return FAIL; |
| 1835 | isn->isn_arg.number = count; |
| 1836 | |
| 1837 | return OK; |
| 1838 | } |
| 1839 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1840 | static int |
| 1841 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1842 | { |
| 1843 | isn_T *isn; |
| 1844 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1845 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1846 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1847 | return FAIL; |
| 1848 | isn->isn_arg.string = vim_strsave(line); |
| 1849 | return OK; |
| 1850 | } |
| 1851 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1852 | static int |
| 1853 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1854 | { |
| 1855 | isn_T *isn; |
| 1856 | |
| 1857 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1858 | return FAIL; |
| 1859 | isn->isn_arg.number = count; |
| 1860 | return OK; |
| 1861 | } |
| 1862 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1863 | /* |
| 1864 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1865 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1866 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1867 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1868 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1869 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1870 | lvar_T *lvar; |
| 1871 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1872 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1873 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1874 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1875 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1879 | return NULL; |
| 1880 | 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] | 1881 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1882 | // Every local variable uses the next entry on the stack. We could re-use |
| 1883 | // the last ones when leaving a scope, but then variables used in a closure |
| 1884 | // might get overwritten. To keep things simple do not re-use stack |
| 1885 | // entries. This is less efficient, but memory is cheap these days. |
| 1886 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1887 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1888 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1889 | lvar->lv_const = isConst; |
| 1890 | lvar->lv_type = type; |
| 1891 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1892 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1896 | * Remove local variables above "new_top". |
| 1897 | */ |
| 1898 | static void |
| 1899 | unwind_locals(cctx_T *cctx, int new_top) |
| 1900 | { |
| 1901 | if (cctx->ctx_locals.ga_len > new_top) |
| 1902 | { |
| 1903 | int idx; |
| 1904 | lvar_T *lvar; |
| 1905 | |
| 1906 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1907 | { |
| 1908 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1909 | vim_free(lvar->lv_name); |
| 1910 | } |
| 1911 | } |
| 1912 | cctx->ctx_locals.ga_len = new_top; |
| 1913 | } |
| 1914 | |
| 1915 | /* |
| 1916 | * Free all local variables. |
| 1917 | */ |
| 1918 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1919 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1920 | { |
| 1921 | unwind_locals(cctx, 0); |
| 1922 | ga_clear(&cctx->ctx_locals); |
| 1923 | } |
| 1924 | |
| 1925 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1926 | * 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] | 1927 | * When "optional" is TRUE then a leading "?" is accepted. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1928 | */ |
| 1929 | char_u * |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 1930 | skip_type(char_u *start, int optional) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1931 | { |
| 1932 | char_u *p = start; |
| 1933 | |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 1934 | if (optional && *p == '?') |
| 1935 | ++p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1936 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1937 | ++p; |
| 1938 | |
| 1939 | // Skip over "<type>"; this is permissive about white space. |
| 1940 | if (*skipwhite(p) == '<') |
| 1941 | { |
| 1942 | p = skipwhite(p); |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 1943 | p = skip_type(skipwhite(p + 1), FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1944 | p = skipwhite(p); |
| 1945 | if (*p == '>') |
| 1946 | ++p; |
| 1947 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1948 | else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1]))) |
| 1949 | && STRNCMP("func", start, 4) == 0) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1950 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1951 | if (*p == '(') |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1952 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1953 | // handle func(args): type |
| 1954 | ++p; |
| 1955 | while (*p != ')' && *p != NUL) |
| 1956 | { |
| 1957 | char_u *sp = p; |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1958 | |
Bram Moolenaar | ace6132 | 2020-07-26 18:16:58 +0200 | [diff] [blame] | 1959 | if (STRNCMP(p, "...", 3) == 0) |
| 1960 | p += 3; |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 1961 | p = skip_type(p, TRUE); |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1962 | if (p == sp) |
| 1963 | return p; // syntax error |
| 1964 | if (*p == ',') |
| 1965 | p = skipwhite(p + 1); |
| 1966 | } |
| 1967 | if (*p == ')') |
| 1968 | { |
| 1969 | if (p[1] == ':') |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 1970 | p = skip_type(skipwhite(p + 2), FALSE); |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1971 | else |
Bram Moolenaar | bfba865 | 2020-07-23 20:09:10 +0200 | [diff] [blame] | 1972 | ++p; |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1973 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1974 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1975 | else |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1976 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1977 | // handle func: return_type |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame] | 1978 | p = skip_type(skipwhite(p + 1), FALSE); |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1979 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1980 | } |
| 1981 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1982 | return p; |
| 1983 | } |
| 1984 | |
| 1985 | /* |
| 1986 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1987 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1988 | * Returns NULL in case of failure. |
| 1989 | */ |
| 1990 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1991 | 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] | 1992 | { |
| 1993 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1994 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1995 | |
| 1996 | if (**arg != '<') |
| 1997 | { |
| 1998 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1999 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2000 | else |
| 2001 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 2002 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2003 | } |
| 2004 | *arg = skipwhite(*arg + 1); |
| 2005 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2006 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2007 | |
| 2008 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2009 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2010 | { |
| 2011 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 2012 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2013 | } |
| 2014 | ++*arg; |
| 2015 | |
| 2016 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2017 | return get_list_type(member_type, type_gap); |
| 2018 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | /* |
| 2022 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 2023 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2024 | */ |
| 2025 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2026 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2027 | { |
| 2028 | char_u *p = *arg; |
| 2029 | size_t len; |
| 2030 | |
| 2031 | // skip over the first word |
| 2032 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 2033 | ++p; |
| 2034 | len = p - *arg; |
| 2035 | |
| 2036 | switch (**arg) |
| 2037 | { |
| 2038 | case 'a': |
| 2039 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 2040 | { |
| 2041 | *arg += len; |
| 2042 | return &t_any; |
| 2043 | } |
| 2044 | break; |
| 2045 | case 'b': |
| 2046 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 2047 | { |
| 2048 | *arg += len; |
| 2049 | return &t_bool; |
| 2050 | } |
| 2051 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 2052 | { |
| 2053 | *arg += len; |
| 2054 | return &t_blob; |
| 2055 | } |
| 2056 | break; |
| 2057 | case 'c': |
| 2058 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 2059 | { |
| 2060 | *arg += len; |
| 2061 | return &t_channel; |
| 2062 | } |
| 2063 | break; |
| 2064 | case 'd': |
| 2065 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 2066 | { |
| 2067 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2068 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2069 | } |
| 2070 | break; |
| 2071 | case 'f': |
| 2072 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 2073 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2074 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2075 | *arg += len; |
| 2076 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2077 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2078 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2079 | return &t_any; |
| 2080 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2081 | } |
| 2082 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 2083 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2084 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2085 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2086 | int argcount = -1; |
| 2087 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2088 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2089 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 2090 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2091 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2092 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2093 | if (**arg == '(') |
| 2094 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2095 | // "func" may or may not return a value, "func()" does |
| 2096 | // not return a value. |
| 2097 | ret_type = &t_void; |
| 2098 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2099 | p = ++*arg; |
| 2100 | argcount = 0; |
| 2101 | while (*p != NUL && *p != ')') |
| 2102 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2103 | if (*p == '?') |
| 2104 | { |
| 2105 | if (first_optional == -1) |
| 2106 | first_optional = argcount; |
| 2107 | ++p; |
| 2108 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2109 | else if (STRNCMP(p, "...", 3) == 0) |
| 2110 | { |
| 2111 | flags |= TTFLAG_VARARGS; |
| 2112 | p += 3; |
| 2113 | } |
Bram Moolenaar | 01865ad | 2020-07-26 18:33:09 +0200 | [diff] [blame] | 2114 | else if (first_optional != -1) |
| 2115 | { |
| 2116 | emsg(_("E1007: mandatory argument after optional argument")); |
| 2117 | return &t_any; |
| 2118 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2119 | |
| 2120 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 2121 | |
| 2122 | // Nothing comes after "...{type}". |
| 2123 | if (flags & TTFLAG_VARARGS) |
| 2124 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2125 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2126 | if (*p != ',' && *skipwhite(p) == ',') |
| 2127 | { |
| 2128 | semsg(_(e_no_white_before), ","); |
| 2129 | return &t_any; |
| 2130 | } |
| 2131 | if (*p == ',') |
| 2132 | { |
| 2133 | ++p; |
| 2134 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2135 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2136 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2137 | return &t_any; |
| 2138 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2139 | } |
| 2140 | p = skipwhite(p); |
| 2141 | if (argcount == MAX_FUNC_ARGS) |
| 2142 | { |
| 2143 | emsg(_("E740: Too many argument types")); |
| 2144 | return &t_any; |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | p = skipwhite(p); |
| 2149 | if (*p != ')') |
| 2150 | { |
| 2151 | emsg(_(e_missing_close)); |
| 2152 | return &t_any; |
| 2153 | } |
| 2154 | *arg = p + 1; |
| 2155 | } |
| 2156 | if (**arg == ':') |
| 2157 | { |
| 2158 | // parse return type |
| 2159 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2160 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2161 | semsg(_(e_white_after), ":"); |
| 2162 | *arg = skipwhite(*arg); |
| 2163 | ret_type = parse_type(arg, type_gap); |
| 2164 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2165 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2166 | type = get_func_type(ret_type, argcount, type_gap); |
| 2167 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2168 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2169 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 2170 | type->tt_flags = flags; |
| 2171 | if (argcount > 0) |
| 2172 | { |
| 2173 | type->tt_argcount = argcount; |
| 2174 | type->tt_min_argcount = first_optional == -1 |
| 2175 | ? argcount : first_optional; |
| 2176 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2177 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2178 | return &t_any; |
| 2179 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2180 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2181 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2182 | } |
| 2183 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2184 | } |
| 2185 | break; |
| 2186 | case 'j': |
| 2187 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 2188 | { |
| 2189 | *arg += len; |
| 2190 | return &t_job; |
| 2191 | } |
| 2192 | break; |
| 2193 | case 'l': |
| 2194 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 2195 | { |
| 2196 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2197 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2198 | } |
| 2199 | break; |
| 2200 | case 'n': |
| 2201 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 2202 | { |
| 2203 | *arg += len; |
| 2204 | return &t_number; |
| 2205 | } |
| 2206 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2207 | case 's': |
| 2208 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 2209 | { |
| 2210 | *arg += len; |
| 2211 | return &t_string; |
| 2212 | } |
| 2213 | break; |
| 2214 | case 'v': |
| 2215 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 2216 | { |
| 2217 | *arg += len; |
| 2218 | return &t_void; |
| 2219 | } |
| 2220 | break; |
| 2221 | } |
| 2222 | |
| 2223 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 2224 | return &t_any; |
| 2225 | } |
| 2226 | |
| 2227 | /* |
| 2228 | * Check if "type1" and "type2" are exactly the same. |
| 2229 | */ |
| 2230 | static int |
| 2231 | equal_type(type_T *type1, type_T *type2) |
| 2232 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2233 | int i; |
| 2234 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2235 | if (type1->tt_type != type2->tt_type) |
| 2236 | return FALSE; |
| 2237 | switch (type1->tt_type) |
| 2238 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2239 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2240 | case VAR_ANY: |
| 2241 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2242 | case VAR_SPECIAL: |
| 2243 | case VAR_BOOL: |
| 2244 | case VAR_NUMBER: |
| 2245 | case VAR_FLOAT: |
| 2246 | case VAR_STRING: |
| 2247 | case VAR_BLOB: |
| 2248 | case VAR_JOB: |
| 2249 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2250 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2251 | case VAR_LIST: |
| 2252 | case VAR_DICT: |
| 2253 | return equal_type(type1->tt_member, type2->tt_member); |
| 2254 | case VAR_FUNC: |
| 2255 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2256 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2257 | || type1->tt_argcount != type2->tt_argcount) |
| 2258 | return FALSE; |
| 2259 | if (type1->tt_argcount < 0 |
| 2260 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2261 | return TRUE; |
| 2262 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2263 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2264 | return FALSE; |
| 2265 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2266 | } |
| 2267 | return TRUE; |
| 2268 | } |
| 2269 | |
| 2270 | /* |
| 2271 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2272 | * "type2" and "dest" may be the same. |
| 2273 | */ |
| 2274 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2275 | 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] | 2276 | { |
| 2277 | if (equal_type(type1, type2)) |
| 2278 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2279 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2280 | return; |
| 2281 | } |
| 2282 | |
| 2283 | if (type1->tt_type == type2->tt_type) |
| 2284 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2285 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2286 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2287 | type_T *common; |
| 2288 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2289 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2290 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2291 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2292 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2293 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2294 | return; |
| 2295 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2296 | if (type1->tt_type == VAR_FUNC) |
| 2297 | { |
| 2298 | type_T *common; |
| 2299 | |
| 2300 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2301 | if (type1->tt_argcount == type2->tt_argcount |
| 2302 | && type1->tt_argcount >= 0) |
| 2303 | { |
| 2304 | int argcount = type1->tt_argcount; |
| 2305 | int i; |
| 2306 | |
| 2307 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2308 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2309 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2310 | if (func_type_add_arg_types(*dest, argcount, |
| 2311 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2312 | for (i = 0; i < argcount; ++i) |
| 2313 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2314 | &(*dest)->tt_args[i], type_gap); |
| 2315 | } |
| 2316 | } |
| 2317 | else |
| 2318 | *dest = alloc_func_type(common, -1, type_gap); |
| 2319 | return; |
| 2320 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2321 | } |
| 2322 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2323 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | char * |
| 2327 | vartype_name(vartype_T type) |
| 2328 | { |
| 2329 | switch (type) |
| 2330 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2331 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2332 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2333 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2334 | case VAR_SPECIAL: return "special"; |
| 2335 | case VAR_BOOL: return "bool"; |
| 2336 | case VAR_NUMBER: return "number"; |
| 2337 | case VAR_FLOAT: return "float"; |
| 2338 | case VAR_STRING: return "string"; |
| 2339 | case VAR_BLOB: return "blob"; |
| 2340 | case VAR_JOB: return "job"; |
| 2341 | case VAR_CHANNEL: return "channel"; |
| 2342 | case VAR_LIST: return "list"; |
| 2343 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2344 | |
| 2345 | case VAR_FUNC: |
| 2346 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2347 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2348 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | /* |
| 2352 | * Return the name of a type. |
| 2353 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2354 | */ |
| 2355 | char * |
| 2356 | type_name(type_T *type, char **tofree) |
| 2357 | { |
| 2358 | char *name = vartype_name(type->tt_type); |
| 2359 | |
| 2360 | *tofree = NULL; |
| 2361 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2362 | { |
| 2363 | char *member_free; |
| 2364 | char *member_name = type_name(type->tt_member, &member_free); |
| 2365 | size_t len; |
| 2366 | |
| 2367 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2368 | *tofree = alloc(len); |
| 2369 | if (*tofree != NULL) |
| 2370 | { |
| 2371 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2372 | vim_free(member_free); |
| 2373 | return *tofree; |
| 2374 | } |
| 2375 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2376 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2377 | { |
| 2378 | garray_T ga; |
| 2379 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2380 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2381 | |
| 2382 | ga_init2(&ga, 1, 100); |
| 2383 | if (ga_grow(&ga, 20) == FAIL) |
| 2384 | return "[unknown]"; |
| 2385 | *tofree = ga.ga_data; |
| 2386 | STRCPY(ga.ga_data, "func("); |
| 2387 | ga.ga_len += 5; |
| 2388 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2389 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2390 | { |
| 2391 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2392 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2393 | int len; |
| 2394 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2395 | if (type->tt_args == NULL) |
| 2396 | arg_type = "[unknown]"; |
| 2397 | else |
| 2398 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2399 | if (i > 0) |
| 2400 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2401 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2402 | ga.ga_len += 2; |
| 2403 | } |
| 2404 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2405 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2406 | { |
| 2407 | vim_free(arg_free); |
| 2408 | return "[unknown]"; |
| 2409 | } |
| 2410 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2411 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2412 | { |
| 2413 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2414 | ga.ga_len += 3; |
| 2415 | } |
| 2416 | else if (i >= type->tt_min_argcount) |
| 2417 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2418 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2419 | ga.ga_len += len; |
| 2420 | vim_free(arg_free); |
| 2421 | } |
| 2422 | |
| 2423 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2424 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2425 | else |
| 2426 | { |
| 2427 | char *ret_free; |
| 2428 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2429 | int len; |
| 2430 | |
| 2431 | len = (int)STRLEN(ret_name) + 4; |
| 2432 | if (ga_grow(&ga, len) == FAIL) |
| 2433 | { |
| 2434 | vim_free(ret_free); |
| 2435 | return "[unknown]"; |
| 2436 | } |
| 2437 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2438 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2439 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2440 | vim_free(ret_free); |
| 2441 | } |
| 2442 | return ga.ga_data; |
| 2443 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2444 | |
| 2445 | return name; |
| 2446 | } |
| 2447 | |
| 2448 | /* |
| 2449 | * Find "name" in script-local items of script "sid". |
| 2450 | * Returns the index in "sn_var_vals" if found. |
| 2451 | * If found but not in "sn_var_vals" returns -1. |
| 2452 | * If not found returns -2. |
| 2453 | */ |
| 2454 | int |
| 2455 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2456 | { |
| 2457 | hashtab_T *ht; |
| 2458 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2459 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2460 | int idx; |
| 2461 | |
| 2462 | // First look the name up in the hashtable. |
| 2463 | if (sid <= 0 || sid > script_items.ga_len) |
| 2464 | return -1; |
| 2465 | ht = &SCRIPT_VARS(sid); |
| 2466 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2467 | if (di == NULL) |
| 2468 | return -2; |
| 2469 | |
| 2470 | // Now find the svar_T index in sn_var_vals. |
| 2471 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2472 | { |
| 2473 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2474 | |
| 2475 | if (sv->sv_tv == &di->di_tv) |
| 2476 | { |
| 2477 | if (check_writable && sv->sv_const) |
| 2478 | semsg(_(e_readonlyvar), name); |
| 2479 | return idx; |
| 2480 | } |
| 2481 | } |
| 2482 | return -1; |
| 2483 | } |
| 2484 | |
| 2485 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2486 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2487 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2488 | */ |
| 2489 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2490 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2491 | { |
Bram Moolenaar | 086eb18 | 2020-07-01 16:00:44 +0200 | [diff] [blame] | 2492 | scriptitem_T *si; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2493 | int idx; |
| 2494 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2495 | if (current_sctx.sc_sid <= 0) |
| 2496 | return NULL; |
| 2497 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2498 | if (cctx != NULL) |
| 2499 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2500 | { |
| 2501 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2502 | + idx; |
| 2503 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2504 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2505 | : STRLEN(import->imp_name) == len |
| 2506 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2507 | return import; |
| 2508 | } |
| 2509 | |
| 2510 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2511 | { |
| 2512 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2513 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2514 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2515 | : STRLEN(import->imp_name) == len |
| 2516 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2517 | return import; |
| 2518 | } |
| 2519 | return NULL; |
| 2520 | } |
| 2521 | |
| 2522 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2523 | * Free all imported variables. |
| 2524 | */ |
| 2525 | static void |
| 2526 | free_imported(cctx_T *cctx) |
| 2527 | { |
| 2528 | int idx; |
| 2529 | |
| 2530 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2531 | { |
| 2532 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2533 | |
| 2534 | vim_free(import->imp_name); |
| 2535 | } |
| 2536 | ga_clear(&cctx->ctx_imports); |
| 2537 | } |
| 2538 | |
| 2539 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2540 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2541 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 2542 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2543 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2544 | { |
| 2545 | return p[0] == '#' && p[1] != '{'; |
| 2546 | } |
| 2547 | |
| 2548 | /* |
| 2549 | * Return a pointer to the next line that isn't empty or only contains a |
| 2550 | * comment. Skips over white space. |
| 2551 | * Returns NULL if there is none. |
| 2552 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2553 | char_u * |
| 2554 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2555 | { |
| 2556 | int lnum = cctx->ctx_lnum; |
| 2557 | |
| 2558 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2559 | { |
| 2560 | 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] | 2561 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2562 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 2563 | if (line == NULL) |
| 2564 | break; |
| 2565 | p = skipwhite(line); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2566 | if (*p != NUL && !vim9_comment_start(p)) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2567 | return p; |
| 2568 | } |
| 2569 | return NULL; |
| 2570 | } |
| 2571 | |
| 2572 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2573 | * Called when checking for a following operator at "arg". When the rest of |
| 2574 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2575 | * line return a pointer to it and set "nextp". |
| 2576 | * Otherwise skip over white space. |
| 2577 | */ |
| 2578 | static char_u * |
| 2579 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2580 | { |
| 2581 | char_u *p = skipwhite(arg); |
| 2582 | |
| 2583 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2584 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2585 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2586 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2587 | if (*nextp != NULL) |
| 2588 | return *nextp; |
| 2589 | } |
| 2590 | return p; |
| 2591 | } |
| 2592 | |
| 2593 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2594 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2595 | * 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] | 2596 | * Returns NULL when at the end. |
| 2597 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2598 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2599 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2600 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2601 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2602 | |
| 2603 | do |
| 2604 | { |
| 2605 | ++cctx->ctx_lnum; |
| 2606 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2607 | { |
| 2608 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2609 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2610 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2611 | 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] | 2612 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2613 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2614 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2615 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2616 | return line; |
| 2617 | } |
| 2618 | |
| 2619 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2620 | * 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] | 2621 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2622 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2623 | */ |
| 2624 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2625 | 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] | 2626 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2627 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2628 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2629 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2630 | |
| 2631 | if (next == NULL) |
| 2632 | return FAIL; |
| 2633 | *arg = skipwhite(next); |
| 2634 | } |
| 2635 | return OK; |
| 2636 | } |
| 2637 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2638 | /* |
| 2639 | * Idem, and give an error when failed. |
| 2640 | */ |
| 2641 | static int |
| 2642 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2643 | { |
| 2644 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2645 | { |
| 2646 | emsg(_("E1097: line incomplete")); |
| 2647 | return FAIL; |
| 2648 | } |
| 2649 | return OK; |
| 2650 | } |
| 2651 | |
| 2652 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2653 | // Structure passed between the compile_expr* functions to keep track of |
| 2654 | // constants that have been parsed but for which no code was produced yet. If |
| 2655 | // possible expressions on these constants are applied at compile time. If |
| 2656 | // that is not possible, the code to push the constants needs to be generated |
| 2657 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2658 | // Using 50 should be more than enough of 5 levels of (). |
| 2659 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2660 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2661 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2662 | int pp_used; // active entries in pp_tv[] |
| 2663 | } ppconst_T; |
| 2664 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2665 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2666 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2667 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2668 | /* |
| 2669 | * Generate a PUSH instruction for "tv". |
| 2670 | * "tv" will be consumed or cleared. |
| 2671 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2672 | */ |
| 2673 | static int |
| 2674 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2675 | { |
| 2676 | if (tv != NULL) |
| 2677 | { |
| 2678 | switch (tv->v_type) |
| 2679 | { |
| 2680 | case VAR_UNKNOWN: |
| 2681 | break; |
| 2682 | case VAR_BOOL: |
| 2683 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2684 | break; |
| 2685 | case VAR_SPECIAL: |
| 2686 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2687 | break; |
| 2688 | case VAR_NUMBER: |
| 2689 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2690 | break; |
| 2691 | #ifdef FEAT_FLOAT |
| 2692 | case VAR_FLOAT: |
| 2693 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2694 | break; |
| 2695 | #endif |
| 2696 | case VAR_BLOB: |
| 2697 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2698 | tv->vval.v_blob = NULL; |
| 2699 | break; |
| 2700 | case VAR_STRING: |
| 2701 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2702 | tv->vval.v_string = NULL; |
| 2703 | break; |
| 2704 | default: |
| 2705 | iemsg("constant type not supported"); |
| 2706 | clear_tv(tv); |
| 2707 | return FAIL; |
| 2708 | } |
| 2709 | tv->v_type = VAR_UNKNOWN; |
| 2710 | } |
| 2711 | return OK; |
| 2712 | } |
| 2713 | |
| 2714 | /* |
| 2715 | * Generate code for any ppconst entries. |
| 2716 | */ |
| 2717 | static int |
| 2718 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2719 | { |
| 2720 | int i; |
| 2721 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2722 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2723 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2724 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2725 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2726 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2727 | ret = FAIL; |
| 2728 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2729 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2730 | return ret; |
| 2731 | } |
| 2732 | |
| 2733 | /* |
| 2734 | * Clear ppconst constants. Used when failing. |
| 2735 | */ |
| 2736 | static void |
| 2737 | clear_ppconst(ppconst_T *ppconst) |
| 2738 | { |
| 2739 | int i; |
| 2740 | |
| 2741 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2742 | clear_tv(&ppconst->pp_tv[i]); |
| 2743 | ppconst->pp_used = 0; |
| 2744 | } |
| 2745 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2746 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2747 | * Generate an instruction to load script-local variable "name", without the |
| 2748 | * leading "s:". |
| 2749 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2750 | */ |
| 2751 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2752 | compile_load_scriptvar( |
| 2753 | cctx_T *cctx, |
| 2754 | char_u *name, // variable NUL terminated |
| 2755 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2756 | char_u **end, // end of variable |
| 2757 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2758 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2759 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2760 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2761 | imported_T *import; |
| 2762 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2763 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2764 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2765 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2766 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2767 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2768 | } |
| 2769 | if (idx >= 0) |
| 2770 | { |
| 2771 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2772 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2773 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2774 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2775 | return OK; |
| 2776 | } |
| 2777 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2778 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2779 | if (import != NULL) |
| 2780 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2781 | if (import->imp_all) |
| 2782 | { |
| 2783 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2784 | char_u *exp_name; |
| 2785 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2786 | ufunc_T *ufunc; |
| 2787 | type_T *type; |
| 2788 | |
| 2789 | // Used "import * as Name", need to lookup the member. |
| 2790 | if (*p != '.') |
| 2791 | { |
| 2792 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2793 | return FAIL; |
| 2794 | } |
| 2795 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2796 | if (VIM_ISWHITE(*p)) |
| 2797 | { |
| 2798 | emsg(_("E1074: no white space allowed after dot")); |
| 2799 | return FAIL; |
| 2800 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2801 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2802 | // isolate one name |
| 2803 | exp_name = p; |
| 2804 | while (eval_isnamec(*p)) |
| 2805 | ++p; |
| 2806 | cc = *p; |
| 2807 | *p = NUL; |
| 2808 | |
| 2809 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2810 | *p = cc; |
| 2811 | p = skipwhite(p); |
| 2812 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2813 | // TODO: what if it is a function? |
| 2814 | if (idx < 0) |
| 2815 | return FAIL; |
| 2816 | *end = p; |
| 2817 | |
| 2818 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2819 | import->imp_sid, |
| 2820 | idx, |
| 2821 | type); |
| 2822 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2823 | else if (import->imp_funcname != NULL) |
| 2824 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2825 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2826 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2827 | import->imp_sid, |
| 2828 | import->imp_var_vals_idx, |
| 2829 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2830 | return OK; |
| 2831 | } |
| 2832 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2833 | if (error) |
| 2834 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2835 | return FAIL; |
| 2836 | } |
| 2837 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2838 | static int |
| 2839 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2840 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2841 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2842 | |
| 2843 | if (ufunc == NULL) |
| 2844 | return FAIL; |
| 2845 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2846 | // Need to compile any default values to get the argument types. |
| 2847 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2848 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2849 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2850 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2851 | } |
| 2852 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2853 | /* |
| 2854 | * Compile a variable name into a load instruction. |
| 2855 | * "end" points to just after the name. |
| 2856 | * When "error" is FALSE do not give an error when not found. |
| 2857 | */ |
| 2858 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2859 | 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] | 2860 | { |
| 2861 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2862 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2863 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2864 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2865 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2866 | |
| 2867 | if (*(*arg + 1) == ':') |
| 2868 | { |
| 2869 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2870 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2871 | { |
| 2872 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2873 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2874 | switch (**arg) |
| 2875 | { |
| 2876 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2877 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2878 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2879 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2880 | default: |
| 2881 | semsg(_(e_namespace), *arg); |
| 2882 | goto theend; |
| 2883 | } |
| 2884 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2885 | goto theend; |
| 2886 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2887 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2888 | else |
| 2889 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2890 | isntype_T isn_type = ISN_DROP; |
| 2891 | |
| 2892 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2893 | if (name == NULL) |
| 2894 | return FAIL; |
| 2895 | |
| 2896 | switch (**arg) |
| 2897 | { |
| 2898 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2899 | break; |
| 2900 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2901 | NULL, NULL, error); |
| 2902 | break; |
| 2903 | case 'g': isn_type = ISN_LOADG; break; |
| 2904 | case 'w': isn_type = ISN_LOADW; break; |
| 2905 | case 't': isn_type = ISN_LOADT; break; |
| 2906 | case 'b': isn_type = ISN_LOADB; break; |
| 2907 | default: semsg(_(e_namespace), *arg); |
| 2908 | goto theend; |
| 2909 | } |
| 2910 | if (isn_type != ISN_DROP) |
| 2911 | { |
| 2912 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2913 | // variables can be defined later, thus we don't check if it |
| 2914 | // exists, give error at runtime. |
| 2915 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2916 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2917 | } |
| 2918 | } |
| 2919 | else |
| 2920 | { |
| 2921 | size_t len = end - *arg; |
| 2922 | int idx; |
| 2923 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2924 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2925 | |
| 2926 | name = vim_strnsave(*arg, end - *arg); |
| 2927 | if (name == NULL) |
| 2928 | return FAIL; |
| 2929 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2930 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2931 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2932 | if (!gen_load_outer) |
| 2933 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2934 | } |
| 2935 | else |
| 2936 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2937 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2938 | |
| 2939 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2940 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2941 | type = lvar->lv_type; |
| 2942 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2943 | if (lvar->lv_from_outer) |
| 2944 | gen_load_outer = TRUE; |
| 2945 | else |
| 2946 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2947 | } |
| 2948 | else |
| 2949 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2950 | // "var" can be script-local even without using "s:" if it |
| 2951 | // already exists. |
| 2952 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2953 | == SCRIPT_VERSION_VIM9 |
| 2954 | || lookup_script(*arg, len) == OK) |
| 2955 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2956 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2957 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2958 | // When the name starts with an uppercase letter or "x:" it |
| 2959 | // can be a user defined function. |
| 2960 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2961 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2962 | } |
| 2963 | } |
| 2964 | if (gen_load) |
| 2965 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2966 | if (gen_load_outer) |
| 2967 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2968 | } |
| 2969 | |
| 2970 | *arg = end; |
| 2971 | |
| 2972 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2973 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2974 | semsg(_(e_var_notfound), name); |
| 2975 | vim_free(name); |
| 2976 | return res; |
| 2977 | } |
| 2978 | |
| 2979 | /* |
| 2980 | * Compile the argument expressions. |
| 2981 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2982 | */ |
| 2983 | static int |
| 2984 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2985 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2986 | char_u *p = *arg; |
| 2987 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2988 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2989 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2990 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2991 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2992 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2993 | if (*p == ')') |
| 2994 | { |
| 2995 | *arg = p + 1; |
| 2996 | return OK; |
| 2997 | } |
| 2998 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2999 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3000 | return FAIL; |
| 3001 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3002 | |
| 3003 | if (*p != ',' && *skipwhite(p) == ',') |
| 3004 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 3005 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3006 | p = skipwhite(p); |
| 3007 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3008 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3009 | { |
| 3010 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 3011 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 3012 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3013 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3014 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3015 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3016 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3017 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 3018 | emsg(_(e_missing_close)); |
| 3019 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | /* |
| 3023 | * Compile a function call: name(arg1, arg2) |
| 3024 | * "arg" points to "name", "arg + varlen" to the "(". |
| 3025 | * "argcount_init" is 1 for "value->method()" |
| 3026 | * Instructions: |
| 3027 | * EVAL arg1 |
| 3028 | * EVAL arg2 |
| 3029 | * BCALL / DCALL / UCALL |
| 3030 | */ |
| 3031 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3032 | compile_call( |
| 3033 | char_u **arg, |
| 3034 | size_t varlen, |
| 3035 | cctx_T *cctx, |
| 3036 | ppconst_T *ppconst, |
| 3037 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3038 | { |
| 3039 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 3040 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3041 | int argcount = argcount_init; |
| 3042 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3043 | char_u fname_buf[FLEN_FIXED + 1]; |
| 3044 | char_u *tofree = NULL; |
| 3045 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3046 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3047 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3048 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3049 | // we can evaluate "has('name')" at compile time |
| 3050 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 3051 | { |
| 3052 | char_u *s = skipwhite(*arg + varlen + 1); |
| 3053 | typval_T argvars[2]; |
| 3054 | |
| 3055 | argvars[0].v_type = VAR_UNKNOWN; |
| 3056 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3057 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3058 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3059 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3060 | s = skipwhite(s); |
| 3061 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 3062 | { |
| 3063 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 3064 | |
| 3065 | *arg = s + 1; |
| 3066 | argvars[1].v_type = VAR_UNKNOWN; |
| 3067 | tv->v_type = VAR_NUMBER; |
| 3068 | tv->vval.v_number = 0; |
| 3069 | f_has(argvars, tv); |
| 3070 | clear_tv(&argvars[0]); |
| 3071 | ++ppconst->pp_used; |
| 3072 | return OK; |
| 3073 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 3074 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3075 | } |
| 3076 | |
| 3077 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3078 | return FAIL; |
| 3079 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3080 | if (varlen >= sizeof(namebuf)) |
| 3081 | { |
| 3082 | semsg(_("E1011: name too long: %s"), name); |
| 3083 | return FAIL; |
| 3084 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3085 | vim_strncpy(namebuf, *arg, varlen); |
| 3086 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3087 | |
| 3088 | *arg = skipwhite(*arg + varlen + 1); |
| 3089 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3090 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3091 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3092 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3093 | { |
| 3094 | int idx; |
| 3095 | |
| 3096 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3097 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3098 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 3099 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3100 | else |
| 3101 | semsg(_(e_unknownfunc), namebuf); |
| 3102 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3103 | } |
| 3104 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3105 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 3106 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3107 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3108 | { |
| 3109 | res = generate_CALL(cctx, ufunc, argcount); |
| 3110 | goto theend; |
| 3111 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3112 | |
| 3113 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3114 | // 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] | 3115 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3116 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 3117 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3118 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3119 | garray_T *stack = &cctx->ctx_type_stack; |
| 3120 | type_T *type; |
| 3121 | |
| 3122 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3123 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3124 | goto theend; |
| 3125 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3126 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3127 | // 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] | 3128 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3129 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 3130 | res = generate_UCALL(cctx, name, argcount); |
| 3131 | else |
| 3132 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3133 | |
| 3134 | theend: |
| 3135 | vim_free(tofree); |
| 3136 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 3140 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 3141 | |
| 3142 | /* |
| 3143 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 3144 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3145 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3146 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 3147 | * valid name. |
| 3148 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3149 | static char_u * |
| 3150 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3151 | { |
| 3152 | char_u *p; |
| 3153 | |
| 3154 | // Quick check for valid starting character. |
| 3155 | if (!eval_isnamec1(*arg)) |
| 3156 | return arg; |
| 3157 | |
| 3158 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 3159 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 3160 | // and can be used in slice "[n:]". |
| 3161 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3162 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3163 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 3164 | break; |
| 3165 | return p; |
| 3166 | } |
| 3167 | |
| 3168 | /* |
| 3169 | * 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] | 3170 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3171 | */ |
| 3172 | char_u * |
| 3173 | to_name_const_end(char_u *arg) |
| 3174 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3175 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3176 | typval_T rettv; |
| 3177 | |
| 3178 | if (p == arg && *arg == '[') |
| 3179 | { |
| 3180 | |
| 3181 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3182 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3183 | p = arg; |
| 3184 | } |
| 3185 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 3186 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3187 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3188 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3189 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3190 | p = arg; |
| 3191 | } |
| 3192 | else if (p == arg && *arg == '{') |
| 3193 | { |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3194 | int ret = get_lambda_tv(&p, &rettv, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3195 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3196 | // Can be "{x -> ret}()". |
| 3197 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3198 | if (ret == NOTDONE) |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3199 | ret = eval_dict(&p, &rettv, NULL, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3200 | if (ret != OK) |
| 3201 | p = arg; |
| 3202 | } |
| 3203 | |
| 3204 | return p; |
| 3205 | } |
| 3206 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3207 | /* |
| 3208 | * parse a list: [expr, expr] |
| 3209 | * "*arg" points to the '['. |
| 3210 | */ |
| 3211 | static int |
| 3212 | compile_list(char_u **arg, cctx_T *cctx) |
| 3213 | { |
| 3214 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3215 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3216 | int count = 0; |
| 3217 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3218 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3219 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3220 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3221 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3222 | semsg(_(e_list_end), *arg); |
| 3223 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3224 | } |
| 3225 | if (*p == ']') |
| 3226 | { |
| 3227 | ++p; |
| 3228 | // Allow for following comment, after at least one space. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3229 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '#') |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3230 | p += STRLEN(p); |
| 3231 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3232 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3233 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3234 | break; |
| 3235 | ++count; |
| 3236 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3237 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3238 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3239 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 3240 | { |
| 3241 | semsg(_(e_white_after), ","); |
| 3242 | return FAIL; |
| 3243 | } |
| 3244 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3245 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3246 | p = skipwhite(p); |
| 3247 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3248 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3249 | |
| 3250 | generate_NEWLIST(cctx, count); |
| 3251 | return OK; |
| 3252 | } |
| 3253 | |
| 3254 | /* |
| 3255 | * parse a lambda: {arg, arg -> expr} |
| 3256 | * "*arg" points to the '{'. |
| 3257 | */ |
| 3258 | static int |
| 3259 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 3260 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3261 | typval_T rettv; |
| 3262 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3263 | evalarg_T evalarg; |
| 3264 | |
| 3265 | CLEAR_FIELD(evalarg); |
| 3266 | evalarg.eval_flags = EVAL_EVALUATE; |
| 3267 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3268 | |
| 3269 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3270 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3271 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3272 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3273 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3274 | ++ufunc->uf_refcount; |
| 3275 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3276 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3277 | |
| 3278 | // The function will have one line: "return {expr}". |
| 3279 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3280 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3281 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 3282 | clear_evalarg(&evalarg, NULL); |
| 3283 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 3284 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 3285 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3286 | |
| 3287 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3288 | return FAIL; |
| 3289 | } |
| 3290 | |
| 3291 | /* |
| 3292 | * Compile a lamda call: expr->{lambda}(args) |
| 3293 | * "arg" points to the "{". |
| 3294 | */ |
| 3295 | static int |
| 3296 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 3297 | { |
| 3298 | ufunc_T *ufunc; |
| 3299 | typval_T rettv; |
| 3300 | int argcount = 1; |
| 3301 | int ret = FAIL; |
| 3302 | |
| 3303 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3304 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3305 | return FAIL; |
| 3306 | |
| 3307 | if (**arg != '(') |
| 3308 | { |
| 3309 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 3310 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3311 | else |
| 3312 | semsg(_(e_missing_paren), "lambda"); |
| 3313 | clear_tv(&rettv); |
| 3314 | return FAIL; |
| 3315 | } |
| 3316 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3317 | ufunc = rettv.vval.v_partial->pt_func; |
| 3318 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3319 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3320 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3321 | |
| 3322 | // The function will have one line: "return {expr}". |
| 3323 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3324 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3325 | |
| 3326 | // compile the arguments |
| 3327 | *arg = skipwhite(*arg + 1); |
| 3328 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 3329 | // call the compiled function |
| 3330 | ret = generate_CALL(cctx, ufunc, argcount); |
| 3331 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3332 | if (ret == FAIL) |
| 3333 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3334 | return ret; |
| 3335 | } |
| 3336 | |
| 3337 | /* |
| 3338 | * parse a dict: {'key': val} or #{key: val} |
| 3339 | * "*arg" points to the '{'. |
| 3340 | */ |
| 3341 | static int |
| 3342 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 3343 | { |
| 3344 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3345 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3346 | int count = 0; |
| 3347 | dict_T *d = dict_alloc(); |
| 3348 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3349 | char_u *whitep = *arg; |
| 3350 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3351 | |
| 3352 | if (d == NULL) |
| 3353 | return FAIL; |
| 3354 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3355 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3356 | { |
| 3357 | char_u *key = NULL; |
| 3358 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3359 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3360 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3361 | *arg = NULL; |
| 3362 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | if (**arg == '}') |
| 3366 | break; |
| 3367 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3368 | if (literal) |
| 3369 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3370 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3371 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3372 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3373 | { |
| 3374 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 3375 | return FAIL; |
| 3376 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3377 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3378 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3379 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3380 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3381 | } |
| 3382 | else |
| 3383 | { |
| 3384 | isn_T *isn; |
| 3385 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3386 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3387 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3388 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3389 | if (isn->isn_type == ISN_PUSHS) |
| 3390 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3391 | else |
| 3392 | { |
| 3393 | type_T *keytype = ((type_T **)stack->ga_data) |
| 3394 | [stack->ga_len - 1]; |
| 3395 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 3396 | return FAIL; |
| 3397 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | // Check for duplicate keys, if using string keys. |
| 3401 | if (key != NULL) |
| 3402 | { |
| 3403 | item = dict_find(d, key, -1); |
| 3404 | if (item != NULL) |
| 3405 | { |
| 3406 | semsg(_(e_duplicate_key), key); |
| 3407 | goto failret; |
| 3408 | } |
| 3409 | item = dictitem_alloc(key); |
| 3410 | if (item != NULL) |
| 3411 | { |
| 3412 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3413 | item->di_tv.v_lock = 0; |
| 3414 | if (dict_add(d, item) == FAIL) |
| 3415 | dictitem_free(item); |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | *arg = skipwhite(*arg); |
| 3420 | if (**arg != ':') |
| 3421 | { |
| 3422 | semsg(_(e_missing_dict_colon), *arg); |
| 3423 | return FAIL; |
| 3424 | } |
| 3425 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3426 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3427 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3428 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3429 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3430 | *arg = NULL; |
| 3431 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3432 | } |
| 3433 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3434 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3435 | return FAIL; |
| 3436 | ++count; |
| 3437 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3438 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3439 | *arg = skipwhite(*arg); |
| 3440 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3441 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3442 | *arg = NULL; |
| 3443 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3444 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3445 | if (**arg == '}') |
| 3446 | break; |
| 3447 | if (**arg != ',') |
| 3448 | { |
| 3449 | semsg(_(e_missing_dict_comma), *arg); |
| 3450 | goto failret; |
| 3451 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3452 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3453 | *arg = skipwhite(*arg + 1); |
| 3454 | } |
| 3455 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3456 | *arg = *arg + 1; |
| 3457 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3458 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3459 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3460 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3461 | *arg += STRLEN(*arg); |
| 3462 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3463 | dict_unref(d); |
| 3464 | return generate_NEWDICT(cctx, count); |
| 3465 | |
| 3466 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3467 | if (*arg == NULL) |
| 3468 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3469 | dict_unref(d); |
| 3470 | return FAIL; |
| 3471 | } |
| 3472 | |
| 3473 | /* |
| 3474 | * Compile "&option". |
| 3475 | */ |
| 3476 | static int |
| 3477 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3478 | { |
| 3479 | typval_T rettv; |
| 3480 | char_u *start = *arg; |
| 3481 | int ret; |
| 3482 | |
| 3483 | // parse the option and get the current value to get the type. |
| 3484 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3485 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3486 | if (ret == OK) |
| 3487 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3488 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3489 | char_u *name = vim_strnsave(start, *arg - start); |
| 3490 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3491 | |
| 3492 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3493 | vim_free(name); |
| 3494 | } |
| 3495 | clear_tv(&rettv); |
| 3496 | |
| 3497 | return ret; |
| 3498 | } |
| 3499 | |
| 3500 | /* |
| 3501 | * Compile "$VAR". |
| 3502 | */ |
| 3503 | static int |
| 3504 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3505 | { |
| 3506 | char_u *start = *arg; |
| 3507 | int len; |
| 3508 | int ret; |
| 3509 | char_u *name; |
| 3510 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3511 | ++*arg; |
| 3512 | len = get_env_len(arg); |
| 3513 | if (len == 0) |
| 3514 | { |
| 3515 | semsg(_(e_syntax_at), start - 1); |
| 3516 | return FAIL; |
| 3517 | } |
| 3518 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3519 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3520 | name = vim_strnsave(start, len + 1); |
| 3521 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3522 | vim_free(name); |
| 3523 | return ret; |
| 3524 | } |
| 3525 | |
| 3526 | /* |
| 3527 | * Compile "@r". |
| 3528 | */ |
| 3529 | static int |
| 3530 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3531 | { |
| 3532 | int ret; |
| 3533 | |
| 3534 | ++*arg; |
| 3535 | if (**arg == NUL) |
| 3536 | { |
| 3537 | semsg(_(e_syntax_at), *arg - 1); |
| 3538 | return FAIL; |
| 3539 | } |
| 3540 | if (!valid_yank_reg(**arg, TRUE)) |
| 3541 | { |
| 3542 | emsg_invreg(**arg); |
| 3543 | return FAIL; |
| 3544 | } |
| 3545 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3546 | ++*arg; |
| 3547 | return ret; |
| 3548 | } |
| 3549 | |
| 3550 | /* |
| 3551 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3552 | */ |
| 3553 | static int |
| 3554 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3555 | { |
| 3556 | char_u *p = end; |
| 3557 | |
| 3558 | // this works from end to start |
| 3559 | while (p > start) |
| 3560 | { |
| 3561 | --p; |
| 3562 | if (*p == '-' || *p == '+') |
| 3563 | { |
| 3564 | // only '-' has an effect, for '+' we only check the type |
| 3565 | #ifdef FEAT_FLOAT |
| 3566 | if (rettv->v_type == VAR_FLOAT) |
| 3567 | { |
| 3568 | if (*p == '-') |
| 3569 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3570 | } |
| 3571 | else |
| 3572 | #endif |
| 3573 | { |
| 3574 | varnumber_T val; |
| 3575 | int error = FALSE; |
| 3576 | |
| 3577 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3578 | // here |
| 3579 | if (check_not_string(rettv) == FAIL) |
| 3580 | return FAIL; |
| 3581 | val = tv_get_number_chk(rettv, &error); |
| 3582 | clear_tv(rettv); |
| 3583 | if (error) |
| 3584 | return FAIL; |
| 3585 | if (*p == '-') |
| 3586 | val = -val; |
| 3587 | rettv->v_type = VAR_NUMBER; |
| 3588 | rettv->vval.v_number = val; |
| 3589 | } |
| 3590 | } |
| 3591 | else |
| 3592 | { |
| 3593 | int v = tv2bool(rettv); |
| 3594 | |
| 3595 | // '!' is permissive in the type. |
| 3596 | clear_tv(rettv); |
| 3597 | rettv->v_type = VAR_BOOL; |
| 3598 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3599 | } |
| 3600 | } |
| 3601 | return OK; |
| 3602 | } |
| 3603 | |
| 3604 | /* |
| 3605 | * Recognize v: variables that are constants and set "rettv". |
| 3606 | */ |
| 3607 | static void |
| 3608 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3609 | { |
| 3610 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3611 | { |
| 3612 | rettv->v_type = VAR_BOOL; |
| 3613 | rettv->vval.v_number = VVAL_TRUE; |
| 3614 | *arg += 6; |
| 3615 | } |
| 3616 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3617 | { |
| 3618 | rettv->v_type = VAR_BOOL; |
| 3619 | rettv->vval.v_number = VVAL_FALSE; |
| 3620 | *arg += 7; |
| 3621 | } |
| 3622 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3623 | { |
| 3624 | rettv->v_type = VAR_SPECIAL; |
| 3625 | rettv->vval.v_number = VVAL_NULL; |
| 3626 | *arg += 6; |
| 3627 | } |
| 3628 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3629 | { |
| 3630 | rettv->v_type = VAR_SPECIAL; |
| 3631 | rettv->vval.v_number = VVAL_NONE; |
| 3632 | *arg += 6; |
| 3633 | } |
| 3634 | } |
| 3635 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3636 | static exptype_T |
| 3637 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3638 | { |
| 3639 | exptype_T type = EXPR_UNKNOWN; |
| 3640 | int i; |
| 3641 | |
| 3642 | switch (p[0]) |
| 3643 | { |
| 3644 | case '=': if (p[1] == '=') |
| 3645 | type = EXPR_EQUAL; |
| 3646 | else if (p[1] == '~') |
| 3647 | type = EXPR_MATCH; |
| 3648 | break; |
| 3649 | case '!': if (p[1] == '=') |
| 3650 | type = EXPR_NEQUAL; |
| 3651 | else if (p[1] == '~') |
| 3652 | type = EXPR_NOMATCH; |
| 3653 | break; |
| 3654 | case '>': if (p[1] != '=') |
| 3655 | { |
| 3656 | type = EXPR_GREATER; |
| 3657 | *len = 1; |
| 3658 | } |
| 3659 | else |
| 3660 | type = EXPR_GEQUAL; |
| 3661 | break; |
| 3662 | case '<': if (p[1] != '=') |
| 3663 | { |
| 3664 | type = EXPR_SMALLER; |
| 3665 | *len = 1; |
| 3666 | } |
| 3667 | else |
| 3668 | type = EXPR_SEQUAL; |
| 3669 | break; |
| 3670 | case 'i': if (p[1] == 's') |
| 3671 | { |
| 3672 | // "is" and "isnot"; but not a prefix of a name |
| 3673 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3674 | *len = 5; |
| 3675 | i = p[*len]; |
| 3676 | if (!isalnum(i) && i != '_') |
| 3677 | { |
| 3678 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3679 | *type_is = TRUE; |
| 3680 | } |
| 3681 | } |
| 3682 | break; |
| 3683 | } |
| 3684 | return type; |
| 3685 | } |
| 3686 | |
| 3687 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3688 | * Compile code to apply '-', '+' and '!'. |
| 3689 | */ |
| 3690 | static int |
| 3691 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3692 | { |
| 3693 | char_u *p = end; |
| 3694 | |
| 3695 | // this works from end to start |
| 3696 | while (p > start) |
| 3697 | { |
| 3698 | --p; |
| 3699 | if (*p == '-' || *p == '+') |
| 3700 | { |
| 3701 | int negate = *p == '-'; |
| 3702 | isn_T *isn; |
| 3703 | |
| 3704 | // TODO: check type |
| 3705 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3706 | { |
| 3707 | --p; |
| 3708 | if (*p == '-') |
| 3709 | negate = !negate; |
| 3710 | } |
| 3711 | // only '-' has an effect, for '+' we only check the type |
| 3712 | if (negate) |
| 3713 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3714 | else |
| 3715 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3716 | if (isn == NULL) |
| 3717 | return FAIL; |
| 3718 | } |
| 3719 | else |
| 3720 | { |
| 3721 | int invert = TRUE; |
| 3722 | |
| 3723 | while (p > start && p[-1] == '!') |
| 3724 | { |
| 3725 | --p; |
| 3726 | invert = !invert; |
| 3727 | } |
| 3728 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3729 | return FAIL; |
| 3730 | } |
| 3731 | } |
| 3732 | return OK; |
| 3733 | } |
| 3734 | |
| 3735 | /* |
| 3736 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3737 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3738 | */ |
| 3739 | static int |
| 3740 | compile_subscript( |
| 3741 | char_u **arg, |
| 3742 | cctx_T *cctx, |
| 3743 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3744 | char_u *end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3745 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3746 | { |
| 3747 | for (;;) |
| 3748 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3749 | char_u *p = skipwhite(*arg); |
| 3750 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3751 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3752 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3753 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3754 | |
| 3755 | // If a following line starts with "->{" or "->X" advance to that |
| 3756 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3757 | // Also if a following line starts with ".x". |
| 3758 | if (next != NULL && |
| 3759 | ((next[0] == '-' && next[1] == '>' |
| 3760 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3761 | || (next[0] == '.' && eval_isdictc(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3762 | { |
| 3763 | next = next_line_from_context(cctx, TRUE); |
| 3764 | if (next == NULL) |
| 3765 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3766 | *arg = next; |
| 3767 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3768 | } |
| 3769 | } |
| 3770 | |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 3771 | // Do not skip over white space to find the "(", "exeucte 'x' ()" is |
| 3772 | // not a function call. |
| 3773 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3774 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3775 | garray_T *stack = &cctx->ctx_type_stack; |
| 3776 | type_T *type; |
| 3777 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3778 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3779 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3780 | return FAIL; |
| 3781 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3782 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3783 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3784 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3785 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3786 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3787 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3788 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3789 | return FAIL; |
| 3790 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3791 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3792 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3793 | char_u *pstart = p; |
| 3794 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3795 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3796 | return FAIL; |
| 3797 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3798 | // something->method() |
| 3799 | // Apply the '!', '-' and '+' first: |
| 3800 | // -1.0->func() works like (-1.0)->func() |
| 3801 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3802 | return FAIL; |
| 3803 | *start_leader = end_leader; // don't apply again later |
| 3804 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3805 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3806 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3807 | // No line break supported right after "->". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3808 | if (**arg == '{') |
| 3809 | { |
| 3810 | // lambda call: list->{lambda} |
| 3811 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3812 | return FAIL; |
| 3813 | } |
| 3814 | else |
| 3815 | { |
| 3816 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3817 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3818 | if (!eval_isnamec1(*p)) |
| 3819 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3820 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3821 | return FAIL; |
| 3822 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3823 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3824 | p += 2; |
| 3825 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3826 | ; |
| 3827 | if (*p != '(') |
| 3828 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3829 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3830 | return FAIL; |
| 3831 | } |
| 3832 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3833 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3834 | return FAIL; |
| 3835 | } |
| 3836 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3837 | else if (*p == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3838 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3839 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3840 | type_T **typep; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3841 | vartype_T vtype; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3842 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3843 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3844 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3845 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3846 | // TODO: blob index |
| 3847 | // TODO: more arguments |
| 3848 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3849 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3850 | return FAIL; |
| 3851 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3852 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3853 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3854 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3855 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3856 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3857 | return FAIL; |
| 3858 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3859 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3860 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3861 | if (**arg != ']') |
| 3862 | { |
| 3863 | emsg(_(e_missbrac)); |
| 3864 | return FAIL; |
| 3865 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3866 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3867 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3868 | // We can index a list and a dict. If we don't know the type |
| 3869 | // we can use the index value type. |
| 3870 | // TODO: If we don't know use an instruction to figure it out at |
| 3871 | // runtime. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3872 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3873 | vtype = (*typep)->tt_type; |
| 3874 | if (*typep == &t_any) |
| 3875 | { |
| 3876 | type_T *valtype = ((type_T **)stack->ga_data) |
| 3877 | [stack->ga_len - 1]; |
| 3878 | if (valtype == &t_string) |
| 3879 | vtype = VAR_DICT; |
| 3880 | } |
| 3881 | if (vtype == VAR_DICT) |
| 3882 | { |
| 3883 | if ((*typep)->tt_type == VAR_DICT) |
| 3884 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3885 | else |
| 3886 | { |
| 3887 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3888 | return FAIL; |
| 3889 | *typep = &t_any; |
| 3890 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3891 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3892 | return FAIL; |
| 3893 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3894 | return FAIL; |
| 3895 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3896 | else if (vtype == VAR_STRING) |
| 3897 | { |
| 3898 | *typep = &t_number; |
| 3899 | if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL) |
| 3900 | return FAIL; |
| 3901 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3902 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3903 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3904 | if ((*typep)->tt_type == VAR_LIST) |
| 3905 | *typep = (*typep)->tt_member; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3906 | if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3907 | return FAIL; |
| 3908 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3909 | else |
| 3910 | { |
| 3911 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3912 | return FAIL; |
| 3913 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3914 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3915 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3916 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3917 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3918 | return FAIL; |
| 3919 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3920 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3921 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3922 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3923 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3924 | p = *arg; |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3925 | if (eval_isdictc(*p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3926 | while (eval_isnamec(*p)) |
| 3927 | MB_PTR_ADV(p); |
| 3928 | if (p == *arg) |
| 3929 | { |
| 3930 | semsg(_(e_syntax_at), *arg); |
| 3931 | return FAIL; |
| 3932 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3933 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3934 | return FAIL; |
| 3935 | *arg = p; |
| 3936 | } |
| 3937 | else |
| 3938 | break; |
| 3939 | } |
| 3940 | |
| 3941 | // TODO - see handle_subscript(): |
| 3942 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3943 | // Don't do this when "Func" is already a partial that was bound |
| 3944 | // explicitly (pt_auto is FALSE). |
| 3945 | |
| 3946 | return OK; |
| 3947 | } |
| 3948 | |
| 3949 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3950 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3951 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3952 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3953 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3954 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3955 | * |
| 3956 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3957 | */ |
| 3958 | |
| 3959 | /* |
| 3960 | * number number constant |
| 3961 | * 0zFFFFFFFF Blob constant |
| 3962 | * "string" string constant |
| 3963 | * 'string' literal string constant |
| 3964 | * &option-name option value |
| 3965 | * @r register contents |
| 3966 | * identifier variable value |
| 3967 | * function() function call |
| 3968 | * $VAR environment variable |
| 3969 | * (expression) nested expression |
| 3970 | * [expr, expr] List |
| 3971 | * {key: val, key: val} Dictionary |
| 3972 | * #{key: val, key: val} Dictionary with literal keys |
| 3973 | * |
| 3974 | * Also handle: |
| 3975 | * ! in front logical NOT |
| 3976 | * - in front unary minus |
| 3977 | * + in front unary plus (ignored) |
| 3978 | * trailing (arg) funcref/partial call |
| 3979 | * trailing [] subscript in String or List |
| 3980 | * trailing .name entry in Dictionary |
| 3981 | * trailing ->name() method call |
| 3982 | */ |
| 3983 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3984 | compile_expr7( |
| 3985 | char_u **arg, |
| 3986 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3987 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3988 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3989 | char_u *start_leader, *end_leader; |
| 3990 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3991 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3992 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3993 | |
| 3994 | /* |
| 3995 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3996 | */ |
| 3997 | start_leader = *arg; |
| 3998 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3999 | *arg = skipwhite(*arg + 1); |
| 4000 | end_leader = *arg; |
| 4001 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4002 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4003 | switch (**arg) |
| 4004 | { |
| 4005 | /* |
| 4006 | * Number constant. |
| 4007 | */ |
| 4008 | case '0': // also for blob starting with 0z |
| 4009 | case '1': |
| 4010 | case '2': |
| 4011 | case '3': |
| 4012 | case '4': |
| 4013 | case '5': |
| 4014 | case '6': |
| 4015 | case '7': |
| 4016 | case '8': |
| 4017 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4018 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4019 | return FAIL; |
| 4020 | break; |
| 4021 | |
| 4022 | /* |
| 4023 | * String constant: "string". |
| 4024 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4025 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4026 | return FAIL; |
| 4027 | break; |
| 4028 | |
| 4029 | /* |
| 4030 | * Literal string constant: 'str''ing'. |
| 4031 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4032 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4033 | return FAIL; |
| 4034 | break; |
| 4035 | |
| 4036 | /* |
| 4037 | * Constant Vim variable. |
| 4038 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4039 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4040 | ret = NOTDONE; |
| 4041 | break; |
| 4042 | |
| 4043 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4044 | * "true" constant |
| 4045 | */ |
| 4046 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 4047 | && !eval_isnamec((*arg)[4])) |
| 4048 | { |
| 4049 | *arg += 4; |
| 4050 | rettv->v_type = VAR_BOOL; |
| 4051 | rettv->vval.v_number = VVAL_TRUE; |
| 4052 | } |
| 4053 | else |
| 4054 | ret = NOTDONE; |
| 4055 | break; |
| 4056 | |
| 4057 | /* |
| 4058 | * "false" constant |
| 4059 | */ |
| 4060 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 4061 | && !eval_isnamec((*arg)[5])) |
| 4062 | { |
| 4063 | *arg += 5; |
| 4064 | rettv->v_type = VAR_BOOL; |
| 4065 | rettv->vval.v_number = VVAL_FALSE; |
| 4066 | } |
| 4067 | else |
| 4068 | ret = NOTDONE; |
| 4069 | break; |
| 4070 | |
| 4071 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4072 | * List: [expr, expr] |
| 4073 | */ |
| 4074 | case '[': ret = compile_list(arg, cctx); |
| 4075 | break; |
| 4076 | |
| 4077 | /* |
| 4078 | * Dictionary: #{key: val, key: val} |
| 4079 | */ |
| 4080 | case '#': if ((*arg)[1] == '{') |
| 4081 | { |
| 4082 | ++*arg; |
| 4083 | ret = compile_dict(arg, cctx, TRUE); |
| 4084 | } |
| 4085 | else |
| 4086 | ret = NOTDONE; |
| 4087 | break; |
| 4088 | |
| 4089 | /* |
| 4090 | * Lambda: {arg, arg -> expr} |
| 4091 | * Dictionary: {'key': val, 'key': val} |
| 4092 | */ |
| 4093 | case '{': { |
| 4094 | char_u *start = skipwhite(*arg + 1); |
| 4095 | |
| 4096 | // Find out what comes after the arguments. |
| 4097 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 4098 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4099 | if (ret != FAIL && *start == '>') |
| 4100 | ret = compile_lambda(arg, cctx); |
| 4101 | else |
| 4102 | ret = compile_dict(arg, cctx, FALSE); |
| 4103 | } |
| 4104 | break; |
| 4105 | |
| 4106 | /* |
| 4107 | * Option value: &name |
| 4108 | */ |
| 4109 | case '&': ret = compile_get_option(arg, cctx); |
| 4110 | break; |
| 4111 | |
| 4112 | /* |
| 4113 | * Environment variable: $VAR. |
| 4114 | */ |
| 4115 | case '$': ret = compile_get_env(arg, cctx); |
| 4116 | break; |
| 4117 | |
| 4118 | /* |
| 4119 | * Register contents: @r. |
| 4120 | */ |
| 4121 | case '@': ret = compile_get_register(arg, cctx); |
| 4122 | break; |
| 4123 | /* |
| 4124 | * nested expression: (expression). |
| 4125 | */ |
| 4126 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4127 | |
| 4128 | // recursive! |
| 4129 | if (ppconst->pp_used <= PPSIZE - 10) |
| 4130 | { |
| 4131 | ret = compile_expr1(arg, cctx, ppconst); |
| 4132 | } |
| 4133 | else |
| 4134 | { |
| 4135 | // Not enough space in ppconst, flush constants. |
| 4136 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4137 | return FAIL; |
| 4138 | ret = compile_expr0(arg, cctx); |
| 4139 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4140 | *arg = skipwhite(*arg); |
| 4141 | if (**arg == ')') |
| 4142 | ++*arg; |
| 4143 | else if (ret == OK) |
| 4144 | { |
| 4145 | emsg(_(e_missing_close)); |
| 4146 | ret = FAIL; |
| 4147 | } |
| 4148 | break; |
| 4149 | |
| 4150 | default: ret = NOTDONE; |
| 4151 | break; |
| 4152 | } |
| 4153 | if (ret == FAIL) |
| 4154 | return FAIL; |
| 4155 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4156 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4157 | { |
| 4158 | // apply the '!', '-' and '+' before the constant |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4159 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4160 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4161 | clear_tv(rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4162 | return FAIL; |
| 4163 | } |
| 4164 | start_leader = end_leader; // don't apply again below |
| 4165 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4166 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4167 | clear_tv(rettv); |
| 4168 | else |
| 4169 | // A constant expression can possibly be handled compile time, |
| 4170 | // return the value instead of generating code. |
| 4171 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4172 | } |
| 4173 | else if (ret == NOTDONE) |
| 4174 | { |
| 4175 | char_u *p; |
| 4176 | int r; |
| 4177 | |
| 4178 | if (!eval_isnamec1(**arg)) |
| 4179 | { |
| 4180 | semsg(_("E1015: Name expected: %s"), *arg); |
| 4181 | return FAIL; |
| 4182 | } |
| 4183 | |
| 4184 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4185 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4186 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4187 | { |
| 4188 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 4189 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4190 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4191 | { |
| 4192 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4193 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4194 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4195 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4196 | if (r == FAIL) |
| 4197 | return FAIL; |
| 4198 | } |
| 4199 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4200 | // Handle following "[]", ".member", etc. |
| 4201 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4202 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4203 | ppconst) == FAIL) |
| 4204 | return FAIL; |
| 4205 | if (ppconst->pp_used > 0) |
| 4206 | { |
| 4207 | // apply the '!', '-' and '+' before the constant |
| 4208 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4209 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
| 4210 | return FAIL; |
| 4211 | return OK; |
| 4212 | } |
| 4213 | if (compile_leader(cctx, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4214 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4215 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4216 | } |
| 4217 | |
| 4218 | /* |
| 4219 | * * number multiplication |
| 4220 | * / number division |
| 4221 | * % number modulo |
| 4222 | */ |
| 4223 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4224 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4225 | { |
| 4226 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4227 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4228 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4229 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4230 | // get the first expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4231 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4232 | return FAIL; |
| 4233 | |
| 4234 | /* |
| 4235 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4236 | */ |
| 4237 | for (;;) |
| 4238 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4239 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4240 | if (*op != '*' && *op != '/' && *op != '%') |
| 4241 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4242 | if (next != NULL) |
| 4243 | { |
| 4244 | *arg = next_line_from_context(cctx, TRUE); |
| 4245 | op = skipwhite(*arg); |
| 4246 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4247 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4248 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4249 | { |
| 4250 | char_u buf[3]; |
| 4251 | |
| 4252 | vim_strncpy(buf, op, 1); |
| 4253 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4254 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4255 | } |
| 4256 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4257 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4258 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4259 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4260 | // get the second expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4261 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4262 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4263 | |
| 4264 | if (ppconst->pp_used == ppconst_used + 2 |
| 4265 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4266 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4267 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4268 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4269 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4270 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4271 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4272 | // both are numbers: compute the result |
| 4273 | switch (*op) |
| 4274 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4275 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4276 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4277 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4278 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4279 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4280 | break; |
| 4281 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4282 | tv1->vval.v_number = res; |
| 4283 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4284 | } |
| 4285 | else |
| 4286 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4287 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4288 | generate_two_op(cctx, op); |
| 4289 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4290 | } |
| 4291 | |
| 4292 | return OK; |
| 4293 | } |
| 4294 | |
| 4295 | /* |
| 4296 | * + number addition |
| 4297 | * - number subtraction |
| 4298 | * .. string concatenation |
| 4299 | */ |
| 4300 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4301 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4302 | { |
| 4303 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4304 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4305 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4306 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4307 | |
| 4308 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4309 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4310 | return FAIL; |
| 4311 | |
| 4312 | /* |
| 4313 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4314 | */ |
| 4315 | for (;;) |
| 4316 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4317 | op = may_peek_next_line(cctx, *arg, &next); |
| 4318 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4319 | break; |
| 4320 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4321 | if (next != NULL) |
| 4322 | { |
| 4323 | *arg = next_line_from_context(cctx, TRUE); |
| 4324 | op = skipwhite(*arg); |
| 4325 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4326 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4327 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4328 | { |
| 4329 | char_u buf[3]; |
| 4330 | |
| 4331 | vim_strncpy(buf, op, oplen); |
| 4332 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4333 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4337 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4338 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4339 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4340 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4341 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4342 | return FAIL; |
| 4343 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4344 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4345 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4346 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4347 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4348 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4349 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4350 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4351 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4352 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4353 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4354 | // concat/subtract/add constant numbers |
| 4355 | if (*op == '+') |
| 4356 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4357 | else if (*op == '-') |
| 4358 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4359 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4360 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4361 | // concatenate constant strings |
| 4362 | char_u *s1 = tv1->vval.v_string; |
| 4363 | char_u *s2 = tv2->vval.v_string; |
| 4364 | size_t len1 = STRLEN(s1); |
| 4365 | |
| 4366 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4367 | if (tv1->vval.v_string == NULL) |
| 4368 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4369 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4370 | return FAIL; |
| 4371 | } |
| 4372 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4373 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4374 | vim_free(s1); |
| 4375 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4376 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4377 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4378 | } |
| 4379 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4380 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4381 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4382 | if (*op == '.') |
| 4383 | { |
| 4384 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4385 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4386 | return FAIL; |
| 4387 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4388 | } |
| 4389 | else |
| 4390 | generate_two_op(cctx, op); |
| 4391 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | return OK; |
| 4395 | } |
| 4396 | |
| 4397 | /* |
| 4398 | * expr5a == expr5b |
| 4399 | * expr5a =~ expr5b |
| 4400 | * expr5a != expr5b |
| 4401 | * expr5a !~ expr5b |
| 4402 | * expr5a > expr5b |
| 4403 | * expr5a >= expr5b |
| 4404 | * expr5a < expr5b |
| 4405 | * expr5a <= expr5b |
| 4406 | * expr5a is expr5b |
| 4407 | * expr5a isnot expr5b |
| 4408 | * |
| 4409 | * Produces instructions: |
| 4410 | * EVAL expr5a Push result of "expr5a" |
| 4411 | * EVAL expr5b Push result of "expr5b" |
| 4412 | * COMPARE one of the compare instructions |
| 4413 | */ |
| 4414 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4415 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4416 | { |
| 4417 | exptype_T type = EXPR_UNKNOWN; |
| 4418 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4419 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4420 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4421 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4422 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4423 | |
| 4424 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4425 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4426 | return FAIL; |
| 4427 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4428 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4429 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4430 | |
| 4431 | /* |
| 4432 | * If there is a comparative operator, use it. |
| 4433 | */ |
| 4434 | if (type != EXPR_UNKNOWN) |
| 4435 | { |
| 4436 | int ic = FALSE; // Default: do not ignore case |
| 4437 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4438 | if (next != NULL) |
| 4439 | { |
| 4440 | *arg = next_line_from_context(cctx, TRUE); |
| 4441 | p = skipwhite(*arg); |
| 4442 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4443 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4444 | { |
| 4445 | semsg(_(e_invexpr2), *arg); |
| 4446 | return FAIL; |
| 4447 | } |
| 4448 | // extra question mark appended: ignore case |
| 4449 | if (p[len] == '?') |
| 4450 | { |
| 4451 | ic = TRUE; |
| 4452 | ++len; |
| 4453 | } |
| 4454 | // extra '#' appended: match case (ignored) |
| 4455 | else if (p[len] == '#') |
| 4456 | ++len; |
| 4457 | // nothing appended: match case |
| 4458 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4459 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4460 | { |
| 4461 | char_u buf[7]; |
| 4462 | |
| 4463 | vim_strncpy(buf, p, len); |
| 4464 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4465 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4466 | } |
| 4467 | |
| 4468 | // get the second variable |
| 4469 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4470 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4471 | return FAIL; |
| 4472 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4473 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4474 | return FAIL; |
| 4475 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4476 | if (ppconst->pp_used == ppconst_used + 2) |
| 4477 | { |
| 4478 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4479 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4480 | int ret; |
| 4481 | |
| 4482 | // Both sides are a constant, compute the result now. |
| 4483 | // First check for a valid combination of types, this is more |
| 4484 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4485 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4486 | ret = FAIL; |
| 4487 | else |
| 4488 | { |
| 4489 | ret = typval_compare(tv1, tv2, type, ic); |
| 4490 | tv1->v_type = VAR_BOOL; |
| 4491 | tv1->vval.v_number = tv1->vval.v_number |
| 4492 | ? VVAL_TRUE : VVAL_FALSE; |
| 4493 | clear_tv(tv2); |
| 4494 | --ppconst->pp_used; |
| 4495 | } |
| 4496 | return ret; |
| 4497 | } |
| 4498 | |
| 4499 | generate_ppconst(cctx, ppconst); |
| 4500 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | return OK; |
| 4504 | } |
| 4505 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4506 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4507 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4508 | /* |
| 4509 | * Compile || or &&. |
| 4510 | */ |
| 4511 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4512 | compile_and_or( |
| 4513 | char_u **arg, |
| 4514 | cctx_T *cctx, |
| 4515 | char *op, |
| 4516 | ppconst_T *ppconst, |
| 4517 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4518 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4519 | char_u *next; |
| 4520 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4521 | int opchar = *op; |
| 4522 | |
| 4523 | if (p[0] == opchar && p[1] == opchar) |
| 4524 | { |
| 4525 | garray_T *instr = &cctx->ctx_instr; |
| 4526 | garray_T end_ga; |
| 4527 | |
| 4528 | /* |
| 4529 | * Repeat until there is no following "||" or "&&" |
| 4530 | */ |
| 4531 | ga_init2(&end_ga, sizeof(int), 10); |
| 4532 | while (p[0] == opchar && p[1] == opchar) |
| 4533 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4534 | if (next != NULL) |
| 4535 | { |
| 4536 | *arg = next_line_from_context(cctx, TRUE); |
| 4537 | p = skipwhite(*arg); |
| 4538 | } |
| 4539 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4540 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4541 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4542 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4543 | return FAIL; |
| 4544 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4545 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4546 | // TODO: use ppconst if the value is a constant |
| 4547 | generate_ppconst(cctx, ppconst); |
| 4548 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4549 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4550 | { |
| 4551 | ga_clear(&end_ga); |
| 4552 | return FAIL; |
| 4553 | } |
| 4554 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4555 | ++end_ga.ga_len; |
| 4556 | generate_JUMP(cctx, opchar == '|' |
| 4557 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 4558 | |
| 4559 | // eval the next expression |
| 4560 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4561 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4562 | return FAIL; |
| 4563 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4564 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4565 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4566 | { |
| 4567 | ga_clear(&end_ga); |
| 4568 | return FAIL; |
| 4569 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4570 | |
| 4571 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4572 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4573 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4574 | |
| 4575 | // Fill in the end label in all jumps. |
| 4576 | while (end_ga.ga_len > 0) |
| 4577 | { |
| 4578 | isn_T *isn; |
| 4579 | |
| 4580 | --end_ga.ga_len; |
| 4581 | isn = ((isn_T *)instr->ga_data) |
| 4582 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4583 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4584 | } |
| 4585 | ga_clear(&end_ga); |
| 4586 | } |
| 4587 | |
| 4588 | return OK; |
| 4589 | } |
| 4590 | |
| 4591 | /* |
| 4592 | * expr4a && expr4a && expr4a logical AND |
| 4593 | * |
| 4594 | * Produces instructions: |
| 4595 | * EVAL expr4a Push result of "expr4a" |
| 4596 | * JUMP_AND_KEEP_IF_FALSE end |
| 4597 | * EVAL expr4b Push result of "expr4b" |
| 4598 | * JUMP_AND_KEEP_IF_FALSE end |
| 4599 | * EVAL expr4c Push result of "expr4c" |
| 4600 | * end: |
| 4601 | */ |
| 4602 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4603 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4604 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4605 | int ppconst_used = ppconst->pp_used; |
| 4606 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4607 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4608 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4609 | return FAIL; |
| 4610 | |
| 4611 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4612 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | /* |
| 4616 | * expr3a || expr3b || expr3c logical OR |
| 4617 | * |
| 4618 | * Produces instructions: |
| 4619 | * EVAL expr3a Push result of "expr3a" |
| 4620 | * JUMP_AND_KEEP_IF_TRUE end |
| 4621 | * EVAL expr3b Push result of "expr3b" |
| 4622 | * JUMP_AND_KEEP_IF_TRUE end |
| 4623 | * EVAL expr3c Push result of "expr3c" |
| 4624 | * end: |
| 4625 | */ |
| 4626 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4627 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4628 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4629 | int ppconst_used = ppconst->pp_used; |
| 4630 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4631 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4632 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4633 | return FAIL; |
| 4634 | |
| 4635 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4636 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | /* |
| 4640 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4641 | * |
| 4642 | * Produces instructions: |
| 4643 | * EVAL expr2 Push result of "expr" |
| 4644 | * JUMP_IF_FALSE alt jump if false |
| 4645 | * EVAL expr1a |
| 4646 | * JUMP_ALWAYS end |
| 4647 | * alt: EVAL expr1b |
| 4648 | * end: |
| 4649 | */ |
| 4650 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4651 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4652 | { |
| 4653 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4654 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4655 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4656 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4657 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4658 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4659 | return FAIL; |
| 4660 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4661 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4662 | if (*p == '?') |
| 4663 | { |
| 4664 | garray_T *instr = &cctx->ctx_instr; |
| 4665 | garray_T *stack = &cctx->ctx_type_stack; |
| 4666 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4667 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4668 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4669 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4670 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4671 | int has_const_expr = FALSE; |
| 4672 | int const_value = FALSE; |
| 4673 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4674 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4675 | if (next != NULL) |
| 4676 | { |
| 4677 | *arg = next_line_from_context(cctx, TRUE); |
| 4678 | p = skipwhite(*arg); |
| 4679 | } |
| 4680 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4681 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4682 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4683 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4684 | return FAIL; |
| 4685 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4686 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4687 | if (ppconst->pp_used == ppconst_used + 1) |
| 4688 | { |
| 4689 | // the condition is a constant, we know whether the ? or the : |
| 4690 | // expression is to be evaluated. |
| 4691 | has_const_expr = TRUE; |
| 4692 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4693 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4694 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4695 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4696 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4697 | } |
| 4698 | else |
| 4699 | { |
| 4700 | generate_ppconst(cctx, ppconst); |
| 4701 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4702 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4703 | |
| 4704 | // evaluate the second expression; any type is accepted |
| 4705 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4706 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4707 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4708 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4709 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4710 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4711 | if (!has_const_expr) |
| 4712 | { |
| 4713 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4714 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4715 | // remember the type and drop it |
| 4716 | --stack->ga_len; |
| 4717 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4718 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4719 | end_idx = instr->ga_len; |
| 4720 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4721 | |
| 4722 | // jump here from JUMP_IF_FALSE |
| 4723 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4724 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4725 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4726 | |
| 4727 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4728 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4729 | if (*p != ':') |
| 4730 | { |
| 4731 | emsg(_(e_missing_colon)); |
| 4732 | return FAIL; |
| 4733 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4734 | if (next != NULL) |
| 4735 | { |
| 4736 | *arg = next_line_from_context(cctx, TRUE); |
| 4737 | p = skipwhite(*arg); |
| 4738 | } |
| 4739 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4740 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4741 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4742 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4743 | return FAIL; |
| 4744 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4745 | |
| 4746 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4747 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4748 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4749 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4750 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4751 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4752 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4753 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4754 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4755 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4756 | if (!has_const_expr) |
| 4757 | { |
| 4758 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4759 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4760 | // If the types differ, the result has a more generic type. |
| 4761 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4762 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4763 | |
| 4764 | // jump here from JUMP_ALWAYS |
| 4765 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4766 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4767 | } |
| 4768 | |
| 4769 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4770 | } |
| 4771 | return OK; |
| 4772 | } |
| 4773 | |
| 4774 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4775 | * Toplevel expression. |
| 4776 | */ |
| 4777 | static int |
| 4778 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4779 | { |
| 4780 | ppconst_T ppconst; |
| 4781 | |
| 4782 | CLEAR_FIELD(ppconst); |
| 4783 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4784 | { |
| 4785 | clear_ppconst(&ppconst); |
| 4786 | return FAIL; |
| 4787 | } |
| 4788 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4789 | return FAIL; |
| 4790 | return OK; |
| 4791 | } |
| 4792 | |
| 4793 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4794 | * compile "return [expr]" |
| 4795 | */ |
| 4796 | static char_u * |
| 4797 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4798 | { |
| 4799 | char_u *p = arg; |
| 4800 | garray_T *stack = &cctx->ctx_type_stack; |
| 4801 | type_T *stack_type; |
| 4802 | |
| 4803 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4804 | { |
| 4805 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4806 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4807 | return NULL; |
| 4808 | |
| 4809 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4810 | if (set_return_type) |
| 4811 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4812 | else |
| 4813 | { |
| 4814 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4815 | && stack_type->tt_type != VAR_VOID |
| 4816 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4817 | { |
| 4818 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4819 | return NULL; |
| 4820 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4821 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4822 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4823 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4824 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4825 | } |
| 4826 | else |
| 4827 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4828 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4829 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4830 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4831 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4832 | { |
| 4833 | emsg(_("E1003: Missing return value")); |
| 4834 | return NULL; |
| 4835 | } |
| 4836 | |
| 4837 | // No argument, return zero. |
| 4838 | generate_PUSHNR(cctx, 0); |
| 4839 | } |
| 4840 | |
| 4841 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4842 | return NULL; |
| 4843 | |
| 4844 | // "return val | endif" is possible |
| 4845 | return skipwhite(p); |
| 4846 | } |
| 4847 | |
| 4848 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4849 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4850 | * Return a pointer to the line in allocated memory. |
| 4851 | * Return NULL for end-of-file or some error. |
| 4852 | */ |
| 4853 | static char_u * |
| 4854 | exarg_getline( |
| 4855 | int c UNUSED, |
| 4856 | void *cookie, |
| 4857 | int indent UNUSED, |
| 4858 | int do_concat UNUSED) |
| 4859 | { |
| 4860 | cctx_T *cctx = (cctx_T *)cookie; |
| 4861 | |
| 4862 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4863 | { |
| 4864 | iemsg("Heredoc got to end"); |
| 4865 | return NULL; |
| 4866 | } |
| 4867 | ++cctx->ctx_lnum; |
| 4868 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4869 | [cctx->ctx_lnum]); |
| 4870 | } |
| 4871 | |
| 4872 | /* |
| 4873 | * Compile a nested :def command. |
| 4874 | */ |
| 4875 | static char_u * |
| 4876 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4877 | { |
| 4878 | char_u *name_start = eap->arg; |
| 4879 | char_u *name_end = to_name_end(eap->arg, FALSE); |
| 4880 | char_u *name = get_lambda_name(); |
| 4881 | lvar_T *lvar; |
| 4882 | ufunc_T *ufunc; |
| 4883 | |
| 4884 | eap->arg = name_end; |
| 4885 | eap->getline = exarg_getline; |
| 4886 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4887 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4888 | eap->forceit = FALSE; |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4889 | ufunc = def_function(eap, name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4890 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4891 | if (ufunc == NULL) |
| 4892 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4893 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4894 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4895 | return NULL; |
| 4896 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4897 | // Define a local variable for the function reference. |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4898 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4899 | TRUE, ufunc->uf_func_type); |
| 4900 | |
| 4901 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL |
| 4902 | || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL) |
| 4903 | return NULL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4904 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4905 | // TODO: warning for trailing text? |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4906 | return (char_u *)""; |
| 4907 | } |
| 4908 | |
| 4909 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4910 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4911 | */ |
| 4912 | int |
| 4913 | assignment_len(char_u *p, int *heredoc) |
| 4914 | { |
| 4915 | if (*p == '=') |
| 4916 | { |
| 4917 | if (p[1] == '<' && p[2] == '<') |
| 4918 | { |
| 4919 | *heredoc = TRUE; |
| 4920 | return 3; |
| 4921 | } |
| 4922 | return 1; |
| 4923 | } |
| 4924 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4925 | return 2; |
| 4926 | if (STRNCMP(p, "..=", 3) == 0) |
| 4927 | return 3; |
| 4928 | return 0; |
| 4929 | } |
| 4930 | |
| 4931 | // words that cannot be used as a variable |
| 4932 | static char *reserved[] = { |
| 4933 | "true", |
| 4934 | "false", |
| 4935 | NULL |
| 4936 | }; |
| 4937 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4938 | typedef enum { |
| 4939 | dest_local, |
| 4940 | dest_option, |
| 4941 | dest_env, |
| 4942 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4943 | dest_buffer, |
| 4944 | dest_window, |
| 4945 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4946 | dest_vimvar, |
| 4947 | dest_script, |
| 4948 | dest_reg, |
| 4949 | } assign_dest_T; |
| 4950 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4951 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4952 | * Generate the load instruction for "name". |
| 4953 | */ |
| 4954 | static void |
| 4955 | generate_loadvar( |
| 4956 | cctx_T *cctx, |
| 4957 | assign_dest_T dest, |
| 4958 | char_u *name, |
| 4959 | lvar_T *lvar, |
| 4960 | type_T *type) |
| 4961 | { |
| 4962 | switch (dest) |
| 4963 | { |
| 4964 | case dest_option: |
| 4965 | // TODO: check the option exists |
| 4966 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4967 | break; |
| 4968 | case dest_global: |
| 4969 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4970 | break; |
| 4971 | case dest_buffer: |
| 4972 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4973 | break; |
| 4974 | case dest_window: |
| 4975 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4976 | break; |
| 4977 | case dest_tab: |
| 4978 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4979 | break; |
| 4980 | case dest_script: |
| 4981 | compile_load_scriptvar(cctx, |
| 4982 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4983 | break; |
| 4984 | case dest_env: |
| 4985 | // Include $ in the name here |
| 4986 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4987 | break; |
| 4988 | case dest_reg: |
| 4989 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4990 | break; |
| 4991 | case dest_vimvar: |
| 4992 | generate_LOADV(cctx, name + 2, TRUE); |
| 4993 | break; |
| 4994 | case dest_local: |
| 4995 | if (lvar->lv_from_outer) |
| 4996 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4997 | NULL, type); |
| 4998 | else |
| 4999 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 5000 | break; |
| 5001 | } |
| 5002 | } |
| 5003 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5004 | void |
| 5005 | vim9_declare_error(char_u *name) |
| 5006 | { |
| 5007 | char *scope = ""; |
| 5008 | |
| 5009 | switch (*name) |
| 5010 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 5011 | case 'g': scope = _("global"); break; |
| 5012 | case 'b': scope = _("buffer"); break; |
| 5013 | case 'w': scope = _("window"); break; |
| 5014 | case 't': scope = _("tab"); break; |
| 5015 | case 'v': scope = "v:"; break; |
| 5016 | case '$': semsg(_(e_declare_env_var), name); return; |
| 5017 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5018 | } |
| 5019 | semsg(_(e_declare_var), scope, name); |
| 5020 | } |
| 5021 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5022 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5023 | * Compile declaration and assignment: |
| 5024 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5025 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5026 | * Return NULL for an error. |
| 5027 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5028 | */ |
| 5029 | static char_u * |
| 5030 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 5031 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5032 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5033 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5034 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5035 | char_u *ret = NULL; |
| 5036 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5037 | int var_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5038 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5039 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5040 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5041 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5042 | int oplen = 0; |
| 5043 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 5044 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5045 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5046 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5047 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5048 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5049 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5050 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 5051 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 5052 | if (p == NULL) |
| 5053 | return *arg == '[' ? arg : NULL; |
| 5054 | |
| 5055 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5056 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 5057 | // TODO: should we allow this, and figure out type inference from list |
| 5058 | // members? |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5059 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5060 | return NULL; |
| 5061 | } |
| 5062 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5063 | sp = p; |
| 5064 | p = skipwhite(p); |
| 5065 | op = p; |
| 5066 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5067 | |
| 5068 | if (var_count > 0 && oplen == 0) |
| 5069 | // can be something like "[1, 2]->func()" |
| 5070 | return arg; |
| 5071 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5072 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 5073 | { |
| 5074 | char_u buf[4]; |
| 5075 | |
| 5076 | vim_strncpy(buf, op, oplen); |
| 5077 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5078 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 5079 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5080 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5081 | if (heredoc) |
| 5082 | { |
| 5083 | list_T *l; |
| 5084 | listitem_T *li; |
| 5085 | |
| 5086 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5087 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5088 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 5089 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5090 | |
| 5091 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 5092 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5093 | { |
| 5094 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 5095 | li->li_tv.vval.v_string = NULL; |
| 5096 | } |
| 5097 | generate_NEWLIST(cctx, l->lv_len); |
| 5098 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5099 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5100 | list_free(l); |
| 5101 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5102 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5103 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5104 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5105 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5106 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 5107 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5108 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5109 | p = skipwhite(op + oplen); |
| 5110 | if (compile_expr0(&p, cctx) == FAIL) |
| 5111 | return NULL; |
| 5112 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5113 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5114 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5115 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5116 | type_T *stacktype; |
| 5117 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5118 | stacktype = stack->ga_len == 0 ? &t_void |
| 5119 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5120 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5121 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5122 | emsg(_(e_cannot_use_void)); |
| 5123 | goto theend; |
| 5124 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5125 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5126 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5127 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5128 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 5129 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5130 | } |
| 5131 | } |
| 5132 | |
| 5133 | /* |
| 5134 | * Loop over variables in "[var, var] = expr". |
| 5135 | * For "var = expr" and "let var: type" this is done only once. |
| 5136 | */ |
| 5137 | if (var_count > 0) |
| 5138 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 5139 | else |
| 5140 | var_start = arg; |
| 5141 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 5142 | { |
| 5143 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 5144 | size_t varlen; |
| 5145 | int new_local = FALSE; |
| 5146 | int opt_type; |
| 5147 | int opt_flags = 0; |
| 5148 | assign_dest_T dest = dest_local; |
| 5149 | int vimvaridx = -1; |
| 5150 | lvar_T *lvar = NULL; |
| 5151 | lvar_T arg_lvar; |
| 5152 | int has_type = FALSE; |
| 5153 | int has_index = FALSE; |
| 5154 | int instr_count = -1; |
| 5155 | |
| 5156 | p = (*var_start == '&' || *var_start == '$' |
| 5157 | || *var_start == '@') ? var_start + 1 : var_start; |
| 5158 | p = to_name_end(p, TRUE); |
| 5159 | |
| 5160 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 5161 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 5162 | --var_end; |
| 5163 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 5164 | --p; |
| 5165 | |
| 5166 | varlen = p - var_start; |
| 5167 | vim_free(name); |
| 5168 | name = vim_strnsave(var_start, varlen); |
| 5169 | if (name == NULL) |
| 5170 | return NULL; |
| 5171 | if (!heredoc) |
| 5172 | type = &t_any; |
| 5173 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5174 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5175 | { |
| 5176 | if (*var_start == '&') |
| 5177 | { |
| 5178 | int cc; |
| 5179 | long numval; |
| 5180 | |
| 5181 | dest = dest_option; |
| 5182 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5183 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5184 | emsg(_(e_const_option)); |
| 5185 | goto theend; |
| 5186 | } |
| 5187 | if (is_decl) |
| 5188 | { |
| 5189 | semsg(_("E1052: Cannot declare an option: %s"), var_start); |
| 5190 | goto theend; |
| 5191 | } |
| 5192 | p = var_start; |
| 5193 | p = find_option_end(&p, &opt_flags); |
| 5194 | if (p == NULL) |
| 5195 | { |
| 5196 | // cannot happen? |
| 5197 | emsg(_(e_letunexp)); |
| 5198 | goto theend; |
| 5199 | } |
| 5200 | cc = *p; |
| 5201 | *p = NUL; |
| 5202 | opt_type = get_option_value(var_start + 1, &numval, |
| 5203 | NULL, opt_flags); |
| 5204 | *p = cc; |
| 5205 | if (opt_type == -3) |
| 5206 | { |
| 5207 | semsg(_(e_unknown_option), var_start); |
| 5208 | goto theend; |
| 5209 | } |
| 5210 | if (opt_type == -2 || opt_type == 0) |
| 5211 | type = &t_string; |
| 5212 | else |
| 5213 | type = &t_number; // both number and boolean option |
| 5214 | } |
| 5215 | else if (*var_start == '$') |
| 5216 | { |
| 5217 | dest = dest_env; |
| 5218 | type = &t_string; |
| 5219 | if (is_decl) |
| 5220 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5221 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5222 | goto theend; |
| 5223 | } |
| 5224 | } |
| 5225 | else if (*var_start == '@') |
| 5226 | { |
| 5227 | if (!valid_yank_reg(var_start[1], TRUE)) |
| 5228 | { |
| 5229 | emsg_invreg(var_start[1]); |
| 5230 | goto theend; |
| 5231 | } |
| 5232 | dest = dest_reg; |
| 5233 | type = &t_string; |
| 5234 | if (is_decl) |
| 5235 | { |
| 5236 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 5237 | goto theend; |
| 5238 | } |
| 5239 | } |
| 5240 | else if (STRNCMP(var_start, "g:", 2) == 0) |
| 5241 | { |
| 5242 | dest = dest_global; |
| 5243 | if (is_decl) |
| 5244 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5245 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5246 | goto theend; |
| 5247 | } |
| 5248 | } |
| 5249 | else if (STRNCMP(var_start, "b:", 2) == 0) |
| 5250 | { |
| 5251 | dest = dest_buffer; |
| 5252 | if (is_decl) |
| 5253 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5254 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5255 | goto theend; |
| 5256 | } |
| 5257 | } |
| 5258 | else if (STRNCMP(var_start, "w:", 2) == 0) |
| 5259 | { |
| 5260 | dest = dest_window; |
| 5261 | if (is_decl) |
| 5262 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5263 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5264 | goto theend; |
| 5265 | } |
| 5266 | } |
| 5267 | else if (STRNCMP(var_start, "t:", 2) == 0) |
| 5268 | { |
| 5269 | dest = dest_tab; |
| 5270 | if (is_decl) |
| 5271 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5272 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5273 | goto theend; |
| 5274 | } |
| 5275 | } |
| 5276 | else if (STRNCMP(var_start, "v:", 2) == 0) |
| 5277 | { |
| 5278 | typval_T *vtv; |
| 5279 | int di_flags; |
| 5280 | |
| 5281 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5282 | if (vimvaridx < 0) |
| 5283 | { |
| 5284 | semsg(_(e_var_notfound), var_start); |
| 5285 | goto theend; |
| 5286 | } |
| 5287 | // We use the current value of "sandbox" here, is that OK? |
| 5288 | if (var_check_ro(di_flags, name, FALSE)) |
| 5289 | goto theend; |
| 5290 | dest = dest_vimvar; |
| 5291 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 5292 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5293 | if (is_decl) |
| 5294 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5295 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5296 | goto theend; |
| 5297 | } |
| 5298 | } |
| 5299 | else |
| 5300 | { |
| 5301 | int idx; |
| 5302 | |
| 5303 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5304 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5305 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5306 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5307 | goto theend; |
| 5308 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5309 | |
| 5310 | lvar = lookup_local(var_start, varlen, cctx); |
| 5311 | if (lvar == NULL) |
| 5312 | { |
| 5313 | CLEAR_FIELD(arg_lvar); |
| 5314 | if (lookup_arg(var_start, varlen, |
| 5315 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 5316 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5317 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5318 | if (is_decl) |
| 5319 | { |
| 5320 | semsg(_(e_used_as_arg), name); |
| 5321 | goto theend; |
| 5322 | } |
| 5323 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5324 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5325 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5326 | if (lvar != NULL) |
| 5327 | { |
| 5328 | if (is_decl) |
| 5329 | { |
| 5330 | semsg(_("E1017: Variable already declared: %s"), name); |
| 5331 | goto theend; |
| 5332 | } |
| 5333 | else if (lvar->lv_const) |
| 5334 | { |
| 5335 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 5336 | name); |
| 5337 | goto theend; |
| 5338 | } |
| 5339 | } |
| 5340 | else if (STRNCMP(var_start, "s:", 2) == 0 |
| 5341 | || lookup_script(var_start, varlen) == OK |
| 5342 | || find_imported(var_start, varlen, cctx) != NULL) |
| 5343 | { |
| 5344 | dest = dest_script; |
| 5345 | if (is_decl) |
| 5346 | { |
| 5347 | semsg(_("E1054: Variable already declared in the script: %s"), |
| 5348 | name); |
| 5349 | goto theend; |
| 5350 | } |
| 5351 | } |
| 5352 | else if (name[1] == ':' && name[2] != NUL) |
| 5353 | { |
| 5354 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
| 5355 | name); |
| 5356 | goto theend; |
| 5357 | } |
| 5358 | else if (!is_decl) |
| 5359 | { |
| 5360 | semsg(_("E1089: unknown variable: %s"), name); |
| 5361 | goto theend; |
| 5362 | } |
| 5363 | } |
| 5364 | } |
| 5365 | |
| 5366 | // handle "a:name" as a name, not index "name" on "a" |
| 5367 | if (varlen > 1 || var_start[varlen] != ':') |
| 5368 | p = var_end; |
| 5369 | |
| 5370 | if (dest != dest_option) |
| 5371 | { |
| 5372 | if (is_decl && *p == ':') |
| 5373 | { |
| 5374 | // parse optional type: "let var: type = expr" |
| 5375 | if (!VIM_ISWHITE(p[1])) |
| 5376 | { |
| 5377 | semsg(_(e_white_after), ":"); |
| 5378 | goto theend; |
| 5379 | } |
| 5380 | p = skipwhite(p + 1); |
| 5381 | type = parse_type(&p, cctx->ctx_type_list); |
| 5382 | has_type = TRUE; |
| 5383 | } |
| 5384 | else if (lvar != NULL) |
| 5385 | type = lvar->lv_type; |
| 5386 | } |
| 5387 | |
| 5388 | if (oplen == 3 && !heredoc && dest != dest_global |
| 5389 | && type->tt_type != VAR_STRING |
| 5390 | && type->tt_type != VAR_ANY) |
| 5391 | { |
| 5392 | emsg(_("E1019: Can only concatenate to string")); |
| 5393 | goto theend; |
| 5394 | } |
| 5395 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5396 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5397 | { |
| 5398 | if (oplen > 1 && !heredoc) |
| 5399 | { |
| 5400 | // +=, /=, etc. require an existing variable |
| 5401 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 5402 | name); |
| 5403 | goto theend; |
| 5404 | } |
| 5405 | |
| 5406 | // new local variable |
| 5407 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
| 5408 | goto theend; |
| 5409 | lvar = reserve_local(cctx, var_start, varlen, |
| 5410 | cmdidx == CMD_const, type); |
| 5411 | if (lvar == NULL) |
| 5412 | goto theend; |
| 5413 | new_local = TRUE; |
| 5414 | } |
| 5415 | |
| 5416 | member_type = type; |
| 5417 | if (var_end > var_start + varlen) |
| 5418 | { |
| 5419 | // Something follows after the variable: "var[idx]". |
| 5420 | if (is_decl) |
| 5421 | { |
| 5422 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 5423 | goto theend; |
| 5424 | } |
| 5425 | |
| 5426 | if (var_start[varlen] == '[') |
| 5427 | { |
| 5428 | has_index = TRUE; |
| 5429 | if (type->tt_member == NULL) |
| 5430 | { |
| 5431 | semsg(_("E1088: cannot use an index on %s"), name); |
| 5432 | goto theend; |
| 5433 | } |
| 5434 | member_type = type->tt_member; |
| 5435 | } |
| 5436 | else |
| 5437 | { |
| 5438 | semsg("Not supported yet: %s", var_start); |
| 5439 | goto theend; |
| 5440 | } |
| 5441 | } |
| 5442 | else if (lvar == &arg_lvar) |
| 5443 | { |
| 5444 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 5445 | goto theend; |
| 5446 | } |
| 5447 | |
| 5448 | if (!heredoc) |
| 5449 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5450 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5451 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5452 | if (oplen > 0 && var_count == 0) |
| 5453 | { |
| 5454 | // skip over the "=" and the expression |
| 5455 | p = skipwhite(op + oplen); |
| 5456 | compile_expr0(&p, cctx); |
| 5457 | } |
| 5458 | } |
| 5459 | else if (oplen > 0) |
| 5460 | { |
| 5461 | type_T *stacktype; |
| 5462 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5463 | // For "var = expr" evaluate the expression. |
| 5464 | if (var_count == 0) |
| 5465 | { |
| 5466 | int r; |
| 5467 | |
| 5468 | // for "+=", "*=", "..=" etc. first load the current value |
| 5469 | if (*op != '=') |
| 5470 | { |
| 5471 | generate_loadvar(cctx, dest, name, lvar, type); |
| 5472 | |
| 5473 | if (has_index) |
| 5474 | { |
| 5475 | // TODO: get member from list or dict |
| 5476 | emsg("Index with operation not supported yet"); |
| 5477 | goto theend; |
| 5478 | } |
| 5479 | } |
| 5480 | |
| 5481 | // Compile the expression. Temporarily hide the new local |
| 5482 | // variable here, it is not available to this expression. |
| 5483 | if (new_local) |
| 5484 | --cctx->ctx_locals.ga_len; |
| 5485 | instr_count = instr->ga_len; |
| 5486 | p = skipwhite(op + oplen); |
| 5487 | r = compile_expr0(&p, cctx); |
| 5488 | if (new_local) |
| 5489 | ++cctx->ctx_locals.ga_len; |
| 5490 | if (r == FAIL) |
| 5491 | goto theend; |
| 5492 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5493 | else if (semicolon && var_idx == var_count - 1) |
| 5494 | { |
| 5495 | // For "[var; var] = expr" get the rest of the list |
| 5496 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5497 | goto theend; |
| 5498 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5499 | else |
| 5500 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5501 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5502 | // list. |
| 5503 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 5504 | return FAIL; |
| 5505 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5506 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5507 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5508 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5509 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5510 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5511 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5512 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5513 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5514 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5515 | emsg(_(e_cannot_use_void)); |
| 5516 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5517 | } |
| 5518 | else |
| 5519 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5520 | // An empty list or dict has a &t_void member, |
| 5521 | // for a variable that implies &t_any. |
| 5522 | if (stacktype == &t_list_empty) |
| 5523 | lvar->lv_type = &t_list_any; |
| 5524 | else if (stacktype == &t_dict_empty) |
| 5525 | lvar->lv_type = &t_dict_any; |
| 5526 | else |
| 5527 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5528 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5529 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5530 | else |
| 5531 | { |
| 5532 | type_T *use_type = lvar->lv_type; |
| 5533 | |
| 5534 | if (has_index) |
| 5535 | { |
| 5536 | use_type = use_type->tt_member; |
| 5537 | if (use_type == NULL) |
| 5538 | use_type = &t_void; |
| 5539 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5540 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5541 | == FAIL) |
| 5542 | goto theend; |
| 5543 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5544 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5545 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5546 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5547 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5548 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5549 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5550 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5551 | emsg(_(e_const_req_value)); |
| 5552 | goto theend; |
| 5553 | } |
| 5554 | else if (!has_type || dest == dest_option) |
| 5555 | { |
| 5556 | emsg(_(e_type_req)); |
| 5557 | goto theend; |
| 5558 | } |
| 5559 | else |
| 5560 | { |
| 5561 | // variables are always initialized |
| 5562 | if (ga_grow(instr, 1) == FAIL) |
| 5563 | goto theend; |
| 5564 | switch (member_type->tt_type) |
| 5565 | { |
| 5566 | case VAR_BOOL: |
| 5567 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5568 | break; |
| 5569 | case VAR_FLOAT: |
| 5570 | #ifdef FEAT_FLOAT |
| 5571 | generate_PUSHF(cctx, 0.0); |
| 5572 | #endif |
| 5573 | break; |
| 5574 | case VAR_STRING: |
| 5575 | generate_PUSHS(cctx, NULL); |
| 5576 | break; |
| 5577 | case VAR_BLOB: |
| 5578 | generate_PUSHBLOB(cctx, NULL); |
| 5579 | break; |
| 5580 | case VAR_FUNC: |
| 5581 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5582 | break; |
| 5583 | case VAR_LIST: |
| 5584 | generate_NEWLIST(cctx, 0); |
| 5585 | break; |
| 5586 | case VAR_DICT: |
| 5587 | generate_NEWDICT(cctx, 0); |
| 5588 | break; |
| 5589 | case VAR_JOB: |
| 5590 | generate_PUSHJOB(cctx, NULL); |
| 5591 | break; |
| 5592 | case VAR_CHANNEL: |
| 5593 | generate_PUSHCHANNEL(cctx, NULL); |
| 5594 | break; |
| 5595 | case VAR_NUMBER: |
| 5596 | case VAR_UNKNOWN: |
| 5597 | case VAR_ANY: |
| 5598 | case VAR_PARTIAL: |
| 5599 | case VAR_VOID: |
| 5600 | case VAR_SPECIAL: // cannot happen |
| 5601 | generate_PUSHNR(cctx, 0); |
| 5602 | break; |
| 5603 | } |
| 5604 | } |
| 5605 | if (var_count == 0) |
| 5606 | end = p; |
| 5607 | } |
| 5608 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5609 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5610 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5611 | break; |
| 5612 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5613 | if (oplen > 0 && *op != '=') |
| 5614 | { |
| 5615 | type_T *expected = &t_number; |
| 5616 | type_T *stacktype; |
| 5617 | |
| 5618 | // TODO: if type is known use float or any operation |
| 5619 | |
| 5620 | if (*op == '.') |
| 5621 | expected = &t_string; |
| 5622 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5623 | if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5624 | goto theend; |
| 5625 | |
| 5626 | if (*op == '.') |
| 5627 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 5628 | else |
| 5629 | { |
| 5630 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 5631 | |
| 5632 | if (isn == NULL) |
| 5633 | goto theend; |
| 5634 | switch (*op) |
| 5635 | { |
| 5636 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 5637 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 5638 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 5639 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 5640 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 5641 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5642 | } |
| 5643 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5644 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5645 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5646 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5647 | int r; |
| 5648 | |
| 5649 | // Compile the "idx" in "var[idx]". |
| 5650 | if (new_local) |
| 5651 | --cctx->ctx_locals.ga_len; |
| 5652 | p = skipwhite(var_start + varlen + 1); |
| 5653 | r = compile_expr0(&p, cctx); |
| 5654 | if (new_local) |
| 5655 | ++cctx->ctx_locals.ga_len; |
| 5656 | if (r == FAIL) |
| 5657 | goto theend; |
| 5658 | if (*skipwhite(p) != ']') |
| 5659 | { |
| 5660 | emsg(_(e_missbrac)); |
| 5661 | goto theend; |
| 5662 | } |
| 5663 | if (type->tt_type == VAR_DICT |
| 5664 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5665 | goto theend; |
| 5666 | if (type->tt_type == VAR_LIST |
| 5667 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5668 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5669 | { |
| 5670 | emsg(_(e_number_exp)); |
| 5671 | goto theend; |
| 5672 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5673 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5674 | // Load the dict or list. On the stack we then have: |
| 5675 | // - value |
| 5676 | // - index |
| 5677 | // - variable |
| 5678 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5679 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5680 | if (type->tt_type == VAR_LIST) |
| 5681 | { |
| 5682 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5683 | return FAIL; |
| 5684 | } |
| 5685 | else if (type->tt_type == VAR_DICT) |
| 5686 | { |
| 5687 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5688 | return FAIL; |
| 5689 | } |
| 5690 | else |
| 5691 | { |
| 5692 | emsg(_(e_listreq)); |
| 5693 | goto theend; |
| 5694 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5695 | } |
| 5696 | else |
| 5697 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5698 | switch (dest) |
| 5699 | { |
| 5700 | case dest_option: |
| 5701 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5702 | break; |
| 5703 | case dest_global: |
| 5704 | // include g: with the name, easier to execute that way |
| 5705 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5706 | break; |
| 5707 | case dest_buffer: |
| 5708 | // include b: with the name, easier to execute that way |
| 5709 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5710 | break; |
| 5711 | case dest_window: |
| 5712 | // include w: with the name, easier to execute that way |
| 5713 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5714 | break; |
| 5715 | case dest_tab: |
| 5716 | // include t: with the name, easier to execute that way |
| 5717 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5718 | break; |
| 5719 | case dest_env: |
| 5720 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5721 | break; |
| 5722 | case dest_reg: |
| 5723 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5724 | break; |
| 5725 | case dest_vimvar: |
| 5726 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5727 | break; |
| 5728 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5729 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5730 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5731 | imported_T *import = NULL; |
| 5732 | int sid = current_sctx.sc_sid; |
| 5733 | int idx; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5734 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5735 | if (name[1] != ':') |
| 5736 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5737 | import = find_imported(name, 0, cctx); |
| 5738 | if (import != NULL) |
| 5739 | sid = import->imp_sid; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5740 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5741 | |
| 5742 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 5743 | // TODO: specific type |
| 5744 | if (idx < 0) |
| 5745 | { |
| 5746 | char_u *name_s = name; |
| 5747 | |
| 5748 | // Include s: in the name for store_var() |
| 5749 | if (name[1] != ':') |
| 5750 | { |
| 5751 | int len = (int)STRLEN(name) + 3; |
| 5752 | |
| 5753 | name_s = alloc(len); |
| 5754 | if (name_s == NULL) |
| 5755 | name_s = name; |
| 5756 | else |
| 5757 | vim_snprintf((char *)name_s, len, |
| 5758 | "s:%s", name); |
| 5759 | } |
| 5760 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5761 | &t_any); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5762 | if (name_s != name) |
| 5763 | vim_free(name_s); |
| 5764 | } |
| 5765 | else |
| 5766 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5767 | sid, idx, &t_any); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5768 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5769 | break; |
| 5770 | case dest_local: |
| 5771 | if (lvar != NULL) |
| 5772 | { |
| 5773 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5774 | + instr->ga_len - 1; |
| 5775 | |
| 5776 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5777 | // ISN_STORE into ISN_STORENR |
| 5778 | if (!lvar->lv_from_outer |
| 5779 | && instr->ga_len == instr_count + 1 |
| 5780 | && isn->isn_type == ISN_PUSHNR) |
| 5781 | { |
| 5782 | varnumber_T val = isn->isn_arg.number; |
| 5783 | |
| 5784 | isn->isn_type = ISN_STORENR; |
| 5785 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5786 | isn->isn_arg.storenr.stnr_val = val; |
| 5787 | if (stack->ga_len > 0) |
| 5788 | --stack->ga_len; |
| 5789 | } |
| 5790 | else if (lvar->lv_from_outer) |
| 5791 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5792 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5793 | else |
| 5794 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5795 | } |
| 5796 | break; |
| 5797 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5798 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5799 | |
| 5800 | if (var_idx + 1 < var_count) |
| 5801 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5802 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5803 | |
| 5804 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5805 | if (var_count > 0 && !semicolon) |
| 5806 | { |
| 5807 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5808 | goto theend; |
| 5809 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5810 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5811 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5812 | |
| 5813 | theend: |
| 5814 | vim_free(name); |
| 5815 | return ret; |
| 5816 | } |
| 5817 | |
| 5818 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5819 | * Check if "name" can be "unlet". |
| 5820 | */ |
| 5821 | int |
| 5822 | check_vim9_unlet(char_u *name) |
| 5823 | { |
| 5824 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5825 | { |
| 5826 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5827 | return FAIL; |
| 5828 | } |
| 5829 | return OK; |
| 5830 | } |
| 5831 | |
| 5832 | /* |
| 5833 | * Callback passed to ex_unletlock(). |
| 5834 | */ |
| 5835 | static int |
| 5836 | compile_unlet( |
| 5837 | lval_T *lvp, |
| 5838 | char_u *name_end, |
| 5839 | exarg_T *eap, |
| 5840 | int deep UNUSED, |
| 5841 | void *coookie) |
| 5842 | { |
| 5843 | cctx_T *cctx = coookie; |
| 5844 | |
| 5845 | if (lvp->ll_tv == NULL) |
| 5846 | { |
| 5847 | char_u *p = lvp->ll_name; |
| 5848 | int cc = *name_end; |
| 5849 | int ret = OK; |
| 5850 | |
| 5851 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5852 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5853 | if (*p == '$') |
| 5854 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5855 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5856 | ret = FAIL; |
| 5857 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5858 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5859 | |
| 5860 | *name_end = cc; |
| 5861 | return ret; |
| 5862 | } |
| 5863 | |
| 5864 | // TODO: unlet {list}[idx] |
| 5865 | // TODO: unlet {dict}[key] |
| 5866 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5867 | return FAIL; |
| 5868 | } |
| 5869 | |
| 5870 | /* |
| 5871 | * compile "unlet var", "lock var" and "unlock var" |
| 5872 | * "arg" points to "var". |
| 5873 | */ |
| 5874 | static char_u * |
| 5875 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5876 | { |
| 5877 | char_u *p = arg; |
| 5878 | |
| 5879 | if (eap->cmdidx != CMD_unlet) |
| 5880 | { |
| 5881 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5882 | return NULL; |
| 5883 | } |
| 5884 | |
| 5885 | if (*p == '!') |
| 5886 | { |
| 5887 | p = skipwhite(p + 1); |
| 5888 | eap->forceit = TRUE; |
| 5889 | } |
| 5890 | |
| 5891 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5892 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5893 | } |
| 5894 | |
| 5895 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5896 | * Compile an :import command. |
| 5897 | */ |
| 5898 | static char_u * |
| 5899 | compile_import(char_u *arg, cctx_T *cctx) |
| 5900 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5901 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5902 | } |
| 5903 | |
| 5904 | /* |
| 5905 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5906 | */ |
| 5907 | static int |
| 5908 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5909 | { |
| 5910 | garray_T *instr = &cctx->ctx_instr; |
| 5911 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5912 | |
| 5913 | if (endlabel == NULL) |
| 5914 | return FAIL; |
| 5915 | endlabel->el_next = *el; |
| 5916 | *el = endlabel; |
| 5917 | endlabel->el_end_label = instr->ga_len; |
| 5918 | |
| 5919 | generate_JUMP(cctx, when, 0); |
| 5920 | return OK; |
| 5921 | } |
| 5922 | |
| 5923 | static void |
| 5924 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5925 | { |
| 5926 | garray_T *instr = &cctx->ctx_instr; |
| 5927 | |
| 5928 | while (*el != NULL) |
| 5929 | { |
| 5930 | endlabel_T *cur = (*el); |
| 5931 | isn_T *isn; |
| 5932 | |
| 5933 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5934 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5935 | *el = cur->el_next; |
| 5936 | vim_free(cur); |
| 5937 | } |
| 5938 | } |
| 5939 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5940 | static void |
| 5941 | compile_free_jump_to_end(endlabel_T **el) |
| 5942 | { |
| 5943 | while (*el != NULL) |
| 5944 | { |
| 5945 | endlabel_T *cur = (*el); |
| 5946 | |
| 5947 | *el = cur->el_next; |
| 5948 | vim_free(cur); |
| 5949 | } |
| 5950 | } |
| 5951 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5952 | /* |
| 5953 | * Create a new scope and set up the generic items. |
| 5954 | */ |
| 5955 | static scope_T * |
| 5956 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5957 | { |
| 5958 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5959 | |
| 5960 | if (scope == NULL) |
| 5961 | return NULL; |
| 5962 | scope->se_outer = cctx->ctx_scope; |
| 5963 | cctx->ctx_scope = scope; |
| 5964 | scope->se_type = type; |
| 5965 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5966 | return scope; |
| 5967 | } |
| 5968 | |
| 5969 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5970 | * Free the current scope and go back to the outer scope. |
| 5971 | */ |
| 5972 | static void |
| 5973 | drop_scope(cctx_T *cctx) |
| 5974 | { |
| 5975 | scope_T *scope = cctx->ctx_scope; |
| 5976 | |
| 5977 | if (scope == NULL) |
| 5978 | { |
| 5979 | iemsg("calling drop_scope() without a scope"); |
| 5980 | return; |
| 5981 | } |
| 5982 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5983 | switch (scope->se_type) |
| 5984 | { |
| 5985 | case IF_SCOPE: |
| 5986 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5987 | case FOR_SCOPE: |
| 5988 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5989 | case WHILE_SCOPE: |
| 5990 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5991 | case TRY_SCOPE: |
| 5992 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5993 | case NO_SCOPE: |
| 5994 | case BLOCK_SCOPE: |
| 5995 | break; |
| 5996 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5997 | vim_free(scope); |
| 5998 | } |
| 5999 | |
| 6000 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6001 | * compile "if expr" |
| 6002 | * |
| 6003 | * "if expr" Produces instructions: |
| 6004 | * EVAL expr Push result of "expr" |
| 6005 | * JUMP_IF_FALSE end |
| 6006 | * ... body ... |
| 6007 | * end: |
| 6008 | * |
| 6009 | * "if expr | else" Produces instructions: |
| 6010 | * EVAL expr Push result of "expr" |
| 6011 | * JUMP_IF_FALSE else |
| 6012 | * ... body ... |
| 6013 | * JUMP_ALWAYS end |
| 6014 | * else: |
| 6015 | * ... body ... |
| 6016 | * end: |
| 6017 | * |
| 6018 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 6019 | * EVAL expr Push result of "expr" |
| 6020 | * JUMP_IF_FALSE elseif |
| 6021 | * ... body ... |
| 6022 | * JUMP_ALWAYS end |
| 6023 | * elseif: |
| 6024 | * EVAL expr Push result of "expr" |
| 6025 | * JUMP_IF_FALSE else |
| 6026 | * ... body ... |
| 6027 | * JUMP_ALWAYS end |
| 6028 | * else: |
| 6029 | * ... body ... |
| 6030 | * end: |
| 6031 | */ |
| 6032 | static char_u * |
| 6033 | compile_if(char_u *arg, cctx_T *cctx) |
| 6034 | { |
| 6035 | char_u *p = arg; |
| 6036 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6037 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6038 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6039 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6040 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6041 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6042 | CLEAR_FIELD(ppconst); |
| 6043 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6044 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6045 | clear_ppconst(&ppconst); |
| 6046 | return NULL; |
| 6047 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6048 | if (cctx->ctx_skip == SKIP_YES) |
| 6049 | clear_ppconst(&ppconst); |
| 6050 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6051 | { |
| 6052 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6053 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6054 | clear_ppconst(&ppconst); |
| 6055 | } |
| 6056 | else |
| 6057 | { |
| 6058 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6059 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6060 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6061 | return NULL; |
| 6062 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6063 | |
| 6064 | scope = new_scope(cctx, IF_SCOPE); |
| 6065 | if (scope == NULL) |
| 6066 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6067 | scope->se_skip_save = skip_save; |
| 6068 | // "is_had_return" will be reset if any block does not end in :return |
| 6069 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6070 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6071 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6072 | { |
| 6073 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6074 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6075 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6076 | } |
| 6077 | else |
| 6078 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6079 | |
| 6080 | return p; |
| 6081 | } |
| 6082 | |
| 6083 | static char_u * |
| 6084 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 6085 | { |
| 6086 | char_u *p = arg; |
| 6087 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6088 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6089 | isn_T *isn; |
| 6090 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6091 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6092 | |
| 6093 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6094 | { |
| 6095 | emsg(_(e_elseif_without_if)); |
| 6096 | return NULL; |
| 6097 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6098 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6099 | if (!cctx->ctx_had_return) |
| 6100 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6101 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6102 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6103 | { |
| 6104 | 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] | 6105 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6106 | return NULL; |
| 6107 | // previous "if" or "elseif" jumps here |
| 6108 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6109 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6110 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6111 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6112 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6113 | CLEAR_FIELD(ppconst); |
| 6114 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6115 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6116 | clear_ppconst(&ppconst); |
| 6117 | return NULL; |
| 6118 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6119 | if (scope->se_skip_save == SKIP_YES) |
| 6120 | clear_ppconst(&ppconst); |
| 6121 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6122 | { |
| 6123 | // The expression results in a constant. |
| 6124 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6125 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6126 | clear_ppconst(&ppconst); |
| 6127 | scope->se_u.se_if.is_if_label = -1; |
| 6128 | } |
| 6129 | else |
| 6130 | { |
| 6131 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6132 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6133 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6134 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6135 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6136 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6137 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6138 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6139 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6140 | |
| 6141 | return p; |
| 6142 | } |
| 6143 | |
| 6144 | static char_u * |
| 6145 | compile_else(char_u *arg, cctx_T *cctx) |
| 6146 | { |
| 6147 | char_u *p = arg; |
| 6148 | garray_T *instr = &cctx->ctx_instr; |
| 6149 | isn_T *isn; |
| 6150 | scope_T *scope = cctx->ctx_scope; |
| 6151 | |
| 6152 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6153 | { |
| 6154 | emsg(_(e_else_without_if)); |
| 6155 | return NULL; |
| 6156 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6157 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6158 | if (!cctx->ctx_had_return) |
| 6159 | scope->se_u.se_if.is_had_return = FALSE; |
| 6160 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6161 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6162 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6163 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6164 | // 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] | 6165 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6166 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6167 | if (!cctx->ctx_had_return |
| 6168 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6169 | JUMP_ALWAYS, cctx) == FAIL) |
| 6170 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6171 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6172 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6173 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6174 | { |
| 6175 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6176 | { |
| 6177 | // previous "if" or "elseif" jumps here |
| 6178 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6179 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6180 | scope->se_u.se_if.is_if_label = -1; |
| 6181 | } |
| 6182 | } |
| 6183 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6184 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6185 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6186 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6187 | |
| 6188 | return p; |
| 6189 | } |
| 6190 | |
| 6191 | static char_u * |
| 6192 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6193 | { |
| 6194 | scope_T *scope = cctx->ctx_scope; |
| 6195 | ifscope_T *ifscope; |
| 6196 | garray_T *instr = &cctx->ctx_instr; |
| 6197 | isn_T *isn; |
| 6198 | |
| 6199 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6200 | { |
| 6201 | emsg(_(e_endif_without_if)); |
| 6202 | return NULL; |
| 6203 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6204 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6205 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6206 | if (!cctx->ctx_had_return) |
| 6207 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6208 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6209 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6210 | { |
| 6211 | // previous "if" or "elseif" jumps here |
| 6212 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6213 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6214 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6215 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6216 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6217 | cctx->ctx_skip = scope->se_skip_save; |
| 6218 | |
| 6219 | // If all the blocks end in :return and there is an :else then the |
| 6220 | // had_return flag is set. |
| 6221 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6222 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6223 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6224 | return arg; |
| 6225 | } |
| 6226 | |
| 6227 | /* |
| 6228 | * compile "for var in expr" |
| 6229 | * |
| 6230 | * Produces instructions: |
| 6231 | * PUSHNR -1 |
| 6232 | * STORE loop-idx Set index to -1 |
| 6233 | * EVAL expr Push result of "expr" |
| 6234 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6235 | * - if beyond end, jump to "end" |
| 6236 | * - otherwise get item from list and push it |
| 6237 | * STORE var Store item in "var" |
| 6238 | * ... body ... |
| 6239 | * JUMP top Jump back to repeat |
| 6240 | * end: DROP Drop the result of "expr" |
| 6241 | * |
| 6242 | */ |
| 6243 | static char_u * |
| 6244 | compile_for(char_u *arg, cctx_T *cctx) |
| 6245 | { |
| 6246 | char_u *p; |
| 6247 | size_t varlen; |
| 6248 | garray_T *instr = &cctx->ctx_instr; |
| 6249 | garray_T *stack = &cctx->ctx_type_stack; |
| 6250 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6251 | lvar_T *loop_lvar; // loop iteration variable |
| 6252 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6253 | type_T *vartype; |
| 6254 | |
| 6255 | // TODO: list of variables: "for [key, value] in dict" |
| 6256 | // parse "var" |
| 6257 | for (p = arg; eval_isnamec1(*p); ++p) |
| 6258 | ; |
| 6259 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6260 | var_lvar = lookup_local(arg, varlen, cctx); |
| 6261 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6262 | { |
| 6263 | semsg(_("E1023: variable already defined: %s"), arg); |
| 6264 | return NULL; |
| 6265 | } |
| 6266 | |
| 6267 | // consume "in" |
| 6268 | p = skipwhite(p); |
| 6269 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 6270 | { |
| 6271 | emsg(_(e_missing_in)); |
| 6272 | return NULL; |
| 6273 | } |
| 6274 | p = skipwhite(p + 2); |
| 6275 | |
| 6276 | |
| 6277 | scope = new_scope(cctx, FOR_SCOPE); |
| 6278 | if (scope == NULL) |
| 6279 | return NULL; |
| 6280 | |
| 6281 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6282 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6283 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6284 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6285 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6286 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6287 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6288 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6289 | |
| 6290 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6291 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 6292 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6293 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6294 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6295 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6296 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6297 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6298 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6299 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6300 | |
| 6301 | // compile "expr", it remains on the stack until "endfor" |
| 6302 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6303 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6304 | { |
| 6305 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6306 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6307 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6308 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6309 | // Now that we know the type of "var", check that it is a list, now or at |
| 6310 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6311 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 6312 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6313 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6314 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6315 | return NULL; |
| 6316 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6317 | 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] | 6318 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6319 | |
| 6320 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6321 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6322 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6323 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 6324 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6325 | |
| 6326 | return arg; |
| 6327 | } |
| 6328 | |
| 6329 | /* |
| 6330 | * compile "endfor" |
| 6331 | */ |
| 6332 | static char_u * |
| 6333 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 6334 | { |
| 6335 | garray_T *instr = &cctx->ctx_instr; |
| 6336 | scope_T *scope = cctx->ctx_scope; |
| 6337 | forscope_T *forscope; |
| 6338 | isn_T *isn; |
| 6339 | |
| 6340 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 6341 | { |
| 6342 | emsg(_(e_for)); |
| 6343 | return NULL; |
| 6344 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6345 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6346 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6347 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6348 | |
| 6349 | // At end of ":for" scope jump back to the FOR instruction. |
| 6350 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 6351 | |
| 6352 | // Fill in the "end" label in the FOR statement so it can jump here |
| 6353 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 6354 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 6355 | |
| 6356 | // Fill in the "end" label any BREAK statements |
| 6357 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 6358 | |
| 6359 | // Below the ":for" scope drop the "expr" list from the stack. |
| 6360 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 6361 | return NULL; |
| 6362 | |
| 6363 | vim_free(scope); |
| 6364 | |
| 6365 | return arg; |
| 6366 | } |
| 6367 | |
| 6368 | /* |
| 6369 | * compile "while expr" |
| 6370 | * |
| 6371 | * Produces instructions: |
| 6372 | * top: EVAL expr Push result of "expr" |
| 6373 | * JUMP_IF_FALSE end jump if false |
| 6374 | * ... body ... |
| 6375 | * JUMP top Jump back to repeat |
| 6376 | * end: |
| 6377 | * |
| 6378 | */ |
| 6379 | static char_u * |
| 6380 | compile_while(char_u *arg, cctx_T *cctx) |
| 6381 | { |
| 6382 | char_u *p = arg; |
| 6383 | garray_T *instr = &cctx->ctx_instr; |
| 6384 | scope_T *scope; |
| 6385 | |
| 6386 | scope = new_scope(cctx, WHILE_SCOPE); |
| 6387 | if (scope == NULL) |
| 6388 | return NULL; |
| 6389 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6390 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6391 | |
| 6392 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6393 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6394 | return NULL; |
| 6395 | |
| 6396 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6397 | 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] | 6398 | JUMP_IF_FALSE, cctx) == FAIL) |
| 6399 | return FAIL; |
| 6400 | |
| 6401 | return p; |
| 6402 | } |
| 6403 | |
| 6404 | /* |
| 6405 | * compile "endwhile" |
| 6406 | */ |
| 6407 | static char_u * |
| 6408 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 6409 | { |
| 6410 | scope_T *scope = cctx->ctx_scope; |
| 6411 | |
| 6412 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 6413 | { |
| 6414 | emsg(_(e_while)); |
| 6415 | return NULL; |
| 6416 | } |
| 6417 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6418 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6419 | |
| 6420 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6421 | 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] | 6422 | |
| 6423 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 6424 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6425 | 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] | 6426 | |
| 6427 | vim_free(scope); |
| 6428 | |
| 6429 | return arg; |
| 6430 | } |
| 6431 | |
| 6432 | /* |
| 6433 | * compile "continue" |
| 6434 | */ |
| 6435 | static char_u * |
| 6436 | compile_continue(char_u *arg, cctx_T *cctx) |
| 6437 | { |
| 6438 | scope_T *scope = cctx->ctx_scope; |
| 6439 | |
| 6440 | for (;;) |
| 6441 | { |
| 6442 | if (scope == NULL) |
| 6443 | { |
| 6444 | emsg(_(e_continue)); |
| 6445 | return NULL; |
| 6446 | } |
| 6447 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6448 | break; |
| 6449 | scope = scope->se_outer; |
| 6450 | } |
| 6451 | |
| 6452 | // Jump back to the FOR or WHILE instruction. |
| 6453 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6454 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 6455 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6456 | return arg; |
| 6457 | } |
| 6458 | |
| 6459 | /* |
| 6460 | * compile "break" |
| 6461 | */ |
| 6462 | static char_u * |
| 6463 | compile_break(char_u *arg, cctx_T *cctx) |
| 6464 | { |
| 6465 | scope_T *scope = cctx->ctx_scope; |
| 6466 | endlabel_T **el; |
| 6467 | |
| 6468 | for (;;) |
| 6469 | { |
| 6470 | if (scope == NULL) |
| 6471 | { |
| 6472 | emsg(_(e_break)); |
| 6473 | return NULL; |
| 6474 | } |
| 6475 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6476 | break; |
| 6477 | scope = scope->se_outer; |
| 6478 | } |
| 6479 | |
| 6480 | // Jump to the end of the FOR or WHILE loop. |
| 6481 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6482 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6483 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6484 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6485 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 6486 | return FAIL; |
| 6487 | |
| 6488 | return arg; |
| 6489 | } |
| 6490 | |
| 6491 | /* |
| 6492 | * compile "{" start of block |
| 6493 | */ |
| 6494 | static char_u * |
| 6495 | compile_block(char_u *arg, cctx_T *cctx) |
| 6496 | { |
| 6497 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6498 | return NULL; |
| 6499 | return skipwhite(arg + 1); |
| 6500 | } |
| 6501 | |
| 6502 | /* |
| 6503 | * compile end of block: drop one scope |
| 6504 | */ |
| 6505 | static void |
| 6506 | compile_endblock(cctx_T *cctx) |
| 6507 | { |
| 6508 | scope_T *scope = cctx->ctx_scope; |
| 6509 | |
| 6510 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6511 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6512 | vim_free(scope); |
| 6513 | } |
| 6514 | |
| 6515 | /* |
| 6516 | * compile "try" |
| 6517 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6518 | * finally. |
| 6519 | * Creates another scope for the "try" block itself. |
| 6520 | * TRY instruction sets up exception handling at runtime. |
| 6521 | * |
| 6522 | * "try" |
| 6523 | * TRY -> catch1, -> finally push trystack entry |
| 6524 | * ... try block |
| 6525 | * "throw {exception}" |
| 6526 | * EVAL {exception} |
| 6527 | * THROW create exception |
| 6528 | * ... try block |
| 6529 | * " catch {expr}" |
| 6530 | * JUMP -> finally |
| 6531 | * catch1: PUSH exeception |
| 6532 | * EVAL {expr} |
| 6533 | * MATCH |
| 6534 | * JUMP nomatch -> catch2 |
| 6535 | * CATCH remove exception |
| 6536 | * ... catch block |
| 6537 | * " catch" |
| 6538 | * JUMP -> finally |
| 6539 | * catch2: CATCH remove exception |
| 6540 | * ... catch block |
| 6541 | * " finally" |
| 6542 | * finally: |
| 6543 | * ... finally block |
| 6544 | * " endtry" |
| 6545 | * ENDTRY pop trystack entry, may rethrow |
| 6546 | */ |
| 6547 | static char_u * |
| 6548 | compile_try(char_u *arg, cctx_T *cctx) |
| 6549 | { |
| 6550 | garray_T *instr = &cctx->ctx_instr; |
| 6551 | scope_T *try_scope; |
| 6552 | scope_T *scope; |
| 6553 | |
| 6554 | // scope that holds the jumps that go to catch/finally/endtry |
| 6555 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6556 | if (try_scope == NULL) |
| 6557 | return NULL; |
| 6558 | |
| 6559 | // "catch" is set when the first ":catch" is found. |
| 6560 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6561 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6562 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6563 | return NULL; |
| 6564 | |
| 6565 | // scope for the try block itself |
| 6566 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6567 | if (scope == NULL) |
| 6568 | return NULL; |
| 6569 | |
| 6570 | return arg; |
| 6571 | } |
| 6572 | |
| 6573 | /* |
| 6574 | * compile "catch {expr}" |
| 6575 | */ |
| 6576 | static char_u * |
| 6577 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6578 | { |
| 6579 | scope_T *scope = cctx->ctx_scope; |
| 6580 | garray_T *instr = &cctx->ctx_instr; |
| 6581 | char_u *p; |
| 6582 | isn_T *isn; |
| 6583 | |
| 6584 | // end block scope from :try or :catch |
| 6585 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6586 | compile_endblock(cctx); |
| 6587 | scope = cctx->ctx_scope; |
| 6588 | |
| 6589 | // Error if not in a :try scope |
| 6590 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6591 | { |
| 6592 | emsg(_(e_catch)); |
| 6593 | return NULL; |
| 6594 | } |
| 6595 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6596 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6597 | { |
| 6598 | emsg(_("E1033: catch unreachable after catch-all")); |
| 6599 | return NULL; |
| 6600 | } |
| 6601 | |
| 6602 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6603 | 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] | 6604 | JUMP_ALWAYS, cctx) == FAIL) |
| 6605 | return NULL; |
| 6606 | |
| 6607 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6608 | 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] | 6609 | if (isn->isn_arg.try.try_catch == 0) |
| 6610 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6611 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6612 | { |
| 6613 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6614 | 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] | 6615 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6616 | } |
| 6617 | |
| 6618 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6619 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6620 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6621 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6622 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6623 | } |
| 6624 | else |
| 6625 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6626 | char_u *end; |
| 6627 | char_u *pat; |
| 6628 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6629 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6630 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6631 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6632 | // Push v:exception, push {expr} and MATCH |
| 6633 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6634 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6635 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6636 | if (*end != *p) |
| 6637 | { |
| 6638 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 6639 | vim_free(tofree); |
| 6640 | return FAIL; |
| 6641 | } |
| 6642 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6643 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6644 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6645 | len = (int)(end - tofree); |
| 6646 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6647 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6648 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6649 | if (pat == NULL) |
| 6650 | return FAIL; |
| 6651 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6652 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6653 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6654 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6655 | return NULL; |
| 6656 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6657 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6658 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6659 | return NULL; |
| 6660 | } |
| 6661 | |
| 6662 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6663 | return NULL; |
| 6664 | |
| 6665 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6666 | return NULL; |
| 6667 | return p; |
| 6668 | } |
| 6669 | |
| 6670 | static char_u * |
| 6671 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6672 | { |
| 6673 | scope_T *scope = cctx->ctx_scope; |
| 6674 | garray_T *instr = &cctx->ctx_instr; |
| 6675 | isn_T *isn; |
| 6676 | |
| 6677 | // end block scope from :try or :catch |
| 6678 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6679 | compile_endblock(cctx); |
| 6680 | scope = cctx->ctx_scope; |
| 6681 | |
| 6682 | // Error if not in a :try scope |
| 6683 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6684 | { |
| 6685 | emsg(_(e_finally)); |
| 6686 | return NULL; |
| 6687 | } |
| 6688 | |
| 6689 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6690 | 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] | 6691 | if (isn->isn_arg.try.try_finally != 0) |
| 6692 | { |
| 6693 | emsg(_(e_finally_dup)); |
| 6694 | return NULL; |
| 6695 | } |
| 6696 | |
| 6697 | // 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] | 6698 | 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] | 6699 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6700 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6701 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6702 | { |
| 6703 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6704 | 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] | 6705 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6706 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6707 | } |
| 6708 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6709 | // TODO: set index in ts_finally_label jumps |
| 6710 | |
| 6711 | return arg; |
| 6712 | } |
| 6713 | |
| 6714 | static char_u * |
| 6715 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6716 | { |
| 6717 | scope_T *scope = cctx->ctx_scope; |
| 6718 | garray_T *instr = &cctx->ctx_instr; |
| 6719 | isn_T *isn; |
| 6720 | |
| 6721 | // end block scope from :catch or :finally |
| 6722 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6723 | compile_endblock(cctx); |
| 6724 | scope = cctx->ctx_scope; |
| 6725 | |
| 6726 | // Error if not in a :try scope |
| 6727 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6728 | { |
| 6729 | if (scope == NULL) |
| 6730 | emsg(_(e_no_endtry)); |
| 6731 | else if (scope->se_type == WHILE_SCOPE) |
| 6732 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6733 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6734 | emsg(_(e_endfor)); |
| 6735 | else |
| 6736 | emsg(_(e_endif)); |
| 6737 | return NULL; |
| 6738 | } |
| 6739 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6740 | 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] | 6741 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6742 | { |
| 6743 | emsg(_("E1032: missing :catch or :finally")); |
| 6744 | return NULL; |
| 6745 | } |
| 6746 | |
| 6747 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6748 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6749 | 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] | 6750 | |
| 6751 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6752 | if (isn->isn_arg.try.try_catch == 0) |
| 6753 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6754 | if (isn->isn_arg.try.try_finally == 0) |
| 6755 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6756 | |
| 6757 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6758 | { |
| 6759 | // Last catch without match jumps here |
| 6760 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6761 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6762 | } |
| 6763 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6764 | compile_endblock(cctx); |
| 6765 | |
| 6766 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6767 | return NULL; |
| 6768 | return arg; |
| 6769 | } |
| 6770 | |
| 6771 | /* |
| 6772 | * compile "throw {expr}" |
| 6773 | */ |
| 6774 | static char_u * |
| 6775 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6776 | { |
| 6777 | char_u *p = skipwhite(arg); |
| 6778 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6779 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6780 | return NULL; |
| 6781 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6782 | return NULL; |
| 6783 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6784 | return NULL; |
| 6785 | |
| 6786 | return p; |
| 6787 | } |
| 6788 | |
| 6789 | /* |
| 6790 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6791 | * compile "echomsg expr" |
| 6792 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6793 | * compile "execute expr" |
| 6794 | */ |
| 6795 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6796 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6797 | { |
| 6798 | char_u *p = arg; |
| 6799 | int count = 0; |
| 6800 | |
| 6801 | for (;;) |
| 6802 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6803 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6804 | return NULL; |
| 6805 | ++count; |
| 6806 | p = skipwhite(p); |
| 6807 | if (ends_excmd(*p)) |
| 6808 | break; |
| 6809 | } |
| 6810 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6811 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6812 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6813 | else if (cmdidx == CMD_execute) |
| 6814 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6815 | else if (cmdidx == CMD_echomsg) |
| 6816 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6817 | else |
| 6818 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6819 | return p; |
| 6820 | } |
| 6821 | |
| 6822 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6823 | * A command that is not compiled, execute with legacy code. |
| 6824 | */ |
| 6825 | static char_u * |
| 6826 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6827 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6828 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6829 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6830 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6831 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6832 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6833 | goto theend; |
| 6834 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6835 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6836 | { |
| 6837 | long argt = excmd_get_argt(eap->cmdidx); |
| 6838 | int usefilter = FALSE; |
| 6839 | |
| 6840 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6841 | |
| 6842 | // If the command can be followed by a bar, find the bar and truncate |
| 6843 | // it, so that the following command can be compiled. |
| 6844 | // The '|' is overwritten with a NUL, it is put back below. |
| 6845 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6846 | && *eap->arg == '!') |
| 6847 | // :w !filter or :r !filter or :r! filter |
| 6848 | usefilter = TRUE; |
| 6849 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6850 | { |
| 6851 | separate_nextcmd(eap); |
| 6852 | if (eap->nextcmd != NULL) |
| 6853 | nextcmd = eap->nextcmd; |
| 6854 | } |
| 6855 | } |
| 6856 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6857 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6858 | { |
| 6859 | // expand filename in "syntax include [@group] filename" |
| 6860 | has_expr = TRUE; |
| 6861 | eap->arg = skipwhite(eap->arg + 7); |
| 6862 | if (*eap->arg == '@') |
| 6863 | eap->arg = skiptowhite(eap->arg); |
| 6864 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6865 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6866 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6867 | { |
| 6868 | int count = 0; |
| 6869 | char_u *start = skipwhite(line); |
| 6870 | |
| 6871 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6872 | // PUSHS ":cmd xxx" |
| 6873 | // eval expr1 |
| 6874 | // PUSHS "yyy" |
| 6875 | // eval expr2 |
| 6876 | // PUSHS "zzz" |
| 6877 | // EXECCONCAT 5 |
| 6878 | for (;;) |
| 6879 | { |
| 6880 | if (p > start) |
| 6881 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6882 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6883 | ++count; |
| 6884 | } |
| 6885 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6886 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6887 | return NULL; |
| 6888 | may_generate_2STRING(-1, cctx); |
| 6889 | ++count; |
| 6890 | p = skipwhite(p); |
| 6891 | if (*p != '`') |
| 6892 | { |
| 6893 | emsg(_("E1083: missing backtick")); |
| 6894 | return NULL; |
| 6895 | } |
| 6896 | start = p + 1; |
| 6897 | |
| 6898 | p = (char_u *)strstr((char *)start, "`="); |
| 6899 | if (p == NULL) |
| 6900 | { |
| 6901 | if (*skipwhite(start) != NUL) |
| 6902 | { |
| 6903 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6904 | ++count; |
| 6905 | } |
| 6906 | break; |
| 6907 | } |
| 6908 | } |
| 6909 | generate_EXECCONCAT(cctx, count); |
| 6910 | } |
| 6911 | else |
| 6912 | generate_EXEC(cctx, line); |
| 6913 | |
| 6914 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6915 | if (*nextcmd != NUL) |
| 6916 | { |
| 6917 | // the parser expects a pointer to the bar, put it back |
| 6918 | --nextcmd; |
| 6919 | *nextcmd = '|'; |
| 6920 | } |
| 6921 | |
| 6922 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6923 | } |
| 6924 | |
| 6925 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6926 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6927 | * 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] | 6928 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6929 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6930 | add_def_function(ufunc_T *ufunc) |
| 6931 | { |
| 6932 | dfunc_T *dfunc; |
| 6933 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6934 | if (def_functions.ga_len == 0) |
| 6935 | { |
| 6936 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6937 | // wasn't set. |
| 6938 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6939 | return FAIL; |
| 6940 | ++def_functions.ga_len; |
| 6941 | } |
| 6942 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6943 | // Add the function to "def_functions". |
| 6944 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6945 | return FAIL; |
| 6946 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6947 | CLEAR_POINTER(dfunc); |
| 6948 | dfunc->df_idx = def_functions.ga_len; |
| 6949 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6950 | dfunc->df_ufunc = ufunc; |
| 6951 | ++def_functions.ga_len; |
| 6952 | return OK; |
| 6953 | } |
| 6954 | |
| 6955 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6956 | * After ex_function() has collected all the function lines: parse and compile |
| 6957 | * the lines into instructions. |
| 6958 | * Adds the function to "def_functions". |
| 6959 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6960 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6961 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6962 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6963 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6964 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6965 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6966 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6967 | 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] | 6968 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6969 | char_u *line = NULL; |
| 6970 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6971 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6972 | cctx_T cctx; |
| 6973 | garray_T *instr; |
| 6974 | int called_emsg_before = called_emsg; |
| 6975 | int ret = FAIL; |
| 6976 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6977 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6978 | int emsg_before = called_emsg; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6979 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6980 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6981 | // When using a function that was compiled before: Free old instructions. |
| 6982 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6983 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6984 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6985 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6986 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6987 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6988 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6989 | else |
| 6990 | { |
| 6991 | if (add_def_function(ufunc) == FAIL) |
| 6992 | return FAIL; |
| 6993 | new_def_function = TRUE; |
| 6994 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6995 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6996 | ufunc->uf_def_status = UF_COMPILING; |
| 6997 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6998 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6999 | cctx.ctx_ufunc = ufunc; |
| 7000 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7001 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7002 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 7003 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 7004 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 7005 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 7006 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 7007 | instr = &cctx.ctx_instr; |
| 7008 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7009 | // Set the context to the function, it may be compiled when called from |
| 7010 | // another script. Set the script version to the most modern one. |
| 7011 | // The line number will be set in next_line_from_context(). |
| 7012 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7013 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 7014 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7015 | // Make sure error messages are OK. |
| 7016 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 7017 | if (do_estack_push) |
| 7018 | estack_push_ufunc(ufunc, 1); |
| 7019 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7020 | if (ufunc->uf_def_args.ga_len > 0) |
| 7021 | { |
| 7022 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7023 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7024 | int i; |
| 7025 | char_u *arg; |
| 7026 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7027 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7028 | |
| 7029 | // Produce instructions for the default values of optional arguments. |
| 7030 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 7031 | // where to start when the function is called, depending on the number |
| 7032 | // of arguments. |
| 7033 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 7034 | if (ufunc->uf_def_arg_idx == NULL) |
| 7035 | goto erret; |
| 7036 | for (i = 0; i < count; ++i) |
| 7037 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7038 | garray_T *stack = &cctx.ctx_type_stack; |
| 7039 | type_T *val_type; |
| 7040 | int arg_idx = first_def_arg + i; |
| 7041 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7042 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 7043 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7044 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7045 | goto erret; |
| 7046 | |
| 7047 | // If no type specified use the type of the default value. |
| 7048 | // Otherwise check that the default value type matches the |
| 7049 | // specified type. |
| 7050 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 7051 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7052 | { |
| 7053 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7054 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7055 | } |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 7056 | 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] | 7057 | == FAIL) |
| 7058 | { |
| 7059 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 7060 | arg_idx + 1); |
| 7061 | goto erret; |
| 7062 | } |
| 7063 | |
| 7064 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7065 | goto erret; |
| 7066 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7067 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7068 | |
| 7069 | if (did_set_arg_type) |
| 7070 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7071 | } |
| 7072 | |
| 7073 | /* |
| 7074 | * Loop over all the lines of the function and generate instructions. |
| 7075 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7076 | for (;;) |
| 7077 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7078 | exarg_T ea; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7079 | int starts_with_colon = FALSE; |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7080 | char_u *cmd; |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7081 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7082 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7083 | // Bail out on the first error to avoid a flood of errors and report |
| 7084 | // the right line number when inside try/catch. |
| 7085 | if (emsg_before != called_emsg) |
| 7086 | goto erret; |
| 7087 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7088 | if (line != NULL && *line == '|') |
| 7089 | // the line continues after a '|' |
| 7090 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 7091 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7092 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 7093 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7094 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 7095 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7096 | goto erret; |
| 7097 | } |
| 7098 | else |
| 7099 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 7100 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 7101 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7102 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7103 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7104 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7105 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7106 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7107 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7108 | ea.cmdlinep = &line; |
| 7109 | ea.cmd = skipwhite(line); |
| 7110 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7111 | // Some things can be recognized by the first character. |
| 7112 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7113 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7114 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7115 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7116 | if (ea.cmd[1] != '{') |
| 7117 | { |
| 7118 | line = (char_u *)""; |
| 7119 | continue; |
| 7120 | } |
| 7121 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7122 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7123 | case '}': |
| 7124 | { |
| 7125 | // "}" ends a block scope |
| 7126 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7127 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7128 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7129 | if (stype == BLOCK_SCOPE) |
| 7130 | { |
| 7131 | compile_endblock(&cctx); |
| 7132 | line = ea.cmd; |
| 7133 | } |
| 7134 | else |
| 7135 | { |
| 7136 | emsg(_("E1025: using } outside of a block scope")); |
| 7137 | goto erret; |
| 7138 | } |
| 7139 | if (line != NULL) |
| 7140 | line = skipwhite(ea.cmd + 1); |
| 7141 | continue; |
| 7142 | } |
| 7143 | |
| 7144 | case '{': |
| 7145 | // "{" starts a block scope |
| 7146 | // "{'a': 1}->func() is something else |
| 7147 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7148 | { |
| 7149 | line = compile_block(ea.cmd, &cctx); |
| 7150 | continue; |
| 7151 | } |
| 7152 | break; |
| 7153 | |
| 7154 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7155 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7156 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7157 | } |
| 7158 | |
| 7159 | /* |
| 7160 | * COMMAND MODIFIERS |
| 7161 | */ |
| 7162 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 7163 | { |
| 7164 | if (errormsg != NULL) |
| 7165 | goto erret; |
| 7166 | // empty line or comment |
| 7167 | line = (char_u *)""; |
| 7168 | continue; |
| 7169 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7170 | // TODO: use modifiers in the command |
| 7171 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | b074e8b | 2020-07-11 13:40:45 +0200 | [diff] [blame] | 7172 | CLEAR_FIELD(cmdmod); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7173 | |
| 7174 | // Skip ":call" to get to the function name. |
| 7175 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 7176 | ea.cmd = skipwhite(ea.cmd); |
| 7177 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7178 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7179 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7180 | char_u *pskip; |
| 7181 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7182 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7183 | // find what follows. |
| 7184 | // Skip over "var.member", "var[idx]" and the like. |
| 7185 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 7186 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7187 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7188 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7189 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7190 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7191 | char_u *var_end; |
| 7192 | int oplen; |
| 7193 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7194 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7195 | var_end = find_name_end(pskip, NULL, NULL, |
| 7196 | FNE_CHECK_START | FNE_INCL_BR); |
| 7197 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7198 | if (oplen > 0) |
| 7199 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7200 | size_t len = p - ea.cmd; |
| 7201 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7202 | // Recognize an assignment if we recognize the variable |
| 7203 | // name: |
| 7204 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 7205 | // "local = expr" where "local" is a local var. |
| 7206 | // "script = expr" where "script" is a script-local var. |
| 7207 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7208 | // "&opt = expr" |
| 7209 | // "$ENV = expr" |
| 7210 | // "@r = expr" |
| 7211 | if (*ea.cmd == '&' |
| 7212 | || *ea.cmd == '$' |
| 7213 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7214 | || ((len) > 2 && ea.cmd[1] == ':') |
| 7215 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 7216 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 7217 | NULL, &cctx) == OK |
| 7218 | || lookup_script(ea.cmd, len) == OK |
| 7219 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7220 | { |
| 7221 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7222 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7223 | goto erret; |
| 7224 | continue; |
| 7225 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7226 | } |
| 7227 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7228 | |
| 7229 | if (*ea.cmd == '[') |
| 7230 | { |
| 7231 | // [var, var] = expr |
| 7232 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 7233 | if (line == NULL) |
| 7234 | goto erret; |
| 7235 | if (line != ea.cmd) |
| 7236 | continue; |
| 7237 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7238 | } |
| 7239 | |
| 7240 | /* |
| 7241 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7242 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7243 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7244 | cmd = ea.cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7245 | if (*cmd != '\'') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7246 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7247 | ea.cmd = skip_range(ea.cmd, NULL); |
| 7248 | if (ea.cmd > cmd && !starts_with_colon) |
| 7249 | { |
| 7250 | emsg(_(e_colon_required)); |
| 7251 | goto erret; |
| 7252 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7253 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7254 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7255 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7256 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7257 | |
| 7258 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 7259 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7260 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7261 | { |
| 7262 | line += STRLEN(line); |
| 7263 | continue; |
| 7264 | } |
| 7265 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7266 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7267 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7268 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7269 | // CMD_let cannot happen, compile_assignment() above is used |
| 7270 | iemsg("Command from find_ex_command() not handled"); |
| 7271 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7272 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7273 | } |
| 7274 | |
| 7275 | p = skipwhite(p); |
| 7276 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7277 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 7278 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7279 | && ea.cmdidx != CMD_elseif |
| 7280 | && ea.cmdidx != CMD_else |
| 7281 | && ea.cmdidx != CMD_endif) |
| 7282 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7283 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7284 | continue; |
| 7285 | } |
| 7286 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7287 | if (ea.cmdidx != CMD_elseif |
| 7288 | && ea.cmdidx != CMD_else |
| 7289 | && ea.cmdidx != CMD_endif |
| 7290 | && ea.cmdidx != CMD_endfor |
| 7291 | && ea.cmdidx != CMD_endwhile |
| 7292 | && ea.cmdidx != CMD_catch |
| 7293 | && ea.cmdidx != CMD_finally |
| 7294 | && ea.cmdidx != CMD_endtry) |
| 7295 | { |
| 7296 | if (cctx.ctx_had_return) |
| 7297 | { |
| 7298 | emsg(_("E1095: Unreachable code after :return")); |
| 7299 | goto erret; |
| 7300 | } |
| 7301 | } |
| 7302 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7303 | switch (ea.cmdidx) |
| 7304 | { |
| 7305 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7306 | ea.arg = p; |
| 7307 | line = compile_nested_function(&ea, &cctx); |
| 7308 | break; |
| 7309 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7310 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7311 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7312 | goto erret; |
| 7313 | |
| 7314 | case CMD_return: |
| 7315 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7316 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7317 | break; |
| 7318 | |
| 7319 | case CMD_let: |
| 7320 | case CMD_const: |
| 7321 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7322 | if (line == p) |
| 7323 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7324 | break; |
| 7325 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7326 | case CMD_unlet: |
| 7327 | case CMD_unlockvar: |
| 7328 | case CMD_lockvar: |
| 7329 | line = compile_unletlock(p, &ea, &cctx); |
| 7330 | break; |
| 7331 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7332 | case CMD_import: |
| 7333 | line = compile_import(p, &cctx); |
| 7334 | break; |
| 7335 | |
| 7336 | case CMD_if: |
| 7337 | line = compile_if(p, &cctx); |
| 7338 | break; |
| 7339 | case CMD_elseif: |
| 7340 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7341 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7342 | break; |
| 7343 | case CMD_else: |
| 7344 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7345 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7346 | break; |
| 7347 | case CMD_endif: |
| 7348 | line = compile_endif(p, &cctx); |
| 7349 | break; |
| 7350 | |
| 7351 | case CMD_while: |
| 7352 | line = compile_while(p, &cctx); |
| 7353 | break; |
| 7354 | case CMD_endwhile: |
| 7355 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7356 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7357 | break; |
| 7358 | |
| 7359 | case CMD_for: |
| 7360 | line = compile_for(p, &cctx); |
| 7361 | break; |
| 7362 | case CMD_endfor: |
| 7363 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7364 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7365 | break; |
| 7366 | case CMD_continue: |
| 7367 | line = compile_continue(p, &cctx); |
| 7368 | break; |
| 7369 | case CMD_break: |
| 7370 | line = compile_break(p, &cctx); |
| 7371 | break; |
| 7372 | |
| 7373 | case CMD_try: |
| 7374 | line = compile_try(p, &cctx); |
| 7375 | break; |
| 7376 | case CMD_catch: |
| 7377 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7378 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7379 | break; |
| 7380 | case CMD_finally: |
| 7381 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7382 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7383 | break; |
| 7384 | case CMD_endtry: |
| 7385 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7386 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7387 | break; |
| 7388 | case CMD_throw: |
| 7389 | line = compile_throw(p, &cctx); |
| 7390 | break; |
| 7391 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7392 | case CMD_eval: |
| 7393 | if (compile_expr0(&p, &cctx) == FAIL) |
| 7394 | goto erret; |
| 7395 | |
| 7396 | // drop the return value |
| 7397 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 7398 | |
| 7399 | line = skipwhite(p); |
| 7400 | break; |
| 7401 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7402 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7403 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7404 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7405 | case CMD_echomsg: |
| 7406 | case CMD_echoerr: |
| 7407 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7408 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7409 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7410 | // TODO: other commands with an expression argument |
| 7411 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7412 | case CMD_SIZE: |
| 7413 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 7414 | goto erret; |
| 7415 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7416 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7417 | // Not recognized, execute with do_cmdline_cmd(). |
| 7418 | ea.arg = p; |
| 7419 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7420 | break; |
| 7421 | } |
| 7422 | if (line == NULL) |
| 7423 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7424 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7425 | |
| 7426 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7427 | { |
| 7428 | iemsg("Type stack underflow"); |
| 7429 | goto erret; |
| 7430 | } |
| 7431 | } |
| 7432 | |
| 7433 | if (cctx.ctx_scope != NULL) |
| 7434 | { |
| 7435 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7436 | emsg(_(e_endif)); |
| 7437 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7438 | emsg(_(e_endwhile)); |
| 7439 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7440 | emsg(_(e_endfor)); |
| 7441 | else |
| 7442 | emsg(_("E1026: Missing }")); |
| 7443 | goto erret; |
| 7444 | } |
| 7445 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7446 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7447 | { |
| 7448 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7449 | { |
| 7450 | emsg(_("E1027: Missing return statement")); |
| 7451 | goto erret; |
| 7452 | } |
| 7453 | |
| 7454 | // Return zero if there is no return at the end. |
| 7455 | generate_PUSHNR(&cctx, 0); |
| 7456 | generate_instr(&cctx, ISN_RETURN); |
| 7457 | } |
| 7458 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7459 | { |
| 7460 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7461 | + ufunc->uf_dfunc_idx; |
| 7462 | dfunc->df_deleted = FALSE; |
| 7463 | dfunc->df_instr = instr->ga_data; |
| 7464 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7465 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 7466 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7467 | if (cctx.ctx_outer_used) |
| 7468 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7469 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7470 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7471 | |
| 7472 | ret = OK; |
| 7473 | |
| 7474 | erret: |
| 7475 | if (ret == FAIL) |
| 7476 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7477 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7478 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7479 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7480 | |
| 7481 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7482 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7483 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7484 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7485 | // If using the last entry in the table and it was added above, we |
| 7486 | // might as well remove it. |
| 7487 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7488 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7489 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7490 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7491 | ufunc->uf_dfunc_idx = 0; |
| 7492 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7493 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7494 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7495 | while (cctx.ctx_scope != NULL) |
| 7496 | drop_scope(&cctx); |
| 7497 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7498 | // Don't execute this function body. |
| 7499 | ga_clear_strings(&ufunc->uf_lines); |
| 7500 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7501 | if (errormsg != NULL) |
| 7502 | emsg(errormsg); |
| 7503 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 7504 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7505 | } |
| 7506 | |
| 7507 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7508 | if (do_estack_push) |
| 7509 | estack_pop(); |
| 7510 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7511 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7512 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7513 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7514 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7515 | } |
| 7516 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7517 | void |
| 7518 | set_function_type(ufunc_T *ufunc) |
| 7519 | { |
| 7520 | int varargs = ufunc->uf_va_name != NULL; |
| 7521 | int argcount = ufunc->uf_args.ga_len; |
| 7522 | |
| 7523 | // Create a type for the function, with the return type and any |
| 7524 | // argument types. |
| 7525 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7526 | // The type is included in "tt_args". |
| 7527 | if (argcount > 0 || varargs) |
| 7528 | { |
| 7529 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7530 | argcount, &ufunc->uf_type_list); |
| 7531 | // Add argument types to the function type. |
| 7532 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7533 | argcount + varargs, |
| 7534 | &ufunc->uf_type_list) == FAIL) |
| 7535 | return; |
| 7536 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7537 | ufunc->uf_func_type->tt_min_argcount = |
| 7538 | argcount - ufunc->uf_def_args.ga_len; |
| 7539 | if (ufunc->uf_arg_types == NULL) |
| 7540 | { |
| 7541 | int i; |
| 7542 | |
| 7543 | // lambda does not have argument types. |
| 7544 | for (i = 0; i < argcount; ++i) |
| 7545 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7546 | } |
| 7547 | else |
| 7548 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7549 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7550 | if (varargs) |
| 7551 | { |
| 7552 | ufunc->uf_func_type->tt_args[argcount] = |
| 7553 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7554 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7555 | } |
| 7556 | } |
| 7557 | else |
| 7558 | // No arguments, can use a predefined type. |
| 7559 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7560 | argcount, &ufunc->uf_type_list); |
| 7561 | } |
| 7562 | |
| 7563 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7564 | /* |
| 7565 | * Delete an instruction, free what it contains. |
| 7566 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7567 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7568 | delete_instr(isn_T *isn) |
| 7569 | { |
| 7570 | switch (isn->isn_type) |
| 7571 | { |
| 7572 | case ISN_EXEC: |
| 7573 | case ISN_LOADENV: |
| 7574 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7575 | case ISN_LOADB: |
| 7576 | case ISN_LOADW: |
| 7577 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7578 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7579 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7580 | case ISN_PUSHEXC: |
| 7581 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7582 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7583 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7584 | case ISN_STOREB: |
| 7585 | case ISN_STOREW: |
| 7586 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7587 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7588 | vim_free(isn->isn_arg.string); |
| 7589 | break; |
| 7590 | |
| 7591 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7592 | case ISN_STORES: |
| 7593 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7594 | break; |
| 7595 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7596 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7597 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7598 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7599 | break; |
| 7600 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7601 | case ISN_STOREOPT: |
| 7602 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7603 | break; |
| 7604 | |
| 7605 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7606 | blob_unref(isn->isn_arg.blob); |
| 7607 | break; |
| 7608 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7609 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7610 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7611 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7612 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7613 | break; |
| 7614 | |
| 7615 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7616 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7617 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7618 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7619 | break; |
| 7620 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7621 | case ISN_UCALL: |
| 7622 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7623 | break; |
| 7624 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7625 | case ISN_FUNCREF: |
| 7626 | { |
| 7627 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7628 | + isn->isn_arg.funcref.fr_func; |
| 7629 | func_ptr_unref(dfunc->df_ufunc); |
| 7630 | } |
| 7631 | break; |
| 7632 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7633 | case ISN_2BOOL: |
| 7634 | case ISN_2STRING: |
| 7635 | case ISN_ADDBLOB: |
| 7636 | case ISN_ADDLIST: |
| 7637 | case ISN_BCALL: |
| 7638 | case ISN_CATCH: |
| 7639 | case ISN_CHECKNR: |
| 7640 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7641 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7642 | case ISN_COMPAREANY: |
| 7643 | case ISN_COMPAREBLOB: |
| 7644 | case ISN_COMPAREBOOL: |
| 7645 | case ISN_COMPAREDICT: |
| 7646 | case ISN_COMPAREFLOAT: |
| 7647 | case ISN_COMPAREFUNC: |
| 7648 | case ISN_COMPARELIST: |
| 7649 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7650 | case ISN_COMPARESPECIAL: |
| 7651 | case ISN_COMPARESTRING: |
| 7652 | case ISN_CONCAT: |
| 7653 | case ISN_DCALL: |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 7654 | case ISN_SHUFFLE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7655 | case ISN_DROP: |
| 7656 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7657 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7658 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7659 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7660 | case ISN_EXECCONCAT: |
| 7661 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7662 | case ISN_FOR: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7663 | case ISN_LISTINDEX: |
| 7664 | case ISN_STRINDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7665 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7666 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7667 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7668 | case ISN_JUMP: |
| 7669 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7670 | case ISN_LOADBDICT: |
| 7671 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7672 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7673 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7674 | case ISN_LOADSCRIPT: |
| 7675 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7676 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7677 | case ISN_LOADWDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7678 | case ISN_NEGATENR: |
| 7679 | case ISN_NEWDICT: |
| 7680 | case ISN_NEWLIST: |
| 7681 | case ISN_OPNR: |
| 7682 | case ISN_OPFLOAT: |
| 7683 | case ISN_OPANY: |
| 7684 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7685 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7686 | case ISN_PUSHF: |
| 7687 | case ISN_PUSHNR: |
| 7688 | case ISN_PUSHBOOL: |
| 7689 | case ISN_PUSHSPEC: |
| 7690 | case ISN_RETURN: |
| 7691 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 7692 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7693 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7694 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7695 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7696 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7697 | case ISN_STOREDICT: |
| 7698 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7699 | case ISN_THROW: |
| 7700 | case ISN_TRY: |
| 7701 | // nothing allocated |
| 7702 | break; |
| 7703 | } |
| 7704 | } |
| 7705 | |
| 7706 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7707 | * Free all instructions for "dfunc". |
| 7708 | */ |
| 7709 | static void |
| 7710 | delete_def_function_contents(dfunc_T *dfunc) |
| 7711 | { |
| 7712 | int idx; |
| 7713 | |
| 7714 | ga_clear(&dfunc->df_def_args_isn); |
| 7715 | |
| 7716 | if (dfunc->df_instr != NULL) |
| 7717 | { |
| 7718 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7719 | delete_instr(dfunc->df_instr + idx); |
| 7720 | VIM_CLEAR(dfunc->df_instr); |
| 7721 | } |
| 7722 | |
| 7723 | dfunc->df_deleted = TRUE; |
| 7724 | } |
| 7725 | |
| 7726 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7727 | * When a user function is deleted, clear the contents of any associated def |
| 7728 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7729 | */ |
| 7730 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7731 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7732 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7733 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7734 | { |
| 7735 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7736 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7737 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7738 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7739 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7740 | } |
| 7741 | } |
| 7742 | |
| 7743 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7744 | /* |
| 7745 | * Free all functions defined with ":def". |
| 7746 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7747 | void |
| 7748 | free_def_functions(void) |
| 7749 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7750 | int idx; |
| 7751 | |
| 7752 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7753 | { |
| 7754 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7755 | |
| 7756 | delete_def_function_contents(dfunc); |
| 7757 | } |
| 7758 | |
| 7759 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7760 | } |
| 7761 | #endif |
| 7762 | |
| 7763 | |
| 7764 | #endif // FEAT_EVAL |