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); |
| 537 | if (ufunc != NULL && ufunc->uf_func_type != NULL) |
| 538 | return ufunc->uf_func_type; |
| 539 | } |
| 540 | |
| 541 | actual = alloc_type(type_gap); |
| 542 | if (actual == NULL) |
| 543 | return NULL; |
| 544 | actual->tt_type = tv->v_type; |
| 545 | actual->tt_member = &t_any; |
| 546 | |
| 547 | return actual; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Get a type_T for a typval_T, used for v: variables. |
| 552 | * "type_list" is used to temporarily create types in. |
| 553 | */ |
| 554 | type_T * |
| 555 | typval2type_vimvar(typval_T *tv, garray_T *type_gap) |
| 556 | { |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 557 | if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles |
| 558 | return &t_list_string; |
| 559 | if (tv->v_type == VAR_DICT) // e.g. for v:completed_item |
| 560 | return &t_dict_any; |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 561 | return typval2type(tv, type_gap); |
| 562 | } |
| 563 | |
| 564 | |
| 565 | /* |
| 566 | * Return FAIL if "expected" and "actual" don't match. |
| 567 | */ |
| 568 | int |
| 569 | check_typval_type(type_T *expected, typval_T *actual_tv) |
| 570 | { |
| 571 | garray_T type_list; |
| 572 | type_T *actual_type; |
| 573 | int res = FAIL; |
| 574 | |
| 575 | ga_init2(&type_list, sizeof(type_T *), 10); |
| 576 | actual_type = typval2type(actual_tv, &type_list); |
| 577 | if (actual_type != NULL) |
| 578 | res = check_type(expected, actual_type, TRUE); |
| 579 | clear_type_list(&type_list); |
| 580 | return res; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 583 | static void |
| 584 | type_mismatch(type_T *expected, type_T *actual) |
| 585 | { |
| 586 | char *tofree1, *tofree2; |
| 587 | |
| 588 | semsg(_("E1013: type mismatch, expected %s but got %s"), |
| 589 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 590 | vim_free(tofree1); |
| 591 | vim_free(tofree2); |
| 592 | } |
| 593 | |
| 594 | static void |
| 595 | arg_type_mismatch(type_T *expected, type_T *actual, int argidx) |
| 596 | { |
| 597 | char *tofree1, *tofree2; |
| 598 | |
| 599 | semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"), |
| 600 | argidx, |
| 601 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 602 | vim_free(tofree1); |
| 603 | vim_free(tofree2); |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * Check if the expected and actual types match. |
| 608 | * Does not allow for assigning "any" to a specific type. |
| 609 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 610 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 611 | check_type(type_T *expected, type_T *actual, int give_msg) |
| 612 | { |
| 613 | int ret = OK; |
| 614 | |
| 615 | // When expected is "unknown" we accept any actual type. |
| 616 | // When expected is "any" we accept any actual type except "void". |
| 617 | if (expected->tt_type != VAR_UNKNOWN |
| 618 | && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) |
| 619 | |
| 620 | { |
| 621 | if (expected->tt_type != actual->tt_type) |
| 622 | { |
| 623 | if (give_msg) |
| 624 | type_mismatch(expected, actual); |
| 625 | return FAIL; |
| 626 | } |
| 627 | if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) |
| 628 | { |
| 629 | // "unknown" is used for an empty list or dict |
| 630 | if (actual->tt_member != &t_unknown) |
| 631 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 632 | } |
| 633 | else if (expected->tt_type == VAR_FUNC) |
| 634 | { |
| 635 | if (expected->tt_member != &t_unknown) |
| 636 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 637 | if (ret == OK && expected->tt_argcount != -1 |
| 638 | && (actual->tt_argcount < expected->tt_min_argcount |
| 639 | || actual->tt_argcount > expected->tt_argcount)) |
| 640 | ret = FAIL; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 641 | if (expected->tt_args != NULL && actual->tt_args != NULL) |
| 642 | { |
| 643 | int i; |
| 644 | |
| 645 | for (i = 0; i < expected->tt_argcount; ++i) |
| 646 | if (check_type(expected->tt_args[i], actual->tt_args[i], |
| 647 | FALSE) == FAIL) |
| 648 | { |
| 649 | ret = FAIL; |
| 650 | break; |
| 651 | } |
| 652 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 653 | } |
| 654 | if (ret == FAIL && give_msg) |
| 655 | type_mismatch(expected, actual); |
| 656 | } |
| 657 | return ret; |
| 658 | } |
| 659 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 660 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 661 | ///////////////////////////////////////////////////////////////////// |
| 662 | // Following generate_ functions expect the caller to call ga_grow(). |
| 663 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 664 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 665 | #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] | 666 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 667 | /* |
| 668 | * Generate an instruction without arguments. |
| 669 | * Returns a pointer to the new instruction, NULL if failed. |
| 670 | */ |
| 671 | static isn_T * |
| 672 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 673 | { |
| 674 | garray_T *instr = &cctx->ctx_instr; |
| 675 | isn_T *isn; |
| 676 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 677 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 678 | if (ga_grow(instr, 1) == FAIL) |
| 679 | return NULL; |
| 680 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 681 | isn->isn_type = isn_type; |
| 682 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 683 | ++instr->ga_len; |
| 684 | |
| 685 | return isn; |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * Generate an instruction without arguments. |
| 690 | * "drop" will be removed from the stack. |
| 691 | * Returns a pointer to the new instruction, NULL if failed. |
| 692 | */ |
| 693 | static isn_T * |
| 694 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 695 | { |
| 696 | garray_T *stack = &cctx->ctx_type_stack; |
| 697 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 698 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 699 | stack->ga_len -= drop; |
| 700 | return generate_instr(cctx, isn_type); |
| 701 | } |
| 702 | |
| 703 | /* |
| 704 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 705 | */ |
| 706 | static isn_T * |
| 707 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 708 | { |
| 709 | isn_T *isn; |
| 710 | garray_T *stack = &cctx->ctx_type_stack; |
| 711 | |
| 712 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 713 | return NULL; |
| 714 | |
| 715 | if (ga_grow(stack, 1) == FAIL) |
| 716 | return NULL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 717 | ((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] | 718 | ++stack->ga_len; |
| 719 | |
| 720 | return isn; |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 725 | */ |
| 726 | static int |
| 727 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 728 | { |
| 729 | isn_T *isn; |
| 730 | garray_T *stack = &cctx->ctx_type_stack; |
| 731 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 732 | |
| 733 | if ((*type)->tt_type == VAR_STRING) |
| 734 | return OK; |
| 735 | *type = &t_string; |
| 736 | |
| 737 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 738 | return FAIL; |
| 739 | isn->isn_arg.number = offset; |
| 740 | |
| 741 | return OK; |
| 742 | } |
| 743 | |
| 744 | static int |
| 745 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 746 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 747 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 748 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 749 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 750 | { |
| 751 | if (*op == '+') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 752 | emsg(_("E1051: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 753 | else |
| 754 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 755 | return FAIL; |
| 756 | } |
| 757 | return OK; |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * Generate an instruction with two arguments. The instruction depends on the |
| 762 | * type of the arguments. |
| 763 | */ |
| 764 | static int |
| 765 | generate_two_op(cctx_T *cctx, char_u *op) |
| 766 | { |
| 767 | garray_T *stack = &cctx->ctx_type_stack; |
| 768 | type_T *type1; |
| 769 | type_T *type2; |
| 770 | vartype_T vartype; |
| 771 | isn_T *isn; |
| 772 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 773 | RETURN_OK_IF_SKIP(cctx); |
| 774 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 775 | // Get the known type of the two items on the stack. If they are matching |
| 776 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 777 | // checking. |
| 778 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 779 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 780 | vartype = VAR_ANY; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 781 | if (type1->tt_type == type2->tt_type |
| 782 | && (type1->tt_type == VAR_NUMBER |
| 783 | || type1->tt_type == VAR_LIST |
| 784 | #ifdef FEAT_FLOAT |
| 785 | || type1->tt_type == VAR_FLOAT |
| 786 | #endif |
| 787 | || type1->tt_type == VAR_BLOB)) |
| 788 | vartype = type1->tt_type; |
| 789 | |
| 790 | switch (*op) |
| 791 | { |
| 792 | case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 793 | && type1->tt_type != VAR_ANY |
| 794 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 795 | && check_number_or_float( |
| 796 | type1->tt_type, type2->tt_type, op) == FAIL) |
| 797 | return FAIL; |
| 798 | isn = generate_instr_drop(cctx, |
| 799 | vartype == VAR_NUMBER ? ISN_OPNR |
| 800 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 801 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 802 | #ifdef FEAT_FLOAT |
| 803 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 804 | #endif |
| 805 | : ISN_OPANY, 1); |
| 806 | if (isn != NULL) |
| 807 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 808 | break; |
| 809 | |
| 810 | case '-': |
| 811 | case '*': |
| 812 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 813 | op) == FAIL) |
| 814 | return FAIL; |
| 815 | if (vartype == VAR_NUMBER) |
| 816 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 817 | #ifdef FEAT_FLOAT |
| 818 | else if (vartype == VAR_FLOAT) |
| 819 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 820 | #endif |
| 821 | else |
| 822 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 823 | if (isn != NULL) |
| 824 | isn->isn_arg.op.op_type = *op == '*' |
| 825 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 826 | break; |
| 827 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 828 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 829 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 830 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 831 | && type2->tt_type != VAR_NUMBER)) |
| 832 | { |
| 833 | emsg(_("E1035: % requires number arguments")); |
| 834 | return FAIL; |
| 835 | } |
| 836 | isn = generate_instr_drop(cctx, |
| 837 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 838 | if (isn != NULL) |
| 839 | isn->isn_arg.op.op_type = EXPR_REM; |
| 840 | break; |
| 841 | } |
| 842 | |
| 843 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 844 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 845 | { |
| 846 | type_T *type = &t_any; |
| 847 | |
| 848 | #ifdef FEAT_FLOAT |
| 849 | // float+number and number+float results in float |
| 850 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 851 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 852 | type = &t_float; |
| 853 | #endif |
| 854 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 855 | } |
| 856 | |
| 857 | return OK; |
| 858 | } |
| 859 | |
| 860 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 861 | * Get the instruction to use for comparing "type1" with "type2" |
| 862 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 863 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 864 | static isntype_T |
| 865 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 866 | { |
| 867 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 868 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 869 | if (type1 == VAR_UNKNOWN) |
| 870 | type1 = VAR_ANY; |
| 871 | if (type2 == VAR_UNKNOWN) |
| 872 | type2 = VAR_ANY; |
| 873 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 874 | if (type1 == type2) |
| 875 | { |
| 876 | switch (type1) |
| 877 | { |
| 878 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 879 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 880 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 881 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 882 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 883 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 884 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 885 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 886 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 887 | default: isntype = ISN_COMPAREANY; break; |
| 888 | } |
| 889 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 890 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 891 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 892 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 893 | isntype = ISN_COMPAREANY; |
| 894 | |
| 895 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 896 | && (isntype == ISN_COMPAREBOOL |
| 897 | || isntype == ISN_COMPARESPECIAL |
| 898 | || isntype == ISN_COMPARENR |
| 899 | || isntype == ISN_COMPAREFLOAT)) |
| 900 | { |
| 901 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 902 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 903 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 904 | } |
| 905 | if (isntype == ISN_DROP |
| 906 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 907 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 908 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 909 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 910 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 911 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 912 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 913 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 914 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 915 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 916 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 917 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 918 | return isntype; |
| 919 | } |
| 920 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 921 | int |
| 922 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 923 | { |
| 924 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 925 | return FAIL; |
| 926 | return OK; |
| 927 | } |
| 928 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 929 | /* |
| 930 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 931 | */ |
| 932 | static int |
| 933 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 934 | { |
| 935 | isntype_T isntype; |
| 936 | isn_T *isn; |
| 937 | garray_T *stack = &cctx->ctx_type_stack; |
| 938 | vartype_T type1; |
| 939 | vartype_T type2; |
| 940 | |
| 941 | RETURN_OK_IF_SKIP(cctx); |
| 942 | |
| 943 | // Get the known type of the two items on the stack. If they are matching |
| 944 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 945 | // checking. |
| 946 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 947 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 948 | isntype = get_compare_isn(exptype, type1, type2); |
| 949 | if (isntype == ISN_DROP) |
| 950 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 951 | |
| 952 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 953 | return FAIL; |
| 954 | isn->isn_arg.op.op_type = exptype; |
| 955 | isn->isn_arg.op.op_ic = ic; |
| 956 | |
| 957 | // takes two arguments, puts one bool back |
| 958 | if (stack->ga_len >= 2) |
| 959 | { |
| 960 | --stack->ga_len; |
| 961 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 962 | } |
| 963 | |
| 964 | return OK; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Generate an ISN_2BOOL instruction. |
| 969 | */ |
| 970 | static int |
| 971 | generate_2BOOL(cctx_T *cctx, int invert) |
| 972 | { |
| 973 | isn_T *isn; |
| 974 | garray_T *stack = &cctx->ctx_type_stack; |
| 975 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 976 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 977 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 978 | return FAIL; |
| 979 | isn->isn_arg.number = invert; |
| 980 | |
| 981 | // type becomes bool |
| 982 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 983 | |
| 984 | return OK; |
| 985 | } |
| 986 | |
| 987 | static int |
| 988 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 989 | { |
| 990 | isn_T *isn; |
| 991 | garray_T *stack = &cctx->ctx_type_stack; |
| 992 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 993 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 994 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 995 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 996 | // TODO: whole type, e.g. for a function also arg and return types |
| 997 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 998 | isn->isn_arg.type.ct_off = offset; |
| 999 | |
| 1000 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1001 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1002 | |
| 1003 | return OK; |
| 1004 | } |
| 1005 | |
| 1006 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1007 | * Check that |
| 1008 | * - "actual" is "expected" type or |
| 1009 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 1010 | * - return FAIL. |
| 1011 | */ |
| 1012 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1013 | need_type( |
| 1014 | type_T *actual, |
| 1015 | type_T *expected, |
| 1016 | int offset, |
| 1017 | cctx_T *cctx, |
| 1018 | int silent) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1019 | { |
| 1020 | if (check_type(expected, actual, FALSE) == OK) |
| 1021 | return OK; |
| 1022 | if (actual->tt_type != VAR_ANY |
| 1023 | && actual->tt_type != VAR_UNKNOWN |
| 1024 | && !(actual->tt_type == VAR_FUNC |
| 1025 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 1026 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1027 | if (!silent) |
| 1028 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1029 | return FAIL; |
| 1030 | } |
| 1031 | generate_TYPECHECK(cctx, expected, offset); |
| 1032 | return OK; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1036 | * Generate an ISN_PUSHNR instruction. |
| 1037 | */ |
| 1038 | static int |
| 1039 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 1040 | { |
| 1041 | isn_T *isn; |
| 1042 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1043 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1044 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 1045 | return FAIL; |
| 1046 | isn->isn_arg.number = number; |
| 1047 | |
| 1048 | return OK; |
| 1049 | } |
| 1050 | |
| 1051 | /* |
| 1052 | * Generate an ISN_PUSHBOOL instruction. |
| 1053 | */ |
| 1054 | static int |
| 1055 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 1056 | { |
| 1057 | isn_T *isn; |
| 1058 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1059 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1060 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 1061 | return FAIL; |
| 1062 | isn->isn_arg.number = number; |
| 1063 | |
| 1064 | return OK; |
| 1065 | } |
| 1066 | |
| 1067 | /* |
| 1068 | * Generate an ISN_PUSHSPEC instruction. |
| 1069 | */ |
| 1070 | static int |
| 1071 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 1072 | { |
| 1073 | isn_T *isn; |
| 1074 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1075 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1076 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 1077 | return FAIL; |
| 1078 | isn->isn_arg.number = number; |
| 1079 | |
| 1080 | return OK; |
| 1081 | } |
| 1082 | |
| 1083 | #ifdef FEAT_FLOAT |
| 1084 | /* |
| 1085 | * Generate an ISN_PUSHF instruction. |
| 1086 | */ |
| 1087 | static int |
| 1088 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 1089 | { |
| 1090 | isn_T *isn; |
| 1091 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1092 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1093 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 1094 | return FAIL; |
| 1095 | isn->isn_arg.fnumber = fnumber; |
| 1096 | |
| 1097 | return OK; |
| 1098 | } |
| 1099 | #endif |
| 1100 | |
| 1101 | /* |
| 1102 | * Generate an ISN_PUSHS instruction. |
| 1103 | * Consumes "str". |
| 1104 | */ |
| 1105 | static int |
| 1106 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 1107 | { |
| 1108 | isn_T *isn; |
| 1109 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1110 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1111 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 1112 | return FAIL; |
| 1113 | isn->isn_arg.string = str; |
| 1114 | |
| 1115 | return OK; |
| 1116 | } |
| 1117 | |
| 1118 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1119 | * Generate an ISN_PUSHCHANNEL instruction. |
| 1120 | * Consumes "channel". |
| 1121 | */ |
| 1122 | static int |
| 1123 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 1124 | { |
| 1125 | isn_T *isn; |
| 1126 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1127 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1128 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 1129 | return FAIL; |
| 1130 | isn->isn_arg.channel = channel; |
| 1131 | |
| 1132 | return OK; |
| 1133 | } |
| 1134 | |
| 1135 | /* |
| 1136 | * Generate an ISN_PUSHJOB instruction. |
| 1137 | * Consumes "job". |
| 1138 | */ |
| 1139 | static int |
| 1140 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 1141 | { |
| 1142 | isn_T *isn; |
| 1143 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1144 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1145 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1146 | return FAIL; |
| 1147 | isn->isn_arg.job = job; |
| 1148 | |
| 1149 | return OK; |
| 1150 | } |
| 1151 | |
| 1152 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1153 | * Generate an ISN_PUSHBLOB instruction. |
| 1154 | * Consumes "blob". |
| 1155 | */ |
| 1156 | static int |
| 1157 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1158 | { |
| 1159 | isn_T *isn; |
| 1160 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1161 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1162 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1163 | return FAIL; |
| 1164 | isn->isn_arg.blob = blob; |
| 1165 | |
| 1166 | return OK; |
| 1167 | } |
| 1168 | |
| 1169 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1170 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1171 | * Consumes "name". |
| 1172 | */ |
| 1173 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1174 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1175 | { |
| 1176 | isn_T *isn; |
| 1177 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1178 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1179 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1180 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 1181 | isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1182 | |
| 1183 | return OK; |
| 1184 | } |
| 1185 | |
| 1186 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1187 | * Generate an ISN_GETITEM instruction with "index". |
| 1188 | */ |
| 1189 | static int |
| 1190 | generate_GETITEM(cctx_T *cctx, int index) |
| 1191 | { |
| 1192 | isn_T *isn; |
| 1193 | garray_T *stack = &cctx->ctx_type_stack; |
| 1194 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1195 | type_T *item_type = &t_any; |
| 1196 | |
| 1197 | RETURN_OK_IF_SKIP(cctx); |
| 1198 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1199 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1200 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1201 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1202 | emsg(_(e_listreq)); |
| 1203 | return FAIL; |
| 1204 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1205 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1206 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1207 | return FAIL; |
| 1208 | isn->isn_arg.number = index; |
| 1209 | |
| 1210 | // add the item type to the type stack |
| 1211 | if (ga_grow(stack, 1) == FAIL) |
| 1212 | return FAIL; |
| 1213 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 1214 | ++stack->ga_len; |
| 1215 | return OK; |
| 1216 | } |
| 1217 | |
| 1218 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 1219 | * Generate an ISN_SLICE instruction with "count". |
| 1220 | */ |
| 1221 | static int |
| 1222 | generate_SLICE(cctx_T *cctx, int count) |
| 1223 | { |
| 1224 | isn_T *isn; |
| 1225 | |
| 1226 | RETURN_OK_IF_SKIP(cctx); |
| 1227 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1228 | return FAIL; |
| 1229 | isn->isn_arg.number = count; |
| 1230 | return OK; |
| 1231 | } |
| 1232 | |
| 1233 | /* |
| 1234 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1235 | */ |
| 1236 | static int |
| 1237 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1238 | { |
| 1239 | isn_T *isn; |
| 1240 | |
| 1241 | RETURN_OK_IF_SKIP(cctx); |
| 1242 | |
| 1243 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1244 | return FAIL; |
| 1245 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1246 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1247 | |
| 1248 | return OK; |
| 1249 | } |
| 1250 | |
| 1251 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1252 | * Generate an ISN_STORE instruction. |
| 1253 | */ |
| 1254 | static int |
| 1255 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1256 | { |
| 1257 | isn_T *isn; |
| 1258 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1259 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1260 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1261 | return FAIL; |
| 1262 | if (name != NULL) |
| 1263 | isn->isn_arg.string = vim_strsave(name); |
| 1264 | else |
| 1265 | isn->isn_arg.number = idx; |
| 1266 | |
| 1267 | return OK; |
| 1268 | } |
| 1269 | |
| 1270 | /* |
| 1271 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1272 | */ |
| 1273 | static int |
| 1274 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1275 | { |
| 1276 | isn_T *isn; |
| 1277 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1278 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1279 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1280 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1281 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1282 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1283 | |
| 1284 | return OK; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * Generate an ISN_STOREOPT instruction |
| 1289 | */ |
| 1290 | static int |
| 1291 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1292 | { |
| 1293 | isn_T *isn; |
| 1294 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1295 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1296 | if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL) |
| 1297 | return FAIL; |
| 1298 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1299 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1300 | |
| 1301 | return OK; |
| 1302 | } |
| 1303 | |
| 1304 | /* |
| 1305 | * Generate an ISN_LOAD or similar instruction. |
| 1306 | */ |
| 1307 | static int |
| 1308 | generate_LOAD( |
| 1309 | cctx_T *cctx, |
| 1310 | isntype_T isn_type, |
| 1311 | int idx, |
| 1312 | char_u *name, |
| 1313 | type_T *type) |
| 1314 | { |
| 1315 | isn_T *isn; |
| 1316 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1317 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1318 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1319 | return FAIL; |
| 1320 | if (name != NULL) |
| 1321 | isn->isn_arg.string = vim_strsave(name); |
| 1322 | else |
| 1323 | isn->isn_arg.number = idx; |
| 1324 | |
| 1325 | return OK; |
| 1326 | } |
| 1327 | |
| 1328 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1329 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1330 | */ |
| 1331 | static int |
| 1332 | generate_LOADV( |
| 1333 | cctx_T *cctx, |
| 1334 | char_u *name, |
| 1335 | int error) |
| 1336 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1337 | int di_flags; |
| 1338 | int vidx = find_vim_var(name, &di_flags); |
| 1339 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1340 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1341 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1342 | if (vidx < 0) |
| 1343 | { |
| 1344 | if (error) |
| 1345 | semsg(_(e_var_notfound), name); |
| 1346 | return FAIL; |
| 1347 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1348 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1349 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1350 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1354 | * Generate an ISN_UNLET instruction. |
| 1355 | */ |
| 1356 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1357 | 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] | 1358 | { |
| 1359 | isn_T *isn; |
| 1360 | |
| 1361 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1362 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1363 | return FAIL; |
| 1364 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1365 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1366 | |
| 1367 | return OK; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1371 | * Generate an ISN_LOADS instruction. |
| 1372 | */ |
| 1373 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1374 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1375 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1376 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1377 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1378 | int sid, |
| 1379 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1380 | { |
| 1381 | isn_T *isn; |
| 1382 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1383 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1384 | if (isn_type == ISN_LOADS) |
| 1385 | isn = generate_instr_type(cctx, isn_type, type); |
| 1386 | else |
| 1387 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1388 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1389 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1390 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1391 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1392 | |
| 1393 | return OK; |
| 1394 | } |
| 1395 | |
| 1396 | /* |
| 1397 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1398 | */ |
| 1399 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1400 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1401 | cctx_T *cctx, |
| 1402 | isntype_T isn_type, |
| 1403 | int sid, |
| 1404 | int idx, |
| 1405 | type_T *type) |
| 1406 | { |
| 1407 | isn_T *isn; |
| 1408 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1409 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1410 | if (isn_type == ISN_LOADSCRIPT) |
| 1411 | isn = generate_instr_type(cctx, isn_type, type); |
| 1412 | else |
| 1413 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1414 | if (isn == NULL) |
| 1415 | return FAIL; |
| 1416 | isn->isn_arg.script.script_sid = sid; |
| 1417 | isn->isn_arg.script.script_idx = idx; |
| 1418 | return OK; |
| 1419 | } |
| 1420 | |
| 1421 | /* |
| 1422 | * Generate an ISN_NEWLIST instruction. |
| 1423 | */ |
| 1424 | static int |
| 1425 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1426 | { |
| 1427 | isn_T *isn; |
| 1428 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1429 | type_T *type; |
| 1430 | type_T *member; |
| 1431 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1432 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1433 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1434 | return FAIL; |
| 1435 | isn->isn_arg.number = count; |
| 1436 | |
| 1437 | // drop the value types |
| 1438 | stack->ga_len -= count; |
| 1439 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1440 | // 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] | 1441 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1442 | if (count > 0) |
| 1443 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1444 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1445 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1446 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1447 | |
| 1448 | // add the list type to the type stack |
| 1449 | if (ga_grow(stack, 1) == FAIL) |
| 1450 | return FAIL; |
| 1451 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1452 | ++stack->ga_len; |
| 1453 | |
| 1454 | return OK; |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * Generate an ISN_NEWDICT instruction. |
| 1459 | */ |
| 1460 | static int |
| 1461 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1462 | { |
| 1463 | isn_T *isn; |
| 1464 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1465 | type_T *type; |
| 1466 | type_T *member; |
| 1467 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1468 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1469 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1470 | return FAIL; |
| 1471 | isn->isn_arg.number = count; |
| 1472 | |
| 1473 | // drop the key and value types |
| 1474 | stack->ga_len -= 2 * count; |
| 1475 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1476 | // Use the first value type for the list member type. Use "void" for an |
| 1477 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1478 | if (count > 0) |
| 1479 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1480 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1481 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1482 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1483 | |
| 1484 | // add the dict type to the type stack |
| 1485 | if (ga_grow(stack, 1) == FAIL) |
| 1486 | return FAIL; |
| 1487 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1488 | ++stack->ga_len; |
| 1489 | |
| 1490 | return OK; |
| 1491 | } |
| 1492 | |
| 1493 | /* |
| 1494 | * Generate an ISN_FUNCREF instruction. |
| 1495 | */ |
| 1496 | static int |
| 1497 | generate_FUNCREF(cctx_T *cctx, int dfunc_idx) |
| 1498 | { |
| 1499 | isn_T *isn; |
| 1500 | garray_T *stack = &cctx->ctx_type_stack; |
| 1501 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1502 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1503 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1504 | return FAIL; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1505 | isn->isn_arg.funcref.fr_func = dfunc_idx; |
| 1506 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1507 | |
| 1508 | if (ga_grow(stack, 1) == FAIL) |
| 1509 | return FAIL; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1510 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1511 | // TODO: argument and return types |
| 1512 | ++stack->ga_len; |
| 1513 | |
| 1514 | return OK; |
| 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * Generate an ISN_JUMP instruction. |
| 1519 | */ |
| 1520 | static int |
| 1521 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1522 | { |
| 1523 | isn_T *isn; |
| 1524 | garray_T *stack = &cctx->ctx_type_stack; |
| 1525 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1526 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1527 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1528 | return FAIL; |
| 1529 | isn->isn_arg.jump.jump_when = when; |
| 1530 | isn->isn_arg.jump.jump_where = where; |
| 1531 | |
| 1532 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1533 | --stack->ga_len; |
| 1534 | |
| 1535 | return OK; |
| 1536 | } |
| 1537 | |
| 1538 | static int |
| 1539 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1540 | { |
| 1541 | isn_T *isn; |
| 1542 | garray_T *stack = &cctx->ctx_type_stack; |
| 1543 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1544 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1545 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1546 | return FAIL; |
| 1547 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1548 | |
| 1549 | if (ga_grow(stack, 1) == FAIL) |
| 1550 | return FAIL; |
| 1551 | // type doesn't matter, will be stored next |
| 1552 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1553 | ++stack->ga_len; |
| 1554 | |
| 1555 | return OK; |
| 1556 | } |
| 1557 | |
| 1558 | /* |
| 1559 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1560 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1561 | * Return FAIL if the number of arguments is wrong. |
| 1562 | */ |
| 1563 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1564 | 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] | 1565 | { |
| 1566 | isn_T *isn; |
| 1567 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1568 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1569 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1570 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1571 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1572 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1573 | argoff = check_internal_func(func_idx, argcount); |
| 1574 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1575 | return FAIL; |
| 1576 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1577 | if (method_call && argoff > 1) |
| 1578 | { |
| 1579 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1580 | return FAIL; |
| 1581 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1582 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1583 | } |
| 1584 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1585 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1586 | return FAIL; |
| 1587 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1588 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1589 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1590 | for (i = 0; i < argcount; ++i) |
| 1591 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1592 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1593 | stack->ga_len -= argcount; // drop the arguments |
| 1594 | if (ga_grow(stack, 1) == FAIL) |
| 1595 | return FAIL; |
| 1596 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1597 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1598 | ++stack->ga_len; // add return value |
| 1599 | |
| 1600 | return OK; |
| 1601 | } |
| 1602 | |
| 1603 | /* |
| 1604 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1605 | * Return FAIL if the number of arguments is wrong. |
| 1606 | */ |
| 1607 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1608 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1609 | { |
| 1610 | isn_T *isn; |
| 1611 | garray_T *stack = &cctx->ctx_type_stack; |
| 1612 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1613 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1614 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1615 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1616 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1617 | { |
| 1618 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1619 | return FAIL; |
| 1620 | } |
| 1621 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1622 | { |
| 1623 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1624 | return FAIL; |
| 1625 | } |
| 1626 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1627 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1628 | { |
| 1629 | int i; |
| 1630 | |
| 1631 | for (i = 0; i < argcount; ++i) |
| 1632 | { |
| 1633 | type_T *expected; |
| 1634 | type_T *actual; |
| 1635 | |
| 1636 | if (i < regular_args) |
| 1637 | { |
| 1638 | if (ufunc->uf_arg_types == NULL) |
| 1639 | continue; |
| 1640 | expected = ufunc->uf_arg_types[i]; |
| 1641 | } |
| 1642 | else |
| 1643 | expected = ufunc->uf_va_type->tt_member; |
| 1644 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1645 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1646 | { |
| 1647 | arg_type_mismatch(expected, actual, i + 1); |
| 1648 | return FAIL; |
| 1649 | } |
| 1650 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1651 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 1652 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1653 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1654 | } |
| 1655 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1656 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1657 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1658 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1659 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1660 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1661 | { |
| 1662 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1663 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1664 | } |
| 1665 | else |
| 1666 | { |
| 1667 | // A user function may be deleted and redefined later, can't use the |
| 1668 | // ufunc pointer, need to look it up again at runtime. |
| 1669 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1670 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1671 | } |
| 1672 | |
| 1673 | stack->ga_len -= argcount; // drop the arguments |
| 1674 | if (ga_grow(stack, 1) == FAIL) |
| 1675 | return FAIL; |
| 1676 | // add return value |
| 1677 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1678 | ++stack->ga_len; |
| 1679 | |
| 1680 | return OK; |
| 1681 | } |
| 1682 | |
| 1683 | /* |
| 1684 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1685 | */ |
| 1686 | static int |
| 1687 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1688 | { |
| 1689 | isn_T *isn; |
| 1690 | garray_T *stack = &cctx->ctx_type_stack; |
| 1691 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1692 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1693 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1694 | return FAIL; |
| 1695 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1696 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1697 | |
| 1698 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1699 | if (ga_grow(stack, 1) == FAIL) |
| 1700 | return FAIL; |
| 1701 | // add return value |
| 1702 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1703 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1704 | |
| 1705 | return OK; |
| 1706 | } |
| 1707 | |
| 1708 | /* |
| 1709 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1710 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1711 | */ |
| 1712 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1713 | generate_PCALL( |
| 1714 | cctx_T *cctx, |
| 1715 | int argcount, |
| 1716 | char_u *name, |
| 1717 | type_T *type, |
| 1718 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1719 | { |
| 1720 | isn_T *isn; |
| 1721 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1722 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1723 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1724 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1725 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1726 | if (type->tt_type == VAR_ANY) |
| 1727 | ret_type = &t_any; |
| 1728 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1729 | { |
| 1730 | if (type->tt_argcount != -1) |
| 1731 | { |
| 1732 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1733 | |
| 1734 | if (argcount < type->tt_min_argcount - varargs) |
| 1735 | { |
| 1736 | semsg(_(e_toofewarg), "[reference]"); |
| 1737 | return FAIL; |
| 1738 | } |
| 1739 | if (!varargs && argcount > type->tt_argcount) |
| 1740 | { |
| 1741 | semsg(_(e_toomanyarg), "[reference]"); |
| 1742 | return FAIL; |
| 1743 | } |
| 1744 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1745 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1746 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1747 | else |
| 1748 | { |
| 1749 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1750 | return FAIL; |
| 1751 | } |
| 1752 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1753 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1754 | return FAIL; |
| 1755 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1756 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1757 | |
| 1758 | stack->ga_len -= argcount; // drop the arguments |
| 1759 | |
| 1760 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1761 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1762 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1763 | // If partial is above the arguments it must be cleared and replaced with |
| 1764 | // the return value. |
| 1765 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1766 | return FAIL; |
| 1767 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1768 | return OK; |
| 1769 | } |
| 1770 | |
| 1771 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1772 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1773 | */ |
| 1774 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1775 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1776 | { |
| 1777 | isn_T *isn; |
| 1778 | garray_T *stack = &cctx->ctx_type_stack; |
| 1779 | type_T *type; |
| 1780 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1781 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1782 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1783 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1784 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1785 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1786 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1787 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1788 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1789 | { |
| 1790 | emsg(_(e_dictreq)); |
| 1791 | return FAIL; |
| 1792 | } |
| 1793 | // change dict type to dict member type |
| 1794 | if (type->tt_type == VAR_DICT) |
| 1795 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1796 | |
| 1797 | return OK; |
| 1798 | } |
| 1799 | |
| 1800 | /* |
| 1801 | * Generate an ISN_ECHO instruction. |
| 1802 | */ |
| 1803 | static int |
| 1804 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1805 | { |
| 1806 | isn_T *isn; |
| 1807 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1808 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1809 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1810 | return FAIL; |
| 1811 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1812 | isn->isn_arg.echo.echo_count = count; |
| 1813 | |
| 1814 | return OK; |
| 1815 | } |
| 1816 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1817 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1818 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1819 | */ |
| 1820 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1821 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1822 | { |
| 1823 | isn_T *isn; |
| 1824 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1825 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1826 | return FAIL; |
| 1827 | isn->isn_arg.number = count; |
| 1828 | |
| 1829 | return OK; |
| 1830 | } |
| 1831 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1832 | static int |
| 1833 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1834 | { |
| 1835 | isn_T *isn; |
| 1836 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1837 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1838 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1839 | return FAIL; |
| 1840 | isn->isn_arg.string = vim_strsave(line); |
| 1841 | return OK; |
| 1842 | } |
| 1843 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1844 | static int |
| 1845 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1846 | { |
| 1847 | isn_T *isn; |
| 1848 | |
| 1849 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1850 | return FAIL; |
| 1851 | isn->isn_arg.number = count; |
| 1852 | return OK; |
| 1853 | } |
| 1854 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1855 | /* |
| 1856 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1857 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1858 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1859 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1860 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1861 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1862 | lvar_T *lvar; |
| 1863 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1864 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1865 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1866 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1867 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1871 | return NULL; |
| 1872 | 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] | 1873 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1874 | // Every local variable uses the next entry on the stack. We could re-use |
| 1875 | // the last ones when leaving a scope, but then variables used in a closure |
| 1876 | // might get overwritten. To keep things simple do not re-use stack |
| 1877 | // entries. This is less efficient, but memory is cheap these days. |
| 1878 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1879 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1880 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1881 | lvar->lv_const = isConst; |
| 1882 | lvar->lv_type = type; |
| 1883 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1884 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1888 | * Remove local variables above "new_top". |
| 1889 | */ |
| 1890 | static void |
| 1891 | unwind_locals(cctx_T *cctx, int new_top) |
| 1892 | { |
| 1893 | if (cctx->ctx_locals.ga_len > new_top) |
| 1894 | { |
| 1895 | int idx; |
| 1896 | lvar_T *lvar; |
| 1897 | |
| 1898 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1899 | { |
| 1900 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1901 | vim_free(lvar->lv_name); |
| 1902 | } |
| 1903 | } |
| 1904 | cctx->ctx_locals.ga_len = new_top; |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * Free all local variables. |
| 1909 | */ |
| 1910 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1911 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1912 | { |
| 1913 | unwind_locals(cctx, 0); |
| 1914 | ga_clear(&cctx->ctx_locals); |
| 1915 | } |
| 1916 | |
| 1917 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1918 | * Skip over a type definition and return a pointer to just after it. |
| 1919 | */ |
| 1920 | char_u * |
| 1921 | skip_type(char_u *start) |
| 1922 | { |
| 1923 | char_u *p = start; |
| 1924 | |
| 1925 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1926 | ++p; |
| 1927 | |
| 1928 | // Skip over "<type>"; this is permissive about white space. |
| 1929 | if (*skipwhite(p) == '<') |
| 1930 | { |
| 1931 | p = skipwhite(p); |
| 1932 | p = skip_type(skipwhite(p + 1)); |
| 1933 | p = skipwhite(p); |
| 1934 | if (*p == '>') |
| 1935 | ++p; |
| 1936 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1937 | else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1]))) |
| 1938 | && STRNCMP("func", start, 4) == 0) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1939 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1940 | if (*p == '(') |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1941 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1942 | // handle func(args): type |
| 1943 | ++p; |
| 1944 | while (*p != ')' && *p != NUL) |
| 1945 | { |
| 1946 | char_u *sp = p; |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1947 | |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1948 | p = skip_type(p); |
| 1949 | if (p == sp) |
| 1950 | return p; // syntax error |
| 1951 | if (*p == ',') |
| 1952 | p = skipwhite(p + 1); |
| 1953 | } |
| 1954 | if (*p == ')') |
| 1955 | { |
| 1956 | if (p[1] == ':') |
| 1957 | p = skip_type(skipwhite(p + 2)); |
| 1958 | else |
Bram Moolenaar | bfba865 | 2020-07-23 20:09:10 +0200 | [diff] [blame] | 1959 | ++p; |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1960 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1961 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1962 | else |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1963 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1964 | // handle func: return_type |
| 1965 | p = skip_type(skipwhite(p + 1)); |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1966 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1967 | } |
| 1968 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1969 | return p; |
| 1970 | } |
| 1971 | |
| 1972 | /* |
| 1973 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1974 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1975 | * Returns NULL in case of failure. |
| 1976 | */ |
| 1977 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1978 | 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] | 1979 | { |
| 1980 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1981 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1982 | |
| 1983 | if (**arg != '<') |
| 1984 | { |
| 1985 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1986 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1987 | else |
| 1988 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1989 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1990 | } |
| 1991 | *arg = skipwhite(*arg + 1); |
| 1992 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1993 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1994 | |
| 1995 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1996 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1997 | { |
| 1998 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1999 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2000 | } |
| 2001 | ++*arg; |
| 2002 | |
| 2003 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2004 | return get_list_type(member_type, type_gap); |
| 2005 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 2010 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2011 | */ |
| 2012 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2013 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2014 | { |
| 2015 | char_u *p = *arg; |
| 2016 | size_t len; |
| 2017 | |
| 2018 | // skip over the first word |
| 2019 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 2020 | ++p; |
| 2021 | len = p - *arg; |
| 2022 | |
| 2023 | switch (**arg) |
| 2024 | { |
| 2025 | case 'a': |
| 2026 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 2027 | { |
| 2028 | *arg += len; |
| 2029 | return &t_any; |
| 2030 | } |
| 2031 | break; |
| 2032 | case 'b': |
| 2033 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 2034 | { |
| 2035 | *arg += len; |
| 2036 | return &t_bool; |
| 2037 | } |
| 2038 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 2039 | { |
| 2040 | *arg += len; |
| 2041 | return &t_blob; |
| 2042 | } |
| 2043 | break; |
| 2044 | case 'c': |
| 2045 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 2046 | { |
| 2047 | *arg += len; |
| 2048 | return &t_channel; |
| 2049 | } |
| 2050 | break; |
| 2051 | case 'd': |
| 2052 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 2053 | { |
| 2054 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2055 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2056 | } |
| 2057 | break; |
| 2058 | case 'f': |
| 2059 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 2060 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2061 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2062 | *arg += len; |
| 2063 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2064 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2065 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2066 | return &t_any; |
| 2067 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2068 | } |
| 2069 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 2070 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2071 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2072 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2073 | int argcount = -1; |
| 2074 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2075 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2076 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 2077 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2078 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2079 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2080 | if (**arg == '(') |
| 2081 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2082 | // "func" may or may not return a value, "func()" does |
| 2083 | // not return a value. |
| 2084 | ret_type = &t_void; |
| 2085 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2086 | p = ++*arg; |
| 2087 | argcount = 0; |
| 2088 | while (*p != NUL && *p != ')') |
| 2089 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2090 | if (*p == '?') |
| 2091 | { |
| 2092 | if (first_optional == -1) |
| 2093 | first_optional = argcount; |
| 2094 | ++p; |
| 2095 | } |
| 2096 | else if (first_optional != -1) |
| 2097 | { |
| 2098 | emsg(_("E1007: mandatory argument after optional argument")); |
| 2099 | return &t_any; |
| 2100 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2101 | else if (STRNCMP(p, "...", 3) == 0) |
| 2102 | { |
| 2103 | flags |= TTFLAG_VARARGS; |
| 2104 | p += 3; |
| 2105 | } |
| 2106 | |
| 2107 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 2108 | |
| 2109 | // Nothing comes after "...{type}". |
| 2110 | if (flags & TTFLAG_VARARGS) |
| 2111 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2112 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2113 | if (*p != ',' && *skipwhite(p) == ',') |
| 2114 | { |
| 2115 | semsg(_(e_no_white_before), ","); |
| 2116 | return &t_any; |
| 2117 | } |
| 2118 | if (*p == ',') |
| 2119 | { |
| 2120 | ++p; |
| 2121 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2122 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2123 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2124 | return &t_any; |
| 2125 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2126 | } |
| 2127 | p = skipwhite(p); |
| 2128 | if (argcount == MAX_FUNC_ARGS) |
| 2129 | { |
| 2130 | emsg(_("E740: Too many argument types")); |
| 2131 | return &t_any; |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | p = skipwhite(p); |
| 2136 | if (*p != ')') |
| 2137 | { |
| 2138 | emsg(_(e_missing_close)); |
| 2139 | return &t_any; |
| 2140 | } |
| 2141 | *arg = p + 1; |
| 2142 | } |
| 2143 | if (**arg == ':') |
| 2144 | { |
| 2145 | // parse return type |
| 2146 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2147 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2148 | semsg(_(e_white_after), ":"); |
| 2149 | *arg = skipwhite(*arg); |
| 2150 | ret_type = parse_type(arg, type_gap); |
| 2151 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2152 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2153 | type = get_func_type(ret_type, argcount, type_gap); |
| 2154 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2155 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2156 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 2157 | type->tt_flags = flags; |
| 2158 | if (argcount > 0) |
| 2159 | { |
| 2160 | type->tt_argcount = argcount; |
| 2161 | type->tt_min_argcount = first_optional == -1 |
| 2162 | ? argcount : first_optional; |
| 2163 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2164 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2165 | return &t_any; |
| 2166 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2167 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2168 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2169 | } |
| 2170 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2171 | } |
| 2172 | break; |
| 2173 | case 'j': |
| 2174 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 2175 | { |
| 2176 | *arg += len; |
| 2177 | return &t_job; |
| 2178 | } |
| 2179 | break; |
| 2180 | case 'l': |
| 2181 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 2182 | { |
| 2183 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2184 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2185 | } |
| 2186 | break; |
| 2187 | case 'n': |
| 2188 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 2189 | { |
| 2190 | *arg += len; |
| 2191 | return &t_number; |
| 2192 | } |
| 2193 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2194 | case 's': |
| 2195 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 2196 | { |
| 2197 | *arg += len; |
| 2198 | return &t_string; |
| 2199 | } |
| 2200 | break; |
| 2201 | case 'v': |
| 2202 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 2203 | { |
| 2204 | *arg += len; |
| 2205 | return &t_void; |
| 2206 | } |
| 2207 | break; |
| 2208 | } |
| 2209 | |
| 2210 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 2211 | return &t_any; |
| 2212 | } |
| 2213 | |
| 2214 | /* |
| 2215 | * Check if "type1" and "type2" are exactly the same. |
| 2216 | */ |
| 2217 | static int |
| 2218 | equal_type(type_T *type1, type_T *type2) |
| 2219 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2220 | int i; |
| 2221 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2222 | if (type1->tt_type != type2->tt_type) |
| 2223 | return FALSE; |
| 2224 | switch (type1->tt_type) |
| 2225 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2226 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2227 | case VAR_ANY: |
| 2228 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2229 | case VAR_SPECIAL: |
| 2230 | case VAR_BOOL: |
| 2231 | case VAR_NUMBER: |
| 2232 | case VAR_FLOAT: |
| 2233 | case VAR_STRING: |
| 2234 | case VAR_BLOB: |
| 2235 | case VAR_JOB: |
| 2236 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2237 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2238 | case VAR_LIST: |
| 2239 | case VAR_DICT: |
| 2240 | return equal_type(type1->tt_member, type2->tt_member); |
| 2241 | case VAR_FUNC: |
| 2242 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2243 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2244 | || type1->tt_argcount != type2->tt_argcount) |
| 2245 | return FALSE; |
| 2246 | if (type1->tt_argcount < 0 |
| 2247 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2248 | return TRUE; |
| 2249 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2250 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2251 | return FALSE; |
| 2252 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2253 | } |
| 2254 | return TRUE; |
| 2255 | } |
| 2256 | |
| 2257 | /* |
| 2258 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2259 | * "type2" and "dest" may be the same. |
| 2260 | */ |
| 2261 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2262 | 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] | 2263 | { |
| 2264 | if (equal_type(type1, type2)) |
| 2265 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2266 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2267 | return; |
| 2268 | } |
| 2269 | |
| 2270 | if (type1->tt_type == type2->tt_type) |
| 2271 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2272 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2273 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2274 | type_T *common; |
| 2275 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2276 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2277 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2278 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2279 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2280 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2281 | return; |
| 2282 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2283 | if (type1->tt_type == VAR_FUNC) |
| 2284 | { |
| 2285 | type_T *common; |
| 2286 | |
| 2287 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2288 | if (type1->tt_argcount == type2->tt_argcount |
| 2289 | && type1->tt_argcount >= 0) |
| 2290 | { |
| 2291 | int argcount = type1->tt_argcount; |
| 2292 | int i; |
| 2293 | |
| 2294 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2295 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2296 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2297 | if (func_type_add_arg_types(*dest, argcount, |
| 2298 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2299 | for (i = 0; i < argcount; ++i) |
| 2300 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2301 | &(*dest)->tt_args[i], type_gap); |
| 2302 | } |
| 2303 | } |
| 2304 | else |
| 2305 | *dest = alloc_func_type(common, -1, type_gap); |
| 2306 | return; |
| 2307 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2308 | } |
| 2309 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2310 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | char * |
| 2314 | vartype_name(vartype_T type) |
| 2315 | { |
| 2316 | switch (type) |
| 2317 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2318 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2319 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2320 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2321 | case VAR_SPECIAL: return "special"; |
| 2322 | case VAR_BOOL: return "bool"; |
| 2323 | case VAR_NUMBER: return "number"; |
| 2324 | case VAR_FLOAT: return "float"; |
| 2325 | case VAR_STRING: return "string"; |
| 2326 | case VAR_BLOB: return "blob"; |
| 2327 | case VAR_JOB: return "job"; |
| 2328 | case VAR_CHANNEL: return "channel"; |
| 2329 | case VAR_LIST: return "list"; |
| 2330 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2331 | |
| 2332 | case VAR_FUNC: |
| 2333 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2334 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2335 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | /* |
| 2339 | * Return the name of a type. |
| 2340 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2341 | */ |
| 2342 | char * |
| 2343 | type_name(type_T *type, char **tofree) |
| 2344 | { |
| 2345 | char *name = vartype_name(type->tt_type); |
| 2346 | |
| 2347 | *tofree = NULL; |
| 2348 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2349 | { |
| 2350 | char *member_free; |
| 2351 | char *member_name = type_name(type->tt_member, &member_free); |
| 2352 | size_t len; |
| 2353 | |
| 2354 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2355 | *tofree = alloc(len); |
| 2356 | if (*tofree != NULL) |
| 2357 | { |
| 2358 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2359 | vim_free(member_free); |
| 2360 | return *tofree; |
| 2361 | } |
| 2362 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2363 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2364 | { |
| 2365 | garray_T ga; |
| 2366 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2367 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2368 | |
| 2369 | ga_init2(&ga, 1, 100); |
| 2370 | if (ga_grow(&ga, 20) == FAIL) |
| 2371 | return "[unknown]"; |
| 2372 | *tofree = ga.ga_data; |
| 2373 | STRCPY(ga.ga_data, "func("); |
| 2374 | ga.ga_len += 5; |
| 2375 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2376 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2377 | { |
| 2378 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2379 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2380 | int len; |
| 2381 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2382 | if (type->tt_args == NULL) |
| 2383 | arg_type = "[unknown]"; |
| 2384 | else |
| 2385 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2386 | if (i > 0) |
| 2387 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2388 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2389 | ga.ga_len += 2; |
| 2390 | } |
| 2391 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2392 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2393 | { |
| 2394 | vim_free(arg_free); |
| 2395 | return "[unknown]"; |
| 2396 | } |
| 2397 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2398 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2399 | { |
| 2400 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2401 | ga.ga_len += 3; |
| 2402 | } |
| 2403 | else if (i >= type->tt_min_argcount) |
| 2404 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2405 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2406 | ga.ga_len += len; |
| 2407 | vim_free(arg_free); |
| 2408 | } |
| 2409 | |
| 2410 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2411 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2412 | else |
| 2413 | { |
| 2414 | char *ret_free; |
| 2415 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2416 | int len; |
| 2417 | |
| 2418 | len = (int)STRLEN(ret_name) + 4; |
| 2419 | if (ga_grow(&ga, len) == FAIL) |
| 2420 | { |
| 2421 | vim_free(ret_free); |
| 2422 | return "[unknown]"; |
| 2423 | } |
| 2424 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2425 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2426 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2427 | vim_free(ret_free); |
| 2428 | } |
| 2429 | return ga.ga_data; |
| 2430 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2431 | |
| 2432 | return name; |
| 2433 | } |
| 2434 | |
| 2435 | /* |
| 2436 | * Find "name" in script-local items of script "sid". |
| 2437 | * Returns the index in "sn_var_vals" if found. |
| 2438 | * If found but not in "sn_var_vals" returns -1. |
| 2439 | * If not found returns -2. |
| 2440 | */ |
| 2441 | int |
| 2442 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2443 | { |
| 2444 | hashtab_T *ht; |
| 2445 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2446 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2447 | int idx; |
| 2448 | |
| 2449 | // First look the name up in the hashtable. |
| 2450 | if (sid <= 0 || sid > script_items.ga_len) |
| 2451 | return -1; |
| 2452 | ht = &SCRIPT_VARS(sid); |
| 2453 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2454 | if (di == NULL) |
| 2455 | return -2; |
| 2456 | |
| 2457 | // Now find the svar_T index in sn_var_vals. |
| 2458 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2459 | { |
| 2460 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2461 | |
| 2462 | if (sv->sv_tv == &di->di_tv) |
| 2463 | { |
| 2464 | if (check_writable && sv->sv_const) |
| 2465 | semsg(_(e_readonlyvar), name); |
| 2466 | return idx; |
| 2467 | } |
| 2468 | } |
| 2469 | return -1; |
| 2470 | } |
| 2471 | |
| 2472 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2473 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2474 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2475 | */ |
| 2476 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2477 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2478 | { |
Bram Moolenaar | 086eb18 | 2020-07-01 16:00:44 +0200 | [diff] [blame] | 2479 | scriptitem_T *si; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2480 | int idx; |
| 2481 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2482 | if (current_sctx.sc_sid <= 0) |
| 2483 | return NULL; |
| 2484 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2485 | if (cctx != NULL) |
| 2486 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2487 | { |
| 2488 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2489 | + idx; |
| 2490 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2491 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2492 | : STRLEN(import->imp_name) == len |
| 2493 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2494 | return import; |
| 2495 | } |
| 2496 | |
| 2497 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2498 | { |
| 2499 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2500 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2501 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2502 | : STRLEN(import->imp_name) == len |
| 2503 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2504 | return import; |
| 2505 | } |
| 2506 | return NULL; |
| 2507 | } |
| 2508 | |
| 2509 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2510 | * Free all imported variables. |
| 2511 | */ |
| 2512 | static void |
| 2513 | free_imported(cctx_T *cctx) |
| 2514 | { |
| 2515 | int idx; |
| 2516 | |
| 2517 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2518 | { |
| 2519 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2520 | |
| 2521 | vim_free(import->imp_name); |
| 2522 | } |
| 2523 | ga_clear(&cctx->ctx_imports); |
| 2524 | } |
| 2525 | |
| 2526 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2527 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2528 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 2529 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2530 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2531 | { |
| 2532 | return p[0] == '#' && p[1] != '{'; |
| 2533 | } |
| 2534 | |
| 2535 | /* |
| 2536 | * Return a pointer to the next line that isn't empty or only contains a |
| 2537 | * comment. Skips over white space. |
| 2538 | * Returns NULL if there is none. |
| 2539 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2540 | char_u * |
| 2541 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2542 | { |
| 2543 | int lnum = cctx->ctx_lnum; |
| 2544 | |
| 2545 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2546 | { |
| 2547 | 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] | 2548 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2549 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 2550 | if (line == NULL) |
| 2551 | break; |
| 2552 | p = skipwhite(line); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2553 | if (*p != NUL && !vim9_comment_start(p)) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2554 | return p; |
| 2555 | } |
| 2556 | return NULL; |
| 2557 | } |
| 2558 | |
| 2559 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2560 | * Called when checking for a following operator at "arg". When the rest of |
| 2561 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2562 | * line return a pointer to it and set "nextp". |
| 2563 | * Otherwise skip over white space. |
| 2564 | */ |
| 2565 | static char_u * |
| 2566 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2567 | { |
| 2568 | char_u *p = skipwhite(arg); |
| 2569 | |
| 2570 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2571 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2572 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2573 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2574 | if (*nextp != NULL) |
| 2575 | return *nextp; |
| 2576 | } |
| 2577 | return p; |
| 2578 | } |
| 2579 | |
| 2580 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2581 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2582 | * 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] | 2583 | * Returns NULL when at the end. |
| 2584 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2585 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2586 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2587 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2588 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2589 | |
| 2590 | do |
| 2591 | { |
| 2592 | ++cctx->ctx_lnum; |
| 2593 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2594 | { |
| 2595 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2596 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2597 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2598 | 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] | 2599 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2600 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2601 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2602 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2603 | return line; |
| 2604 | } |
| 2605 | |
| 2606 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2607 | * 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] | 2608 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2609 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2610 | */ |
| 2611 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2612 | 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] | 2613 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2614 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2615 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2616 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2617 | |
| 2618 | if (next == NULL) |
| 2619 | return FAIL; |
| 2620 | *arg = skipwhite(next); |
| 2621 | } |
| 2622 | return OK; |
| 2623 | } |
| 2624 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2625 | /* |
| 2626 | * Idem, and give an error when failed. |
| 2627 | */ |
| 2628 | static int |
| 2629 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2630 | { |
| 2631 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2632 | { |
| 2633 | emsg(_("E1097: line incomplete")); |
| 2634 | return FAIL; |
| 2635 | } |
| 2636 | return OK; |
| 2637 | } |
| 2638 | |
| 2639 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2640 | // Structure passed between the compile_expr* functions to keep track of |
| 2641 | // constants that have been parsed but for which no code was produced yet. If |
| 2642 | // possible expressions on these constants are applied at compile time. If |
| 2643 | // that is not possible, the code to push the constants needs to be generated |
| 2644 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2645 | // Using 50 should be more than enough of 5 levels of (). |
| 2646 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2647 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2648 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2649 | int pp_used; // active entries in pp_tv[] |
| 2650 | } ppconst_T; |
| 2651 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2652 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2653 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2654 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2655 | /* |
| 2656 | * Generate a PUSH instruction for "tv". |
| 2657 | * "tv" will be consumed or cleared. |
| 2658 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2659 | */ |
| 2660 | static int |
| 2661 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2662 | { |
| 2663 | if (tv != NULL) |
| 2664 | { |
| 2665 | switch (tv->v_type) |
| 2666 | { |
| 2667 | case VAR_UNKNOWN: |
| 2668 | break; |
| 2669 | case VAR_BOOL: |
| 2670 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2671 | break; |
| 2672 | case VAR_SPECIAL: |
| 2673 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2674 | break; |
| 2675 | case VAR_NUMBER: |
| 2676 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2677 | break; |
| 2678 | #ifdef FEAT_FLOAT |
| 2679 | case VAR_FLOAT: |
| 2680 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2681 | break; |
| 2682 | #endif |
| 2683 | case VAR_BLOB: |
| 2684 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2685 | tv->vval.v_blob = NULL; |
| 2686 | break; |
| 2687 | case VAR_STRING: |
| 2688 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2689 | tv->vval.v_string = NULL; |
| 2690 | break; |
| 2691 | default: |
| 2692 | iemsg("constant type not supported"); |
| 2693 | clear_tv(tv); |
| 2694 | return FAIL; |
| 2695 | } |
| 2696 | tv->v_type = VAR_UNKNOWN; |
| 2697 | } |
| 2698 | return OK; |
| 2699 | } |
| 2700 | |
| 2701 | /* |
| 2702 | * Generate code for any ppconst entries. |
| 2703 | */ |
| 2704 | static int |
| 2705 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2706 | { |
| 2707 | int i; |
| 2708 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2709 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2710 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2711 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2712 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2713 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2714 | ret = FAIL; |
| 2715 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2716 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2717 | return ret; |
| 2718 | } |
| 2719 | |
| 2720 | /* |
| 2721 | * Clear ppconst constants. Used when failing. |
| 2722 | */ |
| 2723 | static void |
| 2724 | clear_ppconst(ppconst_T *ppconst) |
| 2725 | { |
| 2726 | int i; |
| 2727 | |
| 2728 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2729 | clear_tv(&ppconst->pp_tv[i]); |
| 2730 | ppconst->pp_used = 0; |
| 2731 | } |
| 2732 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2733 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2734 | * Generate an instruction to load script-local variable "name", without the |
| 2735 | * leading "s:". |
| 2736 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2737 | */ |
| 2738 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2739 | compile_load_scriptvar( |
| 2740 | cctx_T *cctx, |
| 2741 | char_u *name, // variable NUL terminated |
| 2742 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2743 | char_u **end, // end of variable |
| 2744 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2745 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2746 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2747 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2748 | imported_T *import; |
| 2749 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2750 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2751 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2752 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2753 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2754 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2755 | } |
| 2756 | if (idx >= 0) |
| 2757 | { |
| 2758 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2759 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2760 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2761 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2762 | return OK; |
| 2763 | } |
| 2764 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2765 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2766 | if (import != NULL) |
| 2767 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2768 | if (import->imp_all) |
| 2769 | { |
| 2770 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2771 | char_u *exp_name; |
| 2772 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2773 | ufunc_T *ufunc; |
| 2774 | type_T *type; |
| 2775 | |
| 2776 | // Used "import * as Name", need to lookup the member. |
| 2777 | if (*p != '.') |
| 2778 | { |
| 2779 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2780 | return FAIL; |
| 2781 | } |
| 2782 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2783 | if (VIM_ISWHITE(*p)) |
| 2784 | { |
| 2785 | emsg(_("E1074: no white space allowed after dot")); |
| 2786 | return FAIL; |
| 2787 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2788 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2789 | // isolate one name |
| 2790 | exp_name = p; |
| 2791 | while (eval_isnamec(*p)) |
| 2792 | ++p; |
| 2793 | cc = *p; |
| 2794 | *p = NUL; |
| 2795 | |
| 2796 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2797 | *p = cc; |
| 2798 | p = skipwhite(p); |
| 2799 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2800 | // TODO: what if it is a function? |
| 2801 | if (idx < 0) |
| 2802 | return FAIL; |
| 2803 | *end = p; |
| 2804 | |
| 2805 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2806 | import->imp_sid, |
| 2807 | idx, |
| 2808 | type); |
| 2809 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2810 | else if (import->imp_funcname != NULL) |
| 2811 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2812 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2813 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2814 | import->imp_sid, |
| 2815 | import->imp_var_vals_idx, |
| 2816 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2817 | return OK; |
| 2818 | } |
| 2819 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2820 | if (error) |
| 2821 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2822 | return FAIL; |
| 2823 | } |
| 2824 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2825 | static int |
| 2826 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2827 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2828 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2829 | |
| 2830 | if (ufunc == NULL) |
| 2831 | return FAIL; |
| 2832 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2833 | // Need to compile any default values to get the argument types. |
| 2834 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2835 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2836 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2837 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2838 | } |
| 2839 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2840 | /* |
| 2841 | * Compile a variable name into a load instruction. |
| 2842 | * "end" points to just after the name. |
| 2843 | * When "error" is FALSE do not give an error when not found. |
| 2844 | */ |
| 2845 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2846 | 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] | 2847 | { |
| 2848 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2849 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2850 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2851 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2852 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2853 | |
| 2854 | if (*(*arg + 1) == ':') |
| 2855 | { |
| 2856 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2857 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2858 | { |
| 2859 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2860 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2861 | switch (**arg) |
| 2862 | { |
| 2863 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2864 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2865 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2866 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2867 | default: |
| 2868 | semsg(_(e_namespace), *arg); |
| 2869 | goto theend; |
| 2870 | } |
| 2871 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2872 | goto theend; |
| 2873 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2874 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2875 | else |
| 2876 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2877 | isntype_T isn_type = ISN_DROP; |
| 2878 | |
| 2879 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2880 | if (name == NULL) |
| 2881 | return FAIL; |
| 2882 | |
| 2883 | switch (**arg) |
| 2884 | { |
| 2885 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2886 | break; |
| 2887 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2888 | NULL, NULL, error); |
| 2889 | break; |
| 2890 | case 'g': isn_type = ISN_LOADG; break; |
| 2891 | case 'w': isn_type = ISN_LOADW; break; |
| 2892 | case 't': isn_type = ISN_LOADT; break; |
| 2893 | case 'b': isn_type = ISN_LOADB; break; |
| 2894 | default: semsg(_(e_namespace), *arg); |
| 2895 | goto theend; |
| 2896 | } |
| 2897 | if (isn_type != ISN_DROP) |
| 2898 | { |
| 2899 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2900 | // variables can be defined later, thus we don't check if it |
| 2901 | // exists, give error at runtime. |
| 2902 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2903 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2904 | } |
| 2905 | } |
| 2906 | else |
| 2907 | { |
| 2908 | size_t len = end - *arg; |
| 2909 | int idx; |
| 2910 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2911 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2912 | |
| 2913 | name = vim_strnsave(*arg, end - *arg); |
| 2914 | if (name == NULL) |
| 2915 | return FAIL; |
| 2916 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2917 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2918 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2919 | if (!gen_load_outer) |
| 2920 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2921 | } |
| 2922 | else |
| 2923 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2924 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2925 | |
| 2926 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2927 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2928 | type = lvar->lv_type; |
| 2929 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2930 | if (lvar->lv_from_outer) |
| 2931 | gen_load_outer = TRUE; |
| 2932 | else |
| 2933 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2934 | } |
| 2935 | else |
| 2936 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2937 | // "var" can be script-local even without using "s:" if it |
| 2938 | // already exists. |
| 2939 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2940 | == SCRIPT_VERSION_VIM9 |
| 2941 | || lookup_script(*arg, len) == OK) |
| 2942 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2943 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2944 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2945 | // When the name starts with an uppercase letter or "x:" it |
| 2946 | // can be a user defined function. |
| 2947 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2948 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2949 | } |
| 2950 | } |
| 2951 | if (gen_load) |
| 2952 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2953 | if (gen_load_outer) |
| 2954 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | *arg = end; |
| 2958 | |
| 2959 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2960 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2961 | semsg(_(e_var_notfound), name); |
| 2962 | vim_free(name); |
| 2963 | return res; |
| 2964 | } |
| 2965 | |
| 2966 | /* |
| 2967 | * Compile the argument expressions. |
| 2968 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2969 | */ |
| 2970 | static int |
| 2971 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2972 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2973 | char_u *p = *arg; |
| 2974 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2975 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2976 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2977 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2978 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2979 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2980 | if (*p == ')') |
| 2981 | { |
| 2982 | *arg = p + 1; |
| 2983 | return OK; |
| 2984 | } |
| 2985 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2986 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2987 | return FAIL; |
| 2988 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2989 | |
| 2990 | if (*p != ',' && *skipwhite(p) == ',') |
| 2991 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2992 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2993 | p = skipwhite(p); |
| 2994 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2995 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2996 | { |
| 2997 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2998 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2999 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3000 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3001 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3002 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3003 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3004 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 3005 | emsg(_(e_missing_close)); |
| 3006 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | /* |
| 3010 | * Compile a function call: name(arg1, arg2) |
| 3011 | * "arg" points to "name", "arg + varlen" to the "(". |
| 3012 | * "argcount_init" is 1 for "value->method()" |
| 3013 | * Instructions: |
| 3014 | * EVAL arg1 |
| 3015 | * EVAL arg2 |
| 3016 | * BCALL / DCALL / UCALL |
| 3017 | */ |
| 3018 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3019 | compile_call( |
| 3020 | char_u **arg, |
| 3021 | size_t varlen, |
| 3022 | cctx_T *cctx, |
| 3023 | ppconst_T *ppconst, |
| 3024 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3025 | { |
| 3026 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 3027 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3028 | int argcount = argcount_init; |
| 3029 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3030 | char_u fname_buf[FLEN_FIXED + 1]; |
| 3031 | char_u *tofree = NULL; |
| 3032 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3033 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3034 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3035 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3036 | // we can evaluate "has('name')" at compile time |
| 3037 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 3038 | { |
| 3039 | char_u *s = skipwhite(*arg + varlen + 1); |
| 3040 | typval_T argvars[2]; |
| 3041 | |
| 3042 | argvars[0].v_type = VAR_UNKNOWN; |
| 3043 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3044 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3045 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3046 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3047 | s = skipwhite(s); |
| 3048 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 3049 | { |
| 3050 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 3051 | |
| 3052 | *arg = s + 1; |
| 3053 | argvars[1].v_type = VAR_UNKNOWN; |
| 3054 | tv->v_type = VAR_NUMBER; |
| 3055 | tv->vval.v_number = 0; |
| 3056 | f_has(argvars, tv); |
| 3057 | clear_tv(&argvars[0]); |
| 3058 | ++ppconst->pp_used; |
| 3059 | return OK; |
| 3060 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 3061 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3065 | return FAIL; |
| 3066 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3067 | if (varlen >= sizeof(namebuf)) |
| 3068 | { |
| 3069 | semsg(_("E1011: name too long: %s"), name); |
| 3070 | return FAIL; |
| 3071 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3072 | vim_strncpy(namebuf, *arg, varlen); |
| 3073 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3074 | |
| 3075 | *arg = skipwhite(*arg + varlen + 1); |
| 3076 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3077 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3078 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3079 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3080 | { |
| 3081 | int idx; |
| 3082 | |
| 3083 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3084 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3085 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 3086 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3087 | else |
| 3088 | semsg(_(e_unknownfunc), namebuf); |
| 3089 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3090 | } |
| 3091 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3092 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 3093 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3094 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3095 | { |
| 3096 | res = generate_CALL(cctx, ufunc, argcount); |
| 3097 | goto theend; |
| 3098 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3099 | |
| 3100 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3101 | // 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] | 3102 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3103 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 3104 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3105 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3106 | garray_T *stack = &cctx->ctx_type_stack; |
| 3107 | type_T *type; |
| 3108 | |
| 3109 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3110 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3111 | goto theend; |
| 3112 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3113 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3114 | // 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] | 3115 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3116 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 3117 | res = generate_UCALL(cctx, name, argcount); |
| 3118 | else |
| 3119 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3120 | |
| 3121 | theend: |
| 3122 | vim_free(tofree); |
| 3123 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 3127 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 3128 | |
| 3129 | /* |
| 3130 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 3131 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3132 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3133 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 3134 | * valid name. |
| 3135 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3136 | static char_u * |
| 3137 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3138 | { |
| 3139 | char_u *p; |
| 3140 | |
| 3141 | // Quick check for valid starting character. |
| 3142 | if (!eval_isnamec1(*arg)) |
| 3143 | return arg; |
| 3144 | |
| 3145 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 3146 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 3147 | // and can be used in slice "[n:]". |
| 3148 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3149 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3150 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 3151 | break; |
| 3152 | return p; |
| 3153 | } |
| 3154 | |
| 3155 | /* |
| 3156 | * 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] | 3157 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3158 | */ |
| 3159 | char_u * |
| 3160 | to_name_const_end(char_u *arg) |
| 3161 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3162 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3163 | typval_T rettv; |
| 3164 | |
| 3165 | if (p == arg && *arg == '[') |
| 3166 | { |
| 3167 | |
| 3168 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3169 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3170 | p = arg; |
| 3171 | } |
| 3172 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 3173 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3174 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3175 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3176 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3177 | p = arg; |
| 3178 | } |
| 3179 | else if (p == arg && *arg == '{') |
| 3180 | { |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3181 | int ret = get_lambda_tv(&p, &rettv, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3182 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3183 | // Can be "{x -> ret}()". |
| 3184 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3185 | if (ret == NOTDONE) |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3186 | ret = eval_dict(&p, &rettv, NULL, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3187 | if (ret != OK) |
| 3188 | p = arg; |
| 3189 | } |
| 3190 | |
| 3191 | return p; |
| 3192 | } |
| 3193 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3194 | /* |
| 3195 | * parse a list: [expr, expr] |
| 3196 | * "*arg" points to the '['. |
| 3197 | */ |
| 3198 | static int |
| 3199 | compile_list(char_u **arg, cctx_T *cctx) |
| 3200 | { |
| 3201 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3202 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3203 | int count = 0; |
| 3204 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3205 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3206 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3207 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3208 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3209 | semsg(_(e_list_end), *arg); |
| 3210 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3211 | } |
| 3212 | if (*p == ']') |
| 3213 | { |
| 3214 | ++p; |
| 3215 | // Allow for following comment, after at least one space. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3216 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '#') |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3217 | p += STRLEN(p); |
| 3218 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3219 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3220 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3221 | break; |
| 3222 | ++count; |
| 3223 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3224 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3225 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3226 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 3227 | { |
| 3228 | semsg(_(e_white_after), ","); |
| 3229 | return FAIL; |
| 3230 | } |
| 3231 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3232 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3233 | p = skipwhite(p); |
| 3234 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3235 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3236 | |
| 3237 | generate_NEWLIST(cctx, count); |
| 3238 | return OK; |
| 3239 | } |
| 3240 | |
| 3241 | /* |
| 3242 | * parse a lambda: {arg, arg -> expr} |
| 3243 | * "*arg" points to the '{'. |
| 3244 | */ |
| 3245 | static int |
| 3246 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 3247 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3248 | typval_T rettv; |
| 3249 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3250 | evalarg_T evalarg; |
| 3251 | |
| 3252 | CLEAR_FIELD(evalarg); |
| 3253 | evalarg.eval_flags = EVAL_EVALUATE; |
| 3254 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3255 | |
| 3256 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3257 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3258 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3259 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3260 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3261 | ++ufunc->uf_refcount; |
| 3262 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3263 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3264 | |
| 3265 | // The function will have one line: "return {expr}". |
| 3266 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3267 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3268 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 3269 | clear_evalarg(&evalarg, NULL); |
| 3270 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 3271 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 3272 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3273 | |
| 3274 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3275 | return FAIL; |
| 3276 | } |
| 3277 | |
| 3278 | /* |
| 3279 | * Compile a lamda call: expr->{lambda}(args) |
| 3280 | * "arg" points to the "{". |
| 3281 | */ |
| 3282 | static int |
| 3283 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 3284 | { |
| 3285 | ufunc_T *ufunc; |
| 3286 | typval_T rettv; |
| 3287 | int argcount = 1; |
| 3288 | int ret = FAIL; |
| 3289 | |
| 3290 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3291 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3292 | return FAIL; |
| 3293 | |
| 3294 | if (**arg != '(') |
| 3295 | { |
| 3296 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 3297 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3298 | else |
| 3299 | semsg(_(e_missing_paren), "lambda"); |
| 3300 | clear_tv(&rettv); |
| 3301 | return FAIL; |
| 3302 | } |
| 3303 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3304 | ufunc = rettv.vval.v_partial->pt_func; |
| 3305 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3306 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3307 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3308 | |
| 3309 | // The function will have one line: "return {expr}". |
| 3310 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3311 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3312 | |
| 3313 | // compile the arguments |
| 3314 | *arg = skipwhite(*arg + 1); |
| 3315 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 3316 | // call the compiled function |
| 3317 | ret = generate_CALL(cctx, ufunc, argcount); |
| 3318 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3319 | if (ret == FAIL) |
| 3320 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3321 | return ret; |
| 3322 | } |
| 3323 | |
| 3324 | /* |
| 3325 | * parse a dict: {'key': val} or #{key: val} |
| 3326 | * "*arg" points to the '{'. |
| 3327 | */ |
| 3328 | static int |
| 3329 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 3330 | { |
| 3331 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3332 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3333 | int count = 0; |
| 3334 | dict_T *d = dict_alloc(); |
| 3335 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3336 | char_u *whitep = *arg; |
| 3337 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3338 | |
| 3339 | if (d == NULL) |
| 3340 | return FAIL; |
| 3341 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3342 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3343 | { |
| 3344 | char_u *key = NULL; |
| 3345 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3346 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3347 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3348 | *arg = NULL; |
| 3349 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | if (**arg == '}') |
| 3353 | break; |
| 3354 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3355 | if (literal) |
| 3356 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3357 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3358 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3359 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3360 | { |
| 3361 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 3362 | return FAIL; |
| 3363 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3364 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3365 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3366 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3367 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3368 | } |
| 3369 | else |
| 3370 | { |
| 3371 | isn_T *isn; |
| 3372 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3373 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3374 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3375 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3376 | if (isn->isn_type == ISN_PUSHS) |
| 3377 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3378 | else |
| 3379 | { |
| 3380 | type_T *keytype = ((type_T **)stack->ga_data) |
| 3381 | [stack->ga_len - 1]; |
| 3382 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 3383 | return FAIL; |
| 3384 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3385 | } |
| 3386 | |
| 3387 | // Check for duplicate keys, if using string keys. |
| 3388 | if (key != NULL) |
| 3389 | { |
| 3390 | item = dict_find(d, key, -1); |
| 3391 | if (item != NULL) |
| 3392 | { |
| 3393 | semsg(_(e_duplicate_key), key); |
| 3394 | goto failret; |
| 3395 | } |
| 3396 | item = dictitem_alloc(key); |
| 3397 | if (item != NULL) |
| 3398 | { |
| 3399 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3400 | item->di_tv.v_lock = 0; |
| 3401 | if (dict_add(d, item) == FAIL) |
| 3402 | dictitem_free(item); |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | *arg = skipwhite(*arg); |
| 3407 | if (**arg != ':') |
| 3408 | { |
| 3409 | semsg(_(e_missing_dict_colon), *arg); |
| 3410 | return FAIL; |
| 3411 | } |
| 3412 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3413 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3414 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3415 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3416 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3417 | *arg = NULL; |
| 3418 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3419 | } |
| 3420 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3421 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3422 | return FAIL; |
| 3423 | ++count; |
| 3424 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3425 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3426 | *arg = skipwhite(*arg); |
| 3427 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3428 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3429 | *arg = NULL; |
| 3430 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3431 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3432 | if (**arg == '}') |
| 3433 | break; |
| 3434 | if (**arg != ',') |
| 3435 | { |
| 3436 | semsg(_(e_missing_dict_comma), *arg); |
| 3437 | goto failret; |
| 3438 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3439 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3440 | *arg = skipwhite(*arg + 1); |
| 3441 | } |
| 3442 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3443 | *arg = *arg + 1; |
| 3444 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3445 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3446 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3447 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3448 | *arg += STRLEN(*arg); |
| 3449 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3450 | dict_unref(d); |
| 3451 | return generate_NEWDICT(cctx, count); |
| 3452 | |
| 3453 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3454 | if (*arg == NULL) |
| 3455 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3456 | dict_unref(d); |
| 3457 | return FAIL; |
| 3458 | } |
| 3459 | |
| 3460 | /* |
| 3461 | * Compile "&option". |
| 3462 | */ |
| 3463 | static int |
| 3464 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3465 | { |
| 3466 | typval_T rettv; |
| 3467 | char_u *start = *arg; |
| 3468 | int ret; |
| 3469 | |
| 3470 | // parse the option and get the current value to get the type. |
| 3471 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3472 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3473 | if (ret == OK) |
| 3474 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3475 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3476 | char_u *name = vim_strnsave(start, *arg - start); |
| 3477 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3478 | |
| 3479 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3480 | vim_free(name); |
| 3481 | } |
| 3482 | clear_tv(&rettv); |
| 3483 | |
| 3484 | return ret; |
| 3485 | } |
| 3486 | |
| 3487 | /* |
| 3488 | * Compile "$VAR". |
| 3489 | */ |
| 3490 | static int |
| 3491 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3492 | { |
| 3493 | char_u *start = *arg; |
| 3494 | int len; |
| 3495 | int ret; |
| 3496 | char_u *name; |
| 3497 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3498 | ++*arg; |
| 3499 | len = get_env_len(arg); |
| 3500 | if (len == 0) |
| 3501 | { |
| 3502 | semsg(_(e_syntax_at), start - 1); |
| 3503 | return FAIL; |
| 3504 | } |
| 3505 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3506 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3507 | name = vim_strnsave(start, len + 1); |
| 3508 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3509 | vim_free(name); |
| 3510 | return ret; |
| 3511 | } |
| 3512 | |
| 3513 | /* |
| 3514 | * Compile "@r". |
| 3515 | */ |
| 3516 | static int |
| 3517 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3518 | { |
| 3519 | int ret; |
| 3520 | |
| 3521 | ++*arg; |
| 3522 | if (**arg == NUL) |
| 3523 | { |
| 3524 | semsg(_(e_syntax_at), *arg - 1); |
| 3525 | return FAIL; |
| 3526 | } |
| 3527 | if (!valid_yank_reg(**arg, TRUE)) |
| 3528 | { |
| 3529 | emsg_invreg(**arg); |
| 3530 | return FAIL; |
| 3531 | } |
| 3532 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3533 | ++*arg; |
| 3534 | return ret; |
| 3535 | } |
| 3536 | |
| 3537 | /* |
| 3538 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3539 | */ |
| 3540 | static int |
| 3541 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3542 | { |
| 3543 | char_u *p = end; |
| 3544 | |
| 3545 | // this works from end to start |
| 3546 | while (p > start) |
| 3547 | { |
| 3548 | --p; |
| 3549 | if (*p == '-' || *p == '+') |
| 3550 | { |
| 3551 | // only '-' has an effect, for '+' we only check the type |
| 3552 | #ifdef FEAT_FLOAT |
| 3553 | if (rettv->v_type == VAR_FLOAT) |
| 3554 | { |
| 3555 | if (*p == '-') |
| 3556 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3557 | } |
| 3558 | else |
| 3559 | #endif |
| 3560 | { |
| 3561 | varnumber_T val; |
| 3562 | int error = FALSE; |
| 3563 | |
| 3564 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3565 | // here |
| 3566 | if (check_not_string(rettv) == FAIL) |
| 3567 | return FAIL; |
| 3568 | val = tv_get_number_chk(rettv, &error); |
| 3569 | clear_tv(rettv); |
| 3570 | if (error) |
| 3571 | return FAIL; |
| 3572 | if (*p == '-') |
| 3573 | val = -val; |
| 3574 | rettv->v_type = VAR_NUMBER; |
| 3575 | rettv->vval.v_number = val; |
| 3576 | } |
| 3577 | } |
| 3578 | else |
| 3579 | { |
| 3580 | int v = tv2bool(rettv); |
| 3581 | |
| 3582 | // '!' is permissive in the type. |
| 3583 | clear_tv(rettv); |
| 3584 | rettv->v_type = VAR_BOOL; |
| 3585 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3586 | } |
| 3587 | } |
| 3588 | return OK; |
| 3589 | } |
| 3590 | |
| 3591 | /* |
| 3592 | * Recognize v: variables that are constants and set "rettv". |
| 3593 | */ |
| 3594 | static void |
| 3595 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3596 | { |
| 3597 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3598 | { |
| 3599 | rettv->v_type = VAR_BOOL; |
| 3600 | rettv->vval.v_number = VVAL_TRUE; |
| 3601 | *arg += 6; |
| 3602 | } |
| 3603 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3604 | { |
| 3605 | rettv->v_type = VAR_BOOL; |
| 3606 | rettv->vval.v_number = VVAL_FALSE; |
| 3607 | *arg += 7; |
| 3608 | } |
| 3609 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3610 | { |
| 3611 | rettv->v_type = VAR_SPECIAL; |
| 3612 | rettv->vval.v_number = VVAL_NULL; |
| 3613 | *arg += 6; |
| 3614 | } |
| 3615 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3616 | { |
| 3617 | rettv->v_type = VAR_SPECIAL; |
| 3618 | rettv->vval.v_number = VVAL_NONE; |
| 3619 | *arg += 6; |
| 3620 | } |
| 3621 | } |
| 3622 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3623 | static exptype_T |
| 3624 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3625 | { |
| 3626 | exptype_T type = EXPR_UNKNOWN; |
| 3627 | int i; |
| 3628 | |
| 3629 | switch (p[0]) |
| 3630 | { |
| 3631 | case '=': if (p[1] == '=') |
| 3632 | type = EXPR_EQUAL; |
| 3633 | else if (p[1] == '~') |
| 3634 | type = EXPR_MATCH; |
| 3635 | break; |
| 3636 | case '!': if (p[1] == '=') |
| 3637 | type = EXPR_NEQUAL; |
| 3638 | else if (p[1] == '~') |
| 3639 | type = EXPR_NOMATCH; |
| 3640 | break; |
| 3641 | case '>': if (p[1] != '=') |
| 3642 | { |
| 3643 | type = EXPR_GREATER; |
| 3644 | *len = 1; |
| 3645 | } |
| 3646 | else |
| 3647 | type = EXPR_GEQUAL; |
| 3648 | break; |
| 3649 | case '<': if (p[1] != '=') |
| 3650 | { |
| 3651 | type = EXPR_SMALLER; |
| 3652 | *len = 1; |
| 3653 | } |
| 3654 | else |
| 3655 | type = EXPR_SEQUAL; |
| 3656 | break; |
| 3657 | case 'i': if (p[1] == 's') |
| 3658 | { |
| 3659 | // "is" and "isnot"; but not a prefix of a name |
| 3660 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3661 | *len = 5; |
| 3662 | i = p[*len]; |
| 3663 | if (!isalnum(i) && i != '_') |
| 3664 | { |
| 3665 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3666 | *type_is = TRUE; |
| 3667 | } |
| 3668 | } |
| 3669 | break; |
| 3670 | } |
| 3671 | return type; |
| 3672 | } |
| 3673 | |
| 3674 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3675 | * Compile code to apply '-', '+' and '!'. |
| 3676 | */ |
| 3677 | static int |
| 3678 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3679 | { |
| 3680 | char_u *p = end; |
| 3681 | |
| 3682 | // this works from end to start |
| 3683 | while (p > start) |
| 3684 | { |
| 3685 | --p; |
| 3686 | if (*p == '-' || *p == '+') |
| 3687 | { |
| 3688 | int negate = *p == '-'; |
| 3689 | isn_T *isn; |
| 3690 | |
| 3691 | // TODO: check type |
| 3692 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3693 | { |
| 3694 | --p; |
| 3695 | if (*p == '-') |
| 3696 | negate = !negate; |
| 3697 | } |
| 3698 | // only '-' has an effect, for '+' we only check the type |
| 3699 | if (negate) |
| 3700 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3701 | else |
| 3702 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3703 | if (isn == NULL) |
| 3704 | return FAIL; |
| 3705 | } |
| 3706 | else |
| 3707 | { |
| 3708 | int invert = TRUE; |
| 3709 | |
| 3710 | while (p > start && p[-1] == '!') |
| 3711 | { |
| 3712 | --p; |
| 3713 | invert = !invert; |
| 3714 | } |
| 3715 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3716 | return FAIL; |
| 3717 | } |
| 3718 | } |
| 3719 | return OK; |
| 3720 | } |
| 3721 | |
| 3722 | /* |
| 3723 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3724 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3725 | */ |
| 3726 | static int |
| 3727 | compile_subscript( |
| 3728 | char_u **arg, |
| 3729 | cctx_T *cctx, |
| 3730 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3731 | char_u *end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3732 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3733 | { |
| 3734 | for (;;) |
| 3735 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3736 | char_u *p = skipwhite(*arg); |
| 3737 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3738 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3739 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3740 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3741 | |
| 3742 | // If a following line starts with "->{" or "->X" advance to that |
| 3743 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3744 | // Also if a following line starts with ".x". |
| 3745 | if (next != NULL && |
| 3746 | ((next[0] == '-' && next[1] == '>' |
| 3747 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
| 3748 | || (next[0] == '.' && ASCII_ISALPHA(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3749 | { |
| 3750 | next = next_line_from_context(cctx, TRUE); |
| 3751 | if (next == NULL) |
| 3752 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3753 | *arg = next; |
| 3754 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3755 | } |
| 3756 | } |
| 3757 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3758 | if (*p == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3759 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3760 | garray_T *stack = &cctx->ctx_type_stack; |
| 3761 | type_T *type; |
| 3762 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3763 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3764 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3765 | return FAIL; |
| 3766 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3767 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3768 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3769 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3770 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3771 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3772 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3773 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3774 | return FAIL; |
| 3775 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3776 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3777 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3778 | char_u *pstart = p; |
| 3779 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3780 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3781 | return FAIL; |
| 3782 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3783 | // something->method() |
| 3784 | // Apply the '!', '-' and '+' first: |
| 3785 | // -1.0->func() works like (-1.0)->func() |
| 3786 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3787 | return FAIL; |
| 3788 | *start_leader = end_leader; // don't apply again later |
| 3789 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3790 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3791 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3792 | // No line break supported right after "->". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3793 | if (**arg == '{') |
| 3794 | { |
| 3795 | // lambda call: list->{lambda} |
| 3796 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3797 | return FAIL; |
| 3798 | } |
| 3799 | else |
| 3800 | { |
| 3801 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3802 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3803 | if (!eval_isnamec1(*p)) |
| 3804 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3805 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3806 | return FAIL; |
| 3807 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3808 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3809 | p += 2; |
| 3810 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3811 | ; |
| 3812 | if (*p != '(') |
| 3813 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3814 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3815 | return FAIL; |
| 3816 | } |
| 3817 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3818 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3819 | return FAIL; |
| 3820 | } |
| 3821 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3822 | else if (*p == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3823 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3824 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3825 | type_T **typep; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3826 | vartype_T vtype; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3827 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3828 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3829 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3830 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3831 | // TODO: blob index |
| 3832 | // TODO: more arguments |
| 3833 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3834 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3835 | return FAIL; |
| 3836 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3837 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3838 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3839 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3840 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3841 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3842 | return FAIL; |
| 3843 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3844 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3845 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3846 | if (**arg != ']') |
| 3847 | { |
| 3848 | emsg(_(e_missbrac)); |
| 3849 | return FAIL; |
| 3850 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3851 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3852 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3853 | // We can index a list and a dict. If we don't know the type |
| 3854 | // we can use the index value type. |
| 3855 | // TODO: If we don't know use an instruction to figure it out at |
| 3856 | // runtime. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3857 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3858 | vtype = (*typep)->tt_type; |
| 3859 | if (*typep == &t_any) |
| 3860 | { |
| 3861 | type_T *valtype = ((type_T **)stack->ga_data) |
| 3862 | [stack->ga_len - 1]; |
| 3863 | if (valtype == &t_string) |
| 3864 | vtype = VAR_DICT; |
| 3865 | } |
| 3866 | if (vtype == VAR_DICT) |
| 3867 | { |
| 3868 | if ((*typep)->tt_type == VAR_DICT) |
| 3869 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3870 | else |
| 3871 | { |
| 3872 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3873 | return FAIL; |
| 3874 | *typep = &t_any; |
| 3875 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3876 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3877 | return FAIL; |
| 3878 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3879 | return FAIL; |
| 3880 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3881 | else if (vtype == VAR_STRING) |
| 3882 | { |
| 3883 | *typep = &t_number; |
| 3884 | if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL) |
| 3885 | return FAIL; |
| 3886 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3887 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3888 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3889 | if ((*typep)->tt_type == VAR_LIST) |
| 3890 | *typep = (*typep)->tt_member; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3891 | if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3892 | return FAIL; |
| 3893 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3894 | else |
| 3895 | { |
| 3896 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3897 | return FAIL; |
| 3898 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3899 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3900 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3901 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3902 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3903 | return FAIL; |
| 3904 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3905 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3906 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3907 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3908 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3909 | p = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3910 | if (eval_isnamec1(*p)) |
| 3911 | while (eval_isnamec(*p)) |
| 3912 | MB_PTR_ADV(p); |
| 3913 | if (p == *arg) |
| 3914 | { |
| 3915 | semsg(_(e_syntax_at), *arg); |
| 3916 | return FAIL; |
| 3917 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3918 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3919 | return FAIL; |
| 3920 | *arg = p; |
| 3921 | } |
| 3922 | else |
| 3923 | break; |
| 3924 | } |
| 3925 | |
| 3926 | // TODO - see handle_subscript(): |
| 3927 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3928 | // Don't do this when "Func" is already a partial that was bound |
| 3929 | // explicitly (pt_auto is FALSE). |
| 3930 | |
| 3931 | return OK; |
| 3932 | } |
| 3933 | |
| 3934 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3935 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3936 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3937 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3938 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3939 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3940 | * |
| 3941 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3942 | */ |
| 3943 | |
| 3944 | /* |
| 3945 | * number number constant |
| 3946 | * 0zFFFFFFFF Blob constant |
| 3947 | * "string" string constant |
| 3948 | * 'string' literal string constant |
| 3949 | * &option-name option value |
| 3950 | * @r register contents |
| 3951 | * identifier variable value |
| 3952 | * function() function call |
| 3953 | * $VAR environment variable |
| 3954 | * (expression) nested expression |
| 3955 | * [expr, expr] List |
| 3956 | * {key: val, key: val} Dictionary |
| 3957 | * #{key: val, key: val} Dictionary with literal keys |
| 3958 | * |
| 3959 | * Also handle: |
| 3960 | * ! in front logical NOT |
| 3961 | * - in front unary minus |
| 3962 | * + in front unary plus (ignored) |
| 3963 | * trailing (arg) funcref/partial call |
| 3964 | * trailing [] subscript in String or List |
| 3965 | * trailing .name entry in Dictionary |
| 3966 | * trailing ->name() method call |
| 3967 | */ |
| 3968 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3969 | compile_expr7( |
| 3970 | char_u **arg, |
| 3971 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3972 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3973 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3974 | char_u *start_leader, *end_leader; |
| 3975 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3976 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3977 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3978 | |
| 3979 | /* |
| 3980 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3981 | */ |
| 3982 | start_leader = *arg; |
| 3983 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3984 | *arg = skipwhite(*arg + 1); |
| 3985 | end_leader = *arg; |
| 3986 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3987 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3988 | switch (**arg) |
| 3989 | { |
| 3990 | /* |
| 3991 | * Number constant. |
| 3992 | */ |
| 3993 | case '0': // also for blob starting with 0z |
| 3994 | case '1': |
| 3995 | case '2': |
| 3996 | case '3': |
| 3997 | case '4': |
| 3998 | case '5': |
| 3999 | case '6': |
| 4000 | case '7': |
| 4001 | case '8': |
| 4002 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4003 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4004 | return FAIL; |
| 4005 | break; |
| 4006 | |
| 4007 | /* |
| 4008 | * String constant: "string". |
| 4009 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4010 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4011 | return FAIL; |
| 4012 | break; |
| 4013 | |
| 4014 | /* |
| 4015 | * Literal string constant: 'str''ing'. |
| 4016 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4017 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4018 | return FAIL; |
| 4019 | break; |
| 4020 | |
| 4021 | /* |
| 4022 | * Constant Vim variable. |
| 4023 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4024 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4025 | ret = NOTDONE; |
| 4026 | break; |
| 4027 | |
| 4028 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4029 | * "true" constant |
| 4030 | */ |
| 4031 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 4032 | && !eval_isnamec((*arg)[4])) |
| 4033 | { |
| 4034 | *arg += 4; |
| 4035 | rettv->v_type = VAR_BOOL; |
| 4036 | rettv->vval.v_number = VVAL_TRUE; |
| 4037 | } |
| 4038 | else |
| 4039 | ret = NOTDONE; |
| 4040 | break; |
| 4041 | |
| 4042 | /* |
| 4043 | * "false" constant |
| 4044 | */ |
| 4045 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 4046 | && !eval_isnamec((*arg)[5])) |
| 4047 | { |
| 4048 | *arg += 5; |
| 4049 | rettv->v_type = VAR_BOOL; |
| 4050 | rettv->vval.v_number = VVAL_FALSE; |
| 4051 | } |
| 4052 | else |
| 4053 | ret = NOTDONE; |
| 4054 | break; |
| 4055 | |
| 4056 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4057 | * List: [expr, expr] |
| 4058 | */ |
| 4059 | case '[': ret = compile_list(arg, cctx); |
| 4060 | break; |
| 4061 | |
| 4062 | /* |
| 4063 | * Dictionary: #{key: val, key: val} |
| 4064 | */ |
| 4065 | case '#': if ((*arg)[1] == '{') |
| 4066 | { |
| 4067 | ++*arg; |
| 4068 | ret = compile_dict(arg, cctx, TRUE); |
| 4069 | } |
| 4070 | else |
| 4071 | ret = NOTDONE; |
| 4072 | break; |
| 4073 | |
| 4074 | /* |
| 4075 | * Lambda: {arg, arg -> expr} |
| 4076 | * Dictionary: {'key': val, 'key': val} |
| 4077 | */ |
| 4078 | case '{': { |
| 4079 | char_u *start = skipwhite(*arg + 1); |
| 4080 | |
| 4081 | // Find out what comes after the arguments. |
| 4082 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 4083 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4084 | if (ret != FAIL && *start == '>') |
| 4085 | ret = compile_lambda(arg, cctx); |
| 4086 | else |
| 4087 | ret = compile_dict(arg, cctx, FALSE); |
| 4088 | } |
| 4089 | break; |
| 4090 | |
| 4091 | /* |
| 4092 | * Option value: &name |
| 4093 | */ |
| 4094 | case '&': ret = compile_get_option(arg, cctx); |
| 4095 | break; |
| 4096 | |
| 4097 | /* |
| 4098 | * Environment variable: $VAR. |
| 4099 | */ |
| 4100 | case '$': ret = compile_get_env(arg, cctx); |
| 4101 | break; |
| 4102 | |
| 4103 | /* |
| 4104 | * Register contents: @r. |
| 4105 | */ |
| 4106 | case '@': ret = compile_get_register(arg, cctx); |
| 4107 | break; |
| 4108 | /* |
| 4109 | * nested expression: (expression). |
| 4110 | */ |
| 4111 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4112 | |
| 4113 | // recursive! |
| 4114 | if (ppconst->pp_used <= PPSIZE - 10) |
| 4115 | { |
| 4116 | ret = compile_expr1(arg, cctx, ppconst); |
| 4117 | } |
| 4118 | else |
| 4119 | { |
| 4120 | // Not enough space in ppconst, flush constants. |
| 4121 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4122 | return FAIL; |
| 4123 | ret = compile_expr0(arg, cctx); |
| 4124 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4125 | *arg = skipwhite(*arg); |
| 4126 | if (**arg == ')') |
| 4127 | ++*arg; |
| 4128 | else if (ret == OK) |
| 4129 | { |
| 4130 | emsg(_(e_missing_close)); |
| 4131 | ret = FAIL; |
| 4132 | } |
| 4133 | break; |
| 4134 | |
| 4135 | default: ret = NOTDONE; |
| 4136 | break; |
| 4137 | } |
| 4138 | if (ret == FAIL) |
| 4139 | return FAIL; |
| 4140 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4141 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4142 | { |
| 4143 | // apply the '!', '-' and '+' before the constant |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4144 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4145 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4146 | clear_tv(rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4147 | return FAIL; |
| 4148 | } |
| 4149 | start_leader = end_leader; // don't apply again below |
| 4150 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4151 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4152 | clear_tv(rettv); |
| 4153 | else |
| 4154 | // A constant expression can possibly be handled compile time, |
| 4155 | // return the value instead of generating code. |
| 4156 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4157 | } |
| 4158 | else if (ret == NOTDONE) |
| 4159 | { |
| 4160 | char_u *p; |
| 4161 | int r; |
| 4162 | |
| 4163 | if (!eval_isnamec1(**arg)) |
| 4164 | { |
| 4165 | semsg(_("E1015: Name expected: %s"), *arg); |
| 4166 | return FAIL; |
| 4167 | } |
| 4168 | |
| 4169 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4170 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4171 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4172 | { |
| 4173 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 4174 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4175 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4176 | { |
| 4177 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4178 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4179 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4180 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4181 | if (r == FAIL) |
| 4182 | return FAIL; |
| 4183 | } |
| 4184 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4185 | // Handle following "[]", ".member", etc. |
| 4186 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4187 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4188 | ppconst) == FAIL) |
| 4189 | return FAIL; |
| 4190 | if (ppconst->pp_used > 0) |
| 4191 | { |
| 4192 | // apply the '!', '-' and '+' before the constant |
| 4193 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4194 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
| 4195 | return FAIL; |
| 4196 | return OK; |
| 4197 | } |
| 4198 | if (compile_leader(cctx, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4199 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4200 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | /* |
| 4204 | * * number multiplication |
| 4205 | * / number division |
| 4206 | * % number modulo |
| 4207 | */ |
| 4208 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4209 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4210 | { |
| 4211 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4212 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4213 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4214 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4215 | // get the first expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4216 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4217 | return FAIL; |
| 4218 | |
| 4219 | /* |
| 4220 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4221 | */ |
| 4222 | for (;;) |
| 4223 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4224 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4225 | if (*op != '*' && *op != '/' && *op != '%') |
| 4226 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4227 | if (next != NULL) |
| 4228 | { |
| 4229 | *arg = next_line_from_context(cctx, TRUE); |
| 4230 | op = skipwhite(*arg); |
| 4231 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4232 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4233 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4234 | { |
| 4235 | char_u buf[3]; |
| 4236 | |
| 4237 | vim_strncpy(buf, op, 1); |
| 4238 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4239 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4240 | } |
| 4241 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4242 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4243 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4244 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4245 | // get the second expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4246 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4247 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4248 | |
| 4249 | if (ppconst->pp_used == ppconst_used + 2 |
| 4250 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4251 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4252 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4253 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4254 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4255 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4256 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4257 | // both are numbers: compute the result |
| 4258 | switch (*op) |
| 4259 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4260 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4261 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4262 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4263 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4264 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4265 | break; |
| 4266 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4267 | tv1->vval.v_number = res; |
| 4268 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4269 | } |
| 4270 | else |
| 4271 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4272 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4273 | generate_two_op(cctx, op); |
| 4274 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4275 | } |
| 4276 | |
| 4277 | return OK; |
| 4278 | } |
| 4279 | |
| 4280 | /* |
| 4281 | * + number addition |
| 4282 | * - number subtraction |
| 4283 | * .. string concatenation |
| 4284 | */ |
| 4285 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4286 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4287 | { |
| 4288 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4289 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4290 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4291 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4292 | |
| 4293 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4294 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4295 | return FAIL; |
| 4296 | |
| 4297 | /* |
| 4298 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4299 | */ |
| 4300 | for (;;) |
| 4301 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4302 | op = may_peek_next_line(cctx, *arg, &next); |
| 4303 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4304 | break; |
| 4305 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4306 | if (next != NULL) |
| 4307 | { |
| 4308 | *arg = next_line_from_context(cctx, TRUE); |
| 4309 | op = skipwhite(*arg); |
| 4310 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4311 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4312 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4313 | { |
| 4314 | char_u buf[3]; |
| 4315 | |
| 4316 | vim_strncpy(buf, op, oplen); |
| 4317 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4318 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4322 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4323 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4324 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4325 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4326 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4327 | return FAIL; |
| 4328 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4329 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4330 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4331 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4332 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4333 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4334 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4335 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4336 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4337 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4338 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4339 | // concat/subtract/add constant numbers |
| 4340 | if (*op == '+') |
| 4341 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4342 | else if (*op == '-') |
| 4343 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4344 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4345 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4346 | // concatenate constant strings |
| 4347 | char_u *s1 = tv1->vval.v_string; |
| 4348 | char_u *s2 = tv2->vval.v_string; |
| 4349 | size_t len1 = STRLEN(s1); |
| 4350 | |
| 4351 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4352 | if (tv1->vval.v_string == NULL) |
| 4353 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4354 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4355 | return FAIL; |
| 4356 | } |
| 4357 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4358 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4359 | vim_free(s1); |
| 4360 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4361 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4362 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4363 | } |
| 4364 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4365 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4366 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4367 | if (*op == '.') |
| 4368 | { |
| 4369 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4370 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4371 | return FAIL; |
| 4372 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4373 | } |
| 4374 | else |
| 4375 | generate_two_op(cctx, op); |
| 4376 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4377 | } |
| 4378 | |
| 4379 | return OK; |
| 4380 | } |
| 4381 | |
| 4382 | /* |
| 4383 | * expr5a == expr5b |
| 4384 | * expr5a =~ expr5b |
| 4385 | * expr5a != expr5b |
| 4386 | * expr5a !~ expr5b |
| 4387 | * expr5a > expr5b |
| 4388 | * expr5a >= expr5b |
| 4389 | * expr5a < expr5b |
| 4390 | * expr5a <= expr5b |
| 4391 | * expr5a is expr5b |
| 4392 | * expr5a isnot expr5b |
| 4393 | * |
| 4394 | * Produces instructions: |
| 4395 | * EVAL expr5a Push result of "expr5a" |
| 4396 | * EVAL expr5b Push result of "expr5b" |
| 4397 | * COMPARE one of the compare instructions |
| 4398 | */ |
| 4399 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4400 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4401 | { |
| 4402 | exptype_T type = EXPR_UNKNOWN; |
| 4403 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4404 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4405 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4406 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4407 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4408 | |
| 4409 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4410 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4411 | return FAIL; |
| 4412 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4413 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4414 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4415 | |
| 4416 | /* |
| 4417 | * If there is a comparative operator, use it. |
| 4418 | */ |
| 4419 | if (type != EXPR_UNKNOWN) |
| 4420 | { |
| 4421 | int ic = FALSE; // Default: do not ignore case |
| 4422 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4423 | if (next != NULL) |
| 4424 | { |
| 4425 | *arg = next_line_from_context(cctx, TRUE); |
| 4426 | p = skipwhite(*arg); |
| 4427 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4428 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4429 | { |
| 4430 | semsg(_(e_invexpr2), *arg); |
| 4431 | return FAIL; |
| 4432 | } |
| 4433 | // extra question mark appended: ignore case |
| 4434 | if (p[len] == '?') |
| 4435 | { |
| 4436 | ic = TRUE; |
| 4437 | ++len; |
| 4438 | } |
| 4439 | // extra '#' appended: match case (ignored) |
| 4440 | else if (p[len] == '#') |
| 4441 | ++len; |
| 4442 | // nothing appended: match case |
| 4443 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4444 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4445 | { |
| 4446 | char_u buf[7]; |
| 4447 | |
| 4448 | vim_strncpy(buf, p, len); |
| 4449 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4450 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | // get the second variable |
| 4454 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4455 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4456 | return FAIL; |
| 4457 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4458 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4459 | return FAIL; |
| 4460 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4461 | if (ppconst->pp_used == ppconst_used + 2) |
| 4462 | { |
| 4463 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4464 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4465 | int ret; |
| 4466 | |
| 4467 | // Both sides are a constant, compute the result now. |
| 4468 | // First check for a valid combination of types, this is more |
| 4469 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4470 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4471 | ret = FAIL; |
| 4472 | else |
| 4473 | { |
| 4474 | ret = typval_compare(tv1, tv2, type, ic); |
| 4475 | tv1->v_type = VAR_BOOL; |
| 4476 | tv1->vval.v_number = tv1->vval.v_number |
| 4477 | ? VVAL_TRUE : VVAL_FALSE; |
| 4478 | clear_tv(tv2); |
| 4479 | --ppconst->pp_used; |
| 4480 | } |
| 4481 | return ret; |
| 4482 | } |
| 4483 | |
| 4484 | generate_ppconst(cctx, ppconst); |
| 4485 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | return OK; |
| 4489 | } |
| 4490 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4491 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4492 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4493 | /* |
| 4494 | * Compile || or &&. |
| 4495 | */ |
| 4496 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4497 | compile_and_or( |
| 4498 | char_u **arg, |
| 4499 | cctx_T *cctx, |
| 4500 | char *op, |
| 4501 | ppconst_T *ppconst, |
| 4502 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4503 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4504 | char_u *next; |
| 4505 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4506 | int opchar = *op; |
| 4507 | |
| 4508 | if (p[0] == opchar && p[1] == opchar) |
| 4509 | { |
| 4510 | garray_T *instr = &cctx->ctx_instr; |
| 4511 | garray_T end_ga; |
| 4512 | |
| 4513 | /* |
| 4514 | * Repeat until there is no following "||" or "&&" |
| 4515 | */ |
| 4516 | ga_init2(&end_ga, sizeof(int), 10); |
| 4517 | while (p[0] == opchar && p[1] == opchar) |
| 4518 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4519 | if (next != NULL) |
| 4520 | { |
| 4521 | *arg = next_line_from_context(cctx, TRUE); |
| 4522 | p = skipwhite(*arg); |
| 4523 | } |
| 4524 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4525 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4526 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4527 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4528 | return FAIL; |
| 4529 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4530 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4531 | // TODO: use ppconst if the value is a constant |
| 4532 | generate_ppconst(cctx, ppconst); |
| 4533 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4534 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4535 | { |
| 4536 | ga_clear(&end_ga); |
| 4537 | return FAIL; |
| 4538 | } |
| 4539 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4540 | ++end_ga.ga_len; |
| 4541 | generate_JUMP(cctx, opchar == '|' |
| 4542 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 4543 | |
| 4544 | // eval the next expression |
| 4545 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4546 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4547 | return FAIL; |
| 4548 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4549 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4550 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4551 | { |
| 4552 | ga_clear(&end_ga); |
| 4553 | return FAIL; |
| 4554 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4555 | |
| 4556 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4557 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4558 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4559 | |
| 4560 | // Fill in the end label in all jumps. |
| 4561 | while (end_ga.ga_len > 0) |
| 4562 | { |
| 4563 | isn_T *isn; |
| 4564 | |
| 4565 | --end_ga.ga_len; |
| 4566 | isn = ((isn_T *)instr->ga_data) |
| 4567 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4568 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4569 | } |
| 4570 | ga_clear(&end_ga); |
| 4571 | } |
| 4572 | |
| 4573 | return OK; |
| 4574 | } |
| 4575 | |
| 4576 | /* |
| 4577 | * expr4a && expr4a && expr4a logical AND |
| 4578 | * |
| 4579 | * Produces instructions: |
| 4580 | * EVAL expr4a Push result of "expr4a" |
| 4581 | * JUMP_AND_KEEP_IF_FALSE end |
| 4582 | * EVAL expr4b Push result of "expr4b" |
| 4583 | * JUMP_AND_KEEP_IF_FALSE end |
| 4584 | * EVAL expr4c Push result of "expr4c" |
| 4585 | * end: |
| 4586 | */ |
| 4587 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4588 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4589 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4590 | int ppconst_used = ppconst->pp_used; |
| 4591 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4592 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4593 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4594 | return FAIL; |
| 4595 | |
| 4596 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4597 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4598 | } |
| 4599 | |
| 4600 | /* |
| 4601 | * expr3a || expr3b || expr3c logical OR |
| 4602 | * |
| 4603 | * Produces instructions: |
| 4604 | * EVAL expr3a Push result of "expr3a" |
| 4605 | * JUMP_AND_KEEP_IF_TRUE end |
| 4606 | * EVAL expr3b Push result of "expr3b" |
| 4607 | * JUMP_AND_KEEP_IF_TRUE end |
| 4608 | * EVAL expr3c Push result of "expr3c" |
| 4609 | * end: |
| 4610 | */ |
| 4611 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4612 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4613 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4614 | int ppconst_used = ppconst->pp_used; |
| 4615 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4616 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4617 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4618 | return FAIL; |
| 4619 | |
| 4620 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4621 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | /* |
| 4625 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4626 | * |
| 4627 | * Produces instructions: |
| 4628 | * EVAL expr2 Push result of "expr" |
| 4629 | * JUMP_IF_FALSE alt jump if false |
| 4630 | * EVAL expr1a |
| 4631 | * JUMP_ALWAYS end |
| 4632 | * alt: EVAL expr1b |
| 4633 | * end: |
| 4634 | */ |
| 4635 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4636 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4637 | { |
| 4638 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4639 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4640 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4641 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4642 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4643 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4644 | return FAIL; |
| 4645 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4646 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4647 | if (*p == '?') |
| 4648 | { |
| 4649 | garray_T *instr = &cctx->ctx_instr; |
| 4650 | garray_T *stack = &cctx->ctx_type_stack; |
| 4651 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4652 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4653 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4654 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4655 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4656 | int has_const_expr = FALSE; |
| 4657 | int const_value = FALSE; |
| 4658 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4659 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4660 | if (next != NULL) |
| 4661 | { |
| 4662 | *arg = next_line_from_context(cctx, TRUE); |
| 4663 | p = skipwhite(*arg); |
| 4664 | } |
| 4665 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4666 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4667 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4668 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4669 | return FAIL; |
| 4670 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4671 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4672 | if (ppconst->pp_used == ppconst_used + 1) |
| 4673 | { |
| 4674 | // the condition is a constant, we know whether the ? or the : |
| 4675 | // expression is to be evaluated. |
| 4676 | has_const_expr = TRUE; |
| 4677 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4678 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4679 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4680 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4681 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4682 | } |
| 4683 | else |
| 4684 | { |
| 4685 | generate_ppconst(cctx, ppconst); |
| 4686 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4687 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4688 | |
| 4689 | // evaluate the second expression; any type is accepted |
| 4690 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4691 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4692 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4693 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4694 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4695 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4696 | if (!has_const_expr) |
| 4697 | { |
| 4698 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4699 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4700 | // remember the type and drop it |
| 4701 | --stack->ga_len; |
| 4702 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4703 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4704 | end_idx = instr->ga_len; |
| 4705 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4706 | |
| 4707 | // jump here from JUMP_IF_FALSE |
| 4708 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4709 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4710 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4711 | |
| 4712 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4713 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4714 | if (*p != ':') |
| 4715 | { |
| 4716 | emsg(_(e_missing_colon)); |
| 4717 | return FAIL; |
| 4718 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4719 | if (next != NULL) |
| 4720 | { |
| 4721 | *arg = next_line_from_context(cctx, TRUE); |
| 4722 | p = skipwhite(*arg); |
| 4723 | } |
| 4724 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4725 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4726 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4727 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4728 | return FAIL; |
| 4729 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4730 | |
| 4731 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4732 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4733 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4734 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4735 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4736 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4737 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4738 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4739 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4740 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4741 | if (!has_const_expr) |
| 4742 | { |
| 4743 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4744 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4745 | // If the types differ, the result has a more generic type. |
| 4746 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4747 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4748 | |
| 4749 | // jump here from JUMP_ALWAYS |
| 4750 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4751 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4752 | } |
| 4753 | |
| 4754 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4755 | } |
| 4756 | return OK; |
| 4757 | } |
| 4758 | |
| 4759 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4760 | * Toplevel expression. |
| 4761 | */ |
| 4762 | static int |
| 4763 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4764 | { |
| 4765 | ppconst_T ppconst; |
| 4766 | |
| 4767 | CLEAR_FIELD(ppconst); |
| 4768 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4769 | { |
| 4770 | clear_ppconst(&ppconst); |
| 4771 | return FAIL; |
| 4772 | } |
| 4773 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4774 | return FAIL; |
| 4775 | return OK; |
| 4776 | } |
| 4777 | |
| 4778 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4779 | * compile "return [expr]" |
| 4780 | */ |
| 4781 | static char_u * |
| 4782 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4783 | { |
| 4784 | char_u *p = arg; |
| 4785 | garray_T *stack = &cctx->ctx_type_stack; |
| 4786 | type_T *stack_type; |
| 4787 | |
| 4788 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4789 | { |
| 4790 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4791 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4792 | return NULL; |
| 4793 | |
| 4794 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4795 | if (set_return_type) |
| 4796 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4797 | else |
| 4798 | { |
| 4799 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4800 | && stack_type->tt_type != VAR_VOID |
| 4801 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4802 | { |
| 4803 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4804 | return NULL; |
| 4805 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4806 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4807 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4808 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4809 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4810 | } |
| 4811 | else |
| 4812 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4813 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4814 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4815 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4816 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4817 | { |
| 4818 | emsg(_("E1003: Missing return value")); |
| 4819 | return NULL; |
| 4820 | } |
| 4821 | |
| 4822 | // No argument, return zero. |
| 4823 | generate_PUSHNR(cctx, 0); |
| 4824 | } |
| 4825 | |
| 4826 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4827 | return NULL; |
| 4828 | |
| 4829 | // "return val | endif" is possible |
| 4830 | return skipwhite(p); |
| 4831 | } |
| 4832 | |
| 4833 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4834 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4835 | * Return a pointer to the line in allocated memory. |
| 4836 | * Return NULL for end-of-file or some error. |
| 4837 | */ |
| 4838 | static char_u * |
| 4839 | exarg_getline( |
| 4840 | int c UNUSED, |
| 4841 | void *cookie, |
| 4842 | int indent UNUSED, |
| 4843 | int do_concat UNUSED) |
| 4844 | { |
| 4845 | cctx_T *cctx = (cctx_T *)cookie; |
| 4846 | |
| 4847 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4848 | { |
| 4849 | iemsg("Heredoc got to end"); |
| 4850 | return NULL; |
| 4851 | } |
| 4852 | ++cctx->ctx_lnum; |
| 4853 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4854 | [cctx->ctx_lnum]); |
| 4855 | } |
| 4856 | |
| 4857 | /* |
| 4858 | * Compile a nested :def command. |
| 4859 | */ |
| 4860 | static char_u * |
| 4861 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4862 | { |
| 4863 | char_u *name_start = eap->arg; |
| 4864 | char_u *name_end = to_name_end(eap->arg, FALSE); |
| 4865 | char_u *name = get_lambda_name(); |
| 4866 | lvar_T *lvar; |
| 4867 | ufunc_T *ufunc; |
| 4868 | |
| 4869 | eap->arg = name_end; |
| 4870 | eap->getline = exarg_getline; |
| 4871 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4872 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4873 | eap->forceit = FALSE; |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4874 | ufunc = def_function(eap, name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4875 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4876 | if (ufunc == NULL) |
| 4877 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4878 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4879 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4880 | return NULL; |
| 4881 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4882 | // Define a local variable for the function reference. |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4883 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4884 | TRUE, ufunc->uf_func_type); |
| 4885 | |
| 4886 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL |
| 4887 | || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL) |
| 4888 | return NULL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4889 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4890 | // TODO: warning for trailing text? |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4891 | return (char_u *)""; |
| 4892 | } |
| 4893 | |
| 4894 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4895 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4896 | */ |
| 4897 | int |
| 4898 | assignment_len(char_u *p, int *heredoc) |
| 4899 | { |
| 4900 | if (*p == '=') |
| 4901 | { |
| 4902 | if (p[1] == '<' && p[2] == '<') |
| 4903 | { |
| 4904 | *heredoc = TRUE; |
| 4905 | return 3; |
| 4906 | } |
| 4907 | return 1; |
| 4908 | } |
| 4909 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4910 | return 2; |
| 4911 | if (STRNCMP(p, "..=", 3) == 0) |
| 4912 | return 3; |
| 4913 | return 0; |
| 4914 | } |
| 4915 | |
| 4916 | // words that cannot be used as a variable |
| 4917 | static char *reserved[] = { |
| 4918 | "true", |
| 4919 | "false", |
| 4920 | NULL |
| 4921 | }; |
| 4922 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4923 | typedef enum { |
| 4924 | dest_local, |
| 4925 | dest_option, |
| 4926 | dest_env, |
| 4927 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4928 | dest_buffer, |
| 4929 | dest_window, |
| 4930 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4931 | dest_vimvar, |
| 4932 | dest_script, |
| 4933 | dest_reg, |
| 4934 | } assign_dest_T; |
| 4935 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4936 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4937 | * Generate the load instruction for "name". |
| 4938 | */ |
| 4939 | static void |
| 4940 | generate_loadvar( |
| 4941 | cctx_T *cctx, |
| 4942 | assign_dest_T dest, |
| 4943 | char_u *name, |
| 4944 | lvar_T *lvar, |
| 4945 | type_T *type) |
| 4946 | { |
| 4947 | switch (dest) |
| 4948 | { |
| 4949 | case dest_option: |
| 4950 | // TODO: check the option exists |
| 4951 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4952 | break; |
| 4953 | case dest_global: |
| 4954 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4955 | break; |
| 4956 | case dest_buffer: |
| 4957 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4958 | break; |
| 4959 | case dest_window: |
| 4960 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4961 | break; |
| 4962 | case dest_tab: |
| 4963 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4964 | break; |
| 4965 | case dest_script: |
| 4966 | compile_load_scriptvar(cctx, |
| 4967 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4968 | break; |
| 4969 | case dest_env: |
| 4970 | // Include $ in the name here |
| 4971 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4972 | break; |
| 4973 | case dest_reg: |
| 4974 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4975 | break; |
| 4976 | case dest_vimvar: |
| 4977 | generate_LOADV(cctx, name + 2, TRUE); |
| 4978 | break; |
| 4979 | case dest_local: |
| 4980 | if (lvar->lv_from_outer) |
| 4981 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4982 | NULL, type); |
| 4983 | else |
| 4984 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4985 | break; |
| 4986 | } |
| 4987 | } |
| 4988 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4989 | void |
| 4990 | vim9_declare_error(char_u *name) |
| 4991 | { |
| 4992 | char *scope = ""; |
| 4993 | |
| 4994 | switch (*name) |
| 4995 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4996 | case 'g': scope = _("global"); break; |
| 4997 | case 'b': scope = _("buffer"); break; |
| 4998 | case 'w': scope = _("window"); break; |
| 4999 | case 't': scope = _("tab"); break; |
| 5000 | case 'v': scope = "v:"; break; |
| 5001 | case '$': semsg(_(e_declare_env_var), name); return; |
| 5002 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5003 | } |
| 5004 | semsg(_(e_declare_var), scope, name); |
| 5005 | } |
| 5006 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5007 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5008 | * Compile declaration and assignment: |
| 5009 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5010 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5011 | * Return NULL for an error. |
| 5012 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5013 | */ |
| 5014 | static char_u * |
| 5015 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 5016 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5017 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5018 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5019 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5020 | char_u *ret = NULL; |
| 5021 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5022 | int var_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5023 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5024 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5025 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5026 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5027 | int oplen = 0; |
| 5028 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 5029 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5030 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5031 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5032 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5033 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5034 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5035 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 5036 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 5037 | if (p == NULL) |
| 5038 | return *arg == '[' ? arg : NULL; |
| 5039 | |
| 5040 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5041 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 5042 | // TODO: should we allow this, and figure out type inference from list |
| 5043 | // members? |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5044 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5045 | return NULL; |
| 5046 | } |
| 5047 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5048 | sp = p; |
| 5049 | p = skipwhite(p); |
| 5050 | op = p; |
| 5051 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5052 | |
| 5053 | if (var_count > 0 && oplen == 0) |
| 5054 | // can be something like "[1, 2]->func()" |
| 5055 | return arg; |
| 5056 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5057 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 5058 | { |
| 5059 | char_u buf[4]; |
| 5060 | |
| 5061 | vim_strncpy(buf, op, oplen); |
| 5062 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5063 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 5064 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5065 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5066 | if (heredoc) |
| 5067 | { |
| 5068 | list_T *l; |
| 5069 | listitem_T *li; |
| 5070 | |
| 5071 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5072 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5073 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 5074 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5075 | |
| 5076 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 5077 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5078 | { |
| 5079 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 5080 | li->li_tv.vval.v_string = NULL; |
| 5081 | } |
| 5082 | generate_NEWLIST(cctx, l->lv_len); |
| 5083 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5084 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5085 | list_free(l); |
| 5086 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5087 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5088 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5089 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5090 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5091 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 5092 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5093 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5094 | p = skipwhite(op + oplen); |
| 5095 | if (compile_expr0(&p, cctx) == FAIL) |
| 5096 | return NULL; |
| 5097 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5098 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5099 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5100 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5101 | type_T *stacktype; |
| 5102 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5103 | stacktype = stack->ga_len == 0 ? &t_void |
| 5104 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5105 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5106 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5107 | emsg(_(e_cannot_use_void)); |
| 5108 | goto theend; |
| 5109 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5110 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5111 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5112 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5113 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 5114 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5115 | } |
| 5116 | } |
| 5117 | |
| 5118 | /* |
| 5119 | * Loop over variables in "[var, var] = expr". |
| 5120 | * For "var = expr" and "let var: type" this is done only once. |
| 5121 | */ |
| 5122 | if (var_count > 0) |
| 5123 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 5124 | else |
| 5125 | var_start = arg; |
| 5126 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 5127 | { |
| 5128 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 5129 | size_t varlen; |
| 5130 | int new_local = FALSE; |
| 5131 | int opt_type; |
| 5132 | int opt_flags = 0; |
| 5133 | assign_dest_T dest = dest_local; |
| 5134 | int vimvaridx = -1; |
| 5135 | lvar_T *lvar = NULL; |
| 5136 | lvar_T arg_lvar; |
| 5137 | int has_type = FALSE; |
| 5138 | int has_index = FALSE; |
| 5139 | int instr_count = -1; |
| 5140 | |
| 5141 | p = (*var_start == '&' || *var_start == '$' |
| 5142 | || *var_start == '@') ? var_start + 1 : var_start; |
| 5143 | p = to_name_end(p, TRUE); |
| 5144 | |
| 5145 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 5146 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 5147 | --var_end; |
| 5148 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 5149 | --p; |
| 5150 | |
| 5151 | varlen = p - var_start; |
| 5152 | vim_free(name); |
| 5153 | name = vim_strnsave(var_start, varlen); |
| 5154 | if (name == NULL) |
| 5155 | return NULL; |
| 5156 | if (!heredoc) |
| 5157 | type = &t_any; |
| 5158 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5159 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5160 | { |
| 5161 | if (*var_start == '&') |
| 5162 | { |
| 5163 | int cc; |
| 5164 | long numval; |
| 5165 | |
| 5166 | dest = dest_option; |
| 5167 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5168 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5169 | emsg(_(e_const_option)); |
| 5170 | goto theend; |
| 5171 | } |
| 5172 | if (is_decl) |
| 5173 | { |
| 5174 | semsg(_("E1052: Cannot declare an option: %s"), var_start); |
| 5175 | goto theend; |
| 5176 | } |
| 5177 | p = var_start; |
| 5178 | p = find_option_end(&p, &opt_flags); |
| 5179 | if (p == NULL) |
| 5180 | { |
| 5181 | // cannot happen? |
| 5182 | emsg(_(e_letunexp)); |
| 5183 | goto theend; |
| 5184 | } |
| 5185 | cc = *p; |
| 5186 | *p = NUL; |
| 5187 | opt_type = get_option_value(var_start + 1, &numval, |
| 5188 | NULL, opt_flags); |
| 5189 | *p = cc; |
| 5190 | if (opt_type == -3) |
| 5191 | { |
| 5192 | semsg(_(e_unknown_option), var_start); |
| 5193 | goto theend; |
| 5194 | } |
| 5195 | if (opt_type == -2 || opt_type == 0) |
| 5196 | type = &t_string; |
| 5197 | else |
| 5198 | type = &t_number; // both number and boolean option |
| 5199 | } |
| 5200 | else if (*var_start == '$') |
| 5201 | { |
| 5202 | dest = dest_env; |
| 5203 | type = &t_string; |
| 5204 | if (is_decl) |
| 5205 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5206 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5207 | goto theend; |
| 5208 | } |
| 5209 | } |
| 5210 | else if (*var_start == '@') |
| 5211 | { |
| 5212 | if (!valid_yank_reg(var_start[1], TRUE)) |
| 5213 | { |
| 5214 | emsg_invreg(var_start[1]); |
| 5215 | goto theend; |
| 5216 | } |
| 5217 | dest = dest_reg; |
| 5218 | type = &t_string; |
| 5219 | if (is_decl) |
| 5220 | { |
| 5221 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 5222 | goto theend; |
| 5223 | } |
| 5224 | } |
| 5225 | else if (STRNCMP(var_start, "g:", 2) == 0) |
| 5226 | { |
| 5227 | dest = dest_global; |
| 5228 | if (is_decl) |
| 5229 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5230 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5231 | goto theend; |
| 5232 | } |
| 5233 | } |
| 5234 | else if (STRNCMP(var_start, "b:", 2) == 0) |
| 5235 | { |
| 5236 | dest = dest_buffer; |
| 5237 | if (is_decl) |
| 5238 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5239 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5240 | goto theend; |
| 5241 | } |
| 5242 | } |
| 5243 | else if (STRNCMP(var_start, "w:", 2) == 0) |
| 5244 | { |
| 5245 | dest = dest_window; |
| 5246 | if (is_decl) |
| 5247 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5248 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5249 | goto theend; |
| 5250 | } |
| 5251 | } |
| 5252 | else if (STRNCMP(var_start, "t:", 2) == 0) |
| 5253 | { |
| 5254 | dest = dest_tab; |
| 5255 | if (is_decl) |
| 5256 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5257 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5258 | goto theend; |
| 5259 | } |
| 5260 | } |
| 5261 | else if (STRNCMP(var_start, "v:", 2) == 0) |
| 5262 | { |
| 5263 | typval_T *vtv; |
| 5264 | int di_flags; |
| 5265 | |
| 5266 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5267 | if (vimvaridx < 0) |
| 5268 | { |
| 5269 | semsg(_(e_var_notfound), var_start); |
| 5270 | goto theend; |
| 5271 | } |
| 5272 | // We use the current value of "sandbox" here, is that OK? |
| 5273 | if (var_check_ro(di_flags, name, FALSE)) |
| 5274 | goto theend; |
| 5275 | dest = dest_vimvar; |
| 5276 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 5277 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5278 | if (is_decl) |
| 5279 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5280 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5281 | goto theend; |
| 5282 | } |
| 5283 | } |
| 5284 | else |
| 5285 | { |
| 5286 | int idx; |
| 5287 | |
| 5288 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5289 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5290 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5291 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5292 | goto theend; |
| 5293 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5294 | |
| 5295 | lvar = lookup_local(var_start, varlen, cctx); |
| 5296 | if (lvar == NULL) |
| 5297 | { |
| 5298 | CLEAR_FIELD(arg_lvar); |
| 5299 | if (lookup_arg(var_start, varlen, |
| 5300 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 5301 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5302 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5303 | if (is_decl) |
| 5304 | { |
| 5305 | semsg(_(e_used_as_arg), name); |
| 5306 | goto theend; |
| 5307 | } |
| 5308 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5309 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5310 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5311 | if (lvar != NULL) |
| 5312 | { |
| 5313 | if (is_decl) |
| 5314 | { |
| 5315 | semsg(_("E1017: Variable already declared: %s"), name); |
| 5316 | goto theend; |
| 5317 | } |
| 5318 | else if (lvar->lv_const) |
| 5319 | { |
| 5320 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 5321 | name); |
| 5322 | goto theend; |
| 5323 | } |
| 5324 | } |
| 5325 | else if (STRNCMP(var_start, "s:", 2) == 0 |
| 5326 | || lookup_script(var_start, varlen) == OK |
| 5327 | || find_imported(var_start, varlen, cctx) != NULL) |
| 5328 | { |
| 5329 | dest = dest_script; |
| 5330 | if (is_decl) |
| 5331 | { |
| 5332 | semsg(_("E1054: Variable already declared in the script: %s"), |
| 5333 | name); |
| 5334 | goto theend; |
| 5335 | } |
| 5336 | } |
| 5337 | else if (name[1] == ':' && name[2] != NUL) |
| 5338 | { |
| 5339 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
| 5340 | name); |
| 5341 | goto theend; |
| 5342 | } |
| 5343 | else if (!is_decl) |
| 5344 | { |
| 5345 | semsg(_("E1089: unknown variable: %s"), name); |
| 5346 | goto theend; |
| 5347 | } |
| 5348 | } |
| 5349 | } |
| 5350 | |
| 5351 | // handle "a:name" as a name, not index "name" on "a" |
| 5352 | if (varlen > 1 || var_start[varlen] != ':') |
| 5353 | p = var_end; |
| 5354 | |
| 5355 | if (dest != dest_option) |
| 5356 | { |
| 5357 | if (is_decl && *p == ':') |
| 5358 | { |
| 5359 | // parse optional type: "let var: type = expr" |
| 5360 | if (!VIM_ISWHITE(p[1])) |
| 5361 | { |
| 5362 | semsg(_(e_white_after), ":"); |
| 5363 | goto theend; |
| 5364 | } |
| 5365 | p = skipwhite(p + 1); |
| 5366 | type = parse_type(&p, cctx->ctx_type_list); |
| 5367 | has_type = TRUE; |
| 5368 | } |
| 5369 | else if (lvar != NULL) |
| 5370 | type = lvar->lv_type; |
| 5371 | } |
| 5372 | |
| 5373 | if (oplen == 3 && !heredoc && dest != dest_global |
| 5374 | && type->tt_type != VAR_STRING |
| 5375 | && type->tt_type != VAR_ANY) |
| 5376 | { |
| 5377 | emsg(_("E1019: Can only concatenate to string")); |
| 5378 | goto theend; |
| 5379 | } |
| 5380 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5381 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5382 | { |
| 5383 | if (oplen > 1 && !heredoc) |
| 5384 | { |
| 5385 | // +=, /=, etc. require an existing variable |
| 5386 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 5387 | name); |
| 5388 | goto theend; |
| 5389 | } |
| 5390 | |
| 5391 | // new local variable |
| 5392 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
| 5393 | goto theend; |
| 5394 | lvar = reserve_local(cctx, var_start, varlen, |
| 5395 | cmdidx == CMD_const, type); |
| 5396 | if (lvar == NULL) |
| 5397 | goto theend; |
| 5398 | new_local = TRUE; |
| 5399 | } |
| 5400 | |
| 5401 | member_type = type; |
| 5402 | if (var_end > var_start + varlen) |
| 5403 | { |
| 5404 | // Something follows after the variable: "var[idx]". |
| 5405 | if (is_decl) |
| 5406 | { |
| 5407 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 5408 | goto theend; |
| 5409 | } |
| 5410 | |
| 5411 | if (var_start[varlen] == '[') |
| 5412 | { |
| 5413 | has_index = TRUE; |
| 5414 | if (type->tt_member == NULL) |
| 5415 | { |
| 5416 | semsg(_("E1088: cannot use an index on %s"), name); |
| 5417 | goto theend; |
| 5418 | } |
| 5419 | member_type = type->tt_member; |
| 5420 | } |
| 5421 | else |
| 5422 | { |
| 5423 | semsg("Not supported yet: %s", var_start); |
| 5424 | goto theend; |
| 5425 | } |
| 5426 | } |
| 5427 | else if (lvar == &arg_lvar) |
| 5428 | { |
| 5429 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 5430 | goto theend; |
| 5431 | } |
| 5432 | |
| 5433 | if (!heredoc) |
| 5434 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5435 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5436 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5437 | if (oplen > 0 && var_count == 0) |
| 5438 | { |
| 5439 | // skip over the "=" and the expression |
| 5440 | p = skipwhite(op + oplen); |
| 5441 | compile_expr0(&p, cctx); |
| 5442 | } |
| 5443 | } |
| 5444 | else if (oplen > 0) |
| 5445 | { |
| 5446 | type_T *stacktype; |
| 5447 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5448 | // For "var = expr" evaluate the expression. |
| 5449 | if (var_count == 0) |
| 5450 | { |
| 5451 | int r; |
| 5452 | |
| 5453 | // for "+=", "*=", "..=" etc. first load the current value |
| 5454 | if (*op != '=') |
| 5455 | { |
| 5456 | generate_loadvar(cctx, dest, name, lvar, type); |
| 5457 | |
| 5458 | if (has_index) |
| 5459 | { |
| 5460 | // TODO: get member from list or dict |
| 5461 | emsg("Index with operation not supported yet"); |
| 5462 | goto theend; |
| 5463 | } |
| 5464 | } |
| 5465 | |
| 5466 | // Compile the expression. Temporarily hide the new local |
| 5467 | // variable here, it is not available to this expression. |
| 5468 | if (new_local) |
| 5469 | --cctx->ctx_locals.ga_len; |
| 5470 | instr_count = instr->ga_len; |
| 5471 | p = skipwhite(op + oplen); |
| 5472 | r = compile_expr0(&p, cctx); |
| 5473 | if (new_local) |
| 5474 | ++cctx->ctx_locals.ga_len; |
| 5475 | if (r == FAIL) |
| 5476 | goto theend; |
| 5477 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5478 | else if (semicolon && var_idx == var_count - 1) |
| 5479 | { |
| 5480 | // For "[var; var] = expr" get the rest of the list |
| 5481 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5482 | goto theend; |
| 5483 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5484 | else |
| 5485 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5486 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5487 | // list. |
| 5488 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 5489 | return FAIL; |
| 5490 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5491 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5492 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5493 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5494 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5495 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5496 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5497 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5498 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5499 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5500 | emsg(_(e_cannot_use_void)); |
| 5501 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5502 | } |
| 5503 | else |
| 5504 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5505 | // An empty list or dict has a &t_void member, |
| 5506 | // for a variable that implies &t_any. |
| 5507 | if (stacktype == &t_list_empty) |
| 5508 | lvar->lv_type = &t_list_any; |
| 5509 | else if (stacktype == &t_dict_empty) |
| 5510 | lvar->lv_type = &t_dict_any; |
| 5511 | else |
| 5512 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5513 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5514 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5515 | else |
| 5516 | { |
| 5517 | type_T *use_type = lvar->lv_type; |
| 5518 | |
| 5519 | if (has_index) |
| 5520 | { |
| 5521 | use_type = use_type->tt_member; |
| 5522 | if (use_type == NULL) |
| 5523 | use_type = &t_void; |
| 5524 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5525 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5526 | == FAIL) |
| 5527 | goto theend; |
| 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 if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5531 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5532 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5533 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5534 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5535 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5536 | emsg(_(e_const_req_value)); |
| 5537 | goto theend; |
| 5538 | } |
| 5539 | else if (!has_type || dest == dest_option) |
| 5540 | { |
| 5541 | emsg(_(e_type_req)); |
| 5542 | goto theend; |
| 5543 | } |
| 5544 | else |
| 5545 | { |
| 5546 | // variables are always initialized |
| 5547 | if (ga_grow(instr, 1) == FAIL) |
| 5548 | goto theend; |
| 5549 | switch (member_type->tt_type) |
| 5550 | { |
| 5551 | case VAR_BOOL: |
| 5552 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5553 | break; |
| 5554 | case VAR_FLOAT: |
| 5555 | #ifdef FEAT_FLOAT |
| 5556 | generate_PUSHF(cctx, 0.0); |
| 5557 | #endif |
| 5558 | break; |
| 5559 | case VAR_STRING: |
| 5560 | generate_PUSHS(cctx, NULL); |
| 5561 | break; |
| 5562 | case VAR_BLOB: |
| 5563 | generate_PUSHBLOB(cctx, NULL); |
| 5564 | break; |
| 5565 | case VAR_FUNC: |
| 5566 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5567 | break; |
| 5568 | case VAR_LIST: |
| 5569 | generate_NEWLIST(cctx, 0); |
| 5570 | break; |
| 5571 | case VAR_DICT: |
| 5572 | generate_NEWDICT(cctx, 0); |
| 5573 | break; |
| 5574 | case VAR_JOB: |
| 5575 | generate_PUSHJOB(cctx, NULL); |
| 5576 | break; |
| 5577 | case VAR_CHANNEL: |
| 5578 | generate_PUSHCHANNEL(cctx, NULL); |
| 5579 | break; |
| 5580 | case VAR_NUMBER: |
| 5581 | case VAR_UNKNOWN: |
| 5582 | case VAR_ANY: |
| 5583 | case VAR_PARTIAL: |
| 5584 | case VAR_VOID: |
| 5585 | case VAR_SPECIAL: // cannot happen |
| 5586 | generate_PUSHNR(cctx, 0); |
| 5587 | break; |
| 5588 | } |
| 5589 | } |
| 5590 | if (var_count == 0) |
| 5591 | end = p; |
| 5592 | } |
| 5593 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5594 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5595 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5596 | break; |
| 5597 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5598 | if (oplen > 0 && *op != '=') |
| 5599 | { |
| 5600 | type_T *expected = &t_number; |
| 5601 | type_T *stacktype; |
| 5602 | |
| 5603 | // TODO: if type is known use float or any operation |
| 5604 | |
| 5605 | if (*op == '.') |
| 5606 | expected = &t_string; |
| 5607 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5608 | if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5609 | goto theend; |
| 5610 | |
| 5611 | if (*op == '.') |
| 5612 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 5613 | else |
| 5614 | { |
| 5615 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 5616 | |
| 5617 | if (isn == NULL) |
| 5618 | goto theend; |
| 5619 | switch (*op) |
| 5620 | { |
| 5621 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 5622 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 5623 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 5624 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 5625 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 5626 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5627 | } |
| 5628 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5629 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5630 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5631 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5632 | int r; |
| 5633 | |
| 5634 | // Compile the "idx" in "var[idx]". |
| 5635 | if (new_local) |
| 5636 | --cctx->ctx_locals.ga_len; |
| 5637 | p = skipwhite(var_start + varlen + 1); |
| 5638 | r = compile_expr0(&p, cctx); |
| 5639 | if (new_local) |
| 5640 | ++cctx->ctx_locals.ga_len; |
| 5641 | if (r == FAIL) |
| 5642 | goto theend; |
| 5643 | if (*skipwhite(p) != ']') |
| 5644 | { |
| 5645 | emsg(_(e_missbrac)); |
| 5646 | goto theend; |
| 5647 | } |
| 5648 | if (type->tt_type == VAR_DICT |
| 5649 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5650 | goto theend; |
| 5651 | if (type->tt_type == VAR_LIST |
| 5652 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5653 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5654 | { |
| 5655 | emsg(_(e_number_exp)); |
| 5656 | goto theend; |
| 5657 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5658 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5659 | // Load the dict or list. On the stack we then have: |
| 5660 | // - value |
| 5661 | // - index |
| 5662 | // - variable |
| 5663 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5664 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5665 | if (type->tt_type == VAR_LIST) |
| 5666 | { |
| 5667 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5668 | return FAIL; |
| 5669 | } |
| 5670 | else if (type->tt_type == VAR_DICT) |
| 5671 | { |
| 5672 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5673 | return FAIL; |
| 5674 | } |
| 5675 | else |
| 5676 | { |
| 5677 | emsg(_(e_listreq)); |
| 5678 | goto theend; |
| 5679 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5680 | } |
| 5681 | else |
| 5682 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5683 | switch (dest) |
| 5684 | { |
| 5685 | case dest_option: |
| 5686 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5687 | break; |
| 5688 | case dest_global: |
| 5689 | // include g: with the name, easier to execute that way |
| 5690 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5691 | break; |
| 5692 | case dest_buffer: |
| 5693 | // include b: with the name, easier to execute that way |
| 5694 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5695 | break; |
| 5696 | case dest_window: |
| 5697 | // include w: with the name, easier to execute that way |
| 5698 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5699 | break; |
| 5700 | case dest_tab: |
| 5701 | // include t: with the name, easier to execute that way |
| 5702 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5703 | break; |
| 5704 | case dest_env: |
| 5705 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5706 | break; |
| 5707 | case dest_reg: |
| 5708 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5709 | break; |
| 5710 | case dest_vimvar: |
| 5711 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5712 | break; |
| 5713 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5714 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5715 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5716 | imported_T *import = NULL; |
| 5717 | int sid = current_sctx.sc_sid; |
| 5718 | int idx; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5719 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5720 | if (name[1] != ':') |
| 5721 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5722 | import = find_imported(name, 0, cctx); |
| 5723 | if (import != NULL) |
| 5724 | sid = import->imp_sid; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5725 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5726 | |
| 5727 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 5728 | // TODO: specific type |
| 5729 | if (idx < 0) |
| 5730 | { |
| 5731 | char_u *name_s = name; |
| 5732 | |
| 5733 | // Include s: in the name for store_var() |
| 5734 | if (name[1] != ':') |
| 5735 | { |
| 5736 | int len = (int)STRLEN(name) + 3; |
| 5737 | |
| 5738 | name_s = alloc(len); |
| 5739 | if (name_s == NULL) |
| 5740 | name_s = name; |
| 5741 | else |
| 5742 | vim_snprintf((char *)name_s, len, |
| 5743 | "s:%s", name); |
| 5744 | } |
| 5745 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5746 | &t_any); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5747 | if (name_s != name) |
| 5748 | vim_free(name_s); |
| 5749 | } |
| 5750 | else |
| 5751 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5752 | sid, idx, &t_any); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5753 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5754 | break; |
| 5755 | case dest_local: |
| 5756 | if (lvar != NULL) |
| 5757 | { |
| 5758 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5759 | + instr->ga_len - 1; |
| 5760 | |
| 5761 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5762 | // ISN_STORE into ISN_STORENR |
| 5763 | if (!lvar->lv_from_outer |
| 5764 | && instr->ga_len == instr_count + 1 |
| 5765 | && isn->isn_type == ISN_PUSHNR) |
| 5766 | { |
| 5767 | varnumber_T val = isn->isn_arg.number; |
| 5768 | |
| 5769 | isn->isn_type = ISN_STORENR; |
| 5770 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5771 | isn->isn_arg.storenr.stnr_val = val; |
| 5772 | if (stack->ga_len > 0) |
| 5773 | --stack->ga_len; |
| 5774 | } |
| 5775 | else if (lvar->lv_from_outer) |
| 5776 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5777 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5778 | else |
| 5779 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5780 | } |
| 5781 | break; |
| 5782 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5783 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5784 | |
| 5785 | if (var_idx + 1 < var_count) |
| 5786 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5787 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5788 | |
| 5789 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5790 | if (var_count > 0 && !semicolon) |
| 5791 | { |
| 5792 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5793 | goto theend; |
| 5794 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5795 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5796 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5797 | |
| 5798 | theend: |
| 5799 | vim_free(name); |
| 5800 | return ret; |
| 5801 | } |
| 5802 | |
| 5803 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5804 | * Check if "name" can be "unlet". |
| 5805 | */ |
| 5806 | int |
| 5807 | check_vim9_unlet(char_u *name) |
| 5808 | { |
| 5809 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5810 | { |
| 5811 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5812 | return FAIL; |
| 5813 | } |
| 5814 | return OK; |
| 5815 | } |
| 5816 | |
| 5817 | /* |
| 5818 | * Callback passed to ex_unletlock(). |
| 5819 | */ |
| 5820 | static int |
| 5821 | compile_unlet( |
| 5822 | lval_T *lvp, |
| 5823 | char_u *name_end, |
| 5824 | exarg_T *eap, |
| 5825 | int deep UNUSED, |
| 5826 | void *coookie) |
| 5827 | { |
| 5828 | cctx_T *cctx = coookie; |
| 5829 | |
| 5830 | if (lvp->ll_tv == NULL) |
| 5831 | { |
| 5832 | char_u *p = lvp->ll_name; |
| 5833 | int cc = *name_end; |
| 5834 | int ret = OK; |
| 5835 | |
| 5836 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5837 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5838 | if (*p == '$') |
| 5839 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5840 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5841 | ret = FAIL; |
| 5842 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5843 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5844 | |
| 5845 | *name_end = cc; |
| 5846 | return ret; |
| 5847 | } |
| 5848 | |
| 5849 | // TODO: unlet {list}[idx] |
| 5850 | // TODO: unlet {dict}[key] |
| 5851 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5852 | return FAIL; |
| 5853 | } |
| 5854 | |
| 5855 | /* |
| 5856 | * compile "unlet var", "lock var" and "unlock var" |
| 5857 | * "arg" points to "var". |
| 5858 | */ |
| 5859 | static char_u * |
| 5860 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5861 | { |
| 5862 | char_u *p = arg; |
| 5863 | |
| 5864 | if (eap->cmdidx != CMD_unlet) |
| 5865 | { |
| 5866 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5867 | return NULL; |
| 5868 | } |
| 5869 | |
| 5870 | if (*p == '!') |
| 5871 | { |
| 5872 | p = skipwhite(p + 1); |
| 5873 | eap->forceit = TRUE; |
| 5874 | } |
| 5875 | |
| 5876 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5877 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5878 | } |
| 5879 | |
| 5880 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5881 | * Compile an :import command. |
| 5882 | */ |
| 5883 | static char_u * |
| 5884 | compile_import(char_u *arg, cctx_T *cctx) |
| 5885 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5886 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5887 | } |
| 5888 | |
| 5889 | /* |
| 5890 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5891 | */ |
| 5892 | static int |
| 5893 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5894 | { |
| 5895 | garray_T *instr = &cctx->ctx_instr; |
| 5896 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5897 | |
| 5898 | if (endlabel == NULL) |
| 5899 | return FAIL; |
| 5900 | endlabel->el_next = *el; |
| 5901 | *el = endlabel; |
| 5902 | endlabel->el_end_label = instr->ga_len; |
| 5903 | |
| 5904 | generate_JUMP(cctx, when, 0); |
| 5905 | return OK; |
| 5906 | } |
| 5907 | |
| 5908 | static void |
| 5909 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5910 | { |
| 5911 | garray_T *instr = &cctx->ctx_instr; |
| 5912 | |
| 5913 | while (*el != NULL) |
| 5914 | { |
| 5915 | endlabel_T *cur = (*el); |
| 5916 | isn_T *isn; |
| 5917 | |
| 5918 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5919 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5920 | *el = cur->el_next; |
| 5921 | vim_free(cur); |
| 5922 | } |
| 5923 | } |
| 5924 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5925 | static void |
| 5926 | compile_free_jump_to_end(endlabel_T **el) |
| 5927 | { |
| 5928 | while (*el != NULL) |
| 5929 | { |
| 5930 | endlabel_T *cur = (*el); |
| 5931 | |
| 5932 | *el = cur->el_next; |
| 5933 | vim_free(cur); |
| 5934 | } |
| 5935 | } |
| 5936 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5937 | /* |
| 5938 | * Create a new scope and set up the generic items. |
| 5939 | */ |
| 5940 | static scope_T * |
| 5941 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5942 | { |
| 5943 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5944 | |
| 5945 | if (scope == NULL) |
| 5946 | return NULL; |
| 5947 | scope->se_outer = cctx->ctx_scope; |
| 5948 | cctx->ctx_scope = scope; |
| 5949 | scope->se_type = type; |
| 5950 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5951 | return scope; |
| 5952 | } |
| 5953 | |
| 5954 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5955 | * Free the current scope and go back to the outer scope. |
| 5956 | */ |
| 5957 | static void |
| 5958 | drop_scope(cctx_T *cctx) |
| 5959 | { |
| 5960 | scope_T *scope = cctx->ctx_scope; |
| 5961 | |
| 5962 | if (scope == NULL) |
| 5963 | { |
| 5964 | iemsg("calling drop_scope() without a scope"); |
| 5965 | return; |
| 5966 | } |
| 5967 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5968 | switch (scope->se_type) |
| 5969 | { |
| 5970 | case IF_SCOPE: |
| 5971 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5972 | case FOR_SCOPE: |
| 5973 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5974 | case WHILE_SCOPE: |
| 5975 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5976 | case TRY_SCOPE: |
| 5977 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5978 | case NO_SCOPE: |
| 5979 | case BLOCK_SCOPE: |
| 5980 | break; |
| 5981 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5982 | vim_free(scope); |
| 5983 | } |
| 5984 | |
| 5985 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5986 | * compile "if expr" |
| 5987 | * |
| 5988 | * "if expr" Produces instructions: |
| 5989 | * EVAL expr Push result of "expr" |
| 5990 | * JUMP_IF_FALSE end |
| 5991 | * ... body ... |
| 5992 | * end: |
| 5993 | * |
| 5994 | * "if expr | else" Produces instructions: |
| 5995 | * EVAL expr Push result of "expr" |
| 5996 | * JUMP_IF_FALSE else |
| 5997 | * ... body ... |
| 5998 | * JUMP_ALWAYS end |
| 5999 | * else: |
| 6000 | * ... body ... |
| 6001 | * end: |
| 6002 | * |
| 6003 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 6004 | * EVAL expr Push result of "expr" |
| 6005 | * JUMP_IF_FALSE elseif |
| 6006 | * ... body ... |
| 6007 | * JUMP_ALWAYS end |
| 6008 | * elseif: |
| 6009 | * EVAL expr Push result of "expr" |
| 6010 | * JUMP_IF_FALSE else |
| 6011 | * ... body ... |
| 6012 | * JUMP_ALWAYS end |
| 6013 | * else: |
| 6014 | * ... body ... |
| 6015 | * end: |
| 6016 | */ |
| 6017 | static char_u * |
| 6018 | compile_if(char_u *arg, cctx_T *cctx) |
| 6019 | { |
| 6020 | char_u *p = arg; |
| 6021 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6022 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6023 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6024 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6025 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6026 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6027 | CLEAR_FIELD(ppconst); |
| 6028 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6029 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6030 | clear_ppconst(&ppconst); |
| 6031 | return NULL; |
| 6032 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6033 | if (cctx->ctx_skip == SKIP_YES) |
| 6034 | clear_ppconst(&ppconst); |
| 6035 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6036 | { |
| 6037 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6038 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6039 | clear_ppconst(&ppconst); |
| 6040 | } |
| 6041 | else |
| 6042 | { |
| 6043 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6044 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6045 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6046 | return NULL; |
| 6047 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6048 | |
| 6049 | scope = new_scope(cctx, IF_SCOPE); |
| 6050 | if (scope == NULL) |
| 6051 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6052 | scope->se_skip_save = skip_save; |
| 6053 | // "is_had_return" will be reset if any block does not end in :return |
| 6054 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6055 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6056 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6057 | { |
| 6058 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6059 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6060 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6061 | } |
| 6062 | else |
| 6063 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6064 | |
| 6065 | return p; |
| 6066 | } |
| 6067 | |
| 6068 | static char_u * |
| 6069 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 6070 | { |
| 6071 | char_u *p = arg; |
| 6072 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6073 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6074 | isn_T *isn; |
| 6075 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6076 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6077 | |
| 6078 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6079 | { |
| 6080 | emsg(_(e_elseif_without_if)); |
| 6081 | return NULL; |
| 6082 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6083 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6084 | if (!cctx->ctx_had_return) |
| 6085 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6086 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6087 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6088 | { |
| 6089 | 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] | 6090 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6091 | return NULL; |
| 6092 | // previous "if" or "elseif" jumps here |
| 6093 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6094 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6095 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6096 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6097 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6098 | CLEAR_FIELD(ppconst); |
| 6099 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6100 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6101 | clear_ppconst(&ppconst); |
| 6102 | return NULL; |
| 6103 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6104 | if (scope->se_skip_save == SKIP_YES) |
| 6105 | clear_ppconst(&ppconst); |
| 6106 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6107 | { |
| 6108 | // The expression results in a constant. |
| 6109 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6110 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6111 | clear_ppconst(&ppconst); |
| 6112 | scope->se_u.se_if.is_if_label = -1; |
| 6113 | } |
| 6114 | else |
| 6115 | { |
| 6116 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6117 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6118 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6119 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6120 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6121 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6122 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6123 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6124 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6125 | |
| 6126 | return p; |
| 6127 | } |
| 6128 | |
| 6129 | static char_u * |
| 6130 | compile_else(char_u *arg, cctx_T *cctx) |
| 6131 | { |
| 6132 | char_u *p = arg; |
| 6133 | garray_T *instr = &cctx->ctx_instr; |
| 6134 | isn_T *isn; |
| 6135 | scope_T *scope = cctx->ctx_scope; |
| 6136 | |
| 6137 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6138 | { |
| 6139 | emsg(_(e_else_without_if)); |
| 6140 | return NULL; |
| 6141 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6142 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6143 | if (!cctx->ctx_had_return) |
| 6144 | scope->se_u.se_if.is_had_return = FALSE; |
| 6145 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6146 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6147 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6148 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6149 | // 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] | 6150 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6151 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6152 | if (!cctx->ctx_had_return |
| 6153 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6154 | JUMP_ALWAYS, cctx) == FAIL) |
| 6155 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6156 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6157 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6158 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6159 | { |
| 6160 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6161 | { |
| 6162 | // previous "if" or "elseif" jumps here |
| 6163 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6164 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6165 | scope->se_u.se_if.is_if_label = -1; |
| 6166 | } |
| 6167 | } |
| 6168 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6169 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6170 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6171 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6172 | |
| 6173 | return p; |
| 6174 | } |
| 6175 | |
| 6176 | static char_u * |
| 6177 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6178 | { |
| 6179 | scope_T *scope = cctx->ctx_scope; |
| 6180 | ifscope_T *ifscope; |
| 6181 | garray_T *instr = &cctx->ctx_instr; |
| 6182 | isn_T *isn; |
| 6183 | |
| 6184 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6185 | { |
| 6186 | emsg(_(e_endif_without_if)); |
| 6187 | return NULL; |
| 6188 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6189 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6190 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6191 | if (!cctx->ctx_had_return) |
| 6192 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6193 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6194 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6195 | { |
| 6196 | // previous "if" or "elseif" jumps here |
| 6197 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6198 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6199 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6200 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6201 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6202 | cctx->ctx_skip = scope->se_skip_save; |
| 6203 | |
| 6204 | // If all the blocks end in :return and there is an :else then the |
| 6205 | // had_return flag is set. |
| 6206 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6207 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6208 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6209 | return arg; |
| 6210 | } |
| 6211 | |
| 6212 | /* |
| 6213 | * compile "for var in expr" |
| 6214 | * |
| 6215 | * Produces instructions: |
| 6216 | * PUSHNR -1 |
| 6217 | * STORE loop-idx Set index to -1 |
| 6218 | * EVAL expr Push result of "expr" |
| 6219 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6220 | * - if beyond end, jump to "end" |
| 6221 | * - otherwise get item from list and push it |
| 6222 | * STORE var Store item in "var" |
| 6223 | * ... body ... |
| 6224 | * JUMP top Jump back to repeat |
| 6225 | * end: DROP Drop the result of "expr" |
| 6226 | * |
| 6227 | */ |
| 6228 | static char_u * |
| 6229 | compile_for(char_u *arg, cctx_T *cctx) |
| 6230 | { |
| 6231 | char_u *p; |
| 6232 | size_t varlen; |
| 6233 | garray_T *instr = &cctx->ctx_instr; |
| 6234 | garray_T *stack = &cctx->ctx_type_stack; |
| 6235 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6236 | lvar_T *loop_lvar; // loop iteration variable |
| 6237 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6238 | type_T *vartype; |
| 6239 | |
| 6240 | // TODO: list of variables: "for [key, value] in dict" |
| 6241 | // parse "var" |
| 6242 | for (p = arg; eval_isnamec1(*p); ++p) |
| 6243 | ; |
| 6244 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6245 | var_lvar = lookup_local(arg, varlen, cctx); |
| 6246 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6247 | { |
| 6248 | semsg(_("E1023: variable already defined: %s"), arg); |
| 6249 | return NULL; |
| 6250 | } |
| 6251 | |
| 6252 | // consume "in" |
| 6253 | p = skipwhite(p); |
| 6254 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 6255 | { |
| 6256 | emsg(_(e_missing_in)); |
| 6257 | return NULL; |
| 6258 | } |
| 6259 | p = skipwhite(p + 2); |
| 6260 | |
| 6261 | |
| 6262 | scope = new_scope(cctx, FOR_SCOPE); |
| 6263 | if (scope == NULL) |
| 6264 | return NULL; |
| 6265 | |
| 6266 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6267 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6268 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6269 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6270 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6271 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6272 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6273 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6274 | |
| 6275 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6276 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 6277 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6278 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6279 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6280 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6281 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6282 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6283 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6284 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6285 | |
| 6286 | // compile "expr", it remains on the stack until "endfor" |
| 6287 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6288 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6289 | { |
| 6290 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6291 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6292 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6293 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6294 | // Now that we know the type of "var", check that it is a list, now or at |
| 6295 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6296 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 6297 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6298 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6299 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6300 | return NULL; |
| 6301 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6302 | 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] | 6303 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6304 | |
| 6305 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6306 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6307 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6308 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 6309 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6310 | |
| 6311 | return arg; |
| 6312 | } |
| 6313 | |
| 6314 | /* |
| 6315 | * compile "endfor" |
| 6316 | */ |
| 6317 | static char_u * |
| 6318 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 6319 | { |
| 6320 | garray_T *instr = &cctx->ctx_instr; |
| 6321 | scope_T *scope = cctx->ctx_scope; |
| 6322 | forscope_T *forscope; |
| 6323 | isn_T *isn; |
| 6324 | |
| 6325 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 6326 | { |
| 6327 | emsg(_(e_for)); |
| 6328 | return NULL; |
| 6329 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6330 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6331 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6332 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6333 | |
| 6334 | // At end of ":for" scope jump back to the FOR instruction. |
| 6335 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 6336 | |
| 6337 | // Fill in the "end" label in the FOR statement so it can jump here |
| 6338 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 6339 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 6340 | |
| 6341 | // Fill in the "end" label any BREAK statements |
| 6342 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 6343 | |
| 6344 | // Below the ":for" scope drop the "expr" list from the stack. |
| 6345 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 6346 | return NULL; |
| 6347 | |
| 6348 | vim_free(scope); |
| 6349 | |
| 6350 | return arg; |
| 6351 | } |
| 6352 | |
| 6353 | /* |
| 6354 | * compile "while expr" |
| 6355 | * |
| 6356 | * Produces instructions: |
| 6357 | * top: EVAL expr Push result of "expr" |
| 6358 | * JUMP_IF_FALSE end jump if false |
| 6359 | * ... body ... |
| 6360 | * JUMP top Jump back to repeat |
| 6361 | * end: |
| 6362 | * |
| 6363 | */ |
| 6364 | static char_u * |
| 6365 | compile_while(char_u *arg, cctx_T *cctx) |
| 6366 | { |
| 6367 | char_u *p = arg; |
| 6368 | garray_T *instr = &cctx->ctx_instr; |
| 6369 | scope_T *scope; |
| 6370 | |
| 6371 | scope = new_scope(cctx, WHILE_SCOPE); |
| 6372 | if (scope == NULL) |
| 6373 | return NULL; |
| 6374 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6375 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6376 | |
| 6377 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6378 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6379 | return NULL; |
| 6380 | |
| 6381 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6382 | 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] | 6383 | JUMP_IF_FALSE, cctx) == FAIL) |
| 6384 | return FAIL; |
| 6385 | |
| 6386 | return p; |
| 6387 | } |
| 6388 | |
| 6389 | /* |
| 6390 | * compile "endwhile" |
| 6391 | */ |
| 6392 | static char_u * |
| 6393 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 6394 | { |
| 6395 | scope_T *scope = cctx->ctx_scope; |
| 6396 | |
| 6397 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 6398 | { |
| 6399 | emsg(_(e_while)); |
| 6400 | return NULL; |
| 6401 | } |
| 6402 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6403 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6404 | |
| 6405 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6406 | 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] | 6407 | |
| 6408 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 6409 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6410 | 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] | 6411 | |
| 6412 | vim_free(scope); |
| 6413 | |
| 6414 | return arg; |
| 6415 | } |
| 6416 | |
| 6417 | /* |
| 6418 | * compile "continue" |
| 6419 | */ |
| 6420 | static char_u * |
| 6421 | compile_continue(char_u *arg, cctx_T *cctx) |
| 6422 | { |
| 6423 | scope_T *scope = cctx->ctx_scope; |
| 6424 | |
| 6425 | for (;;) |
| 6426 | { |
| 6427 | if (scope == NULL) |
| 6428 | { |
| 6429 | emsg(_(e_continue)); |
| 6430 | return NULL; |
| 6431 | } |
| 6432 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6433 | break; |
| 6434 | scope = scope->se_outer; |
| 6435 | } |
| 6436 | |
| 6437 | // Jump back to the FOR or WHILE instruction. |
| 6438 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6439 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 6440 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6441 | return arg; |
| 6442 | } |
| 6443 | |
| 6444 | /* |
| 6445 | * compile "break" |
| 6446 | */ |
| 6447 | static char_u * |
| 6448 | compile_break(char_u *arg, cctx_T *cctx) |
| 6449 | { |
| 6450 | scope_T *scope = cctx->ctx_scope; |
| 6451 | endlabel_T **el; |
| 6452 | |
| 6453 | for (;;) |
| 6454 | { |
| 6455 | if (scope == NULL) |
| 6456 | { |
| 6457 | emsg(_(e_break)); |
| 6458 | return NULL; |
| 6459 | } |
| 6460 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6461 | break; |
| 6462 | scope = scope->se_outer; |
| 6463 | } |
| 6464 | |
| 6465 | // Jump to the end of the FOR or WHILE loop. |
| 6466 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6467 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6468 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6469 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6470 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 6471 | return FAIL; |
| 6472 | |
| 6473 | return arg; |
| 6474 | } |
| 6475 | |
| 6476 | /* |
| 6477 | * compile "{" start of block |
| 6478 | */ |
| 6479 | static char_u * |
| 6480 | compile_block(char_u *arg, cctx_T *cctx) |
| 6481 | { |
| 6482 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6483 | return NULL; |
| 6484 | return skipwhite(arg + 1); |
| 6485 | } |
| 6486 | |
| 6487 | /* |
| 6488 | * compile end of block: drop one scope |
| 6489 | */ |
| 6490 | static void |
| 6491 | compile_endblock(cctx_T *cctx) |
| 6492 | { |
| 6493 | scope_T *scope = cctx->ctx_scope; |
| 6494 | |
| 6495 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6496 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6497 | vim_free(scope); |
| 6498 | } |
| 6499 | |
| 6500 | /* |
| 6501 | * compile "try" |
| 6502 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6503 | * finally. |
| 6504 | * Creates another scope for the "try" block itself. |
| 6505 | * TRY instruction sets up exception handling at runtime. |
| 6506 | * |
| 6507 | * "try" |
| 6508 | * TRY -> catch1, -> finally push trystack entry |
| 6509 | * ... try block |
| 6510 | * "throw {exception}" |
| 6511 | * EVAL {exception} |
| 6512 | * THROW create exception |
| 6513 | * ... try block |
| 6514 | * " catch {expr}" |
| 6515 | * JUMP -> finally |
| 6516 | * catch1: PUSH exeception |
| 6517 | * EVAL {expr} |
| 6518 | * MATCH |
| 6519 | * JUMP nomatch -> catch2 |
| 6520 | * CATCH remove exception |
| 6521 | * ... catch block |
| 6522 | * " catch" |
| 6523 | * JUMP -> finally |
| 6524 | * catch2: CATCH remove exception |
| 6525 | * ... catch block |
| 6526 | * " finally" |
| 6527 | * finally: |
| 6528 | * ... finally block |
| 6529 | * " endtry" |
| 6530 | * ENDTRY pop trystack entry, may rethrow |
| 6531 | */ |
| 6532 | static char_u * |
| 6533 | compile_try(char_u *arg, cctx_T *cctx) |
| 6534 | { |
| 6535 | garray_T *instr = &cctx->ctx_instr; |
| 6536 | scope_T *try_scope; |
| 6537 | scope_T *scope; |
| 6538 | |
| 6539 | // scope that holds the jumps that go to catch/finally/endtry |
| 6540 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6541 | if (try_scope == NULL) |
| 6542 | return NULL; |
| 6543 | |
| 6544 | // "catch" is set when the first ":catch" is found. |
| 6545 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6546 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6547 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6548 | return NULL; |
| 6549 | |
| 6550 | // scope for the try block itself |
| 6551 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6552 | if (scope == NULL) |
| 6553 | return NULL; |
| 6554 | |
| 6555 | return arg; |
| 6556 | } |
| 6557 | |
| 6558 | /* |
| 6559 | * compile "catch {expr}" |
| 6560 | */ |
| 6561 | static char_u * |
| 6562 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6563 | { |
| 6564 | scope_T *scope = cctx->ctx_scope; |
| 6565 | garray_T *instr = &cctx->ctx_instr; |
| 6566 | char_u *p; |
| 6567 | isn_T *isn; |
| 6568 | |
| 6569 | // end block scope from :try or :catch |
| 6570 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6571 | compile_endblock(cctx); |
| 6572 | scope = cctx->ctx_scope; |
| 6573 | |
| 6574 | // Error if not in a :try scope |
| 6575 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6576 | { |
| 6577 | emsg(_(e_catch)); |
| 6578 | return NULL; |
| 6579 | } |
| 6580 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6581 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6582 | { |
| 6583 | emsg(_("E1033: catch unreachable after catch-all")); |
| 6584 | return NULL; |
| 6585 | } |
| 6586 | |
| 6587 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6588 | 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] | 6589 | JUMP_ALWAYS, cctx) == FAIL) |
| 6590 | return NULL; |
| 6591 | |
| 6592 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6593 | 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] | 6594 | if (isn->isn_arg.try.try_catch == 0) |
| 6595 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6596 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6597 | { |
| 6598 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6599 | 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] | 6600 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6601 | } |
| 6602 | |
| 6603 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6604 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6605 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6606 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6607 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6608 | } |
| 6609 | else |
| 6610 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6611 | char_u *end; |
| 6612 | char_u *pat; |
| 6613 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6614 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6615 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6616 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6617 | // Push v:exception, push {expr} and MATCH |
| 6618 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6619 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6620 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6621 | if (*end != *p) |
| 6622 | { |
| 6623 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 6624 | vim_free(tofree); |
| 6625 | return FAIL; |
| 6626 | } |
| 6627 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6628 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6629 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6630 | len = (int)(end - tofree); |
| 6631 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6632 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6633 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6634 | if (pat == NULL) |
| 6635 | return FAIL; |
| 6636 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6637 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6638 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6639 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6640 | return NULL; |
| 6641 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6642 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6643 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6644 | return NULL; |
| 6645 | } |
| 6646 | |
| 6647 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6648 | return NULL; |
| 6649 | |
| 6650 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6651 | return NULL; |
| 6652 | return p; |
| 6653 | } |
| 6654 | |
| 6655 | static char_u * |
| 6656 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6657 | { |
| 6658 | scope_T *scope = cctx->ctx_scope; |
| 6659 | garray_T *instr = &cctx->ctx_instr; |
| 6660 | isn_T *isn; |
| 6661 | |
| 6662 | // end block scope from :try or :catch |
| 6663 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6664 | compile_endblock(cctx); |
| 6665 | scope = cctx->ctx_scope; |
| 6666 | |
| 6667 | // Error if not in a :try scope |
| 6668 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6669 | { |
| 6670 | emsg(_(e_finally)); |
| 6671 | return NULL; |
| 6672 | } |
| 6673 | |
| 6674 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6675 | 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] | 6676 | if (isn->isn_arg.try.try_finally != 0) |
| 6677 | { |
| 6678 | emsg(_(e_finally_dup)); |
| 6679 | return NULL; |
| 6680 | } |
| 6681 | |
| 6682 | // 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] | 6683 | 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] | 6684 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6685 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6686 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6687 | { |
| 6688 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6689 | 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] | 6690 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6691 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6692 | } |
| 6693 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6694 | // TODO: set index in ts_finally_label jumps |
| 6695 | |
| 6696 | return arg; |
| 6697 | } |
| 6698 | |
| 6699 | static char_u * |
| 6700 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6701 | { |
| 6702 | scope_T *scope = cctx->ctx_scope; |
| 6703 | garray_T *instr = &cctx->ctx_instr; |
| 6704 | isn_T *isn; |
| 6705 | |
| 6706 | // end block scope from :catch or :finally |
| 6707 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6708 | compile_endblock(cctx); |
| 6709 | scope = cctx->ctx_scope; |
| 6710 | |
| 6711 | // Error if not in a :try scope |
| 6712 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6713 | { |
| 6714 | if (scope == NULL) |
| 6715 | emsg(_(e_no_endtry)); |
| 6716 | else if (scope->se_type == WHILE_SCOPE) |
| 6717 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6718 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6719 | emsg(_(e_endfor)); |
| 6720 | else |
| 6721 | emsg(_(e_endif)); |
| 6722 | return NULL; |
| 6723 | } |
| 6724 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6725 | 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] | 6726 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6727 | { |
| 6728 | emsg(_("E1032: missing :catch or :finally")); |
| 6729 | return NULL; |
| 6730 | } |
| 6731 | |
| 6732 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6733 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6734 | 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] | 6735 | |
| 6736 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6737 | if (isn->isn_arg.try.try_catch == 0) |
| 6738 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6739 | if (isn->isn_arg.try.try_finally == 0) |
| 6740 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6741 | |
| 6742 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6743 | { |
| 6744 | // Last catch without match jumps here |
| 6745 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6746 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6747 | } |
| 6748 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6749 | compile_endblock(cctx); |
| 6750 | |
| 6751 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6752 | return NULL; |
| 6753 | return arg; |
| 6754 | } |
| 6755 | |
| 6756 | /* |
| 6757 | * compile "throw {expr}" |
| 6758 | */ |
| 6759 | static char_u * |
| 6760 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6761 | { |
| 6762 | char_u *p = skipwhite(arg); |
| 6763 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6764 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6765 | return NULL; |
| 6766 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6767 | return NULL; |
| 6768 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6769 | return NULL; |
| 6770 | |
| 6771 | return p; |
| 6772 | } |
| 6773 | |
| 6774 | /* |
| 6775 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6776 | * compile "echomsg expr" |
| 6777 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6778 | * compile "execute expr" |
| 6779 | */ |
| 6780 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6781 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6782 | { |
| 6783 | char_u *p = arg; |
| 6784 | int count = 0; |
| 6785 | |
| 6786 | for (;;) |
| 6787 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6788 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6789 | return NULL; |
| 6790 | ++count; |
| 6791 | p = skipwhite(p); |
| 6792 | if (ends_excmd(*p)) |
| 6793 | break; |
| 6794 | } |
| 6795 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6796 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6797 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6798 | else if (cmdidx == CMD_execute) |
| 6799 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6800 | else if (cmdidx == CMD_echomsg) |
| 6801 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6802 | else |
| 6803 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6804 | return p; |
| 6805 | } |
| 6806 | |
| 6807 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6808 | * A command that is not compiled, execute with legacy code. |
| 6809 | */ |
| 6810 | static char_u * |
| 6811 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6812 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6813 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6814 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6815 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6816 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6817 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6818 | goto theend; |
| 6819 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6820 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6821 | { |
| 6822 | long argt = excmd_get_argt(eap->cmdidx); |
| 6823 | int usefilter = FALSE; |
| 6824 | |
| 6825 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6826 | |
| 6827 | // If the command can be followed by a bar, find the bar and truncate |
| 6828 | // it, so that the following command can be compiled. |
| 6829 | // The '|' is overwritten with a NUL, it is put back below. |
| 6830 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6831 | && *eap->arg == '!') |
| 6832 | // :w !filter or :r !filter or :r! filter |
| 6833 | usefilter = TRUE; |
| 6834 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6835 | { |
| 6836 | separate_nextcmd(eap); |
| 6837 | if (eap->nextcmd != NULL) |
| 6838 | nextcmd = eap->nextcmd; |
| 6839 | } |
| 6840 | } |
| 6841 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6842 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6843 | { |
| 6844 | // expand filename in "syntax include [@group] filename" |
| 6845 | has_expr = TRUE; |
| 6846 | eap->arg = skipwhite(eap->arg + 7); |
| 6847 | if (*eap->arg == '@') |
| 6848 | eap->arg = skiptowhite(eap->arg); |
| 6849 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6850 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6851 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6852 | { |
| 6853 | int count = 0; |
| 6854 | char_u *start = skipwhite(line); |
| 6855 | |
| 6856 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6857 | // PUSHS ":cmd xxx" |
| 6858 | // eval expr1 |
| 6859 | // PUSHS "yyy" |
| 6860 | // eval expr2 |
| 6861 | // PUSHS "zzz" |
| 6862 | // EXECCONCAT 5 |
| 6863 | for (;;) |
| 6864 | { |
| 6865 | if (p > start) |
| 6866 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6867 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6868 | ++count; |
| 6869 | } |
| 6870 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6871 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6872 | return NULL; |
| 6873 | may_generate_2STRING(-1, cctx); |
| 6874 | ++count; |
| 6875 | p = skipwhite(p); |
| 6876 | if (*p != '`') |
| 6877 | { |
| 6878 | emsg(_("E1083: missing backtick")); |
| 6879 | return NULL; |
| 6880 | } |
| 6881 | start = p + 1; |
| 6882 | |
| 6883 | p = (char_u *)strstr((char *)start, "`="); |
| 6884 | if (p == NULL) |
| 6885 | { |
| 6886 | if (*skipwhite(start) != NUL) |
| 6887 | { |
| 6888 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6889 | ++count; |
| 6890 | } |
| 6891 | break; |
| 6892 | } |
| 6893 | } |
| 6894 | generate_EXECCONCAT(cctx, count); |
| 6895 | } |
| 6896 | else |
| 6897 | generate_EXEC(cctx, line); |
| 6898 | |
| 6899 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6900 | if (*nextcmd != NUL) |
| 6901 | { |
| 6902 | // the parser expects a pointer to the bar, put it back |
| 6903 | --nextcmd; |
| 6904 | *nextcmd = '|'; |
| 6905 | } |
| 6906 | |
| 6907 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6908 | } |
| 6909 | |
| 6910 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6911 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6912 | * 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] | 6913 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6914 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6915 | add_def_function(ufunc_T *ufunc) |
| 6916 | { |
| 6917 | dfunc_T *dfunc; |
| 6918 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6919 | if (def_functions.ga_len == 0) |
| 6920 | { |
| 6921 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6922 | // wasn't set. |
| 6923 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6924 | return FAIL; |
| 6925 | ++def_functions.ga_len; |
| 6926 | } |
| 6927 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6928 | // Add the function to "def_functions". |
| 6929 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6930 | return FAIL; |
| 6931 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6932 | CLEAR_POINTER(dfunc); |
| 6933 | dfunc->df_idx = def_functions.ga_len; |
| 6934 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6935 | dfunc->df_ufunc = ufunc; |
| 6936 | ++def_functions.ga_len; |
| 6937 | return OK; |
| 6938 | } |
| 6939 | |
| 6940 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6941 | * After ex_function() has collected all the function lines: parse and compile |
| 6942 | * the lines into instructions. |
| 6943 | * Adds the function to "def_functions". |
| 6944 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6945 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6946 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6947 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6948 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6949 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6950 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6951 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6952 | 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] | 6953 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6954 | char_u *line = NULL; |
| 6955 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6956 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6957 | cctx_T cctx; |
| 6958 | garray_T *instr; |
| 6959 | int called_emsg_before = called_emsg; |
| 6960 | int ret = FAIL; |
| 6961 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6962 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6963 | int emsg_before = called_emsg; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6964 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6965 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6966 | // When using a function that was compiled before: Free old instructions. |
| 6967 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6968 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6969 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6970 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6971 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6972 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6973 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6974 | else |
| 6975 | { |
| 6976 | if (add_def_function(ufunc) == FAIL) |
| 6977 | return FAIL; |
| 6978 | new_def_function = TRUE; |
| 6979 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6980 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6981 | ufunc->uf_def_status = UF_COMPILING; |
| 6982 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6983 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6984 | cctx.ctx_ufunc = ufunc; |
| 6985 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6986 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6987 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6988 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6989 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6990 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6991 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6992 | instr = &cctx.ctx_instr; |
| 6993 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6994 | // Set the context to the function, it may be compiled when called from |
| 6995 | // another script. Set the script version to the most modern one. |
| 6996 | // The line number will be set in next_line_from_context(). |
| 6997 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6998 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6999 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7000 | // Make sure error messages are OK. |
| 7001 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 7002 | if (do_estack_push) |
| 7003 | estack_push_ufunc(ufunc, 1); |
| 7004 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7005 | if (ufunc->uf_def_args.ga_len > 0) |
| 7006 | { |
| 7007 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7008 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7009 | int i; |
| 7010 | char_u *arg; |
| 7011 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7012 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7013 | |
| 7014 | // Produce instructions for the default values of optional arguments. |
| 7015 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 7016 | // where to start when the function is called, depending on the number |
| 7017 | // of arguments. |
| 7018 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 7019 | if (ufunc->uf_def_arg_idx == NULL) |
| 7020 | goto erret; |
| 7021 | for (i = 0; i < count; ++i) |
| 7022 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7023 | garray_T *stack = &cctx.ctx_type_stack; |
| 7024 | type_T *val_type; |
| 7025 | int arg_idx = first_def_arg + i; |
| 7026 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7027 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 7028 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7029 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7030 | goto erret; |
| 7031 | |
| 7032 | // If no type specified use the type of the default value. |
| 7033 | // Otherwise check that the default value type matches the |
| 7034 | // specified type. |
| 7035 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 7036 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7037 | { |
| 7038 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7039 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7040 | } |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 7041 | 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] | 7042 | == FAIL) |
| 7043 | { |
| 7044 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 7045 | arg_idx + 1); |
| 7046 | goto erret; |
| 7047 | } |
| 7048 | |
| 7049 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7050 | goto erret; |
| 7051 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7052 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7053 | |
| 7054 | if (did_set_arg_type) |
| 7055 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7056 | } |
| 7057 | |
| 7058 | /* |
| 7059 | * Loop over all the lines of the function and generate instructions. |
| 7060 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7061 | for (;;) |
| 7062 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7063 | exarg_T ea; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7064 | int starts_with_colon = FALSE; |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7065 | char_u *cmd; |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7066 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7067 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7068 | // Bail out on the first error to avoid a flood of errors and report |
| 7069 | // the right line number when inside try/catch. |
| 7070 | if (emsg_before != called_emsg) |
| 7071 | goto erret; |
| 7072 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7073 | if (line != NULL && *line == '|') |
| 7074 | // the line continues after a '|' |
| 7075 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 7076 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7077 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 7078 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7079 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 7080 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7081 | goto erret; |
| 7082 | } |
| 7083 | else |
| 7084 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 7085 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 7086 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7087 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7088 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7089 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7090 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7091 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7092 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7093 | ea.cmdlinep = &line; |
| 7094 | ea.cmd = skipwhite(line); |
| 7095 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7096 | // Some things can be recognized by the first character. |
| 7097 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7098 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7099 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7100 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7101 | if (ea.cmd[1] != '{') |
| 7102 | { |
| 7103 | line = (char_u *)""; |
| 7104 | continue; |
| 7105 | } |
| 7106 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7107 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7108 | case '}': |
| 7109 | { |
| 7110 | // "}" ends a block scope |
| 7111 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7112 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
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 | if (stype == BLOCK_SCOPE) |
| 7115 | { |
| 7116 | compile_endblock(&cctx); |
| 7117 | line = ea.cmd; |
| 7118 | } |
| 7119 | else |
| 7120 | { |
| 7121 | emsg(_("E1025: using } outside of a block scope")); |
| 7122 | goto erret; |
| 7123 | } |
| 7124 | if (line != NULL) |
| 7125 | line = skipwhite(ea.cmd + 1); |
| 7126 | continue; |
| 7127 | } |
| 7128 | |
| 7129 | case '{': |
| 7130 | // "{" starts a block scope |
| 7131 | // "{'a': 1}->func() is something else |
| 7132 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7133 | { |
| 7134 | line = compile_block(ea.cmd, &cctx); |
| 7135 | continue; |
| 7136 | } |
| 7137 | break; |
| 7138 | |
| 7139 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7140 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7141 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7142 | } |
| 7143 | |
| 7144 | /* |
| 7145 | * COMMAND MODIFIERS |
| 7146 | */ |
| 7147 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 7148 | { |
| 7149 | if (errormsg != NULL) |
| 7150 | goto erret; |
| 7151 | // empty line or comment |
| 7152 | line = (char_u *)""; |
| 7153 | continue; |
| 7154 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7155 | // TODO: use modifiers in the command |
| 7156 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | b074e8b | 2020-07-11 13:40:45 +0200 | [diff] [blame] | 7157 | CLEAR_FIELD(cmdmod); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7158 | |
| 7159 | // Skip ":call" to get to the function name. |
| 7160 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 7161 | ea.cmd = skipwhite(ea.cmd); |
| 7162 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7163 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7164 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7165 | char_u *pskip; |
| 7166 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7167 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7168 | // find what follows. |
| 7169 | // Skip over "var.member", "var[idx]" and the like. |
| 7170 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 7171 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7172 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7173 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7174 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7175 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7176 | char_u *var_end; |
| 7177 | int oplen; |
| 7178 | int heredoc; |
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 | var_end = find_name_end(pskip, NULL, NULL, |
| 7181 | FNE_CHECK_START | FNE_INCL_BR); |
| 7182 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7183 | if (oplen > 0) |
| 7184 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7185 | size_t len = p - ea.cmd; |
| 7186 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7187 | // Recognize an assignment if we recognize the variable |
| 7188 | // name: |
| 7189 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 7190 | // "local = expr" where "local" is a local var. |
| 7191 | // "script = expr" where "script" is a script-local var. |
| 7192 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7193 | // "&opt = expr" |
| 7194 | // "$ENV = expr" |
| 7195 | // "@r = expr" |
| 7196 | if (*ea.cmd == '&' |
| 7197 | || *ea.cmd == '$' |
| 7198 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7199 | || ((len) > 2 && ea.cmd[1] == ':') |
| 7200 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 7201 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 7202 | NULL, &cctx) == OK |
| 7203 | || lookup_script(ea.cmd, len) == OK |
| 7204 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7205 | { |
| 7206 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7207 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7208 | goto erret; |
| 7209 | continue; |
| 7210 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7211 | } |
| 7212 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7213 | |
| 7214 | if (*ea.cmd == '[') |
| 7215 | { |
| 7216 | // [var, var] = expr |
| 7217 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 7218 | if (line == NULL) |
| 7219 | goto erret; |
| 7220 | if (line != ea.cmd) |
| 7221 | continue; |
| 7222 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7223 | } |
| 7224 | |
| 7225 | /* |
| 7226 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7227 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7228 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7229 | cmd = ea.cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7230 | if (*cmd != '\'') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7231 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7232 | ea.cmd = skip_range(ea.cmd, NULL); |
| 7233 | if (ea.cmd > cmd && !starts_with_colon) |
| 7234 | { |
| 7235 | emsg(_(e_colon_required)); |
| 7236 | goto erret; |
| 7237 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7238 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7239 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7240 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7241 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7242 | |
| 7243 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 7244 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7245 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7246 | { |
| 7247 | line += STRLEN(line); |
| 7248 | continue; |
| 7249 | } |
| 7250 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7251 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7252 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7253 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7254 | // CMD_let cannot happen, compile_assignment() above is used |
| 7255 | iemsg("Command from find_ex_command() not handled"); |
| 7256 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7257 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7258 | } |
| 7259 | |
| 7260 | p = skipwhite(p); |
| 7261 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7262 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 7263 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7264 | && ea.cmdidx != CMD_elseif |
| 7265 | && ea.cmdidx != CMD_else |
| 7266 | && ea.cmdidx != CMD_endif) |
| 7267 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7268 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7269 | continue; |
| 7270 | } |
| 7271 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7272 | if (ea.cmdidx != CMD_elseif |
| 7273 | && ea.cmdidx != CMD_else |
| 7274 | && ea.cmdidx != CMD_endif |
| 7275 | && ea.cmdidx != CMD_endfor |
| 7276 | && ea.cmdidx != CMD_endwhile |
| 7277 | && ea.cmdidx != CMD_catch |
| 7278 | && ea.cmdidx != CMD_finally |
| 7279 | && ea.cmdidx != CMD_endtry) |
| 7280 | { |
| 7281 | if (cctx.ctx_had_return) |
| 7282 | { |
| 7283 | emsg(_("E1095: Unreachable code after :return")); |
| 7284 | goto erret; |
| 7285 | } |
| 7286 | } |
| 7287 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7288 | switch (ea.cmdidx) |
| 7289 | { |
| 7290 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7291 | ea.arg = p; |
| 7292 | line = compile_nested_function(&ea, &cctx); |
| 7293 | break; |
| 7294 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7295 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7296 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7297 | goto erret; |
| 7298 | |
| 7299 | case CMD_return: |
| 7300 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7301 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7302 | break; |
| 7303 | |
| 7304 | case CMD_let: |
| 7305 | case CMD_const: |
| 7306 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7307 | if (line == p) |
| 7308 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7309 | break; |
| 7310 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7311 | case CMD_unlet: |
| 7312 | case CMD_unlockvar: |
| 7313 | case CMD_lockvar: |
| 7314 | line = compile_unletlock(p, &ea, &cctx); |
| 7315 | break; |
| 7316 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7317 | case CMD_import: |
| 7318 | line = compile_import(p, &cctx); |
| 7319 | break; |
| 7320 | |
| 7321 | case CMD_if: |
| 7322 | line = compile_if(p, &cctx); |
| 7323 | break; |
| 7324 | case CMD_elseif: |
| 7325 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7326 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7327 | break; |
| 7328 | case CMD_else: |
| 7329 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7330 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7331 | break; |
| 7332 | case CMD_endif: |
| 7333 | line = compile_endif(p, &cctx); |
| 7334 | break; |
| 7335 | |
| 7336 | case CMD_while: |
| 7337 | line = compile_while(p, &cctx); |
| 7338 | break; |
| 7339 | case CMD_endwhile: |
| 7340 | line = compile_endwhile(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 | |
| 7344 | case CMD_for: |
| 7345 | line = compile_for(p, &cctx); |
| 7346 | break; |
| 7347 | case CMD_endfor: |
| 7348 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7349 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7350 | break; |
| 7351 | case CMD_continue: |
| 7352 | line = compile_continue(p, &cctx); |
| 7353 | break; |
| 7354 | case CMD_break: |
| 7355 | line = compile_break(p, &cctx); |
| 7356 | break; |
| 7357 | |
| 7358 | case CMD_try: |
| 7359 | line = compile_try(p, &cctx); |
| 7360 | break; |
| 7361 | case CMD_catch: |
| 7362 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7363 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7364 | break; |
| 7365 | case CMD_finally: |
| 7366 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7367 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7368 | break; |
| 7369 | case CMD_endtry: |
| 7370 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7371 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7372 | break; |
| 7373 | case CMD_throw: |
| 7374 | line = compile_throw(p, &cctx); |
| 7375 | break; |
| 7376 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7377 | case CMD_eval: |
| 7378 | if (compile_expr0(&p, &cctx) == FAIL) |
| 7379 | goto erret; |
| 7380 | |
| 7381 | // drop the return value |
| 7382 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 7383 | |
| 7384 | line = skipwhite(p); |
| 7385 | break; |
| 7386 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7387 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7388 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7389 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7390 | case CMD_echomsg: |
| 7391 | case CMD_echoerr: |
| 7392 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7393 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7394 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7395 | // TODO: other commands with an expression argument |
| 7396 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7397 | case CMD_SIZE: |
| 7398 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 7399 | goto erret; |
| 7400 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7401 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7402 | // Not recognized, execute with do_cmdline_cmd(). |
| 7403 | ea.arg = p; |
| 7404 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7405 | break; |
| 7406 | } |
| 7407 | if (line == NULL) |
| 7408 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7409 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7410 | |
| 7411 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7412 | { |
| 7413 | iemsg("Type stack underflow"); |
| 7414 | goto erret; |
| 7415 | } |
| 7416 | } |
| 7417 | |
| 7418 | if (cctx.ctx_scope != NULL) |
| 7419 | { |
| 7420 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7421 | emsg(_(e_endif)); |
| 7422 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7423 | emsg(_(e_endwhile)); |
| 7424 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7425 | emsg(_(e_endfor)); |
| 7426 | else |
| 7427 | emsg(_("E1026: Missing }")); |
| 7428 | goto erret; |
| 7429 | } |
| 7430 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7431 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7432 | { |
| 7433 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7434 | { |
| 7435 | emsg(_("E1027: Missing return statement")); |
| 7436 | goto erret; |
| 7437 | } |
| 7438 | |
| 7439 | // Return zero if there is no return at the end. |
| 7440 | generate_PUSHNR(&cctx, 0); |
| 7441 | generate_instr(&cctx, ISN_RETURN); |
| 7442 | } |
| 7443 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7444 | { |
| 7445 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7446 | + ufunc->uf_dfunc_idx; |
| 7447 | dfunc->df_deleted = FALSE; |
| 7448 | dfunc->df_instr = instr->ga_data; |
| 7449 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7450 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 7451 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7452 | if (cctx.ctx_outer_used) |
| 7453 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7454 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7455 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7456 | |
| 7457 | ret = OK; |
| 7458 | |
| 7459 | erret: |
| 7460 | if (ret == FAIL) |
| 7461 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7462 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7463 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7464 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7465 | |
| 7466 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7467 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7468 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7469 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7470 | // If using the last entry in the table and it was added above, we |
| 7471 | // might as well remove it. |
| 7472 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7473 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7474 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7475 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7476 | ufunc->uf_dfunc_idx = 0; |
| 7477 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7478 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7479 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7480 | while (cctx.ctx_scope != NULL) |
| 7481 | drop_scope(&cctx); |
| 7482 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7483 | // Don't execute this function body. |
| 7484 | ga_clear_strings(&ufunc->uf_lines); |
| 7485 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7486 | if (errormsg != NULL) |
| 7487 | emsg(errormsg); |
| 7488 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 7489 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7490 | } |
| 7491 | |
| 7492 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7493 | if (do_estack_push) |
| 7494 | estack_pop(); |
| 7495 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7496 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7497 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7498 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7499 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7500 | } |
| 7501 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7502 | void |
| 7503 | set_function_type(ufunc_T *ufunc) |
| 7504 | { |
| 7505 | int varargs = ufunc->uf_va_name != NULL; |
| 7506 | int argcount = ufunc->uf_args.ga_len; |
| 7507 | |
| 7508 | // Create a type for the function, with the return type and any |
| 7509 | // argument types. |
| 7510 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7511 | // The type is included in "tt_args". |
| 7512 | if (argcount > 0 || varargs) |
| 7513 | { |
| 7514 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7515 | argcount, &ufunc->uf_type_list); |
| 7516 | // Add argument types to the function type. |
| 7517 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7518 | argcount + varargs, |
| 7519 | &ufunc->uf_type_list) == FAIL) |
| 7520 | return; |
| 7521 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7522 | ufunc->uf_func_type->tt_min_argcount = |
| 7523 | argcount - ufunc->uf_def_args.ga_len; |
| 7524 | if (ufunc->uf_arg_types == NULL) |
| 7525 | { |
| 7526 | int i; |
| 7527 | |
| 7528 | // lambda does not have argument types. |
| 7529 | for (i = 0; i < argcount; ++i) |
| 7530 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7531 | } |
| 7532 | else |
| 7533 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7534 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7535 | if (varargs) |
| 7536 | { |
| 7537 | ufunc->uf_func_type->tt_args[argcount] = |
| 7538 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7539 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7540 | } |
| 7541 | } |
| 7542 | else |
| 7543 | // No arguments, can use a predefined type. |
| 7544 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7545 | argcount, &ufunc->uf_type_list); |
| 7546 | } |
| 7547 | |
| 7548 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7549 | /* |
| 7550 | * Delete an instruction, free what it contains. |
| 7551 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7552 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7553 | delete_instr(isn_T *isn) |
| 7554 | { |
| 7555 | switch (isn->isn_type) |
| 7556 | { |
| 7557 | case ISN_EXEC: |
| 7558 | case ISN_LOADENV: |
| 7559 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7560 | case ISN_LOADB: |
| 7561 | case ISN_LOADW: |
| 7562 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7563 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7564 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7565 | case ISN_PUSHEXC: |
| 7566 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7567 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7568 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7569 | case ISN_STOREB: |
| 7570 | case ISN_STOREW: |
| 7571 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7572 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7573 | vim_free(isn->isn_arg.string); |
| 7574 | break; |
| 7575 | |
| 7576 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7577 | case ISN_STORES: |
| 7578 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7579 | break; |
| 7580 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7581 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7582 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7583 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7584 | break; |
| 7585 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7586 | case ISN_STOREOPT: |
| 7587 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7588 | break; |
| 7589 | |
| 7590 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7591 | blob_unref(isn->isn_arg.blob); |
| 7592 | break; |
| 7593 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7594 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7595 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7596 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7597 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7598 | break; |
| 7599 | |
| 7600 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7601 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7602 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7603 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7604 | break; |
| 7605 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7606 | case ISN_UCALL: |
| 7607 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7608 | break; |
| 7609 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7610 | case ISN_FUNCREF: |
| 7611 | { |
| 7612 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7613 | + isn->isn_arg.funcref.fr_func; |
| 7614 | func_ptr_unref(dfunc->df_ufunc); |
| 7615 | } |
| 7616 | break; |
| 7617 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7618 | case ISN_2BOOL: |
| 7619 | case ISN_2STRING: |
| 7620 | case ISN_ADDBLOB: |
| 7621 | case ISN_ADDLIST: |
| 7622 | case ISN_BCALL: |
| 7623 | case ISN_CATCH: |
| 7624 | case ISN_CHECKNR: |
| 7625 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7626 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7627 | case ISN_COMPAREANY: |
| 7628 | case ISN_COMPAREBLOB: |
| 7629 | case ISN_COMPAREBOOL: |
| 7630 | case ISN_COMPAREDICT: |
| 7631 | case ISN_COMPAREFLOAT: |
| 7632 | case ISN_COMPAREFUNC: |
| 7633 | case ISN_COMPARELIST: |
| 7634 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7635 | case ISN_COMPARESPECIAL: |
| 7636 | case ISN_COMPARESTRING: |
| 7637 | case ISN_CONCAT: |
| 7638 | case ISN_DCALL: |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 7639 | case ISN_SHUFFLE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7640 | case ISN_DROP: |
| 7641 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7642 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7643 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7644 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7645 | case ISN_EXECCONCAT: |
| 7646 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7647 | case ISN_FOR: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7648 | case ISN_LISTINDEX: |
| 7649 | case ISN_STRINDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7650 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7651 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7652 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7653 | case ISN_JUMP: |
| 7654 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7655 | case ISN_LOADBDICT: |
| 7656 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7657 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7658 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7659 | case ISN_LOADSCRIPT: |
| 7660 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7661 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7662 | case ISN_LOADWDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7663 | case ISN_NEGATENR: |
| 7664 | case ISN_NEWDICT: |
| 7665 | case ISN_NEWLIST: |
| 7666 | case ISN_OPNR: |
| 7667 | case ISN_OPFLOAT: |
| 7668 | case ISN_OPANY: |
| 7669 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7670 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7671 | case ISN_PUSHF: |
| 7672 | case ISN_PUSHNR: |
| 7673 | case ISN_PUSHBOOL: |
| 7674 | case ISN_PUSHSPEC: |
| 7675 | case ISN_RETURN: |
| 7676 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 7677 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7678 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7679 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7680 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7681 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7682 | case ISN_STOREDICT: |
| 7683 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7684 | case ISN_THROW: |
| 7685 | case ISN_TRY: |
| 7686 | // nothing allocated |
| 7687 | break; |
| 7688 | } |
| 7689 | } |
| 7690 | |
| 7691 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7692 | * Free all instructions for "dfunc". |
| 7693 | */ |
| 7694 | static void |
| 7695 | delete_def_function_contents(dfunc_T *dfunc) |
| 7696 | { |
| 7697 | int idx; |
| 7698 | |
| 7699 | ga_clear(&dfunc->df_def_args_isn); |
| 7700 | |
| 7701 | if (dfunc->df_instr != NULL) |
| 7702 | { |
| 7703 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7704 | delete_instr(dfunc->df_instr + idx); |
| 7705 | VIM_CLEAR(dfunc->df_instr); |
| 7706 | } |
| 7707 | |
| 7708 | dfunc->df_deleted = TRUE; |
| 7709 | } |
| 7710 | |
| 7711 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7712 | * When a user function is deleted, clear the contents of any associated def |
| 7713 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7714 | */ |
| 7715 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7716 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7717 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7718 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7719 | { |
| 7720 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7721 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7722 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7723 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7724 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7725 | } |
| 7726 | } |
| 7727 | |
| 7728 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7729 | /* |
| 7730 | * Free all functions defined with ":def". |
| 7731 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7732 | void |
| 7733 | free_def_functions(void) |
| 7734 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7735 | int idx; |
| 7736 | |
| 7737 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7738 | { |
| 7739 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7740 | |
| 7741 | delete_def_function_contents(dfunc); |
| 7742 | } |
| 7743 | |
| 7744 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7745 | } |
| 7746 | #endif |
| 7747 | |
| 7748 | |
| 7749 | #endif // FEAT_EVAL |