Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9compile.c: :def and dealing with instructions |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | #ifdef VMS |
| 20 | # include <float.h> |
| 21 | #endif |
| 22 | |
| 23 | #define DEFINE_VIM9_GLOBALS |
| 24 | #include "vim9.h" |
| 25 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 26 | // values for ctx_skip |
| 27 | typedef enum { |
| 28 | SKIP_NOT, // condition is a constant, produce code |
| 29 | SKIP_YES, // condition is a constant, do NOT produce code |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 30 | SKIP_UNKNOWN // condition is not a constant, produce code |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 31 | } skip_T; |
| 32 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Chain of jump instructions where the end label needs to be set. |
| 35 | */ |
| 36 | typedef struct endlabel_S endlabel_T; |
| 37 | struct endlabel_S { |
| 38 | endlabel_T *el_next; // chain end_label locations |
| 39 | int el_end_label; // instruction idx where to set end |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * info specific for the scope of :if / elseif / else |
| 44 | */ |
| 45 | typedef struct { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 46 | int is_seen_else; |
| 47 | int is_had_return; // every block ends in :return |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 48 | int is_if_label; // instruction idx at IF or ELSEIF |
| 49 | endlabel_T *is_end_label; // instructions to set end label |
| 50 | } ifscope_T; |
| 51 | |
| 52 | /* |
| 53 | * info specific for the scope of :while |
| 54 | */ |
| 55 | typedef struct { |
| 56 | int ws_top_label; // instruction idx at WHILE |
| 57 | endlabel_T *ws_end_label; // instructions to set end |
| 58 | } whilescope_T; |
| 59 | |
| 60 | /* |
| 61 | * info specific for the scope of :for |
| 62 | */ |
| 63 | typedef struct { |
| 64 | int fs_top_label; // instruction idx at FOR |
| 65 | endlabel_T *fs_end_label; // break instructions |
| 66 | } forscope_T; |
| 67 | |
| 68 | /* |
| 69 | * info specific for the scope of :try |
| 70 | */ |
| 71 | typedef struct { |
| 72 | int ts_try_label; // instruction idx at TRY |
| 73 | endlabel_T *ts_end_label; // jump to :finally or :endtry |
| 74 | int ts_catch_label; // instruction idx of last CATCH |
| 75 | int ts_caught_all; // "catch" without argument encountered |
| 76 | } tryscope_T; |
| 77 | |
| 78 | typedef enum { |
| 79 | NO_SCOPE, |
| 80 | IF_SCOPE, |
| 81 | WHILE_SCOPE, |
| 82 | FOR_SCOPE, |
| 83 | TRY_SCOPE, |
| 84 | BLOCK_SCOPE |
| 85 | } scopetype_T; |
| 86 | |
| 87 | /* |
| 88 | * Info for one scope, pointed to by "ctx_scope". |
| 89 | */ |
| 90 | typedef struct scope_S scope_T; |
| 91 | struct scope_S { |
| 92 | scope_T *se_outer; // scope containing this one |
| 93 | scopetype_T se_type; |
| 94 | int se_local_count; // ctx_locals.ga_len before scope |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 95 | skip_T se_skip_save; // ctx_skip before the block |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 96 | union { |
| 97 | ifscope_T se_if; |
| 98 | whilescope_T se_while; |
| 99 | forscope_T se_for; |
| 100 | tryscope_T se_try; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 101 | } se_u; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Entry for "ctx_locals". Used for arguments and local variables. |
| 106 | */ |
| 107 | typedef struct { |
| 108 | char_u *lv_name; |
| 109 | type_T *lv_type; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 110 | int lv_idx; // index of the variable on the stack |
| 111 | int lv_from_outer; // when TRUE using ctx_outer scope |
| 112 | int lv_const; // when TRUE cannot be assigned to |
| 113 | int lv_arg; // when TRUE this is an argument |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 114 | } lvar_T; |
| 115 | |
| 116 | /* |
| 117 | * Context for compiling lines of Vim script. |
| 118 | * Stores info about the local variables and condition stack. |
| 119 | */ |
| 120 | struct cctx_S { |
| 121 | ufunc_T *ctx_ufunc; // current function |
| 122 | int ctx_lnum; // line number in current function |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 123 | char_u *ctx_line_start; // start of current line or NULL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 124 | garray_T ctx_instr; // generated instructions |
| 125 | |
| 126 | garray_T ctx_locals; // currently visible local variables |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 127 | int ctx_locals_count; // total number of local variables |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 128 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 129 | int ctx_closure_count; // number of closures created in the |
| 130 | // function |
| 131 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 132 | garray_T ctx_imports; // imported items |
| 133 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 134 | skip_T ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 135 | scope_T *ctx_scope; // current scope, NULL at toplevel |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 136 | int ctx_had_return; // last seen statement was "return" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 137 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 138 | cctx_T *ctx_outer; // outer scope for lambda or nested |
| 139 | // function |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 140 | int ctx_outer_used; // var in ctx_outer was used |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 141 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 142 | garray_T ctx_type_stack; // type of each item on the stack |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 143 | garray_T *ctx_type_list; // list of pointers to allocated types |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static char e_var_notfound[] = N_("E1001: variable not found: %s"); |
| 147 | static char e_syntax_at[] = N_("E1002: Syntax error at %s"); |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 148 | static char e_used_as_arg[] = N_("E1006: %s is used as an argument"); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 149 | static char e_cannot_use_void[] = N_("E1031: Cannot use void value"); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 150 | static char e_namespace[] = N_("E1075: Namespace not supported: %s"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 151 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 152 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 153 | static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 154 | |
| 155 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 156 | * Lookup variable "name" in the local scope and return it. |
| 157 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 158 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 159 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 160 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 161 | { |
| 162 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 163 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 164 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 165 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 166 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 167 | |
| 168 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 169 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 170 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 171 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 172 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 173 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 174 | { |
| 175 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 176 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 177 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 178 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 179 | |
| 180 | // Find local in outer function scope. |
| 181 | if (cctx->ctx_outer != NULL) |
| 182 | { |
| 183 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 184 | if (lvar != NULL) |
| 185 | { |
| 186 | // TODO: are there situations we should not mark the outer scope as |
| 187 | // used? |
| 188 | cctx->ctx_outer_used = TRUE; |
| 189 | lvar->lv_from_outer = TRUE; |
| 190 | return lvar; |
| 191 | } |
| 192 | } |
| 193 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 194 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 198 | * Lookup an argument in the current function and an enclosing function. |
| 199 | * Returns the argument index in "idxp" |
| 200 | * Returns the argument type in "type" |
| 201 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 202 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 203 | */ |
| 204 | static int |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 205 | lookup_arg( |
| 206 | char_u *name, |
| 207 | size_t len, |
| 208 | int *idxp, |
| 209 | type_T **type, |
| 210 | int *gen_load_outer, |
| 211 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 212 | { |
| 213 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 214 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 215 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 216 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 217 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 218 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 219 | { |
| 220 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 221 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 222 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 223 | { |
| 224 | if (idxp != NULL) |
| 225 | { |
| 226 | // Arguments are located above the frame pointer. One further |
| 227 | // if there is a vararg argument |
| 228 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 229 | + STACK_FRAME_SIZE) |
| 230 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 231 | |
| 232 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 233 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 234 | else |
| 235 | *type = &t_any; |
| 236 | } |
| 237 | return OK; |
| 238 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 239 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 240 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 241 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 242 | if (va_name != NULL |
| 243 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 244 | { |
| 245 | if (idxp != NULL) |
| 246 | { |
| 247 | // varargs is always the last argument |
| 248 | *idxp = -STACK_FRAME_SIZE - 1; |
| 249 | *type = cctx->ctx_ufunc->uf_va_type; |
| 250 | } |
| 251 | return OK; |
| 252 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 253 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 254 | if (cctx->ctx_outer != NULL) |
| 255 | { |
| 256 | // Lookup the name for an argument of the outer function. |
| 257 | if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
| 258 | == OK) |
| 259 | { |
| 260 | *gen_load_outer = TRUE; |
| 261 | return OK; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Lookup a variable in the current script. |
| 270 | * Returns OK or FAIL. |
| 271 | */ |
| 272 | static int |
| 273 | lookup_script(char_u *name, size_t len) |
| 274 | { |
| 275 | int cc; |
| 276 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 277 | dictitem_T *di; |
| 278 | |
| 279 | cc = name[len]; |
| 280 | name[len] = NUL; |
| 281 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 282 | name[len] = cc; |
| 283 | return di == NULL ? FAIL: OK; |
| 284 | } |
| 285 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 286 | /* |
| 287 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 288 | * compilation context "cctx". |
| 289 | * Return FAIL and give an error if it defined. |
| 290 | */ |
| 291 | int |
Bram Moolenaar | cbb6bdc | 2020-07-06 21:53:17 +0200 | [diff] [blame] | 292 | check_defined(char_u *p, size_t len, cctx_T *cctx) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 293 | { |
| 294 | if (lookup_script(p, len) == OK |
| 295 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 296 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 297 | || find_imported(p, len, cctx) != NULL))) |
| 298 | { |
| 299 | semsg("E1073: imported name already defined: %s", p); |
| 300 | return FAIL; |
| 301 | } |
| 302 | return OK; |
| 303 | } |
| 304 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 305 | /* |
| 306 | * Allocate memory for a type_T and add the pointer to type_gap, so that it can |
| 307 | * be freed later. |
| 308 | */ |
| 309 | static type_T * |
| 310 | alloc_type(garray_T *type_gap) |
| 311 | { |
| 312 | type_T *type; |
| 313 | |
| 314 | if (ga_grow(type_gap, 1) == FAIL) |
| 315 | return NULL; |
| 316 | type = ALLOC_CLEAR_ONE(type_T); |
| 317 | if (type != NULL) |
| 318 | { |
| 319 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type; |
| 320 | ++type_gap->ga_len; |
| 321 | } |
| 322 | return type; |
| 323 | } |
| 324 | |
Bram Moolenaar | 6110e79 | 2020-07-08 19:35:21 +0200 | [diff] [blame] | 325 | void |
| 326 | clear_type_list(garray_T *gap) |
| 327 | { |
| 328 | while (gap->ga_len > 0) |
| 329 | vim_free(((type_T **)gap->ga_data)[--gap->ga_len]); |
| 330 | ga_clear(gap); |
| 331 | } |
| 332 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 333 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 334 | get_list_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 335 | { |
| 336 | type_T *type; |
| 337 | |
| 338 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 339 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 340 | return &t_list_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 341 | if (member_type->tt_type == VAR_VOID |
| 342 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 343 | return &t_list_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 344 | if (member_type->tt_type == VAR_BOOL) |
| 345 | return &t_list_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 346 | if (member_type->tt_type == VAR_NUMBER) |
| 347 | return &t_list_number; |
| 348 | if (member_type->tt_type == VAR_STRING) |
| 349 | return &t_list_string; |
| 350 | |
| 351 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 352 | type = alloc_type(type_gap); |
| 353 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 354 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 355 | type->tt_type = VAR_LIST; |
| 356 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 357 | type->tt_argcount = 0; |
| 358 | type->tt_args = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 359 | return type; |
| 360 | } |
| 361 | |
| 362 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 363 | get_dict_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 364 | { |
| 365 | type_T *type; |
| 366 | |
| 367 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 368 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 369 | return &t_dict_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 370 | if (member_type->tt_type == VAR_VOID |
| 371 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 372 | return &t_dict_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 373 | if (member_type->tt_type == VAR_BOOL) |
| 374 | return &t_dict_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 375 | if (member_type->tt_type == VAR_NUMBER) |
| 376 | return &t_dict_number; |
| 377 | if (member_type->tt_type == VAR_STRING) |
| 378 | return &t_dict_string; |
| 379 | |
| 380 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 381 | type = alloc_type(type_gap); |
| 382 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 383 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 384 | type->tt_type = VAR_DICT; |
| 385 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 386 | type->tt_argcount = 0; |
| 387 | type->tt_args = NULL; |
| 388 | return type; |
| 389 | } |
| 390 | |
| 391 | /* |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 392 | * Allocate a new type for a function. |
| 393 | */ |
| 394 | static type_T * |
| 395 | alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 396 | { |
| 397 | type_T *type = alloc_type(type_gap); |
| 398 | |
| 399 | if (type == NULL) |
| 400 | return &t_any; |
| 401 | type->tt_type = VAR_FUNC; |
| 402 | type->tt_member = ret_type; |
| 403 | type->tt_argcount = argcount; |
| 404 | type->tt_args = NULL; |
| 405 | return type; |
| 406 | } |
| 407 | |
| 408 | /* |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 409 | * Get a function type, based on the return type "ret_type". |
| 410 | * If "argcount" is -1 or 0 a predefined type can be used. |
| 411 | * If "argcount" > 0 always create a new type, so that arguments can be added. |
| 412 | */ |
| 413 | static type_T * |
| 414 | get_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 415 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 416 | // recognize commonly used types |
| 417 | if (argcount <= 0) |
| 418 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 419 | if (ret_type == &t_unknown) |
| 420 | { |
| 421 | // (argcount == 0) is not possible |
| 422 | return &t_func_unknown; |
| 423 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 424 | if (ret_type == &t_void) |
| 425 | { |
| 426 | if (argcount == 0) |
| 427 | return &t_func_0_void; |
| 428 | else |
| 429 | return &t_func_void; |
| 430 | } |
| 431 | if (ret_type == &t_any) |
| 432 | { |
| 433 | if (argcount == 0) |
| 434 | return &t_func_0_any; |
| 435 | else |
| 436 | return &t_func_any; |
| 437 | } |
| 438 | if (ret_type == &t_number) |
| 439 | { |
| 440 | if (argcount == 0) |
| 441 | return &t_func_0_number; |
| 442 | else |
| 443 | return &t_func_number; |
| 444 | } |
| 445 | if (ret_type == &t_string) |
| 446 | { |
| 447 | if (argcount == 0) |
| 448 | return &t_func_0_string; |
| 449 | else |
| 450 | return &t_func_string; |
| 451 | } |
| 452 | } |
| 453 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 454 | return alloc_func_type(ret_type, argcount, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 455 | } |
| 456 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 457 | /* |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 458 | * For a function type, reserve space for "argcount" argument types (including |
| 459 | * vararg). |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 460 | */ |
| 461 | static int |
| 462 | func_type_add_arg_types( |
| 463 | type_T *functype, |
| 464 | int argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 465 | garray_T *type_gap) |
| 466 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 467 | // To make it easy to free the space needed for the argument types, add the |
| 468 | // pointer to type_gap. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 469 | if (ga_grow(type_gap, 1) == FAIL) |
| 470 | return FAIL; |
| 471 | functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); |
| 472 | if (functype->tt_args == NULL) |
| 473 | return FAIL; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 474 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = |
| 475 | (void *)functype->tt_args; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 476 | ++type_gap->ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 477 | return OK; |
| 478 | } |
| 479 | |
| 480 | /* |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 481 | * Get a type_T for a typval_T. |
| 482 | * "type_list" is used to temporarily create types in. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 483 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 484 | type_T * |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 485 | typval2type(typval_T *tv, garray_T *type_gap) |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 486 | { |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 487 | type_T *actual; |
| 488 | type_T *member_type; |
| 489 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 490 | if (tv->v_type == VAR_NUMBER) |
| 491 | return &t_number; |
| 492 | if (tv->v_type == VAR_BOOL) |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 493 | return &t_bool; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 494 | if (tv->v_type == VAR_STRING) |
| 495 | return &t_string; |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 496 | |
| 497 | if (tv->v_type == VAR_LIST |
| 498 | && tv->vval.v_list != NULL |
| 499 | && tv->vval.v_list->lv_first != NULL) |
| 500 | { |
| 501 | // Use the type of the first member, it is the most specific. |
| 502 | member_type = typval2type(&tv->vval.v_list->lv_first->li_tv, type_gap); |
| 503 | return get_list_type(member_type, type_gap); |
| 504 | } |
| 505 | |
| 506 | if (tv->v_type == VAR_DICT |
| 507 | && tv->vval.v_dict != NULL |
| 508 | && tv->vval.v_dict->dv_hashtab.ht_used > 0) |
| 509 | { |
| 510 | dict_iterator_T iter; |
| 511 | typval_T *value; |
| 512 | |
| 513 | // Use the type of the first value, it is the most specific. |
| 514 | dict_iterate_start(tv, &iter); |
| 515 | dict_iterate_next(&iter, &value); |
| 516 | member_type = typval2type(value, type_gap); |
| 517 | return get_dict_type(member_type, type_gap); |
| 518 | } |
| 519 | |
| 520 | if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL) |
| 521 | { |
| 522 | char_u *name = NULL; |
| 523 | ufunc_T *ufunc = NULL; |
| 524 | |
| 525 | if (tv->v_type == VAR_PARTIAL) |
| 526 | { |
| 527 | if (tv->vval.v_partial->pt_func != NULL) |
| 528 | ufunc = tv->vval.v_partial->pt_func; |
| 529 | else |
| 530 | name = tv->vval.v_partial->pt_name; |
| 531 | } |
| 532 | else |
| 533 | name = tv->vval.v_string; |
| 534 | if (name != NULL) |
| 535 | // TODO: how about a builtin function? |
| 536 | ufunc = find_func(name, FALSE, NULL); |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame^] | 537 | if (ufunc != NULL) |
| 538 | { |
| 539 | // May need to get the argument types from default values by |
| 540 | // compiling the function. |
| 541 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
| 542 | && compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 543 | return NULL; |
| 544 | if (ufunc->uf_func_type != NULL) |
| 545 | return ufunc->uf_func_type; |
| 546 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | actual = alloc_type(type_gap); |
| 550 | if (actual == NULL) |
| 551 | return NULL; |
| 552 | actual->tt_type = tv->v_type; |
| 553 | actual->tt_member = &t_any; |
| 554 | |
| 555 | return actual; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Get a type_T for a typval_T, used for v: variables. |
| 560 | * "type_list" is used to temporarily create types in. |
| 561 | */ |
| 562 | type_T * |
| 563 | typval2type_vimvar(typval_T *tv, garray_T *type_gap) |
| 564 | { |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 565 | if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles |
| 566 | return &t_list_string; |
| 567 | if (tv->v_type == VAR_DICT) // e.g. for v:completed_item |
| 568 | return &t_dict_any; |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 569 | return typval2type(tv, type_gap); |
| 570 | } |
| 571 | |
| 572 | |
| 573 | /* |
| 574 | * Return FAIL if "expected" and "actual" don't match. |
| 575 | */ |
| 576 | int |
| 577 | check_typval_type(type_T *expected, typval_T *actual_tv) |
| 578 | { |
| 579 | garray_T type_list; |
| 580 | type_T *actual_type; |
| 581 | int res = FAIL; |
| 582 | |
| 583 | ga_init2(&type_list, sizeof(type_T *), 10); |
| 584 | actual_type = typval2type(actual_tv, &type_list); |
| 585 | if (actual_type != NULL) |
| 586 | res = check_type(expected, actual_type, TRUE); |
| 587 | clear_type_list(&type_list); |
| 588 | return res; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 589 | } |
| 590 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 591 | static void |
| 592 | type_mismatch(type_T *expected, type_T *actual) |
| 593 | { |
| 594 | char *tofree1, *tofree2; |
| 595 | |
| 596 | semsg(_("E1013: type mismatch, expected %s but got %s"), |
| 597 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 598 | vim_free(tofree1); |
| 599 | vim_free(tofree2); |
| 600 | } |
| 601 | |
| 602 | static void |
| 603 | arg_type_mismatch(type_T *expected, type_T *actual, int argidx) |
| 604 | { |
| 605 | char *tofree1, *tofree2; |
| 606 | |
| 607 | semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"), |
| 608 | argidx, |
| 609 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 610 | vim_free(tofree1); |
| 611 | vim_free(tofree2); |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * Check if the expected and actual types match. |
| 616 | * Does not allow for assigning "any" to a specific type. |
| 617 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 618 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 619 | check_type(type_T *expected, type_T *actual, int give_msg) |
| 620 | { |
| 621 | int ret = OK; |
| 622 | |
| 623 | // When expected is "unknown" we accept any actual type. |
| 624 | // When expected is "any" we accept any actual type except "void". |
| 625 | if (expected->tt_type != VAR_UNKNOWN |
| 626 | && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) |
| 627 | |
| 628 | { |
| 629 | if (expected->tt_type != actual->tt_type) |
| 630 | { |
| 631 | if (give_msg) |
| 632 | type_mismatch(expected, actual); |
| 633 | return FAIL; |
| 634 | } |
| 635 | if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) |
| 636 | { |
| 637 | // "unknown" is used for an empty list or dict |
| 638 | if (actual->tt_member != &t_unknown) |
| 639 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 640 | } |
| 641 | else if (expected->tt_type == VAR_FUNC) |
| 642 | { |
| 643 | if (expected->tt_member != &t_unknown) |
| 644 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 645 | if (ret == OK && expected->tt_argcount != -1 |
| 646 | && (actual->tt_argcount < expected->tt_min_argcount |
| 647 | || actual->tt_argcount > expected->tt_argcount)) |
| 648 | ret = FAIL; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 649 | if (expected->tt_args != NULL && actual->tt_args != NULL) |
| 650 | { |
| 651 | int i; |
| 652 | |
| 653 | for (i = 0; i < expected->tt_argcount; ++i) |
| 654 | if (check_type(expected->tt_args[i], actual->tt_args[i], |
| 655 | FALSE) == FAIL) |
| 656 | { |
| 657 | ret = FAIL; |
| 658 | break; |
| 659 | } |
| 660 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 661 | } |
| 662 | if (ret == FAIL && give_msg) |
| 663 | type_mismatch(expected, actual); |
| 664 | } |
| 665 | return ret; |
| 666 | } |
| 667 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 668 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 669 | ///////////////////////////////////////////////////////////////////// |
| 670 | // Following generate_ functions expect the caller to call ga_grow(). |
| 671 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 672 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 673 | #define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 674 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 675 | /* |
| 676 | * Generate an instruction without arguments. |
| 677 | * Returns a pointer to the new instruction, NULL if failed. |
| 678 | */ |
| 679 | static isn_T * |
| 680 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 681 | { |
| 682 | garray_T *instr = &cctx->ctx_instr; |
| 683 | isn_T *isn; |
| 684 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 685 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 686 | if (ga_grow(instr, 1) == FAIL) |
| 687 | return NULL; |
| 688 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 689 | isn->isn_type = isn_type; |
| 690 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 691 | ++instr->ga_len; |
| 692 | |
| 693 | return isn; |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * Generate an instruction without arguments. |
| 698 | * "drop" will be removed from the stack. |
| 699 | * Returns a pointer to the new instruction, NULL if failed. |
| 700 | */ |
| 701 | static isn_T * |
| 702 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 703 | { |
| 704 | garray_T *stack = &cctx->ctx_type_stack; |
| 705 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 706 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 707 | stack->ga_len -= drop; |
| 708 | return generate_instr(cctx, isn_type); |
| 709 | } |
| 710 | |
| 711 | /* |
| 712 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 713 | */ |
| 714 | static isn_T * |
| 715 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 716 | { |
| 717 | isn_T *isn; |
| 718 | garray_T *stack = &cctx->ctx_type_stack; |
| 719 | |
| 720 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 721 | return NULL; |
| 722 | |
| 723 | if (ga_grow(stack, 1) == FAIL) |
| 724 | return NULL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 725 | ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 726 | ++stack->ga_len; |
| 727 | |
| 728 | return isn; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 733 | */ |
| 734 | static int |
| 735 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 736 | { |
| 737 | isn_T *isn; |
| 738 | garray_T *stack = &cctx->ctx_type_stack; |
| 739 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 740 | |
| 741 | if ((*type)->tt_type == VAR_STRING) |
| 742 | return OK; |
| 743 | *type = &t_string; |
| 744 | |
| 745 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 746 | return FAIL; |
| 747 | isn->isn_arg.number = offset; |
| 748 | |
| 749 | return OK; |
| 750 | } |
| 751 | |
| 752 | static int |
| 753 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 754 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 755 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 756 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 757 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 758 | { |
| 759 | if (*op == '+') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 760 | emsg(_("E1051: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 761 | else |
| 762 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 763 | return FAIL; |
| 764 | } |
| 765 | return OK; |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * Generate an instruction with two arguments. The instruction depends on the |
| 770 | * type of the arguments. |
| 771 | */ |
| 772 | static int |
| 773 | generate_two_op(cctx_T *cctx, char_u *op) |
| 774 | { |
| 775 | garray_T *stack = &cctx->ctx_type_stack; |
| 776 | type_T *type1; |
| 777 | type_T *type2; |
| 778 | vartype_T vartype; |
| 779 | isn_T *isn; |
| 780 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 781 | RETURN_OK_IF_SKIP(cctx); |
| 782 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 783 | // Get the known type of the two items on the stack. If they are matching |
| 784 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 785 | // checking. |
| 786 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 787 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 788 | vartype = VAR_ANY; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 789 | if (type1->tt_type == type2->tt_type |
| 790 | && (type1->tt_type == VAR_NUMBER |
| 791 | || type1->tt_type == VAR_LIST |
| 792 | #ifdef FEAT_FLOAT |
| 793 | || type1->tt_type == VAR_FLOAT |
| 794 | #endif |
| 795 | || type1->tt_type == VAR_BLOB)) |
| 796 | vartype = type1->tt_type; |
| 797 | |
| 798 | switch (*op) |
| 799 | { |
| 800 | case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 801 | && type1->tt_type != VAR_ANY |
| 802 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 803 | && check_number_or_float( |
| 804 | type1->tt_type, type2->tt_type, op) == FAIL) |
| 805 | return FAIL; |
| 806 | isn = generate_instr_drop(cctx, |
| 807 | vartype == VAR_NUMBER ? ISN_OPNR |
| 808 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 809 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 810 | #ifdef FEAT_FLOAT |
| 811 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 812 | #endif |
| 813 | : ISN_OPANY, 1); |
| 814 | if (isn != NULL) |
| 815 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 816 | break; |
| 817 | |
| 818 | case '-': |
| 819 | case '*': |
| 820 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 821 | op) == FAIL) |
| 822 | return FAIL; |
| 823 | if (vartype == VAR_NUMBER) |
| 824 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 825 | #ifdef FEAT_FLOAT |
| 826 | else if (vartype == VAR_FLOAT) |
| 827 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 828 | #endif |
| 829 | else |
| 830 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 831 | if (isn != NULL) |
| 832 | isn->isn_arg.op.op_type = *op == '*' |
| 833 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 834 | break; |
| 835 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 836 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 837 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 838 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 839 | && type2->tt_type != VAR_NUMBER)) |
| 840 | { |
| 841 | emsg(_("E1035: % requires number arguments")); |
| 842 | return FAIL; |
| 843 | } |
| 844 | isn = generate_instr_drop(cctx, |
| 845 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 846 | if (isn != NULL) |
| 847 | isn->isn_arg.op.op_type = EXPR_REM; |
| 848 | break; |
| 849 | } |
| 850 | |
| 851 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 852 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 853 | { |
| 854 | type_T *type = &t_any; |
| 855 | |
| 856 | #ifdef FEAT_FLOAT |
| 857 | // float+number and number+float results in float |
| 858 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 859 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 860 | type = &t_float; |
| 861 | #endif |
| 862 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 863 | } |
| 864 | |
| 865 | return OK; |
| 866 | } |
| 867 | |
| 868 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 869 | * Get the instruction to use for comparing "type1" with "type2" |
| 870 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 871 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 872 | static isntype_T |
| 873 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 874 | { |
| 875 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 876 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 877 | if (type1 == VAR_UNKNOWN) |
| 878 | type1 = VAR_ANY; |
| 879 | if (type2 == VAR_UNKNOWN) |
| 880 | type2 = VAR_ANY; |
| 881 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 882 | if (type1 == type2) |
| 883 | { |
| 884 | switch (type1) |
| 885 | { |
| 886 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 887 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 888 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 889 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 890 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 891 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 892 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 893 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 894 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 895 | default: isntype = ISN_COMPAREANY; break; |
| 896 | } |
| 897 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 898 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 899 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 900 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 901 | isntype = ISN_COMPAREANY; |
| 902 | |
| 903 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 904 | && (isntype == ISN_COMPAREBOOL |
| 905 | || isntype == ISN_COMPARESPECIAL |
| 906 | || isntype == ISN_COMPARENR |
| 907 | || isntype == ISN_COMPAREFLOAT)) |
| 908 | { |
| 909 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 910 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 911 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 912 | } |
| 913 | if (isntype == ISN_DROP |
| 914 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 915 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 916 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 917 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 918 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 919 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 920 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 921 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 922 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 923 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 924 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 925 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 926 | return isntype; |
| 927 | } |
| 928 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 929 | int |
| 930 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 931 | { |
| 932 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 933 | return FAIL; |
| 934 | return OK; |
| 935 | } |
| 936 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 937 | /* |
| 938 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 939 | */ |
| 940 | static int |
| 941 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 942 | { |
| 943 | isntype_T isntype; |
| 944 | isn_T *isn; |
| 945 | garray_T *stack = &cctx->ctx_type_stack; |
| 946 | vartype_T type1; |
| 947 | vartype_T type2; |
| 948 | |
| 949 | RETURN_OK_IF_SKIP(cctx); |
| 950 | |
| 951 | // Get the known type of the two items on the stack. If they are matching |
| 952 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 953 | // checking. |
| 954 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 955 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 956 | isntype = get_compare_isn(exptype, type1, type2); |
| 957 | if (isntype == ISN_DROP) |
| 958 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 959 | |
| 960 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 961 | return FAIL; |
| 962 | isn->isn_arg.op.op_type = exptype; |
| 963 | isn->isn_arg.op.op_ic = ic; |
| 964 | |
| 965 | // takes two arguments, puts one bool back |
| 966 | if (stack->ga_len >= 2) |
| 967 | { |
| 968 | --stack->ga_len; |
| 969 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 970 | } |
| 971 | |
| 972 | return OK; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * Generate an ISN_2BOOL instruction. |
| 977 | */ |
| 978 | static int |
| 979 | generate_2BOOL(cctx_T *cctx, int invert) |
| 980 | { |
| 981 | isn_T *isn; |
| 982 | garray_T *stack = &cctx->ctx_type_stack; |
| 983 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 984 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 985 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 986 | return FAIL; |
| 987 | isn->isn_arg.number = invert; |
| 988 | |
| 989 | // type becomes bool |
| 990 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 991 | |
| 992 | return OK; |
| 993 | } |
| 994 | |
| 995 | static int |
| 996 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 997 | { |
| 998 | isn_T *isn; |
| 999 | garray_T *stack = &cctx->ctx_type_stack; |
| 1000 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1001 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1002 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 1003 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 1004 | // TODO: whole type, e.g. for a function also arg and return types |
| 1005 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1006 | isn->isn_arg.type.ct_off = offset; |
| 1007 | |
| 1008 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1009 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1010 | |
| 1011 | return OK; |
| 1012 | } |
| 1013 | |
| 1014 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1015 | * Check that |
| 1016 | * - "actual" is "expected" type or |
| 1017 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 1018 | * - return FAIL. |
| 1019 | */ |
| 1020 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1021 | need_type( |
| 1022 | type_T *actual, |
| 1023 | type_T *expected, |
| 1024 | int offset, |
| 1025 | cctx_T *cctx, |
| 1026 | int silent) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1027 | { |
| 1028 | if (check_type(expected, actual, FALSE) == OK) |
| 1029 | return OK; |
| 1030 | if (actual->tt_type != VAR_ANY |
| 1031 | && actual->tt_type != VAR_UNKNOWN |
| 1032 | && !(actual->tt_type == VAR_FUNC |
| 1033 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 1034 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1035 | if (!silent) |
| 1036 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1037 | return FAIL; |
| 1038 | } |
| 1039 | generate_TYPECHECK(cctx, expected, offset); |
| 1040 | return OK; |
| 1041 | } |
| 1042 | |
| 1043 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1044 | * Generate an ISN_PUSHNR instruction. |
| 1045 | */ |
| 1046 | static int |
| 1047 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 1048 | { |
| 1049 | isn_T *isn; |
| 1050 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1051 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1052 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 1053 | return FAIL; |
| 1054 | isn->isn_arg.number = number; |
| 1055 | |
| 1056 | return OK; |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | * Generate an ISN_PUSHBOOL instruction. |
| 1061 | */ |
| 1062 | static int |
| 1063 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 1064 | { |
| 1065 | isn_T *isn; |
| 1066 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1067 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1068 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 1069 | return FAIL; |
| 1070 | isn->isn_arg.number = number; |
| 1071 | |
| 1072 | return OK; |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Generate an ISN_PUSHSPEC instruction. |
| 1077 | */ |
| 1078 | static int |
| 1079 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 1080 | { |
| 1081 | isn_T *isn; |
| 1082 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1083 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1084 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 1085 | return FAIL; |
| 1086 | isn->isn_arg.number = number; |
| 1087 | |
| 1088 | return OK; |
| 1089 | } |
| 1090 | |
| 1091 | #ifdef FEAT_FLOAT |
| 1092 | /* |
| 1093 | * Generate an ISN_PUSHF instruction. |
| 1094 | */ |
| 1095 | static int |
| 1096 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 1097 | { |
| 1098 | isn_T *isn; |
| 1099 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1100 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1101 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 1102 | return FAIL; |
| 1103 | isn->isn_arg.fnumber = fnumber; |
| 1104 | |
| 1105 | return OK; |
| 1106 | } |
| 1107 | #endif |
| 1108 | |
| 1109 | /* |
| 1110 | * Generate an ISN_PUSHS instruction. |
| 1111 | * Consumes "str". |
| 1112 | */ |
| 1113 | static int |
| 1114 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 1115 | { |
| 1116 | isn_T *isn; |
| 1117 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1118 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1119 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 1120 | return FAIL; |
| 1121 | isn->isn_arg.string = str; |
| 1122 | |
| 1123 | return OK; |
| 1124 | } |
| 1125 | |
| 1126 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1127 | * Generate an ISN_PUSHCHANNEL instruction. |
| 1128 | * Consumes "channel". |
| 1129 | */ |
| 1130 | static int |
| 1131 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 1132 | { |
| 1133 | isn_T *isn; |
| 1134 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1135 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1136 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 1137 | return FAIL; |
| 1138 | isn->isn_arg.channel = channel; |
| 1139 | |
| 1140 | return OK; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * Generate an ISN_PUSHJOB instruction. |
| 1145 | * Consumes "job". |
| 1146 | */ |
| 1147 | static int |
| 1148 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 1149 | { |
| 1150 | isn_T *isn; |
| 1151 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1152 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1153 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1154 | return FAIL; |
| 1155 | isn->isn_arg.job = job; |
| 1156 | |
| 1157 | return OK; |
| 1158 | } |
| 1159 | |
| 1160 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1161 | * Generate an ISN_PUSHBLOB instruction. |
| 1162 | * Consumes "blob". |
| 1163 | */ |
| 1164 | static int |
| 1165 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1166 | { |
| 1167 | isn_T *isn; |
| 1168 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1169 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1170 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1171 | return FAIL; |
| 1172 | isn->isn_arg.blob = blob; |
| 1173 | |
| 1174 | return OK; |
| 1175 | } |
| 1176 | |
| 1177 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1178 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1179 | * Consumes "name". |
| 1180 | */ |
| 1181 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1182 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1183 | { |
| 1184 | isn_T *isn; |
| 1185 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1186 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1187 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1188 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 1189 | isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1190 | |
| 1191 | return OK; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1195 | * Generate an ISN_GETITEM instruction with "index". |
| 1196 | */ |
| 1197 | static int |
| 1198 | generate_GETITEM(cctx_T *cctx, int index) |
| 1199 | { |
| 1200 | isn_T *isn; |
| 1201 | garray_T *stack = &cctx->ctx_type_stack; |
| 1202 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1203 | type_T *item_type = &t_any; |
| 1204 | |
| 1205 | RETURN_OK_IF_SKIP(cctx); |
| 1206 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1207 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1208 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1209 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1210 | emsg(_(e_listreq)); |
| 1211 | return FAIL; |
| 1212 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1213 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1214 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1215 | return FAIL; |
| 1216 | isn->isn_arg.number = index; |
| 1217 | |
| 1218 | // add the item type to the type stack |
| 1219 | if (ga_grow(stack, 1) == FAIL) |
| 1220 | return FAIL; |
| 1221 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 1222 | ++stack->ga_len; |
| 1223 | return OK; |
| 1224 | } |
| 1225 | |
| 1226 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 1227 | * Generate an ISN_SLICE instruction with "count". |
| 1228 | */ |
| 1229 | static int |
| 1230 | generate_SLICE(cctx_T *cctx, int count) |
| 1231 | { |
| 1232 | isn_T *isn; |
| 1233 | |
| 1234 | RETURN_OK_IF_SKIP(cctx); |
| 1235 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1236 | return FAIL; |
| 1237 | isn->isn_arg.number = count; |
| 1238 | return OK; |
| 1239 | } |
| 1240 | |
| 1241 | /* |
| 1242 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1243 | */ |
| 1244 | static int |
| 1245 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1246 | { |
| 1247 | isn_T *isn; |
| 1248 | |
| 1249 | RETURN_OK_IF_SKIP(cctx); |
| 1250 | |
| 1251 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1252 | return FAIL; |
| 1253 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1254 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1255 | |
| 1256 | return OK; |
| 1257 | } |
| 1258 | |
| 1259 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1260 | * Generate an ISN_STORE instruction. |
| 1261 | */ |
| 1262 | static int |
| 1263 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1264 | { |
| 1265 | isn_T *isn; |
| 1266 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1267 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1268 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1269 | return FAIL; |
| 1270 | if (name != NULL) |
| 1271 | isn->isn_arg.string = vim_strsave(name); |
| 1272 | else |
| 1273 | isn->isn_arg.number = idx; |
| 1274 | |
| 1275 | return OK; |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1280 | */ |
| 1281 | static int |
| 1282 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1283 | { |
| 1284 | isn_T *isn; |
| 1285 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1286 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1287 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1288 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1289 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1290 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1291 | |
| 1292 | return OK; |
| 1293 | } |
| 1294 | |
| 1295 | /* |
| 1296 | * Generate an ISN_STOREOPT instruction |
| 1297 | */ |
| 1298 | static int |
| 1299 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1300 | { |
| 1301 | isn_T *isn; |
| 1302 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1303 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1304 | if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL) |
| 1305 | return FAIL; |
| 1306 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1307 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1308 | |
| 1309 | return OK; |
| 1310 | } |
| 1311 | |
| 1312 | /* |
| 1313 | * Generate an ISN_LOAD or similar instruction. |
| 1314 | */ |
| 1315 | static int |
| 1316 | generate_LOAD( |
| 1317 | cctx_T *cctx, |
| 1318 | isntype_T isn_type, |
| 1319 | int idx, |
| 1320 | char_u *name, |
| 1321 | type_T *type) |
| 1322 | { |
| 1323 | isn_T *isn; |
| 1324 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1325 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1326 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1327 | return FAIL; |
| 1328 | if (name != NULL) |
| 1329 | isn->isn_arg.string = vim_strsave(name); |
| 1330 | else |
| 1331 | isn->isn_arg.number = idx; |
| 1332 | |
| 1333 | return OK; |
| 1334 | } |
| 1335 | |
| 1336 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1337 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1338 | */ |
| 1339 | static int |
| 1340 | generate_LOADV( |
| 1341 | cctx_T *cctx, |
| 1342 | char_u *name, |
| 1343 | int error) |
| 1344 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1345 | int di_flags; |
| 1346 | int vidx = find_vim_var(name, &di_flags); |
| 1347 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1348 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1349 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1350 | if (vidx < 0) |
| 1351 | { |
| 1352 | if (error) |
| 1353 | semsg(_(e_var_notfound), name); |
| 1354 | return FAIL; |
| 1355 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1356 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1357 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1358 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1362 | * Generate an ISN_UNLET instruction. |
| 1363 | */ |
| 1364 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1365 | generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1366 | { |
| 1367 | isn_T *isn; |
| 1368 | |
| 1369 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1370 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1371 | return FAIL; |
| 1372 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1373 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1374 | |
| 1375 | return OK; |
| 1376 | } |
| 1377 | |
| 1378 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1379 | * Generate an ISN_LOADS instruction. |
| 1380 | */ |
| 1381 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1382 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1383 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1384 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1385 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1386 | int sid, |
| 1387 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1388 | { |
| 1389 | isn_T *isn; |
| 1390 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1391 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1392 | if (isn_type == ISN_LOADS) |
| 1393 | isn = generate_instr_type(cctx, isn_type, type); |
| 1394 | else |
| 1395 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1396 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1397 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1398 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1399 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1400 | |
| 1401 | return OK; |
| 1402 | } |
| 1403 | |
| 1404 | /* |
| 1405 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1406 | */ |
| 1407 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1408 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1409 | cctx_T *cctx, |
| 1410 | isntype_T isn_type, |
| 1411 | int sid, |
| 1412 | int idx, |
| 1413 | type_T *type) |
| 1414 | { |
| 1415 | isn_T *isn; |
| 1416 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1417 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1418 | if (isn_type == ISN_LOADSCRIPT) |
| 1419 | isn = generate_instr_type(cctx, isn_type, type); |
| 1420 | else |
| 1421 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1422 | if (isn == NULL) |
| 1423 | return FAIL; |
| 1424 | isn->isn_arg.script.script_sid = sid; |
| 1425 | isn->isn_arg.script.script_idx = idx; |
| 1426 | return OK; |
| 1427 | } |
| 1428 | |
| 1429 | /* |
| 1430 | * Generate an ISN_NEWLIST instruction. |
| 1431 | */ |
| 1432 | static int |
| 1433 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1434 | { |
| 1435 | isn_T *isn; |
| 1436 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1437 | type_T *type; |
| 1438 | type_T *member; |
| 1439 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1440 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1441 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1442 | return FAIL; |
| 1443 | isn->isn_arg.number = count; |
| 1444 | |
| 1445 | // drop the value types |
| 1446 | stack->ga_len -= count; |
| 1447 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1448 | // Use the first value type for the list member type. Use "any" for an |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1449 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1450 | if (count > 0) |
| 1451 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1452 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1453 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1454 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1455 | |
| 1456 | // add the list type to the type stack |
| 1457 | if (ga_grow(stack, 1) == FAIL) |
| 1458 | return FAIL; |
| 1459 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1460 | ++stack->ga_len; |
| 1461 | |
| 1462 | return OK; |
| 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * Generate an ISN_NEWDICT instruction. |
| 1467 | */ |
| 1468 | static int |
| 1469 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1470 | { |
| 1471 | isn_T *isn; |
| 1472 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1473 | type_T *type; |
| 1474 | type_T *member; |
| 1475 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1476 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1477 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1478 | return FAIL; |
| 1479 | isn->isn_arg.number = count; |
| 1480 | |
| 1481 | // drop the key and value types |
| 1482 | stack->ga_len -= 2 * count; |
| 1483 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1484 | // Use the first value type for the list member type. Use "void" for an |
| 1485 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1486 | if (count > 0) |
| 1487 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1488 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1489 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1490 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1491 | |
| 1492 | // add the dict type to the type stack |
| 1493 | if (ga_grow(stack, 1) == FAIL) |
| 1494 | return FAIL; |
| 1495 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1496 | ++stack->ga_len; |
| 1497 | |
| 1498 | return OK; |
| 1499 | } |
| 1500 | |
| 1501 | /* |
| 1502 | * Generate an ISN_FUNCREF instruction. |
| 1503 | */ |
| 1504 | static int |
| 1505 | generate_FUNCREF(cctx_T *cctx, int dfunc_idx) |
| 1506 | { |
| 1507 | isn_T *isn; |
| 1508 | garray_T *stack = &cctx->ctx_type_stack; |
| 1509 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1510 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1511 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1512 | return FAIL; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1513 | isn->isn_arg.funcref.fr_func = dfunc_idx; |
| 1514 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1515 | |
| 1516 | if (ga_grow(stack, 1) == FAIL) |
| 1517 | return FAIL; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1518 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1519 | // TODO: argument and return types |
| 1520 | ++stack->ga_len; |
| 1521 | |
| 1522 | return OK; |
| 1523 | } |
| 1524 | |
| 1525 | /* |
| 1526 | * Generate an ISN_JUMP instruction. |
| 1527 | */ |
| 1528 | static int |
| 1529 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1530 | { |
| 1531 | isn_T *isn; |
| 1532 | garray_T *stack = &cctx->ctx_type_stack; |
| 1533 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1534 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1535 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1536 | return FAIL; |
| 1537 | isn->isn_arg.jump.jump_when = when; |
| 1538 | isn->isn_arg.jump.jump_where = where; |
| 1539 | |
| 1540 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1541 | --stack->ga_len; |
| 1542 | |
| 1543 | return OK; |
| 1544 | } |
| 1545 | |
| 1546 | static int |
| 1547 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1548 | { |
| 1549 | isn_T *isn; |
| 1550 | garray_T *stack = &cctx->ctx_type_stack; |
| 1551 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1552 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1553 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1554 | return FAIL; |
| 1555 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1556 | |
| 1557 | if (ga_grow(stack, 1) == FAIL) |
| 1558 | return FAIL; |
| 1559 | // type doesn't matter, will be stored next |
| 1560 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1561 | ++stack->ga_len; |
| 1562 | |
| 1563 | return OK; |
| 1564 | } |
| 1565 | |
| 1566 | /* |
| 1567 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1568 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1569 | * Return FAIL if the number of arguments is wrong. |
| 1570 | */ |
| 1571 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1572 | generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1573 | { |
| 1574 | isn_T *isn; |
| 1575 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1576 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1577 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1578 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1579 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1580 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1581 | argoff = check_internal_func(func_idx, argcount); |
| 1582 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1583 | return FAIL; |
| 1584 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1585 | if (method_call && argoff > 1) |
| 1586 | { |
| 1587 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1588 | return FAIL; |
| 1589 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1590 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1591 | } |
| 1592 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1593 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1594 | return FAIL; |
| 1595 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1596 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1597 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1598 | for (i = 0; i < argcount; ++i) |
| 1599 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1600 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1601 | stack->ga_len -= argcount; // drop the arguments |
| 1602 | if (ga_grow(stack, 1) == FAIL) |
| 1603 | return FAIL; |
| 1604 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1605 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1606 | ++stack->ga_len; // add return value |
| 1607 | |
| 1608 | return OK; |
| 1609 | } |
| 1610 | |
| 1611 | /* |
| 1612 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1613 | * Return FAIL if the number of arguments is wrong. |
| 1614 | */ |
| 1615 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1616 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1617 | { |
| 1618 | isn_T *isn; |
| 1619 | garray_T *stack = &cctx->ctx_type_stack; |
| 1620 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1621 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1622 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1623 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1624 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1625 | { |
| 1626 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1627 | return FAIL; |
| 1628 | } |
| 1629 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1630 | { |
| 1631 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1632 | return FAIL; |
| 1633 | } |
| 1634 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1635 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1636 | { |
| 1637 | int i; |
| 1638 | |
| 1639 | for (i = 0; i < argcount; ++i) |
| 1640 | { |
| 1641 | type_T *expected; |
| 1642 | type_T *actual; |
| 1643 | |
| 1644 | if (i < regular_args) |
| 1645 | { |
| 1646 | if (ufunc->uf_arg_types == NULL) |
| 1647 | continue; |
| 1648 | expected = ufunc->uf_arg_types[i]; |
| 1649 | } |
| 1650 | else |
| 1651 | expected = ufunc->uf_va_type->tt_member; |
| 1652 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1653 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1654 | { |
| 1655 | arg_type_mismatch(expected, actual, i + 1); |
| 1656 | return FAIL; |
| 1657 | } |
| 1658 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1659 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 1660 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1661 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1662 | } |
| 1663 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1664 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1665 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1666 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1667 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1668 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1669 | { |
| 1670 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1671 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1672 | } |
| 1673 | else |
| 1674 | { |
| 1675 | // A user function may be deleted and redefined later, can't use the |
| 1676 | // ufunc pointer, need to look it up again at runtime. |
| 1677 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1678 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1679 | } |
| 1680 | |
| 1681 | stack->ga_len -= argcount; // drop the arguments |
| 1682 | if (ga_grow(stack, 1) == FAIL) |
| 1683 | return FAIL; |
| 1684 | // add return value |
| 1685 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1686 | ++stack->ga_len; |
| 1687 | |
| 1688 | return OK; |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1693 | */ |
| 1694 | static int |
| 1695 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1696 | { |
| 1697 | isn_T *isn; |
| 1698 | garray_T *stack = &cctx->ctx_type_stack; |
| 1699 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1700 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1701 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1702 | return FAIL; |
| 1703 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1704 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1705 | |
| 1706 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1707 | if (ga_grow(stack, 1) == FAIL) |
| 1708 | return FAIL; |
| 1709 | // add return value |
| 1710 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1711 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1712 | |
| 1713 | return OK; |
| 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1718 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1719 | */ |
| 1720 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1721 | generate_PCALL( |
| 1722 | cctx_T *cctx, |
| 1723 | int argcount, |
| 1724 | char_u *name, |
| 1725 | type_T *type, |
| 1726 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1727 | { |
| 1728 | isn_T *isn; |
| 1729 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1730 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1731 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1732 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1733 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1734 | if (type->tt_type == VAR_ANY) |
| 1735 | ret_type = &t_any; |
| 1736 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1737 | { |
| 1738 | if (type->tt_argcount != -1) |
| 1739 | { |
| 1740 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1741 | |
| 1742 | if (argcount < type->tt_min_argcount - varargs) |
| 1743 | { |
| 1744 | semsg(_(e_toofewarg), "[reference]"); |
| 1745 | return FAIL; |
| 1746 | } |
| 1747 | if (!varargs && argcount > type->tt_argcount) |
| 1748 | { |
| 1749 | semsg(_(e_toomanyarg), "[reference]"); |
| 1750 | return FAIL; |
| 1751 | } |
| 1752 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1753 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1754 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1755 | else |
| 1756 | { |
| 1757 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1758 | return FAIL; |
| 1759 | } |
| 1760 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1761 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1762 | return FAIL; |
| 1763 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1764 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1765 | |
| 1766 | stack->ga_len -= argcount; // drop the arguments |
| 1767 | |
| 1768 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1769 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1770 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1771 | // If partial is above the arguments it must be cleared and replaced with |
| 1772 | // the return value. |
| 1773 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1774 | return FAIL; |
| 1775 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1776 | return OK; |
| 1777 | } |
| 1778 | |
| 1779 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1780 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1781 | */ |
| 1782 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1783 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1784 | { |
| 1785 | isn_T *isn; |
| 1786 | garray_T *stack = &cctx->ctx_type_stack; |
| 1787 | type_T *type; |
| 1788 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1789 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1790 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1791 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1792 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1793 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1794 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1795 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1796 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1797 | { |
| 1798 | emsg(_(e_dictreq)); |
| 1799 | return FAIL; |
| 1800 | } |
| 1801 | // change dict type to dict member type |
| 1802 | if (type->tt_type == VAR_DICT) |
| 1803 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1804 | |
| 1805 | return OK; |
| 1806 | } |
| 1807 | |
| 1808 | /* |
| 1809 | * Generate an ISN_ECHO instruction. |
| 1810 | */ |
| 1811 | static int |
| 1812 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1813 | { |
| 1814 | isn_T *isn; |
| 1815 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1816 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1817 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1818 | return FAIL; |
| 1819 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1820 | isn->isn_arg.echo.echo_count = count; |
| 1821 | |
| 1822 | return OK; |
| 1823 | } |
| 1824 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1825 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1826 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1827 | */ |
| 1828 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1829 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1830 | { |
| 1831 | isn_T *isn; |
| 1832 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1833 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1834 | return FAIL; |
| 1835 | isn->isn_arg.number = count; |
| 1836 | |
| 1837 | return OK; |
| 1838 | } |
| 1839 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1840 | static int |
| 1841 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1842 | { |
| 1843 | isn_T *isn; |
| 1844 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1845 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1846 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1847 | return FAIL; |
| 1848 | isn->isn_arg.string = vim_strsave(line); |
| 1849 | return OK; |
| 1850 | } |
| 1851 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1852 | static int |
| 1853 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1854 | { |
| 1855 | isn_T *isn; |
| 1856 | |
| 1857 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1858 | return FAIL; |
| 1859 | isn->isn_arg.number = count; |
| 1860 | return OK; |
| 1861 | } |
| 1862 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1863 | /* |
| 1864 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1865 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1866 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1867 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1868 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1869 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1870 | lvar_T *lvar; |
| 1871 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1872 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1873 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1874 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1875 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1879 | return NULL; |
| 1880 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1881 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1882 | // Every local variable uses the next entry on the stack. We could re-use |
| 1883 | // the last ones when leaving a scope, but then variables used in a closure |
| 1884 | // might get overwritten. To keep things simple do not re-use stack |
| 1885 | // entries. This is less efficient, but memory is cheap these days. |
| 1886 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1887 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1888 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1889 | lvar->lv_const = isConst; |
| 1890 | lvar->lv_type = type; |
| 1891 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1892 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1896 | * Remove local variables above "new_top". |
| 1897 | */ |
| 1898 | static void |
| 1899 | unwind_locals(cctx_T *cctx, int new_top) |
| 1900 | { |
| 1901 | if (cctx->ctx_locals.ga_len > new_top) |
| 1902 | { |
| 1903 | int idx; |
| 1904 | lvar_T *lvar; |
| 1905 | |
| 1906 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1907 | { |
| 1908 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1909 | vim_free(lvar->lv_name); |
| 1910 | } |
| 1911 | } |
| 1912 | cctx->ctx_locals.ga_len = new_top; |
| 1913 | } |
| 1914 | |
| 1915 | /* |
| 1916 | * Free all local variables. |
| 1917 | */ |
| 1918 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1919 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1920 | { |
| 1921 | unwind_locals(cctx, 0); |
| 1922 | ga_clear(&cctx->ctx_locals); |
| 1923 | } |
| 1924 | |
| 1925 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1926 | * Skip over a type definition and return a pointer to just after it. |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame^] | 1927 | * When "optional" is TRUE then a leading "?" is accepted. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1928 | */ |
| 1929 | char_u * |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame^] | 1930 | skip_type(char_u *start, int optional) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1931 | { |
| 1932 | char_u *p = start; |
| 1933 | |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame^] | 1934 | if (optional && *p == '?') |
| 1935 | ++p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1936 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1937 | ++p; |
| 1938 | |
| 1939 | // Skip over "<type>"; this is permissive about white space. |
| 1940 | if (*skipwhite(p) == '<') |
| 1941 | { |
| 1942 | p = skipwhite(p); |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame^] | 1943 | p = skip_type(skipwhite(p + 1), FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1944 | p = skipwhite(p); |
| 1945 | if (*p == '>') |
| 1946 | ++p; |
| 1947 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1948 | else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1]))) |
| 1949 | && STRNCMP("func", start, 4) == 0) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1950 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1951 | if (*p == '(') |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1952 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1953 | // handle func(args): type |
| 1954 | ++p; |
| 1955 | while (*p != ')' && *p != NUL) |
| 1956 | { |
| 1957 | char_u *sp = p; |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1958 | |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame^] | 1959 | p = skip_type(p, TRUE); |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1960 | if (p == sp) |
| 1961 | return p; // syntax error |
| 1962 | if (*p == ',') |
| 1963 | p = skipwhite(p + 1); |
| 1964 | } |
| 1965 | if (*p == ')') |
| 1966 | { |
| 1967 | if (p[1] == ':') |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame^] | 1968 | p = skip_type(skipwhite(p + 2), FALSE); |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1969 | else |
Bram Moolenaar | bfba865 | 2020-07-23 20:09:10 +0200 | [diff] [blame] | 1970 | ++p; |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1971 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1972 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1973 | else |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1974 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1975 | // handle func: return_type |
Bram Moolenaar | 4fc224c | 2020-07-26 17:56:25 +0200 | [diff] [blame^] | 1976 | p = skip_type(skipwhite(p + 1), FALSE); |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1977 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1978 | } |
| 1979 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1980 | return p; |
| 1981 | } |
| 1982 | |
| 1983 | /* |
| 1984 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1985 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1986 | * Returns NULL in case of failure. |
| 1987 | */ |
| 1988 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1989 | 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] | 1990 | { |
| 1991 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1992 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1993 | |
| 1994 | if (**arg != '<') |
| 1995 | { |
| 1996 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1997 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1998 | else |
| 1999 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 2000 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2001 | } |
| 2002 | *arg = skipwhite(*arg + 1); |
| 2003 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2004 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2005 | |
| 2006 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2007 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2008 | { |
| 2009 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 2010 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2011 | } |
| 2012 | ++*arg; |
| 2013 | |
| 2014 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2015 | return get_list_type(member_type, type_gap); |
| 2016 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | /* |
| 2020 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 2021 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2022 | */ |
| 2023 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2024 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2025 | { |
| 2026 | char_u *p = *arg; |
| 2027 | size_t len; |
| 2028 | |
| 2029 | // skip over the first word |
| 2030 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 2031 | ++p; |
| 2032 | len = p - *arg; |
| 2033 | |
| 2034 | switch (**arg) |
| 2035 | { |
| 2036 | case 'a': |
| 2037 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 2038 | { |
| 2039 | *arg += len; |
| 2040 | return &t_any; |
| 2041 | } |
| 2042 | break; |
| 2043 | case 'b': |
| 2044 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 2045 | { |
| 2046 | *arg += len; |
| 2047 | return &t_bool; |
| 2048 | } |
| 2049 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 2050 | { |
| 2051 | *arg += len; |
| 2052 | return &t_blob; |
| 2053 | } |
| 2054 | break; |
| 2055 | case 'c': |
| 2056 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 2057 | { |
| 2058 | *arg += len; |
| 2059 | return &t_channel; |
| 2060 | } |
| 2061 | break; |
| 2062 | case 'd': |
| 2063 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 2064 | { |
| 2065 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2066 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2067 | } |
| 2068 | break; |
| 2069 | case 'f': |
| 2070 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 2071 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2072 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2073 | *arg += len; |
| 2074 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2075 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2076 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2077 | return &t_any; |
| 2078 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2079 | } |
| 2080 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 2081 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2082 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2083 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2084 | int argcount = -1; |
| 2085 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2086 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2087 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 2088 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2089 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2090 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2091 | if (**arg == '(') |
| 2092 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2093 | // "func" may or may not return a value, "func()" does |
| 2094 | // not return a value. |
| 2095 | ret_type = &t_void; |
| 2096 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2097 | p = ++*arg; |
| 2098 | argcount = 0; |
| 2099 | while (*p != NUL && *p != ')') |
| 2100 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2101 | if (*p == '?') |
| 2102 | { |
| 2103 | if (first_optional == -1) |
| 2104 | first_optional = argcount; |
| 2105 | ++p; |
| 2106 | } |
| 2107 | else if (first_optional != -1) |
| 2108 | { |
| 2109 | emsg(_("E1007: mandatory argument after optional argument")); |
| 2110 | return &t_any; |
| 2111 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2112 | else if (STRNCMP(p, "...", 3) == 0) |
| 2113 | { |
| 2114 | flags |= TTFLAG_VARARGS; |
| 2115 | p += 3; |
| 2116 | } |
| 2117 | |
| 2118 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 2119 | |
| 2120 | // Nothing comes after "...{type}". |
| 2121 | if (flags & TTFLAG_VARARGS) |
| 2122 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2123 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2124 | if (*p != ',' && *skipwhite(p) == ',') |
| 2125 | { |
| 2126 | semsg(_(e_no_white_before), ","); |
| 2127 | return &t_any; |
| 2128 | } |
| 2129 | if (*p == ',') |
| 2130 | { |
| 2131 | ++p; |
| 2132 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2133 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2134 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2135 | return &t_any; |
| 2136 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2137 | } |
| 2138 | p = skipwhite(p); |
| 2139 | if (argcount == MAX_FUNC_ARGS) |
| 2140 | { |
| 2141 | emsg(_("E740: Too many argument types")); |
| 2142 | return &t_any; |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | p = skipwhite(p); |
| 2147 | if (*p != ')') |
| 2148 | { |
| 2149 | emsg(_(e_missing_close)); |
| 2150 | return &t_any; |
| 2151 | } |
| 2152 | *arg = p + 1; |
| 2153 | } |
| 2154 | if (**arg == ':') |
| 2155 | { |
| 2156 | // parse return type |
| 2157 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2158 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2159 | semsg(_(e_white_after), ":"); |
| 2160 | *arg = skipwhite(*arg); |
| 2161 | ret_type = parse_type(arg, type_gap); |
| 2162 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2163 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2164 | type = get_func_type(ret_type, argcount, type_gap); |
| 2165 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2166 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2167 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 2168 | type->tt_flags = flags; |
| 2169 | if (argcount > 0) |
| 2170 | { |
| 2171 | type->tt_argcount = argcount; |
| 2172 | type->tt_min_argcount = first_optional == -1 |
| 2173 | ? argcount : first_optional; |
| 2174 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2175 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2176 | return &t_any; |
| 2177 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2178 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2179 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2180 | } |
| 2181 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2182 | } |
| 2183 | break; |
| 2184 | case 'j': |
| 2185 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 2186 | { |
| 2187 | *arg += len; |
| 2188 | return &t_job; |
| 2189 | } |
| 2190 | break; |
| 2191 | case 'l': |
| 2192 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 2193 | { |
| 2194 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2195 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2196 | } |
| 2197 | break; |
| 2198 | case 'n': |
| 2199 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 2200 | { |
| 2201 | *arg += len; |
| 2202 | return &t_number; |
| 2203 | } |
| 2204 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2205 | case 's': |
| 2206 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 2207 | { |
| 2208 | *arg += len; |
| 2209 | return &t_string; |
| 2210 | } |
| 2211 | break; |
| 2212 | case 'v': |
| 2213 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 2214 | { |
| 2215 | *arg += len; |
| 2216 | return &t_void; |
| 2217 | } |
| 2218 | break; |
| 2219 | } |
| 2220 | |
| 2221 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 2222 | return &t_any; |
| 2223 | } |
| 2224 | |
| 2225 | /* |
| 2226 | * Check if "type1" and "type2" are exactly the same. |
| 2227 | */ |
| 2228 | static int |
| 2229 | equal_type(type_T *type1, type_T *type2) |
| 2230 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2231 | int i; |
| 2232 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2233 | if (type1->tt_type != type2->tt_type) |
| 2234 | return FALSE; |
| 2235 | switch (type1->tt_type) |
| 2236 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2237 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2238 | case VAR_ANY: |
| 2239 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2240 | case VAR_SPECIAL: |
| 2241 | case VAR_BOOL: |
| 2242 | case VAR_NUMBER: |
| 2243 | case VAR_FLOAT: |
| 2244 | case VAR_STRING: |
| 2245 | case VAR_BLOB: |
| 2246 | case VAR_JOB: |
| 2247 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2248 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2249 | case VAR_LIST: |
| 2250 | case VAR_DICT: |
| 2251 | return equal_type(type1->tt_member, type2->tt_member); |
| 2252 | case VAR_FUNC: |
| 2253 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2254 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2255 | || type1->tt_argcount != type2->tt_argcount) |
| 2256 | return FALSE; |
| 2257 | if (type1->tt_argcount < 0 |
| 2258 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2259 | return TRUE; |
| 2260 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2261 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2262 | return FALSE; |
| 2263 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2264 | } |
| 2265 | return TRUE; |
| 2266 | } |
| 2267 | |
| 2268 | /* |
| 2269 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2270 | * "type2" and "dest" may be the same. |
| 2271 | */ |
| 2272 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2273 | 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] | 2274 | { |
| 2275 | if (equal_type(type1, type2)) |
| 2276 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2277 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2278 | return; |
| 2279 | } |
| 2280 | |
| 2281 | if (type1->tt_type == type2->tt_type) |
| 2282 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2283 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2284 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2285 | type_T *common; |
| 2286 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2287 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2288 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2289 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2290 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2291 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2292 | return; |
| 2293 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2294 | if (type1->tt_type == VAR_FUNC) |
| 2295 | { |
| 2296 | type_T *common; |
| 2297 | |
| 2298 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2299 | if (type1->tt_argcount == type2->tt_argcount |
| 2300 | && type1->tt_argcount >= 0) |
| 2301 | { |
| 2302 | int argcount = type1->tt_argcount; |
| 2303 | int i; |
| 2304 | |
| 2305 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2306 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2307 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2308 | if (func_type_add_arg_types(*dest, argcount, |
| 2309 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2310 | for (i = 0; i < argcount; ++i) |
| 2311 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2312 | &(*dest)->tt_args[i], type_gap); |
| 2313 | } |
| 2314 | } |
| 2315 | else |
| 2316 | *dest = alloc_func_type(common, -1, type_gap); |
| 2317 | return; |
| 2318 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2319 | } |
| 2320 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2321 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | char * |
| 2325 | vartype_name(vartype_T type) |
| 2326 | { |
| 2327 | switch (type) |
| 2328 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2329 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2330 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2331 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2332 | case VAR_SPECIAL: return "special"; |
| 2333 | case VAR_BOOL: return "bool"; |
| 2334 | case VAR_NUMBER: return "number"; |
| 2335 | case VAR_FLOAT: return "float"; |
| 2336 | case VAR_STRING: return "string"; |
| 2337 | case VAR_BLOB: return "blob"; |
| 2338 | case VAR_JOB: return "job"; |
| 2339 | case VAR_CHANNEL: return "channel"; |
| 2340 | case VAR_LIST: return "list"; |
| 2341 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2342 | |
| 2343 | case VAR_FUNC: |
| 2344 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2345 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2346 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | /* |
| 2350 | * Return the name of a type. |
| 2351 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2352 | */ |
| 2353 | char * |
| 2354 | type_name(type_T *type, char **tofree) |
| 2355 | { |
| 2356 | char *name = vartype_name(type->tt_type); |
| 2357 | |
| 2358 | *tofree = NULL; |
| 2359 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2360 | { |
| 2361 | char *member_free; |
| 2362 | char *member_name = type_name(type->tt_member, &member_free); |
| 2363 | size_t len; |
| 2364 | |
| 2365 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2366 | *tofree = alloc(len); |
| 2367 | if (*tofree != NULL) |
| 2368 | { |
| 2369 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2370 | vim_free(member_free); |
| 2371 | return *tofree; |
| 2372 | } |
| 2373 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2374 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2375 | { |
| 2376 | garray_T ga; |
| 2377 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2378 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2379 | |
| 2380 | ga_init2(&ga, 1, 100); |
| 2381 | if (ga_grow(&ga, 20) == FAIL) |
| 2382 | return "[unknown]"; |
| 2383 | *tofree = ga.ga_data; |
| 2384 | STRCPY(ga.ga_data, "func("); |
| 2385 | ga.ga_len += 5; |
| 2386 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2387 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2388 | { |
| 2389 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2390 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2391 | int len; |
| 2392 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2393 | if (type->tt_args == NULL) |
| 2394 | arg_type = "[unknown]"; |
| 2395 | else |
| 2396 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2397 | if (i > 0) |
| 2398 | { |
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 | ga.ga_len += 2; |
| 2401 | } |
| 2402 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2403 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2404 | { |
| 2405 | vim_free(arg_free); |
| 2406 | return "[unknown]"; |
| 2407 | } |
| 2408 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2409 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2410 | { |
| 2411 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2412 | ga.ga_len += 3; |
| 2413 | } |
| 2414 | else if (i >= type->tt_min_argcount) |
| 2415 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2416 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2417 | ga.ga_len += len; |
| 2418 | vim_free(arg_free); |
| 2419 | } |
| 2420 | |
| 2421 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2422 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2423 | else |
| 2424 | { |
| 2425 | char *ret_free; |
| 2426 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2427 | int len; |
| 2428 | |
| 2429 | len = (int)STRLEN(ret_name) + 4; |
| 2430 | if (ga_grow(&ga, len) == FAIL) |
| 2431 | { |
| 2432 | vim_free(ret_free); |
| 2433 | return "[unknown]"; |
| 2434 | } |
| 2435 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2436 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2437 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2438 | vim_free(ret_free); |
| 2439 | } |
| 2440 | return ga.ga_data; |
| 2441 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2442 | |
| 2443 | return name; |
| 2444 | } |
| 2445 | |
| 2446 | /* |
| 2447 | * Find "name" in script-local items of script "sid". |
| 2448 | * Returns the index in "sn_var_vals" if found. |
| 2449 | * If found but not in "sn_var_vals" returns -1. |
| 2450 | * If not found returns -2. |
| 2451 | */ |
| 2452 | int |
| 2453 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2454 | { |
| 2455 | hashtab_T *ht; |
| 2456 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2457 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2458 | int idx; |
| 2459 | |
| 2460 | // First look the name up in the hashtable. |
| 2461 | if (sid <= 0 || sid > script_items.ga_len) |
| 2462 | return -1; |
| 2463 | ht = &SCRIPT_VARS(sid); |
| 2464 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2465 | if (di == NULL) |
| 2466 | return -2; |
| 2467 | |
| 2468 | // Now find the svar_T index in sn_var_vals. |
| 2469 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2470 | { |
| 2471 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2472 | |
| 2473 | if (sv->sv_tv == &di->di_tv) |
| 2474 | { |
| 2475 | if (check_writable && sv->sv_const) |
| 2476 | semsg(_(e_readonlyvar), name); |
| 2477 | return idx; |
| 2478 | } |
| 2479 | } |
| 2480 | return -1; |
| 2481 | } |
| 2482 | |
| 2483 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2484 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2485 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2486 | */ |
| 2487 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2488 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2489 | { |
Bram Moolenaar | 086eb18 | 2020-07-01 16:00:44 +0200 | [diff] [blame] | 2490 | scriptitem_T *si; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2491 | int idx; |
| 2492 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2493 | if (current_sctx.sc_sid <= 0) |
| 2494 | return NULL; |
| 2495 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2496 | if (cctx != NULL) |
| 2497 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2498 | { |
| 2499 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2500 | + idx; |
| 2501 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2502 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2503 | : STRLEN(import->imp_name) == len |
| 2504 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2505 | return import; |
| 2506 | } |
| 2507 | |
| 2508 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2509 | { |
| 2510 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2511 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2512 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2513 | : STRLEN(import->imp_name) == len |
| 2514 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2515 | return import; |
| 2516 | } |
| 2517 | return NULL; |
| 2518 | } |
| 2519 | |
| 2520 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2521 | * Free all imported variables. |
| 2522 | */ |
| 2523 | static void |
| 2524 | free_imported(cctx_T *cctx) |
| 2525 | { |
| 2526 | int idx; |
| 2527 | |
| 2528 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2529 | { |
| 2530 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2531 | |
| 2532 | vim_free(import->imp_name); |
| 2533 | } |
| 2534 | ga_clear(&cctx->ctx_imports); |
| 2535 | } |
| 2536 | |
| 2537 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2538 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2539 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 2540 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2541 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2542 | { |
| 2543 | return p[0] == '#' && p[1] != '{'; |
| 2544 | } |
| 2545 | |
| 2546 | /* |
| 2547 | * Return a pointer to the next line that isn't empty or only contains a |
| 2548 | * comment. Skips over white space. |
| 2549 | * Returns NULL if there is none. |
| 2550 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2551 | char_u * |
| 2552 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2553 | { |
| 2554 | int lnum = cctx->ctx_lnum; |
| 2555 | |
| 2556 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2557 | { |
| 2558 | 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] | 2559 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2560 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 2561 | if (line == NULL) |
| 2562 | break; |
| 2563 | p = skipwhite(line); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2564 | if (*p != NUL && !vim9_comment_start(p)) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2565 | return p; |
| 2566 | } |
| 2567 | return NULL; |
| 2568 | } |
| 2569 | |
| 2570 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2571 | * Called when checking for a following operator at "arg". When the rest of |
| 2572 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2573 | * line return a pointer to it and set "nextp". |
| 2574 | * Otherwise skip over white space. |
| 2575 | */ |
| 2576 | static char_u * |
| 2577 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2578 | { |
| 2579 | char_u *p = skipwhite(arg); |
| 2580 | |
| 2581 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2582 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2583 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2584 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2585 | if (*nextp != NULL) |
| 2586 | return *nextp; |
| 2587 | } |
| 2588 | return p; |
| 2589 | } |
| 2590 | |
| 2591 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2592 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2593 | * 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] | 2594 | * Returns NULL when at the end. |
| 2595 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2596 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2597 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2598 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2599 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2600 | |
| 2601 | do |
| 2602 | { |
| 2603 | ++cctx->ctx_lnum; |
| 2604 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2605 | { |
| 2606 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2607 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2608 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2609 | 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] | 2610 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2611 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2612 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2613 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2614 | return line; |
| 2615 | } |
| 2616 | |
| 2617 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2618 | * 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] | 2619 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2620 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2621 | */ |
| 2622 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2623 | 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] | 2624 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2625 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2626 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2627 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2628 | |
| 2629 | if (next == NULL) |
| 2630 | return FAIL; |
| 2631 | *arg = skipwhite(next); |
| 2632 | } |
| 2633 | return OK; |
| 2634 | } |
| 2635 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2636 | /* |
| 2637 | * Idem, and give an error when failed. |
| 2638 | */ |
| 2639 | static int |
| 2640 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2641 | { |
| 2642 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2643 | { |
| 2644 | emsg(_("E1097: line incomplete")); |
| 2645 | return FAIL; |
| 2646 | } |
| 2647 | return OK; |
| 2648 | } |
| 2649 | |
| 2650 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2651 | // Structure passed between the compile_expr* functions to keep track of |
| 2652 | // constants that have been parsed but for which no code was produced yet. If |
| 2653 | // possible expressions on these constants are applied at compile time. If |
| 2654 | // that is not possible, the code to push the constants needs to be generated |
| 2655 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2656 | // Using 50 should be more than enough of 5 levels of (). |
| 2657 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2658 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2659 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2660 | int pp_used; // active entries in pp_tv[] |
| 2661 | } ppconst_T; |
| 2662 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2663 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2664 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2665 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2666 | /* |
| 2667 | * Generate a PUSH instruction for "tv". |
| 2668 | * "tv" will be consumed or cleared. |
| 2669 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2670 | */ |
| 2671 | static int |
| 2672 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2673 | { |
| 2674 | if (tv != NULL) |
| 2675 | { |
| 2676 | switch (tv->v_type) |
| 2677 | { |
| 2678 | case VAR_UNKNOWN: |
| 2679 | break; |
| 2680 | case VAR_BOOL: |
| 2681 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2682 | break; |
| 2683 | case VAR_SPECIAL: |
| 2684 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2685 | break; |
| 2686 | case VAR_NUMBER: |
| 2687 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2688 | break; |
| 2689 | #ifdef FEAT_FLOAT |
| 2690 | case VAR_FLOAT: |
| 2691 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2692 | break; |
| 2693 | #endif |
| 2694 | case VAR_BLOB: |
| 2695 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2696 | tv->vval.v_blob = NULL; |
| 2697 | break; |
| 2698 | case VAR_STRING: |
| 2699 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2700 | tv->vval.v_string = NULL; |
| 2701 | break; |
| 2702 | default: |
| 2703 | iemsg("constant type not supported"); |
| 2704 | clear_tv(tv); |
| 2705 | return FAIL; |
| 2706 | } |
| 2707 | tv->v_type = VAR_UNKNOWN; |
| 2708 | } |
| 2709 | return OK; |
| 2710 | } |
| 2711 | |
| 2712 | /* |
| 2713 | * Generate code for any ppconst entries. |
| 2714 | */ |
| 2715 | static int |
| 2716 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2717 | { |
| 2718 | int i; |
| 2719 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2720 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2721 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2722 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2723 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2724 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2725 | ret = FAIL; |
| 2726 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2727 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2728 | return ret; |
| 2729 | } |
| 2730 | |
| 2731 | /* |
| 2732 | * Clear ppconst constants. Used when failing. |
| 2733 | */ |
| 2734 | static void |
| 2735 | clear_ppconst(ppconst_T *ppconst) |
| 2736 | { |
| 2737 | int i; |
| 2738 | |
| 2739 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2740 | clear_tv(&ppconst->pp_tv[i]); |
| 2741 | ppconst->pp_used = 0; |
| 2742 | } |
| 2743 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2744 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2745 | * Generate an instruction to load script-local variable "name", without the |
| 2746 | * leading "s:". |
| 2747 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2748 | */ |
| 2749 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2750 | compile_load_scriptvar( |
| 2751 | cctx_T *cctx, |
| 2752 | char_u *name, // variable NUL terminated |
| 2753 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2754 | char_u **end, // end of variable |
| 2755 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2756 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2757 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2758 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2759 | imported_T *import; |
| 2760 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2761 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2762 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2763 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2764 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2765 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2766 | } |
| 2767 | if (idx >= 0) |
| 2768 | { |
| 2769 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2770 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2771 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2772 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2773 | return OK; |
| 2774 | } |
| 2775 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2776 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2777 | if (import != NULL) |
| 2778 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2779 | if (import->imp_all) |
| 2780 | { |
| 2781 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2782 | char_u *exp_name; |
| 2783 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2784 | ufunc_T *ufunc; |
| 2785 | type_T *type; |
| 2786 | |
| 2787 | // Used "import * as Name", need to lookup the member. |
| 2788 | if (*p != '.') |
| 2789 | { |
| 2790 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2791 | return FAIL; |
| 2792 | } |
| 2793 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2794 | if (VIM_ISWHITE(*p)) |
| 2795 | { |
| 2796 | emsg(_("E1074: no white space allowed after dot")); |
| 2797 | return FAIL; |
| 2798 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2799 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2800 | // isolate one name |
| 2801 | exp_name = p; |
| 2802 | while (eval_isnamec(*p)) |
| 2803 | ++p; |
| 2804 | cc = *p; |
| 2805 | *p = NUL; |
| 2806 | |
| 2807 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2808 | *p = cc; |
| 2809 | p = skipwhite(p); |
| 2810 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2811 | // TODO: what if it is a function? |
| 2812 | if (idx < 0) |
| 2813 | return FAIL; |
| 2814 | *end = p; |
| 2815 | |
| 2816 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2817 | import->imp_sid, |
| 2818 | idx, |
| 2819 | type); |
| 2820 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2821 | else if (import->imp_funcname != NULL) |
| 2822 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2823 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2824 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2825 | import->imp_sid, |
| 2826 | import->imp_var_vals_idx, |
| 2827 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2828 | return OK; |
| 2829 | } |
| 2830 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2831 | if (error) |
| 2832 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2833 | return FAIL; |
| 2834 | } |
| 2835 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2836 | static int |
| 2837 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2838 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2839 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2840 | |
| 2841 | if (ufunc == NULL) |
| 2842 | return FAIL; |
| 2843 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2844 | // Need to compile any default values to get the argument types. |
| 2845 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2846 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2847 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2848 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2849 | } |
| 2850 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2851 | /* |
| 2852 | * Compile a variable name into a load instruction. |
| 2853 | * "end" points to just after the name. |
| 2854 | * When "error" is FALSE do not give an error when not found. |
| 2855 | */ |
| 2856 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2857 | 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] | 2858 | { |
| 2859 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2860 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2861 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2862 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2863 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2864 | |
| 2865 | if (*(*arg + 1) == ':') |
| 2866 | { |
| 2867 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2868 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2869 | { |
| 2870 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2871 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2872 | switch (**arg) |
| 2873 | { |
| 2874 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2875 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2876 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2877 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2878 | default: |
| 2879 | semsg(_(e_namespace), *arg); |
| 2880 | goto theend; |
| 2881 | } |
| 2882 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2883 | goto theend; |
| 2884 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2885 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2886 | else |
| 2887 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2888 | isntype_T isn_type = ISN_DROP; |
| 2889 | |
| 2890 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2891 | if (name == NULL) |
| 2892 | return FAIL; |
| 2893 | |
| 2894 | switch (**arg) |
| 2895 | { |
| 2896 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2897 | break; |
| 2898 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2899 | NULL, NULL, error); |
| 2900 | break; |
| 2901 | case 'g': isn_type = ISN_LOADG; break; |
| 2902 | case 'w': isn_type = ISN_LOADW; break; |
| 2903 | case 't': isn_type = ISN_LOADT; break; |
| 2904 | case 'b': isn_type = ISN_LOADB; break; |
| 2905 | default: semsg(_(e_namespace), *arg); |
| 2906 | goto theend; |
| 2907 | } |
| 2908 | if (isn_type != ISN_DROP) |
| 2909 | { |
| 2910 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2911 | // variables can be defined later, thus we don't check if it |
| 2912 | // exists, give error at runtime. |
| 2913 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2914 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2915 | } |
| 2916 | } |
| 2917 | else |
| 2918 | { |
| 2919 | size_t len = end - *arg; |
| 2920 | int idx; |
| 2921 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2922 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2923 | |
| 2924 | name = vim_strnsave(*arg, end - *arg); |
| 2925 | if (name == NULL) |
| 2926 | return FAIL; |
| 2927 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2928 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2929 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2930 | if (!gen_load_outer) |
| 2931 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2932 | } |
| 2933 | else |
| 2934 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2935 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2936 | |
| 2937 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2938 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2939 | type = lvar->lv_type; |
| 2940 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2941 | if (lvar->lv_from_outer) |
| 2942 | gen_load_outer = TRUE; |
| 2943 | else |
| 2944 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2945 | } |
| 2946 | else |
| 2947 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2948 | // "var" can be script-local even without using "s:" if it |
| 2949 | // already exists. |
| 2950 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2951 | == SCRIPT_VERSION_VIM9 |
| 2952 | || lookup_script(*arg, len) == OK) |
| 2953 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2954 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2955 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2956 | // When the name starts with an uppercase letter or "x:" it |
| 2957 | // can be a user defined function. |
| 2958 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2959 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2960 | } |
| 2961 | } |
| 2962 | if (gen_load) |
| 2963 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2964 | if (gen_load_outer) |
| 2965 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2966 | } |
| 2967 | |
| 2968 | *arg = end; |
| 2969 | |
| 2970 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2971 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2972 | semsg(_(e_var_notfound), name); |
| 2973 | vim_free(name); |
| 2974 | return res; |
| 2975 | } |
| 2976 | |
| 2977 | /* |
| 2978 | * Compile the argument expressions. |
| 2979 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2980 | */ |
| 2981 | static int |
| 2982 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2983 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2984 | char_u *p = *arg; |
| 2985 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2986 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2987 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2988 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2989 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2990 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2991 | if (*p == ')') |
| 2992 | { |
| 2993 | *arg = p + 1; |
| 2994 | return OK; |
| 2995 | } |
| 2996 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2997 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2998 | return FAIL; |
| 2999 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3000 | |
| 3001 | if (*p != ',' && *skipwhite(p) == ',') |
| 3002 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 3003 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3004 | p = skipwhite(p); |
| 3005 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3006 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3007 | { |
| 3008 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 3009 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 3010 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3011 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3012 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3013 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3014 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3015 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 3016 | emsg(_(e_missing_close)); |
| 3017 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | /* |
| 3021 | * Compile a function call: name(arg1, arg2) |
| 3022 | * "arg" points to "name", "arg + varlen" to the "(". |
| 3023 | * "argcount_init" is 1 for "value->method()" |
| 3024 | * Instructions: |
| 3025 | * EVAL arg1 |
| 3026 | * EVAL arg2 |
| 3027 | * BCALL / DCALL / UCALL |
| 3028 | */ |
| 3029 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3030 | compile_call( |
| 3031 | char_u **arg, |
| 3032 | size_t varlen, |
| 3033 | cctx_T *cctx, |
| 3034 | ppconst_T *ppconst, |
| 3035 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3036 | { |
| 3037 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 3038 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3039 | int argcount = argcount_init; |
| 3040 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3041 | char_u fname_buf[FLEN_FIXED + 1]; |
| 3042 | char_u *tofree = NULL; |
| 3043 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3044 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3045 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3046 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3047 | // we can evaluate "has('name')" at compile time |
| 3048 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 3049 | { |
| 3050 | char_u *s = skipwhite(*arg + varlen + 1); |
| 3051 | typval_T argvars[2]; |
| 3052 | |
| 3053 | argvars[0].v_type = VAR_UNKNOWN; |
| 3054 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3055 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3056 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3057 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3058 | s = skipwhite(s); |
| 3059 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 3060 | { |
| 3061 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 3062 | |
| 3063 | *arg = s + 1; |
| 3064 | argvars[1].v_type = VAR_UNKNOWN; |
| 3065 | tv->v_type = VAR_NUMBER; |
| 3066 | tv->vval.v_number = 0; |
| 3067 | f_has(argvars, tv); |
| 3068 | clear_tv(&argvars[0]); |
| 3069 | ++ppconst->pp_used; |
| 3070 | return OK; |
| 3071 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 3072 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3076 | return FAIL; |
| 3077 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3078 | if (varlen >= sizeof(namebuf)) |
| 3079 | { |
| 3080 | semsg(_("E1011: name too long: %s"), name); |
| 3081 | return FAIL; |
| 3082 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3083 | vim_strncpy(namebuf, *arg, varlen); |
| 3084 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3085 | |
| 3086 | *arg = skipwhite(*arg + varlen + 1); |
| 3087 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3088 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3089 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3090 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3091 | { |
| 3092 | int idx; |
| 3093 | |
| 3094 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3095 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3096 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 3097 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3098 | else |
| 3099 | semsg(_(e_unknownfunc), namebuf); |
| 3100 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3101 | } |
| 3102 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3103 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 3104 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3105 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3106 | { |
| 3107 | res = generate_CALL(cctx, ufunc, argcount); |
| 3108 | goto theend; |
| 3109 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3110 | |
| 3111 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3112 | // 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] | 3113 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3114 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 3115 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3116 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3117 | garray_T *stack = &cctx->ctx_type_stack; |
| 3118 | type_T *type; |
| 3119 | |
| 3120 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3121 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3122 | goto theend; |
| 3123 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3124 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3125 | // 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] | 3126 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3127 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 3128 | res = generate_UCALL(cctx, name, argcount); |
| 3129 | else |
| 3130 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3131 | |
| 3132 | theend: |
| 3133 | vim_free(tofree); |
| 3134 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 3138 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 3139 | |
| 3140 | /* |
| 3141 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 3142 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3143 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3144 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 3145 | * valid name. |
| 3146 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3147 | static char_u * |
| 3148 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3149 | { |
| 3150 | char_u *p; |
| 3151 | |
| 3152 | // Quick check for valid starting character. |
| 3153 | if (!eval_isnamec1(*arg)) |
| 3154 | return arg; |
| 3155 | |
| 3156 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 3157 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 3158 | // and can be used in slice "[n:]". |
| 3159 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3160 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3161 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 3162 | break; |
| 3163 | return p; |
| 3164 | } |
| 3165 | |
| 3166 | /* |
| 3167 | * 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] | 3168 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3169 | */ |
| 3170 | char_u * |
| 3171 | to_name_const_end(char_u *arg) |
| 3172 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3173 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3174 | typval_T rettv; |
| 3175 | |
| 3176 | if (p == arg && *arg == '[') |
| 3177 | { |
| 3178 | |
| 3179 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3180 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3181 | p = arg; |
| 3182 | } |
| 3183 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 3184 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3185 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3186 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3187 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3188 | p = arg; |
| 3189 | } |
| 3190 | else if (p == arg && *arg == '{') |
| 3191 | { |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3192 | int ret = get_lambda_tv(&p, &rettv, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3193 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3194 | // Can be "{x -> ret}()". |
| 3195 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3196 | if (ret == NOTDONE) |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3197 | ret = eval_dict(&p, &rettv, NULL, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3198 | if (ret != OK) |
| 3199 | p = arg; |
| 3200 | } |
| 3201 | |
| 3202 | return p; |
| 3203 | } |
| 3204 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3205 | /* |
| 3206 | * parse a list: [expr, expr] |
| 3207 | * "*arg" points to the '['. |
| 3208 | */ |
| 3209 | static int |
| 3210 | compile_list(char_u **arg, cctx_T *cctx) |
| 3211 | { |
| 3212 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3213 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3214 | int count = 0; |
| 3215 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3216 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3217 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3218 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3219 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3220 | semsg(_(e_list_end), *arg); |
| 3221 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3222 | } |
| 3223 | if (*p == ']') |
| 3224 | { |
| 3225 | ++p; |
| 3226 | // Allow for following comment, after at least one space. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3227 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '#') |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3228 | p += STRLEN(p); |
| 3229 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3230 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3231 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3232 | break; |
| 3233 | ++count; |
| 3234 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3235 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3236 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3237 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 3238 | { |
| 3239 | semsg(_(e_white_after), ","); |
| 3240 | return FAIL; |
| 3241 | } |
| 3242 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3243 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3244 | p = skipwhite(p); |
| 3245 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3246 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3247 | |
| 3248 | generate_NEWLIST(cctx, count); |
| 3249 | return OK; |
| 3250 | } |
| 3251 | |
| 3252 | /* |
| 3253 | * parse a lambda: {arg, arg -> expr} |
| 3254 | * "*arg" points to the '{'. |
| 3255 | */ |
| 3256 | static int |
| 3257 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 3258 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3259 | typval_T rettv; |
| 3260 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3261 | evalarg_T evalarg; |
| 3262 | |
| 3263 | CLEAR_FIELD(evalarg); |
| 3264 | evalarg.eval_flags = EVAL_EVALUATE; |
| 3265 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3266 | |
| 3267 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3268 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3269 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3270 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3271 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3272 | ++ufunc->uf_refcount; |
| 3273 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3274 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3275 | |
| 3276 | // The function will have one line: "return {expr}". |
| 3277 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3278 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3279 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 3280 | clear_evalarg(&evalarg, NULL); |
| 3281 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 3282 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 3283 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3284 | |
| 3285 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3286 | return FAIL; |
| 3287 | } |
| 3288 | |
| 3289 | /* |
| 3290 | * Compile a lamda call: expr->{lambda}(args) |
| 3291 | * "arg" points to the "{". |
| 3292 | */ |
| 3293 | static int |
| 3294 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 3295 | { |
| 3296 | ufunc_T *ufunc; |
| 3297 | typval_T rettv; |
| 3298 | int argcount = 1; |
| 3299 | int ret = FAIL; |
| 3300 | |
| 3301 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3302 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3303 | return FAIL; |
| 3304 | |
| 3305 | if (**arg != '(') |
| 3306 | { |
| 3307 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 3308 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3309 | else |
| 3310 | semsg(_(e_missing_paren), "lambda"); |
| 3311 | clear_tv(&rettv); |
| 3312 | return FAIL; |
| 3313 | } |
| 3314 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3315 | ufunc = rettv.vval.v_partial->pt_func; |
| 3316 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3317 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3318 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3319 | |
| 3320 | // The function will have one line: "return {expr}". |
| 3321 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3322 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3323 | |
| 3324 | // compile the arguments |
| 3325 | *arg = skipwhite(*arg + 1); |
| 3326 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 3327 | // call the compiled function |
| 3328 | ret = generate_CALL(cctx, ufunc, argcount); |
| 3329 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3330 | if (ret == FAIL) |
| 3331 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3332 | return ret; |
| 3333 | } |
| 3334 | |
| 3335 | /* |
| 3336 | * parse a dict: {'key': val} or #{key: val} |
| 3337 | * "*arg" points to the '{'. |
| 3338 | */ |
| 3339 | static int |
| 3340 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 3341 | { |
| 3342 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3343 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3344 | int count = 0; |
| 3345 | dict_T *d = dict_alloc(); |
| 3346 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3347 | char_u *whitep = *arg; |
| 3348 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3349 | |
| 3350 | if (d == NULL) |
| 3351 | return FAIL; |
| 3352 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3353 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3354 | { |
| 3355 | char_u *key = NULL; |
| 3356 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3357 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3358 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3359 | *arg = NULL; |
| 3360 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | if (**arg == '}') |
| 3364 | break; |
| 3365 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3366 | if (literal) |
| 3367 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3368 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3369 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3370 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3371 | { |
| 3372 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 3373 | return FAIL; |
| 3374 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3375 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3376 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3377 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3378 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3379 | } |
| 3380 | else |
| 3381 | { |
| 3382 | isn_T *isn; |
| 3383 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3384 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3385 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3386 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3387 | if (isn->isn_type == ISN_PUSHS) |
| 3388 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3389 | else |
| 3390 | { |
| 3391 | type_T *keytype = ((type_T **)stack->ga_data) |
| 3392 | [stack->ga_len - 1]; |
| 3393 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 3394 | return FAIL; |
| 3395 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | // Check for duplicate keys, if using string keys. |
| 3399 | if (key != NULL) |
| 3400 | { |
| 3401 | item = dict_find(d, key, -1); |
| 3402 | if (item != NULL) |
| 3403 | { |
| 3404 | semsg(_(e_duplicate_key), key); |
| 3405 | goto failret; |
| 3406 | } |
| 3407 | item = dictitem_alloc(key); |
| 3408 | if (item != NULL) |
| 3409 | { |
| 3410 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3411 | item->di_tv.v_lock = 0; |
| 3412 | if (dict_add(d, item) == FAIL) |
| 3413 | dictitem_free(item); |
| 3414 | } |
| 3415 | } |
| 3416 | |
| 3417 | *arg = skipwhite(*arg); |
| 3418 | if (**arg != ':') |
| 3419 | { |
| 3420 | semsg(_(e_missing_dict_colon), *arg); |
| 3421 | return FAIL; |
| 3422 | } |
| 3423 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3424 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3425 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3426 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3427 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3428 | *arg = NULL; |
| 3429 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3430 | } |
| 3431 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3432 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3433 | return FAIL; |
| 3434 | ++count; |
| 3435 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3436 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3437 | *arg = skipwhite(*arg); |
| 3438 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3439 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3440 | *arg = NULL; |
| 3441 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3442 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3443 | if (**arg == '}') |
| 3444 | break; |
| 3445 | if (**arg != ',') |
| 3446 | { |
| 3447 | semsg(_(e_missing_dict_comma), *arg); |
| 3448 | goto failret; |
| 3449 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3450 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3451 | *arg = skipwhite(*arg + 1); |
| 3452 | } |
| 3453 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3454 | *arg = *arg + 1; |
| 3455 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3456 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3457 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3458 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3459 | *arg += STRLEN(*arg); |
| 3460 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3461 | dict_unref(d); |
| 3462 | return generate_NEWDICT(cctx, count); |
| 3463 | |
| 3464 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3465 | if (*arg == NULL) |
| 3466 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3467 | dict_unref(d); |
| 3468 | return FAIL; |
| 3469 | } |
| 3470 | |
| 3471 | /* |
| 3472 | * Compile "&option". |
| 3473 | */ |
| 3474 | static int |
| 3475 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3476 | { |
| 3477 | typval_T rettv; |
| 3478 | char_u *start = *arg; |
| 3479 | int ret; |
| 3480 | |
| 3481 | // parse the option and get the current value to get the type. |
| 3482 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3483 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3484 | if (ret == OK) |
| 3485 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3486 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3487 | char_u *name = vim_strnsave(start, *arg - start); |
| 3488 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3489 | |
| 3490 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3491 | vim_free(name); |
| 3492 | } |
| 3493 | clear_tv(&rettv); |
| 3494 | |
| 3495 | return ret; |
| 3496 | } |
| 3497 | |
| 3498 | /* |
| 3499 | * Compile "$VAR". |
| 3500 | */ |
| 3501 | static int |
| 3502 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3503 | { |
| 3504 | char_u *start = *arg; |
| 3505 | int len; |
| 3506 | int ret; |
| 3507 | char_u *name; |
| 3508 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3509 | ++*arg; |
| 3510 | len = get_env_len(arg); |
| 3511 | if (len == 0) |
| 3512 | { |
| 3513 | semsg(_(e_syntax_at), start - 1); |
| 3514 | return FAIL; |
| 3515 | } |
| 3516 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3517 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3518 | name = vim_strnsave(start, len + 1); |
| 3519 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3520 | vim_free(name); |
| 3521 | return ret; |
| 3522 | } |
| 3523 | |
| 3524 | /* |
| 3525 | * Compile "@r". |
| 3526 | */ |
| 3527 | static int |
| 3528 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3529 | { |
| 3530 | int ret; |
| 3531 | |
| 3532 | ++*arg; |
| 3533 | if (**arg == NUL) |
| 3534 | { |
| 3535 | semsg(_(e_syntax_at), *arg - 1); |
| 3536 | return FAIL; |
| 3537 | } |
| 3538 | if (!valid_yank_reg(**arg, TRUE)) |
| 3539 | { |
| 3540 | emsg_invreg(**arg); |
| 3541 | return FAIL; |
| 3542 | } |
| 3543 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3544 | ++*arg; |
| 3545 | return ret; |
| 3546 | } |
| 3547 | |
| 3548 | /* |
| 3549 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3550 | */ |
| 3551 | static int |
| 3552 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3553 | { |
| 3554 | char_u *p = end; |
| 3555 | |
| 3556 | // this works from end to start |
| 3557 | while (p > start) |
| 3558 | { |
| 3559 | --p; |
| 3560 | if (*p == '-' || *p == '+') |
| 3561 | { |
| 3562 | // only '-' has an effect, for '+' we only check the type |
| 3563 | #ifdef FEAT_FLOAT |
| 3564 | if (rettv->v_type == VAR_FLOAT) |
| 3565 | { |
| 3566 | if (*p == '-') |
| 3567 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3568 | } |
| 3569 | else |
| 3570 | #endif |
| 3571 | { |
| 3572 | varnumber_T val; |
| 3573 | int error = FALSE; |
| 3574 | |
| 3575 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3576 | // here |
| 3577 | if (check_not_string(rettv) == FAIL) |
| 3578 | return FAIL; |
| 3579 | val = tv_get_number_chk(rettv, &error); |
| 3580 | clear_tv(rettv); |
| 3581 | if (error) |
| 3582 | return FAIL; |
| 3583 | if (*p == '-') |
| 3584 | val = -val; |
| 3585 | rettv->v_type = VAR_NUMBER; |
| 3586 | rettv->vval.v_number = val; |
| 3587 | } |
| 3588 | } |
| 3589 | else |
| 3590 | { |
| 3591 | int v = tv2bool(rettv); |
| 3592 | |
| 3593 | // '!' is permissive in the type. |
| 3594 | clear_tv(rettv); |
| 3595 | rettv->v_type = VAR_BOOL; |
| 3596 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3597 | } |
| 3598 | } |
| 3599 | return OK; |
| 3600 | } |
| 3601 | |
| 3602 | /* |
| 3603 | * Recognize v: variables that are constants and set "rettv". |
| 3604 | */ |
| 3605 | static void |
| 3606 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3607 | { |
| 3608 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3609 | { |
| 3610 | rettv->v_type = VAR_BOOL; |
| 3611 | rettv->vval.v_number = VVAL_TRUE; |
| 3612 | *arg += 6; |
| 3613 | } |
| 3614 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3615 | { |
| 3616 | rettv->v_type = VAR_BOOL; |
| 3617 | rettv->vval.v_number = VVAL_FALSE; |
| 3618 | *arg += 7; |
| 3619 | } |
| 3620 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3621 | { |
| 3622 | rettv->v_type = VAR_SPECIAL; |
| 3623 | rettv->vval.v_number = VVAL_NULL; |
| 3624 | *arg += 6; |
| 3625 | } |
| 3626 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3627 | { |
| 3628 | rettv->v_type = VAR_SPECIAL; |
| 3629 | rettv->vval.v_number = VVAL_NONE; |
| 3630 | *arg += 6; |
| 3631 | } |
| 3632 | } |
| 3633 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3634 | static exptype_T |
| 3635 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3636 | { |
| 3637 | exptype_T type = EXPR_UNKNOWN; |
| 3638 | int i; |
| 3639 | |
| 3640 | switch (p[0]) |
| 3641 | { |
| 3642 | case '=': if (p[1] == '=') |
| 3643 | type = EXPR_EQUAL; |
| 3644 | else if (p[1] == '~') |
| 3645 | type = EXPR_MATCH; |
| 3646 | break; |
| 3647 | case '!': if (p[1] == '=') |
| 3648 | type = EXPR_NEQUAL; |
| 3649 | else if (p[1] == '~') |
| 3650 | type = EXPR_NOMATCH; |
| 3651 | break; |
| 3652 | case '>': if (p[1] != '=') |
| 3653 | { |
| 3654 | type = EXPR_GREATER; |
| 3655 | *len = 1; |
| 3656 | } |
| 3657 | else |
| 3658 | type = EXPR_GEQUAL; |
| 3659 | break; |
| 3660 | case '<': if (p[1] != '=') |
| 3661 | { |
| 3662 | type = EXPR_SMALLER; |
| 3663 | *len = 1; |
| 3664 | } |
| 3665 | else |
| 3666 | type = EXPR_SEQUAL; |
| 3667 | break; |
| 3668 | case 'i': if (p[1] == 's') |
| 3669 | { |
| 3670 | // "is" and "isnot"; but not a prefix of a name |
| 3671 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3672 | *len = 5; |
| 3673 | i = p[*len]; |
| 3674 | if (!isalnum(i) && i != '_') |
| 3675 | { |
| 3676 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3677 | *type_is = TRUE; |
| 3678 | } |
| 3679 | } |
| 3680 | break; |
| 3681 | } |
| 3682 | return type; |
| 3683 | } |
| 3684 | |
| 3685 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3686 | * Compile code to apply '-', '+' and '!'. |
| 3687 | */ |
| 3688 | static int |
| 3689 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3690 | { |
| 3691 | char_u *p = end; |
| 3692 | |
| 3693 | // this works from end to start |
| 3694 | while (p > start) |
| 3695 | { |
| 3696 | --p; |
| 3697 | if (*p == '-' || *p == '+') |
| 3698 | { |
| 3699 | int negate = *p == '-'; |
| 3700 | isn_T *isn; |
| 3701 | |
| 3702 | // TODO: check type |
| 3703 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3704 | { |
| 3705 | --p; |
| 3706 | if (*p == '-') |
| 3707 | negate = !negate; |
| 3708 | } |
| 3709 | // only '-' has an effect, for '+' we only check the type |
| 3710 | if (negate) |
| 3711 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3712 | else |
| 3713 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3714 | if (isn == NULL) |
| 3715 | return FAIL; |
| 3716 | } |
| 3717 | else |
| 3718 | { |
| 3719 | int invert = TRUE; |
| 3720 | |
| 3721 | while (p > start && p[-1] == '!') |
| 3722 | { |
| 3723 | --p; |
| 3724 | invert = !invert; |
| 3725 | } |
| 3726 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3727 | return FAIL; |
| 3728 | } |
| 3729 | } |
| 3730 | return OK; |
| 3731 | } |
| 3732 | |
| 3733 | /* |
| 3734 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3735 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3736 | */ |
| 3737 | static int |
| 3738 | compile_subscript( |
| 3739 | char_u **arg, |
| 3740 | cctx_T *cctx, |
| 3741 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3742 | char_u *end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3743 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3744 | { |
| 3745 | for (;;) |
| 3746 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3747 | char_u *p = skipwhite(*arg); |
| 3748 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3749 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3750 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3751 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3752 | |
| 3753 | // If a following line starts with "->{" or "->X" advance to that |
| 3754 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3755 | // Also if a following line starts with ".x". |
| 3756 | if (next != NULL && |
| 3757 | ((next[0] == '-' && next[1] == '>' |
| 3758 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
| 3759 | || (next[0] == '.' && ASCII_ISALPHA(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3760 | { |
| 3761 | next = next_line_from_context(cctx, TRUE); |
| 3762 | if (next == NULL) |
| 3763 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3764 | *arg = next; |
| 3765 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3766 | } |
| 3767 | } |
| 3768 | |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 3769 | // Do not skip over white space to find the "(", "exeucte 'x' ()" is |
| 3770 | // not a function call. |
| 3771 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3772 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3773 | garray_T *stack = &cctx->ctx_type_stack; |
| 3774 | type_T *type; |
| 3775 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3776 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3777 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3778 | return FAIL; |
| 3779 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3780 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3781 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3782 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3783 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3784 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3785 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3786 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3787 | return FAIL; |
| 3788 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3789 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3790 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3791 | char_u *pstart = p; |
| 3792 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3793 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3794 | return FAIL; |
| 3795 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3796 | // something->method() |
| 3797 | // Apply the '!', '-' and '+' first: |
| 3798 | // -1.0->func() works like (-1.0)->func() |
| 3799 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3800 | return FAIL; |
| 3801 | *start_leader = end_leader; // don't apply again later |
| 3802 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3803 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3804 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3805 | // No line break supported right after "->". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3806 | if (**arg == '{') |
| 3807 | { |
| 3808 | // lambda call: list->{lambda} |
| 3809 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3810 | return FAIL; |
| 3811 | } |
| 3812 | else |
| 3813 | { |
| 3814 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3815 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3816 | if (!eval_isnamec1(*p)) |
| 3817 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3818 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3819 | return FAIL; |
| 3820 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3821 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3822 | p += 2; |
| 3823 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3824 | ; |
| 3825 | if (*p != '(') |
| 3826 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3827 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3828 | return FAIL; |
| 3829 | } |
| 3830 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3831 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3832 | return FAIL; |
| 3833 | } |
| 3834 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3835 | else if (*p == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3836 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3837 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3838 | type_T **typep; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3839 | vartype_T vtype; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3840 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3841 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3842 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3843 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3844 | // TODO: blob index |
| 3845 | // TODO: more arguments |
| 3846 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3847 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3848 | return FAIL; |
| 3849 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3850 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3851 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3852 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3853 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3854 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3855 | return FAIL; |
| 3856 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3857 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3858 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3859 | if (**arg != ']') |
| 3860 | { |
| 3861 | emsg(_(e_missbrac)); |
| 3862 | return FAIL; |
| 3863 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3864 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3865 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3866 | // We can index a list and a dict. If we don't know the type |
| 3867 | // we can use the index value type. |
| 3868 | // TODO: If we don't know use an instruction to figure it out at |
| 3869 | // runtime. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3870 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3871 | vtype = (*typep)->tt_type; |
| 3872 | if (*typep == &t_any) |
| 3873 | { |
| 3874 | type_T *valtype = ((type_T **)stack->ga_data) |
| 3875 | [stack->ga_len - 1]; |
| 3876 | if (valtype == &t_string) |
| 3877 | vtype = VAR_DICT; |
| 3878 | } |
| 3879 | if (vtype == VAR_DICT) |
| 3880 | { |
| 3881 | if ((*typep)->tt_type == VAR_DICT) |
| 3882 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3883 | else |
| 3884 | { |
| 3885 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3886 | return FAIL; |
| 3887 | *typep = &t_any; |
| 3888 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3889 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3890 | return FAIL; |
| 3891 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3892 | return FAIL; |
| 3893 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3894 | else if (vtype == VAR_STRING) |
| 3895 | { |
| 3896 | *typep = &t_number; |
| 3897 | if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL) |
| 3898 | return FAIL; |
| 3899 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3900 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3901 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3902 | if ((*typep)->tt_type == VAR_LIST) |
| 3903 | *typep = (*typep)->tt_member; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3904 | if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3905 | return FAIL; |
| 3906 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3907 | else |
| 3908 | { |
| 3909 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3910 | return FAIL; |
| 3911 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3912 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3913 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3914 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3915 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3916 | return FAIL; |
| 3917 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3918 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3919 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3920 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3921 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3922 | p = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3923 | if (eval_isnamec1(*p)) |
| 3924 | while (eval_isnamec(*p)) |
| 3925 | MB_PTR_ADV(p); |
| 3926 | if (p == *arg) |
| 3927 | { |
| 3928 | semsg(_(e_syntax_at), *arg); |
| 3929 | return FAIL; |
| 3930 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3931 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3932 | return FAIL; |
| 3933 | *arg = p; |
| 3934 | } |
| 3935 | else |
| 3936 | break; |
| 3937 | } |
| 3938 | |
| 3939 | // TODO - see handle_subscript(): |
| 3940 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3941 | // Don't do this when "Func" is already a partial that was bound |
| 3942 | // explicitly (pt_auto is FALSE). |
| 3943 | |
| 3944 | return OK; |
| 3945 | } |
| 3946 | |
| 3947 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3948 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3949 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3950 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3951 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3952 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3953 | * |
| 3954 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3955 | */ |
| 3956 | |
| 3957 | /* |
| 3958 | * number number constant |
| 3959 | * 0zFFFFFFFF Blob constant |
| 3960 | * "string" string constant |
| 3961 | * 'string' literal string constant |
| 3962 | * &option-name option value |
| 3963 | * @r register contents |
| 3964 | * identifier variable value |
| 3965 | * function() function call |
| 3966 | * $VAR environment variable |
| 3967 | * (expression) nested expression |
| 3968 | * [expr, expr] List |
| 3969 | * {key: val, key: val} Dictionary |
| 3970 | * #{key: val, key: val} Dictionary with literal keys |
| 3971 | * |
| 3972 | * Also handle: |
| 3973 | * ! in front logical NOT |
| 3974 | * - in front unary minus |
| 3975 | * + in front unary plus (ignored) |
| 3976 | * trailing (arg) funcref/partial call |
| 3977 | * trailing [] subscript in String or List |
| 3978 | * trailing .name entry in Dictionary |
| 3979 | * trailing ->name() method call |
| 3980 | */ |
| 3981 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3982 | compile_expr7( |
| 3983 | char_u **arg, |
| 3984 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3985 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3986 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3987 | char_u *start_leader, *end_leader; |
| 3988 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3989 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3990 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3991 | |
| 3992 | /* |
| 3993 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3994 | */ |
| 3995 | start_leader = *arg; |
| 3996 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3997 | *arg = skipwhite(*arg + 1); |
| 3998 | end_leader = *arg; |
| 3999 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4000 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4001 | switch (**arg) |
| 4002 | { |
| 4003 | /* |
| 4004 | * Number constant. |
| 4005 | */ |
| 4006 | case '0': // also for blob starting with 0z |
| 4007 | case '1': |
| 4008 | case '2': |
| 4009 | case '3': |
| 4010 | case '4': |
| 4011 | case '5': |
| 4012 | case '6': |
| 4013 | case '7': |
| 4014 | case '8': |
| 4015 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4016 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4017 | return FAIL; |
| 4018 | break; |
| 4019 | |
| 4020 | /* |
| 4021 | * String constant: "string". |
| 4022 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4023 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4024 | return FAIL; |
| 4025 | break; |
| 4026 | |
| 4027 | /* |
| 4028 | * Literal string constant: 'str''ing'. |
| 4029 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4030 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4031 | return FAIL; |
| 4032 | break; |
| 4033 | |
| 4034 | /* |
| 4035 | * Constant Vim variable. |
| 4036 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4037 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4038 | ret = NOTDONE; |
| 4039 | break; |
| 4040 | |
| 4041 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4042 | * "true" constant |
| 4043 | */ |
| 4044 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 4045 | && !eval_isnamec((*arg)[4])) |
| 4046 | { |
| 4047 | *arg += 4; |
| 4048 | rettv->v_type = VAR_BOOL; |
| 4049 | rettv->vval.v_number = VVAL_TRUE; |
| 4050 | } |
| 4051 | else |
| 4052 | ret = NOTDONE; |
| 4053 | break; |
| 4054 | |
| 4055 | /* |
| 4056 | * "false" constant |
| 4057 | */ |
| 4058 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 4059 | && !eval_isnamec((*arg)[5])) |
| 4060 | { |
| 4061 | *arg += 5; |
| 4062 | rettv->v_type = VAR_BOOL; |
| 4063 | rettv->vval.v_number = VVAL_FALSE; |
| 4064 | } |
| 4065 | else |
| 4066 | ret = NOTDONE; |
| 4067 | break; |
| 4068 | |
| 4069 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4070 | * List: [expr, expr] |
| 4071 | */ |
| 4072 | case '[': ret = compile_list(arg, cctx); |
| 4073 | break; |
| 4074 | |
| 4075 | /* |
| 4076 | * Dictionary: #{key: val, key: val} |
| 4077 | */ |
| 4078 | case '#': if ((*arg)[1] == '{') |
| 4079 | { |
| 4080 | ++*arg; |
| 4081 | ret = compile_dict(arg, cctx, TRUE); |
| 4082 | } |
| 4083 | else |
| 4084 | ret = NOTDONE; |
| 4085 | break; |
| 4086 | |
| 4087 | /* |
| 4088 | * Lambda: {arg, arg -> expr} |
| 4089 | * Dictionary: {'key': val, 'key': val} |
| 4090 | */ |
| 4091 | case '{': { |
| 4092 | char_u *start = skipwhite(*arg + 1); |
| 4093 | |
| 4094 | // Find out what comes after the arguments. |
| 4095 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 4096 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4097 | if (ret != FAIL && *start == '>') |
| 4098 | ret = compile_lambda(arg, cctx); |
| 4099 | else |
| 4100 | ret = compile_dict(arg, cctx, FALSE); |
| 4101 | } |
| 4102 | break; |
| 4103 | |
| 4104 | /* |
| 4105 | * Option value: &name |
| 4106 | */ |
| 4107 | case '&': ret = compile_get_option(arg, cctx); |
| 4108 | break; |
| 4109 | |
| 4110 | /* |
| 4111 | * Environment variable: $VAR. |
| 4112 | */ |
| 4113 | case '$': ret = compile_get_env(arg, cctx); |
| 4114 | break; |
| 4115 | |
| 4116 | /* |
| 4117 | * Register contents: @r. |
| 4118 | */ |
| 4119 | case '@': ret = compile_get_register(arg, cctx); |
| 4120 | break; |
| 4121 | /* |
| 4122 | * nested expression: (expression). |
| 4123 | */ |
| 4124 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4125 | |
| 4126 | // recursive! |
| 4127 | if (ppconst->pp_used <= PPSIZE - 10) |
| 4128 | { |
| 4129 | ret = compile_expr1(arg, cctx, ppconst); |
| 4130 | } |
| 4131 | else |
| 4132 | { |
| 4133 | // Not enough space in ppconst, flush constants. |
| 4134 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4135 | return FAIL; |
| 4136 | ret = compile_expr0(arg, cctx); |
| 4137 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4138 | *arg = skipwhite(*arg); |
| 4139 | if (**arg == ')') |
| 4140 | ++*arg; |
| 4141 | else if (ret == OK) |
| 4142 | { |
| 4143 | emsg(_(e_missing_close)); |
| 4144 | ret = FAIL; |
| 4145 | } |
| 4146 | break; |
| 4147 | |
| 4148 | default: ret = NOTDONE; |
| 4149 | break; |
| 4150 | } |
| 4151 | if (ret == FAIL) |
| 4152 | return FAIL; |
| 4153 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4154 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4155 | { |
| 4156 | // apply the '!', '-' and '+' before the constant |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4157 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4158 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4159 | clear_tv(rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4160 | return FAIL; |
| 4161 | } |
| 4162 | start_leader = end_leader; // don't apply again below |
| 4163 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4164 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4165 | clear_tv(rettv); |
| 4166 | else |
| 4167 | // A constant expression can possibly be handled compile time, |
| 4168 | // return the value instead of generating code. |
| 4169 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4170 | } |
| 4171 | else if (ret == NOTDONE) |
| 4172 | { |
| 4173 | char_u *p; |
| 4174 | int r; |
| 4175 | |
| 4176 | if (!eval_isnamec1(**arg)) |
| 4177 | { |
| 4178 | semsg(_("E1015: Name expected: %s"), *arg); |
| 4179 | return FAIL; |
| 4180 | } |
| 4181 | |
| 4182 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4183 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4184 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4185 | { |
| 4186 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 4187 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4188 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4189 | { |
| 4190 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4191 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4192 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4193 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4194 | if (r == FAIL) |
| 4195 | return FAIL; |
| 4196 | } |
| 4197 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4198 | // Handle following "[]", ".member", etc. |
| 4199 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4200 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4201 | ppconst) == FAIL) |
| 4202 | return FAIL; |
| 4203 | if (ppconst->pp_used > 0) |
| 4204 | { |
| 4205 | // apply the '!', '-' and '+' before the constant |
| 4206 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4207 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
| 4208 | return FAIL; |
| 4209 | return OK; |
| 4210 | } |
| 4211 | if (compile_leader(cctx, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4212 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4213 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4214 | } |
| 4215 | |
| 4216 | /* |
| 4217 | * * number multiplication |
| 4218 | * / number division |
| 4219 | * % number modulo |
| 4220 | */ |
| 4221 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4222 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4223 | { |
| 4224 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4225 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4226 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4227 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4228 | // get the first expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4229 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4230 | return FAIL; |
| 4231 | |
| 4232 | /* |
| 4233 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4234 | */ |
| 4235 | for (;;) |
| 4236 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4237 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4238 | if (*op != '*' && *op != '/' && *op != '%') |
| 4239 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4240 | if (next != NULL) |
| 4241 | { |
| 4242 | *arg = next_line_from_context(cctx, TRUE); |
| 4243 | op = skipwhite(*arg); |
| 4244 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4245 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4246 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4247 | { |
| 4248 | char_u buf[3]; |
| 4249 | |
| 4250 | vim_strncpy(buf, op, 1); |
| 4251 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4252 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4253 | } |
| 4254 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4255 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4256 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4257 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4258 | // get the second expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4259 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4260 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4261 | |
| 4262 | if (ppconst->pp_used == ppconst_used + 2 |
| 4263 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4264 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4265 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4266 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4267 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4268 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4269 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4270 | // both are numbers: compute the result |
| 4271 | switch (*op) |
| 4272 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4273 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4274 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4275 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4276 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4277 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4278 | break; |
| 4279 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4280 | tv1->vval.v_number = res; |
| 4281 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4282 | } |
| 4283 | else |
| 4284 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4285 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4286 | generate_two_op(cctx, op); |
| 4287 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | return OK; |
| 4291 | } |
| 4292 | |
| 4293 | /* |
| 4294 | * + number addition |
| 4295 | * - number subtraction |
| 4296 | * .. string concatenation |
| 4297 | */ |
| 4298 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4299 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4300 | { |
| 4301 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4302 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4303 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4304 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4305 | |
| 4306 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4307 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4308 | return FAIL; |
| 4309 | |
| 4310 | /* |
| 4311 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4312 | */ |
| 4313 | for (;;) |
| 4314 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4315 | op = may_peek_next_line(cctx, *arg, &next); |
| 4316 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4317 | break; |
| 4318 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4319 | if (next != NULL) |
| 4320 | { |
| 4321 | *arg = next_line_from_context(cctx, TRUE); |
| 4322 | op = skipwhite(*arg); |
| 4323 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4324 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4325 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4326 | { |
| 4327 | char_u buf[3]; |
| 4328 | |
| 4329 | vim_strncpy(buf, op, oplen); |
| 4330 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4331 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4335 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4336 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4337 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4338 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4339 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4340 | return FAIL; |
| 4341 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4342 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4343 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4344 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4345 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4346 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4347 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4348 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4349 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4350 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4351 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4352 | // concat/subtract/add constant numbers |
| 4353 | if (*op == '+') |
| 4354 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4355 | else if (*op == '-') |
| 4356 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4357 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4358 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4359 | // concatenate constant strings |
| 4360 | char_u *s1 = tv1->vval.v_string; |
| 4361 | char_u *s2 = tv2->vval.v_string; |
| 4362 | size_t len1 = STRLEN(s1); |
| 4363 | |
| 4364 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4365 | if (tv1->vval.v_string == NULL) |
| 4366 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4367 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4368 | return FAIL; |
| 4369 | } |
| 4370 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4371 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4372 | vim_free(s1); |
| 4373 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4374 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4375 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4376 | } |
| 4377 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4378 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4379 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4380 | if (*op == '.') |
| 4381 | { |
| 4382 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4383 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4384 | return FAIL; |
| 4385 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4386 | } |
| 4387 | else |
| 4388 | generate_two_op(cctx, op); |
| 4389 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | return OK; |
| 4393 | } |
| 4394 | |
| 4395 | /* |
| 4396 | * expr5a == expr5b |
| 4397 | * expr5a =~ expr5b |
| 4398 | * expr5a != expr5b |
| 4399 | * expr5a !~ expr5b |
| 4400 | * expr5a > expr5b |
| 4401 | * expr5a >= expr5b |
| 4402 | * expr5a < expr5b |
| 4403 | * expr5a <= expr5b |
| 4404 | * expr5a is expr5b |
| 4405 | * expr5a isnot expr5b |
| 4406 | * |
| 4407 | * Produces instructions: |
| 4408 | * EVAL expr5a Push result of "expr5a" |
| 4409 | * EVAL expr5b Push result of "expr5b" |
| 4410 | * COMPARE one of the compare instructions |
| 4411 | */ |
| 4412 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4413 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4414 | { |
| 4415 | exptype_T type = EXPR_UNKNOWN; |
| 4416 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4417 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4418 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4419 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4420 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4421 | |
| 4422 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4423 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4424 | return FAIL; |
| 4425 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4426 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4427 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4428 | |
| 4429 | /* |
| 4430 | * If there is a comparative operator, use it. |
| 4431 | */ |
| 4432 | if (type != EXPR_UNKNOWN) |
| 4433 | { |
| 4434 | int ic = FALSE; // Default: do not ignore case |
| 4435 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4436 | if (next != NULL) |
| 4437 | { |
| 4438 | *arg = next_line_from_context(cctx, TRUE); |
| 4439 | p = skipwhite(*arg); |
| 4440 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4441 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4442 | { |
| 4443 | semsg(_(e_invexpr2), *arg); |
| 4444 | return FAIL; |
| 4445 | } |
| 4446 | // extra question mark appended: ignore case |
| 4447 | if (p[len] == '?') |
| 4448 | { |
| 4449 | ic = TRUE; |
| 4450 | ++len; |
| 4451 | } |
| 4452 | // extra '#' appended: match case (ignored) |
| 4453 | else if (p[len] == '#') |
| 4454 | ++len; |
| 4455 | // nothing appended: match case |
| 4456 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4457 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4458 | { |
| 4459 | char_u buf[7]; |
| 4460 | |
| 4461 | vim_strncpy(buf, p, len); |
| 4462 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4463 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | // get the second variable |
| 4467 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4468 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4469 | return FAIL; |
| 4470 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4471 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4472 | return FAIL; |
| 4473 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4474 | if (ppconst->pp_used == ppconst_used + 2) |
| 4475 | { |
| 4476 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4477 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4478 | int ret; |
| 4479 | |
| 4480 | // Both sides are a constant, compute the result now. |
| 4481 | // First check for a valid combination of types, this is more |
| 4482 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4483 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4484 | ret = FAIL; |
| 4485 | else |
| 4486 | { |
| 4487 | ret = typval_compare(tv1, tv2, type, ic); |
| 4488 | tv1->v_type = VAR_BOOL; |
| 4489 | tv1->vval.v_number = tv1->vval.v_number |
| 4490 | ? VVAL_TRUE : VVAL_FALSE; |
| 4491 | clear_tv(tv2); |
| 4492 | --ppconst->pp_used; |
| 4493 | } |
| 4494 | return ret; |
| 4495 | } |
| 4496 | |
| 4497 | generate_ppconst(cctx, ppconst); |
| 4498 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | return OK; |
| 4502 | } |
| 4503 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4504 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4505 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4506 | /* |
| 4507 | * Compile || or &&. |
| 4508 | */ |
| 4509 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4510 | compile_and_or( |
| 4511 | char_u **arg, |
| 4512 | cctx_T *cctx, |
| 4513 | char *op, |
| 4514 | ppconst_T *ppconst, |
| 4515 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4516 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4517 | char_u *next; |
| 4518 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4519 | int opchar = *op; |
| 4520 | |
| 4521 | if (p[0] == opchar && p[1] == opchar) |
| 4522 | { |
| 4523 | garray_T *instr = &cctx->ctx_instr; |
| 4524 | garray_T end_ga; |
| 4525 | |
| 4526 | /* |
| 4527 | * Repeat until there is no following "||" or "&&" |
| 4528 | */ |
| 4529 | ga_init2(&end_ga, sizeof(int), 10); |
| 4530 | while (p[0] == opchar && p[1] == opchar) |
| 4531 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4532 | if (next != NULL) |
| 4533 | { |
| 4534 | *arg = next_line_from_context(cctx, TRUE); |
| 4535 | p = skipwhite(*arg); |
| 4536 | } |
| 4537 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4538 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4539 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4540 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4541 | return FAIL; |
| 4542 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4543 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4544 | // TODO: use ppconst if the value is a constant |
| 4545 | generate_ppconst(cctx, ppconst); |
| 4546 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4547 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4548 | { |
| 4549 | ga_clear(&end_ga); |
| 4550 | return FAIL; |
| 4551 | } |
| 4552 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4553 | ++end_ga.ga_len; |
| 4554 | generate_JUMP(cctx, opchar == '|' |
| 4555 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 4556 | |
| 4557 | // eval the next expression |
| 4558 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4559 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4560 | return FAIL; |
| 4561 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4562 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4563 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4564 | { |
| 4565 | ga_clear(&end_ga); |
| 4566 | return FAIL; |
| 4567 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4568 | |
| 4569 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4570 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4571 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4572 | |
| 4573 | // Fill in the end label in all jumps. |
| 4574 | while (end_ga.ga_len > 0) |
| 4575 | { |
| 4576 | isn_T *isn; |
| 4577 | |
| 4578 | --end_ga.ga_len; |
| 4579 | isn = ((isn_T *)instr->ga_data) |
| 4580 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4581 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4582 | } |
| 4583 | ga_clear(&end_ga); |
| 4584 | } |
| 4585 | |
| 4586 | return OK; |
| 4587 | } |
| 4588 | |
| 4589 | /* |
| 4590 | * expr4a && expr4a && expr4a logical AND |
| 4591 | * |
| 4592 | * Produces instructions: |
| 4593 | * EVAL expr4a Push result of "expr4a" |
| 4594 | * JUMP_AND_KEEP_IF_FALSE end |
| 4595 | * EVAL expr4b Push result of "expr4b" |
| 4596 | * JUMP_AND_KEEP_IF_FALSE end |
| 4597 | * EVAL expr4c Push result of "expr4c" |
| 4598 | * end: |
| 4599 | */ |
| 4600 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4601 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4602 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4603 | int ppconst_used = ppconst->pp_used; |
| 4604 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4605 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4606 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4607 | return FAIL; |
| 4608 | |
| 4609 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4610 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4611 | } |
| 4612 | |
| 4613 | /* |
| 4614 | * expr3a || expr3b || expr3c logical OR |
| 4615 | * |
| 4616 | * Produces instructions: |
| 4617 | * EVAL expr3a Push result of "expr3a" |
| 4618 | * JUMP_AND_KEEP_IF_TRUE end |
| 4619 | * EVAL expr3b Push result of "expr3b" |
| 4620 | * JUMP_AND_KEEP_IF_TRUE end |
| 4621 | * EVAL expr3c Push result of "expr3c" |
| 4622 | * end: |
| 4623 | */ |
| 4624 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4625 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4626 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4627 | int ppconst_used = ppconst->pp_used; |
| 4628 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4629 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4630 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4631 | return FAIL; |
| 4632 | |
| 4633 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4634 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | /* |
| 4638 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4639 | * |
| 4640 | * Produces instructions: |
| 4641 | * EVAL expr2 Push result of "expr" |
| 4642 | * JUMP_IF_FALSE alt jump if false |
| 4643 | * EVAL expr1a |
| 4644 | * JUMP_ALWAYS end |
| 4645 | * alt: EVAL expr1b |
| 4646 | * end: |
| 4647 | */ |
| 4648 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4649 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4650 | { |
| 4651 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4652 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4653 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4654 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4655 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4656 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4657 | return FAIL; |
| 4658 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4659 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4660 | if (*p == '?') |
| 4661 | { |
| 4662 | garray_T *instr = &cctx->ctx_instr; |
| 4663 | garray_T *stack = &cctx->ctx_type_stack; |
| 4664 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4665 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4666 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4667 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4668 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4669 | int has_const_expr = FALSE; |
| 4670 | int const_value = FALSE; |
| 4671 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4672 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4673 | if (next != NULL) |
| 4674 | { |
| 4675 | *arg = next_line_from_context(cctx, TRUE); |
| 4676 | p = skipwhite(*arg); |
| 4677 | } |
| 4678 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4679 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4680 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4681 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4682 | return FAIL; |
| 4683 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4684 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4685 | if (ppconst->pp_used == ppconst_used + 1) |
| 4686 | { |
| 4687 | // the condition is a constant, we know whether the ? or the : |
| 4688 | // expression is to be evaluated. |
| 4689 | has_const_expr = TRUE; |
| 4690 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4691 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4692 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4693 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4694 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4695 | } |
| 4696 | else |
| 4697 | { |
| 4698 | generate_ppconst(cctx, ppconst); |
| 4699 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4700 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4701 | |
| 4702 | // evaluate the second expression; any type is accepted |
| 4703 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4704 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4705 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4706 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4707 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4708 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4709 | if (!has_const_expr) |
| 4710 | { |
| 4711 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4712 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4713 | // remember the type and drop it |
| 4714 | --stack->ga_len; |
| 4715 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4716 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4717 | end_idx = instr->ga_len; |
| 4718 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4719 | |
| 4720 | // jump here from JUMP_IF_FALSE |
| 4721 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4722 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4723 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4724 | |
| 4725 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4726 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4727 | if (*p != ':') |
| 4728 | { |
| 4729 | emsg(_(e_missing_colon)); |
| 4730 | return FAIL; |
| 4731 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4732 | if (next != NULL) |
| 4733 | { |
| 4734 | *arg = next_line_from_context(cctx, TRUE); |
| 4735 | p = skipwhite(*arg); |
| 4736 | } |
| 4737 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4738 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4739 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4740 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4741 | return FAIL; |
| 4742 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4743 | |
| 4744 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4745 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4746 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4747 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4748 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4749 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4750 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4751 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4752 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4753 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4754 | if (!has_const_expr) |
| 4755 | { |
| 4756 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4757 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4758 | // If the types differ, the result has a more generic type. |
| 4759 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4760 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4761 | |
| 4762 | // jump here from JUMP_ALWAYS |
| 4763 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4764 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4765 | } |
| 4766 | |
| 4767 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4768 | } |
| 4769 | return OK; |
| 4770 | } |
| 4771 | |
| 4772 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4773 | * Toplevel expression. |
| 4774 | */ |
| 4775 | static int |
| 4776 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4777 | { |
| 4778 | ppconst_T ppconst; |
| 4779 | |
| 4780 | CLEAR_FIELD(ppconst); |
| 4781 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4782 | { |
| 4783 | clear_ppconst(&ppconst); |
| 4784 | return FAIL; |
| 4785 | } |
| 4786 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4787 | return FAIL; |
| 4788 | return OK; |
| 4789 | } |
| 4790 | |
| 4791 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4792 | * compile "return [expr]" |
| 4793 | */ |
| 4794 | static char_u * |
| 4795 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4796 | { |
| 4797 | char_u *p = arg; |
| 4798 | garray_T *stack = &cctx->ctx_type_stack; |
| 4799 | type_T *stack_type; |
| 4800 | |
| 4801 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4802 | { |
| 4803 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4804 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4805 | return NULL; |
| 4806 | |
| 4807 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4808 | if (set_return_type) |
| 4809 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4810 | else |
| 4811 | { |
| 4812 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4813 | && stack_type->tt_type != VAR_VOID |
| 4814 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4815 | { |
| 4816 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4817 | return NULL; |
| 4818 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4819 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4820 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4821 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4822 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4823 | } |
| 4824 | else |
| 4825 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4826 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4827 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4828 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4829 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4830 | { |
| 4831 | emsg(_("E1003: Missing return value")); |
| 4832 | return NULL; |
| 4833 | } |
| 4834 | |
| 4835 | // No argument, return zero. |
| 4836 | generate_PUSHNR(cctx, 0); |
| 4837 | } |
| 4838 | |
| 4839 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4840 | return NULL; |
| 4841 | |
| 4842 | // "return val | endif" is possible |
| 4843 | return skipwhite(p); |
| 4844 | } |
| 4845 | |
| 4846 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4847 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4848 | * Return a pointer to the line in allocated memory. |
| 4849 | * Return NULL for end-of-file or some error. |
| 4850 | */ |
| 4851 | static char_u * |
| 4852 | exarg_getline( |
| 4853 | int c UNUSED, |
| 4854 | void *cookie, |
| 4855 | int indent UNUSED, |
| 4856 | int do_concat UNUSED) |
| 4857 | { |
| 4858 | cctx_T *cctx = (cctx_T *)cookie; |
| 4859 | |
| 4860 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4861 | { |
| 4862 | iemsg("Heredoc got to end"); |
| 4863 | return NULL; |
| 4864 | } |
| 4865 | ++cctx->ctx_lnum; |
| 4866 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4867 | [cctx->ctx_lnum]); |
| 4868 | } |
| 4869 | |
| 4870 | /* |
| 4871 | * Compile a nested :def command. |
| 4872 | */ |
| 4873 | static char_u * |
| 4874 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4875 | { |
| 4876 | char_u *name_start = eap->arg; |
| 4877 | char_u *name_end = to_name_end(eap->arg, FALSE); |
| 4878 | char_u *name = get_lambda_name(); |
| 4879 | lvar_T *lvar; |
| 4880 | ufunc_T *ufunc; |
| 4881 | |
| 4882 | eap->arg = name_end; |
| 4883 | eap->getline = exarg_getline; |
| 4884 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4885 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4886 | eap->forceit = FALSE; |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4887 | ufunc = def_function(eap, name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4888 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4889 | if (ufunc == NULL) |
| 4890 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4891 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4892 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4893 | return NULL; |
| 4894 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4895 | // Define a local variable for the function reference. |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4896 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4897 | TRUE, ufunc->uf_func_type); |
| 4898 | |
| 4899 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL |
| 4900 | || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL) |
| 4901 | return NULL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4902 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4903 | // TODO: warning for trailing text? |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4904 | return (char_u *)""; |
| 4905 | } |
| 4906 | |
| 4907 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4908 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4909 | */ |
| 4910 | int |
| 4911 | assignment_len(char_u *p, int *heredoc) |
| 4912 | { |
| 4913 | if (*p == '=') |
| 4914 | { |
| 4915 | if (p[1] == '<' && p[2] == '<') |
| 4916 | { |
| 4917 | *heredoc = TRUE; |
| 4918 | return 3; |
| 4919 | } |
| 4920 | return 1; |
| 4921 | } |
| 4922 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4923 | return 2; |
| 4924 | if (STRNCMP(p, "..=", 3) == 0) |
| 4925 | return 3; |
| 4926 | return 0; |
| 4927 | } |
| 4928 | |
| 4929 | // words that cannot be used as a variable |
| 4930 | static char *reserved[] = { |
| 4931 | "true", |
| 4932 | "false", |
| 4933 | NULL |
| 4934 | }; |
| 4935 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4936 | typedef enum { |
| 4937 | dest_local, |
| 4938 | dest_option, |
| 4939 | dest_env, |
| 4940 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4941 | dest_buffer, |
| 4942 | dest_window, |
| 4943 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4944 | dest_vimvar, |
| 4945 | dest_script, |
| 4946 | dest_reg, |
| 4947 | } assign_dest_T; |
| 4948 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4949 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4950 | * Generate the load instruction for "name". |
| 4951 | */ |
| 4952 | static void |
| 4953 | generate_loadvar( |
| 4954 | cctx_T *cctx, |
| 4955 | assign_dest_T dest, |
| 4956 | char_u *name, |
| 4957 | lvar_T *lvar, |
| 4958 | type_T *type) |
| 4959 | { |
| 4960 | switch (dest) |
| 4961 | { |
| 4962 | case dest_option: |
| 4963 | // TODO: check the option exists |
| 4964 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4965 | break; |
| 4966 | case dest_global: |
| 4967 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4968 | break; |
| 4969 | case dest_buffer: |
| 4970 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4971 | break; |
| 4972 | case dest_window: |
| 4973 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4974 | break; |
| 4975 | case dest_tab: |
| 4976 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4977 | break; |
| 4978 | case dest_script: |
| 4979 | compile_load_scriptvar(cctx, |
| 4980 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4981 | break; |
| 4982 | case dest_env: |
| 4983 | // Include $ in the name here |
| 4984 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4985 | break; |
| 4986 | case dest_reg: |
| 4987 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4988 | break; |
| 4989 | case dest_vimvar: |
| 4990 | generate_LOADV(cctx, name + 2, TRUE); |
| 4991 | break; |
| 4992 | case dest_local: |
| 4993 | if (lvar->lv_from_outer) |
| 4994 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4995 | NULL, type); |
| 4996 | else |
| 4997 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4998 | break; |
| 4999 | } |
| 5000 | } |
| 5001 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5002 | void |
| 5003 | vim9_declare_error(char_u *name) |
| 5004 | { |
| 5005 | char *scope = ""; |
| 5006 | |
| 5007 | switch (*name) |
| 5008 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 5009 | case 'g': scope = _("global"); break; |
| 5010 | case 'b': scope = _("buffer"); break; |
| 5011 | case 'w': scope = _("window"); break; |
| 5012 | case 't': scope = _("tab"); break; |
| 5013 | case 'v': scope = "v:"; break; |
| 5014 | case '$': semsg(_(e_declare_env_var), name); return; |
| 5015 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5016 | } |
| 5017 | semsg(_(e_declare_var), scope, name); |
| 5018 | } |
| 5019 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5020 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5021 | * Compile declaration and assignment: |
| 5022 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5023 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5024 | * Return NULL for an error. |
| 5025 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5026 | */ |
| 5027 | static char_u * |
| 5028 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 5029 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5030 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5031 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5032 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5033 | char_u *ret = NULL; |
| 5034 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5035 | int var_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5036 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5037 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5038 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5039 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5040 | int oplen = 0; |
| 5041 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 5042 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5043 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5044 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5045 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5046 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5047 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5048 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 5049 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 5050 | if (p == NULL) |
| 5051 | return *arg == '[' ? arg : NULL; |
| 5052 | |
| 5053 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5054 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 5055 | // TODO: should we allow this, and figure out type inference from list |
| 5056 | // members? |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5057 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5058 | return NULL; |
| 5059 | } |
| 5060 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5061 | sp = p; |
| 5062 | p = skipwhite(p); |
| 5063 | op = p; |
| 5064 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5065 | |
| 5066 | if (var_count > 0 && oplen == 0) |
| 5067 | // can be something like "[1, 2]->func()" |
| 5068 | return arg; |
| 5069 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5070 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 5071 | { |
| 5072 | char_u buf[4]; |
| 5073 | |
| 5074 | vim_strncpy(buf, op, oplen); |
| 5075 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5076 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 5077 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5078 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5079 | if (heredoc) |
| 5080 | { |
| 5081 | list_T *l; |
| 5082 | listitem_T *li; |
| 5083 | |
| 5084 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5085 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5086 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 5087 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5088 | |
| 5089 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 5090 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5091 | { |
| 5092 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 5093 | li->li_tv.vval.v_string = NULL; |
| 5094 | } |
| 5095 | generate_NEWLIST(cctx, l->lv_len); |
| 5096 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5097 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5098 | list_free(l); |
| 5099 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5100 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5101 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5102 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5103 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5104 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 5105 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5106 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5107 | p = skipwhite(op + oplen); |
| 5108 | if (compile_expr0(&p, cctx) == FAIL) |
| 5109 | return NULL; |
| 5110 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5111 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5112 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5113 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5114 | type_T *stacktype; |
| 5115 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5116 | stacktype = stack->ga_len == 0 ? &t_void |
| 5117 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5118 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5119 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5120 | emsg(_(e_cannot_use_void)); |
| 5121 | goto theend; |
| 5122 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5123 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5124 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5125 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5126 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 5127 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5128 | } |
| 5129 | } |
| 5130 | |
| 5131 | /* |
| 5132 | * Loop over variables in "[var, var] = expr". |
| 5133 | * For "var = expr" and "let var: type" this is done only once. |
| 5134 | */ |
| 5135 | if (var_count > 0) |
| 5136 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 5137 | else |
| 5138 | var_start = arg; |
| 5139 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 5140 | { |
| 5141 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 5142 | size_t varlen; |
| 5143 | int new_local = FALSE; |
| 5144 | int opt_type; |
| 5145 | int opt_flags = 0; |
| 5146 | assign_dest_T dest = dest_local; |
| 5147 | int vimvaridx = -1; |
| 5148 | lvar_T *lvar = NULL; |
| 5149 | lvar_T arg_lvar; |
| 5150 | int has_type = FALSE; |
| 5151 | int has_index = FALSE; |
| 5152 | int instr_count = -1; |
| 5153 | |
| 5154 | p = (*var_start == '&' || *var_start == '$' |
| 5155 | || *var_start == '@') ? var_start + 1 : var_start; |
| 5156 | p = to_name_end(p, TRUE); |
| 5157 | |
| 5158 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 5159 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 5160 | --var_end; |
| 5161 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 5162 | --p; |
| 5163 | |
| 5164 | varlen = p - var_start; |
| 5165 | vim_free(name); |
| 5166 | name = vim_strnsave(var_start, varlen); |
| 5167 | if (name == NULL) |
| 5168 | return NULL; |
| 5169 | if (!heredoc) |
| 5170 | type = &t_any; |
| 5171 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5172 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5173 | { |
| 5174 | if (*var_start == '&') |
| 5175 | { |
| 5176 | int cc; |
| 5177 | long numval; |
| 5178 | |
| 5179 | dest = dest_option; |
| 5180 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5181 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5182 | emsg(_(e_const_option)); |
| 5183 | goto theend; |
| 5184 | } |
| 5185 | if (is_decl) |
| 5186 | { |
| 5187 | semsg(_("E1052: Cannot declare an option: %s"), var_start); |
| 5188 | goto theend; |
| 5189 | } |
| 5190 | p = var_start; |
| 5191 | p = find_option_end(&p, &opt_flags); |
| 5192 | if (p == NULL) |
| 5193 | { |
| 5194 | // cannot happen? |
| 5195 | emsg(_(e_letunexp)); |
| 5196 | goto theend; |
| 5197 | } |
| 5198 | cc = *p; |
| 5199 | *p = NUL; |
| 5200 | opt_type = get_option_value(var_start + 1, &numval, |
| 5201 | NULL, opt_flags); |
| 5202 | *p = cc; |
| 5203 | if (opt_type == -3) |
| 5204 | { |
| 5205 | semsg(_(e_unknown_option), var_start); |
| 5206 | goto theend; |
| 5207 | } |
| 5208 | if (opt_type == -2 || opt_type == 0) |
| 5209 | type = &t_string; |
| 5210 | else |
| 5211 | type = &t_number; // both number and boolean option |
| 5212 | } |
| 5213 | else if (*var_start == '$') |
| 5214 | { |
| 5215 | dest = dest_env; |
| 5216 | type = &t_string; |
| 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 (*var_start == '@') |
| 5224 | { |
| 5225 | if (!valid_yank_reg(var_start[1], TRUE)) |
| 5226 | { |
| 5227 | emsg_invreg(var_start[1]); |
| 5228 | goto theend; |
| 5229 | } |
| 5230 | dest = dest_reg; |
| 5231 | type = &t_string; |
| 5232 | if (is_decl) |
| 5233 | { |
| 5234 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 5235 | goto theend; |
| 5236 | } |
| 5237 | } |
| 5238 | else if (STRNCMP(var_start, "g:", 2) == 0) |
| 5239 | { |
| 5240 | dest = dest_global; |
| 5241 | if (is_decl) |
| 5242 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5243 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5244 | goto theend; |
| 5245 | } |
| 5246 | } |
| 5247 | else if (STRNCMP(var_start, "b:", 2) == 0) |
| 5248 | { |
| 5249 | dest = dest_buffer; |
| 5250 | if (is_decl) |
| 5251 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5252 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5253 | goto theend; |
| 5254 | } |
| 5255 | } |
| 5256 | else if (STRNCMP(var_start, "w:", 2) == 0) |
| 5257 | { |
| 5258 | dest = dest_window; |
| 5259 | if (is_decl) |
| 5260 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5261 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5262 | goto theend; |
| 5263 | } |
| 5264 | } |
| 5265 | else if (STRNCMP(var_start, "t:", 2) == 0) |
| 5266 | { |
| 5267 | dest = dest_tab; |
| 5268 | if (is_decl) |
| 5269 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5270 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5271 | goto theend; |
| 5272 | } |
| 5273 | } |
| 5274 | else if (STRNCMP(var_start, "v:", 2) == 0) |
| 5275 | { |
| 5276 | typval_T *vtv; |
| 5277 | int di_flags; |
| 5278 | |
| 5279 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5280 | if (vimvaridx < 0) |
| 5281 | { |
| 5282 | semsg(_(e_var_notfound), var_start); |
| 5283 | goto theend; |
| 5284 | } |
| 5285 | // We use the current value of "sandbox" here, is that OK? |
| 5286 | if (var_check_ro(di_flags, name, FALSE)) |
| 5287 | goto theend; |
| 5288 | dest = dest_vimvar; |
| 5289 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 5290 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5291 | if (is_decl) |
| 5292 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5293 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5294 | goto theend; |
| 5295 | } |
| 5296 | } |
| 5297 | else |
| 5298 | { |
| 5299 | int idx; |
| 5300 | |
| 5301 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5302 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5303 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5304 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5305 | goto theend; |
| 5306 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5307 | |
| 5308 | lvar = lookup_local(var_start, varlen, cctx); |
| 5309 | if (lvar == NULL) |
| 5310 | { |
| 5311 | CLEAR_FIELD(arg_lvar); |
| 5312 | if (lookup_arg(var_start, varlen, |
| 5313 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 5314 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5315 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5316 | if (is_decl) |
| 5317 | { |
| 5318 | semsg(_(e_used_as_arg), name); |
| 5319 | goto theend; |
| 5320 | } |
| 5321 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5322 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5323 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5324 | if (lvar != NULL) |
| 5325 | { |
| 5326 | if (is_decl) |
| 5327 | { |
| 5328 | semsg(_("E1017: Variable already declared: %s"), name); |
| 5329 | goto theend; |
| 5330 | } |
| 5331 | else if (lvar->lv_const) |
| 5332 | { |
| 5333 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 5334 | name); |
| 5335 | goto theend; |
| 5336 | } |
| 5337 | } |
| 5338 | else if (STRNCMP(var_start, "s:", 2) == 0 |
| 5339 | || lookup_script(var_start, varlen) == OK |
| 5340 | || find_imported(var_start, varlen, cctx) != NULL) |
| 5341 | { |
| 5342 | dest = dest_script; |
| 5343 | if (is_decl) |
| 5344 | { |
| 5345 | semsg(_("E1054: Variable already declared in the script: %s"), |
| 5346 | name); |
| 5347 | goto theend; |
| 5348 | } |
| 5349 | } |
| 5350 | else if (name[1] == ':' && name[2] != NUL) |
| 5351 | { |
| 5352 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
| 5353 | name); |
| 5354 | goto theend; |
| 5355 | } |
| 5356 | else if (!is_decl) |
| 5357 | { |
| 5358 | semsg(_("E1089: unknown variable: %s"), name); |
| 5359 | goto theend; |
| 5360 | } |
| 5361 | } |
| 5362 | } |
| 5363 | |
| 5364 | // handle "a:name" as a name, not index "name" on "a" |
| 5365 | if (varlen > 1 || var_start[varlen] != ':') |
| 5366 | p = var_end; |
| 5367 | |
| 5368 | if (dest != dest_option) |
| 5369 | { |
| 5370 | if (is_decl && *p == ':') |
| 5371 | { |
| 5372 | // parse optional type: "let var: type = expr" |
| 5373 | if (!VIM_ISWHITE(p[1])) |
| 5374 | { |
| 5375 | semsg(_(e_white_after), ":"); |
| 5376 | goto theend; |
| 5377 | } |
| 5378 | p = skipwhite(p + 1); |
| 5379 | type = parse_type(&p, cctx->ctx_type_list); |
| 5380 | has_type = TRUE; |
| 5381 | } |
| 5382 | else if (lvar != NULL) |
| 5383 | type = lvar->lv_type; |
| 5384 | } |
| 5385 | |
| 5386 | if (oplen == 3 && !heredoc && dest != dest_global |
| 5387 | && type->tt_type != VAR_STRING |
| 5388 | && type->tt_type != VAR_ANY) |
| 5389 | { |
| 5390 | emsg(_("E1019: Can only concatenate to string")); |
| 5391 | goto theend; |
| 5392 | } |
| 5393 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5394 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5395 | { |
| 5396 | if (oplen > 1 && !heredoc) |
| 5397 | { |
| 5398 | // +=, /=, etc. require an existing variable |
| 5399 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 5400 | name); |
| 5401 | goto theend; |
| 5402 | } |
| 5403 | |
| 5404 | // new local variable |
| 5405 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
| 5406 | goto theend; |
| 5407 | lvar = reserve_local(cctx, var_start, varlen, |
| 5408 | cmdidx == CMD_const, type); |
| 5409 | if (lvar == NULL) |
| 5410 | goto theend; |
| 5411 | new_local = TRUE; |
| 5412 | } |
| 5413 | |
| 5414 | member_type = type; |
| 5415 | if (var_end > var_start + varlen) |
| 5416 | { |
| 5417 | // Something follows after the variable: "var[idx]". |
| 5418 | if (is_decl) |
| 5419 | { |
| 5420 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 5421 | goto theend; |
| 5422 | } |
| 5423 | |
| 5424 | if (var_start[varlen] == '[') |
| 5425 | { |
| 5426 | has_index = TRUE; |
| 5427 | if (type->tt_member == NULL) |
| 5428 | { |
| 5429 | semsg(_("E1088: cannot use an index on %s"), name); |
| 5430 | goto theend; |
| 5431 | } |
| 5432 | member_type = type->tt_member; |
| 5433 | } |
| 5434 | else |
| 5435 | { |
| 5436 | semsg("Not supported yet: %s", var_start); |
| 5437 | goto theend; |
| 5438 | } |
| 5439 | } |
| 5440 | else if (lvar == &arg_lvar) |
| 5441 | { |
| 5442 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 5443 | goto theend; |
| 5444 | } |
| 5445 | |
| 5446 | if (!heredoc) |
| 5447 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5448 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5449 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5450 | if (oplen > 0 && var_count == 0) |
| 5451 | { |
| 5452 | // skip over the "=" and the expression |
| 5453 | p = skipwhite(op + oplen); |
| 5454 | compile_expr0(&p, cctx); |
| 5455 | } |
| 5456 | } |
| 5457 | else if (oplen > 0) |
| 5458 | { |
| 5459 | type_T *stacktype; |
| 5460 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5461 | // For "var = expr" evaluate the expression. |
| 5462 | if (var_count == 0) |
| 5463 | { |
| 5464 | int r; |
| 5465 | |
| 5466 | // for "+=", "*=", "..=" etc. first load the current value |
| 5467 | if (*op != '=') |
| 5468 | { |
| 5469 | generate_loadvar(cctx, dest, name, lvar, type); |
| 5470 | |
| 5471 | if (has_index) |
| 5472 | { |
| 5473 | // TODO: get member from list or dict |
| 5474 | emsg("Index with operation not supported yet"); |
| 5475 | goto theend; |
| 5476 | } |
| 5477 | } |
| 5478 | |
| 5479 | // Compile the expression. Temporarily hide the new local |
| 5480 | // variable here, it is not available to this expression. |
| 5481 | if (new_local) |
| 5482 | --cctx->ctx_locals.ga_len; |
| 5483 | instr_count = instr->ga_len; |
| 5484 | p = skipwhite(op + oplen); |
| 5485 | r = compile_expr0(&p, cctx); |
| 5486 | if (new_local) |
| 5487 | ++cctx->ctx_locals.ga_len; |
| 5488 | if (r == FAIL) |
| 5489 | goto theend; |
| 5490 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5491 | else if (semicolon && var_idx == var_count - 1) |
| 5492 | { |
| 5493 | // For "[var; var] = expr" get the rest of the list |
| 5494 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5495 | goto theend; |
| 5496 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5497 | else |
| 5498 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5499 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5500 | // list. |
| 5501 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 5502 | return FAIL; |
| 5503 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5504 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5505 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5506 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5507 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5508 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5509 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5510 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5511 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5512 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5513 | emsg(_(e_cannot_use_void)); |
| 5514 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5515 | } |
| 5516 | else |
| 5517 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5518 | // An empty list or dict has a &t_void member, |
| 5519 | // for a variable that implies &t_any. |
| 5520 | if (stacktype == &t_list_empty) |
| 5521 | lvar->lv_type = &t_list_any; |
| 5522 | else if (stacktype == &t_dict_empty) |
| 5523 | lvar->lv_type = &t_dict_any; |
| 5524 | else |
| 5525 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5526 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5527 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5528 | else |
| 5529 | { |
| 5530 | type_T *use_type = lvar->lv_type; |
| 5531 | |
| 5532 | if (has_index) |
| 5533 | { |
| 5534 | use_type = use_type->tt_member; |
| 5535 | if (use_type == NULL) |
| 5536 | use_type = &t_void; |
| 5537 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5538 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5539 | == FAIL) |
| 5540 | goto theend; |
| 5541 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5542 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5543 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5544 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5545 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5546 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5547 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5548 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5549 | emsg(_(e_const_req_value)); |
| 5550 | goto theend; |
| 5551 | } |
| 5552 | else if (!has_type || dest == dest_option) |
| 5553 | { |
| 5554 | emsg(_(e_type_req)); |
| 5555 | goto theend; |
| 5556 | } |
| 5557 | else |
| 5558 | { |
| 5559 | // variables are always initialized |
| 5560 | if (ga_grow(instr, 1) == FAIL) |
| 5561 | goto theend; |
| 5562 | switch (member_type->tt_type) |
| 5563 | { |
| 5564 | case VAR_BOOL: |
| 5565 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5566 | break; |
| 5567 | case VAR_FLOAT: |
| 5568 | #ifdef FEAT_FLOAT |
| 5569 | generate_PUSHF(cctx, 0.0); |
| 5570 | #endif |
| 5571 | break; |
| 5572 | case VAR_STRING: |
| 5573 | generate_PUSHS(cctx, NULL); |
| 5574 | break; |
| 5575 | case VAR_BLOB: |
| 5576 | generate_PUSHBLOB(cctx, NULL); |
| 5577 | break; |
| 5578 | case VAR_FUNC: |
| 5579 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5580 | break; |
| 5581 | case VAR_LIST: |
| 5582 | generate_NEWLIST(cctx, 0); |
| 5583 | break; |
| 5584 | case VAR_DICT: |
| 5585 | generate_NEWDICT(cctx, 0); |
| 5586 | break; |
| 5587 | case VAR_JOB: |
| 5588 | generate_PUSHJOB(cctx, NULL); |
| 5589 | break; |
| 5590 | case VAR_CHANNEL: |
| 5591 | generate_PUSHCHANNEL(cctx, NULL); |
| 5592 | break; |
| 5593 | case VAR_NUMBER: |
| 5594 | case VAR_UNKNOWN: |
| 5595 | case VAR_ANY: |
| 5596 | case VAR_PARTIAL: |
| 5597 | case VAR_VOID: |
| 5598 | case VAR_SPECIAL: // cannot happen |
| 5599 | generate_PUSHNR(cctx, 0); |
| 5600 | break; |
| 5601 | } |
| 5602 | } |
| 5603 | if (var_count == 0) |
| 5604 | end = p; |
| 5605 | } |
| 5606 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5607 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5608 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5609 | break; |
| 5610 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5611 | if (oplen > 0 && *op != '=') |
| 5612 | { |
| 5613 | type_T *expected = &t_number; |
| 5614 | type_T *stacktype; |
| 5615 | |
| 5616 | // TODO: if type is known use float or any operation |
| 5617 | |
| 5618 | if (*op == '.') |
| 5619 | expected = &t_string; |
| 5620 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5621 | if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5622 | goto theend; |
| 5623 | |
| 5624 | if (*op == '.') |
| 5625 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 5626 | else |
| 5627 | { |
| 5628 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 5629 | |
| 5630 | if (isn == NULL) |
| 5631 | goto theend; |
| 5632 | switch (*op) |
| 5633 | { |
| 5634 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 5635 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 5636 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 5637 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 5638 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 5639 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5640 | } |
| 5641 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5642 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5643 | if (has_index) |
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 | int r; |
| 5646 | |
| 5647 | // Compile the "idx" in "var[idx]". |
| 5648 | if (new_local) |
| 5649 | --cctx->ctx_locals.ga_len; |
| 5650 | p = skipwhite(var_start + varlen + 1); |
| 5651 | r = compile_expr0(&p, cctx); |
| 5652 | if (new_local) |
| 5653 | ++cctx->ctx_locals.ga_len; |
| 5654 | if (r == FAIL) |
| 5655 | goto theend; |
| 5656 | if (*skipwhite(p) != ']') |
| 5657 | { |
| 5658 | emsg(_(e_missbrac)); |
| 5659 | goto theend; |
| 5660 | } |
| 5661 | if (type->tt_type == VAR_DICT |
| 5662 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5663 | goto theend; |
| 5664 | if (type->tt_type == VAR_LIST |
| 5665 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5666 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5667 | { |
| 5668 | emsg(_(e_number_exp)); |
| 5669 | goto theend; |
| 5670 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5671 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5672 | // Load the dict or list. On the stack we then have: |
| 5673 | // - value |
| 5674 | // - index |
| 5675 | // - variable |
| 5676 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5677 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5678 | if (type->tt_type == VAR_LIST) |
| 5679 | { |
| 5680 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5681 | return FAIL; |
| 5682 | } |
| 5683 | else if (type->tt_type == VAR_DICT) |
| 5684 | { |
| 5685 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5686 | return FAIL; |
| 5687 | } |
| 5688 | else |
| 5689 | { |
| 5690 | emsg(_(e_listreq)); |
| 5691 | goto theend; |
| 5692 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5693 | } |
| 5694 | else |
| 5695 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5696 | switch (dest) |
| 5697 | { |
| 5698 | case dest_option: |
| 5699 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5700 | break; |
| 5701 | case dest_global: |
| 5702 | // include g: with the name, easier to execute that way |
| 5703 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5704 | break; |
| 5705 | case dest_buffer: |
| 5706 | // include b: with the name, easier to execute that way |
| 5707 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5708 | break; |
| 5709 | case dest_window: |
| 5710 | // include w: with the name, easier to execute that way |
| 5711 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5712 | break; |
| 5713 | case dest_tab: |
| 5714 | // include t: with the name, easier to execute that way |
| 5715 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5716 | break; |
| 5717 | case dest_env: |
| 5718 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5719 | break; |
| 5720 | case dest_reg: |
| 5721 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5722 | break; |
| 5723 | case dest_vimvar: |
| 5724 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5725 | break; |
| 5726 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5727 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5728 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5729 | imported_T *import = NULL; |
| 5730 | int sid = current_sctx.sc_sid; |
| 5731 | int idx; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5732 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5733 | if (name[1] != ':') |
| 5734 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5735 | import = find_imported(name, 0, cctx); |
| 5736 | if (import != NULL) |
| 5737 | sid = import->imp_sid; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5738 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5739 | |
| 5740 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 5741 | // TODO: specific type |
| 5742 | if (idx < 0) |
| 5743 | { |
| 5744 | char_u *name_s = name; |
| 5745 | |
| 5746 | // Include s: in the name for store_var() |
| 5747 | if (name[1] != ':') |
| 5748 | { |
| 5749 | int len = (int)STRLEN(name) + 3; |
| 5750 | |
| 5751 | name_s = alloc(len); |
| 5752 | if (name_s == NULL) |
| 5753 | name_s = name; |
| 5754 | else |
| 5755 | vim_snprintf((char *)name_s, len, |
| 5756 | "s:%s", name); |
| 5757 | } |
| 5758 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5759 | &t_any); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5760 | if (name_s != name) |
| 5761 | vim_free(name_s); |
| 5762 | } |
| 5763 | else |
| 5764 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5765 | sid, idx, &t_any); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5766 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5767 | break; |
| 5768 | case dest_local: |
| 5769 | if (lvar != NULL) |
| 5770 | { |
| 5771 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5772 | + instr->ga_len - 1; |
| 5773 | |
| 5774 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5775 | // ISN_STORE into ISN_STORENR |
| 5776 | if (!lvar->lv_from_outer |
| 5777 | && instr->ga_len == instr_count + 1 |
| 5778 | && isn->isn_type == ISN_PUSHNR) |
| 5779 | { |
| 5780 | varnumber_T val = isn->isn_arg.number; |
| 5781 | |
| 5782 | isn->isn_type = ISN_STORENR; |
| 5783 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5784 | isn->isn_arg.storenr.stnr_val = val; |
| 5785 | if (stack->ga_len > 0) |
| 5786 | --stack->ga_len; |
| 5787 | } |
| 5788 | else if (lvar->lv_from_outer) |
| 5789 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5790 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5791 | else |
| 5792 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5793 | } |
| 5794 | break; |
| 5795 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5796 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5797 | |
| 5798 | if (var_idx + 1 < var_count) |
| 5799 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5800 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5801 | |
| 5802 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5803 | if (var_count > 0 && !semicolon) |
| 5804 | { |
| 5805 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5806 | goto theend; |
| 5807 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5808 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5809 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5810 | |
| 5811 | theend: |
| 5812 | vim_free(name); |
| 5813 | return ret; |
| 5814 | } |
| 5815 | |
| 5816 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5817 | * Check if "name" can be "unlet". |
| 5818 | */ |
| 5819 | int |
| 5820 | check_vim9_unlet(char_u *name) |
| 5821 | { |
| 5822 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5823 | { |
| 5824 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5825 | return FAIL; |
| 5826 | } |
| 5827 | return OK; |
| 5828 | } |
| 5829 | |
| 5830 | /* |
| 5831 | * Callback passed to ex_unletlock(). |
| 5832 | */ |
| 5833 | static int |
| 5834 | compile_unlet( |
| 5835 | lval_T *lvp, |
| 5836 | char_u *name_end, |
| 5837 | exarg_T *eap, |
| 5838 | int deep UNUSED, |
| 5839 | void *coookie) |
| 5840 | { |
| 5841 | cctx_T *cctx = coookie; |
| 5842 | |
| 5843 | if (lvp->ll_tv == NULL) |
| 5844 | { |
| 5845 | char_u *p = lvp->ll_name; |
| 5846 | int cc = *name_end; |
| 5847 | int ret = OK; |
| 5848 | |
| 5849 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5850 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5851 | if (*p == '$') |
| 5852 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5853 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5854 | ret = FAIL; |
| 5855 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5856 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5857 | |
| 5858 | *name_end = cc; |
| 5859 | return ret; |
| 5860 | } |
| 5861 | |
| 5862 | // TODO: unlet {list}[idx] |
| 5863 | // TODO: unlet {dict}[key] |
| 5864 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5865 | return FAIL; |
| 5866 | } |
| 5867 | |
| 5868 | /* |
| 5869 | * compile "unlet var", "lock var" and "unlock var" |
| 5870 | * "arg" points to "var". |
| 5871 | */ |
| 5872 | static char_u * |
| 5873 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5874 | { |
| 5875 | char_u *p = arg; |
| 5876 | |
| 5877 | if (eap->cmdidx != CMD_unlet) |
| 5878 | { |
| 5879 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5880 | return NULL; |
| 5881 | } |
| 5882 | |
| 5883 | if (*p == '!') |
| 5884 | { |
| 5885 | p = skipwhite(p + 1); |
| 5886 | eap->forceit = TRUE; |
| 5887 | } |
| 5888 | |
| 5889 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5890 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5891 | } |
| 5892 | |
| 5893 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5894 | * Compile an :import command. |
| 5895 | */ |
| 5896 | static char_u * |
| 5897 | compile_import(char_u *arg, cctx_T *cctx) |
| 5898 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5899 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5900 | } |
| 5901 | |
| 5902 | /* |
| 5903 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5904 | */ |
| 5905 | static int |
| 5906 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5907 | { |
| 5908 | garray_T *instr = &cctx->ctx_instr; |
| 5909 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5910 | |
| 5911 | if (endlabel == NULL) |
| 5912 | return FAIL; |
| 5913 | endlabel->el_next = *el; |
| 5914 | *el = endlabel; |
| 5915 | endlabel->el_end_label = instr->ga_len; |
| 5916 | |
| 5917 | generate_JUMP(cctx, when, 0); |
| 5918 | return OK; |
| 5919 | } |
| 5920 | |
| 5921 | static void |
| 5922 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5923 | { |
| 5924 | garray_T *instr = &cctx->ctx_instr; |
| 5925 | |
| 5926 | while (*el != NULL) |
| 5927 | { |
| 5928 | endlabel_T *cur = (*el); |
| 5929 | isn_T *isn; |
| 5930 | |
| 5931 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5932 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5933 | *el = cur->el_next; |
| 5934 | vim_free(cur); |
| 5935 | } |
| 5936 | } |
| 5937 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5938 | static void |
| 5939 | compile_free_jump_to_end(endlabel_T **el) |
| 5940 | { |
| 5941 | while (*el != NULL) |
| 5942 | { |
| 5943 | endlabel_T *cur = (*el); |
| 5944 | |
| 5945 | *el = cur->el_next; |
| 5946 | vim_free(cur); |
| 5947 | } |
| 5948 | } |
| 5949 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5950 | /* |
| 5951 | * Create a new scope and set up the generic items. |
| 5952 | */ |
| 5953 | static scope_T * |
| 5954 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5955 | { |
| 5956 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5957 | |
| 5958 | if (scope == NULL) |
| 5959 | return NULL; |
| 5960 | scope->se_outer = cctx->ctx_scope; |
| 5961 | cctx->ctx_scope = scope; |
| 5962 | scope->se_type = type; |
| 5963 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5964 | return scope; |
| 5965 | } |
| 5966 | |
| 5967 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5968 | * Free the current scope and go back to the outer scope. |
| 5969 | */ |
| 5970 | static void |
| 5971 | drop_scope(cctx_T *cctx) |
| 5972 | { |
| 5973 | scope_T *scope = cctx->ctx_scope; |
| 5974 | |
| 5975 | if (scope == NULL) |
| 5976 | { |
| 5977 | iemsg("calling drop_scope() without a scope"); |
| 5978 | return; |
| 5979 | } |
| 5980 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5981 | switch (scope->se_type) |
| 5982 | { |
| 5983 | case IF_SCOPE: |
| 5984 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5985 | case FOR_SCOPE: |
| 5986 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5987 | case WHILE_SCOPE: |
| 5988 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5989 | case TRY_SCOPE: |
| 5990 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5991 | case NO_SCOPE: |
| 5992 | case BLOCK_SCOPE: |
| 5993 | break; |
| 5994 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5995 | vim_free(scope); |
| 5996 | } |
| 5997 | |
| 5998 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5999 | * compile "if expr" |
| 6000 | * |
| 6001 | * "if expr" Produces instructions: |
| 6002 | * EVAL expr Push result of "expr" |
| 6003 | * JUMP_IF_FALSE end |
| 6004 | * ... body ... |
| 6005 | * end: |
| 6006 | * |
| 6007 | * "if expr | else" Produces instructions: |
| 6008 | * EVAL expr Push result of "expr" |
| 6009 | * JUMP_IF_FALSE else |
| 6010 | * ... body ... |
| 6011 | * JUMP_ALWAYS end |
| 6012 | * else: |
| 6013 | * ... body ... |
| 6014 | * end: |
| 6015 | * |
| 6016 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 6017 | * EVAL expr Push result of "expr" |
| 6018 | * JUMP_IF_FALSE elseif |
| 6019 | * ... body ... |
| 6020 | * JUMP_ALWAYS end |
| 6021 | * elseif: |
| 6022 | * EVAL expr Push result of "expr" |
| 6023 | * JUMP_IF_FALSE else |
| 6024 | * ... body ... |
| 6025 | * JUMP_ALWAYS end |
| 6026 | * else: |
| 6027 | * ... body ... |
| 6028 | * end: |
| 6029 | */ |
| 6030 | static char_u * |
| 6031 | compile_if(char_u *arg, cctx_T *cctx) |
| 6032 | { |
| 6033 | char_u *p = arg; |
| 6034 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6035 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6036 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6037 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6038 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6039 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6040 | CLEAR_FIELD(ppconst); |
| 6041 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6042 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6043 | clear_ppconst(&ppconst); |
| 6044 | return NULL; |
| 6045 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6046 | if (cctx->ctx_skip == SKIP_YES) |
| 6047 | clear_ppconst(&ppconst); |
| 6048 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6049 | { |
| 6050 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6051 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6052 | clear_ppconst(&ppconst); |
| 6053 | } |
| 6054 | else |
| 6055 | { |
| 6056 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6057 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6058 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6059 | return NULL; |
| 6060 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6061 | |
| 6062 | scope = new_scope(cctx, IF_SCOPE); |
| 6063 | if (scope == NULL) |
| 6064 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6065 | scope->se_skip_save = skip_save; |
| 6066 | // "is_had_return" will be reset if any block does not end in :return |
| 6067 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6068 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6069 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6070 | { |
| 6071 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6072 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6073 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6074 | } |
| 6075 | else |
| 6076 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6077 | |
| 6078 | return p; |
| 6079 | } |
| 6080 | |
| 6081 | static char_u * |
| 6082 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 6083 | { |
| 6084 | char_u *p = arg; |
| 6085 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6086 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6087 | isn_T *isn; |
| 6088 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6089 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6090 | |
| 6091 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6092 | { |
| 6093 | emsg(_(e_elseif_without_if)); |
| 6094 | return NULL; |
| 6095 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6096 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6097 | if (!cctx->ctx_had_return) |
| 6098 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6099 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6100 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6101 | { |
| 6102 | 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] | 6103 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6104 | return NULL; |
| 6105 | // previous "if" or "elseif" jumps here |
| 6106 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6107 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6108 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6109 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6110 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6111 | CLEAR_FIELD(ppconst); |
| 6112 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6113 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6114 | clear_ppconst(&ppconst); |
| 6115 | return NULL; |
| 6116 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6117 | if (scope->se_skip_save == SKIP_YES) |
| 6118 | clear_ppconst(&ppconst); |
| 6119 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6120 | { |
| 6121 | // The expression results in a constant. |
| 6122 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6123 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6124 | clear_ppconst(&ppconst); |
| 6125 | scope->se_u.se_if.is_if_label = -1; |
| 6126 | } |
| 6127 | else |
| 6128 | { |
| 6129 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6130 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6131 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6132 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6133 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6134 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6135 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6136 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6137 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6138 | |
| 6139 | return p; |
| 6140 | } |
| 6141 | |
| 6142 | static char_u * |
| 6143 | compile_else(char_u *arg, cctx_T *cctx) |
| 6144 | { |
| 6145 | char_u *p = arg; |
| 6146 | garray_T *instr = &cctx->ctx_instr; |
| 6147 | isn_T *isn; |
| 6148 | scope_T *scope = cctx->ctx_scope; |
| 6149 | |
| 6150 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6151 | { |
| 6152 | emsg(_(e_else_without_if)); |
| 6153 | return NULL; |
| 6154 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6155 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6156 | if (!cctx->ctx_had_return) |
| 6157 | scope->se_u.se_if.is_had_return = FALSE; |
| 6158 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6159 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6160 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6161 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6162 | // 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] | 6163 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6164 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6165 | if (!cctx->ctx_had_return |
| 6166 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6167 | JUMP_ALWAYS, cctx) == FAIL) |
| 6168 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6169 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6170 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6171 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6172 | { |
| 6173 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6174 | { |
| 6175 | // previous "if" or "elseif" jumps here |
| 6176 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6177 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6178 | scope->se_u.se_if.is_if_label = -1; |
| 6179 | } |
| 6180 | } |
| 6181 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6182 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6183 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6184 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6185 | |
| 6186 | return p; |
| 6187 | } |
| 6188 | |
| 6189 | static char_u * |
| 6190 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6191 | { |
| 6192 | scope_T *scope = cctx->ctx_scope; |
| 6193 | ifscope_T *ifscope; |
| 6194 | garray_T *instr = &cctx->ctx_instr; |
| 6195 | isn_T *isn; |
| 6196 | |
| 6197 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6198 | { |
| 6199 | emsg(_(e_endif_without_if)); |
| 6200 | return NULL; |
| 6201 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6202 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6203 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6204 | if (!cctx->ctx_had_return) |
| 6205 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6206 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6207 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6208 | { |
| 6209 | // previous "if" or "elseif" jumps here |
| 6210 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6211 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6212 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6213 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6214 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6215 | cctx->ctx_skip = scope->se_skip_save; |
| 6216 | |
| 6217 | // If all the blocks end in :return and there is an :else then the |
| 6218 | // had_return flag is set. |
| 6219 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6220 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6221 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6222 | return arg; |
| 6223 | } |
| 6224 | |
| 6225 | /* |
| 6226 | * compile "for var in expr" |
| 6227 | * |
| 6228 | * Produces instructions: |
| 6229 | * PUSHNR -1 |
| 6230 | * STORE loop-idx Set index to -1 |
| 6231 | * EVAL expr Push result of "expr" |
| 6232 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6233 | * - if beyond end, jump to "end" |
| 6234 | * - otherwise get item from list and push it |
| 6235 | * STORE var Store item in "var" |
| 6236 | * ... body ... |
| 6237 | * JUMP top Jump back to repeat |
| 6238 | * end: DROP Drop the result of "expr" |
| 6239 | * |
| 6240 | */ |
| 6241 | static char_u * |
| 6242 | compile_for(char_u *arg, cctx_T *cctx) |
| 6243 | { |
| 6244 | char_u *p; |
| 6245 | size_t varlen; |
| 6246 | garray_T *instr = &cctx->ctx_instr; |
| 6247 | garray_T *stack = &cctx->ctx_type_stack; |
| 6248 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6249 | lvar_T *loop_lvar; // loop iteration variable |
| 6250 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6251 | type_T *vartype; |
| 6252 | |
| 6253 | // TODO: list of variables: "for [key, value] in dict" |
| 6254 | // parse "var" |
| 6255 | for (p = arg; eval_isnamec1(*p); ++p) |
| 6256 | ; |
| 6257 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6258 | var_lvar = lookup_local(arg, varlen, cctx); |
| 6259 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6260 | { |
| 6261 | semsg(_("E1023: variable already defined: %s"), arg); |
| 6262 | return NULL; |
| 6263 | } |
| 6264 | |
| 6265 | // consume "in" |
| 6266 | p = skipwhite(p); |
| 6267 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 6268 | { |
| 6269 | emsg(_(e_missing_in)); |
| 6270 | return NULL; |
| 6271 | } |
| 6272 | p = skipwhite(p + 2); |
| 6273 | |
| 6274 | |
| 6275 | scope = new_scope(cctx, FOR_SCOPE); |
| 6276 | if (scope == NULL) |
| 6277 | return NULL; |
| 6278 | |
| 6279 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6280 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6281 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6282 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6283 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6284 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6285 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6286 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6287 | |
| 6288 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6289 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 6290 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6291 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6292 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6293 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6294 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6295 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6296 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6297 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6298 | |
| 6299 | // compile "expr", it remains on the stack until "endfor" |
| 6300 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6301 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6302 | { |
| 6303 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6304 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6305 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6306 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6307 | // Now that we know the type of "var", check that it is a list, now or at |
| 6308 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6309 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 6310 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6311 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6312 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6313 | return NULL; |
| 6314 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6315 | 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] | 6316 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6317 | |
| 6318 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6319 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6320 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6321 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 6322 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6323 | |
| 6324 | return arg; |
| 6325 | } |
| 6326 | |
| 6327 | /* |
| 6328 | * compile "endfor" |
| 6329 | */ |
| 6330 | static char_u * |
| 6331 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 6332 | { |
| 6333 | garray_T *instr = &cctx->ctx_instr; |
| 6334 | scope_T *scope = cctx->ctx_scope; |
| 6335 | forscope_T *forscope; |
| 6336 | isn_T *isn; |
| 6337 | |
| 6338 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 6339 | { |
| 6340 | emsg(_(e_for)); |
| 6341 | return NULL; |
| 6342 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6343 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6344 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6345 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6346 | |
| 6347 | // At end of ":for" scope jump back to the FOR instruction. |
| 6348 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 6349 | |
| 6350 | // Fill in the "end" label in the FOR statement so it can jump here |
| 6351 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 6352 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 6353 | |
| 6354 | // Fill in the "end" label any BREAK statements |
| 6355 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 6356 | |
| 6357 | // Below the ":for" scope drop the "expr" list from the stack. |
| 6358 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 6359 | return NULL; |
| 6360 | |
| 6361 | vim_free(scope); |
| 6362 | |
| 6363 | return arg; |
| 6364 | } |
| 6365 | |
| 6366 | /* |
| 6367 | * compile "while expr" |
| 6368 | * |
| 6369 | * Produces instructions: |
| 6370 | * top: EVAL expr Push result of "expr" |
| 6371 | * JUMP_IF_FALSE end jump if false |
| 6372 | * ... body ... |
| 6373 | * JUMP top Jump back to repeat |
| 6374 | * end: |
| 6375 | * |
| 6376 | */ |
| 6377 | static char_u * |
| 6378 | compile_while(char_u *arg, cctx_T *cctx) |
| 6379 | { |
| 6380 | char_u *p = arg; |
| 6381 | garray_T *instr = &cctx->ctx_instr; |
| 6382 | scope_T *scope; |
| 6383 | |
| 6384 | scope = new_scope(cctx, WHILE_SCOPE); |
| 6385 | if (scope == NULL) |
| 6386 | return NULL; |
| 6387 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6388 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6389 | |
| 6390 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6391 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6392 | return NULL; |
| 6393 | |
| 6394 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6395 | 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] | 6396 | JUMP_IF_FALSE, cctx) == FAIL) |
| 6397 | return FAIL; |
| 6398 | |
| 6399 | return p; |
| 6400 | } |
| 6401 | |
| 6402 | /* |
| 6403 | * compile "endwhile" |
| 6404 | */ |
| 6405 | static char_u * |
| 6406 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 6407 | { |
| 6408 | scope_T *scope = cctx->ctx_scope; |
| 6409 | |
| 6410 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 6411 | { |
| 6412 | emsg(_(e_while)); |
| 6413 | return NULL; |
| 6414 | } |
| 6415 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6416 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6417 | |
| 6418 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6419 | 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] | 6420 | |
| 6421 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 6422 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6423 | 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] | 6424 | |
| 6425 | vim_free(scope); |
| 6426 | |
| 6427 | return arg; |
| 6428 | } |
| 6429 | |
| 6430 | /* |
| 6431 | * compile "continue" |
| 6432 | */ |
| 6433 | static char_u * |
| 6434 | compile_continue(char_u *arg, cctx_T *cctx) |
| 6435 | { |
| 6436 | scope_T *scope = cctx->ctx_scope; |
| 6437 | |
| 6438 | for (;;) |
| 6439 | { |
| 6440 | if (scope == NULL) |
| 6441 | { |
| 6442 | emsg(_(e_continue)); |
| 6443 | return NULL; |
| 6444 | } |
| 6445 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6446 | break; |
| 6447 | scope = scope->se_outer; |
| 6448 | } |
| 6449 | |
| 6450 | // Jump back to the FOR or WHILE instruction. |
| 6451 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6452 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 6453 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6454 | return arg; |
| 6455 | } |
| 6456 | |
| 6457 | /* |
| 6458 | * compile "break" |
| 6459 | */ |
| 6460 | static char_u * |
| 6461 | compile_break(char_u *arg, cctx_T *cctx) |
| 6462 | { |
| 6463 | scope_T *scope = cctx->ctx_scope; |
| 6464 | endlabel_T **el; |
| 6465 | |
| 6466 | for (;;) |
| 6467 | { |
| 6468 | if (scope == NULL) |
| 6469 | { |
| 6470 | emsg(_(e_break)); |
| 6471 | return NULL; |
| 6472 | } |
| 6473 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6474 | break; |
| 6475 | scope = scope->se_outer; |
| 6476 | } |
| 6477 | |
| 6478 | // Jump to the end of the FOR or WHILE loop. |
| 6479 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6480 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6481 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6482 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6483 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 6484 | return FAIL; |
| 6485 | |
| 6486 | return arg; |
| 6487 | } |
| 6488 | |
| 6489 | /* |
| 6490 | * compile "{" start of block |
| 6491 | */ |
| 6492 | static char_u * |
| 6493 | compile_block(char_u *arg, cctx_T *cctx) |
| 6494 | { |
| 6495 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6496 | return NULL; |
| 6497 | return skipwhite(arg + 1); |
| 6498 | } |
| 6499 | |
| 6500 | /* |
| 6501 | * compile end of block: drop one scope |
| 6502 | */ |
| 6503 | static void |
| 6504 | compile_endblock(cctx_T *cctx) |
| 6505 | { |
| 6506 | scope_T *scope = cctx->ctx_scope; |
| 6507 | |
| 6508 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6509 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6510 | vim_free(scope); |
| 6511 | } |
| 6512 | |
| 6513 | /* |
| 6514 | * compile "try" |
| 6515 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6516 | * finally. |
| 6517 | * Creates another scope for the "try" block itself. |
| 6518 | * TRY instruction sets up exception handling at runtime. |
| 6519 | * |
| 6520 | * "try" |
| 6521 | * TRY -> catch1, -> finally push trystack entry |
| 6522 | * ... try block |
| 6523 | * "throw {exception}" |
| 6524 | * EVAL {exception} |
| 6525 | * THROW create exception |
| 6526 | * ... try block |
| 6527 | * " catch {expr}" |
| 6528 | * JUMP -> finally |
| 6529 | * catch1: PUSH exeception |
| 6530 | * EVAL {expr} |
| 6531 | * MATCH |
| 6532 | * JUMP nomatch -> catch2 |
| 6533 | * CATCH remove exception |
| 6534 | * ... catch block |
| 6535 | * " catch" |
| 6536 | * JUMP -> finally |
| 6537 | * catch2: CATCH remove exception |
| 6538 | * ... catch block |
| 6539 | * " finally" |
| 6540 | * finally: |
| 6541 | * ... finally block |
| 6542 | * " endtry" |
| 6543 | * ENDTRY pop trystack entry, may rethrow |
| 6544 | */ |
| 6545 | static char_u * |
| 6546 | compile_try(char_u *arg, cctx_T *cctx) |
| 6547 | { |
| 6548 | garray_T *instr = &cctx->ctx_instr; |
| 6549 | scope_T *try_scope; |
| 6550 | scope_T *scope; |
| 6551 | |
| 6552 | // scope that holds the jumps that go to catch/finally/endtry |
| 6553 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6554 | if (try_scope == NULL) |
| 6555 | return NULL; |
| 6556 | |
| 6557 | // "catch" is set when the first ":catch" is found. |
| 6558 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6559 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6560 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6561 | return NULL; |
| 6562 | |
| 6563 | // scope for the try block itself |
| 6564 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6565 | if (scope == NULL) |
| 6566 | return NULL; |
| 6567 | |
| 6568 | return arg; |
| 6569 | } |
| 6570 | |
| 6571 | /* |
| 6572 | * compile "catch {expr}" |
| 6573 | */ |
| 6574 | static char_u * |
| 6575 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6576 | { |
| 6577 | scope_T *scope = cctx->ctx_scope; |
| 6578 | garray_T *instr = &cctx->ctx_instr; |
| 6579 | char_u *p; |
| 6580 | isn_T *isn; |
| 6581 | |
| 6582 | // end block scope from :try or :catch |
| 6583 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6584 | compile_endblock(cctx); |
| 6585 | scope = cctx->ctx_scope; |
| 6586 | |
| 6587 | // Error if not in a :try scope |
| 6588 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6589 | { |
| 6590 | emsg(_(e_catch)); |
| 6591 | return NULL; |
| 6592 | } |
| 6593 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6594 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6595 | { |
| 6596 | emsg(_("E1033: catch unreachable after catch-all")); |
| 6597 | return NULL; |
| 6598 | } |
| 6599 | |
| 6600 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6601 | 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] | 6602 | JUMP_ALWAYS, cctx) == FAIL) |
| 6603 | return NULL; |
| 6604 | |
| 6605 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6606 | 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] | 6607 | if (isn->isn_arg.try.try_catch == 0) |
| 6608 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6609 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6610 | { |
| 6611 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6612 | 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] | 6613 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6614 | } |
| 6615 | |
| 6616 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6617 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6618 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6619 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6620 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6621 | } |
| 6622 | else |
| 6623 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6624 | char_u *end; |
| 6625 | char_u *pat; |
| 6626 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6627 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6628 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6629 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6630 | // Push v:exception, push {expr} and MATCH |
| 6631 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6632 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6633 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6634 | if (*end != *p) |
| 6635 | { |
| 6636 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 6637 | vim_free(tofree); |
| 6638 | return FAIL; |
| 6639 | } |
| 6640 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6641 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6642 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6643 | len = (int)(end - tofree); |
| 6644 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6645 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6646 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6647 | if (pat == NULL) |
| 6648 | return FAIL; |
| 6649 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6650 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6651 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6652 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6653 | return NULL; |
| 6654 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6655 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6656 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6657 | return NULL; |
| 6658 | } |
| 6659 | |
| 6660 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6661 | return NULL; |
| 6662 | |
| 6663 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6664 | return NULL; |
| 6665 | return p; |
| 6666 | } |
| 6667 | |
| 6668 | static char_u * |
| 6669 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6670 | { |
| 6671 | scope_T *scope = cctx->ctx_scope; |
| 6672 | garray_T *instr = &cctx->ctx_instr; |
| 6673 | isn_T *isn; |
| 6674 | |
| 6675 | // end block scope from :try or :catch |
| 6676 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6677 | compile_endblock(cctx); |
| 6678 | scope = cctx->ctx_scope; |
| 6679 | |
| 6680 | // Error if not in a :try scope |
| 6681 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6682 | { |
| 6683 | emsg(_(e_finally)); |
| 6684 | return NULL; |
| 6685 | } |
| 6686 | |
| 6687 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6688 | 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] | 6689 | if (isn->isn_arg.try.try_finally != 0) |
| 6690 | { |
| 6691 | emsg(_(e_finally_dup)); |
| 6692 | return NULL; |
| 6693 | } |
| 6694 | |
| 6695 | // 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] | 6696 | 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] | 6697 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6698 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6699 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6700 | { |
| 6701 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6702 | 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] | 6703 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6704 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6705 | } |
| 6706 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6707 | // TODO: set index in ts_finally_label jumps |
| 6708 | |
| 6709 | return arg; |
| 6710 | } |
| 6711 | |
| 6712 | static char_u * |
| 6713 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6714 | { |
| 6715 | scope_T *scope = cctx->ctx_scope; |
| 6716 | garray_T *instr = &cctx->ctx_instr; |
| 6717 | isn_T *isn; |
| 6718 | |
| 6719 | // end block scope from :catch or :finally |
| 6720 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6721 | compile_endblock(cctx); |
| 6722 | scope = cctx->ctx_scope; |
| 6723 | |
| 6724 | // Error if not in a :try scope |
| 6725 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6726 | { |
| 6727 | if (scope == NULL) |
| 6728 | emsg(_(e_no_endtry)); |
| 6729 | else if (scope->se_type == WHILE_SCOPE) |
| 6730 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6731 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6732 | emsg(_(e_endfor)); |
| 6733 | else |
| 6734 | emsg(_(e_endif)); |
| 6735 | return NULL; |
| 6736 | } |
| 6737 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6738 | 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] | 6739 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6740 | { |
| 6741 | emsg(_("E1032: missing :catch or :finally")); |
| 6742 | return NULL; |
| 6743 | } |
| 6744 | |
| 6745 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6746 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6747 | 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] | 6748 | |
| 6749 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6750 | if (isn->isn_arg.try.try_catch == 0) |
| 6751 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6752 | if (isn->isn_arg.try.try_finally == 0) |
| 6753 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6754 | |
| 6755 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6756 | { |
| 6757 | // Last catch without match jumps here |
| 6758 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6759 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6760 | } |
| 6761 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6762 | compile_endblock(cctx); |
| 6763 | |
| 6764 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6765 | return NULL; |
| 6766 | return arg; |
| 6767 | } |
| 6768 | |
| 6769 | /* |
| 6770 | * compile "throw {expr}" |
| 6771 | */ |
| 6772 | static char_u * |
| 6773 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6774 | { |
| 6775 | char_u *p = skipwhite(arg); |
| 6776 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6777 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6778 | return NULL; |
| 6779 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6780 | return NULL; |
| 6781 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6782 | return NULL; |
| 6783 | |
| 6784 | return p; |
| 6785 | } |
| 6786 | |
| 6787 | /* |
| 6788 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6789 | * compile "echomsg expr" |
| 6790 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6791 | * compile "execute expr" |
| 6792 | */ |
| 6793 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6794 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6795 | { |
| 6796 | char_u *p = arg; |
| 6797 | int count = 0; |
| 6798 | |
| 6799 | for (;;) |
| 6800 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6801 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6802 | return NULL; |
| 6803 | ++count; |
| 6804 | p = skipwhite(p); |
| 6805 | if (ends_excmd(*p)) |
| 6806 | break; |
| 6807 | } |
| 6808 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6809 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6810 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6811 | else if (cmdidx == CMD_execute) |
| 6812 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6813 | else if (cmdidx == CMD_echomsg) |
| 6814 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6815 | else |
| 6816 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6817 | return p; |
| 6818 | } |
| 6819 | |
| 6820 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6821 | * A command that is not compiled, execute with legacy code. |
| 6822 | */ |
| 6823 | static char_u * |
| 6824 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6825 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6826 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6827 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6828 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6829 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6830 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6831 | goto theend; |
| 6832 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6833 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6834 | { |
| 6835 | long argt = excmd_get_argt(eap->cmdidx); |
| 6836 | int usefilter = FALSE; |
| 6837 | |
| 6838 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6839 | |
| 6840 | // If the command can be followed by a bar, find the bar and truncate |
| 6841 | // it, so that the following command can be compiled. |
| 6842 | // The '|' is overwritten with a NUL, it is put back below. |
| 6843 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6844 | && *eap->arg == '!') |
| 6845 | // :w !filter or :r !filter or :r! filter |
| 6846 | usefilter = TRUE; |
| 6847 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6848 | { |
| 6849 | separate_nextcmd(eap); |
| 6850 | if (eap->nextcmd != NULL) |
| 6851 | nextcmd = eap->nextcmd; |
| 6852 | } |
| 6853 | } |
| 6854 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6855 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6856 | { |
| 6857 | // expand filename in "syntax include [@group] filename" |
| 6858 | has_expr = TRUE; |
| 6859 | eap->arg = skipwhite(eap->arg + 7); |
| 6860 | if (*eap->arg == '@') |
| 6861 | eap->arg = skiptowhite(eap->arg); |
| 6862 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6863 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6864 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6865 | { |
| 6866 | int count = 0; |
| 6867 | char_u *start = skipwhite(line); |
| 6868 | |
| 6869 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6870 | // PUSHS ":cmd xxx" |
| 6871 | // eval expr1 |
| 6872 | // PUSHS "yyy" |
| 6873 | // eval expr2 |
| 6874 | // PUSHS "zzz" |
| 6875 | // EXECCONCAT 5 |
| 6876 | for (;;) |
| 6877 | { |
| 6878 | if (p > start) |
| 6879 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6880 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6881 | ++count; |
| 6882 | } |
| 6883 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6884 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6885 | return NULL; |
| 6886 | may_generate_2STRING(-1, cctx); |
| 6887 | ++count; |
| 6888 | p = skipwhite(p); |
| 6889 | if (*p != '`') |
| 6890 | { |
| 6891 | emsg(_("E1083: missing backtick")); |
| 6892 | return NULL; |
| 6893 | } |
| 6894 | start = p + 1; |
| 6895 | |
| 6896 | p = (char_u *)strstr((char *)start, "`="); |
| 6897 | if (p == NULL) |
| 6898 | { |
| 6899 | if (*skipwhite(start) != NUL) |
| 6900 | { |
| 6901 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6902 | ++count; |
| 6903 | } |
| 6904 | break; |
| 6905 | } |
| 6906 | } |
| 6907 | generate_EXECCONCAT(cctx, count); |
| 6908 | } |
| 6909 | else |
| 6910 | generate_EXEC(cctx, line); |
| 6911 | |
| 6912 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6913 | if (*nextcmd != NUL) |
| 6914 | { |
| 6915 | // the parser expects a pointer to the bar, put it back |
| 6916 | --nextcmd; |
| 6917 | *nextcmd = '|'; |
| 6918 | } |
| 6919 | |
| 6920 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6921 | } |
| 6922 | |
| 6923 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6924 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6925 | * 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] | 6926 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6927 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6928 | add_def_function(ufunc_T *ufunc) |
| 6929 | { |
| 6930 | dfunc_T *dfunc; |
| 6931 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6932 | if (def_functions.ga_len == 0) |
| 6933 | { |
| 6934 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6935 | // wasn't set. |
| 6936 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6937 | return FAIL; |
| 6938 | ++def_functions.ga_len; |
| 6939 | } |
| 6940 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6941 | // Add the function to "def_functions". |
| 6942 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6943 | return FAIL; |
| 6944 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6945 | CLEAR_POINTER(dfunc); |
| 6946 | dfunc->df_idx = def_functions.ga_len; |
| 6947 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6948 | dfunc->df_ufunc = ufunc; |
| 6949 | ++def_functions.ga_len; |
| 6950 | return OK; |
| 6951 | } |
| 6952 | |
| 6953 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6954 | * After ex_function() has collected all the function lines: parse and compile |
| 6955 | * the lines into instructions. |
| 6956 | * Adds the function to "def_functions". |
| 6957 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6958 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6959 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6960 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6961 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6962 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6963 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6964 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6965 | 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] | 6966 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6967 | char_u *line = NULL; |
| 6968 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6969 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6970 | cctx_T cctx; |
| 6971 | garray_T *instr; |
| 6972 | int called_emsg_before = called_emsg; |
| 6973 | int ret = FAIL; |
| 6974 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6975 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6976 | int emsg_before = called_emsg; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6977 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6978 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6979 | // When using a function that was compiled before: Free old instructions. |
| 6980 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6981 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6982 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6983 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6984 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6985 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6986 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6987 | else |
| 6988 | { |
| 6989 | if (add_def_function(ufunc) == FAIL) |
| 6990 | return FAIL; |
| 6991 | new_def_function = TRUE; |
| 6992 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6993 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6994 | ufunc->uf_def_status = UF_COMPILING; |
| 6995 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6996 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6997 | cctx.ctx_ufunc = ufunc; |
| 6998 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6999 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7000 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 7001 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 7002 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 7003 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 7004 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 7005 | instr = &cctx.ctx_instr; |
| 7006 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7007 | // Set the context to the function, it may be compiled when called from |
| 7008 | // another script. Set the script version to the most modern one. |
| 7009 | // The line number will be set in next_line_from_context(). |
| 7010 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7011 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 7012 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7013 | // Make sure error messages are OK. |
| 7014 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 7015 | if (do_estack_push) |
| 7016 | estack_push_ufunc(ufunc, 1); |
| 7017 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7018 | if (ufunc->uf_def_args.ga_len > 0) |
| 7019 | { |
| 7020 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7021 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7022 | int i; |
| 7023 | char_u *arg; |
| 7024 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7025 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7026 | |
| 7027 | // Produce instructions for the default values of optional arguments. |
| 7028 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 7029 | // where to start when the function is called, depending on the number |
| 7030 | // of arguments. |
| 7031 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 7032 | if (ufunc->uf_def_arg_idx == NULL) |
| 7033 | goto erret; |
| 7034 | for (i = 0; i < count; ++i) |
| 7035 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7036 | garray_T *stack = &cctx.ctx_type_stack; |
| 7037 | type_T *val_type; |
| 7038 | int arg_idx = first_def_arg + i; |
| 7039 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7040 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 7041 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7042 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7043 | goto erret; |
| 7044 | |
| 7045 | // If no type specified use the type of the default value. |
| 7046 | // Otherwise check that the default value type matches the |
| 7047 | // specified type. |
| 7048 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 7049 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7050 | { |
| 7051 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7052 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7053 | } |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 7054 | 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] | 7055 | == FAIL) |
| 7056 | { |
| 7057 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 7058 | arg_idx + 1); |
| 7059 | goto erret; |
| 7060 | } |
| 7061 | |
| 7062 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7063 | goto erret; |
| 7064 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7065 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7066 | |
| 7067 | if (did_set_arg_type) |
| 7068 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7069 | } |
| 7070 | |
| 7071 | /* |
| 7072 | * Loop over all the lines of the function and generate instructions. |
| 7073 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7074 | for (;;) |
| 7075 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7076 | exarg_T ea; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7077 | int starts_with_colon = FALSE; |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7078 | char_u *cmd; |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7079 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7080 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7081 | // Bail out on the first error to avoid a flood of errors and report |
| 7082 | // the right line number when inside try/catch. |
| 7083 | if (emsg_before != called_emsg) |
| 7084 | goto erret; |
| 7085 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7086 | if (line != NULL && *line == '|') |
| 7087 | // the line continues after a '|' |
| 7088 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 7089 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7090 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 7091 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7092 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 7093 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7094 | goto erret; |
| 7095 | } |
| 7096 | else |
| 7097 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 7098 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 7099 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7100 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7101 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7102 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7103 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7104 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7105 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7106 | ea.cmdlinep = &line; |
| 7107 | ea.cmd = skipwhite(line); |
| 7108 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7109 | // Some things can be recognized by the first character. |
| 7110 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7111 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7112 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7113 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7114 | if (ea.cmd[1] != '{') |
| 7115 | { |
| 7116 | line = (char_u *)""; |
| 7117 | continue; |
| 7118 | } |
| 7119 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7120 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7121 | case '}': |
| 7122 | { |
| 7123 | // "}" ends a block scope |
| 7124 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7125 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7126 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7127 | if (stype == BLOCK_SCOPE) |
| 7128 | { |
| 7129 | compile_endblock(&cctx); |
| 7130 | line = ea.cmd; |
| 7131 | } |
| 7132 | else |
| 7133 | { |
| 7134 | emsg(_("E1025: using } outside of a block scope")); |
| 7135 | goto erret; |
| 7136 | } |
| 7137 | if (line != NULL) |
| 7138 | line = skipwhite(ea.cmd + 1); |
| 7139 | continue; |
| 7140 | } |
| 7141 | |
| 7142 | case '{': |
| 7143 | // "{" starts a block scope |
| 7144 | // "{'a': 1}->func() is something else |
| 7145 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7146 | { |
| 7147 | line = compile_block(ea.cmd, &cctx); |
| 7148 | continue; |
| 7149 | } |
| 7150 | break; |
| 7151 | |
| 7152 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7153 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7154 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7155 | } |
| 7156 | |
| 7157 | /* |
| 7158 | * COMMAND MODIFIERS |
| 7159 | */ |
| 7160 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 7161 | { |
| 7162 | if (errormsg != NULL) |
| 7163 | goto erret; |
| 7164 | // empty line or comment |
| 7165 | line = (char_u *)""; |
| 7166 | continue; |
| 7167 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7168 | // TODO: use modifiers in the command |
| 7169 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | b074e8b | 2020-07-11 13:40:45 +0200 | [diff] [blame] | 7170 | CLEAR_FIELD(cmdmod); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7171 | |
| 7172 | // Skip ":call" to get to the function name. |
| 7173 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 7174 | ea.cmd = skipwhite(ea.cmd); |
| 7175 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7176 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7177 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7178 | char_u *pskip; |
| 7179 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7180 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7181 | // find what follows. |
| 7182 | // Skip over "var.member", "var[idx]" and the like. |
| 7183 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 7184 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7185 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7186 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7187 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7188 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7189 | char_u *var_end; |
| 7190 | int oplen; |
| 7191 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7192 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7193 | var_end = find_name_end(pskip, NULL, NULL, |
| 7194 | FNE_CHECK_START | FNE_INCL_BR); |
| 7195 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7196 | if (oplen > 0) |
| 7197 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7198 | size_t len = p - ea.cmd; |
| 7199 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7200 | // Recognize an assignment if we recognize the variable |
| 7201 | // name: |
| 7202 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 7203 | // "local = expr" where "local" is a local var. |
| 7204 | // "script = expr" where "script" is a script-local var. |
| 7205 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7206 | // "&opt = expr" |
| 7207 | // "$ENV = expr" |
| 7208 | // "@r = expr" |
| 7209 | if (*ea.cmd == '&' |
| 7210 | || *ea.cmd == '$' |
| 7211 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7212 | || ((len) > 2 && ea.cmd[1] == ':') |
| 7213 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 7214 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 7215 | NULL, &cctx) == OK |
| 7216 | || lookup_script(ea.cmd, len) == OK |
| 7217 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7218 | { |
| 7219 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7220 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7221 | goto erret; |
| 7222 | continue; |
| 7223 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7224 | } |
| 7225 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7226 | |
| 7227 | if (*ea.cmd == '[') |
| 7228 | { |
| 7229 | // [var, var] = expr |
| 7230 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 7231 | if (line == NULL) |
| 7232 | goto erret; |
| 7233 | if (line != ea.cmd) |
| 7234 | continue; |
| 7235 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7236 | } |
| 7237 | |
| 7238 | /* |
| 7239 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7240 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7241 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7242 | cmd = ea.cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7243 | if (*cmd != '\'') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7244 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7245 | ea.cmd = skip_range(ea.cmd, NULL); |
| 7246 | if (ea.cmd > cmd && !starts_with_colon) |
| 7247 | { |
| 7248 | emsg(_(e_colon_required)); |
| 7249 | goto erret; |
| 7250 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7251 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7252 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7253 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7254 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7255 | |
| 7256 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 7257 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7258 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7259 | { |
| 7260 | line += STRLEN(line); |
| 7261 | continue; |
| 7262 | } |
| 7263 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7264 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7265 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7266 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7267 | // CMD_let cannot happen, compile_assignment() above is used |
| 7268 | iemsg("Command from find_ex_command() not handled"); |
| 7269 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7270 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7271 | } |
| 7272 | |
| 7273 | p = skipwhite(p); |
| 7274 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7275 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 7276 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7277 | && ea.cmdidx != CMD_elseif |
| 7278 | && ea.cmdidx != CMD_else |
| 7279 | && ea.cmdidx != CMD_endif) |
| 7280 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7281 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7282 | continue; |
| 7283 | } |
| 7284 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7285 | if (ea.cmdidx != CMD_elseif |
| 7286 | && ea.cmdidx != CMD_else |
| 7287 | && ea.cmdidx != CMD_endif |
| 7288 | && ea.cmdidx != CMD_endfor |
| 7289 | && ea.cmdidx != CMD_endwhile |
| 7290 | && ea.cmdidx != CMD_catch |
| 7291 | && ea.cmdidx != CMD_finally |
| 7292 | && ea.cmdidx != CMD_endtry) |
| 7293 | { |
| 7294 | if (cctx.ctx_had_return) |
| 7295 | { |
| 7296 | emsg(_("E1095: Unreachable code after :return")); |
| 7297 | goto erret; |
| 7298 | } |
| 7299 | } |
| 7300 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7301 | switch (ea.cmdidx) |
| 7302 | { |
| 7303 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7304 | ea.arg = p; |
| 7305 | line = compile_nested_function(&ea, &cctx); |
| 7306 | break; |
| 7307 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7308 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7309 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7310 | goto erret; |
| 7311 | |
| 7312 | case CMD_return: |
| 7313 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7314 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7315 | break; |
| 7316 | |
| 7317 | case CMD_let: |
| 7318 | case CMD_const: |
| 7319 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7320 | if (line == p) |
| 7321 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7322 | break; |
| 7323 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7324 | case CMD_unlet: |
| 7325 | case CMD_unlockvar: |
| 7326 | case CMD_lockvar: |
| 7327 | line = compile_unletlock(p, &ea, &cctx); |
| 7328 | break; |
| 7329 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7330 | case CMD_import: |
| 7331 | line = compile_import(p, &cctx); |
| 7332 | break; |
| 7333 | |
| 7334 | case CMD_if: |
| 7335 | line = compile_if(p, &cctx); |
| 7336 | break; |
| 7337 | case CMD_elseif: |
| 7338 | line = compile_elseif(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_else: |
| 7342 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7343 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7344 | break; |
| 7345 | case CMD_endif: |
| 7346 | line = compile_endif(p, &cctx); |
| 7347 | break; |
| 7348 | |
| 7349 | case CMD_while: |
| 7350 | line = compile_while(p, &cctx); |
| 7351 | break; |
| 7352 | case CMD_endwhile: |
| 7353 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7354 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7355 | break; |
| 7356 | |
| 7357 | case CMD_for: |
| 7358 | line = compile_for(p, &cctx); |
| 7359 | break; |
| 7360 | case CMD_endfor: |
| 7361 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7362 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7363 | break; |
| 7364 | case CMD_continue: |
| 7365 | line = compile_continue(p, &cctx); |
| 7366 | break; |
| 7367 | case CMD_break: |
| 7368 | line = compile_break(p, &cctx); |
| 7369 | break; |
| 7370 | |
| 7371 | case CMD_try: |
| 7372 | line = compile_try(p, &cctx); |
| 7373 | break; |
| 7374 | case CMD_catch: |
| 7375 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7376 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7377 | break; |
| 7378 | case CMD_finally: |
| 7379 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7380 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7381 | break; |
| 7382 | case CMD_endtry: |
| 7383 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7384 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7385 | break; |
| 7386 | case CMD_throw: |
| 7387 | line = compile_throw(p, &cctx); |
| 7388 | break; |
| 7389 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7390 | case CMD_eval: |
| 7391 | if (compile_expr0(&p, &cctx) == FAIL) |
| 7392 | goto erret; |
| 7393 | |
| 7394 | // drop the return value |
| 7395 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 7396 | |
| 7397 | line = skipwhite(p); |
| 7398 | break; |
| 7399 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7400 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7401 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7402 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7403 | case CMD_echomsg: |
| 7404 | case CMD_echoerr: |
| 7405 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7406 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7407 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7408 | // TODO: other commands with an expression argument |
| 7409 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7410 | case CMD_SIZE: |
| 7411 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 7412 | goto erret; |
| 7413 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7414 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7415 | // Not recognized, execute with do_cmdline_cmd(). |
| 7416 | ea.arg = p; |
| 7417 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7418 | break; |
| 7419 | } |
| 7420 | if (line == NULL) |
| 7421 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7422 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7423 | |
| 7424 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7425 | { |
| 7426 | iemsg("Type stack underflow"); |
| 7427 | goto erret; |
| 7428 | } |
| 7429 | } |
| 7430 | |
| 7431 | if (cctx.ctx_scope != NULL) |
| 7432 | { |
| 7433 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7434 | emsg(_(e_endif)); |
| 7435 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7436 | emsg(_(e_endwhile)); |
| 7437 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7438 | emsg(_(e_endfor)); |
| 7439 | else |
| 7440 | emsg(_("E1026: Missing }")); |
| 7441 | goto erret; |
| 7442 | } |
| 7443 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7444 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7445 | { |
| 7446 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7447 | { |
| 7448 | emsg(_("E1027: Missing return statement")); |
| 7449 | goto erret; |
| 7450 | } |
| 7451 | |
| 7452 | // Return zero if there is no return at the end. |
| 7453 | generate_PUSHNR(&cctx, 0); |
| 7454 | generate_instr(&cctx, ISN_RETURN); |
| 7455 | } |
| 7456 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7457 | { |
| 7458 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7459 | + ufunc->uf_dfunc_idx; |
| 7460 | dfunc->df_deleted = FALSE; |
| 7461 | dfunc->df_instr = instr->ga_data; |
| 7462 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7463 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 7464 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7465 | if (cctx.ctx_outer_used) |
| 7466 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7467 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7468 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7469 | |
| 7470 | ret = OK; |
| 7471 | |
| 7472 | erret: |
| 7473 | if (ret == FAIL) |
| 7474 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7475 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7476 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7477 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7478 | |
| 7479 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7480 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7481 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7482 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7483 | // If using the last entry in the table and it was added above, we |
| 7484 | // might as well remove it. |
| 7485 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7486 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7487 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7488 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7489 | ufunc->uf_dfunc_idx = 0; |
| 7490 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7491 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7492 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7493 | while (cctx.ctx_scope != NULL) |
| 7494 | drop_scope(&cctx); |
| 7495 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7496 | // Don't execute this function body. |
| 7497 | ga_clear_strings(&ufunc->uf_lines); |
| 7498 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7499 | if (errormsg != NULL) |
| 7500 | emsg(errormsg); |
| 7501 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 7502 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7503 | } |
| 7504 | |
| 7505 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7506 | if (do_estack_push) |
| 7507 | estack_pop(); |
| 7508 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7509 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7510 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7511 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7512 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7513 | } |
| 7514 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7515 | void |
| 7516 | set_function_type(ufunc_T *ufunc) |
| 7517 | { |
| 7518 | int varargs = ufunc->uf_va_name != NULL; |
| 7519 | int argcount = ufunc->uf_args.ga_len; |
| 7520 | |
| 7521 | // Create a type for the function, with the return type and any |
| 7522 | // argument types. |
| 7523 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7524 | // The type is included in "tt_args". |
| 7525 | if (argcount > 0 || varargs) |
| 7526 | { |
| 7527 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7528 | argcount, &ufunc->uf_type_list); |
| 7529 | // Add argument types to the function type. |
| 7530 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7531 | argcount + varargs, |
| 7532 | &ufunc->uf_type_list) == FAIL) |
| 7533 | return; |
| 7534 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7535 | ufunc->uf_func_type->tt_min_argcount = |
| 7536 | argcount - ufunc->uf_def_args.ga_len; |
| 7537 | if (ufunc->uf_arg_types == NULL) |
| 7538 | { |
| 7539 | int i; |
| 7540 | |
| 7541 | // lambda does not have argument types. |
| 7542 | for (i = 0; i < argcount; ++i) |
| 7543 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7544 | } |
| 7545 | else |
| 7546 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7547 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7548 | if (varargs) |
| 7549 | { |
| 7550 | ufunc->uf_func_type->tt_args[argcount] = |
| 7551 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7552 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7553 | } |
| 7554 | } |
| 7555 | else |
| 7556 | // No arguments, can use a predefined type. |
| 7557 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7558 | argcount, &ufunc->uf_type_list); |
| 7559 | } |
| 7560 | |
| 7561 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7562 | /* |
| 7563 | * Delete an instruction, free what it contains. |
| 7564 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7565 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7566 | delete_instr(isn_T *isn) |
| 7567 | { |
| 7568 | switch (isn->isn_type) |
| 7569 | { |
| 7570 | case ISN_EXEC: |
| 7571 | case ISN_LOADENV: |
| 7572 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7573 | case ISN_LOADB: |
| 7574 | case ISN_LOADW: |
| 7575 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7576 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7577 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7578 | case ISN_PUSHEXC: |
| 7579 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7580 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7581 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7582 | case ISN_STOREB: |
| 7583 | case ISN_STOREW: |
| 7584 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7585 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7586 | vim_free(isn->isn_arg.string); |
| 7587 | break; |
| 7588 | |
| 7589 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7590 | case ISN_STORES: |
| 7591 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7592 | break; |
| 7593 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7594 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7595 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7596 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7597 | break; |
| 7598 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7599 | case ISN_STOREOPT: |
| 7600 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7601 | break; |
| 7602 | |
| 7603 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7604 | blob_unref(isn->isn_arg.blob); |
| 7605 | break; |
| 7606 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7607 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7608 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7609 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7610 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7611 | break; |
| 7612 | |
| 7613 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7614 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7615 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7616 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7617 | break; |
| 7618 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7619 | case ISN_UCALL: |
| 7620 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7621 | break; |
| 7622 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7623 | case ISN_FUNCREF: |
| 7624 | { |
| 7625 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7626 | + isn->isn_arg.funcref.fr_func; |
| 7627 | func_ptr_unref(dfunc->df_ufunc); |
| 7628 | } |
| 7629 | break; |
| 7630 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7631 | case ISN_2BOOL: |
| 7632 | case ISN_2STRING: |
| 7633 | case ISN_ADDBLOB: |
| 7634 | case ISN_ADDLIST: |
| 7635 | case ISN_BCALL: |
| 7636 | case ISN_CATCH: |
| 7637 | case ISN_CHECKNR: |
| 7638 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7639 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7640 | case ISN_COMPAREANY: |
| 7641 | case ISN_COMPAREBLOB: |
| 7642 | case ISN_COMPAREBOOL: |
| 7643 | case ISN_COMPAREDICT: |
| 7644 | case ISN_COMPAREFLOAT: |
| 7645 | case ISN_COMPAREFUNC: |
| 7646 | case ISN_COMPARELIST: |
| 7647 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7648 | case ISN_COMPARESPECIAL: |
| 7649 | case ISN_COMPARESTRING: |
| 7650 | case ISN_CONCAT: |
| 7651 | case ISN_DCALL: |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 7652 | case ISN_SHUFFLE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7653 | case ISN_DROP: |
| 7654 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7655 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7656 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7657 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7658 | case ISN_EXECCONCAT: |
| 7659 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7660 | case ISN_FOR: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7661 | case ISN_LISTINDEX: |
| 7662 | case ISN_STRINDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7663 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7664 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7665 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7666 | case ISN_JUMP: |
| 7667 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7668 | case ISN_LOADBDICT: |
| 7669 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7670 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7671 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7672 | case ISN_LOADSCRIPT: |
| 7673 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7674 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7675 | case ISN_LOADWDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7676 | case ISN_NEGATENR: |
| 7677 | case ISN_NEWDICT: |
| 7678 | case ISN_NEWLIST: |
| 7679 | case ISN_OPNR: |
| 7680 | case ISN_OPFLOAT: |
| 7681 | case ISN_OPANY: |
| 7682 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7683 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7684 | case ISN_PUSHF: |
| 7685 | case ISN_PUSHNR: |
| 7686 | case ISN_PUSHBOOL: |
| 7687 | case ISN_PUSHSPEC: |
| 7688 | case ISN_RETURN: |
| 7689 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 7690 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7691 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7692 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7693 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7694 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7695 | case ISN_STOREDICT: |
| 7696 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7697 | case ISN_THROW: |
| 7698 | case ISN_TRY: |
| 7699 | // nothing allocated |
| 7700 | break; |
| 7701 | } |
| 7702 | } |
| 7703 | |
| 7704 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7705 | * Free all instructions for "dfunc". |
| 7706 | */ |
| 7707 | static void |
| 7708 | delete_def_function_contents(dfunc_T *dfunc) |
| 7709 | { |
| 7710 | int idx; |
| 7711 | |
| 7712 | ga_clear(&dfunc->df_def_args_isn); |
| 7713 | |
| 7714 | if (dfunc->df_instr != NULL) |
| 7715 | { |
| 7716 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7717 | delete_instr(dfunc->df_instr + idx); |
| 7718 | VIM_CLEAR(dfunc->df_instr); |
| 7719 | } |
| 7720 | |
| 7721 | dfunc->df_deleted = TRUE; |
| 7722 | } |
| 7723 | |
| 7724 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7725 | * When a user function is deleted, clear the contents of any associated def |
| 7726 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7727 | */ |
| 7728 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7729 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7730 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7731 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7732 | { |
| 7733 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7734 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7735 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7736 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7737 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7738 | } |
| 7739 | } |
| 7740 | |
| 7741 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7742 | /* |
| 7743 | * Free all functions defined with ":def". |
| 7744 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7745 | void |
| 7746 | free_def_functions(void) |
| 7747 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7748 | int idx; |
| 7749 | |
| 7750 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7751 | { |
| 7752 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7753 | |
| 7754 | delete_def_function_contents(dfunc); |
| 7755 | } |
| 7756 | |
| 7757 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7758 | } |
| 7759 | #endif |
| 7760 | |
| 7761 | |
| 7762 | #endif // FEAT_EVAL |