Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9compile.c: :def and dealing with instructions |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | #ifdef VMS |
| 20 | # include <float.h> |
| 21 | #endif |
| 22 | |
| 23 | #define DEFINE_VIM9_GLOBALS |
| 24 | #include "vim9.h" |
| 25 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 26 | // values for ctx_skip |
| 27 | typedef enum { |
| 28 | SKIP_NOT, // condition is a constant, produce code |
| 29 | SKIP_YES, // condition is a constant, do NOT produce code |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 30 | SKIP_UNKNOWN // condition is not a constant, produce code |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 31 | } skip_T; |
| 32 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Chain of jump instructions where the end label needs to be set. |
| 35 | */ |
| 36 | typedef struct endlabel_S endlabel_T; |
| 37 | struct endlabel_S { |
| 38 | endlabel_T *el_next; // chain end_label locations |
| 39 | int el_end_label; // instruction idx where to set end |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * info specific for the scope of :if / elseif / else |
| 44 | */ |
| 45 | typedef struct { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 46 | int is_seen_else; |
| 47 | int is_had_return; // every block ends in :return |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 48 | int is_if_label; // instruction idx at IF or ELSEIF |
| 49 | endlabel_T *is_end_label; // instructions to set end label |
| 50 | } ifscope_T; |
| 51 | |
| 52 | /* |
| 53 | * info specific for the scope of :while |
| 54 | */ |
| 55 | typedef struct { |
| 56 | int ws_top_label; // instruction idx at WHILE |
| 57 | endlabel_T *ws_end_label; // instructions to set end |
| 58 | } whilescope_T; |
| 59 | |
| 60 | /* |
| 61 | * info specific for the scope of :for |
| 62 | */ |
| 63 | typedef struct { |
| 64 | int fs_top_label; // instruction idx at FOR |
| 65 | endlabel_T *fs_end_label; // break instructions |
| 66 | } forscope_T; |
| 67 | |
| 68 | /* |
| 69 | * info specific for the scope of :try |
| 70 | */ |
| 71 | typedef struct { |
| 72 | int ts_try_label; // instruction idx at TRY |
| 73 | endlabel_T *ts_end_label; // jump to :finally or :endtry |
| 74 | int ts_catch_label; // instruction idx of last CATCH |
| 75 | int ts_caught_all; // "catch" without argument encountered |
| 76 | } tryscope_T; |
| 77 | |
| 78 | typedef enum { |
| 79 | NO_SCOPE, |
| 80 | IF_SCOPE, |
| 81 | WHILE_SCOPE, |
| 82 | FOR_SCOPE, |
| 83 | TRY_SCOPE, |
| 84 | BLOCK_SCOPE |
| 85 | } scopetype_T; |
| 86 | |
| 87 | /* |
| 88 | * Info for one scope, pointed to by "ctx_scope". |
| 89 | */ |
| 90 | typedef struct scope_S scope_T; |
| 91 | struct scope_S { |
| 92 | scope_T *se_outer; // scope containing this one |
| 93 | scopetype_T se_type; |
| 94 | int se_local_count; // ctx_locals.ga_len before scope |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 95 | skip_T se_skip_save; // ctx_skip before the block |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 96 | union { |
| 97 | ifscope_T se_if; |
| 98 | whilescope_T se_while; |
| 99 | forscope_T se_for; |
| 100 | tryscope_T se_try; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 101 | } se_u; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Entry for "ctx_locals". Used for arguments and local variables. |
| 106 | */ |
| 107 | typedef struct { |
| 108 | char_u *lv_name; |
| 109 | type_T *lv_type; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 110 | int lv_idx; // index of the variable on the stack |
| 111 | int lv_from_outer; // when TRUE using ctx_outer scope |
| 112 | int lv_const; // when TRUE cannot be assigned to |
| 113 | int lv_arg; // when TRUE this is an argument |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 114 | } lvar_T; |
| 115 | |
| 116 | /* |
| 117 | * Context for compiling lines of Vim script. |
| 118 | * Stores info about the local variables and condition stack. |
| 119 | */ |
| 120 | struct cctx_S { |
| 121 | ufunc_T *ctx_ufunc; // current function |
| 122 | int ctx_lnum; // line number in current function |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 123 | char_u *ctx_line_start; // start of current line or NULL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 124 | garray_T ctx_instr; // generated instructions |
| 125 | |
| 126 | garray_T ctx_locals; // currently visible local variables |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 127 | int ctx_locals_count; // total number of local variables |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 128 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 129 | int ctx_closure_count; // number of closures created in the |
| 130 | // function |
| 131 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 132 | garray_T ctx_imports; // imported items |
| 133 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 134 | skip_T ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 135 | scope_T *ctx_scope; // current scope, NULL at toplevel |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 136 | int ctx_had_return; // last seen statement was "return" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 137 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 138 | cctx_T *ctx_outer; // outer scope for lambda or nested |
| 139 | // function |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 140 | int ctx_outer_used; // var in ctx_outer was used |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 141 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 142 | garray_T ctx_type_stack; // type of each item on the stack |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 143 | garray_T *ctx_type_list; // list of pointers to allocated types |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static char e_var_notfound[] = N_("E1001: variable not found: %s"); |
| 147 | static char e_syntax_at[] = N_("E1002: Syntax error at %s"); |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 148 | static char e_used_as_arg[] = N_("E1006: %s is used as an argument"); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 149 | static char e_cannot_use_void[] = N_("E1031: Cannot use void value"); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 150 | static char e_namespace[] = N_("E1075: Namespace not supported: %s"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 151 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 152 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 153 | static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 154 | |
| 155 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 156 | * Lookup variable "name" in the local scope and return it. |
| 157 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 158 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 159 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 160 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 161 | { |
| 162 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 163 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 164 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 165 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 166 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 167 | |
| 168 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 169 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 170 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 171 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 172 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 173 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 174 | { |
| 175 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 176 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 177 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 178 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 179 | |
| 180 | // Find local in outer function scope. |
| 181 | if (cctx->ctx_outer != NULL) |
| 182 | { |
| 183 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 184 | if (lvar != NULL) |
| 185 | { |
| 186 | // TODO: are there situations we should not mark the outer scope as |
| 187 | // used? |
| 188 | cctx->ctx_outer_used = TRUE; |
| 189 | lvar->lv_from_outer = TRUE; |
| 190 | return lvar; |
| 191 | } |
| 192 | } |
| 193 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 194 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 198 | * Lookup an argument in the current function and an enclosing function. |
| 199 | * Returns the argument index in "idxp" |
| 200 | * Returns the argument type in "type" |
| 201 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 202 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 203 | */ |
| 204 | static int |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 205 | lookup_arg( |
| 206 | char_u *name, |
| 207 | size_t len, |
| 208 | int *idxp, |
| 209 | type_T **type, |
| 210 | int *gen_load_outer, |
| 211 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 212 | { |
| 213 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 214 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 215 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 216 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 217 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 218 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 219 | { |
| 220 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 221 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 222 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 223 | { |
| 224 | if (idxp != NULL) |
| 225 | { |
| 226 | // Arguments are located above the frame pointer. One further |
| 227 | // if there is a vararg argument |
| 228 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 229 | + STACK_FRAME_SIZE) |
| 230 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 231 | |
| 232 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 233 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 234 | else |
| 235 | *type = &t_any; |
| 236 | } |
| 237 | return OK; |
| 238 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 239 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 240 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 241 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 242 | if (va_name != NULL |
| 243 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 244 | { |
| 245 | if (idxp != NULL) |
| 246 | { |
| 247 | // varargs is always the last argument |
| 248 | *idxp = -STACK_FRAME_SIZE - 1; |
| 249 | *type = cctx->ctx_ufunc->uf_va_type; |
| 250 | } |
| 251 | return OK; |
| 252 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 253 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 254 | if (cctx->ctx_outer != NULL) |
| 255 | { |
| 256 | // Lookup the name for an argument of the outer function. |
| 257 | if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
| 258 | == OK) |
| 259 | { |
| 260 | *gen_load_outer = TRUE; |
| 261 | return OK; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Lookup a variable in the current script. |
| 270 | * Returns OK or FAIL. |
| 271 | */ |
| 272 | static int |
| 273 | lookup_script(char_u *name, size_t len) |
| 274 | { |
| 275 | int cc; |
| 276 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 277 | dictitem_T *di; |
| 278 | |
| 279 | cc = name[len]; |
| 280 | name[len] = NUL; |
| 281 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 282 | name[len] = cc; |
| 283 | return di == NULL ? FAIL: OK; |
| 284 | } |
| 285 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 286 | /* |
| 287 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 288 | * compilation context "cctx". |
| 289 | * Return FAIL and give an error if it defined. |
| 290 | */ |
| 291 | int |
Bram Moolenaar | cbb6bdc | 2020-07-06 21:53:17 +0200 | [diff] [blame] | 292 | check_defined(char_u *p, size_t len, cctx_T *cctx) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 293 | { |
| 294 | if (lookup_script(p, len) == OK |
| 295 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 296 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 297 | || find_imported(p, len, cctx) != NULL))) |
| 298 | { |
| 299 | semsg("E1073: imported name already defined: %s", p); |
| 300 | return FAIL; |
| 301 | } |
| 302 | return OK; |
| 303 | } |
| 304 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 305 | /* |
| 306 | * Allocate memory for a type_T and add the pointer to type_gap, so that it can |
| 307 | * be freed later. |
| 308 | */ |
| 309 | static type_T * |
| 310 | alloc_type(garray_T *type_gap) |
| 311 | { |
| 312 | type_T *type; |
| 313 | |
| 314 | if (ga_grow(type_gap, 1) == FAIL) |
| 315 | return NULL; |
| 316 | type = ALLOC_CLEAR_ONE(type_T); |
| 317 | if (type != NULL) |
| 318 | { |
| 319 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type; |
| 320 | ++type_gap->ga_len; |
| 321 | } |
| 322 | return type; |
| 323 | } |
| 324 | |
Bram Moolenaar | 6110e79 | 2020-07-08 19:35:21 +0200 | [diff] [blame] | 325 | void |
| 326 | clear_type_list(garray_T *gap) |
| 327 | { |
| 328 | while (gap->ga_len > 0) |
| 329 | vim_free(((type_T **)gap->ga_data)[--gap->ga_len]); |
| 330 | ga_clear(gap); |
| 331 | } |
| 332 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 333 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 334 | get_list_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 335 | { |
| 336 | type_T *type; |
| 337 | |
| 338 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 339 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 340 | return &t_list_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 341 | if (member_type->tt_type == VAR_VOID |
| 342 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 343 | return &t_list_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 344 | if (member_type->tt_type == VAR_BOOL) |
| 345 | return &t_list_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 346 | if (member_type->tt_type == VAR_NUMBER) |
| 347 | return &t_list_number; |
| 348 | if (member_type->tt_type == VAR_STRING) |
| 349 | return &t_list_string; |
| 350 | |
| 351 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 352 | type = alloc_type(type_gap); |
| 353 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 354 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 355 | type->tt_type = VAR_LIST; |
| 356 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 357 | type->tt_argcount = 0; |
| 358 | type->tt_args = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 359 | return type; |
| 360 | } |
| 361 | |
| 362 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 363 | get_dict_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 364 | { |
| 365 | type_T *type; |
| 366 | |
| 367 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 368 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 369 | return &t_dict_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 370 | if (member_type->tt_type == VAR_VOID |
| 371 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 372 | return &t_dict_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 373 | if (member_type->tt_type == VAR_BOOL) |
| 374 | return &t_dict_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 375 | if (member_type->tt_type == VAR_NUMBER) |
| 376 | return &t_dict_number; |
| 377 | if (member_type->tt_type == VAR_STRING) |
| 378 | return &t_dict_string; |
| 379 | |
| 380 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 381 | type = alloc_type(type_gap); |
| 382 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 383 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 384 | type->tt_type = VAR_DICT; |
| 385 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 386 | type->tt_argcount = 0; |
| 387 | type->tt_args = NULL; |
| 388 | return type; |
| 389 | } |
| 390 | |
| 391 | /* |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 392 | * Allocate a new type for a function. |
| 393 | */ |
| 394 | static type_T * |
| 395 | alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 396 | { |
| 397 | type_T *type = alloc_type(type_gap); |
| 398 | |
| 399 | if (type == NULL) |
| 400 | return &t_any; |
| 401 | type->tt_type = VAR_FUNC; |
| 402 | type->tt_member = ret_type; |
| 403 | type->tt_argcount = argcount; |
| 404 | type->tt_args = NULL; |
| 405 | return type; |
| 406 | } |
| 407 | |
| 408 | /* |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 409 | * Get a function type, based on the return type "ret_type". |
| 410 | * If "argcount" is -1 or 0 a predefined type can be used. |
| 411 | * If "argcount" > 0 always create a new type, so that arguments can be added. |
| 412 | */ |
| 413 | static type_T * |
| 414 | get_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 415 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 416 | // recognize commonly used types |
| 417 | if (argcount <= 0) |
| 418 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 419 | if (ret_type == &t_unknown) |
| 420 | { |
| 421 | // (argcount == 0) is not possible |
| 422 | return &t_func_unknown; |
| 423 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 424 | if (ret_type == &t_void) |
| 425 | { |
| 426 | if (argcount == 0) |
| 427 | return &t_func_0_void; |
| 428 | else |
| 429 | return &t_func_void; |
| 430 | } |
| 431 | if (ret_type == &t_any) |
| 432 | { |
| 433 | if (argcount == 0) |
| 434 | return &t_func_0_any; |
| 435 | else |
| 436 | return &t_func_any; |
| 437 | } |
| 438 | if (ret_type == &t_number) |
| 439 | { |
| 440 | if (argcount == 0) |
| 441 | return &t_func_0_number; |
| 442 | else |
| 443 | return &t_func_number; |
| 444 | } |
| 445 | if (ret_type == &t_string) |
| 446 | { |
| 447 | if (argcount == 0) |
| 448 | return &t_func_0_string; |
| 449 | else |
| 450 | return &t_func_string; |
| 451 | } |
| 452 | } |
| 453 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 454 | return alloc_func_type(ret_type, argcount, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 455 | } |
| 456 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 457 | /* |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 458 | * For a function type, reserve space for "argcount" argument types (including |
| 459 | * vararg). |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 460 | */ |
| 461 | static int |
| 462 | func_type_add_arg_types( |
| 463 | type_T *functype, |
| 464 | int argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 465 | garray_T *type_gap) |
| 466 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 467 | // To make it easy to free the space needed for the argument types, add the |
| 468 | // pointer to type_gap. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 469 | if (ga_grow(type_gap, 1) == FAIL) |
| 470 | return FAIL; |
| 471 | functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); |
| 472 | if (functype->tt_args == NULL) |
| 473 | return FAIL; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 474 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = |
| 475 | (void *)functype->tt_args; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 476 | ++type_gap->ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 477 | return OK; |
| 478 | } |
| 479 | |
| 480 | /* |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 481 | * Return the type_T for a typval. Only for primitive types. |
| 482 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 483 | type_T * |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 484 | typval2type(typval_T *tv) |
| 485 | { |
| 486 | if (tv->v_type == VAR_NUMBER) |
| 487 | return &t_number; |
| 488 | if (tv->v_type == VAR_BOOL) |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 489 | return &t_bool; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 490 | if (tv->v_type == VAR_STRING) |
| 491 | return &t_string; |
| 492 | if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles |
| 493 | return &t_list_string; |
| 494 | if (tv->v_type == VAR_DICT) // e.g. for v:completed_item |
| 495 | return &t_dict_any; |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 496 | return &t_any; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 497 | } |
| 498 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 499 | static void |
| 500 | type_mismatch(type_T *expected, type_T *actual) |
| 501 | { |
| 502 | char *tofree1, *tofree2; |
| 503 | |
| 504 | semsg(_("E1013: type mismatch, expected %s but got %s"), |
| 505 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 506 | vim_free(tofree1); |
| 507 | vim_free(tofree2); |
| 508 | } |
| 509 | |
| 510 | static void |
| 511 | arg_type_mismatch(type_T *expected, type_T *actual, int argidx) |
| 512 | { |
| 513 | char *tofree1, *tofree2; |
| 514 | |
| 515 | semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"), |
| 516 | argidx, |
| 517 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 518 | vim_free(tofree1); |
| 519 | vim_free(tofree2); |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Check if the expected and actual types match. |
| 524 | * Does not allow for assigning "any" to a specific type. |
| 525 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 526 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 527 | check_type(type_T *expected, type_T *actual, int give_msg) |
| 528 | { |
| 529 | int ret = OK; |
| 530 | |
| 531 | // When expected is "unknown" we accept any actual type. |
| 532 | // When expected is "any" we accept any actual type except "void". |
| 533 | if (expected->tt_type != VAR_UNKNOWN |
| 534 | && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) |
| 535 | |
| 536 | { |
| 537 | if (expected->tt_type != actual->tt_type) |
| 538 | { |
| 539 | if (give_msg) |
| 540 | type_mismatch(expected, actual); |
| 541 | return FAIL; |
| 542 | } |
| 543 | if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) |
| 544 | { |
| 545 | // "unknown" is used for an empty list or dict |
| 546 | if (actual->tt_member != &t_unknown) |
| 547 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 548 | } |
| 549 | else if (expected->tt_type == VAR_FUNC) |
| 550 | { |
| 551 | if (expected->tt_member != &t_unknown) |
| 552 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 553 | if (ret == OK && expected->tt_argcount != -1 |
| 554 | && (actual->tt_argcount < expected->tt_min_argcount |
| 555 | || actual->tt_argcount > expected->tt_argcount)) |
| 556 | ret = FAIL; |
| 557 | } |
| 558 | if (ret == FAIL && give_msg) |
| 559 | type_mismatch(expected, actual); |
| 560 | } |
| 561 | return ret; |
| 562 | } |
| 563 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 564 | /* |
| 565 | * Return FAIl if "expected" and "actual" don't match. |
| 566 | * TODO: better type comparison |
| 567 | */ |
| 568 | int |
| 569 | check_argtype(type_T *expected, typval_T *actual_tv) |
| 570 | { |
| 571 | type_T actual; |
| 572 | type_T member; |
| 573 | |
| 574 | // TODO: should should be done with more levels |
| 575 | CLEAR_FIELD(actual); |
| 576 | actual.tt_type = actual_tv->v_type; |
| 577 | if (actual_tv->v_type == VAR_LIST |
| 578 | && actual_tv->vval.v_list != NULL |
| 579 | && actual_tv->vval.v_list->lv_first != NULL) |
| 580 | { |
| 581 | // Use the type of the first member, it is the most specific. |
| 582 | CLEAR_FIELD(member); |
| 583 | member.tt_type = actual_tv->vval.v_list->lv_first->li_tv.v_type; |
| 584 | member.tt_member = &t_any; |
| 585 | actual.tt_member = &member; |
| 586 | } |
| 587 | else if (actual_tv->v_type == VAR_DICT |
| 588 | && actual_tv->vval.v_dict != NULL |
| 589 | && actual_tv->vval.v_dict->dv_hashtab.ht_used > 0) |
| 590 | { |
| 591 | dict_iterator_T iter; |
| 592 | typval_T *value; |
| 593 | |
| 594 | // Use the type of the first value, it is the most specific. |
| 595 | dict_iterate_start(actual_tv, &iter); |
| 596 | dict_iterate_next(&iter, &value); |
| 597 | CLEAR_FIELD(member); |
| 598 | member.tt_type = value->v_type; |
| 599 | member.tt_member = &t_any; |
| 600 | actual.tt_member = &member; |
| 601 | } |
| 602 | else |
| 603 | actual.tt_member = &t_any; |
| 604 | return check_type(expected, &actual, TRUE); |
| 605 | } |
| 606 | |
| 607 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 608 | ///////////////////////////////////////////////////////////////////// |
| 609 | // Following generate_ functions expect the caller to call ga_grow(). |
| 610 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 611 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 612 | #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] | 613 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 614 | /* |
| 615 | * Generate an instruction without arguments. |
| 616 | * Returns a pointer to the new instruction, NULL if failed. |
| 617 | */ |
| 618 | static isn_T * |
| 619 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 620 | { |
| 621 | garray_T *instr = &cctx->ctx_instr; |
| 622 | isn_T *isn; |
| 623 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 624 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 625 | if (ga_grow(instr, 1) == FAIL) |
| 626 | return NULL; |
| 627 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 628 | isn->isn_type = isn_type; |
| 629 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 630 | ++instr->ga_len; |
| 631 | |
| 632 | return isn; |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * Generate an instruction without arguments. |
| 637 | * "drop" will be removed from the stack. |
| 638 | * Returns a pointer to the new instruction, NULL if failed. |
| 639 | */ |
| 640 | static isn_T * |
| 641 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 642 | { |
| 643 | garray_T *stack = &cctx->ctx_type_stack; |
| 644 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 645 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 646 | stack->ga_len -= drop; |
| 647 | return generate_instr(cctx, isn_type); |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 652 | */ |
| 653 | static isn_T * |
| 654 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 655 | { |
| 656 | isn_T *isn; |
| 657 | garray_T *stack = &cctx->ctx_type_stack; |
| 658 | |
| 659 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 660 | return NULL; |
| 661 | |
| 662 | if (ga_grow(stack, 1) == FAIL) |
| 663 | return NULL; |
| 664 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 665 | ++stack->ga_len; |
| 666 | |
| 667 | return isn; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 672 | */ |
| 673 | static int |
| 674 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 675 | { |
| 676 | isn_T *isn; |
| 677 | garray_T *stack = &cctx->ctx_type_stack; |
| 678 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 679 | |
| 680 | if ((*type)->tt_type == VAR_STRING) |
| 681 | return OK; |
| 682 | *type = &t_string; |
| 683 | |
| 684 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 685 | return FAIL; |
| 686 | isn->isn_arg.number = offset; |
| 687 | |
| 688 | return OK; |
| 689 | } |
| 690 | |
| 691 | static int |
| 692 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 693 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 694 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 695 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 696 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 697 | { |
| 698 | if (*op == '+') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 699 | emsg(_("E1051: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 700 | else |
| 701 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 702 | return FAIL; |
| 703 | } |
| 704 | return OK; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Generate an instruction with two arguments. The instruction depends on the |
| 709 | * type of the arguments. |
| 710 | */ |
| 711 | static int |
| 712 | generate_two_op(cctx_T *cctx, char_u *op) |
| 713 | { |
| 714 | garray_T *stack = &cctx->ctx_type_stack; |
| 715 | type_T *type1; |
| 716 | type_T *type2; |
| 717 | vartype_T vartype; |
| 718 | isn_T *isn; |
| 719 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 720 | RETURN_OK_IF_SKIP(cctx); |
| 721 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 722 | // Get the known type of the two items on the stack. If they are matching |
| 723 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 724 | // checking. |
| 725 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 726 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 727 | vartype = VAR_ANY; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 728 | if (type1->tt_type == type2->tt_type |
| 729 | && (type1->tt_type == VAR_NUMBER |
| 730 | || type1->tt_type == VAR_LIST |
| 731 | #ifdef FEAT_FLOAT |
| 732 | || type1->tt_type == VAR_FLOAT |
| 733 | #endif |
| 734 | || type1->tt_type == VAR_BLOB)) |
| 735 | vartype = type1->tt_type; |
| 736 | |
| 737 | switch (*op) |
| 738 | { |
| 739 | case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 740 | && type1->tt_type != VAR_ANY |
| 741 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 742 | && check_number_or_float( |
| 743 | type1->tt_type, type2->tt_type, op) == FAIL) |
| 744 | return FAIL; |
| 745 | isn = generate_instr_drop(cctx, |
| 746 | vartype == VAR_NUMBER ? ISN_OPNR |
| 747 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 748 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 749 | #ifdef FEAT_FLOAT |
| 750 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 751 | #endif |
| 752 | : ISN_OPANY, 1); |
| 753 | if (isn != NULL) |
| 754 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 755 | break; |
| 756 | |
| 757 | case '-': |
| 758 | case '*': |
| 759 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 760 | op) == FAIL) |
| 761 | return FAIL; |
| 762 | if (vartype == VAR_NUMBER) |
| 763 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 764 | #ifdef FEAT_FLOAT |
| 765 | else if (vartype == VAR_FLOAT) |
| 766 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 767 | #endif |
| 768 | else |
| 769 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 770 | if (isn != NULL) |
| 771 | isn->isn_arg.op.op_type = *op == '*' |
| 772 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 773 | break; |
| 774 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 775 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 776 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 777 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 778 | && type2->tt_type != VAR_NUMBER)) |
| 779 | { |
| 780 | emsg(_("E1035: % requires number arguments")); |
| 781 | return FAIL; |
| 782 | } |
| 783 | isn = generate_instr_drop(cctx, |
| 784 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 785 | if (isn != NULL) |
| 786 | isn->isn_arg.op.op_type = EXPR_REM; |
| 787 | break; |
| 788 | } |
| 789 | |
| 790 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 791 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 792 | { |
| 793 | type_T *type = &t_any; |
| 794 | |
| 795 | #ifdef FEAT_FLOAT |
| 796 | // float+number and number+float results in float |
| 797 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 798 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 799 | type = &t_float; |
| 800 | #endif |
| 801 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 802 | } |
| 803 | |
| 804 | return OK; |
| 805 | } |
| 806 | |
| 807 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 808 | * Get the instruction to use for comparing "type1" with "type2" |
| 809 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 810 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 811 | static isntype_T |
| 812 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 813 | { |
| 814 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 815 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 816 | if (type1 == VAR_UNKNOWN) |
| 817 | type1 = VAR_ANY; |
| 818 | if (type2 == VAR_UNKNOWN) |
| 819 | type2 = VAR_ANY; |
| 820 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 821 | if (type1 == type2) |
| 822 | { |
| 823 | switch (type1) |
| 824 | { |
| 825 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 826 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 827 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 828 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 829 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 830 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 831 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 832 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 833 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 834 | default: isntype = ISN_COMPAREANY; break; |
| 835 | } |
| 836 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 837 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 838 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 839 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 840 | isntype = ISN_COMPAREANY; |
| 841 | |
| 842 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 843 | && (isntype == ISN_COMPAREBOOL |
| 844 | || isntype == ISN_COMPARESPECIAL |
| 845 | || isntype == ISN_COMPARENR |
| 846 | || isntype == ISN_COMPAREFLOAT)) |
| 847 | { |
| 848 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 849 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 850 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 851 | } |
| 852 | if (isntype == ISN_DROP |
| 853 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 854 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 855 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 856 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 857 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 858 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 859 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 860 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 861 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 862 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 863 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 864 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 865 | return isntype; |
| 866 | } |
| 867 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 868 | int |
| 869 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 870 | { |
| 871 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 872 | return FAIL; |
| 873 | return OK; |
| 874 | } |
| 875 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 876 | /* |
| 877 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 878 | */ |
| 879 | static int |
| 880 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 881 | { |
| 882 | isntype_T isntype; |
| 883 | isn_T *isn; |
| 884 | garray_T *stack = &cctx->ctx_type_stack; |
| 885 | vartype_T type1; |
| 886 | vartype_T type2; |
| 887 | |
| 888 | RETURN_OK_IF_SKIP(cctx); |
| 889 | |
| 890 | // Get the known type of the two items on the stack. If they are matching |
| 891 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 892 | // checking. |
| 893 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 894 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 895 | isntype = get_compare_isn(exptype, type1, type2); |
| 896 | if (isntype == ISN_DROP) |
| 897 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 898 | |
| 899 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 900 | return FAIL; |
| 901 | isn->isn_arg.op.op_type = exptype; |
| 902 | isn->isn_arg.op.op_ic = ic; |
| 903 | |
| 904 | // takes two arguments, puts one bool back |
| 905 | if (stack->ga_len >= 2) |
| 906 | { |
| 907 | --stack->ga_len; |
| 908 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 909 | } |
| 910 | |
| 911 | return OK; |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | * Generate an ISN_2BOOL instruction. |
| 916 | */ |
| 917 | static int |
| 918 | generate_2BOOL(cctx_T *cctx, int invert) |
| 919 | { |
| 920 | isn_T *isn; |
| 921 | garray_T *stack = &cctx->ctx_type_stack; |
| 922 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 923 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 924 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 925 | return FAIL; |
| 926 | isn->isn_arg.number = invert; |
| 927 | |
| 928 | // type becomes bool |
| 929 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 930 | |
| 931 | return OK; |
| 932 | } |
| 933 | |
| 934 | static int |
| 935 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 936 | { |
| 937 | isn_T *isn; |
| 938 | garray_T *stack = &cctx->ctx_type_stack; |
| 939 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 940 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 941 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 942 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 943 | // TODO: whole type, e.g. for a function also arg and return types |
| 944 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 945 | isn->isn_arg.type.ct_off = offset; |
| 946 | |
| 947 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 948 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 949 | |
| 950 | return OK; |
| 951 | } |
| 952 | |
| 953 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 954 | * Check that |
| 955 | * - "actual" is "expected" type or |
| 956 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 957 | * - return FAIL. |
| 958 | */ |
| 959 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 960 | need_type( |
| 961 | type_T *actual, |
| 962 | type_T *expected, |
| 963 | int offset, |
| 964 | cctx_T *cctx, |
| 965 | int silent) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 966 | { |
| 967 | if (check_type(expected, actual, FALSE) == OK) |
| 968 | return OK; |
| 969 | if (actual->tt_type != VAR_ANY |
| 970 | && actual->tt_type != VAR_UNKNOWN |
| 971 | && !(actual->tt_type == VAR_FUNC |
| 972 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 973 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 974 | if (!silent) |
| 975 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 976 | return FAIL; |
| 977 | } |
| 978 | generate_TYPECHECK(cctx, expected, offset); |
| 979 | return OK; |
| 980 | } |
| 981 | |
| 982 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 983 | * Generate an ISN_PUSHNR instruction. |
| 984 | */ |
| 985 | static int |
| 986 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 987 | { |
| 988 | isn_T *isn; |
| 989 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 990 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 991 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 992 | return FAIL; |
| 993 | isn->isn_arg.number = number; |
| 994 | |
| 995 | return OK; |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Generate an ISN_PUSHBOOL instruction. |
| 1000 | */ |
| 1001 | static int |
| 1002 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 1003 | { |
| 1004 | isn_T *isn; |
| 1005 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1006 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1007 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 1008 | return FAIL; |
| 1009 | isn->isn_arg.number = number; |
| 1010 | |
| 1011 | return OK; |
| 1012 | } |
| 1013 | |
| 1014 | /* |
| 1015 | * Generate an ISN_PUSHSPEC instruction. |
| 1016 | */ |
| 1017 | static int |
| 1018 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 1019 | { |
| 1020 | isn_T *isn; |
| 1021 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1022 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1023 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 1024 | return FAIL; |
| 1025 | isn->isn_arg.number = number; |
| 1026 | |
| 1027 | return OK; |
| 1028 | } |
| 1029 | |
| 1030 | #ifdef FEAT_FLOAT |
| 1031 | /* |
| 1032 | * Generate an ISN_PUSHF instruction. |
| 1033 | */ |
| 1034 | static int |
| 1035 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 1036 | { |
| 1037 | isn_T *isn; |
| 1038 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1039 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1040 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 1041 | return FAIL; |
| 1042 | isn->isn_arg.fnumber = fnumber; |
| 1043 | |
| 1044 | return OK; |
| 1045 | } |
| 1046 | #endif |
| 1047 | |
| 1048 | /* |
| 1049 | * Generate an ISN_PUSHS instruction. |
| 1050 | * Consumes "str". |
| 1051 | */ |
| 1052 | static int |
| 1053 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 1054 | { |
| 1055 | isn_T *isn; |
| 1056 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1057 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1058 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 1059 | return FAIL; |
| 1060 | isn->isn_arg.string = str; |
| 1061 | |
| 1062 | return OK; |
| 1063 | } |
| 1064 | |
| 1065 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1066 | * Generate an ISN_PUSHCHANNEL instruction. |
| 1067 | * Consumes "channel". |
| 1068 | */ |
| 1069 | static int |
| 1070 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 1071 | { |
| 1072 | isn_T *isn; |
| 1073 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1074 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1075 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 1076 | return FAIL; |
| 1077 | isn->isn_arg.channel = channel; |
| 1078 | |
| 1079 | return OK; |
| 1080 | } |
| 1081 | |
| 1082 | /* |
| 1083 | * Generate an ISN_PUSHJOB instruction. |
| 1084 | * Consumes "job". |
| 1085 | */ |
| 1086 | static int |
| 1087 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 1088 | { |
| 1089 | isn_T *isn; |
| 1090 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1091 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1092 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1093 | return FAIL; |
| 1094 | isn->isn_arg.job = job; |
| 1095 | |
| 1096 | return OK; |
| 1097 | } |
| 1098 | |
| 1099 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1100 | * Generate an ISN_PUSHBLOB instruction. |
| 1101 | * Consumes "blob". |
| 1102 | */ |
| 1103 | static int |
| 1104 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1105 | { |
| 1106 | isn_T *isn; |
| 1107 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1108 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1109 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1110 | return FAIL; |
| 1111 | isn->isn_arg.blob = blob; |
| 1112 | |
| 1113 | return OK; |
| 1114 | } |
| 1115 | |
| 1116 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1117 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1118 | * Consumes "name". |
| 1119 | */ |
| 1120 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1121 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1122 | { |
| 1123 | isn_T *isn; |
| 1124 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1125 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1126 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1127 | return FAIL; |
| 1128 | isn->isn_arg.string = name; |
| 1129 | |
| 1130 | return OK; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1134 | * Generate an ISN_GETITEM instruction with "index". |
| 1135 | */ |
| 1136 | static int |
| 1137 | generate_GETITEM(cctx_T *cctx, int index) |
| 1138 | { |
| 1139 | isn_T *isn; |
| 1140 | garray_T *stack = &cctx->ctx_type_stack; |
| 1141 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1142 | type_T *item_type = &t_any; |
| 1143 | |
| 1144 | RETURN_OK_IF_SKIP(cctx); |
| 1145 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1146 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1147 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1148 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1149 | emsg(_(e_listreq)); |
| 1150 | return FAIL; |
| 1151 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1152 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1153 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1154 | return FAIL; |
| 1155 | isn->isn_arg.number = index; |
| 1156 | |
| 1157 | // add the item type to the type stack |
| 1158 | if (ga_grow(stack, 1) == FAIL) |
| 1159 | return FAIL; |
| 1160 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 1161 | ++stack->ga_len; |
| 1162 | return OK; |
| 1163 | } |
| 1164 | |
| 1165 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 1166 | * Generate an ISN_SLICE instruction with "count". |
| 1167 | */ |
| 1168 | static int |
| 1169 | generate_SLICE(cctx_T *cctx, int count) |
| 1170 | { |
| 1171 | isn_T *isn; |
| 1172 | |
| 1173 | RETURN_OK_IF_SKIP(cctx); |
| 1174 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1175 | return FAIL; |
| 1176 | isn->isn_arg.number = count; |
| 1177 | return OK; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1182 | */ |
| 1183 | static int |
| 1184 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1185 | { |
| 1186 | isn_T *isn; |
| 1187 | |
| 1188 | RETURN_OK_IF_SKIP(cctx); |
| 1189 | |
| 1190 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1191 | return FAIL; |
| 1192 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1193 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1194 | |
| 1195 | return OK; |
| 1196 | } |
| 1197 | |
| 1198 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1199 | * Generate an ISN_STORE instruction. |
| 1200 | */ |
| 1201 | static int |
| 1202 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1203 | { |
| 1204 | isn_T *isn; |
| 1205 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1206 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1207 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1208 | return FAIL; |
| 1209 | if (name != NULL) |
| 1210 | isn->isn_arg.string = vim_strsave(name); |
| 1211 | else |
| 1212 | isn->isn_arg.number = idx; |
| 1213 | |
| 1214 | return OK; |
| 1215 | } |
| 1216 | |
| 1217 | /* |
| 1218 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1219 | */ |
| 1220 | static int |
| 1221 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1222 | { |
| 1223 | isn_T *isn; |
| 1224 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1225 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1226 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1227 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1228 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1229 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1230 | |
| 1231 | return OK; |
| 1232 | } |
| 1233 | |
| 1234 | /* |
| 1235 | * Generate an ISN_STOREOPT instruction |
| 1236 | */ |
| 1237 | static int |
| 1238 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1239 | { |
| 1240 | isn_T *isn; |
| 1241 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1242 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1243 | if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL) |
| 1244 | return FAIL; |
| 1245 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1246 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1247 | |
| 1248 | return OK; |
| 1249 | } |
| 1250 | |
| 1251 | /* |
| 1252 | * Generate an ISN_LOAD or similar instruction. |
| 1253 | */ |
| 1254 | static int |
| 1255 | generate_LOAD( |
| 1256 | cctx_T *cctx, |
| 1257 | isntype_T isn_type, |
| 1258 | int idx, |
| 1259 | char_u *name, |
| 1260 | type_T *type) |
| 1261 | { |
| 1262 | isn_T *isn; |
| 1263 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1264 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1265 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1266 | return FAIL; |
| 1267 | if (name != NULL) |
| 1268 | isn->isn_arg.string = vim_strsave(name); |
| 1269 | else |
| 1270 | isn->isn_arg.number = idx; |
| 1271 | |
| 1272 | return OK; |
| 1273 | } |
| 1274 | |
| 1275 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1276 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1277 | */ |
| 1278 | static int |
| 1279 | generate_LOADV( |
| 1280 | cctx_T *cctx, |
| 1281 | char_u *name, |
| 1282 | int error) |
| 1283 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1284 | int di_flags; |
| 1285 | int vidx = find_vim_var(name, &di_flags); |
| 1286 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1287 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1288 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1289 | if (vidx < 0) |
| 1290 | { |
| 1291 | if (error) |
| 1292 | semsg(_(e_var_notfound), name); |
| 1293 | return FAIL; |
| 1294 | } |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1295 | type = typval2type(get_vim_var_tv(vidx)); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1296 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1297 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1301 | * Generate an ISN_UNLET instruction. |
| 1302 | */ |
| 1303 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1304 | 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] | 1305 | { |
| 1306 | isn_T *isn; |
| 1307 | |
| 1308 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1309 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1310 | return FAIL; |
| 1311 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1312 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1313 | |
| 1314 | return OK; |
| 1315 | } |
| 1316 | |
| 1317 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1318 | * Generate an ISN_LOADS instruction. |
| 1319 | */ |
| 1320 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1321 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1322 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1323 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1324 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1325 | int sid, |
| 1326 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1327 | { |
| 1328 | isn_T *isn; |
| 1329 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1330 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1331 | if (isn_type == ISN_LOADS) |
| 1332 | isn = generate_instr_type(cctx, isn_type, type); |
| 1333 | else |
| 1334 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1335 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1336 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1337 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1338 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1339 | |
| 1340 | return OK; |
| 1341 | } |
| 1342 | |
| 1343 | /* |
| 1344 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1345 | */ |
| 1346 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1347 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1348 | cctx_T *cctx, |
| 1349 | isntype_T isn_type, |
| 1350 | int sid, |
| 1351 | int idx, |
| 1352 | type_T *type) |
| 1353 | { |
| 1354 | isn_T *isn; |
| 1355 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1356 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1357 | if (isn_type == ISN_LOADSCRIPT) |
| 1358 | isn = generate_instr_type(cctx, isn_type, type); |
| 1359 | else |
| 1360 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1361 | if (isn == NULL) |
| 1362 | return FAIL; |
| 1363 | isn->isn_arg.script.script_sid = sid; |
| 1364 | isn->isn_arg.script.script_idx = idx; |
| 1365 | return OK; |
| 1366 | } |
| 1367 | |
| 1368 | /* |
| 1369 | * Generate an ISN_NEWLIST instruction. |
| 1370 | */ |
| 1371 | static int |
| 1372 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1373 | { |
| 1374 | isn_T *isn; |
| 1375 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1376 | type_T *type; |
| 1377 | type_T *member; |
| 1378 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1379 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1380 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1381 | return FAIL; |
| 1382 | isn->isn_arg.number = count; |
| 1383 | |
| 1384 | // drop the value types |
| 1385 | stack->ga_len -= count; |
| 1386 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1387 | // 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] | 1388 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1389 | if (count > 0) |
| 1390 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1391 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1392 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1393 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1394 | |
| 1395 | // add the list type to the type stack |
| 1396 | if (ga_grow(stack, 1) == FAIL) |
| 1397 | return FAIL; |
| 1398 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1399 | ++stack->ga_len; |
| 1400 | |
| 1401 | return OK; |
| 1402 | } |
| 1403 | |
| 1404 | /* |
| 1405 | * Generate an ISN_NEWDICT instruction. |
| 1406 | */ |
| 1407 | static int |
| 1408 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1409 | { |
| 1410 | isn_T *isn; |
| 1411 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1412 | type_T *type; |
| 1413 | type_T *member; |
| 1414 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1415 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1416 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1417 | return FAIL; |
| 1418 | isn->isn_arg.number = count; |
| 1419 | |
| 1420 | // drop the key and value types |
| 1421 | stack->ga_len -= 2 * count; |
| 1422 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1423 | // Use the first value type for the list member type. Use "void" for an |
| 1424 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1425 | if (count > 0) |
| 1426 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1427 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1428 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1429 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1430 | |
| 1431 | // add the dict type to the type stack |
| 1432 | if (ga_grow(stack, 1) == FAIL) |
| 1433 | return FAIL; |
| 1434 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1435 | ++stack->ga_len; |
| 1436 | |
| 1437 | return OK; |
| 1438 | } |
| 1439 | |
| 1440 | /* |
| 1441 | * Generate an ISN_FUNCREF instruction. |
| 1442 | */ |
| 1443 | static int |
| 1444 | generate_FUNCREF(cctx_T *cctx, int dfunc_idx) |
| 1445 | { |
| 1446 | isn_T *isn; |
| 1447 | garray_T *stack = &cctx->ctx_type_stack; |
| 1448 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1449 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1450 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1451 | return FAIL; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1452 | isn->isn_arg.funcref.fr_func = dfunc_idx; |
| 1453 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1454 | |
| 1455 | if (ga_grow(stack, 1) == FAIL) |
| 1456 | return FAIL; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1457 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1458 | // TODO: argument and return types |
| 1459 | ++stack->ga_len; |
| 1460 | |
| 1461 | return OK; |
| 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | * Generate an ISN_JUMP instruction. |
| 1466 | */ |
| 1467 | static int |
| 1468 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1469 | { |
| 1470 | isn_T *isn; |
| 1471 | garray_T *stack = &cctx->ctx_type_stack; |
| 1472 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1473 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1474 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1475 | return FAIL; |
| 1476 | isn->isn_arg.jump.jump_when = when; |
| 1477 | isn->isn_arg.jump.jump_where = where; |
| 1478 | |
| 1479 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1480 | --stack->ga_len; |
| 1481 | |
| 1482 | return OK; |
| 1483 | } |
| 1484 | |
| 1485 | static int |
| 1486 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1487 | { |
| 1488 | isn_T *isn; |
| 1489 | garray_T *stack = &cctx->ctx_type_stack; |
| 1490 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1491 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1492 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1493 | return FAIL; |
| 1494 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1495 | |
| 1496 | if (ga_grow(stack, 1) == FAIL) |
| 1497 | return FAIL; |
| 1498 | // type doesn't matter, will be stored next |
| 1499 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1500 | ++stack->ga_len; |
| 1501 | |
| 1502 | return OK; |
| 1503 | } |
| 1504 | |
| 1505 | /* |
| 1506 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1507 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1508 | * Return FAIL if the number of arguments is wrong. |
| 1509 | */ |
| 1510 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1511 | 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] | 1512 | { |
| 1513 | isn_T *isn; |
| 1514 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1515 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1516 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1517 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1518 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1519 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1520 | argoff = check_internal_func(func_idx, argcount); |
| 1521 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1522 | return FAIL; |
| 1523 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1524 | if (method_call && argoff > 1) |
| 1525 | { |
| 1526 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1527 | return FAIL; |
| 1528 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1529 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1530 | } |
| 1531 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1532 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1533 | return FAIL; |
| 1534 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1535 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1536 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1537 | for (i = 0; i < argcount; ++i) |
| 1538 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1539 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1540 | stack->ga_len -= argcount; // drop the arguments |
| 1541 | if (ga_grow(stack, 1) == FAIL) |
| 1542 | return FAIL; |
| 1543 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1544 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1545 | ++stack->ga_len; // add return value |
| 1546 | |
| 1547 | return OK; |
| 1548 | } |
| 1549 | |
| 1550 | /* |
| 1551 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1552 | * Return FAIL if the number of arguments is wrong. |
| 1553 | */ |
| 1554 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1555 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1556 | { |
| 1557 | isn_T *isn; |
| 1558 | garray_T *stack = &cctx->ctx_type_stack; |
| 1559 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1560 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1561 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1562 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1563 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1564 | { |
| 1565 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1566 | return FAIL; |
| 1567 | } |
| 1568 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1569 | { |
| 1570 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1571 | return FAIL; |
| 1572 | } |
| 1573 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1574 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1575 | { |
| 1576 | int i; |
| 1577 | |
| 1578 | for (i = 0; i < argcount; ++i) |
| 1579 | { |
| 1580 | type_T *expected; |
| 1581 | type_T *actual; |
| 1582 | |
| 1583 | if (i < regular_args) |
| 1584 | { |
| 1585 | if (ufunc->uf_arg_types == NULL) |
| 1586 | continue; |
| 1587 | expected = ufunc->uf_arg_types[i]; |
| 1588 | } |
| 1589 | else |
| 1590 | expected = ufunc->uf_va_type->tt_member; |
| 1591 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1592 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1593 | { |
| 1594 | arg_type_mismatch(expected, actual, i + 1); |
| 1595 | return FAIL; |
| 1596 | } |
| 1597 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1598 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 1599 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1600 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1601 | } |
| 1602 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1603 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1604 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1605 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1606 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1607 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1608 | { |
| 1609 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1610 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1611 | } |
| 1612 | else |
| 1613 | { |
| 1614 | // A user function may be deleted and redefined later, can't use the |
| 1615 | // ufunc pointer, need to look it up again at runtime. |
| 1616 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1617 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1618 | } |
| 1619 | |
| 1620 | stack->ga_len -= argcount; // drop the arguments |
| 1621 | if (ga_grow(stack, 1) == FAIL) |
| 1622 | return FAIL; |
| 1623 | // add return value |
| 1624 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1625 | ++stack->ga_len; |
| 1626 | |
| 1627 | return OK; |
| 1628 | } |
| 1629 | |
| 1630 | /* |
| 1631 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1632 | */ |
| 1633 | static int |
| 1634 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1635 | { |
| 1636 | isn_T *isn; |
| 1637 | garray_T *stack = &cctx->ctx_type_stack; |
| 1638 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1639 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1640 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1641 | return FAIL; |
| 1642 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1643 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1644 | |
| 1645 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1646 | if (ga_grow(stack, 1) == FAIL) |
| 1647 | return FAIL; |
| 1648 | // add return value |
| 1649 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1650 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1651 | |
| 1652 | return OK; |
| 1653 | } |
| 1654 | |
| 1655 | /* |
| 1656 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1657 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1658 | */ |
| 1659 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1660 | generate_PCALL( |
| 1661 | cctx_T *cctx, |
| 1662 | int argcount, |
| 1663 | char_u *name, |
| 1664 | type_T *type, |
| 1665 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1666 | { |
| 1667 | isn_T *isn; |
| 1668 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1669 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1670 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1671 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1672 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1673 | if (type->tt_type == VAR_ANY) |
| 1674 | ret_type = &t_any; |
| 1675 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1676 | { |
| 1677 | if (type->tt_argcount != -1) |
| 1678 | { |
| 1679 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1680 | |
| 1681 | if (argcount < type->tt_min_argcount - varargs) |
| 1682 | { |
| 1683 | semsg(_(e_toofewarg), "[reference]"); |
| 1684 | return FAIL; |
| 1685 | } |
| 1686 | if (!varargs && argcount > type->tt_argcount) |
| 1687 | { |
| 1688 | semsg(_(e_toomanyarg), "[reference]"); |
| 1689 | return FAIL; |
| 1690 | } |
| 1691 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1692 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1693 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1694 | else |
| 1695 | { |
| 1696 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1697 | return FAIL; |
| 1698 | } |
| 1699 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1700 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1701 | return FAIL; |
| 1702 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1703 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1704 | |
| 1705 | stack->ga_len -= argcount; // drop the arguments |
| 1706 | |
| 1707 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1708 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1709 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1710 | // If partial is above the arguments it must be cleared and replaced with |
| 1711 | // the return value. |
| 1712 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1713 | return FAIL; |
| 1714 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1715 | return OK; |
| 1716 | } |
| 1717 | |
| 1718 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1719 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1720 | */ |
| 1721 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1722 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1723 | { |
| 1724 | isn_T *isn; |
| 1725 | garray_T *stack = &cctx->ctx_type_stack; |
| 1726 | type_T *type; |
| 1727 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1728 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1729 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1730 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1731 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1732 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1733 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1734 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1735 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1736 | { |
| 1737 | emsg(_(e_dictreq)); |
| 1738 | return FAIL; |
| 1739 | } |
| 1740 | // change dict type to dict member type |
| 1741 | if (type->tt_type == VAR_DICT) |
| 1742 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1743 | |
| 1744 | return OK; |
| 1745 | } |
| 1746 | |
| 1747 | /* |
| 1748 | * Generate an ISN_ECHO instruction. |
| 1749 | */ |
| 1750 | static int |
| 1751 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1752 | { |
| 1753 | isn_T *isn; |
| 1754 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1755 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1756 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1757 | return FAIL; |
| 1758 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1759 | isn->isn_arg.echo.echo_count = count; |
| 1760 | |
| 1761 | return OK; |
| 1762 | } |
| 1763 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1764 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1765 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1766 | */ |
| 1767 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1768 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1769 | { |
| 1770 | isn_T *isn; |
| 1771 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1772 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1773 | return FAIL; |
| 1774 | isn->isn_arg.number = count; |
| 1775 | |
| 1776 | return OK; |
| 1777 | } |
| 1778 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1779 | static int |
| 1780 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1781 | { |
| 1782 | isn_T *isn; |
| 1783 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1784 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1785 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1786 | return FAIL; |
| 1787 | isn->isn_arg.string = vim_strsave(line); |
| 1788 | return OK; |
| 1789 | } |
| 1790 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1791 | static int |
| 1792 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1793 | { |
| 1794 | isn_T *isn; |
| 1795 | |
| 1796 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1797 | return FAIL; |
| 1798 | isn->isn_arg.number = count; |
| 1799 | return OK; |
| 1800 | } |
| 1801 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1802 | /* |
| 1803 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1804 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1805 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1806 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1807 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1808 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1809 | lvar_T *lvar; |
| 1810 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1811 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1812 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1813 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1814 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1818 | return NULL; |
| 1819 | 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] | 1820 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1821 | // Every local variable uses the next entry on the stack. We could re-use |
| 1822 | // the last ones when leaving a scope, but then variables used in a closure |
| 1823 | // might get overwritten. To keep things simple do not re-use stack |
| 1824 | // entries. This is less efficient, but memory is cheap these days. |
| 1825 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1826 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1827 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1828 | lvar->lv_const = isConst; |
| 1829 | lvar->lv_type = type; |
| 1830 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1831 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1835 | * Remove local variables above "new_top". |
| 1836 | */ |
| 1837 | static void |
| 1838 | unwind_locals(cctx_T *cctx, int new_top) |
| 1839 | { |
| 1840 | if (cctx->ctx_locals.ga_len > new_top) |
| 1841 | { |
| 1842 | int idx; |
| 1843 | lvar_T *lvar; |
| 1844 | |
| 1845 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1846 | { |
| 1847 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1848 | vim_free(lvar->lv_name); |
| 1849 | } |
| 1850 | } |
| 1851 | cctx->ctx_locals.ga_len = new_top; |
| 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | * Free all local variables. |
| 1856 | */ |
| 1857 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1858 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1859 | { |
| 1860 | unwind_locals(cctx, 0); |
| 1861 | ga_clear(&cctx->ctx_locals); |
| 1862 | } |
| 1863 | |
| 1864 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1865 | * Skip over a type definition and return a pointer to just after it. |
| 1866 | */ |
| 1867 | char_u * |
| 1868 | skip_type(char_u *start) |
| 1869 | { |
| 1870 | char_u *p = start; |
| 1871 | |
| 1872 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1873 | ++p; |
| 1874 | |
| 1875 | // Skip over "<type>"; this is permissive about white space. |
| 1876 | if (*skipwhite(p) == '<') |
| 1877 | { |
| 1878 | p = skipwhite(p); |
| 1879 | p = skip_type(skipwhite(p + 1)); |
| 1880 | p = skipwhite(p); |
| 1881 | if (*p == '>') |
| 1882 | ++p; |
| 1883 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame^] | 1884 | else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1]))) |
| 1885 | && STRNCMP("func", start, 4) == 0) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1886 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame^] | 1887 | if (*p == '(') |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1888 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame^] | 1889 | // handle func(args): type |
| 1890 | ++p; |
| 1891 | while (*p != ')' && *p != NUL) |
| 1892 | { |
| 1893 | char_u *sp = p; |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1894 | |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame^] | 1895 | p = skip_type(p); |
| 1896 | if (p == sp) |
| 1897 | return p; // syntax error |
| 1898 | if (*p == ',') |
| 1899 | p = skipwhite(p + 1); |
| 1900 | } |
| 1901 | if (*p == ')') |
| 1902 | { |
| 1903 | if (p[1] == ':') |
| 1904 | p = skip_type(skipwhite(p + 2)); |
| 1905 | else |
| 1906 | p = skipwhite(p + 1); |
| 1907 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1908 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame^] | 1909 | else |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1910 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame^] | 1911 | // handle func: return_type |
| 1912 | p = skip_type(skipwhite(p + 1)); |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1913 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1914 | } |
| 1915 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1916 | return p; |
| 1917 | } |
| 1918 | |
| 1919 | /* |
| 1920 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1921 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1922 | * Returns NULL in case of failure. |
| 1923 | */ |
| 1924 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1925 | 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] | 1926 | { |
| 1927 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1928 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1929 | |
| 1930 | if (**arg != '<') |
| 1931 | { |
| 1932 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1933 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1934 | else |
| 1935 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1936 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1937 | } |
| 1938 | *arg = skipwhite(*arg + 1); |
| 1939 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1940 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1941 | |
| 1942 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1943 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1944 | { |
| 1945 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1946 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1947 | } |
| 1948 | ++*arg; |
| 1949 | |
| 1950 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1951 | return get_list_type(member_type, type_gap); |
| 1952 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | /* |
| 1956 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 1957 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1958 | */ |
| 1959 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1960 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1961 | { |
| 1962 | char_u *p = *arg; |
| 1963 | size_t len; |
| 1964 | |
| 1965 | // skip over the first word |
| 1966 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1967 | ++p; |
| 1968 | len = p - *arg; |
| 1969 | |
| 1970 | switch (**arg) |
| 1971 | { |
| 1972 | case 'a': |
| 1973 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 1974 | { |
| 1975 | *arg += len; |
| 1976 | return &t_any; |
| 1977 | } |
| 1978 | break; |
| 1979 | case 'b': |
| 1980 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 1981 | { |
| 1982 | *arg += len; |
| 1983 | return &t_bool; |
| 1984 | } |
| 1985 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 1986 | { |
| 1987 | *arg += len; |
| 1988 | return &t_blob; |
| 1989 | } |
| 1990 | break; |
| 1991 | case 'c': |
| 1992 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 1993 | { |
| 1994 | *arg += len; |
| 1995 | return &t_channel; |
| 1996 | } |
| 1997 | break; |
| 1998 | case 'd': |
| 1999 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 2000 | { |
| 2001 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2002 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2003 | } |
| 2004 | break; |
| 2005 | case 'f': |
| 2006 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 2007 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2008 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2009 | *arg += len; |
| 2010 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2011 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2012 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2013 | return &t_any; |
| 2014 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2015 | } |
| 2016 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 2017 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2018 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2019 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2020 | int argcount = -1; |
| 2021 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2022 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2023 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 2024 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2025 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2026 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2027 | if (**arg == '(') |
| 2028 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2029 | // "func" may or may not return a value, "func()" does |
| 2030 | // not return a value. |
| 2031 | ret_type = &t_void; |
| 2032 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2033 | p = ++*arg; |
| 2034 | argcount = 0; |
| 2035 | while (*p != NUL && *p != ')') |
| 2036 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2037 | if (*p == '?') |
| 2038 | { |
| 2039 | if (first_optional == -1) |
| 2040 | first_optional = argcount; |
| 2041 | ++p; |
| 2042 | } |
| 2043 | else if (first_optional != -1) |
| 2044 | { |
| 2045 | emsg(_("E1007: mandatory argument after optional argument")); |
| 2046 | return &t_any; |
| 2047 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2048 | else if (STRNCMP(p, "...", 3) == 0) |
| 2049 | { |
| 2050 | flags |= TTFLAG_VARARGS; |
| 2051 | p += 3; |
| 2052 | } |
| 2053 | |
| 2054 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 2055 | |
| 2056 | // Nothing comes after "...{type}". |
| 2057 | if (flags & TTFLAG_VARARGS) |
| 2058 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2059 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2060 | if (*p != ',' && *skipwhite(p) == ',') |
| 2061 | { |
| 2062 | semsg(_(e_no_white_before), ","); |
| 2063 | return &t_any; |
| 2064 | } |
| 2065 | if (*p == ',') |
| 2066 | { |
| 2067 | ++p; |
| 2068 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2069 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2070 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2071 | return &t_any; |
| 2072 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2073 | } |
| 2074 | p = skipwhite(p); |
| 2075 | if (argcount == MAX_FUNC_ARGS) |
| 2076 | { |
| 2077 | emsg(_("E740: Too many argument types")); |
| 2078 | return &t_any; |
| 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | p = skipwhite(p); |
| 2083 | if (*p != ')') |
| 2084 | { |
| 2085 | emsg(_(e_missing_close)); |
| 2086 | return &t_any; |
| 2087 | } |
| 2088 | *arg = p + 1; |
| 2089 | } |
| 2090 | if (**arg == ':') |
| 2091 | { |
| 2092 | // parse return type |
| 2093 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2094 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2095 | semsg(_(e_white_after), ":"); |
| 2096 | *arg = skipwhite(*arg); |
| 2097 | ret_type = parse_type(arg, type_gap); |
| 2098 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2099 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2100 | type = get_func_type(ret_type, argcount, type_gap); |
| 2101 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2102 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2103 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 2104 | type->tt_flags = flags; |
| 2105 | if (argcount > 0) |
| 2106 | { |
| 2107 | type->tt_argcount = argcount; |
| 2108 | type->tt_min_argcount = first_optional == -1 |
| 2109 | ? argcount : first_optional; |
| 2110 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2111 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2112 | return &t_any; |
| 2113 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2114 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2115 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2116 | } |
| 2117 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2118 | } |
| 2119 | break; |
| 2120 | case 'j': |
| 2121 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 2122 | { |
| 2123 | *arg += len; |
| 2124 | return &t_job; |
| 2125 | } |
| 2126 | break; |
| 2127 | case 'l': |
| 2128 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 2129 | { |
| 2130 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2131 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2132 | } |
| 2133 | break; |
| 2134 | case 'n': |
| 2135 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 2136 | { |
| 2137 | *arg += len; |
| 2138 | return &t_number; |
| 2139 | } |
| 2140 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2141 | case 's': |
| 2142 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 2143 | { |
| 2144 | *arg += len; |
| 2145 | return &t_string; |
| 2146 | } |
| 2147 | break; |
| 2148 | case 'v': |
| 2149 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 2150 | { |
| 2151 | *arg += len; |
| 2152 | return &t_void; |
| 2153 | } |
| 2154 | break; |
| 2155 | } |
| 2156 | |
| 2157 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 2158 | return &t_any; |
| 2159 | } |
| 2160 | |
| 2161 | /* |
| 2162 | * Check if "type1" and "type2" are exactly the same. |
| 2163 | */ |
| 2164 | static int |
| 2165 | equal_type(type_T *type1, type_T *type2) |
| 2166 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2167 | int i; |
| 2168 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2169 | if (type1->tt_type != type2->tt_type) |
| 2170 | return FALSE; |
| 2171 | switch (type1->tt_type) |
| 2172 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2173 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2174 | case VAR_ANY: |
| 2175 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2176 | case VAR_SPECIAL: |
| 2177 | case VAR_BOOL: |
| 2178 | case VAR_NUMBER: |
| 2179 | case VAR_FLOAT: |
| 2180 | case VAR_STRING: |
| 2181 | case VAR_BLOB: |
| 2182 | case VAR_JOB: |
| 2183 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2184 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2185 | case VAR_LIST: |
| 2186 | case VAR_DICT: |
| 2187 | return equal_type(type1->tt_member, type2->tt_member); |
| 2188 | case VAR_FUNC: |
| 2189 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2190 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2191 | || type1->tt_argcount != type2->tt_argcount) |
| 2192 | return FALSE; |
| 2193 | if (type1->tt_argcount < 0 |
| 2194 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2195 | return TRUE; |
| 2196 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2197 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2198 | return FALSE; |
| 2199 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2200 | } |
| 2201 | return TRUE; |
| 2202 | } |
| 2203 | |
| 2204 | /* |
| 2205 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2206 | * "type2" and "dest" may be the same. |
| 2207 | */ |
| 2208 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2209 | 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] | 2210 | { |
| 2211 | if (equal_type(type1, type2)) |
| 2212 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2213 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2214 | return; |
| 2215 | } |
| 2216 | |
| 2217 | if (type1->tt_type == type2->tt_type) |
| 2218 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2219 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2220 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2221 | type_T *common; |
| 2222 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2223 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2224 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2225 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2226 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2227 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2228 | return; |
| 2229 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2230 | if (type1->tt_type == VAR_FUNC) |
| 2231 | { |
| 2232 | type_T *common; |
| 2233 | |
| 2234 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2235 | if (type1->tt_argcount == type2->tt_argcount |
| 2236 | && type1->tt_argcount >= 0) |
| 2237 | { |
| 2238 | int argcount = type1->tt_argcount; |
| 2239 | int i; |
| 2240 | |
| 2241 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2242 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2243 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2244 | if (func_type_add_arg_types(*dest, argcount, |
| 2245 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2246 | for (i = 0; i < argcount; ++i) |
| 2247 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2248 | &(*dest)->tt_args[i], type_gap); |
| 2249 | } |
| 2250 | } |
| 2251 | else |
| 2252 | *dest = alloc_func_type(common, -1, type_gap); |
| 2253 | return; |
| 2254 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2255 | } |
| 2256 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2257 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | char * |
| 2261 | vartype_name(vartype_T type) |
| 2262 | { |
| 2263 | switch (type) |
| 2264 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2265 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2266 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2267 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2268 | case VAR_SPECIAL: return "special"; |
| 2269 | case VAR_BOOL: return "bool"; |
| 2270 | case VAR_NUMBER: return "number"; |
| 2271 | case VAR_FLOAT: return "float"; |
| 2272 | case VAR_STRING: return "string"; |
| 2273 | case VAR_BLOB: return "blob"; |
| 2274 | case VAR_JOB: return "job"; |
| 2275 | case VAR_CHANNEL: return "channel"; |
| 2276 | case VAR_LIST: return "list"; |
| 2277 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2278 | |
| 2279 | case VAR_FUNC: |
| 2280 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2281 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2282 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | /* |
| 2286 | * Return the name of a type. |
| 2287 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2288 | */ |
| 2289 | char * |
| 2290 | type_name(type_T *type, char **tofree) |
| 2291 | { |
| 2292 | char *name = vartype_name(type->tt_type); |
| 2293 | |
| 2294 | *tofree = NULL; |
| 2295 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2296 | { |
| 2297 | char *member_free; |
| 2298 | char *member_name = type_name(type->tt_member, &member_free); |
| 2299 | size_t len; |
| 2300 | |
| 2301 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2302 | *tofree = alloc(len); |
| 2303 | if (*tofree != NULL) |
| 2304 | { |
| 2305 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2306 | vim_free(member_free); |
| 2307 | return *tofree; |
| 2308 | } |
| 2309 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2310 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2311 | { |
| 2312 | garray_T ga; |
| 2313 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2314 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2315 | |
| 2316 | ga_init2(&ga, 1, 100); |
| 2317 | if (ga_grow(&ga, 20) == FAIL) |
| 2318 | return "[unknown]"; |
| 2319 | *tofree = ga.ga_data; |
| 2320 | STRCPY(ga.ga_data, "func("); |
| 2321 | ga.ga_len += 5; |
| 2322 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2323 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2324 | { |
| 2325 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2326 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2327 | int len; |
| 2328 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2329 | if (type->tt_args == NULL) |
| 2330 | arg_type = "[unknown]"; |
| 2331 | else |
| 2332 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2333 | if (i > 0) |
| 2334 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2335 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2336 | ga.ga_len += 2; |
| 2337 | } |
| 2338 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2339 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2340 | { |
| 2341 | vim_free(arg_free); |
| 2342 | return "[unknown]"; |
| 2343 | } |
| 2344 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2345 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2346 | { |
| 2347 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2348 | ga.ga_len += 3; |
| 2349 | } |
| 2350 | else if (i >= type->tt_min_argcount) |
| 2351 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2352 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2353 | ga.ga_len += len; |
| 2354 | vim_free(arg_free); |
| 2355 | } |
| 2356 | |
| 2357 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2358 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2359 | else |
| 2360 | { |
| 2361 | char *ret_free; |
| 2362 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2363 | int len; |
| 2364 | |
| 2365 | len = (int)STRLEN(ret_name) + 4; |
| 2366 | if (ga_grow(&ga, len) == FAIL) |
| 2367 | { |
| 2368 | vim_free(ret_free); |
| 2369 | return "[unknown]"; |
| 2370 | } |
| 2371 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2372 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2373 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2374 | vim_free(ret_free); |
| 2375 | } |
| 2376 | return ga.ga_data; |
| 2377 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2378 | |
| 2379 | return name; |
| 2380 | } |
| 2381 | |
| 2382 | /* |
| 2383 | * Find "name" in script-local items of script "sid". |
| 2384 | * Returns the index in "sn_var_vals" if found. |
| 2385 | * If found but not in "sn_var_vals" returns -1. |
| 2386 | * If not found returns -2. |
| 2387 | */ |
| 2388 | int |
| 2389 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2390 | { |
| 2391 | hashtab_T *ht; |
| 2392 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2393 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2394 | int idx; |
| 2395 | |
| 2396 | // First look the name up in the hashtable. |
| 2397 | if (sid <= 0 || sid > script_items.ga_len) |
| 2398 | return -1; |
| 2399 | ht = &SCRIPT_VARS(sid); |
| 2400 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2401 | if (di == NULL) |
| 2402 | return -2; |
| 2403 | |
| 2404 | // Now find the svar_T index in sn_var_vals. |
| 2405 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2406 | { |
| 2407 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2408 | |
| 2409 | if (sv->sv_tv == &di->di_tv) |
| 2410 | { |
| 2411 | if (check_writable && sv->sv_const) |
| 2412 | semsg(_(e_readonlyvar), name); |
| 2413 | return idx; |
| 2414 | } |
| 2415 | } |
| 2416 | return -1; |
| 2417 | } |
| 2418 | |
| 2419 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2420 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2421 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2422 | */ |
| 2423 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2424 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2425 | { |
Bram Moolenaar | 086eb18 | 2020-07-01 16:00:44 +0200 | [diff] [blame] | 2426 | scriptitem_T *si; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2427 | int idx; |
| 2428 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2429 | if (current_sctx.sc_sid <= 0) |
| 2430 | return NULL; |
| 2431 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2432 | if (cctx != NULL) |
| 2433 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2434 | { |
| 2435 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2436 | + idx; |
| 2437 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2438 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2439 | : STRLEN(import->imp_name) == len |
| 2440 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2441 | return import; |
| 2442 | } |
| 2443 | |
| 2444 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2445 | { |
| 2446 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2447 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2448 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2449 | : STRLEN(import->imp_name) == len |
| 2450 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2451 | return import; |
| 2452 | } |
| 2453 | return NULL; |
| 2454 | } |
| 2455 | |
| 2456 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2457 | * Free all imported variables. |
| 2458 | */ |
| 2459 | static void |
| 2460 | free_imported(cctx_T *cctx) |
| 2461 | { |
| 2462 | int idx; |
| 2463 | |
| 2464 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2465 | { |
| 2466 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2467 | |
| 2468 | vim_free(import->imp_name); |
| 2469 | } |
| 2470 | ga_clear(&cctx->ctx_imports); |
| 2471 | } |
| 2472 | |
| 2473 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2474 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2475 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 2476 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2477 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2478 | { |
| 2479 | return p[0] == '#' && p[1] != '{'; |
| 2480 | } |
| 2481 | |
| 2482 | /* |
| 2483 | * Return a pointer to the next line that isn't empty or only contains a |
| 2484 | * comment. Skips over white space. |
| 2485 | * Returns NULL if there is none. |
| 2486 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2487 | char_u * |
| 2488 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2489 | { |
| 2490 | int lnum = cctx->ctx_lnum; |
| 2491 | |
| 2492 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2493 | { |
| 2494 | 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] | 2495 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2496 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 2497 | if (line == NULL) |
| 2498 | break; |
| 2499 | p = skipwhite(line); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2500 | if (*p != NUL && !vim9_comment_start(p)) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2501 | return p; |
| 2502 | } |
| 2503 | return NULL; |
| 2504 | } |
| 2505 | |
| 2506 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2507 | * Called when checking for a following operator at "arg". When the rest of |
| 2508 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2509 | * line return a pointer to it and set "nextp". |
| 2510 | * Otherwise skip over white space. |
| 2511 | */ |
| 2512 | static char_u * |
| 2513 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2514 | { |
| 2515 | char_u *p = skipwhite(arg); |
| 2516 | |
| 2517 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2518 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2519 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2520 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2521 | if (*nextp != NULL) |
| 2522 | return *nextp; |
| 2523 | } |
| 2524 | return p; |
| 2525 | } |
| 2526 | |
| 2527 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2528 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2529 | * 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] | 2530 | * Returns NULL when at the end. |
| 2531 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2532 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2533 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2534 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2535 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2536 | |
| 2537 | do |
| 2538 | { |
| 2539 | ++cctx->ctx_lnum; |
| 2540 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2541 | { |
| 2542 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2543 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2544 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2545 | 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] | 2546 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2547 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2548 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2549 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2550 | return line; |
| 2551 | } |
| 2552 | |
| 2553 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2554 | * 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] | 2555 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2556 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2557 | */ |
| 2558 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2559 | 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] | 2560 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2561 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2562 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2563 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2564 | |
| 2565 | if (next == NULL) |
| 2566 | return FAIL; |
| 2567 | *arg = skipwhite(next); |
| 2568 | } |
| 2569 | return OK; |
| 2570 | } |
| 2571 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2572 | /* |
| 2573 | * Idem, and give an error when failed. |
| 2574 | */ |
| 2575 | static int |
| 2576 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2577 | { |
| 2578 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2579 | { |
| 2580 | emsg(_("E1097: line incomplete")); |
| 2581 | return FAIL; |
| 2582 | } |
| 2583 | return OK; |
| 2584 | } |
| 2585 | |
| 2586 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2587 | // Structure passed between the compile_expr* functions to keep track of |
| 2588 | // constants that have been parsed but for which no code was produced yet. If |
| 2589 | // possible expressions on these constants are applied at compile time. If |
| 2590 | // that is not possible, the code to push the constants needs to be generated |
| 2591 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2592 | // Using 50 should be more than enough of 5 levels of (). |
| 2593 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2594 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2595 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2596 | int pp_used; // active entries in pp_tv[] |
| 2597 | } ppconst_T; |
| 2598 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2599 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2600 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2601 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2602 | /* |
| 2603 | * Generate a PUSH instruction for "tv". |
| 2604 | * "tv" will be consumed or cleared. |
| 2605 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2606 | */ |
| 2607 | static int |
| 2608 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2609 | { |
| 2610 | if (tv != NULL) |
| 2611 | { |
| 2612 | switch (tv->v_type) |
| 2613 | { |
| 2614 | case VAR_UNKNOWN: |
| 2615 | break; |
| 2616 | case VAR_BOOL: |
| 2617 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2618 | break; |
| 2619 | case VAR_SPECIAL: |
| 2620 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2621 | break; |
| 2622 | case VAR_NUMBER: |
| 2623 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2624 | break; |
| 2625 | #ifdef FEAT_FLOAT |
| 2626 | case VAR_FLOAT: |
| 2627 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2628 | break; |
| 2629 | #endif |
| 2630 | case VAR_BLOB: |
| 2631 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2632 | tv->vval.v_blob = NULL; |
| 2633 | break; |
| 2634 | case VAR_STRING: |
| 2635 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2636 | tv->vval.v_string = NULL; |
| 2637 | break; |
| 2638 | default: |
| 2639 | iemsg("constant type not supported"); |
| 2640 | clear_tv(tv); |
| 2641 | return FAIL; |
| 2642 | } |
| 2643 | tv->v_type = VAR_UNKNOWN; |
| 2644 | } |
| 2645 | return OK; |
| 2646 | } |
| 2647 | |
| 2648 | /* |
| 2649 | * Generate code for any ppconst entries. |
| 2650 | */ |
| 2651 | static int |
| 2652 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2653 | { |
| 2654 | int i; |
| 2655 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2656 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2657 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2658 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2659 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2660 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2661 | ret = FAIL; |
| 2662 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2663 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2664 | return ret; |
| 2665 | } |
| 2666 | |
| 2667 | /* |
| 2668 | * Clear ppconst constants. Used when failing. |
| 2669 | */ |
| 2670 | static void |
| 2671 | clear_ppconst(ppconst_T *ppconst) |
| 2672 | { |
| 2673 | int i; |
| 2674 | |
| 2675 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2676 | clear_tv(&ppconst->pp_tv[i]); |
| 2677 | ppconst->pp_used = 0; |
| 2678 | } |
| 2679 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2680 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2681 | * Generate an instruction to load script-local variable "name", without the |
| 2682 | * leading "s:". |
| 2683 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2684 | */ |
| 2685 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2686 | compile_load_scriptvar( |
| 2687 | cctx_T *cctx, |
| 2688 | char_u *name, // variable NUL terminated |
| 2689 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2690 | char_u **end, // end of variable |
| 2691 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2692 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2693 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2694 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2695 | imported_T *import; |
| 2696 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2697 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2698 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2699 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2700 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2701 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2702 | } |
| 2703 | if (idx >= 0) |
| 2704 | { |
| 2705 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2706 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2707 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2708 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2709 | return OK; |
| 2710 | } |
| 2711 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2712 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2713 | if (import != NULL) |
| 2714 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2715 | if (import->imp_all) |
| 2716 | { |
| 2717 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2718 | char_u *exp_name; |
| 2719 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2720 | ufunc_T *ufunc; |
| 2721 | type_T *type; |
| 2722 | |
| 2723 | // Used "import * as Name", need to lookup the member. |
| 2724 | if (*p != '.') |
| 2725 | { |
| 2726 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2727 | return FAIL; |
| 2728 | } |
| 2729 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2730 | if (VIM_ISWHITE(*p)) |
| 2731 | { |
| 2732 | emsg(_("E1074: no white space allowed after dot")); |
| 2733 | return FAIL; |
| 2734 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2735 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2736 | // isolate one name |
| 2737 | exp_name = p; |
| 2738 | while (eval_isnamec(*p)) |
| 2739 | ++p; |
| 2740 | cc = *p; |
| 2741 | *p = NUL; |
| 2742 | |
| 2743 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2744 | *p = cc; |
| 2745 | p = skipwhite(p); |
| 2746 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2747 | // TODO: what if it is a function? |
| 2748 | if (idx < 0) |
| 2749 | return FAIL; |
| 2750 | *end = p; |
| 2751 | |
| 2752 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2753 | import->imp_sid, |
| 2754 | idx, |
| 2755 | type); |
| 2756 | } |
| 2757 | else |
| 2758 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2759 | // TODO: check this is a variable, not a function? |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2760 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2761 | import->imp_sid, |
| 2762 | import->imp_var_vals_idx, |
| 2763 | import->imp_type); |
| 2764 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2765 | return OK; |
| 2766 | } |
| 2767 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2768 | if (error) |
| 2769 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2770 | return FAIL; |
| 2771 | } |
| 2772 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2773 | static int |
| 2774 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2775 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2776 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2777 | |
| 2778 | if (ufunc == NULL) |
| 2779 | return FAIL; |
| 2780 | |
Bram Moolenaar | fe465a0 | 2020-07-07 22:50:12 +0200 | [diff] [blame] | 2781 | return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name), |
| 2782 | ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2783 | } |
| 2784 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2785 | /* |
| 2786 | * Compile a variable name into a load instruction. |
| 2787 | * "end" points to just after the name. |
| 2788 | * When "error" is FALSE do not give an error when not found. |
| 2789 | */ |
| 2790 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2791 | 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] | 2792 | { |
| 2793 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2794 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2795 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2796 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2797 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2798 | |
| 2799 | if (*(*arg + 1) == ':') |
| 2800 | { |
| 2801 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2802 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2803 | { |
| 2804 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2805 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2806 | switch (**arg) |
| 2807 | { |
| 2808 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2809 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2810 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2811 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2812 | default: |
| 2813 | semsg(_(e_namespace), *arg); |
| 2814 | goto theend; |
| 2815 | } |
| 2816 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2817 | goto theend; |
| 2818 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2819 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2820 | else |
| 2821 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2822 | isntype_T isn_type = ISN_DROP; |
| 2823 | |
| 2824 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2825 | if (name == NULL) |
| 2826 | return FAIL; |
| 2827 | |
| 2828 | switch (**arg) |
| 2829 | { |
| 2830 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2831 | break; |
| 2832 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2833 | NULL, NULL, error); |
| 2834 | break; |
| 2835 | case 'g': isn_type = ISN_LOADG; break; |
| 2836 | case 'w': isn_type = ISN_LOADW; break; |
| 2837 | case 't': isn_type = ISN_LOADT; break; |
| 2838 | case 'b': isn_type = ISN_LOADB; break; |
| 2839 | default: semsg(_(e_namespace), *arg); |
| 2840 | goto theend; |
| 2841 | } |
| 2842 | if (isn_type != ISN_DROP) |
| 2843 | { |
| 2844 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2845 | // variables can be defined later, thus we don't check if it |
| 2846 | // exists, give error at runtime. |
| 2847 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2848 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2849 | } |
| 2850 | } |
| 2851 | else |
| 2852 | { |
| 2853 | size_t len = end - *arg; |
| 2854 | int idx; |
| 2855 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2856 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2857 | |
| 2858 | name = vim_strnsave(*arg, end - *arg); |
| 2859 | if (name == NULL) |
| 2860 | return FAIL; |
| 2861 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2862 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2863 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2864 | if (!gen_load_outer) |
| 2865 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2866 | } |
| 2867 | else |
| 2868 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2869 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2870 | |
| 2871 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2872 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2873 | type = lvar->lv_type; |
| 2874 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2875 | if (lvar->lv_from_outer) |
| 2876 | gen_load_outer = TRUE; |
| 2877 | else |
| 2878 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2879 | } |
| 2880 | else |
| 2881 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2882 | // "var" can be script-local even without using "s:" if it |
| 2883 | // already exists. |
| 2884 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2885 | == SCRIPT_VERSION_VIM9 |
| 2886 | || lookup_script(*arg, len) == OK) |
| 2887 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2888 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2889 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2890 | // When the name starts with an uppercase letter or "x:" it |
| 2891 | // can be a user defined function. |
| 2892 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2893 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2894 | } |
| 2895 | } |
| 2896 | if (gen_load) |
| 2897 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2898 | if (gen_load_outer) |
| 2899 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | *arg = end; |
| 2903 | |
| 2904 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2905 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2906 | semsg(_(e_var_notfound), name); |
| 2907 | vim_free(name); |
| 2908 | return res; |
| 2909 | } |
| 2910 | |
| 2911 | /* |
| 2912 | * Compile the argument expressions. |
| 2913 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2914 | */ |
| 2915 | static int |
| 2916 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2917 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2918 | char_u *p = *arg; |
| 2919 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2920 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2921 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2922 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2923 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2924 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2925 | if (*p == ')') |
| 2926 | { |
| 2927 | *arg = p + 1; |
| 2928 | return OK; |
| 2929 | } |
| 2930 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2931 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2932 | return FAIL; |
| 2933 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2934 | |
| 2935 | if (*p != ',' && *skipwhite(p) == ',') |
| 2936 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2937 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2938 | p = skipwhite(p); |
| 2939 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2940 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2941 | { |
| 2942 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2943 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2944 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2945 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2946 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2947 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2948 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2949 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2950 | emsg(_(e_missing_close)); |
| 2951 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | /* |
| 2955 | * Compile a function call: name(arg1, arg2) |
| 2956 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2957 | * "argcount_init" is 1 for "value->method()" |
| 2958 | * Instructions: |
| 2959 | * EVAL arg1 |
| 2960 | * EVAL arg2 |
| 2961 | * BCALL / DCALL / UCALL |
| 2962 | */ |
| 2963 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2964 | compile_call( |
| 2965 | char_u **arg, |
| 2966 | size_t varlen, |
| 2967 | cctx_T *cctx, |
| 2968 | ppconst_T *ppconst, |
| 2969 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2970 | { |
| 2971 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2972 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2973 | int argcount = argcount_init; |
| 2974 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2975 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2976 | char_u *tofree = NULL; |
| 2977 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2978 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2979 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2980 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2981 | // we can evaluate "has('name')" at compile time |
| 2982 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2983 | { |
| 2984 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2985 | typval_T argvars[2]; |
| 2986 | |
| 2987 | argvars[0].v_type = VAR_UNKNOWN; |
| 2988 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2989 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2990 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2991 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2992 | s = skipwhite(s); |
| 2993 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 2994 | { |
| 2995 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2996 | |
| 2997 | *arg = s + 1; |
| 2998 | argvars[1].v_type = VAR_UNKNOWN; |
| 2999 | tv->v_type = VAR_NUMBER; |
| 3000 | tv->vval.v_number = 0; |
| 3001 | f_has(argvars, tv); |
| 3002 | clear_tv(&argvars[0]); |
| 3003 | ++ppconst->pp_used; |
| 3004 | return OK; |
| 3005 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 3006 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3010 | return FAIL; |
| 3011 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3012 | if (varlen >= sizeof(namebuf)) |
| 3013 | { |
| 3014 | semsg(_("E1011: name too long: %s"), name); |
| 3015 | return FAIL; |
| 3016 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3017 | vim_strncpy(namebuf, *arg, varlen); |
| 3018 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3019 | |
| 3020 | *arg = skipwhite(*arg + varlen + 1); |
| 3021 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3022 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3023 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3024 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3025 | { |
| 3026 | int idx; |
| 3027 | |
| 3028 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3029 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3030 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 3031 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3032 | else |
| 3033 | semsg(_(e_unknownfunc), namebuf); |
| 3034 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3035 | } |
| 3036 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3037 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 3038 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3039 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3040 | { |
| 3041 | res = generate_CALL(cctx, ufunc, argcount); |
| 3042 | goto theend; |
| 3043 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3044 | |
| 3045 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3046 | // 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] | 3047 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3048 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 3049 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3050 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3051 | garray_T *stack = &cctx->ctx_type_stack; |
| 3052 | type_T *type; |
| 3053 | |
| 3054 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3055 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3056 | goto theend; |
| 3057 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3058 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3059 | // 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] | 3060 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3061 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 3062 | res = generate_UCALL(cctx, name, argcount); |
| 3063 | else |
| 3064 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3065 | |
| 3066 | theend: |
| 3067 | vim_free(tofree); |
| 3068 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 3072 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 3073 | |
| 3074 | /* |
| 3075 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 3076 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3077 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3078 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 3079 | * valid name. |
| 3080 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3081 | static char_u * |
| 3082 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3083 | { |
| 3084 | char_u *p; |
| 3085 | |
| 3086 | // Quick check for valid starting character. |
| 3087 | if (!eval_isnamec1(*arg)) |
| 3088 | return arg; |
| 3089 | |
| 3090 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 3091 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 3092 | // and can be used in slice "[n:]". |
| 3093 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3094 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3095 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 3096 | break; |
| 3097 | return p; |
| 3098 | } |
| 3099 | |
| 3100 | /* |
| 3101 | * 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] | 3102 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3103 | */ |
| 3104 | char_u * |
| 3105 | to_name_const_end(char_u *arg) |
| 3106 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3107 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3108 | typval_T rettv; |
| 3109 | |
| 3110 | if (p == arg && *arg == '[') |
| 3111 | { |
| 3112 | |
| 3113 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3114 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3115 | p = arg; |
| 3116 | } |
| 3117 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 3118 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3119 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3120 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3121 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3122 | p = arg; |
| 3123 | } |
| 3124 | else if (p == arg && *arg == '{') |
| 3125 | { |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3126 | int ret = get_lambda_tv(&p, &rettv, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3127 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3128 | // Can be "{x -> ret}()". |
| 3129 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3130 | if (ret == NOTDONE) |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3131 | ret = eval_dict(&p, &rettv, NULL, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3132 | if (ret != OK) |
| 3133 | p = arg; |
| 3134 | } |
| 3135 | |
| 3136 | return p; |
| 3137 | } |
| 3138 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3139 | /* |
| 3140 | * parse a list: [expr, expr] |
| 3141 | * "*arg" points to the '['. |
| 3142 | */ |
| 3143 | static int |
| 3144 | compile_list(char_u **arg, cctx_T *cctx) |
| 3145 | { |
| 3146 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3147 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3148 | int count = 0; |
| 3149 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3150 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3151 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3152 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3153 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3154 | semsg(_(e_list_end), *arg); |
| 3155 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3156 | } |
| 3157 | if (*p == ']') |
| 3158 | { |
| 3159 | ++p; |
| 3160 | // Allow for following comment, after at least one space. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3161 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '#') |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3162 | p += STRLEN(p); |
| 3163 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3164 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3165 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3166 | break; |
| 3167 | ++count; |
| 3168 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3169 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3170 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3171 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 3172 | { |
| 3173 | semsg(_(e_white_after), ","); |
| 3174 | return FAIL; |
| 3175 | } |
| 3176 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3177 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3178 | p = skipwhite(p); |
| 3179 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3180 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3181 | |
| 3182 | generate_NEWLIST(cctx, count); |
| 3183 | return OK; |
| 3184 | } |
| 3185 | |
| 3186 | /* |
| 3187 | * parse a lambda: {arg, arg -> expr} |
| 3188 | * "*arg" points to the '{'. |
| 3189 | */ |
| 3190 | static int |
| 3191 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 3192 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3193 | typval_T rettv; |
| 3194 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3195 | evalarg_T evalarg; |
| 3196 | |
| 3197 | CLEAR_FIELD(evalarg); |
| 3198 | evalarg.eval_flags = EVAL_EVALUATE; |
| 3199 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3200 | |
| 3201 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3202 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3203 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3204 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3205 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3206 | ++ufunc->uf_refcount; |
| 3207 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3208 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3209 | |
| 3210 | // The function will have one line: "return {expr}". |
| 3211 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3212 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3213 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 3214 | clear_evalarg(&evalarg, NULL); |
| 3215 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 3216 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 3217 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3218 | |
| 3219 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3220 | return FAIL; |
| 3221 | } |
| 3222 | |
| 3223 | /* |
| 3224 | * Compile a lamda call: expr->{lambda}(args) |
| 3225 | * "arg" points to the "{". |
| 3226 | */ |
| 3227 | static int |
| 3228 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 3229 | { |
| 3230 | ufunc_T *ufunc; |
| 3231 | typval_T rettv; |
| 3232 | int argcount = 1; |
| 3233 | int ret = FAIL; |
| 3234 | |
| 3235 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3236 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3237 | return FAIL; |
| 3238 | |
| 3239 | if (**arg != '(') |
| 3240 | { |
| 3241 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 3242 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3243 | else |
| 3244 | semsg(_(e_missing_paren), "lambda"); |
| 3245 | clear_tv(&rettv); |
| 3246 | return FAIL; |
| 3247 | } |
| 3248 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3249 | ufunc = rettv.vval.v_partial->pt_func; |
| 3250 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3251 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3252 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3253 | |
| 3254 | // The function will have one line: "return {expr}". |
| 3255 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3256 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3257 | |
| 3258 | // compile the arguments |
| 3259 | *arg = skipwhite(*arg + 1); |
| 3260 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 3261 | // call the compiled function |
| 3262 | ret = generate_CALL(cctx, ufunc, argcount); |
| 3263 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3264 | if (ret == FAIL) |
| 3265 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3266 | return ret; |
| 3267 | } |
| 3268 | |
| 3269 | /* |
| 3270 | * parse a dict: {'key': val} or #{key: val} |
| 3271 | * "*arg" points to the '{'. |
| 3272 | */ |
| 3273 | static int |
| 3274 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 3275 | { |
| 3276 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3277 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3278 | int count = 0; |
| 3279 | dict_T *d = dict_alloc(); |
| 3280 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3281 | char_u *whitep = *arg; |
| 3282 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3283 | |
| 3284 | if (d == NULL) |
| 3285 | return FAIL; |
| 3286 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3287 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3288 | { |
| 3289 | char_u *key = NULL; |
| 3290 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3291 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3292 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3293 | *arg = NULL; |
| 3294 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | if (**arg == '}') |
| 3298 | break; |
| 3299 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3300 | if (literal) |
| 3301 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3302 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3303 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3304 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3305 | { |
| 3306 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 3307 | return FAIL; |
| 3308 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3309 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3310 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3311 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3312 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3313 | } |
| 3314 | else |
| 3315 | { |
| 3316 | isn_T *isn; |
| 3317 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3318 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3319 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3320 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3321 | if (isn->isn_type == ISN_PUSHS) |
| 3322 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3323 | else |
| 3324 | { |
| 3325 | type_T *keytype = ((type_T **)stack->ga_data) |
| 3326 | [stack->ga_len - 1]; |
| 3327 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 3328 | return FAIL; |
| 3329 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | // Check for duplicate keys, if using string keys. |
| 3333 | if (key != NULL) |
| 3334 | { |
| 3335 | item = dict_find(d, key, -1); |
| 3336 | if (item != NULL) |
| 3337 | { |
| 3338 | semsg(_(e_duplicate_key), key); |
| 3339 | goto failret; |
| 3340 | } |
| 3341 | item = dictitem_alloc(key); |
| 3342 | if (item != NULL) |
| 3343 | { |
| 3344 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3345 | item->di_tv.v_lock = 0; |
| 3346 | if (dict_add(d, item) == FAIL) |
| 3347 | dictitem_free(item); |
| 3348 | } |
| 3349 | } |
| 3350 | |
| 3351 | *arg = skipwhite(*arg); |
| 3352 | if (**arg != ':') |
| 3353 | { |
| 3354 | semsg(_(e_missing_dict_colon), *arg); |
| 3355 | return FAIL; |
| 3356 | } |
| 3357 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3358 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3359 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3360 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3361 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3362 | *arg = NULL; |
| 3363 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3364 | } |
| 3365 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3366 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3367 | return FAIL; |
| 3368 | ++count; |
| 3369 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3370 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3371 | *arg = skipwhite(*arg); |
| 3372 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3373 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3374 | *arg = NULL; |
| 3375 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3376 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3377 | if (**arg == '}') |
| 3378 | break; |
| 3379 | if (**arg != ',') |
| 3380 | { |
| 3381 | semsg(_(e_missing_dict_comma), *arg); |
| 3382 | goto failret; |
| 3383 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3384 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3385 | *arg = skipwhite(*arg + 1); |
| 3386 | } |
| 3387 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3388 | *arg = *arg + 1; |
| 3389 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3390 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3391 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3392 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3393 | *arg += STRLEN(*arg); |
| 3394 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3395 | dict_unref(d); |
| 3396 | return generate_NEWDICT(cctx, count); |
| 3397 | |
| 3398 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3399 | if (*arg == NULL) |
| 3400 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3401 | dict_unref(d); |
| 3402 | return FAIL; |
| 3403 | } |
| 3404 | |
| 3405 | /* |
| 3406 | * Compile "&option". |
| 3407 | */ |
| 3408 | static int |
| 3409 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3410 | { |
| 3411 | typval_T rettv; |
| 3412 | char_u *start = *arg; |
| 3413 | int ret; |
| 3414 | |
| 3415 | // parse the option and get the current value to get the type. |
| 3416 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3417 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3418 | if (ret == OK) |
| 3419 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3420 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3421 | char_u *name = vim_strnsave(start, *arg - start); |
| 3422 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3423 | |
| 3424 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3425 | vim_free(name); |
| 3426 | } |
| 3427 | clear_tv(&rettv); |
| 3428 | |
| 3429 | return ret; |
| 3430 | } |
| 3431 | |
| 3432 | /* |
| 3433 | * Compile "$VAR". |
| 3434 | */ |
| 3435 | static int |
| 3436 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3437 | { |
| 3438 | char_u *start = *arg; |
| 3439 | int len; |
| 3440 | int ret; |
| 3441 | char_u *name; |
| 3442 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3443 | ++*arg; |
| 3444 | len = get_env_len(arg); |
| 3445 | if (len == 0) |
| 3446 | { |
| 3447 | semsg(_(e_syntax_at), start - 1); |
| 3448 | return FAIL; |
| 3449 | } |
| 3450 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3451 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3452 | name = vim_strnsave(start, len + 1); |
| 3453 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3454 | vim_free(name); |
| 3455 | return ret; |
| 3456 | } |
| 3457 | |
| 3458 | /* |
| 3459 | * Compile "@r". |
| 3460 | */ |
| 3461 | static int |
| 3462 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3463 | { |
| 3464 | int ret; |
| 3465 | |
| 3466 | ++*arg; |
| 3467 | if (**arg == NUL) |
| 3468 | { |
| 3469 | semsg(_(e_syntax_at), *arg - 1); |
| 3470 | return FAIL; |
| 3471 | } |
| 3472 | if (!valid_yank_reg(**arg, TRUE)) |
| 3473 | { |
| 3474 | emsg_invreg(**arg); |
| 3475 | return FAIL; |
| 3476 | } |
| 3477 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3478 | ++*arg; |
| 3479 | return ret; |
| 3480 | } |
| 3481 | |
| 3482 | /* |
| 3483 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3484 | */ |
| 3485 | static int |
| 3486 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3487 | { |
| 3488 | char_u *p = end; |
| 3489 | |
| 3490 | // this works from end to start |
| 3491 | while (p > start) |
| 3492 | { |
| 3493 | --p; |
| 3494 | if (*p == '-' || *p == '+') |
| 3495 | { |
| 3496 | // only '-' has an effect, for '+' we only check the type |
| 3497 | #ifdef FEAT_FLOAT |
| 3498 | if (rettv->v_type == VAR_FLOAT) |
| 3499 | { |
| 3500 | if (*p == '-') |
| 3501 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3502 | } |
| 3503 | else |
| 3504 | #endif |
| 3505 | { |
| 3506 | varnumber_T val; |
| 3507 | int error = FALSE; |
| 3508 | |
| 3509 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3510 | // here |
| 3511 | if (check_not_string(rettv) == FAIL) |
| 3512 | return FAIL; |
| 3513 | val = tv_get_number_chk(rettv, &error); |
| 3514 | clear_tv(rettv); |
| 3515 | if (error) |
| 3516 | return FAIL; |
| 3517 | if (*p == '-') |
| 3518 | val = -val; |
| 3519 | rettv->v_type = VAR_NUMBER; |
| 3520 | rettv->vval.v_number = val; |
| 3521 | } |
| 3522 | } |
| 3523 | else |
| 3524 | { |
| 3525 | int v = tv2bool(rettv); |
| 3526 | |
| 3527 | // '!' is permissive in the type. |
| 3528 | clear_tv(rettv); |
| 3529 | rettv->v_type = VAR_BOOL; |
| 3530 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3531 | } |
| 3532 | } |
| 3533 | return OK; |
| 3534 | } |
| 3535 | |
| 3536 | /* |
| 3537 | * Recognize v: variables that are constants and set "rettv". |
| 3538 | */ |
| 3539 | static void |
| 3540 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3541 | { |
| 3542 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3543 | { |
| 3544 | rettv->v_type = VAR_BOOL; |
| 3545 | rettv->vval.v_number = VVAL_TRUE; |
| 3546 | *arg += 6; |
| 3547 | } |
| 3548 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3549 | { |
| 3550 | rettv->v_type = VAR_BOOL; |
| 3551 | rettv->vval.v_number = VVAL_FALSE; |
| 3552 | *arg += 7; |
| 3553 | } |
| 3554 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3555 | { |
| 3556 | rettv->v_type = VAR_SPECIAL; |
| 3557 | rettv->vval.v_number = VVAL_NULL; |
| 3558 | *arg += 6; |
| 3559 | } |
| 3560 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3561 | { |
| 3562 | rettv->v_type = VAR_SPECIAL; |
| 3563 | rettv->vval.v_number = VVAL_NONE; |
| 3564 | *arg += 6; |
| 3565 | } |
| 3566 | } |
| 3567 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3568 | static exptype_T |
| 3569 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3570 | { |
| 3571 | exptype_T type = EXPR_UNKNOWN; |
| 3572 | int i; |
| 3573 | |
| 3574 | switch (p[0]) |
| 3575 | { |
| 3576 | case '=': if (p[1] == '=') |
| 3577 | type = EXPR_EQUAL; |
| 3578 | else if (p[1] == '~') |
| 3579 | type = EXPR_MATCH; |
| 3580 | break; |
| 3581 | case '!': if (p[1] == '=') |
| 3582 | type = EXPR_NEQUAL; |
| 3583 | else if (p[1] == '~') |
| 3584 | type = EXPR_NOMATCH; |
| 3585 | break; |
| 3586 | case '>': if (p[1] != '=') |
| 3587 | { |
| 3588 | type = EXPR_GREATER; |
| 3589 | *len = 1; |
| 3590 | } |
| 3591 | else |
| 3592 | type = EXPR_GEQUAL; |
| 3593 | break; |
| 3594 | case '<': if (p[1] != '=') |
| 3595 | { |
| 3596 | type = EXPR_SMALLER; |
| 3597 | *len = 1; |
| 3598 | } |
| 3599 | else |
| 3600 | type = EXPR_SEQUAL; |
| 3601 | break; |
| 3602 | case 'i': if (p[1] == 's') |
| 3603 | { |
| 3604 | // "is" and "isnot"; but not a prefix of a name |
| 3605 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3606 | *len = 5; |
| 3607 | i = p[*len]; |
| 3608 | if (!isalnum(i) && i != '_') |
| 3609 | { |
| 3610 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3611 | *type_is = TRUE; |
| 3612 | } |
| 3613 | } |
| 3614 | break; |
| 3615 | } |
| 3616 | return type; |
| 3617 | } |
| 3618 | |
| 3619 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3620 | * Compile code to apply '-', '+' and '!'. |
| 3621 | */ |
| 3622 | static int |
| 3623 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3624 | { |
| 3625 | char_u *p = end; |
| 3626 | |
| 3627 | // this works from end to start |
| 3628 | while (p > start) |
| 3629 | { |
| 3630 | --p; |
| 3631 | if (*p == '-' || *p == '+') |
| 3632 | { |
| 3633 | int negate = *p == '-'; |
| 3634 | isn_T *isn; |
| 3635 | |
| 3636 | // TODO: check type |
| 3637 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3638 | { |
| 3639 | --p; |
| 3640 | if (*p == '-') |
| 3641 | negate = !negate; |
| 3642 | } |
| 3643 | // only '-' has an effect, for '+' we only check the type |
| 3644 | if (negate) |
| 3645 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3646 | else |
| 3647 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3648 | if (isn == NULL) |
| 3649 | return FAIL; |
| 3650 | } |
| 3651 | else |
| 3652 | { |
| 3653 | int invert = TRUE; |
| 3654 | |
| 3655 | while (p > start && p[-1] == '!') |
| 3656 | { |
| 3657 | --p; |
| 3658 | invert = !invert; |
| 3659 | } |
| 3660 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3661 | return FAIL; |
| 3662 | } |
| 3663 | } |
| 3664 | return OK; |
| 3665 | } |
| 3666 | |
| 3667 | /* |
| 3668 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3669 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3670 | */ |
| 3671 | static int |
| 3672 | compile_subscript( |
| 3673 | char_u **arg, |
| 3674 | cctx_T *cctx, |
| 3675 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3676 | char_u *end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3677 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3678 | { |
| 3679 | for (;;) |
| 3680 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3681 | char_u *p = skipwhite(*arg); |
| 3682 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3683 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3684 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3685 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3686 | |
| 3687 | // If a following line starts with "->{" or "->X" advance to that |
| 3688 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3689 | // Also if a following line starts with ".x". |
| 3690 | if (next != NULL && |
| 3691 | ((next[0] == '-' && next[1] == '>' |
| 3692 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
| 3693 | || (next[0] == '.' && ASCII_ISALPHA(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3694 | { |
| 3695 | next = next_line_from_context(cctx, TRUE); |
| 3696 | if (next == NULL) |
| 3697 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3698 | *arg = next; |
| 3699 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3700 | } |
| 3701 | } |
| 3702 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3703 | if (*p == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3704 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3705 | garray_T *stack = &cctx->ctx_type_stack; |
| 3706 | type_T *type; |
| 3707 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3708 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3709 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3710 | return FAIL; |
| 3711 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3712 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3713 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3714 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3715 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3716 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3717 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3718 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3719 | return FAIL; |
| 3720 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3721 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3722 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3723 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3724 | return FAIL; |
| 3725 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3726 | // something->method() |
| 3727 | // Apply the '!', '-' and '+' first: |
| 3728 | // -1.0->func() works like (-1.0)->func() |
| 3729 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3730 | return FAIL; |
| 3731 | *start_leader = end_leader; // don't apply again later |
| 3732 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3733 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3734 | *arg = skipwhite(p); |
| 3735 | if (may_get_next_line(p, arg, cctx) == FAIL) |
| 3736 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3737 | if (**arg == '{') |
| 3738 | { |
| 3739 | // lambda call: list->{lambda} |
| 3740 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3741 | return FAIL; |
| 3742 | } |
| 3743 | else |
| 3744 | { |
| 3745 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3746 | p = *arg; |
| 3747 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3748 | p += 2; |
| 3749 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3750 | ; |
| 3751 | if (*p != '(') |
| 3752 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3753 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3754 | return FAIL; |
| 3755 | } |
| 3756 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3757 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3758 | return FAIL; |
| 3759 | } |
| 3760 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3761 | else if (*p == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3762 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3763 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3764 | type_T **typep; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3765 | vartype_T vtype; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3766 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3767 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3768 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3769 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3770 | // TODO: blob index |
| 3771 | // TODO: more arguments |
| 3772 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3773 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3774 | return FAIL; |
| 3775 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3776 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3777 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3778 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3779 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3780 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3781 | return FAIL; |
| 3782 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3783 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3784 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3785 | if (**arg != ']') |
| 3786 | { |
| 3787 | emsg(_(e_missbrac)); |
| 3788 | return FAIL; |
| 3789 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3790 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3791 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3792 | // We can index a list and a dict. If we don't know the type |
| 3793 | // we can use the index value type. |
| 3794 | // TODO: If we don't know use an instruction to figure it out at |
| 3795 | // runtime. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3796 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3797 | vtype = (*typep)->tt_type; |
| 3798 | if (*typep == &t_any) |
| 3799 | { |
| 3800 | type_T *valtype = ((type_T **)stack->ga_data) |
| 3801 | [stack->ga_len - 1]; |
| 3802 | if (valtype == &t_string) |
| 3803 | vtype = VAR_DICT; |
| 3804 | } |
| 3805 | if (vtype == VAR_DICT) |
| 3806 | { |
| 3807 | if ((*typep)->tt_type == VAR_DICT) |
| 3808 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3809 | else |
| 3810 | { |
| 3811 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3812 | return FAIL; |
| 3813 | *typep = &t_any; |
| 3814 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3815 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3816 | return FAIL; |
| 3817 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3818 | return FAIL; |
| 3819 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3820 | else if (vtype == VAR_STRING) |
| 3821 | { |
| 3822 | *typep = &t_number; |
| 3823 | if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL) |
| 3824 | return FAIL; |
| 3825 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3826 | else if (vtype == VAR_LIST || *typep == &t_any) |
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 | if ((*typep)->tt_type == VAR_LIST) |
| 3829 | *typep = (*typep)->tt_member; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3830 | if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3831 | return FAIL; |
| 3832 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3833 | else |
| 3834 | { |
| 3835 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3836 | return FAIL; |
| 3837 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3838 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3839 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3840 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3841 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3842 | return FAIL; |
| 3843 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3844 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3845 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3846 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3847 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3848 | p = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3849 | if (eval_isnamec1(*p)) |
| 3850 | while (eval_isnamec(*p)) |
| 3851 | MB_PTR_ADV(p); |
| 3852 | if (p == *arg) |
| 3853 | { |
| 3854 | semsg(_(e_syntax_at), *arg); |
| 3855 | return FAIL; |
| 3856 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3857 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3858 | return FAIL; |
| 3859 | *arg = p; |
| 3860 | } |
| 3861 | else |
| 3862 | break; |
| 3863 | } |
| 3864 | |
| 3865 | // TODO - see handle_subscript(): |
| 3866 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3867 | // Don't do this when "Func" is already a partial that was bound |
| 3868 | // explicitly (pt_auto is FALSE). |
| 3869 | |
| 3870 | return OK; |
| 3871 | } |
| 3872 | |
| 3873 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3874 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3875 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3876 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3877 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3878 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3879 | * |
| 3880 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3881 | */ |
| 3882 | |
| 3883 | /* |
| 3884 | * number number constant |
| 3885 | * 0zFFFFFFFF Blob constant |
| 3886 | * "string" string constant |
| 3887 | * 'string' literal string constant |
| 3888 | * &option-name option value |
| 3889 | * @r register contents |
| 3890 | * identifier variable value |
| 3891 | * function() function call |
| 3892 | * $VAR environment variable |
| 3893 | * (expression) nested expression |
| 3894 | * [expr, expr] List |
| 3895 | * {key: val, key: val} Dictionary |
| 3896 | * #{key: val, key: val} Dictionary with literal keys |
| 3897 | * |
| 3898 | * Also handle: |
| 3899 | * ! in front logical NOT |
| 3900 | * - in front unary minus |
| 3901 | * + in front unary plus (ignored) |
| 3902 | * trailing (arg) funcref/partial call |
| 3903 | * trailing [] subscript in String or List |
| 3904 | * trailing .name entry in Dictionary |
| 3905 | * trailing ->name() method call |
| 3906 | */ |
| 3907 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3908 | compile_expr7( |
| 3909 | char_u **arg, |
| 3910 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3911 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3912 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3913 | char_u *start_leader, *end_leader; |
| 3914 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3915 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3916 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3917 | |
| 3918 | /* |
| 3919 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3920 | */ |
| 3921 | start_leader = *arg; |
| 3922 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3923 | *arg = skipwhite(*arg + 1); |
| 3924 | end_leader = *arg; |
| 3925 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3926 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3927 | switch (**arg) |
| 3928 | { |
| 3929 | /* |
| 3930 | * Number constant. |
| 3931 | */ |
| 3932 | case '0': // also for blob starting with 0z |
| 3933 | case '1': |
| 3934 | case '2': |
| 3935 | case '3': |
| 3936 | case '4': |
| 3937 | case '5': |
| 3938 | case '6': |
| 3939 | case '7': |
| 3940 | case '8': |
| 3941 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3942 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3943 | return FAIL; |
| 3944 | break; |
| 3945 | |
| 3946 | /* |
| 3947 | * String constant: "string". |
| 3948 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3949 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3950 | return FAIL; |
| 3951 | break; |
| 3952 | |
| 3953 | /* |
| 3954 | * Literal string constant: 'str''ing'. |
| 3955 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3956 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3957 | return FAIL; |
| 3958 | break; |
| 3959 | |
| 3960 | /* |
| 3961 | * Constant Vim variable. |
| 3962 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3963 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3964 | ret = NOTDONE; |
| 3965 | break; |
| 3966 | |
| 3967 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3968 | * "true" constant |
| 3969 | */ |
| 3970 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3971 | && !eval_isnamec((*arg)[4])) |
| 3972 | { |
| 3973 | *arg += 4; |
| 3974 | rettv->v_type = VAR_BOOL; |
| 3975 | rettv->vval.v_number = VVAL_TRUE; |
| 3976 | } |
| 3977 | else |
| 3978 | ret = NOTDONE; |
| 3979 | break; |
| 3980 | |
| 3981 | /* |
| 3982 | * "false" constant |
| 3983 | */ |
| 3984 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3985 | && !eval_isnamec((*arg)[5])) |
| 3986 | { |
| 3987 | *arg += 5; |
| 3988 | rettv->v_type = VAR_BOOL; |
| 3989 | rettv->vval.v_number = VVAL_FALSE; |
| 3990 | } |
| 3991 | else |
| 3992 | ret = NOTDONE; |
| 3993 | break; |
| 3994 | |
| 3995 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3996 | * List: [expr, expr] |
| 3997 | */ |
| 3998 | case '[': ret = compile_list(arg, cctx); |
| 3999 | break; |
| 4000 | |
| 4001 | /* |
| 4002 | * Dictionary: #{key: val, key: val} |
| 4003 | */ |
| 4004 | case '#': if ((*arg)[1] == '{') |
| 4005 | { |
| 4006 | ++*arg; |
| 4007 | ret = compile_dict(arg, cctx, TRUE); |
| 4008 | } |
| 4009 | else |
| 4010 | ret = NOTDONE; |
| 4011 | break; |
| 4012 | |
| 4013 | /* |
| 4014 | * Lambda: {arg, arg -> expr} |
| 4015 | * Dictionary: {'key': val, 'key': val} |
| 4016 | */ |
| 4017 | case '{': { |
| 4018 | char_u *start = skipwhite(*arg + 1); |
| 4019 | |
| 4020 | // Find out what comes after the arguments. |
| 4021 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 4022 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4023 | if (ret != FAIL && *start == '>') |
| 4024 | ret = compile_lambda(arg, cctx); |
| 4025 | else |
| 4026 | ret = compile_dict(arg, cctx, FALSE); |
| 4027 | } |
| 4028 | break; |
| 4029 | |
| 4030 | /* |
| 4031 | * Option value: &name |
| 4032 | */ |
| 4033 | case '&': ret = compile_get_option(arg, cctx); |
| 4034 | break; |
| 4035 | |
| 4036 | /* |
| 4037 | * Environment variable: $VAR. |
| 4038 | */ |
| 4039 | case '$': ret = compile_get_env(arg, cctx); |
| 4040 | break; |
| 4041 | |
| 4042 | /* |
| 4043 | * Register contents: @r. |
| 4044 | */ |
| 4045 | case '@': ret = compile_get_register(arg, cctx); |
| 4046 | break; |
| 4047 | /* |
| 4048 | * nested expression: (expression). |
| 4049 | */ |
| 4050 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4051 | |
| 4052 | // recursive! |
| 4053 | if (ppconst->pp_used <= PPSIZE - 10) |
| 4054 | { |
| 4055 | ret = compile_expr1(arg, cctx, ppconst); |
| 4056 | } |
| 4057 | else |
| 4058 | { |
| 4059 | // Not enough space in ppconst, flush constants. |
| 4060 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4061 | return FAIL; |
| 4062 | ret = compile_expr0(arg, cctx); |
| 4063 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4064 | *arg = skipwhite(*arg); |
| 4065 | if (**arg == ')') |
| 4066 | ++*arg; |
| 4067 | else if (ret == OK) |
| 4068 | { |
| 4069 | emsg(_(e_missing_close)); |
| 4070 | ret = FAIL; |
| 4071 | } |
| 4072 | break; |
| 4073 | |
| 4074 | default: ret = NOTDONE; |
| 4075 | break; |
| 4076 | } |
| 4077 | if (ret == FAIL) |
| 4078 | return FAIL; |
| 4079 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4080 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4081 | { |
| 4082 | // apply the '!', '-' and '+' before the constant |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4083 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4084 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4085 | clear_tv(rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4086 | return FAIL; |
| 4087 | } |
| 4088 | start_leader = end_leader; // don't apply again below |
| 4089 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4090 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4091 | clear_tv(rettv); |
| 4092 | else |
| 4093 | // A constant expression can possibly be handled compile time, |
| 4094 | // return the value instead of generating code. |
| 4095 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4096 | } |
| 4097 | else if (ret == NOTDONE) |
| 4098 | { |
| 4099 | char_u *p; |
| 4100 | int r; |
| 4101 | |
| 4102 | if (!eval_isnamec1(**arg)) |
| 4103 | { |
| 4104 | semsg(_("E1015: Name expected: %s"), *arg); |
| 4105 | return FAIL; |
| 4106 | } |
| 4107 | |
| 4108 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4109 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4110 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4111 | { |
| 4112 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 4113 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4114 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4115 | { |
| 4116 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4117 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4118 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4119 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4120 | if (r == FAIL) |
| 4121 | return FAIL; |
| 4122 | } |
| 4123 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4124 | // Handle following "[]", ".member", etc. |
| 4125 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4126 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4127 | ppconst) == FAIL) |
| 4128 | return FAIL; |
| 4129 | if (ppconst->pp_used > 0) |
| 4130 | { |
| 4131 | // apply the '!', '-' and '+' before the constant |
| 4132 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4133 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
| 4134 | return FAIL; |
| 4135 | return OK; |
| 4136 | } |
| 4137 | if (compile_leader(cctx, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4138 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4139 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4140 | } |
| 4141 | |
| 4142 | /* |
| 4143 | * * number multiplication |
| 4144 | * / number division |
| 4145 | * % number modulo |
| 4146 | */ |
| 4147 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4148 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4149 | { |
| 4150 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4151 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4152 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4153 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4154 | // get the first expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4155 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4156 | return FAIL; |
| 4157 | |
| 4158 | /* |
| 4159 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4160 | */ |
| 4161 | for (;;) |
| 4162 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4163 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4164 | if (*op != '*' && *op != '/' && *op != '%') |
| 4165 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4166 | if (next != NULL) |
| 4167 | { |
| 4168 | *arg = next_line_from_context(cctx, TRUE); |
| 4169 | op = skipwhite(*arg); |
| 4170 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4171 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4172 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4173 | { |
| 4174 | char_u buf[3]; |
| 4175 | |
| 4176 | vim_strncpy(buf, op, 1); |
| 4177 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4178 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4179 | } |
| 4180 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4181 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4182 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4183 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4184 | // get the second expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4185 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4186 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4187 | |
| 4188 | if (ppconst->pp_used == ppconst_used + 2 |
| 4189 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4190 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4191 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4192 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4193 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4194 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4195 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4196 | // both are numbers: compute the result |
| 4197 | switch (*op) |
| 4198 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4199 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4200 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4201 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4202 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4203 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4204 | break; |
| 4205 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4206 | tv1->vval.v_number = res; |
| 4207 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4208 | } |
| 4209 | else |
| 4210 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4211 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4212 | generate_two_op(cctx, op); |
| 4213 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4214 | } |
| 4215 | |
| 4216 | return OK; |
| 4217 | } |
| 4218 | |
| 4219 | /* |
| 4220 | * + number addition |
| 4221 | * - number subtraction |
| 4222 | * .. string concatenation |
| 4223 | */ |
| 4224 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4225 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4226 | { |
| 4227 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4228 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4229 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4230 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4231 | |
| 4232 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4233 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4234 | return FAIL; |
| 4235 | |
| 4236 | /* |
| 4237 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4238 | */ |
| 4239 | for (;;) |
| 4240 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4241 | op = may_peek_next_line(cctx, *arg, &next); |
| 4242 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4243 | break; |
| 4244 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4245 | if (next != NULL) |
| 4246 | { |
| 4247 | *arg = next_line_from_context(cctx, TRUE); |
| 4248 | op = skipwhite(*arg); |
| 4249 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4250 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4251 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4252 | { |
| 4253 | char_u buf[3]; |
| 4254 | |
| 4255 | vim_strncpy(buf, op, oplen); |
| 4256 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4257 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4261 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4262 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4263 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4264 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4265 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4266 | return FAIL; |
| 4267 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4268 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4269 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4270 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4271 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4272 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4273 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4274 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4275 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4276 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4277 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4278 | // concat/subtract/add constant numbers |
| 4279 | if (*op == '+') |
| 4280 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4281 | else if (*op == '-') |
| 4282 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4283 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4284 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4285 | // concatenate constant strings |
| 4286 | char_u *s1 = tv1->vval.v_string; |
| 4287 | char_u *s2 = tv2->vval.v_string; |
| 4288 | size_t len1 = STRLEN(s1); |
| 4289 | |
| 4290 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4291 | if (tv1->vval.v_string == NULL) |
| 4292 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4293 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4294 | return FAIL; |
| 4295 | } |
| 4296 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4297 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4298 | vim_free(s1); |
| 4299 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4300 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4301 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4302 | } |
| 4303 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4304 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4305 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4306 | if (*op == '.') |
| 4307 | { |
| 4308 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4309 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4310 | return FAIL; |
| 4311 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4312 | } |
| 4313 | else |
| 4314 | generate_two_op(cctx, op); |
| 4315 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | return OK; |
| 4319 | } |
| 4320 | |
| 4321 | /* |
| 4322 | * expr5a == expr5b |
| 4323 | * expr5a =~ expr5b |
| 4324 | * expr5a != expr5b |
| 4325 | * expr5a !~ expr5b |
| 4326 | * expr5a > expr5b |
| 4327 | * expr5a >= expr5b |
| 4328 | * expr5a < expr5b |
| 4329 | * expr5a <= expr5b |
| 4330 | * expr5a is expr5b |
| 4331 | * expr5a isnot expr5b |
| 4332 | * |
| 4333 | * Produces instructions: |
| 4334 | * EVAL expr5a Push result of "expr5a" |
| 4335 | * EVAL expr5b Push result of "expr5b" |
| 4336 | * COMPARE one of the compare instructions |
| 4337 | */ |
| 4338 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4339 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4340 | { |
| 4341 | exptype_T type = EXPR_UNKNOWN; |
| 4342 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4343 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4344 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4345 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4346 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4347 | |
| 4348 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4349 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4350 | return FAIL; |
| 4351 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4352 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4353 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4354 | |
| 4355 | /* |
| 4356 | * If there is a comparative operator, use it. |
| 4357 | */ |
| 4358 | if (type != EXPR_UNKNOWN) |
| 4359 | { |
| 4360 | int ic = FALSE; // Default: do not ignore case |
| 4361 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4362 | if (next != NULL) |
| 4363 | { |
| 4364 | *arg = next_line_from_context(cctx, TRUE); |
| 4365 | p = skipwhite(*arg); |
| 4366 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4367 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4368 | { |
| 4369 | semsg(_(e_invexpr2), *arg); |
| 4370 | return FAIL; |
| 4371 | } |
| 4372 | // extra question mark appended: ignore case |
| 4373 | if (p[len] == '?') |
| 4374 | { |
| 4375 | ic = TRUE; |
| 4376 | ++len; |
| 4377 | } |
| 4378 | // extra '#' appended: match case (ignored) |
| 4379 | else if (p[len] == '#') |
| 4380 | ++len; |
| 4381 | // nothing appended: match case |
| 4382 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4383 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4384 | { |
| 4385 | char_u buf[7]; |
| 4386 | |
| 4387 | vim_strncpy(buf, p, len); |
| 4388 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4389 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | // get the second variable |
| 4393 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4394 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4395 | return FAIL; |
| 4396 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4397 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4398 | return FAIL; |
| 4399 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4400 | if (ppconst->pp_used == ppconst_used + 2) |
| 4401 | { |
| 4402 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4403 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4404 | int ret; |
| 4405 | |
| 4406 | // Both sides are a constant, compute the result now. |
| 4407 | // First check for a valid combination of types, this is more |
| 4408 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4409 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4410 | ret = FAIL; |
| 4411 | else |
| 4412 | { |
| 4413 | ret = typval_compare(tv1, tv2, type, ic); |
| 4414 | tv1->v_type = VAR_BOOL; |
| 4415 | tv1->vval.v_number = tv1->vval.v_number |
| 4416 | ? VVAL_TRUE : VVAL_FALSE; |
| 4417 | clear_tv(tv2); |
| 4418 | --ppconst->pp_used; |
| 4419 | } |
| 4420 | return ret; |
| 4421 | } |
| 4422 | |
| 4423 | generate_ppconst(cctx, ppconst); |
| 4424 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | return OK; |
| 4428 | } |
| 4429 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4430 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4431 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4432 | /* |
| 4433 | * Compile || or &&. |
| 4434 | */ |
| 4435 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4436 | compile_and_or( |
| 4437 | char_u **arg, |
| 4438 | cctx_T *cctx, |
| 4439 | char *op, |
| 4440 | ppconst_T *ppconst, |
| 4441 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4442 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4443 | char_u *next; |
| 4444 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4445 | int opchar = *op; |
| 4446 | |
| 4447 | if (p[0] == opchar && p[1] == opchar) |
| 4448 | { |
| 4449 | garray_T *instr = &cctx->ctx_instr; |
| 4450 | garray_T end_ga; |
| 4451 | |
| 4452 | /* |
| 4453 | * Repeat until there is no following "||" or "&&" |
| 4454 | */ |
| 4455 | ga_init2(&end_ga, sizeof(int), 10); |
| 4456 | while (p[0] == opchar && p[1] == opchar) |
| 4457 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4458 | if (next != NULL) |
| 4459 | { |
| 4460 | *arg = next_line_from_context(cctx, TRUE); |
| 4461 | p = skipwhite(*arg); |
| 4462 | } |
| 4463 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4464 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4465 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4466 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4467 | return FAIL; |
| 4468 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4469 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4470 | // TODO: use ppconst if the value is a constant |
| 4471 | generate_ppconst(cctx, ppconst); |
| 4472 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4473 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4474 | { |
| 4475 | ga_clear(&end_ga); |
| 4476 | return FAIL; |
| 4477 | } |
| 4478 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4479 | ++end_ga.ga_len; |
| 4480 | generate_JUMP(cctx, opchar == '|' |
| 4481 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 4482 | |
| 4483 | // eval the next expression |
| 4484 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4485 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4486 | return FAIL; |
| 4487 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4488 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4489 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4490 | { |
| 4491 | ga_clear(&end_ga); |
| 4492 | return FAIL; |
| 4493 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4494 | |
| 4495 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4496 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4497 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4498 | |
| 4499 | // Fill in the end label in all jumps. |
| 4500 | while (end_ga.ga_len > 0) |
| 4501 | { |
| 4502 | isn_T *isn; |
| 4503 | |
| 4504 | --end_ga.ga_len; |
| 4505 | isn = ((isn_T *)instr->ga_data) |
| 4506 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4507 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4508 | } |
| 4509 | ga_clear(&end_ga); |
| 4510 | } |
| 4511 | |
| 4512 | return OK; |
| 4513 | } |
| 4514 | |
| 4515 | /* |
| 4516 | * expr4a && expr4a && expr4a logical AND |
| 4517 | * |
| 4518 | * Produces instructions: |
| 4519 | * EVAL expr4a Push result of "expr4a" |
| 4520 | * JUMP_AND_KEEP_IF_FALSE end |
| 4521 | * EVAL expr4b Push result of "expr4b" |
| 4522 | * JUMP_AND_KEEP_IF_FALSE end |
| 4523 | * EVAL expr4c Push result of "expr4c" |
| 4524 | * end: |
| 4525 | */ |
| 4526 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4527 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4528 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4529 | int ppconst_used = ppconst->pp_used; |
| 4530 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4531 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4532 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4533 | return FAIL; |
| 4534 | |
| 4535 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4536 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4537 | } |
| 4538 | |
| 4539 | /* |
| 4540 | * expr3a || expr3b || expr3c logical OR |
| 4541 | * |
| 4542 | * Produces instructions: |
| 4543 | * EVAL expr3a Push result of "expr3a" |
| 4544 | * JUMP_AND_KEEP_IF_TRUE end |
| 4545 | * EVAL expr3b Push result of "expr3b" |
| 4546 | * JUMP_AND_KEEP_IF_TRUE end |
| 4547 | * EVAL expr3c Push result of "expr3c" |
| 4548 | * end: |
| 4549 | */ |
| 4550 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4551 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4552 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4553 | int ppconst_used = ppconst->pp_used; |
| 4554 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4555 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4556 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4557 | return FAIL; |
| 4558 | |
| 4559 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4560 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4561 | } |
| 4562 | |
| 4563 | /* |
| 4564 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4565 | * |
| 4566 | * Produces instructions: |
| 4567 | * EVAL expr2 Push result of "expr" |
| 4568 | * JUMP_IF_FALSE alt jump if false |
| 4569 | * EVAL expr1a |
| 4570 | * JUMP_ALWAYS end |
| 4571 | * alt: EVAL expr1b |
| 4572 | * end: |
| 4573 | */ |
| 4574 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4575 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4576 | { |
| 4577 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4578 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4579 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4580 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4581 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4582 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4583 | return FAIL; |
| 4584 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4585 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4586 | if (*p == '?') |
| 4587 | { |
| 4588 | garray_T *instr = &cctx->ctx_instr; |
| 4589 | garray_T *stack = &cctx->ctx_type_stack; |
| 4590 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4591 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4592 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4593 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4594 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4595 | int has_const_expr = FALSE; |
| 4596 | int const_value = FALSE; |
| 4597 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4598 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4599 | if (next != NULL) |
| 4600 | { |
| 4601 | *arg = next_line_from_context(cctx, TRUE); |
| 4602 | p = skipwhite(*arg); |
| 4603 | } |
| 4604 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4605 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4606 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4607 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4608 | return FAIL; |
| 4609 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4610 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4611 | if (ppconst->pp_used == ppconst_used + 1) |
| 4612 | { |
| 4613 | // the condition is a constant, we know whether the ? or the : |
| 4614 | // expression is to be evaluated. |
| 4615 | has_const_expr = TRUE; |
| 4616 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4617 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4618 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4619 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4620 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4621 | } |
| 4622 | else |
| 4623 | { |
| 4624 | generate_ppconst(cctx, ppconst); |
| 4625 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4626 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4627 | |
| 4628 | // evaluate the second expression; any type is accepted |
| 4629 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4630 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4631 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4632 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4633 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4634 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4635 | if (!has_const_expr) |
| 4636 | { |
| 4637 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4638 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4639 | // remember the type and drop it |
| 4640 | --stack->ga_len; |
| 4641 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4642 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4643 | end_idx = instr->ga_len; |
| 4644 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4645 | |
| 4646 | // jump here from JUMP_IF_FALSE |
| 4647 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4648 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4649 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4650 | |
| 4651 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4652 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4653 | if (*p != ':') |
| 4654 | { |
| 4655 | emsg(_(e_missing_colon)); |
| 4656 | return FAIL; |
| 4657 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4658 | if (next != NULL) |
| 4659 | { |
| 4660 | *arg = next_line_from_context(cctx, TRUE); |
| 4661 | p = skipwhite(*arg); |
| 4662 | } |
| 4663 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4664 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4665 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4666 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4667 | return FAIL; |
| 4668 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4669 | |
| 4670 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4671 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4672 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4673 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4674 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4675 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4676 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4677 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4678 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4679 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4680 | if (!has_const_expr) |
| 4681 | { |
| 4682 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4683 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4684 | // If the types differ, the result has a more generic type. |
| 4685 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4686 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4687 | |
| 4688 | // jump here from JUMP_ALWAYS |
| 4689 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4690 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4691 | } |
| 4692 | |
| 4693 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4694 | } |
| 4695 | return OK; |
| 4696 | } |
| 4697 | |
| 4698 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4699 | * Toplevel expression. |
| 4700 | */ |
| 4701 | static int |
| 4702 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4703 | { |
| 4704 | ppconst_T ppconst; |
| 4705 | |
| 4706 | CLEAR_FIELD(ppconst); |
| 4707 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4708 | { |
| 4709 | clear_ppconst(&ppconst); |
| 4710 | return FAIL; |
| 4711 | } |
| 4712 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4713 | return FAIL; |
| 4714 | return OK; |
| 4715 | } |
| 4716 | |
| 4717 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4718 | * compile "return [expr]" |
| 4719 | */ |
| 4720 | static char_u * |
| 4721 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4722 | { |
| 4723 | char_u *p = arg; |
| 4724 | garray_T *stack = &cctx->ctx_type_stack; |
| 4725 | type_T *stack_type; |
| 4726 | |
| 4727 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4728 | { |
| 4729 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4730 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4731 | return NULL; |
| 4732 | |
| 4733 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4734 | if (set_return_type) |
| 4735 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4736 | else |
| 4737 | { |
| 4738 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4739 | && stack_type->tt_type != VAR_VOID |
| 4740 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4741 | { |
| 4742 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4743 | return NULL; |
| 4744 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4745 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4746 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4747 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4748 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4749 | } |
| 4750 | else |
| 4751 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4752 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4753 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4754 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4755 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4756 | { |
| 4757 | emsg(_("E1003: Missing return value")); |
| 4758 | return NULL; |
| 4759 | } |
| 4760 | |
| 4761 | // No argument, return zero. |
| 4762 | generate_PUSHNR(cctx, 0); |
| 4763 | } |
| 4764 | |
| 4765 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4766 | return NULL; |
| 4767 | |
| 4768 | // "return val | endif" is possible |
| 4769 | return skipwhite(p); |
| 4770 | } |
| 4771 | |
| 4772 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4773 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4774 | * Return a pointer to the line in allocated memory. |
| 4775 | * Return NULL for end-of-file or some error. |
| 4776 | */ |
| 4777 | static char_u * |
| 4778 | exarg_getline( |
| 4779 | int c UNUSED, |
| 4780 | void *cookie, |
| 4781 | int indent UNUSED, |
| 4782 | int do_concat UNUSED) |
| 4783 | { |
| 4784 | cctx_T *cctx = (cctx_T *)cookie; |
| 4785 | |
| 4786 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4787 | { |
| 4788 | iemsg("Heredoc got to end"); |
| 4789 | return NULL; |
| 4790 | } |
| 4791 | ++cctx->ctx_lnum; |
| 4792 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4793 | [cctx->ctx_lnum]); |
| 4794 | } |
| 4795 | |
| 4796 | /* |
| 4797 | * Compile a nested :def command. |
| 4798 | */ |
| 4799 | static char_u * |
| 4800 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4801 | { |
| 4802 | char_u *name_start = eap->arg; |
| 4803 | char_u *name_end = to_name_end(eap->arg, FALSE); |
| 4804 | char_u *name = get_lambda_name(); |
| 4805 | lvar_T *lvar; |
| 4806 | ufunc_T *ufunc; |
| 4807 | |
| 4808 | eap->arg = name_end; |
| 4809 | eap->getline = exarg_getline; |
| 4810 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4811 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4812 | eap->forceit = FALSE; |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4813 | ufunc = def_function(eap, name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4814 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4815 | if (ufunc == NULL) |
| 4816 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4817 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4818 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4819 | return NULL; |
| 4820 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4821 | // Define a local variable for the function reference. |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4822 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4823 | TRUE, ufunc->uf_func_type); |
| 4824 | |
| 4825 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL |
| 4826 | || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL) |
| 4827 | return NULL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4828 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4829 | // TODO: warning for trailing text? |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4830 | return (char_u *)""; |
| 4831 | } |
| 4832 | |
| 4833 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4834 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4835 | */ |
| 4836 | int |
| 4837 | assignment_len(char_u *p, int *heredoc) |
| 4838 | { |
| 4839 | if (*p == '=') |
| 4840 | { |
| 4841 | if (p[1] == '<' && p[2] == '<') |
| 4842 | { |
| 4843 | *heredoc = TRUE; |
| 4844 | return 3; |
| 4845 | } |
| 4846 | return 1; |
| 4847 | } |
| 4848 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4849 | return 2; |
| 4850 | if (STRNCMP(p, "..=", 3) == 0) |
| 4851 | return 3; |
| 4852 | return 0; |
| 4853 | } |
| 4854 | |
| 4855 | // words that cannot be used as a variable |
| 4856 | static char *reserved[] = { |
| 4857 | "true", |
| 4858 | "false", |
| 4859 | NULL |
| 4860 | }; |
| 4861 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4862 | typedef enum { |
| 4863 | dest_local, |
| 4864 | dest_option, |
| 4865 | dest_env, |
| 4866 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4867 | dest_buffer, |
| 4868 | dest_window, |
| 4869 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4870 | dest_vimvar, |
| 4871 | dest_script, |
| 4872 | dest_reg, |
| 4873 | } assign_dest_T; |
| 4874 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4875 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4876 | * Generate the load instruction for "name". |
| 4877 | */ |
| 4878 | static void |
| 4879 | generate_loadvar( |
| 4880 | cctx_T *cctx, |
| 4881 | assign_dest_T dest, |
| 4882 | char_u *name, |
| 4883 | lvar_T *lvar, |
| 4884 | type_T *type) |
| 4885 | { |
| 4886 | switch (dest) |
| 4887 | { |
| 4888 | case dest_option: |
| 4889 | // TODO: check the option exists |
| 4890 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4891 | break; |
| 4892 | case dest_global: |
| 4893 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4894 | break; |
| 4895 | case dest_buffer: |
| 4896 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4897 | break; |
| 4898 | case dest_window: |
| 4899 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4900 | break; |
| 4901 | case dest_tab: |
| 4902 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4903 | break; |
| 4904 | case dest_script: |
| 4905 | compile_load_scriptvar(cctx, |
| 4906 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4907 | break; |
| 4908 | case dest_env: |
| 4909 | // Include $ in the name here |
| 4910 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4911 | break; |
| 4912 | case dest_reg: |
| 4913 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4914 | break; |
| 4915 | case dest_vimvar: |
| 4916 | generate_LOADV(cctx, name + 2, TRUE); |
| 4917 | break; |
| 4918 | case dest_local: |
| 4919 | if (lvar->lv_from_outer) |
| 4920 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4921 | NULL, type); |
| 4922 | else |
| 4923 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4924 | break; |
| 4925 | } |
| 4926 | } |
| 4927 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4928 | void |
| 4929 | vim9_declare_error(char_u *name) |
| 4930 | { |
| 4931 | char *scope = ""; |
| 4932 | |
| 4933 | switch (*name) |
| 4934 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4935 | case 'g': scope = _("global"); break; |
| 4936 | case 'b': scope = _("buffer"); break; |
| 4937 | case 'w': scope = _("window"); break; |
| 4938 | case 't': scope = _("tab"); break; |
| 4939 | case 'v': scope = "v:"; break; |
| 4940 | case '$': semsg(_(e_declare_env_var), name); return; |
| 4941 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4942 | } |
| 4943 | semsg(_(e_declare_var), scope, name); |
| 4944 | } |
| 4945 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4946 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4947 | * Compile declaration and assignment: |
| 4948 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4949 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4950 | * Return NULL for an error. |
| 4951 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4952 | */ |
| 4953 | static char_u * |
| 4954 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4955 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4956 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4957 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4958 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4959 | char_u *ret = NULL; |
| 4960 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4961 | int var_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4962 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4963 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4964 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4965 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4966 | int oplen = 0; |
| 4967 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4968 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4969 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4970 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4971 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4972 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4973 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4974 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 4975 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 4976 | if (p == NULL) |
| 4977 | return *arg == '[' ? arg : NULL; |
| 4978 | |
| 4979 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4980 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 4981 | // TODO: should we allow this, and figure out type inference from list |
| 4982 | // members? |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4983 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4984 | return NULL; |
| 4985 | } |
| 4986 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4987 | sp = p; |
| 4988 | p = skipwhite(p); |
| 4989 | op = p; |
| 4990 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4991 | |
| 4992 | if (var_count > 0 && oplen == 0) |
| 4993 | // can be something like "[1, 2]->func()" |
| 4994 | return arg; |
| 4995 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4996 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4997 | { |
| 4998 | char_u buf[4]; |
| 4999 | |
| 5000 | vim_strncpy(buf, op, oplen); |
| 5001 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5002 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 5003 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5004 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5005 | if (heredoc) |
| 5006 | { |
| 5007 | list_T *l; |
| 5008 | listitem_T *li; |
| 5009 | |
| 5010 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5011 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5012 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 5013 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5014 | |
| 5015 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 5016 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5017 | { |
| 5018 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 5019 | li->li_tv.vval.v_string = NULL; |
| 5020 | } |
| 5021 | generate_NEWLIST(cctx, l->lv_len); |
| 5022 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5023 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5024 | list_free(l); |
| 5025 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5026 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5027 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5028 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5029 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5030 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 5031 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5032 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5033 | p = skipwhite(op + oplen); |
| 5034 | if (compile_expr0(&p, cctx) == FAIL) |
| 5035 | return NULL; |
| 5036 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5037 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5038 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5039 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5040 | type_T *stacktype; |
| 5041 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5042 | stacktype = stack->ga_len == 0 ? &t_void |
| 5043 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5044 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5045 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5046 | emsg(_(e_cannot_use_void)); |
| 5047 | goto theend; |
| 5048 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5049 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5050 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5051 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5052 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 5053 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5054 | } |
| 5055 | } |
| 5056 | |
| 5057 | /* |
| 5058 | * Loop over variables in "[var, var] = expr". |
| 5059 | * For "var = expr" and "let var: type" this is done only once. |
| 5060 | */ |
| 5061 | if (var_count > 0) |
| 5062 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 5063 | else |
| 5064 | var_start = arg; |
| 5065 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 5066 | { |
| 5067 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 5068 | size_t varlen; |
| 5069 | int new_local = FALSE; |
| 5070 | int opt_type; |
| 5071 | int opt_flags = 0; |
| 5072 | assign_dest_T dest = dest_local; |
| 5073 | int vimvaridx = -1; |
| 5074 | lvar_T *lvar = NULL; |
| 5075 | lvar_T arg_lvar; |
| 5076 | int has_type = FALSE; |
| 5077 | int has_index = FALSE; |
| 5078 | int instr_count = -1; |
| 5079 | |
| 5080 | p = (*var_start == '&' || *var_start == '$' |
| 5081 | || *var_start == '@') ? var_start + 1 : var_start; |
| 5082 | p = to_name_end(p, TRUE); |
| 5083 | |
| 5084 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 5085 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 5086 | --var_end; |
| 5087 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 5088 | --p; |
| 5089 | |
| 5090 | varlen = p - var_start; |
| 5091 | vim_free(name); |
| 5092 | name = vim_strnsave(var_start, varlen); |
| 5093 | if (name == NULL) |
| 5094 | return NULL; |
| 5095 | if (!heredoc) |
| 5096 | type = &t_any; |
| 5097 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5098 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5099 | { |
| 5100 | if (*var_start == '&') |
| 5101 | { |
| 5102 | int cc; |
| 5103 | long numval; |
| 5104 | |
| 5105 | dest = dest_option; |
| 5106 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5107 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5108 | emsg(_(e_const_option)); |
| 5109 | goto theend; |
| 5110 | } |
| 5111 | if (is_decl) |
| 5112 | { |
| 5113 | semsg(_("E1052: Cannot declare an option: %s"), var_start); |
| 5114 | goto theend; |
| 5115 | } |
| 5116 | p = var_start; |
| 5117 | p = find_option_end(&p, &opt_flags); |
| 5118 | if (p == NULL) |
| 5119 | { |
| 5120 | // cannot happen? |
| 5121 | emsg(_(e_letunexp)); |
| 5122 | goto theend; |
| 5123 | } |
| 5124 | cc = *p; |
| 5125 | *p = NUL; |
| 5126 | opt_type = get_option_value(var_start + 1, &numval, |
| 5127 | NULL, opt_flags); |
| 5128 | *p = cc; |
| 5129 | if (opt_type == -3) |
| 5130 | { |
| 5131 | semsg(_(e_unknown_option), var_start); |
| 5132 | goto theend; |
| 5133 | } |
| 5134 | if (opt_type == -2 || opt_type == 0) |
| 5135 | type = &t_string; |
| 5136 | else |
| 5137 | type = &t_number; // both number and boolean option |
| 5138 | } |
| 5139 | else if (*var_start == '$') |
| 5140 | { |
| 5141 | dest = dest_env; |
| 5142 | type = &t_string; |
| 5143 | if (is_decl) |
| 5144 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5145 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5146 | goto theend; |
| 5147 | } |
| 5148 | } |
| 5149 | else if (*var_start == '@') |
| 5150 | { |
| 5151 | if (!valid_yank_reg(var_start[1], TRUE)) |
| 5152 | { |
| 5153 | emsg_invreg(var_start[1]); |
| 5154 | goto theend; |
| 5155 | } |
| 5156 | dest = dest_reg; |
| 5157 | type = &t_string; |
| 5158 | if (is_decl) |
| 5159 | { |
| 5160 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 5161 | goto theend; |
| 5162 | } |
| 5163 | } |
| 5164 | else if (STRNCMP(var_start, "g:", 2) == 0) |
| 5165 | { |
| 5166 | dest = dest_global; |
| 5167 | if (is_decl) |
| 5168 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5169 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5170 | goto theend; |
| 5171 | } |
| 5172 | } |
| 5173 | else if (STRNCMP(var_start, "b:", 2) == 0) |
| 5174 | { |
| 5175 | dest = dest_buffer; |
| 5176 | if (is_decl) |
| 5177 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5178 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5179 | goto theend; |
| 5180 | } |
| 5181 | } |
| 5182 | else if (STRNCMP(var_start, "w:", 2) == 0) |
| 5183 | { |
| 5184 | dest = dest_window; |
| 5185 | if (is_decl) |
| 5186 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5187 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5188 | goto theend; |
| 5189 | } |
| 5190 | } |
| 5191 | else if (STRNCMP(var_start, "t:", 2) == 0) |
| 5192 | { |
| 5193 | dest = dest_tab; |
| 5194 | if (is_decl) |
| 5195 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5196 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5197 | goto theend; |
| 5198 | } |
| 5199 | } |
| 5200 | else if (STRNCMP(var_start, "v:", 2) == 0) |
| 5201 | { |
| 5202 | typval_T *vtv; |
| 5203 | int di_flags; |
| 5204 | |
| 5205 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5206 | if (vimvaridx < 0) |
| 5207 | { |
| 5208 | semsg(_(e_var_notfound), var_start); |
| 5209 | goto theend; |
| 5210 | } |
| 5211 | // We use the current value of "sandbox" here, is that OK? |
| 5212 | if (var_check_ro(di_flags, name, FALSE)) |
| 5213 | goto theend; |
| 5214 | dest = dest_vimvar; |
| 5215 | vtv = get_vim_var_tv(vimvaridx); |
| 5216 | type = typval2type(vtv); |
| 5217 | if (is_decl) |
| 5218 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5219 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5220 | goto theend; |
| 5221 | } |
| 5222 | } |
| 5223 | else |
| 5224 | { |
| 5225 | int idx; |
| 5226 | |
| 5227 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5228 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5229 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5230 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5231 | goto theend; |
| 5232 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5233 | |
| 5234 | lvar = lookup_local(var_start, varlen, cctx); |
| 5235 | if (lvar == NULL) |
| 5236 | { |
| 5237 | CLEAR_FIELD(arg_lvar); |
| 5238 | if (lookup_arg(var_start, varlen, |
| 5239 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 5240 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5241 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5242 | if (is_decl) |
| 5243 | { |
| 5244 | semsg(_(e_used_as_arg), name); |
| 5245 | goto theend; |
| 5246 | } |
| 5247 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5248 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5249 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5250 | if (lvar != NULL) |
| 5251 | { |
| 5252 | if (is_decl) |
| 5253 | { |
| 5254 | semsg(_("E1017: Variable already declared: %s"), name); |
| 5255 | goto theend; |
| 5256 | } |
| 5257 | else if (lvar->lv_const) |
| 5258 | { |
| 5259 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 5260 | name); |
| 5261 | goto theend; |
| 5262 | } |
| 5263 | } |
| 5264 | else if (STRNCMP(var_start, "s:", 2) == 0 |
| 5265 | || lookup_script(var_start, varlen) == OK |
| 5266 | || find_imported(var_start, varlen, cctx) != NULL) |
| 5267 | { |
| 5268 | dest = dest_script; |
| 5269 | if (is_decl) |
| 5270 | { |
| 5271 | semsg(_("E1054: Variable already declared in the script: %s"), |
| 5272 | name); |
| 5273 | goto theend; |
| 5274 | } |
| 5275 | } |
| 5276 | else if (name[1] == ':' && name[2] != NUL) |
| 5277 | { |
| 5278 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
| 5279 | name); |
| 5280 | goto theend; |
| 5281 | } |
| 5282 | else if (!is_decl) |
| 5283 | { |
| 5284 | semsg(_("E1089: unknown variable: %s"), name); |
| 5285 | goto theend; |
| 5286 | } |
| 5287 | } |
| 5288 | } |
| 5289 | |
| 5290 | // handle "a:name" as a name, not index "name" on "a" |
| 5291 | if (varlen > 1 || var_start[varlen] != ':') |
| 5292 | p = var_end; |
| 5293 | |
| 5294 | if (dest != dest_option) |
| 5295 | { |
| 5296 | if (is_decl && *p == ':') |
| 5297 | { |
| 5298 | // parse optional type: "let var: type = expr" |
| 5299 | if (!VIM_ISWHITE(p[1])) |
| 5300 | { |
| 5301 | semsg(_(e_white_after), ":"); |
| 5302 | goto theend; |
| 5303 | } |
| 5304 | p = skipwhite(p + 1); |
| 5305 | type = parse_type(&p, cctx->ctx_type_list); |
| 5306 | has_type = TRUE; |
| 5307 | } |
| 5308 | else if (lvar != NULL) |
| 5309 | type = lvar->lv_type; |
| 5310 | } |
| 5311 | |
| 5312 | if (oplen == 3 && !heredoc && dest != dest_global |
| 5313 | && type->tt_type != VAR_STRING |
| 5314 | && type->tt_type != VAR_ANY) |
| 5315 | { |
| 5316 | emsg(_("E1019: Can only concatenate to string")); |
| 5317 | goto theend; |
| 5318 | } |
| 5319 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5320 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5321 | { |
| 5322 | if (oplen > 1 && !heredoc) |
| 5323 | { |
| 5324 | // +=, /=, etc. require an existing variable |
| 5325 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 5326 | name); |
| 5327 | goto theend; |
| 5328 | } |
| 5329 | |
| 5330 | // new local variable |
| 5331 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
| 5332 | goto theend; |
| 5333 | lvar = reserve_local(cctx, var_start, varlen, |
| 5334 | cmdidx == CMD_const, type); |
| 5335 | if (lvar == NULL) |
| 5336 | goto theend; |
| 5337 | new_local = TRUE; |
| 5338 | } |
| 5339 | |
| 5340 | member_type = type; |
| 5341 | if (var_end > var_start + varlen) |
| 5342 | { |
| 5343 | // Something follows after the variable: "var[idx]". |
| 5344 | if (is_decl) |
| 5345 | { |
| 5346 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 5347 | goto theend; |
| 5348 | } |
| 5349 | |
| 5350 | if (var_start[varlen] == '[') |
| 5351 | { |
| 5352 | has_index = TRUE; |
| 5353 | if (type->tt_member == NULL) |
| 5354 | { |
| 5355 | semsg(_("E1088: cannot use an index on %s"), name); |
| 5356 | goto theend; |
| 5357 | } |
| 5358 | member_type = type->tt_member; |
| 5359 | } |
| 5360 | else |
| 5361 | { |
| 5362 | semsg("Not supported yet: %s", var_start); |
| 5363 | goto theend; |
| 5364 | } |
| 5365 | } |
| 5366 | else if (lvar == &arg_lvar) |
| 5367 | { |
| 5368 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 5369 | goto theend; |
| 5370 | } |
| 5371 | |
| 5372 | if (!heredoc) |
| 5373 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5374 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5375 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5376 | if (oplen > 0 && var_count == 0) |
| 5377 | { |
| 5378 | // skip over the "=" and the expression |
| 5379 | p = skipwhite(op + oplen); |
| 5380 | compile_expr0(&p, cctx); |
| 5381 | } |
| 5382 | } |
| 5383 | else if (oplen > 0) |
| 5384 | { |
| 5385 | type_T *stacktype; |
| 5386 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5387 | // For "var = expr" evaluate the expression. |
| 5388 | if (var_count == 0) |
| 5389 | { |
| 5390 | int r; |
| 5391 | |
| 5392 | // for "+=", "*=", "..=" etc. first load the current value |
| 5393 | if (*op != '=') |
| 5394 | { |
| 5395 | generate_loadvar(cctx, dest, name, lvar, type); |
| 5396 | |
| 5397 | if (has_index) |
| 5398 | { |
| 5399 | // TODO: get member from list or dict |
| 5400 | emsg("Index with operation not supported yet"); |
| 5401 | goto theend; |
| 5402 | } |
| 5403 | } |
| 5404 | |
| 5405 | // Compile the expression. Temporarily hide the new local |
| 5406 | // variable here, it is not available to this expression. |
| 5407 | if (new_local) |
| 5408 | --cctx->ctx_locals.ga_len; |
| 5409 | instr_count = instr->ga_len; |
| 5410 | p = skipwhite(op + oplen); |
| 5411 | r = compile_expr0(&p, cctx); |
| 5412 | if (new_local) |
| 5413 | ++cctx->ctx_locals.ga_len; |
| 5414 | if (r == FAIL) |
| 5415 | goto theend; |
| 5416 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5417 | else if (semicolon && var_idx == var_count - 1) |
| 5418 | { |
| 5419 | // For "[var; var] = expr" get the rest of the list |
| 5420 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5421 | goto theend; |
| 5422 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5423 | else |
| 5424 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5425 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5426 | // list. |
| 5427 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 5428 | return FAIL; |
| 5429 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5430 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5431 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5432 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5433 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5434 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5435 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5436 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5437 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5438 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5439 | emsg(_(e_cannot_use_void)); |
| 5440 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5441 | } |
| 5442 | else |
| 5443 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5444 | // An empty list or dict has a &t_void member, |
| 5445 | // for a variable that implies &t_any. |
| 5446 | if (stacktype == &t_list_empty) |
| 5447 | lvar->lv_type = &t_list_any; |
| 5448 | else if (stacktype == &t_dict_empty) |
| 5449 | lvar->lv_type = &t_dict_any; |
| 5450 | else |
| 5451 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5452 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5453 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5454 | else |
| 5455 | { |
| 5456 | type_T *use_type = lvar->lv_type; |
| 5457 | |
| 5458 | if (has_index) |
| 5459 | { |
| 5460 | use_type = use_type->tt_member; |
| 5461 | if (use_type == NULL) |
| 5462 | use_type = &t_void; |
| 5463 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5464 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5465 | == FAIL) |
| 5466 | goto theend; |
| 5467 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5468 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5469 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5470 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5471 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5472 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5473 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5474 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5475 | emsg(_(e_const_req_value)); |
| 5476 | goto theend; |
| 5477 | } |
| 5478 | else if (!has_type || dest == dest_option) |
| 5479 | { |
| 5480 | emsg(_(e_type_req)); |
| 5481 | goto theend; |
| 5482 | } |
| 5483 | else |
| 5484 | { |
| 5485 | // variables are always initialized |
| 5486 | if (ga_grow(instr, 1) == FAIL) |
| 5487 | goto theend; |
| 5488 | switch (member_type->tt_type) |
| 5489 | { |
| 5490 | case VAR_BOOL: |
| 5491 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5492 | break; |
| 5493 | case VAR_FLOAT: |
| 5494 | #ifdef FEAT_FLOAT |
| 5495 | generate_PUSHF(cctx, 0.0); |
| 5496 | #endif |
| 5497 | break; |
| 5498 | case VAR_STRING: |
| 5499 | generate_PUSHS(cctx, NULL); |
| 5500 | break; |
| 5501 | case VAR_BLOB: |
| 5502 | generate_PUSHBLOB(cctx, NULL); |
| 5503 | break; |
| 5504 | case VAR_FUNC: |
| 5505 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5506 | break; |
| 5507 | case VAR_LIST: |
| 5508 | generate_NEWLIST(cctx, 0); |
| 5509 | break; |
| 5510 | case VAR_DICT: |
| 5511 | generate_NEWDICT(cctx, 0); |
| 5512 | break; |
| 5513 | case VAR_JOB: |
| 5514 | generate_PUSHJOB(cctx, NULL); |
| 5515 | break; |
| 5516 | case VAR_CHANNEL: |
| 5517 | generate_PUSHCHANNEL(cctx, NULL); |
| 5518 | break; |
| 5519 | case VAR_NUMBER: |
| 5520 | case VAR_UNKNOWN: |
| 5521 | case VAR_ANY: |
| 5522 | case VAR_PARTIAL: |
| 5523 | case VAR_VOID: |
| 5524 | case VAR_SPECIAL: // cannot happen |
| 5525 | generate_PUSHNR(cctx, 0); |
| 5526 | break; |
| 5527 | } |
| 5528 | } |
| 5529 | if (var_count == 0) |
| 5530 | end = p; |
| 5531 | } |
| 5532 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5533 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5534 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5535 | break; |
| 5536 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5537 | if (oplen > 0 && *op != '=') |
| 5538 | { |
| 5539 | type_T *expected = &t_number; |
| 5540 | type_T *stacktype; |
| 5541 | |
| 5542 | // TODO: if type is known use float or any operation |
| 5543 | |
| 5544 | if (*op == '.') |
| 5545 | expected = &t_string; |
| 5546 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5547 | if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5548 | goto theend; |
| 5549 | |
| 5550 | if (*op == '.') |
| 5551 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 5552 | else |
| 5553 | { |
| 5554 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 5555 | |
| 5556 | if (isn == NULL) |
| 5557 | goto theend; |
| 5558 | switch (*op) |
| 5559 | { |
| 5560 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 5561 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 5562 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 5563 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 5564 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 5565 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5566 | } |
| 5567 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5568 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5569 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5570 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5571 | int r; |
| 5572 | |
| 5573 | // Compile the "idx" in "var[idx]". |
| 5574 | if (new_local) |
| 5575 | --cctx->ctx_locals.ga_len; |
| 5576 | p = skipwhite(var_start + varlen + 1); |
| 5577 | r = compile_expr0(&p, cctx); |
| 5578 | if (new_local) |
| 5579 | ++cctx->ctx_locals.ga_len; |
| 5580 | if (r == FAIL) |
| 5581 | goto theend; |
| 5582 | if (*skipwhite(p) != ']') |
| 5583 | { |
| 5584 | emsg(_(e_missbrac)); |
| 5585 | goto theend; |
| 5586 | } |
| 5587 | if (type->tt_type == VAR_DICT |
| 5588 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5589 | goto theend; |
| 5590 | if (type->tt_type == VAR_LIST |
| 5591 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5592 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5593 | { |
| 5594 | emsg(_(e_number_exp)); |
| 5595 | goto theend; |
| 5596 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5597 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5598 | // Load the dict or list. On the stack we then have: |
| 5599 | // - value |
| 5600 | // - index |
| 5601 | // - variable |
| 5602 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5603 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5604 | if (type->tt_type == VAR_LIST) |
| 5605 | { |
| 5606 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5607 | return FAIL; |
| 5608 | } |
| 5609 | else if (type->tt_type == VAR_DICT) |
| 5610 | { |
| 5611 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5612 | return FAIL; |
| 5613 | } |
| 5614 | else |
| 5615 | { |
| 5616 | emsg(_(e_listreq)); |
| 5617 | goto theend; |
| 5618 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5619 | } |
| 5620 | else |
| 5621 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5622 | switch (dest) |
| 5623 | { |
| 5624 | case dest_option: |
| 5625 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5626 | break; |
| 5627 | case dest_global: |
| 5628 | // include g: with the name, easier to execute that way |
| 5629 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5630 | break; |
| 5631 | case dest_buffer: |
| 5632 | // include b: with the name, easier to execute that way |
| 5633 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5634 | break; |
| 5635 | case dest_window: |
| 5636 | // include w: with the name, easier to execute that way |
| 5637 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5638 | break; |
| 5639 | case dest_tab: |
| 5640 | // include t: with the name, easier to execute that way |
| 5641 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5642 | break; |
| 5643 | case dest_env: |
| 5644 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5645 | break; |
| 5646 | case dest_reg: |
| 5647 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5648 | break; |
| 5649 | case dest_vimvar: |
| 5650 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5651 | break; |
| 5652 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5653 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5654 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5655 | imported_T *import = NULL; |
| 5656 | int sid = current_sctx.sc_sid; |
| 5657 | int idx; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5658 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5659 | if (name[1] != ':') |
| 5660 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5661 | import = find_imported(name, 0, cctx); |
| 5662 | if (import != NULL) |
| 5663 | sid = import->imp_sid; |
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 | |
| 5666 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 5667 | // TODO: specific type |
| 5668 | if (idx < 0) |
| 5669 | { |
| 5670 | char_u *name_s = name; |
| 5671 | |
| 5672 | // Include s: in the name for store_var() |
| 5673 | if (name[1] != ':') |
| 5674 | { |
| 5675 | int len = (int)STRLEN(name) + 3; |
| 5676 | |
| 5677 | name_s = alloc(len); |
| 5678 | if (name_s == NULL) |
| 5679 | name_s = name; |
| 5680 | else |
| 5681 | vim_snprintf((char *)name_s, len, |
| 5682 | "s:%s", name); |
| 5683 | } |
| 5684 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5685 | &t_any); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5686 | if (name_s != name) |
| 5687 | vim_free(name_s); |
| 5688 | } |
| 5689 | else |
| 5690 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5691 | sid, idx, &t_any); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5692 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5693 | break; |
| 5694 | case dest_local: |
| 5695 | if (lvar != NULL) |
| 5696 | { |
| 5697 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5698 | + instr->ga_len - 1; |
| 5699 | |
| 5700 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5701 | // ISN_STORE into ISN_STORENR |
| 5702 | if (!lvar->lv_from_outer |
| 5703 | && instr->ga_len == instr_count + 1 |
| 5704 | && isn->isn_type == ISN_PUSHNR) |
| 5705 | { |
| 5706 | varnumber_T val = isn->isn_arg.number; |
| 5707 | |
| 5708 | isn->isn_type = ISN_STORENR; |
| 5709 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5710 | isn->isn_arg.storenr.stnr_val = val; |
| 5711 | if (stack->ga_len > 0) |
| 5712 | --stack->ga_len; |
| 5713 | } |
| 5714 | else if (lvar->lv_from_outer) |
| 5715 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5716 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5717 | else |
| 5718 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5719 | } |
| 5720 | break; |
| 5721 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5722 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5723 | |
| 5724 | if (var_idx + 1 < var_count) |
| 5725 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5726 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5727 | |
| 5728 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5729 | if (var_count > 0 && !semicolon) |
| 5730 | { |
| 5731 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5732 | goto theend; |
| 5733 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5734 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5735 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5736 | |
| 5737 | theend: |
| 5738 | vim_free(name); |
| 5739 | return ret; |
| 5740 | } |
| 5741 | |
| 5742 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5743 | * Check if "name" can be "unlet". |
| 5744 | */ |
| 5745 | int |
| 5746 | check_vim9_unlet(char_u *name) |
| 5747 | { |
| 5748 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5749 | { |
| 5750 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5751 | return FAIL; |
| 5752 | } |
| 5753 | return OK; |
| 5754 | } |
| 5755 | |
| 5756 | /* |
| 5757 | * Callback passed to ex_unletlock(). |
| 5758 | */ |
| 5759 | static int |
| 5760 | compile_unlet( |
| 5761 | lval_T *lvp, |
| 5762 | char_u *name_end, |
| 5763 | exarg_T *eap, |
| 5764 | int deep UNUSED, |
| 5765 | void *coookie) |
| 5766 | { |
| 5767 | cctx_T *cctx = coookie; |
| 5768 | |
| 5769 | if (lvp->ll_tv == NULL) |
| 5770 | { |
| 5771 | char_u *p = lvp->ll_name; |
| 5772 | int cc = *name_end; |
| 5773 | int ret = OK; |
| 5774 | |
| 5775 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5776 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5777 | if (*p == '$') |
| 5778 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5779 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5780 | ret = FAIL; |
| 5781 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5782 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5783 | |
| 5784 | *name_end = cc; |
| 5785 | return ret; |
| 5786 | } |
| 5787 | |
| 5788 | // TODO: unlet {list}[idx] |
| 5789 | // TODO: unlet {dict}[key] |
| 5790 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5791 | return FAIL; |
| 5792 | } |
| 5793 | |
| 5794 | /* |
| 5795 | * compile "unlet var", "lock var" and "unlock var" |
| 5796 | * "arg" points to "var". |
| 5797 | */ |
| 5798 | static char_u * |
| 5799 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5800 | { |
| 5801 | char_u *p = arg; |
| 5802 | |
| 5803 | if (eap->cmdidx != CMD_unlet) |
| 5804 | { |
| 5805 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5806 | return NULL; |
| 5807 | } |
| 5808 | |
| 5809 | if (*p == '!') |
| 5810 | { |
| 5811 | p = skipwhite(p + 1); |
| 5812 | eap->forceit = TRUE; |
| 5813 | } |
| 5814 | |
| 5815 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5816 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5817 | } |
| 5818 | |
| 5819 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5820 | * Compile an :import command. |
| 5821 | */ |
| 5822 | static char_u * |
| 5823 | compile_import(char_u *arg, cctx_T *cctx) |
| 5824 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5825 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5826 | } |
| 5827 | |
| 5828 | /* |
| 5829 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5830 | */ |
| 5831 | static int |
| 5832 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5833 | { |
| 5834 | garray_T *instr = &cctx->ctx_instr; |
| 5835 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5836 | |
| 5837 | if (endlabel == NULL) |
| 5838 | return FAIL; |
| 5839 | endlabel->el_next = *el; |
| 5840 | *el = endlabel; |
| 5841 | endlabel->el_end_label = instr->ga_len; |
| 5842 | |
| 5843 | generate_JUMP(cctx, when, 0); |
| 5844 | return OK; |
| 5845 | } |
| 5846 | |
| 5847 | static void |
| 5848 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5849 | { |
| 5850 | garray_T *instr = &cctx->ctx_instr; |
| 5851 | |
| 5852 | while (*el != NULL) |
| 5853 | { |
| 5854 | endlabel_T *cur = (*el); |
| 5855 | isn_T *isn; |
| 5856 | |
| 5857 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5858 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5859 | *el = cur->el_next; |
| 5860 | vim_free(cur); |
| 5861 | } |
| 5862 | } |
| 5863 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5864 | static void |
| 5865 | compile_free_jump_to_end(endlabel_T **el) |
| 5866 | { |
| 5867 | while (*el != NULL) |
| 5868 | { |
| 5869 | endlabel_T *cur = (*el); |
| 5870 | |
| 5871 | *el = cur->el_next; |
| 5872 | vim_free(cur); |
| 5873 | } |
| 5874 | } |
| 5875 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5876 | /* |
| 5877 | * Create a new scope and set up the generic items. |
| 5878 | */ |
| 5879 | static scope_T * |
| 5880 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5881 | { |
| 5882 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5883 | |
| 5884 | if (scope == NULL) |
| 5885 | return NULL; |
| 5886 | scope->se_outer = cctx->ctx_scope; |
| 5887 | cctx->ctx_scope = scope; |
| 5888 | scope->se_type = type; |
| 5889 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5890 | return scope; |
| 5891 | } |
| 5892 | |
| 5893 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5894 | * Free the current scope and go back to the outer scope. |
| 5895 | */ |
| 5896 | static void |
| 5897 | drop_scope(cctx_T *cctx) |
| 5898 | { |
| 5899 | scope_T *scope = cctx->ctx_scope; |
| 5900 | |
| 5901 | if (scope == NULL) |
| 5902 | { |
| 5903 | iemsg("calling drop_scope() without a scope"); |
| 5904 | return; |
| 5905 | } |
| 5906 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5907 | switch (scope->se_type) |
| 5908 | { |
| 5909 | case IF_SCOPE: |
| 5910 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5911 | case FOR_SCOPE: |
| 5912 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5913 | case WHILE_SCOPE: |
| 5914 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5915 | case TRY_SCOPE: |
| 5916 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5917 | case NO_SCOPE: |
| 5918 | case BLOCK_SCOPE: |
| 5919 | break; |
| 5920 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5921 | vim_free(scope); |
| 5922 | } |
| 5923 | |
| 5924 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5925 | * compile "if expr" |
| 5926 | * |
| 5927 | * "if expr" Produces instructions: |
| 5928 | * EVAL expr Push result of "expr" |
| 5929 | * JUMP_IF_FALSE end |
| 5930 | * ... body ... |
| 5931 | * end: |
| 5932 | * |
| 5933 | * "if expr | else" Produces instructions: |
| 5934 | * EVAL expr Push result of "expr" |
| 5935 | * JUMP_IF_FALSE else |
| 5936 | * ... body ... |
| 5937 | * JUMP_ALWAYS end |
| 5938 | * else: |
| 5939 | * ... body ... |
| 5940 | * end: |
| 5941 | * |
| 5942 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5943 | * EVAL expr Push result of "expr" |
| 5944 | * JUMP_IF_FALSE elseif |
| 5945 | * ... body ... |
| 5946 | * JUMP_ALWAYS end |
| 5947 | * elseif: |
| 5948 | * EVAL expr Push result of "expr" |
| 5949 | * JUMP_IF_FALSE else |
| 5950 | * ... body ... |
| 5951 | * JUMP_ALWAYS end |
| 5952 | * else: |
| 5953 | * ... body ... |
| 5954 | * end: |
| 5955 | */ |
| 5956 | static char_u * |
| 5957 | compile_if(char_u *arg, cctx_T *cctx) |
| 5958 | { |
| 5959 | char_u *p = arg; |
| 5960 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5961 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5962 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5963 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5964 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5965 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5966 | CLEAR_FIELD(ppconst); |
| 5967 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5968 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5969 | clear_ppconst(&ppconst); |
| 5970 | return NULL; |
| 5971 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5972 | if (cctx->ctx_skip == SKIP_YES) |
| 5973 | clear_ppconst(&ppconst); |
| 5974 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5975 | { |
| 5976 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5977 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5978 | clear_ppconst(&ppconst); |
| 5979 | } |
| 5980 | else |
| 5981 | { |
| 5982 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5983 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5984 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5985 | return NULL; |
| 5986 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5987 | |
| 5988 | scope = new_scope(cctx, IF_SCOPE); |
| 5989 | if (scope == NULL) |
| 5990 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5991 | scope->se_skip_save = skip_save; |
| 5992 | // "is_had_return" will be reset if any block does not end in :return |
| 5993 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5994 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5995 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5996 | { |
| 5997 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5998 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5999 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6000 | } |
| 6001 | else |
| 6002 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6003 | |
| 6004 | return p; |
| 6005 | } |
| 6006 | |
| 6007 | static char_u * |
| 6008 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 6009 | { |
| 6010 | char_u *p = arg; |
| 6011 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6012 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6013 | isn_T *isn; |
| 6014 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6015 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6016 | |
| 6017 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6018 | { |
| 6019 | emsg(_(e_elseif_without_if)); |
| 6020 | return NULL; |
| 6021 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6022 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6023 | if (!cctx->ctx_had_return) |
| 6024 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6025 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6026 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6027 | { |
| 6028 | 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] | 6029 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6030 | return NULL; |
| 6031 | // previous "if" or "elseif" jumps here |
| 6032 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6033 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6034 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6035 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6036 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6037 | CLEAR_FIELD(ppconst); |
| 6038 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6039 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6040 | clear_ppconst(&ppconst); |
| 6041 | return NULL; |
| 6042 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6043 | if (scope->se_skip_save == SKIP_YES) |
| 6044 | clear_ppconst(&ppconst); |
| 6045 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6046 | { |
| 6047 | // The expression results in a constant. |
| 6048 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6049 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6050 | clear_ppconst(&ppconst); |
| 6051 | scope->se_u.se_if.is_if_label = -1; |
| 6052 | } |
| 6053 | else |
| 6054 | { |
| 6055 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6056 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6057 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6058 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6059 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6060 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6061 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6062 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6063 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6064 | |
| 6065 | return p; |
| 6066 | } |
| 6067 | |
| 6068 | static char_u * |
| 6069 | compile_else(char_u *arg, cctx_T *cctx) |
| 6070 | { |
| 6071 | char_u *p = arg; |
| 6072 | garray_T *instr = &cctx->ctx_instr; |
| 6073 | isn_T *isn; |
| 6074 | scope_T *scope = cctx->ctx_scope; |
| 6075 | |
| 6076 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6077 | { |
| 6078 | emsg(_(e_else_without_if)); |
| 6079 | return NULL; |
| 6080 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6081 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6082 | if (!cctx->ctx_had_return) |
| 6083 | scope->se_u.se_if.is_had_return = FALSE; |
| 6084 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6085 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6086 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6087 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6088 | // 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] | 6089 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6090 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6091 | if (!cctx->ctx_had_return |
| 6092 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6093 | JUMP_ALWAYS, cctx) == FAIL) |
| 6094 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6095 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6096 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6097 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6098 | { |
| 6099 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6100 | { |
| 6101 | // previous "if" or "elseif" jumps here |
| 6102 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6103 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6104 | scope->se_u.se_if.is_if_label = -1; |
| 6105 | } |
| 6106 | } |
| 6107 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6108 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6109 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6110 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6111 | |
| 6112 | return p; |
| 6113 | } |
| 6114 | |
| 6115 | static char_u * |
| 6116 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6117 | { |
| 6118 | scope_T *scope = cctx->ctx_scope; |
| 6119 | ifscope_T *ifscope; |
| 6120 | garray_T *instr = &cctx->ctx_instr; |
| 6121 | isn_T *isn; |
| 6122 | |
| 6123 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6124 | { |
| 6125 | emsg(_(e_endif_without_if)); |
| 6126 | return NULL; |
| 6127 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6128 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6129 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6130 | if (!cctx->ctx_had_return) |
| 6131 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6132 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6133 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6134 | { |
| 6135 | // previous "if" or "elseif" jumps here |
| 6136 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6137 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6138 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6139 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6140 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6141 | cctx->ctx_skip = scope->se_skip_save; |
| 6142 | |
| 6143 | // If all the blocks end in :return and there is an :else then the |
| 6144 | // had_return flag is set. |
| 6145 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6146 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6147 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6148 | return arg; |
| 6149 | } |
| 6150 | |
| 6151 | /* |
| 6152 | * compile "for var in expr" |
| 6153 | * |
| 6154 | * Produces instructions: |
| 6155 | * PUSHNR -1 |
| 6156 | * STORE loop-idx Set index to -1 |
| 6157 | * EVAL expr Push result of "expr" |
| 6158 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6159 | * - if beyond end, jump to "end" |
| 6160 | * - otherwise get item from list and push it |
| 6161 | * STORE var Store item in "var" |
| 6162 | * ... body ... |
| 6163 | * JUMP top Jump back to repeat |
| 6164 | * end: DROP Drop the result of "expr" |
| 6165 | * |
| 6166 | */ |
| 6167 | static char_u * |
| 6168 | compile_for(char_u *arg, cctx_T *cctx) |
| 6169 | { |
| 6170 | char_u *p; |
| 6171 | size_t varlen; |
| 6172 | garray_T *instr = &cctx->ctx_instr; |
| 6173 | garray_T *stack = &cctx->ctx_type_stack; |
| 6174 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6175 | lvar_T *loop_lvar; // loop iteration variable |
| 6176 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6177 | type_T *vartype; |
| 6178 | |
| 6179 | // TODO: list of variables: "for [key, value] in dict" |
| 6180 | // parse "var" |
| 6181 | for (p = arg; eval_isnamec1(*p); ++p) |
| 6182 | ; |
| 6183 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6184 | var_lvar = lookup_local(arg, varlen, cctx); |
| 6185 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6186 | { |
| 6187 | semsg(_("E1023: variable already defined: %s"), arg); |
| 6188 | return NULL; |
| 6189 | } |
| 6190 | |
| 6191 | // consume "in" |
| 6192 | p = skipwhite(p); |
| 6193 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 6194 | { |
| 6195 | emsg(_(e_missing_in)); |
| 6196 | return NULL; |
| 6197 | } |
| 6198 | p = skipwhite(p + 2); |
| 6199 | |
| 6200 | |
| 6201 | scope = new_scope(cctx, FOR_SCOPE); |
| 6202 | if (scope == NULL) |
| 6203 | return NULL; |
| 6204 | |
| 6205 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6206 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6207 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6208 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6209 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6210 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6211 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6212 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6213 | |
| 6214 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6215 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 6216 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6217 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6218 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6219 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6220 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6221 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6222 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6223 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6224 | |
| 6225 | // compile "expr", it remains on the stack until "endfor" |
| 6226 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6227 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6228 | { |
| 6229 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6230 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6231 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6232 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6233 | // Now that we know the type of "var", check that it is a list, now or at |
| 6234 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6235 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 6236 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6237 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6238 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6239 | return NULL; |
| 6240 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6241 | 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] | 6242 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6243 | |
| 6244 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6245 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6246 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6247 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 6248 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6249 | |
| 6250 | return arg; |
| 6251 | } |
| 6252 | |
| 6253 | /* |
| 6254 | * compile "endfor" |
| 6255 | */ |
| 6256 | static char_u * |
| 6257 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 6258 | { |
| 6259 | garray_T *instr = &cctx->ctx_instr; |
| 6260 | scope_T *scope = cctx->ctx_scope; |
| 6261 | forscope_T *forscope; |
| 6262 | isn_T *isn; |
| 6263 | |
| 6264 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 6265 | { |
| 6266 | emsg(_(e_for)); |
| 6267 | return NULL; |
| 6268 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6269 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6270 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6271 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6272 | |
| 6273 | // At end of ":for" scope jump back to the FOR instruction. |
| 6274 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 6275 | |
| 6276 | // Fill in the "end" label in the FOR statement so it can jump here |
| 6277 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 6278 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 6279 | |
| 6280 | // Fill in the "end" label any BREAK statements |
| 6281 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 6282 | |
| 6283 | // Below the ":for" scope drop the "expr" list from the stack. |
| 6284 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 6285 | return NULL; |
| 6286 | |
| 6287 | vim_free(scope); |
| 6288 | |
| 6289 | return arg; |
| 6290 | } |
| 6291 | |
| 6292 | /* |
| 6293 | * compile "while expr" |
| 6294 | * |
| 6295 | * Produces instructions: |
| 6296 | * top: EVAL expr Push result of "expr" |
| 6297 | * JUMP_IF_FALSE end jump if false |
| 6298 | * ... body ... |
| 6299 | * JUMP top Jump back to repeat |
| 6300 | * end: |
| 6301 | * |
| 6302 | */ |
| 6303 | static char_u * |
| 6304 | compile_while(char_u *arg, cctx_T *cctx) |
| 6305 | { |
| 6306 | char_u *p = arg; |
| 6307 | garray_T *instr = &cctx->ctx_instr; |
| 6308 | scope_T *scope; |
| 6309 | |
| 6310 | scope = new_scope(cctx, WHILE_SCOPE); |
| 6311 | if (scope == NULL) |
| 6312 | return NULL; |
| 6313 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6314 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6315 | |
| 6316 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6317 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6318 | return NULL; |
| 6319 | |
| 6320 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6321 | 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] | 6322 | JUMP_IF_FALSE, cctx) == FAIL) |
| 6323 | return FAIL; |
| 6324 | |
| 6325 | return p; |
| 6326 | } |
| 6327 | |
| 6328 | /* |
| 6329 | * compile "endwhile" |
| 6330 | */ |
| 6331 | static char_u * |
| 6332 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 6333 | { |
| 6334 | scope_T *scope = cctx->ctx_scope; |
| 6335 | |
| 6336 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 6337 | { |
| 6338 | emsg(_(e_while)); |
| 6339 | return NULL; |
| 6340 | } |
| 6341 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6342 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6343 | |
| 6344 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6345 | 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] | 6346 | |
| 6347 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 6348 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6349 | 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] | 6350 | |
| 6351 | vim_free(scope); |
| 6352 | |
| 6353 | return arg; |
| 6354 | } |
| 6355 | |
| 6356 | /* |
| 6357 | * compile "continue" |
| 6358 | */ |
| 6359 | static char_u * |
| 6360 | compile_continue(char_u *arg, cctx_T *cctx) |
| 6361 | { |
| 6362 | scope_T *scope = cctx->ctx_scope; |
| 6363 | |
| 6364 | for (;;) |
| 6365 | { |
| 6366 | if (scope == NULL) |
| 6367 | { |
| 6368 | emsg(_(e_continue)); |
| 6369 | return NULL; |
| 6370 | } |
| 6371 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6372 | break; |
| 6373 | scope = scope->se_outer; |
| 6374 | } |
| 6375 | |
| 6376 | // Jump back to the FOR or WHILE instruction. |
| 6377 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6378 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 6379 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6380 | return arg; |
| 6381 | } |
| 6382 | |
| 6383 | /* |
| 6384 | * compile "break" |
| 6385 | */ |
| 6386 | static char_u * |
| 6387 | compile_break(char_u *arg, cctx_T *cctx) |
| 6388 | { |
| 6389 | scope_T *scope = cctx->ctx_scope; |
| 6390 | endlabel_T **el; |
| 6391 | |
| 6392 | for (;;) |
| 6393 | { |
| 6394 | if (scope == NULL) |
| 6395 | { |
| 6396 | emsg(_(e_break)); |
| 6397 | return NULL; |
| 6398 | } |
| 6399 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6400 | break; |
| 6401 | scope = scope->se_outer; |
| 6402 | } |
| 6403 | |
| 6404 | // Jump to the end of the FOR or WHILE loop. |
| 6405 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6406 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6407 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6408 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6409 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 6410 | return FAIL; |
| 6411 | |
| 6412 | return arg; |
| 6413 | } |
| 6414 | |
| 6415 | /* |
| 6416 | * compile "{" start of block |
| 6417 | */ |
| 6418 | static char_u * |
| 6419 | compile_block(char_u *arg, cctx_T *cctx) |
| 6420 | { |
| 6421 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6422 | return NULL; |
| 6423 | return skipwhite(arg + 1); |
| 6424 | } |
| 6425 | |
| 6426 | /* |
| 6427 | * compile end of block: drop one scope |
| 6428 | */ |
| 6429 | static void |
| 6430 | compile_endblock(cctx_T *cctx) |
| 6431 | { |
| 6432 | scope_T *scope = cctx->ctx_scope; |
| 6433 | |
| 6434 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6435 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6436 | vim_free(scope); |
| 6437 | } |
| 6438 | |
| 6439 | /* |
| 6440 | * compile "try" |
| 6441 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6442 | * finally. |
| 6443 | * Creates another scope for the "try" block itself. |
| 6444 | * TRY instruction sets up exception handling at runtime. |
| 6445 | * |
| 6446 | * "try" |
| 6447 | * TRY -> catch1, -> finally push trystack entry |
| 6448 | * ... try block |
| 6449 | * "throw {exception}" |
| 6450 | * EVAL {exception} |
| 6451 | * THROW create exception |
| 6452 | * ... try block |
| 6453 | * " catch {expr}" |
| 6454 | * JUMP -> finally |
| 6455 | * catch1: PUSH exeception |
| 6456 | * EVAL {expr} |
| 6457 | * MATCH |
| 6458 | * JUMP nomatch -> catch2 |
| 6459 | * CATCH remove exception |
| 6460 | * ... catch block |
| 6461 | * " catch" |
| 6462 | * JUMP -> finally |
| 6463 | * catch2: CATCH remove exception |
| 6464 | * ... catch block |
| 6465 | * " finally" |
| 6466 | * finally: |
| 6467 | * ... finally block |
| 6468 | * " endtry" |
| 6469 | * ENDTRY pop trystack entry, may rethrow |
| 6470 | */ |
| 6471 | static char_u * |
| 6472 | compile_try(char_u *arg, cctx_T *cctx) |
| 6473 | { |
| 6474 | garray_T *instr = &cctx->ctx_instr; |
| 6475 | scope_T *try_scope; |
| 6476 | scope_T *scope; |
| 6477 | |
| 6478 | // scope that holds the jumps that go to catch/finally/endtry |
| 6479 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6480 | if (try_scope == NULL) |
| 6481 | return NULL; |
| 6482 | |
| 6483 | // "catch" is set when the first ":catch" is found. |
| 6484 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6485 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6486 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6487 | return NULL; |
| 6488 | |
| 6489 | // scope for the try block itself |
| 6490 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6491 | if (scope == NULL) |
| 6492 | return NULL; |
| 6493 | |
| 6494 | return arg; |
| 6495 | } |
| 6496 | |
| 6497 | /* |
| 6498 | * compile "catch {expr}" |
| 6499 | */ |
| 6500 | static char_u * |
| 6501 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6502 | { |
| 6503 | scope_T *scope = cctx->ctx_scope; |
| 6504 | garray_T *instr = &cctx->ctx_instr; |
| 6505 | char_u *p; |
| 6506 | isn_T *isn; |
| 6507 | |
| 6508 | // end block scope from :try or :catch |
| 6509 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6510 | compile_endblock(cctx); |
| 6511 | scope = cctx->ctx_scope; |
| 6512 | |
| 6513 | // Error if not in a :try scope |
| 6514 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6515 | { |
| 6516 | emsg(_(e_catch)); |
| 6517 | return NULL; |
| 6518 | } |
| 6519 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6520 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6521 | { |
| 6522 | emsg(_("E1033: catch unreachable after catch-all")); |
| 6523 | return NULL; |
| 6524 | } |
| 6525 | |
| 6526 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6527 | 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] | 6528 | JUMP_ALWAYS, cctx) == FAIL) |
| 6529 | return NULL; |
| 6530 | |
| 6531 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6532 | 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] | 6533 | if (isn->isn_arg.try.try_catch == 0) |
| 6534 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6535 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6536 | { |
| 6537 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6538 | 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] | 6539 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6540 | } |
| 6541 | |
| 6542 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6543 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6544 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6545 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6546 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6547 | } |
| 6548 | else |
| 6549 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6550 | char_u *end; |
| 6551 | char_u *pat; |
| 6552 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6553 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6554 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6555 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6556 | // Push v:exception, push {expr} and MATCH |
| 6557 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6558 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6559 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6560 | if (*end != *p) |
| 6561 | { |
| 6562 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 6563 | vim_free(tofree); |
| 6564 | return FAIL; |
| 6565 | } |
| 6566 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6567 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6568 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6569 | len = (int)(end - tofree); |
| 6570 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6571 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6572 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6573 | if (pat == NULL) |
| 6574 | return FAIL; |
| 6575 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6576 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6577 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6578 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6579 | return NULL; |
| 6580 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6581 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6582 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6583 | return NULL; |
| 6584 | } |
| 6585 | |
| 6586 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6587 | return NULL; |
| 6588 | |
| 6589 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6590 | return NULL; |
| 6591 | return p; |
| 6592 | } |
| 6593 | |
| 6594 | static char_u * |
| 6595 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6596 | { |
| 6597 | scope_T *scope = cctx->ctx_scope; |
| 6598 | garray_T *instr = &cctx->ctx_instr; |
| 6599 | isn_T *isn; |
| 6600 | |
| 6601 | // end block scope from :try or :catch |
| 6602 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6603 | compile_endblock(cctx); |
| 6604 | scope = cctx->ctx_scope; |
| 6605 | |
| 6606 | // Error if not in a :try scope |
| 6607 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6608 | { |
| 6609 | emsg(_(e_finally)); |
| 6610 | return NULL; |
| 6611 | } |
| 6612 | |
| 6613 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6614 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6615 | if (isn->isn_arg.try.try_finally != 0) |
| 6616 | { |
| 6617 | emsg(_(e_finally_dup)); |
| 6618 | return NULL; |
| 6619 | } |
| 6620 | |
| 6621 | // 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] | 6622 | 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] | 6623 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6624 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6625 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6626 | { |
| 6627 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6628 | 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] | 6629 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6630 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6631 | } |
| 6632 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6633 | // TODO: set index in ts_finally_label jumps |
| 6634 | |
| 6635 | return arg; |
| 6636 | } |
| 6637 | |
| 6638 | static char_u * |
| 6639 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6640 | { |
| 6641 | scope_T *scope = cctx->ctx_scope; |
| 6642 | garray_T *instr = &cctx->ctx_instr; |
| 6643 | isn_T *isn; |
| 6644 | |
| 6645 | // end block scope from :catch or :finally |
| 6646 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6647 | compile_endblock(cctx); |
| 6648 | scope = cctx->ctx_scope; |
| 6649 | |
| 6650 | // Error if not in a :try scope |
| 6651 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6652 | { |
| 6653 | if (scope == NULL) |
| 6654 | emsg(_(e_no_endtry)); |
| 6655 | else if (scope->se_type == WHILE_SCOPE) |
| 6656 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6657 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6658 | emsg(_(e_endfor)); |
| 6659 | else |
| 6660 | emsg(_(e_endif)); |
| 6661 | return NULL; |
| 6662 | } |
| 6663 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6664 | 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] | 6665 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6666 | { |
| 6667 | emsg(_("E1032: missing :catch or :finally")); |
| 6668 | return NULL; |
| 6669 | } |
| 6670 | |
| 6671 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6672 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6673 | 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] | 6674 | |
| 6675 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6676 | if (isn->isn_arg.try.try_catch == 0) |
| 6677 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6678 | if (isn->isn_arg.try.try_finally == 0) |
| 6679 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6680 | |
| 6681 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6682 | { |
| 6683 | // Last catch without match jumps here |
| 6684 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6685 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6686 | } |
| 6687 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6688 | compile_endblock(cctx); |
| 6689 | |
| 6690 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6691 | return NULL; |
| 6692 | return arg; |
| 6693 | } |
| 6694 | |
| 6695 | /* |
| 6696 | * compile "throw {expr}" |
| 6697 | */ |
| 6698 | static char_u * |
| 6699 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6700 | { |
| 6701 | char_u *p = skipwhite(arg); |
| 6702 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6703 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6704 | return NULL; |
| 6705 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6706 | return NULL; |
| 6707 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6708 | return NULL; |
| 6709 | |
| 6710 | return p; |
| 6711 | } |
| 6712 | |
| 6713 | /* |
| 6714 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6715 | * compile "echomsg expr" |
| 6716 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6717 | * compile "execute expr" |
| 6718 | */ |
| 6719 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6720 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6721 | { |
| 6722 | char_u *p = arg; |
| 6723 | int count = 0; |
| 6724 | |
| 6725 | for (;;) |
| 6726 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6727 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6728 | return NULL; |
| 6729 | ++count; |
| 6730 | p = skipwhite(p); |
| 6731 | if (ends_excmd(*p)) |
| 6732 | break; |
| 6733 | } |
| 6734 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6735 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6736 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6737 | else if (cmdidx == CMD_execute) |
| 6738 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6739 | else if (cmdidx == CMD_echomsg) |
| 6740 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6741 | else |
| 6742 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6743 | return p; |
| 6744 | } |
| 6745 | |
| 6746 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6747 | * A command that is not compiled, execute with legacy code. |
| 6748 | */ |
| 6749 | static char_u * |
| 6750 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6751 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6752 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6753 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6754 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6755 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6756 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6757 | goto theend; |
| 6758 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6759 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6760 | { |
| 6761 | long argt = excmd_get_argt(eap->cmdidx); |
| 6762 | int usefilter = FALSE; |
| 6763 | |
| 6764 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6765 | |
| 6766 | // If the command can be followed by a bar, find the bar and truncate |
| 6767 | // it, so that the following command can be compiled. |
| 6768 | // The '|' is overwritten with a NUL, it is put back below. |
| 6769 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6770 | && *eap->arg == '!') |
| 6771 | // :w !filter or :r !filter or :r! filter |
| 6772 | usefilter = TRUE; |
| 6773 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6774 | { |
| 6775 | separate_nextcmd(eap); |
| 6776 | if (eap->nextcmd != NULL) |
| 6777 | nextcmd = eap->nextcmd; |
| 6778 | } |
| 6779 | } |
| 6780 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6781 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6782 | { |
| 6783 | // expand filename in "syntax include [@group] filename" |
| 6784 | has_expr = TRUE; |
| 6785 | eap->arg = skipwhite(eap->arg + 7); |
| 6786 | if (*eap->arg == '@') |
| 6787 | eap->arg = skiptowhite(eap->arg); |
| 6788 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6789 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6790 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6791 | { |
| 6792 | int count = 0; |
| 6793 | char_u *start = skipwhite(line); |
| 6794 | |
| 6795 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6796 | // PUSHS ":cmd xxx" |
| 6797 | // eval expr1 |
| 6798 | // PUSHS "yyy" |
| 6799 | // eval expr2 |
| 6800 | // PUSHS "zzz" |
| 6801 | // EXECCONCAT 5 |
| 6802 | for (;;) |
| 6803 | { |
| 6804 | if (p > start) |
| 6805 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6806 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6807 | ++count; |
| 6808 | } |
| 6809 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6810 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6811 | return NULL; |
| 6812 | may_generate_2STRING(-1, cctx); |
| 6813 | ++count; |
| 6814 | p = skipwhite(p); |
| 6815 | if (*p != '`') |
| 6816 | { |
| 6817 | emsg(_("E1083: missing backtick")); |
| 6818 | return NULL; |
| 6819 | } |
| 6820 | start = p + 1; |
| 6821 | |
| 6822 | p = (char_u *)strstr((char *)start, "`="); |
| 6823 | if (p == NULL) |
| 6824 | { |
| 6825 | if (*skipwhite(start) != NUL) |
| 6826 | { |
| 6827 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6828 | ++count; |
| 6829 | } |
| 6830 | break; |
| 6831 | } |
| 6832 | } |
| 6833 | generate_EXECCONCAT(cctx, count); |
| 6834 | } |
| 6835 | else |
| 6836 | generate_EXEC(cctx, line); |
| 6837 | |
| 6838 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6839 | if (*nextcmd != NUL) |
| 6840 | { |
| 6841 | // the parser expects a pointer to the bar, put it back |
| 6842 | --nextcmd; |
| 6843 | *nextcmd = '|'; |
| 6844 | } |
| 6845 | |
| 6846 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6847 | } |
| 6848 | |
| 6849 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6850 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6851 | * 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] | 6852 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6853 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6854 | add_def_function(ufunc_T *ufunc) |
| 6855 | { |
| 6856 | dfunc_T *dfunc; |
| 6857 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6858 | if (def_functions.ga_len == 0) |
| 6859 | { |
| 6860 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6861 | // wasn't set. |
| 6862 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6863 | return FAIL; |
| 6864 | ++def_functions.ga_len; |
| 6865 | } |
| 6866 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6867 | // Add the function to "def_functions". |
| 6868 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6869 | return FAIL; |
| 6870 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6871 | CLEAR_POINTER(dfunc); |
| 6872 | dfunc->df_idx = def_functions.ga_len; |
| 6873 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6874 | dfunc->df_ufunc = ufunc; |
| 6875 | ++def_functions.ga_len; |
| 6876 | return OK; |
| 6877 | } |
| 6878 | |
| 6879 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6880 | * After ex_function() has collected all the function lines: parse and compile |
| 6881 | * the lines into instructions. |
| 6882 | * Adds the function to "def_functions". |
| 6883 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6884 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6885 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6886 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6887 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6888 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6889 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6890 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6891 | 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] | 6892 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6893 | char_u *line = NULL; |
| 6894 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6895 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6896 | cctx_T cctx; |
| 6897 | garray_T *instr; |
| 6898 | int called_emsg_before = called_emsg; |
| 6899 | int ret = FAIL; |
| 6900 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6901 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6902 | int emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6903 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6904 | // When using a function that was compiled before: Free old instructions. |
| 6905 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6906 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6907 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6908 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6909 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6910 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6911 | } |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6912 | else if (add_def_function(ufunc) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6913 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6914 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6915 | ufunc->uf_def_status = UF_COMPILING; |
| 6916 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6917 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6918 | cctx.ctx_ufunc = ufunc; |
| 6919 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6920 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6921 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6922 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6923 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6924 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6925 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6926 | instr = &cctx.ctx_instr; |
| 6927 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6928 | // Set the context to the function, it may be compiled when called from |
| 6929 | // another script. Set the script version to the most modern one. |
| 6930 | // The line number will be set in next_line_from_context(). |
| 6931 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6932 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6933 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6934 | // Make sure error messages are OK. |
| 6935 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6936 | if (do_estack_push) |
| 6937 | estack_push_ufunc(ufunc, 1); |
| 6938 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6939 | if (ufunc->uf_def_args.ga_len > 0) |
| 6940 | { |
| 6941 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6942 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6943 | int i; |
| 6944 | char_u *arg; |
| 6945 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 6946 | |
| 6947 | // Produce instructions for the default values of optional arguments. |
| 6948 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6949 | // where to start when the function is called, depending on the number |
| 6950 | // of arguments. |
| 6951 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6952 | if (ufunc->uf_def_arg_idx == NULL) |
| 6953 | goto erret; |
| 6954 | for (i = 0; i < count; ++i) |
| 6955 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6956 | garray_T *stack = &cctx.ctx_type_stack; |
| 6957 | type_T *val_type; |
| 6958 | int arg_idx = first_def_arg + i; |
| 6959 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6960 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6961 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6962 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6963 | goto erret; |
| 6964 | |
| 6965 | // If no type specified use the type of the default value. |
| 6966 | // Otherwise check that the default value type matches the |
| 6967 | // specified type. |
| 6968 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6969 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
| 6970 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 6971 | 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] | 6972 | == FAIL) |
| 6973 | { |
| 6974 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 6975 | arg_idx + 1); |
| 6976 | goto erret; |
| 6977 | } |
| 6978 | |
| 6979 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6980 | goto erret; |
| 6981 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6982 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
| 6983 | } |
| 6984 | |
| 6985 | /* |
| 6986 | * Loop over all the lines of the function and generate instructions. |
| 6987 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6988 | for (;;) |
| 6989 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6990 | exarg_T ea; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6991 | int starts_with_colon = FALSE; |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6992 | char_u *cmd; |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 6993 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6994 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6995 | // Bail out on the first error to avoid a flood of errors and report |
| 6996 | // the right line number when inside try/catch. |
| 6997 | if (emsg_before != called_emsg) |
| 6998 | goto erret; |
| 6999 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7000 | if (line != NULL && *line == '|') |
| 7001 | // the line continues after a '|' |
| 7002 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 7003 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7004 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 7005 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7006 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7007 | semsg(_("E488: Trailing characters: %s"), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7008 | goto erret; |
| 7009 | } |
| 7010 | else |
| 7011 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 7012 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 7013 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7014 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7015 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7016 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7017 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7018 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7019 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7020 | ea.cmdlinep = &line; |
| 7021 | ea.cmd = skipwhite(line); |
| 7022 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7023 | // Some things can be recognized by the first character. |
| 7024 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7025 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7026 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7027 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7028 | if (ea.cmd[1] != '{') |
| 7029 | { |
| 7030 | line = (char_u *)""; |
| 7031 | continue; |
| 7032 | } |
| 7033 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7034 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7035 | case '}': |
| 7036 | { |
| 7037 | // "}" ends a block scope |
| 7038 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7039 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7040 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7041 | if (stype == BLOCK_SCOPE) |
| 7042 | { |
| 7043 | compile_endblock(&cctx); |
| 7044 | line = ea.cmd; |
| 7045 | } |
| 7046 | else |
| 7047 | { |
| 7048 | emsg(_("E1025: using } outside of a block scope")); |
| 7049 | goto erret; |
| 7050 | } |
| 7051 | if (line != NULL) |
| 7052 | line = skipwhite(ea.cmd + 1); |
| 7053 | continue; |
| 7054 | } |
| 7055 | |
| 7056 | case '{': |
| 7057 | // "{" starts a block scope |
| 7058 | // "{'a': 1}->func() is something else |
| 7059 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7060 | { |
| 7061 | line = compile_block(ea.cmd, &cctx); |
| 7062 | continue; |
| 7063 | } |
| 7064 | break; |
| 7065 | |
| 7066 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7067 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7068 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7069 | } |
| 7070 | |
| 7071 | /* |
| 7072 | * COMMAND MODIFIERS |
| 7073 | */ |
| 7074 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 7075 | { |
| 7076 | if (errormsg != NULL) |
| 7077 | goto erret; |
| 7078 | // empty line or comment |
| 7079 | line = (char_u *)""; |
| 7080 | continue; |
| 7081 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7082 | // TODO: use modifiers in the command |
| 7083 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | b074e8b | 2020-07-11 13:40:45 +0200 | [diff] [blame] | 7084 | CLEAR_FIELD(cmdmod); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7085 | |
| 7086 | // Skip ":call" to get to the function name. |
| 7087 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 7088 | ea.cmd = skipwhite(ea.cmd); |
| 7089 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7090 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7091 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7092 | char_u *pskip; |
| 7093 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7094 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7095 | // find what follows. |
| 7096 | // Skip over "var.member", "var[idx]" and the like. |
| 7097 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 7098 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7099 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7100 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7101 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7102 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7103 | char_u *var_end; |
| 7104 | int oplen; |
| 7105 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7106 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7107 | var_end = find_name_end(pskip, NULL, NULL, |
| 7108 | FNE_CHECK_START | FNE_INCL_BR); |
| 7109 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7110 | if (oplen > 0) |
| 7111 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7112 | size_t len = p - ea.cmd; |
| 7113 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7114 | // Recognize an assignment if we recognize the variable |
| 7115 | // name: |
| 7116 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 7117 | // "local = expr" where "local" is a local var. |
| 7118 | // "script = expr" where "script" is a script-local var. |
| 7119 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7120 | // "&opt = expr" |
| 7121 | // "$ENV = expr" |
| 7122 | // "@r = expr" |
| 7123 | if (*ea.cmd == '&' |
| 7124 | || *ea.cmd == '$' |
| 7125 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7126 | || ((len) > 2 && ea.cmd[1] == ':') |
| 7127 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 7128 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 7129 | NULL, &cctx) == OK |
| 7130 | || lookup_script(ea.cmd, len) == OK |
| 7131 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7132 | { |
| 7133 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7134 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7135 | goto erret; |
| 7136 | continue; |
| 7137 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7138 | } |
| 7139 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7140 | |
| 7141 | if (*ea.cmd == '[') |
| 7142 | { |
| 7143 | // [var, var] = expr |
| 7144 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 7145 | if (line == NULL) |
| 7146 | goto erret; |
| 7147 | if (line != ea.cmd) |
| 7148 | continue; |
| 7149 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7150 | } |
| 7151 | |
| 7152 | /* |
| 7153 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7154 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7155 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7156 | cmd = ea.cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7157 | if (*cmd != '\'') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7158 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7159 | ea.cmd = skip_range(ea.cmd, NULL); |
| 7160 | if (ea.cmd > cmd && !starts_with_colon) |
| 7161 | { |
| 7162 | emsg(_(e_colon_required)); |
| 7163 | goto erret; |
| 7164 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7165 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7166 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7167 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7168 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7169 | |
| 7170 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 7171 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7172 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7173 | { |
| 7174 | line += STRLEN(line); |
| 7175 | continue; |
| 7176 | } |
| 7177 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7178 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7179 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7180 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7181 | // CMD_let cannot happen, compile_assignment() above is used |
| 7182 | iemsg("Command from find_ex_command() not handled"); |
| 7183 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7184 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7185 | } |
| 7186 | |
| 7187 | p = skipwhite(p); |
| 7188 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7189 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 7190 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7191 | && ea.cmdidx != CMD_elseif |
| 7192 | && ea.cmdidx != CMD_else |
| 7193 | && ea.cmdidx != CMD_endif) |
| 7194 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7195 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7196 | continue; |
| 7197 | } |
| 7198 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7199 | if (ea.cmdidx != CMD_elseif |
| 7200 | && ea.cmdidx != CMD_else |
| 7201 | && ea.cmdidx != CMD_endif |
| 7202 | && ea.cmdidx != CMD_endfor |
| 7203 | && ea.cmdidx != CMD_endwhile |
| 7204 | && ea.cmdidx != CMD_catch |
| 7205 | && ea.cmdidx != CMD_finally |
| 7206 | && ea.cmdidx != CMD_endtry) |
| 7207 | { |
| 7208 | if (cctx.ctx_had_return) |
| 7209 | { |
| 7210 | emsg(_("E1095: Unreachable code after :return")); |
| 7211 | goto erret; |
| 7212 | } |
| 7213 | } |
| 7214 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7215 | switch (ea.cmdidx) |
| 7216 | { |
| 7217 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7218 | ea.arg = p; |
| 7219 | line = compile_nested_function(&ea, &cctx); |
| 7220 | break; |
| 7221 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7222 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7223 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7224 | goto erret; |
| 7225 | |
| 7226 | case CMD_return: |
| 7227 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7228 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7229 | break; |
| 7230 | |
| 7231 | case CMD_let: |
| 7232 | case CMD_const: |
| 7233 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7234 | if (line == p) |
| 7235 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7236 | break; |
| 7237 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7238 | case CMD_unlet: |
| 7239 | case CMD_unlockvar: |
| 7240 | case CMD_lockvar: |
| 7241 | line = compile_unletlock(p, &ea, &cctx); |
| 7242 | break; |
| 7243 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7244 | case CMD_import: |
| 7245 | line = compile_import(p, &cctx); |
| 7246 | break; |
| 7247 | |
| 7248 | case CMD_if: |
| 7249 | line = compile_if(p, &cctx); |
| 7250 | break; |
| 7251 | case CMD_elseif: |
| 7252 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7253 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7254 | break; |
| 7255 | case CMD_else: |
| 7256 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7257 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7258 | break; |
| 7259 | case CMD_endif: |
| 7260 | line = compile_endif(p, &cctx); |
| 7261 | break; |
| 7262 | |
| 7263 | case CMD_while: |
| 7264 | line = compile_while(p, &cctx); |
| 7265 | break; |
| 7266 | case CMD_endwhile: |
| 7267 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7268 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7269 | break; |
| 7270 | |
| 7271 | case CMD_for: |
| 7272 | line = compile_for(p, &cctx); |
| 7273 | break; |
| 7274 | case CMD_endfor: |
| 7275 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7276 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7277 | break; |
| 7278 | case CMD_continue: |
| 7279 | line = compile_continue(p, &cctx); |
| 7280 | break; |
| 7281 | case CMD_break: |
| 7282 | line = compile_break(p, &cctx); |
| 7283 | break; |
| 7284 | |
| 7285 | case CMD_try: |
| 7286 | line = compile_try(p, &cctx); |
| 7287 | break; |
| 7288 | case CMD_catch: |
| 7289 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7290 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7291 | break; |
| 7292 | case CMD_finally: |
| 7293 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7294 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7295 | break; |
| 7296 | case CMD_endtry: |
| 7297 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7298 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7299 | break; |
| 7300 | case CMD_throw: |
| 7301 | line = compile_throw(p, &cctx); |
| 7302 | break; |
| 7303 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7304 | case CMD_eval: |
| 7305 | if (compile_expr0(&p, &cctx) == FAIL) |
| 7306 | goto erret; |
| 7307 | |
| 7308 | // drop the return value |
| 7309 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 7310 | |
| 7311 | line = skipwhite(p); |
| 7312 | break; |
| 7313 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7314 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7315 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7316 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7317 | case CMD_echomsg: |
| 7318 | case CMD_echoerr: |
| 7319 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7320 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7321 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7322 | // TODO: other commands with an expression argument |
| 7323 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7324 | case CMD_SIZE: |
| 7325 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 7326 | goto erret; |
| 7327 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7328 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7329 | // Not recognized, execute with do_cmdline_cmd(). |
| 7330 | ea.arg = p; |
| 7331 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7332 | break; |
| 7333 | } |
| 7334 | if (line == NULL) |
| 7335 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7336 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7337 | |
| 7338 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7339 | { |
| 7340 | iemsg("Type stack underflow"); |
| 7341 | goto erret; |
| 7342 | } |
| 7343 | } |
| 7344 | |
| 7345 | if (cctx.ctx_scope != NULL) |
| 7346 | { |
| 7347 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7348 | emsg(_(e_endif)); |
| 7349 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7350 | emsg(_(e_endwhile)); |
| 7351 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7352 | emsg(_(e_endfor)); |
| 7353 | else |
| 7354 | emsg(_("E1026: Missing }")); |
| 7355 | goto erret; |
| 7356 | } |
| 7357 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7358 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7359 | { |
| 7360 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7361 | { |
| 7362 | emsg(_("E1027: Missing return statement")); |
| 7363 | goto erret; |
| 7364 | } |
| 7365 | |
| 7366 | // Return zero if there is no return at the end. |
| 7367 | generate_PUSHNR(&cctx, 0); |
| 7368 | generate_instr(&cctx, ISN_RETURN); |
| 7369 | } |
| 7370 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7371 | { |
| 7372 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7373 | + ufunc->uf_dfunc_idx; |
| 7374 | dfunc->df_deleted = FALSE; |
| 7375 | dfunc->df_instr = instr->ga_data; |
| 7376 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7377 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 7378 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7379 | if (cctx.ctx_outer_used) |
| 7380 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7381 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7382 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7383 | |
| 7384 | ret = OK; |
| 7385 | |
| 7386 | erret: |
| 7387 | if (ret == FAIL) |
| 7388 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7389 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7390 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7391 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7392 | |
| 7393 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7394 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7395 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7396 | |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7397 | // if using the last entry in the table we might as well remove it |
| 7398 | if (!dfunc->df_deleted |
| 7399 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7400 | --def_functions.ga_len; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7401 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7402 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7403 | while (cctx.ctx_scope != NULL) |
| 7404 | drop_scope(&cctx); |
| 7405 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7406 | // Don't execute this function body. |
| 7407 | ga_clear_strings(&ufunc->uf_lines); |
| 7408 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7409 | if (errormsg != NULL) |
| 7410 | emsg(errormsg); |
| 7411 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 7412 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7413 | } |
| 7414 | |
| 7415 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7416 | if (do_estack_push) |
| 7417 | estack_pop(); |
| 7418 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7419 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7420 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7421 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7422 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7423 | } |
| 7424 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7425 | void |
| 7426 | set_function_type(ufunc_T *ufunc) |
| 7427 | { |
| 7428 | int varargs = ufunc->uf_va_name != NULL; |
| 7429 | int argcount = ufunc->uf_args.ga_len; |
| 7430 | |
| 7431 | // Create a type for the function, with the return type and any |
| 7432 | // argument types. |
| 7433 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7434 | // The type is included in "tt_args". |
| 7435 | if (argcount > 0 || varargs) |
| 7436 | { |
| 7437 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7438 | argcount, &ufunc->uf_type_list); |
| 7439 | // Add argument types to the function type. |
| 7440 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7441 | argcount + varargs, |
| 7442 | &ufunc->uf_type_list) == FAIL) |
| 7443 | return; |
| 7444 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7445 | ufunc->uf_func_type->tt_min_argcount = |
| 7446 | argcount - ufunc->uf_def_args.ga_len; |
| 7447 | if (ufunc->uf_arg_types == NULL) |
| 7448 | { |
| 7449 | int i; |
| 7450 | |
| 7451 | // lambda does not have argument types. |
| 7452 | for (i = 0; i < argcount; ++i) |
| 7453 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7454 | } |
| 7455 | else |
| 7456 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7457 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7458 | if (varargs) |
| 7459 | { |
| 7460 | ufunc->uf_func_type->tt_args[argcount] = |
| 7461 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7462 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7463 | } |
| 7464 | } |
| 7465 | else |
| 7466 | // No arguments, can use a predefined type. |
| 7467 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7468 | argcount, &ufunc->uf_type_list); |
| 7469 | } |
| 7470 | |
| 7471 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7472 | /* |
| 7473 | * Delete an instruction, free what it contains. |
| 7474 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7475 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7476 | delete_instr(isn_T *isn) |
| 7477 | { |
| 7478 | switch (isn->isn_type) |
| 7479 | { |
| 7480 | case ISN_EXEC: |
| 7481 | case ISN_LOADENV: |
| 7482 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7483 | case ISN_LOADB: |
| 7484 | case ISN_LOADW: |
| 7485 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7486 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7487 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7488 | case ISN_PUSHEXC: |
| 7489 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7490 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7491 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7492 | case ISN_STOREB: |
| 7493 | case ISN_STOREW: |
| 7494 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7495 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7496 | vim_free(isn->isn_arg.string); |
| 7497 | break; |
| 7498 | |
| 7499 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7500 | case ISN_STORES: |
| 7501 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7502 | break; |
| 7503 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7504 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7505 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7506 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7507 | break; |
| 7508 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7509 | case ISN_STOREOPT: |
| 7510 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7511 | break; |
| 7512 | |
| 7513 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7514 | blob_unref(isn->isn_arg.blob); |
| 7515 | break; |
| 7516 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7517 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7518 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7519 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7520 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7521 | break; |
| 7522 | |
| 7523 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7524 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7525 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7526 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7527 | break; |
| 7528 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7529 | case ISN_UCALL: |
| 7530 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7531 | break; |
| 7532 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7533 | case ISN_FUNCREF: |
| 7534 | { |
| 7535 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7536 | + isn->isn_arg.funcref.fr_func; |
| 7537 | func_ptr_unref(dfunc->df_ufunc); |
| 7538 | } |
| 7539 | break; |
| 7540 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7541 | case ISN_2BOOL: |
| 7542 | case ISN_2STRING: |
| 7543 | case ISN_ADDBLOB: |
| 7544 | case ISN_ADDLIST: |
| 7545 | case ISN_BCALL: |
| 7546 | case ISN_CATCH: |
| 7547 | case ISN_CHECKNR: |
| 7548 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7549 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7550 | case ISN_COMPAREANY: |
| 7551 | case ISN_COMPAREBLOB: |
| 7552 | case ISN_COMPAREBOOL: |
| 7553 | case ISN_COMPAREDICT: |
| 7554 | case ISN_COMPAREFLOAT: |
| 7555 | case ISN_COMPAREFUNC: |
| 7556 | case ISN_COMPARELIST: |
| 7557 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7558 | case ISN_COMPARESPECIAL: |
| 7559 | case ISN_COMPARESTRING: |
| 7560 | case ISN_CONCAT: |
| 7561 | case ISN_DCALL: |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 7562 | case ISN_SHUFFLE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7563 | case ISN_DROP: |
| 7564 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7565 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7566 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7567 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7568 | case ISN_EXECCONCAT: |
| 7569 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7570 | case ISN_FOR: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7571 | case ISN_LISTINDEX: |
| 7572 | case ISN_STRINDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7573 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7574 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7575 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7576 | case ISN_JUMP: |
| 7577 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7578 | case ISN_LOADBDICT: |
| 7579 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7580 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7581 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7582 | case ISN_LOADSCRIPT: |
| 7583 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7584 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7585 | case ISN_LOADWDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7586 | case ISN_NEGATENR: |
| 7587 | case ISN_NEWDICT: |
| 7588 | case ISN_NEWLIST: |
| 7589 | case ISN_OPNR: |
| 7590 | case ISN_OPFLOAT: |
| 7591 | case ISN_OPANY: |
| 7592 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7593 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7594 | case ISN_PUSHF: |
| 7595 | case ISN_PUSHNR: |
| 7596 | case ISN_PUSHBOOL: |
| 7597 | case ISN_PUSHSPEC: |
| 7598 | case ISN_RETURN: |
| 7599 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 7600 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7601 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7602 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7603 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7604 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7605 | case ISN_STOREDICT: |
| 7606 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7607 | case ISN_THROW: |
| 7608 | case ISN_TRY: |
| 7609 | // nothing allocated |
| 7610 | break; |
| 7611 | } |
| 7612 | } |
| 7613 | |
| 7614 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7615 | * Free all instructions for "dfunc". |
| 7616 | */ |
| 7617 | static void |
| 7618 | delete_def_function_contents(dfunc_T *dfunc) |
| 7619 | { |
| 7620 | int idx; |
| 7621 | |
| 7622 | ga_clear(&dfunc->df_def_args_isn); |
| 7623 | |
| 7624 | if (dfunc->df_instr != NULL) |
| 7625 | { |
| 7626 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7627 | delete_instr(dfunc->df_instr + idx); |
| 7628 | VIM_CLEAR(dfunc->df_instr); |
| 7629 | } |
| 7630 | |
| 7631 | dfunc->df_deleted = TRUE; |
| 7632 | } |
| 7633 | |
| 7634 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7635 | * When a user function is deleted, clear the contents of any associated def |
| 7636 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7637 | */ |
| 7638 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7639 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7640 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7641 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7642 | { |
| 7643 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7644 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7645 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7646 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7647 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7648 | } |
| 7649 | } |
| 7650 | |
| 7651 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7652 | /* |
| 7653 | * Free all functions defined with ":def". |
| 7654 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7655 | void |
| 7656 | free_def_functions(void) |
| 7657 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7658 | int idx; |
| 7659 | |
| 7660 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7661 | { |
| 7662 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7663 | |
| 7664 | delete_def_function_contents(dfunc); |
| 7665 | } |
| 7666 | |
| 7667 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7668 | } |
| 7669 | #endif |
| 7670 | |
| 7671 | |
| 7672 | #endif // FEAT_EVAL |