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