Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9compile.c: :def and dealing with instructions |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | #ifdef VMS |
| 20 | # include <float.h> |
| 21 | #endif |
| 22 | |
| 23 | #define DEFINE_VIM9_GLOBALS |
| 24 | #include "vim9.h" |
| 25 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 26 | // values for ctx_skip |
| 27 | typedef enum { |
| 28 | SKIP_NOT, // condition is a constant, produce code |
| 29 | SKIP_YES, // condition is a constant, do NOT produce code |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 30 | SKIP_UNKNOWN // condition is not a constant, produce code |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 31 | } skip_T; |
| 32 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Chain of jump instructions where the end label needs to be set. |
| 35 | */ |
| 36 | typedef struct endlabel_S endlabel_T; |
| 37 | struct endlabel_S { |
| 38 | endlabel_T *el_next; // chain end_label locations |
| 39 | int el_end_label; // instruction idx where to set end |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * info specific for the scope of :if / elseif / else |
| 44 | */ |
| 45 | typedef struct { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 46 | int is_seen_else; |
| 47 | int is_had_return; // every block ends in :return |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 48 | int is_if_label; // instruction idx at IF or ELSEIF |
| 49 | endlabel_T *is_end_label; // instructions to set end label |
| 50 | } ifscope_T; |
| 51 | |
| 52 | /* |
| 53 | * info specific for the scope of :while |
| 54 | */ |
| 55 | typedef struct { |
| 56 | int ws_top_label; // instruction idx at WHILE |
| 57 | endlabel_T *ws_end_label; // instructions to set end |
| 58 | } whilescope_T; |
| 59 | |
| 60 | /* |
| 61 | * info specific for the scope of :for |
| 62 | */ |
| 63 | typedef struct { |
| 64 | int fs_top_label; // instruction idx at FOR |
| 65 | endlabel_T *fs_end_label; // break instructions |
| 66 | } forscope_T; |
| 67 | |
| 68 | /* |
| 69 | * info specific for the scope of :try |
| 70 | */ |
| 71 | typedef struct { |
| 72 | int ts_try_label; // instruction idx at TRY |
| 73 | endlabel_T *ts_end_label; // jump to :finally or :endtry |
| 74 | int ts_catch_label; // instruction idx of last CATCH |
| 75 | int ts_caught_all; // "catch" without argument encountered |
| 76 | } tryscope_T; |
| 77 | |
| 78 | typedef enum { |
| 79 | NO_SCOPE, |
| 80 | IF_SCOPE, |
| 81 | WHILE_SCOPE, |
| 82 | FOR_SCOPE, |
| 83 | TRY_SCOPE, |
| 84 | BLOCK_SCOPE |
| 85 | } scopetype_T; |
| 86 | |
| 87 | /* |
| 88 | * Info for one scope, pointed to by "ctx_scope". |
| 89 | */ |
| 90 | typedef struct scope_S scope_T; |
| 91 | struct scope_S { |
| 92 | scope_T *se_outer; // scope containing this one |
| 93 | scopetype_T se_type; |
| 94 | int se_local_count; // ctx_locals.ga_len before scope |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 95 | skip_T se_skip_save; // ctx_skip before the block |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 96 | union { |
| 97 | ifscope_T se_if; |
| 98 | whilescope_T se_while; |
| 99 | forscope_T se_for; |
| 100 | tryscope_T se_try; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 101 | } se_u; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Entry for "ctx_locals". Used for arguments and local variables. |
| 106 | */ |
| 107 | typedef struct { |
| 108 | char_u *lv_name; |
| 109 | type_T *lv_type; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 110 | int lv_idx; // index of the variable on the stack |
| 111 | int lv_from_outer; // when TRUE using ctx_outer scope |
| 112 | int lv_const; // when TRUE cannot be assigned to |
| 113 | int lv_arg; // when TRUE this is an argument |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 114 | } lvar_T; |
| 115 | |
| 116 | /* |
| 117 | * Context for compiling lines of Vim script. |
| 118 | * Stores info about the local variables and condition stack. |
| 119 | */ |
| 120 | struct cctx_S { |
| 121 | ufunc_T *ctx_ufunc; // current function |
| 122 | int ctx_lnum; // line number in current function |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 123 | char_u *ctx_line_start; // start of current line or NULL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 124 | garray_T ctx_instr; // generated instructions |
| 125 | |
| 126 | garray_T ctx_locals; // currently visible local variables |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 127 | int ctx_locals_count; // total number of local variables |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 128 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 129 | int ctx_closure_count; // number of closures created in the |
| 130 | // function |
| 131 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 132 | garray_T ctx_imports; // imported items |
| 133 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 134 | skip_T ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 135 | scope_T *ctx_scope; // current scope, NULL at toplevel |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 136 | int ctx_had_return; // last seen statement was "return" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 137 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 138 | cctx_T *ctx_outer; // outer scope for lambda or nested |
| 139 | // function |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 140 | int ctx_outer_used; // var in ctx_outer was used |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 141 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 142 | garray_T ctx_type_stack; // type of each item on the stack |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 143 | garray_T *ctx_type_list; // list of pointers to allocated types |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static char e_var_notfound[] = N_("E1001: variable not found: %s"); |
| 147 | static char e_syntax_at[] = N_("E1002: Syntax error at %s"); |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 148 | static char e_used_as_arg[] = N_("E1006: %s is used as an argument"); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 149 | static char e_cannot_use_void[] = N_("E1031: Cannot use void value"); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 150 | static char e_namespace[] = N_("E1075: Namespace not supported: %s"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 151 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 152 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 153 | static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 154 | |
| 155 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 156 | * Lookup variable "name" in the local scope and return it. |
| 157 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 158 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 159 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 160 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 161 | { |
| 162 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 163 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 164 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 165 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 166 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 167 | |
| 168 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 169 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 170 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 171 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 172 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 173 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 174 | { |
| 175 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 176 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 177 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 178 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 179 | |
| 180 | // Find local in outer function scope. |
| 181 | if (cctx->ctx_outer != NULL) |
| 182 | { |
| 183 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 184 | if (lvar != NULL) |
| 185 | { |
| 186 | // TODO: are there situations we should not mark the outer scope as |
| 187 | // used? |
| 188 | cctx->ctx_outer_used = TRUE; |
| 189 | lvar->lv_from_outer = TRUE; |
| 190 | return lvar; |
| 191 | } |
| 192 | } |
| 193 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 194 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 198 | * Lookup an argument in the current function and an enclosing function. |
| 199 | * Returns the argument index in "idxp" |
| 200 | * Returns the argument type in "type" |
| 201 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 202 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 203 | */ |
| 204 | static int |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 205 | lookup_arg( |
| 206 | char_u *name, |
| 207 | size_t len, |
| 208 | int *idxp, |
| 209 | type_T **type, |
| 210 | int *gen_load_outer, |
| 211 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 212 | { |
| 213 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 214 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 215 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 216 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 217 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 218 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 219 | { |
| 220 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 221 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 222 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 223 | { |
| 224 | if (idxp != NULL) |
| 225 | { |
| 226 | // Arguments are located above the frame pointer. One further |
| 227 | // if there is a vararg argument |
| 228 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 229 | + STACK_FRAME_SIZE) |
| 230 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 231 | |
| 232 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 233 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 234 | else |
| 235 | *type = &t_any; |
| 236 | } |
| 237 | return OK; |
| 238 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 239 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 240 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 241 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 242 | if (va_name != NULL |
| 243 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 244 | { |
| 245 | if (idxp != NULL) |
| 246 | { |
| 247 | // varargs is always the last argument |
| 248 | *idxp = -STACK_FRAME_SIZE - 1; |
| 249 | *type = cctx->ctx_ufunc->uf_va_type; |
| 250 | } |
| 251 | return OK; |
| 252 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 253 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 254 | if (cctx->ctx_outer != NULL) |
| 255 | { |
| 256 | // Lookup the name for an argument of the outer function. |
| 257 | if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
| 258 | == OK) |
| 259 | { |
| 260 | *gen_load_outer = TRUE; |
| 261 | return OK; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Lookup a variable in the current script. |
| 270 | * Returns OK or FAIL. |
| 271 | */ |
| 272 | static int |
| 273 | lookup_script(char_u *name, size_t len) |
| 274 | { |
| 275 | int cc; |
| 276 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 277 | dictitem_T *di; |
| 278 | |
| 279 | cc = name[len]; |
| 280 | name[len] = NUL; |
| 281 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 282 | name[len] = cc; |
| 283 | return di == NULL ? FAIL: OK; |
| 284 | } |
| 285 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 286 | /* |
| 287 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 288 | * compilation context "cctx". |
| 289 | * Return FAIL and give an error if it defined. |
| 290 | */ |
| 291 | int |
Bram Moolenaar | cbb6bdc | 2020-07-06 21:53:17 +0200 | [diff] [blame] | 292 | check_defined(char_u *p, size_t len, cctx_T *cctx) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 293 | { |
| 294 | if (lookup_script(p, len) == OK |
| 295 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 296 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 297 | || find_imported(p, len, cctx) != NULL))) |
| 298 | { |
| 299 | semsg("E1073: imported name already defined: %s", p); |
| 300 | return FAIL; |
| 301 | } |
| 302 | return OK; |
| 303 | } |
| 304 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 305 | /* |
| 306 | * Allocate memory for a type_T and add the pointer to type_gap, so that it can |
| 307 | * be freed later. |
| 308 | */ |
| 309 | static type_T * |
| 310 | alloc_type(garray_T *type_gap) |
| 311 | { |
| 312 | type_T *type; |
| 313 | |
| 314 | if (ga_grow(type_gap, 1) == FAIL) |
| 315 | return NULL; |
| 316 | type = ALLOC_CLEAR_ONE(type_T); |
| 317 | if (type != NULL) |
| 318 | { |
| 319 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type; |
| 320 | ++type_gap->ga_len; |
| 321 | } |
| 322 | return type; |
| 323 | } |
| 324 | |
Bram Moolenaar | 6110e79 | 2020-07-08 19:35:21 +0200 | [diff] [blame] | 325 | void |
| 326 | clear_type_list(garray_T *gap) |
| 327 | { |
| 328 | while (gap->ga_len > 0) |
| 329 | vim_free(((type_T **)gap->ga_data)[--gap->ga_len]); |
| 330 | ga_clear(gap); |
| 331 | } |
| 332 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 333 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 334 | get_list_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 335 | { |
| 336 | type_T *type; |
| 337 | |
| 338 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 339 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 340 | return &t_list_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 341 | if (member_type->tt_type == VAR_VOID |
| 342 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 343 | return &t_list_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 344 | if (member_type->tt_type == VAR_BOOL) |
| 345 | return &t_list_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 346 | if (member_type->tt_type == VAR_NUMBER) |
| 347 | return &t_list_number; |
| 348 | if (member_type->tt_type == VAR_STRING) |
| 349 | return &t_list_string; |
| 350 | |
| 351 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 352 | type = alloc_type(type_gap); |
| 353 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 354 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 355 | type->tt_type = VAR_LIST; |
| 356 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 357 | type->tt_argcount = 0; |
| 358 | type->tt_args = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 359 | return type; |
| 360 | } |
| 361 | |
| 362 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 363 | get_dict_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 364 | { |
| 365 | type_T *type; |
| 366 | |
| 367 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 368 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 369 | return &t_dict_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 370 | if (member_type->tt_type == VAR_VOID |
| 371 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 372 | return &t_dict_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 373 | if (member_type->tt_type == VAR_BOOL) |
| 374 | return &t_dict_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 375 | if (member_type->tt_type == VAR_NUMBER) |
| 376 | return &t_dict_number; |
| 377 | if (member_type->tt_type == VAR_STRING) |
| 378 | return &t_dict_string; |
| 379 | |
| 380 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 381 | type = alloc_type(type_gap); |
| 382 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 383 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 384 | type->tt_type = VAR_DICT; |
| 385 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 386 | type->tt_argcount = 0; |
| 387 | type->tt_args = NULL; |
| 388 | return type; |
| 389 | } |
| 390 | |
| 391 | /* |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 392 | * Allocate a new type for a function. |
| 393 | */ |
| 394 | static type_T * |
| 395 | alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 396 | { |
| 397 | type_T *type = alloc_type(type_gap); |
| 398 | |
| 399 | if (type == NULL) |
| 400 | return &t_any; |
| 401 | type->tt_type = VAR_FUNC; |
| 402 | type->tt_member = ret_type; |
| 403 | type->tt_argcount = argcount; |
| 404 | type->tt_args = NULL; |
| 405 | return type; |
| 406 | } |
| 407 | |
| 408 | /* |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 409 | * Get a function type, based on the return type "ret_type". |
| 410 | * If "argcount" is -1 or 0 a predefined type can be used. |
| 411 | * If "argcount" > 0 always create a new type, so that arguments can be added. |
| 412 | */ |
| 413 | static type_T * |
| 414 | get_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 415 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 416 | // recognize commonly used types |
| 417 | if (argcount <= 0) |
| 418 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 419 | if (ret_type == &t_unknown) |
| 420 | { |
| 421 | // (argcount == 0) is not possible |
| 422 | return &t_func_unknown; |
| 423 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 424 | if (ret_type == &t_void) |
| 425 | { |
| 426 | if (argcount == 0) |
| 427 | return &t_func_0_void; |
| 428 | else |
| 429 | return &t_func_void; |
| 430 | } |
| 431 | if (ret_type == &t_any) |
| 432 | { |
| 433 | if (argcount == 0) |
| 434 | return &t_func_0_any; |
| 435 | else |
| 436 | return &t_func_any; |
| 437 | } |
| 438 | if (ret_type == &t_number) |
| 439 | { |
| 440 | if (argcount == 0) |
| 441 | return &t_func_0_number; |
| 442 | else |
| 443 | return &t_func_number; |
| 444 | } |
| 445 | if (ret_type == &t_string) |
| 446 | { |
| 447 | if (argcount == 0) |
| 448 | return &t_func_0_string; |
| 449 | else |
| 450 | return &t_func_string; |
| 451 | } |
| 452 | } |
| 453 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 454 | return alloc_func_type(ret_type, argcount, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 455 | } |
| 456 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 457 | /* |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 458 | * For a function type, reserve space for "argcount" argument types (including |
| 459 | * vararg). |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 460 | */ |
| 461 | static int |
| 462 | func_type_add_arg_types( |
| 463 | type_T *functype, |
| 464 | int argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 465 | garray_T *type_gap) |
| 466 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 467 | // To make it easy to free the space needed for the argument types, add the |
| 468 | // pointer to type_gap. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 469 | if (ga_grow(type_gap, 1) == FAIL) |
| 470 | return FAIL; |
| 471 | functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); |
| 472 | if (functype->tt_args == NULL) |
| 473 | return FAIL; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 474 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = |
| 475 | (void *)functype->tt_args; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 476 | ++type_gap->ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 477 | return OK; |
| 478 | } |
| 479 | |
| 480 | /* |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 481 | * Get a type_T for a typval_T. |
| 482 | * "type_list" is used to temporarily create types in. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 483 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 484 | type_T * |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 485 | typval2type(typval_T *tv, garray_T *type_gap) |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 486 | { |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 487 | type_T *actual; |
| 488 | type_T *member_type; |
| 489 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 490 | if (tv->v_type == VAR_NUMBER) |
| 491 | return &t_number; |
| 492 | if (tv->v_type == VAR_BOOL) |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 493 | return &t_bool; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 494 | if (tv->v_type == VAR_STRING) |
| 495 | return &t_string; |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 496 | |
| 497 | if (tv->v_type == VAR_LIST |
| 498 | && tv->vval.v_list != NULL |
| 499 | && tv->vval.v_list->lv_first != NULL) |
| 500 | { |
| 501 | // Use the type of the first member, it is the most specific. |
| 502 | member_type = typval2type(&tv->vval.v_list->lv_first->li_tv, type_gap); |
| 503 | return get_list_type(member_type, type_gap); |
| 504 | } |
| 505 | |
| 506 | if (tv->v_type == VAR_DICT |
| 507 | && tv->vval.v_dict != NULL |
| 508 | && tv->vval.v_dict->dv_hashtab.ht_used > 0) |
| 509 | { |
| 510 | dict_iterator_T iter; |
| 511 | typval_T *value; |
| 512 | |
| 513 | // Use the type of the first value, it is the most specific. |
| 514 | dict_iterate_start(tv, &iter); |
| 515 | dict_iterate_next(&iter, &value); |
| 516 | member_type = typval2type(value, type_gap); |
| 517 | return get_dict_type(member_type, type_gap); |
| 518 | } |
| 519 | |
| 520 | if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL) |
| 521 | { |
| 522 | char_u *name = NULL; |
| 523 | ufunc_T *ufunc = NULL; |
| 524 | |
| 525 | if (tv->v_type == VAR_PARTIAL) |
| 526 | { |
| 527 | if (tv->vval.v_partial->pt_func != NULL) |
| 528 | ufunc = tv->vval.v_partial->pt_func; |
| 529 | else |
| 530 | name = tv->vval.v_partial->pt_name; |
| 531 | } |
| 532 | else |
| 533 | name = tv->vval.v_string; |
| 534 | if (name != NULL) |
| 535 | // TODO: how about a builtin function? |
| 536 | ufunc = find_func(name, FALSE, NULL); |
| 537 | if (ufunc != NULL && ufunc->uf_func_type != NULL) |
| 538 | return ufunc->uf_func_type; |
| 539 | } |
| 540 | |
| 541 | actual = alloc_type(type_gap); |
| 542 | if (actual == NULL) |
| 543 | return NULL; |
| 544 | actual->tt_type = tv->v_type; |
| 545 | actual->tt_member = &t_any; |
| 546 | |
| 547 | return actual; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Get a type_T for a typval_T, used for v: variables. |
| 552 | * "type_list" is used to temporarily create types in. |
| 553 | */ |
| 554 | type_T * |
| 555 | typval2type_vimvar(typval_T *tv, garray_T *type_gap) |
| 556 | { |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 557 | if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles |
| 558 | return &t_list_string; |
| 559 | if (tv->v_type == VAR_DICT) // e.g. for v:completed_item |
| 560 | return &t_dict_any; |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 561 | return typval2type(tv, type_gap); |
| 562 | } |
| 563 | |
| 564 | |
| 565 | /* |
| 566 | * Return FAIL if "expected" and "actual" don't match. |
| 567 | */ |
| 568 | int |
| 569 | check_typval_type(type_T *expected, typval_T *actual_tv) |
| 570 | { |
| 571 | garray_T type_list; |
| 572 | type_T *actual_type; |
| 573 | int res = FAIL; |
| 574 | |
| 575 | ga_init2(&type_list, sizeof(type_T *), 10); |
| 576 | actual_type = typval2type(actual_tv, &type_list); |
| 577 | if (actual_type != NULL) |
| 578 | res = check_type(expected, actual_type, TRUE); |
| 579 | clear_type_list(&type_list); |
| 580 | return res; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 583 | static void |
| 584 | type_mismatch(type_T *expected, type_T *actual) |
| 585 | { |
| 586 | char *tofree1, *tofree2; |
| 587 | |
| 588 | semsg(_("E1013: type mismatch, expected %s but got %s"), |
| 589 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 590 | vim_free(tofree1); |
| 591 | vim_free(tofree2); |
| 592 | } |
| 593 | |
| 594 | static void |
| 595 | arg_type_mismatch(type_T *expected, type_T *actual, int argidx) |
| 596 | { |
| 597 | char *tofree1, *tofree2; |
| 598 | |
| 599 | semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"), |
| 600 | argidx, |
| 601 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 602 | vim_free(tofree1); |
| 603 | vim_free(tofree2); |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * Check if the expected and actual types match. |
| 608 | * Does not allow for assigning "any" to a specific type. |
| 609 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 610 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 611 | check_type(type_T *expected, type_T *actual, int give_msg) |
| 612 | { |
| 613 | int ret = OK; |
| 614 | |
| 615 | // When expected is "unknown" we accept any actual type. |
| 616 | // When expected is "any" we accept any actual type except "void". |
| 617 | if (expected->tt_type != VAR_UNKNOWN |
| 618 | && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) |
| 619 | |
| 620 | { |
| 621 | if (expected->tt_type != actual->tt_type) |
| 622 | { |
| 623 | if (give_msg) |
| 624 | type_mismatch(expected, actual); |
| 625 | return FAIL; |
| 626 | } |
| 627 | if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) |
| 628 | { |
| 629 | // "unknown" is used for an empty list or dict |
| 630 | if (actual->tt_member != &t_unknown) |
| 631 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 632 | } |
| 633 | else if (expected->tt_type == VAR_FUNC) |
| 634 | { |
| 635 | if (expected->tt_member != &t_unknown) |
| 636 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 637 | if (ret == OK && expected->tt_argcount != -1 |
| 638 | && (actual->tt_argcount < expected->tt_min_argcount |
| 639 | || actual->tt_argcount > expected->tt_argcount)) |
| 640 | ret = FAIL; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 641 | if (expected->tt_args != NULL && actual->tt_args != NULL) |
| 642 | { |
| 643 | int i; |
| 644 | |
| 645 | for (i = 0; i < expected->tt_argcount; ++i) |
| 646 | if (check_type(expected->tt_args[i], actual->tt_args[i], |
| 647 | FALSE) == FAIL) |
| 648 | { |
| 649 | ret = FAIL; |
| 650 | break; |
| 651 | } |
| 652 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 653 | } |
| 654 | if (ret == FAIL && give_msg) |
| 655 | type_mismatch(expected, actual); |
| 656 | } |
| 657 | return ret; |
| 658 | } |
| 659 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 660 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 661 | ///////////////////////////////////////////////////////////////////// |
| 662 | // Following generate_ functions expect the caller to call ga_grow(). |
| 663 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 664 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 665 | #define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 666 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 667 | /* |
| 668 | * Generate an instruction without arguments. |
| 669 | * Returns a pointer to the new instruction, NULL if failed. |
| 670 | */ |
| 671 | static isn_T * |
| 672 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 673 | { |
| 674 | garray_T *instr = &cctx->ctx_instr; |
| 675 | isn_T *isn; |
| 676 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 677 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 678 | if (ga_grow(instr, 1) == FAIL) |
| 679 | return NULL; |
| 680 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 681 | isn->isn_type = isn_type; |
| 682 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 683 | ++instr->ga_len; |
| 684 | |
| 685 | return isn; |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * Generate an instruction without arguments. |
| 690 | * "drop" will be removed from the stack. |
| 691 | * Returns a pointer to the new instruction, NULL if failed. |
| 692 | */ |
| 693 | static isn_T * |
| 694 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 695 | { |
| 696 | garray_T *stack = &cctx->ctx_type_stack; |
| 697 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 698 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 699 | stack->ga_len -= drop; |
| 700 | return generate_instr(cctx, isn_type); |
| 701 | } |
| 702 | |
| 703 | /* |
| 704 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 705 | */ |
| 706 | static isn_T * |
| 707 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 708 | { |
| 709 | isn_T *isn; |
| 710 | garray_T *stack = &cctx->ctx_type_stack; |
| 711 | |
| 712 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 713 | return NULL; |
| 714 | |
| 715 | if (ga_grow(stack, 1) == FAIL) |
| 716 | return NULL; |
| 717 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 718 | ++stack->ga_len; |
| 719 | |
| 720 | return isn; |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 725 | */ |
| 726 | static int |
| 727 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 728 | { |
| 729 | isn_T *isn; |
| 730 | garray_T *stack = &cctx->ctx_type_stack; |
| 731 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 732 | |
| 733 | if ((*type)->tt_type == VAR_STRING) |
| 734 | return OK; |
| 735 | *type = &t_string; |
| 736 | |
| 737 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 738 | return FAIL; |
| 739 | isn->isn_arg.number = offset; |
| 740 | |
| 741 | return OK; |
| 742 | } |
| 743 | |
| 744 | static int |
| 745 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 746 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 747 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 748 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 749 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 750 | { |
| 751 | if (*op == '+') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 752 | emsg(_("E1051: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 753 | else |
| 754 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 755 | return FAIL; |
| 756 | } |
| 757 | return OK; |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * Generate an instruction with two arguments. The instruction depends on the |
| 762 | * type of the arguments. |
| 763 | */ |
| 764 | static int |
| 765 | generate_two_op(cctx_T *cctx, char_u *op) |
| 766 | { |
| 767 | garray_T *stack = &cctx->ctx_type_stack; |
| 768 | type_T *type1; |
| 769 | type_T *type2; |
| 770 | vartype_T vartype; |
| 771 | isn_T *isn; |
| 772 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 773 | RETURN_OK_IF_SKIP(cctx); |
| 774 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 775 | // Get the known type of the two items on the stack. If they are matching |
| 776 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 777 | // checking. |
| 778 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 779 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 780 | vartype = VAR_ANY; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 781 | if (type1->tt_type == type2->tt_type |
| 782 | && (type1->tt_type == VAR_NUMBER |
| 783 | || type1->tt_type == VAR_LIST |
| 784 | #ifdef FEAT_FLOAT |
| 785 | || type1->tt_type == VAR_FLOAT |
| 786 | #endif |
| 787 | || type1->tt_type == VAR_BLOB)) |
| 788 | vartype = type1->tt_type; |
| 789 | |
| 790 | switch (*op) |
| 791 | { |
| 792 | case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 793 | && type1->tt_type != VAR_ANY |
| 794 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 795 | && check_number_or_float( |
| 796 | type1->tt_type, type2->tt_type, op) == FAIL) |
| 797 | return FAIL; |
| 798 | isn = generate_instr_drop(cctx, |
| 799 | vartype == VAR_NUMBER ? ISN_OPNR |
| 800 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 801 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 802 | #ifdef FEAT_FLOAT |
| 803 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 804 | #endif |
| 805 | : ISN_OPANY, 1); |
| 806 | if (isn != NULL) |
| 807 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 808 | break; |
| 809 | |
| 810 | case '-': |
| 811 | case '*': |
| 812 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 813 | op) == FAIL) |
| 814 | return FAIL; |
| 815 | if (vartype == VAR_NUMBER) |
| 816 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 817 | #ifdef FEAT_FLOAT |
| 818 | else if (vartype == VAR_FLOAT) |
| 819 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 820 | #endif |
| 821 | else |
| 822 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 823 | if (isn != NULL) |
| 824 | isn->isn_arg.op.op_type = *op == '*' |
| 825 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 826 | break; |
| 827 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 828 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 829 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 830 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 831 | && type2->tt_type != VAR_NUMBER)) |
| 832 | { |
| 833 | emsg(_("E1035: % requires number arguments")); |
| 834 | return FAIL; |
| 835 | } |
| 836 | isn = generate_instr_drop(cctx, |
| 837 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 838 | if (isn != NULL) |
| 839 | isn->isn_arg.op.op_type = EXPR_REM; |
| 840 | break; |
| 841 | } |
| 842 | |
| 843 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 844 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 845 | { |
| 846 | type_T *type = &t_any; |
| 847 | |
| 848 | #ifdef FEAT_FLOAT |
| 849 | // float+number and number+float results in float |
| 850 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 851 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 852 | type = &t_float; |
| 853 | #endif |
| 854 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 855 | } |
| 856 | |
| 857 | return OK; |
| 858 | } |
| 859 | |
| 860 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 861 | * Get the instruction to use for comparing "type1" with "type2" |
| 862 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 863 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 864 | static isntype_T |
| 865 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 866 | { |
| 867 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 868 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 869 | if (type1 == VAR_UNKNOWN) |
| 870 | type1 = VAR_ANY; |
| 871 | if (type2 == VAR_UNKNOWN) |
| 872 | type2 = VAR_ANY; |
| 873 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 874 | if (type1 == type2) |
| 875 | { |
| 876 | switch (type1) |
| 877 | { |
| 878 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 879 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 880 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 881 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 882 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 883 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 884 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 885 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 886 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 887 | default: isntype = ISN_COMPAREANY; break; |
| 888 | } |
| 889 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 890 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 891 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 892 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 893 | isntype = ISN_COMPAREANY; |
| 894 | |
| 895 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 896 | && (isntype == ISN_COMPAREBOOL |
| 897 | || isntype == ISN_COMPARESPECIAL |
| 898 | || isntype == ISN_COMPARENR |
| 899 | || isntype == ISN_COMPAREFLOAT)) |
| 900 | { |
| 901 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 902 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 903 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 904 | } |
| 905 | if (isntype == ISN_DROP |
| 906 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 907 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 908 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 909 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 910 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 911 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 912 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 913 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 914 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 915 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 916 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 917 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 918 | return isntype; |
| 919 | } |
| 920 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 921 | int |
| 922 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 923 | { |
| 924 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 925 | return FAIL; |
| 926 | return OK; |
| 927 | } |
| 928 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 929 | /* |
| 930 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 931 | */ |
| 932 | static int |
| 933 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 934 | { |
| 935 | isntype_T isntype; |
| 936 | isn_T *isn; |
| 937 | garray_T *stack = &cctx->ctx_type_stack; |
| 938 | vartype_T type1; |
| 939 | vartype_T type2; |
| 940 | |
| 941 | RETURN_OK_IF_SKIP(cctx); |
| 942 | |
| 943 | // Get the known type of the two items on the stack. If they are matching |
| 944 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 945 | // checking. |
| 946 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 947 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 948 | isntype = get_compare_isn(exptype, type1, type2); |
| 949 | if (isntype == ISN_DROP) |
| 950 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 951 | |
| 952 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 953 | return FAIL; |
| 954 | isn->isn_arg.op.op_type = exptype; |
| 955 | isn->isn_arg.op.op_ic = ic; |
| 956 | |
| 957 | // takes two arguments, puts one bool back |
| 958 | if (stack->ga_len >= 2) |
| 959 | { |
| 960 | --stack->ga_len; |
| 961 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 962 | } |
| 963 | |
| 964 | return OK; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Generate an ISN_2BOOL instruction. |
| 969 | */ |
| 970 | static int |
| 971 | generate_2BOOL(cctx_T *cctx, int invert) |
| 972 | { |
| 973 | isn_T *isn; |
| 974 | garray_T *stack = &cctx->ctx_type_stack; |
| 975 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 976 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 977 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 978 | return FAIL; |
| 979 | isn->isn_arg.number = invert; |
| 980 | |
| 981 | // type becomes bool |
| 982 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 983 | |
| 984 | return OK; |
| 985 | } |
| 986 | |
| 987 | static int |
| 988 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 989 | { |
| 990 | isn_T *isn; |
| 991 | garray_T *stack = &cctx->ctx_type_stack; |
| 992 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 993 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 994 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 995 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 996 | // TODO: whole type, e.g. for a function also arg and return types |
| 997 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 998 | isn->isn_arg.type.ct_off = offset; |
| 999 | |
| 1000 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1001 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1002 | |
| 1003 | return OK; |
| 1004 | } |
| 1005 | |
| 1006 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1007 | * Check that |
| 1008 | * - "actual" is "expected" type or |
| 1009 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 1010 | * - return FAIL. |
| 1011 | */ |
| 1012 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1013 | need_type( |
| 1014 | type_T *actual, |
| 1015 | type_T *expected, |
| 1016 | int offset, |
| 1017 | cctx_T *cctx, |
| 1018 | int silent) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1019 | { |
| 1020 | if (check_type(expected, actual, FALSE) == OK) |
| 1021 | return OK; |
| 1022 | if (actual->tt_type != VAR_ANY |
| 1023 | && actual->tt_type != VAR_UNKNOWN |
| 1024 | && !(actual->tt_type == VAR_FUNC |
| 1025 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 1026 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1027 | if (!silent) |
| 1028 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1029 | return FAIL; |
| 1030 | } |
| 1031 | generate_TYPECHECK(cctx, expected, offset); |
| 1032 | return OK; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1036 | * Generate an ISN_PUSHNR instruction. |
| 1037 | */ |
| 1038 | static int |
| 1039 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 1040 | { |
| 1041 | isn_T *isn; |
| 1042 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1043 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1044 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 1045 | return FAIL; |
| 1046 | isn->isn_arg.number = number; |
| 1047 | |
| 1048 | return OK; |
| 1049 | } |
| 1050 | |
| 1051 | /* |
| 1052 | * Generate an ISN_PUSHBOOL instruction. |
| 1053 | */ |
| 1054 | static int |
| 1055 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 1056 | { |
| 1057 | isn_T *isn; |
| 1058 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1059 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1060 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 1061 | return FAIL; |
| 1062 | isn->isn_arg.number = number; |
| 1063 | |
| 1064 | return OK; |
| 1065 | } |
| 1066 | |
| 1067 | /* |
| 1068 | * Generate an ISN_PUSHSPEC instruction. |
| 1069 | */ |
| 1070 | static int |
| 1071 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 1072 | { |
| 1073 | isn_T *isn; |
| 1074 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1075 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1076 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 1077 | return FAIL; |
| 1078 | isn->isn_arg.number = number; |
| 1079 | |
| 1080 | return OK; |
| 1081 | } |
| 1082 | |
| 1083 | #ifdef FEAT_FLOAT |
| 1084 | /* |
| 1085 | * Generate an ISN_PUSHF instruction. |
| 1086 | */ |
| 1087 | static int |
| 1088 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 1089 | { |
| 1090 | isn_T *isn; |
| 1091 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1092 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1093 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 1094 | return FAIL; |
| 1095 | isn->isn_arg.fnumber = fnumber; |
| 1096 | |
| 1097 | return OK; |
| 1098 | } |
| 1099 | #endif |
| 1100 | |
| 1101 | /* |
| 1102 | * Generate an ISN_PUSHS instruction. |
| 1103 | * Consumes "str". |
| 1104 | */ |
| 1105 | static int |
| 1106 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 1107 | { |
| 1108 | isn_T *isn; |
| 1109 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1110 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1111 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 1112 | return FAIL; |
| 1113 | isn->isn_arg.string = str; |
| 1114 | |
| 1115 | return OK; |
| 1116 | } |
| 1117 | |
| 1118 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1119 | * Generate an ISN_PUSHCHANNEL instruction. |
| 1120 | * Consumes "channel". |
| 1121 | */ |
| 1122 | static int |
| 1123 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 1124 | { |
| 1125 | isn_T *isn; |
| 1126 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1127 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1128 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 1129 | return FAIL; |
| 1130 | isn->isn_arg.channel = channel; |
| 1131 | |
| 1132 | return OK; |
| 1133 | } |
| 1134 | |
| 1135 | /* |
| 1136 | * Generate an ISN_PUSHJOB instruction. |
| 1137 | * Consumes "job". |
| 1138 | */ |
| 1139 | static int |
| 1140 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 1141 | { |
| 1142 | isn_T *isn; |
| 1143 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1144 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1145 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1146 | return FAIL; |
| 1147 | isn->isn_arg.job = job; |
| 1148 | |
| 1149 | return OK; |
| 1150 | } |
| 1151 | |
| 1152 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1153 | * Generate an ISN_PUSHBLOB instruction. |
| 1154 | * Consumes "blob". |
| 1155 | */ |
| 1156 | static int |
| 1157 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1158 | { |
| 1159 | isn_T *isn; |
| 1160 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1161 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1162 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1163 | return FAIL; |
| 1164 | isn->isn_arg.blob = blob; |
| 1165 | |
| 1166 | return OK; |
| 1167 | } |
| 1168 | |
| 1169 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1170 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1171 | * Consumes "name". |
| 1172 | */ |
| 1173 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1174 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1175 | { |
| 1176 | isn_T *isn; |
| 1177 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1178 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1179 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1180 | return FAIL; |
| 1181 | isn->isn_arg.string = name; |
| 1182 | |
| 1183 | return OK; |
| 1184 | } |
| 1185 | |
| 1186 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1187 | * Generate an ISN_GETITEM instruction with "index". |
| 1188 | */ |
| 1189 | static int |
| 1190 | generate_GETITEM(cctx_T *cctx, int index) |
| 1191 | { |
| 1192 | isn_T *isn; |
| 1193 | garray_T *stack = &cctx->ctx_type_stack; |
| 1194 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1195 | type_T *item_type = &t_any; |
| 1196 | |
| 1197 | RETURN_OK_IF_SKIP(cctx); |
| 1198 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1199 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1200 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1201 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1202 | emsg(_(e_listreq)); |
| 1203 | return FAIL; |
| 1204 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1205 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1206 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1207 | return FAIL; |
| 1208 | isn->isn_arg.number = index; |
| 1209 | |
| 1210 | // add the item type to the type stack |
| 1211 | if (ga_grow(stack, 1) == FAIL) |
| 1212 | return FAIL; |
| 1213 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 1214 | ++stack->ga_len; |
| 1215 | return OK; |
| 1216 | } |
| 1217 | |
| 1218 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 1219 | * Generate an ISN_SLICE instruction with "count". |
| 1220 | */ |
| 1221 | static int |
| 1222 | generate_SLICE(cctx_T *cctx, int count) |
| 1223 | { |
| 1224 | isn_T *isn; |
| 1225 | |
| 1226 | RETURN_OK_IF_SKIP(cctx); |
| 1227 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1228 | return FAIL; |
| 1229 | isn->isn_arg.number = count; |
| 1230 | return OK; |
| 1231 | } |
| 1232 | |
| 1233 | /* |
| 1234 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1235 | */ |
| 1236 | static int |
| 1237 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1238 | { |
| 1239 | isn_T *isn; |
| 1240 | |
| 1241 | RETURN_OK_IF_SKIP(cctx); |
| 1242 | |
| 1243 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1244 | return FAIL; |
| 1245 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1246 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1247 | |
| 1248 | return OK; |
| 1249 | } |
| 1250 | |
| 1251 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1252 | * Generate an ISN_STORE instruction. |
| 1253 | */ |
| 1254 | static int |
| 1255 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1256 | { |
| 1257 | isn_T *isn; |
| 1258 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1259 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1260 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1261 | return FAIL; |
| 1262 | if (name != NULL) |
| 1263 | isn->isn_arg.string = vim_strsave(name); |
| 1264 | else |
| 1265 | isn->isn_arg.number = idx; |
| 1266 | |
| 1267 | return OK; |
| 1268 | } |
| 1269 | |
| 1270 | /* |
| 1271 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1272 | */ |
| 1273 | static int |
| 1274 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1275 | { |
| 1276 | isn_T *isn; |
| 1277 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1278 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1279 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1280 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1281 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1282 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1283 | |
| 1284 | return OK; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * Generate an ISN_STOREOPT instruction |
| 1289 | */ |
| 1290 | static int |
| 1291 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1292 | { |
| 1293 | isn_T *isn; |
| 1294 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1295 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1296 | if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL) |
| 1297 | return FAIL; |
| 1298 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1299 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1300 | |
| 1301 | return OK; |
| 1302 | } |
| 1303 | |
| 1304 | /* |
| 1305 | * Generate an ISN_LOAD or similar instruction. |
| 1306 | */ |
| 1307 | static int |
| 1308 | generate_LOAD( |
| 1309 | cctx_T *cctx, |
| 1310 | isntype_T isn_type, |
| 1311 | int idx, |
| 1312 | char_u *name, |
| 1313 | type_T *type) |
| 1314 | { |
| 1315 | isn_T *isn; |
| 1316 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1317 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1318 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1319 | return FAIL; |
| 1320 | if (name != NULL) |
| 1321 | isn->isn_arg.string = vim_strsave(name); |
| 1322 | else |
| 1323 | isn->isn_arg.number = idx; |
| 1324 | |
| 1325 | return OK; |
| 1326 | } |
| 1327 | |
| 1328 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1329 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1330 | */ |
| 1331 | static int |
| 1332 | generate_LOADV( |
| 1333 | cctx_T *cctx, |
| 1334 | char_u *name, |
| 1335 | int error) |
| 1336 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1337 | int di_flags; |
| 1338 | int vidx = find_vim_var(name, &di_flags); |
| 1339 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1340 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1341 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1342 | if (vidx < 0) |
| 1343 | { |
| 1344 | if (error) |
| 1345 | semsg(_(e_var_notfound), name); |
| 1346 | return FAIL; |
| 1347 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1348 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1349 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1350 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1354 | * Generate an ISN_UNLET instruction. |
| 1355 | */ |
| 1356 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1357 | generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1358 | { |
| 1359 | isn_T *isn; |
| 1360 | |
| 1361 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1362 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1363 | return FAIL; |
| 1364 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1365 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1366 | |
| 1367 | return OK; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1371 | * Generate an ISN_LOADS instruction. |
| 1372 | */ |
| 1373 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1374 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1375 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1376 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1377 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1378 | int sid, |
| 1379 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1380 | { |
| 1381 | isn_T *isn; |
| 1382 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1383 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1384 | if (isn_type == ISN_LOADS) |
| 1385 | isn = generate_instr_type(cctx, isn_type, type); |
| 1386 | else |
| 1387 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1388 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1389 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1390 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1391 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1392 | |
| 1393 | return OK; |
| 1394 | } |
| 1395 | |
| 1396 | /* |
| 1397 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1398 | */ |
| 1399 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1400 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1401 | cctx_T *cctx, |
| 1402 | isntype_T isn_type, |
| 1403 | int sid, |
| 1404 | int idx, |
| 1405 | type_T *type) |
| 1406 | { |
| 1407 | isn_T *isn; |
| 1408 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1409 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1410 | if (isn_type == ISN_LOADSCRIPT) |
| 1411 | isn = generate_instr_type(cctx, isn_type, type); |
| 1412 | else |
| 1413 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1414 | if (isn == NULL) |
| 1415 | return FAIL; |
| 1416 | isn->isn_arg.script.script_sid = sid; |
| 1417 | isn->isn_arg.script.script_idx = idx; |
| 1418 | return OK; |
| 1419 | } |
| 1420 | |
| 1421 | /* |
| 1422 | * Generate an ISN_NEWLIST instruction. |
| 1423 | */ |
| 1424 | static int |
| 1425 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1426 | { |
| 1427 | isn_T *isn; |
| 1428 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1429 | type_T *type; |
| 1430 | type_T *member; |
| 1431 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1432 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1433 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1434 | return FAIL; |
| 1435 | isn->isn_arg.number = count; |
| 1436 | |
| 1437 | // drop the value types |
| 1438 | stack->ga_len -= count; |
| 1439 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1440 | // Use the first value type for the list member type. Use "any" for an |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1441 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1442 | if (count > 0) |
| 1443 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1444 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1445 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1446 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1447 | |
| 1448 | // add the list type to the type stack |
| 1449 | if (ga_grow(stack, 1) == FAIL) |
| 1450 | return FAIL; |
| 1451 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1452 | ++stack->ga_len; |
| 1453 | |
| 1454 | return OK; |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * Generate an ISN_NEWDICT instruction. |
| 1459 | */ |
| 1460 | static int |
| 1461 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1462 | { |
| 1463 | isn_T *isn; |
| 1464 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1465 | type_T *type; |
| 1466 | type_T *member; |
| 1467 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1468 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1469 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1470 | return FAIL; |
| 1471 | isn->isn_arg.number = count; |
| 1472 | |
| 1473 | // drop the key and value types |
| 1474 | stack->ga_len -= 2 * count; |
| 1475 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1476 | // Use the first value type for the list member type. Use "void" for an |
| 1477 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1478 | if (count > 0) |
| 1479 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1480 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1481 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1482 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1483 | |
| 1484 | // add the dict type to the type stack |
| 1485 | if (ga_grow(stack, 1) == FAIL) |
| 1486 | return FAIL; |
| 1487 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1488 | ++stack->ga_len; |
| 1489 | |
| 1490 | return OK; |
| 1491 | } |
| 1492 | |
| 1493 | /* |
| 1494 | * Generate an ISN_FUNCREF instruction. |
| 1495 | */ |
| 1496 | static int |
| 1497 | generate_FUNCREF(cctx_T *cctx, int dfunc_idx) |
| 1498 | { |
| 1499 | isn_T *isn; |
| 1500 | garray_T *stack = &cctx->ctx_type_stack; |
| 1501 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1502 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1503 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1504 | return FAIL; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1505 | isn->isn_arg.funcref.fr_func = dfunc_idx; |
| 1506 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1507 | |
| 1508 | if (ga_grow(stack, 1) == FAIL) |
| 1509 | return FAIL; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1510 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1511 | // TODO: argument and return types |
| 1512 | ++stack->ga_len; |
| 1513 | |
| 1514 | return OK; |
| 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * Generate an ISN_JUMP instruction. |
| 1519 | */ |
| 1520 | static int |
| 1521 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1522 | { |
| 1523 | isn_T *isn; |
| 1524 | garray_T *stack = &cctx->ctx_type_stack; |
| 1525 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1526 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1527 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1528 | return FAIL; |
| 1529 | isn->isn_arg.jump.jump_when = when; |
| 1530 | isn->isn_arg.jump.jump_where = where; |
| 1531 | |
| 1532 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1533 | --stack->ga_len; |
| 1534 | |
| 1535 | return OK; |
| 1536 | } |
| 1537 | |
| 1538 | static int |
| 1539 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1540 | { |
| 1541 | isn_T *isn; |
| 1542 | garray_T *stack = &cctx->ctx_type_stack; |
| 1543 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1544 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1545 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1546 | return FAIL; |
| 1547 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1548 | |
| 1549 | if (ga_grow(stack, 1) == FAIL) |
| 1550 | return FAIL; |
| 1551 | // type doesn't matter, will be stored next |
| 1552 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1553 | ++stack->ga_len; |
| 1554 | |
| 1555 | return OK; |
| 1556 | } |
| 1557 | |
| 1558 | /* |
| 1559 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1560 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1561 | * Return FAIL if the number of arguments is wrong. |
| 1562 | */ |
| 1563 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1564 | generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1565 | { |
| 1566 | isn_T *isn; |
| 1567 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1568 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1569 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1570 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1571 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1572 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1573 | argoff = check_internal_func(func_idx, argcount); |
| 1574 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1575 | return FAIL; |
| 1576 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1577 | if (method_call && argoff > 1) |
| 1578 | { |
| 1579 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1580 | return FAIL; |
| 1581 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1582 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1583 | } |
| 1584 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1585 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1586 | return FAIL; |
| 1587 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1588 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1589 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1590 | for (i = 0; i < argcount; ++i) |
| 1591 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1592 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1593 | stack->ga_len -= argcount; // drop the arguments |
| 1594 | if (ga_grow(stack, 1) == FAIL) |
| 1595 | return FAIL; |
| 1596 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1597 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1598 | ++stack->ga_len; // add return value |
| 1599 | |
| 1600 | return OK; |
| 1601 | } |
| 1602 | |
| 1603 | /* |
| 1604 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1605 | * Return FAIL if the number of arguments is wrong. |
| 1606 | */ |
| 1607 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1608 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1609 | { |
| 1610 | isn_T *isn; |
| 1611 | garray_T *stack = &cctx->ctx_type_stack; |
| 1612 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1613 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1614 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1615 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1616 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1617 | { |
| 1618 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1619 | return FAIL; |
| 1620 | } |
| 1621 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1622 | { |
| 1623 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1624 | return FAIL; |
| 1625 | } |
| 1626 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1627 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1628 | { |
| 1629 | int i; |
| 1630 | |
| 1631 | for (i = 0; i < argcount; ++i) |
| 1632 | { |
| 1633 | type_T *expected; |
| 1634 | type_T *actual; |
| 1635 | |
| 1636 | if (i < regular_args) |
| 1637 | { |
| 1638 | if (ufunc->uf_arg_types == NULL) |
| 1639 | continue; |
| 1640 | expected = ufunc->uf_arg_types[i]; |
| 1641 | } |
| 1642 | else |
| 1643 | expected = ufunc->uf_va_type->tt_member; |
| 1644 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1645 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1646 | { |
| 1647 | arg_type_mismatch(expected, actual, i + 1); |
| 1648 | return FAIL; |
| 1649 | } |
| 1650 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1651 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 1652 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1653 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1654 | } |
| 1655 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1656 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1657 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1658 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1659 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1660 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1661 | { |
| 1662 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1663 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1664 | } |
| 1665 | else |
| 1666 | { |
| 1667 | // A user function may be deleted and redefined later, can't use the |
| 1668 | // ufunc pointer, need to look it up again at runtime. |
| 1669 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1670 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1671 | } |
| 1672 | |
| 1673 | stack->ga_len -= argcount; // drop the arguments |
| 1674 | if (ga_grow(stack, 1) == FAIL) |
| 1675 | return FAIL; |
| 1676 | // add return value |
| 1677 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1678 | ++stack->ga_len; |
| 1679 | |
| 1680 | return OK; |
| 1681 | } |
| 1682 | |
| 1683 | /* |
| 1684 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1685 | */ |
| 1686 | static int |
| 1687 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1688 | { |
| 1689 | isn_T *isn; |
| 1690 | garray_T *stack = &cctx->ctx_type_stack; |
| 1691 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1692 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1693 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1694 | return FAIL; |
| 1695 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1696 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1697 | |
| 1698 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1699 | if (ga_grow(stack, 1) == FAIL) |
| 1700 | return FAIL; |
| 1701 | // add return value |
| 1702 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1703 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1704 | |
| 1705 | return OK; |
| 1706 | } |
| 1707 | |
| 1708 | /* |
| 1709 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1710 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1711 | */ |
| 1712 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1713 | generate_PCALL( |
| 1714 | cctx_T *cctx, |
| 1715 | int argcount, |
| 1716 | char_u *name, |
| 1717 | type_T *type, |
| 1718 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1719 | { |
| 1720 | isn_T *isn; |
| 1721 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1722 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1723 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1724 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1725 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1726 | if (type->tt_type == VAR_ANY) |
| 1727 | ret_type = &t_any; |
| 1728 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1729 | { |
| 1730 | if (type->tt_argcount != -1) |
| 1731 | { |
| 1732 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1733 | |
| 1734 | if (argcount < type->tt_min_argcount - varargs) |
| 1735 | { |
| 1736 | semsg(_(e_toofewarg), "[reference]"); |
| 1737 | return FAIL; |
| 1738 | } |
| 1739 | if (!varargs && argcount > type->tt_argcount) |
| 1740 | { |
| 1741 | semsg(_(e_toomanyarg), "[reference]"); |
| 1742 | return FAIL; |
| 1743 | } |
| 1744 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1745 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1746 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1747 | else |
| 1748 | { |
| 1749 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1750 | return FAIL; |
| 1751 | } |
| 1752 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1753 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1754 | return FAIL; |
| 1755 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1756 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1757 | |
| 1758 | stack->ga_len -= argcount; // drop the arguments |
| 1759 | |
| 1760 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1761 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1762 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1763 | // If partial is above the arguments it must be cleared and replaced with |
| 1764 | // the return value. |
| 1765 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1766 | return FAIL; |
| 1767 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1768 | return OK; |
| 1769 | } |
| 1770 | |
| 1771 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1772 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1773 | */ |
| 1774 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1775 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1776 | { |
| 1777 | isn_T *isn; |
| 1778 | garray_T *stack = &cctx->ctx_type_stack; |
| 1779 | type_T *type; |
| 1780 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1781 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1782 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1783 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1784 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1785 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1786 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1787 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1788 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1789 | { |
| 1790 | emsg(_(e_dictreq)); |
| 1791 | return FAIL; |
| 1792 | } |
| 1793 | // change dict type to dict member type |
| 1794 | if (type->tt_type == VAR_DICT) |
| 1795 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1796 | |
| 1797 | return OK; |
| 1798 | } |
| 1799 | |
| 1800 | /* |
| 1801 | * Generate an ISN_ECHO instruction. |
| 1802 | */ |
| 1803 | static int |
| 1804 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1805 | { |
| 1806 | isn_T *isn; |
| 1807 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1808 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1809 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1810 | return FAIL; |
| 1811 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1812 | isn->isn_arg.echo.echo_count = count; |
| 1813 | |
| 1814 | return OK; |
| 1815 | } |
| 1816 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1817 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1818 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1819 | */ |
| 1820 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1821 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1822 | { |
| 1823 | isn_T *isn; |
| 1824 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1825 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1826 | return FAIL; |
| 1827 | isn->isn_arg.number = count; |
| 1828 | |
| 1829 | return OK; |
| 1830 | } |
| 1831 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1832 | static int |
| 1833 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1834 | { |
| 1835 | isn_T *isn; |
| 1836 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1837 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1838 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1839 | return FAIL; |
| 1840 | isn->isn_arg.string = vim_strsave(line); |
| 1841 | return OK; |
| 1842 | } |
| 1843 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1844 | static int |
| 1845 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1846 | { |
| 1847 | isn_T *isn; |
| 1848 | |
| 1849 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1850 | return FAIL; |
| 1851 | isn->isn_arg.number = count; |
| 1852 | return OK; |
| 1853 | } |
| 1854 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1855 | /* |
| 1856 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1857 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1858 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1859 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1860 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1861 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1862 | lvar_T *lvar; |
| 1863 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1864 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1865 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1866 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1867 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1871 | return NULL; |
| 1872 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1873 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1874 | // Every local variable uses the next entry on the stack. We could re-use |
| 1875 | // the last ones when leaving a scope, but then variables used in a closure |
| 1876 | // might get overwritten. To keep things simple do not re-use stack |
| 1877 | // entries. This is less efficient, but memory is cheap these days. |
| 1878 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1879 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1880 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1881 | lvar->lv_const = isConst; |
| 1882 | lvar->lv_type = type; |
| 1883 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1884 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1888 | * Remove local variables above "new_top". |
| 1889 | */ |
| 1890 | static void |
| 1891 | unwind_locals(cctx_T *cctx, int new_top) |
| 1892 | { |
| 1893 | if (cctx->ctx_locals.ga_len > new_top) |
| 1894 | { |
| 1895 | int idx; |
| 1896 | lvar_T *lvar; |
| 1897 | |
| 1898 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1899 | { |
| 1900 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1901 | vim_free(lvar->lv_name); |
| 1902 | } |
| 1903 | } |
| 1904 | cctx->ctx_locals.ga_len = new_top; |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * Free all local variables. |
| 1909 | */ |
| 1910 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1911 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1912 | { |
| 1913 | unwind_locals(cctx, 0); |
| 1914 | ga_clear(&cctx->ctx_locals); |
| 1915 | } |
| 1916 | |
| 1917 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1918 | * Skip over a type definition and return a pointer to just after it. |
| 1919 | */ |
| 1920 | char_u * |
| 1921 | skip_type(char_u *start) |
| 1922 | { |
| 1923 | char_u *p = start; |
| 1924 | |
| 1925 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1926 | ++p; |
| 1927 | |
| 1928 | // Skip over "<type>"; this is permissive about white space. |
| 1929 | if (*skipwhite(p) == '<') |
| 1930 | { |
| 1931 | p = skipwhite(p); |
| 1932 | p = skip_type(skipwhite(p + 1)); |
| 1933 | p = skipwhite(p); |
| 1934 | if (*p == '>') |
| 1935 | ++p; |
| 1936 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1937 | else if ((*p == '(' || (*p == ':' && VIM_ISWHITE(p[1]))) |
| 1938 | && STRNCMP("func", start, 4) == 0) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1939 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1940 | if (*p == '(') |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1941 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1942 | // handle func(args): type |
| 1943 | ++p; |
| 1944 | while (*p != ')' && *p != NUL) |
| 1945 | { |
| 1946 | char_u *sp = p; |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1947 | |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1948 | p = skip_type(p); |
| 1949 | if (p == sp) |
| 1950 | return p; // syntax error |
| 1951 | if (*p == ',') |
| 1952 | p = skipwhite(p + 1); |
| 1953 | } |
| 1954 | if (*p == ')') |
| 1955 | { |
| 1956 | if (p[1] == ':') |
| 1957 | p = skip_type(skipwhite(p + 2)); |
| 1958 | else |
Bram Moolenaar | bfba865 | 2020-07-23 20:09:10 +0200 | [diff] [blame] | 1959 | ++p; |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1960 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1961 | } |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1962 | else |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1963 | { |
Bram Moolenaar | 2f1980f | 2020-07-22 19:30:06 +0200 | [diff] [blame] | 1964 | // handle func: return_type |
| 1965 | p = skip_type(skipwhite(p + 1)); |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1966 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1967 | } |
| 1968 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1969 | return p; |
| 1970 | } |
| 1971 | |
| 1972 | /* |
| 1973 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1974 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1975 | * Returns NULL in case of failure. |
| 1976 | */ |
| 1977 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1978 | parse_type_member(char_u **arg, type_T *type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1979 | { |
| 1980 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1981 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1982 | |
| 1983 | if (**arg != '<') |
| 1984 | { |
| 1985 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1986 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1987 | else |
| 1988 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1989 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1990 | } |
| 1991 | *arg = skipwhite(*arg + 1); |
| 1992 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1993 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1994 | |
| 1995 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1996 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1997 | { |
| 1998 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1999 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2000 | } |
| 2001 | ++*arg; |
| 2002 | |
| 2003 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2004 | return get_list_type(member_type, type_gap); |
| 2005 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 2010 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2011 | */ |
| 2012 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2013 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2014 | { |
| 2015 | char_u *p = *arg; |
| 2016 | size_t len; |
| 2017 | |
| 2018 | // skip over the first word |
| 2019 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 2020 | ++p; |
| 2021 | len = p - *arg; |
| 2022 | |
| 2023 | switch (**arg) |
| 2024 | { |
| 2025 | case 'a': |
| 2026 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 2027 | { |
| 2028 | *arg += len; |
| 2029 | return &t_any; |
| 2030 | } |
| 2031 | break; |
| 2032 | case 'b': |
| 2033 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 2034 | { |
| 2035 | *arg += len; |
| 2036 | return &t_bool; |
| 2037 | } |
| 2038 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 2039 | { |
| 2040 | *arg += len; |
| 2041 | return &t_blob; |
| 2042 | } |
| 2043 | break; |
| 2044 | case 'c': |
| 2045 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 2046 | { |
| 2047 | *arg += len; |
| 2048 | return &t_channel; |
| 2049 | } |
| 2050 | break; |
| 2051 | case 'd': |
| 2052 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 2053 | { |
| 2054 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2055 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2056 | } |
| 2057 | break; |
| 2058 | case 'f': |
| 2059 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 2060 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2061 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2062 | *arg += len; |
| 2063 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2064 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2065 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2066 | return &t_any; |
| 2067 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2068 | } |
| 2069 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 2070 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2071 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2072 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2073 | int argcount = -1; |
| 2074 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2075 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2076 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 2077 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2078 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2079 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2080 | if (**arg == '(') |
| 2081 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2082 | // "func" may or may not return a value, "func()" does |
| 2083 | // not return a value. |
| 2084 | ret_type = &t_void; |
| 2085 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2086 | p = ++*arg; |
| 2087 | argcount = 0; |
| 2088 | while (*p != NUL && *p != ')') |
| 2089 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2090 | if (*p == '?') |
| 2091 | { |
| 2092 | if (first_optional == -1) |
| 2093 | first_optional = argcount; |
| 2094 | ++p; |
| 2095 | } |
| 2096 | else if (first_optional != -1) |
| 2097 | { |
| 2098 | emsg(_("E1007: mandatory argument after optional argument")); |
| 2099 | return &t_any; |
| 2100 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2101 | else if (STRNCMP(p, "...", 3) == 0) |
| 2102 | { |
| 2103 | flags |= TTFLAG_VARARGS; |
| 2104 | p += 3; |
| 2105 | } |
| 2106 | |
| 2107 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 2108 | |
| 2109 | // Nothing comes after "...{type}". |
| 2110 | if (flags & TTFLAG_VARARGS) |
| 2111 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2112 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2113 | if (*p != ',' && *skipwhite(p) == ',') |
| 2114 | { |
| 2115 | semsg(_(e_no_white_before), ","); |
| 2116 | return &t_any; |
| 2117 | } |
| 2118 | if (*p == ',') |
| 2119 | { |
| 2120 | ++p; |
| 2121 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2122 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2123 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2124 | return &t_any; |
| 2125 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2126 | } |
| 2127 | p = skipwhite(p); |
| 2128 | if (argcount == MAX_FUNC_ARGS) |
| 2129 | { |
| 2130 | emsg(_("E740: Too many argument types")); |
| 2131 | return &t_any; |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | p = skipwhite(p); |
| 2136 | if (*p != ')') |
| 2137 | { |
| 2138 | emsg(_(e_missing_close)); |
| 2139 | return &t_any; |
| 2140 | } |
| 2141 | *arg = p + 1; |
| 2142 | } |
| 2143 | if (**arg == ':') |
| 2144 | { |
| 2145 | // parse return type |
| 2146 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2147 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2148 | semsg(_(e_white_after), ":"); |
| 2149 | *arg = skipwhite(*arg); |
| 2150 | ret_type = parse_type(arg, type_gap); |
| 2151 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2152 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2153 | type = get_func_type(ret_type, argcount, type_gap); |
| 2154 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2155 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2156 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 2157 | type->tt_flags = flags; |
| 2158 | if (argcount > 0) |
| 2159 | { |
| 2160 | type->tt_argcount = argcount; |
| 2161 | type->tt_min_argcount = first_optional == -1 |
| 2162 | ? argcount : first_optional; |
| 2163 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2164 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2165 | return &t_any; |
| 2166 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2167 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2168 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2169 | } |
| 2170 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2171 | } |
| 2172 | break; |
| 2173 | case 'j': |
| 2174 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 2175 | { |
| 2176 | *arg += len; |
| 2177 | return &t_job; |
| 2178 | } |
| 2179 | break; |
| 2180 | case 'l': |
| 2181 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 2182 | { |
| 2183 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2184 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2185 | } |
| 2186 | break; |
| 2187 | case 'n': |
| 2188 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 2189 | { |
| 2190 | *arg += len; |
| 2191 | return &t_number; |
| 2192 | } |
| 2193 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2194 | case 's': |
| 2195 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 2196 | { |
| 2197 | *arg += len; |
| 2198 | return &t_string; |
| 2199 | } |
| 2200 | break; |
| 2201 | case 'v': |
| 2202 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 2203 | { |
| 2204 | *arg += len; |
| 2205 | return &t_void; |
| 2206 | } |
| 2207 | break; |
| 2208 | } |
| 2209 | |
| 2210 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 2211 | return &t_any; |
| 2212 | } |
| 2213 | |
| 2214 | /* |
| 2215 | * Check if "type1" and "type2" are exactly the same. |
| 2216 | */ |
| 2217 | static int |
| 2218 | equal_type(type_T *type1, type_T *type2) |
| 2219 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2220 | int i; |
| 2221 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2222 | if (type1->tt_type != type2->tt_type) |
| 2223 | return FALSE; |
| 2224 | switch (type1->tt_type) |
| 2225 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2226 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2227 | case VAR_ANY: |
| 2228 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2229 | case VAR_SPECIAL: |
| 2230 | case VAR_BOOL: |
| 2231 | case VAR_NUMBER: |
| 2232 | case VAR_FLOAT: |
| 2233 | case VAR_STRING: |
| 2234 | case VAR_BLOB: |
| 2235 | case VAR_JOB: |
| 2236 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2237 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2238 | case VAR_LIST: |
| 2239 | case VAR_DICT: |
| 2240 | return equal_type(type1->tt_member, type2->tt_member); |
| 2241 | case VAR_FUNC: |
| 2242 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2243 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2244 | || type1->tt_argcount != type2->tt_argcount) |
| 2245 | return FALSE; |
| 2246 | if (type1->tt_argcount < 0 |
| 2247 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2248 | return TRUE; |
| 2249 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2250 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2251 | return FALSE; |
| 2252 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2253 | } |
| 2254 | return TRUE; |
| 2255 | } |
| 2256 | |
| 2257 | /* |
| 2258 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2259 | * "type2" and "dest" may be the same. |
| 2260 | */ |
| 2261 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2262 | common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2263 | { |
| 2264 | if (equal_type(type1, type2)) |
| 2265 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2266 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2267 | return; |
| 2268 | } |
| 2269 | |
| 2270 | if (type1->tt_type == type2->tt_type) |
| 2271 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2272 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2273 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2274 | type_T *common; |
| 2275 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2276 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2277 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2278 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2279 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2280 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2281 | return; |
| 2282 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2283 | if (type1->tt_type == VAR_FUNC) |
| 2284 | { |
| 2285 | type_T *common; |
| 2286 | |
| 2287 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2288 | if (type1->tt_argcount == type2->tt_argcount |
| 2289 | && type1->tt_argcount >= 0) |
| 2290 | { |
| 2291 | int argcount = type1->tt_argcount; |
| 2292 | int i; |
| 2293 | |
| 2294 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2295 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2296 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2297 | if (func_type_add_arg_types(*dest, argcount, |
| 2298 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2299 | for (i = 0; i < argcount; ++i) |
| 2300 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2301 | &(*dest)->tt_args[i], type_gap); |
| 2302 | } |
| 2303 | } |
| 2304 | else |
| 2305 | *dest = alloc_func_type(common, -1, type_gap); |
| 2306 | return; |
| 2307 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2308 | } |
| 2309 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2310 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | char * |
| 2314 | vartype_name(vartype_T type) |
| 2315 | { |
| 2316 | switch (type) |
| 2317 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2318 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2319 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2320 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2321 | case VAR_SPECIAL: return "special"; |
| 2322 | case VAR_BOOL: return "bool"; |
| 2323 | case VAR_NUMBER: return "number"; |
| 2324 | case VAR_FLOAT: return "float"; |
| 2325 | case VAR_STRING: return "string"; |
| 2326 | case VAR_BLOB: return "blob"; |
| 2327 | case VAR_JOB: return "job"; |
| 2328 | case VAR_CHANNEL: return "channel"; |
| 2329 | case VAR_LIST: return "list"; |
| 2330 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2331 | |
| 2332 | case VAR_FUNC: |
| 2333 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2334 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2335 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | /* |
| 2339 | * Return the name of a type. |
| 2340 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2341 | */ |
| 2342 | char * |
| 2343 | type_name(type_T *type, char **tofree) |
| 2344 | { |
| 2345 | char *name = vartype_name(type->tt_type); |
| 2346 | |
| 2347 | *tofree = NULL; |
| 2348 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2349 | { |
| 2350 | char *member_free; |
| 2351 | char *member_name = type_name(type->tt_member, &member_free); |
| 2352 | size_t len; |
| 2353 | |
| 2354 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2355 | *tofree = alloc(len); |
| 2356 | if (*tofree != NULL) |
| 2357 | { |
| 2358 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2359 | vim_free(member_free); |
| 2360 | return *tofree; |
| 2361 | } |
| 2362 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2363 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2364 | { |
| 2365 | garray_T ga; |
| 2366 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2367 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2368 | |
| 2369 | ga_init2(&ga, 1, 100); |
| 2370 | if (ga_grow(&ga, 20) == FAIL) |
| 2371 | return "[unknown]"; |
| 2372 | *tofree = ga.ga_data; |
| 2373 | STRCPY(ga.ga_data, "func("); |
| 2374 | ga.ga_len += 5; |
| 2375 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2376 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2377 | { |
| 2378 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2379 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2380 | int len; |
| 2381 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2382 | if (type->tt_args == NULL) |
| 2383 | arg_type = "[unknown]"; |
| 2384 | else |
| 2385 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2386 | if (i > 0) |
| 2387 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2388 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2389 | ga.ga_len += 2; |
| 2390 | } |
| 2391 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2392 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2393 | { |
| 2394 | vim_free(arg_free); |
| 2395 | return "[unknown]"; |
| 2396 | } |
| 2397 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2398 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2399 | { |
| 2400 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2401 | ga.ga_len += 3; |
| 2402 | } |
| 2403 | else if (i >= type->tt_min_argcount) |
| 2404 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2405 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2406 | ga.ga_len += len; |
| 2407 | vim_free(arg_free); |
| 2408 | } |
| 2409 | |
| 2410 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2411 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2412 | else |
| 2413 | { |
| 2414 | char *ret_free; |
| 2415 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2416 | int len; |
| 2417 | |
| 2418 | len = (int)STRLEN(ret_name) + 4; |
| 2419 | if (ga_grow(&ga, len) == FAIL) |
| 2420 | { |
| 2421 | vim_free(ret_free); |
| 2422 | return "[unknown]"; |
| 2423 | } |
| 2424 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2425 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2426 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2427 | vim_free(ret_free); |
| 2428 | } |
| 2429 | return ga.ga_data; |
| 2430 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2431 | |
| 2432 | return name; |
| 2433 | } |
| 2434 | |
| 2435 | /* |
| 2436 | * Find "name" in script-local items of script "sid". |
| 2437 | * Returns the index in "sn_var_vals" if found. |
| 2438 | * If found but not in "sn_var_vals" returns -1. |
| 2439 | * If not found returns -2. |
| 2440 | */ |
| 2441 | int |
| 2442 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2443 | { |
| 2444 | hashtab_T *ht; |
| 2445 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2446 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2447 | int idx; |
| 2448 | |
| 2449 | // First look the name up in the hashtable. |
| 2450 | if (sid <= 0 || sid > script_items.ga_len) |
| 2451 | return -1; |
| 2452 | ht = &SCRIPT_VARS(sid); |
| 2453 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2454 | if (di == NULL) |
| 2455 | return -2; |
| 2456 | |
| 2457 | // Now find the svar_T index in sn_var_vals. |
| 2458 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2459 | { |
| 2460 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2461 | |
| 2462 | if (sv->sv_tv == &di->di_tv) |
| 2463 | { |
| 2464 | if (check_writable && sv->sv_const) |
| 2465 | semsg(_(e_readonlyvar), name); |
| 2466 | return idx; |
| 2467 | } |
| 2468 | } |
| 2469 | return -1; |
| 2470 | } |
| 2471 | |
| 2472 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2473 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2474 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2475 | */ |
| 2476 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2477 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2478 | { |
Bram Moolenaar | 086eb18 | 2020-07-01 16:00:44 +0200 | [diff] [blame] | 2479 | scriptitem_T *si; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2480 | int idx; |
| 2481 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2482 | if (current_sctx.sc_sid <= 0) |
| 2483 | return NULL; |
| 2484 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2485 | if (cctx != NULL) |
| 2486 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2487 | { |
| 2488 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2489 | + idx; |
| 2490 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2491 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2492 | : STRLEN(import->imp_name) == len |
| 2493 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2494 | return import; |
| 2495 | } |
| 2496 | |
| 2497 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2498 | { |
| 2499 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2500 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2501 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2502 | : STRLEN(import->imp_name) == len |
| 2503 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2504 | return import; |
| 2505 | } |
| 2506 | return NULL; |
| 2507 | } |
| 2508 | |
| 2509 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2510 | * Free all imported variables. |
| 2511 | */ |
| 2512 | static void |
| 2513 | free_imported(cctx_T *cctx) |
| 2514 | { |
| 2515 | int idx; |
| 2516 | |
| 2517 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2518 | { |
| 2519 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2520 | |
| 2521 | vim_free(import->imp_name); |
| 2522 | } |
| 2523 | ga_clear(&cctx->ctx_imports); |
| 2524 | } |
| 2525 | |
| 2526 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2527 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2528 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 2529 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2530 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2531 | { |
| 2532 | return p[0] == '#' && p[1] != '{'; |
| 2533 | } |
| 2534 | |
| 2535 | /* |
| 2536 | * Return a pointer to the next line that isn't empty or only contains a |
| 2537 | * comment. Skips over white space. |
| 2538 | * Returns NULL if there is none. |
| 2539 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2540 | char_u * |
| 2541 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2542 | { |
| 2543 | int lnum = cctx->ctx_lnum; |
| 2544 | |
| 2545 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2546 | { |
| 2547 | char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum]; |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 2548 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2549 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 2550 | if (line == NULL) |
| 2551 | break; |
| 2552 | p = skipwhite(line); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2553 | if (*p != NUL && !vim9_comment_start(p)) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2554 | return p; |
| 2555 | } |
| 2556 | return NULL; |
| 2557 | } |
| 2558 | |
| 2559 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2560 | * Called when checking for a following operator at "arg". When the rest of |
| 2561 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2562 | * line return a pointer to it and set "nextp". |
| 2563 | * Otherwise skip over white space. |
| 2564 | */ |
| 2565 | static char_u * |
| 2566 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2567 | { |
| 2568 | char_u *p = skipwhite(arg); |
| 2569 | |
| 2570 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2571 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2572 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2573 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2574 | if (*nextp != NULL) |
| 2575 | return *nextp; |
| 2576 | } |
| 2577 | return p; |
| 2578 | } |
| 2579 | |
| 2580 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2581 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2582 | * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE. |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2583 | * Returns NULL when at the end. |
| 2584 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2585 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2586 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2587 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2588 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2589 | |
| 2590 | do |
| 2591 | { |
| 2592 | ++cctx->ctx_lnum; |
| 2593 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2594 | { |
| 2595 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2596 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2597 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2598 | line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2599 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2600 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2601 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2602 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2603 | return line; |
| 2604 | } |
| 2605 | |
| 2606 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2607 | * If "*arg" is at the end of the line, advance to the next line. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2608 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2609 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2610 | */ |
| 2611 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2612 | may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2613 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2614 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2615 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2616 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2617 | |
| 2618 | if (next == NULL) |
| 2619 | return FAIL; |
| 2620 | *arg = skipwhite(next); |
| 2621 | } |
| 2622 | return OK; |
| 2623 | } |
| 2624 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2625 | /* |
| 2626 | * Idem, and give an error when failed. |
| 2627 | */ |
| 2628 | static int |
| 2629 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2630 | { |
| 2631 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2632 | { |
| 2633 | emsg(_("E1097: line incomplete")); |
| 2634 | return FAIL; |
| 2635 | } |
| 2636 | return OK; |
| 2637 | } |
| 2638 | |
| 2639 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2640 | // Structure passed between the compile_expr* functions to keep track of |
| 2641 | // constants that have been parsed but for which no code was produced yet. If |
| 2642 | // possible expressions on these constants are applied at compile time. If |
| 2643 | // that is not possible, the code to push the constants needs to be generated |
| 2644 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2645 | // Using 50 should be more than enough of 5 levels of (). |
| 2646 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2647 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2648 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2649 | int pp_used; // active entries in pp_tv[] |
| 2650 | } ppconst_T; |
| 2651 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2652 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2653 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2654 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2655 | /* |
| 2656 | * Generate a PUSH instruction for "tv". |
| 2657 | * "tv" will be consumed or cleared. |
| 2658 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2659 | */ |
| 2660 | static int |
| 2661 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2662 | { |
| 2663 | if (tv != NULL) |
| 2664 | { |
| 2665 | switch (tv->v_type) |
| 2666 | { |
| 2667 | case VAR_UNKNOWN: |
| 2668 | break; |
| 2669 | case VAR_BOOL: |
| 2670 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2671 | break; |
| 2672 | case VAR_SPECIAL: |
| 2673 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2674 | break; |
| 2675 | case VAR_NUMBER: |
| 2676 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2677 | break; |
| 2678 | #ifdef FEAT_FLOAT |
| 2679 | case VAR_FLOAT: |
| 2680 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2681 | break; |
| 2682 | #endif |
| 2683 | case VAR_BLOB: |
| 2684 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2685 | tv->vval.v_blob = NULL; |
| 2686 | break; |
| 2687 | case VAR_STRING: |
| 2688 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2689 | tv->vval.v_string = NULL; |
| 2690 | break; |
| 2691 | default: |
| 2692 | iemsg("constant type not supported"); |
| 2693 | clear_tv(tv); |
| 2694 | return FAIL; |
| 2695 | } |
| 2696 | tv->v_type = VAR_UNKNOWN; |
| 2697 | } |
| 2698 | return OK; |
| 2699 | } |
| 2700 | |
| 2701 | /* |
| 2702 | * Generate code for any ppconst entries. |
| 2703 | */ |
| 2704 | static int |
| 2705 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2706 | { |
| 2707 | int i; |
| 2708 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2709 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2710 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2711 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2712 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2713 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2714 | ret = FAIL; |
| 2715 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2716 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2717 | return ret; |
| 2718 | } |
| 2719 | |
| 2720 | /* |
| 2721 | * Clear ppconst constants. Used when failing. |
| 2722 | */ |
| 2723 | static void |
| 2724 | clear_ppconst(ppconst_T *ppconst) |
| 2725 | { |
| 2726 | int i; |
| 2727 | |
| 2728 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2729 | clear_tv(&ppconst->pp_tv[i]); |
| 2730 | ppconst->pp_used = 0; |
| 2731 | } |
| 2732 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2733 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2734 | * Generate an instruction to load script-local variable "name", without the |
| 2735 | * leading "s:". |
| 2736 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2737 | */ |
| 2738 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2739 | compile_load_scriptvar( |
| 2740 | cctx_T *cctx, |
| 2741 | char_u *name, // variable NUL terminated |
| 2742 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2743 | char_u **end, // end of variable |
| 2744 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2745 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2746 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2747 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2748 | imported_T *import; |
| 2749 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2750 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2751 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2752 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2753 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2754 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2755 | } |
| 2756 | if (idx >= 0) |
| 2757 | { |
| 2758 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2759 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2760 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2761 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2762 | return OK; |
| 2763 | } |
| 2764 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2765 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2766 | if (import != NULL) |
| 2767 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2768 | if (import->imp_all) |
| 2769 | { |
| 2770 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2771 | char_u *exp_name; |
| 2772 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2773 | ufunc_T *ufunc; |
| 2774 | type_T *type; |
| 2775 | |
| 2776 | // Used "import * as Name", need to lookup the member. |
| 2777 | if (*p != '.') |
| 2778 | { |
| 2779 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2780 | return FAIL; |
| 2781 | } |
| 2782 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2783 | if (VIM_ISWHITE(*p)) |
| 2784 | { |
| 2785 | emsg(_("E1074: no white space allowed after dot")); |
| 2786 | return FAIL; |
| 2787 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2788 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2789 | // isolate one name |
| 2790 | exp_name = p; |
| 2791 | while (eval_isnamec(*p)) |
| 2792 | ++p; |
| 2793 | cc = *p; |
| 2794 | *p = NUL; |
| 2795 | |
| 2796 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2797 | *p = cc; |
| 2798 | p = skipwhite(p); |
| 2799 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2800 | // TODO: what if it is a function? |
| 2801 | if (idx < 0) |
| 2802 | return FAIL; |
| 2803 | *end = p; |
| 2804 | |
| 2805 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2806 | import->imp_sid, |
| 2807 | idx, |
| 2808 | type); |
| 2809 | } |
| 2810 | else |
| 2811 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2812 | // TODO: check this is a variable, not a function? |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2813 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2814 | import->imp_sid, |
| 2815 | import->imp_var_vals_idx, |
| 2816 | import->imp_type); |
| 2817 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2818 | return OK; |
| 2819 | } |
| 2820 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2821 | if (error) |
| 2822 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2823 | return FAIL; |
| 2824 | } |
| 2825 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2826 | static int |
| 2827 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2828 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2829 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2830 | |
| 2831 | if (ufunc == NULL) |
| 2832 | return FAIL; |
| 2833 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2834 | // Need to compile any default values to get the argument types. |
| 2835 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2836 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2837 | return FAIL; |
Bram Moolenaar | fe465a0 | 2020-07-07 22:50:12 +0200 | [diff] [blame] | 2838 | return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name), |
| 2839 | ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2840 | } |
| 2841 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2842 | /* |
| 2843 | * Compile a variable name into a load instruction. |
| 2844 | * "end" points to just after the name. |
| 2845 | * When "error" is FALSE do not give an error when not found. |
| 2846 | */ |
| 2847 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2848 | 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] | 2849 | { |
| 2850 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2851 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2852 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2853 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2854 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2855 | |
| 2856 | if (*(*arg + 1) == ':') |
| 2857 | { |
| 2858 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2859 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2860 | { |
| 2861 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2862 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2863 | switch (**arg) |
| 2864 | { |
| 2865 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2866 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2867 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2868 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2869 | default: |
| 2870 | semsg(_(e_namespace), *arg); |
| 2871 | goto theend; |
| 2872 | } |
| 2873 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2874 | goto theend; |
| 2875 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2876 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2877 | else |
| 2878 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2879 | isntype_T isn_type = ISN_DROP; |
| 2880 | |
| 2881 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2882 | if (name == NULL) |
| 2883 | return FAIL; |
| 2884 | |
| 2885 | switch (**arg) |
| 2886 | { |
| 2887 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2888 | break; |
| 2889 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2890 | NULL, NULL, error); |
| 2891 | break; |
| 2892 | case 'g': isn_type = ISN_LOADG; break; |
| 2893 | case 'w': isn_type = ISN_LOADW; break; |
| 2894 | case 't': isn_type = ISN_LOADT; break; |
| 2895 | case 'b': isn_type = ISN_LOADB; break; |
| 2896 | default: semsg(_(e_namespace), *arg); |
| 2897 | goto theend; |
| 2898 | } |
| 2899 | if (isn_type != ISN_DROP) |
| 2900 | { |
| 2901 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2902 | // variables can be defined later, thus we don't check if it |
| 2903 | // exists, give error at runtime. |
| 2904 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2905 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2906 | } |
| 2907 | } |
| 2908 | else |
| 2909 | { |
| 2910 | size_t len = end - *arg; |
| 2911 | int idx; |
| 2912 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2913 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2914 | |
| 2915 | name = vim_strnsave(*arg, end - *arg); |
| 2916 | if (name == NULL) |
| 2917 | return FAIL; |
| 2918 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2919 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2920 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2921 | if (!gen_load_outer) |
| 2922 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2923 | } |
| 2924 | else |
| 2925 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2926 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2927 | |
| 2928 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2929 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2930 | type = lvar->lv_type; |
| 2931 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2932 | if (lvar->lv_from_outer) |
| 2933 | gen_load_outer = TRUE; |
| 2934 | else |
| 2935 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2936 | } |
| 2937 | else |
| 2938 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2939 | // "var" can be script-local even without using "s:" if it |
| 2940 | // already exists. |
| 2941 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2942 | == SCRIPT_VERSION_VIM9 |
| 2943 | || lookup_script(*arg, len) == OK) |
| 2944 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2945 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2946 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2947 | // When the name starts with an uppercase letter or "x:" it |
| 2948 | // can be a user defined function. |
| 2949 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2950 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2951 | } |
| 2952 | } |
| 2953 | if (gen_load) |
| 2954 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2955 | if (gen_load_outer) |
| 2956 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | *arg = end; |
| 2960 | |
| 2961 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2962 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2963 | semsg(_(e_var_notfound), name); |
| 2964 | vim_free(name); |
| 2965 | return res; |
| 2966 | } |
| 2967 | |
| 2968 | /* |
| 2969 | * Compile the argument expressions. |
| 2970 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2971 | */ |
| 2972 | static int |
| 2973 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2974 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2975 | char_u *p = *arg; |
| 2976 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2977 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2978 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2979 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2980 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2981 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2982 | if (*p == ')') |
| 2983 | { |
| 2984 | *arg = p + 1; |
| 2985 | return OK; |
| 2986 | } |
| 2987 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2988 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2989 | return FAIL; |
| 2990 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2991 | |
| 2992 | if (*p != ',' && *skipwhite(p) == ',') |
| 2993 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2994 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2995 | p = skipwhite(p); |
| 2996 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2997 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2998 | { |
| 2999 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 3000 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 3001 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3002 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3003 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 3004 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3005 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3006 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 3007 | emsg(_(e_missing_close)); |
| 3008 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | /* |
| 3012 | * Compile a function call: name(arg1, arg2) |
| 3013 | * "arg" points to "name", "arg + varlen" to the "(". |
| 3014 | * "argcount_init" is 1 for "value->method()" |
| 3015 | * Instructions: |
| 3016 | * EVAL arg1 |
| 3017 | * EVAL arg2 |
| 3018 | * BCALL / DCALL / UCALL |
| 3019 | */ |
| 3020 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3021 | compile_call( |
| 3022 | char_u **arg, |
| 3023 | size_t varlen, |
| 3024 | cctx_T *cctx, |
| 3025 | ppconst_T *ppconst, |
| 3026 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3027 | { |
| 3028 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 3029 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3030 | int argcount = argcount_init; |
| 3031 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3032 | char_u fname_buf[FLEN_FIXED + 1]; |
| 3033 | char_u *tofree = NULL; |
| 3034 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3035 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3036 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3037 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3038 | // we can evaluate "has('name')" at compile time |
| 3039 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 3040 | { |
| 3041 | char_u *s = skipwhite(*arg + varlen + 1); |
| 3042 | typval_T argvars[2]; |
| 3043 | |
| 3044 | argvars[0].v_type = VAR_UNKNOWN; |
| 3045 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3046 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3047 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3048 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3049 | s = skipwhite(s); |
| 3050 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 3051 | { |
| 3052 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 3053 | |
| 3054 | *arg = s + 1; |
| 3055 | argvars[1].v_type = VAR_UNKNOWN; |
| 3056 | tv->v_type = VAR_NUMBER; |
| 3057 | tv->vval.v_number = 0; |
| 3058 | f_has(argvars, tv); |
| 3059 | clear_tv(&argvars[0]); |
| 3060 | ++ppconst->pp_used; |
| 3061 | return OK; |
| 3062 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 3063 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3067 | return FAIL; |
| 3068 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3069 | if (varlen >= sizeof(namebuf)) |
| 3070 | { |
| 3071 | semsg(_("E1011: name too long: %s"), name); |
| 3072 | return FAIL; |
| 3073 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3074 | vim_strncpy(namebuf, *arg, varlen); |
| 3075 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3076 | |
| 3077 | *arg = skipwhite(*arg + varlen + 1); |
| 3078 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3079 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3080 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3081 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3082 | { |
| 3083 | int idx; |
| 3084 | |
| 3085 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3086 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3087 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 3088 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3089 | else |
| 3090 | semsg(_(e_unknownfunc), namebuf); |
| 3091 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3092 | } |
| 3093 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3094 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 3095 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3096 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3097 | { |
| 3098 | res = generate_CALL(cctx, ufunc, argcount); |
| 3099 | goto theend; |
| 3100 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3101 | |
| 3102 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3103 | // 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] | 3104 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 3105 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 3106 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3107 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3108 | garray_T *stack = &cctx->ctx_type_stack; |
| 3109 | type_T *type; |
| 3110 | |
| 3111 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3112 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3113 | goto theend; |
| 3114 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3115 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3116 | // 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] | 3117 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 3118 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 3119 | res = generate_UCALL(cctx, name, argcount); |
| 3120 | else |
| 3121 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 3122 | |
| 3123 | theend: |
| 3124 | vim_free(tofree); |
| 3125 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 3129 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 3130 | |
| 3131 | /* |
| 3132 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 3133 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3134 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3135 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 3136 | * valid name. |
| 3137 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3138 | static char_u * |
| 3139 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3140 | { |
| 3141 | char_u *p; |
| 3142 | |
| 3143 | // Quick check for valid starting character. |
| 3144 | if (!eval_isnamec1(*arg)) |
| 3145 | return arg; |
| 3146 | |
| 3147 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 3148 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 3149 | // and can be used in slice "[n:]". |
| 3150 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3151 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3152 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 3153 | break; |
| 3154 | return p; |
| 3155 | } |
| 3156 | |
| 3157 | /* |
| 3158 | * 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] | 3159 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3160 | */ |
| 3161 | char_u * |
| 3162 | to_name_const_end(char_u *arg) |
| 3163 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3164 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3165 | typval_T rettv; |
| 3166 | |
| 3167 | if (p == arg && *arg == '[') |
| 3168 | { |
| 3169 | |
| 3170 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3171 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3172 | p = arg; |
| 3173 | } |
| 3174 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 3175 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3176 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3177 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3178 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3179 | p = arg; |
| 3180 | } |
| 3181 | else if (p == arg && *arg == '{') |
| 3182 | { |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3183 | int ret = get_lambda_tv(&p, &rettv, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3184 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3185 | // Can be "{x -> ret}()". |
| 3186 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3187 | if (ret == NOTDONE) |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3188 | ret = eval_dict(&p, &rettv, NULL, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3189 | if (ret != OK) |
| 3190 | p = arg; |
| 3191 | } |
| 3192 | |
| 3193 | return p; |
| 3194 | } |
| 3195 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3196 | /* |
| 3197 | * parse a list: [expr, expr] |
| 3198 | * "*arg" points to the '['. |
| 3199 | */ |
| 3200 | static int |
| 3201 | compile_list(char_u **arg, cctx_T *cctx) |
| 3202 | { |
| 3203 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3204 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3205 | int count = 0; |
| 3206 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3207 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3208 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3209 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3210 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3211 | semsg(_(e_list_end), *arg); |
| 3212 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3213 | } |
| 3214 | if (*p == ']') |
| 3215 | { |
| 3216 | ++p; |
| 3217 | // Allow for following comment, after at least one space. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3218 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '#') |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3219 | p += STRLEN(p); |
| 3220 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3221 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3222 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3223 | break; |
| 3224 | ++count; |
| 3225 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3226 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3227 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3228 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 3229 | { |
| 3230 | semsg(_(e_white_after), ","); |
| 3231 | return FAIL; |
| 3232 | } |
| 3233 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3234 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3235 | p = skipwhite(p); |
| 3236 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3237 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3238 | |
| 3239 | generate_NEWLIST(cctx, count); |
| 3240 | return OK; |
| 3241 | } |
| 3242 | |
| 3243 | /* |
| 3244 | * parse a lambda: {arg, arg -> expr} |
| 3245 | * "*arg" points to the '{'. |
| 3246 | */ |
| 3247 | static int |
| 3248 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 3249 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3250 | typval_T rettv; |
| 3251 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3252 | evalarg_T evalarg; |
| 3253 | |
| 3254 | CLEAR_FIELD(evalarg); |
| 3255 | evalarg.eval_flags = EVAL_EVALUATE; |
| 3256 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3257 | |
| 3258 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3259 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3260 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3261 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3262 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3263 | ++ufunc->uf_refcount; |
| 3264 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3265 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3266 | |
| 3267 | // The function will have one line: "return {expr}". |
| 3268 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3269 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3270 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 3271 | clear_evalarg(&evalarg, NULL); |
| 3272 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 3273 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 3274 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3275 | |
| 3276 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3277 | return FAIL; |
| 3278 | } |
| 3279 | |
| 3280 | /* |
| 3281 | * Compile a lamda call: expr->{lambda}(args) |
| 3282 | * "arg" points to the "{". |
| 3283 | */ |
| 3284 | static int |
| 3285 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 3286 | { |
| 3287 | ufunc_T *ufunc; |
| 3288 | typval_T rettv; |
| 3289 | int argcount = 1; |
| 3290 | int ret = FAIL; |
| 3291 | |
| 3292 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3293 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3294 | return FAIL; |
| 3295 | |
| 3296 | if (**arg != '(') |
| 3297 | { |
| 3298 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 3299 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3300 | else |
| 3301 | semsg(_(e_missing_paren), "lambda"); |
| 3302 | clear_tv(&rettv); |
| 3303 | return FAIL; |
| 3304 | } |
| 3305 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3306 | ufunc = rettv.vval.v_partial->pt_func; |
| 3307 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3308 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3309 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3310 | |
| 3311 | // The function will have one line: "return {expr}". |
| 3312 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3313 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3314 | |
| 3315 | // compile the arguments |
| 3316 | *arg = skipwhite(*arg + 1); |
| 3317 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 3318 | // call the compiled function |
| 3319 | ret = generate_CALL(cctx, ufunc, argcount); |
| 3320 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3321 | if (ret == FAIL) |
| 3322 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3323 | return ret; |
| 3324 | } |
| 3325 | |
| 3326 | /* |
| 3327 | * parse a dict: {'key': val} or #{key: val} |
| 3328 | * "*arg" points to the '{'. |
| 3329 | */ |
| 3330 | static int |
| 3331 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 3332 | { |
| 3333 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3334 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3335 | int count = 0; |
| 3336 | dict_T *d = dict_alloc(); |
| 3337 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3338 | char_u *whitep = *arg; |
| 3339 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3340 | |
| 3341 | if (d == NULL) |
| 3342 | return FAIL; |
| 3343 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3344 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3345 | { |
| 3346 | char_u *key = NULL; |
| 3347 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3348 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3349 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3350 | *arg = NULL; |
| 3351 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | if (**arg == '}') |
| 3355 | break; |
| 3356 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3357 | if (literal) |
| 3358 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3359 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3360 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3361 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3362 | { |
| 3363 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 3364 | return FAIL; |
| 3365 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3366 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3367 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3368 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3369 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3370 | } |
| 3371 | else |
| 3372 | { |
| 3373 | isn_T *isn; |
| 3374 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3375 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3376 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3377 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3378 | if (isn->isn_type == ISN_PUSHS) |
| 3379 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3380 | else |
| 3381 | { |
| 3382 | type_T *keytype = ((type_T **)stack->ga_data) |
| 3383 | [stack->ga_len - 1]; |
| 3384 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 3385 | return FAIL; |
| 3386 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | // Check for duplicate keys, if using string keys. |
| 3390 | if (key != NULL) |
| 3391 | { |
| 3392 | item = dict_find(d, key, -1); |
| 3393 | if (item != NULL) |
| 3394 | { |
| 3395 | semsg(_(e_duplicate_key), key); |
| 3396 | goto failret; |
| 3397 | } |
| 3398 | item = dictitem_alloc(key); |
| 3399 | if (item != NULL) |
| 3400 | { |
| 3401 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3402 | item->di_tv.v_lock = 0; |
| 3403 | if (dict_add(d, item) == FAIL) |
| 3404 | dictitem_free(item); |
| 3405 | } |
| 3406 | } |
| 3407 | |
| 3408 | *arg = skipwhite(*arg); |
| 3409 | if (**arg != ':') |
| 3410 | { |
| 3411 | semsg(_(e_missing_dict_colon), *arg); |
| 3412 | return FAIL; |
| 3413 | } |
| 3414 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3415 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3416 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3417 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3418 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3419 | *arg = NULL; |
| 3420 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3421 | } |
| 3422 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3423 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3424 | return FAIL; |
| 3425 | ++count; |
| 3426 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3427 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3428 | *arg = skipwhite(*arg); |
| 3429 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3430 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3431 | *arg = NULL; |
| 3432 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3433 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3434 | if (**arg == '}') |
| 3435 | break; |
| 3436 | if (**arg != ',') |
| 3437 | { |
| 3438 | semsg(_(e_missing_dict_comma), *arg); |
| 3439 | goto failret; |
| 3440 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3441 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3442 | *arg = skipwhite(*arg + 1); |
| 3443 | } |
| 3444 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3445 | *arg = *arg + 1; |
| 3446 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3447 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3448 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3449 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3450 | *arg += STRLEN(*arg); |
| 3451 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3452 | dict_unref(d); |
| 3453 | return generate_NEWDICT(cctx, count); |
| 3454 | |
| 3455 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3456 | if (*arg == NULL) |
| 3457 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3458 | dict_unref(d); |
| 3459 | return FAIL; |
| 3460 | } |
| 3461 | |
| 3462 | /* |
| 3463 | * Compile "&option". |
| 3464 | */ |
| 3465 | static int |
| 3466 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3467 | { |
| 3468 | typval_T rettv; |
| 3469 | char_u *start = *arg; |
| 3470 | int ret; |
| 3471 | |
| 3472 | // parse the option and get the current value to get the type. |
| 3473 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3474 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3475 | if (ret == OK) |
| 3476 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3477 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3478 | char_u *name = vim_strnsave(start, *arg - start); |
| 3479 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3480 | |
| 3481 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3482 | vim_free(name); |
| 3483 | } |
| 3484 | clear_tv(&rettv); |
| 3485 | |
| 3486 | return ret; |
| 3487 | } |
| 3488 | |
| 3489 | /* |
| 3490 | * Compile "$VAR". |
| 3491 | */ |
| 3492 | static int |
| 3493 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3494 | { |
| 3495 | char_u *start = *arg; |
| 3496 | int len; |
| 3497 | int ret; |
| 3498 | char_u *name; |
| 3499 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3500 | ++*arg; |
| 3501 | len = get_env_len(arg); |
| 3502 | if (len == 0) |
| 3503 | { |
| 3504 | semsg(_(e_syntax_at), start - 1); |
| 3505 | return FAIL; |
| 3506 | } |
| 3507 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3508 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3509 | name = vim_strnsave(start, len + 1); |
| 3510 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3511 | vim_free(name); |
| 3512 | return ret; |
| 3513 | } |
| 3514 | |
| 3515 | /* |
| 3516 | * Compile "@r". |
| 3517 | */ |
| 3518 | static int |
| 3519 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3520 | { |
| 3521 | int ret; |
| 3522 | |
| 3523 | ++*arg; |
| 3524 | if (**arg == NUL) |
| 3525 | { |
| 3526 | semsg(_(e_syntax_at), *arg - 1); |
| 3527 | return FAIL; |
| 3528 | } |
| 3529 | if (!valid_yank_reg(**arg, TRUE)) |
| 3530 | { |
| 3531 | emsg_invreg(**arg); |
| 3532 | return FAIL; |
| 3533 | } |
| 3534 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3535 | ++*arg; |
| 3536 | return ret; |
| 3537 | } |
| 3538 | |
| 3539 | /* |
| 3540 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3541 | */ |
| 3542 | static int |
| 3543 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3544 | { |
| 3545 | char_u *p = end; |
| 3546 | |
| 3547 | // this works from end to start |
| 3548 | while (p > start) |
| 3549 | { |
| 3550 | --p; |
| 3551 | if (*p == '-' || *p == '+') |
| 3552 | { |
| 3553 | // only '-' has an effect, for '+' we only check the type |
| 3554 | #ifdef FEAT_FLOAT |
| 3555 | if (rettv->v_type == VAR_FLOAT) |
| 3556 | { |
| 3557 | if (*p == '-') |
| 3558 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3559 | } |
| 3560 | else |
| 3561 | #endif |
| 3562 | { |
| 3563 | varnumber_T val; |
| 3564 | int error = FALSE; |
| 3565 | |
| 3566 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3567 | // here |
| 3568 | if (check_not_string(rettv) == FAIL) |
| 3569 | return FAIL; |
| 3570 | val = tv_get_number_chk(rettv, &error); |
| 3571 | clear_tv(rettv); |
| 3572 | if (error) |
| 3573 | return FAIL; |
| 3574 | if (*p == '-') |
| 3575 | val = -val; |
| 3576 | rettv->v_type = VAR_NUMBER; |
| 3577 | rettv->vval.v_number = val; |
| 3578 | } |
| 3579 | } |
| 3580 | else |
| 3581 | { |
| 3582 | int v = tv2bool(rettv); |
| 3583 | |
| 3584 | // '!' is permissive in the type. |
| 3585 | clear_tv(rettv); |
| 3586 | rettv->v_type = VAR_BOOL; |
| 3587 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3588 | } |
| 3589 | } |
| 3590 | return OK; |
| 3591 | } |
| 3592 | |
| 3593 | /* |
| 3594 | * Recognize v: variables that are constants and set "rettv". |
| 3595 | */ |
| 3596 | static void |
| 3597 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3598 | { |
| 3599 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3600 | { |
| 3601 | rettv->v_type = VAR_BOOL; |
| 3602 | rettv->vval.v_number = VVAL_TRUE; |
| 3603 | *arg += 6; |
| 3604 | } |
| 3605 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3606 | { |
| 3607 | rettv->v_type = VAR_BOOL; |
| 3608 | rettv->vval.v_number = VVAL_FALSE; |
| 3609 | *arg += 7; |
| 3610 | } |
| 3611 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3612 | { |
| 3613 | rettv->v_type = VAR_SPECIAL; |
| 3614 | rettv->vval.v_number = VVAL_NULL; |
| 3615 | *arg += 6; |
| 3616 | } |
| 3617 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3618 | { |
| 3619 | rettv->v_type = VAR_SPECIAL; |
| 3620 | rettv->vval.v_number = VVAL_NONE; |
| 3621 | *arg += 6; |
| 3622 | } |
| 3623 | } |
| 3624 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3625 | static exptype_T |
| 3626 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3627 | { |
| 3628 | exptype_T type = EXPR_UNKNOWN; |
| 3629 | int i; |
| 3630 | |
| 3631 | switch (p[0]) |
| 3632 | { |
| 3633 | case '=': if (p[1] == '=') |
| 3634 | type = EXPR_EQUAL; |
| 3635 | else if (p[1] == '~') |
| 3636 | type = EXPR_MATCH; |
| 3637 | break; |
| 3638 | case '!': if (p[1] == '=') |
| 3639 | type = EXPR_NEQUAL; |
| 3640 | else if (p[1] == '~') |
| 3641 | type = EXPR_NOMATCH; |
| 3642 | break; |
| 3643 | case '>': if (p[1] != '=') |
| 3644 | { |
| 3645 | type = EXPR_GREATER; |
| 3646 | *len = 1; |
| 3647 | } |
| 3648 | else |
| 3649 | type = EXPR_GEQUAL; |
| 3650 | break; |
| 3651 | case '<': if (p[1] != '=') |
| 3652 | { |
| 3653 | type = EXPR_SMALLER; |
| 3654 | *len = 1; |
| 3655 | } |
| 3656 | else |
| 3657 | type = EXPR_SEQUAL; |
| 3658 | break; |
| 3659 | case 'i': if (p[1] == 's') |
| 3660 | { |
| 3661 | // "is" and "isnot"; but not a prefix of a name |
| 3662 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3663 | *len = 5; |
| 3664 | i = p[*len]; |
| 3665 | if (!isalnum(i) && i != '_') |
| 3666 | { |
| 3667 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3668 | *type_is = TRUE; |
| 3669 | } |
| 3670 | } |
| 3671 | break; |
| 3672 | } |
| 3673 | return type; |
| 3674 | } |
| 3675 | |
| 3676 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3677 | * Compile code to apply '-', '+' and '!'. |
| 3678 | */ |
| 3679 | static int |
| 3680 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3681 | { |
| 3682 | char_u *p = end; |
| 3683 | |
| 3684 | // this works from end to start |
| 3685 | while (p > start) |
| 3686 | { |
| 3687 | --p; |
| 3688 | if (*p == '-' || *p == '+') |
| 3689 | { |
| 3690 | int negate = *p == '-'; |
| 3691 | isn_T *isn; |
| 3692 | |
| 3693 | // TODO: check type |
| 3694 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3695 | { |
| 3696 | --p; |
| 3697 | if (*p == '-') |
| 3698 | negate = !negate; |
| 3699 | } |
| 3700 | // only '-' has an effect, for '+' we only check the type |
| 3701 | if (negate) |
| 3702 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3703 | else |
| 3704 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3705 | if (isn == NULL) |
| 3706 | return FAIL; |
| 3707 | } |
| 3708 | else |
| 3709 | { |
| 3710 | int invert = TRUE; |
| 3711 | |
| 3712 | while (p > start && p[-1] == '!') |
| 3713 | { |
| 3714 | --p; |
| 3715 | invert = !invert; |
| 3716 | } |
| 3717 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3718 | return FAIL; |
| 3719 | } |
| 3720 | } |
| 3721 | return OK; |
| 3722 | } |
| 3723 | |
| 3724 | /* |
| 3725 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3726 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3727 | */ |
| 3728 | static int |
| 3729 | compile_subscript( |
| 3730 | char_u **arg, |
| 3731 | cctx_T *cctx, |
| 3732 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3733 | char_u *end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3734 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3735 | { |
| 3736 | for (;;) |
| 3737 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3738 | char_u *p = skipwhite(*arg); |
| 3739 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3740 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3741 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3742 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3743 | |
| 3744 | // If a following line starts with "->{" or "->X" advance to that |
| 3745 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3746 | // Also if a following line starts with ".x". |
| 3747 | if (next != NULL && |
| 3748 | ((next[0] == '-' && next[1] == '>' |
| 3749 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
| 3750 | || (next[0] == '.' && ASCII_ISALPHA(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3751 | { |
| 3752 | next = next_line_from_context(cctx, TRUE); |
| 3753 | if (next == NULL) |
| 3754 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3755 | *arg = next; |
| 3756 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3757 | } |
| 3758 | } |
| 3759 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3760 | if (*p == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3761 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3762 | garray_T *stack = &cctx->ctx_type_stack; |
| 3763 | type_T *type; |
| 3764 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3765 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3766 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3767 | return FAIL; |
| 3768 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3769 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3770 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3771 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3772 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3773 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3774 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3775 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3776 | return FAIL; |
| 3777 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3778 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3779 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3780 | char_u *pstart = p; |
| 3781 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3782 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3783 | return FAIL; |
| 3784 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3785 | // something->method() |
| 3786 | // Apply the '!', '-' and '+' first: |
| 3787 | // -1.0->func() works like (-1.0)->func() |
| 3788 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3789 | return FAIL; |
| 3790 | *start_leader = end_leader; // don't apply again later |
| 3791 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3792 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3793 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3794 | // No line break supported right after "->". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3795 | if (**arg == '{') |
| 3796 | { |
| 3797 | // lambda call: list->{lambda} |
| 3798 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3799 | return FAIL; |
| 3800 | } |
| 3801 | else |
| 3802 | { |
| 3803 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3804 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3805 | if (!eval_isnamec1(*p)) |
| 3806 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3807 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3808 | return FAIL; |
| 3809 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3810 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3811 | p += 2; |
| 3812 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3813 | ; |
| 3814 | if (*p != '(') |
| 3815 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3816 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3817 | return FAIL; |
| 3818 | } |
| 3819 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3820 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3821 | return FAIL; |
| 3822 | } |
| 3823 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3824 | else if (*p == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3825 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3826 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3827 | type_T **typep; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3828 | vartype_T vtype; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3829 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3830 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3831 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3832 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3833 | // TODO: blob index |
| 3834 | // TODO: more arguments |
| 3835 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3836 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3837 | return FAIL; |
| 3838 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3839 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3840 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3841 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3842 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3843 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3844 | return FAIL; |
| 3845 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3846 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3847 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3848 | if (**arg != ']') |
| 3849 | { |
| 3850 | emsg(_(e_missbrac)); |
| 3851 | return FAIL; |
| 3852 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3853 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3854 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3855 | // We can index a list and a dict. If we don't know the type |
| 3856 | // we can use the index value type. |
| 3857 | // TODO: If we don't know use an instruction to figure it out at |
| 3858 | // runtime. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3859 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3860 | vtype = (*typep)->tt_type; |
| 3861 | if (*typep == &t_any) |
| 3862 | { |
| 3863 | type_T *valtype = ((type_T **)stack->ga_data) |
| 3864 | [stack->ga_len - 1]; |
| 3865 | if (valtype == &t_string) |
| 3866 | vtype = VAR_DICT; |
| 3867 | } |
| 3868 | if (vtype == VAR_DICT) |
| 3869 | { |
| 3870 | if ((*typep)->tt_type == VAR_DICT) |
| 3871 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3872 | else |
| 3873 | { |
| 3874 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3875 | return FAIL; |
| 3876 | *typep = &t_any; |
| 3877 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3878 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3879 | return FAIL; |
| 3880 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3881 | return FAIL; |
| 3882 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3883 | else if (vtype == VAR_STRING) |
| 3884 | { |
| 3885 | *typep = &t_number; |
| 3886 | if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL) |
| 3887 | return FAIL; |
| 3888 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3889 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3890 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3891 | if ((*typep)->tt_type == VAR_LIST) |
| 3892 | *typep = (*typep)->tt_member; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3893 | if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3894 | return FAIL; |
| 3895 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3896 | else |
| 3897 | { |
| 3898 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3899 | return FAIL; |
| 3900 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3901 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3902 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3903 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3904 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3905 | return FAIL; |
| 3906 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3907 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3908 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3909 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3910 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3911 | p = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3912 | if (eval_isnamec1(*p)) |
| 3913 | while (eval_isnamec(*p)) |
| 3914 | MB_PTR_ADV(p); |
| 3915 | if (p == *arg) |
| 3916 | { |
| 3917 | semsg(_(e_syntax_at), *arg); |
| 3918 | return FAIL; |
| 3919 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3920 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3921 | return FAIL; |
| 3922 | *arg = p; |
| 3923 | } |
| 3924 | else |
| 3925 | break; |
| 3926 | } |
| 3927 | |
| 3928 | // TODO - see handle_subscript(): |
| 3929 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3930 | // Don't do this when "Func" is already a partial that was bound |
| 3931 | // explicitly (pt_auto is FALSE). |
| 3932 | |
| 3933 | return OK; |
| 3934 | } |
| 3935 | |
| 3936 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3937 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3938 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3939 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3940 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3941 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3942 | * |
| 3943 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3944 | */ |
| 3945 | |
| 3946 | /* |
| 3947 | * number number constant |
| 3948 | * 0zFFFFFFFF Blob constant |
| 3949 | * "string" string constant |
| 3950 | * 'string' literal string constant |
| 3951 | * &option-name option value |
| 3952 | * @r register contents |
| 3953 | * identifier variable value |
| 3954 | * function() function call |
| 3955 | * $VAR environment variable |
| 3956 | * (expression) nested expression |
| 3957 | * [expr, expr] List |
| 3958 | * {key: val, key: val} Dictionary |
| 3959 | * #{key: val, key: val} Dictionary with literal keys |
| 3960 | * |
| 3961 | * Also handle: |
| 3962 | * ! in front logical NOT |
| 3963 | * - in front unary minus |
| 3964 | * + in front unary plus (ignored) |
| 3965 | * trailing (arg) funcref/partial call |
| 3966 | * trailing [] subscript in String or List |
| 3967 | * trailing .name entry in Dictionary |
| 3968 | * trailing ->name() method call |
| 3969 | */ |
| 3970 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3971 | compile_expr7( |
| 3972 | char_u **arg, |
| 3973 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3974 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3975 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3976 | char_u *start_leader, *end_leader; |
| 3977 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3978 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3979 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3980 | |
| 3981 | /* |
| 3982 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3983 | */ |
| 3984 | start_leader = *arg; |
| 3985 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3986 | *arg = skipwhite(*arg + 1); |
| 3987 | end_leader = *arg; |
| 3988 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3989 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3990 | switch (**arg) |
| 3991 | { |
| 3992 | /* |
| 3993 | * Number constant. |
| 3994 | */ |
| 3995 | case '0': // also for blob starting with 0z |
| 3996 | case '1': |
| 3997 | case '2': |
| 3998 | case '3': |
| 3999 | case '4': |
| 4000 | case '5': |
| 4001 | case '6': |
| 4002 | case '7': |
| 4003 | case '8': |
| 4004 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4005 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4006 | return FAIL; |
| 4007 | break; |
| 4008 | |
| 4009 | /* |
| 4010 | * String constant: "string". |
| 4011 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4012 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4013 | return FAIL; |
| 4014 | break; |
| 4015 | |
| 4016 | /* |
| 4017 | * Literal string constant: 'str''ing'. |
| 4018 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4019 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4020 | return FAIL; |
| 4021 | break; |
| 4022 | |
| 4023 | /* |
| 4024 | * Constant Vim variable. |
| 4025 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4026 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4027 | ret = NOTDONE; |
| 4028 | break; |
| 4029 | |
| 4030 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4031 | * "true" constant |
| 4032 | */ |
| 4033 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 4034 | && !eval_isnamec((*arg)[4])) |
| 4035 | { |
| 4036 | *arg += 4; |
| 4037 | rettv->v_type = VAR_BOOL; |
| 4038 | rettv->vval.v_number = VVAL_TRUE; |
| 4039 | } |
| 4040 | else |
| 4041 | ret = NOTDONE; |
| 4042 | break; |
| 4043 | |
| 4044 | /* |
| 4045 | * "false" constant |
| 4046 | */ |
| 4047 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 4048 | && !eval_isnamec((*arg)[5])) |
| 4049 | { |
| 4050 | *arg += 5; |
| 4051 | rettv->v_type = VAR_BOOL; |
| 4052 | rettv->vval.v_number = VVAL_FALSE; |
| 4053 | } |
| 4054 | else |
| 4055 | ret = NOTDONE; |
| 4056 | break; |
| 4057 | |
| 4058 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4059 | * List: [expr, expr] |
| 4060 | */ |
| 4061 | case '[': ret = compile_list(arg, cctx); |
| 4062 | break; |
| 4063 | |
| 4064 | /* |
| 4065 | * Dictionary: #{key: val, key: val} |
| 4066 | */ |
| 4067 | case '#': if ((*arg)[1] == '{') |
| 4068 | { |
| 4069 | ++*arg; |
| 4070 | ret = compile_dict(arg, cctx, TRUE); |
| 4071 | } |
| 4072 | else |
| 4073 | ret = NOTDONE; |
| 4074 | break; |
| 4075 | |
| 4076 | /* |
| 4077 | * Lambda: {arg, arg -> expr} |
| 4078 | * Dictionary: {'key': val, 'key': val} |
| 4079 | */ |
| 4080 | case '{': { |
| 4081 | char_u *start = skipwhite(*arg + 1); |
| 4082 | |
| 4083 | // Find out what comes after the arguments. |
| 4084 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 4085 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4086 | if (ret != FAIL && *start == '>') |
| 4087 | ret = compile_lambda(arg, cctx); |
| 4088 | else |
| 4089 | ret = compile_dict(arg, cctx, FALSE); |
| 4090 | } |
| 4091 | break; |
| 4092 | |
| 4093 | /* |
| 4094 | * Option value: &name |
| 4095 | */ |
| 4096 | case '&': ret = compile_get_option(arg, cctx); |
| 4097 | break; |
| 4098 | |
| 4099 | /* |
| 4100 | * Environment variable: $VAR. |
| 4101 | */ |
| 4102 | case '$': ret = compile_get_env(arg, cctx); |
| 4103 | break; |
| 4104 | |
| 4105 | /* |
| 4106 | * Register contents: @r. |
| 4107 | */ |
| 4108 | case '@': ret = compile_get_register(arg, cctx); |
| 4109 | break; |
| 4110 | /* |
| 4111 | * nested expression: (expression). |
| 4112 | */ |
| 4113 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4114 | |
| 4115 | // recursive! |
| 4116 | if (ppconst->pp_used <= PPSIZE - 10) |
| 4117 | { |
| 4118 | ret = compile_expr1(arg, cctx, ppconst); |
| 4119 | } |
| 4120 | else |
| 4121 | { |
| 4122 | // Not enough space in ppconst, flush constants. |
| 4123 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4124 | return FAIL; |
| 4125 | ret = compile_expr0(arg, cctx); |
| 4126 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4127 | *arg = skipwhite(*arg); |
| 4128 | if (**arg == ')') |
| 4129 | ++*arg; |
| 4130 | else if (ret == OK) |
| 4131 | { |
| 4132 | emsg(_(e_missing_close)); |
| 4133 | ret = FAIL; |
| 4134 | } |
| 4135 | break; |
| 4136 | |
| 4137 | default: ret = NOTDONE; |
| 4138 | break; |
| 4139 | } |
| 4140 | if (ret == FAIL) |
| 4141 | return FAIL; |
| 4142 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4143 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4144 | { |
| 4145 | // apply the '!', '-' and '+' before the constant |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4146 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4147 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4148 | clear_tv(rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4149 | return FAIL; |
| 4150 | } |
| 4151 | start_leader = end_leader; // don't apply again below |
| 4152 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4153 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4154 | clear_tv(rettv); |
| 4155 | else |
| 4156 | // A constant expression can possibly be handled compile time, |
| 4157 | // return the value instead of generating code. |
| 4158 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4159 | } |
| 4160 | else if (ret == NOTDONE) |
| 4161 | { |
| 4162 | char_u *p; |
| 4163 | int r; |
| 4164 | |
| 4165 | if (!eval_isnamec1(**arg)) |
| 4166 | { |
| 4167 | semsg(_("E1015: Name expected: %s"), *arg); |
| 4168 | return FAIL; |
| 4169 | } |
| 4170 | |
| 4171 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4172 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4173 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4174 | { |
| 4175 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 4176 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4177 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4178 | { |
| 4179 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4180 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4181 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4182 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4183 | if (r == FAIL) |
| 4184 | return FAIL; |
| 4185 | } |
| 4186 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4187 | // Handle following "[]", ".member", etc. |
| 4188 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4189 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4190 | ppconst) == FAIL) |
| 4191 | return FAIL; |
| 4192 | if (ppconst->pp_used > 0) |
| 4193 | { |
| 4194 | // apply the '!', '-' and '+' before the constant |
| 4195 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4196 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
| 4197 | return FAIL; |
| 4198 | return OK; |
| 4199 | } |
| 4200 | if (compile_leader(cctx, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4201 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4202 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4203 | } |
| 4204 | |
| 4205 | /* |
| 4206 | * * number multiplication |
| 4207 | * / number division |
| 4208 | * % number modulo |
| 4209 | */ |
| 4210 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4211 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4212 | { |
| 4213 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4214 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4215 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4216 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4217 | // get the first expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4218 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4219 | return FAIL; |
| 4220 | |
| 4221 | /* |
| 4222 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4223 | */ |
| 4224 | for (;;) |
| 4225 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4226 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4227 | if (*op != '*' && *op != '/' && *op != '%') |
| 4228 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4229 | if (next != NULL) |
| 4230 | { |
| 4231 | *arg = next_line_from_context(cctx, TRUE); |
| 4232 | op = skipwhite(*arg); |
| 4233 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4234 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4235 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4236 | { |
| 4237 | char_u buf[3]; |
| 4238 | |
| 4239 | vim_strncpy(buf, op, 1); |
| 4240 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4241 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4242 | } |
| 4243 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4244 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4245 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4246 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4247 | // get the second expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4248 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4249 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4250 | |
| 4251 | if (ppconst->pp_used == ppconst_used + 2 |
| 4252 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4253 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4254 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4255 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4256 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4257 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4258 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4259 | // both are numbers: compute the result |
| 4260 | switch (*op) |
| 4261 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4262 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4263 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4264 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4265 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4266 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4267 | break; |
| 4268 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4269 | tv1->vval.v_number = res; |
| 4270 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4271 | } |
| 4272 | else |
| 4273 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4274 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4275 | generate_two_op(cctx, op); |
| 4276 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4277 | } |
| 4278 | |
| 4279 | return OK; |
| 4280 | } |
| 4281 | |
| 4282 | /* |
| 4283 | * + number addition |
| 4284 | * - number subtraction |
| 4285 | * .. string concatenation |
| 4286 | */ |
| 4287 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4288 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4289 | { |
| 4290 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4291 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4292 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4293 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4294 | |
| 4295 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4296 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4297 | return FAIL; |
| 4298 | |
| 4299 | /* |
| 4300 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4301 | */ |
| 4302 | for (;;) |
| 4303 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4304 | op = may_peek_next_line(cctx, *arg, &next); |
| 4305 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4306 | break; |
| 4307 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4308 | if (next != NULL) |
| 4309 | { |
| 4310 | *arg = next_line_from_context(cctx, TRUE); |
| 4311 | op = skipwhite(*arg); |
| 4312 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4313 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4314 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4315 | { |
| 4316 | char_u buf[3]; |
| 4317 | |
| 4318 | vim_strncpy(buf, op, oplen); |
| 4319 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4320 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4324 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4325 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4326 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4327 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4328 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4329 | return FAIL; |
| 4330 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4331 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4332 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4333 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4334 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4335 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4336 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4337 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4338 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4339 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4340 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4341 | // concat/subtract/add constant numbers |
| 4342 | if (*op == '+') |
| 4343 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4344 | else if (*op == '-') |
| 4345 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4346 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4347 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4348 | // concatenate constant strings |
| 4349 | char_u *s1 = tv1->vval.v_string; |
| 4350 | char_u *s2 = tv2->vval.v_string; |
| 4351 | size_t len1 = STRLEN(s1); |
| 4352 | |
| 4353 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4354 | if (tv1->vval.v_string == NULL) |
| 4355 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4356 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4357 | return FAIL; |
| 4358 | } |
| 4359 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4360 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4361 | vim_free(s1); |
| 4362 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4363 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4364 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4365 | } |
| 4366 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4367 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4368 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4369 | if (*op == '.') |
| 4370 | { |
| 4371 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4372 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4373 | return FAIL; |
| 4374 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4375 | } |
| 4376 | else |
| 4377 | generate_two_op(cctx, op); |
| 4378 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4379 | } |
| 4380 | |
| 4381 | return OK; |
| 4382 | } |
| 4383 | |
| 4384 | /* |
| 4385 | * expr5a == expr5b |
| 4386 | * expr5a =~ expr5b |
| 4387 | * expr5a != expr5b |
| 4388 | * expr5a !~ expr5b |
| 4389 | * expr5a > expr5b |
| 4390 | * expr5a >= expr5b |
| 4391 | * expr5a < expr5b |
| 4392 | * expr5a <= expr5b |
| 4393 | * expr5a is expr5b |
| 4394 | * expr5a isnot expr5b |
| 4395 | * |
| 4396 | * Produces instructions: |
| 4397 | * EVAL expr5a Push result of "expr5a" |
| 4398 | * EVAL expr5b Push result of "expr5b" |
| 4399 | * COMPARE one of the compare instructions |
| 4400 | */ |
| 4401 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4402 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4403 | { |
| 4404 | exptype_T type = EXPR_UNKNOWN; |
| 4405 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4406 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4407 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4408 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4409 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4410 | |
| 4411 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4412 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4413 | return FAIL; |
| 4414 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4415 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4416 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4417 | |
| 4418 | /* |
| 4419 | * If there is a comparative operator, use it. |
| 4420 | */ |
| 4421 | if (type != EXPR_UNKNOWN) |
| 4422 | { |
| 4423 | int ic = FALSE; // Default: do not ignore case |
| 4424 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4425 | if (next != NULL) |
| 4426 | { |
| 4427 | *arg = next_line_from_context(cctx, TRUE); |
| 4428 | p = skipwhite(*arg); |
| 4429 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4430 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4431 | { |
| 4432 | semsg(_(e_invexpr2), *arg); |
| 4433 | return FAIL; |
| 4434 | } |
| 4435 | // extra question mark appended: ignore case |
| 4436 | if (p[len] == '?') |
| 4437 | { |
| 4438 | ic = TRUE; |
| 4439 | ++len; |
| 4440 | } |
| 4441 | // extra '#' appended: match case (ignored) |
| 4442 | else if (p[len] == '#') |
| 4443 | ++len; |
| 4444 | // nothing appended: match case |
| 4445 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4446 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4447 | { |
| 4448 | char_u buf[7]; |
| 4449 | |
| 4450 | vim_strncpy(buf, p, len); |
| 4451 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4452 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4453 | } |
| 4454 | |
| 4455 | // get the second variable |
| 4456 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4457 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4458 | return FAIL; |
| 4459 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4460 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4461 | return FAIL; |
| 4462 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4463 | if (ppconst->pp_used == ppconst_used + 2) |
| 4464 | { |
| 4465 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4466 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4467 | int ret; |
| 4468 | |
| 4469 | // Both sides are a constant, compute the result now. |
| 4470 | // First check for a valid combination of types, this is more |
| 4471 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4472 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4473 | ret = FAIL; |
| 4474 | else |
| 4475 | { |
| 4476 | ret = typval_compare(tv1, tv2, type, ic); |
| 4477 | tv1->v_type = VAR_BOOL; |
| 4478 | tv1->vval.v_number = tv1->vval.v_number |
| 4479 | ? VVAL_TRUE : VVAL_FALSE; |
| 4480 | clear_tv(tv2); |
| 4481 | --ppconst->pp_used; |
| 4482 | } |
| 4483 | return ret; |
| 4484 | } |
| 4485 | |
| 4486 | generate_ppconst(cctx, ppconst); |
| 4487 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4488 | } |
| 4489 | |
| 4490 | return OK; |
| 4491 | } |
| 4492 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4493 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4494 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4495 | /* |
| 4496 | * Compile || or &&. |
| 4497 | */ |
| 4498 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4499 | compile_and_or( |
| 4500 | char_u **arg, |
| 4501 | cctx_T *cctx, |
| 4502 | char *op, |
| 4503 | ppconst_T *ppconst, |
| 4504 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4505 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4506 | char_u *next; |
| 4507 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4508 | int opchar = *op; |
| 4509 | |
| 4510 | if (p[0] == opchar && p[1] == opchar) |
| 4511 | { |
| 4512 | garray_T *instr = &cctx->ctx_instr; |
| 4513 | garray_T end_ga; |
| 4514 | |
| 4515 | /* |
| 4516 | * Repeat until there is no following "||" or "&&" |
| 4517 | */ |
| 4518 | ga_init2(&end_ga, sizeof(int), 10); |
| 4519 | while (p[0] == opchar && p[1] == opchar) |
| 4520 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4521 | if (next != NULL) |
| 4522 | { |
| 4523 | *arg = next_line_from_context(cctx, TRUE); |
| 4524 | p = skipwhite(*arg); |
| 4525 | } |
| 4526 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4527 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4528 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4529 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4530 | return FAIL; |
| 4531 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4532 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4533 | // TODO: use ppconst if the value is a constant |
| 4534 | generate_ppconst(cctx, ppconst); |
| 4535 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4536 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4537 | { |
| 4538 | ga_clear(&end_ga); |
| 4539 | return FAIL; |
| 4540 | } |
| 4541 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4542 | ++end_ga.ga_len; |
| 4543 | generate_JUMP(cctx, opchar == '|' |
| 4544 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 4545 | |
| 4546 | // eval the next expression |
| 4547 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4548 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4549 | return FAIL; |
| 4550 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4551 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4552 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4553 | { |
| 4554 | ga_clear(&end_ga); |
| 4555 | return FAIL; |
| 4556 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4557 | |
| 4558 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4559 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4560 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4561 | |
| 4562 | // Fill in the end label in all jumps. |
| 4563 | while (end_ga.ga_len > 0) |
| 4564 | { |
| 4565 | isn_T *isn; |
| 4566 | |
| 4567 | --end_ga.ga_len; |
| 4568 | isn = ((isn_T *)instr->ga_data) |
| 4569 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4570 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4571 | } |
| 4572 | ga_clear(&end_ga); |
| 4573 | } |
| 4574 | |
| 4575 | return OK; |
| 4576 | } |
| 4577 | |
| 4578 | /* |
| 4579 | * expr4a && expr4a && expr4a logical AND |
| 4580 | * |
| 4581 | * Produces instructions: |
| 4582 | * EVAL expr4a Push result of "expr4a" |
| 4583 | * JUMP_AND_KEEP_IF_FALSE end |
| 4584 | * EVAL expr4b Push result of "expr4b" |
| 4585 | * JUMP_AND_KEEP_IF_FALSE end |
| 4586 | * EVAL expr4c Push result of "expr4c" |
| 4587 | * end: |
| 4588 | */ |
| 4589 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4590 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4591 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4592 | int ppconst_used = ppconst->pp_used; |
| 4593 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4594 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4595 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4596 | return FAIL; |
| 4597 | |
| 4598 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4599 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4600 | } |
| 4601 | |
| 4602 | /* |
| 4603 | * expr3a || expr3b || expr3c logical OR |
| 4604 | * |
| 4605 | * Produces instructions: |
| 4606 | * EVAL expr3a Push result of "expr3a" |
| 4607 | * JUMP_AND_KEEP_IF_TRUE end |
| 4608 | * EVAL expr3b Push result of "expr3b" |
| 4609 | * JUMP_AND_KEEP_IF_TRUE end |
| 4610 | * EVAL expr3c Push result of "expr3c" |
| 4611 | * end: |
| 4612 | */ |
| 4613 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4614 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4615 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4616 | int ppconst_used = ppconst->pp_used; |
| 4617 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4618 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4619 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4620 | return FAIL; |
| 4621 | |
| 4622 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4623 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | /* |
| 4627 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4628 | * |
| 4629 | * Produces instructions: |
| 4630 | * EVAL expr2 Push result of "expr" |
| 4631 | * JUMP_IF_FALSE alt jump if false |
| 4632 | * EVAL expr1a |
| 4633 | * JUMP_ALWAYS end |
| 4634 | * alt: EVAL expr1b |
| 4635 | * end: |
| 4636 | */ |
| 4637 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4638 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4639 | { |
| 4640 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4641 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4642 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4643 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4644 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4645 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4646 | return FAIL; |
| 4647 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4648 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4649 | if (*p == '?') |
| 4650 | { |
| 4651 | garray_T *instr = &cctx->ctx_instr; |
| 4652 | garray_T *stack = &cctx->ctx_type_stack; |
| 4653 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4654 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4655 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4656 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4657 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4658 | int has_const_expr = FALSE; |
| 4659 | int const_value = FALSE; |
| 4660 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4661 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4662 | if (next != NULL) |
| 4663 | { |
| 4664 | *arg = next_line_from_context(cctx, TRUE); |
| 4665 | p = skipwhite(*arg); |
| 4666 | } |
| 4667 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4668 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4669 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4670 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4671 | return FAIL; |
| 4672 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4673 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4674 | if (ppconst->pp_used == ppconst_used + 1) |
| 4675 | { |
| 4676 | // the condition is a constant, we know whether the ? or the : |
| 4677 | // expression is to be evaluated. |
| 4678 | has_const_expr = TRUE; |
| 4679 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4680 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4681 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4682 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4683 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4684 | } |
| 4685 | else |
| 4686 | { |
| 4687 | generate_ppconst(cctx, ppconst); |
| 4688 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4689 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4690 | |
| 4691 | // evaluate the second expression; any type is accepted |
| 4692 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4693 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4694 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4695 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4696 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4697 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4698 | if (!has_const_expr) |
| 4699 | { |
| 4700 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4701 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4702 | // remember the type and drop it |
| 4703 | --stack->ga_len; |
| 4704 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4705 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4706 | end_idx = instr->ga_len; |
| 4707 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4708 | |
| 4709 | // jump here from JUMP_IF_FALSE |
| 4710 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4711 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4712 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4713 | |
| 4714 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4715 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4716 | if (*p != ':') |
| 4717 | { |
| 4718 | emsg(_(e_missing_colon)); |
| 4719 | return FAIL; |
| 4720 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4721 | if (next != NULL) |
| 4722 | { |
| 4723 | *arg = next_line_from_context(cctx, TRUE); |
| 4724 | p = skipwhite(*arg); |
| 4725 | } |
| 4726 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4727 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4728 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4729 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4730 | return FAIL; |
| 4731 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4732 | |
| 4733 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4734 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4735 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4736 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4737 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4738 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4739 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4740 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4741 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4742 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4743 | if (!has_const_expr) |
| 4744 | { |
| 4745 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4746 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4747 | // If the types differ, the result has a more generic type. |
| 4748 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4749 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4750 | |
| 4751 | // jump here from JUMP_ALWAYS |
| 4752 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4753 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4754 | } |
| 4755 | |
| 4756 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4757 | } |
| 4758 | return OK; |
| 4759 | } |
| 4760 | |
| 4761 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4762 | * Toplevel expression. |
| 4763 | */ |
| 4764 | static int |
| 4765 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4766 | { |
| 4767 | ppconst_T ppconst; |
| 4768 | |
| 4769 | CLEAR_FIELD(ppconst); |
| 4770 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4771 | { |
| 4772 | clear_ppconst(&ppconst); |
| 4773 | return FAIL; |
| 4774 | } |
| 4775 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4776 | return FAIL; |
| 4777 | return OK; |
| 4778 | } |
| 4779 | |
| 4780 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4781 | * compile "return [expr]" |
| 4782 | */ |
| 4783 | static char_u * |
| 4784 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4785 | { |
| 4786 | char_u *p = arg; |
| 4787 | garray_T *stack = &cctx->ctx_type_stack; |
| 4788 | type_T *stack_type; |
| 4789 | |
| 4790 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4791 | { |
| 4792 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4793 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4794 | return NULL; |
| 4795 | |
| 4796 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4797 | if (set_return_type) |
| 4798 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4799 | else |
| 4800 | { |
| 4801 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4802 | && stack_type->tt_type != VAR_VOID |
| 4803 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4804 | { |
| 4805 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4806 | return NULL; |
| 4807 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4808 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4809 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4810 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4811 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4812 | } |
| 4813 | else |
| 4814 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4815 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4816 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4817 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4818 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4819 | { |
| 4820 | emsg(_("E1003: Missing return value")); |
| 4821 | return NULL; |
| 4822 | } |
| 4823 | |
| 4824 | // No argument, return zero. |
| 4825 | generate_PUSHNR(cctx, 0); |
| 4826 | } |
| 4827 | |
| 4828 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4829 | return NULL; |
| 4830 | |
| 4831 | // "return val | endif" is possible |
| 4832 | return skipwhite(p); |
| 4833 | } |
| 4834 | |
| 4835 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4836 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4837 | * Return a pointer to the line in allocated memory. |
| 4838 | * Return NULL for end-of-file or some error. |
| 4839 | */ |
| 4840 | static char_u * |
| 4841 | exarg_getline( |
| 4842 | int c UNUSED, |
| 4843 | void *cookie, |
| 4844 | int indent UNUSED, |
| 4845 | int do_concat UNUSED) |
| 4846 | { |
| 4847 | cctx_T *cctx = (cctx_T *)cookie; |
| 4848 | |
| 4849 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4850 | { |
| 4851 | iemsg("Heredoc got to end"); |
| 4852 | return NULL; |
| 4853 | } |
| 4854 | ++cctx->ctx_lnum; |
| 4855 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4856 | [cctx->ctx_lnum]); |
| 4857 | } |
| 4858 | |
| 4859 | /* |
| 4860 | * Compile a nested :def command. |
| 4861 | */ |
| 4862 | static char_u * |
| 4863 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4864 | { |
| 4865 | char_u *name_start = eap->arg; |
| 4866 | char_u *name_end = to_name_end(eap->arg, FALSE); |
| 4867 | char_u *name = get_lambda_name(); |
| 4868 | lvar_T *lvar; |
| 4869 | ufunc_T *ufunc; |
| 4870 | |
| 4871 | eap->arg = name_end; |
| 4872 | eap->getline = exarg_getline; |
| 4873 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4874 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4875 | eap->forceit = FALSE; |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4876 | ufunc = def_function(eap, name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4877 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4878 | if (ufunc == NULL) |
| 4879 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4880 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4881 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4882 | return NULL; |
| 4883 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4884 | // Define a local variable for the function reference. |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4885 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4886 | TRUE, ufunc->uf_func_type); |
| 4887 | |
| 4888 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL |
| 4889 | || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL) |
| 4890 | return NULL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4891 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4892 | // TODO: warning for trailing text? |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4893 | return (char_u *)""; |
| 4894 | } |
| 4895 | |
| 4896 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4897 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4898 | */ |
| 4899 | int |
| 4900 | assignment_len(char_u *p, int *heredoc) |
| 4901 | { |
| 4902 | if (*p == '=') |
| 4903 | { |
| 4904 | if (p[1] == '<' && p[2] == '<') |
| 4905 | { |
| 4906 | *heredoc = TRUE; |
| 4907 | return 3; |
| 4908 | } |
| 4909 | return 1; |
| 4910 | } |
| 4911 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4912 | return 2; |
| 4913 | if (STRNCMP(p, "..=", 3) == 0) |
| 4914 | return 3; |
| 4915 | return 0; |
| 4916 | } |
| 4917 | |
| 4918 | // words that cannot be used as a variable |
| 4919 | static char *reserved[] = { |
| 4920 | "true", |
| 4921 | "false", |
| 4922 | NULL |
| 4923 | }; |
| 4924 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4925 | typedef enum { |
| 4926 | dest_local, |
| 4927 | dest_option, |
| 4928 | dest_env, |
| 4929 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4930 | dest_buffer, |
| 4931 | dest_window, |
| 4932 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4933 | dest_vimvar, |
| 4934 | dest_script, |
| 4935 | dest_reg, |
| 4936 | } assign_dest_T; |
| 4937 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4938 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4939 | * Generate the load instruction for "name". |
| 4940 | */ |
| 4941 | static void |
| 4942 | generate_loadvar( |
| 4943 | cctx_T *cctx, |
| 4944 | assign_dest_T dest, |
| 4945 | char_u *name, |
| 4946 | lvar_T *lvar, |
| 4947 | type_T *type) |
| 4948 | { |
| 4949 | switch (dest) |
| 4950 | { |
| 4951 | case dest_option: |
| 4952 | // TODO: check the option exists |
| 4953 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4954 | break; |
| 4955 | case dest_global: |
| 4956 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4957 | break; |
| 4958 | case dest_buffer: |
| 4959 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4960 | break; |
| 4961 | case dest_window: |
| 4962 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4963 | break; |
| 4964 | case dest_tab: |
| 4965 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4966 | break; |
| 4967 | case dest_script: |
| 4968 | compile_load_scriptvar(cctx, |
| 4969 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4970 | break; |
| 4971 | case dest_env: |
| 4972 | // Include $ in the name here |
| 4973 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4974 | break; |
| 4975 | case dest_reg: |
| 4976 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4977 | break; |
| 4978 | case dest_vimvar: |
| 4979 | generate_LOADV(cctx, name + 2, TRUE); |
| 4980 | break; |
| 4981 | case dest_local: |
| 4982 | if (lvar->lv_from_outer) |
| 4983 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4984 | NULL, type); |
| 4985 | else |
| 4986 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4987 | break; |
| 4988 | } |
| 4989 | } |
| 4990 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4991 | void |
| 4992 | vim9_declare_error(char_u *name) |
| 4993 | { |
| 4994 | char *scope = ""; |
| 4995 | |
| 4996 | switch (*name) |
| 4997 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4998 | case 'g': scope = _("global"); break; |
| 4999 | case 'b': scope = _("buffer"); break; |
| 5000 | case 'w': scope = _("window"); break; |
| 5001 | case 't': scope = _("tab"); break; |
| 5002 | case 'v': scope = "v:"; break; |
| 5003 | case '$': semsg(_(e_declare_env_var), name); return; |
| 5004 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5005 | } |
| 5006 | semsg(_(e_declare_var), scope, name); |
| 5007 | } |
| 5008 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5009 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5010 | * Compile declaration and assignment: |
| 5011 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5012 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5013 | * Return NULL for an error. |
| 5014 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5015 | */ |
| 5016 | static char_u * |
| 5017 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 5018 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5019 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5020 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5021 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5022 | char_u *ret = NULL; |
| 5023 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5024 | int var_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5025 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5026 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5027 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5028 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5029 | int oplen = 0; |
| 5030 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 5031 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5032 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5033 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5034 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5035 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5036 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5037 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 5038 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 5039 | if (p == NULL) |
| 5040 | return *arg == '[' ? arg : NULL; |
| 5041 | |
| 5042 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5043 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 5044 | // TODO: should we allow this, and figure out type inference from list |
| 5045 | // members? |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5046 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5047 | return NULL; |
| 5048 | } |
| 5049 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5050 | sp = p; |
| 5051 | p = skipwhite(p); |
| 5052 | op = p; |
| 5053 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5054 | |
| 5055 | if (var_count > 0 && oplen == 0) |
| 5056 | // can be something like "[1, 2]->func()" |
| 5057 | return arg; |
| 5058 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5059 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 5060 | { |
| 5061 | char_u buf[4]; |
| 5062 | |
| 5063 | vim_strncpy(buf, op, oplen); |
| 5064 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5065 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 5066 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5067 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5068 | if (heredoc) |
| 5069 | { |
| 5070 | list_T *l; |
| 5071 | listitem_T *li; |
| 5072 | |
| 5073 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5074 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5075 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 5076 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5077 | |
| 5078 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 5079 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5080 | { |
| 5081 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 5082 | li->li_tv.vval.v_string = NULL; |
| 5083 | } |
| 5084 | generate_NEWLIST(cctx, l->lv_len); |
| 5085 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5086 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5087 | list_free(l); |
| 5088 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5089 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5090 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5091 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5092 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5093 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 5094 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5095 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5096 | p = skipwhite(op + oplen); |
| 5097 | if (compile_expr0(&p, cctx) == FAIL) |
| 5098 | return NULL; |
| 5099 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5100 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5101 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5102 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5103 | type_T *stacktype; |
| 5104 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5105 | stacktype = stack->ga_len == 0 ? &t_void |
| 5106 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5107 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5108 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5109 | emsg(_(e_cannot_use_void)); |
| 5110 | goto theend; |
| 5111 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5112 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5113 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5114 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5115 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 5116 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5117 | } |
| 5118 | } |
| 5119 | |
| 5120 | /* |
| 5121 | * Loop over variables in "[var, var] = expr". |
| 5122 | * For "var = expr" and "let var: type" this is done only once. |
| 5123 | */ |
| 5124 | if (var_count > 0) |
| 5125 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 5126 | else |
| 5127 | var_start = arg; |
| 5128 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 5129 | { |
| 5130 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 5131 | size_t varlen; |
| 5132 | int new_local = FALSE; |
| 5133 | int opt_type; |
| 5134 | int opt_flags = 0; |
| 5135 | assign_dest_T dest = dest_local; |
| 5136 | int vimvaridx = -1; |
| 5137 | lvar_T *lvar = NULL; |
| 5138 | lvar_T arg_lvar; |
| 5139 | int has_type = FALSE; |
| 5140 | int has_index = FALSE; |
| 5141 | int instr_count = -1; |
| 5142 | |
| 5143 | p = (*var_start == '&' || *var_start == '$' |
| 5144 | || *var_start == '@') ? var_start + 1 : var_start; |
| 5145 | p = to_name_end(p, TRUE); |
| 5146 | |
| 5147 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 5148 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 5149 | --var_end; |
| 5150 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 5151 | --p; |
| 5152 | |
| 5153 | varlen = p - var_start; |
| 5154 | vim_free(name); |
| 5155 | name = vim_strnsave(var_start, varlen); |
| 5156 | if (name == NULL) |
| 5157 | return NULL; |
| 5158 | if (!heredoc) |
| 5159 | type = &t_any; |
| 5160 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5161 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5162 | { |
| 5163 | if (*var_start == '&') |
| 5164 | { |
| 5165 | int cc; |
| 5166 | long numval; |
| 5167 | |
| 5168 | dest = dest_option; |
| 5169 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5170 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5171 | emsg(_(e_const_option)); |
| 5172 | goto theend; |
| 5173 | } |
| 5174 | if (is_decl) |
| 5175 | { |
| 5176 | semsg(_("E1052: Cannot declare an option: %s"), var_start); |
| 5177 | goto theend; |
| 5178 | } |
| 5179 | p = var_start; |
| 5180 | p = find_option_end(&p, &opt_flags); |
| 5181 | if (p == NULL) |
| 5182 | { |
| 5183 | // cannot happen? |
| 5184 | emsg(_(e_letunexp)); |
| 5185 | goto theend; |
| 5186 | } |
| 5187 | cc = *p; |
| 5188 | *p = NUL; |
| 5189 | opt_type = get_option_value(var_start + 1, &numval, |
| 5190 | NULL, opt_flags); |
| 5191 | *p = cc; |
| 5192 | if (opt_type == -3) |
| 5193 | { |
| 5194 | semsg(_(e_unknown_option), var_start); |
| 5195 | goto theend; |
| 5196 | } |
| 5197 | if (opt_type == -2 || opt_type == 0) |
| 5198 | type = &t_string; |
| 5199 | else |
| 5200 | type = &t_number; // both number and boolean option |
| 5201 | } |
| 5202 | else if (*var_start == '$') |
| 5203 | { |
| 5204 | dest = dest_env; |
| 5205 | type = &t_string; |
| 5206 | if (is_decl) |
| 5207 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5208 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5209 | goto theend; |
| 5210 | } |
| 5211 | } |
| 5212 | else if (*var_start == '@') |
| 5213 | { |
| 5214 | if (!valid_yank_reg(var_start[1], TRUE)) |
| 5215 | { |
| 5216 | emsg_invreg(var_start[1]); |
| 5217 | goto theend; |
| 5218 | } |
| 5219 | dest = dest_reg; |
| 5220 | type = &t_string; |
| 5221 | if (is_decl) |
| 5222 | { |
| 5223 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 5224 | goto theend; |
| 5225 | } |
| 5226 | } |
| 5227 | else if (STRNCMP(var_start, "g:", 2) == 0) |
| 5228 | { |
| 5229 | dest = dest_global; |
| 5230 | if (is_decl) |
| 5231 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5232 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5233 | goto theend; |
| 5234 | } |
| 5235 | } |
| 5236 | else if (STRNCMP(var_start, "b:", 2) == 0) |
| 5237 | { |
| 5238 | dest = dest_buffer; |
| 5239 | if (is_decl) |
| 5240 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5241 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5242 | goto theend; |
| 5243 | } |
| 5244 | } |
| 5245 | else if (STRNCMP(var_start, "w:", 2) == 0) |
| 5246 | { |
| 5247 | dest = dest_window; |
| 5248 | if (is_decl) |
| 5249 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5250 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5251 | goto theend; |
| 5252 | } |
| 5253 | } |
| 5254 | else if (STRNCMP(var_start, "t:", 2) == 0) |
| 5255 | { |
| 5256 | dest = dest_tab; |
| 5257 | if (is_decl) |
| 5258 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5259 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5260 | goto theend; |
| 5261 | } |
| 5262 | } |
| 5263 | else if (STRNCMP(var_start, "v:", 2) == 0) |
| 5264 | { |
| 5265 | typval_T *vtv; |
| 5266 | int di_flags; |
| 5267 | |
| 5268 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5269 | if (vimvaridx < 0) |
| 5270 | { |
| 5271 | semsg(_(e_var_notfound), var_start); |
| 5272 | goto theend; |
| 5273 | } |
| 5274 | // We use the current value of "sandbox" here, is that OK? |
| 5275 | if (var_check_ro(di_flags, name, FALSE)) |
| 5276 | goto theend; |
| 5277 | dest = dest_vimvar; |
| 5278 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 5279 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5280 | if (is_decl) |
| 5281 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5282 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5283 | goto theend; |
| 5284 | } |
| 5285 | } |
| 5286 | else |
| 5287 | { |
| 5288 | int idx; |
| 5289 | |
| 5290 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5291 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5292 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5293 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5294 | goto theend; |
| 5295 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5296 | |
| 5297 | lvar = lookup_local(var_start, varlen, cctx); |
| 5298 | if (lvar == NULL) |
| 5299 | { |
| 5300 | CLEAR_FIELD(arg_lvar); |
| 5301 | if (lookup_arg(var_start, varlen, |
| 5302 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 5303 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5304 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5305 | if (is_decl) |
| 5306 | { |
| 5307 | semsg(_(e_used_as_arg), name); |
| 5308 | goto theend; |
| 5309 | } |
| 5310 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5311 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5312 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5313 | if (lvar != NULL) |
| 5314 | { |
| 5315 | if (is_decl) |
| 5316 | { |
| 5317 | semsg(_("E1017: Variable already declared: %s"), name); |
| 5318 | goto theend; |
| 5319 | } |
| 5320 | else if (lvar->lv_const) |
| 5321 | { |
| 5322 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 5323 | name); |
| 5324 | goto theend; |
| 5325 | } |
| 5326 | } |
| 5327 | else if (STRNCMP(var_start, "s:", 2) == 0 |
| 5328 | || lookup_script(var_start, varlen) == OK |
| 5329 | || find_imported(var_start, varlen, cctx) != NULL) |
| 5330 | { |
| 5331 | dest = dest_script; |
| 5332 | if (is_decl) |
| 5333 | { |
| 5334 | semsg(_("E1054: Variable already declared in the script: %s"), |
| 5335 | name); |
| 5336 | goto theend; |
| 5337 | } |
| 5338 | } |
| 5339 | else if (name[1] == ':' && name[2] != NUL) |
| 5340 | { |
| 5341 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
| 5342 | name); |
| 5343 | goto theend; |
| 5344 | } |
| 5345 | else if (!is_decl) |
| 5346 | { |
| 5347 | semsg(_("E1089: unknown variable: %s"), name); |
| 5348 | goto theend; |
| 5349 | } |
| 5350 | } |
| 5351 | } |
| 5352 | |
| 5353 | // handle "a:name" as a name, not index "name" on "a" |
| 5354 | if (varlen > 1 || var_start[varlen] != ':') |
| 5355 | p = var_end; |
| 5356 | |
| 5357 | if (dest != dest_option) |
| 5358 | { |
| 5359 | if (is_decl && *p == ':') |
| 5360 | { |
| 5361 | // parse optional type: "let var: type = expr" |
| 5362 | if (!VIM_ISWHITE(p[1])) |
| 5363 | { |
| 5364 | semsg(_(e_white_after), ":"); |
| 5365 | goto theend; |
| 5366 | } |
| 5367 | p = skipwhite(p + 1); |
| 5368 | type = parse_type(&p, cctx->ctx_type_list); |
| 5369 | has_type = TRUE; |
| 5370 | } |
| 5371 | else if (lvar != NULL) |
| 5372 | type = lvar->lv_type; |
| 5373 | } |
| 5374 | |
| 5375 | if (oplen == 3 && !heredoc && dest != dest_global |
| 5376 | && type->tt_type != VAR_STRING |
| 5377 | && type->tt_type != VAR_ANY) |
| 5378 | { |
| 5379 | emsg(_("E1019: Can only concatenate to string")); |
| 5380 | goto theend; |
| 5381 | } |
| 5382 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5383 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5384 | { |
| 5385 | if (oplen > 1 && !heredoc) |
| 5386 | { |
| 5387 | // +=, /=, etc. require an existing variable |
| 5388 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 5389 | name); |
| 5390 | goto theend; |
| 5391 | } |
| 5392 | |
| 5393 | // new local variable |
| 5394 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
| 5395 | goto theend; |
| 5396 | lvar = reserve_local(cctx, var_start, varlen, |
| 5397 | cmdidx == CMD_const, type); |
| 5398 | if (lvar == NULL) |
| 5399 | goto theend; |
| 5400 | new_local = TRUE; |
| 5401 | } |
| 5402 | |
| 5403 | member_type = type; |
| 5404 | if (var_end > var_start + varlen) |
| 5405 | { |
| 5406 | // Something follows after the variable: "var[idx]". |
| 5407 | if (is_decl) |
| 5408 | { |
| 5409 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 5410 | goto theend; |
| 5411 | } |
| 5412 | |
| 5413 | if (var_start[varlen] == '[') |
| 5414 | { |
| 5415 | has_index = TRUE; |
| 5416 | if (type->tt_member == NULL) |
| 5417 | { |
| 5418 | semsg(_("E1088: cannot use an index on %s"), name); |
| 5419 | goto theend; |
| 5420 | } |
| 5421 | member_type = type->tt_member; |
| 5422 | } |
| 5423 | else |
| 5424 | { |
| 5425 | semsg("Not supported yet: %s", var_start); |
| 5426 | goto theend; |
| 5427 | } |
| 5428 | } |
| 5429 | else if (lvar == &arg_lvar) |
| 5430 | { |
| 5431 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 5432 | goto theend; |
| 5433 | } |
| 5434 | |
| 5435 | if (!heredoc) |
| 5436 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5437 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5438 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5439 | if (oplen > 0 && var_count == 0) |
| 5440 | { |
| 5441 | // skip over the "=" and the expression |
| 5442 | p = skipwhite(op + oplen); |
| 5443 | compile_expr0(&p, cctx); |
| 5444 | } |
| 5445 | } |
| 5446 | else if (oplen > 0) |
| 5447 | { |
| 5448 | type_T *stacktype; |
| 5449 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5450 | // For "var = expr" evaluate the expression. |
| 5451 | if (var_count == 0) |
| 5452 | { |
| 5453 | int r; |
| 5454 | |
| 5455 | // for "+=", "*=", "..=" etc. first load the current value |
| 5456 | if (*op != '=') |
| 5457 | { |
| 5458 | generate_loadvar(cctx, dest, name, lvar, type); |
| 5459 | |
| 5460 | if (has_index) |
| 5461 | { |
| 5462 | // TODO: get member from list or dict |
| 5463 | emsg("Index with operation not supported yet"); |
| 5464 | goto theend; |
| 5465 | } |
| 5466 | } |
| 5467 | |
| 5468 | // Compile the expression. Temporarily hide the new local |
| 5469 | // variable here, it is not available to this expression. |
| 5470 | if (new_local) |
| 5471 | --cctx->ctx_locals.ga_len; |
| 5472 | instr_count = instr->ga_len; |
| 5473 | p = skipwhite(op + oplen); |
| 5474 | r = compile_expr0(&p, cctx); |
| 5475 | if (new_local) |
| 5476 | ++cctx->ctx_locals.ga_len; |
| 5477 | if (r == FAIL) |
| 5478 | goto theend; |
| 5479 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5480 | else if (semicolon && var_idx == var_count - 1) |
| 5481 | { |
| 5482 | // For "[var; var] = expr" get the rest of the list |
| 5483 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5484 | goto theend; |
| 5485 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5486 | else |
| 5487 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5488 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5489 | // list. |
| 5490 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 5491 | return FAIL; |
| 5492 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5493 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5494 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5495 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5496 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5497 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5498 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5499 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5500 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5501 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5502 | emsg(_(e_cannot_use_void)); |
| 5503 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5504 | } |
| 5505 | else |
| 5506 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5507 | // An empty list or dict has a &t_void member, |
| 5508 | // for a variable that implies &t_any. |
| 5509 | if (stacktype == &t_list_empty) |
| 5510 | lvar->lv_type = &t_list_any; |
| 5511 | else if (stacktype == &t_dict_empty) |
| 5512 | lvar->lv_type = &t_dict_any; |
| 5513 | else |
| 5514 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5515 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5516 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5517 | else |
| 5518 | { |
| 5519 | type_T *use_type = lvar->lv_type; |
| 5520 | |
| 5521 | if (has_index) |
| 5522 | { |
| 5523 | use_type = use_type->tt_member; |
| 5524 | if (use_type == NULL) |
| 5525 | use_type = &t_void; |
| 5526 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5527 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5528 | == FAIL) |
| 5529 | goto theend; |
| 5530 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5531 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5532 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5533 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5534 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5535 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5536 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5537 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5538 | emsg(_(e_const_req_value)); |
| 5539 | goto theend; |
| 5540 | } |
| 5541 | else if (!has_type || dest == dest_option) |
| 5542 | { |
| 5543 | emsg(_(e_type_req)); |
| 5544 | goto theend; |
| 5545 | } |
| 5546 | else |
| 5547 | { |
| 5548 | // variables are always initialized |
| 5549 | if (ga_grow(instr, 1) == FAIL) |
| 5550 | goto theend; |
| 5551 | switch (member_type->tt_type) |
| 5552 | { |
| 5553 | case VAR_BOOL: |
| 5554 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5555 | break; |
| 5556 | case VAR_FLOAT: |
| 5557 | #ifdef FEAT_FLOAT |
| 5558 | generate_PUSHF(cctx, 0.0); |
| 5559 | #endif |
| 5560 | break; |
| 5561 | case VAR_STRING: |
| 5562 | generate_PUSHS(cctx, NULL); |
| 5563 | break; |
| 5564 | case VAR_BLOB: |
| 5565 | generate_PUSHBLOB(cctx, NULL); |
| 5566 | break; |
| 5567 | case VAR_FUNC: |
| 5568 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5569 | break; |
| 5570 | case VAR_LIST: |
| 5571 | generate_NEWLIST(cctx, 0); |
| 5572 | break; |
| 5573 | case VAR_DICT: |
| 5574 | generate_NEWDICT(cctx, 0); |
| 5575 | break; |
| 5576 | case VAR_JOB: |
| 5577 | generate_PUSHJOB(cctx, NULL); |
| 5578 | break; |
| 5579 | case VAR_CHANNEL: |
| 5580 | generate_PUSHCHANNEL(cctx, NULL); |
| 5581 | break; |
| 5582 | case VAR_NUMBER: |
| 5583 | case VAR_UNKNOWN: |
| 5584 | case VAR_ANY: |
| 5585 | case VAR_PARTIAL: |
| 5586 | case VAR_VOID: |
| 5587 | case VAR_SPECIAL: // cannot happen |
| 5588 | generate_PUSHNR(cctx, 0); |
| 5589 | break; |
| 5590 | } |
| 5591 | } |
| 5592 | if (var_count == 0) |
| 5593 | end = p; |
| 5594 | } |
| 5595 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5596 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5597 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5598 | break; |
| 5599 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5600 | if (oplen > 0 && *op != '=') |
| 5601 | { |
| 5602 | type_T *expected = &t_number; |
| 5603 | type_T *stacktype; |
| 5604 | |
| 5605 | // TODO: if type is known use float or any operation |
| 5606 | |
| 5607 | if (*op == '.') |
| 5608 | expected = &t_string; |
| 5609 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5610 | if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5611 | goto theend; |
| 5612 | |
| 5613 | if (*op == '.') |
| 5614 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 5615 | else |
| 5616 | { |
| 5617 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 5618 | |
| 5619 | if (isn == NULL) |
| 5620 | goto theend; |
| 5621 | switch (*op) |
| 5622 | { |
| 5623 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 5624 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 5625 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 5626 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 5627 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 5628 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5629 | } |
| 5630 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5631 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5632 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5633 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5634 | int r; |
| 5635 | |
| 5636 | // Compile the "idx" in "var[idx]". |
| 5637 | if (new_local) |
| 5638 | --cctx->ctx_locals.ga_len; |
| 5639 | p = skipwhite(var_start + varlen + 1); |
| 5640 | r = compile_expr0(&p, cctx); |
| 5641 | if (new_local) |
| 5642 | ++cctx->ctx_locals.ga_len; |
| 5643 | if (r == FAIL) |
| 5644 | goto theend; |
| 5645 | if (*skipwhite(p) != ']') |
| 5646 | { |
| 5647 | emsg(_(e_missbrac)); |
| 5648 | goto theend; |
| 5649 | } |
| 5650 | if (type->tt_type == VAR_DICT |
| 5651 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5652 | goto theend; |
| 5653 | if (type->tt_type == VAR_LIST |
| 5654 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5655 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5656 | { |
| 5657 | emsg(_(e_number_exp)); |
| 5658 | goto theend; |
| 5659 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5660 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5661 | // Load the dict or list. On the stack we then have: |
| 5662 | // - value |
| 5663 | // - index |
| 5664 | // - variable |
| 5665 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5666 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5667 | if (type->tt_type == VAR_LIST) |
| 5668 | { |
| 5669 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5670 | return FAIL; |
| 5671 | } |
| 5672 | else if (type->tt_type == VAR_DICT) |
| 5673 | { |
| 5674 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5675 | return FAIL; |
| 5676 | } |
| 5677 | else |
| 5678 | { |
| 5679 | emsg(_(e_listreq)); |
| 5680 | goto theend; |
| 5681 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5682 | } |
| 5683 | else |
| 5684 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5685 | switch (dest) |
| 5686 | { |
| 5687 | case dest_option: |
| 5688 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5689 | break; |
| 5690 | case dest_global: |
| 5691 | // include g: with the name, easier to execute that way |
| 5692 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5693 | break; |
| 5694 | case dest_buffer: |
| 5695 | // include b: with the name, easier to execute that way |
| 5696 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5697 | break; |
| 5698 | case dest_window: |
| 5699 | // include w: with the name, easier to execute that way |
| 5700 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5701 | break; |
| 5702 | case dest_tab: |
| 5703 | // include t: with the name, easier to execute that way |
| 5704 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5705 | break; |
| 5706 | case dest_env: |
| 5707 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5708 | break; |
| 5709 | case dest_reg: |
| 5710 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5711 | break; |
| 5712 | case dest_vimvar: |
| 5713 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5714 | break; |
| 5715 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5716 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5717 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5718 | imported_T *import = NULL; |
| 5719 | int sid = current_sctx.sc_sid; |
| 5720 | int idx; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5721 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5722 | if (name[1] != ':') |
| 5723 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5724 | import = find_imported(name, 0, cctx); |
| 5725 | if (import != NULL) |
| 5726 | sid = import->imp_sid; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5727 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5728 | |
| 5729 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 5730 | // TODO: specific type |
| 5731 | if (idx < 0) |
| 5732 | { |
| 5733 | char_u *name_s = name; |
| 5734 | |
| 5735 | // Include s: in the name for store_var() |
| 5736 | if (name[1] != ':') |
| 5737 | { |
| 5738 | int len = (int)STRLEN(name) + 3; |
| 5739 | |
| 5740 | name_s = alloc(len); |
| 5741 | if (name_s == NULL) |
| 5742 | name_s = name; |
| 5743 | else |
| 5744 | vim_snprintf((char *)name_s, len, |
| 5745 | "s:%s", name); |
| 5746 | } |
| 5747 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5748 | &t_any); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5749 | if (name_s != name) |
| 5750 | vim_free(name_s); |
| 5751 | } |
| 5752 | else |
| 5753 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5754 | sid, idx, &t_any); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5755 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5756 | break; |
| 5757 | case dest_local: |
| 5758 | if (lvar != NULL) |
| 5759 | { |
| 5760 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5761 | + instr->ga_len - 1; |
| 5762 | |
| 5763 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5764 | // ISN_STORE into ISN_STORENR |
| 5765 | if (!lvar->lv_from_outer |
| 5766 | && instr->ga_len == instr_count + 1 |
| 5767 | && isn->isn_type == ISN_PUSHNR) |
| 5768 | { |
| 5769 | varnumber_T val = isn->isn_arg.number; |
| 5770 | |
| 5771 | isn->isn_type = ISN_STORENR; |
| 5772 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5773 | isn->isn_arg.storenr.stnr_val = val; |
| 5774 | if (stack->ga_len > 0) |
| 5775 | --stack->ga_len; |
| 5776 | } |
| 5777 | else if (lvar->lv_from_outer) |
| 5778 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5779 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5780 | else |
| 5781 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5782 | } |
| 5783 | break; |
| 5784 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5785 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5786 | |
| 5787 | if (var_idx + 1 < var_count) |
| 5788 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5789 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5790 | |
| 5791 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5792 | if (var_count > 0 && !semicolon) |
| 5793 | { |
| 5794 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5795 | goto theend; |
| 5796 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5797 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5798 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5799 | |
| 5800 | theend: |
| 5801 | vim_free(name); |
| 5802 | return ret; |
| 5803 | } |
| 5804 | |
| 5805 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5806 | * Check if "name" can be "unlet". |
| 5807 | */ |
| 5808 | int |
| 5809 | check_vim9_unlet(char_u *name) |
| 5810 | { |
| 5811 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5812 | { |
| 5813 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5814 | return FAIL; |
| 5815 | } |
| 5816 | return OK; |
| 5817 | } |
| 5818 | |
| 5819 | /* |
| 5820 | * Callback passed to ex_unletlock(). |
| 5821 | */ |
| 5822 | static int |
| 5823 | compile_unlet( |
| 5824 | lval_T *lvp, |
| 5825 | char_u *name_end, |
| 5826 | exarg_T *eap, |
| 5827 | int deep UNUSED, |
| 5828 | void *coookie) |
| 5829 | { |
| 5830 | cctx_T *cctx = coookie; |
| 5831 | |
| 5832 | if (lvp->ll_tv == NULL) |
| 5833 | { |
| 5834 | char_u *p = lvp->ll_name; |
| 5835 | int cc = *name_end; |
| 5836 | int ret = OK; |
| 5837 | |
| 5838 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5839 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5840 | if (*p == '$') |
| 5841 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5842 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5843 | ret = FAIL; |
| 5844 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5845 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5846 | |
| 5847 | *name_end = cc; |
| 5848 | return ret; |
| 5849 | } |
| 5850 | |
| 5851 | // TODO: unlet {list}[idx] |
| 5852 | // TODO: unlet {dict}[key] |
| 5853 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5854 | return FAIL; |
| 5855 | } |
| 5856 | |
| 5857 | /* |
| 5858 | * compile "unlet var", "lock var" and "unlock var" |
| 5859 | * "arg" points to "var". |
| 5860 | */ |
| 5861 | static char_u * |
| 5862 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5863 | { |
| 5864 | char_u *p = arg; |
| 5865 | |
| 5866 | if (eap->cmdidx != CMD_unlet) |
| 5867 | { |
| 5868 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5869 | return NULL; |
| 5870 | } |
| 5871 | |
| 5872 | if (*p == '!') |
| 5873 | { |
| 5874 | p = skipwhite(p + 1); |
| 5875 | eap->forceit = TRUE; |
| 5876 | } |
| 5877 | |
| 5878 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5879 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5880 | } |
| 5881 | |
| 5882 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5883 | * Compile an :import command. |
| 5884 | */ |
| 5885 | static char_u * |
| 5886 | compile_import(char_u *arg, cctx_T *cctx) |
| 5887 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5888 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5889 | } |
| 5890 | |
| 5891 | /* |
| 5892 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5893 | */ |
| 5894 | static int |
| 5895 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5896 | { |
| 5897 | garray_T *instr = &cctx->ctx_instr; |
| 5898 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5899 | |
| 5900 | if (endlabel == NULL) |
| 5901 | return FAIL; |
| 5902 | endlabel->el_next = *el; |
| 5903 | *el = endlabel; |
| 5904 | endlabel->el_end_label = instr->ga_len; |
| 5905 | |
| 5906 | generate_JUMP(cctx, when, 0); |
| 5907 | return OK; |
| 5908 | } |
| 5909 | |
| 5910 | static void |
| 5911 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5912 | { |
| 5913 | garray_T *instr = &cctx->ctx_instr; |
| 5914 | |
| 5915 | while (*el != NULL) |
| 5916 | { |
| 5917 | endlabel_T *cur = (*el); |
| 5918 | isn_T *isn; |
| 5919 | |
| 5920 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5921 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5922 | *el = cur->el_next; |
| 5923 | vim_free(cur); |
| 5924 | } |
| 5925 | } |
| 5926 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5927 | static void |
| 5928 | compile_free_jump_to_end(endlabel_T **el) |
| 5929 | { |
| 5930 | while (*el != NULL) |
| 5931 | { |
| 5932 | endlabel_T *cur = (*el); |
| 5933 | |
| 5934 | *el = cur->el_next; |
| 5935 | vim_free(cur); |
| 5936 | } |
| 5937 | } |
| 5938 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5939 | /* |
| 5940 | * Create a new scope and set up the generic items. |
| 5941 | */ |
| 5942 | static scope_T * |
| 5943 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5944 | { |
| 5945 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5946 | |
| 5947 | if (scope == NULL) |
| 5948 | return NULL; |
| 5949 | scope->se_outer = cctx->ctx_scope; |
| 5950 | cctx->ctx_scope = scope; |
| 5951 | scope->se_type = type; |
| 5952 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5953 | return scope; |
| 5954 | } |
| 5955 | |
| 5956 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5957 | * Free the current scope and go back to the outer scope. |
| 5958 | */ |
| 5959 | static void |
| 5960 | drop_scope(cctx_T *cctx) |
| 5961 | { |
| 5962 | scope_T *scope = cctx->ctx_scope; |
| 5963 | |
| 5964 | if (scope == NULL) |
| 5965 | { |
| 5966 | iemsg("calling drop_scope() without a scope"); |
| 5967 | return; |
| 5968 | } |
| 5969 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5970 | switch (scope->se_type) |
| 5971 | { |
| 5972 | case IF_SCOPE: |
| 5973 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5974 | case FOR_SCOPE: |
| 5975 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5976 | case WHILE_SCOPE: |
| 5977 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5978 | case TRY_SCOPE: |
| 5979 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5980 | case NO_SCOPE: |
| 5981 | case BLOCK_SCOPE: |
| 5982 | break; |
| 5983 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5984 | vim_free(scope); |
| 5985 | } |
| 5986 | |
| 5987 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5988 | * compile "if expr" |
| 5989 | * |
| 5990 | * "if expr" Produces instructions: |
| 5991 | * EVAL expr Push result of "expr" |
| 5992 | * JUMP_IF_FALSE end |
| 5993 | * ... body ... |
| 5994 | * end: |
| 5995 | * |
| 5996 | * "if expr | else" Produces instructions: |
| 5997 | * EVAL expr Push result of "expr" |
| 5998 | * JUMP_IF_FALSE else |
| 5999 | * ... body ... |
| 6000 | * JUMP_ALWAYS end |
| 6001 | * else: |
| 6002 | * ... body ... |
| 6003 | * end: |
| 6004 | * |
| 6005 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 6006 | * EVAL expr Push result of "expr" |
| 6007 | * JUMP_IF_FALSE elseif |
| 6008 | * ... body ... |
| 6009 | * JUMP_ALWAYS end |
| 6010 | * elseif: |
| 6011 | * EVAL expr Push result of "expr" |
| 6012 | * JUMP_IF_FALSE else |
| 6013 | * ... body ... |
| 6014 | * JUMP_ALWAYS end |
| 6015 | * else: |
| 6016 | * ... body ... |
| 6017 | * end: |
| 6018 | */ |
| 6019 | static char_u * |
| 6020 | compile_if(char_u *arg, cctx_T *cctx) |
| 6021 | { |
| 6022 | char_u *p = arg; |
| 6023 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6024 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6025 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6026 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6027 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6028 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6029 | CLEAR_FIELD(ppconst); |
| 6030 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6031 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6032 | clear_ppconst(&ppconst); |
| 6033 | return NULL; |
| 6034 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6035 | if (cctx->ctx_skip == SKIP_YES) |
| 6036 | clear_ppconst(&ppconst); |
| 6037 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6038 | { |
| 6039 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6040 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6041 | clear_ppconst(&ppconst); |
| 6042 | } |
| 6043 | else |
| 6044 | { |
| 6045 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6046 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6047 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6048 | return NULL; |
| 6049 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6050 | |
| 6051 | scope = new_scope(cctx, IF_SCOPE); |
| 6052 | if (scope == NULL) |
| 6053 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6054 | scope->se_skip_save = skip_save; |
| 6055 | // "is_had_return" will be reset if any block does not end in :return |
| 6056 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6057 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6058 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6059 | { |
| 6060 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6061 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6062 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6063 | } |
| 6064 | else |
| 6065 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6066 | |
| 6067 | return p; |
| 6068 | } |
| 6069 | |
| 6070 | static char_u * |
| 6071 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 6072 | { |
| 6073 | char_u *p = arg; |
| 6074 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6075 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6076 | isn_T *isn; |
| 6077 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6078 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6079 | |
| 6080 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6081 | { |
| 6082 | emsg(_(e_elseif_without_if)); |
| 6083 | return NULL; |
| 6084 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6085 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6086 | if (!cctx->ctx_had_return) |
| 6087 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6088 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6089 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6090 | { |
| 6091 | 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] | 6092 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6093 | return NULL; |
| 6094 | // previous "if" or "elseif" jumps here |
| 6095 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6096 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6097 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6098 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6099 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6100 | CLEAR_FIELD(ppconst); |
| 6101 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6102 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6103 | clear_ppconst(&ppconst); |
| 6104 | return NULL; |
| 6105 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6106 | if (scope->se_skip_save == SKIP_YES) |
| 6107 | clear_ppconst(&ppconst); |
| 6108 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6109 | { |
| 6110 | // The expression results in a constant. |
| 6111 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6112 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6113 | clear_ppconst(&ppconst); |
| 6114 | scope->se_u.se_if.is_if_label = -1; |
| 6115 | } |
| 6116 | else |
| 6117 | { |
| 6118 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6119 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6120 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6121 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6122 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6123 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6124 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6125 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6126 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6127 | |
| 6128 | return p; |
| 6129 | } |
| 6130 | |
| 6131 | static char_u * |
| 6132 | compile_else(char_u *arg, cctx_T *cctx) |
| 6133 | { |
| 6134 | char_u *p = arg; |
| 6135 | garray_T *instr = &cctx->ctx_instr; |
| 6136 | isn_T *isn; |
| 6137 | scope_T *scope = cctx->ctx_scope; |
| 6138 | |
| 6139 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6140 | { |
| 6141 | emsg(_(e_else_without_if)); |
| 6142 | return NULL; |
| 6143 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6144 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6145 | if (!cctx->ctx_had_return) |
| 6146 | scope->se_u.se_if.is_had_return = FALSE; |
| 6147 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6148 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6149 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6150 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6151 | // 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] | 6152 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6153 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6154 | if (!cctx->ctx_had_return |
| 6155 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6156 | JUMP_ALWAYS, cctx) == FAIL) |
| 6157 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6158 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6159 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6160 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6161 | { |
| 6162 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6163 | { |
| 6164 | // previous "if" or "elseif" jumps here |
| 6165 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6166 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6167 | scope->se_u.se_if.is_if_label = -1; |
| 6168 | } |
| 6169 | } |
| 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 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6173 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6174 | |
| 6175 | return p; |
| 6176 | } |
| 6177 | |
| 6178 | static char_u * |
| 6179 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6180 | { |
| 6181 | scope_T *scope = cctx->ctx_scope; |
| 6182 | ifscope_T *ifscope; |
| 6183 | garray_T *instr = &cctx->ctx_instr; |
| 6184 | isn_T *isn; |
| 6185 | |
| 6186 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6187 | { |
| 6188 | emsg(_(e_endif_without_if)); |
| 6189 | return NULL; |
| 6190 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6191 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6192 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6193 | if (!cctx->ctx_had_return) |
| 6194 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6195 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6196 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6197 | { |
| 6198 | // previous "if" or "elseif" jumps here |
| 6199 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6200 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6201 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6202 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6203 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6204 | cctx->ctx_skip = scope->se_skip_save; |
| 6205 | |
| 6206 | // If all the blocks end in :return and there is an :else then the |
| 6207 | // had_return flag is set. |
| 6208 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6209 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6210 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6211 | return arg; |
| 6212 | } |
| 6213 | |
| 6214 | /* |
| 6215 | * compile "for var in expr" |
| 6216 | * |
| 6217 | * Produces instructions: |
| 6218 | * PUSHNR -1 |
| 6219 | * STORE loop-idx Set index to -1 |
| 6220 | * EVAL expr Push result of "expr" |
| 6221 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6222 | * - if beyond end, jump to "end" |
| 6223 | * - otherwise get item from list and push it |
| 6224 | * STORE var Store item in "var" |
| 6225 | * ... body ... |
| 6226 | * JUMP top Jump back to repeat |
| 6227 | * end: DROP Drop the result of "expr" |
| 6228 | * |
| 6229 | */ |
| 6230 | static char_u * |
| 6231 | compile_for(char_u *arg, cctx_T *cctx) |
| 6232 | { |
| 6233 | char_u *p; |
| 6234 | size_t varlen; |
| 6235 | garray_T *instr = &cctx->ctx_instr; |
| 6236 | garray_T *stack = &cctx->ctx_type_stack; |
| 6237 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6238 | lvar_T *loop_lvar; // loop iteration variable |
| 6239 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6240 | type_T *vartype; |
| 6241 | |
| 6242 | // TODO: list of variables: "for [key, value] in dict" |
| 6243 | // parse "var" |
| 6244 | for (p = arg; eval_isnamec1(*p); ++p) |
| 6245 | ; |
| 6246 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6247 | var_lvar = lookup_local(arg, varlen, cctx); |
| 6248 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6249 | { |
| 6250 | semsg(_("E1023: variable already defined: %s"), arg); |
| 6251 | return NULL; |
| 6252 | } |
| 6253 | |
| 6254 | // consume "in" |
| 6255 | p = skipwhite(p); |
| 6256 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 6257 | { |
| 6258 | emsg(_(e_missing_in)); |
| 6259 | return NULL; |
| 6260 | } |
| 6261 | p = skipwhite(p + 2); |
| 6262 | |
| 6263 | |
| 6264 | scope = new_scope(cctx, FOR_SCOPE); |
| 6265 | if (scope == NULL) |
| 6266 | return NULL; |
| 6267 | |
| 6268 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6269 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6270 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6271 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6272 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6273 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6274 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6275 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6276 | |
| 6277 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6278 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 6279 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6280 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6281 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6282 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6283 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6284 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6285 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6286 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6287 | |
| 6288 | // compile "expr", it remains on the stack until "endfor" |
| 6289 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6290 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6291 | { |
| 6292 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6293 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6294 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6295 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6296 | // Now that we know the type of "var", check that it is a list, now or at |
| 6297 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6298 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 6299 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6300 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6301 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6302 | return NULL; |
| 6303 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6304 | 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] | 6305 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6306 | |
| 6307 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6308 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6309 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6310 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 6311 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6312 | |
| 6313 | return arg; |
| 6314 | } |
| 6315 | |
| 6316 | /* |
| 6317 | * compile "endfor" |
| 6318 | */ |
| 6319 | static char_u * |
| 6320 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 6321 | { |
| 6322 | garray_T *instr = &cctx->ctx_instr; |
| 6323 | scope_T *scope = cctx->ctx_scope; |
| 6324 | forscope_T *forscope; |
| 6325 | isn_T *isn; |
| 6326 | |
| 6327 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 6328 | { |
| 6329 | emsg(_(e_for)); |
| 6330 | return NULL; |
| 6331 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6332 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6333 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6334 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6335 | |
| 6336 | // At end of ":for" scope jump back to the FOR instruction. |
| 6337 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 6338 | |
| 6339 | // Fill in the "end" label in the FOR statement so it can jump here |
| 6340 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 6341 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 6342 | |
| 6343 | // Fill in the "end" label any BREAK statements |
| 6344 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 6345 | |
| 6346 | // Below the ":for" scope drop the "expr" list from the stack. |
| 6347 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 6348 | return NULL; |
| 6349 | |
| 6350 | vim_free(scope); |
| 6351 | |
| 6352 | return arg; |
| 6353 | } |
| 6354 | |
| 6355 | /* |
| 6356 | * compile "while expr" |
| 6357 | * |
| 6358 | * Produces instructions: |
| 6359 | * top: EVAL expr Push result of "expr" |
| 6360 | * JUMP_IF_FALSE end jump if false |
| 6361 | * ... body ... |
| 6362 | * JUMP top Jump back to repeat |
| 6363 | * end: |
| 6364 | * |
| 6365 | */ |
| 6366 | static char_u * |
| 6367 | compile_while(char_u *arg, cctx_T *cctx) |
| 6368 | { |
| 6369 | char_u *p = arg; |
| 6370 | garray_T *instr = &cctx->ctx_instr; |
| 6371 | scope_T *scope; |
| 6372 | |
| 6373 | scope = new_scope(cctx, WHILE_SCOPE); |
| 6374 | if (scope == NULL) |
| 6375 | return NULL; |
| 6376 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6377 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6378 | |
| 6379 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6380 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6381 | return NULL; |
| 6382 | |
| 6383 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6384 | 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] | 6385 | JUMP_IF_FALSE, cctx) == FAIL) |
| 6386 | return FAIL; |
| 6387 | |
| 6388 | return p; |
| 6389 | } |
| 6390 | |
| 6391 | /* |
| 6392 | * compile "endwhile" |
| 6393 | */ |
| 6394 | static char_u * |
| 6395 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 6396 | { |
| 6397 | scope_T *scope = cctx->ctx_scope; |
| 6398 | |
| 6399 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 6400 | { |
| 6401 | emsg(_(e_while)); |
| 6402 | return NULL; |
| 6403 | } |
| 6404 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6405 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6406 | |
| 6407 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6408 | 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] | 6409 | |
| 6410 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 6411 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6412 | 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] | 6413 | |
| 6414 | vim_free(scope); |
| 6415 | |
| 6416 | return arg; |
| 6417 | } |
| 6418 | |
| 6419 | /* |
| 6420 | * compile "continue" |
| 6421 | */ |
| 6422 | static char_u * |
| 6423 | compile_continue(char_u *arg, cctx_T *cctx) |
| 6424 | { |
| 6425 | scope_T *scope = cctx->ctx_scope; |
| 6426 | |
| 6427 | for (;;) |
| 6428 | { |
| 6429 | if (scope == NULL) |
| 6430 | { |
| 6431 | emsg(_(e_continue)); |
| 6432 | return NULL; |
| 6433 | } |
| 6434 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6435 | break; |
| 6436 | scope = scope->se_outer; |
| 6437 | } |
| 6438 | |
| 6439 | // Jump back to the FOR or WHILE instruction. |
| 6440 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6441 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 6442 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6443 | return arg; |
| 6444 | } |
| 6445 | |
| 6446 | /* |
| 6447 | * compile "break" |
| 6448 | */ |
| 6449 | static char_u * |
| 6450 | compile_break(char_u *arg, cctx_T *cctx) |
| 6451 | { |
| 6452 | scope_T *scope = cctx->ctx_scope; |
| 6453 | endlabel_T **el; |
| 6454 | |
| 6455 | for (;;) |
| 6456 | { |
| 6457 | if (scope == NULL) |
| 6458 | { |
| 6459 | emsg(_(e_break)); |
| 6460 | return NULL; |
| 6461 | } |
| 6462 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6463 | break; |
| 6464 | scope = scope->se_outer; |
| 6465 | } |
| 6466 | |
| 6467 | // Jump to the end of the FOR or WHILE loop. |
| 6468 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6469 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6470 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6471 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6472 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 6473 | return FAIL; |
| 6474 | |
| 6475 | return arg; |
| 6476 | } |
| 6477 | |
| 6478 | /* |
| 6479 | * compile "{" start of block |
| 6480 | */ |
| 6481 | static char_u * |
| 6482 | compile_block(char_u *arg, cctx_T *cctx) |
| 6483 | { |
| 6484 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6485 | return NULL; |
| 6486 | return skipwhite(arg + 1); |
| 6487 | } |
| 6488 | |
| 6489 | /* |
| 6490 | * compile end of block: drop one scope |
| 6491 | */ |
| 6492 | static void |
| 6493 | compile_endblock(cctx_T *cctx) |
| 6494 | { |
| 6495 | scope_T *scope = cctx->ctx_scope; |
| 6496 | |
| 6497 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6498 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6499 | vim_free(scope); |
| 6500 | } |
| 6501 | |
| 6502 | /* |
| 6503 | * compile "try" |
| 6504 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6505 | * finally. |
| 6506 | * Creates another scope for the "try" block itself. |
| 6507 | * TRY instruction sets up exception handling at runtime. |
| 6508 | * |
| 6509 | * "try" |
| 6510 | * TRY -> catch1, -> finally push trystack entry |
| 6511 | * ... try block |
| 6512 | * "throw {exception}" |
| 6513 | * EVAL {exception} |
| 6514 | * THROW create exception |
| 6515 | * ... try block |
| 6516 | * " catch {expr}" |
| 6517 | * JUMP -> finally |
| 6518 | * catch1: PUSH exeception |
| 6519 | * EVAL {expr} |
| 6520 | * MATCH |
| 6521 | * JUMP nomatch -> catch2 |
| 6522 | * CATCH remove exception |
| 6523 | * ... catch block |
| 6524 | * " catch" |
| 6525 | * JUMP -> finally |
| 6526 | * catch2: CATCH remove exception |
| 6527 | * ... catch block |
| 6528 | * " finally" |
| 6529 | * finally: |
| 6530 | * ... finally block |
| 6531 | * " endtry" |
| 6532 | * ENDTRY pop trystack entry, may rethrow |
| 6533 | */ |
| 6534 | static char_u * |
| 6535 | compile_try(char_u *arg, cctx_T *cctx) |
| 6536 | { |
| 6537 | garray_T *instr = &cctx->ctx_instr; |
| 6538 | scope_T *try_scope; |
| 6539 | scope_T *scope; |
| 6540 | |
| 6541 | // scope that holds the jumps that go to catch/finally/endtry |
| 6542 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6543 | if (try_scope == NULL) |
| 6544 | return NULL; |
| 6545 | |
| 6546 | // "catch" is set when the first ":catch" is found. |
| 6547 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6548 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6549 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6550 | return NULL; |
| 6551 | |
| 6552 | // scope for the try block itself |
| 6553 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6554 | if (scope == NULL) |
| 6555 | return NULL; |
| 6556 | |
| 6557 | return arg; |
| 6558 | } |
| 6559 | |
| 6560 | /* |
| 6561 | * compile "catch {expr}" |
| 6562 | */ |
| 6563 | static char_u * |
| 6564 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6565 | { |
| 6566 | scope_T *scope = cctx->ctx_scope; |
| 6567 | garray_T *instr = &cctx->ctx_instr; |
| 6568 | char_u *p; |
| 6569 | isn_T *isn; |
| 6570 | |
| 6571 | // end block scope from :try or :catch |
| 6572 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6573 | compile_endblock(cctx); |
| 6574 | scope = cctx->ctx_scope; |
| 6575 | |
| 6576 | // Error if not in a :try scope |
| 6577 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6578 | { |
| 6579 | emsg(_(e_catch)); |
| 6580 | return NULL; |
| 6581 | } |
| 6582 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6583 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6584 | { |
| 6585 | emsg(_("E1033: catch unreachable after catch-all")); |
| 6586 | return NULL; |
| 6587 | } |
| 6588 | |
| 6589 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6590 | 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] | 6591 | JUMP_ALWAYS, cctx) == FAIL) |
| 6592 | return NULL; |
| 6593 | |
| 6594 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6595 | 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] | 6596 | if (isn->isn_arg.try.try_catch == 0) |
| 6597 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6598 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6599 | { |
| 6600 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6601 | 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] | 6602 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6603 | } |
| 6604 | |
| 6605 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6606 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6607 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6608 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6609 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6610 | } |
| 6611 | else |
| 6612 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6613 | char_u *end; |
| 6614 | char_u *pat; |
| 6615 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6616 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6617 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6618 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6619 | // Push v:exception, push {expr} and MATCH |
| 6620 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6621 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6622 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6623 | if (*end != *p) |
| 6624 | { |
| 6625 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 6626 | vim_free(tofree); |
| 6627 | return FAIL; |
| 6628 | } |
| 6629 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6630 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6631 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6632 | len = (int)(end - tofree); |
| 6633 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6634 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6635 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6636 | if (pat == NULL) |
| 6637 | return FAIL; |
| 6638 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6639 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6640 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6641 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6642 | return NULL; |
| 6643 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6644 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6645 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6646 | return NULL; |
| 6647 | } |
| 6648 | |
| 6649 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6650 | return NULL; |
| 6651 | |
| 6652 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6653 | return NULL; |
| 6654 | return p; |
| 6655 | } |
| 6656 | |
| 6657 | static char_u * |
| 6658 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6659 | { |
| 6660 | scope_T *scope = cctx->ctx_scope; |
| 6661 | garray_T *instr = &cctx->ctx_instr; |
| 6662 | isn_T *isn; |
| 6663 | |
| 6664 | // end block scope from :try or :catch |
| 6665 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6666 | compile_endblock(cctx); |
| 6667 | scope = cctx->ctx_scope; |
| 6668 | |
| 6669 | // Error if not in a :try scope |
| 6670 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6671 | { |
| 6672 | emsg(_(e_finally)); |
| 6673 | return NULL; |
| 6674 | } |
| 6675 | |
| 6676 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6677 | 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] | 6678 | if (isn->isn_arg.try.try_finally != 0) |
| 6679 | { |
| 6680 | emsg(_(e_finally_dup)); |
| 6681 | return NULL; |
| 6682 | } |
| 6683 | |
| 6684 | // 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] | 6685 | 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] | 6686 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6687 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6688 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6689 | { |
| 6690 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6691 | 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] | 6692 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6693 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6694 | } |
| 6695 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6696 | // TODO: set index in ts_finally_label jumps |
| 6697 | |
| 6698 | return arg; |
| 6699 | } |
| 6700 | |
| 6701 | static char_u * |
| 6702 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6703 | { |
| 6704 | scope_T *scope = cctx->ctx_scope; |
| 6705 | garray_T *instr = &cctx->ctx_instr; |
| 6706 | isn_T *isn; |
| 6707 | |
| 6708 | // end block scope from :catch or :finally |
| 6709 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6710 | compile_endblock(cctx); |
| 6711 | scope = cctx->ctx_scope; |
| 6712 | |
| 6713 | // Error if not in a :try scope |
| 6714 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6715 | { |
| 6716 | if (scope == NULL) |
| 6717 | emsg(_(e_no_endtry)); |
| 6718 | else if (scope->se_type == WHILE_SCOPE) |
| 6719 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6720 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6721 | emsg(_(e_endfor)); |
| 6722 | else |
| 6723 | emsg(_(e_endif)); |
| 6724 | return NULL; |
| 6725 | } |
| 6726 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6727 | 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] | 6728 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6729 | { |
| 6730 | emsg(_("E1032: missing :catch or :finally")); |
| 6731 | return NULL; |
| 6732 | } |
| 6733 | |
| 6734 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6735 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6736 | 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] | 6737 | |
| 6738 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6739 | if (isn->isn_arg.try.try_catch == 0) |
| 6740 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6741 | if (isn->isn_arg.try.try_finally == 0) |
| 6742 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6743 | |
| 6744 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6745 | { |
| 6746 | // Last catch without match jumps here |
| 6747 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6748 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6749 | } |
| 6750 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6751 | compile_endblock(cctx); |
| 6752 | |
| 6753 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6754 | return NULL; |
| 6755 | return arg; |
| 6756 | } |
| 6757 | |
| 6758 | /* |
| 6759 | * compile "throw {expr}" |
| 6760 | */ |
| 6761 | static char_u * |
| 6762 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6763 | { |
| 6764 | char_u *p = skipwhite(arg); |
| 6765 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6766 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6767 | return NULL; |
| 6768 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6769 | return NULL; |
| 6770 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6771 | return NULL; |
| 6772 | |
| 6773 | return p; |
| 6774 | } |
| 6775 | |
| 6776 | /* |
| 6777 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6778 | * compile "echomsg expr" |
| 6779 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6780 | * compile "execute expr" |
| 6781 | */ |
| 6782 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6783 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6784 | { |
| 6785 | char_u *p = arg; |
| 6786 | int count = 0; |
| 6787 | |
| 6788 | for (;;) |
| 6789 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6790 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6791 | return NULL; |
| 6792 | ++count; |
| 6793 | p = skipwhite(p); |
| 6794 | if (ends_excmd(*p)) |
| 6795 | break; |
| 6796 | } |
| 6797 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6798 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6799 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6800 | else if (cmdidx == CMD_execute) |
| 6801 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6802 | else if (cmdidx == CMD_echomsg) |
| 6803 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6804 | else |
| 6805 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6806 | return p; |
| 6807 | } |
| 6808 | |
| 6809 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6810 | * A command that is not compiled, execute with legacy code. |
| 6811 | */ |
| 6812 | static char_u * |
| 6813 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6814 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6815 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6816 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6817 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6818 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6819 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6820 | goto theend; |
| 6821 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6822 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6823 | { |
| 6824 | long argt = excmd_get_argt(eap->cmdidx); |
| 6825 | int usefilter = FALSE; |
| 6826 | |
| 6827 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6828 | |
| 6829 | // If the command can be followed by a bar, find the bar and truncate |
| 6830 | // it, so that the following command can be compiled. |
| 6831 | // The '|' is overwritten with a NUL, it is put back below. |
| 6832 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6833 | && *eap->arg == '!') |
| 6834 | // :w !filter or :r !filter or :r! filter |
| 6835 | usefilter = TRUE; |
| 6836 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6837 | { |
| 6838 | separate_nextcmd(eap); |
| 6839 | if (eap->nextcmd != NULL) |
| 6840 | nextcmd = eap->nextcmd; |
| 6841 | } |
| 6842 | } |
| 6843 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6844 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6845 | { |
| 6846 | // expand filename in "syntax include [@group] filename" |
| 6847 | has_expr = TRUE; |
| 6848 | eap->arg = skipwhite(eap->arg + 7); |
| 6849 | if (*eap->arg == '@') |
| 6850 | eap->arg = skiptowhite(eap->arg); |
| 6851 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6852 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6853 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6854 | { |
| 6855 | int count = 0; |
| 6856 | char_u *start = skipwhite(line); |
| 6857 | |
| 6858 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6859 | // PUSHS ":cmd xxx" |
| 6860 | // eval expr1 |
| 6861 | // PUSHS "yyy" |
| 6862 | // eval expr2 |
| 6863 | // PUSHS "zzz" |
| 6864 | // EXECCONCAT 5 |
| 6865 | for (;;) |
| 6866 | { |
| 6867 | if (p > start) |
| 6868 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6869 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6870 | ++count; |
| 6871 | } |
| 6872 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6873 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6874 | return NULL; |
| 6875 | may_generate_2STRING(-1, cctx); |
| 6876 | ++count; |
| 6877 | p = skipwhite(p); |
| 6878 | if (*p != '`') |
| 6879 | { |
| 6880 | emsg(_("E1083: missing backtick")); |
| 6881 | return NULL; |
| 6882 | } |
| 6883 | start = p + 1; |
| 6884 | |
| 6885 | p = (char_u *)strstr((char *)start, "`="); |
| 6886 | if (p == NULL) |
| 6887 | { |
| 6888 | if (*skipwhite(start) != NUL) |
| 6889 | { |
| 6890 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6891 | ++count; |
| 6892 | } |
| 6893 | break; |
| 6894 | } |
| 6895 | } |
| 6896 | generate_EXECCONCAT(cctx, count); |
| 6897 | } |
| 6898 | else |
| 6899 | generate_EXEC(cctx, line); |
| 6900 | |
| 6901 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6902 | if (*nextcmd != NUL) |
| 6903 | { |
| 6904 | // the parser expects a pointer to the bar, put it back |
| 6905 | --nextcmd; |
| 6906 | *nextcmd = '|'; |
| 6907 | } |
| 6908 | |
| 6909 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6910 | } |
| 6911 | |
| 6912 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6913 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6914 | * 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] | 6915 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6916 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6917 | add_def_function(ufunc_T *ufunc) |
| 6918 | { |
| 6919 | dfunc_T *dfunc; |
| 6920 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6921 | if (def_functions.ga_len == 0) |
| 6922 | { |
| 6923 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6924 | // wasn't set. |
| 6925 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6926 | return FAIL; |
| 6927 | ++def_functions.ga_len; |
| 6928 | } |
| 6929 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6930 | // Add the function to "def_functions". |
| 6931 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6932 | return FAIL; |
| 6933 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6934 | CLEAR_POINTER(dfunc); |
| 6935 | dfunc->df_idx = def_functions.ga_len; |
| 6936 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6937 | dfunc->df_ufunc = ufunc; |
| 6938 | ++def_functions.ga_len; |
| 6939 | return OK; |
| 6940 | } |
| 6941 | |
| 6942 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6943 | * After ex_function() has collected all the function lines: parse and compile |
| 6944 | * the lines into instructions. |
| 6945 | * Adds the function to "def_functions". |
| 6946 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6947 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6948 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6949 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6950 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6951 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6952 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6953 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6954 | 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] | 6955 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6956 | char_u *line = NULL; |
| 6957 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6958 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6959 | cctx_T cctx; |
| 6960 | garray_T *instr; |
| 6961 | int called_emsg_before = called_emsg; |
| 6962 | int ret = FAIL; |
| 6963 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6964 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6965 | int emsg_before = called_emsg; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6966 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6967 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6968 | // When using a function that was compiled before: Free old instructions. |
| 6969 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6970 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6971 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6972 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6973 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6974 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6975 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6976 | else |
| 6977 | { |
| 6978 | if (add_def_function(ufunc) == FAIL) |
| 6979 | return FAIL; |
| 6980 | new_def_function = TRUE; |
| 6981 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6982 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6983 | ufunc->uf_def_status = UF_COMPILING; |
| 6984 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6985 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6986 | cctx.ctx_ufunc = ufunc; |
| 6987 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6988 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6989 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6990 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6991 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6992 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6993 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6994 | instr = &cctx.ctx_instr; |
| 6995 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6996 | // Set the context to the function, it may be compiled when called from |
| 6997 | // another script. Set the script version to the most modern one. |
| 6998 | // The line number will be set in next_line_from_context(). |
| 6999 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7000 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 7001 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7002 | // Make sure error messages are OK. |
| 7003 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 7004 | if (do_estack_push) |
| 7005 | estack_push_ufunc(ufunc, 1); |
| 7006 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7007 | if (ufunc->uf_def_args.ga_len > 0) |
| 7008 | { |
| 7009 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7010 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7011 | int i; |
| 7012 | char_u *arg; |
| 7013 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7014 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7015 | |
| 7016 | // Produce instructions for the default values of optional arguments. |
| 7017 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 7018 | // where to start when the function is called, depending on the number |
| 7019 | // of arguments. |
| 7020 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 7021 | if (ufunc->uf_def_arg_idx == NULL) |
| 7022 | goto erret; |
| 7023 | for (i = 0; i < count; ++i) |
| 7024 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7025 | garray_T *stack = &cctx.ctx_type_stack; |
| 7026 | type_T *val_type; |
| 7027 | int arg_idx = first_def_arg + i; |
| 7028 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7029 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 7030 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7031 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7032 | goto erret; |
| 7033 | |
| 7034 | // If no type specified use the type of the default value. |
| 7035 | // Otherwise check that the default value type matches the |
| 7036 | // specified type. |
| 7037 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 7038 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7039 | { |
| 7040 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7041 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7042 | } |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 7043 | 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] | 7044 | == FAIL) |
| 7045 | { |
| 7046 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 7047 | arg_idx + 1); |
| 7048 | goto erret; |
| 7049 | } |
| 7050 | |
| 7051 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7052 | goto erret; |
| 7053 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7054 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7055 | |
| 7056 | if (did_set_arg_type) |
| 7057 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7058 | } |
| 7059 | |
| 7060 | /* |
| 7061 | * Loop over all the lines of the function and generate instructions. |
| 7062 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7063 | for (;;) |
| 7064 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7065 | exarg_T ea; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7066 | int starts_with_colon = FALSE; |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7067 | char_u *cmd; |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7068 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7069 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7070 | // Bail out on the first error to avoid a flood of errors and report |
| 7071 | // the right line number when inside try/catch. |
| 7072 | if (emsg_before != called_emsg) |
| 7073 | goto erret; |
| 7074 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7075 | if (line != NULL && *line == '|') |
| 7076 | // the line continues after a '|' |
| 7077 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 7078 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7079 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 7080 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7081 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 7082 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7083 | goto erret; |
| 7084 | } |
| 7085 | else |
| 7086 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 7087 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 7088 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7089 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7090 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7091 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7092 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7093 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7094 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7095 | ea.cmdlinep = &line; |
| 7096 | ea.cmd = skipwhite(line); |
| 7097 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7098 | // Some things can be recognized by the first character. |
| 7099 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7100 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7101 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7102 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7103 | if (ea.cmd[1] != '{') |
| 7104 | { |
| 7105 | line = (char_u *)""; |
| 7106 | continue; |
| 7107 | } |
| 7108 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7109 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7110 | case '}': |
| 7111 | { |
| 7112 | // "}" ends a block scope |
| 7113 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7114 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7115 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7116 | if (stype == BLOCK_SCOPE) |
| 7117 | { |
| 7118 | compile_endblock(&cctx); |
| 7119 | line = ea.cmd; |
| 7120 | } |
| 7121 | else |
| 7122 | { |
| 7123 | emsg(_("E1025: using } outside of a block scope")); |
| 7124 | goto erret; |
| 7125 | } |
| 7126 | if (line != NULL) |
| 7127 | line = skipwhite(ea.cmd + 1); |
| 7128 | continue; |
| 7129 | } |
| 7130 | |
| 7131 | case '{': |
| 7132 | // "{" starts a block scope |
| 7133 | // "{'a': 1}->func() is something else |
| 7134 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7135 | { |
| 7136 | line = compile_block(ea.cmd, &cctx); |
| 7137 | continue; |
| 7138 | } |
| 7139 | break; |
| 7140 | |
| 7141 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7142 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7143 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7144 | } |
| 7145 | |
| 7146 | /* |
| 7147 | * COMMAND MODIFIERS |
| 7148 | */ |
| 7149 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 7150 | { |
| 7151 | if (errormsg != NULL) |
| 7152 | goto erret; |
| 7153 | // empty line or comment |
| 7154 | line = (char_u *)""; |
| 7155 | continue; |
| 7156 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7157 | // TODO: use modifiers in the command |
| 7158 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | b074e8b | 2020-07-11 13:40:45 +0200 | [diff] [blame] | 7159 | CLEAR_FIELD(cmdmod); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7160 | |
| 7161 | // Skip ":call" to get to the function name. |
| 7162 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 7163 | ea.cmd = skipwhite(ea.cmd); |
| 7164 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7165 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7166 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7167 | char_u *pskip; |
| 7168 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7169 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7170 | // find what follows. |
| 7171 | // Skip over "var.member", "var[idx]" and the like. |
| 7172 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 7173 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7174 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7175 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7176 | if (p > ea.cmd && *p != NUL) |
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 *var_end; |
| 7179 | int oplen; |
| 7180 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7181 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7182 | var_end = find_name_end(pskip, NULL, NULL, |
| 7183 | FNE_CHECK_START | FNE_INCL_BR); |
| 7184 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7185 | if (oplen > 0) |
| 7186 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7187 | size_t len = p - ea.cmd; |
| 7188 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7189 | // Recognize an assignment if we recognize the variable |
| 7190 | // name: |
| 7191 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 7192 | // "local = expr" where "local" is a local var. |
| 7193 | // "script = expr" where "script" is a script-local var. |
| 7194 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7195 | // "&opt = expr" |
| 7196 | // "$ENV = expr" |
| 7197 | // "@r = expr" |
| 7198 | if (*ea.cmd == '&' |
| 7199 | || *ea.cmd == '$' |
| 7200 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7201 | || ((len) > 2 && ea.cmd[1] == ':') |
| 7202 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 7203 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 7204 | NULL, &cctx) == OK |
| 7205 | || lookup_script(ea.cmd, len) == OK |
| 7206 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7207 | { |
| 7208 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7209 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7210 | goto erret; |
| 7211 | continue; |
| 7212 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7213 | } |
| 7214 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7215 | |
| 7216 | if (*ea.cmd == '[') |
| 7217 | { |
| 7218 | // [var, var] = expr |
| 7219 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 7220 | if (line == NULL) |
| 7221 | goto erret; |
| 7222 | if (line != ea.cmd) |
| 7223 | continue; |
| 7224 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7225 | } |
| 7226 | |
| 7227 | /* |
| 7228 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7229 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7230 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7231 | cmd = ea.cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7232 | if (*cmd != '\'') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7233 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7234 | ea.cmd = skip_range(ea.cmd, NULL); |
| 7235 | if (ea.cmd > cmd && !starts_with_colon) |
| 7236 | { |
| 7237 | emsg(_(e_colon_required)); |
| 7238 | goto erret; |
| 7239 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7240 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7241 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7242 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7243 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7244 | |
| 7245 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 7246 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7247 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7248 | { |
| 7249 | line += STRLEN(line); |
| 7250 | continue; |
| 7251 | } |
| 7252 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7253 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7254 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7255 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7256 | // CMD_let cannot happen, compile_assignment() above is used |
| 7257 | iemsg("Command from find_ex_command() not handled"); |
| 7258 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7259 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7260 | } |
| 7261 | |
| 7262 | p = skipwhite(p); |
| 7263 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7264 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 7265 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7266 | && ea.cmdidx != CMD_elseif |
| 7267 | && ea.cmdidx != CMD_else |
| 7268 | && ea.cmdidx != CMD_endif) |
| 7269 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7270 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7271 | continue; |
| 7272 | } |
| 7273 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7274 | if (ea.cmdidx != CMD_elseif |
| 7275 | && ea.cmdidx != CMD_else |
| 7276 | && ea.cmdidx != CMD_endif |
| 7277 | && ea.cmdidx != CMD_endfor |
| 7278 | && ea.cmdidx != CMD_endwhile |
| 7279 | && ea.cmdidx != CMD_catch |
| 7280 | && ea.cmdidx != CMD_finally |
| 7281 | && ea.cmdidx != CMD_endtry) |
| 7282 | { |
| 7283 | if (cctx.ctx_had_return) |
| 7284 | { |
| 7285 | emsg(_("E1095: Unreachable code after :return")); |
| 7286 | goto erret; |
| 7287 | } |
| 7288 | } |
| 7289 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7290 | switch (ea.cmdidx) |
| 7291 | { |
| 7292 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7293 | ea.arg = p; |
| 7294 | line = compile_nested_function(&ea, &cctx); |
| 7295 | break; |
| 7296 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7297 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7298 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7299 | goto erret; |
| 7300 | |
| 7301 | case CMD_return: |
| 7302 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7303 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7304 | break; |
| 7305 | |
| 7306 | case CMD_let: |
| 7307 | case CMD_const: |
| 7308 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7309 | if (line == p) |
| 7310 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7311 | break; |
| 7312 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7313 | case CMD_unlet: |
| 7314 | case CMD_unlockvar: |
| 7315 | case CMD_lockvar: |
| 7316 | line = compile_unletlock(p, &ea, &cctx); |
| 7317 | break; |
| 7318 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7319 | case CMD_import: |
| 7320 | line = compile_import(p, &cctx); |
| 7321 | break; |
| 7322 | |
| 7323 | case CMD_if: |
| 7324 | line = compile_if(p, &cctx); |
| 7325 | break; |
| 7326 | case CMD_elseif: |
| 7327 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7328 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7329 | break; |
| 7330 | case CMD_else: |
| 7331 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7332 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7333 | break; |
| 7334 | case CMD_endif: |
| 7335 | line = compile_endif(p, &cctx); |
| 7336 | break; |
| 7337 | |
| 7338 | case CMD_while: |
| 7339 | line = compile_while(p, &cctx); |
| 7340 | break; |
| 7341 | case CMD_endwhile: |
| 7342 | line = compile_endwhile(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 | |
| 7346 | case CMD_for: |
| 7347 | line = compile_for(p, &cctx); |
| 7348 | break; |
| 7349 | case CMD_endfor: |
| 7350 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7351 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7352 | break; |
| 7353 | case CMD_continue: |
| 7354 | line = compile_continue(p, &cctx); |
| 7355 | break; |
| 7356 | case CMD_break: |
| 7357 | line = compile_break(p, &cctx); |
| 7358 | break; |
| 7359 | |
| 7360 | case CMD_try: |
| 7361 | line = compile_try(p, &cctx); |
| 7362 | break; |
| 7363 | case CMD_catch: |
| 7364 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7365 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7366 | break; |
| 7367 | case CMD_finally: |
| 7368 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7369 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7370 | break; |
| 7371 | case CMD_endtry: |
| 7372 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7373 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7374 | break; |
| 7375 | case CMD_throw: |
| 7376 | line = compile_throw(p, &cctx); |
| 7377 | break; |
| 7378 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7379 | case CMD_eval: |
| 7380 | if (compile_expr0(&p, &cctx) == FAIL) |
| 7381 | goto erret; |
| 7382 | |
| 7383 | // drop the return value |
| 7384 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 7385 | |
| 7386 | line = skipwhite(p); |
| 7387 | break; |
| 7388 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7389 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7390 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7391 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7392 | case CMD_echomsg: |
| 7393 | case CMD_echoerr: |
| 7394 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7395 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7396 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7397 | // TODO: other commands with an expression argument |
| 7398 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7399 | case CMD_SIZE: |
| 7400 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 7401 | goto erret; |
| 7402 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7403 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7404 | // Not recognized, execute with do_cmdline_cmd(). |
| 7405 | ea.arg = p; |
| 7406 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7407 | break; |
| 7408 | } |
| 7409 | if (line == NULL) |
| 7410 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7411 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7412 | |
| 7413 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7414 | { |
| 7415 | iemsg("Type stack underflow"); |
| 7416 | goto erret; |
| 7417 | } |
| 7418 | } |
| 7419 | |
| 7420 | if (cctx.ctx_scope != NULL) |
| 7421 | { |
| 7422 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7423 | emsg(_(e_endif)); |
| 7424 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7425 | emsg(_(e_endwhile)); |
| 7426 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7427 | emsg(_(e_endfor)); |
| 7428 | else |
| 7429 | emsg(_("E1026: Missing }")); |
| 7430 | goto erret; |
| 7431 | } |
| 7432 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7433 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7434 | { |
| 7435 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7436 | { |
| 7437 | emsg(_("E1027: Missing return statement")); |
| 7438 | goto erret; |
| 7439 | } |
| 7440 | |
| 7441 | // Return zero if there is no return at the end. |
| 7442 | generate_PUSHNR(&cctx, 0); |
| 7443 | generate_instr(&cctx, ISN_RETURN); |
| 7444 | } |
| 7445 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7446 | { |
| 7447 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7448 | + ufunc->uf_dfunc_idx; |
| 7449 | dfunc->df_deleted = FALSE; |
| 7450 | dfunc->df_instr = instr->ga_data; |
| 7451 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7452 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 7453 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7454 | if (cctx.ctx_outer_used) |
| 7455 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7456 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7457 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7458 | |
| 7459 | ret = OK; |
| 7460 | |
| 7461 | erret: |
| 7462 | if (ret == FAIL) |
| 7463 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7464 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7465 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7466 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7467 | |
| 7468 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7469 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7470 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7471 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7472 | // If using the last entry in the table and it was added above, we |
| 7473 | // might as well remove it. |
| 7474 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7475 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7476 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7477 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7478 | ufunc->uf_dfunc_idx = 0; |
| 7479 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7480 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7481 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7482 | while (cctx.ctx_scope != NULL) |
| 7483 | drop_scope(&cctx); |
| 7484 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7485 | // Don't execute this function body. |
| 7486 | ga_clear_strings(&ufunc->uf_lines); |
| 7487 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7488 | if (errormsg != NULL) |
| 7489 | emsg(errormsg); |
| 7490 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 7491 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7492 | } |
| 7493 | |
| 7494 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7495 | if (do_estack_push) |
| 7496 | estack_pop(); |
| 7497 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7498 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7499 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7500 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7501 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7502 | } |
| 7503 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7504 | void |
| 7505 | set_function_type(ufunc_T *ufunc) |
| 7506 | { |
| 7507 | int varargs = ufunc->uf_va_name != NULL; |
| 7508 | int argcount = ufunc->uf_args.ga_len; |
| 7509 | |
| 7510 | // Create a type for the function, with the return type and any |
| 7511 | // argument types. |
| 7512 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7513 | // The type is included in "tt_args". |
| 7514 | if (argcount > 0 || varargs) |
| 7515 | { |
| 7516 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7517 | argcount, &ufunc->uf_type_list); |
| 7518 | // Add argument types to the function type. |
| 7519 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7520 | argcount + varargs, |
| 7521 | &ufunc->uf_type_list) == FAIL) |
| 7522 | return; |
| 7523 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7524 | ufunc->uf_func_type->tt_min_argcount = |
| 7525 | argcount - ufunc->uf_def_args.ga_len; |
| 7526 | if (ufunc->uf_arg_types == NULL) |
| 7527 | { |
| 7528 | int i; |
| 7529 | |
| 7530 | // lambda does not have argument types. |
| 7531 | for (i = 0; i < argcount; ++i) |
| 7532 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7533 | } |
| 7534 | else |
| 7535 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7536 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7537 | if (varargs) |
| 7538 | { |
| 7539 | ufunc->uf_func_type->tt_args[argcount] = |
| 7540 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7541 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7542 | } |
| 7543 | } |
| 7544 | else |
| 7545 | // No arguments, can use a predefined type. |
| 7546 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7547 | argcount, &ufunc->uf_type_list); |
| 7548 | } |
| 7549 | |
| 7550 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7551 | /* |
| 7552 | * Delete an instruction, free what it contains. |
| 7553 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7554 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7555 | delete_instr(isn_T *isn) |
| 7556 | { |
| 7557 | switch (isn->isn_type) |
| 7558 | { |
| 7559 | case ISN_EXEC: |
| 7560 | case ISN_LOADENV: |
| 7561 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7562 | case ISN_LOADB: |
| 7563 | case ISN_LOADW: |
| 7564 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7565 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7566 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7567 | case ISN_PUSHEXC: |
| 7568 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7569 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7570 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7571 | case ISN_STOREB: |
| 7572 | case ISN_STOREW: |
| 7573 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7574 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7575 | vim_free(isn->isn_arg.string); |
| 7576 | break; |
| 7577 | |
| 7578 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7579 | case ISN_STORES: |
| 7580 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7581 | break; |
| 7582 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7583 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7584 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7585 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7586 | break; |
| 7587 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7588 | case ISN_STOREOPT: |
| 7589 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7590 | break; |
| 7591 | |
| 7592 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7593 | blob_unref(isn->isn_arg.blob); |
| 7594 | break; |
| 7595 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7596 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7597 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7598 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7599 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7600 | break; |
| 7601 | |
| 7602 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7603 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7604 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7605 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7606 | break; |
| 7607 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7608 | case ISN_UCALL: |
| 7609 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7610 | break; |
| 7611 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7612 | case ISN_FUNCREF: |
| 7613 | { |
| 7614 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7615 | + isn->isn_arg.funcref.fr_func; |
| 7616 | func_ptr_unref(dfunc->df_ufunc); |
| 7617 | } |
| 7618 | break; |
| 7619 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7620 | case ISN_2BOOL: |
| 7621 | case ISN_2STRING: |
| 7622 | case ISN_ADDBLOB: |
| 7623 | case ISN_ADDLIST: |
| 7624 | case ISN_BCALL: |
| 7625 | case ISN_CATCH: |
| 7626 | case ISN_CHECKNR: |
| 7627 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7628 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7629 | case ISN_COMPAREANY: |
| 7630 | case ISN_COMPAREBLOB: |
| 7631 | case ISN_COMPAREBOOL: |
| 7632 | case ISN_COMPAREDICT: |
| 7633 | case ISN_COMPAREFLOAT: |
| 7634 | case ISN_COMPAREFUNC: |
| 7635 | case ISN_COMPARELIST: |
| 7636 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7637 | case ISN_COMPARESPECIAL: |
| 7638 | case ISN_COMPARESTRING: |
| 7639 | case ISN_CONCAT: |
| 7640 | case ISN_DCALL: |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 7641 | case ISN_SHUFFLE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7642 | case ISN_DROP: |
| 7643 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7644 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7645 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7646 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7647 | case ISN_EXECCONCAT: |
| 7648 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7649 | case ISN_FOR: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7650 | case ISN_LISTINDEX: |
| 7651 | case ISN_STRINDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7652 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7653 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7654 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7655 | case ISN_JUMP: |
| 7656 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7657 | case ISN_LOADBDICT: |
| 7658 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7659 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7660 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7661 | case ISN_LOADSCRIPT: |
| 7662 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7663 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7664 | case ISN_LOADWDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7665 | case ISN_NEGATENR: |
| 7666 | case ISN_NEWDICT: |
| 7667 | case ISN_NEWLIST: |
| 7668 | case ISN_OPNR: |
| 7669 | case ISN_OPFLOAT: |
| 7670 | case ISN_OPANY: |
| 7671 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7672 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7673 | case ISN_PUSHF: |
| 7674 | case ISN_PUSHNR: |
| 7675 | case ISN_PUSHBOOL: |
| 7676 | case ISN_PUSHSPEC: |
| 7677 | case ISN_RETURN: |
| 7678 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 7679 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7680 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7681 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7682 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7683 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7684 | case ISN_STOREDICT: |
| 7685 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7686 | case ISN_THROW: |
| 7687 | case ISN_TRY: |
| 7688 | // nothing allocated |
| 7689 | break; |
| 7690 | } |
| 7691 | } |
| 7692 | |
| 7693 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7694 | * Free all instructions for "dfunc". |
| 7695 | */ |
| 7696 | static void |
| 7697 | delete_def_function_contents(dfunc_T *dfunc) |
| 7698 | { |
| 7699 | int idx; |
| 7700 | |
| 7701 | ga_clear(&dfunc->df_def_args_isn); |
| 7702 | |
| 7703 | if (dfunc->df_instr != NULL) |
| 7704 | { |
| 7705 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7706 | delete_instr(dfunc->df_instr + idx); |
| 7707 | VIM_CLEAR(dfunc->df_instr); |
| 7708 | } |
| 7709 | |
| 7710 | dfunc->df_deleted = TRUE; |
| 7711 | } |
| 7712 | |
| 7713 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7714 | * When a user function is deleted, clear the contents of any associated def |
| 7715 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7716 | */ |
| 7717 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7718 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7719 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7720 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7721 | { |
| 7722 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7723 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7724 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7725 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7726 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7727 | } |
| 7728 | } |
| 7729 | |
| 7730 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7731 | /* |
| 7732 | * Free all functions defined with ":def". |
| 7733 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7734 | void |
| 7735 | free_def_functions(void) |
| 7736 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7737 | int idx; |
| 7738 | |
| 7739 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7740 | { |
| 7741 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7742 | |
| 7743 | delete_def_function_contents(dfunc); |
| 7744 | } |
| 7745 | |
| 7746 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7747 | } |
| 7748 | #endif |
| 7749 | |
| 7750 | |
| 7751 | #endif // FEAT_EVAL |