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 | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 150 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 151 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 152 | 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] | 153 | |
| 154 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 155 | * Lookup variable "name" in the local scope and return it. |
| 156 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 157 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 158 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 159 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 160 | { |
| 161 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 162 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 163 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 164 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 165 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 166 | |
| 167 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 168 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 169 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 170 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 171 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 172 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 173 | { |
| 174 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 175 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 176 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 177 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 178 | |
| 179 | // Find local in outer function scope. |
| 180 | if (cctx->ctx_outer != NULL) |
| 181 | { |
| 182 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 183 | if (lvar != NULL) |
| 184 | { |
| 185 | // TODO: are there situations we should not mark the outer scope as |
| 186 | // used? |
| 187 | cctx->ctx_outer_used = TRUE; |
| 188 | lvar->lv_from_outer = TRUE; |
| 189 | return lvar; |
| 190 | } |
| 191 | } |
| 192 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 193 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 197 | * Lookup an argument in the current function and an enclosing function. |
| 198 | * Returns the argument index in "idxp" |
| 199 | * Returns the argument type in "type" |
| 200 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 201 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 202 | */ |
| 203 | static int |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 204 | lookup_arg( |
| 205 | char_u *name, |
| 206 | size_t len, |
| 207 | int *idxp, |
| 208 | type_T **type, |
| 209 | int *gen_load_outer, |
| 210 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 211 | { |
| 212 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 213 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 214 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 215 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 216 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 217 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 218 | { |
| 219 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 220 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 221 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 222 | { |
| 223 | if (idxp != NULL) |
| 224 | { |
| 225 | // Arguments are located above the frame pointer. One further |
| 226 | // if there is a vararg argument |
| 227 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 228 | + STACK_FRAME_SIZE) |
| 229 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 230 | |
| 231 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 232 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 233 | else |
| 234 | *type = &t_any; |
| 235 | } |
| 236 | return OK; |
| 237 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 238 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 239 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 240 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 241 | if (va_name != NULL |
| 242 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 243 | { |
| 244 | if (idxp != NULL) |
| 245 | { |
| 246 | // varargs is always the last argument |
| 247 | *idxp = -STACK_FRAME_SIZE - 1; |
| 248 | *type = cctx->ctx_ufunc->uf_va_type; |
| 249 | } |
| 250 | return OK; |
| 251 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 252 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 253 | if (cctx->ctx_outer != NULL) |
| 254 | { |
| 255 | // Lookup the name for an argument of the outer function. |
| 256 | if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
| 257 | == OK) |
| 258 | { |
| 259 | *gen_load_outer = TRUE; |
| 260 | return OK; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Lookup a variable in the current script. |
| 269 | * Returns OK or FAIL. |
| 270 | */ |
| 271 | static int |
| 272 | lookup_script(char_u *name, size_t len) |
| 273 | { |
| 274 | int cc; |
| 275 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 276 | dictitem_T *di; |
| 277 | |
| 278 | cc = name[len]; |
| 279 | name[len] = NUL; |
| 280 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 281 | name[len] = cc; |
| 282 | return di == NULL ? FAIL: OK; |
| 283 | } |
| 284 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 285 | /* |
| 286 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 287 | * compilation context "cctx". |
| 288 | * Return FAIL and give an error if it defined. |
| 289 | */ |
| 290 | int |
Bram Moolenaar | cbb6bdc | 2020-07-06 21:53:17 +0200 | [diff] [blame] | 291 | check_defined(char_u *p, size_t len, cctx_T *cctx) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 292 | { |
| 293 | if (lookup_script(p, len) == OK |
| 294 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 295 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 296 | || find_imported(p, len, cctx) != NULL))) |
| 297 | { |
| 298 | semsg("E1073: imported name already defined: %s", p); |
| 299 | return FAIL; |
| 300 | } |
| 301 | return OK; |
| 302 | } |
| 303 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 304 | /* |
| 305 | * Allocate memory for a type_T and add the pointer to type_gap, so that it can |
| 306 | * be freed later. |
| 307 | */ |
| 308 | static type_T * |
| 309 | alloc_type(garray_T *type_gap) |
| 310 | { |
| 311 | type_T *type; |
| 312 | |
| 313 | if (ga_grow(type_gap, 1) == FAIL) |
| 314 | return NULL; |
| 315 | type = ALLOC_CLEAR_ONE(type_T); |
| 316 | if (type != NULL) |
| 317 | { |
| 318 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = type; |
| 319 | ++type_gap->ga_len; |
| 320 | } |
| 321 | return type; |
| 322 | } |
| 323 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 324 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 325 | get_list_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 326 | { |
| 327 | type_T *type; |
| 328 | |
| 329 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 330 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 331 | return &t_list_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 332 | if (member_type->tt_type == VAR_VOID |
| 333 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 334 | return &t_list_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 335 | if (member_type->tt_type == VAR_BOOL) |
| 336 | return &t_list_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 337 | if (member_type->tt_type == VAR_NUMBER) |
| 338 | return &t_list_number; |
| 339 | if (member_type->tt_type == VAR_STRING) |
| 340 | return &t_list_string; |
| 341 | |
| 342 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 343 | type = alloc_type(type_gap); |
| 344 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 345 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 346 | type->tt_type = VAR_LIST; |
| 347 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 348 | type->tt_argcount = 0; |
| 349 | type->tt_args = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 350 | return type; |
| 351 | } |
| 352 | |
| 353 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 354 | get_dict_type(type_T *member_type, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 355 | { |
| 356 | type_T *type; |
| 357 | |
| 358 | // recognize commonly used types |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 359 | if (member_type->tt_type == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 360 | return &t_dict_any; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 361 | if (member_type->tt_type == VAR_VOID |
| 362 | || member_type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 363 | return &t_dict_empty; |
Bram Moolenaar | 0c2ca58 | 2020-02-25 22:58:29 +0100 | [diff] [blame] | 364 | if (member_type->tt_type == VAR_BOOL) |
| 365 | return &t_dict_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 366 | if (member_type->tt_type == VAR_NUMBER) |
| 367 | return &t_dict_number; |
| 368 | if (member_type->tt_type == VAR_STRING) |
| 369 | return &t_dict_string; |
| 370 | |
| 371 | // Not a common type, create a new entry. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 372 | type = alloc_type(type_gap); |
| 373 | if (type == NULL) |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 374 | return &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 375 | type->tt_type = VAR_DICT; |
| 376 | type->tt_member = member_type; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 377 | type->tt_argcount = 0; |
| 378 | type->tt_args = NULL; |
| 379 | return type; |
| 380 | } |
| 381 | |
| 382 | /* |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 383 | * Allocate a new type for a function. |
| 384 | */ |
| 385 | static type_T * |
| 386 | alloc_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 387 | { |
| 388 | type_T *type = alloc_type(type_gap); |
| 389 | |
| 390 | if (type == NULL) |
| 391 | return &t_any; |
| 392 | type->tt_type = VAR_FUNC; |
| 393 | type->tt_member = ret_type; |
| 394 | type->tt_argcount = argcount; |
| 395 | type->tt_args = NULL; |
| 396 | return type; |
| 397 | } |
| 398 | |
| 399 | /* |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 400 | * Get a function type, based on the return type "ret_type". |
| 401 | * If "argcount" is -1 or 0 a predefined type can be used. |
| 402 | * If "argcount" > 0 always create a new type, so that arguments can be added. |
| 403 | */ |
| 404 | static type_T * |
| 405 | get_func_type(type_T *ret_type, int argcount, garray_T *type_gap) |
| 406 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 407 | // recognize commonly used types |
| 408 | if (argcount <= 0) |
| 409 | { |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 410 | if (ret_type == &t_unknown) |
| 411 | { |
| 412 | // (argcount == 0) is not possible |
| 413 | return &t_func_unknown; |
| 414 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 415 | if (ret_type == &t_void) |
| 416 | { |
| 417 | if (argcount == 0) |
| 418 | return &t_func_0_void; |
| 419 | else |
| 420 | return &t_func_void; |
| 421 | } |
| 422 | if (ret_type == &t_any) |
| 423 | { |
| 424 | if (argcount == 0) |
| 425 | return &t_func_0_any; |
| 426 | else |
| 427 | return &t_func_any; |
| 428 | } |
| 429 | if (ret_type == &t_number) |
| 430 | { |
| 431 | if (argcount == 0) |
| 432 | return &t_func_0_number; |
| 433 | else |
| 434 | return &t_func_number; |
| 435 | } |
| 436 | if (ret_type == &t_string) |
| 437 | { |
| 438 | if (argcount == 0) |
| 439 | return &t_func_0_string; |
| 440 | else |
| 441 | return &t_func_string; |
| 442 | } |
| 443 | } |
| 444 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 445 | return alloc_func_type(ret_type, argcount, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 448 | /* |
Bram Moolenaar | 5d905c2 | 2020-04-05 18:20:45 +0200 | [diff] [blame] | 449 | * For a function type, reserve space for "argcount" argument types (including |
| 450 | * vararg). |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 451 | */ |
| 452 | static int |
| 453 | func_type_add_arg_types( |
| 454 | type_T *functype, |
| 455 | int argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 456 | garray_T *type_gap) |
| 457 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 458 | // To make it easy to free the space needed for the argument types, add the |
| 459 | // pointer to type_gap. |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 460 | if (ga_grow(type_gap, 1) == FAIL) |
| 461 | return FAIL; |
| 462 | functype->tt_args = ALLOC_CLEAR_MULT(type_T *, argcount); |
| 463 | if (functype->tt_args == NULL) |
| 464 | return FAIL; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 465 | ((type_T **)type_gap->ga_data)[type_gap->ga_len] = |
| 466 | (void *)functype->tt_args; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 467 | ++type_gap->ga_len; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 468 | return OK; |
| 469 | } |
| 470 | |
| 471 | /* |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 472 | * Return the type_T for a typval. Only for primitive types. |
| 473 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 474 | type_T * |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 475 | typval2type(typval_T *tv) |
| 476 | { |
| 477 | if (tv->v_type == VAR_NUMBER) |
| 478 | return &t_number; |
| 479 | if (tv->v_type == VAR_BOOL) |
Bram Moolenaar | 9c8bb7c | 2020-04-09 21:08:09 +0200 | [diff] [blame] | 480 | return &t_bool; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 481 | if (tv->v_type == VAR_STRING) |
| 482 | return &t_string; |
| 483 | if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles |
| 484 | return &t_list_string; |
| 485 | if (tv->v_type == VAR_DICT) // e.g. for v:completed_item |
| 486 | return &t_dict_any; |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 487 | return &t_any; // not used |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 490 | static void |
| 491 | type_mismatch(type_T *expected, type_T *actual) |
| 492 | { |
| 493 | char *tofree1, *tofree2; |
| 494 | |
| 495 | semsg(_("E1013: type mismatch, expected %s but got %s"), |
| 496 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 497 | vim_free(tofree1); |
| 498 | vim_free(tofree2); |
| 499 | } |
| 500 | |
| 501 | static void |
| 502 | arg_type_mismatch(type_T *expected, type_T *actual, int argidx) |
| 503 | { |
| 504 | char *tofree1, *tofree2; |
| 505 | |
| 506 | semsg(_("E1013: argument %d: type mismatch, expected %s but got %s"), |
| 507 | argidx, |
| 508 | type_name(expected, &tofree1), type_name(actual, &tofree2)); |
| 509 | vim_free(tofree1); |
| 510 | vim_free(tofree2); |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Check if the expected and actual types match. |
| 515 | * Does not allow for assigning "any" to a specific type. |
| 516 | */ |
Bram Moolenaar | 34db91f | 2020-06-13 19:00:10 +0200 | [diff] [blame] | 517 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 518 | check_type(type_T *expected, type_T *actual, int give_msg) |
| 519 | { |
| 520 | int ret = OK; |
| 521 | |
| 522 | // When expected is "unknown" we accept any actual type. |
| 523 | // When expected is "any" we accept any actual type except "void". |
| 524 | if (expected->tt_type != VAR_UNKNOWN |
| 525 | && !(expected->tt_type == VAR_ANY && actual->tt_type != VAR_VOID)) |
| 526 | |
| 527 | { |
| 528 | if (expected->tt_type != actual->tt_type) |
| 529 | { |
| 530 | if (give_msg) |
| 531 | type_mismatch(expected, actual); |
| 532 | return FAIL; |
| 533 | } |
| 534 | if (expected->tt_type == VAR_DICT || expected->tt_type == VAR_LIST) |
| 535 | { |
| 536 | // "unknown" is used for an empty list or dict |
| 537 | if (actual->tt_member != &t_unknown) |
| 538 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 539 | } |
| 540 | else if (expected->tt_type == VAR_FUNC) |
| 541 | { |
| 542 | if (expected->tt_member != &t_unknown) |
| 543 | ret = check_type(expected->tt_member, actual->tt_member, FALSE); |
| 544 | if (ret == OK && expected->tt_argcount != -1 |
| 545 | && (actual->tt_argcount < expected->tt_min_argcount |
| 546 | || actual->tt_argcount > expected->tt_argcount)) |
| 547 | ret = FAIL; |
| 548 | } |
| 549 | if (ret == FAIL && give_msg) |
| 550 | type_mismatch(expected, actual); |
| 551 | } |
| 552 | return ret; |
| 553 | } |
| 554 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 555 | ///////////////////////////////////////////////////////////////////// |
| 556 | // Following generate_ functions expect the caller to call ga_grow(). |
| 557 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 558 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 559 | #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] | 560 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 561 | /* |
| 562 | * Generate an instruction without arguments. |
| 563 | * Returns a pointer to the new instruction, NULL if failed. |
| 564 | */ |
| 565 | static isn_T * |
| 566 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 567 | { |
| 568 | garray_T *instr = &cctx->ctx_instr; |
| 569 | isn_T *isn; |
| 570 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 571 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 572 | if (ga_grow(instr, 1) == FAIL) |
| 573 | return NULL; |
| 574 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 575 | isn->isn_type = isn_type; |
| 576 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 577 | ++instr->ga_len; |
| 578 | |
| 579 | return isn; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Generate an instruction without arguments. |
| 584 | * "drop" will be removed from the stack. |
| 585 | * Returns a pointer to the new instruction, NULL if failed. |
| 586 | */ |
| 587 | static isn_T * |
| 588 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 589 | { |
| 590 | garray_T *stack = &cctx->ctx_type_stack; |
| 591 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 592 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 593 | stack->ga_len -= drop; |
| 594 | return generate_instr(cctx, isn_type); |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 599 | */ |
| 600 | static isn_T * |
| 601 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 602 | { |
| 603 | isn_T *isn; |
| 604 | garray_T *stack = &cctx->ctx_type_stack; |
| 605 | |
| 606 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 607 | return NULL; |
| 608 | |
| 609 | if (ga_grow(stack, 1) == FAIL) |
| 610 | return NULL; |
| 611 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 612 | ++stack->ga_len; |
| 613 | |
| 614 | return isn; |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 619 | */ |
| 620 | static int |
| 621 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 622 | { |
| 623 | isn_T *isn; |
| 624 | garray_T *stack = &cctx->ctx_type_stack; |
| 625 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 626 | |
| 627 | if ((*type)->tt_type == VAR_STRING) |
| 628 | return OK; |
| 629 | *type = &t_string; |
| 630 | |
| 631 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 632 | return FAIL; |
| 633 | isn->isn_arg.number = offset; |
| 634 | |
| 635 | return OK; |
| 636 | } |
| 637 | |
| 638 | static int |
| 639 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 640 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 641 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 642 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 643 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 644 | { |
| 645 | if (*op == '+') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 646 | emsg(_("E1051: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 647 | else |
| 648 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 649 | return FAIL; |
| 650 | } |
| 651 | return OK; |
| 652 | } |
| 653 | |
| 654 | /* |
| 655 | * Generate an instruction with two arguments. The instruction depends on the |
| 656 | * type of the arguments. |
| 657 | */ |
| 658 | static int |
| 659 | generate_two_op(cctx_T *cctx, char_u *op) |
| 660 | { |
| 661 | garray_T *stack = &cctx->ctx_type_stack; |
| 662 | type_T *type1; |
| 663 | type_T *type2; |
| 664 | vartype_T vartype; |
| 665 | isn_T *isn; |
| 666 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 667 | RETURN_OK_IF_SKIP(cctx); |
| 668 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 669 | // Get the known type of the two items on the stack. If they are matching |
| 670 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 671 | // checking. |
| 672 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 673 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 674 | vartype = VAR_ANY; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 675 | if (type1->tt_type == type2->tt_type |
| 676 | && (type1->tt_type == VAR_NUMBER |
| 677 | || type1->tt_type == VAR_LIST |
| 678 | #ifdef FEAT_FLOAT |
| 679 | || type1->tt_type == VAR_FLOAT |
| 680 | #endif |
| 681 | || type1->tt_type == VAR_BLOB)) |
| 682 | vartype = type1->tt_type; |
| 683 | |
| 684 | switch (*op) |
| 685 | { |
| 686 | case '+': if (vartype != VAR_LIST && vartype != VAR_BLOB |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 687 | && type1->tt_type != VAR_ANY |
| 688 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 689 | && check_number_or_float( |
| 690 | type1->tt_type, type2->tt_type, op) == FAIL) |
| 691 | return FAIL; |
| 692 | isn = generate_instr_drop(cctx, |
| 693 | vartype == VAR_NUMBER ? ISN_OPNR |
| 694 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 695 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 696 | #ifdef FEAT_FLOAT |
| 697 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 698 | #endif |
| 699 | : ISN_OPANY, 1); |
| 700 | if (isn != NULL) |
| 701 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 702 | break; |
| 703 | |
| 704 | case '-': |
| 705 | case '*': |
| 706 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 707 | op) == FAIL) |
| 708 | return FAIL; |
| 709 | if (vartype == VAR_NUMBER) |
| 710 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 711 | #ifdef FEAT_FLOAT |
| 712 | else if (vartype == VAR_FLOAT) |
| 713 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 714 | #endif |
| 715 | else |
| 716 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 717 | if (isn != NULL) |
| 718 | isn->isn_arg.op.op_type = *op == '*' |
| 719 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 720 | break; |
| 721 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 722 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 723 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 724 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 725 | && type2->tt_type != VAR_NUMBER)) |
| 726 | { |
| 727 | emsg(_("E1035: % requires number arguments")); |
| 728 | return FAIL; |
| 729 | } |
| 730 | isn = generate_instr_drop(cctx, |
| 731 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 732 | if (isn != NULL) |
| 733 | isn->isn_arg.op.op_type = EXPR_REM; |
| 734 | break; |
| 735 | } |
| 736 | |
| 737 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 738 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 739 | { |
| 740 | type_T *type = &t_any; |
| 741 | |
| 742 | #ifdef FEAT_FLOAT |
| 743 | // float+number and number+float results in float |
| 744 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 745 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 746 | type = &t_float; |
| 747 | #endif |
| 748 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 749 | } |
| 750 | |
| 751 | return OK; |
| 752 | } |
| 753 | |
| 754 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 755 | * Get the instruction to use for comparing "type1" with "type2" |
| 756 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 757 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 758 | static isntype_T |
| 759 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 760 | { |
| 761 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 762 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 763 | if (type1 == VAR_UNKNOWN) |
| 764 | type1 = VAR_ANY; |
| 765 | if (type2 == VAR_UNKNOWN) |
| 766 | type2 = VAR_ANY; |
| 767 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 768 | if (type1 == type2) |
| 769 | { |
| 770 | switch (type1) |
| 771 | { |
| 772 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 773 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 774 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 775 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 776 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 777 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 778 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 779 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 780 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 781 | default: isntype = ISN_COMPAREANY; break; |
| 782 | } |
| 783 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 784 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 785 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 786 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 787 | isntype = ISN_COMPAREANY; |
| 788 | |
| 789 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 790 | && (isntype == ISN_COMPAREBOOL |
| 791 | || isntype == ISN_COMPARESPECIAL |
| 792 | || isntype == ISN_COMPARENR |
| 793 | || isntype == ISN_COMPAREFLOAT)) |
| 794 | { |
| 795 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 796 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 797 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 798 | } |
| 799 | if (isntype == ISN_DROP |
| 800 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 801 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 802 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 803 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 804 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 805 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 806 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 807 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 808 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 809 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 810 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 811 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 812 | return isntype; |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 817 | */ |
| 818 | static int |
| 819 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 820 | { |
| 821 | isntype_T isntype; |
| 822 | isn_T *isn; |
| 823 | garray_T *stack = &cctx->ctx_type_stack; |
| 824 | vartype_T type1; |
| 825 | vartype_T type2; |
| 826 | |
| 827 | RETURN_OK_IF_SKIP(cctx); |
| 828 | |
| 829 | // Get the known type of the two items on the stack. If they are matching |
| 830 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 831 | // checking. |
| 832 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 833 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 834 | isntype = get_compare_isn(exptype, type1, type2); |
| 835 | if (isntype == ISN_DROP) |
| 836 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 837 | |
| 838 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 839 | return FAIL; |
| 840 | isn->isn_arg.op.op_type = exptype; |
| 841 | isn->isn_arg.op.op_ic = ic; |
| 842 | |
| 843 | // takes two arguments, puts one bool back |
| 844 | if (stack->ga_len >= 2) |
| 845 | { |
| 846 | --stack->ga_len; |
| 847 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 848 | } |
| 849 | |
| 850 | return OK; |
| 851 | } |
| 852 | |
| 853 | /* |
| 854 | * Generate an ISN_2BOOL instruction. |
| 855 | */ |
| 856 | static int |
| 857 | generate_2BOOL(cctx_T *cctx, int invert) |
| 858 | { |
| 859 | isn_T *isn; |
| 860 | garray_T *stack = &cctx->ctx_type_stack; |
| 861 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 862 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 863 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 864 | return FAIL; |
| 865 | isn->isn_arg.number = invert; |
| 866 | |
| 867 | // type becomes bool |
| 868 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 869 | |
| 870 | return OK; |
| 871 | } |
| 872 | |
| 873 | static int |
| 874 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 875 | { |
| 876 | isn_T *isn; |
| 877 | garray_T *stack = &cctx->ctx_type_stack; |
| 878 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 879 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 880 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 881 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 882 | // TODO: whole type, e.g. for a function also arg and return types |
| 883 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 884 | isn->isn_arg.type.ct_off = offset; |
| 885 | |
| 886 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 887 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 888 | |
| 889 | return OK; |
| 890 | } |
| 891 | |
| 892 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 893 | * Check that |
| 894 | * - "actual" is "expected" type or |
| 895 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 896 | * - return FAIL. |
| 897 | */ |
| 898 | static int |
| 899 | need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx) |
| 900 | { |
| 901 | if (check_type(expected, actual, FALSE) == OK) |
| 902 | return OK; |
| 903 | if (actual->tt_type != VAR_ANY |
| 904 | && actual->tt_type != VAR_UNKNOWN |
| 905 | && !(actual->tt_type == VAR_FUNC |
| 906 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 907 | { |
| 908 | type_mismatch(expected, actual); |
| 909 | return FAIL; |
| 910 | } |
| 911 | generate_TYPECHECK(cctx, expected, offset); |
| 912 | return OK; |
| 913 | } |
| 914 | |
| 915 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 916 | * Generate an ISN_PUSHNR instruction. |
| 917 | */ |
| 918 | static int |
| 919 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 920 | { |
| 921 | isn_T *isn; |
| 922 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 923 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 924 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 925 | return FAIL; |
| 926 | isn->isn_arg.number = number; |
| 927 | |
| 928 | return OK; |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * Generate an ISN_PUSHBOOL instruction. |
| 933 | */ |
| 934 | static int |
| 935 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 936 | { |
| 937 | isn_T *isn; |
| 938 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 939 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 940 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 941 | return FAIL; |
| 942 | isn->isn_arg.number = number; |
| 943 | |
| 944 | return OK; |
| 945 | } |
| 946 | |
| 947 | /* |
| 948 | * Generate an ISN_PUSHSPEC instruction. |
| 949 | */ |
| 950 | static int |
| 951 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 952 | { |
| 953 | isn_T *isn; |
| 954 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 955 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 956 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 957 | return FAIL; |
| 958 | isn->isn_arg.number = number; |
| 959 | |
| 960 | return OK; |
| 961 | } |
| 962 | |
| 963 | #ifdef FEAT_FLOAT |
| 964 | /* |
| 965 | * Generate an ISN_PUSHF instruction. |
| 966 | */ |
| 967 | static int |
| 968 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 969 | { |
| 970 | isn_T *isn; |
| 971 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 972 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 973 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 974 | return FAIL; |
| 975 | isn->isn_arg.fnumber = fnumber; |
| 976 | |
| 977 | return OK; |
| 978 | } |
| 979 | #endif |
| 980 | |
| 981 | /* |
| 982 | * Generate an ISN_PUSHS instruction. |
| 983 | * Consumes "str". |
| 984 | */ |
| 985 | static int |
| 986 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 987 | { |
| 988 | isn_T *isn; |
| 989 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 990 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 991 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 992 | return FAIL; |
| 993 | isn->isn_arg.string = str; |
| 994 | |
| 995 | return OK; |
| 996 | } |
| 997 | |
| 998 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 999 | * Generate an ISN_PUSHCHANNEL instruction. |
| 1000 | * Consumes "channel". |
| 1001 | */ |
| 1002 | static int |
| 1003 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 1004 | { |
| 1005 | isn_T *isn; |
| 1006 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1007 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1008 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 1009 | return FAIL; |
| 1010 | isn->isn_arg.channel = channel; |
| 1011 | |
| 1012 | return OK; |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * Generate an ISN_PUSHJOB instruction. |
| 1017 | * Consumes "job". |
| 1018 | */ |
| 1019 | static int |
| 1020 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 1021 | { |
| 1022 | isn_T *isn; |
| 1023 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1024 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1025 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1026 | return FAIL; |
| 1027 | isn->isn_arg.job = job; |
| 1028 | |
| 1029 | return OK; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1033 | * Generate an ISN_PUSHBLOB instruction. |
| 1034 | * Consumes "blob". |
| 1035 | */ |
| 1036 | static int |
| 1037 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1038 | { |
| 1039 | isn_T *isn; |
| 1040 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1041 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1042 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1043 | return FAIL; |
| 1044 | isn->isn_arg.blob = blob; |
| 1045 | |
| 1046 | return OK; |
| 1047 | } |
| 1048 | |
| 1049 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1050 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1051 | * Consumes "name". |
| 1052 | */ |
| 1053 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1054 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1055 | { |
| 1056 | isn_T *isn; |
| 1057 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1058 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1059 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1060 | return FAIL; |
| 1061 | isn->isn_arg.string = name; |
| 1062 | |
| 1063 | return OK; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1067 | * Generate an ISN_GETITEM instruction with "index". |
| 1068 | */ |
| 1069 | static int |
| 1070 | generate_GETITEM(cctx_T *cctx, int index) |
| 1071 | { |
| 1072 | isn_T *isn; |
| 1073 | garray_T *stack = &cctx->ctx_type_stack; |
| 1074 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1075 | type_T *item_type = &t_any; |
| 1076 | |
| 1077 | RETURN_OK_IF_SKIP(cctx); |
| 1078 | |
| 1079 | if (type->tt_type == VAR_LIST) |
| 1080 | item_type = type->tt_member; |
| 1081 | else if (type->tt_type != VAR_ANY) |
| 1082 | { |
| 1083 | emsg(_(e_listreq)); |
| 1084 | return FAIL; |
| 1085 | } |
| 1086 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1087 | return FAIL; |
| 1088 | isn->isn_arg.number = index; |
| 1089 | |
| 1090 | // add the item type to the type stack |
| 1091 | if (ga_grow(stack, 1) == FAIL) |
| 1092 | return FAIL; |
| 1093 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 1094 | ++stack->ga_len; |
| 1095 | return OK; |
| 1096 | } |
| 1097 | |
| 1098 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 1099 | * Generate an ISN_SLICE instruction with "count". |
| 1100 | */ |
| 1101 | static int |
| 1102 | generate_SLICE(cctx_T *cctx, int count) |
| 1103 | { |
| 1104 | isn_T *isn; |
| 1105 | |
| 1106 | RETURN_OK_IF_SKIP(cctx); |
| 1107 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1108 | return FAIL; |
| 1109 | isn->isn_arg.number = count; |
| 1110 | return OK; |
| 1111 | } |
| 1112 | |
| 1113 | /* |
| 1114 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1115 | */ |
| 1116 | static int |
| 1117 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1118 | { |
| 1119 | isn_T *isn; |
| 1120 | |
| 1121 | RETURN_OK_IF_SKIP(cctx); |
| 1122 | |
| 1123 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1124 | return FAIL; |
| 1125 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1126 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1127 | |
| 1128 | return OK; |
| 1129 | } |
| 1130 | |
| 1131 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1132 | * Generate an ISN_STORE instruction. |
| 1133 | */ |
| 1134 | static int |
| 1135 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1136 | { |
| 1137 | isn_T *isn; |
| 1138 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1139 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1140 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1141 | return FAIL; |
| 1142 | if (name != NULL) |
| 1143 | isn->isn_arg.string = vim_strsave(name); |
| 1144 | else |
| 1145 | isn->isn_arg.number = idx; |
| 1146 | |
| 1147 | return OK; |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1152 | */ |
| 1153 | static int |
| 1154 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1155 | { |
| 1156 | isn_T *isn; |
| 1157 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1158 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1159 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1160 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1161 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1162 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1163 | |
| 1164 | return OK; |
| 1165 | } |
| 1166 | |
| 1167 | /* |
| 1168 | * Generate an ISN_STOREOPT instruction |
| 1169 | */ |
| 1170 | static int |
| 1171 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1172 | { |
| 1173 | isn_T *isn; |
| 1174 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1175 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1176 | if ((isn = generate_instr(cctx, ISN_STOREOPT)) == NULL) |
| 1177 | return FAIL; |
| 1178 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1179 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1180 | |
| 1181 | return OK; |
| 1182 | } |
| 1183 | |
| 1184 | /* |
| 1185 | * Generate an ISN_LOAD or similar instruction. |
| 1186 | */ |
| 1187 | static int |
| 1188 | generate_LOAD( |
| 1189 | cctx_T *cctx, |
| 1190 | isntype_T isn_type, |
| 1191 | int idx, |
| 1192 | char_u *name, |
| 1193 | type_T *type) |
| 1194 | { |
| 1195 | isn_T *isn; |
| 1196 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1197 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1198 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1199 | return FAIL; |
| 1200 | if (name != NULL) |
| 1201 | isn->isn_arg.string = vim_strsave(name); |
| 1202 | else |
| 1203 | isn->isn_arg.number = idx; |
| 1204 | |
| 1205 | return OK; |
| 1206 | } |
| 1207 | |
| 1208 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1209 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1210 | */ |
| 1211 | static int |
| 1212 | generate_LOADV( |
| 1213 | cctx_T *cctx, |
| 1214 | char_u *name, |
| 1215 | int error) |
| 1216 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1217 | int di_flags; |
| 1218 | int vidx = find_vim_var(name, &di_flags); |
| 1219 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1220 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1221 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1222 | if (vidx < 0) |
| 1223 | { |
| 1224 | if (error) |
| 1225 | semsg(_(e_var_notfound), name); |
| 1226 | return FAIL; |
| 1227 | } |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1228 | type = typval2type(get_vim_var_tv(vidx)); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1229 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1230 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1234 | * Generate an ISN_UNLET instruction. |
| 1235 | */ |
| 1236 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1237 | 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] | 1238 | { |
| 1239 | isn_T *isn; |
| 1240 | |
| 1241 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1242 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1243 | return FAIL; |
| 1244 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1245 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1246 | |
| 1247 | return OK; |
| 1248 | } |
| 1249 | |
| 1250 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1251 | * Generate an ISN_LOADS instruction. |
| 1252 | */ |
| 1253 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1254 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1255 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1256 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1257 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1258 | int sid, |
| 1259 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1260 | { |
| 1261 | isn_T *isn; |
| 1262 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1263 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1264 | if (isn_type == ISN_LOADS) |
| 1265 | isn = generate_instr_type(cctx, isn_type, type); |
| 1266 | else |
| 1267 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1268 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1269 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1270 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1271 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1272 | |
| 1273 | return OK; |
| 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1278 | */ |
| 1279 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1280 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1281 | cctx_T *cctx, |
| 1282 | isntype_T isn_type, |
| 1283 | int sid, |
| 1284 | int idx, |
| 1285 | type_T *type) |
| 1286 | { |
| 1287 | isn_T *isn; |
| 1288 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1289 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1290 | if (isn_type == ISN_LOADSCRIPT) |
| 1291 | isn = generate_instr_type(cctx, isn_type, type); |
| 1292 | else |
| 1293 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1294 | if (isn == NULL) |
| 1295 | return FAIL; |
| 1296 | isn->isn_arg.script.script_sid = sid; |
| 1297 | isn->isn_arg.script.script_idx = idx; |
| 1298 | return OK; |
| 1299 | } |
| 1300 | |
| 1301 | /* |
| 1302 | * Generate an ISN_NEWLIST instruction. |
| 1303 | */ |
| 1304 | static int |
| 1305 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1306 | { |
| 1307 | isn_T *isn; |
| 1308 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1309 | type_T *type; |
| 1310 | type_T *member; |
| 1311 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1312 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1313 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1314 | return FAIL; |
| 1315 | isn->isn_arg.number = count; |
| 1316 | |
| 1317 | // drop the value types |
| 1318 | stack->ga_len -= count; |
| 1319 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1320 | // 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] | 1321 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1322 | if (count > 0) |
| 1323 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1324 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1325 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1326 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1327 | |
| 1328 | // add the list type to the type stack |
| 1329 | if (ga_grow(stack, 1) == FAIL) |
| 1330 | return FAIL; |
| 1331 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1332 | ++stack->ga_len; |
| 1333 | |
| 1334 | return OK; |
| 1335 | } |
| 1336 | |
| 1337 | /* |
| 1338 | * Generate an ISN_NEWDICT instruction. |
| 1339 | */ |
| 1340 | static int |
| 1341 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1342 | { |
| 1343 | isn_T *isn; |
| 1344 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1345 | type_T *type; |
| 1346 | type_T *member; |
| 1347 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1348 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1349 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1350 | return FAIL; |
| 1351 | isn->isn_arg.number = count; |
| 1352 | |
| 1353 | // drop the key and value types |
| 1354 | stack->ga_len -= 2 * count; |
| 1355 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1356 | // Use the first value type for the list member type. Use "void" for an |
| 1357 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1358 | if (count > 0) |
| 1359 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1360 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1361 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1362 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1363 | |
| 1364 | // add the dict type to the type stack |
| 1365 | if (ga_grow(stack, 1) == FAIL) |
| 1366 | return FAIL; |
| 1367 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1368 | ++stack->ga_len; |
| 1369 | |
| 1370 | return OK; |
| 1371 | } |
| 1372 | |
| 1373 | /* |
| 1374 | * Generate an ISN_FUNCREF instruction. |
| 1375 | */ |
| 1376 | static int |
| 1377 | generate_FUNCREF(cctx_T *cctx, int dfunc_idx) |
| 1378 | { |
| 1379 | isn_T *isn; |
| 1380 | garray_T *stack = &cctx->ctx_type_stack; |
| 1381 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1382 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1383 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1384 | return FAIL; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1385 | isn->isn_arg.funcref.fr_func = dfunc_idx; |
| 1386 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1387 | |
| 1388 | if (ga_grow(stack, 1) == FAIL) |
| 1389 | return FAIL; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1390 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_func_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1391 | // TODO: argument and return types |
| 1392 | ++stack->ga_len; |
| 1393 | |
| 1394 | return OK; |
| 1395 | } |
| 1396 | |
| 1397 | /* |
| 1398 | * Generate an ISN_JUMP instruction. |
| 1399 | */ |
| 1400 | static int |
| 1401 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1402 | { |
| 1403 | isn_T *isn; |
| 1404 | garray_T *stack = &cctx->ctx_type_stack; |
| 1405 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1406 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1407 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1408 | return FAIL; |
| 1409 | isn->isn_arg.jump.jump_when = when; |
| 1410 | isn->isn_arg.jump.jump_where = where; |
| 1411 | |
| 1412 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1413 | --stack->ga_len; |
| 1414 | |
| 1415 | return OK; |
| 1416 | } |
| 1417 | |
| 1418 | static int |
| 1419 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1420 | { |
| 1421 | isn_T *isn; |
| 1422 | garray_T *stack = &cctx->ctx_type_stack; |
| 1423 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1424 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1425 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1426 | return FAIL; |
| 1427 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1428 | |
| 1429 | if (ga_grow(stack, 1) == FAIL) |
| 1430 | return FAIL; |
| 1431 | // type doesn't matter, will be stored next |
| 1432 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1433 | ++stack->ga_len; |
| 1434 | |
| 1435 | return OK; |
| 1436 | } |
| 1437 | |
| 1438 | /* |
| 1439 | * Generate an ISN_BCALL instruction. |
| 1440 | * Return FAIL if the number of arguments is wrong. |
| 1441 | */ |
| 1442 | static int |
| 1443 | generate_BCALL(cctx_T *cctx, int func_idx, int argcount) |
| 1444 | { |
| 1445 | isn_T *isn; |
| 1446 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1447 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1448 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1449 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1450 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1451 | if (check_internal_func(func_idx, argcount) == FAIL) |
| 1452 | return FAIL; |
| 1453 | |
| 1454 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1455 | return FAIL; |
| 1456 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1457 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1458 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1459 | for (i = 0; i < argcount; ++i) |
| 1460 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1461 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1462 | stack->ga_len -= argcount; // drop the arguments |
| 1463 | if (ga_grow(stack, 1) == FAIL) |
| 1464 | return FAIL; |
| 1465 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1466 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1467 | ++stack->ga_len; // add return value |
| 1468 | |
| 1469 | return OK; |
| 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1474 | * Return FAIL if the number of arguments is wrong. |
| 1475 | */ |
| 1476 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1477 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1478 | { |
| 1479 | isn_T *isn; |
| 1480 | garray_T *stack = &cctx->ctx_type_stack; |
| 1481 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1482 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1483 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1484 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1485 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1486 | { |
| 1487 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1488 | return FAIL; |
| 1489 | } |
| 1490 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1491 | { |
| 1492 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1493 | return FAIL; |
| 1494 | } |
| 1495 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1496 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1497 | { |
| 1498 | int i; |
| 1499 | |
| 1500 | for (i = 0; i < argcount; ++i) |
| 1501 | { |
| 1502 | type_T *expected; |
| 1503 | type_T *actual; |
| 1504 | |
| 1505 | if (i < regular_args) |
| 1506 | { |
| 1507 | if (ufunc->uf_arg_types == NULL) |
| 1508 | continue; |
| 1509 | expected = ufunc->uf_arg_types[i]; |
| 1510 | } |
| 1511 | else |
| 1512 | expected = ufunc->uf_va_type->tt_member; |
| 1513 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1514 | if (need_type(actual, expected, -argcount + i, cctx) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1515 | { |
| 1516 | arg_type_mismatch(expected, actual, i + 1); |
| 1517 | return FAIL; |
| 1518 | } |
| 1519 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1520 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 1521 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1522 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1523 | } |
| 1524 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1525 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1526 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1527 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1528 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1529 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1530 | { |
| 1531 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1532 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1533 | } |
| 1534 | else |
| 1535 | { |
| 1536 | // A user function may be deleted and redefined later, can't use the |
| 1537 | // ufunc pointer, need to look it up again at runtime. |
| 1538 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1539 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1540 | } |
| 1541 | |
| 1542 | stack->ga_len -= argcount; // drop the arguments |
| 1543 | if (ga_grow(stack, 1) == FAIL) |
| 1544 | return FAIL; |
| 1545 | // add return value |
| 1546 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1547 | ++stack->ga_len; |
| 1548 | |
| 1549 | return OK; |
| 1550 | } |
| 1551 | |
| 1552 | /* |
| 1553 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1554 | */ |
| 1555 | static int |
| 1556 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1557 | { |
| 1558 | isn_T *isn; |
| 1559 | garray_T *stack = &cctx->ctx_type_stack; |
| 1560 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1561 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1562 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1563 | return FAIL; |
| 1564 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1565 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1566 | |
| 1567 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1568 | if (ga_grow(stack, 1) == FAIL) |
| 1569 | return FAIL; |
| 1570 | // add return value |
| 1571 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1572 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1573 | |
| 1574 | return OK; |
| 1575 | } |
| 1576 | |
| 1577 | /* |
| 1578 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1579 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1580 | */ |
| 1581 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1582 | generate_PCALL( |
| 1583 | cctx_T *cctx, |
| 1584 | int argcount, |
| 1585 | char_u *name, |
| 1586 | type_T *type, |
| 1587 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1588 | { |
| 1589 | isn_T *isn; |
| 1590 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1591 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1592 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1593 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1594 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1595 | if (type->tt_type == VAR_ANY) |
| 1596 | ret_type = &t_any; |
| 1597 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1598 | { |
| 1599 | if (type->tt_argcount != -1) |
| 1600 | { |
| 1601 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1602 | |
| 1603 | if (argcount < type->tt_min_argcount - varargs) |
| 1604 | { |
| 1605 | semsg(_(e_toofewarg), "[reference]"); |
| 1606 | return FAIL; |
| 1607 | } |
| 1608 | if (!varargs && argcount > type->tt_argcount) |
| 1609 | { |
| 1610 | semsg(_(e_toomanyarg), "[reference]"); |
| 1611 | return FAIL; |
| 1612 | } |
| 1613 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1614 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1615 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1616 | else |
| 1617 | { |
| 1618 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1619 | return FAIL; |
| 1620 | } |
| 1621 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1622 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1623 | return FAIL; |
| 1624 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1625 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1626 | |
| 1627 | stack->ga_len -= argcount; // drop the arguments |
| 1628 | |
| 1629 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1630 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1631 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1632 | // If partial is above the arguments it must be cleared and replaced with |
| 1633 | // the return value. |
| 1634 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1635 | return FAIL; |
| 1636 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1637 | return OK; |
| 1638 | } |
| 1639 | |
| 1640 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1641 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1642 | */ |
| 1643 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1644 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1645 | { |
| 1646 | isn_T *isn; |
| 1647 | garray_T *stack = &cctx->ctx_type_stack; |
| 1648 | type_T *type; |
| 1649 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1650 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1651 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1652 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1653 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1654 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1655 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1656 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1657 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1658 | { |
| 1659 | emsg(_(e_dictreq)); |
| 1660 | return FAIL; |
| 1661 | } |
| 1662 | // change dict type to dict member type |
| 1663 | if (type->tt_type == VAR_DICT) |
| 1664 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1665 | |
| 1666 | return OK; |
| 1667 | } |
| 1668 | |
| 1669 | /* |
| 1670 | * Generate an ISN_ECHO instruction. |
| 1671 | */ |
| 1672 | static int |
| 1673 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1674 | { |
| 1675 | isn_T *isn; |
| 1676 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1677 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1678 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1679 | return FAIL; |
| 1680 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1681 | isn->isn_arg.echo.echo_count = count; |
| 1682 | |
| 1683 | return OK; |
| 1684 | } |
| 1685 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1686 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1687 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1688 | */ |
| 1689 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1690 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1691 | { |
| 1692 | isn_T *isn; |
| 1693 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1694 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1695 | return FAIL; |
| 1696 | isn->isn_arg.number = count; |
| 1697 | |
| 1698 | return OK; |
| 1699 | } |
| 1700 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1701 | static int |
| 1702 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1703 | { |
| 1704 | isn_T *isn; |
| 1705 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1706 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1707 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1708 | return FAIL; |
| 1709 | isn->isn_arg.string = vim_strsave(line); |
| 1710 | return OK; |
| 1711 | } |
| 1712 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1713 | static int |
| 1714 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1715 | { |
| 1716 | isn_T *isn; |
| 1717 | |
| 1718 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1719 | return FAIL; |
| 1720 | isn->isn_arg.number = count; |
| 1721 | return OK; |
| 1722 | } |
| 1723 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1724 | /* |
| 1725 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1726 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1727 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1728 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1729 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1730 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1731 | lvar_T *lvar; |
| 1732 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1733 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1734 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1735 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1736 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1740 | return NULL; |
| 1741 | 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] | 1742 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1743 | // Every local variable uses the next entry on the stack. We could re-use |
| 1744 | // the last ones when leaving a scope, but then variables used in a closure |
| 1745 | // might get overwritten. To keep things simple do not re-use stack |
| 1746 | // entries. This is less efficient, but memory is cheap these days. |
| 1747 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1748 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1749 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1750 | lvar->lv_const = isConst; |
| 1751 | lvar->lv_type = type; |
| 1752 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1753 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1757 | * Remove local variables above "new_top". |
| 1758 | */ |
| 1759 | static void |
| 1760 | unwind_locals(cctx_T *cctx, int new_top) |
| 1761 | { |
| 1762 | if (cctx->ctx_locals.ga_len > new_top) |
| 1763 | { |
| 1764 | int idx; |
| 1765 | lvar_T *lvar; |
| 1766 | |
| 1767 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1768 | { |
| 1769 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1770 | vim_free(lvar->lv_name); |
| 1771 | } |
| 1772 | } |
| 1773 | cctx->ctx_locals.ga_len = new_top; |
| 1774 | } |
| 1775 | |
| 1776 | /* |
| 1777 | * Free all local variables. |
| 1778 | */ |
| 1779 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1780 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1781 | { |
| 1782 | unwind_locals(cctx, 0); |
| 1783 | ga_clear(&cctx->ctx_locals); |
| 1784 | } |
| 1785 | |
| 1786 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1787 | * Skip over a type definition and return a pointer to just after it. |
| 1788 | */ |
| 1789 | char_u * |
| 1790 | skip_type(char_u *start) |
| 1791 | { |
| 1792 | char_u *p = start; |
| 1793 | |
| 1794 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1795 | ++p; |
| 1796 | |
| 1797 | // Skip over "<type>"; this is permissive about white space. |
| 1798 | if (*skipwhite(p) == '<') |
| 1799 | { |
| 1800 | p = skipwhite(p); |
| 1801 | p = skip_type(skipwhite(p + 1)); |
| 1802 | p = skipwhite(p); |
| 1803 | if (*p == '>') |
| 1804 | ++p; |
| 1805 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1806 | else if (*p == '(' && STRNCMP("func", start, 4) == 0) |
| 1807 | { |
| 1808 | // handle func(args): type |
| 1809 | ++p; |
| 1810 | while (*p != ')' && *p != NUL) |
| 1811 | { |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1812 | char_u *sp = p; |
| 1813 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1814 | p = skip_type(p); |
Bram Moolenaar | 1c0d44f | 2020-05-02 19:04:58 +0200 | [diff] [blame] | 1815 | if (p == sp) |
| 1816 | return p; // syntax error |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1817 | if (*p == ',') |
| 1818 | p = skipwhite(p + 1); |
| 1819 | } |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 1820 | if (*p == ')') |
| 1821 | { |
| 1822 | if (p[1] == ':') |
| 1823 | p = skip_type(skipwhite(p + 2)); |
| 1824 | else |
| 1825 | p = skipwhite(p + 1); |
| 1826 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1827 | } |
| 1828 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1829 | return p; |
| 1830 | } |
| 1831 | |
| 1832 | /* |
| 1833 | * Parse the member type: "<type>" and return "type" with the member set. |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1834 | * Use "type_gap" if a new type needs to be added. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1835 | * Returns NULL in case of failure. |
| 1836 | */ |
| 1837 | static type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1838 | 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] | 1839 | { |
| 1840 | type_T *member_type; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1841 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1842 | |
| 1843 | if (**arg != '<') |
| 1844 | { |
| 1845 | if (*skipwhite(*arg) == '<') |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1846 | semsg(_(e_no_white_before), "<"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1847 | else |
| 1848 | emsg(_("E1008: Missing <type>")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1849 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1850 | } |
| 1851 | *arg = skipwhite(*arg + 1); |
| 1852 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1853 | member_type = parse_type(arg, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1854 | |
| 1855 | *arg = skipwhite(*arg); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1856 | if (**arg != '>' && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1857 | { |
| 1858 | emsg(_("E1009: Missing > after type")); |
Bram Moolenaar | cf3f8bf | 2020-03-26 13:15:42 +0100 | [diff] [blame] | 1859 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1860 | } |
| 1861 | ++*arg; |
| 1862 | |
| 1863 | if (type->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1864 | return get_list_type(member_type, type_gap); |
| 1865 | return get_dict_type(member_type, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | /* |
| 1869 | * Parse a type at "arg" and advance over it. |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 1870 | * Return &t_any for failure. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1871 | */ |
| 1872 | type_T * |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1873 | parse_type(char_u **arg, garray_T *type_gap) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1874 | { |
| 1875 | char_u *p = *arg; |
| 1876 | size_t len; |
| 1877 | |
| 1878 | // skip over the first word |
| 1879 | while (ASCII_ISALNUM(*p) || *p == '_') |
| 1880 | ++p; |
| 1881 | len = p - *arg; |
| 1882 | |
| 1883 | switch (**arg) |
| 1884 | { |
| 1885 | case 'a': |
| 1886 | if (len == 3 && STRNCMP(*arg, "any", len) == 0) |
| 1887 | { |
| 1888 | *arg += len; |
| 1889 | return &t_any; |
| 1890 | } |
| 1891 | break; |
| 1892 | case 'b': |
| 1893 | if (len == 4 && STRNCMP(*arg, "bool", len) == 0) |
| 1894 | { |
| 1895 | *arg += len; |
| 1896 | return &t_bool; |
| 1897 | } |
| 1898 | if (len == 4 && STRNCMP(*arg, "blob", len) == 0) |
| 1899 | { |
| 1900 | *arg += len; |
| 1901 | return &t_blob; |
| 1902 | } |
| 1903 | break; |
| 1904 | case 'c': |
| 1905 | if (len == 7 && STRNCMP(*arg, "channel", len) == 0) |
| 1906 | { |
| 1907 | *arg += len; |
| 1908 | return &t_channel; |
| 1909 | } |
| 1910 | break; |
| 1911 | case 'd': |
| 1912 | if (len == 4 && STRNCMP(*arg, "dict", len) == 0) |
| 1913 | { |
| 1914 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1915 | return parse_type_member(arg, &t_dict_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1916 | } |
| 1917 | break; |
| 1918 | case 'f': |
| 1919 | if (len == 5 && STRNCMP(*arg, "float", len) == 0) |
| 1920 | { |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1921 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1922 | *arg += len; |
| 1923 | return &t_float; |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1924 | #else |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1925 | emsg(_("E1076: This Vim is not compiled with float support")); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1926 | return &t_any; |
| 1927 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1928 | } |
| 1929 | if (len == 4 && STRNCMP(*arg, "func", len) == 0) |
| 1930 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1931 | type_T *type; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 1932 | type_T *ret_type = &t_unknown; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1933 | int argcount = -1; |
| 1934 | int flags = 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1935 | int first_optional = -1; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1936 | type_T *arg_type[MAX_FUNC_ARGS + 1]; |
| 1937 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1938 | // func({type}, ...{type}): {type} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1939 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1940 | if (**arg == '(') |
| 1941 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1942 | // "func" may or may not return a value, "func()" does |
| 1943 | // not return a value. |
| 1944 | ret_type = &t_void; |
| 1945 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1946 | p = ++*arg; |
| 1947 | argcount = 0; |
| 1948 | while (*p != NUL && *p != ')') |
| 1949 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1950 | if (*p == '?') |
| 1951 | { |
| 1952 | if (first_optional == -1) |
| 1953 | first_optional = argcount; |
| 1954 | ++p; |
| 1955 | } |
| 1956 | else if (first_optional != -1) |
| 1957 | { |
| 1958 | emsg(_("E1007: mandatory argument after optional argument")); |
| 1959 | return &t_any; |
| 1960 | } |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1961 | else if (STRNCMP(p, "...", 3) == 0) |
| 1962 | { |
| 1963 | flags |= TTFLAG_VARARGS; |
| 1964 | p += 3; |
| 1965 | } |
| 1966 | |
| 1967 | arg_type[argcount++] = parse_type(&p, type_gap); |
| 1968 | |
| 1969 | // Nothing comes after "...{type}". |
| 1970 | if (flags & TTFLAG_VARARGS) |
| 1971 | break; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1972 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1973 | if (*p != ',' && *skipwhite(p) == ',') |
| 1974 | { |
| 1975 | semsg(_(e_no_white_before), ","); |
| 1976 | return &t_any; |
| 1977 | } |
| 1978 | if (*p == ',') |
| 1979 | { |
| 1980 | ++p; |
| 1981 | if (!VIM_ISWHITE(*p)) |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1982 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1983 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 1984 | return &t_any; |
| 1985 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1986 | } |
| 1987 | p = skipwhite(p); |
| 1988 | if (argcount == MAX_FUNC_ARGS) |
| 1989 | { |
| 1990 | emsg(_("E740: Too many argument types")); |
| 1991 | return &t_any; |
| 1992 | } |
| 1993 | } |
| 1994 | |
| 1995 | p = skipwhite(p); |
| 1996 | if (*p != ')') |
| 1997 | { |
| 1998 | emsg(_(e_missing_close)); |
| 1999 | return &t_any; |
| 2000 | } |
| 2001 | *arg = p + 1; |
| 2002 | } |
| 2003 | if (**arg == ':') |
| 2004 | { |
| 2005 | // parse return type |
| 2006 | ++*arg; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 2007 | if (!VIM_ISWHITE(**arg)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2008 | semsg(_(e_white_after), ":"); |
| 2009 | *arg = skipwhite(*arg); |
| 2010 | ret_type = parse_type(arg, type_gap); |
| 2011 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2012 | if (flags == 0 && first_optional == -1 && argcount <= 0) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2013 | type = get_func_type(ret_type, argcount, type_gap); |
| 2014 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2015 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2016 | type = alloc_func_type(ret_type, argcount, type_gap); |
| 2017 | type->tt_flags = flags; |
| 2018 | if (argcount > 0) |
| 2019 | { |
| 2020 | type->tt_argcount = argcount; |
| 2021 | type->tt_min_argcount = first_optional == -1 |
| 2022 | ? argcount : first_optional; |
| 2023 | if (func_type_add_arg_types(type, argcount, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2024 | type_gap) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2025 | return &t_any; |
| 2026 | mch_memmove(type->tt_args, arg_type, |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2027 | sizeof(type_T *) * argcount); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2028 | } |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2029 | } |
| 2030 | return type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2031 | } |
| 2032 | break; |
| 2033 | case 'j': |
| 2034 | if (len == 3 && STRNCMP(*arg, "job", len) == 0) |
| 2035 | { |
| 2036 | *arg += len; |
| 2037 | return &t_job; |
| 2038 | } |
| 2039 | break; |
| 2040 | case 'l': |
| 2041 | if (len == 4 && STRNCMP(*arg, "list", len) == 0) |
| 2042 | { |
| 2043 | *arg += len; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2044 | return parse_type_member(arg, &t_list_any, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2045 | } |
| 2046 | break; |
| 2047 | case 'n': |
| 2048 | if (len == 6 && STRNCMP(*arg, "number", len) == 0) |
| 2049 | { |
| 2050 | *arg += len; |
| 2051 | return &t_number; |
| 2052 | } |
| 2053 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2054 | case 's': |
| 2055 | if (len == 6 && STRNCMP(*arg, "string", len) == 0) |
| 2056 | { |
| 2057 | *arg += len; |
| 2058 | return &t_string; |
| 2059 | } |
| 2060 | break; |
| 2061 | case 'v': |
| 2062 | if (len == 4 && STRNCMP(*arg, "void", len) == 0) |
| 2063 | { |
| 2064 | *arg += len; |
| 2065 | return &t_void; |
| 2066 | } |
| 2067 | break; |
| 2068 | } |
| 2069 | |
| 2070 | semsg(_("E1010: Type not recognized: %s"), *arg); |
| 2071 | return &t_any; |
| 2072 | } |
| 2073 | |
| 2074 | /* |
| 2075 | * Check if "type1" and "type2" are exactly the same. |
| 2076 | */ |
| 2077 | static int |
| 2078 | equal_type(type_T *type1, type_T *type2) |
| 2079 | { |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2080 | int i; |
| 2081 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2082 | if (type1->tt_type != type2->tt_type) |
| 2083 | return FALSE; |
| 2084 | switch (type1->tt_type) |
| 2085 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2086 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2087 | case VAR_ANY: |
| 2088 | case VAR_VOID: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2089 | case VAR_SPECIAL: |
| 2090 | case VAR_BOOL: |
| 2091 | case VAR_NUMBER: |
| 2092 | case VAR_FLOAT: |
| 2093 | case VAR_STRING: |
| 2094 | case VAR_BLOB: |
| 2095 | case VAR_JOB: |
| 2096 | case VAR_CHANNEL: |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2097 | break; // not composite is always OK |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2098 | case VAR_LIST: |
| 2099 | case VAR_DICT: |
| 2100 | return equal_type(type1->tt_member, type2->tt_member); |
| 2101 | case VAR_FUNC: |
| 2102 | case VAR_PARTIAL: |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2103 | if (!equal_type(type1->tt_member, type2->tt_member) |
| 2104 | || type1->tt_argcount != type2->tt_argcount) |
| 2105 | return FALSE; |
| 2106 | if (type1->tt_argcount < 0 |
| 2107 | || type1->tt_args == NULL || type2->tt_args == NULL) |
| 2108 | return TRUE; |
| 2109 | for (i = 0; i < type1->tt_argcount; ++i) |
| 2110 | if (!equal_type(type1->tt_args[i], type2->tt_args[i])) |
| 2111 | return FALSE; |
| 2112 | return TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2113 | } |
| 2114 | return TRUE; |
| 2115 | } |
| 2116 | |
| 2117 | /* |
| 2118 | * Find the common type of "type1" and "type2" and put it in "dest". |
| 2119 | * "type2" and "dest" may be the same. |
| 2120 | */ |
| 2121 | static void |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2122 | 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] | 2123 | { |
| 2124 | if (equal_type(type1, type2)) |
| 2125 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2126 | *dest = type1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2127 | return; |
| 2128 | } |
| 2129 | |
| 2130 | if (type1->tt_type == type2->tt_type) |
| 2131 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2132 | if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT) |
| 2133 | { |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2134 | type_T *common; |
| 2135 | |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2136 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2137 | if (type1->tt_type == VAR_LIST) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2138 | *dest = get_list_type(common, type_gap); |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2139 | else |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2140 | *dest = get_dict_type(common, type_gap); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2141 | return; |
| 2142 | } |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2143 | if (type1->tt_type == VAR_FUNC) |
| 2144 | { |
| 2145 | type_T *common; |
| 2146 | |
| 2147 | common_type(type1->tt_member, type2->tt_member, &common, type_gap); |
| 2148 | if (type1->tt_argcount == type2->tt_argcount |
| 2149 | && type1->tt_argcount >= 0) |
| 2150 | { |
| 2151 | int argcount = type1->tt_argcount; |
| 2152 | int i; |
| 2153 | |
| 2154 | *dest = alloc_func_type(common, argcount, type_gap); |
| 2155 | if (type1->tt_args != NULL && type2->tt_args != NULL) |
| 2156 | { |
Bram Moolenaar | c5f1ef5 | 2020-04-12 17:11:27 +0200 | [diff] [blame] | 2157 | if (func_type_add_arg_types(*dest, argcount, |
| 2158 | type_gap) == OK) |
Bram Moolenaar | 99aaf0c | 2020-04-12 14:39:53 +0200 | [diff] [blame] | 2159 | for (i = 0; i < argcount; ++i) |
| 2160 | common_type(type1->tt_args[i], type2->tt_args[i], |
| 2161 | &(*dest)->tt_args[i], type_gap); |
| 2162 | } |
| 2163 | } |
| 2164 | else |
| 2165 | *dest = alloc_func_type(common, -1, type_gap); |
| 2166 | return; |
| 2167 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2168 | } |
| 2169 | |
Bram Moolenaar | 61a6d4e | 2020-03-01 23:32:25 +0100 | [diff] [blame] | 2170 | *dest = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | char * |
| 2174 | vartype_name(vartype_T type) |
| 2175 | { |
| 2176 | switch (type) |
| 2177 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2178 | case VAR_UNKNOWN: break; |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2179 | case VAR_ANY: return "any"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2180 | case VAR_VOID: return "void"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2181 | case VAR_SPECIAL: return "special"; |
| 2182 | case VAR_BOOL: return "bool"; |
| 2183 | case VAR_NUMBER: return "number"; |
| 2184 | case VAR_FLOAT: return "float"; |
| 2185 | case VAR_STRING: return "string"; |
| 2186 | case VAR_BLOB: return "blob"; |
| 2187 | case VAR_JOB: return "job"; |
| 2188 | case VAR_CHANNEL: return "channel"; |
| 2189 | case VAR_LIST: return "list"; |
| 2190 | case VAR_DICT: return "dict"; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2191 | |
| 2192 | case VAR_FUNC: |
| 2193 | case VAR_PARTIAL: return "func"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2194 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 2195 | return "unknown"; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | /* |
| 2199 | * Return the name of a type. |
| 2200 | * The result may be in allocated memory, in which case "tofree" is set. |
| 2201 | */ |
| 2202 | char * |
| 2203 | type_name(type_T *type, char **tofree) |
| 2204 | { |
| 2205 | char *name = vartype_name(type->tt_type); |
| 2206 | |
| 2207 | *tofree = NULL; |
| 2208 | if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) |
| 2209 | { |
| 2210 | char *member_free; |
| 2211 | char *member_name = type_name(type->tt_member, &member_free); |
| 2212 | size_t len; |
| 2213 | |
| 2214 | len = STRLEN(name) + STRLEN(member_name) + 3; |
| 2215 | *tofree = alloc(len); |
| 2216 | if (*tofree != NULL) |
| 2217 | { |
| 2218 | vim_snprintf(*tofree, len, "%s<%s>", name, member_name); |
| 2219 | vim_free(member_free); |
| 2220 | return *tofree; |
| 2221 | } |
| 2222 | } |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2223 | if (type->tt_type == VAR_FUNC) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2224 | { |
| 2225 | garray_T ga; |
| 2226 | int i; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2227 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2228 | |
| 2229 | ga_init2(&ga, 1, 100); |
| 2230 | if (ga_grow(&ga, 20) == FAIL) |
| 2231 | return "[unknown]"; |
| 2232 | *tofree = ga.ga_data; |
| 2233 | STRCPY(ga.ga_data, "func("); |
| 2234 | ga.ga_len += 5; |
| 2235 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2236 | for (i = 0; i < type->tt_argcount; ++i) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2237 | { |
| 2238 | char *arg_free; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2239 | char *arg_type; |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2240 | int len; |
| 2241 | |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2242 | if (type->tt_args == NULL) |
| 2243 | arg_type = "[unknown]"; |
| 2244 | else |
| 2245 | arg_type = type_name(type->tt_args[i], &arg_free); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2246 | if (i > 0) |
| 2247 | { |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2248 | STRCPY((char *)ga.ga_data + ga.ga_len, ", "); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2249 | ga.ga_len += 2; |
| 2250 | } |
| 2251 | len = (int)STRLEN(arg_type); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2252 | if (ga_grow(&ga, len + 8) == FAIL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2253 | { |
| 2254 | vim_free(arg_free); |
| 2255 | return "[unknown]"; |
| 2256 | } |
| 2257 | *tofree = ga.ga_data; |
Bram Moolenaar | 08938ee | 2020-04-11 23:17:17 +0200 | [diff] [blame] | 2258 | if (varargs && i == type->tt_argcount - 1) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 2259 | { |
| 2260 | STRCPY((char *)ga.ga_data + ga.ga_len, "..."); |
| 2261 | ga.ga_len += 3; |
| 2262 | } |
| 2263 | else if (i >= type->tt_min_argcount) |
| 2264 | *((char *)ga.ga_data + ga.ga_len++) = '?'; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2265 | STRCPY((char *)ga.ga_data + ga.ga_len, arg_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2266 | ga.ga_len += len; |
| 2267 | vim_free(arg_free); |
| 2268 | } |
| 2269 | |
| 2270 | if (type->tt_member == &t_void) |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2271 | STRCPY((char *)ga.ga_data + ga.ga_len, ")"); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2272 | else |
| 2273 | { |
| 2274 | char *ret_free; |
| 2275 | char *ret_name = type_name(type->tt_member, &ret_free); |
| 2276 | int len; |
| 2277 | |
| 2278 | len = (int)STRLEN(ret_name) + 4; |
| 2279 | if (ga_grow(&ga, len) == FAIL) |
| 2280 | { |
| 2281 | vim_free(ret_free); |
| 2282 | return "[unknown]"; |
| 2283 | } |
| 2284 | *tofree = ga.ga_data; |
Bram Moolenaar | b8ed3aa | 2020-04-05 19:09:05 +0200 | [diff] [blame] | 2285 | STRCPY((char *)ga.ga_data + ga.ga_len, "): "); |
| 2286 | STRCPY((char *)ga.ga_data + ga.ga_len + 3, ret_name); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2287 | vim_free(ret_free); |
| 2288 | } |
| 2289 | return ga.ga_data; |
| 2290 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2291 | |
| 2292 | return name; |
| 2293 | } |
| 2294 | |
| 2295 | /* |
| 2296 | * Find "name" in script-local items of script "sid". |
| 2297 | * Returns the index in "sn_var_vals" if found. |
| 2298 | * If found but not in "sn_var_vals" returns -1. |
| 2299 | * If not found returns -2. |
| 2300 | */ |
| 2301 | int |
| 2302 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 2303 | { |
| 2304 | hashtab_T *ht; |
| 2305 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2306 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2307 | int idx; |
| 2308 | |
| 2309 | // First look the name up in the hashtable. |
| 2310 | if (sid <= 0 || sid > script_items.ga_len) |
| 2311 | return -1; |
| 2312 | ht = &SCRIPT_VARS(sid); |
| 2313 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2314 | if (di == NULL) |
| 2315 | return -2; |
| 2316 | |
| 2317 | // Now find the svar_T index in sn_var_vals. |
| 2318 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2319 | { |
| 2320 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2321 | |
| 2322 | if (sv->sv_tv == &di->di_tv) |
| 2323 | { |
| 2324 | if (check_writable && sv->sv_const) |
| 2325 | semsg(_(e_readonlyvar), name); |
| 2326 | return idx; |
| 2327 | } |
| 2328 | } |
| 2329 | return -1; |
| 2330 | } |
| 2331 | |
| 2332 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2333 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2334 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2335 | */ |
| 2336 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2337 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2338 | { |
Bram Moolenaar | 086eb18 | 2020-07-01 16:00:44 +0200 | [diff] [blame] | 2339 | scriptitem_T *si; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2340 | int idx; |
| 2341 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2342 | if (current_sctx.sc_sid <= 0) |
| 2343 | return NULL; |
| 2344 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2345 | if (cctx != NULL) |
| 2346 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2347 | { |
| 2348 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2349 | + idx; |
| 2350 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2351 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2352 | : STRLEN(import->imp_name) == len |
| 2353 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2354 | return import; |
| 2355 | } |
| 2356 | |
| 2357 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2358 | { |
| 2359 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2360 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2361 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2362 | : STRLEN(import->imp_name) == len |
| 2363 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2364 | return import; |
| 2365 | } |
| 2366 | return NULL; |
| 2367 | } |
| 2368 | |
| 2369 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2370 | * Free all imported variables. |
| 2371 | */ |
| 2372 | static void |
| 2373 | free_imported(cctx_T *cctx) |
| 2374 | { |
| 2375 | int idx; |
| 2376 | |
| 2377 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2378 | { |
| 2379 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2380 | |
| 2381 | vim_free(import->imp_name); |
| 2382 | } |
| 2383 | ga_clear(&cctx->ctx_imports); |
| 2384 | } |
| 2385 | |
| 2386 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2387 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 2388 | */ |
| 2389 | static int |
| 2390 | comment_start(char_u *p) |
| 2391 | { |
| 2392 | return p[0] == '#' && p[1] != '{'; |
| 2393 | } |
| 2394 | |
| 2395 | /* |
| 2396 | * Return a pointer to the next line that isn't empty or only contains a |
| 2397 | * comment. Skips over white space. |
| 2398 | * Returns NULL if there is none. |
| 2399 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2400 | char_u * |
| 2401 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2402 | { |
| 2403 | int lnum = cctx->ctx_lnum; |
| 2404 | |
| 2405 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2406 | { |
| 2407 | 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] | 2408 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2409 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 2410 | if (line == NULL) |
| 2411 | break; |
| 2412 | p = skipwhite(line); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2413 | if (*p != NUL && !comment_start(p)) |
| 2414 | return p; |
| 2415 | } |
| 2416 | return NULL; |
| 2417 | } |
| 2418 | |
| 2419 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2420 | * Called when checking for a following operator at "arg". When the rest of |
| 2421 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2422 | * line return a pointer to it and set "nextp". |
| 2423 | * Otherwise skip over white space. |
| 2424 | */ |
| 2425 | static char_u * |
| 2426 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2427 | { |
| 2428 | char_u *p = skipwhite(arg); |
| 2429 | |
| 2430 | *nextp = NULL; |
| 2431 | if (*p == NUL || (VIM_ISWHITE(*arg) && comment_start(p))) |
| 2432 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2433 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2434 | if (*nextp != NULL) |
| 2435 | return *nextp; |
| 2436 | } |
| 2437 | return p; |
| 2438 | } |
| 2439 | |
| 2440 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2441 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2442 | * 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] | 2443 | * Returns NULL when at the end. |
| 2444 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2445 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2446 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2447 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2448 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2449 | |
| 2450 | do |
| 2451 | { |
| 2452 | ++cctx->ctx_lnum; |
| 2453 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2454 | { |
| 2455 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2456 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2457 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2458 | 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] | 2459 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2460 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2461 | } while (line == NULL || *skipwhite(line) == NUL |
| 2462 | || (skip_comment && comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2463 | return line; |
| 2464 | } |
| 2465 | |
| 2466 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2467 | * 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] | 2468 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2469 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2470 | */ |
| 2471 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2472 | 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] | 2473 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2474 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2475 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2476 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2477 | |
| 2478 | if (next == NULL) |
| 2479 | return FAIL; |
| 2480 | *arg = skipwhite(next); |
| 2481 | } |
| 2482 | return OK; |
| 2483 | } |
| 2484 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2485 | // Structure passed between the compile_expr* functions to keep track of |
| 2486 | // constants that have been parsed but for which no code was produced yet. If |
| 2487 | // possible expressions on these constants are applied at compile time. If |
| 2488 | // that is not possible, the code to push the constants needs to be generated |
| 2489 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2490 | // Using 50 should be more than enough of 5 levels of (). |
| 2491 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2492 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2493 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2494 | int pp_used; // active entries in pp_tv[] |
| 2495 | } ppconst_T; |
| 2496 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2497 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2498 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2499 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2500 | /* |
| 2501 | * Generate a PUSH instruction for "tv". |
| 2502 | * "tv" will be consumed or cleared. |
| 2503 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2504 | */ |
| 2505 | static int |
| 2506 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2507 | { |
| 2508 | if (tv != NULL) |
| 2509 | { |
| 2510 | switch (tv->v_type) |
| 2511 | { |
| 2512 | case VAR_UNKNOWN: |
| 2513 | break; |
| 2514 | case VAR_BOOL: |
| 2515 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2516 | break; |
| 2517 | case VAR_SPECIAL: |
| 2518 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2519 | break; |
| 2520 | case VAR_NUMBER: |
| 2521 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2522 | break; |
| 2523 | #ifdef FEAT_FLOAT |
| 2524 | case VAR_FLOAT: |
| 2525 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2526 | break; |
| 2527 | #endif |
| 2528 | case VAR_BLOB: |
| 2529 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2530 | tv->vval.v_blob = NULL; |
| 2531 | break; |
| 2532 | case VAR_STRING: |
| 2533 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2534 | tv->vval.v_string = NULL; |
| 2535 | break; |
| 2536 | default: |
| 2537 | iemsg("constant type not supported"); |
| 2538 | clear_tv(tv); |
| 2539 | return FAIL; |
| 2540 | } |
| 2541 | tv->v_type = VAR_UNKNOWN; |
| 2542 | } |
| 2543 | return OK; |
| 2544 | } |
| 2545 | |
| 2546 | /* |
| 2547 | * Generate code for any ppconst entries. |
| 2548 | */ |
| 2549 | static int |
| 2550 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2551 | { |
| 2552 | int i; |
| 2553 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2554 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2555 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2556 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2557 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2558 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2559 | ret = FAIL; |
| 2560 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2561 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2562 | return ret; |
| 2563 | } |
| 2564 | |
| 2565 | /* |
| 2566 | * Clear ppconst constants. Used when failing. |
| 2567 | */ |
| 2568 | static void |
| 2569 | clear_ppconst(ppconst_T *ppconst) |
| 2570 | { |
| 2571 | int i; |
| 2572 | |
| 2573 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2574 | clear_tv(&ppconst->pp_tv[i]); |
| 2575 | ppconst->pp_used = 0; |
| 2576 | } |
| 2577 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2578 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2579 | * Generate an instruction to load script-local variable "name", without the |
| 2580 | * leading "s:". |
| 2581 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2582 | */ |
| 2583 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2584 | compile_load_scriptvar( |
| 2585 | cctx_T *cctx, |
| 2586 | char_u *name, // variable NUL terminated |
| 2587 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2588 | char_u **end, // end of variable |
| 2589 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2590 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2591 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2592 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 2593 | imported_T *import; |
| 2594 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2595 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2596 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2597 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2598 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2599 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2600 | } |
| 2601 | if (idx >= 0) |
| 2602 | { |
| 2603 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2604 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2605 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2606 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2607 | return OK; |
| 2608 | } |
| 2609 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2610 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2611 | if (import != NULL) |
| 2612 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2613 | if (import->imp_all) |
| 2614 | { |
| 2615 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2616 | char_u *exp_name; |
| 2617 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2618 | ufunc_T *ufunc; |
| 2619 | type_T *type; |
| 2620 | |
| 2621 | // Used "import * as Name", need to lookup the member. |
| 2622 | if (*p != '.') |
| 2623 | { |
| 2624 | semsg(_("E1060: expected dot after name: %s"), start); |
| 2625 | return FAIL; |
| 2626 | } |
| 2627 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2628 | if (VIM_ISWHITE(*p)) |
| 2629 | { |
| 2630 | emsg(_("E1074: no white space allowed after dot")); |
| 2631 | return FAIL; |
| 2632 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2633 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2634 | // isolate one name |
| 2635 | exp_name = p; |
| 2636 | while (eval_isnamec(*p)) |
| 2637 | ++p; |
| 2638 | cc = *p; |
| 2639 | *p = NUL; |
| 2640 | |
| 2641 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2642 | *p = cc; |
| 2643 | p = skipwhite(p); |
| 2644 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2645 | // TODO: what if it is a function? |
| 2646 | if (idx < 0) |
| 2647 | return FAIL; |
| 2648 | *end = p; |
| 2649 | |
| 2650 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2651 | import->imp_sid, |
| 2652 | idx, |
| 2653 | type); |
| 2654 | } |
| 2655 | else |
| 2656 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2657 | // TODO: check this is a variable, not a function? |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2658 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2659 | import->imp_sid, |
| 2660 | import->imp_var_vals_idx, |
| 2661 | import->imp_type); |
| 2662 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2663 | return OK; |
| 2664 | } |
| 2665 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2666 | if (error) |
| 2667 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2668 | return FAIL; |
| 2669 | } |
| 2670 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2671 | static int |
| 2672 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2673 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2674 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2675 | |
| 2676 | if (ufunc == NULL) |
| 2677 | return FAIL; |
| 2678 | |
Bram Moolenaar | fe465a0 | 2020-07-07 22:50:12 +0200 | [diff] [blame] | 2679 | return generate_PUSHFUNC(cctx, vim_strsave(ufunc->uf_name), |
| 2680 | ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2681 | } |
| 2682 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2683 | /* |
| 2684 | * Compile a variable name into a load instruction. |
| 2685 | * "end" points to just after the name. |
| 2686 | * When "error" is FALSE do not give an error when not found. |
| 2687 | */ |
| 2688 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2689 | 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] | 2690 | { |
| 2691 | type_T *type; |
| 2692 | char_u *name; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2693 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2694 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2695 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2696 | |
| 2697 | if (*(*arg + 1) == ':') |
| 2698 | { |
| 2699 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2700 | if (end <= *arg + 2) |
| 2701 | name = vim_strsave((char_u *)"[empty]"); |
| 2702 | else |
| 2703 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2704 | if (name == NULL) |
| 2705 | return FAIL; |
| 2706 | |
| 2707 | if (**arg == 'v') |
| 2708 | { |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2709 | res = generate_LOADV(cctx, name, error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2710 | } |
| 2711 | else if (**arg == 'g') |
| 2712 | { |
| 2713 | // Global variables can be defined later, thus we don't check if it |
| 2714 | // exists, give error at runtime. |
| 2715 | res = generate_LOAD(cctx, ISN_LOADG, 0, name, &t_any); |
| 2716 | } |
| 2717 | else if (**arg == 's') |
| 2718 | { |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2719 | res = compile_load_scriptvar(cctx, name, NULL, NULL, error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2720 | } |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2721 | else if (**arg == 'b') |
| 2722 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2723 | // Buffer-local variables can be defined later, thus we don't check |
| 2724 | // if it exists, give error at runtime. |
| 2725 | res = generate_LOAD(cctx, ISN_LOADB, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2726 | } |
| 2727 | else if (**arg == 'w') |
| 2728 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2729 | // Window-local variables can be defined later, thus we don't check |
| 2730 | // if it exists, give error at runtime. |
| 2731 | res = generate_LOAD(cctx, ISN_LOADW, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2732 | } |
| 2733 | else if (**arg == 't') |
| 2734 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2735 | // Tabpage-local variables can be defined later, thus we don't |
| 2736 | // check if it exists, give error at runtime. |
| 2737 | res = generate_LOAD(cctx, ISN_LOADT, 0, name, &t_any); |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2738 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2739 | else |
| 2740 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2741 | semsg("E1075: Namespace not supported: %s", *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2742 | goto theend; |
| 2743 | } |
| 2744 | } |
| 2745 | else |
| 2746 | { |
| 2747 | size_t len = end - *arg; |
| 2748 | int idx; |
| 2749 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2750 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2751 | |
| 2752 | name = vim_strnsave(*arg, end - *arg); |
| 2753 | if (name == NULL) |
| 2754 | return FAIL; |
| 2755 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2756 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2757 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2758 | if (!gen_load_outer) |
| 2759 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2760 | } |
| 2761 | else |
| 2762 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2763 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2764 | |
| 2765 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2766 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2767 | type = lvar->lv_type; |
| 2768 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2769 | if (lvar->lv_from_outer) |
| 2770 | gen_load_outer = TRUE; |
| 2771 | else |
| 2772 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2773 | } |
| 2774 | else |
| 2775 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2776 | // "var" can be script-local even without using "s:" if it |
| 2777 | // already exists. |
| 2778 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2779 | == SCRIPT_VERSION_VIM9 |
| 2780 | || lookup_script(*arg, len) == OK) |
| 2781 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2782 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2783 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2784 | // When the name starts with an uppercase letter or "x:" it |
| 2785 | // can be a user defined function. |
| 2786 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2787 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2788 | } |
| 2789 | } |
| 2790 | if (gen_load) |
| 2791 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2792 | if (gen_load_outer) |
| 2793 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | *arg = end; |
| 2797 | |
| 2798 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2799 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2800 | semsg(_(e_var_notfound), name); |
| 2801 | vim_free(name); |
| 2802 | return res; |
| 2803 | } |
| 2804 | |
| 2805 | /* |
| 2806 | * Compile the argument expressions. |
| 2807 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2808 | */ |
| 2809 | static int |
| 2810 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2811 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2812 | char_u *p = *arg; |
| 2813 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2814 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2815 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2816 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2817 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2818 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2819 | if (*p == ')') |
| 2820 | { |
| 2821 | *arg = p + 1; |
| 2822 | return OK; |
| 2823 | } |
| 2824 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2825 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2826 | return FAIL; |
| 2827 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2828 | |
| 2829 | if (*p != ',' && *skipwhite(p) == ',') |
| 2830 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2831 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2832 | p = skipwhite(p); |
| 2833 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2834 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2835 | { |
| 2836 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2837 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2838 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2839 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2840 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2841 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2842 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2843 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2844 | emsg(_(e_missing_close)); |
| 2845 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | /* |
| 2849 | * Compile a function call: name(arg1, arg2) |
| 2850 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2851 | * "argcount_init" is 1 for "value->method()" |
| 2852 | * Instructions: |
| 2853 | * EVAL arg1 |
| 2854 | * EVAL arg2 |
| 2855 | * BCALL / DCALL / UCALL |
| 2856 | */ |
| 2857 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2858 | compile_call( |
| 2859 | char_u **arg, |
| 2860 | size_t varlen, |
| 2861 | cctx_T *cctx, |
| 2862 | ppconst_T *ppconst, |
| 2863 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2864 | { |
| 2865 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2866 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2867 | int argcount = argcount_init; |
| 2868 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2869 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2870 | char_u *tofree = NULL; |
| 2871 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2872 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2873 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2874 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2875 | // we can evaluate "has('name')" at compile time |
| 2876 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2877 | { |
| 2878 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2879 | typval_T argvars[2]; |
| 2880 | |
| 2881 | argvars[0].v_type = VAR_UNKNOWN; |
| 2882 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2883 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2884 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2885 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2886 | s = skipwhite(s); |
| 2887 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 2888 | { |
| 2889 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2890 | |
| 2891 | *arg = s + 1; |
| 2892 | argvars[1].v_type = VAR_UNKNOWN; |
| 2893 | tv->v_type = VAR_NUMBER; |
| 2894 | tv->vval.v_number = 0; |
| 2895 | f_has(argvars, tv); |
| 2896 | clear_tv(&argvars[0]); |
| 2897 | ++ppconst->pp_used; |
| 2898 | return OK; |
| 2899 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2900 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2904 | return FAIL; |
| 2905 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2906 | if (varlen >= sizeof(namebuf)) |
| 2907 | { |
| 2908 | semsg(_("E1011: name too long: %s"), name); |
| 2909 | return FAIL; |
| 2910 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2911 | vim_strncpy(namebuf, *arg, varlen); |
| 2912 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2913 | |
| 2914 | *arg = skipwhite(*arg + varlen + 1); |
| 2915 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2916 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2917 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2918 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2919 | { |
| 2920 | int idx; |
| 2921 | |
| 2922 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2923 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2924 | if (idx >= 0) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2925 | res = generate_BCALL(cctx, idx, argcount); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2926 | else |
| 2927 | semsg(_(e_unknownfunc), namebuf); |
| 2928 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2929 | } |
| 2930 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2931 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2932 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2933 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2934 | { |
| 2935 | res = generate_CALL(cctx, ufunc, argcount); |
| 2936 | goto theend; |
| 2937 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2938 | |
| 2939 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2940 | // 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] | 2941 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2942 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 2943 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2944 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2945 | garray_T *stack = &cctx->ctx_type_stack; |
| 2946 | type_T *type; |
| 2947 | |
| 2948 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2949 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2950 | goto theend; |
| 2951 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2952 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2953 | // 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] | 2954 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2955 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 2956 | res = generate_UCALL(cctx, name, argcount); |
| 2957 | else |
| 2958 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2959 | |
| 2960 | theend: |
| 2961 | vim_free(tofree); |
| 2962 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2966 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2967 | |
| 2968 | /* |
| 2969 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2970 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2971 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2972 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2973 | * valid name. |
| 2974 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2975 | static char_u * |
| 2976 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2977 | { |
| 2978 | char_u *p; |
| 2979 | |
| 2980 | // Quick check for valid starting character. |
| 2981 | if (!eval_isnamec1(*arg)) |
| 2982 | return arg; |
| 2983 | |
| 2984 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2985 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2986 | // and can be used in slice "[n:]". |
| 2987 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2988 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2989 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2990 | break; |
| 2991 | return p; |
| 2992 | } |
| 2993 | |
| 2994 | /* |
| 2995 | * 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] | 2996 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2997 | */ |
| 2998 | char_u * |
| 2999 | to_name_const_end(char_u *arg) |
| 3000 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3001 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3002 | typval_T rettv; |
| 3003 | |
| 3004 | if (p == arg && *arg == '[') |
| 3005 | { |
| 3006 | |
| 3007 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3008 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3009 | p = arg; |
| 3010 | } |
| 3011 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 3012 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3013 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3014 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3015 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3016 | p = arg; |
| 3017 | } |
| 3018 | else if (p == arg && *arg == '{') |
| 3019 | { |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3020 | int ret = get_lambda_tv(&p, &rettv, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3021 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 3022 | // Can be "{x -> ret}()". |
| 3023 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3024 | if (ret == NOTDONE) |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 3025 | ret = eval_dict(&p, &rettv, NULL, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3026 | if (ret != OK) |
| 3027 | p = arg; |
| 3028 | } |
| 3029 | |
| 3030 | return p; |
| 3031 | } |
| 3032 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3033 | /* |
| 3034 | * parse a list: [expr, expr] |
| 3035 | * "*arg" points to the '['. |
| 3036 | */ |
| 3037 | static int |
| 3038 | compile_list(char_u **arg, cctx_T *cctx) |
| 3039 | { |
| 3040 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3041 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3042 | int count = 0; |
| 3043 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3044 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3045 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3046 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3047 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3048 | semsg(_(e_list_end), *arg); |
| 3049 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3050 | } |
| 3051 | if (*p == ']') |
| 3052 | { |
| 3053 | ++p; |
| 3054 | // Allow for following comment, after at least one space. |
| 3055 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '"') |
| 3056 | p += STRLEN(p); |
| 3057 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3058 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3059 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3060 | break; |
| 3061 | ++count; |
| 3062 | if (*p == ',') |
| 3063 | ++p; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3064 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3065 | p = skipwhite(p); |
| 3066 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3067 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3068 | |
| 3069 | generate_NEWLIST(cctx, count); |
| 3070 | return OK; |
| 3071 | } |
| 3072 | |
| 3073 | /* |
| 3074 | * parse a lambda: {arg, arg -> expr} |
| 3075 | * "*arg" points to the '{'. |
| 3076 | */ |
| 3077 | static int |
| 3078 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 3079 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3080 | typval_T rettv; |
| 3081 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3082 | evalarg_T evalarg; |
| 3083 | |
| 3084 | CLEAR_FIELD(evalarg); |
| 3085 | evalarg.eval_flags = EVAL_EVALUATE; |
| 3086 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3087 | |
| 3088 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3089 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3090 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3091 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3092 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3093 | ++ufunc->uf_refcount; |
| 3094 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3095 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3096 | |
| 3097 | // The function will have one line: "return {expr}". |
| 3098 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3099 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3100 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 3101 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 3102 | return generate_FUNCREF(cctx, ufunc->uf_dfunc_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3103 | return FAIL; |
| 3104 | } |
| 3105 | |
| 3106 | /* |
| 3107 | * Compile a lamda call: expr->{lambda}(args) |
| 3108 | * "arg" points to the "{". |
| 3109 | */ |
| 3110 | static int |
| 3111 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 3112 | { |
| 3113 | ufunc_T *ufunc; |
| 3114 | typval_T rettv; |
| 3115 | int argcount = 1; |
| 3116 | int ret = FAIL; |
| 3117 | |
| 3118 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 3119 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3120 | return FAIL; |
| 3121 | |
| 3122 | if (**arg != '(') |
| 3123 | { |
| 3124 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 3125 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3126 | else |
| 3127 | semsg(_(e_missing_paren), "lambda"); |
| 3128 | clear_tv(&rettv); |
| 3129 | return FAIL; |
| 3130 | } |
| 3131 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3132 | ufunc = rettv.vval.v_partial->pt_func; |
| 3133 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3134 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 3135 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3136 | |
| 3137 | // The function will have one line: "return {expr}". |
| 3138 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3139 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3140 | |
| 3141 | // compile the arguments |
| 3142 | *arg = skipwhite(*arg + 1); |
| 3143 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 3144 | // call the compiled function |
| 3145 | ret = generate_CALL(cctx, ufunc, argcount); |
| 3146 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3147 | return ret; |
| 3148 | } |
| 3149 | |
| 3150 | /* |
| 3151 | * parse a dict: {'key': val} or #{key: val} |
| 3152 | * "*arg" points to the '{'. |
| 3153 | */ |
| 3154 | static int |
| 3155 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 3156 | { |
| 3157 | garray_T *instr = &cctx->ctx_instr; |
| 3158 | int count = 0; |
| 3159 | dict_T *d = dict_alloc(); |
| 3160 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3161 | char_u *whitep = *arg; |
| 3162 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3163 | |
| 3164 | if (d == NULL) |
| 3165 | return FAIL; |
| 3166 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3167 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3168 | { |
| 3169 | char_u *key = NULL; |
| 3170 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3171 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3172 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3173 | *arg = NULL; |
| 3174 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3175 | } |
| 3176 | |
| 3177 | if (**arg == '}') |
| 3178 | break; |
| 3179 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3180 | if (literal) |
| 3181 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3182 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3183 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3184 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3185 | { |
| 3186 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 3187 | return FAIL; |
| 3188 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3189 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3190 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3191 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3192 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3193 | } |
| 3194 | else |
| 3195 | { |
| 3196 | isn_T *isn; |
| 3197 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3198 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3199 | return FAIL; |
| 3200 | // TODO: check type is string |
| 3201 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3202 | if (isn->isn_type == ISN_PUSHS) |
| 3203 | key = isn->isn_arg.string; |
| 3204 | } |
| 3205 | |
| 3206 | // Check for duplicate keys, if using string keys. |
| 3207 | if (key != NULL) |
| 3208 | { |
| 3209 | item = dict_find(d, key, -1); |
| 3210 | if (item != NULL) |
| 3211 | { |
| 3212 | semsg(_(e_duplicate_key), key); |
| 3213 | goto failret; |
| 3214 | } |
| 3215 | item = dictitem_alloc(key); |
| 3216 | if (item != NULL) |
| 3217 | { |
| 3218 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3219 | item->di_tv.v_lock = 0; |
| 3220 | if (dict_add(d, item) == FAIL) |
| 3221 | dictitem_free(item); |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | *arg = skipwhite(*arg); |
| 3226 | if (**arg != ':') |
| 3227 | { |
| 3228 | semsg(_(e_missing_dict_colon), *arg); |
| 3229 | return FAIL; |
| 3230 | } |
| 3231 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3232 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3233 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3234 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3235 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3236 | *arg = NULL; |
| 3237 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3238 | } |
| 3239 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3240 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3241 | return FAIL; |
| 3242 | ++count; |
| 3243 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3244 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3245 | *arg = skipwhite(*arg); |
| 3246 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3247 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3248 | *arg = NULL; |
| 3249 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3250 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3251 | if (**arg == '}') |
| 3252 | break; |
| 3253 | if (**arg != ',') |
| 3254 | { |
| 3255 | semsg(_(e_missing_dict_comma), *arg); |
| 3256 | goto failret; |
| 3257 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3258 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3259 | *arg = skipwhite(*arg + 1); |
| 3260 | } |
| 3261 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3262 | *arg = *arg + 1; |
| 3263 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3264 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3265 | p = skipwhite(*arg); |
| 3266 | if (VIM_ISWHITE(**arg) && (*p == '"' || comment_start(p))) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3267 | *arg += STRLEN(*arg); |
| 3268 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3269 | dict_unref(d); |
| 3270 | return generate_NEWDICT(cctx, count); |
| 3271 | |
| 3272 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3273 | if (*arg == NULL) |
| 3274 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3275 | dict_unref(d); |
| 3276 | return FAIL; |
| 3277 | } |
| 3278 | |
| 3279 | /* |
| 3280 | * Compile "&option". |
| 3281 | */ |
| 3282 | static int |
| 3283 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3284 | { |
| 3285 | typval_T rettv; |
| 3286 | char_u *start = *arg; |
| 3287 | int ret; |
| 3288 | |
| 3289 | // parse the option and get the current value to get the type. |
| 3290 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3291 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3292 | if (ret == OK) |
| 3293 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3294 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3295 | char_u *name = vim_strnsave(start, *arg - start); |
| 3296 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3297 | |
| 3298 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3299 | vim_free(name); |
| 3300 | } |
| 3301 | clear_tv(&rettv); |
| 3302 | |
| 3303 | return ret; |
| 3304 | } |
| 3305 | |
| 3306 | /* |
| 3307 | * Compile "$VAR". |
| 3308 | */ |
| 3309 | static int |
| 3310 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3311 | { |
| 3312 | char_u *start = *arg; |
| 3313 | int len; |
| 3314 | int ret; |
| 3315 | char_u *name; |
| 3316 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3317 | ++*arg; |
| 3318 | len = get_env_len(arg); |
| 3319 | if (len == 0) |
| 3320 | { |
| 3321 | semsg(_(e_syntax_at), start - 1); |
| 3322 | return FAIL; |
| 3323 | } |
| 3324 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3325 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3326 | name = vim_strnsave(start, len + 1); |
| 3327 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3328 | vim_free(name); |
| 3329 | return ret; |
| 3330 | } |
| 3331 | |
| 3332 | /* |
| 3333 | * Compile "@r". |
| 3334 | */ |
| 3335 | static int |
| 3336 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3337 | { |
| 3338 | int ret; |
| 3339 | |
| 3340 | ++*arg; |
| 3341 | if (**arg == NUL) |
| 3342 | { |
| 3343 | semsg(_(e_syntax_at), *arg - 1); |
| 3344 | return FAIL; |
| 3345 | } |
| 3346 | if (!valid_yank_reg(**arg, TRUE)) |
| 3347 | { |
| 3348 | emsg_invreg(**arg); |
| 3349 | return FAIL; |
| 3350 | } |
| 3351 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3352 | ++*arg; |
| 3353 | return ret; |
| 3354 | } |
| 3355 | |
| 3356 | /* |
| 3357 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 3358 | */ |
| 3359 | static int |
| 3360 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 3361 | { |
| 3362 | char_u *p = end; |
| 3363 | |
| 3364 | // this works from end to start |
| 3365 | while (p > start) |
| 3366 | { |
| 3367 | --p; |
| 3368 | if (*p == '-' || *p == '+') |
| 3369 | { |
| 3370 | // only '-' has an effect, for '+' we only check the type |
| 3371 | #ifdef FEAT_FLOAT |
| 3372 | if (rettv->v_type == VAR_FLOAT) |
| 3373 | { |
| 3374 | if (*p == '-') |
| 3375 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3376 | } |
| 3377 | else |
| 3378 | #endif |
| 3379 | { |
| 3380 | varnumber_T val; |
| 3381 | int error = FALSE; |
| 3382 | |
| 3383 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3384 | // here |
| 3385 | if (check_not_string(rettv) == FAIL) |
| 3386 | return FAIL; |
| 3387 | val = tv_get_number_chk(rettv, &error); |
| 3388 | clear_tv(rettv); |
| 3389 | if (error) |
| 3390 | return FAIL; |
| 3391 | if (*p == '-') |
| 3392 | val = -val; |
| 3393 | rettv->v_type = VAR_NUMBER; |
| 3394 | rettv->vval.v_number = val; |
| 3395 | } |
| 3396 | } |
| 3397 | else |
| 3398 | { |
| 3399 | int v = tv2bool(rettv); |
| 3400 | |
| 3401 | // '!' is permissive in the type. |
| 3402 | clear_tv(rettv); |
| 3403 | rettv->v_type = VAR_BOOL; |
| 3404 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3405 | } |
| 3406 | } |
| 3407 | return OK; |
| 3408 | } |
| 3409 | |
| 3410 | /* |
| 3411 | * Recognize v: variables that are constants and set "rettv". |
| 3412 | */ |
| 3413 | static void |
| 3414 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3415 | { |
| 3416 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3417 | { |
| 3418 | rettv->v_type = VAR_BOOL; |
| 3419 | rettv->vval.v_number = VVAL_TRUE; |
| 3420 | *arg += 6; |
| 3421 | } |
| 3422 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3423 | { |
| 3424 | rettv->v_type = VAR_BOOL; |
| 3425 | rettv->vval.v_number = VVAL_FALSE; |
| 3426 | *arg += 7; |
| 3427 | } |
| 3428 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3429 | { |
| 3430 | rettv->v_type = VAR_SPECIAL; |
| 3431 | rettv->vval.v_number = VVAL_NULL; |
| 3432 | *arg += 6; |
| 3433 | } |
| 3434 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3435 | { |
| 3436 | rettv->v_type = VAR_SPECIAL; |
| 3437 | rettv->vval.v_number = VVAL_NONE; |
| 3438 | *arg += 6; |
| 3439 | } |
| 3440 | } |
| 3441 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3442 | static exptype_T |
| 3443 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3444 | { |
| 3445 | exptype_T type = EXPR_UNKNOWN; |
| 3446 | int i; |
| 3447 | |
| 3448 | switch (p[0]) |
| 3449 | { |
| 3450 | case '=': if (p[1] == '=') |
| 3451 | type = EXPR_EQUAL; |
| 3452 | else if (p[1] == '~') |
| 3453 | type = EXPR_MATCH; |
| 3454 | break; |
| 3455 | case '!': if (p[1] == '=') |
| 3456 | type = EXPR_NEQUAL; |
| 3457 | else if (p[1] == '~') |
| 3458 | type = EXPR_NOMATCH; |
| 3459 | break; |
| 3460 | case '>': if (p[1] != '=') |
| 3461 | { |
| 3462 | type = EXPR_GREATER; |
| 3463 | *len = 1; |
| 3464 | } |
| 3465 | else |
| 3466 | type = EXPR_GEQUAL; |
| 3467 | break; |
| 3468 | case '<': if (p[1] != '=') |
| 3469 | { |
| 3470 | type = EXPR_SMALLER; |
| 3471 | *len = 1; |
| 3472 | } |
| 3473 | else |
| 3474 | type = EXPR_SEQUAL; |
| 3475 | break; |
| 3476 | case 'i': if (p[1] == 's') |
| 3477 | { |
| 3478 | // "is" and "isnot"; but not a prefix of a name |
| 3479 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3480 | *len = 5; |
| 3481 | i = p[*len]; |
| 3482 | if (!isalnum(i) && i != '_') |
| 3483 | { |
| 3484 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3485 | *type_is = TRUE; |
| 3486 | } |
| 3487 | } |
| 3488 | break; |
| 3489 | } |
| 3490 | return type; |
| 3491 | } |
| 3492 | |
| 3493 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3494 | * Compile code to apply '-', '+' and '!'. |
| 3495 | */ |
| 3496 | static int |
| 3497 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 3498 | { |
| 3499 | char_u *p = end; |
| 3500 | |
| 3501 | // this works from end to start |
| 3502 | while (p > start) |
| 3503 | { |
| 3504 | --p; |
| 3505 | if (*p == '-' || *p == '+') |
| 3506 | { |
| 3507 | int negate = *p == '-'; |
| 3508 | isn_T *isn; |
| 3509 | |
| 3510 | // TODO: check type |
| 3511 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3512 | { |
| 3513 | --p; |
| 3514 | if (*p == '-') |
| 3515 | negate = !negate; |
| 3516 | } |
| 3517 | // only '-' has an effect, for '+' we only check the type |
| 3518 | if (negate) |
| 3519 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3520 | else |
| 3521 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3522 | if (isn == NULL) |
| 3523 | return FAIL; |
| 3524 | } |
| 3525 | else |
| 3526 | { |
| 3527 | int invert = TRUE; |
| 3528 | |
| 3529 | while (p > start && p[-1] == '!') |
| 3530 | { |
| 3531 | --p; |
| 3532 | invert = !invert; |
| 3533 | } |
| 3534 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3535 | return FAIL; |
| 3536 | } |
| 3537 | } |
| 3538 | return OK; |
| 3539 | } |
| 3540 | |
| 3541 | /* |
| 3542 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3543 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3544 | */ |
| 3545 | static int |
| 3546 | compile_subscript( |
| 3547 | char_u **arg, |
| 3548 | cctx_T *cctx, |
| 3549 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3550 | char_u *end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3551 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3552 | { |
| 3553 | for (;;) |
| 3554 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3555 | char_u *p = skipwhite(*arg); |
| 3556 | |
| 3557 | if (*p == NUL || (VIM_ISWHITE(**arg) && comment_start(p))) |
| 3558 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3559 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3560 | |
| 3561 | // If a following line starts with "->{" or "->X" advance to that |
| 3562 | // line, so that a line break before "->" is allowed. |
| 3563 | if (next != NULL && next[0] == '-' && next[1] == '>' |
| 3564 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
| 3565 | { |
| 3566 | next = next_line_from_context(cctx, TRUE); |
| 3567 | if (next == NULL) |
| 3568 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3569 | *arg = next; |
| 3570 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3571 | } |
| 3572 | } |
| 3573 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3574 | if (*p == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3575 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3576 | garray_T *stack = &cctx->ctx_type_stack; |
| 3577 | type_T *type; |
| 3578 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3579 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3580 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3581 | return FAIL; |
| 3582 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3583 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3584 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3585 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3586 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3587 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3588 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3589 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3590 | return FAIL; |
| 3591 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3592 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3593 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3594 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3595 | return FAIL; |
| 3596 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3597 | // something->method() |
| 3598 | // Apply the '!', '-' and '+' first: |
| 3599 | // -1.0->func() works like (-1.0)->func() |
| 3600 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 3601 | return FAIL; |
| 3602 | *start_leader = end_leader; // don't apply again later |
| 3603 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3604 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3605 | *arg = skipwhite(p); |
| 3606 | if (may_get_next_line(p, arg, cctx) == FAIL) |
| 3607 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3608 | if (**arg == '{') |
| 3609 | { |
| 3610 | // lambda call: list->{lambda} |
| 3611 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3612 | return FAIL; |
| 3613 | } |
| 3614 | else |
| 3615 | { |
| 3616 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3617 | p = *arg; |
| 3618 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3619 | p += 2; |
| 3620 | for ( ; eval_isnamec1(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3621 | ; |
| 3622 | if (*p != '(') |
| 3623 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3624 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3625 | return FAIL; |
| 3626 | } |
| 3627 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3628 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3629 | return FAIL; |
| 3630 | } |
| 3631 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3632 | else if (*p == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3633 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3634 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3635 | type_T **typep; |
| 3636 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3637 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3638 | // dict member: dict[key] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3639 | // TODO: blob index |
| 3640 | // TODO: more arguments |
| 3641 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3642 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3643 | return FAIL; |
| 3644 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3645 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3646 | *arg = skipwhite(p); |
| 3647 | if (may_get_next_line(p, arg, cctx) == FAIL) |
| 3648 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3649 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3650 | return FAIL; |
| 3651 | |
| 3652 | if (**arg != ']') |
| 3653 | { |
| 3654 | emsg(_(e_missbrac)); |
| 3655 | return FAIL; |
| 3656 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3657 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3658 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3659 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
| 3660 | if ((*typep)->tt_type == VAR_LIST || (*typep) == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3661 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3662 | if ((*typep)->tt_type == VAR_LIST) |
| 3663 | *typep = (*typep)->tt_member; |
| 3664 | if (generate_instr_drop(cctx, ISN_INDEX, 1) == FAIL) |
| 3665 | return FAIL; |
| 3666 | } |
| 3667 | else if ((*typep)->tt_type == VAR_DICT) |
| 3668 | { |
| 3669 | *typep = (*typep)->tt_member; |
| 3670 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3671 | return FAIL; |
| 3672 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3673 | return FAIL; |
| 3674 | } |
| 3675 | else |
| 3676 | { |
| 3677 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3678 | return FAIL; |
| 3679 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3680 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3681 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3682 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3683 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3684 | return FAIL; |
| 3685 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3686 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3687 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3688 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3689 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3690 | p = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3691 | if (eval_isnamec1(*p)) |
| 3692 | while (eval_isnamec(*p)) |
| 3693 | MB_PTR_ADV(p); |
| 3694 | if (p == *arg) |
| 3695 | { |
| 3696 | semsg(_(e_syntax_at), *arg); |
| 3697 | return FAIL; |
| 3698 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3699 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3700 | return FAIL; |
| 3701 | *arg = p; |
| 3702 | } |
| 3703 | else |
| 3704 | break; |
| 3705 | } |
| 3706 | |
| 3707 | // TODO - see handle_subscript(): |
| 3708 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3709 | // Don't do this when "Func" is already a partial that was bound |
| 3710 | // explicitly (pt_auto is FALSE). |
| 3711 | |
| 3712 | return OK; |
| 3713 | } |
| 3714 | |
| 3715 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3716 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3717 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3718 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3719 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3720 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3721 | * |
| 3722 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3723 | */ |
| 3724 | |
| 3725 | /* |
| 3726 | * number number constant |
| 3727 | * 0zFFFFFFFF Blob constant |
| 3728 | * "string" string constant |
| 3729 | * 'string' literal string constant |
| 3730 | * &option-name option value |
| 3731 | * @r register contents |
| 3732 | * identifier variable value |
| 3733 | * function() function call |
| 3734 | * $VAR environment variable |
| 3735 | * (expression) nested expression |
| 3736 | * [expr, expr] List |
| 3737 | * {key: val, key: val} Dictionary |
| 3738 | * #{key: val, key: val} Dictionary with literal keys |
| 3739 | * |
| 3740 | * Also handle: |
| 3741 | * ! in front logical NOT |
| 3742 | * - in front unary minus |
| 3743 | * + in front unary plus (ignored) |
| 3744 | * trailing (arg) funcref/partial call |
| 3745 | * trailing [] subscript in String or List |
| 3746 | * trailing .name entry in Dictionary |
| 3747 | * trailing ->name() method call |
| 3748 | */ |
| 3749 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3750 | compile_expr7( |
| 3751 | char_u **arg, |
| 3752 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3753 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3754 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3755 | char_u *start_leader, *end_leader; |
| 3756 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3757 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3758 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3759 | |
| 3760 | /* |
| 3761 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3762 | */ |
| 3763 | start_leader = *arg; |
| 3764 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3765 | *arg = skipwhite(*arg + 1); |
| 3766 | end_leader = *arg; |
| 3767 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3768 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3769 | switch (**arg) |
| 3770 | { |
| 3771 | /* |
| 3772 | * Number constant. |
| 3773 | */ |
| 3774 | case '0': // also for blob starting with 0z |
| 3775 | case '1': |
| 3776 | case '2': |
| 3777 | case '3': |
| 3778 | case '4': |
| 3779 | case '5': |
| 3780 | case '6': |
| 3781 | case '7': |
| 3782 | case '8': |
| 3783 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3784 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3785 | return FAIL; |
| 3786 | break; |
| 3787 | |
| 3788 | /* |
| 3789 | * String constant: "string". |
| 3790 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3791 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3792 | return FAIL; |
| 3793 | break; |
| 3794 | |
| 3795 | /* |
| 3796 | * Literal string constant: 'str''ing'. |
| 3797 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3798 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3799 | return FAIL; |
| 3800 | break; |
| 3801 | |
| 3802 | /* |
| 3803 | * Constant Vim variable. |
| 3804 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3805 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3806 | ret = NOTDONE; |
| 3807 | break; |
| 3808 | |
| 3809 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3810 | * "true" constant |
| 3811 | */ |
| 3812 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3813 | && !eval_isnamec((*arg)[4])) |
| 3814 | { |
| 3815 | *arg += 4; |
| 3816 | rettv->v_type = VAR_BOOL; |
| 3817 | rettv->vval.v_number = VVAL_TRUE; |
| 3818 | } |
| 3819 | else |
| 3820 | ret = NOTDONE; |
| 3821 | break; |
| 3822 | |
| 3823 | /* |
| 3824 | * "false" constant |
| 3825 | */ |
| 3826 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3827 | && !eval_isnamec((*arg)[5])) |
| 3828 | { |
| 3829 | *arg += 5; |
| 3830 | rettv->v_type = VAR_BOOL; |
| 3831 | rettv->vval.v_number = VVAL_FALSE; |
| 3832 | } |
| 3833 | else |
| 3834 | ret = NOTDONE; |
| 3835 | break; |
| 3836 | |
| 3837 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3838 | * List: [expr, expr] |
| 3839 | */ |
| 3840 | case '[': ret = compile_list(arg, cctx); |
| 3841 | break; |
| 3842 | |
| 3843 | /* |
| 3844 | * Dictionary: #{key: val, key: val} |
| 3845 | */ |
| 3846 | case '#': if ((*arg)[1] == '{') |
| 3847 | { |
| 3848 | ++*arg; |
| 3849 | ret = compile_dict(arg, cctx, TRUE); |
| 3850 | } |
| 3851 | else |
| 3852 | ret = NOTDONE; |
| 3853 | break; |
| 3854 | |
| 3855 | /* |
| 3856 | * Lambda: {arg, arg -> expr} |
| 3857 | * Dictionary: {'key': val, 'key': val} |
| 3858 | */ |
| 3859 | case '{': { |
| 3860 | char_u *start = skipwhite(*arg + 1); |
| 3861 | |
| 3862 | // Find out what comes after the arguments. |
| 3863 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3864 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3865 | if (ret != FAIL && *start == '>') |
| 3866 | ret = compile_lambda(arg, cctx); |
| 3867 | else |
| 3868 | ret = compile_dict(arg, cctx, FALSE); |
| 3869 | } |
| 3870 | break; |
| 3871 | |
| 3872 | /* |
| 3873 | * Option value: &name |
| 3874 | */ |
| 3875 | case '&': ret = compile_get_option(arg, cctx); |
| 3876 | break; |
| 3877 | |
| 3878 | /* |
| 3879 | * Environment variable: $VAR. |
| 3880 | */ |
| 3881 | case '$': ret = compile_get_env(arg, cctx); |
| 3882 | break; |
| 3883 | |
| 3884 | /* |
| 3885 | * Register contents: @r. |
| 3886 | */ |
| 3887 | case '@': ret = compile_get_register(arg, cctx); |
| 3888 | break; |
| 3889 | /* |
| 3890 | * nested expression: (expression). |
| 3891 | */ |
| 3892 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3893 | |
| 3894 | // recursive! |
| 3895 | if (ppconst->pp_used <= PPSIZE - 10) |
| 3896 | { |
| 3897 | ret = compile_expr1(arg, cctx, ppconst); |
| 3898 | } |
| 3899 | else |
| 3900 | { |
| 3901 | // Not enough space in ppconst, flush constants. |
| 3902 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3903 | return FAIL; |
| 3904 | ret = compile_expr0(arg, cctx); |
| 3905 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3906 | *arg = skipwhite(*arg); |
| 3907 | if (**arg == ')') |
| 3908 | ++*arg; |
| 3909 | else if (ret == OK) |
| 3910 | { |
| 3911 | emsg(_(e_missing_close)); |
| 3912 | ret = FAIL; |
| 3913 | } |
| 3914 | break; |
| 3915 | |
| 3916 | default: ret = NOTDONE; |
| 3917 | break; |
| 3918 | } |
| 3919 | if (ret == FAIL) |
| 3920 | return FAIL; |
| 3921 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3922 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3923 | { |
| 3924 | // apply the '!', '-' and '+' before the constant |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3925 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3926 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3927 | clear_tv(rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3928 | return FAIL; |
| 3929 | } |
| 3930 | start_leader = end_leader; // don't apply again below |
| 3931 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3932 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3933 | clear_tv(rettv); |
| 3934 | else |
| 3935 | // A constant expression can possibly be handled compile time, |
| 3936 | // return the value instead of generating code. |
| 3937 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3938 | } |
| 3939 | else if (ret == NOTDONE) |
| 3940 | { |
| 3941 | char_u *p; |
| 3942 | int r; |
| 3943 | |
| 3944 | if (!eval_isnamec1(**arg)) |
| 3945 | { |
| 3946 | semsg(_("E1015: Name expected: %s"), *arg); |
| 3947 | return FAIL; |
| 3948 | } |
| 3949 | |
| 3950 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3951 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3952 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3953 | { |
| 3954 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 3955 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3956 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3957 | { |
| 3958 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3959 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3960 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3961 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3962 | if (r == FAIL) |
| 3963 | return FAIL; |
| 3964 | } |
| 3965 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3966 | // Handle following "[]", ".member", etc. |
| 3967 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3968 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3969 | ppconst) == FAIL) |
| 3970 | return FAIL; |
| 3971 | if (ppconst->pp_used > 0) |
| 3972 | { |
| 3973 | // apply the '!', '-' and '+' before the constant |
| 3974 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3975 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
| 3976 | return FAIL; |
| 3977 | return OK; |
| 3978 | } |
| 3979 | if (compile_leader(cctx, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3980 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3981 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | /* |
| 3985 | * * number multiplication |
| 3986 | * / number division |
| 3987 | * % number modulo |
| 3988 | */ |
| 3989 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3990 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3991 | { |
| 3992 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3993 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3994 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3995 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3996 | // get the first expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3997 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3998 | return FAIL; |
| 3999 | |
| 4000 | /* |
| 4001 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4002 | */ |
| 4003 | for (;;) |
| 4004 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4005 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4006 | if (*op != '*' && *op != '/' && *op != '%') |
| 4007 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4008 | if (next != NULL) |
| 4009 | { |
| 4010 | *arg = next_line_from_context(cctx, TRUE); |
| 4011 | op = skipwhite(*arg); |
| 4012 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4013 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4014 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4015 | { |
| 4016 | char_u buf[3]; |
| 4017 | |
| 4018 | vim_strncpy(buf, op, 1); |
| 4019 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4020 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4021 | } |
| 4022 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4023 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4024 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4025 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4026 | // get the second expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4027 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4028 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4029 | |
| 4030 | if (ppconst->pp_used == ppconst_used + 2 |
| 4031 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4032 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4033 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4034 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4035 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4036 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4037 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4038 | // both are numbers: compute the result |
| 4039 | switch (*op) |
| 4040 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4041 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4042 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4043 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4044 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4045 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4046 | break; |
| 4047 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4048 | tv1->vval.v_number = res; |
| 4049 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4050 | } |
| 4051 | else |
| 4052 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4053 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4054 | generate_two_op(cctx, op); |
| 4055 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | return OK; |
| 4059 | } |
| 4060 | |
| 4061 | /* |
| 4062 | * + number addition |
| 4063 | * - number subtraction |
| 4064 | * .. string concatenation |
| 4065 | */ |
| 4066 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4067 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4068 | { |
| 4069 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4070 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4071 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4072 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4073 | |
| 4074 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4075 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4076 | return FAIL; |
| 4077 | |
| 4078 | /* |
| 4079 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4080 | */ |
| 4081 | for (;;) |
| 4082 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4083 | op = may_peek_next_line(cctx, *arg, &next); |
| 4084 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4085 | break; |
| 4086 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4087 | if (next != NULL) |
| 4088 | { |
| 4089 | *arg = next_line_from_context(cctx, TRUE); |
| 4090 | op = skipwhite(*arg); |
| 4091 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4092 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4093 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4094 | { |
| 4095 | char_u buf[3]; |
| 4096 | |
| 4097 | vim_strncpy(buf, op, oplen); |
| 4098 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4099 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4103 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4104 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4105 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4106 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4107 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4108 | return FAIL; |
| 4109 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4110 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4111 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4112 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4113 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4114 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4115 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4116 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4117 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4118 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4119 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4120 | // concat/subtract/add constant numbers |
| 4121 | if (*op == '+') |
| 4122 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4123 | else if (*op == '-') |
| 4124 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4125 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4126 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4127 | // concatenate constant strings |
| 4128 | char_u *s1 = tv1->vval.v_string; |
| 4129 | char_u *s2 = tv2->vval.v_string; |
| 4130 | size_t len1 = STRLEN(s1); |
| 4131 | |
| 4132 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4133 | if (tv1->vval.v_string == NULL) |
| 4134 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4135 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4136 | return FAIL; |
| 4137 | } |
| 4138 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4139 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4140 | vim_free(s1); |
| 4141 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4142 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4143 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4144 | } |
| 4145 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4146 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4147 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4148 | if (*op == '.') |
| 4149 | { |
| 4150 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4151 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4152 | return FAIL; |
| 4153 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4154 | } |
| 4155 | else |
| 4156 | generate_two_op(cctx, op); |
| 4157 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | return OK; |
| 4161 | } |
| 4162 | |
| 4163 | /* |
| 4164 | * expr5a == expr5b |
| 4165 | * expr5a =~ expr5b |
| 4166 | * expr5a != expr5b |
| 4167 | * expr5a !~ expr5b |
| 4168 | * expr5a > expr5b |
| 4169 | * expr5a >= expr5b |
| 4170 | * expr5a < expr5b |
| 4171 | * expr5a <= expr5b |
| 4172 | * expr5a is expr5b |
| 4173 | * expr5a isnot expr5b |
| 4174 | * |
| 4175 | * Produces instructions: |
| 4176 | * EVAL expr5a Push result of "expr5a" |
| 4177 | * EVAL expr5b Push result of "expr5b" |
| 4178 | * COMPARE one of the compare instructions |
| 4179 | */ |
| 4180 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4181 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4182 | { |
| 4183 | exptype_T type = EXPR_UNKNOWN; |
| 4184 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4185 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4186 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4187 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4188 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4189 | |
| 4190 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4191 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4192 | return FAIL; |
| 4193 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4194 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4195 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4196 | |
| 4197 | /* |
| 4198 | * If there is a comparative operator, use it. |
| 4199 | */ |
| 4200 | if (type != EXPR_UNKNOWN) |
| 4201 | { |
| 4202 | int ic = FALSE; // Default: do not ignore case |
| 4203 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4204 | if (next != NULL) |
| 4205 | { |
| 4206 | *arg = next_line_from_context(cctx, TRUE); |
| 4207 | p = skipwhite(*arg); |
| 4208 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4209 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4210 | { |
| 4211 | semsg(_(e_invexpr2), *arg); |
| 4212 | return FAIL; |
| 4213 | } |
| 4214 | // extra question mark appended: ignore case |
| 4215 | if (p[len] == '?') |
| 4216 | { |
| 4217 | ic = TRUE; |
| 4218 | ++len; |
| 4219 | } |
| 4220 | // extra '#' appended: match case (ignored) |
| 4221 | else if (p[len] == '#') |
| 4222 | ++len; |
| 4223 | // nothing appended: match case |
| 4224 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4225 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4226 | { |
| 4227 | char_u buf[7]; |
| 4228 | |
| 4229 | vim_strncpy(buf, p, len); |
| 4230 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4231 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4232 | } |
| 4233 | |
| 4234 | // get the second variable |
| 4235 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4236 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4237 | return FAIL; |
| 4238 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4239 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4240 | return FAIL; |
| 4241 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4242 | if (ppconst->pp_used == ppconst_used + 2) |
| 4243 | { |
| 4244 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4245 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4246 | int ret; |
| 4247 | |
| 4248 | // Both sides are a constant, compute the result now. |
| 4249 | // First check for a valid combination of types, this is more |
| 4250 | // strict than typval_compare(). |
| 4251 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 4252 | ret = FAIL; |
| 4253 | else |
| 4254 | { |
| 4255 | ret = typval_compare(tv1, tv2, type, ic); |
| 4256 | tv1->v_type = VAR_BOOL; |
| 4257 | tv1->vval.v_number = tv1->vval.v_number |
| 4258 | ? VVAL_TRUE : VVAL_FALSE; |
| 4259 | clear_tv(tv2); |
| 4260 | --ppconst->pp_used; |
| 4261 | } |
| 4262 | return ret; |
| 4263 | } |
| 4264 | |
| 4265 | generate_ppconst(cctx, ppconst); |
| 4266 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | return OK; |
| 4270 | } |
| 4271 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4272 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4273 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4274 | /* |
| 4275 | * Compile || or &&. |
| 4276 | */ |
| 4277 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4278 | compile_and_or( |
| 4279 | char_u **arg, |
| 4280 | cctx_T *cctx, |
| 4281 | char *op, |
| 4282 | ppconst_T *ppconst, |
| 4283 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4284 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4285 | char_u *next; |
| 4286 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4287 | int opchar = *op; |
| 4288 | |
| 4289 | if (p[0] == opchar && p[1] == opchar) |
| 4290 | { |
| 4291 | garray_T *instr = &cctx->ctx_instr; |
| 4292 | garray_T end_ga; |
| 4293 | |
| 4294 | /* |
| 4295 | * Repeat until there is no following "||" or "&&" |
| 4296 | */ |
| 4297 | ga_init2(&end_ga, sizeof(int), 10); |
| 4298 | while (p[0] == opchar && p[1] == opchar) |
| 4299 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4300 | if (next != NULL) |
| 4301 | { |
| 4302 | *arg = next_line_from_context(cctx, TRUE); |
| 4303 | p = skipwhite(*arg); |
| 4304 | } |
| 4305 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4306 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4307 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4308 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4309 | return FAIL; |
| 4310 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4311 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4312 | // TODO: use ppconst if the value is a constant |
| 4313 | generate_ppconst(cctx, ppconst); |
| 4314 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4315 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4316 | { |
| 4317 | ga_clear(&end_ga); |
| 4318 | return FAIL; |
| 4319 | } |
| 4320 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4321 | ++end_ga.ga_len; |
| 4322 | generate_JUMP(cctx, opchar == '|' |
| 4323 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 4324 | |
| 4325 | // eval the next expression |
| 4326 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4327 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4328 | return FAIL; |
| 4329 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4330 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4331 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4332 | { |
| 4333 | ga_clear(&end_ga); |
| 4334 | return FAIL; |
| 4335 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4336 | |
| 4337 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4338 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4339 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4340 | |
| 4341 | // Fill in the end label in all jumps. |
| 4342 | while (end_ga.ga_len > 0) |
| 4343 | { |
| 4344 | isn_T *isn; |
| 4345 | |
| 4346 | --end_ga.ga_len; |
| 4347 | isn = ((isn_T *)instr->ga_data) |
| 4348 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4349 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4350 | } |
| 4351 | ga_clear(&end_ga); |
| 4352 | } |
| 4353 | |
| 4354 | return OK; |
| 4355 | } |
| 4356 | |
| 4357 | /* |
| 4358 | * expr4a && expr4a && expr4a logical AND |
| 4359 | * |
| 4360 | * Produces instructions: |
| 4361 | * EVAL expr4a Push result of "expr4a" |
| 4362 | * JUMP_AND_KEEP_IF_FALSE end |
| 4363 | * EVAL expr4b Push result of "expr4b" |
| 4364 | * JUMP_AND_KEEP_IF_FALSE end |
| 4365 | * EVAL expr4c Push result of "expr4c" |
| 4366 | * end: |
| 4367 | */ |
| 4368 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4369 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4370 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4371 | int ppconst_used = ppconst->pp_used; |
| 4372 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4373 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4374 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4375 | return FAIL; |
| 4376 | |
| 4377 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4378 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4379 | } |
| 4380 | |
| 4381 | /* |
| 4382 | * expr3a || expr3b || expr3c logical OR |
| 4383 | * |
| 4384 | * Produces instructions: |
| 4385 | * EVAL expr3a Push result of "expr3a" |
| 4386 | * JUMP_AND_KEEP_IF_TRUE end |
| 4387 | * EVAL expr3b Push result of "expr3b" |
| 4388 | * JUMP_AND_KEEP_IF_TRUE end |
| 4389 | * EVAL expr3c Push result of "expr3c" |
| 4390 | * end: |
| 4391 | */ |
| 4392 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4393 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4394 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4395 | int ppconst_used = ppconst->pp_used; |
| 4396 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4397 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4398 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4399 | return FAIL; |
| 4400 | |
| 4401 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4402 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4403 | } |
| 4404 | |
| 4405 | /* |
| 4406 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4407 | * |
| 4408 | * Produces instructions: |
| 4409 | * EVAL expr2 Push result of "expr" |
| 4410 | * JUMP_IF_FALSE alt jump if false |
| 4411 | * EVAL expr1a |
| 4412 | * JUMP_ALWAYS end |
| 4413 | * alt: EVAL expr1b |
| 4414 | * end: |
| 4415 | */ |
| 4416 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4417 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4418 | { |
| 4419 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4420 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4421 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4422 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4423 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4424 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4425 | return FAIL; |
| 4426 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4427 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4428 | if (*p == '?') |
| 4429 | { |
| 4430 | garray_T *instr = &cctx->ctx_instr; |
| 4431 | garray_T *stack = &cctx->ctx_type_stack; |
| 4432 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4433 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4434 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4435 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4436 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4437 | int has_const_expr = FALSE; |
| 4438 | int const_value = FALSE; |
| 4439 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4440 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4441 | if (next != NULL) |
| 4442 | { |
| 4443 | *arg = next_line_from_context(cctx, TRUE); |
| 4444 | p = skipwhite(*arg); |
| 4445 | } |
| 4446 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4447 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4448 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4449 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4450 | return FAIL; |
| 4451 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4452 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4453 | if (ppconst->pp_used == ppconst_used + 1) |
| 4454 | { |
| 4455 | // the condition is a constant, we know whether the ? or the : |
| 4456 | // expression is to be evaluated. |
| 4457 | has_const_expr = TRUE; |
| 4458 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4459 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4460 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4461 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4462 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4463 | } |
| 4464 | else |
| 4465 | { |
| 4466 | generate_ppconst(cctx, ppconst); |
| 4467 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4468 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4469 | |
| 4470 | // evaluate the second expression; any type is accepted |
| 4471 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4472 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4473 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4474 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4475 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4476 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4477 | if (!has_const_expr) |
| 4478 | { |
| 4479 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4480 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4481 | // remember the type and drop it |
| 4482 | --stack->ga_len; |
| 4483 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4484 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4485 | end_idx = instr->ga_len; |
| 4486 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4487 | |
| 4488 | // jump here from JUMP_IF_FALSE |
| 4489 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4490 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4491 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4492 | |
| 4493 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4494 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4495 | if (*p != ':') |
| 4496 | { |
| 4497 | emsg(_(e_missing_colon)); |
| 4498 | return FAIL; |
| 4499 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4500 | if (next != NULL) |
| 4501 | { |
| 4502 | *arg = next_line_from_context(cctx, TRUE); |
| 4503 | p = skipwhite(*arg); |
| 4504 | } |
| 4505 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4506 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4507 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4508 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4509 | return FAIL; |
| 4510 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4511 | |
| 4512 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4513 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4514 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4515 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4516 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4517 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4518 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4519 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4520 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4521 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4522 | if (!has_const_expr) |
| 4523 | { |
| 4524 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4525 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4526 | // If the types differ, the result has a more generic type. |
| 4527 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4528 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4529 | |
| 4530 | // jump here from JUMP_ALWAYS |
| 4531 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4532 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4533 | } |
| 4534 | |
| 4535 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4536 | } |
| 4537 | return OK; |
| 4538 | } |
| 4539 | |
| 4540 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4541 | * Toplevel expression. |
| 4542 | */ |
| 4543 | static int |
| 4544 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4545 | { |
| 4546 | ppconst_T ppconst; |
| 4547 | |
| 4548 | CLEAR_FIELD(ppconst); |
| 4549 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4550 | { |
| 4551 | clear_ppconst(&ppconst); |
| 4552 | return FAIL; |
| 4553 | } |
| 4554 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4555 | return FAIL; |
| 4556 | return OK; |
| 4557 | } |
| 4558 | |
| 4559 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4560 | * compile "return [expr]" |
| 4561 | */ |
| 4562 | static char_u * |
| 4563 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4564 | { |
| 4565 | char_u *p = arg; |
| 4566 | garray_T *stack = &cctx->ctx_type_stack; |
| 4567 | type_T *stack_type; |
| 4568 | |
| 4569 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4570 | { |
| 4571 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4572 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4573 | return NULL; |
| 4574 | |
| 4575 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4576 | if (set_return_type) |
| 4577 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4578 | else |
| 4579 | { |
| 4580 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4581 | && stack_type->tt_type != VAR_VOID |
| 4582 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4583 | { |
| 4584 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4585 | return NULL; |
| 4586 | } |
| 4587 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4588 | == FAIL) |
| 4589 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4590 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4591 | } |
| 4592 | else |
| 4593 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4594 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4595 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4596 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4597 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4598 | { |
| 4599 | emsg(_("E1003: Missing return value")); |
| 4600 | return NULL; |
| 4601 | } |
| 4602 | |
| 4603 | // No argument, return zero. |
| 4604 | generate_PUSHNR(cctx, 0); |
| 4605 | } |
| 4606 | |
| 4607 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4608 | return NULL; |
| 4609 | |
| 4610 | // "return val | endif" is possible |
| 4611 | return skipwhite(p); |
| 4612 | } |
| 4613 | |
| 4614 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4615 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4616 | * Return a pointer to the line in allocated memory. |
| 4617 | * Return NULL for end-of-file or some error. |
| 4618 | */ |
| 4619 | static char_u * |
| 4620 | exarg_getline( |
| 4621 | int c UNUSED, |
| 4622 | void *cookie, |
| 4623 | int indent UNUSED, |
| 4624 | int do_concat UNUSED) |
| 4625 | { |
| 4626 | cctx_T *cctx = (cctx_T *)cookie; |
| 4627 | |
| 4628 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4629 | { |
| 4630 | iemsg("Heredoc got to end"); |
| 4631 | return NULL; |
| 4632 | } |
| 4633 | ++cctx->ctx_lnum; |
| 4634 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4635 | [cctx->ctx_lnum]); |
| 4636 | } |
| 4637 | |
| 4638 | /* |
| 4639 | * Compile a nested :def command. |
| 4640 | */ |
| 4641 | static char_u * |
| 4642 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4643 | { |
| 4644 | char_u *name_start = eap->arg; |
| 4645 | char_u *name_end = to_name_end(eap->arg, FALSE); |
| 4646 | char_u *name = get_lambda_name(); |
| 4647 | lvar_T *lvar; |
| 4648 | ufunc_T *ufunc; |
| 4649 | |
| 4650 | eap->arg = name_end; |
| 4651 | eap->getline = exarg_getline; |
| 4652 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4653 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4654 | eap->forceit = FALSE; |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4655 | ufunc = def_function(eap, name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4656 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4657 | if (ufunc == NULL) |
| 4658 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4659 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4660 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4661 | return NULL; |
| 4662 | |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4663 | // Define a local variable for the function reference. |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4664 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4665 | TRUE, ufunc->uf_func_type); |
| 4666 | |
| 4667 | if (generate_FUNCREF(cctx, ufunc->uf_dfunc_idx) == FAIL |
| 4668 | || generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL) == FAIL) |
| 4669 | return NULL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4670 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4671 | // TODO: warning for trailing text? |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4672 | return (char_u *)""; |
| 4673 | } |
| 4674 | |
| 4675 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4676 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4677 | */ |
| 4678 | int |
| 4679 | assignment_len(char_u *p, int *heredoc) |
| 4680 | { |
| 4681 | if (*p == '=') |
| 4682 | { |
| 4683 | if (p[1] == '<' && p[2] == '<') |
| 4684 | { |
| 4685 | *heredoc = TRUE; |
| 4686 | return 3; |
| 4687 | } |
| 4688 | return 1; |
| 4689 | } |
| 4690 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4691 | return 2; |
| 4692 | if (STRNCMP(p, "..=", 3) == 0) |
| 4693 | return 3; |
| 4694 | return 0; |
| 4695 | } |
| 4696 | |
| 4697 | // words that cannot be used as a variable |
| 4698 | static char *reserved[] = { |
| 4699 | "true", |
| 4700 | "false", |
| 4701 | NULL |
| 4702 | }; |
| 4703 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4704 | typedef enum { |
| 4705 | dest_local, |
| 4706 | dest_option, |
| 4707 | dest_env, |
| 4708 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4709 | dest_buffer, |
| 4710 | dest_window, |
| 4711 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4712 | dest_vimvar, |
| 4713 | dest_script, |
| 4714 | dest_reg, |
| 4715 | } assign_dest_T; |
| 4716 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4717 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4718 | * Generate the load instruction for "name". |
| 4719 | */ |
| 4720 | static void |
| 4721 | generate_loadvar( |
| 4722 | cctx_T *cctx, |
| 4723 | assign_dest_T dest, |
| 4724 | char_u *name, |
| 4725 | lvar_T *lvar, |
| 4726 | type_T *type) |
| 4727 | { |
| 4728 | switch (dest) |
| 4729 | { |
| 4730 | case dest_option: |
| 4731 | // TODO: check the option exists |
| 4732 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4733 | break; |
| 4734 | case dest_global: |
| 4735 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4736 | break; |
| 4737 | case dest_buffer: |
| 4738 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4739 | break; |
| 4740 | case dest_window: |
| 4741 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4742 | break; |
| 4743 | case dest_tab: |
| 4744 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4745 | break; |
| 4746 | case dest_script: |
| 4747 | compile_load_scriptvar(cctx, |
| 4748 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4749 | break; |
| 4750 | case dest_env: |
| 4751 | // Include $ in the name here |
| 4752 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4753 | break; |
| 4754 | case dest_reg: |
| 4755 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4756 | break; |
| 4757 | case dest_vimvar: |
| 4758 | generate_LOADV(cctx, name + 2, TRUE); |
| 4759 | break; |
| 4760 | case dest_local: |
| 4761 | if (lvar->lv_from_outer) |
| 4762 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4763 | NULL, type); |
| 4764 | else |
| 4765 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4766 | break; |
| 4767 | } |
| 4768 | } |
| 4769 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4770 | void |
| 4771 | vim9_declare_error(char_u *name) |
| 4772 | { |
| 4773 | char *scope = ""; |
| 4774 | |
| 4775 | switch (*name) |
| 4776 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4777 | case 'g': scope = _("global"); break; |
| 4778 | case 'b': scope = _("buffer"); break; |
| 4779 | case 'w': scope = _("window"); break; |
| 4780 | case 't': scope = _("tab"); break; |
| 4781 | case 'v': scope = "v:"; break; |
| 4782 | case '$': semsg(_(e_declare_env_var), name); return; |
| 4783 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4784 | } |
| 4785 | semsg(_(e_declare_var), scope, name); |
| 4786 | } |
| 4787 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4788 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4789 | * Compile declaration and assignment: |
| 4790 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4791 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4792 | * Return NULL for an error. |
| 4793 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4794 | */ |
| 4795 | static char_u * |
| 4796 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4797 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4798 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4799 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4800 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4801 | char_u *ret = NULL; |
| 4802 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4803 | int var_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4804 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4805 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4806 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4807 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4808 | int oplen = 0; |
| 4809 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4810 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4811 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4812 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4813 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4814 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4815 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4816 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 4817 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 4818 | if (p == NULL) |
| 4819 | return *arg == '[' ? arg : NULL; |
| 4820 | |
| 4821 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4822 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4823 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4824 | return NULL; |
| 4825 | } |
| 4826 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4827 | sp = p; |
| 4828 | p = skipwhite(p); |
| 4829 | op = p; |
| 4830 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4831 | |
| 4832 | if (var_count > 0 && oplen == 0) |
| 4833 | // can be something like "[1, 2]->func()" |
| 4834 | return arg; |
| 4835 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4836 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4837 | { |
| 4838 | char_u buf[4]; |
| 4839 | |
| 4840 | vim_strncpy(buf, op, oplen); |
| 4841 | semsg(_(e_white_both), buf); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4842 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4843 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4844 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4845 | if (heredoc) |
| 4846 | { |
| 4847 | list_T *l; |
| 4848 | listitem_T *li; |
| 4849 | |
| 4850 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4851 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4852 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 4853 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4854 | |
| 4855 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 4856 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4857 | { |
| 4858 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4859 | li->li_tv.vval.v_string = NULL; |
| 4860 | } |
| 4861 | generate_NEWLIST(cctx, l->lv_len); |
| 4862 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4863 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4864 | list_free(l); |
| 4865 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4866 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4867 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4868 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4869 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4870 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 4871 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4872 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4873 | p = skipwhite(op + oplen); |
| 4874 | if (compile_expr0(&p, cctx) == FAIL) |
| 4875 | return NULL; |
| 4876 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4877 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4878 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4879 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4880 | type_T *stacktype; |
| 4881 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4882 | stacktype = stack->ga_len == 0 ? &t_void |
| 4883 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4884 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4885 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4886 | emsg(_(e_cannot_use_void)); |
| 4887 | goto theend; |
| 4888 | } |
| 4889 | if (need_type(stacktype, &t_list_any, -1, cctx) == FAIL) |
| 4890 | goto theend; |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4891 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 4892 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4893 | } |
| 4894 | } |
| 4895 | |
| 4896 | /* |
| 4897 | * Loop over variables in "[var, var] = expr". |
| 4898 | * For "var = expr" and "let var: type" this is done only once. |
| 4899 | */ |
| 4900 | if (var_count > 0) |
| 4901 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 4902 | else |
| 4903 | var_start = arg; |
| 4904 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 4905 | { |
| 4906 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 4907 | size_t varlen; |
| 4908 | int new_local = FALSE; |
| 4909 | int opt_type; |
| 4910 | int opt_flags = 0; |
| 4911 | assign_dest_T dest = dest_local; |
| 4912 | int vimvaridx = -1; |
| 4913 | lvar_T *lvar = NULL; |
| 4914 | lvar_T arg_lvar; |
| 4915 | int has_type = FALSE; |
| 4916 | int has_index = FALSE; |
| 4917 | int instr_count = -1; |
| 4918 | |
| 4919 | p = (*var_start == '&' || *var_start == '$' |
| 4920 | || *var_start == '@') ? var_start + 1 : var_start; |
| 4921 | p = to_name_end(p, TRUE); |
| 4922 | |
| 4923 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 4924 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 4925 | --var_end; |
| 4926 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 4927 | --p; |
| 4928 | |
| 4929 | varlen = p - var_start; |
| 4930 | vim_free(name); |
| 4931 | name = vim_strnsave(var_start, varlen); |
| 4932 | if (name == NULL) |
| 4933 | return NULL; |
| 4934 | if (!heredoc) |
| 4935 | type = &t_any; |
| 4936 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4937 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4938 | { |
| 4939 | if (*var_start == '&') |
| 4940 | { |
| 4941 | int cc; |
| 4942 | long numval; |
| 4943 | |
| 4944 | dest = dest_option; |
| 4945 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4946 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4947 | emsg(_(e_const_option)); |
| 4948 | goto theend; |
| 4949 | } |
| 4950 | if (is_decl) |
| 4951 | { |
| 4952 | semsg(_("E1052: Cannot declare an option: %s"), var_start); |
| 4953 | goto theend; |
| 4954 | } |
| 4955 | p = var_start; |
| 4956 | p = find_option_end(&p, &opt_flags); |
| 4957 | if (p == NULL) |
| 4958 | { |
| 4959 | // cannot happen? |
| 4960 | emsg(_(e_letunexp)); |
| 4961 | goto theend; |
| 4962 | } |
| 4963 | cc = *p; |
| 4964 | *p = NUL; |
| 4965 | opt_type = get_option_value(var_start + 1, &numval, |
| 4966 | NULL, opt_flags); |
| 4967 | *p = cc; |
| 4968 | if (opt_type == -3) |
| 4969 | { |
| 4970 | semsg(_(e_unknown_option), var_start); |
| 4971 | goto theend; |
| 4972 | } |
| 4973 | if (opt_type == -2 || opt_type == 0) |
| 4974 | type = &t_string; |
| 4975 | else |
| 4976 | type = &t_number; // both number and boolean option |
| 4977 | } |
| 4978 | else if (*var_start == '$') |
| 4979 | { |
| 4980 | dest = dest_env; |
| 4981 | type = &t_string; |
| 4982 | if (is_decl) |
| 4983 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4984 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4985 | goto theend; |
| 4986 | } |
| 4987 | } |
| 4988 | else if (*var_start == '@') |
| 4989 | { |
| 4990 | if (!valid_yank_reg(var_start[1], TRUE)) |
| 4991 | { |
| 4992 | emsg_invreg(var_start[1]); |
| 4993 | goto theend; |
| 4994 | } |
| 4995 | dest = dest_reg; |
| 4996 | type = &t_string; |
| 4997 | if (is_decl) |
| 4998 | { |
| 4999 | semsg(_("E1066: Cannot declare a register: %s"), name); |
| 5000 | goto theend; |
| 5001 | } |
| 5002 | } |
| 5003 | else if (STRNCMP(var_start, "g:", 2) == 0) |
| 5004 | { |
| 5005 | dest = dest_global; |
| 5006 | if (is_decl) |
| 5007 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5008 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5009 | goto theend; |
| 5010 | } |
| 5011 | } |
| 5012 | else if (STRNCMP(var_start, "b:", 2) == 0) |
| 5013 | { |
| 5014 | dest = dest_buffer; |
| 5015 | if (is_decl) |
| 5016 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5017 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5018 | goto theend; |
| 5019 | } |
| 5020 | } |
| 5021 | else if (STRNCMP(var_start, "w:", 2) == 0) |
| 5022 | { |
| 5023 | dest = dest_window; |
| 5024 | if (is_decl) |
| 5025 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5026 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5027 | goto theend; |
| 5028 | } |
| 5029 | } |
| 5030 | else if (STRNCMP(var_start, "t:", 2) == 0) |
| 5031 | { |
| 5032 | dest = dest_tab; |
| 5033 | if (is_decl) |
| 5034 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5035 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5036 | goto theend; |
| 5037 | } |
| 5038 | } |
| 5039 | else if (STRNCMP(var_start, "v:", 2) == 0) |
| 5040 | { |
| 5041 | typval_T *vtv; |
| 5042 | int di_flags; |
| 5043 | |
| 5044 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5045 | if (vimvaridx < 0) |
| 5046 | { |
| 5047 | semsg(_(e_var_notfound), var_start); |
| 5048 | goto theend; |
| 5049 | } |
| 5050 | // We use the current value of "sandbox" here, is that OK? |
| 5051 | if (var_check_ro(di_flags, name, FALSE)) |
| 5052 | goto theend; |
| 5053 | dest = dest_vimvar; |
| 5054 | vtv = get_vim_var_tv(vimvaridx); |
| 5055 | type = typval2type(vtv); |
| 5056 | if (is_decl) |
| 5057 | { |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5058 | vim9_declare_error(name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5059 | goto theend; |
| 5060 | } |
| 5061 | } |
| 5062 | else |
| 5063 | { |
| 5064 | int idx; |
| 5065 | |
| 5066 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5067 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5068 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5069 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5070 | goto theend; |
| 5071 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5072 | |
| 5073 | lvar = lookup_local(var_start, varlen, cctx); |
| 5074 | if (lvar == NULL) |
| 5075 | { |
| 5076 | CLEAR_FIELD(arg_lvar); |
| 5077 | if (lookup_arg(var_start, varlen, |
| 5078 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 5079 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5080 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5081 | if (is_decl) |
| 5082 | { |
| 5083 | semsg(_(e_used_as_arg), name); |
| 5084 | goto theend; |
| 5085 | } |
| 5086 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5087 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5088 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5089 | if (lvar != NULL) |
| 5090 | { |
| 5091 | if (is_decl) |
| 5092 | { |
| 5093 | semsg(_("E1017: Variable already declared: %s"), name); |
| 5094 | goto theend; |
| 5095 | } |
| 5096 | else if (lvar->lv_const) |
| 5097 | { |
| 5098 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 5099 | name); |
| 5100 | goto theend; |
| 5101 | } |
| 5102 | } |
| 5103 | else if (STRNCMP(var_start, "s:", 2) == 0 |
| 5104 | || lookup_script(var_start, varlen) == OK |
| 5105 | || find_imported(var_start, varlen, cctx) != NULL) |
| 5106 | { |
| 5107 | dest = dest_script; |
| 5108 | if (is_decl) |
| 5109 | { |
| 5110 | semsg(_("E1054: Variable already declared in the script: %s"), |
| 5111 | name); |
| 5112 | goto theend; |
| 5113 | } |
| 5114 | } |
| 5115 | else if (name[1] == ':' && name[2] != NUL) |
| 5116 | { |
| 5117 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
| 5118 | name); |
| 5119 | goto theend; |
| 5120 | } |
| 5121 | else if (!is_decl) |
| 5122 | { |
| 5123 | semsg(_("E1089: unknown variable: %s"), name); |
| 5124 | goto theend; |
| 5125 | } |
| 5126 | } |
| 5127 | } |
| 5128 | |
| 5129 | // handle "a:name" as a name, not index "name" on "a" |
| 5130 | if (varlen > 1 || var_start[varlen] != ':') |
| 5131 | p = var_end; |
| 5132 | |
| 5133 | if (dest != dest_option) |
| 5134 | { |
| 5135 | if (is_decl && *p == ':') |
| 5136 | { |
| 5137 | // parse optional type: "let var: type = expr" |
| 5138 | if (!VIM_ISWHITE(p[1])) |
| 5139 | { |
| 5140 | semsg(_(e_white_after), ":"); |
| 5141 | goto theend; |
| 5142 | } |
| 5143 | p = skipwhite(p + 1); |
| 5144 | type = parse_type(&p, cctx->ctx_type_list); |
| 5145 | has_type = TRUE; |
| 5146 | } |
| 5147 | else if (lvar != NULL) |
| 5148 | type = lvar->lv_type; |
| 5149 | } |
| 5150 | |
| 5151 | if (oplen == 3 && !heredoc && dest != dest_global |
| 5152 | && type->tt_type != VAR_STRING |
| 5153 | && type->tt_type != VAR_ANY) |
| 5154 | { |
| 5155 | emsg(_("E1019: Can only concatenate to string")); |
| 5156 | goto theend; |
| 5157 | } |
| 5158 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5159 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5160 | { |
| 5161 | if (oplen > 1 && !heredoc) |
| 5162 | { |
| 5163 | // +=, /=, etc. require an existing variable |
| 5164 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 5165 | name); |
| 5166 | goto theend; |
| 5167 | } |
| 5168 | |
| 5169 | // new local variable |
| 5170 | if (type->tt_type == VAR_FUNC && var_check_func_name(name, TRUE)) |
| 5171 | goto theend; |
| 5172 | lvar = reserve_local(cctx, var_start, varlen, |
| 5173 | cmdidx == CMD_const, type); |
| 5174 | if (lvar == NULL) |
| 5175 | goto theend; |
| 5176 | new_local = TRUE; |
| 5177 | } |
| 5178 | |
| 5179 | member_type = type; |
| 5180 | if (var_end > var_start + varlen) |
| 5181 | { |
| 5182 | // Something follows after the variable: "var[idx]". |
| 5183 | if (is_decl) |
| 5184 | { |
| 5185 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 5186 | goto theend; |
| 5187 | } |
| 5188 | |
| 5189 | if (var_start[varlen] == '[') |
| 5190 | { |
| 5191 | has_index = TRUE; |
| 5192 | if (type->tt_member == NULL) |
| 5193 | { |
| 5194 | semsg(_("E1088: cannot use an index on %s"), name); |
| 5195 | goto theend; |
| 5196 | } |
| 5197 | member_type = type->tt_member; |
| 5198 | } |
| 5199 | else |
| 5200 | { |
| 5201 | semsg("Not supported yet: %s", var_start); |
| 5202 | goto theend; |
| 5203 | } |
| 5204 | } |
| 5205 | else if (lvar == &arg_lvar) |
| 5206 | { |
| 5207 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 5208 | goto theend; |
| 5209 | } |
| 5210 | |
| 5211 | if (!heredoc) |
| 5212 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5213 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5214 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5215 | if (oplen > 0 && var_count == 0) |
| 5216 | { |
| 5217 | // skip over the "=" and the expression |
| 5218 | p = skipwhite(op + oplen); |
| 5219 | compile_expr0(&p, cctx); |
| 5220 | } |
| 5221 | } |
| 5222 | else if (oplen > 0) |
| 5223 | { |
| 5224 | type_T *stacktype; |
| 5225 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5226 | // For "var = expr" evaluate the expression. |
| 5227 | if (var_count == 0) |
| 5228 | { |
| 5229 | int r; |
| 5230 | |
| 5231 | // for "+=", "*=", "..=" etc. first load the current value |
| 5232 | if (*op != '=') |
| 5233 | { |
| 5234 | generate_loadvar(cctx, dest, name, lvar, type); |
| 5235 | |
| 5236 | if (has_index) |
| 5237 | { |
| 5238 | // TODO: get member from list or dict |
| 5239 | emsg("Index with operation not supported yet"); |
| 5240 | goto theend; |
| 5241 | } |
| 5242 | } |
| 5243 | |
| 5244 | // Compile the expression. Temporarily hide the new local |
| 5245 | // variable here, it is not available to this expression. |
| 5246 | if (new_local) |
| 5247 | --cctx->ctx_locals.ga_len; |
| 5248 | instr_count = instr->ga_len; |
| 5249 | p = skipwhite(op + oplen); |
| 5250 | r = compile_expr0(&p, cctx); |
| 5251 | if (new_local) |
| 5252 | ++cctx->ctx_locals.ga_len; |
| 5253 | if (r == FAIL) |
| 5254 | goto theend; |
| 5255 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5256 | else if (semicolon && var_idx == var_count - 1) |
| 5257 | { |
| 5258 | // For "[var; var] = expr" get the rest of the list |
| 5259 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5260 | goto theend; |
| 5261 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5262 | else |
| 5263 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5264 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5265 | // list. |
| 5266 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 5267 | return FAIL; |
| 5268 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5269 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5270 | stacktype = stack->ga_len == 0 ? &t_void |
| 5271 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 5272 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5273 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5274 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5275 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5276 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5277 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5278 | emsg(_(e_cannot_use_void)); |
| 5279 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5280 | } |
| 5281 | else |
| 5282 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5283 | // An empty list or dict has a &t_void member, |
| 5284 | // for a variable that implies &t_any. |
| 5285 | if (stacktype == &t_list_empty) |
| 5286 | lvar->lv_type = &t_list_any; |
| 5287 | else if (stacktype == &t_dict_empty) |
| 5288 | lvar->lv_type = &t_dict_any; |
| 5289 | else |
| 5290 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5291 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5292 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5293 | else |
| 5294 | { |
| 5295 | type_T *use_type = lvar->lv_type; |
| 5296 | |
| 5297 | if (has_index) |
| 5298 | { |
| 5299 | use_type = use_type->tt_member; |
| 5300 | if (use_type == NULL) |
| 5301 | use_type = &t_void; |
| 5302 | } |
| 5303 | if (need_type(stacktype, use_type, -1, cctx) |
| 5304 | == FAIL) |
| 5305 | goto theend; |
| 5306 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5307 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5308 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
| 5309 | cctx) == FAIL) |
| 5310 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5311 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5312 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5313 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5314 | emsg(_(e_const_req_value)); |
| 5315 | goto theend; |
| 5316 | } |
| 5317 | else if (!has_type || dest == dest_option) |
| 5318 | { |
| 5319 | emsg(_(e_type_req)); |
| 5320 | goto theend; |
| 5321 | } |
| 5322 | else |
| 5323 | { |
| 5324 | // variables are always initialized |
| 5325 | if (ga_grow(instr, 1) == FAIL) |
| 5326 | goto theend; |
| 5327 | switch (member_type->tt_type) |
| 5328 | { |
| 5329 | case VAR_BOOL: |
| 5330 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5331 | break; |
| 5332 | case VAR_FLOAT: |
| 5333 | #ifdef FEAT_FLOAT |
| 5334 | generate_PUSHF(cctx, 0.0); |
| 5335 | #endif |
| 5336 | break; |
| 5337 | case VAR_STRING: |
| 5338 | generate_PUSHS(cctx, NULL); |
| 5339 | break; |
| 5340 | case VAR_BLOB: |
| 5341 | generate_PUSHBLOB(cctx, NULL); |
| 5342 | break; |
| 5343 | case VAR_FUNC: |
| 5344 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5345 | break; |
| 5346 | case VAR_LIST: |
| 5347 | generate_NEWLIST(cctx, 0); |
| 5348 | break; |
| 5349 | case VAR_DICT: |
| 5350 | generate_NEWDICT(cctx, 0); |
| 5351 | break; |
| 5352 | case VAR_JOB: |
| 5353 | generate_PUSHJOB(cctx, NULL); |
| 5354 | break; |
| 5355 | case VAR_CHANNEL: |
| 5356 | generate_PUSHCHANNEL(cctx, NULL); |
| 5357 | break; |
| 5358 | case VAR_NUMBER: |
| 5359 | case VAR_UNKNOWN: |
| 5360 | case VAR_ANY: |
| 5361 | case VAR_PARTIAL: |
| 5362 | case VAR_VOID: |
| 5363 | case VAR_SPECIAL: // cannot happen |
| 5364 | generate_PUSHNR(cctx, 0); |
| 5365 | break; |
| 5366 | } |
| 5367 | } |
| 5368 | if (var_count == 0) |
| 5369 | end = p; |
| 5370 | } |
| 5371 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5372 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5373 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5374 | break; |
| 5375 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5376 | if (oplen > 0 && *op != '=') |
| 5377 | { |
| 5378 | type_T *expected = &t_number; |
| 5379 | type_T *stacktype; |
| 5380 | |
| 5381 | // TODO: if type is known use float or any operation |
| 5382 | |
| 5383 | if (*op == '.') |
| 5384 | expected = &t_string; |
| 5385 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 5386 | if (need_type(stacktype, expected, -1, cctx) == FAIL) |
| 5387 | goto theend; |
| 5388 | |
| 5389 | if (*op == '.') |
| 5390 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 5391 | else |
| 5392 | { |
| 5393 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 5394 | |
| 5395 | if (isn == NULL) |
| 5396 | goto theend; |
| 5397 | switch (*op) |
| 5398 | { |
| 5399 | case '+': isn->isn_arg.op.op_type = EXPR_ADD; break; |
| 5400 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 5401 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 5402 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 5403 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 5404 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5405 | } |
| 5406 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5407 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5408 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5409 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5410 | int r; |
| 5411 | |
| 5412 | // Compile the "idx" in "var[idx]". |
| 5413 | if (new_local) |
| 5414 | --cctx->ctx_locals.ga_len; |
| 5415 | p = skipwhite(var_start + varlen + 1); |
| 5416 | r = compile_expr0(&p, cctx); |
| 5417 | if (new_local) |
| 5418 | ++cctx->ctx_locals.ga_len; |
| 5419 | if (r == FAIL) |
| 5420 | goto theend; |
| 5421 | if (*skipwhite(p) != ']') |
| 5422 | { |
| 5423 | emsg(_(e_missbrac)); |
| 5424 | goto theend; |
| 5425 | } |
| 5426 | if (type->tt_type == VAR_DICT |
| 5427 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5428 | goto theend; |
| 5429 | if (type->tt_type == VAR_LIST |
| 5430 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5431 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5432 | { |
| 5433 | emsg(_(e_number_exp)); |
| 5434 | goto theend; |
| 5435 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5436 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5437 | // Load the dict or list. On the stack we then have: |
| 5438 | // - value |
| 5439 | // - index |
| 5440 | // - variable |
| 5441 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5442 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5443 | if (type->tt_type == VAR_LIST) |
| 5444 | { |
| 5445 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5446 | return FAIL; |
| 5447 | } |
| 5448 | else if (type->tt_type == VAR_DICT) |
| 5449 | { |
| 5450 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5451 | return FAIL; |
| 5452 | } |
| 5453 | else |
| 5454 | { |
| 5455 | emsg(_(e_listreq)); |
| 5456 | goto theend; |
| 5457 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5458 | } |
| 5459 | else |
| 5460 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5461 | switch (dest) |
| 5462 | { |
| 5463 | case dest_option: |
| 5464 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5465 | break; |
| 5466 | case dest_global: |
| 5467 | // include g: with the name, easier to execute that way |
| 5468 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5469 | break; |
| 5470 | case dest_buffer: |
| 5471 | // include b: with the name, easier to execute that way |
| 5472 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5473 | break; |
| 5474 | case dest_window: |
| 5475 | // include w: with the name, easier to execute that way |
| 5476 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5477 | break; |
| 5478 | case dest_tab: |
| 5479 | // include t: with the name, easier to execute that way |
| 5480 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5481 | break; |
| 5482 | case dest_env: |
| 5483 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5484 | break; |
| 5485 | case dest_reg: |
| 5486 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5487 | break; |
| 5488 | case dest_vimvar: |
| 5489 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5490 | break; |
| 5491 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5492 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5493 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5494 | imported_T *import = NULL; |
| 5495 | int sid = current_sctx.sc_sid; |
| 5496 | int idx; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5497 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5498 | if (name[1] != ':') |
| 5499 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5500 | import = find_imported(name, 0, cctx); |
| 5501 | if (import != NULL) |
| 5502 | sid = import->imp_sid; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5503 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5504 | |
| 5505 | idx = get_script_item_idx(sid, rawname, TRUE); |
| 5506 | // TODO: specific type |
| 5507 | if (idx < 0) |
| 5508 | { |
| 5509 | char_u *name_s = name; |
| 5510 | |
| 5511 | // Include s: in the name for store_var() |
| 5512 | if (name[1] != ':') |
| 5513 | { |
| 5514 | int len = (int)STRLEN(name) + 3; |
| 5515 | |
| 5516 | name_s = alloc(len); |
| 5517 | if (name_s == NULL) |
| 5518 | name_s = name; |
| 5519 | else |
| 5520 | vim_snprintf((char *)name_s, len, |
| 5521 | "s:%s", name); |
| 5522 | } |
| 5523 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, sid, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5524 | &t_any); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5525 | if (name_s != name) |
| 5526 | vim_free(name_s); |
| 5527 | } |
| 5528 | else |
| 5529 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5530 | sid, idx, &t_any); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5531 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5532 | break; |
| 5533 | case dest_local: |
| 5534 | if (lvar != NULL) |
| 5535 | { |
| 5536 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5537 | + instr->ga_len - 1; |
| 5538 | |
| 5539 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5540 | // ISN_STORE into ISN_STORENR |
| 5541 | if (!lvar->lv_from_outer |
| 5542 | && instr->ga_len == instr_count + 1 |
| 5543 | && isn->isn_type == ISN_PUSHNR) |
| 5544 | { |
| 5545 | varnumber_T val = isn->isn_arg.number; |
| 5546 | |
| 5547 | isn->isn_type = ISN_STORENR; |
| 5548 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5549 | isn->isn_arg.storenr.stnr_val = val; |
| 5550 | if (stack->ga_len > 0) |
| 5551 | --stack->ga_len; |
| 5552 | } |
| 5553 | else if (lvar->lv_from_outer) |
| 5554 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5555 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5556 | else |
| 5557 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5558 | } |
| 5559 | break; |
| 5560 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5561 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5562 | |
| 5563 | if (var_idx + 1 < var_count) |
| 5564 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5565 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5566 | |
| 5567 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5568 | if (var_count > 0 && !semicolon) |
| 5569 | { |
| 5570 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5571 | goto theend; |
| 5572 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5573 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5574 | ret = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5575 | |
| 5576 | theend: |
| 5577 | vim_free(name); |
| 5578 | return ret; |
| 5579 | } |
| 5580 | |
| 5581 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5582 | * Check if "name" can be "unlet". |
| 5583 | */ |
| 5584 | int |
| 5585 | check_vim9_unlet(char_u *name) |
| 5586 | { |
| 5587 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5588 | { |
| 5589 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5590 | return FAIL; |
| 5591 | } |
| 5592 | return OK; |
| 5593 | } |
| 5594 | |
| 5595 | /* |
| 5596 | * Callback passed to ex_unletlock(). |
| 5597 | */ |
| 5598 | static int |
| 5599 | compile_unlet( |
| 5600 | lval_T *lvp, |
| 5601 | char_u *name_end, |
| 5602 | exarg_T *eap, |
| 5603 | int deep UNUSED, |
| 5604 | void *coookie) |
| 5605 | { |
| 5606 | cctx_T *cctx = coookie; |
| 5607 | |
| 5608 | if (lvp->ll_tv == NULL) |
| 5609 | { |
| 5610 | char_u *p = lvp->ll_name; |
| 5611 | int cc = *name_end; |
| 5612 | int ret = OK; |
| 5613 | |
| 5614 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5615 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5616 | if (*p == '$') |
| 5617 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5618 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5619 | ret = FAIL; |
| 5620 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5621 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5622 | |
| 5623 | *name_end = cc; |
| 5624 | return ret; |
| 5625 | } |
| 5626 | |
| 5627 | // TODO: unlet {list}[idx] |
| 5628 | // TODO: unlet {dict}[key] |
| 5629 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5630 | return FAIL; |
| 5631 | } |
| 5632 | |
| 5633 | /* |
| 5634 | * compile "unlet var", "lock var" and "unlock var" |
| 5635 | * "arg" points to "var". |
| 5636 | */ |
| 5637 | static char_u * |
| 5638 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5639 | { |
| 5640 | char_u *p = arg; |
| 5641 | |
| 5642 | if (eap->cmdidx != CMD_unlet) |
| 5643 | { |
| 5644 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5645 | return NULL; |
| 5646 | } |
| 5647 | |
| 5648 | if (*p == '!') |
| 5649 | { |
| 5650 | p = skipwhite(p + 1); |
| 5651 | eap->forceit = TRUE; |
| 5652 | } |
| 5653 | |
| 5654 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5655 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5656 | } |
| 5657 | |
| 5658 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5659 | * Compile an :import command. |
| 5660 | */ |
| 5661 | static char_u * |
| 5662 | compile_import(char_u *arg, cctx_T *cctx) |
| 5663 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5664 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5665 | } |
| 5666 | |
| 5667 | /* |
| 5668 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5669 | */ |
| 5670 | static int |
| 5671 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5672 | { |
| 5673 | garray_T *instr = &cctx->ctx_instr; |
| 5674 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5675 | |
| 5676 | if (endlabel == NULL) |
| 5677 | return FAIL; |
| 5678 | endlabel->el_next = *el; |
| 5679 | *el = endlabel; |
| 5680 | endlabel->el_end_label = instr->ga_len; |
| 5681 | |
| 5682 | generate_JUMP(cctx, when, 0); |
| 5683 | return OK; |
| 5684 | } |
| 5685 | |
| 5686 | static void |
| 5687 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5688 | { |
| 5689 | garray_T *instr = &cctx->ctx_instr; |
| 5690 | |
| 5691 | while (*el != NULL) |
| 5692 | { |
| 5693 | endlabel_T *cur = (*el); |
| 5694 | isn_T *isn; |
| 5695 | |
| 5696 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5697 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5698 | *el = cur->el_next; |
| 5699 | vim_free(cur); |
| 5700 | } |
| 5701 | } |
| 5702 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5703 | static void |
| 5704 | compile_free_jump_to_end(endlabel_T **el) |
| 5705 | { |
| 5706 | while (*el != NULL) |
| 5707 | { |
| 5708 | endlabel_T *cur = (*el); |
| 5709 | |
| 5710 | *el = cur->el_next; |
| 5711 | vim_free(cur); |
| 5712 | } |
| 5713 | } |
| 5714 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5715 | /* |
| 5716 | * Create a new scope and set up the generic items. |
| 5717 | */ |
| 5718 | static scope_T * |
| 5719 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5720 | { |
| 5721 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5722 | |
| 5723 | if (scope == NULL) |
| 5724 | return NULL; |
| 5725 | scope->se_outer = cctx->ctx_scope; |
| 5726 | cctx->ctx_scope = scope; |
| 5727 | scope->se_type = type; |
| 5728 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5729 | return scope; |
| 5730 | } |
| 5731 | |
| 5732 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5733 | * Free the current scope and go back to the outer scope. |
| 5734 | */ |
| 5735 | static void |
| 5736 | drop_scope(cctx_T *cctx) |
| 5737 | { |
| 5738 | scope_T *scope = cctx->ctx_scope; |
| 5739 | |
| 5740 | if (scope == NULL) |
| 5741 | { |
| 5742 | iemsg("calling drop_scope() without a scope"); |
| 5743 | return; |
| 5744 | } |
| 5745 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5746 | switch (scope->se_type) |
| 5747 | { |
| 5748 | case IF_SCOPE: |
| 5749 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5750 | case FOR_SCOPE: |
| 5751 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5752 | case WHILE_SCOPE: |
| 5753 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5754 | case TRY_SCOPE: |
| 5755 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5756 | case NO_SCOPE: |
| 5757 | case BLOCK_SCOPE: |
| 5758 | break; |
| 5759 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5760 | vim_free(scope); |
| 5761 | } |
| 5762 | |
| 5763 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5764 | * compile "if expr" |
| 5765 | * |
| 5766 | * "if expr" Produces instructions: |
| 5767 | * EVAL expr Push result of "expr" |
| 5768 | * JUMP_IF_FALSE end |
| 5769 | * ... body ... |
| 5770 | * end: |
| 5771 | * |
| 5772 | * "if expr | else" Produces instructions: |
| 5773 | * EVAL expr Push result of "expr" |
| 5774 | * JUMP_IF_FALSE else |
| 5775 | * ... body ... |
| 5776 | * JUMP_ALWAYS end |
| 5777 | * else: |
| 5778 | * ... body ... |
| 5779 | * end: |
| 5780 | * |
| 5781 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5782 | * EVAL expr Push result of "expr" |
| 5783 | * JUMP_IF_FALSE elseif |
| 5784 | * ... body ... |
| 5785 | * JUMP_ALWAYS end |
| 5786 | * elseif: |
| 5787 | * EVAL expr Push result of "expr" |
| 5788 | * JUMP_IF_FALSE else |
| 5789 | * ... body ... |
| 5790 | * JUMP_ALWAYS end |
| 5791 | * else: |
| 5792 | * ... body ... |
| 5793 | * end: |
| 5794 | */ |
| 5795 | static char_u * |
| 5796 | compile_if(char_u *arg, cctx_T *cctx) |
| 5797 | { |
| 5798 | char_u *p = arg; |
| 5799 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5800 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5801 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5802 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5803 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5804 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5805 | CLEAR_FIELD(ppconst); |
| 5806 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5807 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5808 | clear_ppconst(&ppconst); |
| 5809 | return NULL; |
| 5810 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5811 | if (cctx->ctx_skip == SKIP_YES) |
| 5812 | clear_ppconst(&ppconst); |
| 5813 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5814 | { |
| 5815 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5816 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5817 | clear_ppconst(&ppconst); |
| 5818 | } |
| 5819 | else |
| 5820 | { |
| 5821 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5822 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5823 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5824 | return NULL; |
| 5825 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5826 | |
| 5827 | scope = new_scope(cctx, IF_SCOPE); |
| 5828 | if (scope == NULL) |
| 5829 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5830 | scope->se_skip_save = skip_save; |
| 5831 | // "is_had_return" will be reset if any block does not end in :return |
| 5832 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5833 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5834 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5835 | { |
| 5836 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5837 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5838 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5839 | } |
| 5840 | else |
| 5841 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5842 | |
| 5843 | return p; |
| 5844 | } |
| 5845 | |
| 5846 | static char_u * |
| 5847 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 5848 | { |
| 5849 | char_u *p = arg; |
| 5850 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5851 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5852 | isn_T *isn; |
| 5853 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5854 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5855 | |
| 5856 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5857 | { |
| 5858 | emsg(_(e_elseif_without_if)); |
| 5859 | return NULL; |
| 5860 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5861 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5862 | if (!cctx->ctx_had_return) |
| 5863 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5864 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5865 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5866 | { |
| 5867 | 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] | 5868 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5869 | return NULL; |
| 5870 | // previous "if" or "elseif" jumps here |
| 5871 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5872 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5873 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5874 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5875 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5876 | CLEAR_FIELD(ppconst); |
| 5877 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5878 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5879 | clear_ppconst(&ppconst); |
| 5880 | return NULL; |
| 5881 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5882 | if (scope->se_skip_save == SKIP_YES) |
| 5883 | clear_ppconst(&ppconst); |
| 5884 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5885 | { |
| 5886 | // The expression results in a constant. |
| 5887 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5888 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5889 | clear_ppconst(&ppconst); |
| 5890 | scope->se_u.se_if.is_if_label = -1; |
| 5891 | } |
| 5892 | else |
| 5893 | { |
| 5894 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5895 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5896 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5897 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5898 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5899 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5900 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5901 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5902 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5903 | |
| 5904 | return p; |
| 5905 | } |
| 5906 | |
| 5907 | static char_u * |
| 5908 | compile_else(char_u *arg, cctx_T *cctx) |
| 5909 | { |
| 5910 | char_u *p = arg; |
| 5911 | garray_T *instr = &cctx->ctx_instr; |
| 5912 | isn_T *isn; |
| 5913 | scope_T *scope = cctx->ctx_scope; |
| 5914 | |
| 5915 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5916 | { |
| 5917 | emsg(_(e_else_without_if)); |
| 5918 | return NULL; |
| 5919 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5920 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5921 | if (!cctx->ctx_had_return) |
| 5922 | scope->se_u.se_if.is_had_return = FALSE; |
| 5923 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5924 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5925 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5926 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5927 | // 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] | 5928 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5929 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5930 | if (!cctx->ctx_had_return |
| 5931 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 5932 | JUMP_ALWAYS, cctx) == FAIL) |
| 5933 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5934 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5935 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5936 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5937 | { |
| 5938 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5939 | { |
| 5940 | // previous "if" or "elseif" jumps here |
| 5941 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5942 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5943 | scope->se_u.se_if.is_if_label = -1; |
| 5944 | } |
| 5945 | } |
| 5946 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5947 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5948 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 5949 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5950 | |
| 5951 | return p; |
| 5952 | } |
| 5953 | |
| 5954 | static char_u * |
| 5955 | compile_endif(char_u *arg, cctx_T *cctx) |
| 5956 | { |
| 5957 | scope_T *scope = cctx->ctx_scope; |
| 5958 | ifscope_T *ifscope; |
| 5959 | garray_T *instr = &cctx->ctx_instr; |
| 5960 | isn_T *isn; |
| 5961 | |
| 5962 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5963 | { |
| 5964 | emsg(_(e_endif_without_if)); |
| 5965 | return NULL; |
| 5966 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5967 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5968 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5969 | if (!cctx->ctx_had_return) |
| 5970 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5971 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5972 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5973 | { |
| 5974 | // previous "if" or "elseif" jumps here |
| 5975 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5976 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5977 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5978 | // Fill in the "end" label in jumps at the end of the blocks. |
| 5979 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5980 | cctx->ctx_skip = scope->se_skip_save; |
| 5981 | |
| 5982 | // If all the blocks end in :return and there is an :else then the |
| 5983 | // had_return flag is set. |
| 5984 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5985 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5986 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5987 | return arg; |
| 5988 | } |
| 5989 | |
| 5990 | /* |
| 5991 | * compile "for var in expr" |
| 5992 | * |
| 5993 | * Produces instructions: |
| 5994 | * PUSHNR -1 |
| 5995 | * STORE loop-idx Set index to -1 |
| 5996 | * EVAL expr Push result of "expr" |
| 5997 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 5998 | * - if beyond end, jump to "end" |
| 5999 | * - otherwise get item from list and push it |
| 6000 | * STORE var Store item in "var" |
| 6001 | * ... body ... |
| 6002 | * JUMP top Jump back to repeat |
| 6003 | * end: DROP Drop the result of "expr" |
| 6004 | * |
| 6005 | */ |
| 6006 | static char_u * |
| 6007 | compile_for(char_u *arg, cctx_T *cctx) |
| 6008 | { |
| 6009 | char_u *p; |
| 6010 | size_t varlen; |
| 6011 | garray_T *instr = &cctx->ctx_instr; |
| 6012 | garray_T *stack = &cctx->ctx_type_stack; |
| 6013 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6014 | lvar_T *loop_lvar; // loop iteration variable |
| 6015 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6016 | type_T *vartype; |
| 6017 | |
| 6018 | // TODO: list of variables: "for [key, value] in dict" |
| 6019 | // parse "var" |
| 6020 | for (p = arg; eval_isnamec1(*p); ++p) |
| 6021 | ; |
| 6022 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6023 | var_lvar = lookup_local(arg, varlen, cctx); |
| 6024 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6025 | { |
| 6026 | semsg(_("E1023: variable already defined: %s"), arg); |
| 6027 | return NULL; |
| 6028 | } |
| 6029 | |
| 6030 | // consume "in" |
| 6031 | p = skipwhite(p); |
| 6032 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 6033 | { |
| 6034 | emsg(_(e_missing_in)); |
| 6035 | return NULL; |
| 6036 | } |
| 6037 | p = skipwhite(p + 2); |
| 6038 | |
| 6039 | |
| 6040 | scope = new_scope(cctx, FOR_SCOPE); |
| 6041 | if (scope == NULL) |
| 6042 | return NULL; |
| 6043 | |
| 6044 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6045 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6046 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6047 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6048 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6049 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6050 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6051 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6052 | |
| 6053 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6054 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 6055 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6056 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6057 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6058 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6059 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6060 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6061 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6062 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6063 | |
| 6064 | // compile "expr", it remains on the stack until "endfor" |
| 6065 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6066 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6067 | { |
| 6068 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6069 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6070 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6071 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6072 | // Now that we know the type of "var", check that it is a list, now or at |
| 6073 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6074 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6075 | if (need_type(vartype, &t_list_any, -1, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6076 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6077 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6078 | return NULL; |
| 6079 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6080 | 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] | 6081 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6082 | |
| 6083 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6084 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6085 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6086 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 6087 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6088 | |
| 6089 | return arg; |
| 6090 | } |
| 6091 | |
| 6092 | /* |
| 6093 | * compile "endfor" |
| 6094 | */ |
| 6095 | static char_u * |
| 6096 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 6097 | { |
| 6098 | garray_T *instr = &cctx->ctx_instr; |
| 6099 | scope_T *scope = cctx->ctx_scope; |
| 6100 | forscope_T *forscope; |
| 6101 | isn_T *isn; |
| 6102 | |
| 6103 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 6104 | { |
| 6105 | emsg(_(e_for)); |
| 6106 | return NULL; |
| 6107 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6108 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6109 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6110 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6111 | |
| 6112 | // At end of ":for" scope jump back to the FOR instruction. |
| 6113 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 6114 | |
| 6115 | // Fill in the "end" label in the FOR statement so it can jump here |
| 6116 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 6117 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 6118 | |
| 6119 | // Fill in the "end" label any BREAK statements |
| 6120 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 6121 | |
| 6122 | // Below the ":for" scope drop the "expr" list from the stack. |
| 6123 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 6124 | return NULL; |
| 6125 | |
| 6126 | vim_free(scope); |
| 6127 | |
| 6128 | return arg; |
| 6129 | } |
| 6130 | |
| 6131 | /* |
| 6132 | * compile "while expr" |
| 6133 | * |
| 6134 | * Produces instructions: |
| 6135 | * top: EVAL expr Push result of "expr" |
| 6136 | * JUMP_IF_FALSE end jump if false |
| 6137 | * ... body ... |
| 6138 | * JUMP top Jump back to repeat |
| 6139 | * end: |
| 6140 | * |
| 6141 | */ |
| 6142 | static char_u * |
| 6143 | compile_while(char_u *arg, cctx_T *cctx) |
| 6144 | { |
| 6145 | char_u *p = arg; |
| 6146 | garray_T *instr = &cctx->ctx_instr; |
| 6147 | scope_T *scope; |
| 6148 | |
| 6149 | scope = new_scope(cctx, WHILE_SCOPE); |
| 6150 | if (scope == NULL) |
| 6151 | return NULL; |
| 6152 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6153 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6154 | |
| 6155 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6156 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6157 | return NULL; |
| 6158 | |
| 6159 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6160 | 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] | 6161 | JUMP_IF_FALSE, cctx) == FAIL) |
| 6162 | return FAIL; |
| 6163 | |
| 6164 | return p; |
| 6165 | } |
| 6166 | |
| 6167 | /* |
| 6168 | * compile "endwhile" |
| 6169 | */ |
| 6170 | static char_u * |
| 6171 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 6172 | { |
| 6173 | scope_T *scope = cctx->ctx_scope; |
| 6174 | |
| 6175 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 6176 | { |
| 6177 | emsg(_(e_while)); |
| 6178 | return NULL; |
| 6179 | } |
| 6180 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6181 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6182 | |
| 6183 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6184 | 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] | 6185 | |
| 6186 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 6187 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6188 | 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] | 6189 | |
| 6190 | vim_free(scope); |
| 6191 | |
| 6192 | return arg; |
| 6193 | } |
| 6194 | |
| 6195 | /* |
| 6196 | * compile "continue" |
| 6197 | */ |
| 6198 | static char_u * |
| 6199 | compile_continue(char_u *arg, cctx_T *cctx) |
| 6200 | { |
| 6201 | scope_T *scope = cctx->ctx_scope; |
| 6202 | |
| 6203 | for (;;) |
| 6204 | { |
| 6205 | if (scope == NULL) |
| 6206 | { |
| 6207 | emsg(_(e_continue)); |
| 6208 | return NULL; |
| 6209 | } |
| 6210 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6211 | break; |
| 6212 | scope = scope->se_outer; |
| 6213 | } |
| 6214 | |
| 6215 | // Jump back to the FOR or WHILE instruction. |
| 6216 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6217 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 6218 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6219 | return arg; |
| 6220 | } |
| 6221 | |
| 6222 | /* |
| 6223 | * compile "break" |
| 6224 | */ |
| 6225 | static char_u * |
| 6226 | compile_break(char_u *arg, cctx_T *cctx) |
| 6227 | { |
| 6228 | scope_T *scope = cctx->ctx_scope; |
| 6229 | endlabel_T **el; |
| 6230 | |
| 6231 | for (;;) |
| 6232 | { |
| 6233 | if (scope == NULL) |
| 6234 | { |
| 6235 | emsg(_(e_break)); |
| 6236 | return NULL; |
| 6237 | } |
| 6238 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6239 | break; |
| 6240 | scope = scope->se_outer; |
| 6241 | } |
| 6242 | |
| 6243 | // Jump to the end of the FOR or WHILE loop. |
| 6244 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6245 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6246 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6247 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6248 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 6249 | return FAIL; |
| 6250 | |
| 6251 | return arg; |
| 6252 | } |
| 6253 | |
| 6254 | /* |
| 6255 | * compile "{" start of block |
| 6256 | */ |
| 6257 | static char_u * |
| 6258 | compile_block(char_u *arg, cctx_T *cctx) |
| 6259 | { |
| 6260 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6261 | return NULL; |
| 6262 | return skipwhite(arg + 1); |
| 6263 | } |
| 6264 | |
| 6265 | /* |
| 6266 | * compile end of block: drop one scope |
| 6267 | */ |
| 6268 | static void |
| 6269 | compile_endblock(cctx_T *cctx) |
| 6270 | { |
| 6271 | scope_T *scope = cctx->ctx_scope; |
| 6272 | |
| 6273 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6274 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6275 | vim_free(scope); |
| 6276 | } |
| 6277 | |
| 6278 | /* |
| 6279 | * compile "try" |
| 6280 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6281 | * finally. |
| 6282 | * Creates another scope for the "try" block itself. |
| 6283 | * TRY instruction sets up exception handling at runtime. |
| 6284 | * |
| 6285 | * "try" |
| 6286 | * TRY -> catch1, -> finally push trystack entry |
| 6287 | * ... try block |
| 6288 | * "throw {exception}" |
| 6289 | * EVAL {exception} |
| 6290 | * THROW create exception |
| 6291 | * ... try block |
| 6292 | * " catch {expr}" |
| 6293 | * JUMP -> finally |
| 6294 | * catch1: PUSH exeception |
| 6295 | * EVAL {expr} |
| 6296 | * MATCH |
| 6297 | * JUMP nomatch -> catch2 |
| 6298 | * CATCH remove exception |
| 6299 | * ... catch block |
| 6300 | * " catch" |
| 6301 | * JUMP -> finally |
| 6302 | * catch2: CATCH remove exception |
| 6303 | * ... catch block |
| 6304 | * " finally" |
| 6305 | * finally: |
| 6306 | * ... finally block |
| 6307 | * " endtry" |
| 6308 | * ENDTRY pop trystack entry, may rethrow |
| 6309 | */ |
| 6310 | static char_u * |
| 6311 | compile_try(char_u *arg, cctx_T *cctx) |
| 6312 | { |
| 6313 | garray_T *instr = &cctx->ctx_instr; |
| 6314 | scope_T *try_scope; |
| 6315 | scope_T *scope; |
| 6316 | |
| 6317 | // scope that holds the jumps that go to catch/finally/endtry |
| 6318 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6319 | if (try_scope == NULL) |
| 6320 | return NULL; |
| 6321 | |
| 6322 | // "catch" is set when the first ":catch" is found. |
| 6323 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6324 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6325 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6326 | return NULL; |
| 6327 | |
| 6328 | // scope for the try block itself |
| 6329 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6330 | if (scope == NULL) |
| 6331 | return NULL; |
| 6332 | |
| 6333 | return arg; |
| 6334 | } |
| 6335 | |
| 6336 | /* |
| 6337 | * compile "catch {expr}" |
| 6338 | */ |
| 6339 | static char_u * |
| 6340 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6341 | { |
| 6342 | scope_T *scope = cctx->ctx_scope; |
| 6343 | garray_T *instr = &cctx->ctx_instr; |
| 6344 | char_u *p; |
| 6345 | isn_T *isn; |
| 6346 | |
| 6347 | // end block scope from :try or :catch |
| 6348 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6349 | compile_endblock(cctx); |
| 6350 | scope = cctx->ctx_scope; |
| 6351 | |
| 6352 | // Error if not in a :try scope |
| 6353 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6354 | { |
| 6355 | emsg(_(e_catch)); |
| 6356 | return NULL; |
| 6357 | } |
| 6358 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6359 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6360 | { |
| 6361 | emsg(_("E1033: catch unreachable after catch-all")); |
| 6362 | return NULL; |
| 6363 | } |
| 6364 | |
| 6365 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6366 | 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] | 6367 | JUMP_ALWAYS, cctx) == FAIL) |
| 6368 | return NULL; |
| 6369 | |
| 6370 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6371 | 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] | 6372 | if (isn->isn_arg.try.try_catch == 0) |
| 6373 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6374 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6375 | { |
| 6376 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6377 | 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] | 6378 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6379 | } |
| 6380 | |
| 6381 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6382 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6383 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6384 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6385 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6386 | } |
| 6387 | else |
| 6388 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6389 | char_u *end; |
| 6390 | char_u *pat; |
| 6391 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6392 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6393 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6394 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6395 | // Push v:exception, push {expr} and MATCH |
| 6396 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6397 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6398 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6399 | if (*end != *p) |
| 6400 | { |
| 6401 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 6402 | vim_free(tofree); |
| 6403 | return FAIL; |
| 6404 | } |
| 6405 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6406 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6407 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6408 | len = (int)(end - tofree); |
| 6409 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6410 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6411 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6412 | if (pat == NULL) |
| 6413 | return FAIL; |
| 6414 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6415 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6416 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6417 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6418 | return NULL; |
| 6419 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6420 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6421 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6422 | return NULL; |
| 6423 | } |
| 6424 | |
| 6425 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6426 | return NULL; |
| 6427 | |
| 6428 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6429 | return NULL; |
| 6430 | return p; |
| 6431 | } |
| 6432 | |
| 6433 | static char_u * |
| 6434 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6435 | { |
| 6436 | scope_T *scope = cctx->ctx_scope; |
| 6437 | garray_T *instr = &cctx->ctx_instr; |
| 6438 | isn_T *isn; |
| 6439 | |
| 6440 | // end block scope from :try or :catch |
| 6441 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6442 | compile_endblock(cctx); |
| 6443 | scope = cctx->ctx_scope; |
| 6444 | |
| 6445 | // Error if not in a :try scope |
| 6446 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6447 | { |
| 6448 | emsg(_(e_finally)); |
| 6449 | return NULL; |
| 6450 | } |
| 6451 | |
| 6452 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6453 | 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] | 6454 | if (isn->isn_arg.try.try_finally != 0) |
| 6455 | { |
| 6456 | emsg(_(e_finally_dup)); |
| 6457 | return NULL; |
| 6458 | } |
| 6459 | |
| 6460 | // 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] | 6461 | 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] | 6462 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6463 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6464 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6465 | { |
| 6466 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6467 | 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] | 6468 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6469 | } |
| 6470 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6471 | // TODO: set index in ts_finally_label jumps |
| 6472 | |
| 6473 | return arg; |
| 6474 | } |
| 6475 | |
| 6476 | static char_u * |
| 6477 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6478 | { |
| 6479 | scope_T *scope = cctx->ctx_scope; |
| 6480 | garray_T *instr = &cctx->ctx_instr; |
| 6481 | isn_T *isn; |
| 6482 | |
| 6483 | // end block scope from :catch or :finally |
| 6484 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6485 | compile_endblock(cctx); |
| 6486 | scope = cctx->ctx_scope; |
| 6487 | |
| 6488 | // Error if not in a :try scope |
| 6489 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6490 | { |
| 6491 | if (scope == NULL) |
| 6492 | emsg(_(e_no_endtry)); |
| 6493 | else if (scope->se_type == WHILE_SCOPE) |
| 6494 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6495 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6496 | emsg(_(e_endfor)); |
| 6497 | else |
| 6498 | emsg(_(e_endif)); |
| 6499 | return NULL; |
| 6500 | } |
| 6501 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6502 | 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] | 6503 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6504 | { |
| 6505 | emsg(_("E1032: missing :catch or :finally")); |
| 6506 | return NULL; |
| 6507 | } |
| 6508 | |
| 6509 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6510 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6511 | 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] | 6512 | |
| 6513 | // End :catch or :finally scope: set value in ISN_TRY instruction |
| 6514 | if (isn->isn_arg.try.try_finally == 0) |
| 6515 | isn->isn_arg.try.try_finally = instr->ga_len; |
| 6516 | compile_endblock(cctx); |
| 6517 | |
| 6518 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6519 | return NULL; |
| 6520 | return arg; |
| 6521 | } |
| 6522 | |
| 6523 | /* |
| 6524 | * compile "throw {expr}" |
| 6525 | */ |
| 6526 | static char_u * |
| 6527 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6528 | { |
| 6529 | char_u *p = skipwhite(arg); |
| 6530 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6531 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6532 | return NULL; |
| 6533 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6534 | return NULL; |
| 6535 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6536 | return NULL; |
| 6537 | |
| 6538 | return p; |
| 6539 | } |
| 6540 | |
| 6541 | /* |
| 6542 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6543 | * compile "echomsg expr" |
| 6544 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6545 | * compile "execute expr" |
| 6546 | */ |
| 6547 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6548 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6549 | { |
| 6550 | char_u *p = arg; |
| 6551 | int count = 0; |
| 6552 | |
| 6553 | for (;;) |
| 6554 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6555 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6556 | return NULL; |
| 6557 | ++count; |
| 6558 | p = skipwhite(p); |
| 6559 | if (ends_excmd(*p)) |
| 6560 | break; |
| 6561 | } |
| 6562 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6563 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6564 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6565 | else if (cmdidx == CMD_execute) |
| 6566 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6567 | else if (cmdidx == CMD_echomsg) |
| 6568 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6569 | else |
| 6570 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6571 | return p; |
| 6572 | } |
| 6573 | |
| 6574 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6575 | * A command that is not compiled, execute with legacy code. |
| 6576 | */ |
| 6577 | static char_u * |
| 6578 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6579 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6580 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6581 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6582 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6583 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6584 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6585 | goto theend; |
| 6586 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6587 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6588 | { |
| 6589 | long argt = excmd_get_argt(eap->cmdidx); |
| 6590 | int usefilter = FALSE; |
| 6591 | |
| 6592 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6593 | |
| 6594 | // If the command can be followed by a bar, find the bar and truncate |
| 6595 | // it, so that the following command can be compiled. |
| 6596 | // The '|' is overwritten with a NUL, it is put back below. |
| 6597 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6598 | && *eap->arg == '!') |
| 6599 | // :w !filter or :r !filter or :r! filter |
| 6600 | usefilter = TRUE; |
| 6601 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6602 | { |
| 6603 | separate_nextcmd(eap); |
| 6604 | if (eap->nextcmd != NULL) |
| 6605 | nextcmd = eap->nextcmd; |
| 6606 | } |
| 6607 | } |
| 6608 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6609 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6610 | { |
| 6611 | // expand filename in "syntax include [@group] filename" |
| 6612 | has_expr = TRUE; |
| 6613 | eap->arg = skipwhite(eap->arg + 7); |
| 6614 | if (*eap->arg == '@') |
| 6615 | eap->arg = skiptowhite(eap->arg); |
| 6616 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6617 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6618 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6619 | { |
| 6620 | int count = 0; |
| 6621 | char_u *start = skipwhite(line); |
| 6622 | |
| 6623 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6624 | // PUSHS ":cmd xxx" |
| 6625 | // eval expr1 |
| 6626 | // PUSHS "yyy" |
| 6627 | // eval expr2 |
| 6628 | // PUSHS "zzz" |
| 6629 | // EXECCONCAT 5 |
| 6630 | for (;;) |
| 6631 | { |
| 6632 | if (p > start) |
| 6633 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6634 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6635 | ++count; |
| 6636 | } |
| 6637 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6638 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6639 | return NULL; |
| 6640 | may_generate_2STRING(-1, cctx); |
| 6641 | ++count; |
| 6642 | p = skipwhite(p); |
| 6643 | if (*p != '`') |
| 6644 | { |
| 6645 | emsg(_("E1083: missing backtick")); |
| 6646 | return NULL; |
| 6647 | } |
| 6648 | start = p + 1; |
| 6649 | |
| 6650 | p = (char_u *)strstr((char *)start, "`="); |
| 6651 | if (p == NULL) |
| 6652 | { |
| 6653 | if (*skipwhite(start) != NUL) |
| 6654 | { |
| 6655 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6656 | ++count; |
| 6657 | } |
| 6658 | break; |
| 6659 | } |
| 6660 | } |
| 6661 | generate_EXECCONCAT(cctx, count); |
| 6662 | } |
| 6663 | else |
| 6664 | generate_EXEC(cctx, line); |
| 6665 | |
| 6666 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6667 | if (*nextcmd != NUL) |
| 6668 | { |
| 6669 | // the parser expects a pointer to the bar, put it back |
| 6670 | --nextcmd; |
| 6671 | *nextcmd = '|'; |
| 6672 | } |
| 6673 | |
| 6674 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6675 | } |
| 6676 | |
| 6677 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6678 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6679 | * 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] | 6680 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6681 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6682 | add_def_function(ufunc_T *ufunc) |
| 6683 | { |
| 6684 | dfunc_T *dfunc; |
| 6685 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6686 | if (def_functions.ga_len == 0) |
| 6687 | { |
| 6688 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6689 | // wasn't set. |
| 6690 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6691 | return FAIL; |
| 6692 | ++def_functions.ga_len; |
| 6693 | } |
| 6694 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6695 | // Add the function to "def_functions". |
| 6696 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6697 | return FAIL; |
| 6698 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6699 | CLEAR_POINTER(dfunc); |
| 6700 | dfunc->df_idx = def_functions.ga_len; |
| 6701 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6702 | dfunc->df_ufunc = ufunc; |
| 6703 | ++def_functions.ga_len; |
| 6704 | return OK; |
| 6705 | } |
| 6706 | |
| 6707 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6708 | * After ex_function() has collected all the function lines: parse and compile |
| 6709 | * the lines into instructions. |
| 6710 | * Adds the function to "def_functions". |
| 6711 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6712 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6713 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6714 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6715 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6716 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6717 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6718 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6719 | 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] | 6720 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6721 | char_u *line = NULL; |
| 6722 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6723 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6724 | cctx_T cctx; |
| 6725 | garray_T *instr; |
| 6726 | int called_emsg_before = called_emsg; |
| 6727 | int ret = FAIL; |
| 6728 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6729 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6730 | int emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6731 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6732 | // When using a function that was compiled before: Free old instructions. |
| 6733 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6734 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6735 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6736 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6737 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6738 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6739 | } |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6740 | else if (add_def_function(ufunc) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6741 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6742 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6743 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6744 | cctx.ctx_ufunc = ufunc; |
| 6745 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6746 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6747 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6748 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6749 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6750 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6751 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6752 | instr = &cctx.ctx_instr; |
| 6753 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6754 | // Set the context to the function, it may be compiled when called from |
| 6755 | // another script. Set the script version to the most modern one. |
| 6756 | // The line number will be set in next_line_from_context(). |
| 6757 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6758 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6759 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6760 | // Make sure error messages are OK. |
| 6761 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6762 | if (do_estack_push) |
| 6763 | estack_push_ufunc(ufunc, 1); |
| 6764 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6765 | if (ufunc->uf_def_args.ga_len > 0) |
| 6766 | { |
| 6767 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6768 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6769 | int i; |
| 6770 | char_u *arg; |
| 6771 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 6772 | |
| 6773 | // Produce instructions for the default values of optional arguments. |
| 6774 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6775 | // where to start when the function is called, depending on the number |
| 6776 | // of arguments. |
| 6777 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6778 | if (ufunc->uf_def_arg_idx == NULL) |
| 6779 | goto erret; |
| 6780 | for (i = 0; i < count; ++i) |
| 6781 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6782 | garray_T *stack = &cctx.ctx_type_stack; |
| 6783 | type_T *val_type; |
| 6784 | int arg_idx = first_def_arg + i; |
| 6785 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6786 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6787 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6788 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6789 | goto erret; |
| 6790 | |
| 6791 | // If no type specified use the type of the default value. |
| 6792 | // Otherwise check that the default value type matches the |
| 6793 | // specified type. |
| 6794 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6795 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
| 6796 | ufunc->uf_arg_types[arg_idx] = val_type; |
| 6797 | else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE) |
| 6798 | == FAIL) |
| 6799 | { |
| 6800 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 6801 | arg_idx + 1); |
| 6802 | goto erret; |
| 6803 | } |
| 6804 | |
| 6805 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6806 | goto erret; |
| 6807 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6808 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
| 6809 | } |
| 6810 | |
| 6811 | /* |
| 6812 | * Loop over all the lines of the function and generate instructions. |
| 6813 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6814 | for (;;) |
| 6815 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6816 | exarg_T ea; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6817 | int starts_with_colon = FALSE; |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6818 | char_u *cmd; |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 6819 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6820 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6821 | // Bail out on the first error to avoid a flood of errors and report |
| 6822 | // the right line number when inside try/catch. |
| 6823 | if (emsg_before != called_emsg) |
| 6824 | goto erret; |
| 6825 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6826 | if (line != NULL && *line == '|') |
| 6827 | // the line continues after a '|' |
| 6828 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 6829 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6830 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 6831 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6832 | { |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6833 | semsg(_("E488: Trailing characters: %s"), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6834 | goto erret; |
| 6835 | } |
| 6836 | else |
| 6837 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 6838 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 6839 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6840 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6841 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6842 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6843 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6844 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6845 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6846 | ea.cmdlinep = &line; |
| 6847 | ea.cmd = skipwhite(line); |
| 6848 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6849 | // Some things can be recognized by the first character. |
| 6850 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6851 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6852 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6853 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6854 | if (ea.cmd[1] != '{') |
| 6855 | { |
| 6856 | line = (char_u *)""; |
| 6857 | continue; |
| 6858 | } |
| 6859 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6860 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6861 | case '}': |
| 6862 | { |
| 6863 | // "}" ends a block scope |
| 6864 | scopetype_T stype = cctx.ctx_scope == NULL |
| 6865 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6866 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6867 | if (stype == BLOCK_SCOPE) |
| 6868 | { |
| 6869 | compile_endblock(&cctx); |
| 6870 | line = ea.cmd; |
| 6871 | } |
| 6872 | else |
| 6873 | { |
| 6874 | emsg(_("E1025: using } outside of a block scope")); |
| 6875 | goto erret; |
| 6876 | } |
| 6877 | if (line != NULL) |
| 6878 | line = skipwhite(ea.cmd + 1); |
| 6879 | continue; |
| 6880 | } |
| 6881 | |
| 6882 | case '{': |
| 6883 | // "{" starts a block scope |
| 6884 | // "{'a': 1}->func() is something else |
| 6885 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 6886 | { |
| 6887 | line = compile_block(ea.cmd, &cctx); |
| 6888 | continue; |
| 6889 | } |
| 6890 | break; |
| 6891 | |
| 6892 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6893 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6894 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6895 | } |
| 6896 | |
| 6897 | /* |
| 6898 | * COMMAND MODIFIERS |
| 6899 | */ |
| 6900 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 6901 | { |
| 6902 | if (errormsg != NULL) |
| 6903 | goto erret; |
| 6904 | // empty line or comment |
| 6905 | line = (char_u *)""; |
| 6906 | continue; |
| 6907 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 6908 | // TODO: use modifiers in the command |
| 6909 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6910 | |
| 6911 | // Skip ":call" to get to the function name. |
| 6912 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 6913 | ea.cmd = skipwhite(ea.cmd); |
| 6914 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6915 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6916 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6917 | char_u *pskip; |
| 6918 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6919 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6920 | // find what follows. |
| 6921 | // Skip over "var.member", "var[idx]" and the like. |
| 6922 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 6923 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6924 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6925 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6926 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6927 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6928 | char_u *var_end; |
| 6929 | int oplen; |
| 6930 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6931 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6932 | var_end = find_name_end(pskip, NULL, NULL, |
| 6933 | FNE_CHECK_START | FNE_INCL_BR); |
| 6934 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6935 | if (oplen > 0) |
| 6936 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6937 | size_t len = p - ea.cmd; |
| 6938 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6939 | // Recognize an assignment if we recognize the variable |
| 6940 | // name: |
| 6941 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 6942 | // "local = expr" where "local" is a local var. |
| 6943 | // "script = expr" where "script" is a script-local var. |
| 6944 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6945 | // "&opt = expr" |
| 6946 | // "$ENV = expr" |
| 6947 | // "@r = expr" |
| 6948 | if (*ea.cmd == '&' |
| 6949 | || *ea.cmd == '$' |
| 6950 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6951 | || ((len) > 2 && ea.cmd[1] == ':') |
| 6952 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 6953 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 6954 | NULL, &cctx) == OK |
| 6955 | || lookup_script(ea.cmd, len) == OK |
| 6956 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6957 | { |
| 6958 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6959 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6960 | goto erret; |
| 6961 | continue; |
| 6962 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6963 | } |
| 6964 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6965 | |
| 6966 | if (*ea.cmd == '[') |
| 6967 | { |
| 6968 | // [var, var] = expr |
| 6969 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 6970 | if (line == NULL) |
| 6971 | goto erret; |
| 6972 | if (line != ea.cmd) |
| 6973 | continue; |
| 6974 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6975 | } |
| 6976 | |
| 6977 | /* |
| 6978 | * COMMAND after range |
| 6979 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6980 | cmd = ea.cmd; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6981 | ea.cmd = skip_range(ea.cmd, NULL); |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6982 | if (ea.cmd > cmd && !starts_with_colon) |
| 6983 | { |
| 6984 | emsg(_(e_colon_required)); |
| 6985 | goto erret; |
| 6986 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6987 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6988 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6989 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6990 | |
| 6991 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 6992 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6993 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6994 | { |
| 6995 | line += STRLEN(line); |
| 6996 | continue; |
| 6997 | } |
| 6998 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6999 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7000 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7001 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7002 | // CMD_let cannot happen, compile_assignment() above is used |
| 7003 | iemsg("Command from find_ex_command() not handled"); |
| 7004 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7005 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7006 | } |
| 7007 | |
| 7008 | p = skipwhite(p); |
| 7009 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7010 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 7011 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7012 | && ea.cmdidx != CMD_elseif |
| 7013 | && ea.cmdidx != CMD_else |
| 7014 | && ea.cmdidx != CMD_endif) |
| 7015 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7016 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7017 | continue; |
| 7018 | } |
| 7019 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7020 | if (ea.cmdidx != CMD_elseif |
| 7021 | && ea.cmdidx != CMD_else |
| 7022 | && ea.cmdidx != CMD_endif |
| 7023 | && ea.cmdidx != CMD_endfor |
| 7024 | && ea.cmdidx != CMD_endwhile |
| 7025 | && ea.cmdidx != CMD_catch |
| 7026 | && ea.cmdidx != CMD_finally |
| 7027 | && ea.cmdidx != CMD_endtry) |
| 7028 | { |
| 7029 | if (cctx.ctx_had_return) |
| 7030 | { |
| 7031 | emsg(_("E1095: Unreachable code after :return")); |
| 7032 | goto erret; |
| 7033 | } |
| 7034 | } |
| 7035 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7036 | switch (ea.cmdidx) |
| 7037 | { |
| 7038 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7039 | ea.arg = p; |
| 7040 | line = compile_nested_function(&ea, &cctx); |
| 7041 | break; |
| 7042 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7043 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7044 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7045 | goto erret; |
| 7046 | |
| 7047 | case CMD_return: |
| 7048 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7049 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7050 | break; |
| 7051 | |
| 7052 | case CMD_let: |
| 7053 | case CMD_const: |
| 7054 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7055 | if (line == p) |
| 7056 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7057 | break; |
| 7058 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7059 | case CMD_unlet: |
| 7060 | case CMD_unlockvar: |
| 7061 | case CMD_lockvar: |
| 7062 | line = compile_unletlock(p, &ea, &cctx); |
| 7063 | break; |
| 7064 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7065 | case CMD_import: |
| 7066 | line = compile_import(p, &cctx); |
| 7067 | break; |
| 7068 | |
| 7069 | case CMD_if: |
| 7070 | line = compile_if(p, &cctx); |
| 7071 | break; |
| 7072 | case CMD_elseif: |
| 7073 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7074 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7075 | break; |
| 7076 | case CMD_else: |
| 7077 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7078 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7079 | break; |
| 7080 | case CMD_endif: |
| 7081 | line = compile_endif(p, &cctx); |
| 7082 | break; |
| 7083 | |
| 7084 | case CMD_while: |
| 7085 | line = compile_while(p, &cctx); |
| 7086 | break; |
| 7087 | case CMD_endwhile: |
| 7088 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7089 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7090 | break; |
| 7091 | |
| 7092 | case CMD_for: |
| 7093 | line = compile_for(p, &cctx); |
| 7094 | break; |
| 7095 | case CMD_endfor: |
| 7096 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7097 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7098 | break; |
| 7099 | case CMD_continue: |
| 7100 | line = compile_continue(p, &cctx); |
| 7101 | break; |
| 7102 | case CMD_break: |
| 7103 | line = compile_break(p, &cctx); |
| 7104 | break; |
| 7105 | |
| 7106 | case CMD_try: |
| 7107 | line = compile_try(p, &cctx); |
| 7108 | break; |
| 7109 | case CMD_catch: |
| 7110 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7111 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7112 | break; |
| 7113 | case CMD_finally: |
| 7114 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7115 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7116 | break; |
| 7117 | case CMD_endtry: |
| 7118 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7119 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7120 | break; |
| 7121 | case CMD_throw: |
| 7122 | line = compile_throw(p, &cctx); |
| 7123 | break; |
| 7124 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7125 | case CMD_eval: |
| 7126 | if (compile_expr0(&p, &cctx) == FAIL) |
| 7127 | goto erret; |
| 7128 | |
| 7129 | // drop the return value |
| 7130 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 7131 | |
| 7132 | line = skipwhite(p); |
| 7133 | break; |
| 7134 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7135 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7136 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7137 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7138 | case CMD_echomsg: |
| 7139 | case CMD_echoerr: |
| 7140 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7141 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7142 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7143 | // TODO: other commands with an expression argument |
| 7144 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7145 | case CMD_SIZE: |
| 7146 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 7147 | goto erret; |
| 7148 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7149 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7150 | // Not recognized, execute with do_cmdline_cmd(). |
| 7151 | ea.arg = p; |
| 7152 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7153 | break; |
| 7154 | } |
| 7155 | if (line == NULL) |
| 7156 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7157 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7158 | |
| 7159 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7160 | { |
| 7161 | iemsg("Type stack underflow"); |
| 7162 | goto erret; |
| 7163 | } |
| 7164 | } |
| 7165 | |
| 7166 | if (cctx.ctx_scope != NULL) |
| 7167 | { |
| 7168 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7169 | emsg(_(e_endif)); |
| 7170 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7171 | emsg(_(e_endwhile)); |
| 7172 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7173 | emsg(_(e_endfor)); |
| 7174 | else |
| 7175 | emsg(_("E1026: Missing }")); |
| 7176 | goto erret; |
| 7177 | } |
| 7178 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7179 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7180 | { |
| 7181 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7182 | { |
| 7183 | emsg(_("E1027: Missing return statement")); |
| 7184 | goto erret; |
| 7185 | } |
| 7186 | |
| 7187 | // Return zero if there is no return at the end. |
| 7188 | generate_PUSHNR(&cctx, 0); |
| 7189 | generate_instr(&cctx, ISN_RETURN); |
| 7190 | } |
| 7191 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7192 | { |
| 7193 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7194 | + ufunc->uf_dfunc_idx; |
| 7195 | dfunc->df_deleted = FALSE; |
| 7196 | dfunc->df_instr = instr->ga_data; |
| 7197 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7198 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 7199 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7200 | if (cctx.ctx_outer_used) |
| 7201 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7202 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7203 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7204 | |
| 7205 | ret = OK; |
| 7206 | |
| 7207 | erret: |
| 7208 | if (ret == FAIL) |
| 7209 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7210 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7211 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7212 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7213 | |
| 7214 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7215 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7216 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7217 | |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7218 | // if using the last entry in the table we might as well remove it |
| 7219 | if (!dfunc->df_deleted |
| 7220 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7221 | --def_functions.ga_len; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7222 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7223 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7224 | while (cctx.ctx_scope != NULL) |
| 7225 | drop_scope(&cctx); |
| 7226 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7227 | // Don't execute this function body. |
| 7228 | ga_clear_strings(&ufunc->uf_lines); |
| 7229 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7230 | if (errormsg != NULL) |
| 7231 | emsg(errormsg); |
| 7232 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 7233 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7234 | } |
| 7235 | |
| 7236 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7237 | if (do_estack_push) |
| 7238 | estack_pop(); |
| 7239 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7240 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7241 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7242 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7243 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7244 | } |
| 7245 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7246 | void |
| 7247 | set_function_type(ufunc_T *ufunc) |
| 7248 | { |
| 7249 | int varargs = ufunc->uf_va_name != NULL; |
| 7250 | int argcount = ufunc->uf_args.ga_len; |
| 7251 | |
| 7252 | // Create a type for the function, with the return type and any |
| 7253 | // argument types. |
| 7254 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7255 | // The type is included in "tt_args". |
| 7256 | if (argcount > 0 || varargs) |
| 7257 | { |
| 7258 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7259 | argcount, &ufunc->uf_type_list); |
| 7260 | // Add argument types to the function type. |
| 7261 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7262 | argcount + varargs, |
| 7263 | &ufunc->uf_type_list) == FAIL) |
| 7264 | return; |
| 7265 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7266 | ufunc->uf_func_type->tt_min_argcount = |
| 7267 | argcount - ufunc->uf_def_args.ga_len; |
| 7268 | if (ufunc->uf_arg_types == NULL) |
| 7269 | { |
| 7270 | int i; |
| 7271 | |
| 7272 | // lambda does not have argument types. |
| 7273 | for (i = 0; i < argcount; ++i) |
| 7274 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7275 | } |
| 7276 | else |
| 7277 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7278 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7279 | if (varargs) |
| 7280 | { |
| 7281 | ufunc->uf_func_type->tt_args[argcount] = |
| 7282 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7283 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7284 | } |
| 7285 | } |
| 7286 | else |
| 7287 | // No arguments, can use a predefined type. |
| 7288 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7289 | argcount, &ufunc->uf_type_list); |
| 7290 | } |
| 7291 | |
| 7292 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7293 | /* |
| 7294 | * Delete an instruction, free what it contains. |
| 7295 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7296 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7297 | delete_instr(isn_T *isn) |
| 7298 | { |
| 7299 | switch (isn->isn_type) |
| 7300 | { |
| 7301 | case ISN_EXEC: |
| 7302 | case ISN_LOADENV: |
| 7303 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7304 | case ISN_LOADB: |
| 7305 | case ISN_LOADW: |
| 7306 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7307 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7308 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7309 | case ISN_PUSHEXC: |
| 7310 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7311 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7312 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7313 | case ISN_STOREB: |
| 7314 | case ISN_STOREW: |
| 7315 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7316 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7317 | vim_free(isn->isn_arg.string); |
| 7318 | break; |
| 7319 | |
| 7320 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7321 | case ISN_STORES: |
| 7322 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7323 | break; |
| 7324 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7325 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7326 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7327 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7328 | break; |
| 7329 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7330 | case ISN_STOREOPT: |
| 7331 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7332 | break; |
| 7333 | |
| 7334 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7335 | blob_unref(isn->isn_arg.blob); |
| 7336 | break; |
| 7337 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7338 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7339 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7340 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7341 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7342 | break; |
| 7343 | |
| 7344 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7345 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7346 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7347 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7348 | break; |
| 7349 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7350 | case ISN_UCALL: |
| 7351 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7352 | break; |
| 7353 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7354 | case ISN_FUNCREF: |
| 7355 | { |
| 7356 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7357 | + isn->isn_arg.funcref.fr_func; |
| 7358 | func_ptr_unref(dfunc->df_ufunc); |
| 7359 | } |
| 7360 | break; |
| 7361 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7362 | case ISN_2BOOL: |
| 7363 | case ISN_2STRING: |
| 7364 | case ISN_ADDBLOB: |
| 7365 | case ISN_ADDLIST: |
| 7366 | case ISN_BCALL: |
| 7367 | case ISN_CATCH: |
| 7368 | case ISN_CHECKNR: |
| 7369 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7370 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7371 | case ISN_COMPAREANY: |
| 7372 | case ISN_COMPAREBLOB: |
| 7373 | case ISN_COMPAREBOOL: |
| 7374 | case ISN_COMPAREDICT: |
| 7375 | case ISN_COMPAREFLOAT: |
| 7376 | case ISN_COMPAREFUNC: |
| 7377 | case ISN_COMPARELIST: |
| 7378 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7379 | case ISN_COMPARESPECIAL: |
| 7380 | case ISN_COMPARESTRING: |
| 7381 | case ISN_CONCAT: |
| 7382 | case ISN_DCALL: |
| 7383 | case ISN_DROP: |
| 7384 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7385 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7386 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7387 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7388 | case ISN_EXECCONCAT: |
| 7389 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7390 | case ISN_FOR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7391 | case ISN_INDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7392 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7393 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7394 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7395 | case ISN_JUMP: |
| 7396 | case ISN_LOAD: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7397 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7398 | case ISN_LOADSCRIPT: |
| 7399 | case ISN_LOADREG: |
| 7400 | case ISN_LOADV: |
| 7401 | case ISN_NEGATENR: |
| 7402 | case ISN_NEWDICT: |
| 7403 | case ISN_NEWLIST: |
| 7404 | case ISN_OPNR: |
| 7405 | case ISN_OPFLOAT: |
| 7406 | case ISN_OPANY: |
| 7407 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7408 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7409 | case ISN_PUSHF: |
| 7410 | case ISN_PUSHNR: |
| 7411 | case ISN_PUSHBOOL: |
| 7412 | case ISN_PUSHSPEC: |
| 7413 | case ISN_RETURN: |
| 7414 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 7415 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7416 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7417 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7418 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7419 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7420 | case ISN_STOREDICT: |
| 7421 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7422 | case ISN_THROW: |
| 7423 | case ISN_TRY: |
| 7424 | // nothing allocated |
| 7425 | break; |
| 7426 | } |
| 7427 | } |
| 7428 | |
| 7429 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7430 | * Free all instructions for "dfunc". |
| 7431 | */ |
| 7432 | static void |
| 7433 | delete_def_function_contents(dfunc_T *dfunc) |
| 7434 | { |
| 7435 | int idx; |
| 7436 | |
| 7437 | ga_clear(&dfunc->df_def_args_isn); |
| 7438 | |
| 7439 | if (dfunc->df_instr != NULL) |
| 7440 | { |
| 7441 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7442 | delete_instr(dfunc->df_instr + idx); |
| 7443 | VIM_CLEAR(dfunc->df_instr); |
| 7444 | } |
| 7445 | |
| 7446 | dfunc->df_deleted = TRUE; |
| 7447 | } |
| 7448 | |
| 7449 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7450 | * When a user function is deleted, clear the contents of any associated def |
| 7451 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7452 | */ |
| 7453 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7454 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7455 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7456 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7457 | { |
| 7458 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7459 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7460 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7461 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7462 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7463 | } |
| 7464 | } |
| 7465 | |
| 7466 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7467 | /* |
| 7468 | * Free all functions defined with ":def". |
| 7469 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7470 | void |
| 7471 | free_def_functions(void) |
| 7472 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7473 | int idx; |
| 7474 | |
| 7475 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7476 | { |
| 7477 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7478 | |
| 7479 | delete_def_function_contents(dfunc); |
| 7480 | } |
| 7481 | |
| 7482 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7483 | } |
| 7484 | #endif |
| 7485 | |
| 7486 | |
| 7487 | #endif // FEAT_EVAL |