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 | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1884 | else if (*p == '(' && STRNCMP("func", start, 4) == 0) |
| 1885 | { |
| 1886 | // handle func(args): type |
| 1887 | ++p; |
| 1888 | while (*p != ')' && *p != NUL) |
| 1889 | { |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1890 | char_u *sp = p; |
| 1891 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1892 | p = skip_type(p); |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1893 | if (p == sp) |
| 1894 | return p; // syntax error |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1895 | if (*p == ',') |
| 1896 | p = skipwhite(p + 1); |
| 1897 | } |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1898 | if (*p == ')') |
| 1899 | { |
| 1900 | if (p[1] == ':') |
| 1901 | p = skip_type(skipwhite(p + 2)); |
| 1902 | else |
| 1903 | p = skipwhite(p + 1); |
| 1904 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1905 | } |
| 1906 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1907 | return p; |
| 1908 | } |
| 1909 | |
| 1910 | /* |
| 1911 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1912 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1913 | * Returns NULL in case of failure. |
| 1914 | */ |
| 1915 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1916 | 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] | 1917 | { |
| 1918 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1919 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1920 | |
| 1921 | if (**arg != '<') |
| 1922 | { |
| 1923 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1924 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1925 | else |
| 1926 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1927 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1928 | } |
| 1929 | *arg = skipwhite(*arg + 1); |
| 1930 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1931 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1932 | |
| 1933 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1934 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1935 | { |
| 1936 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1937 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1938 | } |
| 1939 | ++*arg; |
| 1940 | |
| 1941 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1942 | return get_list_type(member_type, type_gap); |
| 1943 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | /* |
| 1947 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 1948 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1949 | */ |
| 1950 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1951 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1952 | { |
| 1953 | char_u *p = *arg; |
| 1954 | size_t len; |
| 1955 | |
| 1956 | // skip over the first word |
| 1957 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1958 | ++p; |
| 1959 | len = p - *arg; |
| 1960 | |
| 1961 | switch (**arg) |
| 1962 | { |
| 1963 | case 'a': |
| 1964 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 1965 | { |
| 1966 | *arg += len; |
| 1967 | return &t_any; |
| 1968 | } |
| 1969 | break; |
| 1970 | case 'b': |
| 1971 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 1972 | { |
| 1973 | *arg += len; |
| 1974 | return &t_bool; |
| 1975 | } |
| 1976 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 1977 | { |
| 1978 | *arg += len; |
| 1979 | return &t_blob; |
| 1980 | } |
| 1981 | break; |
| 1982 | case 'c': |
| 1983 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 1984 | { |
| 1985 | *arg += len; |
| 1986 | return &t_channel; |
| 1987 | } |
| 1988 | break; |
| 1989 | case 'd': |
| 1990 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 1991 | { |
| 1992 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1993 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1994 | } |
| 1995 | break; |
| 1996 | case 'f': |
| 1997 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 1998 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1999 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2000 | *arg += len; |
| 2001 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2002 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2003 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2004 | return &t_any; |
| 2005 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2006 | } |
| 2007 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 2008 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2009 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2010 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2011 | int argcount = -1; |
| 2012 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2013 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2014 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 2015 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2016 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2017 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2018 | if (**arg == '(') |
| 2019 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2020 | // "func" may or may not return a value, "func()" does |
| 2021 | // not return a value. |
| 2022 | ret_type = &t_void; |
| 2023 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2024 | p = ++*arg; |
| 2025 | argcount = 0; |
| 2026 | while (*p != NUL && *p != ')') |
| 2027 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2028 | if (*p == '?') |
| 2029 | { |
| 2030 | if (first_optional == -1) |
| 2031 | first_optional = argcount; |
| 2032 | ++p; |
| 2033 | } |
| 2034 | else if (first_optional != -1) |
| 2035 | { |
| 2036 | emsg(_("E1007: mandatory argument after optional argument")); |
| 2037 | return &t_any; |
| 2038 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2039 | else if (STRNCMP(p, "...", 3) == 0) |
| 2040 | { |
| 2041 | flags |= TTFLAG_VARARGS; |
| 2042 | p += 3; |
| 2043 | } |
| 2044 | |
| 2045 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 2046 | |
| 2047 | // Nothing comes after "...{type}". |
| 2048 | if (flags & TTFLAG_VARARGS) |
| 2049 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2050 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2051 | if (*p != ',' && *skipwhite(p) == ',') |
| 2052 | { |
| 2053 | semsg(_(e_no_white_before), ","); |
| 2054 | return &t_any; |
| 2055 | } |
| 2056 | if (*p == ',') |
| 2057 | { |
| 2058 | ++p; |
| 2059 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2060 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2061 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2062 | return &t_any; |
| 2063 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2064 | } |
| 2065 | p = skipwhite(p); |
| 2066 | if (argcount == MAX_FUNC_ARGS) |
| 2067 | { |
| 2068 | emsg(_("E740: Too many argument types")); |
| 2069 | return &t_any; |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | p = skipwhite(p); |
| 2074 | if (*p != ')') |
| 2075 | { |
| 2076 | emsg(_(e_missing_close)); |
| 2077 | return &t_any; |
| 2078 | } |
| 2079 | *arg = p + 1; |
| 2080 | } |
| 2081 | if (**arg == ':') |
| 2082 | { |
| 2083 | // parse return type |
| 2084 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2085 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2086 | semsg(_(e_white_after), ":"); |
| 2087 | *arg = skipwhite(*arg); |
| 2088 | ret_type = parse_type(arg, type_gap); |
| 2089 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2090 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2091 | type = get_func_type(ret_type, argcount, type_gap); |
| 2092 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2093 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2094 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 2095 | type->tt_flags = flags; |
| 2096 | if (argcount > 0) |
| 2097 | { |
| 2098 | type->tt_argcount = argcount; |
| 2099 | type->tt_min_argcount = first_optional == -1 |
| 2100 | ? argcount : first_optional; |
| 2101 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2102 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2103 | return &t_any; |
| 2104 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2105 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2106 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2107 | } |
| 2108 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2109 | } |
| 2110 | break; |
| 2111 | case 'j': |
| 2112 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 2113 | { |
| 2114 | *arg += len; |
| 2115 | return &t_job; |
| 2116 | } |
| 2117 | break; |
| 2118 | case 'l': |
| 2119 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 2120 | { |
| 2121 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2122 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2123 | } |
| 2124 | break; |
| 2125 | case 'n': |
| 2126 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 2127 | { |
| 2128 | *arg += len; |
| 2129 | return &t_number; |
| 2130 | } |
| 2131 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2132 | case 's': |
| 2133 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 2134 | { |
| 2135 | *arg += len; |
| 2136 | return &t_string; |
| 2137 | } |
| 2138 | break; |
| 2139 | case 'v': |
| 2140 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 2141 | { |
| 2142 | *arg += len; |
| 2143 | return &t_void; |
| 2144 | } |
| 2145 | break; |
| 2146 | } |
| 2147 | |
| 2148 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 2149 | return &t_any; |
| 2150 | } |
| 2151 | |
| 2152 | /* |
| 2153 | * Check if "type1" and "type2" are exactly the same. |
| 2154 | */ |
| 2155 | static int |
| 2156 | equal_type(type_T *type1, type_T *type2) |
| 2157 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2158 | int i; |
| 2159 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2160 | if (type1->tt_type != type2->tt_type) |
| 2161 | return FALSE; |
| 2162 | switch (type1->tt_type) |
| 2163 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2164 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2165 | case VAR_ANY: |
| 2166 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2167 | case VAR_SPECIAL: |
| 2168 | case VAR_BOOL: |
| 2169 | case VAR_NUMBER: |
| 2170 | case VAR_FLOAT: |
| 2171 | case VAR_STRING: |
| 2172 | case VAR_BLOB: |
| 2173 | case VAR_JOB: |
| 2174 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2175 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2176 | case VAR_LIST: |
| 2177 | case VAR_DICT: |
| 2178 | return equal_type(type1->tt_member, type2->tt_member); |
| 2179 | case VAR_FUNC: |
| 2180 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2181 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2182 | || type1->tt_argcount != type2->tt_argcount) |
| 2183 | return FALSE; |
| 2184 | if (type1->tt_argcount < 0 |
| 2185 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2186 | return TRUE; |
| 2187 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2188 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2189 | return FALSE; |
| 2190 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2191 | } |
| 2192 | return TRUE; |
| 2193 | } |
| 2194 | |
| 2195 | /* |
| 2196 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2197 | * "type2" and "dest" may be the same. |
| 2198 | */ |
| 2199 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2200 | 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] | 2201 | { |
| 2202 | if (equal_type(type1, type2)) |
| 2203 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2204 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2205 | return; |
| 2206 | } |
| 2207 | |
| 2208 | if (type1->tt_type == type2->tt_type) |
| 2209 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2210 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2211 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2212 | type_T *common; |
| 2213 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2214 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2215 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2216 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2217 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2218 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2219 | return; |
| 2220 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2221 | if (type1->tt_type == VAR_FUNC) |
| 2222 | { |
| 2223 | type_T *common; |
| 2224 | |
| 2225 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2226 | if (type1->tt_argcount == type2->tt_argcount |
| 2227 | && type1->tt_argcount >= 0) |
| 2228 | { |
| 2229 | int argcount = type1->tt_argcount; |
| 2230 | int i; |
| 2231 | |
| 2232 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2233 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2234 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2235 | if (func_type_add_arg_types(*dest, argcount, |
| 2236 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2237 | for (i = 0; i < argcount; ++i) |
| 2238 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2239 | &(*dest)->tt_args[i], type_gap); |
| 2240 | } |
| 2241 | } |
| 2242 | else |
| 2243 | *dest = alloc_func_type(common, -1, type_gap); |
| 2244 | return; |
| 2245 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2246 | } |
| 2247 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2248 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | char * |
| 2252 | vartype_name(vartype_T type) |
| 2253 | { |
| 2254 | switch (type) |
| 2255 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2256 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2257 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2258 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2259 | case VAR_SPECIAL: return "special"; |
| 2260 | case VAR_BOOL: return "bool"; |
| 2261 | case VAR_NUMBER: return "number"; |
| 2262 | case VAR_FLOAT: return "float"; |
| 2263 | case VAR_STRING: return "string"; |
| 2264 | case VAR_BLOB: return "blob"; |
| 2265 | case VAR_JOB: return "job"; |
| 2266 | case VAR_CHANNEL: return "channel"; |
| 2267 | case VAR_LIST: return "list"; |
| 2268 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2269 | |
| 2270 | case VAR_FUNC: |
| 2271 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2272 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2273 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | /* |
| 2277 | * Return the name of a type. |
| 2278 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2279 | */ |
| 2280 | char * |
| 2281 | type_name(type_T *type, char **tofree) |
| 2282 | { |
| 2283 | char *name = vartype_name(type->tt_type); |
| 2284 | |
| 2285 | *tofree = NULL; |
| 2286 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2287 | { |
| 2288 | char *member_free; |
| 2289 | char *member_name = type_name(type->tt_member, &member_free); |
| 2290 | size_t len; |
| 2291 | |
| 2292 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2293 | *tofree = alloc(len); |
| 2294 | if (*tofree != NULL) |
| 2295 | { |
| 2296 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2297 | vim_free(member_free); |
| 2298 | return *tofree; |
| 2299 | } |
| 2300 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2301 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2302 | { |
| 2303 | garray_T ga; |
| 2304 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2305 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2306 | |
| 2307 | ga_init2(&ga, 1, 100); |
| 2308 | if (ga_grow(&ga, 20) == FAIL) |
| 2309 | return "[unknown]"; |
| 2310 | *tofree = ga.ga_data; |
| 2311 | STRCPY(ga.ga_data, "func("); |
| 2312 | ga.ga_len += 5; |
| 2313 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2314 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2315 | { |
| 2316 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2317 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2318 | int len; |
| 2319 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2320 | if (type->tt_args == NULL) |
| 2321 | arg_type = "[unknown]"; |
| 2322 | else |
| 2323 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2324 | if (i > 0) |
| 2325 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2326 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2327 | ga.ga_len += 2; |
| 2328 | } |
| 2329 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2330 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2331 | { |
| 2332 | vim_free(arg_free); |
| 2333 | return "[unknown]"; |
| 2334 | } |
| 2335 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2336 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2337 | { |
| 2338 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2339 | ga.ga_len += 3; |
| 2340 | } |
| 2341 | else if (i >= type->tt_min_argcount) |
| 2342 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2343 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2344 | ga.ga_len += len; |
| 2345 | vim_free(arg_free); |
| 2346 | } |
| 2347 | |
| 2348 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2349 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2350 | else |
| 2351 | { |
| 2352 | char *ret_free; |
| 2353 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2354 | int len; |
| 2355 | |
| 2356 | len = (int)STRLEN(ret_name) + 4; |
| 2357 | if (ga_grow(&ga, len) == FAIL) |
| 2358 | { |
| 2359 | vim_free(ret_free); |
| 2360 | return "[unknown]"; |
| 2361 | } |
| 2362 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2363 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2364 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2365 | vim_free(ret_free); |
| 2366 | } |
| 2367 | return ga.ga_data; |
| 2368 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2369 | |
| 2370 | return name; |
| 2371 | } |
| 2372 | |
| 2373 | /* |
| 2374 | * Find "name" in script-local items of script "sid". |
| 2375 | * Returns the index in "sn_var_vals" if found. |
| 2376 | * If found but not in "sn_var_vals" returns -1. |
| 2377 | * If not found returns -2. |
| 2378 | */ |
| 2379 | int |
| 2380 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2381 | { |
| 2382 | hashtab_T *ht; |
| 2383 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2384 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2385 | int idx; |
| 2386 | |
| 2387 | // First look the name up in the hashtable. |
| 2388 | if (sid <= 0 || sid > script_items.ga_len) |
| 2389 | return -1; |
| 2390 | ht = &SCRIPT_VARS(sid); |
| 2391 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2392 | if (di == NULL) |
| 2393 | return -2; |
| 2394 | |
| 2395 | // Now find the svar_T index in sn_var_vals. |
| 2396 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2397 | { |
| 2398 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2399 | |
| 2400 | if (sv->sv_tv == &di->di_tv) |
| 2401 | { |
| 2402 | if (check_writable && sv->sv_const) |
| 2403 | semsg(_(e_readonlyvar), name); |
| 2404 | return idx; |
| 2405 | } |
| 2406 | } |
| 2407 | return -1; |
| 2408 | } |
| 2409 | |
| 2410 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2411 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2412 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2413 | */ |
| 2414 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2415 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2416 | { |
Bram Moolenaar | 086eb18 | 2020-07-01 16:00:44 +0200 | [diff] [blame] | 2417 | scriptitem_T *si; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2418 | int idx; |
| 2419 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2420 | if (current_sctx.sc_sid <= 0) |
| 2421 | return NULL; |
| 2422 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2423 | if (cctx != NULL) |
| 2424 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2425 | { |
| 2426 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2427 | + idx; |
| 2428 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2429 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2430 | : STRLEN(import->imp_name) == len |
| 2431 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2432 | return import; |
| 2433 | } |
| 2434 | |
| 2435 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2436 | { |
| 2437 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2438 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2439 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2440 | : STRLEN(import->imp_name) == len |
| 2441 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2442 | return import; |
| 2443 | } |
| 2444 | return NULL; |
| 2445 | } |
| 2446 | |
| 2447 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2448 | * Free all imported variables. |
| 2449 | */ |
| 2450 | static void |
| 2451 | free_imported(cctx_T *cctx) |
| 2452 | { |
| 2453 | int idx; |
| 2454 | |
| 2455 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2456 | { |
| 2457 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2458 | |
| 2459 | vim_free(import->imp_name); |
| 2460 | } |
| 2461 | ga_clear(&cctx->ctx_imports); |
| 2462 | } |
| 2463 | |
| 2464 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2465 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2466 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 2467 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2468 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2469 | { |
| 2470 | return p[0] == '#' && p[1] != '{'; |
| 2471 | } |
| 2472 | |
| 2473 | /* |
| 2474 | * Return a pointer to the next line that isn't empty or only contains a |
| 2475 | * comment. Skips over white space. |
| 2476 | * Returns NULL if there is none. |
| 2477 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2478 | char_u * |
| 2479 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2480 | { |
| 2481 | int lnum = cctx->ctx_lnum; |
| 2482 | |
| 2483 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2484 | { |
| 2485 | 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] | 2486 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2487 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 2488 | if (line == NULL) |
| 2489 | break; |
| 2490 | p = skipwhite(line); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2491 | if (*p != NUL && !vim9_comment_start(p)) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2492 | return p; |
| 2493 | } |
| 2494 | return NULL; |
| 2495 | } |
| 2496 | |
| 2497 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2498 | * Called when checking for a following operator at "arg". When the rest of |
| 2499 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2500 | * line return a pointer to it and set "nextp". |
| 2501 | * Otherwise skip over white space. |
| 2502 | */ |
| 2503 | static char_u * |
| 2504 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2505 | { |
| 2506 | char_u *p = skipwhite(arg); |
| 2507 | |
| 2508 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2509 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2510 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2511 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2512 | if (*nextp != NULL) |
| 2513 | return *nextp; |
| 2514 | } |
| 2515 | return p; |
| 2516 | } |
| 2517 | |
| 2518 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2519 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2520 | * 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] | 2521 | * Returns NULL when at the end. |
| 2522 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2523 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2524 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2525 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2526 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2527 | |
| 2528 | do |
| 2529 | { |
| 2530 | ++cctx->ctx_lnum; |
| 2531 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2532 | { |
| 2533 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2534 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2535 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2536 | 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] | 2537 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2538 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2539 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2540 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2541 | return line; |
| 2542 | } |
| 2543 | |
| 2544 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2545 | * 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] | 2546 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2547 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2548 | */ |
| 2549 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2550 | 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] | 2551 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2552 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2553 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2554 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2555 | |
| 2556 | if (next == NULL) |
| 2557 | return FAIL; |
| 2558 | *arg = skipwhite(next); |
| 2559 | } |
| 2560 | return OK; |
| 2561 | } |
| 2562 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2563 | /* |
| 2564 | * Idem, and give an error when failed. |
| 2565 | */ |
| 2566 | static int |
| 2567 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2568 | { |
| 2569 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2570 | { |
| 2571 | emsg(_("E1097: line incomplete")); |
| 2572 | return FAIL; |
| 2573 | } |
| 2574 | return OK; |
| 2575 | } |
| 2576 | |
| 2577 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2578 | // Structure passed between the compile_expr* functions to keep track of |
| 2579 | // constants that have been parsed but for which no code was produced yet. If |
| 2580 | // possible expressions on these constants are applied at compile time. If |
| 2581 | // that is not possible, the code to push the constants needs to be generated |
| 2582 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2583 | // Using 50 should be more than enough of 5 levels of (). |
| 2584 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2585 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2586 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2587 | int pp_used; // active entries in pp_tv[] |
| 2588 | } ppconst_T; |
| 2589 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2590 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2591 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2592 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2593 | /* |
| 2594 | * Generate a PUSH instruction for "tv". |
| 2595 | * "tv" will be consumed or cleared. |
| 2596 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2597 | */ |
| 2598 | static int |
| 2599 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2600 | { |
| 2601 | if (tv != NULL) |
| 2602 | { |
| 2603 | switch (tv->v_type) |
| 2604 | { |
| 2605 | case VAR_UNKNOWN: |
| 2606 | break; |
| 2607 | case VAR_BOOL: |
| 2608 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2609 | break; |
| 2610 | case VAR_SPECIAL: |
| 2611 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2612 | break; |
| 2613 | case VAR_NUMBER: |
| 2614 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2615 | break; |
| 2616 | #ifdef FEAT_FLOAT |
| 2617 | case VAR_FLOAT: |
| 2618 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2619 | break; |
| 2620 | #endif |
| 2621 | case VAR_BLOB: |
| 2622 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2623 | tv->vval.v_blob = NULL; |
| 2624 | break; |
| 2625 | case VAR_STRING: |
| 2626 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2627 | tv->vval.v_string = NULL; |
| 2628 | break; |
| 2629 | default: |
| 2630 | iemsg("constant type not supported"); |
| 2631 | clear_tv(tv); |
| 2632 | return FAIL; |
| 2633 | } |
| 2634 | tv->v_type = VAR_UNKNOWN; |
| 2635 | } |
| 2636 | return OK; |
| 2637 | } |
| 2638 | |
| 2639 | /* |
| 2640 | * Generate code for any ppconst entries. |
| 2641 | */ |
| 2642 | static int |
| 2643 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2644 | { |
| 2645 | int i; |
| 2646 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2647 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2648 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2649 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2650 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2651 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2652 | ret = FAIL; |
| 2653 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2654 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2655 | return ret; |
| 2656 | } |
| 2657 | |
| 2658 | /* |
| 2659 | * Clear ppconst constants. Used when failing. |
| 2660 | */ |
| 2661 | static void |
| 2662 | clear_ppconst(ppconst_T *ppconst) |
| 2663 | { |
| 2664 | int i; |
| 2665 | |
| 2666 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2667 | clear_tv(&ppconst->pp_tv[i]); |
| 2668 | ppconst->pp_used = 0; |
| 2669 | } |
| 2670 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2671 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2672 | * Generate an instruction to load script-local variable "name", without the |
| 2673 | * leading "s:". |
| 2674 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2675 | */ |
| 2676 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2677 | compile_load_scriptvar( |
| 2678 | cctx_T *cctx, |
| 2679 | char_u *name, // variable NUL terminated |
| 2680 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2681 | char_u **end, // end of variable |
| 2682 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2683 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2684 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2685 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2686 | imported_T *import; |
| 2687 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2688 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2689 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2690 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2691 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2692 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2693 | } |
| 2694 | if (idx >= 0) |
| 2695 | { |
| 2696 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2697 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2698 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2699 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2700 | return OK; |
| 2701 | } |
| 2702 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2703 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2704 | if (import != NULL) |
| 2705 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2706 | if (import->imp_all) |
| 2707 | { |
| 2708 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2709 | char_u *exp_name; |
| 2710 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2711 | ufunc_T *ufunc; |
| 2712 | type_T *type; |
| 2713 | |
| 2714 | // Used "import * as Name", need to lookup the member. |
| 2715 | if (*p != '.') |
| 2716 | { |
| 2717 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2718 | return FAIL; |
| 2719 | } |
| 2720 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2721 | if (VIM_ISWHITE(*p)) |
| 2722 | { |
| 2723 | emsg(_("E1074: no white space allowed after dot")); |
| 2724 | return FAIL; |
| 2725 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2726 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2727 | // isolate one name |
| 2728 | exp_name = p; |
| 2729 | while (eval_isnamec(*p)) |
| 2730 | ++p; |
| 2731 | cc = *p; |
| 2732 | *p = NUL; |
| 2733 | |
| 2734 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2735 | *p = cc; |
| 2736 | p = skipwhite(p); |
| 2737 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2738 | // TODO: what if it is a function? |
| 2739 | if (idx < 0) |
| 2740 | return FAIL; |
| 2741 | *end = p; |
| 2742 | |
| 2743 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2744 | import->imp_sid, |
| 2745 | idx, |
| 2746 | type); |
| 2747 | } |
| 2748 | else |
| 2749 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2750 | // TODO: check this is a variable, not a function? |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2751 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2752 | import->imp_sid, |
| 2753 | import->imp_var_vals_idx, |
| 2754 | import->imp_type); |
| 2755 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2756 | return OK; |
| 2757 | } |
| 2758 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2759 | if (error) |
| 2760 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2761 | return FAIL; |
| 2762 | } |
| 2763 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2764 | static int |
| 2765 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2766 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2767 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2768 | |
| 2769 | if (ufunc == NULL) |
| 2770 | return FAIL; |
| 2771 | |
Bram Moolenaar | fe465a0 | 2020-07-07 22:50:12 +0200 | [diff] [blame] | 2772 | return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name), |
| 2773 | ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2774 | } |
| 2775 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2776 | /* |
| 2777 | * Compile a variable name into a load instruction. |
| 2778 | * "end" points to just after the name. |
| 2779 | * When "error" is FALSE do not give an error when not found. |
| 2780 | */ |
| 2781 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2782 | 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] | 2783 | { |
| 2784 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2785 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2786 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2787 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2788 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2789 | |
| 2790 | if (*(*arg + 1) == ':') |
| 2791 | { |
| 2792 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2793 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2794 | { |
| 2795 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2796 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2797 | switch (**arg) |
| 2798 | { |
| 2799 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2800 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2801 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2802 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2803 | default: |
| 2804 | semsg(_(e_namespace), *arg); |
| 2805 | goto theend; |
| 2806 | } |
| 2807 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2808 | goto theend; |
| 2809 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2810 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2811 | else |
| 2812 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2813 | isntype_T isn_type = ISN_DROP; |
| 2814 | |
| 2815 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2816 | if (name == NULL) |
| 2817 | return FAIL; |
| 2818 | |
| 2819 | switch (**arg) |
| 2820 | { |
| 2821 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2822 | break; |
| 2823 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2824 | NULL, NULL, error); |
| 2825 | break; |
| 2826 | case 'g': isn_type = ISN_LOADG; break; |
| 2827 | case 'w': isn_type = ISN_LOADW; break; |
| 2828 | case 't': isn_type = ISN_LOADT; break; |
| 2829 | case 'b': isn_type = ISN_LOADB; break; |
| 2830 | default: semsg(_(e_namespace), *arg); |
| 2831 | goto theend; |
| 2832 | } |
| 2833 | if (isn_type != ISN_DROP) |
| 2834 | { |
| 2835 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2836 | // variables can be defined later, thus we don't check if it |
| 2837 | // exists, give error at runtime. |
| 2838 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2839 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | else |
| 2843 | { |
| 2844 | size_t len = end - *arg; |
| 2845 | int idx; |
| 2846 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2847 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2848 | |
| 2849 | name = vim_strnsave(*arg, end - *arg); |
| 2850 | if (name == NULL) |
| 2851 | return FAIL; |
| 2852 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2853 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2854 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2855 | if (!gen_load_outer) |
| 2856 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2857 | } |
| 2858 | else |
| 2859 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2860 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2861 | |
| 2862 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2863 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2864 | type = lvar->lv_type; |
| 2865 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2866 | if (lvar->lv_from_outer) |
| 2867 | gen_load_outer = TRUE; |
| 2868 | else |
| 2869 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2870 | } |
| 2871 | else |
| 2872 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2873 | // "var" can be script-local even without using "s:" if it |
| 2874 | // already exists. |
| 2875 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2876 | == SCRIPT_VERSION_VIM9 |
| 2877 | || lookup_script(*arg, len) == OK) |
| 2878 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2879 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2880 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2881 | // When the name starts with an uppercase letter or "x:" it |
| 2882 | // can be a user defined function. |
| 2883 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2884 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2885 | } |
| 2886 | } |
| 2887 | if (gen_load) |
| 2888 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2889 | if (gen_load_outer) |
| 2890 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2891 | } |
| 2892 | |
| 2893 | *arg = end; |
| 2894 | |
| 2895 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2896 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2897 | semsg(_(e_var_notfound), name); |
| 2898 | vim_free(name); |
| 2899 | return res; |
| 2900 | } |
| 2901 | |
| 2902 | /* |
| 2903 | * Compile the argument expressions. |
| 2904 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2905 | */ |
| 2906 | static int |
| 2907 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2908 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2909 | char_u *p = *arg; |
| 2910 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2911 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2912 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2913 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2914 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2915 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2916 | if (*p == ')') |
| 2917 | { |
| 2918 | *arg = p + 1; |
| 2919 | return OK; |
| 2920 | } |
| 2921 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2922 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2923 | return FAIL; |
| 2924 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2925 | |
| 2926 | if (*p != ',' && *skipwhite(p) == ',') |
| 2927 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2928 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2929 | p = skipwhite(p); |
| 2930 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2931 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2932 | { |
| 2933 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2934 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2935 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2936 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2937 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2938 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2939 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2940 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2941 | emsg(_(e_missing_close)); |
| 2942 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | /* |
| 2946 | * Compile a function call: name(arg1, arg2) |
| 2947 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2948 | * "argcount_init" is 1 for "value->method()" |
| 2949 | * Instructions: |
| 2950 | * EVAL arg1 |
| 2951 | * EVAL arg2 |
| 2952 | * BCALL / DCALL / UCALL |
| 2953 | */ |
| 2954 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2955 | compile_call( |
| 2956 | char_u **arg, |
| 2957 | size_t varlen, |
| 2958 | cctx_T *cctx, |
| 2959 | ppconst_T *ppconst, |
| 2960 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2961 | { |
| 2962 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2963 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2964 | int argcount = argcount_init; |
| 2965 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2966 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2967 | char_u *tofree = NULL; |
| 2968 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2969 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2970 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2971 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2972 | // we can evaluate "has('name')" at compile time |
| 2973 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2974 | { |
| 2975 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2976 | typval_T argvars[2]; |
| 2977 | |
| 2978 | argvars[0].v_type = VAR_UNKNOWN; |
| 2979 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2980 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2981 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2982 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2983 | s = skipwhite(s); |
| 2984 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 2985 | { |
| 2986 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2987 | |
| 2988 | *arg = s + 1; |
| 2989 | argvars[1].v_type = VAR_UNKNOWN; |
| 2990 | tv->v_type = VAR_NUMBER; |
| 2991 | tv->vval.v_number = 0; |
| 2992 | f_has(argvars, tv); |
| 2993 | clear_tv(&argvars[0]); |
| 2994 | ++ppconst->pp_used; |
| 2995 | return OK; |
| 2996 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2997 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3001 | return FAIL; |
| 3002 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3003 | if (varlen >= sizeof(namebuf)) |
| 3004 | { |
| 3005 | semsg(_("E1011: name too long: %s"), name); |
| 3006 | return FAIL; |
| 3007 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3008 | vim_strncpy(namebuf, *arg, varlen); |
| 3009 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3010 | |
| 3011 | *arg = skipwhite(*arg + varlen + 1); |
| 3012 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3013 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3014 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3015 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3016 | { |
| 3017 | int idx; |
| 3018 | |
| 3019 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3020 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3021 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 3022 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3023 | else |
| 3024 | semsg(_(e_unknownfunc), namebuf); |
| 3025 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3026 | } |
| 3027 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3028 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 3029 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3030 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3031 | { |
| 3032 | res = generate_CALL(cctx, ufunc, argcount); |
| 3033 | goto theend; |
| 3034 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3035 | |
| 3036 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3037 | // 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] | 3038 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3039 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 3040 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3041 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3042 | garray_T *stack = &cctx->ctx_type_stack; |
| 3043 | type_T *type; |
| 3044 | |
| 3045 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3046 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3047 | goto theend; |
| 3048 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3049 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3050 | // 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] | 3051 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3052 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 3053 | res = generate_UCALL(cctx, name, argcount); |
| 3054 | else |
| 3055 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3056 | |
| 3057 | theend: |
| 3058 | vim_free(tofree); |
| 3059 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3060 | } |
| 3061 | |
| 3062 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 3063 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 3064 | |
| 3065 | /* |
| 3066 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 3067 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3068 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3069 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 3070 | * valid name. |
| 3071 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3072 | static char_u * |
| 3073 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3074 | { |
| 3075 | char_u *p; |
| 3076 | |
| 3077 | // Quick check for valid starting character. |
| 3078 | if (!eval_isnamec1(*arg)) |
| 3079 | return arg; |
| 3080 | |
| 3081 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 3082 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 3083 | // and can be used in slice "[n:]". |
| 3084 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3085 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3086 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 3087 | break; |
| 3088 | return p; |
| 3089 | } |
| 3090 | |
| 3091 | /* |
| 3092 | * 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] | 3093 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3094 | */ |
| 3095 | char_u * |
| 3096 | to_name_const_end(char_u *arg) |
| 3097 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3098 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3099 | typval_T rettv; |
| 3100 | |
| 3101 | if (p == arg && *arg == '[') |
| 3102 | { |
| 3103 | |
| 3104 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3105 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3106 | p = arg; |
| 3107 | } |
| 3108 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 3109 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3110 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3111 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3112 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3113 | p = arg; |
| 3114 | } |
| 3115 | else if (p == arg && *arg == '{') |
| 3116 | { |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3117 | int ret = get_lambda_tv(&p, &rettv, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3118 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3119 | // Can be "{x -> ret}()". |
| 3120 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3121 | if (ret == NOTDONE) |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3122 | ret = eval_dict(&p, &rettv, NULL, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3123 | if (ret != OK) |
| 3124 | p = arg; |
| 3125 | } |
| 3126 | |
| 3127 | return p; |
| 3128 | } |
| 3129 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3130 | /* |
| 3131 | * parse a list: [expr, expr] |
| 3132 | * "*arg" points to the '['. |
| 3133 | */ |
| 3134 | static int |
| 3135 | compile_list(char_u **arg, cctx_T *cctx) |
| 3136 | { |
| 3137 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3138 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3139 | int count = 0; |
| 3140 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3141 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3142 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3143 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3144 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3145 | semsg(_(e_list_end), *arg); |
| 3146 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3147 | } |
| 3148 | if (*p == ']') |
| 3149 | { |
| 3150 | ++p; |
| 3151 | // Allow for following comment, after at least one space. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3152 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '#') |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3153 | p += STRLEN(p); |
| 3154 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3155 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3156 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3157 | break; |
| 3158 | ++count; |
| 3159 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3160 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3161 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3162 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 3163 | { |
| 3164 | semsg(_(e_white_after), ","); |
| 3165 | return FAIL; |
| 3166 | } |
| 3167 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3168 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3169 | p = skipwhite(p); |
| 3170 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3171 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3172 | |
| 3173 | generate_NEWLIST(cctx, count); |
| 3174 | return OK; |
| 3175 | } |
| 3176 | |
| 3177 | /* |
| 3178 | * parse a lambda: {arg, arg -> expr} |
| 3179 | * "*arg" points to the '{'. |
| 3180 | */ |
| 3181 | static int |
| 3182 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 3183 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3184 | typval_T rettv; |
| 3185 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3186 | evalarg_T evalarg; |
| 3187 | |
| 3188 | CLEAR_FIELD(evalarg); |
| 3189 | evalarg.eval_flags = EVAL_EVALUATE; |
| 3190 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3191 | |
| 3192 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3193 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3194 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3195 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3196 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3197 | ++ufunc->uf_refcount; |
| 3198 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3199 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3200 | |
| 3201 | // The function will have one line: "return {expr}". |
| 3202 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3203 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3204 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 3205 | clear_evalarg(&evalarg, NULL); |
| 3206 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 3207 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 3208 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3209 | |
| 3210 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3211 | return FAIL; |
| 3212 | } |
| 3213 | |
| 3214 | /* |
| 3215 | * Compile a lamda call: expr->{lambda}(args) |
| 3216 | * "arg" points to the "{". |
| 3217 | */ |
| 3218 | static int |
| 3219 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 3220 | { |
| 3221 | ufunc_T *ufunc; |
| 3222 | typval_T rettv; |
| 3223 | int argcount = 1; |
| 3224 | int ret = FAIL; |
| 3225 | |
| 3226 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3227 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3228 | return FAIL; |
| 3229 | |
| 3230 | if (**arg != '(') |
| 3231 | { |
| 3232 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 3233 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3234 | else |
| 3235 | semsg(_(e_missing_paren), "lambda"); |
| 3236 | clear_tv(&rettv); |
| 3237 | return FAIL; |
| 3238 | } |
| 3239 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3240 | ufunc = rettv.vval.v_partial->pt_func; |
| 3241 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3242 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3243 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3244 | |
| 3245 | // The function will have one line: "return {expr}". |
| 3246 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3247 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3248 | |
| 3249 | // compile the arguments |
| 3250 | *arg = skipwhite(*arg + 1); |
| 3251 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 3252 | // call the compiled function |
| 3253 | ret = generate_CALL(cctx, ufunc, argcount); |
| 3254 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3255 | if (ret == FAIL) |
| 3256 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3257 | return ret; |
| 3258 | } |
| 3259 | |
| 3260 | /* |
| 3261 | * parse a dict: {'key': val} or #{key: val} |
| 3262 | * "*arg" points to the '{'. |
| 3263 | */ |
| 3264 | static int |
| 3265 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 3266 | { |
| 3267 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3268 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3269 | int count = 0; |
| 3270 | dict_T *d = dict_alloc(); |
| 3271 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3272 | char_u *whitep = *arg; |
| 3273 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3274 | |
| 3275 | if (d == NULL) |
| 3276 | return FAIL; |
| 3277 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3278 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3279 | { |
| 3280 | char_u *key = NULL; |
| 3281 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3282 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3283 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3284 | *arg = NULL; |
| 3285 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | if (**arg == '}') |
| 3289 | break; |
| 3290 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3291 | if (literal) |
| 3292 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3293 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3294 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3295 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3296 | { |
| 3297 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 3298 | return FAIL; |
| 3299 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3300 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3301 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3302 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3303 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3304 | } |
| 3305 | else |
| 3306 | { |
| 3307 | isn_T *isn; |
| 3308 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3309 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3310 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3311 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3312 | if (isn->isn_type == ISN_PUSHS) |
| 3313 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3314 | else |
| 3315 | { |
| 3316 | type_T *keytype = ((type_T **)stack->ga_data) |
| 3317 | [stack->ga_len - 1]; |
| 3318 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 3319 | return FAIL; |
| 3320 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | // Check for duplicate keys, if using string keys. |
| 3324 | if (key != NULL) |
| 3325 | { |
| 3326 | item = dict_find(d, key, -1); |
| 3327 | if (item != NULL) |
| 3328 | { |
| 3329 | semsg(_(e_duplicate_key), key); |
| 3330 | goto failret; |
| 3331 | } |
| 3332 | item = dictitem_alloc(key); |
| 3333 | if (item != NULL) |
| 3334 | { |
| 3335 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3336 | item->di_tv.v_lock = 0; |
| 3337 | if (dict_add(d, item) == FAIL) |
| 3338 | dictitem_free(item); |
| 3339 | } |
| 3340 | } |
| 3341 | |
| 3342 | *arg = skipwhite(*arg); |
| 3343 | if (**arg != ':') |
| 3344 | { |
| 3345 | semsg(_(e_missing_dict_colon), *arg); |
| 3346 | return FAIL; |
| 3347 | } |
| 3348 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3349 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3350 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3351 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3352 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3353 | *arg = NULL; |
| 3354 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3355 | } |
| 3356 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3357 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3358 | return FAIL; |
| 3359 | ++count; |
| 3360 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3361 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3362 | *arg = skipwhite(*arg); |
| 3363 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3364 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3365 | *arg = NULL; |
| 3366 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3367 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3368 | if (**arg == '}') |
| 3369 | break; |
| 3370 | if (**arg != ',') |
| 3371 | { |
| 3372 | semsg(_(e_missing_dict_comma), *arg); |
| 3373 | goto failret; |
| 3374 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3375 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3376 | *arg = skipwhite(*arg + 1); |
| 3377 | } |
| 3378 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3379 | *arg = *arg + 1; |
| 3380 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3381 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3382 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3383 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3384 | *arg += STRLEN(*arg); |
| 3385 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3386 | dict_unref(d); |
| 3387 | return generate_NEWDICT(cctx, count); |
| 3388 | |
| 3389 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3390 | if (*arg == NULL) |
| 3391 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3392 | dict_unref(d); |
| 3393 | return FAIL; |
| 3394 | } |
| 3395 | |
| 3396 | /* |
| 3397 | * Compile "&option". |
| 3398 | */ |
| 3399 | static int |
| 3400 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3401 | { |
| 3402 | typval_T rettv; |
| 3403 | char_u *start = *arg; |
| 3404 | int ret; |
| 3405 | |
| 3406 | // parse the option and get the current value to get the type. |
| 3407 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3408 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3409 | if (ret == OK) |
| 3410 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3411 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3412 | char_u *name = vim_strnsave(start, *arg - start); |
| 3413 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3414 | |
| 3415 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3416 | vim_free(name); |
| 3417 | } |
| 3418 | clear_tv(&rettv); |
| 3419 | |
| 3420 | return ret; |
| 3421 | } |
| 3422 | |
| 3423 | /* |
| 3424 | * Compile "$VAR". |
| 3425 | */ |
| 3426 | static int |
| 3427 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3428 | { |
| 3429 | char_u *start = *arg; |
| 3430 | int len; |
| 3431 | int ret; |
| 3432 | char_u *name; |
| 3433 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3434 | ++*arg; |
| 3435 | len = get_env_len(arg); |
| 3436 | if (len == 0) |
| 3437 | { |
| 3438 | semsg(_(e_syntax_at), start - 1); |
| 3439 | return FAIL; |
| 3440 | } |
| 3441 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3442 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3443 | name = vim_strnsave(start, len + 1); |
| 3444 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3445 | vim_free(name); |
| 3446 | return ret; |
| 3447 | } |
| 3448 | |
| 3449 | /* |
| 3450 | * Compile "@r". |
| 3451 | */ |
| 3452 | static int |
| 3453 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3454 | { |
| 3455 | int ret; |
| 3456 | |
| 3457 | ++*arg; |
| 3458 | if (**arg == NUL) |
| 3459 | { |
| 3460 | semsg(_(e_syntax_at), *arg - 1); |
| 3461 | return FAIL; |
| 3462 | } |
| 3463 | if (!valid_yank_reg(**arg, TRUE)) |
| 3464 | { |
| 3465 | emsg_invreg(**arg); |
| 3466 | return FAIL; |
| 3467 | } |
| 3468 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3469 | ++*arg; |
| 3470 | return ret; |
| 3471 | } |
| 3472 | |
| 3473 | /* |
| 3474 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3475 | */ |
| 3476 | static int |
| 3477 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3478 | { |
| 3479 | char_u *p = end; |
| 3480 | |
| 3481 | // this works from end to start |
| 3482 | while (p > start) |
| 3483 | { |
| 3484 | --p; |
| 3485 | if (*p == '-' || *p == '+') |
| 3486 | { |
| 3487 | // only '-' has an effect, for '+' we only check the type |
| 3488 | #ifdef FEAT_FLOAT |
| 3489 | if (rettv->v_type == VAR_FLOAT) |
| 3490 | { |
| 3491 | if (*p == '-') |
| 3492 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3493 | } |
| 3494 | else |
| 3495 | #endif |
| 3496 | { |
| 3497 | varnumber_T val; |
| 3498 | int error = FALSE; |
| 3499 | |
| 3500 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3501 | // here |
| 3502 | if (check_not_string(rettv) == FAIL) |
| 3503 | return FAIL; |
| 3504 | val = tv_get_number_chk(rettv, &error); |
| 3505 | clear_tv(rettv); |
| 3506 | if (error) |
| 3507 | return FAIL; |
| 3508 | if (*p == '-') |
| 3509 | val = -val; |
| 3510 | rettv->v_type = VAR_NUMBER; |
| 3511 | rettv->vval.v_number = val; |
| 3512 | } |
| 3513 | } |
| 3514 | else |
| 3515 | { |
| 3516 | int v = tv2bool(rettv); |
| 3517 | |
| 3518 | // '!' is permissive in the type. |
| 3519 | clear_tv(rettv); |
| 3520 | rettv->v_type = VAR_BOOL; |
| 3521 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3522 | } |
| 3523 | } |
| 3524 | return OK; |
| 3525 | } |
| 3526 | |
| 3527 | /* |
| 3528 | * Recognize v: variables that are constants and set "rettv". |
| 3529 | */ |
| 3530 | static void |
| 3531 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3532 | { |
| 3533 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3534 | { |
| 3535 | rettv->v_type = VAR_BOOL; |
| 3536 | rettv->vval.v_number = VVAL_TRUE; |
| 3537 | *arg += 6; |
| 3538 | } |
| 3539 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3540 | { |
| 3541 | rettv->v_type = VAR_BOOL; |
| 3542 | rettv->vval.v_number = VVAL_FALSE; |
| 3543 | *arg += 7; |
| 3544 | } |
| 3545 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3546 | { |
| 3547 | rettv->v_type = VAR_SPECIAL; |
| 3548 | rettv->vval.v_number = VVAL_NULL; |
| 3549 | *arg += 6; |
| 3550 | } |
| 3551 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3552 | { |
| 3553 | rettv->v_type = VAR_SPECIAL; |
| 3554 | rettv->vval.v_number = VVAL_NONE; |
| 3555 | *arg += 6; |
| 3556 | } |
| 3557 | } |
| 3558 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3559 | static exptype_T |
| 3560 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3561 | { |
| 3562 | exptype_T type = EXPR_UNKNOWN; |
| 3563 | int i; |
| 3564 | |
| 3565 | switch (p[0]) |
| 3566 | { |
| 3567 | case '=': if (p[1] == '=') |
| 3568 | type = EXPR_EQUAL; |
| 3569 | else if (p[1] == '~') |
| 3570 | type = EXPR_MATCH; |
| 3571 | break; |
| 3572 | case '!': if (p[1] == '=') |
| 3573 | type = EXPR_NEQUAL; |
| 3574 | else if (p[1] == '~') |
| 3575 | type = EXPR_NOMATCH; |
| 3576 | break; |
| 3577 | case '>': if (p[1] != '=') |
| 3578 | { |
| 3579 | type = EXPR_GREATER; |
| 3580 | *len = 1; |
| 3581 | } |
| 3582 | else |
| 3583 | type = EXPR_GEQUAL; |
| 3584 | break; |
| 3585 | case '<': if (p[1] != '=') |
| 3586 | { |
| 3587 | type = EXPR_SMALLER; |
| 3588 | *len = 1; |
| 3589 | } |
| 3590 | else |
| 3591 | type = EXPR_SEQUAL; |
| 3592 | break; |
| 3593 | case 'i': if (p[1] == 's') |
| 3594 | { |
| 3595 | // "is" and "isnot"; but not a prefix of a name |
| 3596 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3597 | *len = 5; |
| 3598 | i = p[*len]; |
| 3599 | if (!isalnum(i) && i != '_') |
| 3600 | { |
| 3601 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3602 | *type_is = TRUE; |
| 3603 | } |
| 3604 | } |
| 3605 | break; |
| 3606 | } |
| 3607 | return type; |
| 3608 | } |
| 3609 | |
| 3610 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3611 | * Compile code to apply '-', '+' and '!'. |
| 3612 | */ |
| 3613 | static int |
| 3614 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3615 | { |
| 3616 | char_u *p = end; |
| 3617 | |
| 3618 | // this works from end to start |
| 3619 | while (p > start) |
| 3620 | { |
| 3621 | --p; |
| 3622 | if (*p == '-' || *p == '+') |
| 3623 | { |
| 3624 | int negate = *p == '-'; |
| 3625 | isn_T *isn; |
| 3626 | |
| 3627 | // TODO: check type |
| 3628 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3629 | { |
| 3630 | --p; |
| 3631 | if (*p == '-') |
| 3632 | negate = !negate; |
| 3633 | } |
| 3634 | // only '-' has an effect, for '+' we only check the type |
| 3635 | if (negate) |
| 3636 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3637 | else |
| 3638 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3639 | if (isn == NULL) |
| 3640 | return FAIL; |
| 3641 | } |
| 3642 | else |
| 3643 | { |
| 3644 | int invert = TRUE; |
| 3645 | |
| 3646 | while (p > start && p[-1] == '!') |
| 3647 | { |
| 3648 | --p; |
| 3649 | invert = !invert; |
| 3650 | } |
| 3651 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3652 | return FAIL; |
| 3653 | } |
| 3654 | } |
| 3655 | return OK; |
| 3656 | } |
| 3657 | |
| 3658 | /* |
| 3659 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3660 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3661 | */ |
| 3662 | static int |
| 3663 | compile_subscript( |
| 3664 | char_u **arg, |
| 3665 | cctx_T *cctx, |
| 3666 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3667 | char_u *end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3668 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3669 | { |
| 3670 | for (;;) |
| 3671 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3672 | char_u *p = skipwhite(*arg); |
| 3673 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3674 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3675 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3676 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3677 | |
| 3678 | // If a following line starts with "->{" or "->X" advance to that |
| 3679 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3680 | // Also if a following line starts with ".x". |
| 3681 | if (next != NULL && |
| 3682 | ((next[0] == '-' && next[1] == '>' |
| 3683 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
| 3684 | || (next[0] == '.' && ASCII_ISALPHA(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3685 | { |
| 3686 | next = next_line_from_context(cctx, TRUE); |
| 3687 | if (next == NULL) |
| 3688 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3689 | *arg = next; |
| 3690 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3691 | } |
| 3692 | } |
| 3693 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3694 | if (*p == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3695 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3696 | garray_T *stack = &cctx->ctx_type_stack; |
| 3697 | type_T *type; |
| 3698 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3699 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3700 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3701 | return FAIL; |
| 3702 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3703 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3704 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3705 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3706 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3707 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3708 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3709 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3710 | return FAIL; |
| 3711 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3712 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3713 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3714 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3715 | return FAIL; |
| 3716 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3717 | // something->method() |
| 3718 | // Apply the '!', '-' and '+' first: |
| 3719 | // -1.0->func() works like (-1.0)->func() |
| 3720 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3721 | return FAIL; |
| 3722 | *start_leader = end_leader; // don't apply again later |
| 3723 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3724 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3725 | *arg = skipwhite(p); |
| 3726 | if (may_get_next_line(p, arg, cctx) == FAIL) |
| 3727 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3728 | if (**arg == '{') |
| 3729 | { |
| 3730 | // lambda call: list->{lambda} |
| 3731 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3732 | return FAIL; |
| 3733 | } |
| 3734 | else |
| 3735 | { |
| 3736 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3737 | p = *arg; |
| 3738 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3739 | p += 2; |
| 3740 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3741 | ; |
| 3742 | if (*p != '(') |
| 3743 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3744 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3745 | return FAIL; |
| 3746 | } |
| 3747 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3748 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3749 | return FAIL; |
| 3750 | } |
| 3751 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3752 | else if (*p == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3753 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3754 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3755 | type_T **typep; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3756 | vartype_T vtype; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3757 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3758 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3759 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3760 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3761 | // TODO: blob index |
| 3762 | // TODO: more arguments |
| 3763 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3764 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3765 | return FAIL; |
| 3766 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3767 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3768 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3769 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3770 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3771 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3772 | return FAIL; |
| 3773 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3774 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3775 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3776 | if (**arg != ']') |
| 3777 | { |
| 3778 | emsg(_(e_missbrac)); |
| 3779 | return FAIL; |
| 3780 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3781 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3782 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3783 | // We can index a list and a dict. If we don't know the type |
| 3784 | // we can use the index value type. |
| 3785 | // TODO: If we don't know use an instruction to figure it out at |
| 3786 | // runtime. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3787 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3788 | vtype = (*typep)->tt_type; |
| 3789 | if (*typep == &t_any) |
| 3790 | { |
| 3791 | type_T *valtype = ((type_T **)stack->ga_data) |
| 3792 | [stack->ga_len - 1]; |
| 3793 | if (valtype == &t_string) |
| 3794 | vtype = VAR_DICT; |
| 3795 | } |
| 3796 | if (vtype == VAR_DICT) |
| 3797 | { |
| 3798 | if ((*typep)->tt_type == VAR_DICT) |
| 3799 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3800 | else |
| 3801 | { |
| 3802 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3803 | return FAIL; |
| 3804 | *typep = &t_any; |
| 3805 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3806 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3807 | return FAIL; |
| 3808 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3809 | return FAIL; |
| 3810 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3811 | else if (vtype == VAR_STRING) |
| 3812 | { |
| 3813 | *typep = &t_number; |
| 3814 | if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL) |
| 3815 | return FAIL; |
| 3816 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3817 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3818 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3819 | if ((*typep)->tt_type == VAR_LIST) |
| 3820 | *typep = (*typep)->tt_member; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3821 | if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3822 | return FAIL; |
| 3823 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3824 | else |
| 3825 | { |
| 3826 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3827 | return FAIL; |
| 3828 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3829 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3830 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3831 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3832 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3833 | return FAIL; |
| 3834 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3835 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3836 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3837 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3838 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3839 | p = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3840 | if (eval_isnamec1(*p)) |
| 3841 | while (eval_isnamec(*p)) |
| 3842 | MB_PTR_ADV(p); |
| 3843 | if (p == *arg) |
| 3844 | { |
| 3845 | semsg(_(e_syntax_at), *arg); |
| 3846 | return FAIL; |
| 3847 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3848 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3849 | return FAIL; |
| 3850 | *arg = p; |
| 3851 | } |
| 3852 | else |
| 3853 | break; |
| 3854 | } |
| 3855 | |
| 3856 | // TODO - see handle_subscript(): |
| 3857 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3858 | // Don't do this when "Func" is already a partial that was bound |
| 3859 | // explicitly (pt_auto is FALSE). |
| 3860 | |
| 3861 | return OK; |
| 3862 | } |
| 3863 | |
| 3864 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3865 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3866 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3867 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3868 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3869 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3870 | * |
| 3871 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3872 | */ |
| 3873 | |
| 3874 | /* |
| 3875 | * number number constant |
| 3876 | * 0zFFFFFFFF Blob constant |
| 3877 | * "string" string constant |
| 3878 | * 'string' literal string constant |
| 3879 | * &option-name option value |
| 3880 | * @r register contents |
| 3881 | * identifier variable value |
| 3882 | * function() function call |
| 3883 | * $VAR environment variable |
| 3884 | * (expression) nested expression |
| 3885 | * [expr, expr] List |
| 3886 | * {key: val, key: val} Dictionary |
| 3887 | * #{key: val, key: val} Dictionary with literal keys |
| 3888 | * |
| 3889 | * Also handle: |
| 3890 | * ! in front logical NOT |
| 3891 | * - in front unary minus |
| 3892 | * + in front unary plus (ignored) |
| 3893 | * trailing (arg) funcref/partial call |
| 3894 | * trailing [] subscript in String or List |
| 3895 | * trailing .name entry in Dictionary |
| 3896 | * trailing ->name() method call |
| 3897 | */ |
| 3898 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3899 | compile_expr7( |
| 3900 | char_u **arg, |
| 3901 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3902 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3903 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3904 | char_u *start_leader, *end_leader; |
| 3905 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3906 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3907 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3908 | |
| 3909 | /* |
| 3910 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3911 | */ |
| 3912 | start_leader = *arg; |
| 3913 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3914 | *arg = skipwhite(*arg + 1); |
| 3915 | end_leader = *arg; |
| 3916 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3917 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3918 | switch (**arg) |
| 3919 | { |
| 3920 | /* |
| 3921 | * Number constant. |
| 3922 | */ |
| 3923 | case '0': // also for blob starting with 0z |
| 3924 | case '1': |
| 3925 | case '2': |
| 3926 | case '3': |
| 3927 | case '4': |
| 3928 | case '5': |
| 3929 | case '6': |
| 3930 | case '7': |
| 3931 | case '8': |
| 3932 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3933 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3934 | return FAIL; |
| 3935 | break; |
| 3936 | |
| 3937 | /* |
| 3938 | * String constant: "string". |
| 3939 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3940 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3941 | return FAIL; |
| 3942 | break; |
| 3943 | |
| 3944 | /* |
| 3945 | * Literal string constant: 'str''ing'. |
| 3946 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3947 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3948 | return FAIL; |
| 3949 | break; |
| 3950 | |
| 3951 | /* |
| 3952 | * Constant Vim variable. |
| 3953 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3954 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3955 | ret = NOTDONE; |
| 3956 | break; |
| 3957 | |
| 3958 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3959 | * "true" constant |
| 3960 | */ |
| 3961 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3962 | && !eval_isnamec((*arg)[4])) |
| 3963 | { |
| 3964 | *arg += 4; |
| 3965 | rettv->v_type = VAR_BOOL; |
| 3966 | rettv->vval.v_number = VVAL_TRUE; |
| 3967 | } |
| 3968 | else |
| 3969 | ret = NOTDONE; |
| 3970 | break; |
| 3971 | |
| 3972 | /* |
| 3973 | * "false" constant |
| 3974 | */ |
| 3975 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3976 | && !eval_isnamec((*arg)[5])) |
| 3977 | { |
| 3978 | *arg += 5; |
| 3979 | rettv->v_type = VAR_BOOL; |
| 3980 | rettv->vval.v_number = VVAL_FALSE; |
| 3981 | } |
| 3982 | else |
| 3983 | ret = NOTDONE; |
| 3984 | break; |
| 3985 | |
| 3986 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3987 | * List: [expr, expr] |
| 3988 | */ |
| 3989 | case '[': ret = compile_list(arg, cctx); |
| 3990 | break; |
| 3991 | |
| 3992 | /* |
| 3993 | * Dictionary: #{key: val, key: val} |
| 3994 | */ |
| 3995 | case '#': if ((*arg)[1] == '{') |
| 3996 | { |
| 3997 | ++*arg; |
| 3998 | ret = compile_dict(arg, cctx, TRUE); |
| 3999 | } |
| 4000 | else |
| 4001 | ret = NOTDONE; |
| 4002 | break; |
| 4003 | |
| 4004 | /* |
| 4005 | * Lambda: {arg, arg -> expr} |
| 4006 | * Dictionary: {'key': val, 'key': val} |
| 4007 | */ |
| 4008 | case '{': { |
| 4009 | char_u *start = skipwhite(*arg + 1); |
| 4010 | |
| 4011 | // Find out what comes after the arguments. |
| 4012 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 4013 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4014 | if (ret != FAIL && *start == '>') |
| 4015 | ret = compile_lambda(arg, cctx); |
| 4016 | else |
| 4017 | ret = compile_dict(arg, cctx, FALSE); |
| 4018 | } |
| 4019 | break; |
| 4020 | |
| 4021 | /* |
| 4022 | * Option value: &name |
| 4023 | */ |
| 4024 | case '&': ret = compile_get_option(arg, cctx); |
| 4025 | break; |
| 4026 | |
| 4027 | /* |
| 4028 | * Environment variable: $VAR. |
| 4029 | */ |
| 4030 | case '$': ret = compile_get_env(arg, cctx); |
| 4031 | break; |
| 4032 | |
| 4033 | /* |
| 4034 | * Register contents: @r. |
| 4035 | */ |
| 4036 | case '@': ret = compile_get_register(arg, cctx); |
| 4037 | break; |
| 4038 | /* |
| 4039 | * nested expression: (expression). |
| 4040 | */ |
| 4041 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4042 | |
| 4043 | // recursive! |
| 4044 | if (ppconst->pp_used <= PPSIZE - 10) |
| 4045 | { |
| 4046 | ret = compile_expr1(arg, cctx, ppconst); |
| 4047 | } |
| 4048 | else |
| 4049 | { |
| 4050 | // Not enough space in ppconst, flush constants. |
| 4051 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4052 | return FAIL; |
| 4053 | ret = compile_expr0(arg, cctx); |
| 4054 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4055 | *arg = skipwhite(*arg); |
| 4056 | if (**arg == ')') |
| 4057 | ++*arg; |
| 4058 | else if (ret == OK) |
| 4059 | { |
| 4060 | emsg(_(e_missing_close)); |
| 4061 | ret = FAIL; |
| 4062 | } |
| 4063 | break; |
| 4064 | |
| 4065 | default: ret = NOTDONE; |
| 4066 | break; |
| 4067 | } |
| 4068 | if (ret == FAIL) |
| 4069 | return FAIL; |
| 4070 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4071 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4072 | { |
| 4073 | // apply the '!', '-' and '+' before the constant |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4074 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4075 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4076 | clear_tv(rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4077 | return FAIL; |
| 4078 | } |
| 4079 | start_leader = end_leader; // don't apply again below |
| 4080 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4081 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4082 | clear_tv(rettv); |
| 4083 | else |
| 4084 | // A constant expression can possibly be handled compile time, |
| 4085 | // return the value instead of generating code. |
| 4086 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4087 | } |
| 4088 | else if (ret == NOTDONE) |
| 4089 | { |
| 4090 | char_u *p; |
| 4091 | int r; |
| 4092 | |
| 4093 | if (!eval_isnamec1(**arg)) |
| 4094 | { |
| 4095 | semsg(_("E1015: Name expected: %s"), *arg); |
| 4096 | return FAIL; |
| 4097 | } |
| 4098 | |
| 4099 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4100 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4101 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4102 | { |
| 4103 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 4104 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4105 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4106 | { |
| 4107 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4108 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4109 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4110 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4111 | if (r == FAIL) |
| 4112 | return FAIL; |
| 4113 | } |
| 4114 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4115 | // Handle following "[]", ".member", etc. |
| 4116 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4117 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4118 | ppconst) == FAIL) |
| 4119 | return FAIL; |
| 4120 | if (ppconst->pp_used > 0) |
| 4121 | { |
| 4122 | // apply the '!', '-' and '+' before the constant |
| 4123 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4124 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
| 4125 | return FAIL; |
| 4126 | return OK; |
| 4127 | } |
| 4128 | if (compile_leader(cctx, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4129 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4130 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | /* |
| 4134 | * * number multiplication |
| 4135 | * / number division |
| 4136 | * % number modulo |
| 4137 | */ |
| 4138 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4139 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4140 | { |
| 4141 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4142 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4143 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4144 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4145 | // get the first expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4146 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4147 | return FAIL; |
| 4148 | |
| 4149 | /* |
| 4150 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4151 | */ |
| 4152 | for (;;) |
| 4153 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4154 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4155 | if (*op != '*' && *op != '/' && *op != '%') |
| 4156 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4157 | if (next != NULL) |
| 4158 | { |
| 4159 | *arg = next_line_from_context(cctx, TRUE); |
| 4160 | op = skipwhite(*arg); |
| 4161 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4162 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4163 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4164 | { |
| 4165 | char_u buf[3]; |
| 4166 | |
| 4167 | vim_strncpy(buf, op, 1); |
| 4168 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4169 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4170 | } |
| 4171 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4172 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4173 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4174 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4175 | // get the second expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4176 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4177 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4178 | |
| 4179 | if (ppconst->pp_used == ppconst_used + 2 |
| 4180 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4181 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4182 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4183 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4184 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4185 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4186 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4187 | // both are numbers: compute the result |
| 4188 | switch (*op) |
| 4189 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4190 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4191 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4192 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4193 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4194 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4195 | break; |
| 4196 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4197 | tv1->vval.v_number = res; |
| 4198 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4199 | } |
| 4200 | else |
| 4201 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4202 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4203 | generate_two_op(cctx, op); |
| 4204 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | return OK; |
| 4208 | } |
| 4209 | |
| 4210 | /* |
| 4211 | * + number addition |
| 4212 | * - number subtraction |
| 4213 | * .. string concatenation |
| 4214 | */ |
| 4215 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4216 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4217 | { |
| 4218 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4219 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4220 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4221 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4222 | |
| 4223 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4224 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4225 | return FAIL; |
| 4226 | |
| 4227 | /* |
| 4228 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4229 | */ |
| 4230 | for (;;) |
| 4231 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4232 | op = may_peek_next_line(cctx, *arg, &next); |
| 4233 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4234 | break; |
| 4235 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4236 | if (next != NULL) |
| 4237 | { |
| 4238 | *arg = next_line_from_context(cctx, TRUE); |
| 4239 | op = skipwhite(*arg); |
| 4240 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4241 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4242 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4243 | { |
| 4244 | char_u buf[3]; |
| 4245 | |
| 4246 | vim_strncpy(buf, op, oplen); |
| 4247 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4248 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4252 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4253 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4254 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4255 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4256 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4257 | return FAIL; |
| 4258 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4259 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4260 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4261 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4262 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4263 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4264 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4265 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4266 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4267 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4268 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4269 | // concat/subtract/add constant numbers |
| 4270 | if (*op == '+') |
| 4271 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4272 | else if (*op == '-') |
| 4273 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4274 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4275 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4276 | // concatenate constant strings |
| 4277 | char_u *s1 = tv1->vval.v_string; |
| 4278 | char_u *s2 = tv2->vval.v_string; |
| 4279 | size_t len1 = STRLEN(s1); |
| 4280 | |
| 4281 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4282 | if (tv1->vval.v_string == NULL) |
| 4283 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4284 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4285 | return FAIL; |
| 4286 | } |
| 4287 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4288 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4289 | vim_free(s1); |
| 4290 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4291 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4292 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4293 | } |
| 4294 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4295 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4296 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4297 | if (*op == '.') |
| 4298 | { |
| 4299 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4300 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4301 | return FAIL; |
| 4302 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4303 | } |
| 4304 | else |
| 4305 | generate_two_op(cctx, op); |
| 4306 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | return OK; |
| 4310 | } |
| 4311 | |
| 4312 | /* |
| 4313 | * expr5a == expr5b |
| 4314 | * expr5a =~ expr5b |
| 4315 | * expr5a != expr5b |
| 4316 | * expr5a !~ expr5b |
| 4317 | * expr5a > expr5b |
| 4318 | * expr5a >= expr5b |
| 4319 | * expr5a < expr5b |
| 4320 | * expr5a <= expr5b |
| 4321 | * expr5a is expr5b |
| 4322 | * expr5a isnot expr5b |
| 4323 | * |
| 4324 | * Produces instructions: |
| 4325 | * EVAL expr5a Push result of "expr5a" |
| 4326 | * EVAL expr5b Push result of "expr5b" |
| 4327 | * COMPARE one of the compare instructions |
| 4328 | */ |
| 4329 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4330 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4331 | { |
| 4332 | exptype_T type = EXPR_UNKNOWN; |
| 4333 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4334 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4335 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4336 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4337 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4338 | |
| 4339 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4340 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4341 | return FAIL; |
| 4342 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4343 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4344 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4345 | |
| 4346 | /* |
| 4347 | * If there is a comparative operator, use it. |
| 4348 | */ |
| 4349 | if (type != EXPR_UNKNOWN) |
| 4350 | { |
| 4351 | int ic = FALSE; // Default: do not ignore case |
| 4352 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4353 | if (next != NULL) |
| 4354 | { |
| 4355 | *arg = next_line_from_context(cctx, TRUE); |
| 4356 | p = skipwhite(*arg); |
| 4357 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4358 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4359 | { |
| 4360 | semsg(_(e_invexpr2), *arg); |
| 4361 | return FAIL; |
| 4362 | } |
| 4363 | // extra question mark appended: ignore case |
| 4364 | if (p[len] == '?') |
| 4365 | { |
| 4366 | ic = TRUE; |
| 4367 | ++len; |
| 4368 | } |
| 4369 | // extra '#' appended: match case (ignored) |
| 4370 | else if (p[len] == '#') |
| 4371 | ++len; |
| 4372 | // nothing appended: match case |
| 4373 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4374 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4375 | { |
| 4376 | char_u buf[7]; |
| 4377 | |
| 4378 | vim_strncpy(buf, p, len); |
| 4379 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4380 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | // get the second variable |
| 4384 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4385 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4386 | return FAIL; |
| 4387 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4388 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4389 | return FAIL; |
| 4390 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4391 | if (ppconst->pp_used == ppconst_used + 2) |
| 4392 | { |
| 4393 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4394 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4395 | int ret; |
| 4396 | |
| 4397 | // Both sides are a constant, compute the result now. |
| 4398 | // First check for a valid combination of types, this is more |
| 4399 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4400 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4401 | ret = FAIL; |
| 4402 | else |
| 4403 | { |
| 4404 | ret = typval_compare(tv1, tv2, type, ic); |
| 4405 | tv1->v_type = VAR_BOOL; |
| 4406 | tv1->vval.v_number = tv1->vval.v_number |
| 4407 | ? VVAL_TRUE : VVAL_FALSE; |
| 4408 | clear_tv(tv2); |
| 4409 | --ppconst->pp_used; |
| 4410 | } |
| 4411 | return ret; |
| 4412 | } |
| 4413 | |
| 4414 | generate_ppconst(cctx, ppconst); |
| 4415 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | return OK; |
| 4419 | } |
| 4420 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4421 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4422 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4423 | /* |
| 4424 | * Compile || or &&. |
| 4425 | */ |
| 4426 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4427 | compile_and_or( |
| 4428 | char_u **arg, |
| 4429 | cctx_T *cctx, |
| 4430 | char *op, |
| 4431 | ppconst_T *ppconst, |
| 4432 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4433 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4434 | char_u *next; |
| 4435 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4436 | int opchar = *op; |
| 4437 | |
| 4438 | if (p[0] == opchar && p[1] == opchar) |
| 4439 | { |
| 4440 | garray_T *instr = &cctx->ctx_instr; |
| 4441 | garray_T end_ga; |
| 4442 | |
| 4443 | /* |
| 4444 | * Repeat until there is no following "||" or "&&" |
| 4445 | */ |
| 4446 | ga_init2(&end_ga, sizeof(int), 10); |
| 4447 | while (p[0] == opchar && p[1] == opchar) |
| 4448 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4449 | if (next != NULL) |
| 4450 | { |
| 4451 | *arg = next_line_from_context(cctx, TRUE); |
| 4452 | p = skipwhite(*arg); |
| 4453 | } |
| 4454 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4455 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4456 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4457 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4458 | return FAIL; |
| 4459 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4460 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4461 | // TODO: use ppconst if the value is a constant |
| 4462 | generate_ppconst(cctx, ppconst); |
| 4463 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4464 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4465 | { |
| 4466 | ga_clear(&end_ga); |
| 4467 | return FAIL; |
| 4468 | } |
| 4469 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4470 | ++end_ga.ga_len; |
| 4471 | generate_JUMP(cctx, opchar == '|' |
| 4472 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 4473 | |
| 4474 | // eval the next expression |
| 4475 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4476 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4477 | return FAIL; |
| 4478 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4479 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4480 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4481 | { |
| 4482 | ga_clear(&end_ga); |
| 4483 | return FAIL; |
| 4484 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4485 | |
| 4486 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4487 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4488 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4489 | |
| 4490 | // Fill in the end label in all jumps. |
| 4491 | while (end_ga.ga_len > 0) |
| 4492 | { |
| 4493 | isn_T *isn; |
| 4494 | |
| 4495 | --end_ga.ga_len; |
| 4496 | isn = ((isn_T *)instr->ga_data) |
| 4497 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4498 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4499 | } |
| 4500 | ga_clear(&end_ga); |
| 4501 | } |
| 4502 | |
| 4503 | return OK; |
| 4504 | } |
| 4505 | |
| 4506 | /* |
| 4507 | * expr4a && expr4a && expr4a logical AND |
| 4508 | * |
| 4509 | * Produces instructions: |
| 4510 | * EVAL expr4a Push result of "expr4a" |
| 4511 | * JUMP_AND_KEEP_IF_FALSE end |
| 4512 | * EVAL expr4b Push result of "expr4b" |
| 4513 | * JUMP_AND_KEEP_IF_FALSE end |
| 4514 | * EVAL expr4c Push result of "expr4c" |
| 4515 | * end: |
| 4516 | */ |
| 4517 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4518 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4519 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4520 | int ppconst_used = ppconst->pp_used; |
| 4521 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4522 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4523 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4524 | return FAIL; |
| 4525 | |
| 4526 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4527 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | /* |
| 4531 | * expr3a || expr3b || expr3c logical OR |
| 4532 | * |
| 4533 | * Produces instructions: |
| 4534 | * EVAL expr3a Push result of "expr3a" |
| 4535 | * JUMP_AND_KEEP_IF_TRUE end |
| 4536 | * EVAL expr3b Push result of "expr3b" |
| 4537 | * JUMP_AND_KEEP_IF_TRUE end |
| 4538 | * EVAL expr3c Push result of "expr3c" |
| 4539 | * end: |
| 4540 | */ |
| 4541 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4542 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4543 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4544 | int ppconst_used = ppconst->pp_used; |
| 4545 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4546 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4547 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4548 | return FAIL; |
| 4549 | |
| 4550 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4551 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4552 | } |
| 4553 | |
| 4554 | /* |
| 4555 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4556 | * |
| 4557 | * Produces instructions: |
| 4558 | * EVAL expr2 Push result of "expr" |
| 4559 | * JUMP_IF_FALSE alt jump if false |
| 4560 | * EVAL expr1a |
| 4561 | * JUMP_ALWAYS end |
| 4562 | * alt: EVAL expr1b |
| 4563 | * end: |
| 4564 | */ |
| 4565 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4566 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4567 | { |
| 4568 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4569 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4570 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4571 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4572 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4573 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4574 | return FAIL; |
| 4575 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4576 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4577 | if (*p == '?') |
| 4578 | { |
| 4579 | garray_T *instr = &cctx->ctx_instr; |
| 4580 | garray_T *stack = &cctx->ctx_type_stack; |
| 4581 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4582 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4583 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4584 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4585 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4586 | int has_const_expr = FALSE; |
| 4587 | int const_value = FALSE; |
| 4588 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4589 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4590 | if (next != NULL) |
| 4591 | { |
| 4592 | *arg = next_line_from_context(cctx, TRUE); |
| 4593 | p = skipwhite(*arg); |
| 4594 | } |
| 4595 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4596 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4597 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4598 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4599 | return FAIL; |
| 4600 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4601 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4602 | if (ppconst->pp_used == ppconst_used + 1) |
| 4603 | { |
| 4604 | // the condition is a constant, we know whether the ? or the : |
| 4605 | // expression is to be evaluated. |
| 4606 | has_const_expr = TRUE; |
| 4607 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4608 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4609 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4610 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4611 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4612 | } |
| 4613 | else |
| 4614 | { |
| 4615 | generate_ppconst(cctx, ppconst); |
| 4616 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4617 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4618 | |
| 4619 | // evaluate the second expression; any type is accepted |
| 4620 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4621 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4622 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4623 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4624 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4625 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4626 | if (!has_const_expr) |
| 4627 | { |
| 4628 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4629 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4630 | // remember the type and drop it |
| 4631 | --stack->ga_len; |
| 4632 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4633 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4634 | end_idx = instr->ga_len; |
| 4635 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4636 | |
| 4637 | // jump here from JUMP_IF_FALSE |
| 4638 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4639 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4640 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4641 | |
| 4642 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4643 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4644 | if (*p != ':') |
| 4645 | { |
| 4646 | emsg(_(e_missing_colon)); |
| 4647 | return FAIL; |
| 4648 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4649 | if (next != NULL) |
| 4650 | { |
| 4651 | *arg = next_line_from_context(cctx, TRUE); |
| 4652 | p = skipwhite(*arg); |
| 4653 | } |
| 4654 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4655 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4656 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4657 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4658 | return FAIL; |
| 4659 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4660 | |
| 4661 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4662 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4663 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4664 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4665 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4666 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4667 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4668 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4669 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4670 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4671 | if (!has_const_expr) |
| 4672 | { |
| 4673 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4674 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4675 | // If the types differ, the result has a more generic type. |
| 4676 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4677 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4678 | |
| 4679 | // jump here from JUMP_ALWAYS |
| 4680 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4681 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4682 | } |
| 4683 | |
| 4684 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4685 | } |
| 4686 | return OK; |
| 4687 | } |
| 4688 | |
| 4689 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4690 | * Toplevel expression. |
| 4691 | */ |
| 4692 | static int |
| 4693 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4694 | { |
| 4695 | ppconst_T ppconst; |
| 4696 | |
| 4697 | CLEAR_FIELD(ppconst); |
| 4698 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4699 | { |
| 4700 | clear_ppconst(&ppconst); |
| 4701 | return FAIL; |
| 4702 | } |
| 4703 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4704 | return FAIL; |
| 4705 | return OK; |
| 4706 | } |
| 4707 | |
| 4708 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4709 | * compile "return [expr]" |
| 4710 | */ |
| 4711 | static char_u * |
| 4712 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4713 | { |
| 4714 | char_u *p = arg; |
| 4715 | garray_T *stack = &cctx->ctx_type_stack; |
| 4716 | type_T *stack_type; |
| 4717 | |
| 4718 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4719 | { |
| 4720 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4721 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4722 | return NULL; |
| 4723 | |
| 4724 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4725 | if (set_return_type) |
| 4726 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4727 | else |
| 4728 | { |
| 4729 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4730 | && stack_type->tt_type != VAR_VOID |
| 4731 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4732 | { |
| 4733 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4734 | return NULL; |
| 4735 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4736 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4737 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4738 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4739 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4740 | } |
| 4741 | else |
| 4742 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4743 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4744 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4745 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4746 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4747 | { |
| 4748 | emsg(_("E1003: Missing return value")); |
| 4749 | return NULL; |
| 4750 | } |
| 4751 | |
| 4752 | // No argument, return zero. |
| 4753 | generate_PUSHNR(cctx, 0); |
| 4754 | } |
| 4755 | |
| 4756 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4757 | return NULL; |
| 4758 | |
| 4759 | // "return val | endif" is possible |
| 4760 | return skipwhite(p); |
| 4761 | } |
| 4762 | |
| 4763 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4764 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4765 | * Return a pointer to the line in allocated memory. |
| 4766 | * Return NULL for end-of-file or some error. |
| 4767 | */ |
| 4768 | static char_u * |
| 4769 | exarg_getline( |
| 4770 | int c UNUSED, |
| 4771 | void *cookie, |
| 4772 | int indent UNUSED, |
| 4773 | int do_concat UNUSED) |
| 4774 | { |
| 4775 | cctx_T *cctx = (cctx_T *)cookie; |
| 4776 | |
| 4777 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4778 | { |
| 4779 | iemsg("Heredoc got to end"); |
| 4780 | return NULL; |
| 4781 | } |
| 4782 | ++cctx->ctx_lnum; |
| 4783 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4784 | [cctx->ctx_lnum]); |
| 4785 | } |
| 4786 | |
| 4787 | /* |
| 4788 | * Compile a nested :def command. |
| 4789 | */ |
| 4790 | static char_u * |
| 4791 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4792 | { |
| 4793 | char_u *name_start = eap->arg; |
| 4794 | char_u *name_end = to_name_end(eap->arg, FALSE); |
| 4795 | char_u *name = get_lambda_name(); |
| 4796 | lvar_T *lvar; |
| 4797 | ufunc_T *ufunc; |
| 4798 | |
| 4799 | eap->arg = name_end; |
| 4800 | eap->getline = exarg_getline; |
| 4801 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4802 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4803 | eap->forceit = FALSE; |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4804 | ufunc = def_function(eap, name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4805 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4806 | if (ufunc == NULL) |
| 4807 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4808 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4809 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4810 | return NULL; |
| 4811 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4812 | // Define a local variable for the function reference. |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4813 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4814 | TRUE, ufunc->uf_func_type); |
| 4815 | |
| 4816 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL |
| 4817 | || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL) |
| 4818 | return NULL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4819 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4820 | // TODO: warning for trailing text? |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4821 | return (char_u *)""; |
| 4822 | } |
| 4823 | |
| 4824 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4825 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4826 | */ |
| 4827 | int |
| 4828 | assignment_len(char_u *p, int *heredoc) |
| 4829 | { |
| 4830 | if (*p == '=') |
| 4831 | { |
| 4832 | if (p[1] == '<' && p[2] == '<') |
| 4833 | { |
| 4834 | *heredoc = TRUE; |
| 4835 | return 3; |
| 4836 | } |
| 4837 | return 1; |
| 4838 | } |
| 4839 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4840 | return 2; |
| 4841 | if (STRNCMP(p, "..=", 3) == 0) |
| 4842 | return 3; |
| 4843 | return 0; |
| 4844 | } |
| 4845 | |
| 4846 | // words that cannot be used as a variable |
| 4847 | static char *reserved[] = { |
| 4848 | "true", |
| 4849 | "false", |
| 4850 | NULL |
| 4851 | }; |
| 4852 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4853 | typedef enum { |
| 4854 | dest_local, |
| 4855 | dest_option, |
| 4856 | dest_env, |
| 4857 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4858 | dest_buffer, |
| 4859 | dest_window, |
| 4860 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4861 | dest_vimvar, |
| 4862 | dest_script, |
| 4863 | dest_reg, |
| 4864 | } assign_dest_T; |
| 4865 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4866 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4867 | * Generate the load instruction for "name". |
| 4868 | */ |
| 4869 | static void |
| 4870 | generate_loadvar( |
| 4871 | cctx_T *cctx, |
| 4872 | assign_dest_T dest, |
| 4873 | char_u *name, |
| 4874 | lvar_T *lvar, |
| 4875 | type_T *type) |
| 4876 | { |
| 4877 | switch (dest) |
| 4878 | { |
| 4879 | case dest_option: |
| 4880 | // TODO: check the option exists |
| 4881 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4882 | break; |
| 4883 | case dest_global: |
| 4884 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4885 | break; |
| 4886 | case dest_buffer: |
| 4887 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4888 | break; |
| 4889 | case dest_window: |
| 4890 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4891 | break; |
| 4892 | case dest_tab: |
| 4893 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4894 | break; |
| 4895 | case dest_script: |
| 4896 | compile_load_scriptvar(cctx, |
| 4897 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4898 | break; |
| 4899 | case dest_env: |
| 4900 | // Include $ in the name here |
| 4901 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4902 | break; |
| 4903 | case dest_reg: |
| 4904 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4905 | break; |
| 4906 | case dest_vimvar: |
| 4907 | generate_LOADV(cctx, name + 2, TRUE); |
| 4908 | break; |
| 4909 | case dest_local: |
| 4910 | if (lvar->lv_from_outer) |
| 4911 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4912 | NULL, type); |
| 4913 | else |
| 4914 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4915 | break; |
| 4916 | } |
| 4917 | } |
| 4918 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4919 | void |
| 4920 | vim9_declare_error(char_u *name) |
| 4921 | { |
| 4922 | char *scope = ""; |
| 4923 | |
| 4924 | switch (*name) |
| 4925 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4926 | case 'g': scope = _("global"); break; |
| 4927 | case 'b': scope = _("buffer"); break; |
| 4928 | case 'w': scope = _("window"); break; |
| 4929 | case 't': scope = _("tab"); break; |
| 4930 | case 'v': scope = "v:"; break; |
| 4931 | case '$': semsg(_(e_declare_env_var), name); return; |
| 4932 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4933 | } |
| 4934 | semsg(_(e_declare_var), scope, name); |
| 4935 | } |
| 4936 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4937 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4938 | * Compile declaration and assignment: |
| 4939 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4940 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4941 | * Return NULL for an error. |
| 4942 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4943 | */ |
| 4944 | static char_u * |
| 4945 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4946 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4947 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4948 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4949 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4950 | char_u *ret = NULL; |
| 4951 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4952 | int var_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4953 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4954 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4955 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4956 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4957 | int oplen = 0; |
| 4958 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4959 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4960 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4961 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4962 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4963 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4964 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4965 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 4966 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 4967 | if (p == NULL) |
| 4968 | return *arg == '[' ? arg : NULL; |
| 4969 | |
| 4970 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4971 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 4972 | // TODO: should we allow this, and figure out type inference from list |
| 4973 | // members? |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4974 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4975 | return NULL; |
| 4976 | } |
| 4977 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4978 | sp = p; |
| 4979 | p = skipwhite(p); |
| 4980 | op = p; |
| 4981 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4982 | |
| 4983 | if (var_count > 0 && oplen == 0) |
| 4984 | // can be something like "[1, 2]->func()" |
| 4985 | return arg; |
| 4986 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4987 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4988 | { |
| 4989 | char_u buf[4]; |
| 4990 | |
| 4991 | vim_strncpy(buf, op, oplen); |
| 4992 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4993 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4994 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4995 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4996 | if (heredoc) |
| 4997 | { |
| 4998 | list_T *l; |
| 4999 | listitem_T *li; |
| 5000 | |
| 5001 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5002 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5003 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 5004 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5005 | |
| 5006 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 5007 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5008 | { |
| 5009 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 5010 | li->li_tv.vval.v_string = NULL; |
| 5011 | } |
| 5012 | generate_NEWLIST(cctx, l->lv_len); |
| 5013 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5014 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5015 | list_free(l); |
| 5016 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5017 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5018 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5019 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5020 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5021 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 5022 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5023 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5024 | p = skipwhite(op + oplen); |
| 5025 | if (compile_expr0(&p, cctx) == FAIL) |
| 5026 | return NULL; |
| 5027 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5028 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5029 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5030 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5031 | type_T *stacktype; |
| 5032 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5033 | stacktype = stack->ga_len == 0 ? &t_void |
| 5034 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5035 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5036 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5037 | emsg(_(e_cannot_use_void)); |
| 5038 | goto theend; |
| 5039 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5040 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5041 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5042 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5043 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 5044 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5045 | } |
| 5046 | } |
| 5047 | |
| 5048 | /* |
| 5049 | * Loop over variables in "[var, var] = expr". |
| 5050 | * For "var = expr" and "let var: type" this is done only once. |
| 5051 | */ |
| 5052 | if (var_count > 0) |
| 5053 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 5054 | else |
| 5055 | var_start = arg; |
| 5056 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 5057 | { |
| 5058 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 5059 | size_t varlen; |
| 5060 | int new_local = FALSE; |
| 5061 | int opt_type; |
| 5062 | int opt_flags = 0; |
| 5063 | assign_dest_T dest = dest_local; |
| 5064 | int vimvaridx = -1; |
| 5065 | lvar_T *lvar = NULL; |
| 5066 | lvar_T arg_lvar; |
| 5067 | int has_type = FALSE; |
| 5068 | int has_index = FALSE; |
| 5069 | int instr_count = -1; |
| 5070 | |
| 5071 | p = (*var_start == '&' || *var_start == '$' |
| 5072 | || *var_start == '@') ? var_start + 1 : var_start; |
| 5073 | p = to_name_end(p, TRUE); |
| 5074 | |
| 5075 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 5076 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 5077 | --var_end; |
| 5078 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 5079 | --p; |
| 5080 | |
| 5081 | varlen = p - var_start; |
| 5082 | vim_free(name); |
| 5083 | name = vim_strnsave(var_start, varlen); |
| 5084 | if (name == NULL) |
| 5085 | return NULL; |
| 5086 | if (!heredoc) |
| 5087 | type = &t_any; |
| 5088 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5089 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5090 | { |
| 5091 | if (*var_start == '&') |
| 5092 | { |
| 5093 | int cc; |
| 5094 | long numval; |
| 5095 | |
| 5096 | dest = dest_option; |
| 5097 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5098 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5099 | emsg(_(e_const_option)); |
| 5100 | goto theend; |
| 5101 | } |
| 5102 | if (is_decl) |
| 5103 | { |
| 5104 | semsg(_("E1052: Cannot declare an option: %s"), var_start); |
| 5105 | goto theend; |
| 5106 | } |
| 5107 | p = var_start; |
| 5108 | p = find_option_end(&p, &opt_flags); |
| 5109 | if (p == NULL) |
| 5110 | { |
| 5111 | // cannot happen? |
| 5112 | emsg(_(e_letunexp)); |
| 5113 | goto theend; |
| 5114 | } |
| 5115 | cc = *p; |
| 5116 | *p = NUL; |
| 5117 | opt_type = get_option_value(var_start + 1, &numval, |
| 5118 | NULL, opt_flags); |
| 5119 | *p = cc; |
| 5120 | if (opt_type == -3) |
| 5121 | { |
| 5122 | semsg(_(e_unknown_option), var_start); |
| 5123 | goto theend; |
| 5124 | } |
| 5125 | if (opt_type == -2 || opt_type == 0) |
| 5126 | type = &t_string; |
| 5127 | else |
| 5128 | type = &t_number; // both number and boolean option |
| 5129 | } |
| 5130 | else if (*var_start == '$') |
| 5131 | { |
| 5132 | dest = dest_env; |
| 5133 | type = &t_string; |
| 5134 | if (is_decl) |
| 5135 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5136 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5137 | goto theend; |
| 5138 | } |
| 5139 | } |
| 5140 | else if (*var_start == '@') |
| 5141 | { |
| 5142 | if (!valid_yank_reg(var_start[1], TRUE)) |
| 5143 | { |
| 5144 | emsg_invreg(var_start[1]); |
| 5145 | goto theend; |
| 5146 | } |
| 5147 | dest = dest_reg; |
| 5148 | type = &t_string; |
| 5149 | if (is_decl) |
| 5150 | { |
| 5151 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 5152 | goto theend; |
| 5153 | } |
| 5154 | } |
| 5155 | else if (STRNCMP(var_start, "g:", 2) == 0) |
| 5156 | { |
| 5157 | dest = dest_global; |
| 5158 | if (is_decl) |
| 5159 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5160 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5161 | goto theend; |
| 5162 | } |
| 5163 | } |
| 5164 | else if (STRNCMP(var_start, "b:", 2) == 0) |
| 5165 | { |
| 5166 | dest = dest_buffer; |
| 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, "w:", 2) == 0) |
| 5174 | { |
| 5175 | dest = dest_window; |
| 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, "t:", 2) == 0) |
| 5183 | { |
| 5184 | dest = dest_tab; |
| 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, "v:", 2) == 0) |
| 5192 | { |
| 5193 | typval_T *vtv; |
| 5194 | int di_flags; |
| 5195 | |
| 5196 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5197 | if (vimvaridx < 0) |
| 5198 | { |
| 5199 | semsg(_(e_var_notfound), var_start); |
| 5200 | goto theend; |
| 5201 | } |
| 5202 | // We use the current value of "sandbox" here, is that OK? |
| 5203 | if (var_check_ro(di_flags, name, FALSE)) |
| 5204 | goto theend; |
| 5205 | dest = dest_vimvar; |
| 5206 | vtv = get_vim_var_tv(vimvaridx); |
| 5207 | type = typval2type(vtv); |
| 5208 | if (is_decl) |
| 5209 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5210 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5211 | goto theend; |
| 5212 | } |
| 5213 | } |
| 5214 | else |
| 5215 | { |
| 5216 | int idx; |
| 5217 | |
| 5218 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5219 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5220 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5221 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5222 | goto theend; |
| 5223 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5224 | |
| 5225 | lvar = lookup_local(var_start, varlen, cctx); |
| 5226 | if (lvar == NULL) |
| 5227 | { |
| 5228 | CLEAR_FIELD(arg_lvar); |
| 5229 | if (lookup_arg(var_start, varlen, |
| 5230 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 5231 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5232 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5233 | if (is_decl) |
| 5234 | { |
| 5235 | semsg(_(e_used_as_arg), name); |
| 5236 | goto theend; |
| 5237 | } |
| 5238 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5239 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5240 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5241 | if (lvar != NULL) |
| 5242 | { |
| 5243 | if (is_decl) |
| 5244 | { |
| 5245 | semsg(_("E1017: Variable already declared: %s"), name); |
| 5246 | goto theend; |
| 5247 | } |
| 5248 | else if (lvar->lv_const) |
| 5249 | { |
| 5250 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 5251 | name); |
| 5252 | goto theend; |
| 5253 | } |
| 5254 | } |
| 5255 | else if (STRNCMP(var_start, "s:", 2) == 0 |
| 5256 | || lookup_script(var_start, varlen) == OK |
| 5257 | || find_imported(var_start, varlen, cctx) != NULL) |
| 5258 | { |
| 5259 | dest = dest_script; |
| 5260 | if (is_decl) |
| 5261 | { |
| 5262 | semsg(_("E1054: Variable already declared in the script: %s"), |
| 5263 | name); |
| 5264 | goto theend; |
| 5265 | } |
| 5266 | } |
| 5267 | else if (name[1] == ':' && name[2] != NUL) |
| 5268 | { |
| 5269 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
| 5270 | name); |
| 5271 | goto theend; |
| 5272 | } |
| 5273 | else if (!is_decl) |
| 5274 | { |
| 5275 | semsg(_("E1089: unknown variable: %s"), name); |
| 5276 | goto theend; |
| 5277 | } |
| 5278 | } |
| 5279 | } |
| 5280 | |
| 5281 | // handle "a:name" as a name, not index "name" on "a" |
| 5282 | if (varlen > 1 || var_start[varlen] != ':') |
| 5283 | p = var_end; |
| 5284 | |
| 5285 | if (dest != dest_option) |
| 5286 | { |
| 5287 | if (is_decl && *p == ':') |
| 5288 | { |
| 5289 | // parse optional type: "let var: type = expr" |
| 5290 | if (!VIM_ISWHITE(p[1])) |
| 5291 | { |
| 5292 | semsg(_(e_white_after), ":"); |
| 5293 | goto theend; |
| 5294 | } |
| 5295 | p = skipwhite(p + 1); |
| 5296 | type = parse_type(&p, cctx->ctx_type_list); |
| 5297 | has_type = TRUE; |
| 5298 | } |
| 5299 | else if (lvar != NULL) |
| 5300 | type = lvar->lv_type; |
| 5301 | } |
| 5302 | |
| 5303 | if (oplen == 3 && !heredoc && dest != dest_global |
| 5304 | && type->tt_type != VAR_STRING |
| 5305 | && type->tt_type != VAR_ANY) |
| 5306 | { |
| 5307 | emsg(_("E1019: Can only concatenate to string")); |
| 5308 | goto theend; |
| 5309 | } |
| 5310 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5311 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5312 | { |
| 5313 | if (oplen > 1 && !heredoc) |
| 5314 | { |
| 5315 | // +=, /=, etc. require an existing variable |
| 5316 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 5317 | name); |
| 5318 | goto theend; |
| 5319 | } |
| 5320 | |
| 5321 | // new local variable |
| 5322 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
| 5323 | goto theend; |
| 5324 | lvar = reserve_local(cctx, var_start, varlen, |
| 5325 | cmdidx == CMD_const, type); |
| 5326 | if (lvar == NULL) |
| 5327 | goto theend; |
| 5328 | new_local = TRUE; |
| 5329 | } |
| 5330 | |
| 5331 | member_type = type; |
| 5332 | if (var_end > var_start + varlen) |
| 5333 | { |
| 5334 | // Something follows after the variable: "var[idx]". |
| 5335 | if (is_decl) |
| 5336 | { |
| 5337 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 5338 | goto theend; |
| 5339 | } |
| 5340 | |
| 5341 | if (var_start[varlen] == '[') |
| 5342 | { |
| 5343 | has_index = TRUE; |
| 5344 | if (type->tt_member == NULL) |
| 5345 | { |
| 5346 | semsg(_("E1088: cannot use an index on %s"), name); |
| 5347 | goto theend; |
| 5348 | } |
| 5349 | member_type = type->tt_member; |
| 5350 | } |
| 5351 | else |
| 5352 | { |
| 5353 | semsg("Not supported yet: %s", var_start); |
| 5354 | goto theend; |
| 5355 | } |
| 5356 | } |
| 5357 | else if (lvar == &arg_lvar) |
| 5358 | { |
| 5359 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 5360 | goto theend; |
| 5361 | } |
| 5362 | |
| 5363 | if (!heredoc) |
| 5364 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5365 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5366 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5367 | if (oplen > 0 && var_count == 0) |
| 5368 | { |
| 5369 | // skip over the "=" and the expression |
| 5370 | p = skipwhite(op + oplen); |
| 5371 | compile_expr0(&p, cctx); |
| 5372 | } |
| 5373 | } |
| 5374 | else if (oplen > 0) |
| 5375 | { |
| 5376 | type_T *stacktype; |
| 5377 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5378 | // For "var = expr" evaluate the expression. |
| 5379 | if (var_count == 0) |
| 5380 | { |
| 5381 | int r; |
| 5382 | |
| 5383 | // for "+=", "*=", "..=" etc. first load the current value |
| 5384 | if (*op != '=') |
| 5385 | { |
| 5386 | generate_loadvar(cctx, dest, name, lvar, type); |
| 5387 | |
| 5388 | if (has_index) |
| 5389 | { |
| 5390 | // TODO: get member from list or dict |
| 5391 | emsg("Index with operation not supported yet"); |
| 5392 | goto theend; |
| 5393 | } |
| 5394 | } |
| 5395 | |
| 5396 | // Compile the expression. Temporarily hide the new local |
| 5397 | // variable here, it is not available to this expression. |
| 5398 | if (new_local) |
| 5399 | --cctx->ctx_locals.ga_len; |
| 5400 | instr_count = instr->ga_len; |
| 5401 | p = skipwhite(op + oplen); |
| 5402 | r = compile_expr0(&p, cctx); |
| 5403 | if (new_local) |
| 5404 | ++cctx->ctx_locals.ga_len; |
| 5405 | if (r == FAIL) |
| 5406 | goto theend; |
| 5407 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5408 | else if (semicolon && var_idx == var_count - 1) |
| 5409 | { |
| 5410 | // For "[var; var] = expr" get the rest of the list |
| 5411 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5412 | goto theend; |
| 5413 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5414 | else |
| 5415 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5416 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5417 | // list. |
| 5418 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 5419 | return FAIL; |
| 5420 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5421 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5422 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5423 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5424 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5425 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5426 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5427 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5428 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5429 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5430 | emsg(_(e_cannot_use_void)); |
| 5431 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5432 | } |
| 5433 | else |
| 5434 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5435 | // An empty list or dict has a &t_void member, |
| 5436 | // for a variable that implies &t_any. |
| 5437 | if (stacktype == &t_list_empty) |
| 5438 | lvar->lv_type = &t_list_any; |
| 5439 | else if (stacktype == &t_dict_empty) |
| 5440 | lvar->lv_type = &t_dict_any; |
| 5441 | else |
| 5442 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5443 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5444 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5445 | else |
| 5446 | { |
| 5447 | type_T *use_type = lvar->lv_type; |
| 5448 | |
| 5449 | if (has_index) |
| 5450 | { |
| 5451 | use_type = use_type->tt_member; |
| 5452 | if (use_type == NULL) |
| 5453 | use_type = &t_void; |
| 5454 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5455 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5456 | == FAIL) |
| 5457 | goto theend; |
| 5458 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5459 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5460 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5461 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5462 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5463 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5464 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5465 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5466 | emsg(_(e_const_req_value)); |
| 5467 | goto theend; |
| 5468 | } |
| 5469 | else if (!has_type || dest == dest_option) |
| 5470 | { |
| 5471 | emsg(_(e_type_req)); |
| 5472 | goto theend; |
| 5473 | } |
| 5474 | else |
| 5475 | { |
| 5476 | // variables are always initialized |
| 5477 | if (ga_grow(instr, 1) == FAIL) |
| 5478 | goto theend; |
| 5479 | switch (member_type->tt_type) |
| 5480 | { |
| 5481 | case VAR_BOOL: |
| 5482 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5483 | break; |
| 5484 | case VAR_FLOAT: |
| 5485 | #ifdef FEAT_FLOAT |
| 5486 | generate_PUSHF(cctx, 0.0); |
| 5487 | #endif |
| 5488 | break; |
| 5489 | case VAR_STRING: |
| 5490 | generate_PUSHS(cctx, NULL); |
| 5491 | break; |
| 5492 | case VAR_BLOB: |
| 5493 | generate_PUSHBLOB(cctx, NULL); |
| 5494 | break; |
| 5495 | case VAR_FUNC: |
| 5496 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5497 | break; |
| 5498 | case VAR_LIST: |
| 5499 | generate_NEWLIST(cctx, 0); |
| 5500 | break; |
| 5501 | case VAR_DICT: |
| 5502 | generate_NEWDICT(cctx, 0); |
| 5503 | break; |
| 5504 | case VAR_JOB: |
| 5505 | generate_PUSHJOB(cctx, NULL); |
| 5506 | break; |
| 5507 | case VAR_CHANNEL: |
| 5508 | generate_PUSHCHANNEL(cctx, NULL); |
| 5509 | break; |
| 5510 | case VAR_NUMBER: |
| 5511 | case VAR_UNKNOWN: |
| 5512 | case VAR_ANY: |
| 5513 | case VAR_PARTIAL: |
| 5514 | case VAR_VOID: |
| 5515 | case VAR_SPECIAL: // cannot happen |
| 5516 | generate_PUSHNR(cctx, 0); |
| 5517 | break; |
| 5518 | } |
| 5519 | } |
| 5520 | if (var_count == 0) |
| 5521 | end = p; |
| 5522 | } |
| 5523 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5524 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5525 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5526 | break; |
| 5527 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5528 | if (oplen > 0 && *op != '=') |
| 5529 | { |
| 5530 | type_T *expected = &t_number; |
| 5531 | type_T *stacktype; |
| 5532 | |
| 5533 | // TODO: if type is known use float or any operation |
| 5534 | |
| 5535 | if (*op == '.') |
| 5536 | expected = &t_string; |
| 5537 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5538 | if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5539 | goto theend; |
| 5540 | |
| 5541 | if (*op == '.') |
| 5542 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 5543 | else |
| 5544 | { |
| 5545 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 5546 | |
| 5547 | if (isn == NULL) |
| 5548 | goto theend; |
| 5549 | switch (*op) |
| 5550 | { |
| 5551 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 5552 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 5553 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 5554 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 5555 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 5556 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5557 | } |
| 5558 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5559 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5560 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5561 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5562 | int r; |
| 5563 | |
| 5564 | // Compile the "idx" in "var[idx]". |
| 5565 | if (new_local) |
| 5566 | --cctx->ctx_locals.ga_len; |
| 5567 | p = skipwhite(var_start + varlen + 1); |
| 5568 | r = compile_expr0(&p, cctx); |
| 5569 | if (new_local) |
| 5570 | ++cctx->ctx_locals.ga_len; |
| 5571 | if (r == FAIL) |
| 5572 | goto theend; |
| 5573 | if (*skipwhite(p) != ']') |
| 5574 | { |
| 5575 | emsg(_(e_missbrac)); |
| 5576 | goto theend; |
| 5577 | } |
| 5578 | if (type->tt_type == VAR_DICT |
| 5579 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5580 | goto theend; |
| 5581 | if (type->tt_type == VAR_LIST |
| 5582 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5583 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5584 | { |
| 5585 | emsg(_(e_number_exp)); |
| 5586 | goto theend; |
| 5587 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5588 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5589 | // Load the dict or list. On the stack we then have: |
| 5590 | // - value |
| 5591 | // - index |
| 5592 | // - variable |
| 5593 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5594 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5595 | if (type->tt_type == VAR_LIST) |
| 5596 | { |
| 5597 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5598 | return FAIL; |
| 5599 | } |
| 5600 | else if (type->tt_type == VAR_DICT) |
| 5601 | { |
| 5602 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5603 | return FAIL; |
| 5604 | } |
| 5605 | else |
| 5606 | { |
| 5607 | emsg(_(e_listreq)); |
| 5608 | goto theend; |
| 5609 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5610 | } |
| 5611 | else |
| 5612 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5613 | switch (dest) |
| 5614 | { |
| 5615 | case dest_option: |
| 5616 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5617 | break; |
| 5618 | case dest_global: |
| 5619 | // include g: with the name, easier to execute that way |
| 5620 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5621 | break; |
| 5622 | case dest_buffer: |
| 5623 | // include b: with the name, easier to execute that way |
| 5624 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5625 | break; |
| 5626 | case dest_window: |
| 5627 | // include w: with the name, easier to execute that way |
| 5628 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5629 | break; |
| 5630 | case dest_tab: |
| 5631 | // include t: with the name, easier to execute that way |
| 5632 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5633 | break; |
| 5634 | case dest_env: |
| 5635 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5636 | break; |
| 5637 | case dest_reg: |
| 5638 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5639 | break; |
| 5640 | case dest_vimvar: |
| 5641 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5642 | break; |
| 5643 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5644 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5645 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5646 | imported_T *import = NULL; |
| 5647 | int sid = current_sctx.sc_sid; |
| 5648 | int idx; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5649 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5650 | if (name[1] != ':') |
| 5651 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5652 | import = find_imported(name, 0, cctx); |
| 5653 | if (import != NULL) |
| 5654 | sid = import->imp_sid; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5655 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5656 | |
| 5657 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 5658 | // TODO: specific type |
| 5659 | if (idx < 0) |
| 5660 | { |
| 5661 | char_u *name_s = name; |
| 5662 | |
| 5663 | // Include s: in the name for store_var() |
| 5664 | if (name[1] != ':') |
| 5665 | { |
| 5666 | int len = (int)STRLEN(name) + 3; |
| 5667 | |
| 5668 | name_s = alloc(len); |
| 5669 | if (name_s == NULL) |
| 5670 | name_s = name; |
| 5671 | else |
| 5672 | vim_snprintf((char *)name_s, len, |
| 5673 | "s:%s", name); |
| 5674 | } |
| 5675 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5676 | &t_any); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5677 | if (name_s != name) |
| 5678 | vim_free(name_s); |
| 5679 | } |
| 5680 | else |
| 5681 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5682 | sid, idx, &t_any); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5683 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5684 | break; |
| 5685 | case dest_local: |
| 5686 | if (lvar != NULL) |
| 5687 | { |
| 5688 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5689 | + instr->ga_len - 1; |
| 5690 | |
| 5691 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5692 | // ISN_STORE into ISN_STORENR |
| 5693 | if (!lvar->lv_from_outer |
| 5694 | && instr->ga_len == instr_count + 1 |
| 5695 | && isn->isn_type == ISN_PUSHNR) |
| 5696 | { |
| 5697 | varnumber_T val = isn->isn_arg.number; |
| 5698 | |
| 5699 | isn->isn_type = ISN_STORENR; |
| 5700 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5701 | isn->isn_arg.storenr.stnr_val = val; |
| 5702 | if (stack->ga_len > 0) |
| 5703 | --stack->ga_len; |
| 5704 | } |
| 5705 | else if (lvar->lv_from_outer) |
| 5706 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5707 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5708 | else |
| 5709 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5710 | } |
| 5711 | break; |
| 5712 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5713 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5714 | |
| 5715 | if (var_idx + 1 < var_count) |
| 5716 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5717 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5718 | |
| 5719 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5720 | if (var_count > 0 && !semicolon) |
| 5721 | { |
| 5722 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5723 | goto theend; |
| 5724 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5725 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5726 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5727 | |
| 5728 | theend: |
| 5729 | vim_free(name); |
| 5730 | return ret; |
| 5731 | } |
| 5732 | |
| 5733 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5734 | * Check if "name" can be "unlet". |
| 5735 | */ |
| 5736 | int |
| 5737 | check_vim9_unlet(char_u *name) |
| 5738 | { |
| 5739 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5740 | { |
| 5741 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5742 | return FAIL; |
| 5743 | } |
| 5744 | return OK; |
| 5745 | } |
| 5746 | |
| 5747 | /* |
| 5748 | * Callback passed to ex_unletlock(). |
| 5749 | */ |
| 5750 | static int |
| 5751 | compile_unlet( |
| 5752 | lval_T *lvp, |
| 5753 | char_u *name_end, |
| 5754 | exarg_T *eap, |
| 5755 | int deep UNUSED, |
| 5756 | void *coookie) |
| 5757 | { |
| 5758 | cctx_T *cctx = coookie; |
| 5759 | |
| 5760 | if (lvp->ll_tv == NULL) |
| 5761 | { |
| 5762 | char_u *p = lvp->ll_name; |
| 5763 | int cc = *name_end; |
| 5764 | int ret = OK; |
| 5765 | |
| 5766 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5767 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5768 | if (*p == '$') |
| 5769 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5770 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5771 | ret = FAIL; |
| 5772 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5773 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5774 | |
| 5775 | *name_end = cc; |
| 5776 | return ret; |
| 5777 | } |
| 5778 | |
| 5779 | // TODO: unlet {list}[idx] |
| 5780 | // TODO: unlet {dict}[key] |
| 5781 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5782 | return FAIL; |
| 5783 | } |
| 5784 | |
| 5785 | /* |
| 5786 | * compile "unlet var", "lock var" and "unlock var" |
| 5787 | * "arg" points to "var". |
| 5788 | */ |
| 5789 | static char_u * |
| 5790 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5791 | { |
| 5792 | char_u *p = arg; |
| 5793 | |
| 5794 | if (eap->cmdidx != CMD_unlet) |
| 5795 | { |
| 5796 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5797 | return NULL; |
| 5798 | } |
| 5799 | |
| 5800 | if (*p == '!') |
| 5801 | { |
| 5802 | p = skipwhite(p + 1); |
| 5803 | eap->forceit = TRUE; |
| 5804 | } |
| 5805 | |
| 5806 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5807 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5808 | } |
| 5809 | |
| 5810 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5811 | * Compile an :import command. |
| 5812 | */ |
| 5813 | static char_u * |
| 5814 | compile_import(char_u *arg, cctx_T *cctx) |
| 5815 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5816 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5817 | } |
| 5818 | |
| 5819 | /* |
| 5820 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5821 | */ |
| 5822 | static int |
| 5823 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5824 | { |
| 5825 | garray_T *instr = &cctx->ctx_instr; |
| 5826 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5827 | |
| 5828 | if (endlabel == NULL) |
| 5829 | return FAIL; |
| 5830 | endlabel->el_next = *el; |
| 5831 | *el = endlabel; |
| 5832 | endlabel->el_end_label = instr->ga_len; |
| 5833 | |
| 5834 | generate_JUMP(cctx, when, 0); |
| 5835 | return OK; |
| 5836 | } |
| 5837 | |
| 5838 | static void |
| 5839 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5840 | { |
| 5841 | garray_T *instr = &cctx->ctx_instr; |
| 5842 | |
| 5843 | while (*el != NULL) |
| 5844 | { |
| 5845 | endlabel_T *cur = (*el); |
| 5846 | isn_T *isn; |
| 5847 | |
| 5848 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5849 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5850 | *el = cur->el_next; |
| 5851 | vim_free(cur); |
| 5852 | } |
| 5853 | } |
| 5854 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5855 | static void |
| 5856 | compile_free_jump_to_end(endlabel_T **el) |
| 5857 | { |
| 5858 | while (*el != NULL) |
| 5859 | { |
| 5860 | endlabel_T *cur = (*el); |
| 5861 | |
| 5862 | *el = cur->el_next; |
| 5863 | vim_free(cur); |
| 5864 | } |
| 5865 | } |
| 5866 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5867 | /* |
| 5868 | * Create a new scope and set up the generic items. |
| 5869 | */ |
| 5870 | static scope_T * |
| 5871 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5872 | { |
| 5873 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5874 | |
| 5875 | if (scope == NULL) |
| 5876 | return NULL; |
| 5877 | scope->se_outer = cctx->ctx_scope; |
| 5878 | cctx->ctx_scope = scope; |
| 5879 | scope->se_type = type; |
| 5880 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5881 | return scope; |
| 5882 | } |
| 5883 | |
| 5884 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5885 | * Free the current scope and go back to the outer scope. |
| 5886 | */ |
| 5887 | static void |
| 5888 | drop_scope(cctx_T *cctx) |
| 5889 | { |
| 5890 | scope_T *scope = cctx->ctx_scope; |
| 5891 | |
| 5892 | if (scope == NULL) |
| 5893 | { |
| 5894 | iemsg("calling drop_scope() without a scope"); |
| 5895 | return; |
| 5896 | } |
| 5897 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5898 | switch (scope->se_type) |
| 5899 | { |
| 5900 | case IF_SCOPE: |
| 5901 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5902 | case FOR_SCOPE: |
| 5903 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5904 | case WHILE_SCOPE: |
| 5905 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5906 | case TRY_SCOPE: |
| 5907 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5908 | case NO_SCOPE: |
| 5909 | case BLOCK_SCOPE: |
| 5910 | break; |
| 5911 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5912 | vim_free(scope); |
| 5913 | } |
| 5914 | |
| 5915 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5916 | * compile "if expr" |
| 5917 | * |
| 5918 | * "if expr" Produces instructions: |
| 5919 | * EVAL expr Push result of "expr" |
| 5920 | * JUMP_IF_FALSE end |
| 5921 | * ... body ... |
| 5922 | * end: |
| 5923 | * |
| 5924 | * "if expr | else" Produces instructions: |
| 5925 | * EVAL expr Push result of "expr" |
| 5926 | * JUMP_IF_FALSE else |
| 5927 | * ... body ... |
| 5928 | * JUMP_ALWAYS end |
| 5929 | * else: |
| 5930 | * ... body ... |
| 5931 | * end: |
| 5932 | * |
| 5933 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5934 | * EVAL expr Push result of "expr" |
| 5935 | * JUMP_IF_FALSE elseif |
| 5936 | * ... body ... |
| 5937 | * JUMP_ALWAYS end |
| 5938 | * elseif: |
| 5939 | * EVAL expr Push result of "expr" |
| 5940 | * JUMP_IF_FALSE else |
| 5941 | * ... body ... |
| 5942 | * JUMP_ALWAYS end |
| 5943 | * else: |
| 5944 | * ... body ... |
| 5945 | * end: |
| 5946 | */ |
| 5947 | static char_u * |
| 5948 | compile_if(char_u *arg, cctx_T *cctx) |
| 5949 | { |
| 5950 | char_u *p = arg; |
| 5951 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5952 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5953 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5954 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5955 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5956 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5957 | CLEAR_FIELD(ppconst); |
| 5958 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5959 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5960 | clear_ppconst(&ppconst); |
| 5961 | return NULL; |
| 5962 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5963 | if (cctx->ctx_skip == SKIP_YES) |
| 5964 | clear_ppconst(&ppconst); |
| 5965 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5966 | { |
| 5967 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5968 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5969 | clear_ppconst(&ppconst); |
| 5970 | } |
| 5971 | else |
| 5972 | { |
| 5973 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5974 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5975 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5976 | return NULL; |
| 5977 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5978 | |
| 5979 | scope = new_scope(cctx, IF_SCOPE); |
| 5980 | if (scope == NULL) |
| 5981 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5982 | scope->se_skip_save = skip_save; |
| 5983 | // "is_had_return" will be reset if any block does not end in :return |
| 5984 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5985 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5986 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5987 | { |
| 5988 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5989 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5990 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5991 | } |
| 5992 | else |
| 5993 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5994 | |
| 5995 | return p; |
| 5996 | } |
| 5997 | |
| 5998 | static char_u * |
| 5999 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 6000 | { |
| 6001 | char_u *p = arg; |
| 6002 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6003 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6004 | isn_T *isn; |
| 6005 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6006 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6007 | |
| 6008 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6009 | { |
| 6010 | emsg(_(e_elseif_without_if)); |
| 6011 | return NULL; |
| 6012 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6013 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6014 | if (!cctx->ctx_had_return) |
| 6015 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6016 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6017 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6018 | { |
| 6019 | 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] | 6020 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6021 | return NULL; |
| 6022 | // previous "if" or "elseif" jumps here |
| 6023 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6024 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6025 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6026 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6027 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6028 | CLEAR_FIELD(ppconst); |
| 6029 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6030 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6031 | clear_ppconst(&ppconst); |
| 6032 | return NULL; |
| 6033 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6034 | if (scope->se_skip_save == SKIP_YES) |
| 6035 | clear_ppconst(&ppconst); |
| 6036 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6037 | { |
| 6038 | // The expression results in a constant. |
| 6039 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6040 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6041 | clear_ppconst(&ppconst); |
| 6042 | scope->se_u.se_if.is_if_label = -1; |
| 6043 | } |
| 6044 | else |
| 6045 | { |
| 6046 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6047 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6048 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6049 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6050 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6051 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6052 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6053 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6054 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6055 | |
| 6056 | return p; |
| 6057 | } |
| 6058 | |
| 6059 | static char_u * |
| 6060 | compile_else(char_u *arg, cctx_T *cctx) |
| 6061 | { |
| 6062 | char_u *p = arg; |
| 6063 | garray_T *instr = &cctx->ctx_instr; |
| 6064 | isn_T *isn; |
| 6065 | scope_T *scope = cctx->ctx_scope; |
| 6066 | |
| 6067 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6068 | { |
| 6069 | emsg(_(e_else_without_if)); |
| 6070 | return NULL; |
| 6071 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6072 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6073 | if (!cctx->ctx_had_return) |
| 6074 | scope->se_u.se_if.is_had_return = FALSE; |
| 6075 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6076 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6077 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6078 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6079 | // 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] | 6080 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6081 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6082 | if (!cctx->ctx_had_return |
| 6083 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6084 | JUMP_ALWAYS, cctx) == FAIL) |
| 6085 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6086 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6087 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6088 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6089 | { |
| 6090 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6091 | { |
| 6092 | // previous "if" or "elseif" jumps here |
| 6093 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6094 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6095 | scope->se_u.se_if.is_if_label = -1; |
| 6096 | } |
| 6097 | } |
| 6098 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6099 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6100 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6101 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6102 | |
| 6103 | return p; |
| 6104 | } |
| 6105 | |
| 6106 | static char_u * |
| 6107 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6108 | { |
| 6109 | scope_T *scope = cctx->ctx_scope; |
| 6110 | ifscope_T *ifscope; |
| 6111 | garray_T *instr = &cctx->ctx_instr; |
| 6112 | isn_T *isn; |
| 6113 | |
| 6114 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6115 | { |
| 6116 | emsg(_(e_endif_without_if)); |
| 6117 | return NULL; |
| 6118 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6119 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6120 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6121 | if (!cctx->ctx_had_return) |
| 6122 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6123 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6124 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6125 | { |
| 6126 | // previous "if" or "elseif" jumps here |
| 6127 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6128 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6129 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6130 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6131 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6132 | cctx->ctx_skip = scope->se_skip_save; |
| 6133 | |
| 6134 | // If all the blocks end in :return and there is an :else then the |
| 6135 | // had_return flag is set. |
| 6136 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6137 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6138 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6139 | return arg; |
| 6140 | } |
| 6141 | |
| 6142 | /* |
| 6143 | * compile "for var in expr" |
| 6144 | * |
| 6145 | * Produces instructions: |
| 6146 | * PUSHNR -1 |
| 6147 | * STORE loop-idx Set index to -1 |
| 6148 | * EVAL expr Push result of "expr" |
| 6149 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6150 | * - if beyond end, jump to "end" |
| 6151 | * - otherwise get item from list and push it |
| 6152 | * STORE var Store item in "var" |
| 6153 | * ... body ... |
| 6154 | * JUMP top Jump back to repeat |
| 6155 | * end: DROP Drop the result of "expr" |
| 6156 | * |
| 6157 | */ |
| 6158 | static char_u * |
| 6159 | compile_for(char_u *arg, cctx_T *cctx) |
| 6160 | { |
| 6161 | char_u *p; |
| 6162 | size_t varlen; |
| 6163 | garray_T *instr = &cctx->ctx_instr; |
| 6164 | garray_T *stack = &cctx->ctx_type_stack; |
| 6165 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6166 | lvar_T *loop_lvar; // loop iteration variable |
| 6167 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6168 | type_T *vartype; |
| 6169 | |
| 6170 | // TODO: list of variables: "for [key, value] in dict" |
| 6171 | // parse "var" |
| 6172 | for (p = arg; eval_isnamec1(*p); ++p) |
| 6173 | ; |
| 6174 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6175 | var_lvar = lookup_local(arg, varlen, cctx); |
| 6176 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6177 | { |
| 6178 | semsg(_("E1023: variable already defined: %s"), arg); |
| 6179 | return NULL; |
| 6180 | } |
| 6181 | |
| 6182 | // consume "in" |
| 6183 | p = skipwhite(p); |
| 6184 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 6185 | { |
| 6186 | emsg(_(e_missing_in)); |
| 6187 | return NULL; |
| 6188 | } |
| 6189 | p = skipwhite(p + 2); |
| 6190 | |
| 6191 | |
| 6192 | scope = new_scope(cctx, FOR_SCOPE); |
| 6193 | if (scope == NULL) |
| 6194 | return NULL; |
| 6195 | |
| 6196 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6197 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6198 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6199 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6200 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6201 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6202 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6203 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6204 | |
| 6205 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6206 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 6207 | if (var_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 or used as an argument |
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 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6214 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6215 | |
| 6216 | // compile "expr", it remains on the stack until "endfor" |
| 6217 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6218 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6219 | { |
| 6220 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6221 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6222 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6223 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6224 | // Now that we know the type of "var", check that it is a list, now or at |
| 6225 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6226 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 6227 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6228 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6229 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6230 | return NULL; |
| 6231 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6232 | 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] | 6233 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6234 | |
| 6235 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6236 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6237 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6238 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 6239 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6240 | |
| 6241 | return arg; |
| 6242 | } |
| 6243 | |
| 6244 | /* |
| 6245 | * compile "endfor" |
| 6246 | */ |
| 6247 | static char_u * |
| 6248 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 6249 | { |
| 6250 | garray_T *instr = &cctx->ctx_instr; |
| 6251 | scope_T *scope = cctx->ctx_scope; |
| 6252 | forscope_T *forscope; |
| 6253 | isn_T *isn; |
| 6254 | |
| 6255 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 6256 | { |
| 6257 | emsg(_(e_for)); |
| 6258 | return NULL; |
| 6259 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6260 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6261 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6262 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6263 | |
| 6264 | // At end of ":for" scope jump back to the FOR instruction. |
| 6265 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 6266 | |
| 6267 | // Fill in the "end" label in the FOR statement so it can jump here |
| 6268 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 6269 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 6270 | |
| 6271 | // Fill in the "end" label any BREAK statements |
| 6272 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 6273 | |
| 6274 | // Below the ":for" scope drop the "expr" list from the stack. |
| 6275 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 6276 | return NULL; |
| 6277 | |
| 6278 | vim_free(scope); |
| 6279 | |
| 6280 | return arg; |
| 6281 | } |
| 6282 | |
| 6283 | /* |
| 6284 | * compile "while expr" |
| 6285 | * |
| 6286 | * Produces instructions: |
| 6287 | * top: EVAL expr Push result of "expr" |
| 6288 | * JUMP_IF_FALSE end jump if false |
| 6289 | * ... body ... |
| 6290 | * JUMP top Jump back to repeat |
| 6291 | * end: |
| 6292 | * |
| 6293 | */ |
| 6294 | static char_u * |
| 6295 | compile_while(char_u *arg, cctx_T *cctx) |
| 6296 | { |
| 6297 | char_u *p = arg; |
| 6298 | garray_T *instr = &cctx->ctx_instr; |
| 6299 | scope_T *scope; |
| 6300 | |
| 6301 | scope = new_scope(cctx, WHILE_SCOPE); |
| 6302 | if (scope == NULL) |
| 6303 | return NULL; |
| 6304 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6305 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6306 | |
| 6307 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6308 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6309 | return NULL; |
| 6310 | |
| 6311 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6312 | 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] | 6313 | JUMP_IF_FALSE, cctx) == FAIL) |
| 6314 | return FAIL; |
| 6315 | |
| 6316 | return p; |
| 6317 | } |
| 6318 | |
| 6319 | /* |
| 6320 | * compile "endwhile" |
| 6321 | */ |
| 6322 | static char_u * |
| 6323 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 6324 | { |
| 6325 | scope_T *scope = cctx->ctx_scope; |
| 6326 | |
| 6327 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 6328 | { |
| 6329 | emsg(_(e_while)); |
| 6330 | return NULL; |
| 6331 | } |
| 6332 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6333 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6334 | |
| 6335 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6336 | 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] | 6337 | |
| 6338 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 6339 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6340 | 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] | 6341 | |
| 6342 | vim_free(scope); |
| 6343 | |
| 6344 | return arg; |
| 6345 | } |
| 6346 | |
| 6347 | /* |
| 6348 | * compile "continue" |
| 6349 | */ |
| 6350 | static char_u * |
| 6351 | compile_continue(char_u *arg, cctx_T *cctx) |
| 6352 | { |
| 6353 | scope_T *scope = cctx->ctx_scope; |
| 6354 | |
| 6355 | for (;;) |
| 6356 | { |
| 6357 | if (scope == NULL) |
| 6358 | { |
| 6359 | emsg(_(e_continue)); |
| 6360 | return NULL; |
| 6361 | } |
| 6362 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6363 | break; |
| 6364 | scope = scope->se_outer; |
| 6365 | } |
| 6366 | |
| 6367 | // Jump back to the FOR or WHILE instruction. |
| 6368 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6369 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 6370 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6371 | return arg; |
| 6372 | } |
| 6373 | |
| 6374 | /* |
| 6375 | * compile "break" |
| 6376 | */ |
| 6377 | static char_u * |
| 6378 | compile_break(char_u *arg, cctx_T *cctx) |
| 6379 | { |
| 6380 | scope_T *scope = cctx->ctx_scope; |
| 6381 | endlabel_T **el; |
| 6382 | |
| 6383 | for (;;) |
| 6384 | { |
| 6385 | if (scope == NULL) |
| 6386 | { |
| 6387 | emsg(_(e_break)); |
| 6388 | return NULL; |
| 6389 | } |
| 6390 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6391 | break; |
| 6392 | scope = scope->se_outer; |
| 6393 | } |
| 6394 | |
| 6395 | // Jump to the end of the FOR or WHILE loop. |
| 6396 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6397 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6398 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6399 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6400 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 6401 | return FAIL; |
| 6402 | |
| 6403 | return arg; |
| 6404 | } |
| 6405 | |
| 6406 | /* |
| 6407 | * compile "{" start of block |
| 6408 | */ |
| 6409 | static char_u * |
| 6410 | compile_block(char_u *arg, cctx_T *cctx) |
| 6411 | { |
| 6412 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6413 | return NULL; |
| 6414 | return skipwhite(arg + 1); |
| 6415 | } |
| 6416 | |
| 6417 | /* |
| 6418 | * compile end of block: drop one scope |
| 6419 | */ |
| 6420 | static void |
| 6421 | compile_endblock(cctx_T *cctx) |
| 6422 | { |
| 6423 | scope_T *scope = cctx->ctx_scope; |
| 6424 | |
| 6425 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6426 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6427 | vim_free(scope); |
| 6428 | } |
| 6429 | |
| 6430 | /* |
| 6431 | * compile "try" |
| 6432 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6433 | * finally. |
| 6434 | * Creates another scope for the "try" block itself. |
| 6435 | * TRY instruction sets up exception handling at runtime. |
| 6436 | * |
| 6437 | * "try" |
| 6438 | * TRY -> catch1, -> finally push trystack entry |
| 6439 | * ... try block |
| 6440 | * "throw {exception}" |
| 6441 | * EVAL {exception} |
| 6442 | * THROW create exception |
| 6443 | * ... try block |
| 6444 | * " catch {expr}" |
| 6445 | * JUMP -> finally |
| 6446 | * catch1: PUSH exeception |
| 6447 | * EVAL {expr} |
| 6448 | * MATCH |
| 6449 | * JUMP nomatch -> catch2 |
| 6450 | * CATCH remove exception |
| 6451 | * ... catch block |
| 6452 | * " catch" |
| 6453 | * JUMP -> finally |
| 6454 | * catch2: CATCH remove exception |
| 6455 | * ... catch block |
| 6456 | * " finally" |
| 6457 | * finally: |
| 6458 | * ... finally block |
| 6459 | * " endtry" |
| 6460 | * ENDTRY pop trystack entry, may rethrow |
| 6461 | */ |
| 6462 | static char_u * |
| 6463 | compile_try(char_u *arg, cctx_T *cctx) |
| 6464 | { |
| 6465 | garray_T *instr = &cctx->ctx_instr; |
| 6466 | scope_T *try_scope; |
| 6467 | scope_T *scope; |
| 6468 | |
| 6469 | // scope that holds the jumps that go to catch/finally/endtry |
| 6470 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6471 | if (try_scope == NULL) |
| 6472 | return NULL; |
| 6473 | |
| 6474 | // "catch" is set when the first ":catch" is found. |
| 6475 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6476 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6477 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6478 | return NULL; |
| 6479 | |
| 6480 | // scope for the try block itself |
| 6481 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6482 | if (scope == NULL) |
| 6483 | return NULL; |
| 6484 | |
| 6485 | return arg; |
| 6486 | } |
| 6487 | |
| 6488 | /* |
| 6489 | * compile "catch {expr}" |
| 6490 | */ |
| 6491 | static char_u * |
| 6492 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6493 | { |
| 6494 | scope_T *scope = cctx->ctx_scope; |
| 6495 | garray_T *instr = &cctx->ctx_instr; |
| 6496 | char_u *p; |
| 6497 | isn_T *isn; |
| 6498 | |
| 6499 | // end block scope from :try or :catch |
| 6500 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6501 | compile_endblock(cctx); |
| 6502 | scope = cctx->ctx_scope; |
| 6503 | |
| 6504 | // Error if not in a :try scope |
| 6505 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6506 | { |
| 6507 | emsg(_(e_catch)); |
| 6508 | return NULL; |
| 6509 | } |
| 6510 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6511 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6512 | { |
| 6513 | emsg(_("E1033: catch unreachable after catch-all")); |
| 6514 | return NULL; |
| 6515 | } |
| 6516 | |
| 6517 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6518 | 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] | 6519 | JUMP_ALWAYS, cctx) == FAIL) |
| 6520 | return NULL; |
| 6521 | |
| 6522 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6523 | 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] | 6524 | if (isn->isn_arg.try.try_catch == 0) |
| 6525 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6526 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6527 | { |
| 6528 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6529 | 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] | 6530 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6531 | } |
| 6532 | |
| 6533 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6534 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6535 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6536 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6537 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6538 | } |
| 6539 | else |
| 6540 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6541 | char_u *end; |
| 6542 | char_u *pat; |
| 6543 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6544 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6545 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6546 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6547 | // Push v:exception, push {expr} and MATCH |
| 6548 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6549 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6550 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6551 | if (*end != *p) |
| 6552 | { |
| 6553 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 6554 | vim_free(tofree); |
| 6555 | return FAIL; |
| 6556 | } |
| 6557 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6558 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6559 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6560 | len = (int)(end - tofree); |
| 6561 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6562 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6563 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6564 | if (pat == NULL) |
| 6565 | return FAIL; |
| 6566 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6567 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6568 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6569 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6570 | return NULL; |
| 6571 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6572 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6573 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6574 | return NULL; |
| 6575 | } |
| 6576 | |
| 6577 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6578 | return NULL; |
| 6579 | |
| 6580 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6581 | return NULL; |
| 6582 | return p; |
| 6583 | } |
| 6584 | |
| 6585 | static char_u * |
| 6586 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6587 | { |
| 6588 | scope_T *scope = cctx->ctx_scope; |
| 6589 | garray_T *instr = &cctx->ctx_instr; |
| 6590 | isn_T *isn; |
| 6591 | |
| 6592 | // end block scope from :try or :catch |
| 6593 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6594 | compile_endblock(cctx); |
| 6595 | scope = cctx->ctx_scope; |
| 6596 | |
| 6597 | // Error if not in a :try scope |
| 6598 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6599 | { |
| 6600 | emsg(_(e_finally)); |
| 6601 | return NULL; |
| 6602 | } |
| 6603 | |
| 6604 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6605 | 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] | 6606 | if (isn->isn_arg.try.try_finally != 0) |
| 6607 | { |
| 6608 | emsg(_(e_finally_dup)); |
| 6609 | return NULL; |
| 6610 | } |
| 6611 | |
| 6612 | // 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] | 6613 | 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] | 6614 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6615 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6616 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6617 | { |
| 6618 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6619 | 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] | 6620 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6621 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6622 | } |
| 6623 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6624 | // TODO: set index in ts_finally_label jumps |
| 6625 | |
| 6626 | return arg; |
| 6627 | } |
| 6628 | |
| 6629 | static char_u * |
| 6630 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6631 | { |
| 6632 | scope_T *scope = cctx->ctx_scope; |
| 6633 | garray_T *instr = &cctx->ctx_instr; |
| 6634 | isn_T *isn; |
| 6635 | |
| 6636 | // end block scope from :catch or :finally |
| 6637 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6638 | compile_endblock(cctx); |
| 6639 | scope = cctx->ctx_scope; |
| 6640 | |
| 6641 | // Error if not in a :try scope |
| 6642 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6643 | { |
| 6644 | if (scope == NULL) |
| 6645 | emsg(_(e_no_endtry)); |
| 6646 | else if (scope->se_type == WHILE_SCOPE) |
| 6647 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6648 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6649 | emsg(_(e_endfor)); |
| 6650 | else |
| 6651 | emsg(_(e_endif)); |
| 6652 | return NULL; |
| 6653 | } |
| 6654 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6655 | 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] | 6656 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6657 | { |
| 6658 | emsg(_("E1032: missing :catch or :finally")); |
| 6659 | return NULL; |
| 6660 | } |
| 6661 | |
| 6662 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6663 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6664 | 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] | 6665 | |
| 6666 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6667 | if (isn->isn_arg.try.try_catch == 0) |
| 6668 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6669 | if (isn->isn_arg.try.try_finally == 0) |
| 6670 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6671 | |
| 6672 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6673 | { |
| 6674 | // Last catch without match jumps here |
| 6675 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6676 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6677 | } |
| 6678 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6679 | compile_endblock(cctx); |
| 6680 | |
| 6681 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6682 | return NULL; |
| 6683 | return arg; |
| 6684 | } |
| 6685 | |
| 6686 | /* |
| 6687 | * compile "throw {expr}" |
| 6688 | */ |
| 6689 | static char_u * |
| 6690 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6691 | { |
| 6692 | char_u *p = skipwhite(arg); |
| 6693 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6694 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6695 | return NULL; |
| 6696 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6697 | return NULL; |
| 6698 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6699 | return NULL; |
| 6700 | |
| 6701 | return p; |
| 6702 | } |
| 6703 | |
| 6704 | /* |
| 6705 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6706 | * compile "echomsg expr" |
| 6707 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6708 | * compile "execute expr" |
| 6709 | */ |
| 6710 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6711 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6712 | { |
| 6713 | char_u *p = arg; |
| 6714 | int count = 0; |
| 6715 | |
| 6716 | for (;;) |
| 6717 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6718 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6719 | return NULL; |
| 6720 | ++count; |
| 6721 | p = skipwhite(p); |
| 6722 | if (ends_excmd(*p)) |
| 6723 | break; |
| 6724 | } |
| 6725 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6726 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6727 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6728 | else if (cmdidx == CMD_execute) |
| 6729 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6730 | else if (cmdidx == CMD_echomsg) |
| 6731 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6732 | else |
| 6733 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6734 | return p; |
| 6735 | } |
| 6736 | |
| 6737 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6738 | * A command that is not compiled, execute with legacy code. |
| 6739 | */ |
| 6740 | static char_u * |
| 6741 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6742 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6743 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6744 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6745 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6746 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6747 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6748 | goto theend; |
| 6749 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6750 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6751 | { |
| 6752 | long argt = excmd_get_argt(eap->cmdidx); |
| 6753 | int usefilter = FALSE; |
| 6754 | |
| 6755 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6756 | |
| 6757 | // If the command can be followed by a bar, find the bar and truncate |
| 6758 | // it, so that the following command can be compiled. |
| 6759 | // The '|' is overwritten with a NUL, it is put back below. |
| 6760 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6761 | && *eap->arg == '!') |
| 6762 | // :w !filter or :r !filter or :r! filter |
| 6763 | usefilter = TRUE; |
| 6764 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6765 | { |
| 6766 | separate_nextcmd(eap); |
| 6767 | if (eap->nextcmd != NULL) |
| 6768 | nextcmd = eap->nextcmd; |
| 6769 | } |
| 6770 | } |
| 6771 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6772 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6773 | { |
| 6774 | // expand filename in "syntax include [@group] filename" |
| 6775 | has_expr = TRUE; |
| 6776 | eap->arg = skipwhite(eap->arg + 7); |
| 6777 | if (*eap->arg == '@') |
| 6778 | eap->arg = skiptowhite(eap->arg); |
| 6779 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6780 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6781 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6782 | { |
| 6783 | int count = 0; |
| 6784 | char_u *start = skipwhite(line); |
| 6785 | |
| 6786 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6787 | // PUSHS ":cmd xxx" |
| 6788 | // eval expr1 |
| 6789 | // PUSHS "yyy" |
| 6790 | // eval expr2 |
| 6791 | // PUSHS "zzz" |
| 6792 | // EXECCONCAT 5 |
| 6793 | for (;;) |
| 6794 | { |
| 6795 | if (p > start) |
| 6796 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6797 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6798 | ++count; |
| 6799 | } |
| 6800 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6801 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6802 | return NULL; |
| 6803 | may_generate_2STRING(-1, cctx); |
| 6804 | ++count; |
| 6805 | p = skipwhite(p); |
| 6806 | if (*p != '`') |
| 6807 | { |
| 6808 | emsg(_("E1083: missing backtick")); |
| 6809 | return NULL; |
| 6810 | } |
| 6811 | start = p + 1; |
| 6812 | |
| 6813 | p = (char_u *)strstr((char *)start, "`="); |
| 6814 | if (p == NULL) |
| 6815 | { |
| 6816 | if (*skipwhite(start) != NUL) |
| 6817 | { |
| 6818 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6819 | ++count; |
| 6820 | } |
| 6821 | break; |
| 6822 | } |
| 6823 | } |
| 6824 | generate_EXECCONCAT(cctx, count); |
| 6825 | } |
| 6826 | else |
| 6827 | generate_EXEC(cctx, line); |
| 6828 | |
| 6829 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6830 | if (*nextcmd != NUL) |
| 6831 | { |
| 6832 | // the parser expects a pointer to the bar, put it back |
| 6833 | --nextcmd; |
| 6834 | *nextcmd = '|'; |
| 6835 | } |
| 6836 | |
| 6837 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6838 | } |
| 6839 | |
| 6840 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6841 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6842 | * 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] | 6843 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6844 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6845 | add_def_function(ufunc_T *ufunc) |
| 6846 | { |
| 6847 | dfunc_T *dfunc; |
| 6848 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6849 | if (def_functions.ga_len == 0) |
| 6850 | { |
| 6851 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6852 | // wasn't set. |
| 6853 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6854 | return FAIL; |
| 6855 | ++def_functions.ga_len; |
| 6856 | } |
| 6857 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6858 | // Add the function to "def_functions". |
| 6859 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6860 | return FAIL; |
| 6861 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6862 | CLEAR_POINTER(dfunc); |
| 6863 | dfunc->df_idx = def_functions.ga_len; |
| 6864 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6865 | dfunc->df_ufunc = ufunc; |
| 6866 | ++def_functions.ga_len; |
| 6867 | return OK; |
| 6868 | } |
| 6869 | |
| 6870 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6871 | * After ex_function() has collected all the function lines: parse and compile |
| 6872 | * the lines into instructions. |
| 6873 | * Adds the function to "def_functions". |
| 6874 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6875 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6876 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6877 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6878 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6879 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6880 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6881 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6882 | 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] | 6883 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6884 | char_u *line = NULL; |
| 6885 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6886 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6887 | cctx_T cctx; |
| 6888 | garray_T *instr; |
| 6889 | int called_emsg_before = called_emsg; |
| 6890 | int ret = FAIL; |
| 6891 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6892 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6893 | int emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6894 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6895 | // When using a function that was compiled before: Free old instructions. |
| 6896 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6897 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6898 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6899 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6900 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6901 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6902 | } |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6903 | else if (add_def_function(ufunc) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6904 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6905 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6906 | ufunc->uf_def_status = UF_COMPILING; |
| 6907 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6908 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6909 | cctx.ctx_ufunc = ufunc; |
| 6910 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6911 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6912 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6913 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6914 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6915 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6916 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6917 | instr = &cctx.ctx_instr; |
| 6918 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6919 | // Set the context to the function, it may be compiled when called from |
| 6920 | // another script. Set the script version to the most modern one. |
| 6921 | // The line number will be set in next_line_from_context(). |
| 6922 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6923 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6924 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6925 | // Make sure error messages are OK. |
| 6926 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6927 | if (do_estack_push) |
| 6928 | estack_push_ufunc(ufunc, 1); |
| 6929 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6930 | if (ufunc->uf_def_args.ga_len > 0) |
| 6931 | { |
| 6932 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6933 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6934 | int i; |
| 6935 | char_u *arg; |
| 6936 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 6937 | |
| 6938 | // Produce instructions for the default values of optional arguments. |
| 6939 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6940 | // where to start when the function is called, depending on the number |
| 6941 | // of arguments. |
| 6942 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6943 | if (ufunc->uf_def_arg_idx == NULL) |
| 6944 | goto erret; |
| 6945 | for (i = 0; i < count; ++i) |
| 6946 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6947 | garray_T *stack = &cctx.ctx_type_stack; |
| 6948 | type_T *val_type; |
| 6949 | int arg_idx = first_def_arg + i; |
| 6950 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6951 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6952 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6953 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6954 | goto erret; |
| 6955 | |
| 6956 | // If no type specified use the type of the default value. |
| 6957 | // Otherwise check that the default value type matches the |
| 6958 | // specified type. |
| 6959 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6960 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
| 6961 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 6962 | 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] | 6963 | == FAIL) |
| 6964 | { |
| 6965 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 6966 | arg_idx + 1); |
| 6967 | goto erret; |
| 6968 | } |
| 6969 | |
| 6970 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6971 | goto erret; |
| 6972 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6973 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
| 6974 | } |
| 6975 | |
| 6976 | /* |
| 6977 | * Loop over all the lines of the function and generate instructions. |
| 6978 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6979 | for (;;) |
| 6980 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6981 | exarg_T ea; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6982 | int starts_with_colon = FALSE; |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6983 | char_u *cmd; |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 6984 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6985 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6986 | // Bail out on the first error to avoid a flood of errors and report |
| 6987 | // the right line number when inside try/catch. |
| 6988 | if (emsg_before != called_emsg) |
| 6989 | goto erret; |
| 6990 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6991 | if (line != NULL && *line == '|') |
| 6992 | // the line continues after a '|' |
| 6993 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 6994 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6995 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 6996 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6997 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6998 | semsg(_("E488: Trailing characters: %s"), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6999 | goto erret; |
| 7000 | } |
| 7001 | else |
| 7002 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 7003 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 7004 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7005 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7006 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7007 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7008 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7009 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7010 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7011 | ea.cmdlinep = &line; |
| 7012 | ea.cmd = skipwhite(line); |
| 7013 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7014 | // Some things can be recognized by the first character. |
| 7015 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7016 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7017 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7018 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7019 | if (ea.cmd[1] != '{') |
| 7020 | { |
| 7021 | line = (char_u *)""; |
| 7022 | continue; |
| 7023 | } |
| 7024 | break; |
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 '}': |
| 7027 | { |
| 7028 | // "}" ends a block scope |
| 7029 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7030 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7031 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7032 | if (stype == BLOCK_SCOPE) |
| 7033 | { |
| 7034 | compile_endblock(&cctx); |
| 7035 | line = ea.cmd; |
| 7036 | } |
| 7037 | else |
| 7038 | { |
| 7039 | emsg(_("E1025: using } outside of a block scope")); |
| 7040 | goto erret; |
| 7041 | } |
| 7042 | if (line != NULL) |
| 7043 | line = skipwhite(ea.cmd + 1); |
| 7044 | continue; |
| 7045 | } |
| 7046 | |
| 7047 | case '{': |
| 7048 | // "{" starts a block scope |
| 7049 | // "{'a': 1}->func() is something else |
| 7050 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7051 | { |
| 7052 | line = compile_block(ea.cmd, &cctx); |
| 7053 | continue; |
| 7054 | } |
| 7055 | break; |
| 7056 | |
| 7057 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7058 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7059 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7060 | } |
| 7061 | |
| 7062 | /* |
| 7063 | * COMMAND MODIFIERS |
| 7064 | */ |
| 7065 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 7066 | { |
| 7067 | if (errormsg != NULL) |
| 7068 | goto erret; |
| 7069 | // empty line or comment |
| 7070 | line = (char_u *)""; |
| 7071 | continue; |
| 7072 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7073 | // TODO: use modifiers in the command |
| 7074 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | b074e8b | 2020-07-11 13:40:45 +0200 | [diff] [blame] | 7075 | CLEAR_FIELD(cmdmod); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7076 | |
| 7077 | // Skip ":call" to get to the function name. |
| 7078 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 7079 | ea.cmd = skipwhite(ea.cmd); |
| 7080 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7081 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7082 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7083 | char_u *pskip; |
| 7084 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7085 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7086 | // find what follows. |
| 7087 | // Skip over "var.member", "var[idx]" and the like. |
| 7088 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 7089 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7090 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7091 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7092 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7093 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7094 | char_u *var_end; |
| 7095 | int oplen; |
| 7096 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7097 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7098 | var_end = find_name_end(pskip, NULL, NULL, |
| 7099 | FNE_CHECK_START | FNE_INCL_BR); |
| 7100 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7101 | if (oplen > 0) |
| 7102 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7103 | size_t len = p - ea.cmd; |
| 7104 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7105 | // Recognize an assignment if we recognize the variable |
| 7106 | // name: |
| 7107 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 7108 | // "local = expr" where "local" is a local var. |
| 7109 | // "script = expr" where "script" is a script-local var. |
| 7110 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7111 | // "&opt = expr" |
| 7112 | // "$ENV = expr" |
| 7113 | // "@r = expr" |
| 7114 | if (*ea.cmd == '&' |
| 7115 | || *ea.cmd == '$' |
| 7116 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7117 | || ((len) > 2 && ea.cmd[1] == ':') |
| 7118 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 7119 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 7120 | NULL, &cctx) == OK |
| 7121 | || lookup_script(ea.cmd, len) == OK |
| 7122 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7123 | { |
| 7124 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7125 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7126 | goto erret; |
| 7127 | continue; |
| 7128 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7129 | } |
| 7130 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7131 | |
| 7132 | if (*ea.cmd == '[') |
| 7133 | { |
| 7134 | // [var, var] = expr |
| 7135 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 7136 | if (line == NULL) |
| 7137 | goto erret; |
| 7138 | if (line != ea.cmd) |
| 7139 | continue; |
| 7140 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7141 | } |
| 7142 | |
| 7143 | /* |
| 7144 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7145 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7146 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7147 | cmd = ea.cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7148 | if (*cmd != '\'') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7149 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7150 | ea.cmd = skip_range(ea.cmd, NULL); |
| 7151 | if (ea.cmd > cmd && !starts_with_colon) |
| 7152 | { |
| 7153 | emsg(_(e_colon_required)); |
| 7154 | goto erret; |
| 7155 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7156 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7157 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7158 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7159 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7160 | |
| 7161 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 7162 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7163 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7164 | { |
| 7165 | line += STRLEN(line); |
| 7166 | continue; |
| 7167 | } |
| 7168 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7169 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7170 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7171 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7172 | // CMD_let cannot happen, compile_assignment() above is used |
| 7173 | iemsg("Command from find_ex_command() not handled"); |
| 7174 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7175 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7176 | } |
| 7177 | |
| 7178 | p = skipwhite(p); |
| 7179 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7180 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 7181 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7182 | && ea.cmdidx != CMD_elseif |
| 7183 | && ea.cmdidx != CMD_else |
| 7184 | && ea.cmdidx != CMD_endif) |
| 7185 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7186 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7187 | continue; |
| 7188 | } |
| 7189 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7190 | if (ea.cmdidx != CMD_elseif |
| 7191 | && ea.cmdidx != CMD_else |
| 7192 | && ea.cmdidx != CMD_endif |
| 7193 | && ea.cmdidx != CMD_endfor |
| 7194 | && ea.cmdidx != CMD_endwhile |
| 7195 | && ea.cmdidx != CMD_catch |
| 7196 | && ea.cmdidx != CMD_finally |
| 7197 | && ea.cmdidx != CMD_endtry) |
| 7198 | { |
| 7199 | if (cctx.ctx_had_return) |
| 7200 | { |
| 7201 | emsg(_("E1095: Unreachable code after :return")); |
| 7202 | goto erret; |
| 7203 | } |
| 7204 | } |
| 7205 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7206 | switch (ea.cmdidx) |
| 7207 | { |
| 7208 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7209 | ea.arg = p; |
| 7210 | line = compile_nested_function(&ea, &cctx); |
| 7211 | break; |
| 7212 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7213 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7214 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7215 | goto erret; |
| 7216 | |
| 7217 | case CMD_return: |
| 7218 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7219 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7220 | break; |
| 7221 | |
| 7222 | case CMD_let: |
| 7223 | case CMD_const: |
| 7224 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7225 | if (line == p) |
| 7226 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7227 | break; |
| 7228 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7229 | case CMD_unlet: |
| 7230 | case CMD_unlockvar: |
| 7231 | case CMD_lockvar: |
| 7232 | line = compile_unletlock(p, &ea, &cctx); |
| 7233 | break; |
| 7234 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7235 | case CMD_import: |
| 7236 | line = compile_import(p, &cctx); |
| 7237 | break; |
| 7238 | |
| 7239 | case CMD_if: |
| 7240 | line = compile_if(p, &cctx); |
| 7241 | break; |
| 7242 | case CMD_elseif: |
| 7243 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7244 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7245 | break; |
| 7246 | case CMD_else: |
| 7247 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7248 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7249 | break; |
| 7250 | case CMD_endif: |
| 7251 | line = compile_endif(p, &cctx); |
| 7252 | break; |
| 7253 | |
| 7254 | case CMD_while: |
| 7255 | line = compile_while(p, &cctx); |
| 7256 | break; |
| 7257 | case CMD_endwhile: |
| 7258 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7259 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7260 | break; |
| 7261 | |
| 7262 | case CMD_for: |
| 7263 | line = compile_for(p, &cctx); |
| 7264 | break; |
| 7265 | case CMD_endfor: |
| 7266 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7267 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7268 | break; |
| 7269 | case CMD_continue: |
| 7270 | line = compile_continue(p, &cctx); |
| 7271 | break; |
| 7272 | case CMD_break: |
| 7273 | line = compile_break(p, &cctx); |
| 7274 | break; |
| 7275 | |
| 7276 | case CMD_try: |
| 7277 | line = compile_try(p, &cctx); |
| 7278 | break; |
| 7279 | case CMD_catch: |
| 7280 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7281 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7282 | break; |
| 7283 | case CMD_finally: |
| 7284 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7285 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7286 | break; |
| 7287 | case CMD_endtry: |
| 7288 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7289 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7290 | break; |
| 7291 | case CMD_throw: |
| 7292 | line = compile_throw(p, &cctx); |
| 7293 | break; |
| 7294 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7295 | case CMD_eval: |
| 7296 | if (compile_expr0(&p, &cctx) == FAIL) |
| 7297 | goto erret; |
| 7298 | |
| 7299 | // drop the return value |
| 7300 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 7301 | |
| 7302 | line = skipwhite(p); |
| 7303 | break; |
| 7304 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7305 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7306 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7307 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7308 | case CMD_echomsg: |
| 7309 | case CMD_echoerr: |
| 7310 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7311 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7312 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7313 | // TODO: other commands with an expression argument |
| 7314 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7315 | case CMD_SIZE: |
| 7316 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 7317 | goto erret; |
| 7318 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7319 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7320 | // Not recognized, execute with do_cmdline_cmd(). |
| 7321 | ea.arg = p; |
| 7322 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7323 | break; |
| 7324 | } |
| 7325 | if (line == NULL) |
| 7326 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7327 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7328 | |
| 7329 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7330 | { |
| 7331 | iemsg("Type stack underflow"); |
| 7332 | goto erret; |
| 7333 | } |
| 7334 | } |
| 7335 | |
| 7336 | if (cctx.ctx_scope != NULL) |
| 7337 | { |
| 7338 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7339 | emsg(_(e_endif)); |
| 7340 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7341 | emsg(_(e_endwhile)); |
| 7342 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7343 | emsg(_(e_endfor)); |
| 7344 | else |
| 7345 | emsg(_("E1026: Missing }")); |
| 7346 | goto erret; |
| 7347 | } |
| 7348 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7349 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7350 | { |
| 7351 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7352 | { |
| 7353 | emsg(_("E1027: Missing return statement")); |
| 7354 | goto erret; |
| 7355 | } |
| 7356 | |
| 7357 | // Return zero if there is no return at the end. |
| 7358 | generate_PUSHNR(&cctx, 0); |
| 7359 | generate_instr(&cctx, ISN_RETURN); |
| 7360 | } |
| 7361 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7362 | { |
| 7363 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7364 | + ufunc->uf_dfunc_idx; |
| 7365 | dfunc->df_deleted = FALSE; |
| 7366 | dfunc->df_instr = instr->ga_data; |
| 7367 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7368 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 7369 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7370 | if (cctx.ctx_outer_used) |
| 7371 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7372 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7373 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7374 | |
| 7375 | ret = OK; |
| 7376 | |
| 7377 | erret: |
| 7378 | if (ret == FAIL) |
| 7379 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7380 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7381 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7382 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7383 | |
| 7384 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7385 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7386 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7387 | |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7388 | // if using the last entry in the table we might as well remove it |
| 7389 | if (!dfunc->df_deleted |
| 7390 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7391 | --def_functions.ga_len; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7392 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7393 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7394 | while (cctx.ctx_scope != NULL) |
| 7395 | drop_scope(&cctx); |
| 7396 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7397 | // Don't execute this function body. |
| 7398 | ga_clear_strings(&ufunc->uf_lines); |
| 7399 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7400 | if (errormsg != NULL) |
| 7401 | emsg(errormsg); |
| 7402 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 7403 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7404 | } |
| 7405 | |
| 7406 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7407 | if (do_estack_push) |
| 7408 | estack_pop(); |
| 7409 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7410 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7411 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7412 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7413 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7414 | } |
| 7415 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7416 | void |
| 7417 | set_function_type(ufunc_T *ufunc) |
| 7418 | { |
| 7419 | int varargs = ufunc->uf_va_name != NULL; |
| 7420 | int argcount = ufunc->uf_args.ga_len; |
| 7421 | |
| 7422 | // Create a type for the function, with the return type and any |
| 7423 | // argument types. |
| 7424 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7425 | // The type is included in "tt_args". |
| 7426 | if (argcount > 0 || varargs) |
| 7427 | { |
| 7428 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7429 | argcount, &ufunc->uf_type_list); |
| 7430 | // Add argument types to the function type. |
| 7431 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7432 | argcount + varargs, |
| 7433 | &ufunc->uf_type_list) == FAIL) |
| 7434 | return; |
| 7435 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7436 | ufunc->uf_func_type->tt_min_argcount = |
| 7437 | argcount - ufunc->uf_def_args.ga_len; |
| 7438 | if (ufunc->uf_arg_types == NULL) |
| 7439 | { |
| 7440 | int i; |
| 7441 | |
| 7442 | // lambda does not have argument types. |
| 7443 | for (i = 0; i < argcount; ++i) |
| 7444 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7445 | } |
| 7446 | else |
| 7447 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7448 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7449 | if (varargs) |
| 7450 | { |
| 7451 | ufunc->uf_func_type->tt_args[argcount] = |
| 7452 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7453 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7454 | } |
| 7455 | } |
| 7456 | else |
| 7457 | // No arguments, can use a predefined type. |
| 7458 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7459 | argcount, &ufunc->uf_type_list); |
| 7460 | } |
| 7461 | |
| 7462 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7463 | /* |
| 7464 | * Delete an instruction, free what it contains. |
| 7465 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7466 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7467 | delete_instr(isn_T *isn) |
| 7468 | { |
| 7469 | switch (isn->isn_type) |
| 7470 | { |
| 7471 | case ISN_EXEC: |
| 7472 | case ISN_LOADENV: |
| 7473 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7474 | case ISN_LOADB: |
| 7475 | case ISN_LOADW: |
| 7476 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7477 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7478 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7479 | case ISN_PUSHEXC: |
| 7480 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7481 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7482 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7483 | case ISN_STOREB: |
| 7484 | case ISN_STOREW: |
| 7485 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7486 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7487 | vim_free(isn->isn_arg.string); |
| 7488 | break; |
| 7489 | |
| 7490 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7491 | case ISN_STORES: |
| 7492 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7493 | break; |
| 7494 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7495 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7496 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7497 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7498 | break; |
| 7499 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7500 | case ISN_STOREOPT: |
| 7501 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7502 | break; |
| 7503 | |
| 7504 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7505 | blob_unref(isn->isn_arg.blob); |
| 7506 | break; |
| 7507 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7508 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7509 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7510 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7511 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7512 | break; |
| 7513 | |
| 7514 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7515 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7516 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7517 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7518 | break; |
| 7519 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7520 | case ISN_UCALL: |
| 7521 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7522 | break; |
| 7523 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7524 | case ISN_FUNCREF: |
| 7525 | { |
| 7526 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7527 | + isn->isn_arg.funcref.fr_func; |
| 7528 | func_ptr_unref(dfunc->df_ufunc); |
| 7529 | } |
| 7530 | break; |
| 7531 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7532 | case ISN_2BOOL: |
| 7533 | case ISN_2STRING: |
| 7534 | case ISN_ADDBLOB: |
| 7535 | case ISN_ADDLIST: |
| 7536 | case ISN_BCALL: |
| 7537 | case ISN_CATCH: |
| 7538 | case ISN_CHECKNR: |
| 7539 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7540 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7541 | case ISN_COMPAREANY: |
| 7542 | case ISN_COMPAREBLOB: |
| 7543 | case ISN_COMPAREBOOL: |
| 7544 | case ISN_COMPAREDICT: |
| 7545 | case ISN_COMPAREFLOAT: |
| 7546 | case ISN_COMPAREFUNC: |
| 7547 | case ISN_COMPARELIST: |
| 7548 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7549 | case ISN_COMPARESPECIAL: |
| 7550 | case ISN_COMPARESTRING: |
| 7551 | case ISN_CONCAT: |
| 7552 | case ISN_DCALL: |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 7553 | case ISN_SHUFFLE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7554 | case ISN_DROP: |
| 7555 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7556 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7557 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7558 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7559 | case ISN_EXECCONCAT: |
| 7560 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7561 | case ISN_FOR: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7562 | case ISN_LISTINDEX: |
| 7563 | case ISN_STRINDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7564 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7565 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7566 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7567 | case ISN_JUMP: |
| 7568 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7569 | case ISN_LOADBDICT: |
| 7570 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7571 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7572 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7573 | case ISN_LOADSCRIPT: |
| 7574 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7575 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7576 | case ISN_LOADWDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7577 | case ISN_NEGATENR: |
| 7578 | case ISN_NEWDICT: |
| 7579 | case ISN_NEWLIST: |
| 7580 | case ISN_OPNR: |
| 7581 | case ISN_OPFLOAT: |
| 7582 | case ISN_OPANY: |
| 7583 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7584 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7585 | case ISN_PUSHF: |
| 7586 | case ISN_PUSHNR: |
| 7587 | case ISN_PUSHBOOL: |
| 7588 | case ISN_PUSHSPEC: |
| 7589 | case ISN_RETURN: |
| 7590 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 7591 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7592 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7593 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7594 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7595 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7596 | case ISN_STOREDICT: |
| 7597 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7598 | case ISN_THROW: |
| 7599 | case ISN_TRY: |
| 7600 | // nothing allocated |
| 7601 | break; |
| 7602 | } |
| 7603 | } |
| 7604 | |
| 7605 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7606 | * Free all instructions for "dfunc". |
| 7607 | */ |
| 7608 | static void |
| 7609 | delete_def_function_contents(dfunc_T *dfunc) |
| 7610 | { |
| 7611 | int idx; |
| 7612 | |
| 7613 | ga_clear(&dfunc->df_def_args_isn); |
| 7614 | |
| 7615 | if (dfunc->df_instr != NULL) |
| 7616 | { |
| 7617 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7618 | delete_instr(dfunc->df_instr + idx); |
| 7619 | VIM_CLEAR(dfunc->df_instr); |
| 7620 | } |
| 7621 | |
| 7622 | dfunc->df_deleted = TRUE; |
| 7623 | } |
| 7624 | |
| 7625 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7626 | * When a user function is deleted, clear the contents of any associated def |
| 7627 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7628 | */ |
| 7629 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7630 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7631 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7632 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7633 | { |
| 7634 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7635 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7636 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7637 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7638 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7639 | } |
| 7640 | } |
| 7641 | |
| 7642 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7643 | /* |
| 7644 | * Free all functions defined with ":def". |
| 7645 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7646 | void |
| 7647 | free_def_functions(void) |
| 7648 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7649 | int idx; |
| 7650 | |
| 7651 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7652 | { |
| 7653 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7654 | |
| 7655 | delete_def_function_contents(dfunc); |
| 7656 | } |
| 7657 | |
| 7658 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7659 | } |
| 7660 | #endif |
| 7661 | |
| 7662 | |
| 7663 | #endif // FEAT_EVAL |