Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9compile.c: :def and dealing with instructions |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | #ifdef VMS |
| 20 | # include <float.h> |
| 21 | #endif |
| 22 | |
| 23 | #define DEFINE_VIM9_GLOBALS |
| 24 | #include "vim9.h" |
| 25 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 26 | // values for ctx_skip |
| 27 | typedef enum { |
| 28 | SKIP_NOT, // condition is a constant, produce code |
| 29 | SKIP_YES, // condition is a constant, do NOT produce code |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 30 | SKIP_UNKNOWN // condition is not a constant, produce code |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 31 | } skip_T; |
| 32 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Chain of jump instructions where the end label needs to be set. |
| 35 | */ |
| 36 | typedef struct endlabel_S endlabel_T; |
| 37 | struct endlabel_S { |
| 38 | endlabel_T *el_next; // chain end_label locations |
| 39 | int el_end_label; // instruction idx where to set end |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * info specific for the scope of :if / elseif / else |
| 44 | */ |
| 45 | typedef struct { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 46 | int is_seen_else; |
| 47 | int is_had_return; // every block ends in :return |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 48 | int is_if_label; // instruction idx at IF or ELSEIF |
| 49 | endlabel_T *is_end_label; // instructions to set end label |
| 50 | } ifscope_T; |
| 51 | |
| 52 | /* |
| 53 | * info specific for the scope of :while |
| 54 | */ |
| 55 | typedef struct { |
| 56 | int ws_top_label; // instruction idx at WHILE |
| 57 | endlabel_T *ws_end_label; // instructions to set end |
| 58 | } whilescope_T; |
| 59 | |
| 60 | /* |
| 61 | * info specific for the scope of :for |
| 62 | */ |
| 63 | typedef struct { |
| 64 | int fs_top_label; // instruction idx at FOR |
| 65 | endlabel_T *fs_end_label; // break instructions |
| 66 | } forscope_T; |
| 67 | |
| 68 | /* |
| 69 | * info specific for the scope of :try |
| 70 | */ |
| 71 | typedef struct { |
| 72 | int ts_try_label; // instruction idx at TRY |
| 73 | endlabel_T *ts_end_label; // jump to :finally or :endtry |
| 74 | int ts_catch_label; // instruction idx of last CATCH |
| 75 | int ts_caught_all; // "catch" without argument encountered |
| 76 | } tryscope_T; |
| 77 | |
| 78 | typedef enum { |
| 79 | NO_SCOPE, |
| 80 | IF_SCOPE, |
| 81 | WHILE_SCOPE, |
| 82 | FOR_SCOPE, |
| 83 | TRY_SCOPE, |
| 84 | BLOCK_SCOPE |
| 85 | } scopetype_T; |
| 86 | |
| 87 | /* |
| 88 | * Info for one scope, pointed to by "ctx_scope". |
| 89 | */ |
| 90 | typedef struct scope_S scope_T; |
| 91 | struct scope_S { |
| 92 | scope_T *se_outer; // scope containing this one |
| 93 | scopetype_T se_type; |
| 94 | int se_local_count; // ctx_locals.ga_len before scope |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 95 | skip_T se_skip_save; // ctx_skip before the block |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 96 | union { |
| 97 | ifscope_T se_if; |
| 98 | whilescope_T se_while; |
| 99 | forscope_T se_for; |
| 100 | tryscope_T se_try; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 101 | } se_u; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Entry for "ctx_locals". Used for arguments and local variables. |
| 106 | */ |
| 107 | typedef struct { |
| 108 | char_u *lv_name; |
| 109 | type_T *lv_type; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 110 | int lv_idx; // index of the variable on the stack |
| 111 | int lv_from_outer; // when TRUE using ctx_outer scope |
| 112 | int lv_const; // when TRUE cannot be assigned to |
| 113 | int lv_arg; // when TRUE this is an argument |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 114 | } lvar_T; |
| 115 | |
| 116 | /* |
| 117 | * Context for compiling lines of Vim script. |
| 118 | * Stores info about the local variables and condition stack. |
| 119 | */ |
| 120 | struct cctx_S { |
| 121 | ufunc_T *ctx_ufunc; // current function |
| 122 | int ctx_lnum; // line number in current function |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 123 | char_u *ctx_line_start; // start of current line or NULL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 124 | garray_T ctx_instr; // generated instructions |
| 125 | |
| 126 | garray_T ctx_locals; // currently visible local variables |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 127 | int ctx_locals_count; // total number of local variables |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 128 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 129 | int ctx_closure_count; // number of closures created in the |
| 130 | // function |
| 131 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 132 | garray_T ctx_imports; // imported items |
| 133 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 134 | skip_T ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 135 | scope_T *ctx_scope; // current scope, NULL at toplevel |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 136 | int ctx_had_return; // last seen statement was "return" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 137 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 138 | cctx_T *ctx_outer; // outer scope for lambda or nested |
| 139 | // function |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 140 | int ctx_outer_used; // var in ctx_outer was used |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 141 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 142 | garray_T ctx_type_stack; // type of each item on the stack |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 143 | garray_T *ctx_type_list; // list of pointers to allocated types |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static char e_var_notfound[] = N_("E1001: variable not found: %s"); |
| 147 | static char e_syntax_at[] = N_("E1002: Syntax error at %s"); |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 148 | static char e_used_as_arg[] = N_("E1006: %s is used as an argument"); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 149 | static char e_cannot_use_void[] = N_("E1031: Cannot use void value"); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 150 | static char e_namespace[] = N_("E1075: Namespace not supported: %s"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 151 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 152 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 153 | static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 154 | |
| 155 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 156 | * Lookup variable "name" in the local scope and return it. |
| 157 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 158 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 159 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 160 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 161 | { |
| 162 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 163 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 164 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 165 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 166 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 167 | |
| 168 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 169 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 170 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 171 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 172 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 173 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 174 | { |
| 175 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 176 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 177 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 178 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 179 | |
| 180 | // Find local in outer function scope. |
| 181 | if (cctx->ctx_outer != NULL) |
| 182 | { |
| 183 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 184 | if (lvar != NULL) |
| 185 | { |
| 186 | // TODO: are there situations we should not mark the outer scope as |
| 187 | // used? |
| 188 | cctx->ctx_outer_used = TRUE; |
| 189 | lvar->lv_from_outer = TRUE; |
| 190 | return lvar; |
| 191 | } |
| 192 | } |
| 193 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 194 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 198 | * Lookup an argument in the current function and an enclosing function. |
| 199 | * Returns the argument index in "idxp" |
| 200 | * Returns the argument type in "type" |
| 201 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 202 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 203 | */ |
| 204 | static int |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 205 | lookup_arg( |
| 206 | char_u *name, |
| 207 | size_t len, |
| 208 | int *idxp, |
| 209 | type_T **type, |
| 210 | int *gen_load_outer, |
| 211 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 212 | { |
| 213 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 214 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 215 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 216 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 217 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 218 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 219 | { |
| 220 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 221 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 222 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 223 | { |
| 224 | if (idxp != NULL) |
| 225 | { |
| 226 | // Arguments are located above the frame pointer. One further |
| 227 | // if there is a vararg argument |
| 228 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 229 | + STACK_FRAME_SIZE) |
| 230 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 231 | |
| 232 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 233 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 234 | else |
| 235 | *type = &t_any; |
| 236 | } |
| 237 | return OK; |
| 238 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 239 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 240 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 241 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 242 | if (va_name != NULL |
| 243 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 244 | { |
| 245 | if (idxp != NULL) |
| 246 | { |
| 247 | // varargs is always the last argument |
| 248 | *idxp = -STACK_FRAME_SIZE - 1; |
| 249 | *type = cctx->ctx_ufunc->uf_va_type; |
| 250 | } |
| 251 | return OK; |
| 252 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 253 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 254 | if (cctx->ctx_outer != NULL) |
| 255 | { |
| 256 | // Lookup the name for an argument of the outer function. |
| 257 | if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
| 258 | == OK) |
| 259 | { |
| 260 | *gen_load_outer = TRUE; |
| 261 | return OK; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Lookup a variable in the current script. |
| 270 | * Returns OK or FAIL. |
| 271 | */ |
| 272 | static int |
| 273 | lookup_script(char_u *name, size_t len) |
| 274 | { |
| 275 | int cc; |
| 276 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 277 | dictitem_T *di; |
| 278 | |
| 279 | cc = name[len]; |
| 280 | name[len] = NUL; |
| 281 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 282 | name[len] = cc; |
| 283 | return di == NULL ? FAIL: OK; |
| 284 | } |
| 285 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 286 | /* |
| 287 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 288 | * compilation context "cctx". |
| 289 | * Return FAIL and give an error if it defined. |
| 290 | */ |
| 291 | int |
Bram Moolenaar | cbb6bdc | 2020-07-06 21:53:17 +0200 | [diff] [blame] | 292 | check_defined(char_u *p, size_t len, cctx_T *cctx) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 293 | { |
| 294 | if (lookup_script(p, len) == OK |
| 295 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 296 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 297 | || find_imported(p, len, cctx) != NULL))) |
| 298 | { |
| 299 | semsg("E1073: imported name already defined: %s", p); |
| 300 | return FAIL; |
| 301 | } |
| 302 | return OK; |
| 303 | } |
| 304 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 305 | /* |
| 306 | * Allocate memory for a type_T and add the pointer to type_gap, so that it can |
| 307 | * be freed later. |
| 308 | */ |
| 309 | static type_T * |
| 310 | alloc_type(garray_T *type_gap) |
| 311 | { |
| 312 | type_T *type; |
| 313 | |
| 314 | if (ga_grow(type_gap, 1) == FAIL) |
| 315 | return NULL; |
| 316 | type = ALLOC_CLEAR_ONE(type_T); |
| 317 | if (type != NULL) |
| 318 | { |
| 319 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type; |
| 320 | ++type_gap->ga_len; |
| 321 | } |
| 322 | return type; |
| 323 | } |
| 324 | |
Bram Moolenaar | 6110e79 | 2020-07-08 19:35:21 +0200 | [diff] [blame] | 325 | void |
| 326 | clear_type_list(garray_T *gap) |
| 327 | { |
| 328 | while (gap->ga_len > 0) |
| 329 | vim_free(((type_T **)gap->ga_data)[--gap->ga_len]); |
| 330 | ga_clear(gap); |
| 331 | } |
| 332 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 333 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 334 | get_list_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 335 | { |
| 336 | type_T *type; |
| 337 | |
| 338 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 339 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 340 | return &t_list_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 341 | if (member_type->tt_type == VAR_VOID |
| 342 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 343 | return &t_list_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 344 | if (member_type->tt_type == VAR_BOOL) |
| 345 | return &t_list_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 346 | if (member_type->tt_type == VAR_NUMBER) |
| 347 | return &t_list_number; |
| 348 | if (member_type->tt_type == VAR_STRING) |
| 349 | return &t_list_string; |
| 350 | |
| 351 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 352 | type = alloc_type(type_gap); |
| 353 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 354 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 355 | type->tt_type = VAR_LIST; |
| 356 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 357 | type->tt_argcount = 0; |
| 358 | type->tt_args = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 359 | return type; |
| 360 | } |
| 361 | |
| 362 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 363 | get_dict_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 364 | { |
| 365 | type_T *type; |
| 366 | |
| 367 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 368 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 369 | return &t_dict_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 370 | if (member_type->tt_type == VAR_VOID |
| 371 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 372 | return &t_dict_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 373 | if (member_type->tt_type == VAR_BOOL) |
| 374 | return &t_dict_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 375 | if (member_type->tt_type == VAR_NUMBER) |
| 376 | return &t_dict_number; |
| 377 | if (member_type->tt_type == VAR_STRING) |
| 378 | return &t_dict_string; |
| 379 | |
| 380 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 381 | type = alloc_type(type_gap); |
| 382 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 383 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 384 | type->tt_type = VAR_DICT; |
| 385 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 386 | type->tt_argcount = 0; |
| 387 | type->tt_args = NULL; |
| 388 | return type; |
| 389 | } |
| 390 | |
| 391 | /* |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 392 | * Allocate a new type for a function. |
| 393 | */ |
| 394 | static type_T * |
| 395 | alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 396 | { |
| 397 | type_T *type = alloc_type(type_gap); |
| 398 | |
| 399 | if (type == NULL) |
| 400 | return &t_any; |
| 401 | type->tt_type = VAR_FUNC; |
| 402 | type->tt_member = ret_type; |
| 403 | type->tt_argcount = argcount; |
| 404 | type->tt_args = NULL; |
| 405 | return type; |
| 406 | } |
| 407 | |
| 408 | /* |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 409 | * Get a function type, based on the return type "ret_type". |
| 410 | * If "argcount" is -1 or 0 a predefined type can be used. |
| 411 | * If "argcount" > 0 always create a new type, so that arguments can be added. |
| 412 | */ |
| 413 | static type_T * |
| 414 | get_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 415 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 416 | // recognize commonly used types |
| 417 | if (argcount <= 0) |
| 418 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 419 | if (ret_type == &t_unknown) |
| 420 | { |
| 421 | // (argcount == 0) is not possible |
| 422 | return &t_func_unknown; |
| 423 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 424 | if (ret_type == &t_void) |
| 425 | { |
| 426 | if (argcount == 0) |
| 427 | return &t_func_0_void; |
| 428 | else |
| 429 | return &t_func_void; |
| 430 | } |
| 431 | if (ret_type == &t_any) |
| 432 | { |
| 433 | if (argcount == 0) |
| 434 | return &t_func_0_any; |
| 435 | else |
| 436 | return &t_func_any; |
| 437 | } |
| 438 | if (ret_type == &t_number) |
| 439 | { |
| 440 | if (argcount == 0) |
| 441 | return &t_func_0_number; |
| 442 | else |
| 443 | return &t_func_number; |
| 444 | } |
| 445 | if (ret_type == &t_string) |
| 446 | { |
| 447 | if (argcount == 0) |
| 448 | return &t_func_0_string; |
| 449 | else |
| 450 | return &t_func_string; |
| 451 | } |
| 452 | } |
| 453 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 454 | return alloc_func_type(ret_type, argcount, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 455 | } |
| 456 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 457 | /* |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 458 | * For a function type, reserve space for "argcount" argument types (including |
| 459 | * vararg). |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 460 | */ |
| 461 | static int |
| 462 | func_type_add_arg_types( |
| 463 | type_T *functype, |
| 464 | int argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 465 | garray_T *type_gap) |
| 466 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 467 | // To make it easy to free the space needed for the argument types, add the |
| 468 | // pointer to type_gap. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 469 | if (ga_grow(type_gap, 1) == FAIL) |
| 470 | return FAIL; |
| 471 | functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); |
| 472 | if (functype->tt_args == NULL) |
| 473 | return FAIL; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 474 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = |
| 475 | (void *)functype->tt_args; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 476 | ++type_gap->ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 477 | return OK; |
| 478 | } |
| 479 | |
| 480 | /* |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 481 | * Get a type_T for a typval_T. |
| 482 | * "type_list" is used to temporarily create types in. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 483 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 484 | type_T * |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 485 | typval2type(typval_T *tv, garray_T *type_gap) |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 486 | { |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 487 | type_T *actual; |
| 488 | type_T *member_type; |
| 489 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 490 | if (tv->v_type == VAR_NUMBER) |
| 491 | return &t_number; |
| 492 | if (tv->v_type == VAR_BOOL) |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 493 | return &t_bool; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 494 | if (tv->v_type == VAR_STRING) |
| 495 | return &t_string; |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 496 | |
| 497 | if (tv->v_type == VAR_LIST |
| 498 | && tv->vval.v_list != NULL |
| 499 | && tv->vval.v_list->lv_first != NULL) |
| 500 | { |
| 501 | // Use the type of the first member, it is the most specific. |
| 502 | member_type = typval2type(&tv->vval.v_list->lv_first->li_tv, type_gap); |
| 503 | return get_list_type(member_type, type_gap); |
| 504 | } |
| 505 | |
| 506 | if (tv->v_type == VAR_DICT |
| 507 | && tv->vval.v_dict != NULL |
| 508 | && tv->vval.v_dict->dv_hashtab.ht_used > 0) |
| 509 | { |
| 510 | dict_iterator_T iter; |
| 511 | typval_T *value; |
| 512 | |
| 513 | // Use the type of the first value, it is the most specific. |
| 514 | dict_iterate_start(tv, &iter); |
| 515 | dict_iterate_next(&iter, &value); |
| 516 | member_type = typval2type(value, type_gap); |
| 517 | return get_dict_type(member_type, type_gap); |
| 518 | } |
| 519 | |
| 520 | if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL) |
| 521 | { |
| 522 | char_u *name = NULL; |
| 523 | ufunc_T *ufunc = NULL; |
| 524 | |
| 525 | if (tv->v_type == VAR_PARTIAL) |
| 526 | { |
| 527 | if (tv->vval.v_partial->pt_func != NULL) |
| 528 | ufunc = tv->vval.v_partial->pt_func; |
| 529 | else |
| 530 | name = tv->vval.v_partial->pt_name; |
| 531 | } |
| 532 | else |
| 533 | name = tv->vval.v_string; |
| 534 | if (name != NULL) |
| 535 | // TODO: how about a builtin function? |
| 536 | ufunc = find_func(name, FALSE, NULL); |
| 537 | if (ufunc != NULL && ufunc->uf_func_type != NULL) |
| 538 | return ufunc->uf_func_type; |
| 539 | } |
| 540 | |
| 541 | actual = alloc_type(type_gap); |
| 542 | if (actual == NULL) |
| 543 | return NULL; |
| 544 | actual->tt_type = tv->v_type; |
| 545 | actual->tt_member = &t_any; |
| 546 | |
| 547 | return actual; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Get a type_T for a typval_T, used for v: variables. |
| 552 | * "type_list" is used to temporarily create types in. |
| 553 | */ |
| 554 | type_T * |
| 555 | typval2type_vimvar(typval_T *tv, garray_T *type_gap) |
| 556 | { |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 557 | if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles |
| 558 | return &t_list_string; |
| 559 | if (tv->v_type == VAR_DICT) // e.g. for v:completed_item |
| 560 | return &t_dict_any; |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 561 | return typval2type(tv, type_gap); |
| 562 | } |
| 563 | |
| 564 | |
| 565 | /* |
| 566 | * Return FAIL if "expected" and "actual" don't match. |
| 567 | */ |
| 568 | int |
| 569 | check_typval_type(type_T *expected, typval_T *actual_tv) |
| 570 | { |
| 571 | garray_T type_list; |
| 572 | type_T *actual_type; |
| 573 | int res = FAIL; |
| 574 | |
| 575 | ga_init2(&type_list, sizeof(type_T *), 10); |
| 576 | actual_type = typval2type(actual_tv, &type_list); |
| 577 | if (actual_type != NULL) |
| 578 | res = check_type(expected, actual_type, TRUE); |
| 579 | clear_type_list(&type_list); |
| 580 | return res; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 583 | static void |
| 584 | type_mismatch(type_T *expected, type_T *actual) |
| 585 | { |
| 586 | char *tofree1, *tofree2; |
| 587 | |
| 588 | semsg(_("E1013: type mismatch, expected %s but got %s"), |
| 589 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 590 | vim_free(tofree1); |
| 591 | vim_free(tofree2); |
| 592 | } |
| 593 | |
| 594 | static void |
| 595 | arg_type_mismatch(type_T *expected, type_T *actual, int argidx) |
| 596 | { |
| 597 | char *tofree1, *tofree2; |
| 598 | |
| 599 | semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"), |
| 600 | argidx, |
| 601 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 602 | vim_free(tofree1); |
| 603 | vim_free(tofree2); |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * Check if the expected and actual types match. |
| 608 | * Does not allow for assigning "any" to a specific type. |
| 609 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 610 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 611 | check_type(type_T *expected, type_T *actual, int give_msg) |
| 612 | { |
| 613 | int ret = OK; |
| 614 | |
| 615 | // When expected is "unknown" we accept any actual type. |
| 616 | // When expected is "any" we accept any actual type except "void". |
| 617 | if (expected->tt_type != VAR_UNKNOWN |
| 618 | && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) |
| 619 | |
| 620 | { |
| 621 | if (expected->tt_type != actual->tt_type) |
| 622 | { |
| 623 | if (give_msg) |
| 624 | type_mismatch(expected, actual); |
| 625 | return FAIL; |
| 626 | } |
| 627 | if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) |
| 628 | { |
| 629 | // "unknown" is used for an empty list or dict |
| 630 | if (actual->tt_member != &t_unknown) |
| 631 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 632 | } |
| 633 | else if (expected->tt_type == VAR_FUNC) |
| 634 | { |
| 635 | if (expected->tt_member != &t_unknown) |
| 636 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 637 | if (ret == OK && expected->tt_argcount != -1 |
| 638 | && (actual->tt_argcount < expected->tt_min_argcount |
| 639 | || actual->tt_argcount > expected->tt_argcount)) |
| 640 | ret = FAIL; |
| 641 | } |
| 642 | if (ret == FAIL && give_msg) |
| 643 | type_mismatch(expected, actual); |
| 644 | } |
| 645 | return ret; |
| 646 | } |
| 647 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 648 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 649 | ///////////////////////////////////////////////////////////////////// |
| 650 | // Following generate_ functions expect the caller to call ga_grow(). |
| 651 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 652 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 653 | #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] | 654 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 655 | /* |
| 656 | * Generate an instruction without arguments. |
| 657 | * Returns a pointer to the new instruction, NULL if failed. |
| 658 | */ |
| 659 | static isn_T * |
| 660 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 661 | { |
| 662 | garray_T *instr = &cctx->ctx_instr; |
| 663 | isn_T *isn; |
| 664 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 665 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 666 | if (ga_grow(instr, 1) == FAIL) |
| 667 | return NULL; |
| 668 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 669 | isn->isn_type = isn_type; |
| 670 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 671 | ++instr->ga_len; |
| 672 | |
| 673 | return isn; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * Generate an instruction without arguments. |
| 678 | * "drop" will be removed from the stack. |
| 679 | * Returns a pointer to the new instruction, NULL if failed. |
| 680 | */ |
| 681 | static isn_T * |
| 682 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 683 | { |
| 684 | garray_T *stack = &cctx->ctx_type_stack; |
| 685 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 686 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 687 | stack->ga_len -= drop; |
| 688 | return generate_instr(cctx, isn_type); |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 693 | */ |
| 694 | static isn_T * |
| 695 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 696 | { |
| 697 | isn_T *isn; |
| 698 | garray_T *stack = &cctx->ctx_type_stack; |
| 699 | |
| 700 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 701 | return NULL; |
| 702 | |
| 703 | if (ga_grow(stack, 1) == FAIL) |
| 704 | return NULL; |
| 705 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 706 | ++stack->ga_len; |
| 707 | |
| 708 | return isn; |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 713 | */ |
| 714 | static int |
| 715 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 716 | { |
| 717 | isn_T *isn; |
| 718 | garray_T *stack = &cctx->ctx_type_stack; |
| 719 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 720 | |
| 721 | if ((*type)->tt_type == VAR_STRING) |
| 722 | return OK; |
| 723 | *type = &t_string; |
| 724 | |
| 725 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 726 | return FAIL; |
| 727 | isn->isn_arg.number = offset; |
| 728 | |
| 729 | return OK; |
| 730 | } |
| 731 | |
| 732 | static int |
| 733 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 734 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 735 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 736 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 737 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 738 | { |
| 739 | if (*op == '+') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 740 | emsg(_("E1051: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 741 | else |
| 742 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 743 | return FAIL; |
| 744 | } |
| 745 | return OK; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Generate an instruction with two arguments. The instruction depends on the |
| 750 | * type of the arguments. |
| 751 | */ |
| 752 | static int |
| 753 | generate_two_op(cctx_T *cctx, char_u *op) |
| 754 | { |
| 755 | garray_T *stack = &cctx->ctx_type_stack; |
| 756 | type_T *type1; |
| 757 | type_T *type2; |
| 758 | vartype_T vartype; |
| 759 | isn_T *isn; |
| 760 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 761 | RETURN_OK_IF_SKIP(cctx); |
| 762 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 763 | // Get the known type of the two items on the stack. If they are matching |
| 764 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 765 | // checking. |
| 766 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 767 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 768 | vartype = VAR_ANY; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 769 | if (type1->tt_type == type2->tt_type |
| 770 | && (type1->tt_type == VAR_NUMBER |
| 771 | || type1->tt_type == VAR_LIST |
| 772 | #ifdef FEAT_FLOAT |
| 773 | || type1->tt_type == VAR_FLOAT |
| 774 | #endif |
| 775 | || type1->tt_type == VAR_BLOB)) |
| 776 | vartype = type1->tt_type; |
| 777 | |
| 778 | switch (*op) |
| 779 | { |
| 780 | case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 781 | && type1->tt_type != VAR_ANY |
| 782 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 783 | && check_number_or_float( |
| 784 | type1->tt_type, type2->tt_type, op) == FAIL) |
| 785 | return FAIL; |
| 786 | isn = generate_instr_drop(cctx, |
| 787 | vartype == VAR_NUMBER ? ISN_OPNR |
| 788 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 789 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 790 | #ifdef FEAT_FLOAT |
| 791 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 792 | #endif |
| 793 | : ISN_OPANY, 1); |
| 794 | if (isn != NULL) |
| 795 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 796 | break; |
| 797 | |
| 798 | case '-': |
| 799 | case '*': |
| 800 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 801 | op) == FAIL) |
| 802 | return FAIL; |
| 803 | if (vartype == VAR_NUMBER) |
| 804 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 805 | #ifdef FEAT_FLOAT |
| 806 | else if (vartype == VAR_FLOAT) |
| 807 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 808 | #endif |
| 809 | else |
| 810 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 811 | if (isn != NULL) |
| 812 | isn->isn_arg.op.op_type = *op == '*' |
| 813 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 814 | break; |
| 815 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 816 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 817 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 818 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 819 | && type2->tt_type != VAR_NUMBER)) |
| 820 | { |
| 821 | emsg(_("E1035: % requires number arguments")); |
| 822 | return FAIL; |
| 823 | } |
| 824 | isn = generate_instr_drop(cctx, |
| 825 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 826 | if (isn != NULL) |
| 827 | isn->isn_arg.op.op_type = EXPR_REM; |
| 828 | break; |
| 829 | } |
| 830 | |
| 831 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 832 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 833 | { |
| 834 | type_T *type = &t_any; |
| 835 | |
| 836 | #ifdef FEAT_FLOAT |
| 837 | // float+number and number+float results in float |
| 838 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 839 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 840 | type = &t_float; |
| 841 | #endif |
| 842 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 843 | } |
| 844 | |
| 845 | return OK; |
| 846 | } |
| 847 | |
| 848 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 849 | * Get the instruction to use for comparing "type1" with "type2" |
| 850 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 851 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 852 | static isntype_T |
| 853 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 854 | { |
| 855 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 856 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 857 | if (type1 == VAR_UNKNOWN) |
| 858 | type1 = VAR_ANY; |
| 859 | if (type2 == VAR_UNKNOWN) |
| 860 | type2 = VAR_ANY; |
| 861 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 862 | if (type1 == type2) |
| 863 | { |
| 864 | switch (type1) |
| 865 | { |
| 866 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 867 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 868 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 869 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 870 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 871 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 872 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 873 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 874 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 875 | default: isntype = ISN_COMPAREANY; break; |
| 876 | } |
| 877 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 878 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 879 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 880 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 881 | isntype = ISN_COMPAREANY; |
| 882 | |
| 883 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 884 | && (isntype == ISN_COMPAREBOOL |
| 885 | || isntype == ISN_COMPARESPECIAL |
| 886 | || isntype == ISN_COMPARENR |
| 887 | || isntype == ISN_COMPAREFLOAT)) |
| 888 | { |
| 889 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 890 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 891 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 892 | } |
| 893 | if (isntype == ISN_DROP |
| 894 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 895 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 896 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 897 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 898 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 899 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 900 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 901 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 902 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 903 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 904 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 905 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 906 | return isntype; |
| 907 | } |
| 908 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 909 | int |
| 910 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 911 | { |
| 912 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 913 | return FAIL; |
| 914 | return OK; |
| 915 | } |
| 916 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 917 | /* |
| 918 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 919 | */ |
| 920 | static int |
| 921 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 922 | { |
| 923 | isntype_T isntype; |
| 924 | isn_T *isn; |
| 925 | garray_T *stack = &cctx->ctx_type_stack; |
| 926 | vartype_T type1; |
| 927 | vartype_T type2; |
| 928 | |
| 929 | RETURN_OK_IF_SKIP(cctx); |
| 930 | |
| 931 | // Get the known type of the two items on the stack. If they are matching |
| 932 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 933 | // checking. |
| 934 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 935 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 936 | isntype = get_compare_isn(exptype, type1, type2); |
| 937 | if (isntype == ISN_DROP) |
| 938 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 939 | |
| 940 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 941 | return FAIL; |
| 942 | isn->isn_arg.op.op_type = exptype; |
| 943 | isn->isn_arg.op.op_ic = ic; |
| 944 | |
| 945 | // takes two arguments, puts one bool back |
| 946 | if (stack->ga_len >= 2) |
| 947 | { |
| 948 | --stack->ga_len; |
| 949 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 950 | } |
| 951 | |
| 952 | return OK; |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Generate an ISN_2BOOL instruction. |
| 957 | */ |
| 958 | static int |
| 959 | generate_2BOOL(cctx_T *cctx, int invert) |
| 960 | { |
| 961 | isn_T *isn; |
| 962 | garray_T *stack = &cctx->ctx_type_stack; |
| 963 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 964 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 965 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 966 | return FAIL; |
| 967 | isn->isn_arg.number = invert; |
| 968 | |
| 969 | // type becomes bool |
| 970 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 971 | |
| 972 | return OK; |
| 973 | } |
| 974 | |
| 975 | static int |
| 976 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 977 | { |
| 978 | isn_T *isn; |
| 979 | garray_T *stack = &cctx->ctx_type_stack; |
| 980 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 981 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 982 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 983 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 984 | // TODO: whole type, e.g. for a function also arg and return types |
| 985 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 986 | isn->isn_arg.type.ct_off = offset; |
| 987 | |
| 988 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 989 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 990 | |
| 991 | return OK; |
| 992 | } |
| 993 | |
| 994 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 995 | * Check that |
| 996 | * - "actual" is "expected" type or |
| 997 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 998 | * - return FAIL. |
| 999 | */ |
| 1000 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1001 | need_type( |
| 1002 | type_T *actual, |
| 1003 | type_T *expected, |
| 1004 | int offset, |
| 1005 | cctx_T *cctx, |
| 1006 | int silent) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1007 | { |
| 1008 | if (check_type(expected, actual, FALSE) == OK) |
| 1009 | return OK; |
| 1010 | if (actual->tt_type != VAR_ANY |
| 1011 | && actual->tt_type != VAR_UNKNOWN |
| 1012 | && !(actual->tt_type == VAR_FUNC |
| 1013 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 1014 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1015 | if (!silent) |
| 1016 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1017 | return FAIL; |
| 1018 | } |
| 1019 | generate_TYPECHECK(cctx, expected, offset); |
| 1020 | return OK; |
| 1021 | } |
| 1022 | |
| 1023 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1024 | * Generate an ISN_PUSHNR instruction. |
| 1025 | */ |
| 1026 | static int |
| 1027 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 1028 | { |
| 1029 | isn_T *isn; |
| 1030 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1031 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1032 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 1033 | return FAIL; |
| 1034 | isn->isn_arg.number = number; |
| 1035 | |
| 1036 | return OK; |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * Generate an ISN_PUSHBOOL instruction. |
| 1041 | */ |
| 1042 | static int |
| 1043 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 1044 | { |
| 1045 | isn_T *isn; |
| 1046 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1047 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1048 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 1049 | return FAIL; |
| 1050 | isn->isn_arg.number = number; |
| 1051 | |
| 1052 | return OK; |
| 1053 | } |
| 1054 | |
| 1055 | /* |
| 1056 | * Generate an ISN_PUSHSPEC instruction. |
| 1057 | */ |
| 1058 | static int |
| 1059 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 1060 | { |
| 1061 | isn_T *isn; |
| 1062 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1063 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1064 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 1065 | return FAIL; |
| 1066 | isn->isn_arg.number = number; |
| 1067 | |
| 1068 | return OK; |
| 1069 | } |
| 1070 | |
| 1071 | #ifdef FEAT_FLOAT |
| 1072 | /* |
| 1073 | * Generate an ISN_PUSHF instruction. |
| 1074 | */ |
| 1075 | static int |
| 1076 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 1077 | { |
| 1078 | isn_T *isn; |
| 1079 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1080 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1081 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 1082 | return FAIL; |
| 1083 | isn->isn_arg.fnumber = fnumber; |
| 1084 | |
| 1085 | return OK; |
| 1086 | } |
| 1087 | #endif |
| 1088 | |
| 1089 | /* |
| 1090 | * Generate an ISN_PUSHS instruction. |
| 1091 | * Consumes "str". |
| 1092 | */ |
| 1093 | static int |
| 1094 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 1095 | { |
| 1096 | isn_T *isn; |
| 1097 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1098 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1099 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 1100 | return FAIL; |
| 1101 | isn->isn_arg.string = str; |
| 1102 | |
| 1103 | return OK; |
| 1104 | } |
| 1105 | |
| 1106 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1107 | * Generate an ISN_PUSHCHANNEL instruction. |
| 1108 | * Consumes "channel". |
| 1109 | */ |
| 1110 | static int |
| 1111 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 1112 | { |
| 1113 | isn_T *isn; |
| 1114 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1115 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1116 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 1117 | return FAIL; |
| 1118 | isn->isn_arg.channel = channel; |
| 1119 | |
| 1120 | return OK; |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * Generate an ISN_PUSHJOB instruction. |
| 1125 | * Consumes "job". |
| 1126 | */ |
| 1127 | static int |
| 1128 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 1129 | { |
| 1130 | isn_T *isn; |
| 1131 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1132 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1133 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1134 | return FAIL; |
| 1135 | isn->isn_arg.job = job; |
| 1136 | |
| 1137 | return OK; |
| 1138 | } |
| 1139 | |
| 1140 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1141 | * Generate an ISN_PUSHBLOB instruction. |
| 1142 | * Consumes "blob". |
| 1143 | */ |
| 1144 | static int |
| 1145 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1146 | { |
| 1147 | isn_T *isn; |
| 1148 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1149 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1150 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1151 | return FAIL; |
| 1152 | isn->isn_arg.blob = blob; |
| 1153 | |
| 1154 | return OK; |
| 1155 | } |
| 1156 | |
| 1157 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1158 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1159 | * Consumes "name". |
| 1160 | */ |
| 1161 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1162 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1163 | { |
| 1164 | isn_T *isn; |
| 1165 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1166 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1167 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1168 | return FAIL; |
| 1169 | isn->isn_arg.string = name; |
| 1170 | |
| 1171 | return OK; |
| 1172 | } |
| 1173 | |
| 1174 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1175 | * Generate an ISN_GETITEM instruction with "index". |
| 1176 | */ |
| 1177 | static int |
| 1178 | generate_GETITEM(cctx_T *cctx, int index) |
| 1179 | { |
| 1180 | isn_T *isn; |
| 1181 | garray_T *stack = &cctx->ctx_type_stack; |
| 1182 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1183 | type_T *item_type = &t_any; |
| 1184 | |
| 1185 | RETURN_OK_IF_SKIP(cctx); |
| 1186 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1187 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1188 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1189 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1190 | emsg(_(e_listreq)); |
| 1191 | return FAIL; |
| 1192 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1193 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1194 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1195 | return FAIL; |
| 1196 | isn->isn_arg.number = index; |
| 1197 | |
| 1198 | // add the item type to the type stack |
| 1199 | if (ga_grow(stack, 1) == FAIL) |
| 1200 | return FAIL; |
| 1201 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 1202 | ++stack->ga_len; |
| 1203 | return OK; |
| 1204 | } |
| 1205 | |
| 1206 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 1207 | * Generate an ISN_SLICE instruction with "count". |
| 1208 | */ |
| 1209 | static int |
| 1210 | generate_SLICE(cctx_T *cctx, int count) |
| 1211 | { |
| 1212 | isn_T *isn; |
| 1213 | |
| 1214 | RETURN_OK_IF_SKIP(cctx); |
| 1215 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1216 | return FAIL; |
| 1217 | isn->isn_arg.number = count; |
| 1218 | return OK; |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1223 | */ |
| 1224 | static int |
| 1225 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1226 | { |
| 1227 | isn_T *isn; |
| 1228 | |
| 1229 | RETURN_OK_IF_SKIP(cctx); |
| 1230 | |
| 1231 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1232 | return FAIL; |
| 1233 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1234 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1235 | |
| 1236 | return OK; |
| 1237 | } |
| 1238 | |
| 1239 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1240 | * Generate an ISN_STORE instruction. |
| 1241 | */ |
| 1242 | static int |
| 1243 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1244 | { |
| 1245 | isn_T *isn; |
| 1246 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1247 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1248 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1249 | return FAIL; |
| 1250 | if (name != NULL) |
| 1251 | isn->isn_arg.string = vim_strsave(name); |
| 1252 | else |
| 1253 | isn->isn_arg.number = idx; |
| 1254 | |
| 1255 | return OK; |
| 1256 | } |
| 1257 | |
| 1258 | /* |
| 1259 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1260 | */ |
| 1261 | static int |
| 1262 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1263 | { |
| 1264 | isn_T *isn; |
| 1265 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1266 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1267 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1268 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1269 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1270 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1271 | |
| 1272 | return OK; |
| 1273 | } |
| 1274 | |
| 1275 | /* |
| 1276 | * Generate an ISN_STOREOPT instruction |
| 1277 | */ |
| 1278 | static int |
| 1279 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1280 | { |
| 1281 | isn_T *isn; |
| 1282 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1283 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1284 | if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL) |
| 1285 | return FAIL; |
| 1286 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1287 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1288 | |
| 1289 | return OK; |
| 1290 | } |
| 1291 | |
| 1292 | /* |
| 1293 | * Generate an ISN_LOAD or similar instruction. |
| 1294 | */ |
| 1295 | static int |
| 1296 | generate_LOAD( |
| 1297 | cctx_T *cctx, |
| 1298 | isntype_T isn_type, |
| 1299 | int idx, |
| 1300 | char_u *name, |
| 1301 | type_T *type) |
| 1302 | { |
| 1303 | isn_T *isn; |
| 1304 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1305 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1306 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1307 | return FAIL; |
| 1308 | if (name != NULL) |
| 1309 | isn->isn_arg.string = vim_strsave(name); |
| 1310 | else |
| 1311 | isn->isn_arg.number = idx; |
| 1312 | |
| 1313 | return OK; |
| 1314 | } |
| 1315 | |
| 1316 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1317 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1318 | */ |
| 1319 | static int |
| 1320 | generate_LOADV( |
| 1321 | cctx_T *cctx, |
| 1322 | char_u *name, |
| 1323 | int error) |
| 1324 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1325 | int di_flags; |
| 1326 | int vidx = find_vim_var(name, &di_flags); |
| 1327 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1328 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1329 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1330 | if (vidx < 0) |
| 1331 | { |
| 1332 | if (error) |
| 1333 | semsg(_(e_var_notfound), name); |
| 1334 | return FAIL; |
| 1335 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1336 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1337 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1338 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1342 | * Generate an ISN_UNLET instruction. |
| 1343 | */ |
| 1344 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1345 | 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] | 1346 | { |
| 1347 | isn_T *isn; |
| 1348 | |
| 1349 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1350 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1351 | return FAIL; |
| 1352 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1353 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1354 | |
| 1355 | return OK; |
| 1356 | } |
| 1357 | |
| 1358 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1359 | * Generate an ISN_LOADS instruction. |
| 1360 | */ |
| 1361 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1362 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1363 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1364 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1365 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1366 | int sid, |
| 1367 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1368 | { |
| 1369 | isn_T *isn; |
| 1370 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1371 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1372 | if (isn_type == ISN_LOADS) |
| 1373 | isn = generate_instr_type(cctx, isn_type, type); |
| 1374 | else |
| 1375 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1376 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1377 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1378 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1379 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1380 | |
| 1381 | return OK; |
| 1382 | } |
| 1383 | |
| 1384 | /* |
| 1385 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1386 | */ |
| 1387 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1388 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1389 | cctx_T *cctx, |
| 1390 | isntype_T isn_type, |
| 1391 | int sid, |
| 1392 | int idx, |
| 1393 | type_T *type) |
| 1394 | { |
| 1395 | isn_T *isn; |
| 1396 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1397 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1398 | if (isn_type == ISN_LOADSCRIPT) |
| 1399 | isn = generate_instr_type(cctx, isn_type, type); |
| 1400 | else |
| 1401 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1402 | if (isn == NULL) |
| 1403 | return FAIL; |
| 1404 | isn->isn_arg.script.script_sid = sid; |
| 1405 | isn->isn_arg.script.script_idx = idx; |
| 1406 | return OK; |
| 1407 | } |
| 1408 | |
| 1409 | /* |
| 1410 | * Generate an ISN_NEWLIST instruction. |
| 1411 | */ |
| 1412 | static int |
| 1413 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1414 | { |
| 1415 | isn_T *isn; |
| 1416 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1417 | type_T *type; |
| 1418 | type_T *member; |
| 1419 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1420 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1421 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1422 | return FAIL; |
| 1423 | isn->isn_arg.number = count; |
| 1424 | |
| 1425 | // drop the value types |
| 1426 | stack->ga_len -= count; |
| 1427 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1428 | // 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] | 1429 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1430 | if (count > 0) |
| 1431 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1432 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1433 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1434 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1435 | |
| 1436 | // add the list type to the type stack |
| 1437 | if (ga_grow(stack, 1) == FAIL) |
| 1438 | return FAIL; |
| 1439 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1440 | ++stack->ga_len; |
| 1441 | |
| 1442 | return OK; |
| 1443 | } |
| 1444 | |
| 1445 | /* |
| 1446 | * Generate an ISN_NEWDICT instruction. |
| 1447 | */ |
| 1448 | static int |
| 1449 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1450 | { |
| 1451 | isn_T *isn; |
| 1452 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1453 | type_T *type; |
| 1454 | type_T *member; |
| 1455 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1456 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1457 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1458 | return FAIL; |
| 1459 | isn->isn_arg.number = count; |
| 1460 | |
| 1461 | // drop the key and value types |
| 1462 | stack->ga_len -= 2 * count; |
| 1463 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1464 | // Use the first value type for the list member type. Use "void" for an |
| 1465 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1466 | if (count > 0) |
| 1467 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1468 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1469 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1470 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1471 | |
| 1472 | // add the dict type to the type stack |
| 1473 | if (ga_grow(stack, 1) == FAIL) |
| 1474 | return FAIL; |
| 1475 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1476 | ++stack->ga_len; |
| 1477 | |
| 1478 | return OK; |
| 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | * Generate an ISN_FUNCREF instruction. |
| 1483 | */ |
| 1484 | static int |
| 1485 | generate_FUNCREF(cctx_T *cctx, int dfunc_idx) |
| 1486 | { |
| 1487 | isn_T *isn; |
| 1488 | garray_T *stack = &cctx->ctx_type_stack; |
| 1489 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1490 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1491 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1492 | return FAIL; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1493 | isn->isn_arg.funcref.fr_func = dfunc_idx; |
| 1494 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1495 | |
| 1496 | if (ga_grow(stack, 1) == FAIL) |
| 1497 | return FAIL; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1498 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1499 | // TODO: argument and return types |
| 1500 | ++stack->ga_len; |
| 1501 | |
| 1502 | return OK; |
| 1503 | } |
| 1504 | |
| 1505 | /* |
| 1506 | * Generate an ISN_JUMP instruction. |
| 1507 | */ |
| 1508 | static int |
| 1509 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1510 | { |
| 1511 | isn_T *isn; |
| 1512 | garray_T *stack = &cctx->ctx_type_stack; |
| 1513 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1514 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1515 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1516 | return FAIL; |
| 1517 | isn->isn_arg.jump.jump_when = when; |
| 1518 | isn->isn_arg.jump.jump_where = where; |
| 1519 | |
| 1520 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1521 | --stack->ga_len; |
| 1522 | |
| 1523 | return OK; |
| 1524 | } |
| 1525 | |
| 1526 | static int |
| 1527 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1528 | { |
| 1529 | isn_T *isn; |
| 1530 | garray_T *stack = &cctx->ctx_type_stack; |
| 1531 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1532 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1533 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1534 | return FAIL; |
| 1535 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1536 | |
| 1537 | if (ga_grow(stack, 1) == FAIL) |
| 1538 | return FAIL; |
| 1539 | // type doesn't matter, will be stored next |
| 1540 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1541 | ++stack->ga_len; |
| 1542 | |
| 1543 | return OK; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
| 1547 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1548 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1549 | * Return FAIL if the number of arguments is wrong. |
| 1550 | */ |
| 1551 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1552 | 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] | 1553 | { |
| 1554 | isn_T *isn; |
| 1555 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1556 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1557 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1558 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1559 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1560 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1561 | argoff = check_internal_func(func_idx, argcount); |
| 1562 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1563 | return FAIL; |
| 1564 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1565 | if (method_call && argoff > 1) |
| 1566 | { |
| 1567 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1568 | return FAIL; |
| 1569 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1570 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1571 | } |
| 1572 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1573 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1574 | return FAIL; |
| 1575 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1576 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1577 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1578 | for (i = 0; i < argcount; ++i) |
| 1579 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1580 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1581 | stack->ga_len -= argcount; // drop the arguments |
| 1582 | if (ga_grow(stack, 1) == FAIL) |
| 1583 | return FAIL; |
| 1584 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1585 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1586 | ++stack->ga_len; // add return value |
| 1587 | |
| 1588 | return OK; |
| 1589 | } |
| 1590 | |
| 1591 | /* |
| 1592 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1593 | * Return FAIL if the number of arguments is wrong. |
| 1594 | */ |
| 1595 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1596 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1597 | { |
| 1598 | isn_T *isn; |
| 1599 | garray_T *stack = &cctx->ctx_type_stack; |
| 1600 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1601 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1602 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1603 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1604 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1605 | { |
| 1606 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1607 | return FAIL; |
| 1608 | } |
| 1609 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1610 | { |
| 1611 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1612 | return FAIL; |
| 1613 | } |
| 1614 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1615 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1616 | { |
| 1617 | int i; |
| 1618 | |
| 1619 | for (i = 0; i < argcount; ++i) |
| 1620 | { |
| 1621 | type_T *expected; |
| 1622 | type_T *actual; |
| 1623 | |
| 1624 | if (i < regular_args) |
| 1625 | { |
| 1626 | if (ufunc->uf_arg_types == NULL) |
| 1627 | continue; |
| 1628 | expected = ufunc->uf_arg_types[i]; |
| 1629 | } |
| 1630 | else |
| 1631 | expected = ufunc->uf_va_type->tt_member; |
| 1632 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1633 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1634 | { |
| 1635 | arg_type_mismatch(expected, actual, i + 1); |
| 1636 | return FAIL; |
| 1637 | } |
| 1638 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1639 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 1640 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1641 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1642 | } |
| 1643 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1644 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1645 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1646 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1647 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1648 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1649 | { |
| 1650 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1651 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1652 | } |
| 1653 | else |
| 1654 | { |
| 1655 | // A user function may be deleted and redefined later, can't use the |
| 1656 | // ufunc pointer, need to look it up again at runtime. |
| 1657 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1658 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1659 | } |
| 1660 | |
| 1661 | stack->ga_len -= argcount; // drop the arguments |
| 1662 | if (ga_grow(stack, 1) == FAIL) |
| 1663 | return FAIL; |
| 1664 | // add return value |
| 1665 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1666 | ++stack->ga_len; |
| 1667 | |
| 1668 | return OK; |
| 1669 | } |
| 1670 | |
| 1671 | /* |
| 1672 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1673 | */ |
| 1674 | static int |
| 1675 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1676 | { |
| 1677 | isn_T *isn; |
| 1678 | garray_T *stack = &cctx->ctx_type_stack; |
| 1679 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1680 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1681 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1682 | return FAIL; |
| 1683 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1684 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1685 | |
| 1686 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1687 | if (ga_grow(stack, 1) == FAIL) |
| 1688 | return FAIL; |
| 1689 | // add return value |
| 1690 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1691 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1692 | |
| 1693 | return OK; |
| 1694 | } |
| 1695 | |
| 1696 | /* |
| 1697 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1698 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1699 | */ |
| 1700 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1701 | generate_PCALL( |
| 1702 | cctx_T *cctx, |
| 1703 | int argcount, |
| 1704 | char_u *name, |
| 1705 | type_T *type, |
| 1706 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1707 | { |
| 1708 | isn_T *isn; |
| 1709 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1710 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1711 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1712 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1713 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1714 | if (type->tt_type == VAR_ANY) |
| 1715 | ret_type = &t_any; |
| 1716 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1717 | { |
| 1718 | if (type->tt_argcount != -1) |
| 1719 | { |
| 1720 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1721 | |
| 1722 | if (argcount < type->tt_min_argcount - varargs) |
| 1723 | { |
| 1724 | semsg(_(e_toofewarg), "[reference]"); |
| 1725 | return FAIL; |
| 1726 | } |
| 1727 | if (!varargs && argcount > type->tt_argcount) |
| 1728 | { |
| 1729 | semsg(_(e_toomanyarg), "[reference]"); |
| 1730 | return FAIL; |
| 1731 | } |
| 1732 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1733 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1734 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1735 | else |
| 1736 | { |
| 1737 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1738 | return FAIL; |
| 1739 | } |
| 1740 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1741 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1742 | return FAIL; |
| 1743 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1744 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1745 | |
| 1746 | stack->ga_len -= argcount; // drop the arguments |
| 1747 | |
| 1748 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1749 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1750 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1751 | // If partial is above the arguments it must be cleared and replaced with |
| 1752 | // the return value. |
| 1753 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1754 | return FAIL; |
| 1755 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1756 | return OK; |
| 1757 | } |
| 1758 | |
| 1759 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1760 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1761 | */ |
| 1762 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1763 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1764 | { |
| 1765 | isn_T *isn; |
| 1766 | garray_T *stack = &cctx->ctx_type_stack; |
| 1767 | type_T *type; |
| 1768 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1769 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1770 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1771 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1772 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1773 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1774 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1775 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1776 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1777 | { |
| 1778 | emsg(_(e_dictreq)); |
| 1779 | return FAIL; |
| 1780 | } |
| 1781 | // change dict type to dict member type |
| 1782 | if (type->tt_type == VAR_DICT) |
| 1783 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1784 | |
| 1785 | return OK; |
| 1786 | } |
| 1787 | |
| 1788 | /* |
| 1789 | * Generate an ISN_ECHO instruction. |
| 1790 | */ |
| 1791 | static int |
| 1792 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1793 | { |
| 1794 | isn_T *isn; |
| 1795 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1796 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1797 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1798 | return FAIL; |
| 1799 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1800 | isn->isn_arg.echo.echo_count = count; |
| 1801 | |
| 1802 | return OK; |
| 1803 | } |
| 1804 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1805 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1806 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1807 | */ |
| 1808 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1809 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1810 | { |
| 1811 | isn_T *isn; |
| 1812 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1813 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1814 | return FAIL; |
| 1815 | isn->isn_arg.number = count; |
| 1816 | |
| 1817 | return OK; |
| 1818 | } |
| 1819 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1820 | static int |
| 1821 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1822 | { |
| 1823 | isn_T *isn; |
| 1824 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1825 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1826 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1827 | return FAIL; |
| 1828 | isn->isn_arg.string = vim_strsave(line); |
| 1829 | return OK; |
| 1830 | } |
| 1831 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1832 | static int |
| 1833 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1834 | { |
| 1835 | isn_T *isn; |
| 1836 | |
| 1837 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1838 | return FAIL; |
| 1839 | isn->isn_arg.number = count; |
| 1840 | return OK; |
| 1841 | } |
| 1842 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1843 | /* |
| 1844 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1845 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1846 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1847 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1848 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1849 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1850 | lvar_T *lvar; |
| 1851 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1852 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1853 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1854 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1855 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1859 | return NULL; |
| 1860 | 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] | 1861 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1862 | // Every local variable uses the next entry on the stack. We could re-use |
| 1863 | // the last ones when leaving a scope, but then variables used in a closure |
| 1864 | // might get overwritten. To keep things simple do not re-use stack |
| 1865 | // entries. This is less efficient, but memory is cheap these days. |
| 1866 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1867 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1868 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1869 | lvar->lv_const = isConst; |
| 1870 | lvar->lv_type = type; |
| 1871 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1872 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1876 | * Remove local variables above "new_top". |
| 1877 | */ |
| 1878 | static void |
| 1879 | unwind_locals(cctx_T *cctx, int new_top) |
| 1880 | { |
| 1881 | if (cctx->ctx_locals.ga_len > new_top) |
| 1882 | { |
| 1883 | int idx; |
| 1884 | lvar_T *lvar; |
| 1885 | |
| 1886 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1887 | { |
| 1888 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1889 | vim_free(lvar->lv_name); |
| 1890 | } |
| 1891 | } |
| 1892 | cctx->ctx_locals.ga_len = new_top; |
| 1893 | } |
| 1894 | |
| 1895 | /* |
| 1896 | * Free all local variables. |
| 1897 | */ |
| 1898 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1899 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1900 | { |
| 1901 | unwind_locals(cctx, 0); |
| 1902 | ga_clear(&cctx->ctx_locals); |
| 1903 | } |
| 1904 | |
| 1905 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1906 | * Skip over a type definition and return a pointer to just after it. |
| 1907 | */ |
| 1908 | char_u * |
| 1909 | skip_type(char_u *start) |
| 1910 | { |
| 1911 | char_u *p = start; |
| 1912 | |
| 1913 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1914 | ++p; |
| 1915 | |
| 1916 | // Skip over "<type>"; this is permissive about white space. |
| 1917 | if (*skipwhite(p) == '<') |
| 1918 | { |
| 1919 | p = skipwhite(p); |
| 1920 | p = skip_type(skipwhite(p + 1)); |
| 1921 | p = skipwhite(p); |
| 1922 | if (*p == '>') |
| 1923 | ++p; |
| 1924 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1925 | else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1]))) |
| 1926 | && STRNCMP("func", start, 4) == 0) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1927 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1928 | if (*p == '(') |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1929 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1930 | // handle func(args): type |
| 1931 | ++p; |
| 1932 | while (*p != ')' && *p != NUL) |
| 1933 | { |
| 1934 | char_u *sp = p; |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1935 | |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1936 | p = skip_type(p); |
| 1937 | if (p == sp) |
| 1938 | return p; // syntax error |
| 1939 | if (*p == ',') |
| 1940 | p = skipwhite(p + 1); |
| 1941 | } |
| 1942 | if (*p == ')') |
| 1943 | { |
| 1944 | if (p[1] == ':') |
| 1945 | p = skip_type(skipwhite(p + 2)); |
| 1946 | else |
| 1947 | p = skipwhite(p + 1); |
| 1948 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1949 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1950 | else |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1951 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1952 | // handle func: return_type |
| 1953 | p = skip_type(skipwhite(p + 1)); |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1954 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1955 | } |
| 1956 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1957 | return p; |
| 1958 | } |
| 1959 | |
| 1960 | /* |
| 1961 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1962 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1963 | * Returns NULL in case of failure. |
| 1964 | */ |
| 1965 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1966 | 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] | 1967 | { |
| 1968 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1969 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1970 | |
| 1971 | if (**arg != '<') |
| 1972 | { |
| 1973 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1974 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1975 | else |
| 1976 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1977 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1978 | } |
| 1979 | *arg = skipwhite(*arg + 1); |
| 1980 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1981 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1982 | |
| 1983 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1984 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1985 | { |
| 1986 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1987 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1988 | } |
| 1989 | ++*arg; |
| 1990 | |
| 1991 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1992 | return get_list_type(member_type, type_gap); |
| 1993 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | /* |
| 1997 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 1998 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1999 | */ |
| 2000 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2001 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2002 | { |
| 2003 | char_u *p = *arg; |
| 2004 | size_t len; |
| 2005 | |
| 2006 | // skip over the first word |
| 2007 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 2008 | ++p; |
| 2009 | len = p - *arg; |
| 2010 | |
| 2011 | switch (**arg) |
| 2012 | { |
| 2013 | case 'a': |
| 2014 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 2015 | { |
| 2016 | *arg += len; |
| 2017 | return &t_any; |
| 2018 | } |
| 2019 | break; |
| 2020 | case 'b': |
| 2021 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 2022 | { |
| 2023 | *arg += len; |
| 2024 | return &t_bool; |
| 2025 | } |
| 2026 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 2027 | { |
| 2028 | *arg += len; |
| 2029 | return &t_blob; |
| 2030 | } |
| 2031 | break; |
| 2032 | case 'c': |
| 2033 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 2034 | { |
| 2035 | *arg += len; |
| 2036 | return &t_channel; |
| 2037 | } |
| 2038 | break; |
| 2039 | case 'd': |
| 2040 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 2041 | { |
| 2042 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2043 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2044 | } |
| 2045 | break; |
| 2046 | case 'f': |
| 2047 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 2048 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2049 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2050 | *arg += len; |
| 2051 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2052 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2053 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2054 | return &t_any; |
| 2055 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2056 | } |
| 2057 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 2058 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2059 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2060 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2061 | int argcount = -1; |
| 2062 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2063 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2064 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 2065 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2066 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2067 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2068 | if (**arg == '(') |
| 2069 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2070 | // "func" may or may not return a value, "func()" does |
| 2071 | // not return a value. |
| 2072 | ret_type = &t_void; |
| 2073 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2074 | p = ++*arg; |
| 2075 | argcount = 0; |
| 2076 | while (*p != NUL && *p != ')') |
| 2077 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2078 | if (*p == '?') |
| 2079 | { |
| 2080 | if (first_optional == -1) |
| 2081 | first_optional = argcount; |
| 2082 | ++p; |
| 2083 | } |
| 2084 | else if (first_optional != -1) |
| 2085 | { |
| 2086 | emsg(_("E1007: mandatory argument after optional argument")); |
| 2087 | return &t_any; |
| 2088 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2089 | else if (STRNCMP(p, "...", 3) == 0) |
| 2090 | { |
| 2091 | flags |= TTFLAG_VARARGS; |
| 2092 | p += 3; |
| 2093 | } |
| 2094 | |
| 2095 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 2096 | |
| 2097 | // Nothing comes after "...{type}". |
| 2098 | if (flags & TTFLAG_VARARGS) |
| 2099 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2100 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2101 | if (*p != ',' && *skipwhite(p) == ',') |
| 2102 | { |
| 2103 | semsg(_(e_no_white_before), ","); |
| 2104 | return &t_any; |
| 2105 | } |
| 2106 | if (*p == ',') |
| 2107 | { |
| 2108 | ++p; |
| 2109 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2110 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2111 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2112 | return &t_any; |
| 2113 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2114 | } |
| 2115 | p = skipwhite(p); |
| 2116 | if (argcount == MAX_FUNC_ARGS) |
| 2117 | { |
| 2118 | emsg(_("E740: Too many argument types")); |
| 2119 | return &t_any; |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | p = skipwhite(p); |
| 2124 | if (*p != ')') |
| 2125 | { |
| 2126 | emsg(_(e_missing_close)); |
| 2127 | return &t_any; |
| 2128 | } |
| 2129 | *arg = p + 1; |
| 2130 | } |
| 2131 | if (**arg == ':') |
| 2132 | { |
| 2133 | // parse return type |
| 2134 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2135 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2136 | semsg(_(e_white_after), ":"); |
| 2137 | *arg = skipwhite(*arg); |
| 2138 | ret_type = parse_type(arg, type_gap); |
| 2139 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2140 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2141 | type = get_func_type(ret_type, argcount, type_gap); |
| 2142 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2143 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2144 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 2145 | type->tt_flags = flags; |
| 2146 | if (argcount > 0) |
| 2147 | { |
| 2148 | type->tt_argcount = argcount; |
| 2149 | type->tt_min_argcount = first_optional == -1 |
| 2150 | ? argcount : first_optional; |
| 2151 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2152 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2153 | return &t_any; |
| 2154 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2155 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2156 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2157 | } |
| 2158 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2159 | } |
| 2160 | break; |
| 2161 | case 'j': |
| 2162 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 2163 | { |
| 2164 | *arg += len; |
| 2165 | return &t_job; |
| 2166 | } |
| 2167 | break; |
| 2168 | case 'l': |
| 2169 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 2170 | { |
| 2171 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2172 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2173 | } |
| 2174 | break; |
| 2175 | case 'n': |
| 2176 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 2177 | { |
| 2178 | *arg += len; |
| 2179 | return &t_number; |
| 2180 | } |
| 2181 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2182 | case 's': |
| 2183 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 2184 | { |
| 2185 | *arg += len; |
| 2186 | return &t_string; |
| 2187 | } |
| 2188 | break; |
| 2189 | case 'v': |
| 2190 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 2191 | { |
| 2192 | *arg += len; |
| 2193 | return &t_void; |
| 2194 | } |
| 2195 | break; |
| 2196 | } |
| 2197 | |
| 2198 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 2199 | return &t_any; |
| 2200 | } |
| 2201 | |
| 2202 | /* |
| 2203 | * Check if "type1" and "type2" are exactly the same. |
| 2204 | */ |
| 2205 | static int |
| 2206 | equal_type(type_T *type1, type_T *type2) |
| 2207 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2208 | int i; |
| 2209 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2210 | if (type1->tt_type != type2->tt_type) |
| 2211 | return FALSE; |
| 2212 | switch (type1->tt_type) |
| 2213 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2214 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2215 | case VAR_ANY: |
| 2216 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2217 | case VAR_SPECIAL: |
| 2218 | case VAR_BOOL: |
| 2219 | case VAR_NUMBER: |
| 2220 | case VAR_FLOAT: |
| 2221 | case VAR_STRING: |
| 2222 | case VAR_BLOB: |
| 2223 | case VAR_JOB: |
| 2224 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2225 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2226 | case VAR_LIST: |
| 2227 | case VAR_DICT: |
| 2228 | return equal_type(type1->tt_member, type2->tt_member); |
| 2229 | case VAR_FUNC: |
| 2230 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2231 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2232 | || type1->tt_argcount != type2->tt_argcount) |
| 2233 | return FALSE; |
| 2234 | if (type1->tt_argcount < 0 |
| 2235 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2236 | return TRUE; |
| 2237 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2238 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2239 | return FALSE; |
| 2240 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2241 | } |
| 2242 | return TRUE; |
| 2243 | } |
| 2244 | |
| 2245 | /* |
| 2246 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2247 | * "type2" and "dest" may be the same. |
| 2248 | */ |
| 2249 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2250 | 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] | 2251 | { |
| 2252 | if (equal_type(type1, type2)) |
| 2253 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2254 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2255 | return; |
| 2256 | } |
| 2257 | |
| 2258 | if (type1->tt_type == type2->tt_type) |
| 2259 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2260 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2261 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2262 | type_T *common; |
| 2263 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2264 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2265 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2266 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2267 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2268 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2269 | return; |
| 2270 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2271 | if (type1->tt_type == VAR_FUNC) |
| 2272 | { |
| 2273 | type_T *common; |
| 2274 | |
| 2275 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2276 | if (type1->tt_argcount == type2->tt_argcount |
| 2277 | && type1->tt_argcount >= 0) |
| 2278 | { |
| 2279 | int argcount = type1->tt_argcount; |
| 2280 | int i; |
| 2281 | |
| 2282 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2283 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2284 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2285 | if (func_type_add_arg_types(*dest, argcount, |
| 2286 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2287 | for (i = 0; i < argcount; ++i) |
| 2288 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2289 | &(*dest)->tt_args[i], type_gap); |
| 2290 | } |
| 2291 | } |
| 2292 | else |
| 2293 | *dest = alloc_func_type(common, -1, type_gap); |
| 2294 | return; |
| 2295 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2296 | } |
| 2297 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2298 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | char * |
| 2302 | vartype_name(vartype_T type) |
| 2303 | { |
| 2304 | switch (type) |
| 2305 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2306 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2307 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2308 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2309 | case VAR_SPECIAL: return "special"; |
| 2310 | case VAR_BOOL: return "bool"; |
| 2311 | case VAR_NUMBER: return "number"; |
| 2312 | case VAR_FLOAT: return "float"; |
| 2313 | case VAR_STRING: return "string"; |
| 2314 | case VAR_BLOB: return "blob"; |
| 2315 | case VAR_JOB: return "job"; |
| 2316 | case VAR_CHANNEL: return "channel"; |
| 2317 | case VAR_LIST: return "list"; |
| 2318 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2319 | |
| 2320 | case VAR_FUNC: |
| 2321 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2322 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2323 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | /* |
| 2327 | * Return the name of a type. |
| 2328 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2329 | */ |
| 2330 | char * |
| 2331 | type_name(type_T *type, char **tofree) |
| 2332 | { |
| 2333 | char *name = vartype_name(type->tt_type); |
| 2334 | |
| 2335 | *tofree = NULL; |
| 2336 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2337 | { |
| 2338 | char *member_free; |
| 2339 | char *member_name = type_name(type->tt_member, &member_free); |
| 2340 | size_t len; |
| 2341 | |
| 2342 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2343 | *tofree = alloc(len); |
| 2344 | if (*tofree != NULL) |
| 2345 | { |
| 2346 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2347 | vim_free(member_free); |
| 2348 | return *tofree; |
| 2349 | } |
| 2350 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2351 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2352 | { |
| 2353 | garray_T ga; |
| 2354 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2355 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2356 | |
| 2357 | ga_init2(&ga, 1, 100); |
| 2358 | if (ga_grow(&ga, 20) == FAIL) |
| 2359 | return "[unknown]"; |
| 2360 | *tofree = ga.ga_data; |
| 2361 | STRCPY(ga.ga_data, "func("); |
| 2362 | ga.ga_len += 5; |
| 2363 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2364 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2365 | { |
| 2366 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2367 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2368 | int len; |
| 2369 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2370 | if (type->tt_args == NULL) |
| 2371 | arg_type = "[unknown]"; |
| 2372 | else |
| 2373 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2374 | if (i > 0) |
| 2375 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2376 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2377 | ga.ga_len += 2; |
| 2378 | } |
| 2379 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2380 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2381 | { |
| 2382 | vim_free(arg_free); |
| 2383 | return "[unknown]"; |
| 2384 | } |
| 2385 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2386 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2387 | { |
| 2388 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2389 | ga.ga_len += 3; |
| 2390 | } |
| 2391 | else if (i >= type->tt_min_argcount) |
| 2392 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2393 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2394 | ga.ga_len += len; |
| 2395 | vim_free(arg_free); |
| 2396 | } |
| 2397 | |
| 2398 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2399 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2400 | else |
| 2401 | { |
| 2402 | char *ret_free; |
| 2403 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2404 | int len; |
| 2405 | |
| 2406 | len = (int)STRLEN(ret_name) + 4; |
| 2407 | if (ga_grow(&ga, len) == FAIL) |
| 2408 | { |
| 2409 | vim_free(ret_free); |
| 2410 | return "[unknown]"; |
| 2411 | } |
| 2412 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2413 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2414 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2415 | vim_free(ret_free); |
| 2416 | } |
| 2417 | return ga.ga_data; |
| 2418 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2419 | |
| 2420 | return name; |
| 2421 | } |
| 2422 | |
| 2423 | /* |
| 2424 | * Find "name" in script-local items of script "sid". |
| 2425 | * Returns the index in "sn_var_vals" if found. |
| 2426 | * If found but not in "sn_var_vals" returns -1. |
| 2427 | * If not found returns -2. |
| 2428 | */ |
| 2429 | int |
| 2430 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2431 | { |
| 2432 | hashtab_T *ht; |
| 2433 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2434 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2435 | int idx; |
| 2436 | |
| 2437 | // First look the name up in the hashtable. |
| 2438 | if (sid <= 0 || sid > script_items.ga_len) |
| 2439 | return -1; |
| 2440 | ht = &SCRIPT_VARS(sid); |
| 2441 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2442 | if (di == NULL) |
| 2443 | return -2; |
| 2444 | |
| 2445 | // Now find the svar_T index in sn_var_vals. |
| 2446 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2447 | { |
| 2448 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2449 | |
| 2450 | if (sv->sv_tv == &di->di_tv) |
| 2451 | { |
| 2452 | if (check_writable && sv->sv_const) |
| 2453 | semsg(_(e_readonlyvar), name); |
| 2454 | return idx; |
| 2455 | } |
| 2456 | } |
| 2457 | return -1; |
| 2458 | } |
| 2459 | |
| 2460 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2461 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2462 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2463 | */ |
| 2464 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2465 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2466 | { |
Bram Moolenaar | 086eb18 | 2020-07-01 16:00:44 +0200 | [diff] [blame] | 2467 | scriptitem_T *si; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2468 | int idx; |
| 2469 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2470 | if (current_sctx.sc_sid <= 0) |
| 2471 | return NULL; |
| 2472 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2473 | if (cctx != NULL) |
| 2474 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2475 | { |
| 2476 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2477 | + idx; |
| 2478 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2479 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2480 | : STRLEN(import->imp_name) == len |
| 2481 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2482 | return import; |
| 2483 | } |
| 2484 | |
| 2485 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2486 | { |
| 2487 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2488 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2489 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2490 | : STRLEN(import->imp_name) == len |
| 2491 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2492 | return import; |
| 2493 | } |
| 2494 | return NULL; |
| 2495 | } |
| 2496 | |
| 2497 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2498 | * Free all imported variables. |
| 2499 | */ |
| 2500 | static void |
| 2501 | free_imported(cctx_T *cctx) |
| 2502 | { |
| 2503 | int idx; |
| 2504 | |
| 2505 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2506 | { |
| 2507 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2508 | |
| 2509 | vim_free(import->imp_name); |
| 2510 | } |
| 2511 | ga_clear(&cctx->ctx_imports); |
| 2512 | } |
| 2513 | |
| 2514 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2515 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2516 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 2517 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2518 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2519 | { |
| 2520 | return p[0] == '#' && p[1] != '{'; |
| 2521 | } |
| 2522 | |
| 2523 | /* |
| 2524 | * Return a pointer to the next line that isn't empty or only contains a |
| 2525 | * comment. Skips over white space. |
| 2526 | * Returns NULL if there is none. |
| 2527 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2528 | char_u * |
| 2529 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2530 | { |
| 2531 | int lnum = cctx->ctx_lnum; |
| 2532 | |
| 2533 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2534 | { |
| 2535 | 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] | 2536 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2537 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 2538 | if (line == NULL) |
| 2539 | break; |
| 2540 | p = skipwhite(line); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2541 | if (*p != NUL && !vim9_comment_start(p)) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2542 | return p; |
| 2543 | } |
| 2544 | return NULL; |
| 2545 | } |
| 2546 | |
| 2547 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2548 | * Called when checking for a following operator at "arg". When the rest of |
| 2549 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2550 | * line return a pointer to it and set "nextp". |
| 2551 | * Otherwise skip over white space. |
| 2552 | */ |
| 2553 | static char_u * |
| 2554 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2555 | { |
| 2556 | char_u *p = skipwhite(arg); |
| 2557 | |
| 2558 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2559 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2560 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2561 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2562 | if (*nextp != NULL) |
| 2563 | return *nextp; |
| 2564 | } |
| 2565 | return p; |
| 2566 | } |
| 2567 | |
| 2568 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2569 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2570 | * 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] | 2571 | * Returns NULL when at the end. |
| 2572 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2573 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2574 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2575 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2576 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2577 | |
| 2578 | do |
| 2579 | { |
| 2580 | ++cctx->ctx_lnum; |
| 2581 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2582 | { |
| 2583 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2584 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2585 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2586 | 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] | 2587 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2588 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2589 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2590 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2591 | return line; |
| 2592 | } |
| 2593 | |
| 2594 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2595 | * 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] | 2596 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2597 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2598 | */ |
| 2599 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2600 | 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] | 2601 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2602 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2603 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2604 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2605 | |
| 2606 | if (next == NULL) |
| 2607 | return FAIL; |
| 2608 | *arg = skipwhite(next); |
| 2609 | } |
| 2610 | return OK; |
| 2611 | } |
| 2612 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2613 | /* |
| 2614 | * Idem, and give an error when failed. |
| 2615 | */ |
| 2616 | static int |
| 2617 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2618 | { |
| 2619 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2620 | { |
| 2621 | emsg(_("E1097: line incomplete")); |
| 2622 | return FAIL; |
| 2623 | } |
| 2624 | return OK; |
| 2625 | } |
| 2626 | |
| 2627 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2628 | // Structure passed between the compile_expr* functions to keep track of |
| 2629 | // constants that have been parsed but for which no code was produced yet. If |
| 2630 | // possible expressions on these constants are applied at compile time. If |
| 2631 | // that is not possible, the code to push the constants needs to be generated |
| 2632 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2633 | // Using 50 should be more than enough of 5 levels of (). |
| 2634 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2635 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2636 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2637 | int pp_used; // active entries in pp_tv[] |
| 2638 | } ppconst_T; |
| 2639 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2640 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2641 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2642 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2643 | /* |
| 2644 | * Generate a PUSH instruction for "tv". |
| 2645 | * "tv" will be consumed or cleared. |
| 2646 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2647 | */ |
| 2648 | static int |
| 2649 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2650 | { |
| 2651 | if (tv != NULL) |
| 2652 | { |
| 2653 | switch (tv->v_type) |
| 2654 | { |
| 2655 | case VAR_UNKNOWN: |
| 2656 | break; |
| 2657 | case VAR_BOOL: |
| 2658 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2659 | break; |
| 2660 | case VAR_SPECIAL: |
| 2661 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2662 | break; |
| 2663 | case VAR_NUMBER: |
| 2664 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2665 | break; |
| 2666 | #ifdef FEAT_FLOAT |
| 2667 | case VAR_FLOAT: |
| 2668 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2669 | break; |
| 2670 | #endif |
| 2671 | case VAR_BLOB: |
| 2672 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2673 | tv->vval.v_blob = NULL; |
| 2674 | break; |
| 2675 | case VAR_STRING: |
| 2676 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2677 | tv->vval.v_string = NULL; |
| 2678 | break; |
| 2679 | default: |
| 2680 | iemsg("constant type not supported"); |
| 2681 | clear_tv(tv); |
| 2682 | return FAIL; |
| 2683 | } |
| 2684 | tv->v_type = VAR_UNKNOWN; |
| 2685 | } |
| 2686 | return OK; |
| 2687 | } |
| 2688 | |
| 2689 | /* |
| 2690 | * Generate code for any ppconst entries. |
| 2691 | */ |
| 2692 | static int |
| 2693 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2694 | { |
| 2695 | int i; |
| 2696 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2697 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2698 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2699 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2700 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2701 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2702 | ret = FAIL; |
| 2703 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2704 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2705 | return ret; |
| 2706 | } |
| 2707 | |
| 2708 | /* |
| 2709 | * Clear ppconst constants. Used when failing. |
| 2710 | */ |
| 2711 | static void |
| 2712 | clear_ppconst(ppconst_T *ppconst) |
| 2713 | { |
| 2714 | int i; |
| 2715 | |
| 2716 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2717 | clear_tv(&ppconst->pp_tv[i]); |
| 2718 | ppconst->pp_used = 0; |
| 2719 | } |
| 2720 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2721 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2722 | * Generate an instruction to load script-local variable "name", without the |
| 2723 | * leading "s:". |
| 2724 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2725 | */ |
| 2726 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2727 | compile_load_scriptvar( |
| 2728 | cctx_T *cctx, |
| 2729 | char_u *name, // variable NUL terminated |
| 2730 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2731 | char_u **end, // end of variable |
| 2732 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2733 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2734 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2735 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2736 | imported_T *import; |
| 2737 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2738 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2739 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2740 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2741 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2742 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2743 | } |
| 2744 | if (idx >= 0) |
| 2745 | { |
| 2746 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2747 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2748 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2749 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2750 | return OK; |
| 2751 | } |
| 2752 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2753 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2754 | if (import != NULL) |
| 2755 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2756 | if (import->imp_all) |
| 2757 | { |
| 2758 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2759 | char_u *exp_name; |
| 2760 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2761 | ufunc_T *ufunc; |
| 2762 | type_T *type; |
| 2763 | |
| 2764 | // Used "import * as Name", need to lookup the member. |
| 2765 | if (*p != '.') |
| 2766 | { |
| 2767 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2768 | return FAIL; |
| 2769 | } |
| 2770 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2771 | if (VIM_ISWHITE(*p)) |
| 2772 | { |
| 2773 | emsg(_("E1074: no white space allowed after dot")); |
| 2774 | return FAIL; |
| 2775 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2776 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2777 | // isolate one name |
| 2778 | exp_name = p; |
| 2779 | while (eval_isnamec(*p)) |
| 2780 | ++p; |
| 2781 | cc = *p; |
| 2782 | *p = NUL; |
| 2783 | |
| 2784 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2785 | *p = cc; |
| 2786 | p = skipwhite(p); |
| 2787 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2788 | // TODO: what if it is a function? |
| 2789 | if (idx < 0) |
| 2790 | return FAIL; |
| 2791 | *end = p; |
| 2792 | |
| 2793 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2794 | import->imp_sid, |
| 2795 | idx, |
| 2796 | type); |
| 2797 | } |
| 2798 | else |
| 2799 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2800 | // TODO: check this is a variable, not a function? |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2801 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2802 | import->imp_sid, |
| 2803 | import->imp_var_vals_idx, |
| 2804 | import->imp_type); |
| 2805 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2806 | return OK; |
| 2807 | } |
| 2808 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2809 | if (error) |
| 2810 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2811 | return FAIL; |
| 2812 | } |
| 2813 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2814 | static int |
| 2815 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2816 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2817 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2818 | |
| 2819 | if (ufunc == NULL) |
| 2820 | return FAIL; |
| 2821 | |
Bram Moolenaar | fe465a0 | 2020-07-07 22:50:12 +0200 | [diff] [blame] | 2822 | return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name), |
| 2823 | ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2824 | } |
| 2825 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2826 | /* |
| 2827 | * Compile a variable name into a load instruction. |
| 2828 | * "end" points to just after the name. |
| 2829 | * When "error" is FALSE do not give an error when not found. |
| 2830 | */ |
| 2831 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2832 | 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] | 2833 | { |
| 2834 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2835 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2836 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2837 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2838 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2839 | |
| 2840 | if (*(*arg + 1) == ':') |
| 2841 | { |
| 2842 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2843 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2844 | { |
| 2845 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2846 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2847 | switch (**arg) |
| 2848 | { |
| 2849 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2850 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2851 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2852 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2853 | default: |
| 2854 | semsg(_(e_namespace), *arg); |
| 2855 | goto theend; |
| 2856 | } |
| 2857 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2858 | goto theend; |
| 2859 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2860 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2861 | else |
| 2862 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2863 | isntype_T isn_type = ISN_DROP; |
| 2864 | |
| 2865 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2866 | if (name == NULL) |
| 2867 | return FAIL; |
| 2868 | |
| 2869 | switch (**arg) |
| 2870 | { |
| 2871 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2872 | break; |
| 2873 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2874 | NULL, NULL, error); |
| 2875 | break; |
| 2876 | case 'g': isn_type = ISN_LOADG; break; |
| 2877 | case 'w': isn_type = ISN_LOADW; break; |
| 2878 | case 't': isn_type = ISN_LOADT; break; |
| 2879 | case 'b': isn_type = ISN_LOADB; break; |
| 2880 | default: semsg(_(e_namespace), *arg); |
| 2881 | goto theend; |
| 2882 | } |
| 2883 | if (isn_type != ISN_DROP) |
| 2884 | { |
| 2885 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2886 | // variables can be defined later, thus we don't check if it |
| 2887 | // exists, give error at runtime. |
| 2888 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2889 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2890 | } |
| 2891 | } |
| 2892 | else |
| 2893 | { |
| 2894 | size_t len = end - *arg; |
| 2895 | int idx; |
| 2896 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2897 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2898 | |
| 2899 | name = vim_strnsave(*arg, end - *arg); |
| 2900 | if (name == NULL) |
| 2901 | return FAIL; |
| 2902 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2903 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2904 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2905 | if (!gen_load_outer) |
| 2906 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2907 | } |
| 2908 | else |
| 2909 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2910 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2911 | |
| 2912 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2913 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2914 | type = lvar->lv_type; |
| 2915 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2916 | if (lvar->lv_from_outer) |
| 2917 | gen_load_outer = TRUE; |
| 2918 | else |
| 2919 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2920 | } |
| 2921 | else |
| 2922 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2923 | // "var" can be script-local even without using "s:" if it |
| 2924 | // already exists. |
| 2925 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2926 | == SCRIPT_VERSION_VIM9 |
| 2927 | || lookup_script(*arg, len) == OK) |
| 2928 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2929 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2930 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2931 | // When the name starts with an uppercase letter or "x:" it |
| 2932 | // can be a user defined function. |
| 2933 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2934 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2935 | } |
| 2936 | } |
| 2937 | if (gen_load) |
| 2938 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2939 | if (gen_load_outer) |
| 2940 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | *arg = end; |
| 2944 | |
| 2945 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2946 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2947 | semsg(_(e_var_notfound), name); |
| 2948 | vim_free(name); |
| 2949 | return res; |
| 2950 | } |
| 2951 | |
| 2952 | /* |
| 2953 | * Compile the argument expressions. |
| 2954 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2955 | */ |
| 2956 | static int |
| 2957 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2958 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2959 | char_u *p = *arg; |
| 2960 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2961 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2962 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2963 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2964 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2965 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2966 | if (*p == ')') |
| 2967 | { |
| 2968 | *arg = p + 1; |
| 2969 | return OK; |
| 2970 | } |
| 2971 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2972 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2973 | return FAIL; |
| 2974 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2975 | |
| 2976 | if (*p != ',' && *skipwhite(p) == ',') |
| 2977 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2978 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2979 | p = skipwhite(p); |
| 2980 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2981 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2982 | { |
| 2983 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2984 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2985 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2986 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2987 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2988 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2989 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2990 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2991 | emsg(_(e_missing_close)); |
| 2992 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | /* |
| 2996 | * Compile a function call: name(arg1, arg2) |
| 2997 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2998 | * "argcount_init" is 1 for "value->method()" |
| 2999 | * Instructions: |
| 3000 | * EVAL arg1 |
| 3001 | * EVAL arg2 |
| 3002 | * BCALL / DCALL / UCALL |
| 3003 | */ |
| 3004 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3005 | compile_call( |
| 3006 | char_u **arg, |
| 3007 | size_t varlen, |
| 3008 | cctx_T *cctx, |
| 3009 | ppconst_T *ppconst, |
| 3010 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3011 | { |
| 3012 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 3013 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3014 | int argcount = argcount_init; |
| 3015 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3016 | char_u fname_buf[FLEN_FIXED + 1]; |
| 3017 | char_u *tofree = NULL; |
| 3018 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3019 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3020 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3021 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3022 | // we can evaluate "has('name')" at compile time |
| 3023 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 3024 | { |
| 3025 | char_u *s = skipwhite(*arg + varlen + 1); |
| 3026 | typval_T argvars[2]; |
| 3027 | |
| 3028 | argvars[0].v_type = VAR_UNKNOWN; |
| 3029 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3030 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3031 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3032 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3033 | s = skipwhite(s); |
| 3034 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 3035 | { |
| 3036 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 3037 | |
| 3038 | *arg = s + 1; |
| 3039 | argvars[1].v_type = VAR_UNKNOWN; |
| 3040 | tv->v_type = VAR_NUMBER; |
| 3041 | tv->vval.v_number = 0; |
| 3042 | f_has(argvars, tv); |
| 3043 | clear_tv(&argvars[0]); |
| 3044 | ++ppconst->pp_used; |
| 3045 | return OK; |
| 3046 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 3047 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3051 | return FAIL; |
| 3052 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3053 | if (varlen >= sizeof(namebuf)) |
| 3054 | { |
| 3055 | semsg(_("E1011: name too long: %s"), name); |
| 3056 | return FAIL; |
| 3057 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3058 | vim_strncpy(namebuf, *arg, varlen); |
| 3059 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3060 | |
| 3061 | *arg = skipwhite(*arg + varlen + 1); |
| 3062 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3063 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3064 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3065 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3066 | { |
| 3067 | int idx; |
| 3068 | |
| 3069 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3070 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3071 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 3072 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3073 | else |
| 3074 | semsg(_(e_unknownfunc), namebuf); |
| 3075 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3076 | } |
| 3077 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3078 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 3079 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3080 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3081 | { |
| 3082 | res = generate_CALL(cctx, ufunc, argcount); |
| 3083 | goto theend; |
| 3084 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3085 | |
| 3086 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3087 | // 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] | 3088 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3089 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 3090 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3091 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3092 | garray_T *stack = &cctx->ctx_type_stack; |
| 3093 | type_T *type; |
| 3094 | |
| 3095 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3096 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3097 | goto theend; |
| 3098 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3099 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3100 | // 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] | 3101 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3102 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 3103 | res = generate_UCALL(cctx, name, argcount); |
| 3104 | else |
| 3105 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3106 | |
| 3107 | theend: |
| 3108 | vim_free(tofree); |
| 3109 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3110 | } |
| 3111 | |
| 3112 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 3113 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 3114 | |
| 3115 | /* |
| 3116 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 3117 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3118 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3119 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 3120 | * valid name. |
| 3121 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3122 | static char_u * |
| 3123 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3124 | { |
| 3125 | char_u *p; |
| 3126 | |
| 3127 | // Quick check for valid starting character. |
| 3128 | if (!eval_isnamec1(*arg)) |
| 3129 | return arg; |
| 3130 | |
| 3131 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 3132 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 3133 | // and can be used in slice "[n:]". |
| 3134 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3135 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3136 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 3137 | break; |
| 3138 | return p; |
| 3139 | } |
| 3140 | |
| 3141 | /* |
| 3142 | * 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] | 3143 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3144 | */ |
| 3145 | char_u * |
| 3146 | to_name_const_end(char_u *arg) |
| 3147 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3148 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3149 | typval_T rettv; |
| 3150 | |
| 3151 | if (p == arg && *arg == '[') |
| 3152 | { |
| 3153 | |
| 3154 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3155 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3156 | p = arg; |
| 3157 | } |
| 3158 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 3159 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3160 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3161 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3162 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3163 | p = arg; |
| 3164 | } |
| 3165 | else if (p == arg && *arg == '{') |
| 3166 | { |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3167 | int ret = get_lambda_tv(&p, &rettv, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3168 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3169 | // Can be "{x -> ret}()". |
| 3170 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3171 | if (ret == NOTDONE) |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3172 | ret = eval_dict(&p, &rettv, NULL, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3173 | if (ret != OK) |
| 3174 | p = arg; |
| 3175 | } |
| 3176 | |
| 3177 | return p; |
| 3178 | } |
| 3179 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3180 | /* |
| 3181 | * parse a list: [expr, expr] |
| 3182 | * "*arg" points to the '['. |
| 3183 | */ |
| 3184 | static int |
| 3185 | compile_list(char_u **arg, cctx_T *cctx) |
| 3186 | { |
| 3187 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3188 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3189 | int count = 0; |
| 3190 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3191 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3192 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3193 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3194 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3195 | semsg(_(e_list_end), *arg); |
| 3196 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3197 | } |
| 3198 | if (*p == ']') |
| 3199 | { |
| 3200 | ++p; |
| 3201 | // Allow for following comment, after at least one space. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3202 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '#') |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3203 | p += STRLEN(p); |
| 3204 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3205 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3206 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3207 | break; |
| 3208 | ++count; |
| 3209 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3210 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3211 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3212 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 3213 | { |
| 3214 | semsg(_(e_white_after), ","); |
| 3215 | return FAIL; |
| 3216 | } |
| 3217 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3218 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3219 | p = skipwhite(p); |
| 3220 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3221 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3222 | |
| 3223 | generate_NEWLIST(cctx, count); |
| 3224 | return OK; |
| 3225 | } |
| 3226 | |
| 3227 | /* |
| 3228 | * parse a lambda: {arg, arg -> expr} |
| 3229 | * "*arg" points to the '{'. |
| 3230 | */ |
| 3231 | static int |
| 3232 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 3233 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3234 | typval_T rettv; |
| 3235 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3236 | evalarg_T evalarg; |
| 3237 | |
| 3238 | CLEAR_FIELD(evalarg); |
| 3239 | evalarg.eval_flags = EVAL_EVALUATE; |
| 3240 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3241 | |
| 3242 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3243 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3244 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3245 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3246 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3247 | ++ufunc->uf_refcount; |
| 3248 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3249 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3250 | |
| 3251 | // The function will have one line: "return {expr}". |
| 3252 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3253 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3254 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 3255 | clear_evalarg(&evalarg, NULL); |
| 3256 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 3257 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 3258 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3259 | |
| 3260 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3261 | return FAIL; |
| 3262 | } |
| 3263 | |
| 3264 | /* |
| 3265 | * Compile a lamda call: expr->{lambda}(args) |
| 3266 | * "arg" points to the "{". |
| 3267 | */ |
| 3268 | static int |
| 3269 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 3270 | { |
| 3271 | ufunc_T *ufunc; |
| 3272 | typval_T rettv; |
| 3273 | int argcount = 1; |
| 3274 | int ret = FAIL; |
| 3275 | |
| 3276 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3277 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3278 | return FAIL; |
| 3279 | |
| 3280 | if (**arg != '(') |
| 3281 | { |
| 3282 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 3283 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3284 | else |
| 3285 | semsg(_(e_missing_paren), "lambda"); |
| 3286 | clear_tv(&rettv); |
| 3287 | return FAIL; |
| 3288 | } |
| 3289 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3290 | ufunc = rettv.vval.v_partial->pt_func; |
| 3291 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3292 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3293 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3294 | |
| 3295 | // The function will have one line: "return {expr}". |
| 3296 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3297 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3298 | |
| 3299 | // compile the arguments |
| 3300 | *arg = skipwhite(*arg + 1); |
| 3301 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 3302 | // call the compiled function |
| 3303 | ret = generate_CALL(cctx, ufunc, argcount); |
| 3304 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3305 | if (ret == FAIL) |
| 3306 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3307 | return ret; |
| 3308 | } |
| 3309 | |
| 3310 | /* |
| 3311 | * parse a dict: {'key': val} or #{key: val} |
| 3312 | * "*arg" points to the '{'. |
| 3313 | */ |
| 3314 | static int |
| 3315 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 3316 | { |
| 3317 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3318 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3319 | int count = 0; |
| 3320 | dict_T *d = dict_alloc(); |
| 3321 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3322 | char_u *whitep = *arg; |
| 3323 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3324 | |
| 3325 | if (d == NULL) |
| 3326 | return FAIL; |
| 3327 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3328 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3329 | { |
| 3330 | char_u *key = NULL; |
| 3331 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3332 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3333 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3334 | *arg = NULL; |
| 3335 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | if (**arg == '}') |
| 3339 | break; |
| 3340 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3341 | if (literal) |
| 3342 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3343 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3344 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3345 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3346 | { |
| 3347 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 3348 | return FAIL; |
| 3349 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3350 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3351 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3352 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3353 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3354 | } |
| 3355 | else |
| 3356 | { |
| 3357 | isn_T *isn; |
| 3358 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3359 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3360 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3361 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3362 | if (isn->isn_type == ISN_PUSHS) |
| 3363 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3364 | else |
| 3365 | { |
| 3366 | type_T *keytype = ((type_T **)stack->ga_data) |
| 3367 | [stack->ga_len - 1]; |
| 3368 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 3369 | return FAIL; |
| 3370 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3371 | } |
| 3372 | |
| 3373 | // Check for duplicate keys, if using string keys. |
| 3374 | if (key != NULL) |
| 3375 | { |
| 3376 | item = dict_find(d, key, -1); |
| 3377 | if (item != NULL) |
| 3378 | { |
| 3379 | semsg(_(e_duplicate_key), key); |
| 3380 | goto failret; |
| 3381 | } |
| 3382 | item = dictitem_alloc(key); |
| 3383 | if (item != NULL) |
| 3384 | { |
| 3385 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3386 | item->di_tv.v_lock = 0; |
| 3387 | if (dict_add(d, item) == FAIL) |
| 3388 | dictitem_free(item); |
| 3389 | } |
| 3390 | } |
| 3391 | |
| 3392 | *arg = skipwhite(*arg); |
| 3393 | if (**arg != ':') |
| 3394 | { |
| 3395 | semsg(_(e_missing_dict_colon), *arg); |
| 3396 | return FAIL; |
| 3397 | } |
| 3398 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3399 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3400 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3401 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3402 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3403 | *arg = NULL; |
| 3404 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3405 | } |
| 3406 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3407 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3408 | return FAIL; |
| 3409 | ++count; |
| 3410 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3411 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3412 | *arg = skipwhite(*arg); |
| 3413 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3414 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3415 | *arg = NULL; |
| 3416 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3417 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3418 | if (**arg == '}') |
| 3419 | break; |
| 3420 | if (**arg != ',') |
| 3421 | { |
| 3422 | semsg(_(e_missing_dict_comma), *arg); |
| 3423 | goto failret; |
| 3424 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3425 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3426 | *arg = skipwhite(*arg + 1); |
| 3427 | } |
| 3428 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3429 | *arg = *arg + 1; |
| 3430 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3431 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3432 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3433 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3434 | *arg += STRLEN(*arg); |
| 3435 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3436 | dict_unref(d); |
| 3437 | return generate_NEWDICT(cctx, count); |
| 3438 | |
| 3439 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3440 | if (*arg == NULL) |
| 3441 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3442 | dict_unref(d); |
| 3443 | return FAIL; |
| 3444 | } |
| 3445 | |
| 3446 | /* |
| 3447 | * Compile "&option". |
| 3448 | */ |
| 3449 | static int |
| 3450 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3451 | { |
| 3452 | typval_T rettv; |
| 3453 | char_u *start = *arg; |
| 3454 | int ret; |
| 3455 | |
| 3456 | // parse the option and get the current value to get the type. |
| 3457 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3458 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3459 | if (ret == OK) |
| 3460 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3461 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3462 | char_u *name = vim_strnsave(start, *arg - start); |
| 3463 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3464 | |
| 3465 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3466 | vim_free(name); |
| 3467 | } |
| 3468 | clear_tv(&rettv); |
| 3469 | |
| 3470 | return ret; |
| 3471 | } |
| 3472 | |
| 3473 | /* |
| 3474 | * Compile "$VAR". |
| 3475 | */ |
| 3476 | static int |
| 3477 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3478 | { |
| 3479 | char_u *start = *arg; |
| 3480 | int len; |
| 3481 | int ret; |
| 3482 | char_u *name; |
| 3483 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3484 | ++*arg; |
| 3485 | len = get_env_len(arg); |
| 3486 | if (len == 0) |
| 3487 | { |
| 3488 | semsg(_(e_syntax_at), start - 1); |
| 3489 | return FAIL; |
| 3490 | } |
| 3491 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3492 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3493 | name = vim_strnsave(start, len + 1); |
| 3494 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3495 | vim_free(name); |
| 3496 | return ret; |
| 3497 | } |
| 3498 | |
| 3499 | /* |
| 3500 | * Compile "@r". |
| 3501 | */ |
| 3502 | static int |
| 3503 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3504 | { |
| 3505 | int ret; |
| 3506 | |
| 3507 | ++*arg; |
| 3508 | if (**arg == NUL) |
| 3509 | { |
| 3510 | semsg(_(e_syntax_at), *arg - 1); |
| 3511 | return FAIL; |
| 3512 | } |
| 3513 | if (!valid_yank_reg(**arg, TRUE)) |
| 3514 | { |
| 3515 | emsg_invreg(**arg); |
| 3516 | return FAIL; |
| 3517 | } |
| 3518 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3519 | ++*arg; |
| 3520 | return ret; |
| 3521 | } |
| 3522 | |
| 3523 | /* |
| 3524 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3525 | */ |
| 3526 | static int |
| 3527 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3528 | { |
| 3529 | char_u *p = end; |
| 3530 | |
| 3531 | // this works from end to start |
| 3532 | while (p > start) |
| 3533 | { |
| 3534 | --p; |
| 3535 | if (*p == '-' || *p == '+') |
| 3536 | { |
| 3537 | // only '-' has an effect, for '+' we only check the type |
| 3538 | #ifdef FEAT_FLOAT |
| 3539 | if (rettv->v_type == VAR_FLOAT) |
| 3540 | { |
| 3541 | if (*p == '-') |
| 3542 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3543 | } |
| 3544 | else |
| 3545 | #endif |
| 3546 | { |
| 3547 | varnumber_T val; |
| 3548 | int error = FALSE; |
| 3549 | |
| 3550 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3551 | // here |
| 3552 | if (check_not_string(rettv) == FAIL) |
| 3553 | return FAIL; |
| 3554 | val = tv_get_number_chk(rettv, &error); |
| 3555 | clear_tv(rettv); |
| 3556 | if (error) |
| 3557 | return FAIL; |
| 3558 | if (*p == '-') |
| 3559 | val = -val; |
| 3560 | rettv->v_type = VAR_NUMBER; |
| 3561 | rettv->vval.v_number = val; |
| 3562 | } |
| 3563 | } |
| 3564 | else |
| 3565 | { |
| 3566 | int v = tv2bool(rettv); |
| 3567 | |
| 3568 | // '!' is permissive in the type. |
| 3569 | clear_tv(rettv); |
| 3570 | rettv->v_type = VAR_BOOL; |
| 3571 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3572 | } |
| 3573 | } |
| 3574 | return OK; |
| 3575 | } |
| 3576 | |
| 3577 | /* |
| 3578 | * Recognize v: variables that are constants and set "rettv". |
| 3579 | */ |
| 3580 | static void |
| 3581 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3582 | { |
| 3583 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3584 | { |
| 3585 | rettv->v_type = VAR_BOOL; |
| 3586 | rettv->vval.v_number = VVAL_TRUE; |
| 3587 | *arg += 6; |
| 3588 | } |
| 3589 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3590 | { |
| 3591 | rettv->v_type = VAR_BOOL; |
| 3592 | rettv->vval.v_number = VVAL_FALSE; |
| 3593 | *arg += 7; |
| 3594 | } |
| 3595 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3596 | { |
| 3597 | rettv->v_type = VAR_SPECIAL; |
| 3598 | rettv->vval.v_number = VVAL_NULL; |
| 3599 | *arg += 6; |
| 3600 | } |
| 3601 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3602 | { |
| 3603 | rettv->v_type = VAR_SPECIAL; |
| 3604 | rettv->vval.v_number = VVAL_NONE; |
| 3605 | *arg += 6; |
| 3606 | } |
| 3607 | } |
| 3608 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3609 | static exptype_T |
| 3610 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3611 | { |
| 3612 | exptype_T type = EXPR_UNKNOWN; |
| 3613 | int i; |
| 3614 | |
| 3615 | switch (p[0]) |
| 3616 | { |
| 3617 | case '=': if (p[1] == '=') |
| 3618 | type = EXPR_EQUAL; |
| 3619 | else if (p[1] == '~') |
| 3620 | type = EXPR_MATCH; |
| 3621 | break; |
| 3622 | case '!': if (p[1] == '=') |
| 3623 | type = EXPR_NEQUAL; |
| 3624 | else if (p[1] == '~') |
| 3625 | type = EXPR_NOMATCH; |
| 3626 | break; |
| 3627 | case '>': if (p[1] != '=') |
| 3628 | { |
| 3629 | type = EXPR_GREATER; |
| 3630 | *len = 1; |
| 3631 | } |
| 3632 | else |
| 3633 | type = EXPR_GEQUAL; |
| 3634 | break; |
| 3635 | case '<': if (p[1] != '=') |
| 3636 | { |
| 3637 | type = EXPR_SMALLER; |
| 3638 | *len = 1; |
| 3639 | } |
| 3640 | else |
| 3641 | type = EXPR_SEQUAL; |
| 3642 | break; |
| 3643 | case 'i': if (p[1] == 's') |
| 3644 | { |
| 3645 | // "is" and "isnot"; but not a prefix of a name |
| 3646 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3647 | *len = 5; |
| 3648 | i = p[*len]; |
| 3649 | if (!isalnum(i) && i != '_') |
| 3650 | { |
| 3651 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3652 | *type_is = TRUE; |
| 3653 | } |
| 3654 | } |
| 3655 | break; |
| 3656 | } |
| 3657 | return type; |
| 3658 | } |
| 3659 | |
| 3660 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3661 | * Compile code to apply '-', '+' and '!'. |
| 3662 | */ |
| 3663 | static int |
| 3664 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3665 | { |
| 3666 | char_u *p = end; |
| 3667 | |
| 3668 | // this works from end to start |
| 3669 | while (p > start) |
| 3670 | { |
| 3671 | --p; |
| 3672 | if (*p == '-' || *p == '+') |
| 3673 | { |
| 3674 | int negate = *p == '-'; |
| 3675 | isn_T *isn; |
| 3676 | |
| 3677 | // TODO: check type |
| 3678 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3679 | { |
| 3680 | --p; |
| 3681 | if (*p == '-') |
| 3682 | negate = !negate; |
| 3683 | } |
| 3684 | // only '-' has an effect, for '+' we only check the type |
| 3685 | if (negate) |
| 3686 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3687 | else |
| 3688 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3689 | if (isn == NULL) |
| 3690 | return FAIL; |
| 3691 | } |
| 3692 | else |
| 3693 | { |
| 3694 | int invert = TRUE; |
| 3695 | |
| 3696 | while (p > start && p[-1] == '!') |
| 3697 | { |
| 3698 | --p; |
| 3699 | invert = !invert; |
| 3700 | } |
| 3701 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3702 | return FAIL; |
| 3703 | } |
| 3704 | } |
| 3705 | return OK; |
| 3706 | } |
| 3707 | |
| 3708 | /* |
| 3709 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3710 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3711 | */ |
| 3712 | static int |
| 3713 | compile_subscript( |
| 3714 | char_u **arg, |
| 3715 | cctx_T *cctx, |
| 3716 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3717 | char_u *end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3718 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3719 | { |
| 3720 | for (;;) |
| 3721 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3722 | char_u *p = skipwhite(*arg); |
| 3723 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3724 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3725 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3726 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3727 | |
| 3728 | // If a following line starts with "->{" or "->X" advance to that |
| 3729 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3730 | // Also if a following line starts with ".x". |
| 3731 | if (next != NULL && |
| 3732 | ((next[0] == '-' && next[1] == '>' |
| 3733 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
| 3734 | || (next[0] == '.' && ASCII_ISALPHA(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3735 | { |
| 3736 | next = next_line_from_context(cctx, TRUE); |
| 3737 | if (next == NULL) |
| 3738 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3739 | *arg = next; |
| 3740 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3741 | } |
| 3742 | } |
| 3743 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3744 | if (*p == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3745 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3746 | garray_T *stack = &cctx->ctx_type_stack; |
| 3747 | type_T *type; |
| 3748 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3749 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3750 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3751 | return FAIL; |
| 3752 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3753 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3754 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3755 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3756 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3757 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3758 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3759 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3760 | return FAIL; |
| 3761 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3762 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3763 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3764 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3765 | return FAIL; |
| 3766 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3767 | // something->method() |
| 3768 | // Apply the '!', '-' and '+' first: |
| 3769 | // -1.0->func() works like (-1.0)->func() |
| 3770 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3771 | return FAIL; |
| 3772 | *start_leader = end_leader; // don't apply again later |
| 3773 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3774 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3775 | *arg = skipwhite(p); |
| 3776 | if (may_get_next_line(p, arg, cctx) == FAIL) |
| 3777 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3778 | if (**arg == '{') |
| 3779 | { |
| 3780 | // lambda call: list->{lambda} |
| 3781 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3782 | return FAIL; |
| 3783 | } |
| 3784 | else |
| 3785 | { |
| 3786 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3787 | p = *arg; |
| 3788 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3789 | p += 2; |
| 3790 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3791 | ; |
| 3792 | if (*p != '(') |
| 3793 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3794 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3795 | return FAIL; |
| 3796 | } |
| 3797 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3798 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3799 | return FAIL; |
| 3800 | } |
| 3801 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3802 | else if (*p == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3803 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3804 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3805 | type_T **typep; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3806 | vartype_T vtype; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3807 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3808 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3809 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3810 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3811 | // TODO: blob index |
| 3812 | // TODO: more arguments |
| 3813 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3814 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3815 | return FAIL; |
| 3816 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3817 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3818 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3819 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3820 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3821 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3822 | return FAIL; |
| 3823 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3824 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3825 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3826 | if (**arg != ']') |
| 3827 | { |
| 3828 | emsg(_(e_missbrac)); |
| 3829 | return FAIL; |
| 3830 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3831 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3832 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3833 | // We can index a list and a dict. If we don't know the type |
| 3834 | // we can use the index value type. |
| 3835 | // TODO: If we don't know use an instruction to figure it out at |
| 3836 | // runtime. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3837 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3838 | vtype = (*typep)->tt_type; |
| 3839 | if (*typep == &t_any) |
| 3840 | { |
| 3841 | type_T *valtype = ((type_T **)stack->ga_data) |
| 3842 | [stack->ga_len - 1]; |
| 3843 | if (valtype == &t_string) |
| 3844 | vtype = VAR_DICT; |
| 3845 | } |
| 3846 | if (vtype == VAR_DICT) |
| 3847 | { |
| 3848 | if ((*typep)->tt_type == VAR_DICT) |
| 3849 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3850 | else |
| 3851 | { |
| 3852 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3853 | return FAIL; |
| 3854 | *typep = &t_any; |
| 3855 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3856 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3857 | return FAIL; |
| 3858 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3859 | return FAIL; |
| 3860 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3861 | else if (vtype == VAR_STRING) |
| 3862 | { |
| 3863 | *typep = &t_number; |
| 3864 | if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL) |
| 3865 | return FAIL; |
| 3866 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3867 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3868 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3869 | if ((*typep)->tt_type == VAR_LIST) |
| 3870 | *typep = (*typep)->tt_member; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3871 | if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3872 | return FAIL; |
| 3873 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3874 | else |
| 3875 | { |
| 3876 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3877 | return FAIL; |
| 3878 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3879 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3880 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3881 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3882 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3883 | return FAIL; |
| 3884 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3885 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3886 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3887 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3888 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3889 | p = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3890 | if (eval_isnamec1(*p)) |
| 3891 | while (eval_isnamec(*p)) |
| 3892 | MB_PTR_ADV(p); |
| 3893 | if (p == *arg) |
| 3894 | { |
| 3895 | semsg(_(e_syntax_at), *arg); |
| 3896 | return FAIL; |
| 3897 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3898 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3899 | return FAIL; |
| 3900 | *arg = p; |
| 3901 | } |
| 3902 | else |
| 3903 | break; |
| 3904 | } |
| 3905 | |
| 3906 | // TODO - see handle_subscript(): |
| 3907 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3908 | // Don't do this when "Func" is already a partial that was bound |
| 3909 | // explicitly (pt_auto is FALSE). |
| 3910 | |
| 3911 | return OK; |
| 3912 | } |
| 3913 | |
| 3914 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3915 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3916 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3917 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3918 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3919 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3920 | * |
| 3921 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3922 | */ |
| 3923 | |
| 3924 | /* |
| 3925 | * number number constant |
| 3926 | * 0zFFFFFFFF Blob constant |
| 3927 | * "string" string constant |
| 3928 | * 'string' literal string constant |
| 3929 | * &option-name option value |
| 3930 | * @r register contents |
| 3931 | * identifier variable value |
| 3932 | * function() function call |
| 3933 | * $VAR environment variable |
| 3934 | * (expression) nested expression |
| 3935 | * [expr, expr] List |
| 3936 | * {key: val, key: val} Dictionary |
| 3937 | * #{key: val, key: val} Dictionary with literal keys |
| 3938 | * |
| 3939 | * Also handle: |
| 3940 | * ! in front logical NOT |
| 3941 | * - in front unary minus |
| 3942 | * + in front unary plus (ignored) |
| 3943 | * trailing (arg) funcref/partial call |
| 3944 | * trailing [] subscript in String or List |
| 3945 | * trailing .name entry in Dictionary |
| 3946 | * trailing ->name() method call |
| 3947 | */ |
| 3948 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3949 | compile_expr7( |
| 3950 | char_u **arg, |
| 3951 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3952 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3953 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3954 | char_u *start_leader, *end_leader; |
| 3955 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3956 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3957 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3958 | |
| 3959 | /* |
| 3960 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3961 | */ |
| 3962 | start_leader = *arg; |
| 3963 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3964 | *arg = skipwhite(*arg + 1); |
| 3965 | end_leader = *arg; |
| 3966 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3967 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3968 | switch (**arg) |
| 3969 | { |
| 3970 | /* |
| 3971 | * Number constant. |
| 3972 | */ |
| 3973 | case '0': // also for blob starting with 0z |
| 3974 | case '1': |
| 3975 | case '2': |
| 3976 | case '3': |
| 3977 | case '4': |
| 3978 | case '5': |
| 3979 | case '6': |
| 3980 | case '7': |
| 3981 | case '8': |
| 3982 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3983 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3984 | return FAIL; |
| 3985 | break; |
| 3986 | |
| 3987 | /* |
| 3988 | * String constant: "string". |
| 3989 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3990 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3991 | return FAIL; |
| 3992 | break; |
| 3993 | |
| 3994 | /* |
| 3995 | * Literal string constant: 'str''ing'. |
| 3996 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3997 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3998 | return FAIL; |
| 3999 | break; |
| 4000 | |
| 4001 | /* |
| 4002 | * Constant Vim variable. |
| 4003 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4004 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4005 | ret = NOTDONE; |
| 4006 | break; |
| 4007 | |
| 4008 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4009 | * "true" constant |
| 4010 | */ |
| 4011 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 4012 | && !eval_isnamec((*arg)[4])) |
| 4013 | { |
| 4014 | *arg += 4; |
| 4015 | rettv->v_type = VAR_BOOL; |
| 4016 | rettv->vval.v_number = VVAL_TRUE; |
| 4017 | } |
| 4018 | else |
| 4019 | ret = NOTDONE; |
| 4020 | break; |
| 4021 | |
| 4022 | /* |
| 4023 | * "false" constant |
| 4024 | */ |
| 4025 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 4026 | && !eval_isnamec((*arg)[5])) |
| 4027 | { |
| 4028 | *arg += 5; |
| 4029 | rettv->v_type = VAR_BOOL; |
| 4030 | rettv->vval.v_number = VVAL_FALSE; |
| 4031 | } |
| 4032 | else |
| 4033 | ret = NOTDONE; |
| 4034 | break; |
| 4035 | |
| 4036 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4037 | * List: [expr, expr] |
| 4038 | */ |
| 4039 | case '[': ret = compile_list(arg, cctx); |
| 4040 | break; |
| 4041 | |
| 4042 | /* |
| 4043 | * Dictionary: #{key: val, key: val} |
| 4044 | */ |
| 4045 | case '#': if ((*arg)[1] == '{') |
| 4046 | { |
| 4047 | ++*arg; |
| 4048 | ret = compile_dict(arg, cctx, TRUE); |
| 4049 | } |
| 4050 | else |
| 4051 | ret = NOTDONE; |
| 4052 | break; |
| 4053 | |
| 4054 | /* |
| 4055 | * Lambda: {arg, arg -> expr} |
| 4056 | * Dictionary: {'key': val, 'key': val} |
| 4057 | */ |
| 4058 | case '{': { |
| 4059 | char_u *start = skipwhite(*arg + 1); |
| 4060 | |
| 4061 | // Find out what comes after the arguments. |
| 4062 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 4063 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4064 | if (ret != FAIL && *start == '>') |
| 4065 | ret = compile_lambda(arg, cctx); |
| 4066 | else |
| 4067 | ret = compile_dict(arg, cctx, FALSE); |
| 4068 | } |
| 4069 | break; |
| 4070 | |
| 4071 | /* |
| 4072 | * Option value: &name |
| 4073 | */ |
| 4074 | case '&': ret = compile_get_option(arg, cctx); |
| 4075 | break; |
| 4076 | |
| 4077 | /* |
| 4078 | * Environment variable: $VAR. |
| 4079 | */ |
| 4080 | case '$': ret = compile_get_env(arg, cctx); |
| 4081 | break; |
| 4082 | |
| 4083 | /* |
| 4084 | * Register contents: @r. |
| 4085 | */ |
| 4086 | case '@': ret = compile_get_register(arg, cctx); |
| 4087 | break; |
| 4088 | /* |
| 4089 | * nested expression: (expression). |
| 4090 | */ |
| 4091 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4092 | |
| 4093 | // recursive! |
| 4094 | if (ppconst->pp_used <= PPSIZE - 10) |
| 4095 | { |
| 4096 | ret = compile_expr1(arg, cctx, ppconst); |
| 4097 | } |
| 4098 | else |
| 4099 | { |
| 4100 | // Not enough space in ppconst, flush constants. |
| 4101 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4102 | return FAIL; |
| 4103 | ret = compile_expr0(arg, cctx); |
| 4104 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4105 | *arg = skipwhite(*arg); |
| 4106 | if (**arg == ')') |
| 4107 | ++*arg; |
| 4108 | else if (ret == OK) |
| 4109 | { |
| 4110 | emsg(_(e_missing_close)); |
| 4111 | ret = FAIL; |
| 4112 | } |
| 4113 | break; |
| 4114 | |
| 4115 | default: ret = NOTDONE; |
| 4116 | break; |
| 4117 | } |
| 4118 | if (ret == FAIL) |
| 4119 | return FAIL; |
| 4120 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4121 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4122 | { |
| 4123 | // apply the '!', '-' and '+' before the constant |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4124 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4125 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4126 | clear_tv(rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4127 | return FAIL; |
| 4128 | } |
| 4129 | start_leader = end_leader; // don't apply again below |
| 4130 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4131 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4132 | clear_tv(rettv); |
| 4133 | else |
| 4134 | // A constant expression can possibly be handled compile time, |
| 4135 | // return the value instead of generating code. |
| 4136 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4137 | } |
| 4138 | else if (ret == NOTDONE) |
| 4139 | { |
| 4140 | char_u *p; |
| 4141 | int r; |
| 4142 | |
| 4143 | if (!eval_isnamec1(**arg)) |
| 4144 | { |
| 4145 | semsg(_("E1015: Name expected: %s"), *arg); |
| 4146 | return FAIL; |
| 4147 | } |
| 4148 | |
| 4149 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4150 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4151 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4152 | { |
| 4153 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 4154 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4155 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4156 | { |
| 4157 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4158 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4159 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4160 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4161 | if (r == FAIL) |
| 4162 | return FAIL; |
| 4163 | } |
| 4164 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4165 | // Handle following "[]", ".member", etc. |
| 4166 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4167 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4168 | ppconst) == FAIL) |
| 4169 | return FAIL; |
| 4170 | if (ppconst->pp_used > 0) |
| 4171 | { |
| 4172 | // apply the '!', '-' and '+' before the constant |
| 4173 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4174 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
| 4175 | return FAIL; |
| 4176 | return OK; |
| 4177 | } |
| 4178 | if (compile_leader(cctx, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4179 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4180 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4181 | } |
| 4182 | |
| 4183 | /* |
| 4184 | * * number multiplication |
| 4185 | * / number division |
| 4186 | * % number modulo |
| 4187 | */ |
| 4188 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4189 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4190 | { |
| 4191 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4192 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4193 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4194 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4195 | // get the first expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4196 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4197 | return FAIL; |
| 4198 | |
| 4199 | /* |
| 4200 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4201 | */ |
| 4202 | for (;;) |
| 4203 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4204 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4205 | if (*op != '*' && *op != '/' && *op != '%') |
| 4206 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4207 | if (next != NULL) |
| 4208 | { |
| 4209 | *arg = next_line_from_context(cctx, TRUE); |
| 4210 | op = skipwhite(*arg); |
| 4211 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4212 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4213 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4214 | { |
| 4215 | char_u buf[3]; |
| 4216 | |
| 4217 | vim_strncpy(buf, op, 1); |
| 4218 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4219 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4220 | } |
| 4221 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4222 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4223 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4224 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4225 | // get the second expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4226 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4227 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4228 | |
| 4229 | if (ppconst->pp_used == ppconst_used + 2 |
| 4230 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4231 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4232 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4233 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4234 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4235 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4236 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4237 | // both are numbers: compute the result |
| 4238 | switch (*op) |
| 4239 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4240 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4241 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4242 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4243 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4244 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4245 | break; |
| 4246 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4247 | tv1->vval.v_number = res; |
| 4248 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4249 | } |
| 4250 | else |
| 4251 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4252 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4253 | generate_two_op(cctx, op); |
| 4254 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4255 | } |
| 4256 | |
| 4257 | return OK; |
| 4258 | } |
| 4259 | |
| 4260 | /* |
| 4261 | * + number addition |
| 4262 | * - number subtraction |
| 4263 | * .. string concatenation |
| 4264 | */ |
| 4265 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4266 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4267 | { |
| 4268 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4269 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4270 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4271 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4272 | |
| 4273 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4274 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4275 | return FAIL; |
| 4276 | |
| 4277 | /* |
| 4278 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4279 | */ |
| 4280 | for (;;) |
| 4281 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4282 | op = may_peek_next_line(cctx, *arg, &next); |
| 4283 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4284 | break; |
| 4285 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4286 | if (next != NULL) |
| 4287 | { |
| 4288 | *arg = next_line_from_context(cctx, TRUE); |
| 4289 | op = skipwhite(*arg); |
| 4290 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4291 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4292 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4293 | { |
| 4294 | char_u buf[3]; |
| 4295 | |
| 4296 | vim_strncpy(buf, op, oplen); |
| 4297 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4298 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4299 | } |
| 4300 | |
| 4301 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4302 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4303 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4304 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4305 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4306 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4307 | return FAIL; |
| 4308 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4309 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4310 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4311 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4312 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4313 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4314 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4315 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4316 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4317 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4318 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4319 | // concat/subtract/add constant numbers |
| 4320 | if (*op == '+') |
| 4321 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4322 | else if (*op == '-') |
| 4323 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4324 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4325 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4326 | // concatenate constant strings |
| 4327 | char_u *s1 = tv1->vval.v_string; |
| 4328 | char_u *s2 = tv2->vval.v_string; |
| 4329 | size_t len1 = STRLEN(s1); |
| 4330 | |
| 4331 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4332 | if (tv1->vval.v_string == NULL) |
| 4333 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4334 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4335 | return FAIL; |
| 4336 | } |
| 4337 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4338 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4339 | vim_free(s1); |
| 4340 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4341 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4342 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4343 | } |
| 4344 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4345 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4346 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4347 | if (*op == '.') |
| 4348 | { |
| 4349 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4350 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4351 | return FAIL; |
| 4352 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4353 | } |
| 4354 | else |
| 4355 | generate_two_op(cctx, op); |
| 4356 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4357 | } |
| 4358 | |
| 4359 | return OK; |
| 4360 | } |
| 4361 | |
| 4362 | /* |
| 4363 | * expr5a == expr5b |
| 4364 | * expr5a =~ expr5b |
| 4365 | * expr5a != expr5b |
| 4366 | * expr5a !~ expr5b |
| 4367 | * expr5a > expr5b |
| 4368 | * expr5a >= expr5b |
| 4369 | * expr5a < expr5b |
| 4370 | * expr5a <= expr5b |
| 4371 | * expr5a is expr5b |
| 4372 | * expr5a isnot expr5b |
| 4373 | * |
| 4374 | * Produces instructions: |
| 4375 | * EVAL expr5a Push result of "expr5a" |
| 4376 | * EVAL expr5b Push result of "expr5b" |
| 4377 | * COMPARE one of the compare instructions |
| 4378 | */ |
| 4379 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4380 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4381 | { |
| 4382 | exptype_T type = EXPR_UNKNOWN; |
| 4383 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4384 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4385 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4386 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4387 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4388 | |
| 4389 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4390 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4391 | return FAIL; |
| 4392 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4393 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4394 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4395 | |
| 4396 | /* |
| 4397 | * If there is a comparative operator, use it. |
| 4398 | */ |
| 4399 | if (type != EXPR_UNKNOWN) |
| 4400 | { |
| 4401 | int ic = FALSE; // Default: do not ignore case |
| 4402 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4403 | if (next != NULL) |
| 4404 | { |
| 4405 | *arg = next_line_from_context(cctx, TRUE); |
| 4406 | p = skipwhite(*arg); |
| 4407 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4408 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4409 | { |
| 4410 | semsg(_(e_invexpr2), *arg); |
| 4411 | return FAIL; |
| 4412 | } |
| 4413 | // extra question mark appended: ignore case |
| 4414 | if (p[len] == '?') |
| 4415 | { |
| 4416 | ic = TRUE; |
| 4417 | ++len; |
| 4418 | } |
| 4419 | // extra '#' appended: match case (ignored) |
| 4420 | else if (p[len] == '#') |
| 4421 | ++len; |
| 4422 | // nothing appended: match case |
| 4423 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4424 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4425 | { |
| 4426 | char_u buf[7]; |
| 4427 | |
| 4428 | vim_strncpy(buf, p, len); |
| 4429 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4430 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4431 | } |
| 4432 | |
| 4433 | // get the second variable |
| 4434 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4435 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4436 | return FAIL; |
| 4437 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4438 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4439 | return FAIL; |
| 4440 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4441 | if (ppconst->pp_used == ppconst_used + 2) |
| 4442 | { |
| 4443 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4444 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4445 | int ret; |
| 4446 | |
| 4447 | // Both sides are a constant, compute the result now. |
| 4448 | // First check for a valid combination of types, this is more |
| 4449 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4450 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4451 | ret = FAIL; |
| 4452 | else |
| 4453 | { |
| 4454 | ret = typval_compare(tv1, tv2, type, ic); |
| 4455 | tv1->v_type = VAR_BOOL; |
| 4456 | tv1->vval.v_number = tv1->vval.v_number |
| 4457 | ? VVAL_TRUE : VVAL_FALSE; |
| 4458 | clear_tv(tv2); |
| 4459 | --ppconst->pp_used; |
| 4460 | } |
| 4461 | return ret; |
| 4462 | } |
| 4463 | |
| 4464 | generate_ppconst(cctx, ppconst); |
| 4465 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4466 | } |
| 4467 | |
| 4468 | return OK; |
| 4469 | } |
| 4470 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4471 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4472 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4473 | /* |
| 4474 | * Compile || or &&. |
| 4475 | */ |
| 4476 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4477 | compile_and_or( |
| 4478 | char_u **arg, |
| 4479 | cctx_T *cctx, |
| 4480 | char *op, |
| 4481 | ppconst_T *ppconst, |
| 4482 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4483 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4484 | char_u *next; |
| 4485 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4486 | int opchar = *op; |
| 4487 | |
| 4488 | if (p[0] == opchar && p[1] == opchar) |
| 4489 | { |
| 4490 | garray_T *instr = &cctx->ctx_instr; |
| 4491 | garray_T end_ga; |
| 4492 | |
| 4493 | /* |
| 4494 | * Repeat until there is no following "||" or "&&" |
| 4495 | */ |
| 4496 | ga_init2(&end_ga, sizeof(int), 10); |
| 4497 | while (p[0] == opchar && p[1] == opchar) |
| 4498 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4499 | if (next != NULL) |
| 4500 | { |
| 4501 | *arg = next_line_from_context(cctx, TRUE); |
| 4502 | p = skipwhite(*arg); |
| 4503 | } |
| 4504 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4505 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4506 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4507 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4508 | return FAIL; |
| 4509 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4510 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4511 | // TODO: use ppconst if the value is a constant |
| 4512 | generate_ppconst(cctx, ppconst); |
| 4513 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4514 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4515 | { |
| 4516 | ga_clear(&end_ga); |
| 4517 | return FAIL; |
| 4518 | } |
| 4519 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4520 | ++end_ga.ga_len; |
| 4521 | generate_JUMP(cctx, opchar == '|' |
| 4522 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 4523 | |
| 4524 | // eval the next expression |
| 4525 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4526 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4527 | return FAIL; |
| 4528 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4529 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4530 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4531 | { |
| 4532 | ga_clear(&end_ga); |
| 4533 | return FAIL; |
| 4534 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4535 | |
| 4536 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4537 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4538 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4539 | |
| 4540 | // Fill in the end label in all jumps. |
| 4541 | while (end_ga.ga_len > 0) |
| 4542 | { |
| 4543 | isn_T *isn; |
| 4544 | |
| 4545 | --end_ga.ga_len; |
| 4546 | isn = ((isn_T *)instr->ga_data) |
| 4547 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4548 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4549 | } |
| 4550 | ga_clear(&end_ga); |
| 4551 | } |
| 4552 | |
| 4553 | return OK; |
| 4554 | } |
| 4555 | |
| 4556 | /* |
| 4557 | * expr4a && expr4a && expr4a logical AND |
| 4558 | * |
| 4559 | * Produces instructions: |
| 4560 | * EVAL expr4a Push result of "expr4a" |
| 4561 | * JUMP_AND_KEEP_IF_FALSE end |
| 4562 | * EVAL expr4b Push result of "expr4b" |
| 4563 | * JUMP_AND_KEEP_IF_FALSE end |
| 4564 | * EVAL expr4c Push result of "expr4c" |
| 4565 | * end: |
| 4566 | */ |
| 4567 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4568 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4569 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4570 | int ppconst_used = ppconst->pp_used; |
| 4571 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4572 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4573 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4574 | return FAIL; |
| 4575 | |
| 4576 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4577 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4578 | } |
| 4579 | |
| 4580 | /* |
| 4581 | * expr3a || expr3b || expr3c logical OR |
| 4582 | * |
| 4583 | * Produces instructions: |
| 4584 | * EVAL expr3a Push result of "expr3a" |
| 4585 | * JUMP_AND_KEEP_IF_TRUE end |
| 4586 | * EVAL expr3b Push result of "expr3b" |
| 4587 | * JUMP_AND_KEEP_IF_TRUE end |
| 4588 | * EVAL expr3c Push result of "expr3c" |
| 4589 | * end: |
| 4590 | */ |
| 4591 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4592 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4593 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4594 | int ppconst_used = ppconst->pp_used; |
| 4595 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4596 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4597 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4598 | return FAIL; |
| 4599 | |
| 4600 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4601 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | /* |
| 4605 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4606 | * |
| 4607 | * Produces instructions: |
| 4608 | * EVAL expr2 Push result of "expr" |
| 4609 | * JUMP_IF_FALSE alt jump if false |
| 4610 | * EVAL expr1a |
| 4611 | * JUMP_ALWAYS end |
| 4612 | * alt: EVAL expr1b |
| 4613 | * end: |
| 4614 | */ |
| 4615 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4616 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4617 | { |
| 4618 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4619 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4620 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4621 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4622 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4623 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4624 | return FAIL; |
| 4625 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4626 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4627 | if (*p == '?') |
| 4628 | { |
| 4629 | garray_T *instr = &cctx->ctx_instr; |
| 4630 | garray_T *stack = &cctx->ctx_type_stack; |
| 4631 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4632 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4633 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4634 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4635 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4636 | int has_const_expr = FALSE; |
| 4637 | int const_value = FALSE; |
| 4638 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4639 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4640 | if (next != NULL) |
| 4641 | { |
| 4642 | *arg = next_line_from_context(cctx, TRUE); |
| 4643 | p = skipwhite(*arg); |
| 4644 | } |
| 4645 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4646 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4647 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4648 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4649 | return FAIL; |
| 4650 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4651 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4652 | if (ppconst->pp_used == ppconst_used + 1) |
| 4653 | { |
| 4654 | // the condition is a constant, we know whether the ? or the : |
| 4655 | // expression is to be evaluated. |
| 4656 | has_const_expr = TRUE; |
| 4657 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4658 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4659 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4660 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4661 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4662 | } |
| 4663 | else |
| 4664 | { |
| 4665 | generate_ppconst(cctx, ppconst); |
| 4666 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4667 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4668 | |
| 4669 | // evaluate the second expression; any type is accepted |
| 4670 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4671 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4672 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4673 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4674 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4675 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4676 | if (!has_const_expr) |
| 4677 | { |
| 4678 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4679 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4680 | // remember the type and drop it |
| 4681 | --stack->ga_len; |
| 4682 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4683 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4684 | end_idx = instr->ga_len; |
| 4685 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4686 | |
| 4687 | // jump here from JUMP_IF_FALSE |
| 4688 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4689 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4690 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4691 | |
| 4692 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4693 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4694 | if (*p != ':') |
| 4695 | { |
| 4696 | emsg(_(e_missing_colon)); |
| 4697 | return FAIL; |
| 4698 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4699 | if (next != NULL) |
| 4700 | { |
| 4701 | *arg = next_line_from_context(cctx, TRUE); |
| 4702 | p = skipwhite(*arg); |
| 4703 | } |
| 4704 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4705 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4706 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4707 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4708 | return FAIL; |
| 4709 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4710 | |
| 4711 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4712 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4713 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4714 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4715 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4716 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4717 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4718 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4719 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4720 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4721 | if (!has_const_expr) |
| 4722 | { |
| 4723 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4724 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4725 | // If the types differ, the result has a more generic type. |
| 4726 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4727 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4728 | |
| 4729 | // jump here from JUMP_ALWAYS |
| 4730 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4731 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4732 | } |
| 4733 | |
| 4734 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4735 | } |
| 4736 | return OK; |
| 4737 | } |
| 4738 | |
| 4739 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4740 | * Toplevel expression. |
| 4741 | */ |
| 4742 | static int |
| 4743 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4744 | { |
| 4745 | ppconst_T ppconst; |
| 4746 | |
| 4747 | CLEAR_FIELD(ppconst); |
| 4748 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4749 | { |
| 4750 | clear_ppconst(&ppconst); |
| 4751 | return FAIL; |
| 4752 | } |
| 4753 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4754 | return FAIL; |
| 4755 | return OK; |
| 4756 | } |
| 4757 | |
| 4758 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4759 | * compile "return [expr]" |
| 4760 | */ |
| 4761 | static char_u * |
| 4762 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4763 | { |
| 4764 | char_u *p = arg; |
| 4765 | garray_T *stack = &cctx->ctx_type_stack; |
| 4766 | type_T *stack_type; |
| 4767 | |
| 4768 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4769 | { |
| 4770 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4771 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4772 | return NULL; |
| 4773 | |
| 4774 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4775 | if (set_return_type) |
| 4776 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4777 | else |
| 4778 | { |
| 4779 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4780 | && stack_type->tt_type != VAR_VOID |
| 4781 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4782 | { |
| 4783 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4784 | return NULL; |
| 4785 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4786 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4787 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4788 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4789 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4790 | } |
| 4791 | else |
| 4792 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4793 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4794 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4795 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4796 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4797 | { |
| 4798 | emsg(_("E1003: Missing return value")); |
| 4799 | return NULL; |
| 4800 | } |
| 4801 | |
| 4802 | // No argument, return zero. |
| 4803 | generate_PUSHNR(cctx, 0); |
| 4804 | } |
| 4805 | |
| 4806 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4807 | return NULL; |
| 4808 | |
| 4809 | // "return val | endif" is possible |
| 4810 | return skipwhite(p); |
| 4811 | } |
| 4812 | |
| 4813 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4814 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4815 | * Return a pointer to the line in allocated memory. |
| 4816 | * Return NULL for end-of-file or some error. |
| 4817 | */ |
| 4818 | static char_u * |
| 4819 | exarg_getline( |
| 4820 | int c UNUSED, |
| 4821 | void *cookie, |
| 4822 | int indent UNUSED, |
| 4823 | int do_concat UNUSED) |
| 4824 | { |
| 4825 | cctx_T *cctx = (cctx_T *)cookie; |
| 4826 | |
| 4827 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4828 | { |
| 4829 | iemsg("Heredoc got to end"); |
| 4830 | return NULL; |
| 4831 | } |
| 4832 | ++cctx->ctx_lnum; |
| 4833 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4834 | [cctx->ctx_lnum]); |
| 4835 | } |
| 4836 | |
| 4837 | /* |
| 4838 | * Compile a nested :def command. |
| 4839 | */ |
| 4840 | static char_u * |
| 4841 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4842 | { |
| 4843 | char_u *name_start = eap->arg; |
| 4844 | char_u *name_end = to_name_end(eap->arg, FALSE); |
| 4845 | char_u *name = get_lambda_name(); |
| 4846 | lvar_T *lvar; |
| 4847 | ufunc_T *ufunc; |
| 4848 | |
| 4849 | eap->arg = name_end; |
| 4850 | eap->getline = exarg_getline; |
| 4851 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4852 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4853 | eap->forceit = FALSE; |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4854 | ufunc = def_function(eap, name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4855 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4856 | if (ufunc == NULL) |
| 4857 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4858 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4859 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4860 | return NULL; |
| 4861 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4862 | // Define a local variable for the function reference. |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4863 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4864 | TRUE, ufunc->uf_func_type); |
| 4865 | |
| 4866 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL |
| 4867 | || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL) |
| 4868 | return NULL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4869 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4870 | // TODO: warning for trailing text? |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4871 | return (char_u *)""; |
| 4872 | } |
| 4873 | |
| 4874 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4875 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4876 | */ |
| 4877 | int |
| 4878 | assignment_len(char_u *p, int *heredoc) |
| 4879 | { |
| 4880 | if (*p == '=') |
| 4881 | { |
| 4882 | if (p[1] == '<' && p[2] == '<') |
| 4883 | { |
| 4884 | *heredoc = TRUE; |
| 4885 | return 3; |
| 4886 | } |
| 4887 | return 1; |
| 4888 | } |
| 4889 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4890 | return 2; |
| 4891 | if (STRNCMP(p, "..=", 3) == 0) |
| 4892 | return 3; |
| 4893 | return 0; |
| 4894 | } |
| 4895 | |
| 4896 | // words that cannot be used as a variable |
| 4897 | static char *reserved[] = { |
| 4898 | "true", |
| 4899 | "false", |
| 4900 | NULL |
| 4901 | }; |
| 4902 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4903 | typedef enum { |
| 4904 | dest_local, |
| 4905 | dest_option, |
| 4906 | dest_env, |
| 4907 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4908 | dest_buffer, |
| 4909 | dest_window, |
| 4910 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4911 | dest_vimvar, |
| 4912 | dest_script, |
| 4913 | dest_reg, |
| 4914 | } assign_dest_T; |
| 4915 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4916 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4917 | * Generate the load instruction for "name". |
| 4918 | */ |
| 4919 | static void |
| 4920 | generate_loadvar( |
| 4921 | cctx_T *cctx, |
| 4922 | assign_dest_T dest, |
| 4923 | char_u *name, |
| 4924 | lvar_T *lvar, |
| 4925 | type_T *type) |
| 4926 | { |
| 4927 | switch (dest) |
| 4928 | { |
| 4929 | case dest_option: |
| 4930 | // TODO: check the option exists |
| 4931 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4932 | break; |
| 4933 | case dest_global: |
| 4934 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4935 | break; |
| 4936 | case dest_buffer: |
| 4937 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4938 | break; |
| 4939 | case dest_window: |
| 4940 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4941 | break; |
| 4942 | case dest_tab: |
| 4943 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4944 | break; |
| 4945 | case dest_script: |
| 4946 | compile_load_scriptvar(cctx, |
| 4947 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4948 | break; |
| 4949 | case dest_env: |
| 4950 | // Include $ in the name here |
| 4951 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4952 | break; |
| 4953 | case dest_reg: |
| 4954 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4955 | break; |
| 4956 | case dest_vimvar: |
| 4957 | generate_LOADV(cctx, name + 2, TRUE); |
| 4958 | break; |
| 4959 | case dest_local: |
| 4960 | if (lvar->lv_from_outer) |
| 4961 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4962 | NULL, type); |
| 4963 | else |
| 4964 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4965 | break; |
| 4966 | } |
| 4967 | } |
| 4968 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4969 | void |
| 4970 | vim9_declare_error(char_u *name) |
| 4971 | { |
| 4972 | char *scope = ""; |
| 4973 | |
| 4974 | switch (*name) |
| 4975 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4976 | case 'g': scope = _("global"); break; |
| 4977 | case 'b': scope = _("buffer"); break; |
| 4978 | case 'w': scope = _("window"); break; |
| 4979 | case 't': scope = _("tab"); break; |
| 4980 | case 'v': scope = "v:"; break; |
| 4981 | case '$': semsg(_(e_declare_env_var), name); return; |
| 4982 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4983 | } |
| 4984 | semsg(_(e_declare_var), scope, name); |
| 4985 | } |
| 4986 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4987 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4988 | * Compile declaration and assignment: |
| 4989 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4990 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4991 | * Return NULL for an error. |
| 4992 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4993 | */ |
| 4994 | static char_u * |
| 4995 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4996 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4997 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4998 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4999 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5000 | char_u *ret = NULL; |
| 5001 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5002 | int var_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5003 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5004 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5005 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5006 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5007 | int oplen = 0; |
| 5008 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 5009 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5010 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5011 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5012 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5013 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5014 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5015 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 5016 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 5017 | if (p == NULL) |
| 5018 | return *arg == '[' ? arg : NULL; |
| 5019 | |
| 5020 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5021 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 5022 | // TODO: should we allow this, and figure out type inference from list |
| 5023 | // members? |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5024 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5025 | return NULL; |
| 5026 | } |
| 5027 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5028 | sp = p; |
| 5029 | p = skipwhite(p); |
| 5030 | op = p; |
| 5031 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5032 | |
| 5033 | if (var_count > 0 && oplen == 0) |
| 5034 | // can be something like "[1, 2]->func()" |
| 5035 | return arg; |
| 5036 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5037 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 5038 | { |
| 5039 | char_u buf[4]; |
| 5040 | |
| 5041 | vim_strncpy(buf, op, oplen); |
| 5042 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5043 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 5044 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5045 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5046 | if (heredoc) |
| 5047 | { |
| 5048 | list_T *l; |
| 5049 | listitem_T *li; |
| 5050 | |
| 5051 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5052 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5053 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 5054 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5055 | |
| 5056 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 5057 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5058 | { |
| 5059 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 5060 | li->li_tv.vval.v_string = NULL; |
| 5061 | } |
| 5062 | generate_NEWLIST(cctx, l->lv_len); |
| 5063 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5064 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5065 | list_free(l); |
| 5066 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5067 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5068 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5069 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5070 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5071 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 5072 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5073 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5074 | p = skipwhite(op + oplen); |
| 5075 | if (compile_expr0(&p, cctx) == FAIL) |
| 5076 | return NULL; |
| 5077 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5078 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5079 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5080 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5081 | type_T *stacktype; |
| 5082 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5083 | stacktype = stack->ga_len == 0 ? &t_void |
| 5084 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5085 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5086 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5087 | emsg(_(e_cannot_use_void)); |
| 5088 | goto theend; |
| 5089 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5090 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5091 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5092 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5093 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 5094 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5095 | } |
| 5096 | } |
| 5097 | |
| 5098 | /* |
| 5099 | * Loop over variables in "[var, var] = expr". |
| 5100 | * For "var = expr" and "let var: type" this is done only once. |
| 5101 | */ |
| 5102 | if (var_count > 0) |
| 5103 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 5104 | else |
| 5105 | var_start = arg; |
| 5106 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 5107 | { |
| 5108 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 5109 | size_t varlen; |
| 5110 | int new_local = FALSE; |
| 5111 | int opt_type; |
| 5112 | int opt_flags = 0; |
| 5113 | assign_dest_T dest = dest_local; |
| 5114 | int vimvaridx = -1; |
| 5115 | lvar_T *lvar = NULL; |
| 5116 | lvar_T arg_lvar; |
| 5117 | int has_type = FALSE; |
| 5118 | int has_index = FALSE; |
| 5119 | int instr_count = -1; |
| 5120 | |
| 5121 | p = (*var_start == '&' || *var_start == '$' |
| 5122 | || *var_start == '@') ? var_start + 1 : var_start; |
| 5123 | p = to_name_end(p, TRUE); |
| 5124 | |
| 5125 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 5126 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 5127 | --var_end; |
| 5128 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 5129 | --p; |
| 5130 | |
| 5131 | varlen = p - var_start; |
| 5132 | vim_free(name); |
| 5133 | name = vim_strnsave(var_start, varlen); |
| 5134 | if (name == NULL) |
| 5135 | return NULL; |
| 5136 | if (!heredoc) |
| 5137 | type = &t_any; |
| 5138 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5139 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5140 | { |
| 5141 | if (*var_start == '&') |
| 5142 | { |
| 5143 | int cc; |
| 5144 | long numval; |
| 5145 | |
| 5146 | dest = dest_option; |
| 5147 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5148 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5149 | emsg(_(e_const_option)); |
| 5150 | goto theend; |
| 5151 | } |
| 5152 | if (is_decl) |
| 5153 | { |
| 5154 | semsg(_("E1052: Cannot declare an option: %s"), var_start); |
| 5155 | goto theend; |
| 5156 | } |
| 5157 | p = var_start; |
| 5158 | p = find_option_end(&p, &opt_flags); |
| 5159 | if (p == NULL) |
| 5160 | { |
| 5161 | // cannot happen? |
| 5162 | emsg(_(e_letunexp)); |
| 5163 | goto theend; |
| 5164 | } |
| 5165 | cc = *p; |
| 5166 | *p = NUL; |
| 5167 | opt_type = get_option_value(var_start + 1, &numval, |
| 5168 | NULL, opt_flags); |
| 5169 | *p = cc; |
| 5170 | if (opt_type == -3) |
| 5171 | { |
| 5172 | semsg(_(e_unknown_option), var_start); |
| 5173 | goto theend; |
| 5174 | } |
| 5175 | if (opt_type == -2 || opt_type == 0) |
| 5176 | type = &t_string; |
| 5177 | else |
| 5178 | type = &t_number; // both number and boolean option |
| 5179 | } |
| 5180 | else if (*var_start == '$') |
| 5181 | { |
| 5182 | dest = dest_env; |
| 5183 | type = &t_string; |
| 5184 | if (is_decl) |
| 5185 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5186 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5187 | goto theend; |
| 5188 | } |
| 5189 | } |
| 5190 | else if (*var_start == '@') |
| 5191 | { |
| 5192 | if (!valid_yank_reg(var_start[1], TRUE)) |
| 5193 | { |
| 5194 | emsg_invreg(var_start[1]); |
| 5195 | goto theend; |
| 5196 | } |
| 5197 | dest = dest_reg; |
| 5198 | type = &t_string; |
| 5199 | if (is_decl) |
| 5200 | { |
| 5201 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 5202 | goto theend; |
| 5203 | } |
| 5204 | } |
| 5205 | else if (STRNCMP(var_start, "g:", 2) == 0) |
| 5206 | { |
| 5207 | dest = dest_global; |
| 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 if (STRNCMP(var_start, "b:", 2) == 0) |
| 5215 | { |
| 5216 | dest = dest_buffer; |
| 5217 | if (is_decl) |
| 5218 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5219 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5220 | goto theend; |
| 5221 | } |
| 5222 | } |
| 5223 | else if (STRNCMP(var_start, "w:", 2) == 0) |
| 5224 | { |
| 5225 | dest = dest_window; |
| 5226 | if (is_decl) |
| 5227 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5228 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5229 | goto theend; |
| 5230 | } |
| 5231 | } |
| 5232 | else if (STRNCMP(var_start, "t:", 2) == 0) |
| 5233 | { |
| 5234 | dest = dest_tab; |
| 5235 | if (is_decl) |
| 5236 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5237 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5238 | goto theend; |
| 5239 | } |
| 5240 | } |
| 5241 | else if (STRNCMP(var_start, "v:", 2) == 0) |
| 5242 | { |
| 5243 | typval_T *vtv; |
| 5244 | int di_flags; |
| 5245 | |
| 5246 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5247 | if (vimvaridx < 0) |
| 5248 | { |
| 5249 | semsg(_(e_var_notfound), var_start); |
| 5250 | goto theend; |
| 5251 | } |
| 5252 | // We use the current value of "sandbox" here, is that OK? |
| 5253 | if (var_check_ro(di_flags, name, FALSE)) |
| 5254 | goto theend; |
| 5255 | dest = dest_vimvar; |
| 5256 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 5257 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5258 | if (is_decl) |
| 5259 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5260 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5261 | goto theend; |
| 5262 | } |
| 5263 | } |
| 5264 | else |
| 5265 | { |
| 5266 | int idx; |
| 5267 | |
| 5268 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5269 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5270 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5271 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5272 | goto theend; |
| 5273 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5274 | |
| 5275 | lvar = lookup_local(var_start, varlen, cctx); |
| 5276 | if (lvar == NULL) |
| 5277 | { |
| 5278 | CLEAR_FIELD(arg_lvar); |
| 5279 | if (lookup_arg(var_start, varlen, |
| 5280 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 5281 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5282 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5283 | if (is_decl) |
| 5284 | { |
| 5285 | semsg(_(e_used_as_arg), name); |
| 5286 | goto theend; |
| 5287 | } |
| 5288 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5289 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5290 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5291 | if (lvar != NULL) |
| 5292 | { |
| 5293 | if (is_decl) |
| 5294 | { |
| 5295 | semsg(_("E1017: Variable already declared: %s"), name); |
| 5296 | goto theend; |
| 5297 | } |
| 5298 | else if (lvar->lv_const) |
| 5299 | { |
| 5300 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 5301 | name); |
| 5302 | goto theend; |
| 5303 | } |
| 5304 | } |
| 5305 | else if (STRNCMP(var_start, "s:", 2) == 0 |
| 5306 | || lookup_script(var_start, varlen) == OK |
| 5307 | || find_imported(var_start, varlen, cctx) != NULL) |
| 5308 | { |
| 5309 | dest = dest_script; |
| 5310 | if (is_decl) |
| 5311 | { |
| 5312 | semsg(_("E1054: Variable already declared in the script: %s"), |
| 5313 | name); |
| 5314 | goto theend; |
| 5315 | } |
| 5316 | } |
| 5317 | else if (name[1] == ':' && name[2] != NUL) |
| 5318 | { |
| 5319 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
| 5320 | name); |
| 5321 | goto theend; |
| 5322 | } |
| 5323 | else if (!is_decl) |
| 5324 | { |
| 5325 | semsg(_("E1089: unknown variable: %s"), name); |
| 5326 | goto theend; |
| 5327 | } |
| 5328 | } |
| 5329 | } |
| 5330 | |
| 5331 | // handle "a:name" as a name, not index "name" on "a" |
| 5332 | if (varlen > 1 || var_start[varlen] != ':') |
| 5333 | p = var_end; |
| 5334 | |
| 5335 | if (dest != dest_option) |
| 5336 | { |
| 5337 | if (is_decl && *p == ':') |
| 5338 | { |
| 5339 | // parse optional type: "let var: type = expr" |
| 5340 | if (!VIM_ISWHITE(p[1])) |
| 5341 | { |
| 5342 | semsg(_(e_white_after), ":"); |
| 5343 | goto theend; |
| 5344 | } |
| 5345 | p = skipwhite(p + 1); |
| 5346 | type = parse_type(&p, cctx->ctx_type_list); |
| 5347 | has_type = TRUE; |
| 5348 | } |
| 5349 | else if (lvar != NULL) |
| 5350 | type = lvar->lv_type; |
| 5351 | } |
| 5352 | |
| 5353 | if (oplen == 3 && !heredoc && dest != dest_global |
| 5354 | && type->tt_type != VAR_STRING |
| 5355 | && type->tt_type != VAR_ANY) |
| 5356 | { |
| 5357 | emsg(_("E1019: Can only concatenate to string")); |
| 5358 | goto theend; |
| 5359 | } |
| 5360 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5361 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5362 | { |
| 5363 | if (oplen > 1 && !heredoc) |
| 5364 | { |
| 5365 | // +=, /=, etc. require an existing variable |
| 5366 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 5367 | name); |
| 5368 | goto theend; |
| 5369 | } |
| 5370 | |
| 5371 | // new local variable |
| 5372 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
| 5373 | goto theend; |
| 5374 | lvar = reserve_local(cctx, var_start, varlen, |
| 5375 | cmdidx == CMD_const, type); |
| 5376 | if (lvar == NULL) |
| 5377 | goto theend; |
| 5378 | new_local = TRUE; |
| 5379 | } |
| 5380 | |
| 5381 | member_type = type; |
| 5382 | if (var_end > var_start + varlen) |
| 5383 | { |
| 5384 | // Something follows after the variable: "var[idx]". |
| 5385 | if (is_decl) |
| 5386 | { |
| 5387 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 5388 | goto theend; |
| 5389 | } |
| 5390 | |
| 5391 | if (var_start[varlen] == '[') |
| 5392 | { |
| 5393 | has_index = TRUE; |
| 5394 | if (type->tt_member == NULL) |
| 5395 | { |
| 5396 | semsg(_("E1088: cannot use an index on %s"), name); |
| 5397 | goto theend; |
| 5398 | } |
| 5399 | member_type = type->tt_member; |
| 5400 | } |
| 5401 | else |
| 5402 | { |
| 5403 | semsg("Not supported yet: %s", var_start); |
| 5404 | goto theend; |
| 5405 | } |
| 5406 | } |
| 5407 | else if (lvar == &arg_lvar) |
| 5408 | { |
| 5409 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 5410 | goto theend; |
| 5411 | } |
| 5412 | |
| 5413 | if (!heredoc) |
| 5414 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5415 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5416 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5417 | if (oplen > 0 && var_count == 0) |
| 5418 | { |
| 5419 | // skip over the "=" and the expression |
| 5420 | p = skipwhite(op + oplen); |
| 5421 | compile_expr0(&p, cctx); |
| 5422 | } |
| 5423 | } |
| 5424 | else if (oplen > 0) |
| 5425 | { |
| 5426 | type_T *stacktype; |
| 5427 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5428 | // For "var = expr" evaluate the expression. |
| 5429 | if (var_count == 0) |
| 5430 | { |
| 5431 | int r; |
| 5432 | |
| 5433 | // for "+=", "*=", "..=" etc. first load the current value |
| 5434 | if (*op != '=') |
| 5435 | { |
| 5436 | generate_loadvar(cctx, dest, name, lvar, type); |
| 5437 | |
| 5438 | if (has_index) |
| 5439 | { |
| 5440 | // TODO: get member from list or dict |
| 5441 | emsg("Index with operation not supported yet"); |
| 5442 | goto theend; |
| 5443 | } |
| 5444 | } |
| 5445 | |
| 5446 | // Compile the expression. Temporarily hide the new local |
| 5447 | // variable here, it is not available to this expression. |
| 5448 | if (new_local) |
| 5449 | --cctx->ctx_locals.ga_len; |
| 5450 | instr_count = instr->ga_len; |
| 5451 | p = skipwhite(op + oplen); |
| 5452 | r = compile_expr0(&p, cctx); |
| 5453 | if (new_local) |
| 5454 | ++cctx->ctx_locals.ga_len; |
| 5455 | if (r == FAIL) |
| 5456 | goto theend; |
| 5457 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5458 | else if (semicolon && var_idx == var_count - 1) |
| 5459 | { |
| 5460 | // For "[var; var] = expr" get the rest of the list |
| 5461 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5462 | goto theend; |
| 5463 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5464 | else |
| 5465 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5466 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5467 | // list. |
| 5468 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 5469 | return FAIL; |
| 5470 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5471 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5472 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5473 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5474 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5475 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5476 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5477 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5478 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5479 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5480 | emsg(_(e_cannot_use_void)); |
| 5481 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5482 | } |
| 5483 | else |
| 5484 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5485 | // An empty list or dict has a &t_void member, |
| 5486 | // for a variable that implies &t_any. |
| 5487 | if (stacktype == &t_list_empty) |
| 5488 | lvar->lv_type = &t_list_any; |
| 5489 | else if (stacktype == &t_dict_empty) |
| 5490 | lvar->lv_type = &t_dict_any; |
| 5491 | else |
| 5492 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5493 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5494 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5495 | else |
| 5496 | { |
| 5497 | type_T *use_type = lvar->lv_type; |
| 5498 | |
| 5499 | if (has_index) |
| 5500 | { |
| 5501 | use_type = use_type->tt_member; |
| 5502 | if (use_type == NULL) |
| 5503 | use_type = &t_void; |
| 5504 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5505 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5506 | == FAIL) |
| 5507 | goto theend; |
| 5508 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5509 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5510 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5511 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5512 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5513 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5514 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5515 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5516 | emsg(_(e_const_req_value)); |
| 5517 | goto theend; |
| 5518 | } |
| 5519 | else if (!has_type || dest == dest_option) |
| 5520 | { |
| 5521 | emsg(_(e_type_req)); |
| 5522 | goto theend; |
| 5523 | } |
| 5524 | else |
| 5525 | { |
| 5526 | // variables are always initialized |
| 5527 | if (ga_grow(instr, 1) == FAIL) |
| 5528 | goto theend; |
| 5529 | switch (member_type->tt_type) |
| 5530 | { |
| 5531 | case VAR_BOOL: |
| 5532 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5533 | break; |
| 5534 | case VAR_FLOAT: |
| 5535 | #ifdef FEAT_FLOAT |
| 5536 | generate_PUSHF(cctx, 0.0); |
| 5537 | #endif |
| 5538 | break; |
| 5539 | case VAR_STRING: |
| 5540 | generate_PUSHS(cctx, NULL); |
| 5541 | break; |
| 5542 | case VAR_BLOB: |
| 5543 | generate_PUSHBLOB(cctx, NULL); |
| 5544 | break; |
| 5545 | case VAR_FUNC: |
| 5546 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5547 | break; |
| 5548 | case VAR_LIST: |
| 5549 | generate_NEWLIST(cctx, 0); |
| 5550 | break; |
| 5551 | case VAR_DICT: |
| 5552 | generate_NEWDICT(cctx, 0); |
| 5553 | break; |
| 5554 | case VAR_JOB: |
| 5555 | generate_PUSHJOB(cctx, NULL); |
| 5556 | break; |
| 5557 | case VAR_CHANNEL: |
| 5558 | generate_PUSHCHANNEL(cctx, NULL); |
| 5559 | break; |
| 5560 | case VAR_NUMBER: |
| 5561 | case VAR_UNKNOWN: |
| 5562 | case VAR_ANY: |
| 5563 | case VAR_PARTIAL: |
| 5564 | case VAR_VOID: |
| 5565 | case VAR_SPECIAL: // cannot happen |
| 5566 | generate_PUSHNR(cctx, 0); |
| 5567 | break; |
| 5568 | } |
| 5569 | } |
| 5570 | if (var_count == 0) |
| 5571 | end = p; |
| 5572 | } |
| 5573 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5574 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5575 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5576 | break; |
| 5577 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5578 | if (oplen > 0 && *op != '=') |
| 5579 | { |
| 5580 | type_T *expected = &t_number; |
| 5581 | type_T *stacktype; |
| 5582 | |
| 5583 | // TODO: if type is known use float or any operation |
| 5584 | |
| 5585 | if (*op == '.') |
| 5586 | expected = &t_string; |
| 5587 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5588 | if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5589 | goto theend; |
| 5590 | |
| 5591 | if (*op == '.') |
| 5592 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 5593 | else |
| 5594 | { |
| 5595 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 5596 | |
| 5597 | if (isn == NULL) |
| 5598 | goto theend; |
| 5599 | switch (*op) |
| 5600 | { |
| 5601 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 5602 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 5603 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 5604 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 5605 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 5606 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5607 | } |
| 5608 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5609 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5610 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5611 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5612 | int r; |
| 5613 | |
| 5614 | // Compile the "idx" in "var[idx]". |
| 5615 | if (new_local) |
| 5616 | --cctx->ctx_locals.ga_len; |
| 5617 | p = skipwhite(var_start + varlen + 1); |
| 5618 | r = compile_expr0(&p, cctx); |
| 5619 | if (new_local) |
| 5620 | ++cctx->ctx_locals.ga_len; |
| 5621 | if (r == FAIL) |
| 5622 | goto theend; |
| 5623 | if (*skipwhite(p) != ']') |
| 5624 | { |
| 5625 | emsg(_(e_missbrac)); |
| 5626 | goto theend; |
| 5627 | } |
| 5628 | if (type->tt_type == VAR_DICT |
| 5629 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5630 | goto theend; |
| 5631 | if (type->tt_type == VAR_LIST |
| 5632 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5633 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5634 | { |
| 5635 | emsg(_(e_number_exp)); |
| 5636 | goto theend; |
| 5637 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5638 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5639 | // Load the dict or list. On the stack we then have: |
| 5640 | // - value |
| 5641 | // - index |
| 5642 | // - variable |
| 5643 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5644 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5645 | if (type->tt_type == VAR_LIST) |
| 5646 | { |
| 5647 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5648 | return FAIL; |
| 5649 | } |
| 5650 | else if (type->tt_type == VAR_DICT) |
| 5651 | { |
| 5652 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5653 | return FAIL; |
| 5654 | } |
| 5655 | else |
| 5656 | { |
| 5657 | emsg(_(e_listreq)); |
| 5658 | goto theend; |
| 5659 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5660 | } |
| 5661 | else |
| 5662 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5663 | switch (dest) |
| 5664 | { |
| 5665 | case dest_option: |
| 5666 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5667 | break; |
| 5668 | case dest_global: |
| 5669 | // include g: with the name, easier to execute that way |
| 5670 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5671 | break; |
| 5672 | case dest_buffer: |
| 5673 | // include b: with the name, easier to execute that way |
| 5674 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5675 | break; |
| 5676 | case dest_window: |
| 5677 | // include w: with the name, easier to execute that way |
| 5678 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5679 | break; |
| 5680 | case dest_tab: |
| 5681 | // include t: with the name, easier to execute that way |
| 5682 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5683 | break; |
| 5684 | case dest_env: |
| 5685 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5686 | break; |
| 5687 | case dest_reg: |
| 5688 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5689 | break; |
| 5690 | case dest_vimvar: |
| 5691 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5692 | break; |
| 5693 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5694 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5695 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5696 | imported_T *import = NULL; |
| 5697 | int sid = current_sctx.sc_sid; |
| 5698 | int idx; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5699 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5700 | if (name[1] != ':') |
| 5701 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5702 | import = find_imported(name, 0, cctx); |
| 5703 | if (import != NULL) |
| 5704 | sid = import->imp_sid; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5705 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5706 | |
| 5707 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 5708 | // TODO: specific type |
| 5709 | if (idx < 0) |
| 5710 | { |
| 5711 | char_u *name_s = name; |
| 5712 | |
| 5713 | // Include s: in the name for store_var() |
| 5714 | if (name[1] != ':') |
| 5715 | { |
| 5716 | int len = (int)STRLEN(name) + 3; |
| 5717 | |
| 5718 | name_s = alloc(len); |
| 5719 | if (name_s == NULL) |
| 5720 | name_s = name; |
| 5721 | else |
| 5722 | vim_snprintf((char *)name_s, len, |
| 5723 | "s:%s", name); |
| 5724 | } |
| 5725 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5726 | &t_any); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5727 | if (name_s != name) |
| 5728 | vim_free(name_s); |
| 5729 | } |
| 5730 | else |
| 5731 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5732 | sid, idx, &t_any); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5733 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5734 | break; |
| 5735 | case dest_local: |
| 5736 | if (lvar != NULL) |
| 5737 | { |
| 5738 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5739 | + instr->ga_len - 1; |
| 5740 | |
| 5741 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5742 | // ISN_STORE into ISN_STORENR |
| 5743 | if (!lvar->lv_from_outer |
| 5744 | && instr->ga_len == instr_count + 1 |
| 5745 | && isn->isn_type == ISN_PUSHNR) |
| 5746 | { |
| 5747 | varnumber_T val = isn->isn_arg.number; |
| 5748 | |
| 5749 | isn->isn_type = ISN_STORENR; |
| 5750 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5751 | isn->isn_arg.storenr.stnr_val = val; |
| 5752 | if (stack->ga_len > 0) |
| 5753 | --stack->ga_len; |
| 5754 | } |
| 5755 | else if (lvar->lv_from_outer) |
| 5756 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5757 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5758 | else |
| 5759 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5760 | } |
| 5761 | break; |
| 5762 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5763 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5764 | |
| 5765 | if (var_idx + 1 < var_count) |
| 5766 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5767 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5768 | |
| 5769 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5770 | if (var_count > 0 && !semicolon) |
| 5771 | { |
| 5772 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5773 | goto theend; |
| 5774 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5775 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5776 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5777 | |
| 5778 | theend: |
| 5779 | vim_free(name); |
| 5780 | return ret; |
| 5781 | } |
| 5782 | |
| 5783 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5784 | * Check if "name" can be "unlet". |
| 5785 | */ |
| 5786 | int |
| 5787 | check_vim9_unlet(char_u *name) |
| 5788 | { |
| 5789 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5790 | { |
| 5791 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5792 | return FAIL; |
| 5793 | } |
| 5794 | return OK; |
| 5795 | } |
| 5796 | |
| 5797 | /* |
| 5798 | * Callback passed to ex_unletlock(). |
| 5799 | */ |
| 5800 | static int |
| 5801 | compile_unlet( |
| 5802 | lval_T *lvp, |
| 5803 | char_u *name_end, |
| 5804 | exarg_T *eap, |
| 5805 | int deep UNUSED, |
| 5806 | void *coookie) |
| 5807 | { |
| 5808 | cctx_T *cctx = coookie; |
| 5809 | |
| 5810 | if (lvp->ll_tv == NULL) |
| 5811 | { |
| 5812 | char_u *p = lvp->ll_name; |
| 5813 | int cc = *name_end; |
| 5814 | int ret = OK; |
| 5815 | |
| 5816 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5817 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5818 | if (*p == '$') |
| 5819 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5820 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5821 | ret = FAIL; |
| 5822 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5823 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5824 | |
| 5825 | *name_end = cc; |
| 5826 | return ret; |
| 5827 | } |
| 5828 | |
| 5829 | // TODO: unlet {list}[idx] |
| 5830 | // TODO: unlet {dict}[key] |
| 5831 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5832 | return FAIL; |
| 5833 | } |
| 5834 | |
| 5835 | /* |
| 5836 | * compile "unlet var", "lock var" and "unlock var" |
| 5837 | * "arg" points to "var". |
| 5838 | */ |
| 5839 | static char_u * |
| 5840 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5841 | { |
| 5842 | char_u *p = arg; |
| 5843 | |
| 5844 | if (eap->cmdidx != CMD_unlet) |
| 5845 | { |
| 5846 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5847 | return NULL; |
| 5848 | } |
| 5849 | |
| 5850 | if (*p == '!') |
| 5851 | { |
| 5852 | p = skipwhite(p + 1); |
| 5853 | eap->forceit = TRUE; |
| 5854 | } |
| 5855 | |
| 5856 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5857 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5858 | } |
| 5859 | |
| 5860 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5861 | * Compile an :import command. |
| 5862 | */ |
| 5863 | static char_u * |
| 5864 | compile_import(char_u *arg, cctx_T *cctx) |
| 5865 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5866 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | /* |
| 5870 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5871 | */ |
| 5872 | static int |
| 5873 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5874 | { |
| 5875 | garray_T *instr = &cctx->ctx_instr; |
| 5876 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5877 | |
| 5878 | if (endlabel == NULL) |
| 5879 | return FAIL; |
| 5880 | endlabel->el_next = *el; |
| 5881 | *el = endlabel; |
| 5882 | endlabel->el_end_label = instr->ga_len; |
| 5883 | |
| 5884 | generate_JUMP(cctx, when, 0); |
| 5885 | return OK; |
| 5886 | } |
| 5887 | |
| 5888 | static void |
| 5889 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5890 | { |
| 5891 | garray_T *instr = &cctx->ctx_instr; |
| 5892 | |
| 5893 | while (*el != NULL) |
| 5894 | { |
| 5895 | endlabel_T *cur = (*el); |
| 5896 | isn_T *isn; |
| 5897 | |
| 5898 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5899 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5900 | *el = cur->el_next; |
| 5901 | vim_free(cur); |
| 5902 | } |
| 5903 | } |
| 5904 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5905 | static void |
| 5906 | compile_free_jump_to_end(endlabel_T **el) |
| 5907 | { |
| 5908 | while (*el != NULL) |
| 5909 | { |
| 5910 | endlabel_T *cur = (*el); |
| 5911 | |
| 5912 | *el = cur->el_next; |
| 5913 | vim_free(cur); |
| 5914 | } |
| 5915 | } |
| 5916 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5917 | /* |
| 5918 | * Create a new scope and set up the generic items. |
| 5919 | */ |
| 5920 | static scope_T * |
| 5921 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5922 | { |
| 5923 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5924 | |
| 5925 | if (scope == NULL) |
| 5926 | return NULL; |
| 5927 | scope->se_outer = cctx->ctx_scope; |
| 5928 | cctx->ctx_scope = scope; |
| 5929 | scope->se_type = type; |
| 5930 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5931 | return scope; |
| 5932 | } |
| 5933 | |
| 5934 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5935 | * Free the current scope and go back to the outer scope. |
| 5936 | */ |
| 5937 | static void |
| 5938 | drop_scope(cctx_T *cctx) |
| 5939 | { |
| 5940 | scope_T *scope = cctx->ctx_scope; |
| 5941 | |
| 5942 | if (scope == NULL) |
| 5943 | { |
| 5944 | iemsg("calling drop_scope() without a scope"); |
| 5945 | return; |
| 5946 | } |
| 5947 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5948 | switch (scope->se_type) |
| 5949 | { |
| 5950 | case IF_SCOPE: |
| 5951 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5952 | case FOR_SCOPE: |
| 5953 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5954 | case WHILE_SCOPE: |
| 5955 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5956 | case TRY_SCOPE: |
| 5957 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5958 | case NO_SCOPE: |
| 5959 | case BLOCK_SCOPE: |
| 5960 | break; |
| 5961 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5962 | vim_free(scope); |
| 5963 | } |
| 5964 | |
| 5965 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5966 | * compile "if expr" |
| 5967 | * |
| 5968 | * "if expr" Produces instructions: |
| 5969 | * EVAL expr Push result of "expr" |
| 5970 | * JUMP_IF_FALSE end |
| 5971 | * ... body ... |
| 5972 | * end: |
| 5973 | * |
| 5974 | * "if expr | else" Produces instructions: |
| 5975 | * EVAL expr Push result of "expr" |
| 5976 | * JUMP_IF_FALSE else |
| 5977 | * ... body ... |
| 5978 | * JUMP_ALWAYS end |
| 5979 | * else: |
| 5980 | * ... body ... |
| 5981 | * end: |
| 5982 | * |
| 5983 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5984 | * EVAL expr Push result of "expr" |
| 5985 | * JUMP_IF_FALSE elseif |
| 5986 | * ... body ... |
| 5987 | * JUMP_ALWAYS end |
| 5988 | * elseif: |
| 5989 | * EVAL expr Push result of "expr" |
| 5990 | * JUMP_IF_FALSE else |
| 5991 | * ... body ... |
| 5992 | * JUMP_ALWAYS end |
| 5993 | * else: |
| 5994 | * ... body ... |
| 5995 | * end: |
| 5996 | */ |
| 5997 | static char_u * |
| 5998 | compile_if(char_u *arg, cctx_T *cctx) |
| 5999 | { |
| 6000 | char_u *p = arg; |
| 6001 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6002 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6003 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6004 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6005 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6006 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6007 | CLEAR_FIELD(ppconst); |
| 6008 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6009 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6010 | clear_ppconst(&ppconst); |
| 6011 | return NULL; |
| 6012 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6013 | if (cctx->ctx_skip == SKIP_YES) |
| 6014 | clear_ppconst(&ppconst); |
| 6015 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6016 | { |
| 6017 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6018 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6019 | clear_ppconst(&ppconst); |
| 6020 | } |
| 6021 | else |
| 6022 | { |
| 6023 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6024 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6025 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6026 | return NULL; |
| 6027 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6028 | |
| 6029 | scope = new_scope(cctx, IF_SCOPE); |
| 6030 | if (scope == NULL) |
| 6031 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6032 | scope->se_skip_save = skip_save; |
| 6033 | // "is_had_return" will be reset if any block does not end in :return |
| 6034 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6035 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6036 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6037 | { |
| 6038 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6039 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6040 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6041 | } |
| 6042 | else |
| 6043 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6044 | |
| 6045 | return p; |
| 6046 | } |
| 6047 | |
| 6048 | static char_u * |
| 6049 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 6050 | { |
| 6051 | char_u *p = arg; |
| 6052 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6053 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6054 | isn_T *isn; |
| 6055 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6056 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6057 | |
| 6058 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6059 | { |
| 6060 | emsg(_(e_elseif_without_if)); |
| 6061 | return NULL; |
| 6062 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6063 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6064 | if (!cctx->ctx_had_return) |
| 6065 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6066 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6067 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6068 | { |
| 6069 | 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] | 6070 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6071 | return NULL; |
| 6072 | // previous "if" or "elseif" jumps here |
| 6073 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6074 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6075 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6076 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6077 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6078 | CLEAR_FIELD(ppconst); |
| 6079 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6080 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6081 | clear_ppconst(&ppconst); |
| 6082 | return NULL; |
| 6083 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6084 | if (scope->se_skip_save == SKIP_YES) |
| 6085 | clear_ppconst(&ppconst); |
| 6086 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6087 | { |
| 6088 | // The expression results in a constant. |
| 6089 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6090 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6091 | clear_ppconst(&ppconst); |
| 6092 | scope->se_u.se_if.is_if_label = -1; |
| 6093 | } |
| 6094 | else |
| 6095 | { |
| 6096 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6097 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6098 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6099 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6100 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6101 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6102 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6103 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6104 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6105 | |
| 6106 | return p; |
| 6107 | } |
| 6108 | |
| 6109 | static char_u * |
| 6110 | compile_else(char_u *arg, cctx_T *cctx) |
| 6111 | { |
| 6112 | char_u *p = arg; |
| 6113 | garray_T *instr = &cctx->ctx_instr; |
| 6114 | isn_T *isn; |
| 6115 | scope_T *scope = cctx->ctx_scope; |
| 6116 | |
| 6117 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6118 | { |
| 6119 | emsg(_(e_else_without_if)); |
| 6120 | return NULL; |
| 6121 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6122 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6123 | if (!cctx->ctx_had_return) |
| 6124 | scope->se_u.se_if.is_had_return = FALSE; |
| 6125 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6126 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6127 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6128 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6129 | // 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] | 6130 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6131 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6132 | if (!cctx->ctx_had_return |
| 6133 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6134 | JUMP_ALWAYS, cctx) == FAIL) |
| 6135 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6136 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6137 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6138 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6139 | { |
| 6140 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6141 | { |
| 6142 | // previous "if" or "elseif" jumps here |
| 6143 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6144 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6145 | scope->se_u.se_if.is_if_label = -1; |
| 6146 | } |
| 6147 | } |
| 6148 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6149 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6150 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6151 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6152 | |
| 6153 | return p; |
| 6154 | } |
| 6155 | |
| 6156 | static char_u * |
| 6157 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6158 | { |
| 6159 | scope_T *scope = cctx->ctx_scope; |
| 6160 | ifscope_T *ifscope; |
| 6161 | garray_T *instr = &cctx->ctx_instr; |
| 6162 | isn_T *isn; |
| 6163 | |
| 6164 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6165 | { |
| 6166 | emsg(_(e_endif_without_if)); |
| 6167 | return NULL; |
| 6168 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6169 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6170 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6171 | if (!cctx->ctx_had_return) |
| 6172 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6173 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6174 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6175 | { |
| 6176 | // previous "if" or "elseif" jumps here |
| 6177 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6178 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6179 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6180 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6181 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6182 | cctx->ctx_skip = scope->se_skip_save; |
| 6183 | |
| 6184 | // If all the blocks end in :return and there is an :else then the |
| 6185 | // had_return flag is set. |
| 6186 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6187 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6188 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6189 | return arg; |
| 6190 | } |
| 6191 | |
| 6192 | /* |
| 6193 | * compile "for var in expr" |
| 6194 | * |
| 6195 | * Produces instructions: |
| 6196 | * PUSHNR -1 |
| 6197 | * STORE loop-idx Set index to -1 |
| 6198 | * EVAL expr Push result of "expr" |
| 6199 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6200 | * - if beyond end, jump to "end" |
| 6201 | * - otherwise get item from list and push it |
| 6202 | * STORE var Store item in "var" |
| 6203 | * ... body ... |
| 6204 | * JUMP top Jump back to repeat |
| 6205 | * end: DROP Drop the result of "expr" |
| 6206 | * |
| 6207 | */ |
| 6208 | static char_u * |
| 6209 | compile_for(char_u *arg, cctx_T *cctx) |
| 6210 | { |
| 6211 | char_u *p; |
| 6212 | size_t varlen; |
| 6213 | garray_T *instr = &cctx->ctx_instr; |
| 6214 | garray_T *stack = &cctx->ctx_type_stack; |
| 6215 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6216 | lvar_T *loop_lvar; // loop iteration variable |
| 6217 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6218 | type_T *vartype; |
| 6219 | |
| 6220 | // TODO: list of variables: "for [key, value] in dict" |
| 6221 | // parse "var" |
| 6222 | for (p = arg; eval_isnamec1(*p); ++p) |
| 6223 | ; |
| 6224 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6225 | var_lvar = lookup_local(arg, varlen, cctx); |
| 6226 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6227 | { |
| 6228 | semsg(_("E1023: variable already defined: %s"), arg); |
| 6229 | return NULL; |
| 6230 | } |
| 6231 | |
| 6232 | // consume "in" |
| 6233 | p = skipwhite(p); |
| 6234 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 6235 | { |
| 6236 | emsg(_(e_missing_in)); |
| 6237 | return NULL; |
| 6238 | } |
| 6239 | p = skipwhite(p + 2); |
| 6240 | |
| 6241 | |
| 6242 | scope = new_scope(cctx, FOR_SCOPE); |
| 6243 | if (scope == NULL) |
| 6244 | return NULL; |
| 6245 | |
| 6246 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6247 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6248 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6249 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6250 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6251 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6252 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6253 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6254 | |
| 6255 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6256 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 6257 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6258 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6259 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6260 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6261 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6262 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6263 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6264 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6265 | |
| 6266 | // compile "expr", it remains on the stack until "endfor" |
| 6267 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6268 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6269 | { |
| 6270 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6271 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6272 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6273 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6274 | // Now that we know the type of "var", check that it is a list, now or at |
| 6275 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6276 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 6277 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6278 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6279 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6280 | return NULL; |
| 6281 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6282 | 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] | 6283 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6284 | |
| 6285 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6286 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6287 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6288 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 6289 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6290 | |
| 6291 | return arg; |
| 6292 | } |
| 6293 | |
| 6294 | /* |
| 6295 | * compile "endfor" |
| 6296 | */ |
| 6297 | static char_u * |
| 6298 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 6299 | { |
| 6300 | garray_T *instr = &cctx->ctx_instr; |
| 6301 | scope_T *scope = cctx->ctx_scope; |
| 6302 | forscope_T *forscope; |
| 6303 | isn_T *isn; |
| 6304 | |
| 6305 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 6306 | { |
| 6307 | emsg(_(e_for)); |
| 6308 | return NULL; |
| 6309 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6310 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6311 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6312 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6313 | |
| 6314 | // At end of ":for" scope jump back to the FOR instruction. |
| 6315 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 6316 | |
| 6317 | // Fill in the "end" label in the FOR statement so it can jump here |
| 6318 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 6319 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 6320 | |
| 6321 | // Fill in the "end" label any BREAK statements |
| 6322 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 6323 | |
| 6324 | // Below the ":for" scope drop the "expr" list from the stack. |
| 6325 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 6326 | return NULL; |
| 6327 | |
| 6328 | vim_free(scope); |
| 6329 | |
| 6330 | return arg; |
| 6331 | } |
| 6332 | |
| 6333 | /* |
| 6334 | * compile "while expr" |
| 6335 | * |
| 6336 | * Produces instructions: |
| 6337 | * top: EVAL expr Push result of "expr" |
| 6338 | * JUMP_IF_FALSE end jump if false |
| 6339 | * ... body ... |
| 6340 | * JUMP top Jump back to repeat |
| 6341 | * end: |
| 6342 | * |
| 6343 | */ |
| 6344 | static char_u * |
| 6345 | compile_while(char_u *arg, cctx_T *cctx) |
| 6346 | { |
| 6347 | char_u *p = arg; |
| 6348 | garray_T *instr = &cctx->ctx_instr; |
| 6349 | scope_T *scope; |
| 6350 | |
| 6351 | scope = new_scope(cctx, WHILE_SCOPE); |
| 6352 | if (scope == NULL) |
| 6353 | return NULL; |
| 6354 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6355 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6356 | |
| 6357 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6358 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6359 | return NULL; |
| 6360 | |
| 6361 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6362 | 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] | 6363 | JUMP_IF_FALSE, cctx) == FAIL) |
| 6364 | return FAIL; |
| 6365 | |
| 6366 | return p; |
| 6367 | } |
| 6368 | |
| 6369 | /* |
| 6370 | * compile "endwhile" |
| 6371 | */ |
| 6372 | static char_u * |
| 6373 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 6374 | { |
| 6375 | scope_T *scope = cctx->ctx_scope; |
| 6376 | |
| 6377 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 6378 | { |
| 6379 | emsg(_(e_while)); |
| 6380 | return NULL; |
| 6381 | } |
| 6382 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6383 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6384 | |
| 6385 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6386 | 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] | 6387 | |
| 6388 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 6389 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6390 | 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] | 6391 | |
| 6392 | vim_free(scope); |
| 6393 | |
| 6394 | return arg; |
| 6395 | } |
| 6396 | |
| 6397 | /* |
| 6398 | * compile "continue" |
| 6399 | */ |
| 6400 | static char_u * |
| 6401 | compile_continue(char_u *arg, cctx_T *cctx) |
| 6402 | { |
| 6403 | scope_T *scope = cctx->ctx_scope; |
| 6404 | |
| 6405 | for (;;) |
| 6406 | { |
| 6407 | if (scope == NULL) |
| 6408 | { |
| 6409 | emsg(_(e_continue)); |
| 6410 | return NULL; |
| 6411 | } |
| 6412 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6413 | break; |
| 6414 | scope = scope->se_outer; |
| 6415 | } |
| 6416 | |
| 6417 | // Jump back to the FOR or WHILE instruction. |
| 6418 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6419 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 6420 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6421 | return arg; |
| 6422 | } |
| 6423 | |
| 6424 | /* |
| 6425 | * compile "break" |
| 6426 | */ |
| 6427 | static char_u * |
| 6428 | compile_break(char_u *arg, cctx_T *cctx) |
| 6429 | { |
| 6430 | scope_T *scope = cctx->ctx_scope; |
| 6431 | endlabel_T **el; |
| 6432 | |
| 6433 | for (;;) |
| 6434 | { |
| 6435 | if (scope == NULL) |
| 6436 | { |
| 6437 | emsg(_(e_break)); |
| 6438 | return NULL; |
| 6439 | } |
| 6440 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6441 | break; |
| 6442 | scope = scope->se_outer; |
| 6443 | } |
| 6444 | |
| 6445 | // Jump to the end of the FOR or WHILE loop. |
| 6446 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6447 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6448 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6449 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6450 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 6451 | return FAIL; |
| 6452 | |
| 6453 | return arg; |
| 6454 | } |
| 6455 | |
| 6456 | /* |
| 6457 | * compile "{" start of block |
| 6458 | */ |
| 6459 | static char_u * |
| 6460 | compile_block(char_u *arg, cctx_T *cctx) |
| 6461 | { |
| 6462 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6463 | return NULL; |
| 6464 | return skipwhite(arg + 1); |
| 6465 | } |
| 6466 | |
| 6467 | /* |
| 6468 | * compile end of block: drop one scope |
| 6469 | */ |
| 6470 | static void |
| 6471 | compile_endblock(cctx_T *cctx) |
| 6472 | { |
| 6473 | scope_T *scope = cctx->ctx_scope; |
| 6474 | |
| 6475 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6476 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6477 | vim_free(scope); |
| 6478 | } |
| 6479 | |
| 6480 | /* |
| 6481 | * compile "try" |
| 6482 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6483 | * finally. |
| 6484 | * Creates another scope for the "try" block itself. |
| 6485 | * TRY instruction sets up exception handling at runtime. |
| 6486 | * |
| 6487 | * "try" |
| 6488 | * TRY -> catch1, -> finally push trystack entry |
| 6489 | * ... try block |
| 6490 | * "throw {exception}" |
| 6491 | * EVAL {exception} |
| 6492 | * THROW create exception |
| 6493 | * ... try block |
| 6494 | * " catch {expr}" |
| 6495 | * JUMP -> finally |
| 6496 | * catch1: PUSH exeception |
| 6497 | * EVAL {expr} |
| 6498 | * MATCH |
| 6499 | * JUMP nomatch -> catch2 |
| 6500 | * CATCH remove exception |
| 6501 | * ... catch block |
| 6502 | * " catch" |
| 6503 | * JUMP -> finally |
| 6504 | * catch2: CATCH remove exception |
| 6505 | * ... catch block |
| 6506 | * " finally" |
| 6507 | * finally: |
| 6508 | * ... finally block |
| 6509 | * " endtry" |
| 6510 | * ENDTRY pop trystack entry, may rethrow |
| 6511 | */ |
| 6512 | static char_u * |
| 6513 | compile_try(char_u *arg, cctx_T *cctx) |
| 6514 | { |
| 6515 | garray_T *instr = &cctx->ctx_instr; |
| 6516 | scope_T *try_scope; |
| 6517 | scope_T *scope; |
| 6518 | |
| 6519 | // scope that holds the jumps that go to catch/finally/endtry |
| 6520 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6521 | if (try_scope == NULL) |
| 6522 | return NULL; |
| 6523 | |
| 6524 | // "catch" is set when the first ":catch" is found. |
| 6525 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6526 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6527 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6528 | return NULL; |
| 6529 | |
| 6530 | // scope for the try block itself |
| 6531 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6532 | if (scope == NULL) |
| 6533 | return NULL; |
| 6534 | |
| 6535 | return arg; |
| 6536 | } |
| 6537 | |
| 6538 | /* |
| 6539 | * compile "catch {expr}" |
| 6540 | */ |
| 6541 | static char_u * |
| 6542 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6543 | { |
| 6544 | scope_T *scope = cctx->ctx_scope; |
| 6545 | garray_T *instr = &cctx->ctx_instr; |
| 6546 | char_u *p; |
| 6547 | isn_T *isn; |
| 6548 | |
| 6549 | // end block scope from :try or :catch |
| 6550 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6551 | compile_endblock(cctx); |
| 6552 | scope = cctx->ctx_scope; |
| 6553 | |
| 6554 | // Error if not in a :try scope |
| 6555 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6556 | { |
| 6557 | emsg(_(e_catch)); |
| 6558 | return NULL; |
| 6559 | } |
| 6560 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6561 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6562 | { |
| 6563 | emsg(_("E1033: catch unreachable after catch-all")); |
| 6564 | return NULL; |
| 6565 | } |
| 6566 | |
| 6567 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6568 | 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] | 6569 | JUMP_ALWAYS, cctx) == FAIL) |
| 6570 | return NULL; |
| 6571 | |
| 6572 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6573 | 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] | 6574 | if (isn->isn_arg.try.try_catch == 0) |
| 6575 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6576 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6577 | { |
| 6578 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6579 | 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] | 6580 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6581 | } |
| 6582 | |
| 6583 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6584 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6585 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6586 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6587 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6588 | } |
| 6589 | else |
| 6590 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6591 | char_u *end; |
| 6592 | char_u *pat; |
| 6593 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6594 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6595 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6596 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6597 | // Push v:exception, push {expr} and MATCH |
| 6598 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6599 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6600 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6601 | if (*end != *p) |
| 6602 | { |
| 6603 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 6604 | vim_free(tofree); |
| 6605 | return FAIL; |
| 6606 | } |
| 6607 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6608 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6609 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6610 | len = (int)(end - tofree); |
| 6611 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6612 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6613 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6614 | if (pat == NULL) |
| 6615 | return FAIL; |
| 6616 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6617 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6618 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6619 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6620 | return NULL; |
| 6621 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6622 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6623 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6624 | return NULL; |
| 6625 | } |
| 6626 | |
| 6627 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6628 | return NULL; |
| 6629 | |
| 6630 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6631 | return NULL; |
| 6632 | return p; |
| 6633 | } |
| 6634 | |
| 6635 | static char_u * |
| 6636 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6637 | { |
| 6638 | scope_T *scope = cctx->ctx_scope; |
| 6639 | garray_T *instr = &cctx->ctx_instr; |
| 6640 | isn_T *isn; |
| 6641 | |
| 6642 | // end block scope from :try or :catch |
| 6643 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6644 | compile_endblock(cctx); |
| 6645 | scope = cctx->ctx_scope; |
| 6646 | |
| 6647 | // Error if not in a :try scope |
| 6648 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6649 | { |
| 6650 | emsg(_(e_finally)); |
| 6651 | return NULL; |
| 6652 | } |
| 6653 | |
| 6654 | // End :catch or :finally scope: set value in ISN_TRY instruction |
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_finally != 0) |
| 6657 | { |
| 6658 | emsg(_(e_finally_dup)); |
| 6659 | return NULL; |
| 6660 | } |
| 6661 | |
| 6662 | // 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] | 6663 | 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] | 6664 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6665 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6666 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6667 | { |
| 6668 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6669 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6670 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6671 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6672 | } |
| 6673 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6674 | // TODO: set index in ts_finally_label jumps |
| 6675 | |
| 6676 | return arg; |
| 6677 | } |
| 6678 | |
| 6679 | static char_u * |
| 6680 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6681 | { |
| 6682 | scope_T *scope = cctx->ctx_scope; |
| 6683 | garray_T *instr = &cctx->ctx_instr; |
| 6684 | isn_T *isn; |
| 6685 | |
| 6686 | // end block scope from :catch or :finally |
| 6687 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6688 | compile_endblock(cctx); |
| 6689 | scope = cctx->ctx_scope; |
| 6690 | |
| 6691 | // Error if not in a :try scope |
| 6692 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6693 | { |
| 6694 | if (scope == NULL) |
| 6695 | emsg(_(e_no_endtry)); |
| 6696 | else if (scope->se_type == WHILE_SCOPE) |
| 6697 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6698 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6699 | emsg(_(e_endfor)); |
| 6700 | else |
| 6701 | emsg(_(e_endif)); |
| 6702 | return NULL; |
| 6703 | } |
| 6704 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6705 | 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] | 6706 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6707 | { |
| 6708 | emsg(_("E1032: missing :catch or :finally")); |
| 6709 | return NULL; |
| 6710 | } |
| 6711 | |
| 6712 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6713 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6714 | 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] | 6715 | |
| 6716 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6717 | if (isn->isn_arg.try.try_catch == 0) |
| 6718 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6719 | if (isn->isn_arg.try.try_finally == 0) |
| 6720 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6721 | |
| 6722 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6723 | { |
| 6724 | // Last catch without match jumps here |
| 6725 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6726 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6727 | } |
| 6728 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6729 | compile_endblock(cctx); |
| 6730 | |
| 6731 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6732 | return NULL; |
| 6733 | return arg; |
| 6734 | } |
| 6735 | |
| 6736 | /* |
| 6737 | * compile "throw {expr}" |
| 6738 | */ |
| 6739 | static char_u * |
| 6740 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6741 | { |
| 6742 | char_u *p = skipwhite(arg); |
| 6743 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6744 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6745 | return NULL; |
| 6746 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6747 | return NULL; |
| 6748 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6749 | return NULL; |
| 6750 | |
| 6751 | return p; |
| 6752 | } |
| 6753 | |
| 6754 | /* |
| 6755 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6756 | * compile "echomsg expr" |
| 6757 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6758 | * compile "execute expr" |
| 6759 | */ |
| 6760 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6761 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6762 | { |
| 6763 | char_u *p = arg; |
| 6764 | int count = 0; |
| 6765 | |
| 6766 | for (;;) |
| 6767 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6768 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6769 | return NULL; |
| 6770 | ++count; |
| 6771 | p = skipwhite(p); |
| 6772 | if (ends_excmd(*p)) |
| 6773 | break; |
| 6774 | } |
| 6775 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6776 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6777 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6778 | else if (cmdidx == CMD_execute) |
| 6779 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6780 | else if (cmdidx == CMD_echomsg) |
| 6781 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6782 | else |
| 6783 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6784 | return p; |
| 6785 | } |
| 6786 | |
| 6787 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6788 | * A command that is not compiled, execute with legacy code. |
| 6789 | */ |
| 6790 | static char_u * |
| 6791 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6792 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6793 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6794 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6795 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6796 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6797 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6798 | goto theend; |
| 6799 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6800 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6801 | { |
| 6802 | long argt = excmd_get_argt(eap->cmdidx); |
| 6803 | int usefilter = FALSE; |
| 6804 | |
| 6805 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6806 | |
| 6807 | // If the command can be followed by a bar, find the bar and truncate |
| 6808 | // it, so that the following command can be compiled. |
| 6809 | // The '|' is overwritten with a NUL, it is put back below. |
| 6810 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6811 | && *eap->arg == '!') |
| 6812 | // :w !filter or :r !filter or :r! filter |
| 6813 | usefilter = TRUE; |
| 6814 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6815 | { |
| 6816 | separate_nextcmd(eap); |
| 6817 | if (eap->nextcmd != NULL) |
| 6818 | nextcmd = eap->nextcmd; |
| 6819 | } |
| 6820 | } |
| 6821 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6822 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6823 | { |
| 6824 | // expand filename in "syntax include [@group] filename" |
| 6825 | has_expr = TRUE; |
| 6826 | eap->arg = skipwhite(eap->arg + 7); |
| 6827 | if (*eap->arg == '@') |
| 6828 | eap->arg = skiptowhite(eap->arg); |
| 6829 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6830 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6831 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6832 | { |
| 6833 | int count = 0; |
| 6834 | char_u *start = skipwhite(line); |
| 6835 | |
| 6836 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6837 | // PUSHS ":cmd xxx" |
| 6838 | // eval expr1 |
| 6839 | // PUSHS "yyy" |
| 6840 | // eval expr2 |
| 6841 | // PUSHS "zzz" |
| 6842 | // EXECCONCAT 5 |
| 6843 | for (;;) |
| 6844 | { |
| 6845 | if (p > start) |
| 6846 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6847 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6848 | ++count; |
| 6849 | } |
| 6850 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6851 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6852 | return NULL; |
| 6853 | may_generate_2STRING(-1, cctx); |
| 6854 | ++count; |
| 6855 | p = skipwhite(p); |
| 6856 | if (*p != '`') |
| 6857 | { |
| 6858 | emsg(_("E1083: missing backtick")); |
| 6859 | return NULL; |
| 6860 | } |
| 6861 | start = p + 1; |
| 6862 | |
| 6863 | p = (char_u *)strstr((char *)start, "`="); |
| 6864 | if (p == NULL) |
| 6865 | { |
| 6866 | if (*skipwhite(start) != NUL) |
| 6867 | { |
| 6868 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6869 | ++count; |
| 6870 | } |
| 6871 | break; |
| 6872 | } |
| 6873 | } |
| 6874 | generate_EXECCONCAT(cctx, count); |
| 6875 | } |
| 6876 | else |
| 6877 | generate_EXEC(cctx, line); |
| 6878 | |
| 6879 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6880 | if (*nextcmd != NUL) |
| 6881 | { |
| 6882 | // the parser expects a pointer to the bar, put it back |
| 6883 | --nextcmd; |
| 6884 | *nextcmd = '|'; |
| 6885 | } |
| 6886 | |
| 6887 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6888 | } |
| 6889 | |
| 6890 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6891 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6892 | * 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] | 6893 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6894 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6895 | add_def_function(ufunc_T *ufunc) |
| 6896 | { |
| 6897 | dfunc_T *dfunc; |
| 6898 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6899 | if (def_functions.ga_len == 0) |
| 6900 | { |
| 6901 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6902 | // wasn't set. |
| 6903 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6904 | return FAIL; |
| 6905 | ++def_functions.ga_len; |
| 6906 | } |
| 6907 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6908 | // Add the function to "def_functions". |
| 6909 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6910 | return FAIL; |
| 6911 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6912 | CLEAR_POINTER(dfunc); |
| 6913 | dfunc->df_idx = def_functions.ga_len; |
| 6914 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6915 | dfunc->df_ufunc = ufunc; |
| 6916 | ++def_functions.ga_len; |
| 6917 | return OK; |
| 6918 | } |
| 6919 | |
| 6920 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6921 | * After ex_function() has collected all the function lines: parse and compile |
| 6922 | * the lines into instructions. |
| 6923 | * Adds the function to "def_functions". |
| 6924 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6925 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6926 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6927 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6928 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6929 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6930 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6931 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6932 | 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] | 6933 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6934 | char_u *line = NULL; |
| 6935 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6936 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6937 | cctx_T cctx; |
| 6938 | garray_T *instr; |
| 6939 | int called_emsg_before = called_emsg; |
| 6940 | int ret = FAIL; |
| 6941 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6942 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6943 | int emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6944 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6945 | // When using a function that was compiled before: Free old instructions. |
| 6946 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6947 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6948 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6949 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6950 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6951 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6952 | } |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6953 | else if (add_def_function(ufunc) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6954 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6955 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6956 | ufunc->uf_def_status = UF_COMPILING; |
| 6957 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6958 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6959 | cctx.ctx_ufunc = ufunc; |
| 6960 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6961 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6962 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6963 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6964 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6965 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6966 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6967 | instr = &cctx.ctx_instr; |
| 6968 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6969 | // Set the context to the function, it may be compiled when called from |
| 6970 | // another script. Set the script version to the most modern one. |
| 6971 | // The line number will be set in next_line_from_context(). |
| 6972 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6973 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6974 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6975 | // Make sure error messages are OK. |
| 6976 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6977 | if (do_estack_push) |
| 6978 | estack_push_ufunc(ufunc, 1); |
| 6979 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6980 | if (ufunc->uf_def_args.ga_len > 0) |
| 6981 | { |
| 6982 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6983 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6984 | int i; |
| 6985 | char_u *arg; |
| 6986 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 6987 | |
| 6988 | // Produce instructions for the default values of optional arguments. |
| 6989 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6990 | // where to start when the function is called, depending on the number |
| 6991 | // of arguments. |
| 6992 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6993 | if (ufunc->uf_def_arg_idx == NULL) |
| 6994 | goto erret; |
| 6995 | for (i = 0; i < count; ++i) |
| 6996 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6997 | garray_T *stack = &cctx.ctx_type_stack; |
| 6998 | type_T *val_type; |
| 6999 | int arg_idx = first_def_arg + i; |
| 7000 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7001 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 7002 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7003 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7004 | goto erret; |
| 7005 | |
| 7006 | // If no type specified use the type of the default value. |
| 7007 | // Otherwise check that the default value type matches the |
| 7008 | // specified type. |
| 7009 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 7010 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
| 7011 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 7012 | 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] | 7013 | == FAIL) |
| 7014 | { |
| 7015 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 7016 | arg_idx + 1); |
| 7017 | goto erret; |
| 7018 | } |
| 7019 | |
| 7020 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7021 | goto erret; |
| 7022 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7023 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
| 7024 | } |
| 7025 | |
| 7026 | /* |
| 7027 | * Loop over all the lines of the function and generate instructions. |
| 7028 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7029 | for (;;) |
| 7030 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7031 | exarg_T ea; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7032 | int starts_with_colon = FALSE; |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7033 | char_u *cmd; |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7034 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7035 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7036 | // Bail out on the first error to avoid a flood of errors and report |
| 7037 | // the right line number when inside try/catch. |
| 7038 | if (emsg_before != called_emsg) |
| 7039 | goto erret; |
| 7040 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7041 | if (line != NULL && *line == '|') |
| 7042 | // the line continues after a '|' |
| 7043 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 7044 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7045 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 7046 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7047 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7048 | semsg(_("E488: Trailing characters: %s"), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7049 | goto erret; |
| 7050 | } |
| 7051 | else |
| 7052 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 7053 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 7054 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7055 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7056 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7057 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7058 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7059 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7060 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7061 | ea.cmdlinep = &line; |
| 7062 | ea.cmd = skipwhite(line); |
| 7063 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7064 | // Some things can be recognized by the first character. |
| 7065 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7066 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7067 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7068 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7069 | if (ea.cmd[1] != '{') |
| 7070 | { |
| 7071 | line = (char_u *)""; |
| 7072 | continue; |
| 7073 | } |
| 7074 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7075 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7076 | case '}': |
| 7077 | { |
| 7078 | // "}" ends a block scope |
| 7079 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7080 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7081 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7082 | if (stype == BLOCK_SCOPE) |
| 7083 | { |
| 7084 | compile_endblock(&cctx); |
| 7085 | line = ea.cmd; |
| 7086 | } |
| 7087 | else |
| 7088 | { |
| 7089 | emsg(_("E1025: using } outside of a block scope")); |
| 7090 | goto erret; |
| 7091 | } |
| 7092 | if (line != NULL) |
| 7093 | line = skipwhite(ea.cmd + 1); |
| 7094 | continue; |
| 7095 | } |
| 7096 | |
| 7097 | case '{': |
| 7098 | // "{" starts a block scope |
| 7099 | // "{'a': 1}->func() is something else |
| 7100 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7101 | { |
| 7102 | line = compile_block(ea.cmd, &cctx); |
| 7103 | continue; |
| 7104 | } |
| 7105 | break; |
| 7106 | |
| 7107 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7108 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7109 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7110 | } |
| 7111 | |
| 7112 | /* |
| 7113 | * COMMAND MODIFIERS |
| 7114 | */ |
| 7115 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 7116 | { |
| 7117 | if (errormsg != NULL) |
| 7118 | goto erret; |
| 7119 | // empty line or comment |
| 7120 | line = (char_u *)""; |
| 7121 | continue; |
| 7122 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7123 | // TODO: use modifiers in the command |
| 7124 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | b074e8b | 2020-07-11 13:40:45 +0200 | [diff] [blame] | 7125 | CLEAR_FIELD(cmdmod); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7126 | |
| 7127 | // Skip ":call" to get to the function name. |
| 7128 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 7129 | ea.cmd = skipwhite(ea.cmd); |
| 7130 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7131 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7132 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7133 | char_u *pskip; |
| 7134 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7135 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7136 | // find what follows. |
| 7137 | // Skip over "var.member", "var[idx]" and the like. |
| 7138 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 7139 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7140 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7141 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7142 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7143 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7144 | char_u *var_end; |
| 7145 | int oplen; |
| 7146 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7147 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7148 | var_end = find_name_end(pskip, NULL, NULL, |
| 7149 | FNE_CHECK_START | FNE_INCL_BR); |
| 7150 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7151 | if (oplen > 0) |
| 7152 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7153 | size_t len = p - ea.cmd; |
| 7154 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7155 | // Recognize an assignment if we recognize the variable |
| 7156 | // name: |
| 7157 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 7158 | // "local = expr" where "local" is a local var. |
| 7159 | // "script = expr" where "script" is a script-local var. |
| 7160 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7161 | // "&opt = expr" |
| 7162 | // "$ENV = expr" |
| 7163 | // "@r = expr" |
| 7164 | if (*ea.cmd == '&' |
| 7165 | || *ea.cmd == '$' |
| 7166 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7167 | || ((len) > 2 && ea.cmd[1] == ':') |
| 7168 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 7169 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 7170 | NULL, &cctx) == OK |
| 7171 | || lookup_script(ea.cmd, len) == OK |
| 7172 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7173 | { |
| 7174 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7175 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7176 | goto erret; |
| 7177 | continue; |
| 7178 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7179 | } |
| 7180 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7181 | |
| 7182 | if (*ea.cmd == '[') |
| 7183 | { |
| 7184 | // [var, var] = expr |
| 7185 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 7186 | if (line == NULL) |
| 7187 | goto erret; |
| 7188 | if (line != ea.cmd) |
| 7189 | continue; |
| 7190 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7191 | } |
| 7192 | |
| 7193 | /* |
| 7194 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7195 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7196 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7197 | cmd = ea.cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7198 | if (*cmd != '\'') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7199 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7200 | ea.cmd = skip_range(ea.cmd, NULL); |
| 7201 | if (ea.cmd > cmd && !starts_with_colon) |
| 7202 | { |
| 7203 | emsg(_(e_colon_required)); |
| 7204 | goto erret; |
| 7205 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7206 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7207 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7208 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7209 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7210 | |
| 7211 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 7212 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7213 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7214 | { |
| 7215 | line += STRLEN(line); |
| 7216 | continue; |
| 7217 | } |
| 7218 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7219 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7220 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7221 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7222 | // CMD_let cannot happen, compile_assignment() above is used |
| 7223 | iemsg("Command from find_ex_command() not handled"); |
| 7224 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7225 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7226 | } |
| 7227 | |
| 7228 | p = skipwhite(p); |
| 7229 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7230 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 7231 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7232 | && ea.cmdidx != CMD_elseif |
| 7233 | && ea.cmdidx != CMD_else |
| 7234 | && ea.cmdidx != CMD_endif) |
| 7235 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7236 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7237 | continue; |
| 7238 | } |
| 7239 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7240 | if (ea.cmdidx != CMD_elseif |
| 7241 | && ea.cmdidx != CMD_else |
| 7242 | && ea.cmdidx != CMD_endif |
| 7243 | && ea.cmdidx != CMD_endfor |
| 7244 | && ea.cmdidx != CMD_endwhile |
| 7245 | && ea.cmdidx != CMD_catch |
| 7246 | && ea.cmdidx != CMD_finally |
| 7247 | && ea.cmdidx != CMD_endtry) |
| 7248 | { |
| 7249 | if (cctx.ctx_had_return) |
| 7250 | { |
| 7251 | emsg(_("E1095: Unreachable code after :return")); |
| 7252 | goto erret; |
| 7253 | } |
| 7254 | } |
| 7255 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7256 | switch (ea.cmdidx) |
| 7257 | { |
| 7258 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7259 | ea.arg = p; |
| 7260 | line = compile_nested_function(&ea, &cctx); |
| 7261 | break; |
| 7262 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7263 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7264 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7265 | goto erret; |
| 7266 | |
| 7267 | case CMD_return: |
| 7268 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7269 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7270 | break; |
| 7271 | |
| 7272 | case CMD_let: |
| 7273 | case CMD_const: |
| 7274 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7275 | if (line == p) |
| 7276 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7277 | break; |
| 7278 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7279 | case CMD_unlet: |
| 7280 | case CMD_unlockvar: |
| 7281 | case CMD_lockvar: |
| 7282 | line = compile_unletlock(p, &ea, &cctx); |
| 7283 | break; |
| 7284 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7285 | case CMD_import: |
| 7286 | line = compile_import(p, &cctx); |
| 7287 | break; |
| 7288 | |
| 7289 | case CMD_if: |
| 7290 | line = compile_if(p, &cctx); |
| 7291 | break; |
| 7292 | case CMD_elseif: |
| 7293 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7294 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7295 | break; |
| 7296 | case CMD_else: |
| 7297 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7298 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7299 | break; |
| 7300 | case CMD_endif: |
| 7301 | line = compile_endif(p, &cctx); |
| 7302 | break; |
| 7303 | |
| 7304 | case CMD_while: |
| 7305 | line = compile_while(p, &cctx); |
| 7306 | break; |
| 7307 | case CMD_endwhile: |
| 7308 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7309 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7310 | break; |
| 7311 | |
| 7312 | case CMD_for: |
| 7313 | line = compile_for(p, &cctx); |
| 7314 | break; |
| 7315 | case CMD_endfor: |
| 7316 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7317 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7318 | break; |
| 7319 | case CMD_continue: |
| 7320 | line = compile_continue(p, &cctx); |
| 7321 | break; |
| 7322 | case CMD_break: |
| 7323 | line = compile_break(p, &cctx); |
| 7324 | break; |
| 7325 | |
| 7326 | case CMD_try: |
| 7327 | line = compile_try(p, &cctx); |
| 7328 | break; |
| 7329 | case CMD_catch: |
| 7330 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7331 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7332 | break; |
| 7333 | case CMD_finally: |
| 7334 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7335 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7336 | break; |
| 7337 | case CMD_endtry: |
| 7338 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7339 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7340 | break; |
| 7341 | case CMD_throw: |
| 7342 | line = compile_throw(p, &cctx); |
| 7343 | break; |
| 7344 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7345 | case CMD_eval: |
| 7346 | if (compile_expr0(&p, &cctx) == FAIL) |
| 7347 | goto erret; |
| 7348 | |
| 7349 | // drop the return value |
| 7350 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 7351 | |
| 7352 | line = skipwhite(p); |
| 7353 | break; |
| 7354 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7355 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7356 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7357 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7358 | case CMD_echomsg: |
| 7359 | case CMD_echoerr: |
| 7360 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7361 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7362 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7363 | // TODO: other commands with an expression argument |
| 7364 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7365 | case CMD_SIZE: |
| 7366 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 7367 | goto erret; |
| 7368 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7369 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7370 | // Not recognized, execute with do_cmdline_cmd(). |
| 7371 | ea.arg = p; |
| 7372 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7373 | break; |
| 7374 | } |
| 7375 | if (line == NULL) |
| 7376 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7377 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7378 | |
| 7379 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7380 | { |
| 7381 | iemsg("Type stack underflow"); |
| 7382 | goto erret; |
| 7383 | } |
| 7384 | } |
| 7385 | |
| 7386 | if (cctx.ctx_scope != NULL) |
| 7387 | { |
| 7388 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7389 | emsg(_(e_endif)); |
| 7390 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7391 | emsg(_(e_endwhile)); |
| 7392 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7393 | emsg(_(e_endfor)); |
| 7394 | else |
| 7395 | emsg(_("E1026: Missing }")); |
| 7396 | goto erret; |
| 7397 | } |
| 7398 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7399 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7400 | { |
| 7401 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7402 | { |
| 7403 | emsg(_("E1027: Missing return statement")); |
| 7404 | goto erret; |
| 7405 | } |
| 7406 | |
| 7407 | // Return zero if there is no return at the end. |
| 7408 | generate_PUSHNR(&cctx, 0); |
| 7409 | generate_instr(&cctx, ISN_RETURN); |
| 7410 | } |
| 7411 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7412 | { |
| 7413 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7414 | + ufunc->uf_dfunc_idx; |
| 7415 | dfunc->df_deleted = FALSE; |
| 7416 | dfunc->df_instr = instr->ga_data; |
| 7417 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7418 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 7419 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7420 | if (cctx.ctx_outer_used) |
| 7421 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7422 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7423 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7424 | |
| 7425 | ret = OK; |
| 7426 | |
| 7427 | erret: |
| 7428 | if (ret == FAIL) |
| 7429 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7430 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7431 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7432 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7433 | |
| 7434 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7435 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7436 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7437 | |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7438 | // if using the last entry in the table we might as well remove it |
| 7439 | if (!dfunc->df_deleted |
| 7440 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7441 | --def_functions.ga_len; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7442 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7443 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7444 | while (cctx.ctx_scope != NULL) |
| 7445 | drop_scope(&cctx); |
| 7446 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7447 | // Don't execute this function body. |
| 7448 | ga_clear_strings(&ufunc->uf_lines); |
| 7449 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7450 | if (errormsg != NULL) |
| 7451 | emsg(errormsg); |
| 7452 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 7453 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7454 | } |
| 7455 | |
| 7456 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7457 | if (do_estack_push) |
| 7458 | estack_pop(); |
| 7459 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7460 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7461 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7462 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7463 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7464 | } |
| 7465 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7466 | void |
| 7467 | set_function_type(ufunc_T *ufunc) |
| 7468 | { |
| 7469 | int varargs = ufunc->uf_va_name != NULL; |
| 7470 | int argcount = ufunc->uf_args.ga_len; |
| 7471 | |
| 7472 | // Create a type for the function, with the return type and any |
| 7473 | // argument types. |
| 7474 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7475 | // The type is included in "tt_args". |
| 7476 | if (argcount > 0 || varargs) |
| 7477 | { |
| 7478 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7479 | argcount, &ufunc->uf_type_list); |
| 7480 | // Add argument types to the function type. |
| 7481 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7482 | argcount + varargs, |
| 7483 | &ufunc->uf_type_list) == FAIL) |
| 7484 | return; |
| 7485 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7486 | ufunc->uf_func_type->tt_min_argcount = |
| 7487 | argcount - ufunc->uf_def_args.ga_len; |
| 7488 | if (ufunc->uf_arg_types == NULL) |
| 7489 | { |
| 7490 | int i; |
| 7491 | |
| 7492 | // lambda does not have argument types. |
| 7493 | for (i = 0; i < argcount; ++i) |
| 7494 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7495 | } |
| 7496 | else |
| 7497 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7498 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7499 | if (varargs) |
| 7500 | { |
| 7501 | ufunc->uf_func_type->tt_args[argcount] = |
| 7502 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7503 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7504 | } |
| 7505 | } |
| 7506 | else |
| 7507 | // No arguments, can use a predefined type. |
| 7508 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7509 | argcount, &ufunc->uf_type_list); |
| 7510 | } |
| 7511 | |
| 7512 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7513 | /* |
| 7514 | * Delete an instruction, free what it contains. |
| 7515 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7516 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7517 | delete_instr(isn_T *isn) |
| 7518 | { |
| 7519 | switch (isn->isn_type) |
| 7520 | { |
| 7521 | case ISN_EXEC: |
| 7522 | case ISN_LOADENV: |
| 7523 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7524 | case ISN_LOADB: |
| 7525 | case ISN_LOADW: |
| 7526 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7527 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7528 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7529 | case ISN_PUSHEXC: |
| 7530 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7531 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7532 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7533 | case ISN_STOREB: |
| 7534 | case ISN_STOREW: |
| 7535 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7536 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7537 | vim_free(isn->isn_arg.string); |
| 7538 | break; |
| 7539 | |
| 7540 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7541 | case ISN_STORES: |
| 7542 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7543 | break; |
| 7544 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7545 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7546 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7547 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7548 | break; |
| 7549 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7550 | case ISN_STOREOPT: |
| 7551 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7552 | break; |
| 7553 | |
| 7554 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7555 | blob_unref(isn->isn_arg.blob); |
| 7556 | break; |
| 7557 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7558 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7559 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7560 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7561 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7562 | break; |
| 7563 | |
| 7564 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7565 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7566 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7567 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7568 | break; |
| 7569 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7570 | case ISN_UCALL: |
| 7571 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7572 | break; |
| 7573 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7574 | case ISN_FUNCREF: |
| 7575 | { |
| 7576 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7577 | + isn->isn_arg.funcref.fr_func; |
| 7578 | func_ptr_unref(dfunc->df_ufunc); |
| 7579 | } |
| 7580 | break; |
| 7581 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7582 | case ISN_2BOOL: |
| 7583 | case ISN_2STRING: |
| 7584 | case ISN_ADDBLOB: |
| 7585 | case ISN_ADDLIST: |
| 7586 | case ISN_BCALL: |
| 7587 | case ISN_CATCH: |
| 7588 | case ISN_CHECKNR: |
| 7589 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7590 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7591 | case ISN_COMPAREANY: |
| 7592 | case ISN_COMPAREBLOB: |
| 7593 | case ISN_COMPAREBOOL: |
| 7594 | case ISN_COMPAREDICT: |
| 7595 | case ISN_COMPAREFLOAT: |
| 7596 | case ISN_COMPAREFUNC: |
| 7597 | case ISN_COMPARELIST: |
| 7598 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7599 | case ISN_COMPARESPECIAL: |
| 7600 | case ISN_COMPARESTRING: |
| 7601 | case ISN_CONCAT: |
| 7602 | case ISN_DCALL: |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 7603 | case ISN_SHUFFLE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7604 | case ISN_DROP: |
| 7605 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7606 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7607 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7608 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7609 | case ISN_EXECCONCAT: |
| 7610 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7611 | case ISN_FOR: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7612 | case ISN_LISTINDEX: |
| 7613 | case ISN_STRINDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7614 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7615 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7616 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7617 | case ISN_JUMP: |
| 7618 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7619 | case ISN_LOADBDICT: |
| 7620 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7621 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7622 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7623 | case ISN_LOADSCRIPT: |
| 7624 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7625 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7626 | case ISN_LOADWDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7627 | case ISN_NEGATENR: |
| 7628 | case ISN_NEWDICT: |
| 7629 | case ISN_NEWLIST: |
| 7630 | case ISN_OPNR: |
| 7631 | case ISN_OPFLOAT: |
| 7632 | case ISN_OPANY: |
| 7633 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7634 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7635 | case ISN_PUSHF: |
| 7636 | case ISN_PUSHNR: |
| 7637 | case ISN_PUSHBOOL: |
| 7638 | case ISN_PUSHSPEC: |
| 7639 | case ISN_RETURN: |
| 7640 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 7641 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7642 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7643 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7644 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7645 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7646 | case ISN_STOREDICT: |
| 7647 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7648 | case ISN_THROW: |
| 7649 | case ISN_TRY: |
| 7650 | // nothing allocated |
| 7651 | break; |
| 7652 | } |
| 7653 | } |
| 7654 | |
| 7655 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7656 | * Free all instructions for "dfunc". |
| 7657 | */ |
| 7658 | static void |
| 7659 | delete_def_function_contents(dfunc_T *dfunc) |
| 7660 | { |
| 7661 | int idx; |
| 7662 | |
| 7663 | ga_clear(&dfunc->df_def_args_isn); |
| 7664 | |
| 7665 | if (dfunc->df_instr != NULL) |
| 7666 | { |
| 7667 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7668 | delete_instr(dfunc->df_instr + idx); |
| 7669 | VIM_CLEAR(dfunc->df_instr); |
| 7670 | } |
| 7671 | |
| 7672 | dfunc->df_deleted = TRUE; |
| 7673 | } |
| 7674 | |
| 7675 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7676 | * When a user function is deleted, clear the contents of any associated def |
| 7677 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7678 | */ |
| 7679 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7680 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7681 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7682 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7683 | { |
| 7684 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7685 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7686 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7687 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7688 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7689 | } |
| 7690 | } |
| 7691 | |
| 7692 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7693 | /* |
| 7694 | * Free all functions defined with ":def". |
| 7695 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7696 | void |
| 7697 | free_def_functions(void) |
| 7698 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7699 | int idx; |
| 7700 | |
| 7701 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7702 | { |
| 7703 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7704 | |
| 7705 | delete_def_function_contents(dfunc); |
| 7706 | } |
| 7707 | |
| 7708 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7709 | } |
| 7710 | #endif |
| 7711 | |
| 7712 | |
| 7713 | #endif // FEAT_EVAL |