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 |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 111 | int lv_from_outer; // nesting level, using ctx_outer scope |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 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 | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 129 | int ctx_has_closure; // set to one if a closures was created in |
| 130 | // the function |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 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 | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 144 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 145 | int ctx_has_cmdmod; // ISN_CMDMOD was generated |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 146 | }; |
| 147 | |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 148 | static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 149 | |
| 150 | /* |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 151 | * Lookup variable "name" in the local scope and return it in "lvar". |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 152 | * "lvar->lv_from_outer" is incremented accordingly. |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 153 | * If "lvar" is NULL only check if the variable can be found. |
| 154 | * Return FAIL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 155 | */ |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 156 | static int |
| 157 | lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 158 | { |
| 159 | int idx; |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 160 | lvar_T *lvp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 161 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 162 | if (len == 0) |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 163 | return FAIL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 164 | |
| 165 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 166 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 167 | { |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 168 | lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 169 | if (STRNCMP(name, lvp->lv_name, len) == 0 |
| 170 | && STRLEN(lvp->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 171 | { |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 172 | if (lvar != NULL) |
| 173 | { |
| 174 | *lvar = *lvp; |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 175 | lvar->lv_from_outer = 0; |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 176 | } |
| 177 | return OK; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 178 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 179 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 180 | |
| 181 | // Find local in outer function scope. |
| 182 | if (cctx->ctx_outer != NULL) |
| 183 | { |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 184 | if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 185 | { |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 186 | if (lvar != NULL) |
| 187 | { |
| 188 | cctx->ctx_outer_used = TRUE; |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 189 | ++lvar->lv_from_outer; |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 190 | } |
| 191 | return OK; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 195 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 199 | * Lookup an argument in the current function and an enclosing function. |
| 200 | * Returns the argument index in "idxp" |
| 201 | * Returns the argument type in "type" |
| 202 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 203 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 204 | */ |
| 205 | static int |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 206 | arg_exists( |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 207 | char_u *name, |
| 208 | size_t len, |
| 209 | int *idxp, |
| 210 | type_T **type, |
| 211 | int *gen_load_outer, |
| 212 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 213 | { |
| 214 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 215 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 216 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 217 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 218 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 219 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 220 | { |
| 221 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 222 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 223 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 224 | { |
| 225 | if (idxp != NULL) |
| 226 | { |
| 227 | // Arguments are located above the frame pointer. One further |
| 228 | // if there is a vararg argument |
| 229 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 230 | + STACK_FRAME_SIZE) |
| 231 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 232 | |
| 233 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 234 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 235 | else |
| 236 | *type = &t_any; |
| 237 | } |
| 238 | return OK; |
| 239 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 240 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 241 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 242 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 243 | if (va_name != NULL |
| 244 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 245 | { |
| 246 | if (idxp != NULL) |
| 247 | { |
| 248 | // varargs is always the last argument |
| 249 | *idxp = -STACK_FRAME_SIZE - 1; |
| 250 | *type = cctx->ctx_ufunc->uf_va_type; |
| 251 | } |
| 252 | return OK; |
| 253 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 254 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 255 | if (cctx->ctx_outer != NULL) |
| 256 | { |
| 257 | // Lookup the name for an argument of the outer function. |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 258 | if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 259 | == OK) |
| 260 | { |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 261 | ++*gen_load_outer; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 262 | return OK; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 270 | * Lookup a script-local variable in the current script, possibly defined in a |
| 271 | * block that contains the function "cctx->ctx_ufunc". |
| 272 | * "cctx" is NULL at the script level. |
| 273 | * if "len" is <= 0 "name" must be NUL terminated. |
| 274 | * Return NULL when not found. |
| 275 | */ |
| 276 | static sallvar_T * |
| 277 | find_script_var(char_u *name, size_t len, cctx_T *cctx) |
| 278 | { |
| 279 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
| 280 | hashitem_T *hi; |
| 281 | int cc; |
| 282 | sallvar_T *sav; |
| 283 | ufunc_T *ufunc; |
| 284 | |
| 285 | // Find the list of all script variables with the right name. |
| 286 | if (len > 0) |
| 287 | { |
| 288 | cc = name[len]; |
| 289 | name[len] = NUL; |
| 290 | } |
| 291 | hi = hash_find(&si->sn_all_vars.dv_hashtab, name); |
| 292 | if (len > 0) |
| 293 | name[len] = cc; |
| 294 | if (HASHITEM_EMPTY(hi)) |
| 295 | return NULL; |
| 296 | |
| 297 | sav = HI2SAV(hi); |
| 298 | if (sav->sav_block_id == 0 || cctx == NULL) |
| 299 | // variable defined in the script scope or not in a function. |
| 300 | return sav; |
| 301 | |
| 302 | // Go over the variables with this name and find one that was visible |
| 303 | // from the function. |
| 304 | ufunc = cctx->ctx_ufunc; |
| 305 | while (sav != NULL) |
| 306 | { |
| 307 | int idx; |
| 308 | |
| 309 | // Go over the blocks that this function was defined in. If the |
| 310 | // variable block ID matches it was visible to the function. |
| 311 | for (idx = 0; idx < ufunc->uf_block_depth; ++idx) |
| 312 | if (ufunc->uf_block_ids[idx] == sav->sav_block_id) |
| 313 | return sav; |
| 314 | sav = sav->sav_next; |
| 315 | } |
| 316 | |
| 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | /* |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 321 | * Return TRUE if the script context is Vim9 script. |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 322 | */ |
| 323 | static int |
| 324 | script_is_vim9() |
| 325 | { |
| 326 | return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9; |
| 327 | } |
| 328 | |
| 329 | /* |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 330 | * Lookup a variable (without s: prefix) in the current script. |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 331 | * If "vim9script" is TRUE the script must be Vim9 script. Used for "var" |
| 332 | * without "s:". |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 333 | * "cctx" is NULL at the script level. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 334 | * Returns OK or FAIL. |
| 335 | */ |
| 336 | static int |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 337 | script_var_exists(char_u *name, size_t len, int vim9script, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 338 | { |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 339 | int is_vim9_script; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 340 | |
Bram Moolenaar | 9c4f552 | 2020-09-25 21:47:28 +0200 | [diff] [blame] | 341 | if (current_sctx.sc_sid <= 0) |
| 342 | return FAIL; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 343 | is_vim9_script = script_is_vim9(); |
| 344 | if (vim9script && !is_vim9_script) |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 345 | return FAIL; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 346 | if (is_vim9_script) |
| 347 | { |
| 348 | // Check script variables that were visible where the function was |
| 349 | // defined. |
| 350 | if (find_script_var(name, len, cctx) != NULL) |
| 351 | return OK; |
| 352 | } |
| 353 | else |
| 354 | { |
| 355 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 356 | dictitem_T *di; |
| 357 | int cc; |
| 358 | |
| 359 | // Check script variables that are currently visible |
| 360 | cc = name[len]; |
| 361 | name[len] = NUL; |
| 362 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 363 | name[len] = cc; |
| 364 | if (di != NULL) |
| 365 | return OK; |
| 366 | } |
| 367 | |
| 368 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 371 | /* |
| 372 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 373 | * compilation context "cctx". "cctx" is NULL at the script level. |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 374 | * Does not check the global namespace. |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 375 | * Return FAIL and give an error if it defined. |
| 376 | */ |
| 377 | int |
Bram Moolenaar | cbb6bdc | 2020-07-06 21:53:17 +0200 | [diff] [blame] | 378 | check_defined(char_u *p, size_t len, cctx_T *cctx) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 379 | { |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 380 | int c = p[len]; |
| 381 | ufunc_T *ufunc = NULL; |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 382 | |
| 383 | p[len] = NUL; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 384 | if (script_var_exists(p, len, FALSE, cctx) == OK |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 385 | || (cctx != NULL |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 386 | && (lookup_local(p, len, NULL, cctx) == OK |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 387 | || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK)) |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 388 | || find_imported(p, len, cctx) != NULL |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 389 | || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 390 | { |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 391 | // A local or script-local function can shadow a global function. |
| 392 | if (ufunc == NULL || !func_is_global(ufunc) |
| 393 | || (p[0] == 'g' && p[1] == ':')) |
| 394 | { |
| 395 | p[len] = c; |
| 396 | semsg(_(e_name_already_defined_str), p); |
| 397 | return FAIL; |
| 398 | } |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 399 | } |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 400 | p[len] = c; |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 401 | return OK; |
| 402 | } |
| 403 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 404 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 405 | ///////////////////////////////////////////////////////////////////// |
| 406 | // Following generate_ functions expect the caller to call ga_grow(). |
| 407 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 408 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 409 | #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] | 410 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 411 | /* |
| 412 | * Generate an instruction without arguments. |
| 413 | * Returns a pointer to the new instruction, NULL if failed. |
| 414 | */ |
| 415 | static isn_T * |
| 416 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 417 | { |
| 418 | garray_T *instr = &cctx->ctx_instr; |
| 419 | isn_T *isn; |
| 420 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 421 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 422 | if (ga_grow(instr, 1) == FAIL) |
| 423 | return NULL; |
| 424 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 425 | isn->isn_type = isn_type; |
| 426 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 427 | ++instr->ga_len; |
| 428 | |
| 429 | return isn; |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * Generate an instruction without arguments. |
| 434 | * "drop" will be removed from the stack. |
| 435 | * Returns a pointer to the new instruction, NULL if failed. |
| 436 | */ |
| 437 | static isn_T * |
| 438 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 439 | { |
| 440 | garray_T *stack = &cctx->ctx_type_stack; |
| 441 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 442 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 443 | stack->ga_len -= drop; |
| 444 | return generate_instr(cctx, isn_type); |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 449 | */ |
| 450 | static isn_T * |
| 451 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 452 | { |
| 453 | isn_T *isn; |
| 454 | garray_T *stack = &cctx->ctx_type_stack; |
| 455 | |
| 456 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 457 | return NULL; |
| 458 | |
| 459 | if (ga_grow(stack, 1) == FAIL) |
| 460 | return NULL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 461 | ((type_T **)stack->ga_data)[stack->ga_len] = type == NULL ? &t_any : type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 462 | ++stack->ga_len; |
| 463 | |
| 464 | return isn; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 469 | * But only for simple types. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 470 | */ |
| 471 | static int |
| 472 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 473 | { |
| 474 | isn_T *isn; |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 475 | isntype_T isntype = ISN_2STRING; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 476 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 9e1d9e3 | 2021-01-11 20:17:34 +0100 | [diff] [blame] | 477 | type_T **type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 478 | |
Bram Moolenaar | 9e1d9e3 | 2021-01-11 20:17:34 +0100 | [diff] [blame] | 479 | RETURN_OK_IF_SKIP(cctx); |
| 480 | type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 481 | switch ((*type)->tt_type) |
| 482 | { |
| 483 | // nothing to be done |
| 484 | case VAR_STRING: return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 485 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 486 | // conversion possible |
| 487 | case VAR_SPECIAL: |
| 488 | case VAR_BOOL: |
| 489 | case VAR_NUMBER: |
| 490 | case VAR_FLOAT: |
| 491 | break; |
| 492 | |
| 493 | // conversion possible (with runtime check) |
| 494 | case VAR_ANY: |
| 495 | case VAR_UNKNOWN: |
| 496 | isntype = ISN_2STRING_ANY; |
| 497 | break; |
| 498 | |
| 499 | // conversion not possible |
| 500 | case VAR_VOID: |
| 501 | case VAR_BLOB: |
| 502 | case VAR_FUNC: |
| 503 | case VAR_PARTIAL: |
| 504 | case VAR_LIST: |
| 505 | case VAR_DICT: |
| 506 | case VAR_JOB: |
| 507 | case VAR_CHANNEL: |
| 508 | to_string_error((*type)->tt_type); |
| 509 | return FAIL; |
| 510 | } |
| 511 | |
| 512 | *type = &t_string; |
| 513 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 514 | return FAIL; |
| 515 | isn->isn_arg.number = offset; |
| 516 | |
| 517 | return OK; |
| 518 | } |
| 519 | |
| 520 | static int |
| 521 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 522 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 523 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 524 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 525 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 526 | { |
| 527 | if (*op == '+') |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 528 | emsg(_(e_wrong_argument_type_for_plus)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 529 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 530 | semsg(_(e_char_requires_number_or_float_arguments), *op); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 531 | return FAIL; |
| 532 | } |
| 533 | return OK; |
| 534 | } |
| 535 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 536 | static int |
| 537 | generate_add_instr( |
| 538 | cctx_T *cctx, |
| 539 | vartype_T vartype, |
| 540 | type_T *type1, |
| 541 | type_T *type2) |
| 542 | { |
Bram Moolenaar | 399ea81 | 2020-12-15 21:28:57 +0100 | [diff] [blame] | 543 | garray_T *stack = &cctx->ctx_type_stack; |
| 544 | isn_T *isn = generate_instr_drop(cctx, |
| 545 | vartype == VAR_NUMBER ? ISN_OPNR |
| 546 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 547 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 548 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 399ea81 | 2020-12-15 21:28:57 +0100 | [diff] [blame] | 549 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 550 | #endif |
Bram Moolenaar | 399ea81 | 2020-12-15 21:28:57 +0100 | [diff] [blame] | 551 | : ISN_OPANY, 1); |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 552 | |
| 553 | if (vartype != VAR_LIST && vartype != VAR_BLOB |
| 554 | && type1->tt_type != VAR_ANY |
| 555 | && type2->tt_type != VAR_ANY |
| 556 | && check_number_or_float( |
| 557 | type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL) |
| 558 | return FAIL; |
| 559 | |
| 560 | if (isn != NULL) |
| 561 | isn->isn_arg.op.op_type = EXPR_ADD; |
Bram Moolenaar | 399ea81 | 2020-12-15 21:28:57 +0100 | [diff] [blame] | 562 | |
| 563 | // When concatenating two lists with different member types the member type |
| 564 | // becomes "any". |
| 565 | if (vartype == VAR_LIST |
| 566 | && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST |
| 567 | && type1->tt_member != type2->tt_member) |
| 568 | (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any; |
| 569 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 570 | return isn == NULL ? FAIL : OK; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Get the type to use for an instruction for an operation on "type1" and |
| 575 | * "type2". If they are matching use a type-specific instruction. Otherwise |
| 576 | * fall back to runtime type checking. |
| 577 | */ |
| 578 | static vartype_T |
| 579 | operator_type(type_T *type1, type_T *type2) |
| 580 | { |
| 581 | if (type1->tt_type == type2->tt_type |
| 582 | && (type1->tt_type == VAR_NUMBER |
| 583 | || type1->tt_type == VAR_LIST |
| 584 | #ifdef FEAT_FLOAT |
| 585 | || type1->tt_type == VAR_FLOAT |
| 586 | #endif |
| 587 | || type1->tt_type == VAR_BLOB)) |
| 588 | return type1->tt_type; |
| 589 | return VAR_ANY; |
| 590 | } |
| 591 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 592 | /* |
| 593 | * Generate an instruction with two arguments. The instruction depends on the |
| 594 | * type of the arguments. |
| 595 | */ |
| 596 | static int |
| 597 | generate_two_op(cctx_T *cctx, char_u *op) |
| 598 | { |
| 599 | garray_T *stack = &cctx->ctx_type_stack; |
| 600 | type_T *type1; |
| 601 | type_T *type2; |
| 602 | vartype_T vartype; |
| 603 | isn_T *isn; |
| 604 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 605 | RETURN_OK_IF_SKIP(cctx); |
| 606 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 607 | // Get the known type of the two items on the stack. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 608 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 609 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 610 | vartype = operator_type(type1, type2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 611 | |
| 612 | switch (*op) |
| 613 | { |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 614 | case '+': |
| 615 | if (generate_add_instr(cctx, vartype, type1, type2) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 616 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 617 | break; |
| 618 | |
| 619 | case '-': |
| 620 | case '*': |
| 621 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 622 | op) == FAIL) |
| 623 | return FAIL; |
| 624 | if (vartype == VAR_NUMBER) |
| 625 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 626 | #ifdef FEAT_FLOAT |
| 627 | else if (vartype == VAR_FLOAT) |
| 628 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 629 | #endif |
| 630 | else |
| 631 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 632 | if (isn != NULL) |
| 633 | isn->isn_arg.op.op_type = *op == '*' |
| 634 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 635 | break; |
| 636 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 637 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 638 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 639 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 640 | && type2->tt_type != VAR_NUMBER)) |
| 641 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 642 | emsg(_(e_percent_requires_number_arguments)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 643 | return FAIL; |
| 644 | } |
| 645 | isn = generate_instr_drop(cctx, |
| 646 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 647 | if (isn != NULL) |
| 648 | isn->isn_arg.op.op_type = EXPR_REM; |
| 649 | break; |
| 650 | } |
| 651 | |
| 652 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 653 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 654 | { |
| 655 | type_T *type = &t_any; |
| 656 | |
| 657 | #ifdef FEAT_FLOAT |
| 658 | // float+number and number+float results in float |
| 659 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 660 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 661 | type = &t_float; |
| 662 | #endif |
| 663 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 664 | } |
| 665 | |
| 666 | return OK; |
| 667 | } |
| 668 | |
| 669 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 670 | * Get the instruction to use for comparing "type1" with "type2" |
| 671 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 672 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 673 | static isntype_T |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 674 | get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 675 | { |
| 676 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 677 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 678 | if (type1 == VAR_UNKNOWN) |
| 679 | type1 = VAR_ANY; |
| 680 | if (type2 == VAR_UNKNOWN) |
| 681 | type2 = VAR_ANY; |
| 682 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 683 | if (type1 == type2) |
| 684 | { |
| 685 | switch (type1) |
| 686 | { |
| 687 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 688 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 689 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 690 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 691 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 692 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 693 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 694 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 695 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 696 | default: isntype = ISN_COMPAREANY; break; |
| 697 | } |
| 698 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 699 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 700 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 701 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 702 | isntype = ISN_COMPAREANY; |
| 703 | |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 704 | if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 705 | && (isntype == ISN_COMPAREBOOL |
| 706 | || isntype == ISN_COMPARESPECIAL |
| 707 | || isntype == ISN_COMPARENR |
| 708 | || isntype == ISN_COMPAREFLOAT)) |
| 709 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 710 | semsg(_(e_cannot_use_str_with_str), |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 711 | exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 712 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 713 | } |
| 714 | if (isntype == ISN_DROP |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 715 | || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 716 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 717 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 718 | || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL |
| 719 | && exprtype != EXPR_IS && exprtype != EXPR_ISNOT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 720 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 721 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 722 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 723 | semsg(_(e_cannot_compare_str_with_str), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 724 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 725 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 726 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 727 | return isntype; |
| 728 | } |
| 729 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 730 | int |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 731 | check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2) |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 732 | { |
| 733 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 734 | return FAIL; |
| 735 | return OK; |
| 736 | } |
| 737 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 738 | /* |
| 739 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 740 | */ |
| 741 | static int |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 742 | generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 743 | { |
| 744 | isntype_T isntype; |
| 745 | isn_T *isn; |
| 746 | garray_T *stack = &cctx->ctx_type_stack; |
| 747 | vartype_T type1; |
| 748 | vartype_T type2; |
| 749 | |
| 750 | RETURN_OK_IF_SKIP(cctx); |
| 751 | |
| 752 | // Get the known type of the two items on the stack. If they are matching |
| 753 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 754 | // checking. |
| 755 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 756 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 757 | isntype = get_compare_isn(exprtype, type1, type2); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 758 | if (isntype == ISN_DROP) |
| 759 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 760 | |
| 761 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 762 | return FAIL; |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 763 | isn->isn_arg.op.op_type = exprtype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 764 | isn->isn_arg.op.op_ic = ic; |
| 765 | |
| 766 | // takes two arguments, puts one bool back |
| 767 | if (stack->ga_len >= 2) |
| 768 | { |
| 769 | --stack->ga_len; |
| 770 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 771 | } |
| 772 | |
| 773 | return OK; |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * Generate an ISN_2BOOL instruction. |
| 778 | */ |
| 779 | static int |
| 780 | generate_2BOOL(cctx_T *cctx, int invert) |
| 781 | { |
| 782 | isn_T *isn; |
| 783 | garray_T *stack = &cctx->ctx_type_stack; |
| 784 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 785 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 786 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 787 | return FAIL; |
| 788 | isn->isn_arg.number = invert; |
| 789 | |
| 790 | // type becomes bool |
| 791 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 792 | |
| 793 | return OK; |
| 794 | } |
| 795 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 796 | /* |
| 797 | * Generate an ISN_COND2BOOL instruction. |
| 798 | */ |
| 799 | static int |
| 800 | generate_COND2BOOL(cctx_T *cctx) |
| 801 | { |
| 802 | isn_T *isn; |
| 803 | garray_T *stack = &cctx->ctx_type_stack; |
| 804 | |
| 805 | RETURN_OK_IF_SKIP(cctx); |
| 806 | if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL) |
| 807 | return FAIL; |
| 808 | |
| 809 | // type becomes bool |
| 810 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 811 | |
| 812 | return OK; |
| 813 | } |
| 814 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 815 | static int |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 816 | generate_TYPECHECK( |
| 817 | cctx_T *cctx, |
| 818 | type_T *expected, |
| 819 | int offset) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 820 | { |
| 821 | isn_T *isn; |
| 822 | garray_T *stack = &cctx->ctx_type_stack; |
| 823 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 824 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 825 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 826 | return FAIL; |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 827 | isn->isn_arg.type.ct_type = alloc_type(expected); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 828 | isn->isn_arg.type.ct_off = offset; |
| 829 | |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 830 | // type becomes expected |
| 831 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 832 | |
| 833 | return OK; |
| 834 | } |
| 835 | |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 836 | static int |
| 837 | generate_SETTYPE( |
| 838 | cctx_T *cctx, |
| 839 | type_T *expected) |
| 840 | { |
| 841 | isn_T *isn; |
| 842 | |
| 843 | RETURN_OK_IF_SKIP(cctx); |
| 844 | if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL) |
| 845 | return FAIL; |
| 846 | isn->isn_arg.type.ct_type = alloc_type(expected); |
| 847 | return OK; |
| 848 | } |
| 849 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 850 | /* |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 851 | * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be |
| 852 | * used. Return FALSE if the types will never match. |
| 853 | */ |
Bram Moolenaar | 193f620 | 2020-11-16 20:08:35 +0100 | [diff] [blame] | 854 | int |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 855 | use_typecheck(type_T *actual, type_T *expected) |
| 856 | { |
| 857 | if (actual->tt_type == VAR_ANY |
| 858 | || actual->tt_type == VAR_UNKNOWN |
| 859 | || (actual->tt_type == VAR_FUNC |
| 860 | && (expected->tt_type == VAR_FUNC |
| 861 | || expected->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 328eac2 | 2021-01-07 19:23:08 +0100 | [diff] [blame] | 862 | && (actual->tt_member == &t_any || actual->tt_argcount < 0) |
| 863 | && ((actual->tt_member == &t_void) |
| 864 | == (expected->tt_member == &t_void)))) |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 865 | return TRUE; |
| 866 | if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT) |
| 867 | && actual->tt_type == expected->tt_type) |
| 868 | // This takes care of a nested list or dict. |
| 869 | return use_typecheck(actual->tt_member, expected->tt_member); |
| 870 | return FALSE; |
| 871 | } |
| 872 | |
| 873 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 874 | * Check that |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 875 | * - "actual" matches "expected" type or |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 876 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 877 | * - return FAIL. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 878 | * If "actual_is_const" is TRUE then the type won't change at runtime, do not |
| 879 | * generate a TYPECHECK. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 880 | */ |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 881 | int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 882 | need_type( |
| 883 | type_T *actual, |
| 884 | type_T *expected, |
| 885 | int offset, |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 886 | int arg_idx, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 887 | cctx_T *cctx, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 888 | int silent, |
| 889 | int actual_is_const) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 890 | { |
Bram Moolenaar | 4ed124c | 2020-09-09 20:03:46 +0200 | [diff] [blame] | 891 | if (expected == &t_bool && actual != &t_bool |
| 892 | && (actual->tt_flags & TTFLAG_BOOL_OK)) |
| 893 | { |
| 894 | // Using "0", "1" or the result of an expression with "&&" or "||" as a |
| 895 | // boolean is OK but requires a conversion. |
| 896 | generate_2BOOL(cctx, FALSE); |
| 897 | return OK; |
| 898 | } |
| 899 | |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 900 | if (check_type(expected, actual, FALSE, arg_idx) == OK) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 901 | return OK; |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 902 | |
| 903 | // If the actual type can be the expected type add a runtime check. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 904 | // If it's a constant a runtime check makes no sense. |
| 905 | if (!actual_is_const && use_typecheck(actual, expected)) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 906 | { |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 907 | generate_TYPECHECK(cctx, expected, offset); |
| 908 | return OK; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 909 | } |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 910 | |
| 911 | if (!silent) |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 912 | arg_type_mismatch(expected, actual, arg_idx); |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 913 | return FAIL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /* |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 917 | * Check that the top of the type stack has a type that can be used as a |
| 918 | * condition. Give an error and return FAIL if not. |
| 919 | */ |
| 920 | static int |
| 921 | bool_on_stack(cctx_T *cctx) |
| 922 | { |
| 923 | garray_T *stack = &cctx->ctx_type_stack; |
| 924 | type_T *type; |
| 925 | |
| 926 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 927 | if (type == &t_bool) |
| 928 | return OK; |
| 929 | |
| 930 | if (type == &t_any || type == &t_number) |
| 931 | // Number 0 and 1 are OK to use as a bool. "any" could also be a bool. |
| 932 | // This requires a runtime type check. |
| 933 | return generate_COND2BOOL(cctx); |
| 934 | |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 935 | return need_type(type, &t_bool, -1, 0, cctx, FALSE, FALSE); |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 939 | * Generate an ISN_PUSHNR instruction. |
| 940 | */ |
| 941 | static int |
| 942 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 943 | { |
| 944 | isn_T *isn; |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 945 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 946 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 947 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 948 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 949 | return FAIL; |
| 950 | isn->isn_arg.number = number; |
| 951 | |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 952 | if (number == 0 || number == 1) |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 953 | // A 0 or 1 number can also be used as a bool. |
Bram Moolenaar | 3868f59 | 2020-12-25 13:20:41 +0100 | [diff] [blame] | 954 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 955 | return OK; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Generate an ISN_PUSHBOOL instruction. |
| 960 | */ |
| 961 | static int |
| 962 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 963 | { |
| 964 | isn_T *isn; |
| 965 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 966 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 967 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 968 | return FAIL; |
| 969 | isn->isn_arg.number = number; |
| 970 | |
| 971 | return OK; |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * Generate an ISN_PUSHSPEC instruction. |
| 976 | */ |
| 977 | static int |
| 978 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 979 | { |
| 980 | isn_T *isn; |
| 981 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 982 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 983 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 984 | return FAIL; |
| 985 | isn->isn_arg.number = number; |
| 986 | |
| 987 | return OK; |
| 988 | } |
| 989 | |
| 990 | #ifdef FEAT_FLOAT |
| 991 | /* |
| 992 | * Generate an ISN_PUSHF instruction. |
| 993 | */ |
| 994 | static int |
| 995 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 996 | { |
| 997 | isn_T *isn; |
| 998 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 999 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1000 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 1001 | return FAIL; |
| 1002 | isn->isn_arg.fnumber = fnumber; |
| 1003 | |
| 1004 | return OK; |
| 1005 | } |
| 1006 | #endif |
| 1007 | |
| 1008 | /* |
| 1009 | * Generate an ISN_PUSHS instruction. |
| 1010 | * Consumes "str". |
| 1011 | */ |
| 1012 | static int |
| 1013 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 1014 | { |
| 1015 | isn_T *isn; |
| 1016 | |
Bram Moolenaar | 508b561 | 2021-01-02 18:17:26 +0100 | [diff] [blame] | 1017 | if (cctx->ctx_skip == SKIP_YES) |
| 1018 | { |
| 1019 | vim_free(str); |
| 1020 | return OK; |
| 1021 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1022 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 1023 | return FAIL; |
| 1024 | isn->isn_arg.string = str; |
| 1025 | |
| 1026 | return OK; |
| 1027 | } |
| 1028 | |
| 1029 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1030 | * Generate an ISN_PUSHCHANNEL instruction. |
| 1031 | * Consumes "channel". |
| 1032 | */ |
| 1033 | static int |
| 1034 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 1035 | { |
| 1036 | isn_T *isn; |
| 1037 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1038 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1039 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 1040 | return FAIL; |
| 1041 | isn->isn_arg.channel = channel; |
| 1042 | |
| 1043 | return OK; |
| 1044 | } |
| 1045 | |
| 1046 | /* |
| 1047 | * Generate an ISN_PUSHJOB instruction. |
| 1048 | * Consumes "job". |
| 1049 | */ |
| 1050 | static int |
| 1051 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 1052 | { |
| 1053 | isn_T *isn; |
| 1054 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1055 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1056 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1057 | return FAIL; |
| 1058 | isn->isn_arg.job = job; |
| 1059 | |
| 1060 | return OK; |
| 1061 | } |
| 1062 | |
| 1063 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1064 | * Generate an ISN_PUSHBLOB instruction. |
| 1065 | * Consumes "blob". |
| 1066 | */ |
| 1067 | static int |
| 1068 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1069 | { |
| 1070 | isn_T *isn; |
| 1071 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1072 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1073 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1074 | return FAIL; |
| 1075 | isn->isn_arg.blob = blob; |
| 1076 | |
| 1077 | return OK; |
| 1078 | } |
| 1079 | |
| 1080 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1081 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1082 | * Consumes "name". |
| 1083 | */ |
| 1084 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1085 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1086 | { |
| 1087 | isn_T *isn; |
| 1088 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1089 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1090 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1091 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 1092 | isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1093 | |
| 1094 | return OK; |
| 1095 | } |
| 1096 | |
| 1097 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1098 | * Generate an ISN_GETITEM instruction with "index". |
| 1099 | */ |
| 1100 | static int |
| 1101 | generate_GETITEM(cctx_T *cctx, int index) |
| 1102 | { |
| 1103 | isn_T *isn; |
| 1104 | garray_T *stack = &cctx->ctx_type_stack; |
| 1105 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1106 | type_T *item_type = &t_any; |
| 1107 | |
| 1108 | RETURN_OK_IF_SKIP(cctx); |
| 1109 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1110 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1111 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1112 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1113 | emsg(_(e_listreq)); |
| 1114 | return FAIL; |
| 1115 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1116 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1117 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1118 | return FAIL; |
| 1119 | isn->isn_arg.number = index; |
| 1120 | |
| 1121 | // add the item type to the type stack |
| 1122 | if (ga_grow(stack, 1) == FAIL) |
| 1123 | return FAIL; |
| 1124 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 1125 | ++stack->ga_len; |
| 1126 | return OK; |
| 1127 | } |
| 1128 | |
| 1129 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 1130 | * Generate an ISN_SLICE instruction with "count". |
| 1131 | */ |
| 1132 | static int |
| 1133 | generate_SLICE(cctx_T *cctx, int count) |
| 1134 | { |
| 1135 | isn_T *isn; |
| 1136 | |
| 1137 | RETURN_OK_IF_SKIP(cctx); |
| 1138 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1139 | return FAIL; |
| 1140 | isn->isn_arg.number = count; |
| 1141 | return OK; |
| 1142 | } |
| 1143 | |
| 1144 | /* |
| 1145 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1146 | */ |
| 1147 | static int |
| 1148 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1149 | { |
| 1150 | isn_T *isn; |
| 1151 | |
| 1152 | RETURN_OK_IF_SKIP(cctx); |
| 1153 | |
| 1154 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1155 | return FAIL; |
| 1156 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1157 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1158 | |
| 1159 | return OK; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1163 | * Generate an ISN_STORE instruction. |
| 1164 | */ |
| 1165 | static int |
| 1166 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1167 | { |
| 1168 | isn_T *isn; |
| 1169 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1170 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1171 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1172 | return FAIL; |
| 1173 | if (name != NULL) |
| 1174 | isn->isn_arg.string = vim_strsave(name); |
| 1175 | else |
| 1176 | isn->isn_arg.number = idx; |
| 1177 | |
| 1178 | return OK; |
| 1179 | } |
| 1180 | |
| 1181 | /* |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 1182 | * Generate an ISN_STOREOUTER instruction. |
| 1183 | */ |
| 1184 | static int |
| 1185 | generate_STOREOUTER(cctx_T *cctx, int idx, int level) |
| 1186 | { |
| 1187 | isn_T *isn; |
| 1188 | |
| 1189 | RETURN_OK_IF_SKIP(cctx); |
| 1190 | if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL) |
| 1191 | return FAIL; |
| 1192 | isn->isn_arg.outer.outer_idx = idx; |
| 1193 | isn->isn_arg.outer.outer_depth = level; |
| 1194 | |
| 1195 | return OK; |
| 1196 | } |
| 1197 | |
| 1198 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1199 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1200 | */ |
| 1201 | static int |
| 1202 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1203 | { |
| 1204 | isn_T *isn; |
| 1205 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1206 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1207 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1208 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1209 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1210 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1211 | |
| 1212 | return OK; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * Generate an ISN_STOREOPT instruction |
| 1217 | */ |
| 1218 | static int |
| 1219 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1220 | { |
| 1221 | isn_T *isn; |
| 1222 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1223 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1c199f9 | 2020-08-07 21:28:34 +0200 | [diff] [blame] | 1224 | if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1225 | return FAIL; |
| 1226 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1227 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1228 | |
| 1229 | return OK; |
| 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * Generate an ISN_LOAD or similar instruction. |
| 1234 | */ |
| 1235 | static int |
| 1236 | generate_LOAD( |
| 1237 | cctx_T *cctx, |
| 1238 | isntype_T isn_type, |
| 1239 | int idx, |
| 1240 | char_u *name, |
| 1241 | type_T *type) |
| 1242 | { |
| 1243 | isn_T *isn; |
| 1244 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1245 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1246 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1247 | return FAIL; |
| 1248 | if (name != NULL) |
| 1249 | isn->isn_arg.string = vim_strsave(name); |
| 1250 | else |
| 1251 | isn->isn_arg.number = idx; |
| 1252 | |
| 1253 | return OK; |
| 1254 | } |
| 1255 | |
| 1256 | /* |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 1257 | * Generate an ISN_LOADOUTER instruction |
| 1258 | */ |
| 1259 | static int |
| 1260 | generate_LOADOUTER( |
| 1261 | cctx_T *cctx, |
| 1262 | int idx, |
| 1263 | int nesting, |
| 1264 | type_T *type) |
| 1265 | { |
| 1266 | isn_T *isn; |
| 1267 | |
| 1268 | RETURN_OK_IF_SKIP(cctx); |
| 1269 | if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL) |
| 1270 | return FAIL; |
| 1271 | isn->isn_arg.outer.outer_idx = idx; |
| 1272 | isn->isn_arg.outer.outer_depth = nesting; |
| 1273 | |
| 1274 | return OK; |
| 1275 | } |
| 1276 | |
| 1277 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1278 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1279 | */ |
| 1280 | static int |
| 1281 | generate_LOADV( |
| 1282 | cctx_T *cctx, |
| 1283 | char_u *name, |
| 1284 | int error) |
| 1285 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1286 | int di_flags; |
| 1287 | int vidx = find_vim_var(name, &di_flags); |
| 1288 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1289 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1290 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1291 | if (vidx < 0) |
| 1292 | { |
| 1293 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1294 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1295 | return FAIL; |
| 1296 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1297 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1298 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1299 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1303 | * Generate an ISN_UNLET instruction. |
| 1304 | */ |
| 1305 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1306 | 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] | 1307 | { |
| 1308 | isn_T *isn; |
| 1309 | |
| 1310 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1311 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1312 | return FAIL; |
| 1313 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1314 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1315 | |
| 1316 | return OK; |
| 1317 | } |
| 1318 | |
| 1319 | /* |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 1320 | * Generate an ISN_LOCKCONST instruction. |
| 1321 | */ |
| 1322 | static int |
| 1323 | generate_LOCKCONST(cctx_T *cctx) |
| 1324 | { |
| 1325 | isn_T *isn; |
| 1326 | |
| 1327 | RETURN_OK_IF_SKIP(cctx); |
| 1328 | if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL) |
| 1329 | return FAIL; |
| 1330 | return OK; |
| 1331 | } |
| 1332 | |
| 1333 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1334 | * Generate an ISN_LOADS instruction. |
| 1335 | */ |
| 1336 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1337 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1338 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1339 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1340 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1341 | int sid, |
| 1342 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1343 | { |
| 1344 | isn_T *isn; |
| 1345 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1346 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1347 | if (isn_type == ISN_LOADS) |
| 1348 | isn = generate_instr_type(cctx, isn_type, type); |
| 1349 | else |
| 1350 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1351 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1352 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1353 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1354 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1355 | |
| 1356 | return OK; |
| 1357 | } |
| 1358 | |
| 1359 | /* |
| 1360 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1361 | */ |
| 1362 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1363 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1364 | cctx_T *cctx, |
| 1365 | isntype_T isn_type, |
| 1366 | int sid, |
| 1367 | int idx, |
| 1368 | type_T *type) |
| 1369 | { |
| 1370 | isn_T *isn; |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 1371 | scriptref_T *sref; |
| 1372 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1373 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1374 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1375 | if (isn_type == ISN_LOADSCRIPT) |
| 1376 | isn = generate_instr_type(cctx, isn_type, type); |
| 1377 | else |
| 1378 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1379 | if (isn == NULL) |
| 1380 | return FAIL; |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 1381 | |
| 1382 | // This requires three arguments, which doesn't fit in an instruction, thus |
| 1383 | // we need to allocate a struct for this. |
| 1384 | sref = ALLOC_ONE(scriptref_T); |
| 1385 | if (sref == NULL) |
| 1386 | return FAIL; |
| 1387 | isn->isn_arg.script.scriptref = sref; |
| 1388 | sref->sref_sid = sid; |
| 1389 | sref->sref_idx = idx; |
| 1390 | sref->sref_seq = si->sn_script_seq; |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1391 | sref->sref_type = type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1392 | return OK; |
| 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | * Generate an ISN_NEWLIST instruction. |
| 1397 | */ |
| 1398 | static int |
| 1399 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1400 | { |
| 1401 | isn_T *isn; |
| 1402 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1403 | type_T *type; |
| 1404 | type_T *member; |
| 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_NEWLIST)) == NULL) |
| 1408 | return FAIL; |
| 1409 | isn->isn_arg.number = count; |
| 1410 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1411 | // get the member type from all the items on the stack. |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1412 | if (count == 0) |
| 1413 | member = &t_void; |
| 1414 | else |
| 1415 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1416 | ((type_T **)stack->ga_data) + stack->ga_len, count, 1, |
| 1417 | cctx->ctx_type_list); |
| 1418 | type = get_list_type(member, cctx->ctx_type_list); |
| 1419 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1420 | // drop the value types |
| 1421 | stack->ga_len -= count; |
| 1422 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1423 | // add the list type to the type stack |
| 1424 | if (ga_grow(stack, 1) == FAIL) |
| 1425 | return FAIL; |
| 1426 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1427 | ++stack->ga_len; |
| 1428 | |
| 1429 | return OK; |
| 1430 | } |
| 1431 | |
| 1432 | /* |
| 1433 | * Generate an ISN_NEWDICT instruction. |
| 1434 | */ |
| 1435 | static int |
| 1436 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1437 | { |
| 1438 | isn_T *isn; |
| 1439 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1440 | type_T *type; |
| 1441 | type_T *member; |
| 1442 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1443 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1444 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1445 | return FAIL; |
| 1446 | isn->isn_arg.number = count; |
| 1447 | |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1448 | if (count == 0) |
| 1449 | member = &t_void; |
| 1450 | else |
| 1451 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1452 | ((type_T **)stack->ga_data) + stack->ga_len, count, 2, |
| 1453 | cctx->ctx_type_list); |
| 1454 | type = get_dict_type(member, cctx->ctx_type_list); |
| 1455 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1456 | // drop the key and value types |
| 1457 | stack->ga_len -= 2 * count; |
| 1458 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1459 | // add the dict type to the type stack |
| 1460 | if (ga_grow(stack, 1) == FAIL) |
| 1461 | return FAIL; |
| 1462 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1463 | ++stack->ga_len; |
| 1464 | |
| 1465 | return OK; |
| 1466 | } |
| 1467 | |
| 1468 | /* |
| 1469 | * Generate an ISN_FUNCREF instruction. |
| 1470 | */ |
| 1471 | static int |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1472 | generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1473 | { |
| 1474 | isn_T *isn; |
| 1475 | garray_T *stack = &cctx->ctx_type_stack; |
| 1476 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1477 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1478 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1479 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1480 | isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 1481 | cctx->ctx_has_closure = 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1482 | |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 1483 | // if the referenced function is a closure, it may use items further up in |
| 1484 | // the nested context, including this one. |
| 1485 | if (ufunc->uf_flags & FC_CLOSURE) |
| 1486 | cctx->ctx_ufunc->uf_flags |= FC_CLOSURE; |
| 1487 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1488 | if (ga_grow(stack, 1) == FAIL) |
| 1489 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1490 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 1491 | ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1492 | ++stack->ga_len; |
| 1493 | |
| 1494 | return OK; |
| 1495 | } |
| 1496 | |
| 1497 | /* |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1498 | * Generate an ISN_NEWFUNC instruction. |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 1499 | * "lambda_name" and "func_name" must be in allocated memory and will be |
| 1500 | * consumed. |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1501 | */ |
| 1502 | static int |
| 1503 | generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) |
| 1504 | { |
| 1505 | isn_T *isn; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1506 | |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 1507 | if (cctx->ctx_skip == SKIP_YES) |
| 1508 | { |
| 1509 | vim_free(lambda_name); |
| 1510 | vim_free(func_name); |
| 1511 | return OK; |
| 1512 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1513 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 1514 | { |
| 1515 | vim_free(lambda_name); |
| 1516 | vim_free(func_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1517 | return FAIL; |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 1518 | } |
| 1519 | isn->isn_arg.newfunc.nf_lambda = lambda_name; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1520 | isn->isn_arg.newfunc.nf_global = func_name; |
| 1521 | |
| 1522 | return OK; |
| 1523 | } |
| 1524 | |
| 1525 | /* |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 1526 | * Generate an ISN_DEF instruction: list functions |
| 1527 | */ |
| 1528 | static int |
| 1529 | generate_DEF(cctx_T *cctx, char_u *name, size_t len) |
| 1530 | { |
| 1531 | isn_T *isn; |
| 1532 | |
| 1533 | RETURN_OK_IF_SKIP(cctx); |
| 1534 | if ((isn = generate_instr(cctx, ISN_DEF)) == NULL) |
| 1535 | return FAIL; |
| 1536 | if (len > 0) |
| 1537 | { |
| 1538 | isn->isn_arg.string = vim_strnsave(name, len); |
| 1539 | if (isn->isn_arg.string == NULL) |
| 1540 | return FAIL; |
| 1541 | } |
| 1542 | return OK; |
| 1543 | } |
| 1544 | |
| 1545 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1546 | * Generate an ISN_JUMP instruction. |
| 1547 | */ |
| 1548 | static int |
| 1549 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1550 | { |
| 1551 | isn_T *isn; |
| 1552 | garray_T *stack = &cctx->ctx_type_stack; |
| 1553 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1554 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1555 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1556 | return FAIL; |
| 1557 | isn->isn_arg.jump.jump_when = when; |
| 1558 | isn->isn_arg.jump.jump_where = where; |
| 1559 | |
| 1560 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1561 | --stack->ga_len; |
| 1562 | |
| 1563 | return OK; |
| 1564 | } |
| 1565 | |
| 1566 | static int |
| 1567 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1568 | { |
| 1569 | isn_T *isn; |
| 1570 | garray_T *stack = &cctx->ctx_type_stack; |
| 1571 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1572 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1573 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1574 | return FAIL; |
| 1575 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1576 | |
| 1577 | if (ga_grow(stack, 1) == FAIL) |
| 1578 | return FAIL; |
| 1579 | // type doesn't matter, will be stored next |
| 1580 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1581 | ++stack->ga_len; |
| 1582 | |
| 1583 | return OK; |
| 1584 | } |
| 1585 | |
| 1586 | /* |
| 1587 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1588 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1589 | * Return FAIL if the number of arguments is wrong. |
| 1590 | */ |
| 1591 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1592 | generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1593 | { |
| 1594 | isn_T *isn; |
| 1595 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1596 | int argoff; |
Bram Moolenaar | a1224cb | 2020-10-22 12:31:49 +0200 | [diff] [blame] | 1597 | type_T **argtypes = NULL; |
Bram Moolenaar | 75ab91f | 2021-01-10 22:42:50 +0100 | [diff] [blame] | 1598 | type_T *maptype = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1599 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1600 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1601 | argoff = check_internal_func(func_idx, argcount); |
| 1602 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1603 | return FAIL; |
| 1604 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1605 | if (method_call && argoff > 1) |
| 1606 | { |
| 1607 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1608 | return FAIL; |
| 1609 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1610 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1611 | } |
| 1612 | |
Bram Moolenaar | af7a906 | 2020-10-21 16:49:17 +0200 | [diff] [blame] | 1613 | if (argcount > 0) |
| 1614 | { |
| 1615 | // Check the types of the arguments. |
| 1616 | argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount; |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 1617 | if (internal_func_check_arg_types(argtypes, func_idx, argcount, |
| 1618 | cctx) == FAIL) |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 1619 | return FAIL; |
Bram Moolenaar | 75ab91f | 2021-01-10 22:42:50 +0100 | [diff] [blame] | 1620 | if (internal_func_is_map(func_idx)) |
| 1621 | maptype = *argtypes; |
Bram Moolenaar | af7a906 | 2020-10-21 16:49:17 +0200 | [diff] [blame] | 1622 | } |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 1623 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1624 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1625 | return FAIL; |
| 1626 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1627 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1628 | |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 1629 | // Drop the argument types and push the return type. |
| 1630 | stack->ga_len -= argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1631 | if (ga_grow(stack, 1) == FAIL) |
| 1632 | return FAIL; |
| 1633 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1634 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 1635 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1636 | |
Bram Moolenaar | 75ab91f | 2021-01-10 22:42:50 +0100 | [diff] [blame] | 1637 | if (maptype != NULL && maptype->tt_member != NULL |
| 1638 | && maptype->tt_member != &t_any) |
| 1639 | // Check that map() didn't change the item types. |
| 1640 | generate_TYPECHECK(cctx, maptype, -1); |
| 1641 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1642 | return OK; |
| 1643 | } |
| 1644 | |
| 1645 | /* |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 1646 | * Generate an ISN_LISTAPPEND instruction. Works like add(). |
| 1647 | * Argument count is already checked. |
| 1648 | */ |
| 1649 | static int |
| 1650 | generate_LISTAPPEND(cctx_T *cctx) |
| 1651 | { |
| 1652 | garray_T *stack = &cctx->ctx_type_stack; |
| 1653 | type_T *list_type; |
| 1654 | type_T *item_type; |
| 1655 | type_T *expected; |
| 1656 | |
| 1657 | // Caller already checked that list_type is a list. |
| 1658 | list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 1659 | item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1660 | expected = list_type->tt_member; |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 1661 | if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 1662 | return FAIL; |
| 1663 | |
| 1664 | if (generate_instr(cctx, ISN_LISTAPPEND) == NULL) |
| 1665 | return FAIL; |
| 1666 | |
| 1667 | --stack->ga_len; // drop the argument |
| 1668 | return OK; |
| 1669 | } |
| 1670 | |
| 1671 | /* |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 1672 | * Generate an ISN_BLOBAPPEND instruction. Works like add(). |
| 1673 | * Argument count is already checked. |
| 1674 | */ |
| 1675 | static int |
| 1676 | generate_BLOBAPPEND(cctx_T *cctx) |
| 1677 | { |
| 1678 | garray_T *stack = &cctx->ctx_type_stack; |
| 1679 | type_T *item_type; |
| 1680 | |
| 1681 | // Caller already checked that blob_type is a blob. |
| 1682 | item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 1683 | if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 1684 | return FAIL; |
| 1685 | |
| 1686 | if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL) |
| 1687 | return FAIL; |
| 1688 | |
| 1689 | --stack->ga_len; // drop the argument |
| 1690 | return OK; |
| 1691 | } |
| 1692 | |
| 1693 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1694 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1695 | * Return FAIL if the number of arguments is wrong. |
| 1696 | */ |
| 1697 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1698 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1699 | { |
| 1700 | isn_T *isn; |
| 1701 | garray_T *stack = &cctx->ctx_type_stack; |
| 1702 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1703 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1704 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1705 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1706 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1707 | { |
Bram Moolenaar | b185a40 | 2020-09-18 22:42:00 +0200 | [diff] [blame] | 1708 | semsg(_(e_toomanyarg), printable_func_name(ufunc)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1709 | return FAIL; |
| 1710 | } |
| 1711 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1712 | { |
Bram Moolenaar | b185a40 | 2020-09-18 22:42:00 +0200 | [diff] [blame] | 1713 | semsg(_(e_toofewarg), printable_func_name(ufunc)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1714 | return FAIL; |
| 1715 | } |
| 1716 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1717 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1718 | { |
| 1719 | int i; |
| 1720 | |
| 1721 | for (i = 0; i < argcount; ++i) |
| 1722 | { |
| 1723 | type_T *expected; |
| 1724 | type_T *actual; |
| 1725 | |
| 1726 | if (i < regular_args) |
| 1727 | { |
| 1728 | if (ufunc->uf_arg_types == NULL) |
| 1729 | continue; |
| 1730 | expected = ufunc->uf_arg_types[i]; |
| 1731 | } |
Bram Moolenaar | 2f8cbc4 | 2020-09-16 17:22:59 +0200 | [diff] [blame] | 1732 | else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any) |
| 1733 | // possibly a lambda or "...: any" |
Bram Moolenaar | 79e8db9 | 2020-08-14 22:16:33 +0200 | [diff] [blame] | 1734 | expected = &t_any; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1735 | else |
| 1736 | expected = ufunc->uf_va_type->tt_member; |
| 1737 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 1738 | if (need_type(actual, expected, -argcount + i, 0, cctx, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 1739 | TRUE, FALSE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1740 | { |
| 1741 | arg_type_mismatch(expected, actual, i + 1); |
| 1742 | return FAIL; |
| 1743 | } |
| 1744 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1745 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1746 | if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL) |
| 1747 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1748 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1749 | } |
| 1750 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1751 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1752 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1753 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1754 | return FAIL; |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 1755 | if (isn->isn_type == ISN_DCALL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1756 | { |
| 1757 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1758 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1759 | } |
| 1760 | else |
| 1761 | { |
| 1762 | // A user function may be deleted and redefined later, can't use the |
| 1763 | // ufunc pointer, need to look it up again at runtime. |
| 1764 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1765 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1766 | } |
| 1767 | |
| 1768 | stack->ga_len -= argcount; // drop the arguments |
| 1769 | if (ga_grow(stack, 1) == FAIL) |
| 1770 | return FAIL; |
| 1771 | // add return value |
| 1772 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1773 | ++stack->ga_len; |
| 1774 | |
| 1775 | return OK; |
| 1776 | } |
| 1777 | |
| 1778 | /* |
| 1779 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1780 | */ |
| 1781 | static int |
| 1782 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1783 | { |
| 1784 | isn_T *isn; |
| 1785 | garray_T *stack = &cctx->ctx_type_stack; |
| 1786 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1787 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1788 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1789 | return FAIL; |
| 1790 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1791 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1792 | |
| 1793 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1794 | if (ga_grow(stack, 1) == FAIL) |
| 1795 | return FAIL; |
| 1796 | // add return value |
| 1797 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1798 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1799 | |
| 1800 | return OK; |
| 1801 | } |
| 1802 | |
| 1803 | /* |
| 1804 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1805 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1806 | */ |
| 1807 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1808 | generate_PCALL( |
| 1809 | cctx_T *cctx, |
| 1810 | int argcount, |
| 1811 | char_u *name, |
| 1812 | type_T *type, |
| 1813 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1814 | { |
| 1815 | isn_T *isn; |
| 1816 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1817 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1818 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1819 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1820 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1821 | if (type->tt_type == VAR_ANY) |
| 1822 | ret_type = &t_any; |
| 1823 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1824 | { |
| 1825 | if (type->tt_argcount != -1) |
| 1826 | { |
| 1827 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1828 | |
| 1829 | if (argcount < type->tt_min_argcount - varargs) |
| 1830 | { |
Bram Moolenaar | 6cf7e3b | 2020-10-28 14:31:16 +0100 | [diff] [blame] | 1831 | semsg(_(e_toofewarg), name); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1832 | return FAIL; |
| 1833 | } |
| 1834 | if (!varargs && argcount > type->tt_argcount) |
| 1835 | { |
Bram Moolenaar | 6cf7e3b | 2020-10-28 14:31:16 +0100 | [diff] [blame] | 1836 | semsg(_(e_toomanyarg), name); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1837 | return FAIL; |
| 1838 | } |
Bram Moolenaar | b4d16cb | 2020-11-05 18:45:46 +0100 | [diff] [blame] | 1839 | if (type->tt_args != NULL) |
| 1840 | { |
| 1841 | int i; |
| 1842 | |
| 1843 | for (i = 0; i < argcount; ++i) |
| 1844 | { |
| 1845 | int offset = -argcount + i - 1; |
| 1846 | type_T *actual = ((type_T **)stack->ga_data)[ |
| 1847 | stack->ga_len + offset]; |
| 1848 | type_T *expected; |
| 1849 | |
Bram Moolenaar | 32b3f82 | 2021-01-06 21:59:39 +0100 | [diff] [blame] | 1850 | if (varargs && i >= type->tt_argcount - 1) |
Bram Moolenaar | b4d16cb | 2020-11-05 18:45:46 +0100 | [diff] [blame] | 1851 | expected = type->tt_args[ |
Bram Moolenaar | 32b3f82 | 2021-01-06 21:59:39 +0100 | [diff] [blame] | 1852 | type->tt_argcount - 1]->tt_member; |
Bram Moolenaar | b4d16cb | 2020-11-05 18:45:46 +0100 | [diff] [blame] | 1853 | else |
| 1854 | expected = type->tt_args[i]; |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 1855 | if (need_type(actual, expected, offset, 0, |
Bram Moolenaar | b4d16cb | 2020-11-05 18:45:46 +0100 | [diff] [blame] | 1856 | cctx, TRUE, FALSE) == FAIL) |
| 1857 | { |
| 1858 | arg_type_mismatch(expected, actual, i + 1); |
| 1859 | return FAIL; |
| 1860 | } |
| 1861 | } |
| 1862 | } |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1863 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1864 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1865 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1866 | else |
| 1867 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1868 | semsg(_(e_not_callable_type_str), name); |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1869 | return FAIL; |
| 1870 | } |
| 1871 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1872 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1873 | return FAIL; |
| 1874 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1875 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1876 | |
| 1877 | stack->ga_len -= argcount; // drop the arguments |
| 1878 | |
| 1879 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1880 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1881 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1882 | // If partial is above the arguments it must be cleared and replaced with |
| 1883 | // the return value. |
| 1884 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1885 | return FAIL; |
| 1886 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1887 | return OK; |
| 1888 | } |
| 1889 | |
| 1890 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1891 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1892 | */ |
| 1893 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1894 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1895 | { |
| 1896 | isn_T *isn; |
| 1897 | garray_T *stack = &cctx->ctx_type_stack; |
| 1898 | type_T *type; |
| 1899 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1900 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1901 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1902 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1903 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1904 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1905 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1906 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1907 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1908 | { |
| 1909 | emsg(_(e_dictreq)); |
| 1910 | return FAIL; |
| 1911 | } |
| 1912 | // change dict type to dict member type |
| 1913 | if (type->tt_type == VAR_DICT) |
Bram Moolenaar | 31a11b9 | 2021-01-10 19:23:27 +0100 | [diff] [blame] | 1914 | { |
| 1915 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = |
| 1916 | type->tt_member == &t_unknown ? &t_any : type->tt_member; |
| 1917 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1918 | |
| 1919 | return OK; |
| 1920 | } |
| 1921 | |
| 1922 | /* |
| 1923 | * Generate an ISN_ECHO instruction. |
| 1924 | */ |
| 1925 | static int |
| 1926 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1927 | { |
| 1928 | isn_T *isn; |
| 1929 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1930 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1931 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1932 | return FAIL; |
| 1933 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1934 | isn->isn_arg.echo.echo_count = count; |
| 1935 | |
| 1936 | return OK; |
| 1937 | } |
| 1938 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1939 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1940 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1941 | */ |
| 1942 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1943 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1944 | { |
| 1945 | isn_T *isn; |
| 1946 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1947 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1948 | return FAIL; |
| 1949 | isn->isn_arg.number = count; |
| 1950 | |
| 1951 | return OK; |
| 1952 | } |
| 1953 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 1954 | /* |
| 1955 | * Generate an ISN_PUT instruction. |
| 1956 | */ |
| 1957 | static int |
| 1958 | generate_PUT(cctx_T *cctx, int regname, linenr_T lnum) |
| 1959 | { |
| 1960 | isn_T *isn; |
| 1961 | |
| 1962 | RETURN_OK_IF_SKIP(cctx); |
| 1963 | if ((isn = generate_instr(cctx, ISN_PUT)) == NULL) |
| 1964 | return FAIL; |
| 1965 | isn->isn_arg.put.put_regname = regname; |
| 1966 | isn->isn_arg.put.put_lnum = lnum; |
| 1967 | return OK; |
| 1968 | } |
| 1969 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1970 | static int |
| 1971 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1972 | { |
| 1973 | isn_T *isn; |
| 1974 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1975 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1976 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1977 | return FAIL; |
| 1978 | isn->isn_arg.string = vim_strsave(line); |
| 1979 | return OK; |
| 1980 | } |
| 1981 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1982 | static int |
| 1983 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1984 | { |
| 1985 | isn_T *isn; |
| 1986 | |
| 1987 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1988 | return FAIL; |
| 1989 | isn->isn_arg.number = count; |
| 1990 | return OK; |
| 1991 | } |
| 1992 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 1993 | /* |
| 1994 | * Generate ISN_RANGE. Consumes "range". Return OK/FAIL. |
| 1995 | */ |
| 1996 | static int |
| 1997 | generate_RANGE(cctx_T *cctx, char_u *range) |
| 1998 | { |
| 1999 | isn_T *isn; |
| 2000 | garray_T *stack = &cctx->ctx_type_stack; |
| 2001 | |
| 2002 | if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL) |
| 2003 | return FAIL; |
| 2004 | isn->isn_arg.string = range; |
| 2005 | |
| 2006 | if (ga_grow(stack, 1) == FAIL) |
| 2007 | return FAIL; |
| 2008 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_number; |
| 2009 | ++stack->ga_len; |
| 2010 | return OK; |
| 2011 | } |
| 2012 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 2013 | static int |
| 2014 | generate_UNPACK(cctx_T *cctx, int var_count, int semicolon) |
| 2015 | { |
| 2016 | isn_T *isn; |
| 2017 | |
| 2018 | RETURN_OK_IF_SKIP(cctx); |
| 2019 | if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL) |
| 2020 | return FAIL; |
| 2021 | isn->isn_arg.unpack.unp_count = var_count; |
| 2022 | isn->isn_arg.unpack.unp_semicolon = semicolon; |
| 2023 | return OK; |
| 2024 | } |
| 2025 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2026 | /* |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2027 | * Generate an instruction for any command modifiers. |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2028 | */ |
| 2029 | static int |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 2030 | generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod) |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2031 | { |
| 2032 | isn_T *isn; |
| 2033 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2034 | if (cmod->cmod_flags != 0 |
| 2035 | || cmod->cmod_split != 0 |
| 2036 | || cmod->cmod_verbose != 0 |
| 2037 | || cmod->cmod_tab != 0 |
| 2038 | || cmod->cmod_filter_regmatch.regprog != NULL) |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2039 | { |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2040 | cctx->ctx_has_cmdmod = TRUE; |
| 2041 | |
| 2042 | if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL) |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2043 | return FAIL; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2044 | isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T); |
| 2045 | if (isn->isn_arg.cmdmod.cf_cmdmod == NULL) |
| 2046 | return FAIL; |
| 2047 | mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T)); |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 2048 | // filter program now belongs to the instruction |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2049 | cmod->cmod_filter_regmatch.regprog = NULL; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2050 | } |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2051 | |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2052 | return OK; |
| 2053 | } |
| 2054 | |
| 2055 | static int |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2056 | generate_undo_cmdmods(cctx_T *cctx) |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2057 | { |
Bram Moolenaar | f665e97 | 2020-12-05 19:17:16 +0100 | [diff] [blame] | 2058 | if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL) |
| 2059 | return FAIL; |
Bram Moolenaar | 7cd2422 | 2021-01-12 18:58:39 +0100 | [diff] [blame] | 2060 | cctx->ctx_has_cmdmod = FALSE; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2061 | return OK; |
| 2062 | } |
| 2063 | |
| 2064 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2065 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2066 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2067 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2068 | static lvar_T * |
Bram Moolenaar | e8211a3 | 2020-10-09 22:04:29 +0200 | [diff] [blame] | 2069 | reserve_local( |
| 2070 | cctx_T *cctx, |
| 2071 | char_u *name, |
| 2072 | size_t len, |
| 2073 | int isConst, |
| 2074 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2075 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2076 | lvar_T *lvar; |
| 2077 | |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2078 | if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2079 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2080 | emsg_namelen(_(e_str_is_used_as_argument), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2081 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2085 | return NULL; |
| 2086 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++; |
Bram Moolenaar | e8211a3 | 2020-10-09 22:04:29 +0200 | [diff] [blame] | 2087 | CLEAR_POINTER(lvar); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2088 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2089 | // Every local variable uses the next entry on the stack. We could re-use |
| 2090 | // the last ones when leaving a scope, but then variables used in a closure |
| 2091 | // might get overwritten. To keep things simple do not re-use stack |
| 2092 | // entries. This is less efficient, but memory is cheap these days. |
| 2093 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 2094 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 2095 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2096 | lvar->lv_const = isConst; |
| 2097 | lvar->lv_type = type; |
| 2098 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2099 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2103 | * Remove local variables above "new_top". |
| 2104 | */ |
| 2105 | static void |
| 2106 | unwind_locals(cctx_T *cctx, int new_top) |
| 2107 | { |
| 2108 | if (cctx->ctx_locals.ga_len > new_top) |
| 2109 | { |
| 2110 | int idx; |
| 2111 | lvar_T *lvar; |
| 2112 | |
| 2113 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 2114 | { |
| 2115 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 2116 | vim_free(lvar->lv_name); |
| 2117 | } |
| 2118 | } |
| 2119 | cctx->ctx_locals.ga_len = new_top; |
| 2120 | } |
| 2121 | |
| 2122 | /* |
| 2123 | * Free all local variables. |
| 2124 | */ |
| 2125 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2126 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2127 | { |
| 2128 | unwind_locals(cctx, 0); |
| 2129 | ga_clear(&cctx->ctx_locals); |
| 2130 | } |
| 2131 | |
| 2132 | /* |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 2133 | * If "check_writable" is ASSIGN_CONST give an error if the variable was |
| 2134 | * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an |
| 2135 | * error if the variable was defined with :const. |
| 2136 | */ |
| 2137 | static int |
| 2138 | check_item_writable(svar_T *sv, int check_writable, char_u *name) |
| 2139 | { |
| 2140 | if ((check_writable == ASSIGN_CONST && sv->sv_const != 0) |
| 2141 | || (check_writable == ASSIGN_FINAL |
| 2142 | && sv->sv_const == ASSIGN_CONST)) |
| 2143 | { |
| 2144 | semsg(_(e_readonlyvar), name); |
| 2145 | return FAIL; |
| 2146 | } |
| 2147 | return OK; |
| 2148 | } |
| 2149 | |
| 2150 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2151 | * Find "name" in script-local items of script "sid". |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 2152 | * Pass "check_writable" to check_item_writable(). |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2153 | * Returns the index in "sn_var_vals" if found. |
| 2154 | * If found but not in "sn_var_vals" returns -1. |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 2155 | * If not found or the variable is not writable returns -2. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2156 | */ |
| 2157 | int |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2158 | get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2159 | { |
| 2160 | hashtab_T *ht; |
| 2161 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2162 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2163 | svar_T *sv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2164 | int idx; |
| 2165 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2166 | if (!SCRIPT_ID_VALID(sid)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2167 | return -1; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2168 | if (sid == current_sctx.sc_sid) |
| 2169 | { |
Bram Moolenaar | 209f020 | 2020-10-15 13:57:56 +0200 | [diff] [blame] | 2170 | sallvar_T *sav = find_script_var(name, 0, cctx); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2171 | |
| 2172 | if (sav == NULL) |
| 2173 | return -2; |
| 2174 | idx = sav->sav_var_vals_idx; |
| 2175 | sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 2176 | if (check_item_writable(sv, check_writable, name) == FAIL) |
| 2177 | return -2; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2178 | return idx; |
| 2179 | } |
| 2180 | |
| 2181 | // First look the name up in the hashtable. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2182 | ht = &SCRIPT_VARS(sid); |
| 2183 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2184 | if (di == NULL) |
| 2185 | return -2; |
| 2186 | |
| 2187 | // Now find the svar_T index in sn_var_vals. |
| 2188 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2189 | { |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2190 | sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2191 | if (sv->sv_tv == &di->di_tv) |
| 2192 | { |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 2193 | if (check_item_writable(sv, check_writable, name) == FAIL) |
| 2194 | return -2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2195 | return idx; |
| 2196 | } |
| 2197 | } |
| 2198 | return -1; |
| 2199 | } |
| 2200 | |
| 2201 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2202 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2203 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2204 | */ |
| 2205 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2206 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2207 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2208 | int idx; |
| 2209 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2210 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2211 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2212 | if (cctx != NULL) |
| 2213 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2214 | { |
| 2215 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2216 | + idx; |
| 2217 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2218 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2219 | : STRLEN(import->imp_name) == len |
| 2220 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2221 | return import; |
| 2222 | } |
| 2223 | |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 2224 | return find_imported_in_script(name, len, current_sctx.sc_sid); |
| 2225 | } |
| 2226 | |
| 2227 | imported_T * |
| 2228 | find_imported_in_script(char_u *name, size_t len, int sid) |
| 2229 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2230 | scriptitem_T *si; |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 2231 | int idx; |
| 2232 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2233 | if (!SCRIPT_ID_VALID(sid)) |
| 2234 | return NULL; |
| 2235 | si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2236 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2237 | { |
| 2238 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2239 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2240 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2241 | : STRLEN(import->imp_name) == len |
| 2242 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2243 | return import; |
| 2244 | } |
| 2245 | return NULL; |
| 2246 | } |
| 2247 | |
| 2248 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2249 | * Free all imported variables. |
| 2250 | */ |
| 2251 | static void |
| 2252 | free_imported(cctx_T *cctx) |
| 2253 | { |
| 2254 | int idx; |
| 2255 | |
| 2256 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2257 | { |
| 2258 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2259 | |
| 2260 | vim_free(import->imp_name); |
| 2261 | } |
| 2262 | ga_clear(&cctx->ctx_imports); |
| 2263 | } |
| 2264 | |
| 2265 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2266 | * Return a pointer to the next line that isn't empty or only contains a |
| 2267 | * comment. Skips over white space. |
| 2268 | * Returns NULL if there is none. |
| 2269 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2270 | char_u * |
| 2271 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2272 | { |
| 2273 | int lnum = cctx->ctx_lnum; |
| 2274 | |
| 2275 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2276 | { |
| 2277 | 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] | 2278 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2279 | |
Bram Moolenaar | ba60cc4 | 2020-08-12 19:15:33 +0200 | [diff] [blame] | 2280 | // ignore NULLs inserted for continuation lines |
| 2281 | if (line != NULL) |
| 2282 | { |
| 2283 | p = skipwhite(line); |
| 2284 | if (*p != NUL && !vim9_comment_start(p)) |
| 2285 | return p; |
| 2286 | } |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2287 | } |
| 2288 | return NULL; |
| 2289 | } |
| 2290 | |
| 2291 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2292 | * Called when checking for a following operator at "arg". When the rest of |
| 2293 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2294 | * line return a pointer to it and set "nextp". |
| 2295 | * Otherwise skip over white space. |
| 2296 | */ |
| 2297 | static char_u * |
| 2298 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2299 | { |
| 2300 | char_u *p = skipwhite(arg); |
| 2301 | |
| 2302 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2303 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2304 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2305 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2306 | if (*nextp != NULL) |
| 2307 | return *nextp; |
| 2308 | } |
| 2309 | return p; |
| 2310 | } |
| 2311 | |
| 2312 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2313 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2314 | * 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] | 2315 | * Returns NULL when at the end. |
| 2316 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2317 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2318 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2319 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2320 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2321 | |
| 2322 | do |
| 2323 | { |
| 2324 | ++cctx->ctx_lnum; |
| 2325 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2326 | { |
| 2327 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2328 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2329 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2330 | 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] | 2331 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2332 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2333 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 2334 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2335 | return line; |
| 2336 | } |
| 2337 | |
| 2338 | /* |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 2339 | * Skip over white space at "whitep" and assign to "*arg". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2340 | * 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] | 2341 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2342 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2343 | */ |
| 2344 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2345 | 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] | 2346 | { |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 2347 | *arg = skipwhite(whitep); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2348 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2349 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2350 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2351 | |
| 2352 | if (next == NULL) |
| 2353 | return FAIL; |
| 2354 | *arg = skipwhite(next); |
| 2355 | } |
| 2356 | return OK; |
| 2357 | } |
| 2358 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2359 | /* |
| 2360 | * Idem, and give an error when failed. |
| 2361 | */ |
| 2362 | static int |
| 2363 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2364 | { |
| 2365 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2366 | { |
Bram Moolenaar | 8ff16e0 | 2020-12-07 21:49:52 +0100 | [diff] [blame] | 2367 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2368 | emsg(_(e_line_incomplete)); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2369 | return FAIL; |
| 2370 | } |
| 2371 | return OK; |
| 2372 | } |
| 2373 | |
| 2374 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2375 | // Structure passed between the compile_expr* functions to keep track of |
| 2376 | // constants that have been parsed but for which no code was produced yet. If |
| 2377 | // possible expressions on these constants are applied at compile time. If |
| 2378 | // that is not possible, the code to push the constants needs to be generated |
| 2379 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2380 | // Using 50 should be more than enough of 5 levels of (). |
| 2381 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2382 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2383 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2384 | int pp_used; // active entries in pp_tv[] |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2385 | int pp_is_const; // all generated code was constants, used for a |
| 2386 | // list or dict with constant members |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2387 | } ppconst_T; |
| 2388 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2389 | static int compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2390 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2391 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2392 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2393 | /* |
| 2394 | * Generate a PUSH instruction for "tv". |
| 2395 | * "tv" will be consumed or cleared. |
| 2396 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2397 | */ |
| 2398 | static int |
| 2399 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2400 | { |
| 2401 | if (tv != NULL) |
| 2402 | { |
| 2403 | switch (tv->v_type) |
| 2404 | { |
| 2405 | case VAR_UNKNOWN: |
| 2406 | break; |
| 2407 | case VAR_BOOL: |
| 2408 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2409 | break; |
| 2410 | case VAR_SPECIAL: |
| 2411 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2412 | break; |
| 2413 | case VAR_NUMBER: |
| 2414 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2415 | break; |
| 2416 | #ifdef FEAT_FLOAT |
| 2417 | case VAR_FLOAT: |
| 2418 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2419 | break; |
| 2420 | #endif |
| 2421 | case VAR_BLOB: |
| 2422 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2423 | tv->vval.v_blob = NULL; |
| 2424 | break; |
| 2425 | case VAR_STRING: |
| 2426 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2427 | tv->vval.v_string = NULL; |
| 2428 | break; |
| 2429 | default: |
| 2430 | iemsg("constant type not supported"); |
| 2431 | clear_tv(tv); |
| 2432 | return FAIL; |
| 2433 | } |
| 2434 | tv->v_type = VAR_UNKNOWN; |
| 2435 | } |
| 2436 | return OK; |
| 2437 | } |
| 2438 | |
| 2439 | /* |
| 2440 | * Generate code for any ppconst entries. |
| 2441 | */ |
| 2442 | static int |
| 2443 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2444 | { |
| 2445 | int i; |
| 2446 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2447 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2448 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2449 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2450 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2451 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2452 | ret = FAIL; |
| 2453 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2454 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2455 | return ret; |
| 2456 | } |
| 2457 | |
| 2458 | /* |
| 2459 | * Clear ppconst constants. Used when failing. |
| 2460 | */ |
| 2461 | static void |
| 2462 | clear_ppconst(ppconst_T *ppconst) |
| 2463 | { |
| 2464 | int i; |
| 2465 | |
| 2466 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2467 | clear_tv(&ppconst->pp_tv[i]); |
| 2468 | ppconst->pp_used = 0; |
| 2469 | } |
| 2470 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2471 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2472 | * Generate an instruction to load script-local variable "name", without the |
| 2473 | * leading "s:". |
| 2474 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2475 | */ |
| 2476 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2477 | compile_load_scriptvar( |
| 2478 | cctx_T *cctx, |
| 2479 | char_u *name, // variable NUL terminated |
| 2480 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2481 | char_u **end, // end of variable |
| 2482 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2483 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2484 | scriptitem_T *si; |
| 2485 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2486 | imported_T *import; |
| 2487 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2488 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
| 2489 | return FAIL; |
| 2490 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 2491 | idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx); |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2492 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2493 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2494 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2495 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2496 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2497 | } |
| 2498 | if (idx >= 0) |
| 2499 | { |
| 2500 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2501 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2502 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2503 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2504 | return OK; |
| 2505 | } |
| 2506 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2507 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2508 | if (import != NULL) |
| 2509 | { |
Bram Moolenaar | a629495 | 2020-12-27 13:39:50 +0100 | [diff] [blame] | 2510 | if (import->imp_flags & IMP_FLAGS_STAR) |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2511 | { |
| 2512 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2513 | char_u *exp_name; |
| 2514 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2515 | ufunc_T *ufunc; |
| 2516 | type_T *type; |
| 2517 | |
| 2518 | // Used "import * as Name", need to lookup the member. |
| 2519 | if (*p != '.') |
| 2520 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2521 | semsg(_(e_expected_dot_after_name_str), start); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2522 | return FAIL; |
| 2523 | } |
| 2524 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2525 | if (VIM_ISWHITE(*p)) |
| 2526 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2527 | emsg(_(e_no_white_space_allowed_after_dot)); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2528 | return FAIL; |
| 2529 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2530 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2531 | // isolate one name |
| 2532 | exp_name = p; |
| 2533 | while (eval_isnamec(*p)) |
| 2534 | ++p; |
| 2535 | cc = *p; |
| 2536 | *p = NUL; |
| 2537 | |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2538 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2539 | *p = cc; |
| 2540 | p = skipwhite(p); |
| 2541 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2542 | // TODO: what if it is a function? |
| 2543 | if (idx < 0) |
| 2544 | return FAIL; |
| 2545 | *end = p; |
| 2546 | |
| 2547 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2548 | import->imp_sid, |
| 2549 | idx, |
| 2550 | type); |
| 2551 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2552 | else if (import->imp_funcname != NULL) |
| 2553 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2554 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2555 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2556 | import->imp_sid, |
| 2557 | import->imp_var_vals_idx, |
| 2558 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2559 | return OK; |
| 2560 | } |
| 2561 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2562 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2563 | semsg(_(e_item_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2564 | return FAIL; |
| 2565 | } |
| 2566 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2567 | static int |
| 2568 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2569 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2570 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2571 | |
| 2572 | if (ufunc == NULL) |
| 2573 | return FAIL; |
| 2574 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2575 | // Need to compile any default values to get the argument types. |
| 2576 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2577 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2578 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2579 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2580 | } |
| 2581 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2582 | /* |
| 2583 | * Compile a variable name into a load instruction. |
| 2584 | * "end" points to just after the name. |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2585 | * "is_expr" is TRUE when evaluating an expression, might be a funcref. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2586 | * When "error" is FALSE do not give an error when not found. |
| 2587 | */ |
| 2588 | static int |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2589 | compile_load( |
| 2590 | char_u **arg, |
| 2591 | char_u *end_arg, |
| 2592 | cctx_T *cctx, |
| 2593 | int is_expr, |
| 2594 | int error) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2595 | { |
| 2596 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2597 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2598 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2599 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2600 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2601 | |
| 2602 | if (*(*arg + 1) == ':') |
| 2603 | { |
| 2604 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2605 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2606 | { |
| 2607 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2608 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2609 | switch (**arg) |
| 2610 | { |
| 2611 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2612 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2613 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2614 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2615 | default: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2616 | semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2617 | goto theend; |
| 2618 | } |
| 2619 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2620 | goto theend; |
| 2621 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2622 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2623 | else |
| 2624 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2625 | isntype_T isn_type = ISN_DROP; |
| 2626 | |
| 2627 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2628 | if (name == NULL) |
| 2629 | return FAIL; |
| 2630 | |
| 2631 | switch (**arg) |
| 2632 | { |
| 2633 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2634 | break; |
| 2635 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2636 | NULL, NULL, error); |
| 2637 | break; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2638 | case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL) |
| 2639 | isn_type = ISN_LOADG; |
| 2640 | else |
| 2641 | { |
| 2642 | isn_type = ISN_LOADAUTO; |
| 2643 | vim_free(name); |
| 2644 | name = vim_strnsave(*arg, end - *arg); |
| 2645 | if (name == NULL) |
| 2646 | return FAIL; |
| 2647 | } |
| 2648 | break; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2649 | case 'w': isn_type = ISN_LOADW; break; |
| 2650 | case 't': isn_type = ISN_LOADT; break; |
| 2651 | case 'b': isn_type = ISN_LOADB; break; |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 2652 | default: // cannot happen, just in case |
| 2653 | semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2654 | goto theend; |
| 2655 | } |
| 2656 | if (isn_type != ISN_DROP) |
| 2657 | { |
| 2658 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2659 | // variables can be defined later, thus we don't check if it |
| 2660 | // exists, give error at runtime. |
| 2661 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2662 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2663 | } |
| 2664 | } |
| 2665 | else |
| 2666 | { |
| 2667 | size_t len = end - *arg; |
| 2668 | int idx; |
| 2669 | int gen_load = FALSE; |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 2670 | int gen_load_outer = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2671 | |
| 2672 | name = vim_strnsave(*arg, end - *arg); |
| 2673 | if (name == NULL) |
| 2674 | return FAIL; |
| 2675 | |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2676 | if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2677 | { |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 2678 | if (gen_load_outer == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2679 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2680 | } |
| 2681 | else |
| 2682 | { |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 2683 | lvar_T lvar; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2684 | |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 2685 | if (lookup_local(*arg, len, &lvar, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2686 | { |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 2687 | type = lvar.lv_type; |
| 2688 | idx = lvar.lv_idx; |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 2689 | if (lvar.lv_from_outer != 0) |
| 2690 | gen_load_outer = lvar.lv_from_outer; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2691 | else |
| 2692 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2693 | } |
| 2694 | else |
| 2695 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2696 | // "var" can be script-local even without using "s:" if it |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2697 | // already exists in a Vim9 script or when it's imported. |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2698 | if (script_var_exists(*arg, len, TRUE, cctx) == OK |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2699 | || find_imported(name, 0, cctx) != NULL) |
| 2700 | res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2701 | |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2702 | // When evaluating an expression and the name starts with an |
| 2703 | // uppercase letter or "x:" it can be a user defined function. |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2704 | // TODO: this is just guessing |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2705 | if (res == FAIL && is_expr |
| 2706 | && (ASCII_ISUPPER(*name) || name[1] == ':')) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2707 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2708 | } |
| 2709 | } |
| 2710 | if (gen_load) |
| 2711 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 2712 | if (gen_load_outer > 0) |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2713 | { |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 2714 | res = generate_LOADOUTER(cctx, idx, gen_load_outer, type); |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2715 | cctx->ctx_outer_used = TRUE; |
| 2716 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2717 | } |
| 2718 | |
| 2719 | *arg = end; |
| 2720 | |
| 2721 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2722 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2723 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2724 | vim_free(name); |
| 2725 | return res; |
| 2726 | } |
| 2727 | |
| 2728 | /* |
| 2729 | * Compile the argument expressions. |
| 2730 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2731 | */ |
| 2732 | static int |
| 2733 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2734 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2735 | char_u *p = *arg; |
| 2736 | char_u *whitep = *arg; |
Bram Moolenaar | 10e4f12 | 2020-09-20 22:43:52 +0200 | [diff] [blame] | 2737 | int must_end = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2738 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2739 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2740 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2741 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2742 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2743 | if (*p == ')') |
| 2744 | { |
| 2745 | *arg = p + 1; |
| 2746 | return OK; |
| 2747 | } |
Bram Moolenaar | 10e4f12 | 2020-09-20 22:43:52 +0200 | [diff] [blame] | 2748 | if (must_end) |
| 2749 | { |
| 2750 | semsg(_(e_missing_comma_before_argument_str), p); |
| 2751 | return FAIL; |
| 2752 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2753 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2754 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2755 | return FAIL; |
| 2756 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2757 | |
| 2758 | if (*p != ',' && *skipwhite(p) == ',') |
| 2759 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2760 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2761 | p = skipwhite(p); |
| 2762 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2763 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2764 | { |
| 2765 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2766 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2767 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2768 | } |
Bram Moolenaar | 10e4f12 | 2020-09-20 22:43:52 +0200 | [diff] [blame] | 2769 | else |
| 2770 | must_end = TRUE; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2771 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2772 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2773 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2774 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2775 | emsg(_(e_missing_close)); |
| 2776 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | /* |
| 2780 | * Compile a function call: name(arg1, arg2) |
| 2781 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2782 | * "argcount_init" is 1 for "value->method()" |
| 2783 | * Instructions: |
| 2784 | * EVAL arg1 |
| 2785 | * EVAL arg2 |
| 2786 | * BCALL / DCALL / UCALL |
| 2787 | */ |
| 2788 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2789 | compile_call( |
| 2790 | char_u **arg, |
| 2791 | size_t varlen, |
| 2792 | cctx_T *cctx, |
| 2793 | ppconst_T *ppconst, |
| 2794 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2795 | { |
| 2796 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2797 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2798 | int argcount = argcount_init; |
| 2799 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2800 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2801 | char_u *tofree = NULL; |
| 2802 | int error = FCERR_NONE; |
Bram Moolenaar | b3a0194 | 2020-11-17 19:56:09 +0100 | [diff] [blame] | 2803 | ufunc_T *ufunc = NULL; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2804 | int res = FAIL; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2805 | int is_autoload; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2806 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2807 | // we can evaluate "has('name')" at compile time |
| 2808 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2809 | { |
| 2810 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2811 | typval_T argvars[2]; |
| 2812 | |
| 2813 | argvars[0].v_type = VAR_UNKNOWN; |
| 2814 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2815 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2816 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2817 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2818 | s = skipwhite(s); |
Bram Moolenaar | 8cebd43 | 2020-11-08 12:49:47 +0100 | [diff] [blame] | 2819 | if (*s == ')' && argvars[0].v_type == VAR_STRING |
| 2820 | && !dynamic_feature(argvars[0].vval.v_string)) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2821 | { |
| 2822 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2823 | |
| 2824 | *arg = s + 1; |
| 2825 | argvars[1].v_type = VAR_UNKNOWN; |
| 2826 | tv->v_type = VAR_NUMBER; |
| 2827 | tv->vval.v_number = 0; |
| 2828 | f_has(argvars, tv); |
| 2829 | clear_tv(&argvars[0]); |
| 2830 | ++ppconst->pp_used; |
| 2831 | return OK; |
| 2832 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2833 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2837 | return FAIL; |
| 2838 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2839 | if (varlen >= sizeof(namebuf)) |
| 2840 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2841 | semsg(_(e_name_too_long_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2842 | return FAIL; |
| 2843 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2844 | vim_strncpy(namebuf, *arg, varlen); |
| 2845 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2846 | |
| 2847 | *arg = skipwhite(*arg + varlen + 1); |
| 2848 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2849 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2850 | |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2851 | is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2852 | if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2853 | { |
| 2854 | int idx; |
| 2855 | |
| 2856 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2857 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2858 | if (idx >= 0) |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 2859 | { |
| 2860 | if (STRCMP(name, "add") == 0 && argcount == 2) |
| 2861 | { |
| 2862 | garray_T *stack = &cctx->ctx_type_stack; |
| 2863 | type_T *type = ((type_T **)stack->ga_data)[ |
| 2864 | stack->ga_len - 2]; |
| 2865 | |
Bram Moolenaar | e88c8e8 | 2020-11-01 17:03:37 +0100 | [diff] [blame] | 2866 | // add() can be compiled to instructions if we know the type |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 2867 | if (type->tt_type == VAR_LIST) |
| 2868 | { |
| 2869 | // inline "add(list, item)" so that the type can be checked |
| 2870 | res = generate_LISTAPPEND(cctx); |
| 2871 | idx = -1; |
| 2872 | } |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 2873 | else if (type->tt_type == VAR_BLOB) |
| 2874 | { |
| 2875 | // inline "add(blob, nr)" so that the type can be checked |
| 2876 | res = generate_BLOBAPPEND(cctx); |
| 2877 | idx = -1; |
| 2878 | } |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | if (idx >= 0) |
| 2882 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
| 2883 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2884 | else |
| 2885 | semsg(_(e_unknownfunc), namebuf); |
| 2886 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2887 | } |
| 2888 | |
Bram Moolenaar | 52bf81c | 2020-11-17 18:50:44 +0100 | [diff] [blame] | 2889 | // An argument or local variable can be a function reference, this |
| 2890 | // overrules a function name. |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 2891 | if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL |
Bram Moolenaar | 52bf81c | 2020-11-17 18:50:44 +0100 | [diff] [blame] | 2892 | && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2893 | { |
Bram Moolenaar | 52bf81c | 2020-11-17 18:50:44 +0100 | [diff] [blame] | 2894 | // If we can find the function by name generate the right call. |
| 2895 | // Skip global functions here, a local funcref takes precedence. |
| 2896 | ufunc = find_func(name, FALSE, cctx); |
| 2897 | if (ufunc != NULL && !func_is_global(ufunc)) |
| 2898 | { |
| 2899 | res = generate_CALL(cctx, ufunc, argcount); |
| 2900 | goto theend; |
| 2901 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2902 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2903 | |
| 2904 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2905 | // Not for g:Func(), we don't know if it is a variable or not. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2906 | // Not for eome#Func(), it will be loaded later. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2907 | p = namebuf; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2908 | if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2909 | && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2910 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2911 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 2912 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2913 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2914 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2915 | goto theend; |
| 2916 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2917 | |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2918 | // If we can find a global function by name generate the right call. |
| 2919 | if (ufunc != NULL) |
| 2920 | { |
| 2921 | res = generate_CALL(cctx, ufunc, argcount); |
| 2922 | goto theend; |
| 2923 | } |
| 2924 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2925 | // 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] | 2926 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2927 | if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload) |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2928 | res = generate_UCALL(cctx, name, argcount); |
| 2929 | else |
| 2930 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2931 | |
| 2932 | theend: |
| 2933 | vim_free(tofree); |
| 2934 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2938 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2939 | |
| 2940 | /* |
| 2941 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2942 | * does not recognize magic braces. |
Bram Moolenaar | bebaa0d | 2020-11-20 18:59:19 +0100 | [diff] [blame] | 2943 | * When "use_namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2944 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2945 | * valid name. |
| 2946 | */ |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 2947 | char_u * |
Bram Moolenaar | bebaa0d | 2020-11-20 18:59:19 +0100 | [diff] [blame] | 2948 | to_name_end(char_u *arg, int use_namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2949 | { |
| 2950 | char_u *p; |
| 2951 | |
| 2952 | // Quick check for valid starting character. |
| 2953 | if (!eval_isnamec1(*arg)) |
| 2954 | return arg; |
| 2955 | |
| 2956 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2957 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2958 | // and can be used in slice "[n:]". |
| 2959 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | bebaa0d | 2020-11-20 18:59:19 +0100 | [diff] [blame] | 2960 | || !use_namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2961 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2962 | break; |
| 2963 | return p; |
| 2964 | } |
| 2965 | |
| 2966 | /* |
| 2967 | * 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] | 2968 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2969 | */ |
| 2970 | char_u * |
| 2971 | to_name_const_end(char_u *arg) |
| 2972 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2973 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2974 | typval_T rettv; |
| 2975 | |
| 2976 | if (p == arg && *arg == '[') |
| 2977 | { |
| 2978 | |
| 2979 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2980 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2981 | p = arg; |
| 2982 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2983 | return p; |
| 2984 | } |
| 2985 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2986 | /* |
| 2987 | * parse a list: [expr, expr] |
| 2988 | * "*arg" points to the '['. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2989 | * ppconst->pp_is_const is set if all items are a constant. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2990 | */ |
| 2991 | static int |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2992 | compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2993 | { |
| 2994 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2995 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2996 | int count = 0; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2997 | int is_const; |
| 2998 | int is_all_const = TRUE; // reset when non-const encountered |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2999 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3000 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3001 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3002 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3003 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3004 | semsg(_(e_list_end), *arg); |
| 3005 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3006 | } |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 3007 | if (*p == ',') |
| 3008 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3009 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 3010 | return FAIL; |
| 3011 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3012 | if (*p == ']') |
| 3013 | { |
| 3014 | ++p; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3015 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3016 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3017 | if (compile_expr0_ext(&p, cctx, &is_const) == FAIL) |
Bram Moolenaar | c1f0066 | 2020-10-03 13:41:53 +0200 | [diff] [blame] | 3018 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3019 | if (!is_const) |
| 3020 | is_all_const = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3021 | ++count; |
| 3022 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3023 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3024 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3025 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 3026 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3027 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3028 | return FAIL; |
| 3029 | } |
| 3030 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3031 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3032 | p = skipwhite(p); |
| 3033 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3034 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3035 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3036 | ppconst->pp_is_const = is_all_const; |
| 3037 | return generate_NEWLIST(cctx, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | /* |
Bram Moolenaar | 2949cfd | 2020-12-31 21:28:47 +0100 | [diff] [blame] | 3041 | * Parse a lambda: "(arg, arg) => expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3042 | * "*arg" points to the '{'. |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 3043 | * Returns OK/FAIL when a lambda is recognized, NOTDONE if it's not a lambda. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3044 | */ |
| 3045 | static int |
| 3046 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 3047 | { |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 3048 | int r; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3049 | typval_T rettv; |
| 3050 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3051 | evalarg_T evalarg; |
| 3052 | |
| 3053 | CLEAR_FIELD(evalarg); |
| 3054 | evalarg.eval_flags = EVAL_EVALUATE; |
| 3055 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3056 | |
| 3057 | // Get the funcref in "rettv". |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 3058 | r = get_lambda_tv(arg, &rettv, TRUE, &evalarg); |
| 3059 | if (r != OK) |
Bram Moolenaar | 2ea79ad | 2020-10-18 23:32:13 +0200 | [diff] [blame] | 3060 | { |
| 3061 | clear_evalarg(&evalarg, NULL); |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 3062 | return r; |
Bram Moolenaar | 2ea79ad | 2020-10-18 23:32:13 +0200 | [diff] [blame] | 3063 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3064 | |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 3065 | // "rettv" will now be a partial referencing the function. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3066 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3067 | ++ufunc->uf_refcount; |
| 3068 | clear_tv(&rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3069 | |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 3070 | // Compile the function into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3071 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3072 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 3073 | clear_evalarg(&evalarg, NULL); |
| 3074 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 3075 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 3076 | { |
| 3077 | // The return type will now be known. |
| 3078 | set_function_type(ufunc); |
| 3079 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 3080 | // The function reference count will be 1. When the ISN_FUNCREF |
| 3081 | // instruction is deleted the reference count is decremented and the |
| 3082 | // function is freed. |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 3083 | return generate_FUNCREF(cctx, ufunc); |
| 3084 | } |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3085 | |
| 3086 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3087 | return FAIL; |
| 3088 | } |
| 3089 | |
| 3090 | /* |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3091 | * parse a dict: {key: val, [key]: val} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3092 | * "*arg" points to the '{'. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3093 | * ppconst->pp_is_const is set if all item values are a constant. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3094 | */ |
| 3095 | static int |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3096 | compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3097 | { |
| 3098 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3099 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3100 | int count = 0; |
| 3101 | dict_T *d = dict_alloc(); |
| 3102 | dictitem_T *item; |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3103 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3104 | char_u *p; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3105 | int is_const; |
| 3106 | int is_all_const = TRUE; // reset when non-const encountered |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3107 | |
| 3108 | if (d == NULL) |
| 3109 | return FAIL; |
Bram Moolenaar | d62d87d | 2021-01-04 17:40:12 +0100 | [diff] [blame] | 3110 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3111 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3112 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3113 | { |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3114 | char_u *key = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3115 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3116 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3117 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3118 | *arg = NULL; |
| 3119 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | if (**arg == '}') |
| 3123 | break; |
| 3124 | |
Bram Moolenaar | c5e6a71 | 2020-12-04 19:12:14 +0100 | [diff] [blame] | 3125 | if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3126 | { |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3127 | isn_T *isn; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3128 | |
Bram Moolenaar | c5e6a71 | 2020-12-04 19:12:14 +0100 | [diff] [blame] | 3129 | // {[expr]: value} uses an evaluated key. |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3130 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3131 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3132 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3133 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3134 | if (isn->isn_type == ISN_PUSHS) |
| 3135 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3136 | else |
| 3137 | { |
| 3138 | type_T *keytype = ((type_T **)stack->ga_data) |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3139 | [stack->ga_len - 1]; |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 3140 | if (need_type(keytype, &t_string, -1, 0, cctx, |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3141 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3142 | return FAIL; |
| 3143 | } |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3144 | *arg = skipwhite(*arg); |
| 3145 | if (**arg != ']') |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3146 | { |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3147 | emsg(_(e_missing_matching_bracket_after_dict_key)); |
| 3148 | return FAIL; |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3149 | } |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3150 | ++*arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3151 | } |
Bram Moolenaar | c5e6a71 | 2020-12-04 19:12:14 +0100 | [diff] [blame] | 3152 | else |
| 3153 | { |
| 3154 | // {"name": value}, |
| 3155 | // {'name': value}, |
| 3156 | // {name: value} use "name" as a literal key |
| 3157 | key = get_literal_key(arg); |
| 3158 | if (key == NULL) |
| 3159 | return FAIL; |
| 3160 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3161 | return FAIL; |
| 3162 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3163 | |
| 3164 | // Check for duplicate keys, if using string keys. |
| 3165 | if (key != NULL) |
| 3166 | { |
| 3167 | item = dict_find(d, key, -1); |
| 3168 | if (item != NULL) |
| 3169 | { |
| 3170 | semsg(_(e_duplicate_key), key); |
| 3171 | goto failret; |
| 3172 | } |
| 3173 | item = dictitem_alloc(key); |
| 3174 | if (item != NULL) |
| 3175 | { |
| 3176 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3177 | item->di_tv.v_lock = 0; |
| 3178 | if (dict_add(d, item) == FAIL) |
| 3179 | dictitem_free(item); |
| 3180 | } |
| 3181 | } |
| 3182 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3183 | if (**arg != ':') |
| 3184 | { |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 3185 | if (*skipwhite(*arg) == ':') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3186 | semsg(_(e_no_white_space_allowed_before_str), ":"); |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 3187 | else |
| 3188 | semsg(_(e_missing_dict_colon), *arg); |
| 3189 | return FAIL; |
| 3190 | } |
| 3191 | whitep = *arg + 1; |
| 3192 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 3193 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3194 | semsg(_(e_white_space_required_after_str), ":"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3195 | return FAIL; |
| 3196 | } |
| 3197 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3198 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3199 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3200 | *arg = NULL; |
| 3201 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3202 | } |
| 3203 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3204 | if (compile_expr0_ext(arg, cctx, &is_const) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3205 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3206 | if (!is_const) |
| 3207 | is_all_const = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3208 | ++count; |
| 3209 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3210 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3211 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3212 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3213 | *arg = NULL; |
| 3214 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3215 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3216 | if (**arg == '}') |
| 3217 | break; |
| 3218 | if (**arg != ',') |
| 3219 | { |
| 3220 | semsg(_(e_missing_dict_comma), *arg); |
| 3221 | goto failret; |
| 3222 | } |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 3223 | if (IS_WHITE_OR_NUL(*whitep)) |
| 3224 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3225 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 3226 | return FAIL; |
| 3227 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3228 | whitep = *arg + 1; |
Bram Moolenaar | 9a13e18 | 2020-10-19 21:45:07 +0200 | [diff] [blame] | 3229 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 3230 | { |
| 3231 | semsg(_(e_white_space_required_after_str), ","); |
| 3232 | return FAIL; |
| 3233 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3234 | *arg = skipwhite(*arg + 1); |
| 3235 | } |
| 3236 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3237 | *arg = *arg + 1; |
| 3238 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3239 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3240 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3241 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3242 | *arg += STRLEN(*arg); |
| 3243 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3244 | dict_unref(d); |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3245 | ppconst->pp_is_const = is_all_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3246 | return generate_NEWDICT(cctx, count); |
| 3247 | |
| 3248 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3249 | if (*arg == NULL) |
Bram Moolenaar | 44aefff | 2020-10-05 19:23:59 +0200 | [diff] [blame] | 3250 | { |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3251 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 44aefff | 2020-10-05 19:23:59 +0200 | [diff] [blame] | 3252 | *arg = (char_u *)""; |
| 3253 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3254 | dict_unref(d); |
| 3255 | return FAIL; |
| 3256 | } |
| 3257 | |
| 3258 | /* |
| 3259 | * Compile "&option". |
| 3260 | */ |
| 3261 | static int |
| 3262 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3263 | { |
| 3264 | typval_T rettv; |
| 3265 | char_u *start = *arg; |
| 3266 | int ret; |
| 3267 | |
| 3268 | // parse the option and get the current value to get the type. |
| 3269 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3270 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3271 | if (ret == OK) |
| 3272 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3273 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | d5ea8f0 | 2021-01-01 14:49:15 +0100 | [diff] [blame] | 3274 | char_u *name = vim_strnsave(start, *arg - start); |
| 3275 | type_T *type = rettv.v_type == VAR_BOOL ? &t_bool |
| 3276 | : rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3277 | |
| 3278 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3279 | vim_free(name); |
| 3280 | } |
| 3281 | clear_tv(&rettv); |
| 3282 | |
| 3283 | return ret; |
| 3284 | } |
| 3285 | |
| 3286 | /* |
| 3287 | * Compile "$VAR". |
| 3288 | */ |
| 3289 | static int |
| 3290 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3291 | { |
| 3292 | char_u *start = *arg; |
| 3293 | int len; |
| 3294 | int ret; |
| 3295 | char_u *name; |
| 3296 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3297 | ++*arg; |
| 3298 | len = get_env_len(arg); |
| 3299 | if (len == 0) |
| 3300 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3301 | semsg(_(e_syntax_error_at_str), start - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3302 | return FAIL; |
| 3303 | } |
| 3304 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3305 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3306 | name = vim_strnsave(start, len + 1); |
| 3307 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3308 | vim_free(name); |
| 3309 | return ret; |
| 3310 | } |
| 3311 | |
| 3312 | /* |
| 3313 | * Compile "@r". |
| 3314 | */ |
| 3315 | static int |
| 3316 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3317 | { |
| 3318 | int ret; |
| 3319 | |
| 3320 | ++*arg; |
| 3321 | if (**arg == NUL) |
| 3322 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3323 | semsg(_(e_syntax_error_at_str), *arg - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3324 | return FAIL; |
| 3325 | } |
Bram Moolenaar | 7226e5b | 2020-08-02 17:33:26 +0200 | [diff] [blame] | 3326 | if (!valid_yank_reg(**arg, FALSE)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3327 | { |
| 3328 | emsg_invreg(**arg); |
| 3329 | return FAIL; |
| 3330 | } |
| 3331 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3332 | ++*arg; |
| 3333 | return ret; |
| 3334 | } |
| 3335 | |
| 3336 | /* |
| 3337 | * Apply leading '!', '-' and '+' to constant "rettv". |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3338 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3339 | */ |
| 3340 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3341 | apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3342 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3343 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3344 | |
| 3345 | // this works from end to start |
| 3346 | while (p > start) |
| 3347 | { |
| 3348 | --p; |
| 3349 | if (*p == '-' || *p == '+') |
| 3350 | { |
| 3351 | // only '-' has an effect, for '+' we only check the type |
| 3352 | #ifdef FEAT_FLOAT |
| 3353 | if (rettv->v_type == VAR_FLOAT) |
| 3354 | { |
| 3355 | if (*p == '-') |
| 3356 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3357 | } |
| 3358 | else |
| 3359 | #endif |
| 3360 | { |
| 3361 | varnumber_T val; |
| 3362 | int error = FALSE; |
| 3363 | |
| 3364 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3365 | // here |
| 3366 | if (check_not_string(rettv) == FAIL) |
| 3367 | return FAIL; |
| 3368 | val = tv_get_number_chk(rettv, &error); |
| 3369 | clear_tv(rettv); |
| 3370 | if (error) |
| 3371 | return FAIL; |
| 3372 | if (*p == '-') |
| 3373 | val = -val; |
| 3374 | rettv->v_type = VAR_NUMBER; |
| 3375 | rettv->vval.v_number = val; |
| 3376 | } |
| 3377 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3378 | else if (numeric_only) |
| 3379 | { |
| 3380 | ++p; |
| 3381 | break; |
| 3382 | } |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3383 | else if (*p == '!') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3384 | { |
| 3385 | int v = tv2bool(rettv); |
| 3386 | |
| 3387 | // '!' is permissive in the type. |
| 3388 | clear_tv(rettv); |
| 3389 | rettv->v_type = VAR_BOOL; |
| 3390 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3391 | } |
| 3392 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3393 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3394 | return OK; |
| 3395 | } |
| 3396 | |
| 3397 | /* |
| 3398 | * Recognize v: variables that are constants and set "rettv". |
| 3399 | */ |
| 3400 | static void |
| 3401 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3402 | { |
| 3403 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3404 | { |
| 3405 | rettv->v_type = VAR_BOOL; |
| 3406 | rettv->vval.v_number = VVAL_TRUE; |
| 3407 | *arg += 6; |
| 3408 | } |
| 3409 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3410 | { |
| 3411 | rettv->v_type = VAR_BOOL; |
| 3412 | rettv->vval.v_number = VVAL_FALSE; |
| 3413 | *arg += 7; |
| 3414 | } |
| 3415 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3416 | { |
| 3417 | rettv->v_type = VAR_SPECIAL; |
| 3418 | rettv->vval.v_number = VVAL_NULL; |
| 3419 | *arg += 6; |
| 3420 | } |
| 3421 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3422 | { |
| 3423 | rettv->v_type = VAR_SPECIAL; |
| 3424 | rettv->vval.v_number = VVAL_NONE; |
| 3425 | *arg += 6; |
| 3426 | } |
| 3427 | } |
| 3428 | |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 3429 | exprtype_T |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3430 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3431 | { |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 3432 | exprtype_T type = EXPR_UNKNOWN; |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3433 | int i; |
| 3434 | |
| 3435 | switch (p[0]) |
| 3436 | { |
| 3437 | case '=': if (p[1] == '=') |
| 3438 | type = EXPR_EQUAL; |
| 3439 | else if (p[1] == '~') |
| 3440 | type = EXPR_MATCH; |
| 3441 | break; |
| 3442 | case '!': if (p[1] == '=') |
| 3443 | type = EXPR_NEQUAL; |
| 3444 | else if (p[1] == '~') |
| 3445 | type = EXPR_NOMATCH; |
| 3446 | break; |
| 3447 | case '>': if (p[1] != '=') |
| 3448 | { |
| 3449 | type = EXPR_GREATER; |
| 3450 | *len = 1; |
| 3451 | } |
| 3452 | else |
| 3453 | type = EXPR_GEQUAL; |
| 3454 | break; |
| 3455 | case '<': if (p[1] != '=') |
| 3456 | { |
| 3457 | type = EXPR_SMALLER; |
| 3458 | *len = 1; |
| 3459 | } |
| 3460 | else |
| 3461 | type = EXPR_SEQUAL; |
| 3462 | break; |
| 3463 | case 'i': if (p[1] == 's') |
| 3464 | { |
| 3465 | // "is" and "isnot"; but not a prefix of a name |
| 3466 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3467 | *len = 5; |
| 3468 | i = p[*len]; |
| 3469 | if (!isalnum(i) && i != '_') |
| 3470 | { |
| 3471 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3472 | *type_is = TRUE; |
| 3473 | } |
| 3474 | } |
| 3475 | break; |
| 3476 | } |
| 3477 | return type; |
| 3478 | } |
| 3479 | |
| 3480 | /* |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3481 | * Skip over an expression, ignoring most errors. |
| 3482 | */ |
| 3483 | static void |
| 3484 | skip_expr_cctx(char_u **arg, cctx_T *cctx) |
| 3485 | { |
| 3486 | evalarg_T evalarg; |
| 3487 | |
| 3488 | CLEAR_FIELD(evalarg); |
| 3489 | evalarg.eval_cctx = cctx; |
| 3490 | skip_expr(arg, &evalarg); |
| 3491 | } |
| 3492 | |
| 3493 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3494 | * Compile code to apply '-', '+' and '!'. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3495 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3496 | */ |
| 3497 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3498 | compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3499 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3500 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3501 | |
| 3502 | // this works from end to start |
| 3503 | while (p > start) |
| 3504 | { |
| 3505 | --p; |
Bram Moolenaar | 79cdf80 | 2020-11-18 17:39:05 +0100 | [diff] [blame] | 3506 | while (VIM_ISWHITE(*p)) |
| 3507 | --p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3508 | if (*p == '-' || *p == '+') |
| 3509 | { |
| 3510 | int negate = *p == '-'; |
| 3511 | isn_T *isn; |
| 3512 | |
| 3513 | // TODO: check type |
| 3514 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3515 | { |
| 3516 | --p; |
| 3517 | if (*p == '-') |
| 3518 | negate = !negate; |
| 3519 | } |
| 3520 | // only '-' has an effect, for '+' we only check the type |
| 3521 | if (negate) |
| 3522 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3523 | else |
| 3524 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3525 | if (isn == NULL) |
| 3526 | return FAIL; |
| 3527 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3528 | else if (numeric_only) |
| 3529 | { |
| 3530 | ++p; |
| 3531 | break; |
| 3532 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3533 | else |
| 3534 | { |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3535 | int invert = *p == '!'; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3536 | |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3537 | while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1]))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3538 | { |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3539 | if (p[-1] == '!') |
| 3540 | invert = !invert; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3541 | --p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3542 | } |
| 3543 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3544 | return FAIL; |
| 3545 | } |
| 3546 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3547 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3548 | return OK; |
| 3549 | } |
| 3550 | |
| 3551 | /* |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3552 | * Compile "(expression)": recursive! |
| 3553 | * Return FAIL/OK. |
| 3554 | */ |
| 3555 | static int |
| 3556 | compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3557 | { |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3558 | int ret; |
Bram Moolenaar | 2415669 | 2021-01-14 20:35:49 +0100 | [diff] [blame] | 3559 | char_u *p = *arg + 1; |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3560 | |
Bram Moolenaar | 2415669 | 2021-01-14 20:35:49 +0100 | [diff] [blame] | 3561 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3562 | return FAIL; |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3563 | if (ppconst->pp_used <= PPSIZE - 10) |
| 3564 | { |
| 3565 | ret = compile_expr1(arg, cctx, ppconst); |
| 3566 | } |
| 3567 | else |
| 3568 | { |
| 3569 | // Not enough space in ppconst, flush constants. |
| 3570 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3571 | return FAIL; |
| 3572 | ret = compile_expr0(arg, cctx); |
| 3573 | } |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3574 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
| 3575 | return FAIL; |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3576 | if (**arg == ')') |
| 3577 | ++*arg; |
| 3578 | else if (ret == OK) |
| 3579 | { |
| 3580 | emsg(_(e_missing_close)); |
| 3581 | ret = FAIL; |
| 3582 | } |
| 3583 | return ret; |
| 3584 | } |
| 3585 | |
| 3586 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3587 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3588 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3589 | */ |
| 3590 | static int |
| 3591 | compile_subscript( |
| 3592 | char_u **arg, |
| 3593 | cctx_T *cctx, |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3594 | char_u *start_leader, |
| 3595 | char_u **end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3596 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3597 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3598 | char_u *name_start = *end_leader; |
| 3599 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3600 | for (;;) |
| 3601 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3602 | char_u *p = skipwhite(*arg); |
| 3603 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3604 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3605 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3606 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3607 | |
| 3608 | // If a following line starts with "->{" or "->X" advance to that |
| 3609 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3610 | // Also if a following line starts with ".x". |
| 3611 | if (next != NULL && |
| 3612 | ((next[0] == '-' && next[1] == '>' |
| 3613 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3614 | || (next[0] == '.' && eval_isdictc(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3615 | { |
| 3616 | next = next_line_from_context(cctx, TRUE); |
| 3617 | if (next == NULL) |
| 3618 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3619 | *arg = next; |
| 3620 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3621 | } |
| 3622 | } |
| 3623 | |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 3624 | // Do not skip over white space to find the "(", "execute 'x' ()" is |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 3625 | // not a function call. |
| 3626 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3627 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3628 | garray_T *stack = &cctx->ctx_type_stack; |
| 3629 | type_T *type; |
| 3630 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3631 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3632 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3633 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3634 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3635 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3636 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3637 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3638 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3639 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3640 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3641 | return FAIL; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3642 | if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3643 | return FAIL; |
| 3644 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3645 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3646 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3647 | char_u *pstart = p; |
| 3648 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3649 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3650 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3651 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3652 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3653 | // something->method() |
| 3654 | // Apply the '!', '-' and '+' first: |
| 3655 | // -1.0->func() works like (-1.0)->func() |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3656 | if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3657 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3658 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3659 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3660 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3661 | // No line break supported right after "->". |
Bram Moolenaar | 2949cfd | 2020-12-31 21:28:47 +0100 | [diff] [blame] | 3662 | if (**arg == '(') |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 3663 | { |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3664 | int argcount = 1; |
| 3665 | char_u *expr; |
| 3666 | garray_T *stack; |
| 3667 | type_T *type; |
| 3668 | |
| 3669 | // Funcref call: list->(Refs[2])(arg) |
| 3670 | // or lambda: list->((arg) => expr)(arg) |
| 3671 | // Fist compile the arguments. |
| 3672 | expr = *arg; |
| 3673 | *arg = skipwhite(*arg + 1); |
| 3674 | skip_expr_cctx(arg, cctx); |
| 3675 | *arg = skipwhite(*arg); |
| 3676 | if (**arg != ')') |
| 3677 | { |
| 3678 | semsg(_(e_missing_paren), *arg); |
| 3679 | return FAIL; |
| 3680 | } |
| 3681 | ++*arg; |
| 3682 | if (**arg != '(') |
| 3683 | { |
Bram Moolenaar | 2949cfd | 2020-12-31 21:28:47 +0100 | [diff] [blame] | 3684 | if (*skipwhite(*arg) == '(') |
| 3685 | emsg(_(e_nowhitespace)); |
| 3686 | else |
| 3687 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3688 | return FAIL; |
| 3689 | } |
| 3690 | |
| 3691 | *arg = skipwhite(*arg + 1); |
| 3692 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3693 | return FAIL; |
| 3694 | |
| 3695 | // Compile the function expression. |
| 3696 | if (compile_parenthesis(&expr, cctx, ppconst) == FAIL) |
| 3697 | return FAIL; |
| 3698 | |
| 3699 | stack = &cctx->ctx_type_stack; |
| 3700 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3701 | if (generate_PCALL(cctx, argcount, |
| 3702 | (char_u *)"[expression]", type, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3703 | return FAIL; |
| 3704 | } |
| 3705 | else |
| 3706 | { |
| 3707 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3708 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3709 | if (!eval_isnamec1(*p)) |
| 3710 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3711 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3712 | return FAIL; |
| 3713 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3714 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3715 | p += 2; |
Bram Moolenaar | c5da1fb | 2020-08-05 15:43:44 +0200 | [diff] [blame] | 3716 | for ( ; eval_isnamec(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3717 | ; |
| 3718 | if (*p != '(') |
| 3719 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3720 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3721 | return FAIL; |
| 3722 | } |
| 3723 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3724 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3725 | return FAIL; |
| 3726 | } |
| 3727 | } |
Bram Moolenaar | badd848 | 2020-07-31 22:38:17 +0200 | [diff] [blame] | 3728 | else if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3729 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3730 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3731 | type_T **typep; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3732 | type_T *valtype; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3733 | vartype_T vtype; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3734 | int is_slice = FALSE; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3735 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3736 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3737 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3738 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3739 | // TODO: blob index |
| 3740 | // TODO: more arguments |
| 3741 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3742 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3743 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3744 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3745 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3746 | ++p; |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3747 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3748 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3749 | if (**arg == ':') |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3750 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3751 | // missing first index is equal to zero |
| 3752 | generate_PUSHNR(cctx, 0); |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3753 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3754 | else |
| 3755 | { |
| 3756 | if (compile_expr0(arg, cctx) == FAIL) |
| 3757 | return FAIL; |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3758 | if (**arg == ':') |
| 3759 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 3760 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 3761 | ":", *arg); |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3762 | return FAIL; |
| 3763 | } |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3764 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3765 | return FAIL; |
| 3766 | *arg = skipwhite(*arg); |
| 3767 | } |
| 3768 | if (**arg == ':') |
| 3769 | { |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3770 | is_slice = TRUE; |
| 3771 | ++*arg; |
| 3772 | if (!IS_WHITE_OR_NUL(**arg) && **arg != ']') |
| 3773 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 3774 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 3775 | ":", *arg); |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3776 | return FAIL; |
| 3777 | } |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3778 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3779 | return FAIL; |
| 3780 | if (**arg == ']') |
| 3781 | // missing second index is equal to end of string |
| 3782 | generate_PUSHNR(cctx, -1); |
| 3783 | else |
| 3784 | { |
| 3785 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 3786 | return FAIL; |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3787 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3788 | return FAIL; |
| 3789 | *arg = skipwhite(*arg); |
| 3790 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3791 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3792 | |
| 3793 | if (**arg != ']') |
| 3794 | { |
| 3795 | emsg(_(e_missbrac)); |
| 3796 | return FAIL; |
| 3797 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3798 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3799 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3800 | // We can index a list and a dict. If we don't know the type |
| 3801 | // we can use the index value type. |
| 3802 | // TODO: If we don't know use an instruction to figure it out at |
| 3803 | // runtime. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3804 | typep = ((type_T **)stack->ga_data) + stack->ga_len |
| 3805 | - (is_slice ? 3 : 2); |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3806 | vtype = (*typep)->tt_type; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3807 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3808 | // If the index is a string, the variable must be a Dict. |
| 3809 | if (*typep == &t_any && valtype == &t_string) |
| 3810 | vtype = VAR_DICT; |
| 3811 | if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB) |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3812 | { |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 3813 | if (need_type(valtype, &t_number, -1, 0, cctx, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3814 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3815 | return FAIL; |
| 3816 | if (is_slice) |
| 3817 | { |
| 3818 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 3819 | if (need_type(valtype, &t_number, -2, 0, cctx, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3820 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3821 | return FAIL; |
| 3822 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3823 | } |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3824 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3825 | if (vtype == VAR_DICT) |
| 3826 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3827 | if (is_slice) |
| 3828 | { |
| 3829 | emsg(_(e_cannot_slice_dictionary)); |
| 3830 | return FAIL; |
| 3831 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3832 | if ((*typep)->tt_type == VAR_DICT) |
Bram Moolenaar | 31a11b9 | 2021-01-10 19:23:27 +0100 | [diff] [blame] | 3833 | { |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3834 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 31a11b9 | 2021-01-10 19:23:27 +0100 | [diff] [blame] | 3835 | if (*typep == &t_unknown) |
| 3836 | // empty dict was used |
| 3837 | *typep = &t_any; |
| 3838 | } |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3839 | else |
| 3840 | { |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 3841 | if (need_type(*typep, &t_dict_any, -2, 0, cctx, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3842 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3843 | return FAIL; |
| 3844 | *typep = &t_any; |
| 3845 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3846 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3847 | return FAIL; |
| 3848 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3849 | return FAIL; |
| 3850 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3851 | else if (vtype == VAR_STRING) |
| 3852 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3853 | *typep = &t_string; |
| 3854 | if ((is_slice |
| 3855 | ? generate_instr_drop(cctx, ISN_STRSLICE, 2) |
| 3856 | : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL) |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3857 | return FAIL; |
| 3858 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3859 | else if (vtype == VAR_BLOB) |
| 3860 | { |
| 3861 | emsg("Sorry, blob index and slice not implemented yet"); |
| 3862 | return FAIL; |
| 3863 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3864 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3865 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3866 | if (is_slice) |
| 3867 | { |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3868 | if (generate_instr_drop(cctx, |
| 3869 | vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE, |
| 3870 | 2) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3871 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3872 | } |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3873 | else |
| 3874 | { |
| 3875 | if ((*typep)->tt_type == VAR_LIST) |
Bram Moolenaar | 31a11b9 | 2021-01-10 19:23:27 +0100 | [diff] [blame] | 3876 | { |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3877 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 31a11b9 | 2021-01-10 19:23:27 +0100 | [diff] [blame] | 3878 | if (*typep == &t_unknown) |
| 3879 | // empty list was used |
| 3880 | *typep = &t_any; |
| 3881 | } |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3882 | if (generate_instr_drop(cctx, |
| 3883 | vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, |
| 3884 | 1) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3885 | return FAIL; |
| 3886 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3887 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3888 | else |
| 3889 | { |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3890 | emsg(_(e_string_list_dict_or_blob_required)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3891 | return FAIL; |
| 3892 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3893 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3894 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3895 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3896 | // dictionary member: dict.name |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3897 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3898 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3899 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3900 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3901 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3902 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | c1f0066 | 2020-10-03 13:41:53 +0200 | [diff] [blame] | 3903 | { |
| 3904 | emsg(_(e_missing_name_after_dot)); |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3905 | return FAIL; |
Bram Moolenaar | c1f0066 | 2020-10-03 13:41:53 +0200 | [diff] [blame] | 3906 | } |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3907 | p = *arg; |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3908 | if (eval_isdictc(*p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3909 | while (eval_isnamec(*p)) |
| 3910 | MB_PTR_ADV(p); |
| 3911 | if (p == *arg) |
| 3912 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3913 | semsg(_(e_syntax_error_at_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3914 | return FAIL; |
| 3915 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3916 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3917 | return FAIL; |
| 3918 | *arg = p; |
| 3919 | } |
| 3920 | else |
| 3921 | break; |
| 3922 | } |
| 3923 | |
| 3924 | // TODO - see handle_subscript(): |
| 3925 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3926 | // Don't do this when "Func" is already a partial that was bound |
| 3927 | // explicitly (pt_auto is FALSE). |
| 3928 | |
| 3929 | return OK; |
| 3930 | } |
| 3931 | |
| 3932 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3933 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3934 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3935 | * |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3936 | * If the value is a constant "ppconst->pp_used" will be non-zero. |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3937 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3938 | * |
| 3939 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3940 | */ |
| 3941 | |
| 3942 | /* |
| 3943 | * number number constant |
| 3944 | * 0zFFFFFFFF Blob constant |
| 3945 | * "string" string constant |
| 3946 | * 'string' literal string constant |
| 3947 | * &option-name option value |
| 3948 | * @r register contents |
| 3949 | * identifier variable value |
| 3950 | * function() function call |
| 3951 | * $VAR environment variable |
| 3952 | * (expression) nested expression |
| 3953 | * [expr, expr] List |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3954 | * {key: val, [key]: val} Dictionary |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3955 | * |
| 3956 | * Also handle: |
| 3957 | * ! in front logical NOT |
| 3958 | * - in front unary minus |
| 3959 | * + in front unary plus (ignored) |
| 3960 | * trailing (arg) funcref/partial call |
| 3961 | * trailing [] subscript in String or List |
| 3962 | * trailing .name entry in Dictionary |
| 3963 | * trailing ->name() method call |
| 3964 | */ |
| 3965 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3966 | compile_expr7( |
| 3967 | char_u **arg, |
| 3968 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3969 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3970 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3971 | char_u *start_leader, *end_leader; |
| 3972 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3973 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3974 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3975 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3976 | ppconst->pp_is_const = FALSE; |
| 3977 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3978 | /* |
| 3979 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3980 | */ |
| 3981 | start_leader = *arg; |
Bram Moolenaar | b23279d | 2021-01-05 22:08:20 +0100 | [diff] [blame] | 3982 | if (eval_leader(arg, TRUE) == FAIL) |
| 3983 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3984 | end_leader = *arg; |
| 3985 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3986 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3987 | switch (**arg) |
| 3988 | { |
| 3989 | /* |
| 3990 | * Number constant. |
| 3991 | */ |
| 3992 | case '0': // also for blob starting with 0z |
| 3993 | case '1': |
| 3994 | case '2': |
| 3995 | case '3': |
| 3996 | case '4': |
| 3997 | case '5': |
| 3998 | case '6': |
| 3999 | case '7': |
| 4000 | case '8': |
| 4001 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4002 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4003 | return FAIL; |
Bram Moolenaar | 4301a72 | 2020-08-11 20:51:08 +0200 | [diff] [blame] | 4004 | // Apply "-" and "+" just before the number now, right to |
| 4005 | // left. Matters especially when "->" follows. Stops at |
| 4006 | // '!'. |
| 4007 | if (apply_leader(rettv, TRUE, |
| 4008 | start_leader, &end_leader) == FAIL) |
| 4009 | { |
| 4010 | clear_tv(rettv); |
| 4011 | return FAIL; |
| 4012 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4013 | break; |
| 4014 | |
| 4015 | /* |
| 4016 | * String constant: "string". |
| 4017 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4018 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4019 | return FAIL; |
| 4020 | break; |
| 4021 | |
| 4022 | /* |
| 4023 | * Literal string constant: 'str''ing'. |
| 4024 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4025 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4026 | return FAIL; |
| 4027 | break; |
| 4028 | |
| 4029 | /* |
| 4030 | * Constant Vim variable. |
| 4031 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4032 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4033 | ret = NOTDONE; |
| 4034 | break; |
| 4035 | |
| 4036 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4037 | * "true" constant |
| 4038 | */ |
| 4039 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 4040 | && !eval_isnamec((*arg)[4])) |
| 4041 | { |
| 4042 | *arg += 4; |
| 4043 | rettv->v_type = VAR_BOOL; |
| 4044 | rettv->vval.v_number = VVAL_TRUE; |
| 4045 | } |
| 4046 | else |
| 4047 | ret = NOTDONE; |
| 4048 | break; |
| 4049 | |
| 4050 | /* |
| 4051 | * "false" constant |
| 4052 | */ |
| 4053 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 4054 | && !eval_isnamec((*arg)[5])) |
| 4055 | { |
| 4056 | *arg += 5; |
| 4057 | rettv->v_type = VAR_BOOL; |
| 4058 | rettv->vval.v_number = VVAL_FALSE; |
| 4059 | } |
| 4060 | else |
| 4061 | ret = NOTDONE; |
| 4062 | break; |
| 4063 | |
| 4064 | /* |
Bram Moolenaar | 6797782 | 2021-01-03 21:53:53 +0100 | [diff] [blame] | 4065 | * "null" constant |
| 4066 | */ |
| 4067 | case 'n': if (STRNCMP(*arg, "null", 4) == 0 |
| 4068 | && !eval_isnamec((*arg)[5])) |
| 4069 | { |
| 4070 | *arg += 4; |
| 4071 | rettv->v_type = VAR_SPECIAL; |
| 4072 | rettv->vval.v_number = VVAL_NULL; |
| 4073 | } |
| 4074 | else |
| 4075 | ret = NOTDONE; |
| 4076 | break; |
| 4077 | |
| 4078 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4079 | * List: [expr, expr] |
| 4080 | */ |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4081 | case '[': ret = compile_list(arg, cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4082 | break; |
| 4083 | |
| 4084 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4085 | * Dictionary: {'key': val, 'key': val} |
| 4086 | */ |
Bram Moolenaar | 2949cfd | 2020-12-31 21:28:47 +0100 | [diff] [blame] | 4087 | case '{': ret = compile_dict(arg, cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4088 | break; |
| 4089 | |
| 4090 | /* |
| 4091 | * Option value: &name |
| 4092 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 4093 | case '&': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4094 | return FAIL; |
| 4095 | ret = compile_get_option(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4096 | break; |
| 4097 | |
| 4098 | /* |
| 4099 | * Environment variable: $VAR. |
| 4100 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 4101 | case '$': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4102 | return FAIL; |
| 4103 | ret = compile_get_env(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4104 | break; |
| 4105 | |
| 4106 | /* |
| 4107 | * Register contents: @r. |
| 4108 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 4109 | case '@': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4110 | return FAIL; |
| 4111 | ret = compile_get_register(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4112 | break; |
| 4113 | /* |
| 4114 | * nested expression: (expression). |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 4115 | * lambda: (arg, arg) => expr |
| 4116 | * funcref: (arg, arg) => { statement } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4117 | */ |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 4118 | case '(': // if compile_lambda returns NOTDONE then it must be (expr) |
| 4119 | ret = compile_lambda(arg, cctx); |
| 4120 | if (ret == NOTDONE) |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 4121 | ret = compile_parenthesis(arg, cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4122 | break; |
| 4123 | |
| 4124 | default: ret = NOTDONE; |
| 4125 | break; |
| 4126 | } |
| 4127 | if (ret == FAIL) |
| 4128 | return FAIL; |
| 4129 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4130 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4131 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4132 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4133 | clear_tv(rettv); |
| 4134 | else |
| 4135 | // A constant expression can possibly be handled compile time, |
| 4136 | // return the value instead of generating code. |
| 4137 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4138 | } |
| 4139 | else if (ret == NOTDONE) |
| 4140 | { |
| 4141 | char_u *p; |
| 4142 | int r; |
| 4143 | |
| 4144 | if (!eval_isnamec1(**arg)) |
| 4145 | { |
Bram Moolenaar | e498429 | 2020-12-13 14:19:25 +0100 | [diff] [blame] | 4146 | if (ends_excmd(*skipwhite(*arg))) |
| 4147 | semsg(_(e_empty_expression_str), *arg); |
| 4148 | else |
| 4149 | semsg(_(e_name_expected_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4150 | return FAIL; |
| 4151 | } |
| 4152 | |
| 4153 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4154 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4155 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4156 | { |
| 4157 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 4158 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4159 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4160 | { |
| 4161 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4162 | return FAIL; |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 4163 | r = compile_load(arg, p, cctx, TRUE, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4164 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4165 | if (r == FAIL) |
| 4166 | return FAIL; |
| 4167 | } |
| 4168 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4169 | // Handle following "[]", ".member", etc. |
| 4170 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 4171 | if (compile_subscript(arg, cctx, start_leader, &end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4172 | ppconst) == FAIL) |
| 4173 | return FAIL; |
| 4174 | if (ppconst->pp_used > 0) |
| 4175 | { |
| 4176 | // apply the '!', '-' and '+' before the constant |
| 4177 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 4178 | if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4179 | return FAIL; |
| 4180 | return OK; |
| 4181 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 4182 | if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4183 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4184 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | /* |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4188 | * Give the "white on both sides" error, taking the operator from "p[len]". |
| 4189 | */ |
| 4190 | void |
| 4191 | error_white_both(char_u *op, int len) |
| 4192 | { |
| 4193 | char_u buf[10]; |
| 4194 | |
| 4195 | vim_strncpy(buf, op, len); |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 4196 | semsg(_(e_white_space_required_before_and_after_str_at_str), buf, op); |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4197 | } |
| 4198 | |
| 4199 | /* |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4200 | * <type>expr7: runtime type check / conversion |
| 4201 | */ |
| 4202 | static int |
| 4203 | compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 4204 | { |
| 4205 | type_T *want_type = NULL; |
| 4206 | |
| 4207 | // Recognize <type> |
| 4208 | if (**arg == '<' && eval_isnamec1((*arg)[1])) |
| 4209 | { |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4210 | ++*arg; |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4211 | want_type = parse_type(arg, cctx->ctx_type_list, TRUE); |
| 4212 | if (want_type == NULL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4213 | return FAIL; |
| 4214 | |
| 4215 | if (**arg != '>') |
| 4216 | { |
| 4217 | if (*skipwhite(*arg) == '>') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4218 | semsg(_(e_no_white_space_allowed_before_str), ">"); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4219 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4220 | emsg(_(e_missing_gt)); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4221 | return FAIL; |
| 4222 | } |
| 4223 | ++*arg; |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 4224 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4225 | return FAIL; |
| 4226 | } |
| 4227 | |
| 4228 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
| 4229 | return FAIL; |
| 4230 | |
| 4231 | if (want_type != NULL) |
| 4232 | { |
| 4233 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 4234 | type_T *actual; |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4235 | |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 4236 | generate_ppconst(cctx, ppconst); |
| 4237 | actual = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 4238 | if (check_type(want_type, actual, FALSE, 0) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4239 | { |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 4240 | if (need_type(actual, want_type, -1, 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4241 | return FAIL; |
| 4242 | } |
| 4243 | } |
| 4244 | |
| 4245 | return OK; |
| 4246 | } |
| 4247 | |
| 4248 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4249 | * * number multiplication |
| 4250 | * / number division |
| 4251 | * % number modulo |
| 4252 | */ |
| 4253 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4254 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4255 | { |
| 4256 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4257 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4258 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4259 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4260 | // get the first expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4261 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4262 | return FAIL; |
| 4263 | |
| 4264 | /* |
| 4265 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4266 | */ |
| 4267 | for (;;) |
| 4268 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4269 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4270 | if (*op != '*' && *op != '/' && *op != '%') |
| 4271 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4272 | if (next != NULL) |
| 4273 | { |
| 4274 | *arg = next_line_from_context(cctx, TRUE); |
| 4275 | op = skipwhite(*arg); |
| 4276 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4277 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4278 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4279 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4280 | error_white_both(op, 1); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4281 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4282 | } |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4283 | if (may_get_next_line_error(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4284 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4285 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4286 | // get the second expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4287 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4288 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4289 | |
| 4290 | if (ppconst->pp_used == ppconst_used + 2 |
| 4291 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4292 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4293 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4294 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4295 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4296 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4297 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4298 | // both are numbers: compute the result |
| 4299 | switch (*op) |
| 4300 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4301 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4302 | break; |
Bram Moolenaar | e64f83c | 2021-01-19 22:16:41 +0100 | [diff] [blame] | 4303 | case '/': res = num_divide(tv1->vval.v_number, |
| 4304 | tv2->vval.v_number); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4305 | break; |
Bram Moolenaar | e64f83c | 2021-01-19 22:16:41 +0100 | [diff] [blame] | 4306 | case '%': res = num_modulus(tv1->vval.v_number, |
| 4307 | tv2->vval.v_number); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4308 | break; |
| 4309 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4310 | tv1->vval.v_number = res; |
| 4311 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4312 | } |
| 4313 | else |
| 4314 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4315 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4316 | generate_two_op(cctx, op); |
| 4317 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | return OK; |
| 4321 | } |
| 4322 | |
| 4323 | /* |
| 4324 | * + number addition |
| 4325 | * - number subtraction |
| 4326 | * .. string concatenation |
| 4327 | */ |
| 4328 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4329 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4330 | { |
| 4331 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4332 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4333 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4334 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4335 | |
| 4336 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4337 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4338 | return FAIL; |
| 4339 | |
| 4340 | /* |
| 4341 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4342 | */ |
| 4343 | for (;;) |
| 4344 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4345 | op = may_peek_next_line(cctx, *arg, &next); |
| 4346 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4347 | break; |
| 4348 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4349 | if (next != NULL) |
| 4350 | { |
| 4351 | *arg = next_line_from_context(cctx, TRUE); |
| 4352 | op = skipwhite(*arg); |
| 4353 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4354 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4355 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4356 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4357 | error_white_both(op, oplen); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4358 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4359 | } |
| 4360 | |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 4361 | if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4362 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4363 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4364 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4365 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4366 | return FAIL; |
| 4367 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4368 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4369 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4370 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4371 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4372 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4373 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4374 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4375 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4376 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4377 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4378 | // concat/subtract/add constant numbers |
| 4379 | if (*op == '+') |
| 4380 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4381 | else if (*op == '-') |
| 4382 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4383 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4384 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4385 | // concatenate constant strings |
| 4386 | char_u *s1 = tv1->vval.v_string; |
| 4387 | char_u *s2 = tv2->vval.v_string; |
| 4388 | size_t len1 = STRLEN(s1); |
| 4389 | |
| 4390 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4391 | if (tv1->vval.v_string == NULL) |
| 4392 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4393 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4394 | return FAIL; |
| 4395 | } |
| 4396 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4397 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4398 | vim_free(s1); |
| 4399 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4400 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4401 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4402 | } |
| 4403 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4404 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4405 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4406 | if (*op == '.') |
| 4407 | { |
| 4408 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4409 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4410 | return FAIL; |
| 4411 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4412 | } |
| 4413 | else |
| 4414 | generate_two_op(cctx, op); |
| 4415 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | return OK; |
| 4419 | } |
| 4420 | |
| 4421 | /* |
| 4422 | * expr5a == expr5b |
| 4423 | * expr5a =~ expr5b |
| 4424 | * expr5a != expr5b |
| 4425 | * expr5a !~ expr5b |
| 4426 | * expr5a > expr5b |
| 4427 | * expr5a >= expr5b |
| 4428 | * expr5a < expr5b |
| 4429 | * expr5a <= expr5b |
| 4430 | * expr5a is expr5b |
| 4431 | * expr5a isnot expr5b |
| 4432 | * |
| 4433 | * Produces instructions: |
| 4434 | * EVAL expr5a Push result of "expr5a" |
| 4435 | * EVAL expr5b Push result of "expr5b" |
| 4436 | * COMPARE one of the compare instructions |
| 4437 | */ |
| 4438 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4439 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4440 | { |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 4441 | exprtype_T type = EXPR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4442 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4443 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4444 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4445 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4446 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4447 | |
| 4448 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4449 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4450 | return FAIL; |
| 4451 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4452 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4453 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4454 | |
| 4455 | /* |
| 4456 | * If there is a comparative operator, use it. |
| 4457 | */ |
| 4458 | if (type != EXPR_UNKNOWN) |
| 4459 | { |
| 4460 | int ic = FALSE; // Default: do not ignore case |
| 4461 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4462 | if (next != NULL) |
| 4463 | { |
| 4464 | *arg = next_line_from_context(cctx, TRUE); |
| 4465 | p = skipwhite(*arg); |
| 4466 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4467 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4468 | { |
| 4469 | semsg(_(e_invexpr2), *arg); |
| 4470 | return FAIL; |
| 4471 | } |
| 4472 | // extra question mark appended: ignore case |
| 4473 | if (p[len] == '?') |
| 4474 | { |
| 4475 | ic = TRUE; |
| 4476 | ++len; |
| 4477 | } |
| 4478 | // extra '#' appended: match case (ignored) |
| 4479 | else if (p[len] == '#') |
| 4480 | ++len; |
| 4481 | // nothing appended: match case |
| 4482 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4483 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4484 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4485 | error_white_both(p, len); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4486 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | // get the second variable |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4490 | if (may_get_next_line_error(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4491 | return FAIL; |
| 4492 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4493 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4494 | return FAIL; |
| 4495 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4496 | if (ppconst->pp_used == ppconst_used + 2) |
| 4497 | { |
| 4498 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4499 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4500 | int ret; |
| 4501 | |
| 4502 | // Both sides are a constant, compute the result now. |
| 4503 | // First check for a valid combination of types, this is more |
| 4504 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4505 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4506 | ret = FAIL; |
| 4507 | else |
| 4508 | { |
| 4509 | ret = typval_compare(tv1, tv2, type, ic); |
| 4510 | tv1->v_type = VAR_BOOL; |
| 4511 | tv1->vval.v_number = tv1->vval.v_number |
| 4512 | ? VVAL_TRUE : VVAL_FALSE; |
| 4513 | clear_tv(tv2); |
| 4514 | --ppconst->pp_used; |
| 4515 | } |
| 4516 | return ret; |
| 4517 | } |
| 4518 | |
| 4519 | generate_ppconst(cctx, ppconst); |
| 4520 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | return OK; |
| 4524 | } |
| 4525 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4526 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4527 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4528 | /* |
| 4529 | * Compile || or &&. |
| 4530 | */ |
| 4531 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4532 | compile_and_or( |
| 4533 | char_u **arg, |
| 4534 | cctx_T *cctx, |
| 4535 | char *op, |
| 4536 | ppconst_T *ppconst, |
| 4537 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4538 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4539 | char_u *next; |
| 4540 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4541 | int opchar = *op; |
| 4542 | |
| 4543 | if (p[0] == opchar && p[1] == opchar) |
| 4544 | { |
| 4545 | garray_T *instr = &cctx->ctx_instr; |
| 4546 | garray_T end_ga; |
| 4547 | |
| 4548 | /* |
| 4549 | * Repeat until there is no following "||" or "&&" |
| 4550 | */ |
| 4551 | ga_init2(&end_ga, sizeof(int), 10); |
| 4552 | while (p[0] == opchar && p[1] == opchar) |
| 4553 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4554 | if (next != NULL) |
| 4555 | { |
| 4556 | *arg = next_line_from_context(cctx, TRUE); |
| 4557 | p = skipwhite(*arg); |
| 4558 | } |
| 4559 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4560 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4561 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 4562 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 4563 | op, *arg); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4564 | return FAIL; |
| 4565 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4566 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4567 | // TODO: use ppconst if the value is a constant and check |
| 4568 | // evaluating to bool |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4569 | generate_ppconst(cctx, ppconst); |
| 4570 | |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 4571 | // Every part must evaluate to a bool. |
| 4572 | if (bool_on_stack(cctx) == FAIL) |
| 4573 | { |
| 4574 | ga_clear(&end_ga); |
| 4575 | return FAIL; |
| 4576 | } |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4577 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4578 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4579 | { |
| 4580 | ga_clear(&end_ga); |
| 4581 | return FAIL; |
| 4582 | } |
| 4583 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4584 | ++end_ga.ga_len; |
| 4585 | generate_JUMP(cctx, opchar == '|' |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4586 | ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4587 | |
| 4588 | // eval the next expression |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4589 | if (may_get_next_line_error(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 8bb0f54 | 2020-12-06 16:03:55 +0100 | [diff] [blame] | 4590 | { |
| 4591 | ga_clear(&end_ga); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4592 | return FAIL; |
Bram Moolenaar | 8bb0f54 | 2020-12-06 16:03:55 +0100 | [diff] [blame] | 4593 | } |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4594 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4595 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4596 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4597 | { |
| 4598 | ga_clear(&end_ga); |
| 4599 | return FAIL; |
| 4600 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4601 | |
| 4602 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4603 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4604 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4605 | |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 4606 | // Every part must evaluate to a bool. |
| 4607 | if (bool_on_stack(cctx) == FAIL) |
| 4608 | { |
| 4609 | ga_clear(&end_ga); |
| 4610 | return FAIL; |
| 4611 | } |
| 4612 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4613 | // Fill in the end label in all jumps. |
| 4614 | while (end_ga.ga_len > 0) |
| 4615 | { |
| 4616 | isn_T *isn; |
| 4617 | |
| 4618 | --end_ga.ga_len; |
| 4619 | isn = ((isn_T *)instr->ga_data) |
| 4620 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4621 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4622 | } |
| 4623 | ga_clear(&end_ga); |
| 4624 | } |
| 4625 | |
| 4626 | return OK; |
| 4627 | } |
| 4628 | |
| 4629 | /* |
| 4630 | * expr4a && expr4a && expr4a logical AND |
| 4631 | * |
| 4632 | * Produces instructions: |
| 4633 | * EVAL expr4a Push result of "expr4a" |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 4634 | * COND2BOOL convert to bool if needed |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4635 | * JUMP_IF_COND_FALSE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4636 | * EVAL expr4b Push result of "expr4b" |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4637 | * JUMP_IF_COND_FALSE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4638 | * EVAL expr4c Push result of "expr4c" |
| 4639 | * end: |
| 4640 | */ |
| 4641 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4642 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4643 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4644 | int ppconst_used = ppconst->pp_used; |
| 4645 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4646 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4647 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4648 | return FAIL; |
| 4649 | |
| 4650 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4651 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | /* |
| 4655 | * expr3a || expr3b || expr3c logical OR |
| 4656 | * |
| 4657 | * Produces instructions: |
| 4658 | * EVAL expr3a Push result of "expr3a" |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 4659 | * COND2BOOL convert to bool if needed |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4660 | * JUMP_IF_COND_TRUE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4661 | * EVAL expr3b Push result of "expr3b" |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4662 | * JUMP_IF_COND_TRUE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4663 | * EVAL expr3c Push result of "expr3c" |
| 4664 | * end: |
| 4665 | */ |
| 4666 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4667 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4668 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4669 | int ppconst_used = ppconst->pp_used; |
| 4670 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4671 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4672 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4673 | return FAIL; |
| 4674 | |
| 4675 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4676 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4677 | } |
| 4678 | |
| 4679 | /* |
| 4680 | * Toplevel expression: expr2 ? expr1a : expr1b |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4681 | * Produces instructions: |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4682 | * EVAL expr2 Push result of "expr2" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4683 | * JUMP_IF_FALSE alt jump if false |
| 4684 | * EVAL expr1a |
| 4685 | * JUMP_ALWAYS end |
| 4686 | * alt: EVAL expr1b |
| 4687 | * end: |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4688 | * |
| 4689 | * Toplevel expression: expr2 ?? expr1 |
| 4690 | * Produces instructions: |
| 4691 | * EVAL expr2 Push result of "expr2" |
| 4692 | * JUMP_AND_KEEP_IF_TRUE end jump if true |
| 4693 | * EVAL expr1 |
| 4694 | * end: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4695 | */ |
| 4696 | static int |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 4697 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4698 | { |
| 4699 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4700 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4701 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4702 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 4703 | // Ignore all kinds of errors when not producing code. |
| 4704 | if (cctx->ctx_skip == SKIP_YES) |
| 4705 | { |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 4706 | skip_expr_cctx(arg, cctx); |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 4707 | return OK; |
| 4708 | } |
| 4709 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4710 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4711 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4712 | return FAIL; |
| 4713 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4714 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4715 | if (*p == '?') |
| 4716 | { |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4717 | int op_falsy = p[1] == '?'; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4718 | garray_T *instr = &cctx->ctx_instr; |
| 4719 | garray_T *stack = &cctx->ctx_type_stack; |
| 4720 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4721 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4722 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4723 | type_T *type1 = NULL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4724 | int has_const_expr = FALSE; |
| 4725 | int const_value = FALSE; |
| 4726 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4727 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4728 | if (next != NULL) |
| 4729 | { |
| 4730 | *arg = next_line_from_context(cctx, TRUE); |
| 4731 | p = skipwhite(*arg); |
| 4732 | } |
| 4733 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4734 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1 + op_falsy])) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4735 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 4736 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 4737 | op_falsy ? "??" : "?", *arg); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4738 | return FAIL; |
| 4739 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4740 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4741 | if (ppconst->pp_used == ppconst_used + 1) |
| 4742 | { |
| 4743 | // the condition is a constant, we know whether the ? or the : |
| 4744 | // expression is to be evaluated. |
| 4745 | has_const_expr = TRUE; |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 4746 | if (op_falsy) |
| 4747 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4748 | else |
| 4749 | { |
| 4750 | int error = FALSE; |
| 4751 | |
| 4752 | const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used], |
| 4753 | &error); |
| 4754 | if (error) |
| 4755 | return FAIL; |
| 4756 | } |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4757 | cctx->ctx_skip = save_skip == SKIP_YES || |
| 4758 | (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT; |
| 4759 | |
| 4760 | if (op_falsy && cctx->ctx_skip == SKIP_YES) |
| 4761 | // "left ?? right" and "left" is truthy: produce "left" |
| 4762 | generate_ppconst(cctx, ppconst); |
| 4763 | else |
| 4764 | { |
| 4765 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4766 | --ppconst->pp_used; |
| 4767 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4768 | } |
| 4769 | else |
| 4770 | { |
| 4771 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4772 | if (op_falsy) |
| 4773 | end_idx = instr->ga_len; |
| 4774 | generate_JUMP(cctx, op_falsy |
| 4775 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0); |
| 4776 | if (op_falsy) |
| 4777 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4778 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4779 | |
| 4780 | // evaluate the second expression; any type is accepted |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4781 | if (may_get_next_line_error(p + 1 + op_falsy, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4782 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4783 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4784 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4785 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4786 | if (!has_const_expr) |
| 4787 | { |
| 4788 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4789 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4790 | if (!op_falsy) |
| 4791 | { |
| 4792 | // remember the type and drop it |
| 4793 | --stack->ga_len; |
| 4794 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4795 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4796 | end_idx = instr->ga_len; |
| 4797 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4798 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4799 | // jump here from JUMP_IF_FALSE |
| 4800 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4801 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4802 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4803 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4804 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4805 | if (!op_falsy) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4806 | { |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4807 | // Check for the ":". |
| 4808 | p = may_peek_next_line(cctx, *arg, &next); |
| 4809 | if (*p != ':') |
| 4810 | { |
| 4811 | emsg(_(e_missing_colon)); |
| 4812 | return FAIL; |
| 4813 | } |
| 4814 | if (next != NULL) |
| 4815 | { |
| 4816 | *arg = next_line_from_context(cctx, TRUE); |
| 4817 | p = skipwhite(*arg); |
| 4818 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4819 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4820 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4821 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 4822 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 4823 | ":", p); |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4824 | return FAIL; |
| 4825 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4826 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4827 | // evaluate the third expression |
| 4828 | if (has_const_expr) |
| 4829 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4830 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4831 | if (may_get_next_line_error(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4832 | return FAIL; |
| 4833 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
| 4834 | return FAIL; |
| 4835 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4836 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4837 | if (!has_const_expr) |
| 4838 | { |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4839 | type_T **typep; |
| 4840 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4841 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4842 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4843 | // If the types differ, the result has a more generic type. |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4844 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 1; |
| 4845 | common_type(type1, *typep, typep, cctx->ctx_type_list); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4846 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4847 | // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4848 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4849 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4850 | } |
| 4851 | |
| 4852 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4853 | } |
| 4854 | return OK; |
| 4855 | } |
| 4856 | |
| 4857 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4858 | * Toplevel expression. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4859 | * Sets "is_const" (if not NULL) to indicate the value is a constant. |
| 4860 | * Returns OK or FAIL. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4861 | */ |
| 4862 | static int |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4863 | compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4864 | { |
| 4865 | ppconst_T ppconst; |
| 4866 | |
| 4867 | CLEAR_FIELD(ppconst); |
| 4868 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4869 | { |
| 4870 | clear_ppconst(&ppconst); |
| 4871 | return FAIL; |
| 4872 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4873 | if (is_const != NULL) |
| 4874 | *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4875 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4876 | return FAIL; |
| 4877 | return OK; |
| 4878 | } |
| 4879 | |
| 4880 | /* |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4881 | * Toplevel expression. |
| 4882 | */ |
| 4883 | static int |
| 4884 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4885 | { |
| 4886 | return compile_expr0_ext(arg, cctx, NULL); |
| 4887 | } |
| 4888 | |
| 4889 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4890 | * compile "return [expr]" |
| 4891 | */ |
| 4892 | static char_u * |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4893 | compile_return(char_u *arg, int check_return_type, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4894 | { |
| 4895 | char_u *p = arg; |
| 4896 | garray_T *stack = &cctx->ctx_type_stack; |
| 4897 | type_T *stack_type; |
| 4898 | |
| 4899 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4900 | { |
| 4901 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4902 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4903 | return NULL; |
| 4904 | |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4905 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4906 | { |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4907 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 6b55377 | 2020-12-31 13:31:23 +0100 | [diff] [blame] | 4908 | if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL |
Bram Moolenaar | 328eac2 | 2021-01-07 19:23:08 +0100 | [diff] [blame] | 4909 | || cctx->ctx_ufunc->uf_ret_type == &t_unknown |
| 4910 | || cctx->ctx_ufunc->uf_ret_type == &t_any)) |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4911 | { |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4912 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4913 | } |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4914 | else |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4915 | { |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4916 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4917 | && stack_type->tt_type != VAR_VOID |
| 4918 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4919 | { |
| 4920 | emsg(_(e_returning_value_in_function_without_return_type)); |
| 4921 | return NULL; |
| 4922 | } |
| 4923 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 4924 | 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4925 | return NULL; |
| 4926 | } |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4927 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4928 | } |
| 4929 | else |
| 4930 | { |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4931 | // "check_return_type" cannot be TRUE, only used for a lambda which |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4932 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4933 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4934 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4935 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4936 | emsg(_(e_missing_return_value)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4937 | return NULL; |
| 4938 | } |
| 4939 | |
| 4940 | // No argument, return zero. |
| 4941 | generate_PUSHNR(cctx, 0); |
| 4942 | } |
Bram Moolenaar | 7cd2422 | 2021-01-12 18:58:39 +0100 | [diff] [blame] | 4943 | |
| 4944 | // Undo any command modifiers. |
| 4945 | generate_undo_cmdmods(cctx); |
| 4946 | |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4947 | if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4948 | return NULL; |
| 4949 | |
| 4950 | // "return val | endif" is possible |
| 4951 | return skipwhite(p); |
| 4952 | } |
| 4953 | |
| 4954 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4955 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4956 | * Return a pointer to the line in allocated memory. |
| 4957 | * Return NULL for end-of-file or some error. |
| 4958 | */ |
| 4959 | static char_u * |
| 4960 | exarg_getline( |
| 4961 | int c UNUSED, |
| 4962 | void *cookie, |
| 4963 | int indent UNUSED, |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4964 | getline_opt_T options UNUSED) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4965 | { |
| 4966 | cctx_T *cctx = (cctx_T *)cookie; |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4967 | char_u *p; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4968 | |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4969 | for (;;) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4970 | { |
Bram Moolenaar | 2914a20 | 2020-09-27 18:24:03 +0200 | [diff] [blame] | 4971 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1) |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4972 | return NULL; |
| 4973 | ++cctx->ctx_lnum; |
| 4974 | p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]; |
| 4975 | // Comment lines result in NULL pointers, skip them. |
| 4976 | if (p != NULL) |
| 4977 | return vim_strsave(p); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4978 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4979 | } |
| 4980 | |
| 4981 | /* |
| 4982 | * Compile a nested :def command. |
| 4983 | */ |
| 4984 | static char_u * |
| 4985 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4986 | { |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4987 | int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4988 | char_u *name_start = eap->arg; |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4989 | char_u *name_end = to_name_end(eap->arg, TRUE); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4990 | char_u *lambda_name; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4991 | ufunc_T *ufunc; |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 4992 | int r = FAIL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4993 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 4994 | if (eap->forceit) |
Bram Moolenaar | 8b848ca | 2020-09-10 22:28:01 +0200 | [diff] [blame] | 4995 | { |
| 4996 | emsg(_(e_cannot_use_bang_with_nested_def)); |
| 4997 | return NULL; |
| 4998 | } |
| 4999 | |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 5000 | if (*name_start == '/') |
| 5001 | { |
| 5002 | name_end = skip_regexp(name_start + 1, '/', TRUE); |
| 5003 | if (*name_end == '/') |
| 5004 | ++name_end; |
| 5005 | eap->nextcmd = check_nextcmd(name_end); |
| 5006 | } |
| 5007 | if (name_end == name_start || *skipwhite(name_end) != '(') |
| 5008 | { |
| 5009 | if (!ends_excmd2(name_start, name_end)) |
| 5010 | { |
| 5011 | semsg(_(e_invalid_command_str), eap->cmd); |
| 5012 | return NULL; |
| 5013 | } |
| 5014 | |
| 5015 | // "def" or "def Name": list functions |
| 5016 | if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL) |
| 5017 | return NULL; |
| 5018 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5019 | } |
| 5020 | |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 5021 | // Only g:Func() can use a namespace. |
| 5022 | if (name_start[1] == ':' && !is_global) |
| 5023 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5024 | semsg(_(e_namespace_not_supported_str), name_start); |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 5025 | return NULL; |
| 5026 | } |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 5027 | if (check_defined(name_start, name_end - name_start, cctx) == FAIL) |
| 5028 | return NULL; |
| 5029 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5030 | eap->arg = name_end; |
| 5031 | eap->getline = exarg_getline; |
| 5032 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5033 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5034 | eap->forceit = FALSE; |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5035 | lambda_name = vim_strsave(get_lambda_name()); |
| 5036 | if (lambda_name == NULL) |
| 5037 | return NULL; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 5038 | ufunc = define_function(eap, lambda_name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5039 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 5040 | if (ufunc == NULL) |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5041 | { |
| 5042 | r = eap->skip ? OK : FAIL; |
| 5043 | goto theend; |
| 5044 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 5045 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 5046 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 4ee711f | 2020-09-23 18:51:11 +0200 | [diff] [blame] | 5047 | { |
| 5048 | func_ptr_unref(ufunc); |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5049 | goto theend; |
Bram Moolenaar | 4ee711f | 2020-09-23 18:51:11 +0200 | [diff] [blame] | 5050 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5051 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5052 | if (is_global) |
| 5053 | { |
| 5054 | char_u *func_name = vim_strnsave(name_start + 2, |
| 5055 | name_end - name_start - 2); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 5056 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5057 | if (func_name == NULL) |
| 5058 | r = FAIL; |
| 5059 | else |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5060 | { |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 5061 | r = generate_NEWFUNC(cctx, lambda_name, func_name); |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5062 | lambda_name = NULL; |
| 5063 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5064 | } |
| 5065 | else |
| 5066 | { |
| 5067 | // Define a local variable for the function reference. |
Bram Moolenaar | e8211a3 | 2020-10-09 22:04:29 +0200 | [diff] [blame] | 5068 | lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5069 | TRUE, ufunc->uf_func_type); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 5070 | int block_depth = cctx->ctx_ufunc->uf_block_depth; |
Bram Moolenaar | e8211a3 | 2020-10-09 22:04:29 +0200 | [diff] [blame] | 5071 | |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 5072 | if (lvar == NULL) |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5073 | goto theend; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 5074 | if (generate_FUNCREF(cctx, ufunc) == FAIL) |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5075 | goto theend; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5076 | r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 5077 | |
| 5078 | // copy over the block scope IDs |
| 5079 | if (block_depth > 0) |
| 5080 | { |
| 5081 | ufunc->uf_block_ids = ALLOC_MULT(int, block_depth); |
| 5082 | if (ufunc->uf_block_ids != NULL) |
| 5083 | { |
| 5084 | mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids, |
| 5085 | sizeof(int) * block_depth); |
| 5086 | ufunc->uf_block_depth = block_depth; |
| 5087 | } |
| 5088 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5089 | } |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 5090 | // TODO: warning for trailing text? |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5091 | r = OK; |
| 5092 | |
| 5093 | theend: |
| 5094 | vim_free(lambda_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5095 | return r == FAIL ? NULL : (char_u *)""; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5096 | } |
| 5097 | |
| 5098 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5099 | * Return the length of an assignment operator, or zero if there isn't one. |
| 5100 | */ |
| 5101 | int |
| 5102 | assignment_len(char_u *p, int *heredoc) |
| 5103 | { |
| 5104 | if (*p == '=') |
| 5105 | { |
| 5106 | if (p[1] == '<' && p[2] == '<') |
| 5107 | { |
| 5108 | *heredoc = TRUE; |
| 5109 | return 3; |
| 5110 | } |
| 5111 | return 1; |
| 5112 | } |
| 5113 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 5114 | return 2; |
| 5115 | if (STRNCMP(p, "..=", 3) == 0) |
| 5116 | return 3; |
| 5117 | return 0; |
| 5118 | } |
| 5119 | |
| 5120 | // words that cannot be used as a variable |
| 5121 | static char *reserved[] = { |
| 5122 | "true", |
| 5123 | "false", |
Bram Moolenaar | 6797782 | 2021-01-03 21:53:53 +0100 | [diff] [blame] | 5124 | "null", |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5125 | NULL |
| 5126 | }; |
| 5127 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5128 | // Destination for an assignment or ":unlet" with an index. |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5129 | typedef enum { |
| 5130 | dest_local, |
| 5131 | dest_option, |
| 5132 | dest_env, |
| 5133 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 5134 | dest_buffer, |
| 5135 | dest_window, |
| 5136 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5137 | dest_vimvar, |
| 5138 | dest_script, |
| 5139 | dest_reg, |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5140 | dest_expr, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5141 | } assign_dest_T; |
| 5142 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5143 | // Used by compile_lhs() to store information about the LHS of an assignment |
| 5144 | // and one argument of ":unlet" with an index. |
| 5145 | typedef struct { |
| 5146 | assign_dest_T lhs_dest; // type of destination |
| 5147 | |
| 5148 | char_u *lhs_name; // allocated name including |
| 5149 | // "[expr]" or ".name". |
| 5150 | size_t lhs_varlen; // length of the variable without |
| 5151 | // "[expr]" or ".name" |
| 5152 | char_u *lhs_dest_end; // end of the destination, including |
| 5153 | // "[expr]" or ".name". |
| 5154 | |
| 5155 | int lhs_has_index; // has "[expr]" or ".name" |
| 5156 | |
| 5157 | int lhs_new_local; // create new local variable |
| 5158 | int lhs_opt_flags; // for when destination is an option |
| 5159 | int lhs_vimvaridx; // for when destination is a v:var |
| 5160 | |
| 5161 | lvar_T lhs_local_lvar; // used for existing local destination |
| 5162 | lvar_T lhs_arg_lvar; // used for argument destination |
| 5163 | lvar_T *lhs_lvar; // points to destination lvar |
| 5164 | int lhs_scriptvar_sid; |
| 5165 | int lhs_scriptvar_idx; |
| 5166 | |
| 5167 | int lhs_has_type; // type was specified |
| 5168 | type_T *lhs_type; |
| 5169 | type_T *lhs_member_type; |
| 5170 | } lhs_T; |
| 5171 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5172 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5173 | * Generate the load instruction for "name". |
| 5174 | */ |
| 5175 | static void |
| 5176 | generate_loadvar( |
| 5177 | cctx_T *cctx, |
| 5178 | assign_dest_T dest, |
| 5179 | char_u *name, |
| 5180 | lvar_T *lvar, |
| 5181 | type_T *type) |
| 5182 | { |
| 5183 | switch (dest) |
| 5184 | { |
| 5185 | case dest_option: |
| 5186 | // TODO: check the option exists |
| 5187 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 5188 | break; |
| 5189 | case dest_global: |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 5190 | if (vim_strchr(name, AUTOLOAD_CHAR) == NULL) |
| 5191 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 5192 | else |
| 5193 | generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5194 | break; |
| 5195 | case dest_buffer: |
| 5196 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 5197 | break; |
| 5198 | case dest_window: |
| 5199 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 5200 | break; |
| 5201 | case dest_tab: |
| 5202 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 5203 | break; |
| 5204 | case dest_script: |
| 5205 | compile_load_scriptvar(cctx, |
| 5206 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 5207 | break; |
| 5208 | case dest_env: |
| 5209 | // Include $ in the name here |
| 5210 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 5211 | break; |
| 5212 | case dest_reg: |
| 5213 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 5214 | break; |
| 5215 | case dest_vimvar: |
| 5216 | generate_LOADV(cctx, name + 2, TRUE); |
| 5217 | break; |
| 5218 | case dest_local: |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 5219 | if (lvar->lv_from_outer > 0) |
| 5220 | generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer, |
| 5221 | type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5222 | else |
| 5223 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 5224 | break; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5225 | case dest_expr: |
| 5226 | // list or dict value should already be on the stack. |
| 5227 | break; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5228 | } |
| 5229 | } |
| 5230 | |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5231 | /* |
| 5232 | * Skip over "[expr]" or ".member". |
| 5233 | * Does not check for any errors. |
| 5234 | */ |
| 5235 | static char_u * |
| 5236 | skip_index(char_u *start) |
| 5237 | { |
| 5238 | char_u *p = start; |
| 5239 | |
| 5240 | if (*p == '[') |
| 5241 | { |
| 5242 | p = skipwhite(p + 1); |
| 5243 | (void)skip_expr(&p, NULL); |
| 5244 | p = skipwhite(p); |
| 5245 | if (*p == ']') |
| 5246 | return p + 1; |
| 5247 | return p; |
| 5248 | } |
| 5249 | // if (*p == '.') |
| 5250 | return to_name_end(p + 1, TRUE); |
| 5251 | } |
| 5252 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5253 | void |
| 5254 | vim9_declare_error(char_u *name) |
| 5255 | { |
| 5256 | char *scope = ""; |
| 5257 | |
| 5258 | switch (*name) |
| 5259 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 5260 | case 'g': scope = _("global"); break; |
| 5261 | case 'b': scope = _("buffer"); break; |
| 5262 | case 'w': scope = _("window"); break; |
| 5263 | case 't': scope = _("tab"); break; |
| 5264 | case 'v': scope = "v:"; break; |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 5265 | case '$': semsg(_(e_cannot_declare_an_environment_variable), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5266 | return; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5267 | case '&': semsg(_(e_cannot_declare_an_option), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5268 | return; |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 5269 | case '@': semsg(_(e_cannot_declare_a_register_str), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5270 | return; |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 5271 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5272 | } |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 5273 | semsg(_(e_cannot_declare_a_scope_variable), scope, name); |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5274 | } |
| 5275 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5276 | /* |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5277 | * For one assignment figure out the type of destination. Return it in "dest". |
| 5278 | * When not recognized "dest" is not set. |
| 5279 | * For an option "opt_flags" is set. |
| 5280 | * For a v:var "vimvaridx" is set. |
| 5281 | * "type" is set to the destination type if known, unchanted otherwise. |
| 5282 | * Return FAIL if an error message was given. |
| 5283 | */ |
| 5284 | static int |
| 5285 | get_var_dest( |
| 5286 | char_u *name, |
| 5287 | assign_dest_T *dest, |
| 5288 | int cmdidx, |
| 5289 | int *opt_flags, |
| 5290 | int *vimvaridx, |
| 5291 | type_T **type, |
| 5292 | cctx_T *cctx) |
| 5293 | { |
| 5294 | char_u *p; |
| 5295 | |
| 5296 | if (*name == '&') |
| 5297 | { |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5298 | int cc; |
| 5299 | long numval; |
| 5300 | getoption_T opt_type; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5301 | |
| 5302 | *dest = dest_option; |
| 5303 | if (cmdidx == CMD_final || cmdidx == CMD_const) |
| 5304 | { |
| 5305 | emsg(_(e_const_option)); |
| 5306 | return FAIL; |
| 5307 | } |
| 5308 | p = name; |
| 5309 | p = find_option_end(&p, opt_flags); |
| 5310 | if (p == NULL) |
| 5311 | { |
| 5312 | // cannot happen? |
| 5313 | emsg(_(e_letunexp)); |
| 5314 | return FAIL; |
| 5315 | } |
| 5316 | cc = *p; |
| 5317 | *p = NUL; |
| 5318 | opt_type = get_option_value(skip_option_env_lead(name), |
| 5319 | &numval, NULL, *opt_flags); |
| 5320 | *p = cc; |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5321 | switch (opt_type) |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5322 | { |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5323 | case gov_unknown: |
| 5324 | semsg(_(e_unknown_option), name); |
| 5325 | return FAIL; |
| 5326 | case gov_string: |
| 5327 | case gov_hidden_string: |
| 5328 | *type = &t_string; |
| 5329 | break; |
| 5330 | case gov_bool: |
| 5331 | case gov_hidden_bool: |
| 5332 | *type = &t_bool; |
| 5333 | break; |
| 5334 | case gov_number: |
| 5335 | case gov_hidden_number: |
| 5336 | *type = &t_number; |
| 5337 | break; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5338 | } |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5339 | } |
| 5340 | else if (*name == '$') |
| 5341 | { |
| 5342 | *dest = dest_env; |
| 5343 | *type = &t_string; |
| 5344 | } |
| 5345 | else if (*name == '@') |
| 5346 | { |
| 5347 | if (!valid_yank_reg(name[1], FALSE) || name[1] == '.') |
| 5348 | { |
| 5349 | emsg_invreg(name[1]); |
| 5350 | return FAIL; |
| 5351 | } |
| 5352 | *dest = dest_reg; |
| 5353 | *type = &t_string; |
| 5354 | } |
| 5355 | else if (STRNCMP(name, "g:", 2) == 0) |
| 5356 | { |
| 5357 | *dest = dest_global; |
| 5358 | } |
| 5359 | else if (STRNCMP(name, "b:", 2) == 0) |
| 5360 | { |
| 5361 | *dest = dest_buffer; |
| 5362 | } |
| 5363 | else if (STRNCMP(name, "w:", 2) == 0) |
| 5364 | { |
| 5365 | *dest = dest_window; |
| 5366 | } |
| 5367 | else if (STRNCMP(name, "t:", 2) == 0) |
| 5368 | { |
| 5369 | *dest = dest_tab; |
| 5370 | } |
| 5371 | else if (STRNCMP(name, "v:", 2) == 0) |
| 5372 | { |
| 5373 | typval_T *vtv; |
| 5374 | int di_flags; |
| 5375 | |
| 5376 | *vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5377 | if (*vimvaridx < 0) |
| 5378 | { |
| 5379 | semsg(_(e_variable_not_found_str), name); |
| 5380 | return FAIL; |
| 5381 | } |
| 5382 | // We use the current value of "sandbox" here, is that OK? |
| 5383 | if (var_check_ro(di_flags, name, FALSE)) |
| 5384 | return FAIL; |
| 5385 | *dest = dest_vimvar; |
| 5386 | vtv = get_vim_var_tv(*vimvaridx); |
| 5387 | *type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
| 5388 | } |
| 5389 | return OK; |
| 5390 | } |
| 5391 | |
| 5392 | /* |
| 5393 | * Generate a STORE instruction for "dest", not being "dest_local". |
| 5394 | * Return FAIL when out of memory. |
| 5395 | */ |
| 5396 | static int |
| 5397 | generate_store_var( |
| 5398 | cctx_T *cctx, |
| 5399 | assign_dest_T dest, |
| 5400 | int opt_flags, |
| 5401 | int vimvaridx, |
| 5402 | int scriptvar_idx, |
| 5403 | int scriptvar_sid, |
| 5404 | type_T *type, |
| 5405 | char_u *name) |
| 5406 | { |
| 5407 | switch (dest) |
| 5408 | { |
| 5409 | case dest_option: |
| 5410 | return generate_STOREOPT(cctx, skip_option_env_lead(name), |
| 5411 | opt_flags); |
| 5412 | case dest_global: |
| 5413 | // include g: with the name, easier to execute that way |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 5414 | return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL |
| 5415 | ? ISN_STOREG : ISN_STOREAUTO, 0, name); |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5416 | case dest_buffer: |
| 5417 | // include b: with the name, easier to execute that way |
| 5418 | return generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5419 | case dest_window: |
| 5420 | // include w: with the name, easier to execute that way |
| 5421 | return generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5422 | case dest_tab: |
| 5423 | // include t: with the name, easier to execute that way |
| 5424 | return generate_STORE(cctx, ISN_STORET, 0, name); |
| 5425 | case dest_env: |
| 5426 | return generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5427 | case dest_reg: |
| 5428 | return generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5429 | case dest_vimvar: |
| 5430 | return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5431 | case dest_script: |
| 5432 | if (scriptvar_idx < 0) |
| 5433 | { |
| 5434 | char_u *name_s = name; |
Bram Moolenaar | 41d6196 | 2020-12-06 20:12:43 +0100 | [diff] [blame] | 5435 | int r; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5436 | |
Bram Moolenaar | 41d6196 | 2020-12-06 20:12:43 +0100 | [diff] [blame] | 5437 | // "s:" is included in the name. |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5438 | r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s, |
| 5439 | scriptvar_sid, type); |
| 5440 | if (name_s != name) |
| 5441 | vim_free(name_s); |
| 5442 | return r; |
| 5443 | } |
| 5444 | return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
| 5445 | scriptvar_sid, scriptvar_idx, type); |
| 5446 | case dest_local: |
| 5447 | case dest_expr: |
| 5448 | // cannot happen |
| 5449 | break; |
| 5450 | } |
| 5451 | return FAIL; |
| 5452 | } |
| 5453 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5454 | static int |
| 5455 | is_decl_command(int cmdidx) |
| 5456 | { |
| 5457 | return cmdidx == CMD_let || cmdidx == CMD_var |
| 5458 | || cmdidx == CMD_final || cmdidx == CMD_const; |
| 5459 | } |
| 5460 | |
| 5461 | /* |
| 5462 | * Figure out the LHS type and other properties for an assignment or one item |
| 5463 | * of ":unlet" with an index. |
| 5464 | * Returns OK or FAIL. |
| 5465 | */ |
| 5466 | static int |
| 5467 | compile_lhs( |
| 5468 | char_u *var_start, |
| 5469 | lhs_T *lhs, |
| 5470 | int cmdidx, |
| 5471 | int heredoc, |
| 5472 | int oplen, |
| 5473 | cctx_T *cctx) |
| 5474 | { |
| 5475 | char_u *var_end; |
| 5476 | int is_decl = is_decl_command(cmdidx); |
| 5477 | |
| 5478 | CLEAR_POINTER(lhs); |
| 5479 | lhs->lhs_dest = dest_local; |
| 5480 | lhs->lhs_vimvaridx = -1; |
| 5481 | lhs->lhs_scriptvar_idx = -1; |
| 5482 | |
| 5483 | // "dest_end" is the end of the destination, including "[expr]" or |
| 5484 | // ".name". |
| 5485 | // "var_end" is the end of the variable/option/etc. name. |
| 5486 | lhs->lhs_dest_end = skip_var_one(var_start, FALSE); |
| 5487 | if (*var_start == '@') |
| 5488 | var_end = var_start + 2; |
| 5489 | else |
| 5490 | { |
| 5491 | // skip over the leading "&", "&l:", "&g:" and "$" |
| 5492 | var_end = skip_option_env_lead(var_start); |
| 5493 | var_end = to_name_end(var_end, TRUE); |
| 5494 | } |
| 5495 | |
| 5496 | // "a: type" is declaring variable "a" with a type, not dict "a:". |
| 5497 | if (is_decl && lhs->lhs_dest_end == var_start + 2 |
| 5498 | && lhs->lhs_dest_end[-1] == ':') |
| 5499 | --lhs->lhs_dest_end; |
| 5500 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 5501 | --var_end; |
| 5502 | |
| 5503 | // compute the length of the destination without "[expr]" or ".name" |
| 5504 | lhs->lhs_varlen = var_end - var_start; |
| 5505 | lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen); |
| 5506 | if (lhs->lhs_name == NULL) |
| 5507 | return FAIL; |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 5508 | |
| 5509 | if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen) |
| 5510 | // Something follows after the variable: "var[idx]" or "var.key". |
| 5511 | lhs->lhs_has_index = TRUE; |
| 5512 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5513 | if (heredoc) |
| 5514 | lhs->lhs_type = &t_list_string; |
| 5515 | else |
| 5516 | lhs->lhs_type = &t_any; |
| 5517 | |
| 5518 | if (cctx->ctx_skip != SKIP_YES) |
| 5519 | { |
| 5520 | int declare_error = FALSE; |
| 5521 | |
| 5522 | if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx, |
| 5523 | &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx, |
| 5524 | &lhs->lhs_type, cctx) == FAIL) |
| 5525 | return FAIL; |
| 5526 | if (lhs->lhs_dest != dest_local) |
| 5527 | { |
| 5528 | // Specific kind of variable recognized. |
| 5529 | declare_error = is_decl; |
| 5530 | } |
| 5531 | else |
| 5532 | { |
| 5533 | int idx; |
| 5534 | |
| 5535 | // No specific kind of variable recognized, just a name. |
| 5536 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5537 | if (STRCMP(reserved[idx], lhs->lhs_name) == 0) |
| 5538 | { |
| 5539 | semsg(_(e_cannot_use_reserved_name), lhs->lhs_name); |
| 5540 | return FAIL; |
| 5541 | } |
| 5542 | |
| 5543 | |
| 5544 | if (lookup_local(var_start, lhs->lhs_varlen, |
| 5545 | &lhs->lhs_local_lvar, cctx) == OK) |
| 5546 | lhs->lhs_lvar = &lhs->lhs_local_lvar; |
| 5547 | else |
| 5548 | { |
| 5549 | CLEAR_FIELD(lhs->lhs_arg_lvar); |
| 5550 | if (arg_exists(var_start, lhs->lhs_varlen, |
| 5551 | &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type, |
| 5552 | &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK) |
| 5553 | { |
| 5554 | if (is_decl) |
| 5555 | { |
| 5556 | semsg(_(e_str_is_used_as_argument), lhs->lhs_name); |
| 5557 | return FAIL; |
| 5558 | } |
| 5559 | lhs->lhs_lvar = &lhs->lhs_arg_lvar; |
| 5560 | } |
| 5561 | } |
| 5562 | if (lhs->lhs_lvar != NULL) |
| 5563 | { |
| 5564 | if (is_decl) |
| 5565 | { |
| 5566 | semsg(_(e_variable_already_declared), lhs->lhs_name); |
| 5567 | return FAIL; |
| 5568 | } |
| 5569 | } |
| 5570 | else |
| 5571 | { |
| 5572 | int script_namespace = lhs->lhs_varlen > 1 |
| 5573 | && STRNCMP(var_start, "s:", 2) == 0; |
| 5574 | int script_var = (script_namespace |
| 5575 | ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2, |
| 5576 | FALSE, cctx) |
| 5577 | : script_var_exists(var_start, lhs->lhs_varlen, |
| 5578 | TRUE, cctx)) == OK; |
| 5579 | imported_T *import = |
| 5580 | find_imported(var_start, lhs->lhs_varlen, cctx); |
| 5581 | |
| 5582 | if (script_namespace || script_var || import != NULL) |
| 5583 | { |
| 5584 | char_u *rawname = lhs->lhs_name |
| 5585 | + (lhs->lhs_name[1] == ':' ? 2 : 0); |
| 5586 | |
| 5587 | if (is_decl) |
| 5588 | { |
| 5589 | if (script_namespace) |
| 5590 | semsg(_(e_cannot_declare_script_variable_in_function), |
| 5591 | lhs->lhs_name); |
| 5592 | else |
| 5593 | semsg(_(e_variable_already_declared_in_script), |
| 5594 | lhs->lhs_name); |
| 5595 | return FAIL; |
| 5596 | } |
| 5597 | else if (cctx->ctx_ufunc->uf_script_ctx_version |
| 5598 | == SCRIPT_VERSION_VIM9 |
| 5599 | && script_namespace |
| 5600 | && !script_var && import == NULL) |
| 5601 | { |
| 5602 | semsg(_(e_unknown_variable_str), lhs->lhs_name); |
| 5603 | return FAIL; |
| 5604 | } |
| 5605 | |
| 5606 | lhs->lhs_dest = dest_script; |
| 5607 | |
| 5608 | // existing script-local variables should have a type |
| 5609 | lhs->lhs_scriptvar_sid = current_sctx.sc_sid; |
| 5610 | if (import != NULL) |
| 5611 | lhs->lhs_scriptvar_sid = import->imp_sid; |
| 5612 | if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid)) |
| 5613 | { |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 5614 | // Check writable only when no index follows. |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5615 | lhs->lhs_scriptvar_idx = get_script_item_idx( |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 5616 | lhs->lhs_scriptvar_sid, rawname, |
| 5617 | lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST, |
| 5618 | cctx); |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5619 | if (lhs->lhs_scriptvar_idx >= 0) |
| 5620 | { |
| 5621 | scriptitem_T *si = SCRIPT_ITEM( |
| 5622 | lhs->lhs_scriptvar_sid); |
| 5623 | svar_T *sv = |
| 5624 | ((svar_T *)si->sn_var_vals.ga_data) |
| 5625 | + lhs->lhs_scriptvar_idx; |
| 5626 | lhs->lhs_type = sv->sv_type; |
| 5627 | } |
| 5628 | } |
| 5629 | } |
| 5630 | else if (check_defined(var_start, lhs->lhs_varlen, cctx) |
| 5631 | == FAIL) |
| 5632 | return FAIL; |
| 5633 | } |
| 5634 | } |
| 5635 | |
| 5636 | if (declare_error) |
| 5637 | { |
| 5638 | vim9_declare_error(lhs->lhs_name); |
| 5639 | return FAIL; |
| 5640 | } |
| 5641 | } |
| 5642 | |
| 5643 | // handle "a:name" as a name, not index "name" on "a" |
| 5644 | if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':') |
| 5645 | var_end = lhs->lhs_dest_end; |
| 5646 | |
| 5647 | if (lhs->lhs_dest != dest_option) |
| 5648 | { |
| 5649 | if (is_decl && *var_end == ':') |
| 5650 | { |
| 5651 | char_u *p; |
| 5652 | |
| 5653 | // parse optional type: "let var: type = expr" |
| 5654 | if (!VIM_ISWHITE(var_end[1])) |
| 5655 | { |
| 5656 | semsg(_(e_white_space_required_after_str), ":"); |
| 5657 | return FAIL; |
| 5658 | } |
| 5659 | p = skipwhite(var_end + 1); |
| 5660 | lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE); |
| 5661 | if (lhs->lhs_type == NULL) |
| 5662 | return FAIL; |
| 5663 | lhs->lhs_has_type = TRUE; |
| 5664 | } |
| 5665 | else if (lhs->lhs_lvar != NULL) |
| 5666 | lhs->lhs_type = lhs->lhs_lvar->lv_type; |
| 5667 | } |
| 5668 | |
| 5669 | if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global |
| 5670 | && lhs->lhs_type->tt_type != VAR_STRING |
| 5671 | && lhs->lhs_type->tt_type != VAR_ANY) |
| 5672 | { |
| 5673 | emsg(_(e_can_only_concatenate_to_string)); |
| 5674 | return FAIL; |
| 5675 | } |
| 5676 | |
| 5677 | if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local |
| 5678 | && cctx->ctx_skip != SKIP_YES) |
| 5679 | { |
| 5680 | if (oplen > 1 && !heredoc) |
| 5681 | { |
| 5682 | // +=, /=, etc. require an existing variable |
| 5683 | semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name); |
| 5684 | return FAIL; |
| 5685 | } |
| 5686 | if (!is_decl) |
| 5687 | { |
| 5688 | semsg(_(e_unknown_variable_str), lhs->lhs_name); |
| 5689 | return FAIL; |
| 5690 | } |
| 5691 | |
| 5692 | // new local variable |
| 5693 | if ((lhs->lhs_type->tt_type == VAR_FUNC |
| 5694 | || lhs->lhs_type->tt_type == VAR_PARTIAL) |
| 5695 | && var_wrong_func_name(lhs->lhs_name, TRUE)) |
| 5696 | return FAIL; |
| 5697 | lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen, |
| 5698 | cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type); |
| 5699 | if (lhs->lhs_lvar == NULL) |
| 5700 | return FAIL; |
| 5701 | lhs->lhs_new_local = TRUE; |
| 5702 | } |
| 5703 | |
| 5704 | lhs->lhs_member_type = lhs->lhs_type; |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 5705 | if (lhs->lhs_has_index) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5706 | { |
| 5707 | // Something follows after the variable: "var[idx]" or "var.key". |
| 5708 | // TODO: should we also handle "->func()" here? |
| 5709 | if (is_decl) |
| 5710 | { |
| 5711 | emsg(_(e_cannot_use_index_when_declaring_variable)); |
| 5712 | return FAIL; |
| 5713 | } |
| 5714 | |
| 5715 | if (var_start[lhs->lhs_varlen] == '[' |
| 5716 | || var_start[lhs->lhs_varlen] == '.') |
| 5717 | { |
| 5718 | char_u *after = var_start + lhs->lhs_varlen; |
| 5719 | char_u *p; |
| 5720 | |
| 5721 | // Only the last index is used below, if there are others |
| 5722 | // before it generate code for the expression. Thus for |
| 5723 | // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index. |
| 5724 | for (;;) |
| 5725 | { |
| 5726 | p = skip_index(after); |
| 5727 | if (*p != '[' && *p != '.') |
| 5728 | break; |
| 5729 | after = p; |
| 5730 | } |
| 5731 | if (after > var_start + lhs->lhs_varlen) |
| 5732 | { |
| 5733 | lhs->lhs_varlen = after - var_start; |
| 5734 | lhs->lhs_dest = dest_expr; |
| 5735 | // We don't know the type before evaluating the expression, |
| 5736 | // use "any" until then. |
| 5737 | lhs->lhs_type = &t_any; |
| 5738 | } |
| 5739 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5740 | if (lhs->lhs_type->tt_member == NULL) |
| 5741 | lhs->lhs_member_type = &t_any; |
| 5742 | else |
| 5743 | lhs->lhs_member_type = lhs->lhs_type->tt_member; |
| 5744 | } |
| 5745 | else |
| 5746 | { |
| 5747 | semsg("Not supported yet: %s", var_start); |
| 5748 | return FAIL; |
| 5749 | } |
| 5750 | } |
| 5751 | return OK; |
| 5752 | } |
| 5753 | |
| 5754 | /* |
| 5755 | * Assignment to a list or dict member, or ":unlet" for the item, using the |
| 5756 | * information in "lhs". |
| 5757 | * Returns OK or FAIL. |
| 5758 | */ |
| 5759 | static int |
| 5760 | compile_assign_unlet( |
| 5761 | char_u *var_start, |
| 5762 | lhs_T *lhs, |
| 5763 | int is_assign, |
| 5764 | type_T *rhs_type, |
| 5765 | cctx_T *cctx) |
| 5766 | { |
| 5767 | char_u *p; |
| 5768 | int r; |
| 5769 | vartype_T dest_type; |
| 5770 | size_t varlen = lhs->lhs_varlen; |
| 5771 | garray_T *stack = &cctx->ctx_type_stack; |
| 5772 | |
| 5773 | // Compile the "idx" in "var[idx]" or "key" in "var.key". |
| 5774 | p = var_start + varlen; |
| 5775 | if (*p == '[') |
| 5776 | { |
| 5777 | p = skipwhite(p + 1); |
| 5778 | r = compile_expr0(&p, cctx); |
| 5779 | if (r == OK && *skipwhite(p) != ']') |
| 5780 | { |
| 5781 | // this should not happen |
| 5782 | emsg(_(e_missbrac)); |
| 5783 | r = FAIL; |
| 5784 | } |
| 5785 | } |
| 5786 | else // if (*p == '.') |
| 5787 | { |
| 5788 | char_u *key_end = to_name_end(p + 1, TRUE); |
| 5789 | char_u *key = vim_strnsave(p + 1, key_end - p - 1); |
| 5790 | |
| 5791 | r = generate_PUSHS(cctx, key); |
| 5792 | } |
| 5793 | if (r == FAIL) |
| 5794 | return FAIL; |
| 5795 | |
| 5796 | if (lhs->lhs_type == &t_any) |
| 5797 | { |
| 5798 | // Index on variable of unknown type: check at runtime. |
| 5799 | dest_type = VAR_ANY; |
| 5800 | } |
| 5801 | else |
| 5802 | { |
| 5803 | dest_type = lhs->lhs_type->tt_type; |
| 5804 | if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL) |
| 5805 | return FAIL; |
| 5806 | if (dest_type == VAR_LIST |
Bram Moolenaar | f30a14d | 2021-01-17 21:51:24 +0100 | [diff] [blame] | 5807 | && need_type(((type_T **)stack->ga_data)[stack->ga_len - 1], |
| 5808 | &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5809 | return FAIL; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5810 | } |
| 5811 | |
| 5812 | // Load the dict or list. On the stack we then have: |
| 5813 | // - value (for assignment, not for :unlet) |
| 5814 | // - index |
| 5815 | // - variable |
| 5816 | if (lhs->lhs_dest == dest_expr) |
| 5817 | { |
| 5818 | int c = var_start[varlen]; |
| 5819 | |
| 5820 | // Evaluate "ll[expr]" of "ll[expr][idx]" |
| 5821 | p = var_start; |
| 5822 | var_start[varlen] = NUL; |
| 5823 | if (compile_expr0(&p, cctx) == OK && p != var_start + varlen) |
| 5824 | { |
| 5825 | // this should not happen |
| 5826 | emsg(_(e_missbrac)); |
| 5827 | return FAIL; |
| 5828 | } |
| 5829 | var_start[varlen] = c; |
| 5830 | |
| 5831 | lhs->lhs_type = stack->ga_len == 0 ? &t_void |
| 5832 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 5833 | // now we can properly check the type |
| 5834 | if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 5835 | && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx, |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5836 | FALSE, FALSE) == FAIL) |
| 5837 | return FAIL; |
| 5838 | } |
| 5839 | else |
| 5840 | generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name, |
| 5841 | lhs->lhs_lvar, lhs->lhs_type); |
| 5842 | |
| 5843 | if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY) |
| 5844 | { |
| 5845 | if (is_assign) |
| 5846 | { |
| 5847 | isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3); |
| 5848 | |
| 5849 | if (isn == NULL) |
| 5850 | return FAIL; |
| 5851 | isn->isn_arg.vartype = dest_type; |
| 5852 | } |
| 5853 | else |
| 5854 | { |
| 5855 | if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL) |
| 5856 | return FAIL; |
| 5857 | } |
| 5858 | } |
| 5859 | else |
| 5860 | { |
| 5861 | emsg(_(e_indexable_type_required)); |
| 5862 | return FAIL; |
| 5863 | } |
| 5864 | |
| 5865 | return OK; |
| 5866 | } |
| 5867 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5868 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5869 | * Compile declaration and assignment: |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 5870 | * "let name" |
| 5871 | * "var name = expr" |
| 5872 | * "final name = expr" |
| 5873 | * "const name = expr" |
| 5874 | * "name = expr" |
| 5875 | * "arg" points to "name". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5876 | * Return NULL for an error. |
| 5877 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5878 | */ |
| 5879 | static char_u * |
| 5880 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 5881 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5882 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5883 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5884 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5885 | char_u *ret = NULL; |
| 5886 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5887 | int var_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5888 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5889 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5890 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5891 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5892 | int oplen = 0; |
| 5893 | int heredoc = FALSE; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5894 | type_T *rhs_type = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5895 | char_u *sp; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5896 | int is_decl = is_decl_command(cmdidx); |
| 5897 | lhs_T lhs; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5898 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5899 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 5900 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 5901 | if (p == NULL) |
| 5902 | return *arg == '[' ? arg : NULL; |
| 5903 | |
| 5904 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5905 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 5906 | // TODO: should we allow this, and figure out type inference from list |
| 5907 | // members? |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5908 | emsg(_(e_cannot_use_list_for_declaration)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5909 | return NULL; |
| 5910 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5911 | lhs.lhs_name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5912 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5913 | sp = p; |
| 5914 | p = skipwhite(p); |
| 5915 | op = p; |
| 5916 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5917 | |
| 5918 | if (var_count > 0 && oplen == 0) |
| 5919 | // can be something like "[1, 2]->func()" |
| 5920 | return arg; |
| 5921 | |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5922 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen]))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5923 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 5924 | error_white_both(op, oplen); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5925 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 5926 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5927 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5928 | if (heredoc) |
| 5929 | { |
| 5930 | list_T *l; |
| 5931 | listitem_T *li; |
| 5932 | |
| 5933 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5934 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5935 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 5936 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | c0e2901 | 2020-09-27 14:22:48 +0200 | [diff] [blame] | 5937 | if (l == NULL) |
| 5938 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5939 | |
Bram Moolenaar | 078269b | 2020-09-21 20:35:55 +0200 | [diff] [blame] | 5940 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5941 | { |
Bram Moolenaar | 078269b | 2020-09-21 20:35:55 +0200 | [diff] [blame] | 5942 | // Push each line and the create the list. |
| 5943 | FOR_ALL_LIST_ITEMS(l, li) |
| 5944 | { |
| 5945 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 5946 | li->li_tv.vval.v_string = NULL; |
| 5947 | } |
| 5948 | generate_NEWLIST(cctx, l->lv_len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5949 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5950 | list_free(l); |
| 5951 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5952 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5953 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5954 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5955 | { |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5956 | char_u *wp; |
| 5957 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5958 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 5959 | // list of variables below. |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5960 | // A line break may follow the "=". |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5961 | |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5962 | wp = op + oplen; |
Bram Moolenaar | 8ff16e0 | 2020-12-07 21:49:52 +0100 | [diff] [blame] | 5963 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5964 | return FAIL; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5965 | if (compile_expr0(&p, cctx) == FAIL) |
| 5966 | return NULL; |
| 5967 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5968 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5969 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5970 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5971 | type_T *stacktype; |
| 5972 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5973 | stacktype = stack->ga_len == 0 ? &t_void |
| 5974 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5975 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5976 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5977 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5978 | goto theend; |
| 5979 | } |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 5980 | if (need_type(stacktype, &t_list_any, -1, 0, cctx, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 5981 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5982 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5983 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5984 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 5985 | semicolon); |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5986 | if (stacktype->tt_member != NULL) |
| 5987 | rhs_type = stacktype->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5988 | } |
| 5989 | } |
| 5990 | |
| 5991 | /* |
| 5992 | * Loop over variables in "[var, var] = expr". |
| 5993 | * For "var = expr" and "let var: type" this is done only once. |
| 5994 | */ |
| 5995 | if (var_count > 0) |
| 5996 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 5997 | else |
| 5998 | var_start = arg; |
| 5999 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 6000 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6001 | int instr_count = -1; |
| 6002 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6003 | vim_free(lhs.lhs_name); |
| 6004 | |
| 6005 | /* |
| 6006 | * Figure out the LHS type and other properties. |
| 6007 | */ |
| 6008 | if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL) |
| 6009 | goto theend; |
| 6010 | |
| 6011 | if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar) |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 6012 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6013 | semsg(_(e_cannot_assign_to_argument), lhs.lhs_name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6014 | goto theend; |
| 6015 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6016 | if (!is_decl && lhs.lhs_lvar != NULL |
| 6017 | && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6018 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6019 | semsg(_(e_cannot_assign_to_constant), lhs.lhs_name); |
Bram Moolenaar | dbeecb2 | 2020-09-14 18:15:09 +0200 | [diff] [blame] | 6020 | goto theend; |
| 6021 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6022 | |
| 6023 | if (!heredoc) |
| 6024 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6025 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6026 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6027 | if (oplen > 0 && var_count == 0) |
| 6028 | { |
| 6029 | // skip over the "=" and the expression |
| 6030 | p = skipwhite(op + oplen); |
| 6031 | compile_expr0(&p, cctx); |
| 6032 | } |
| 6033 | } |
| 6034 | else if (oplen > 0) |
| 6035 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 6036 | int is_const = FALSE; |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 6037 | char_u *wp; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6038 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6039 | // For "var = expr" evaluate the expression. |
| 6040 | if (var_count == 0) |
| 6041 | { |
| 6042 | int r; |
| 6043 | |
| 6044 | // for "+=", "*=", "..=" etc. first load the current value |
| 6045 | if (*op != '=') |
| 6046 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6047 | generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name, |
| 6048 | lhs.lhs_lvar, lhs.lhs_type); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6049 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6050 | if (lhs.lhs_has_index) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6051 | { |
| 6052 | // TODO: get member from list or dict |
| 6053 | emsg("Index with operation not supported yet"); |
| 6054 | goto theend; |
| 6055 | } |
| 6056 | } |
| 6057 | |
| 6058 | // Compile the expression. Temporarily hide the new local |
| 6059 | // variable here, it is not available to this expression. |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6060 | if (lhs.lhs_new_local) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6061 | --cctx->ctx_locals.ga_len; |
| 6062 | instr_count = instr->ga_len; |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 6063 | wp = op + oplen; |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 6064 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
Bram Moolenaar | 21e5122 | 2020-12-04 12:43:29 +0100 | [diff] [blame] | 6065 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6066 | if (lhs.lhs_new_local) |
Bram Moolenaar | 21e5122 | 2020-12-04 12:43:29 +0100 | [diff] [blame] | 6067 | ++cctx->ctx_locals.ga_len; |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 6068 | goto theend; |
Bram Moolenaar | 21e5122 | 2020-12-04 12:43:29 +0100 | [diff] [blame] | 6069 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 6070 | r = compile_expr0_ext(&p, cctx, &is_const); |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6071 | if (lhs.lhs_new_local) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6072 | ++cctx->ctx_locals.ga_len; |
| 6073 | if (r == FAIL) |
| 6074 | goto theend; |
| 6075 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 6076 | else if (semicolon && var_idx == var_count - 1) |
| 6077 | { |
| 6078 | // For "[var; var] = expr" get the rest of the list |
| 6079 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 6080 | goto theend; |
| 6081 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6082 | else |
| 6083 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6084 | // For "[var, var] = expr" get the "var_idx" item from the |
| 6085 | // list. |
| 6086 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 6087 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6088 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6089 | |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6090 | rhs_type = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 6091 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6092 | if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6093 | { |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6094 | if ((rhs_type->tt_type == VAR_FUNC |
| 6095 | || rhs_type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6096 | && var_wrong_func_name(lhs.lhs_name, TRUE)) |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 6097 | goto theend; |
| 6098 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6099 | if (lhs.lhs_new_local && !lhs.lhs_has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6100 | { |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6101 | if (rhs_type->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6102 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6103 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6104 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6105 | } |
| 6106 | else |
| 6107 | { |
Bram Moolenaar | 04bdd57 | 2020-09-23 13:25:32 +0200 | [diff] [blame] | 6108 | // An empty list or dict has a &t_unknown member, |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6109 | // for a variable that implies &t_any. |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6110 | if (rhs_type == &t_list_empty) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6111 | lhs.lhs_lvar->lv_type = &t_list_any; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6112 | else if (rhs_type == &t_dict_empty) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6113 | lhs.lhs_lvar->lv_type = &t_dict_any; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6114 | else if (rhs_type == &t_unknown) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6115 | lhs.lhs_lvar->lv_type = &t_any; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6116 | else |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6117 | lhs.lhs_lvar->lv_type = rhs_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6118 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6119 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6120 | else if (*op == '=') |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6121 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6122 | type_T *use_type = lhs.lhs_lvar->lv_type; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6123 | |
Bram Moolenaar | 71abe48 | 2020-09-14 22:28:30 +0200 | [diff] [blame] | 6124 | // without operator check type here, otherwise below |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6125 | if (lhs.lhs_has_index) |
| 6126 | use_type = lhs.lhs_member_type; |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 6127 | if (need_type(rhs_type, use_type, -1, 0, cctx, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 6128 | FALSE, is_const) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6129 | goto theend; |
| 6130 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6131 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6132 | else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type, |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 6133 | -1, 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6134 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6135 | } |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 6136 | else if (cmdidx == CMD_final) |
| 6137 | { |
| 6138 | emsg(_(e_final_requires_a_value)); |
| 6139 | goto theend; |
| 6140 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6141 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6142 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 6143 | emsg(_(e_const_requires_a_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6144 | goto theend; |
| 6145 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6146 | else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6147 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 6148 | emsg(_(e_type_or_initialization_required)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6149 | goto theend; |
| 6150 | } |
| 6151 | else |
| 6152 | { |
| 6153 | // variables are always initialized |
| 6154 | if (ga_grow(instr, 1) == FAIL) |
| 6155 | goto theend; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6156 | switch (lhs.lhs_member_type->tt_type) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6157 | { |
| 6158 | case VAR_BOOL: |
| 6159 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 6160 | break; |
| 6161 | case VAR_FLOAT: |
| 6162 | #ifdef FEAT_FLOAT |
| 6163 | generate_PUSHF(cctx, 0.0); |
| 6164 | #endif |
| 6165 | break; |
| 6166 | case VAR_STRING: |
| 6167 | generate_PUSHS(cctx, NULL); |
| 6168 | break; |
| 6169 | case VAR_BLOB: |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 6170 | generate_PUSHBLOB(cctx, blob_alloc()); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6171 | break; |
| 6172 | case VAR_FUNC: |
| 6173 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 6174 | break; |
| 6175 | case VAR_LIST: |
| 6176 | generate_NEWLIST(cctx, 0); |
| 6177 | break; |
| 6178 | case VAR_DICT: |
| 6179 | generate_NEWDICT(cctx, 0); |
| 6180 | break; |
| 6181 | case VAR_JOB: |
| 6182 | generate_PUSHJOB(cctx, NULL); |
| 6183 | break; |
| 6184 | case VAR_CHANNEL: |
| 6185 | generate_PUSHCHANNEL(cctx, NULL); |
| 6186 | break; |
| 6187 | case VAR_NUMBER: |
| 6188 | case VAR_UNKNOWN: |
| 6189 | case VAR_ANY: |
| 6190 | case VAR_PARTIAL: |
| 6191 | case VAR_VOID: |
| 6192 | case VAR_SPECIAL: // cannot happen |
| 6193 | generate_PUSHNR(cctx, 0); |
| 6194 | break; |
| 6195 | } |
| 6196 | } |
| 6197 | if (var_count == 0) |
| 6198 | end = p; |
| 6199 | } |
| 6200 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6201 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6202 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6203 | break; |
| 6204 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6205 | if (oplen > 0 && *op != '=') |
| 6206 | { |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6207 | type_T *expected; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6208 | type_T *stacktype; |
| 6209 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6210 | if (*op == '.') |
| 6211 | expected = &t_string; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6212 | else |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6213 | expected = lhs.lhs_member_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6214 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6215 | if ( |
| 6216 | #ifdef FEAT_FLOAT |
| 6217 | // If variable is float operation with number is OK. |
| 6218 | !(expected == &t_float && stacktype == &t_number) && |
| 6219 | #endif |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 6220 | need_type(stacktype, expected, -1, 0, cctx, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 6221 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6222 | goto theend; |
| 6223 | |
| 6224 | if (*op == '.') |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 6225 | { |
| 6226 | if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL) |
| 6227 | goto theend; |
| 6228 | } |
| 6229 | else if (*op == '+') |
| 6230 | { |
| 6231 | if (generate_add_instr(cctx, |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6232 | operator_type(lhs.lhs_member_type, stacktype), |
| 6233 | lhs.lhs_member_type, stacktype) == FAIL) |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 6234 | goto theend; |
| 6235 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6236 | else if (generate_two_op(cctx, op) == FAIL) |
| 6237 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6238 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6239 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6240 | if (lhs.lhs_has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6241 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6242 | // Use the info in "lhs" to store the value at the index in the |
| 6243 | // list or dict. |
| 6244 | if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx) |
| 6245 | == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6246 | goto theend; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6247 | } |
| 6248 | else |
| 6249 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6250 | if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script |
| 6251 | || lhs.lhs_dest == dest_local)) |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 6252 | // ":const var": lock the value, but not referenced variables |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 6253 | generate_LOCKCONST(cctx); |
| 6254 | |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 6255 | if (is_decl |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6256 | && (lhs.lhs_type->tt_type == VAR_DICT |
| 6257 | || lhs.lhs_type->tt_type == VAR_LIST) |
| 6258 | && lhs.lhs_type->tt_member != NULL |
| 6259 | && lhs.lhs_type->tt_member != &t_any |
| 6260 | && lhs.lhs_type->tt_member != &t_unknown) |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 6261 | // Set the type in the list or dict, so that it can be checked, |
| 6262 | // also in legacy script. |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6263 | generate_SETTYPE(cctx, lhs.lhs_type); |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 6264 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6265 | if (lhs.lhs_dest != dest_local) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6266 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6267 | if (generate_store_var(cctx, lhs.lhs_dest, |
| 6268 | lhs.lhs_opt_flags, lhs.lhs_vimvaridx, |
| 6269 | lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid, |
| 6270 | lhs.lhs_type, lhs.lhs_name) == FAIL) |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6271 | goto theend; |
| 6272 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6273 | else if (lhs.lhs_lvar != NULL) |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6274 | { |
| 6275 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 6276 | + instr->ga_len - 1; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6277 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6278 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 6279 | // ISN_STORE into ISN_STORENR |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 6280 | if (lhs.lhs_lvar->lv_from_outer == 0 |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6281 | && instr->ga_len == instr_count + 1 |
| 6282 | && isn->isn_type == ISN_PUSHNR) |
| 6283 | { |
| 6284 | varnumber_T val = isn->isn_arg.number; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6285 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6286 | isn->isn_type = ISN_STORENR; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6287 | isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6288 | isn->isn_arg.storenr.stnr_val = val; |
| 6289 | if (stack->ga_len > 0) |
| 6290 | --stack->ga_len; |
| 6291 | } |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 6292 | else if (lhs.lhs_lvar->lv_from_outer > 0) |
| 6293 | generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx, |
| 6294 | lhs.lhs_lvar->lv_from_outer); |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6295 | else |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6296 | generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6297 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6298 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6299 | |
| 6300 | if (var_idx + 1 < var_count) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6301 | var_start = skipwhite(lhs.lhs_dest_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6302 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6303 | |
| 6304 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 6305 | if (var_count > 0 && !semicolon) |
| 6306 | { |
Bram Moolenaar | ec79229 | 2020-12-13 21:26:56 +0100 | [diff] [blame] | 6307 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 6308 | goto theend; |
| 6309 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6310 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 6311 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6312 | |
| 6313 | theend: |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6314 | vim_free(lhs.lhs_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6315 | return ret; |
| 6316 | } |
| 6317 | |
| 6318 | /* |
Bram Moolenaar | 17126b1 | 2021-01-07 22:03:02 +0100 | [diff] [blame] | 6319 | * Check for an assignment at "eap->cmd", compile it if found. |
| 6320 | * Return NOTDONE if there is none, FAIL for failure, OK if done. |
| 6321 | */ |
| 6322 | static int |
| 6323 | may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx) |
| 6324 | { |
| 6325 | char_u *pskip; |
| 6326 | char_u *p; |
| 6327 | |
| 6328 | // Assuming the command starts with a variable or function name, |
| 6329 | // find what follows. |
| 6330 | // Skip over "var.member", "var[idx]" and the like. |
| 6331 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 6332 | pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@') |
| 6333 | ? eap->cmd + 1 : eap->cmd; |
| 6334 | p = to_name_end(pskip, TRUE); |
| 6335 | if (p > eap->cmd && *p != NUL) |
| 6336 | { |
| 6337 | char_u *var_end; |
| 6338 | int oplen; |
| 6339 | int heredoc; |
| 6340 | |
| 6341 | if (eap->cmd[0] == '@') |
| 6342 | var_end = eap->cmd + 2; |
| 6343 | else |
| 6344 | var_end = find_name_end(pskip, NULL, NULL, |
| 6345 | FNE_CHECK_START | FNE_INCL_BR); |
| 6346 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
| 6347 | if (oplen > 0) |
| 6348 | { |
| 6349 | size_t len = p - eap->cmd; |
| 6350 | |
| 6351 | // Recognize an assignment if we recognize the variable |
| 6352 | // name: |
| 6353 | // "g:var = expr" |
| 6354 | // "local = expr" where "local" is a local var. |
| 6355 | // "script = expr" where "script" is a script-local var. |
| 6356 | // "import = expr" where "import" is an imported var |
| 6357 | // "&opt = expr" |
| 6358 | // "$ENV = expr" |
| 6359 | // "@r = expr" |
| 6360 | if (*eap->cmd == '&' |
| 6361 | || *eap->cmd == '$' |
| 6362 | || *eap->cmd == '@' |
| 6363 | || ((len) > 2 && eap->cmd[1] == ':') |
| 6364 | || lookup_local(eap->cmd, len, NULL, cctx) == OK |
| 6365 | || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK |
| 6366 | || script_var_exists(eap->cmd, len, FALSE, cctx) == OK |
| 6367 | || find_imported(eap->cmd, len, cctx) != NULL) |
| 6368 | { |
| 6369 | *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx); |
| 6370 | if (*line == NULL || *line == eap->cmd) |
| 6371 | return FAIL; |
| 6372 | return OK; |
| 6373 | } |
| 6374 | } |
| 6375 | } |
| 6376 | |
| 6377 | if (*eap->cmd == '[') |
| 6378 | { |
| 6379 | // [var, var] = expr |
| 6380 | *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx); |
| 6381 | if (*line == NULL) |
| 6382 | return FAIL; |
| 6383 | if (*line != eap->cmd) |
| 6384 | return OK; |
| 6385 | } |
| 6386 | return NOTDONE; |
| 6387 | } |
| 6388 | |
| 6389 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6390 | * Check if "name" can be "unlet". |
| 6391 | */ |
| 6392 | int |
| 6393 | check_vim9_unlet(char_u *name) |
| 6394 | { |
| 6395 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 6396 | { |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 6397 | // "unlet s:var" is allowed in legacy script. |
| 6398 | if (*name == 's' && !script_is_vim9()) |
| 6399 | return OK; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6400 | semsg(_(e_cannot_unlet_str), name); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6401 | return FAIL; |
| 6402 | } |
| 6403 | return OK; |
| 6404 | } |
| 6405 | |
| 6406 | /* |
| 6407 | * Callback passed to ex_unletlock(). |
| 6408 | */ |
| 6409 | static int |
| 6410 | compile_unlet( |
| 6411 | lval_T *lvp, |
| 6412 | char_u *name_end, |
| 6413 | exarg_T *eap, |
| 6414 | int deep UNUSED, |
| 6415 | void *coookie) |
| 6416 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6417 | cctx_T *cctx = coookie; |
| 6418 | char_u *p = lvp->ll_name; |
| 6419 | int cc = *name_end; |
| 6420 | int ret = OK; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6421 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6422 | if (cctx->ctx_skip == SKIP_YES) |
| 6423 | return OK; |
| 6424 | |
| 6425 | *name_end = NUL; |
| 6426 | if (*p == '$') |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6427 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6428 | // :unlet $ENV_VAR |
| 6429 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 6430 | } |
| 6431 | else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL) |
| 6432 | { |
| 6433 | lhs_T lhs; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6434 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6435 | // This is similar to assigning: lookup the list/dict, compile the |
| 6436 | // idx/key. Then instead of storing the value unlet the item. |
| 6437 | // unlet {list}[idx] |
| 6438 | // unlet {dict}[key] dict.key |
| 6439 | // |
| 6440 | // Figure out the LHS type and other properties. |
| 6441 | // |
| 6442 | ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx); |
| 6443 | |
| 6444 | // : unlet an indexed item |
| 6445 | if (!lhs.lhs_has_index) |
Bram Moolenaar | 2ef951d | 2021-01-03 20:55:26 +0100 | [diff] [blame] | 6446 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6447 | iemsg("called compile_lhs() without an index"); |
| 6448 | ret = FAIL; |
| 6449 | } |
| 6450 | else |
| 6451 | { |
| 6452 | // Use the info in "lhs" to unlet the item at the index in the |
| 6453 | // list or dict. |
| 6454 | ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx); |
Bram Moolenaar | 2ef951d | 2021-01-03 20:55:26 +0100 | [diff] [blame] | 6455 | } |
| 6456 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6457 | vim_free(lhs.lhs_name); |
| 6458 | } |
| 6459 | else if (check_vim9_unlet(p) == FAIL) |
| 6460 | { |
| 6461 | ret = FAIL; |
| 6462 | } |
| 6463 | else |
| 6464 | { |
| 6465 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 6466 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6467 | } |
| 6468 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6469 | *name_end = cc; |
| 6470 | return ret; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6471 | } |
| 6472 | |
| 6473 | /* |
| 6474 | * compile "unlet var", "lock var" and "unlock var" |
| 6475 | * "arg" points to "var". |
| 6476 | */ |
| 6477 | static char_u * |
| 6478 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 6479 | { |
| 6480 | char_u *p = arg; |
| 6481 | |
| 6482 | if (eap->cmdidx != CMD_unlet) |
| 6483 | { |
| 6484 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 6485 | return NULL; |
| 6486 | } |
| 6487 | |
Bram Moolenaar | 2ef951d | 2021-01-03 20:55:26 +0100 | [diff] [blame] | 6488 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING, |
| 6489 | compile_unlet, cctx); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6490 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 6491 | } |
| 6492 | |
| 6493 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6494 | * Compile an :import command. |
| 6495 | */ |
| 6496 | static char_u * |
| 6497 | compile_import(char_u *arg, cctx_T *cctx) |
| 6498 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 6499 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6500 | } |
| 6501 | |
| 6502 | /* |
| 6503 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 6504 | */ |
| 6505 | static int |
| 6506 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 6507 | { |
| 6508 | garray_T *instr = &cctx->ctx_instr; |
| 6509 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 6510 | |
| 6511 | if (endlabel == NULL) |
| 6512 | return FAIL; |
| 6513 | endlabel->el_next = *el; |
| 6514 | *el = endlabel; |
| 6515 | endlabel->el_end_label = instr->ga_len; |
| 6516 | |
| 6517 | generate_JUMP(cctx, when, 0); |
| 6518 | return OK; |
| 6519 | } |
| 6520 | |
| 6521 | static void |
| 6522 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 6523 | { |
| 6524 | garray_T *instr = &cctx->ctx_instr; |
| 6525 | |
| 6526 | while (*el != NULL) |
| 6527 | { |
| 6528 | endlabel_T *cur = (*el); |
| 6529 | isn_T *isn; |
| 6530 | |
| 6531 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 6532 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6533 | *el = cur->el_next; |
| 6534 | vim_free(cur); |
| 6535 | } |
| 6536 | } |
| 6537 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6538 | static void |
| 6539 | compile_free_jump_to_end(endlabel_T **el) |
| 6540 | { |
| 6541 | while (*el != NULL) |
| 6542 | { |
| 6543 | endlabel_T *cur = (*el); |
| 6544 | |
| 6545 | *el = cur->el_next; |
| 6546 | vim_free(cur); |
| 6547 | } |
| 6548 | } |
| 6549 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6550 | /* |
| 6551 | * Create a new scope and set up the generic items. |
| 6552 | */ |
| 6553 | static scope_T * |
| 6554 | new_scope(cctx_T *cctx, scopetype_T type) |
| 6555 | { |
| 6556 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 6557 | |
| 6558 | if (scope == NULL) |
| 6559 | return NULL; |
| 6560 | scope->se_outer = cctx->ctx_scope; |
| 6561 | cctx->ctx_scope = scope; |
| 6562 | scope->se_type = type; |
| 6563 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 6564 | return scope; |
| 6565 | } |
| 6566 | |
| 6567 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6568 | * Free the current scope and go back to the outer scope. |
| 6569 | */ |
| 6570 | static void |
| 6571 | drop_scope(cctx_T *cctx) |
| 6572 | { |
| 6573 | scope_T *scope = cctx->ctx_scope; |
| 6574 | |
| 6575 | if (scope == NULL) |
| 6576 | { |
| 6577 | iemsg("calling drop_scope() without a scope"); |
| 6578 | return; |
| 6579 | } |
| 6580 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6581 | switch (scope->se_type) |
| 6582 | { |
| 6583 | case IF_SCOPE: |
| 6584 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 6585 | case FOR_SCOPE: |
| 6586 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 6587 | case WHILE_SCOPE: |
| 6588 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 6589 | case TRY_SCOPE: |
| 6590 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 6591 | case NO_SCOPE: |
| 6592 | case BLOCK_SCOPE: |
| 6593 | break; |
| 6594 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6595 | vim_free(scope); |
| 6596 | } |
| 6597 | |
| 6598 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6599 | * compile "if expr" |
| 6600 | * |
| 6601 | * "if expr" Produces instructions: |
| 6602 | * EVAL expr Push result of "expr" |
| 6603 | * JUMP_IF_FALSE end |
| 6604 | * ... body ... |
| 6605 | * end: |
| 6606 | * |
| 6607 | * "if expr | else" Produces instructions: |
| 6608 | * EVAL expr Push result of "expr" |
| 6609 | * JUMP_IF_FALSE else |
| 6610 | * ... body ... |
| 6611 | * JUMP_ALWAYS end |
| 6612 | * else: |
| 6613 | * ... body ... |
| 6614 | * end: |
| 6615 | * |
| 6616 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 6617 | * EVAL expr Push result of "expr" |
| 6618 | * JUMP_IF_FALSE elseif |
| 6619 | * ... body ... |
| 6620 | * JUMP_ALWAYS end |
| 6621 | * elseif: |
| 6622 | * EVAL expr Push result of "expr" |
| 6623 | * JUMP_IF_FALSE else |
| 6624 | * ... body ... |
| 6625 | * JUMP_ALWAYS end |
| 6626 | * else: |
| 6627 | * ... body ... |
| 6628 | * end: |
| 6629 | */ |
| 6630 | static char_u * |
| 6631 | compile_if(char_u *arg, cctx_T *cctx) |
| 6632 | { |
| 6633 | char_u *p = arg; |
| 6634 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6635 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6636 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6637 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6638 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6639 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6640 | CLEAR_FIELD(ppconst); |
| 6641 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6642 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6643 | clear_ppconst(&ppconst); |
| 6644 | return NULL; |
| 6645 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6646 | if (cctx->ctx_skip == SKIP_YES) |
| 6647 | clear_ppconst(&ppconst); |
| 6648 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6649 | { |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6650 | int error = FALSE; |
| 6651 | int v; |
| 6652 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6653 | // The expression results in a constant. |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6654 | v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); |
Bram Moolenaar | dda749c | 2020-10-04 17:24:29 +0200 | [diff] [blame] | 6655 | clear_ppconst(&ppconst); |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6656 | if (error) |
| 6657 | return NULL; |
| 6658 | cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6659 | } |
| 6660 | else |
| 6661 | { |
| 6662 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6663 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6664 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6665 | return NULL; |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6666 | if (bool_on_stack(cctx) == FAIL) |
| 6667 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6668 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6669 | |
| 6670 | scope = new_scope(cctx, IF_SCOPE); |
| 6671 | if (scope == NULL) |
| 6672 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6673 | scope->se_skip_save = skip_save; |
| 6674 | // "is_had_return" will be reset if any block does not end in :return |
| 6675 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6676 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6677 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6678 | { |
| 6679 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6680 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6681 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6682 | } |
| 6683 | else |
| 6684 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6685 | |
| 6686 | return p; |
| 6687 | } |
| 6688 | |
| 6689 | static char_u * |
| 6690 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 6691 | { |
| 6692 | char_u *p = arg; |
| 6693 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6694 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6695 | isn_T *isn; |
| 6696 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6697 | ppconst_T ppconst; |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 6698 | skip_T save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6699 | |
| 6700 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6701 | { |
| 6702 | emsg(_(e_elseif_without_if)); |
| 6703 | return NULL; |
| 6704 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6705 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6706 | if (!cctx->ctx_had_return) |
| 6707 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6708 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6709 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6710 | { |
| 6711 | 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] | 6712 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6713 | return NULL; |
| 6714 | // previous "if" or "elseif" jumps here |
| 6715 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6716 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6717 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6718 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6719 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6720 | CLEAR_FIELD(ppconst); |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 6721 | if (cctx->ctx_skip == SKIP_YES) |
| 6722 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6723 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6724 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6725 | clear_ppconst(&ppconst); |
| 6726 | return NULL; |
| 6727 | } |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 6728 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6729 | if (scope->se_skip_save == SKIP_YES) |
| 6730 | clear_ppconst(&ppconst); |
| 6731 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6732 | { |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6733 | int error = FALSE; |
| 6734 | int v; |
| 6735 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6736 | // The expression results in a constant. |
| 6737 | // TODO: how about nesting? |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6738 | v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); |
| 6739 | if (error) |
| 6740 | return NULL; |
| 6741 | cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6742 | clear_ppconst(&ppconst); |
| 6743 | scope->se_u.se_if.is_if_label = -1; |
| 6744 | } |
| 6745 | else |
| 6746 | { |
| 6747 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6748 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6749 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6750 | return NULL; |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6751 | if (bool_on_stack(cctx) == FAIL) |
| 6752 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6753 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6754 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6755 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6756 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6757 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6758 | |
| 6759 | return p; |
| 6760 | } |
| 6761 | |
| 6762 | static char_u * |
| 6763 | compile_else(char_u *arg, cctx_T *cctx) |
| 6764 | { |
| 6765 | char_u *p = arg; |
| 6766 | garray_T *instr = &cctx->ctx_instr; |
| 6767 | isn_T *isn; |
| 6768 | scope_T *scope = cctx->ctx_scope; |
| 6769 | |
| 6770 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6771 | { |
| 6772 | emsg(_(e_else_without_if)); |
| 6773 | return NULL; |
| 6774 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6775 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6776 | if (!cctx->ctx_had_return) |
| 6777 | scope->se_u.se_if.is_had_return = FALSE; |
| 6778 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6779 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6780 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6781 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6782 | // 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] | 6783 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6784 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6785 | if (!cctx->ctx_had_return |
| 6786 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6787 | JUMP_ALWAYS, cctx) == FAIL) |
| 6788 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6789 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6790 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6791 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6792 | { |
| 6793 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6794 | { |
| 6795 | // previous "if" or "elseif" jumps here |
| 6796 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6797 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6798 | scope->se_u.se_if.is_if_label = -1; |
| 6799 | } |
| 6800 | } |
| 6801 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6802 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6803 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6804 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6805 | |
| 6806 | return p; |
| 6807 | } |
| 6808 | |
| 6809 | static char_u * |
| 6810 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6811 | { |
| 6812 | scope_T *scope = cctx->ctx_scope; |
| 6813 | ifscope_T *ifscope; |
| 6814 | garray_T *instr = &cctx->ctx_instr; |
| 6815 | isn_T *isn; |
| 6816 | |
| 6817 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6818 | { |
| 6819 | emsg(_(e_endif_without_if)); |
| 6820 | return NULL; |
| 6821 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6822 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6823 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6824 | if (!cctx->ctx_had_return) |
| 6825 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6826 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6827 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6828 | { |
| 6829 | // previous "if" or "elseif" jumps here |
| 6830 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6831 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6832 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6833 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6834 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6835 | cctx->ctx_skip = scope->se_skip_save; |
| 6836 | |
| 6837 | // If all the blocks end in :return and there is an :else then the |
| 6838 | // had_return flag is set. |
| 6839 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6840 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6841 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6842 | return arg; |
| 6843 | } |
| 6844 | |
| 6845 | /* |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6846 | * Compile "for var in expr": |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6847 | * |
| 6848 | * Produces instructions: |
| 6849 | * PUSHNR -1 |
| 6850 | * STORE loop-idx Set index to -1 |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6851 | * EVAL expr result of "expr" on top of stack |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6852 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6853 | * - if beyond end, jump to "end" |
| 6854 | * - otherwise get item from list and push it |
| 6855 | * STORE var Store item in "var" |
| 6856 | * ... body ... |
| 6857 | * JUMP top Jump back to repeat |
| 6858 | * end: DROP Drop the result of "expr" |
| 6859 | * |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6860 | * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var": |
| 6861 | * UNPACK 2 Split item in 2 |
| 6862 | * STORE var1 Store item in "var1" |
| 6863 | * STORE var2 Store item in "var2" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6864 | */ |
| 6865 | static char_u * |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6866 | compile_for(char_u *arg_start, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6867 | { |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6868 | char_u *arg; |
| 6869 | char_u *arg_end; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6870 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6871 | char_u *p; |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6872 | char_u *wp; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6873 | int var_count = 0; |
| 6874 | int semicolon = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6875 | size_t varlen; |
| 6876 | garray_T *instr = &cctx->ctx_instr; |
| 6877 | garray_T *stack = &cctx->ctx_type_stack; |
| 6878 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6879 | lvar_T *loop_lvar; // loop iteration variable |
| 6880 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6881 | type_T *vartype; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6882 | type_T *item_type = &t_any; |
| 6883 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6884 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6885 | p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE); |
Bram Moolenaar | 036d071 | 2021-01-17 20:23:38 +0100 | [diff] [blame] | 6886 | if (p == NULL) |
| 6887 | return NULL; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6888 | if (var_count == 0) |
| 6889 | var_count = 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6890 | |
| 6891 | // consume "in" |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6892 | wp = p; |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6893 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
| 6894 | return NULL; |
| 6895 | if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6896 | { |
| 6897 | emsg(_(e_missing_in)); |
| 6898 | return NULL; |
| 6899 | } |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6900 | wp = p + 2; |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6901 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
| 6902 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6903 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6904 | scope = new_scope(cctx, FOR_SCOPE); |
| 6905 | if (scope == NULL) |
| 6906 | return NULL; |
| 6907 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6908 | // Reserve a variable to store the loop iteration counter and initialize it |
| 6909 | // to -1. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6910 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6911 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6912 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6913 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6914 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6915 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6916 | } |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6917 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6918 | |
| 6919 | // compile "expr", it remains on the stack until "endfor" |
| 6920 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6921 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6922 | { |
| 6923 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6924 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6925 | } |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6926 | arg_end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6927 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6928 | // Now that we know the type of "var", check that it is a list, now or at |
| 6929 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6930 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 351ead0 | 2021-01-16 16:07:01 +0100 | [diff] [blame] | 6931 | if (need_type(vartype, &t_list_any, -1, 0, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6932 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6933 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6934 | return NULL; |
| 6935 | } |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6936 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6937 | if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY) |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6938 | { |
| 6939 | if (var_count == 1) |
| 6940 | item_type = vartype->tt_member; |
| 6941 | else if (vartype->tt_member->tt_type == VAR_LIST |
| 6942 | && vartype->tt_member->tt_member->tt_type != VAR_ANY) |
| 6943 | item_type = vartype->tt_member->tt_member; |
| 6944 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6945 | |
| 6946 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6947 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6948 | generate_FOR(cctx, loop_lvar->lv_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6949 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6950 | arg = arg_start; |
| 6951 | if (var_count > 1) |
| 6952 | { |
| 6953 | generate_UNPACK(cctx, var_count, semicolon); |
| 6954 | arg = skipwhite(arg + 1); // skip white after '[' |
| 6955 | |
| 6956 | // the list item is replaced by a number of items |
| 6957 | if (ga_grow(stack, var_count - 1) == FAIL) |
| 6958 | { |
| 6959 | drop_scope(cctx); |
| 6960 | return NULL; |
| 6961 | } |
| 6962 | --stack->ga_len; |
| 6963 | for (idx = 0; idx < var_count; ++idx) |
| 6964 | { |
| 6965 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 6966 | (semicolon && idx == 0) ? vartype : item_type; |
| 6967 | ++stack->ga_len; |
| 6968 | } |
| 6969 | } |
| 6970 | |
| 6971 | for (idx = 0; idx < var_count; ++idx) |
| 6972 | { |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6973 | assign_dest_T dest = dest_local; |
| 6974 | int opt_flags = 0; |
| 6975 | int vimvaridx = -1; |
| 6976 | type_T *type = &t_any; |
| 6977 | |
| 6978 | p = skip_var_one(arg, FALSE); |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6979 | varlen = p - arg; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6980 | name = vim_strnsave(arg, varlen); |
| 6981 | if (name == NULL) |
| 6982 | goto failed; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6983 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6984 | // TODO: script var not supported? |
| 6985 | if (get_var_dest(name, &dest, CMD_for, &opt_flags, |
| 6986 | &vimvaridx, &type, cctx) == FAIL) |
| 6987 | goto failed; |
| 6988 | if (dest != dest_local) |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6989 | { |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6990 | if (generate_store_var(cctx, dest, opt_flags, vimvaridx, |
| 6991 | 0, 0, type, name) == FAIL) |
| 6992 | goto failed; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6993 | } |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6994 | else |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6995 | { |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 6996 | if (lookup_local(arg, varlen, NULL, cctx) == OK) |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6997 | { |
| 6998 | semsg(_(e_variable_already_declared), arg); |
| 6999 | goto failed; |
| 7000 | } |
| 7001 | |
Bram Moolenaar | ea87069 | 2020-12-02 14:24:30 +0100 | [diff] [blame] | 7002 | if (STRNCMP(name, "s:", 2) == 0) |
| 7003 | { |
| 7004 | semsg(_(e_cannot_declare_script_variable_in_function), name); |
| 7005 | goto failed; |
| 7006 | } |
| 7007 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 7008 | // Reserve a variable to store "var". |
| 7009 | // TODO: check for type |
| 7010 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 7011 | if (var_lvar == NULL) |
| 7012 | // out of memory or used as an argument |
| 7013 | goto failed; |
| 7014 | |
| 7015 | if (semicolon && idx == var_count - 1) |
| 7016 | var_lvar->lv_type = vartype; |
| 7017 | else |
| 7018 | var_lvar->lv_type = item_type; |
| 7019 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
| 7020 | } |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 7021 | |
Bram Moolenaar | 036d071 | 2021-01-17 20:23:38 +0100 | [diff] [blame] | 7022 | if (*p == ':') |
| 7023 | p = skip_type(skipwhite(p + 1), FALSE); |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 7024 | if (*p == ',' || *p == ';') |
| 7025 | ++p; |
| 7026 | arg = skipwhite(p); |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 7027 | vim_free(name); |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 7028 | } |
| 7029 | |
| 7030 | return arg_end; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 7031 | |
| 7032 | failed: |
| 7033 | vim_free(name); |
| 7034 | drop_scope(cctx); |
| 7035 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7036 | } |
| 7037 | |
| 7038 | /* |
| 7039 | * compile "endfor" |
| 7040 | */ |
| 7041 | static char_u * |
| 7042 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 7043 | { |
| 7044 | garray_T *instr = &cctx->ctx_instr; |
| 7045 | scope_T *scope = cctx->ctx_scope; |
| 7046 | forscope_T *forscope; |
| 7047 | isn_T *isn; |
| 7048 | |
| 7049 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 7050 | { |
| 7051 | emsg(_(e_for)); |
| 7052 | return NULL; |
| 7053 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7054 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7055 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7056 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7057 | |
| 7058 | // At end of ":for" scope jump back to the FOR instruction. |
| 7059 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 7060 | |
| 7061 | // Fill in the "end" label in the FOR statement so it can jump here |
| 7062 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 7063 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 7064 | |
| 7065 | // Fill in the "end" label any BREAK statements |
| 7066 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 7067 | |
| 7068 | // Below the ":for" scope drop the "expr" list from the stack. |
| 7069 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 7070 | return NULL; |
| 7071 | |
| 7072 | vim_free(scope); |
| 7073 | |
| 7074 | return arg; |
| 7075 | } |
| 7076 | |
| 7077 | /* |
| 7078 | * compile "while expr" |
| 7079 | * |
| 7080 | * Produces instructions: |
| 7081 | * top: EVAL expr Push result of "expr" |
| 7082 | * JUMP_IF_FALSE end jump if false |
| 7083 | * ... body ... |
| 7084 | * JUMP top Jump back to repeat |
| 7085 | * end: |
| 7086 | * |
| 7087 | */ |
| 7088 | static char_u * |
| 7089 | compile_while(char_u *arg, cctx_T *cctx) |
| 7090 | { |
| 7091 | char_u *p = arg; |
| 7092 | garray_T *instr = &cctx->ctx_instr; |
| 7093 | scope_T *scope; |
| 7094 | |
| 7095 | scope = new_scope(cctx, WHILE_SCOPE); |
| 7096 | if (scope == NULL) |
| 7097 | return NULL; |
| 7098 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7099 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7100 | |
| 7101 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7102 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7103 | return NULL; |
| 7104 | |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 7105 | if (bool_on_stack(cctx) == FAIL) |
| 7106 | return FAIL; |
| 7107 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7108 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7109 | 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] | 7110 | JUMP_IF_FALSE, cctx) == FAIL) |
| 7111 | return FAIL; |
| 7112 | |
| 7113 | return p; |
| 7114 | } |
| 7115 | |
| 7116 | /* |
| 7117 | * compile "endwhile" |
| 7118 | */ |
| 7119 | static char_u * |
| 7120 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 7121 | { |
| 7122 | scope_T *scope = cctx->ctx_scope; |
| 7123 | |
| 7124 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 7125 | { |
| 7126 | emsg(_(e_while)); |
| 7127 | return NULL; |
| 7128 | } |
| 7129 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7130 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7131 | |
| 7132 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7133 | 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] | 7134 | |
| 7135 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 7136 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7137 | 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] | 7138 | |
| 7139 | vim_free(scope); |
| 7140 | |
| 7141 | return arg; |
| 7142 | } |
| 7143 | |
| 7144 | /* |
| 7145 | * compile "continue" |
| 7146 | */ |
| 7147 | static char_u * |
| 7148 | compile_continue(char_u *arg, cctx_T *cctx) |
| 7149 | { |
| 7150 | scope_T *scope = cctx->ctx_scope; |
| 7151 | |
| 7152 | for (;;) |
| 7153 | { |
| 7154 | if (scope == NULL) |
| 7155 | { |
| 7156 | emsg(_(e_continue)); |
| 7157 | return NULL; |
| 7158 | } |
| 7159 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 7160 | break; |
| 7161 | scope = scope->se_outer; |
| 7162 | } |
| 7163 | |
| 7164 | // Jump back to the FOR or WHILE instruction. |
| 7165 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7166 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 7167 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7168 | return arg; |
| 7169 | } |
| 7170 | |
| 7171 | /* |
| 7172 | * compile "break" |
| 7173 | */ |
| 7174 | static char_u * |
| 7175 | compile_break(char_u *arg, cctx_T *cctx) |
| 7176 | { |
| 7177 | scope_T *scope = cctx->ctx_scope; |
| 7178 | endlabel_T **el; |
| 7179 | |
| 7180 | for (;;) |
| 7181 | { |
| 7182 | if (scope == NULL) |
| 7183 | { |
| 7184 | emsg(_(e_break)); |
| 7185 | return NULL; |
| 7186 | } |
| 7187 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 7188 | break; |
| 7189 | scope = scope->se_outer; |
| 7190 | } |
| 7191 | |
| 7192 | // Jump to the end of the FOR or WHILE loop. |
| 7193 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7194 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7195 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7196 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7197 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 7198 | return FAIL; |
| 7199 | |
| 7200 | return arg; |
| 7201 | } |
| 7202 | |
| 7203 | /* |
| 7204 | * compile "{" start of block |
| 7205 | */ |
| 7206 | static char_u * |
| 7207 | compile_block(char_u *arg, cctx_T *cctx) |
| 7208 | { |
| 7209 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 7210 | return NULL; |
| 7211 | return skipwhite(arg + 1); |
| 7212 | } |
| 7213 | |
| 7214 | /* |
| 7215 | * compile end of block: drop one scope |
| 7216 | */ |
| 7217 | static void |
| 7218 | compile_endblock(cctx_T *cctx) |
| 7219 | { |
| 7220 | scope_T *scope = cctx->ctx_scope; |
| 7221 | |
| 7222 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7223 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7224 | vim_free(scope); |
| 7225 | } |
| 7226 | |
| 7227 | /* |
| 7228 | * compile "try" |
| 7229 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 7230 | * finally. |
| 7231 | * Creates another scope for the "try" block itself. |
| 7232 | * TRY instruction sets up exception handling at runtime. |
| 7233 | * |
| 7234 | * "try" |
| 7235 | * TRY -> catch1, -> finally push trystack entry |
| 7236 | * ... try block |
| 7237 | * "throw {exception}" |
| 7238 | * EVAL {exception} |
| 7239 | * THROW create exception |
| 7240 | * ... try block |
| 7241 | * " catch {expr}" |
| 7242 | * JUMP -> finally |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 7243 | * catch1: PUSH exception |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7244 | * EVAL {expr} |
| 7245 | * MATCH |
| 7246 | * JUMP nomatch -> catch2 |
| 7247 | * CATCH remove exception |
| 7248 | * ... catch block |
| 7249 | * " catch" |
| 7250 | * JUMP -> finally |
| 7251 | * catch2: CATCH remove exception |
| 7252 | * ... catch block |
| 7253 | * " finally" |
| 7254 | * finally: |
| 7255 | * ... finally block |
| 7256 | * " endtry" |
| 7257 | * ENDTRY pop trystack entry, may rethrow |
| 7258 | */ |
| 7259 | static char_u * |
| 7260 | compile_try(char_u *arg, cctx_T *cctx) |
| 7261 | { |
| 7262 | garray_T *instr = &cctx->ctx_instr; |
| 7263 | scope_T *try_scope; |
| 7264 | scope_T *scope; |
| 7265 | |
| 7266 | // scope that holds the jumps that go to catch/finally/endtry |
| 7267 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 7268 | if (try_scope == NULL) |
| 7269 | return NULL; |
| 7270 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7271 | if (cctx->ctx_skip != SKIP_YES) |
| 7272 | { |
| 7273 | // "catch" is set when the first ":catch" is found. |
| 7274 | // "finally" is set when ":finally" or ":endtry" is found |
| 7275 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
| 7276 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 7277 | return NULL; |
| 7278 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7279 | |
| 7280 | // scope for the try block itself |
| 7281 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 7282 | if (scope == NULL) |
| 7283 | return NULL; |
| 7284 | |
| 7285 | return arg; |
| 7286 | } |
| 7287 | |
| 7288 | /* |
| 7289 | * compile "catch {expr}" |
| 7290 | */ |
| 7291 | static char_u * |
| 7292 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 7293 | { |
| 7294 | scope_T *scope = cctx->ctx_scope; |
| 7295 | garray_T *instr = &cctx->ctx_instr; |
| 7296 | char_u *p; |
| 7297 | isn_T *isn; |
| 7298 | |
| 7299 | // end block scope from :try or :catch |
| 7300 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 7301 | compile_endblock(cctx); |
| 7302 | scope = cctx->ctx_scope; |
| 7303 | |
| 7304 | // Error if not in a :try scope |
| 7305 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 7306 | { |
| 7307 | emsg(_(e_catch)); |
| 7308 | return NULL; |
| 7309 | } |
| 7310 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7311 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7312 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7313 | emsg(_(e_catch_unreachable_after_catch_all)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7314 | return NULL; |
| 7315 | } |
| 7316 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7317 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7318 | { |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7319 | // Jump from end of previous block to :finally or :endtry |
| 7320 | if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label, |
| 7321 | JUMP_ALWAYS, cctx) == FAIL) |
| 7322 | return NULL; |
| 7323 | |
| 7324 | // End :try or :catch scope: set value in ISN_TRY instruction |
| 7325 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
| 7326 | if (isn->isn_arg.try.try_catch == 0) |
| 7327 | isn->isn_arg.try.try_catch = instr->ga_len; |
| 7328 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 7329 | { |
| 7330 | // Previous catch without match jumps here |
| 7331 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 7332 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 7333 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7334 | } |
| 7335 | |
| 7336 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7337 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7338 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7339 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 7340 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7341 | } |
| 7342 | else |
| 7343 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7344 | char_u *end; |
| 7345 | char_u *pat; |
| 7346 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 7347 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 7348 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7349 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7350 | // Push v:exception, push {expr} and MATCH |
| 7351 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 7352 | |
Bram Moolenaar | d93a7fc | 2021-01-04 12:42:13 +0100 | [diff] [blame] | 7353 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7354 | if (*end != *p) |
| 7355 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 7356 | semsg(_(e_separator_mismatch_str), p); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7357 | vim_free(tofree); |
| 7358 | return FAIL; |
| 7359 | } |
| 7360 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 7361 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7362 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 7363 | len = (int)(end - tofree); |
| 7364 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7365 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 7366 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7367 | if (pat == NULL) |
| 7368 | return FAIL; |
| 7369 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 7370 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7371 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7372 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 7373 | return NULL; |
| 7374 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7375 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7376 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 7377 | return NULL; |
| 7378 | } |
| 7379 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7380 | if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7381 | return NULL; |
| 7382 | |
| 7383 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 7384 | return NULL; |
| 7385 | return p; |
| 7386 | } |
| 7387 | |
| 7388 | static char_u * |
| 7389 | compile_finally(char_u *arg, cctx_T *cctx) |
| 7390 | { |
| 7391 | scope_T *scope = cctx->ctx_scope; |
| 7392 | garray_T *instr = &cctx->ctx_instr; |
| 7393 | isn_T *isn; |
| 7394 | |
| 7395 | // end block scope from :try or :catch |
| 7396 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 7397 | compile_endblock(cctx); |
| 7398 | scope = cctx->ctx_scope; |
| 7399 | |
| 7400 | // Error if not in a :try scope |
| 7401 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 7402 | { |
| 7403 | emsg(_(e_finally)); |
| 7404 | return NULL; |
| 7405 | } |
| 7406 | |
| 7407 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7408 | 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] | 7409 | if (isn->isn_arg.try.try_finally != 0) |
| 7410 | { |
| 7411 | emsg(_(e_finally_dup)); |
| 7412 | return NULL; |
| 7413 | } |
| 7414 | |
| 7415 | // 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] | 7416 | 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] | 7417 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7418 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7419 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7420 | { |
| 7421 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7422 | 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] | 7423 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 7424 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7425 | } |
| 7426 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7427 | // TODO: set index in ts_finally_label jumps |
| 7428 | |
| 7429 | return arg; |
| 7430 | } |
| 7431 | |
| 7432 | static char_u * |
| 7433 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 7434 | { |
| 7435 | scope_T *scope = cctx->ctx_scope; |
| 7436 | garray_T *instr = &cctx->ctx_instr; |
| 7437 | isn_T *isn; |
| 7438 | |
| 7439 | // end block scope from :catch or :finally |
| 7440 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 7441 | compile_endblock(cctx); |
| 7442 | scope = cctx->ctx_scope; |
| 7443 | |
| 7444 | // Error if not in a :try scope |
| 7445 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 7446 | { |
| 7447 | if (scope == NULL) |
| 7448 | emsg(_(e_no_endtry)); |
| 7449 | else if (scope->se_type == WHILE_SCOPE) |
| 7450 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 7451 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7452 | emsg(_(e_endfor)); |
| 7453 | else |
| 7454 | emsg(_(e_endif)); |
| 7455 | return NULL; |
| 7456 | } |
| 7457 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7458 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7459 | { |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7460 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
| 7461 | if (isn->isn_arg.try.try_catch == 0 |
| 7462 | && isn->isn_arg.try.try_finally == 0) |
| 7463 | { |
| 7464 | emsg(_(e_missing_catch_or_finally)); |
| 7465 | return NULL; |
| 7466 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7467 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7468 | // Fill in the "end" label in jumps at the end of the blocks, if not |
| 7469 | // done by ":finally". |
| 7470 | 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] | 7471 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7472 | // End :catch or :finally scope: set value in ISN_TRY instruction |
| 7473 | if (isn->isn_arg.try.try_catch == 0) |
| 7474 | isn->isn_arg.try.try_catch = instr->ga_len; |
| 7475 | if (isn->isn_arg.try.try_finally == 0) |
| 7476 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 7477 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7478 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 7479 | { |
| 7480 | // Last catch without match jumps here |
| 7481 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 7482 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 7483 | } |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 7484 | } |
| 7485 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7486 | compile_endblock(cctx); |
| 7487 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7488 | if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7489 | return NULL; |
| 7490 | return arg; |
| 7491 | } |
| 7492 | |
| 7493 | /* |
| 7494 | * compile "throw {expr}" |
| 7495 | */ |
| 7496 | static char_u * |
| 7497 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 7498 | { |
| 7499 | char_u *p = skipwhite(arg); |
| 7500 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7501 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7502 | return NULL; |
Bram Moolenaar | 9e1d9e3 | 2021-01-11 20:17:34 +0100 | [diff] [blame] | 7503 | if (cctx->ctx_skip == SKIP_YES) |
| 7504 | return p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7505 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 7506 | return NULL; |
| 7507 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 7508 | return NULL; |
| 7509 | |
| 7510 | return p; |
| 7511 | } |
| 7512 | |
| 7513 | /* |
| 7514 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7515 | * compile "echomsg expr" |
| 7516 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7517 | * compile "execute expr" |
| 7518 | */ |
| 7519 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7520 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7521 | { |
| 7522 | char_u *p = arg; |
Bram Moolenaar | e498429 | 2020-12-13 14:19:25 +0100 | [diff] [blame] | 7523 | char_u *prev = arg; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7524 | int count = 0; |
| 7525 | |
| 7526 | for (;;) |
| 7527 | { |
Bram Moolenaar | e498429 | 2020-12-13 14:19:25 +0100 | [diff] [blame] | 7528 | if (ends_excmd2(prev, p)) |
| 7529 | break; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7530 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7531 | return NULL; |
| 7532 | ++count; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 7533 | prev = p; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7534 | p = skipwhite(p); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7535 | } |
| 7536 | |
Bram Moolenaar | e498429 | 2020-12-13 14:19:25 +0100 | [diff] [blame] | 7537 | if (count > 0) |
| 7538 | { |
| 7539 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 7540 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 7541 | else if (cmdidx == CMD_execute) |
| 7542 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 7543 | else if (cmdidx == CMD_echomsg) |
| 7544 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 7545 | else |
| 7546 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
| 7547 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7548 | return p; |
| 7549 | } |
| 7550 | |
| 7551 | /* |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 7552 | * If "eap" has a range that is not a constant generate an ISN_RANGE |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 7553 | * instruction to compute it and return OK. |
| 7554 | * Otherwise return FAIL, the caller must deal with any range. |
| 7555 | */ |
| 7556 | static int |
| 7557 | compile_variable_range(exarg_T *eap, cctx_T *cctx) |
| 7558 | { |
| 7559 | char_u *range_end = skip_range(eap->cmd, TRUE, NULL); |
| 7560 | char_u *p = skipdigits(eap->cmd); |
| 7561 | |
| 7562 | if (p == range_end) |
| 7563 | return FAIL; |
| 7564 | return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd)); |
| 7565 | } |
| 7566 | |
| 7567 | /* |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7568 | * :put r |
| 7569 | * :put ={expr} |
| 7570 | */ |
| 7571 | static char_u * |
| 7572 | compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 7573 | { |
| 7574 | char_u *line = arg; |
| 7575 | linenr_T lnum; |
| 7576 | char *errormsg; |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 7577 | int above = eap->forceit; |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7578 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7579 | eap->regname = *line; |
| 7580 | |
| 7581 | if (eap->regname == '=') |
| 7582 | { |
| 7583 | char_u *p = line + 1; |
| 7584 | |
| 7585 | if (compile_expr0(&p, cctx) == FAIL) |
| 7586 | return NULL; |
| 7587 | line = p; |
| 7588 | } |
| 7589 | else if (eap->regname != NUL) |
| 7590 | ++line; |
| 7591 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 7592 | if (compile_variable_range(eap, cctx) == OK) |
| 7593 | { |
| 7594 | lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE; |
| 7595 | } |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7596 | else |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 7597 | { |
| 7598 | // Either no range or a number. |
| 7599 | // "errormsg" will not be set because the range is ADDR_LINES. |
| 7600 | if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL) |
Bram Moolenaar | 399ea81 | 2020-12-15 21:28:57 +0100 | [diff] [blame] | 7601 | // cannot happen |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 7602 | return NULL; |
| 7603 | if (eap->addr_count == 0) |
| 7604 | lnum = -1; |
| 7605 | else |
| 7606 | lnum = eap->line2; |
| 7607 | if (above) |
| 7608 | --lnum; |
| 7609 | } |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7610 | |
| 7611 | generate_PUT(cctx, eap->regname, lnum); |
| 7612 | return line; |
| 7613 | } |
| 7614 | |
| 7615 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7616 | * A command that is not compiled, execute with legacy code. |
| 7617 | */ |
| 7618 | static char_u * |
| 7619 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 7620 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 7621 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 7622 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7623 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7624 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7625 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7626 | goto theend; |
| 7627 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 7628 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7629 | { |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 7630 | long argt = eap->argt; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7631 | int usefilter = FALSE; |
| 7632 | |
| 7633 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 7634 | |
| 7635 | // If the command can be followed by a bar, find the bar and truncate |
| 7636 | // it, so that the following command can be compiled. |
| 7637 | // The '|' is overwritten with a NUL, it is put back below. |
| 7638 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 7639 | && *eap->arg == '!') |
| 7640 | // :w !filter or :r !filter or :r! filter |
| 7641 | usefilter = TRUE; |
| 7642 | if ((argt & EX_TRLBAR) && !usefilter) |
| 7643 | { |
Bram Moolenaar | b8a9296 | 2020-08-20 18:02:47 +0200 | [diff] [blame] | 7644 | eap->argt = argt; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7645 | separate_nextcmd(eap); |
| 7646 | if (eap->nextcmd != NULL) |
| 7647 | nextcmd = eap->nextcmd; |
| 7648 | } |
Bram Moolenaar | a11919f | 2021-01-02 19:44:56 +0100 | [diff] [blame] | 7649 | else if (eap->cmdidx == CMD_wincmd) |
| 7650 | { |
| 7651 | p = eap->arg; |
| 7652 | if (*p != NUL) |
| 7653 | ++p; |
| 7654 | if (*p == 'g' || *p == Ctrl_G) |
| 7655 | ++p; |
| 7656 | p = skipwhite(p); |
| 7657 | if (*p == '|') |
| 7658 | { |
| 7659 | *p = NUL; |
| 7660 | nextcmd = p + 1; |
| 7661 | } |
| 7662 | } |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7663 | } |
| 7664 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 7665 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 7666 | { |
| 7667 | // expand filename in "syntax include [@group] filename" |
| 7668 | has_expr = TRUE; |
| 7669 | eap->arg = skipwhite(eap->arg + 7); |
| 7670 | if (*eap->arg == '@') |
| 7671 | eap->arg = skiptowhite(eap->arg); |
| 7672 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7673 | |
Bram Moolenaar | 56ce9ea | 2020-12-25 18:35:29 +0100 | [diff] [blame] | 7674 | if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal) |
| 7675 | && STRLEN(eap->arg) > 4) |
| 7676 | { |
| 7677 | int delim = *eap->arg; |
| 7678 | |
Bram Moolenaar | d93a7fc | 2021-01-04 12:42:13 +0100 | [diff] [blame] | 7679 | p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL); |
Bram Moolenaar | 56ce9ea | 2020-12-25 18:35:29 +0100 | [diff] [blame] | 7680 | if (*p == delim) |
| 7681 | { |
| 7682 | eap->arg = p + 1; |
| 7683 | has_expr = TRUE; |
| 7684 | } |
| 7685 | } |
| 7686 | |
Bram Moolenaar | ecac591 | 2021-01-05 19:23:28 +0100 | [diff] [blame] | 7687 | if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed) |
| 7688 | { |
| 7689 | // TODO: should only expand when appropriate for the command |
| 7690 | eap->arg = skiptowhite(eap->arg); |
| 7691 | has_expr = TRUE; |
| 7692 | } |
| 7693 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 7694 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7695 | { |
| 7696 | int count = 0; |
| 7697 | char_u *start = skipwhite(line); |
| 7698 | |
| 7699 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 7700 | // PUSHS ":cmd xxx" |
| 7701 | // eval expr1 |
| 7702 | // PUSHS "yyy" |
| 7703 | // eval expr2 |
| 7704 | // PUSHS "zzz" |
| 7705 | // EXECCONCAT 5 |
| 7706 | for (;;) |
| 7707 | { |
| 7708 | if (p > start) |
| 7709 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 7710 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7711 | ++count; |
| 7712 | } |
| 7713 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7714 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7715 | return NULL; |
| 7716 | may_generate_2STRING(-1, cctx); |
| 7717 | ++count; |
| 7718 | p = skipwhite(p); |
| 7719 | if (*p != '`') |
| 7720 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7721 | emsg(_(e_missing_backtick)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7722 | return NULL; |
| 7723 | } |
| 7724 | start = p + 1; |
| 7725 | |
| 7726 | p = (char_u *)strstr((char *)start, "`="); |
| 7727 | if (p == NULL) |
| 7728 | { |
| 7729 | if (*skipwhite(start) != NUL) |
| 7730 | { |
| 7731 | generate_PUSHS(cctx, vim_strsave(start)); |
| 7732 | ++count; |
| 7733 | } |
| 7734 | break; |
| 7735 | } |
| 7736 | } |
| 7737 | generate_EXECCONCAT(cctx, count); |
| 7738 | } |
| 7739 | else |
| 7740 | generate_EXEC(cctx, line); |
| 7741 | |
| 7742 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7743 | if (*nextcmd != NUL) |
| 7744 | { |
| 7745 | // the parser expects a pointer to the bar, put it back |
| 7746 | --nextcmd; |
| 7747 | *nextcmd = '|'; |
| 7748 | } |
| 7749 | |
| 7750 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7751 | } |
| 7752 | |
| 7753 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7754 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7755 | * 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] | 7756 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7757 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7758 | add_def_function(ufunc_T *ufunc) |
| 7759 | { |
| 7760 | dfunc_T *dfunc; |
| 7761 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7762 | if (def_functions.ga_len == 0) |
| 7763 | { |
| 7764 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 7765 | // wasn't set. |
| 7766 | if (ga_grow(&def_functions, 1) == FAIL) |
| 7767 | return FAIL; |
| 7768 | ++def_functions.ga_len; |
| 7769 | } |
| 7770 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7771 | // Add the function to "def_functions". |
| 7772 | if (ga_grow(&def_functions, 1) == FAIL) |
| 7773 | return FAIL; |
| 7774 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 7775 | CLEAR_POINTER(dfunc); |
| 7776 | dfunc->df_idx = def_functions.ga_len; |
| 7777 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 7778 | dfunc->df_ufunc = ufunc; |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 7779 | dfunc->df_name = vim_strsave(ufunc->uf_name); |
| 7780 | ++dfunc->df_refcount; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7781 | ++def_functions.ga_len; |
| 7782 | return OK; |
| 7783 | } |
| 7784 | |
| 7785 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7786 | * After ex_function() has collected all the function lines: parse and compile |
| 7787 | * the lines into instructions. |
| 7788 | * Adds the function to "def_functions". |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 7789 | * When "check_return_type" is set then set ufunc->uf_ret_type to the type of |
| 7790 | * the return statement (used for lambda). When uf_ret_type is already set |
| 7791 | * then check that it matches. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7792 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7793 | * This can be used recursively through compile_lambda(), which may reallocate |
| 7794 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7795 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7796 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7797 | int |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 7798 | compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7799 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7800 | char_u *line = NULL; |
| 7801 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7802 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7803 | cctx_T cctx; |
| 7804 | garray_T *instr; |
Bram Moolenaar | d66960b | 2020-10-30 20:46:26 +0100 | [diff] [blame] | 7805 | int did_emsg_before = did_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7806 | int ret = FAIL; |
| 7807 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | f4e8cdd | 2020-10-12 22:07:13 +0200 | [diff] [blame] | 7808 | int save_estack_compiling = estack_compiling; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7809 | int do_estack_push; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7810 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7811 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7812 | // When using a function that was compiled before: Free old instructions. |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 7813 | // The index is reused. Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7814 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7815 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7816 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7817 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 7818 | delete_def_function_contents(dfunc, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7819 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7820 | else |
| 7821 | { |
| 7822 | if (add_def_function(ufunc) == FAIL) |
| 7823 | return FAIL; |
| 7824 | new_def_function = TRUE; |
| 7825 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7826 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 7827 | ufunc->uf_def_status = UF_COMPILING; |
| 7828 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7829 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7830 | cctx.ctx_ufunc = ufunc; |
| 7831 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7832 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7833 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 7834 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 7835 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 7836 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 7837 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 7838 | instr = &cctx.ctx_instr; |
| 7839 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7840 | // Set the context to the function, it may be compiled when called from |
| 7841 | // another script. Set the script version to the most modern one. |
| 7842 | // The line number will be set in next_line_from_context(). |
| 7843 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7844 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 7845 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7846 | // Make sure error messages are OK. |
| 7847 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 7848 | if (do_estack_push) |
| 7849 | estack_push_ufunc(ufunc, 1); |
Bram Moolenaar | f4e8cdd | 2020-10-12 22:07:13 +0200 | [diff] [blame] | 7850 | estack_compiling = TRUE; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7851 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7852 | if (ufunc->uf_def_args.ga_len > 0) |
| 7853 | { |
| 7854 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7855 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7856 | int i; |
| 7857 | char_u *arg; |
| 7858 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7859 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7860 | |
| 7861 | // Produce instructions for the default values of optional arguments. |
| 7862 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 7863 | // where to start when the function is called, depending on the number |
| 7864 | // of arguments. |
| 7865 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 7866 | if (ufunc->uf_def_arg_idx == NULL) |
| 7867 | goto erret; |
| 7868 | for (i = 0; i < count; ++i) |
| 7869 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7870 | garray_T *stack = &cctx.ctx_type_stack; |
| 7871 | type_T *val_type; |
| 7872 | int arg_idx = first_def_arg + i; |
| 7873 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7874 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 7875 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7876 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7877 | goto erret; |
| 7878 | |
| 7879 | // If no type specified use the type of the default value. |
| 7880 | // Otherwise check that the default value type matches the |
| 7881 | // specified type. |
| 7882 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 7883 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7884 | { |
| 7885 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7886 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7887 | } |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 7888 | else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, |
| 7889 | TRUE, arg_idx + 1) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7890 | goto erret; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7891 | |
| 7892 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7893 | goto erret; |
| 7894 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7895 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7896 | |
| 7897 | if (did_set_arg_type) |
| 7898 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7899 | } |
| 7900 | |
| 7901 | /* |
| 7902 | * Loop over all the lines of the function and generate instructions. |
| 7903 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7904 | for (;;) |
| 7905 | { |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 7906 | exarg_T ea; |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 7907 | int starts_with_colon = FALSE; |
| 7908 | char_u *cmd; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 7909 | cmdmod_T local_cmdmod; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7910 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7911 | // Bail out on the first error to avoid a flood of errors and report |
| 7912 | // the right line number when inside try/catch. |
Bram Moolenaar | d66960b | 2020-10-30 20:46:26 +0100 | [diff] [blame] | 7913 | if (did_emsg_before != did_emsg) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7914 | goto erret; |
| 7915 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7916 | if (line != NULL && *line == '|') |
| 7917 | // the line continues after a '|' |
| 7918 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 7919 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7920 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 7921 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7922 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 7923 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7924 | goto erret; |
| 7925 | } |
| 7926 | else |
| 7927 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 7928 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 7929 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7930 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7931 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7932 | } |
| 7933 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7934 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7935 | ea.cmdlinep = &line; |
| 7936 | ea.cmd = skipwhite(line); |
| 7937 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7938 | // Some things can be recognized by the first character. |
| 7939 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7940 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7941 | case '#': |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 7942 | // "#" starts a comment |
| 7943 | line = (char_u *)""; |
| 7944 | continue; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7945 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7946 | case '}': |
| 7947 | { |
| 7948 | // "}" ends a block scope |
| 7949 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7950 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7951 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7952 | if (stype == BLOCK_SCOPE) |
| 7953 | { |
| 7954 | compile_endblock(&cctx); |
| 7955 | line = ea.cmd; |
| 7956 | } |
| 7957 | else |
| 7958 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7959 | emsg(_(e_using_rcurly_outside_if_block_scope)); |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7960 | goto erret; |
| 7961 | } |
| 7962 | if (line != NULL) |
| 7963 | line = skipwhite(ea.cmd + 1); |
| 7964 | continue; |
| 7965 | } |
| 7966 | |
| 7967 | case '{': |
| 7968 | // "{" starts a block scope |
| 7969 | // "{'a': 1}->func() is something else |
| 7970 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7971 | { |
| 7972 | line = compile_block(ea.cmd, &cctx); |
| 7973 | continue; |
| 7974 | } |
| 7975 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7976 | } |
| 7977 | |
| 7978 | /* |
| 7979 | * COMMAND MODIFIERS |
| 7980 | */ |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 7981 | cctx.ctx_has_cmdmod = FALSE; |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 7982 | if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE) |
| 7983 | == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7984 | { |
| 7985 | if (errormsg != NULL) |
| 7986 | goto erret; |
| 7987 | // empty line or comment |
| 7988 | line = (char_u *)""; |
| 7989 | continue; |
| 7990 | } |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 7991 | generate_cmdmods(&cctx, &local_cmdmod); |
| 7992 | undo_cmdmod(&local_cmdmod); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7993 | |
Bram Moolenaar | e88c8e8 | 2020-11-01 17:03:37 +0100 | [diff] [blame] | 7994 | // Check if there was a colon after the last command modifier or before |
| 7995 | // the current position. |
| 7996 | for (p = ea.cmd; p >= line; --p) |
| 7997 | { |
| 7998 | if (*p == ':') |
| 7999 | starts_with_colon = TRUE; |
| 8000 | if (p < ea.cmd && !VIM_ISWHITE(*p)) |
| 8001 | break; |
| 8002 | } |
| 8003 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8004 | // Skip ":call" to get to the function name. |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 8005 | p = ea.cmd; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8006 | if (checkforcmd(&ea.cmd, "call", 3)) |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 8007 | { |
| 8008 | if (*ea.cmd == '(') |
| 8009 | // not for "call()" |
| 8010 | ea.cmd = p; |
| 8011 | else |
| 8012 | ea.cmd = skipwhite(ea.cmd); |
| 8013 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8014 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 8015 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8016 | { |
Bram Moolenaar | 17126b1 | 2021-01-07 22:03:02 +0100 | [diff] [blame] | 8017 | int assign; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 8018 | |
Bram Moolenaar | 17126b1 | 2021-01-07 22:03:02 +0100 | [diff] [blame] | 8019 | // Check for assignment after command modifiers. |
| 8020 | assign = may_compile_assignment(&ea, &line, &cctx); |
| 8021 | if (assign == OK) |
| 8022 | goto nextline; |
| 8023 | if (assign == FAIL) |
| 8024 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8025 | } |
| 8026 | |
| 8027 | /* |
| 8028 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 8029 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8030 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 8031 | cmd = ea.cmd; |
Bram Moolenaar | 7c5ad34 | 2020-08-12 15:48:55 +0200 | [diff] [blame] | 8032 | if (*cmd != '\'' || starts_with_colon) |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 8033 | { |
Bram Moolenaar | 3bd8de4 | 2020-09-14 16:37:34 +0200 | [diff] [blame] | 8034 | ea.cmd = skip_range(ea.cmd, TRUE, NULL); |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 8035 | if (ea.cmd > cmd) |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 8036 | { |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 8037 | if (!starts_with_colon) |
| 8038 | { |
Bram Moolenaar | 6e2c2c5 | 2020-12-25 19:25:45 +0100 | [diff] [blame] | 8039 | semsg(_(e_colon_required_before_range_str), cmd); |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 8040 | goto erret; |
| 8041 | } |
| 8042 | if (ends_excmd2(line, ea.cmd)) |
| 8043 | { |
| 8044 | // A range without a command: jump to the line. |
| 8045 | // TODO: compile to a more efficient command, possibly |
| 8046 | // calling parse_cmd_address(). |
| 8047 | ea.cmdidx = CMD_SIZE; |
| 8048 | line = compile_exec(line, &ea, &cctx); |
| 8049 | goto nextline; |
| 8050 | } |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 8051 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 8052 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 8053 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 8054 | : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 8055 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8056 | |
Bram Moolenaar | d1510ee | 2021-01-04 16:15:58 +0100 | [diff] [blame] | 8057 | if (p == NULL) |
| 8058 | { |
| 8059 | if (cctx.ctx_skip != SKIP_YES) |
| 8060 | emsg(_(e_ambiguous_use_of_user_defined_command)); |
| 8061 | goto erret; |
| 8062 | } |
| 8063 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8064 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 8065 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 8066 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 8067 | { |
| 8068 | line += STRLEN(line); |
Bram Moolenaar | f665e97 | 2020-12-05 19:17:16 +0100 | [diff] [blame] | 8069 | goto nextline; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 8070 | } |
| 8071 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8072 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 8073 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8074 | { |
Bram Moolenaar | 52c124d | 2020-12-20 21:43:35 +0100 | [diff] [blame] | 8075 | // CMD_var cannot happen, compile_assignment() above would be |
| 8076 | // used. Most likely an assignment to a non-existing variable. |
| 8077 | semsg(_(e_command_not_recognized_str), ea.cmd); |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 8078 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8079 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8080 | } |
| 8081 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 8082 | if (cctx.ctx_had_return |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 8083 | && ea.cmdidx != CMD_elseif |
| 8084 | && ea.cmdidx != CMD_else |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8085 | && ea.cmdidx != CMD_endif |
| 8086 | && ea.cmdidx != CMD_endfor |
| 8087 | && ea.cmdidx != CMD_endwhile |
| 8088 | && ea.cmdidx != CMD_catch |
| 8089 | && ea.cmdidx != CMD_finally |
| 8090 | && ea.cmdidx != CMD_endtry) |
| 8091 | { |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 8092 | emsg(_(e_unreachable_code_after_return)); |
| 8093 | goto erret; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8094 | } |
| 8095 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 8096 | p = skipwhite(p); |
| 8097 | if (ea.cmdidx != CMD_SIZE |
| 8098 | && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read) |
| 8099 | { |
Bram Moolenaar | 9b123d8 | 2020-09-14 22:39:11 +0200 | [diff] [blame] | 8100 | if (ea.cmdidx >= 0) |
| 8101 | ea.argt = excmd_get_argt(ea.cmdidx); |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 8102 | if ((ea.argt & EX_BANG) && *p == '!') |
| 8103 | { |
| 8104 | ea.forceit = TRUE; |
| 8105 | p = skipwhite(p + 1); |
| 8106 | } |
| 8107 | } |
| 8108 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8109 | switch (ea.cmdidx) |
| 8110 | { |
| 8111 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 8112 | ea.arg = p; |
| 8113 | line = compile_nested_function(&ea, &cctx); |
| 8114 | break; |
| 8115 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8116 | case CMD_function: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 8117 | // TODO: should we allow this, e.g. to declare a global |
| 8118 | // function? |
| 8119 | emsg(_(e_cannot_use_function_inside_def)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8120 | goto erret; |
| 8121 | |
| 8122 | case CMD_return: |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 8123 | line = compile_return(p, check_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8124 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8125 | break; |
| 8126 | |
| 8127 | case CMD_let: |
Bram Moolenaar | c58f545 | 2020-10-21 20:58:52 +0200 | [diff] [blame] | 8128 | emsg(_(e_cannot_use_let_in_vim9_script)); |
| 8129 | break; |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 8130 | case CMD_var: |
| 8131 | case CMD_final: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8132 | case CMD_const: |
| 8133 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 8134 | if (line == p) |
| 8135 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8136 | break; |
| 8137 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 8138 | case CMD_unlet: |
| 8139 | case CMD_unlockvar: |
| 8140 | case CMD_lockvar: |
| 8141 | line = compile_unletlock(p, &ea, &cctx); |
| 8142 | break; |
| 8143 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8144 | case CMD_import: |
| 8145 | line = compile_import(p, &cctx); |
| 8146 | break; |
| 8147 | |
| 8148 | case CMD_if: |
| 8149 | line = compile_if(p, &cctx); |
| 8150 | break; |
| 8151 | case CMD_elseif: |
| 8152 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8153 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8154 | break; |
| 8155 | case CMD_else: |
| 8156 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8157 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8158 | break; |
| 8159 | case CMD_endif: |
| 8160 | line = compile_endif(p, &cctx); |
| 8161 | break; |
| 8162 | |
| 8163 | case CMD_while: |
| 8164 | line = compile_while(p, &cctx); |
| 8165 | break; |
| 8166 | case CMD_endwhile: |
| 8167 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8168 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8169 | break; |
| 8170 | |
| 8171 | case CMD_for: |
| 8172 | line = compile_for(p, &cctx); |
| 8173 | break; |
| 8174 | case CMD_endfor: |
| 8175 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8176 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8177 | break; |
| 8178 | case CMD_continue: |
| 8179 | line = compile_continue(p, &cctx); |
| 8180 | break; |
| 8181 | case CMD_break: |
| 8182 | line = compile_break(p, &cctx); |
| 8183 | break; |
| 8184 | |
| 8185 | case CMD_try: |
| 8186 | line = compile_try(p, &cctx); |
| 8187 | break; |
| 8188 | case CMD_catch: |
| 8189 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8190 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8191 | break; |
| 8192 | case CMD_finally: |
| 8193 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8194 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8195 | break; |
| 8196 | case CMD_endtry: |
| 8197 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8198 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8199 | break; |
| 8200 | case CMD_throw: |
| 8201 | line = compile_throw(p, &cctx); |
| 8202 | break; |
| 8203 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 8204 | case CMD_eval: |
| 8205 | if (compile_expr0(&p, &cctx) == FAIL) |
| 8206 | goto erret; |
| 8207 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 8208 | // drop the result |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 8209 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 8210 | |
| 8211 | line = skipwhite(p); |
| 8212 | break; |
| 8213 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8214 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8215 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 8216 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 8217 | case CMD_echomsg: |
| 8218 | case CMD_echoerr: |
| 8219 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 8220 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8221 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 8222 | case CMD_put: |
| 8223 | ea.cmd = cmd; |
| 8224 | line = compile_put(p, &ea, &cctx); |
| 8225 | break; |
| 8226 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 8227 | // TODO: any other commands with an expression argument? |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 8228 | |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 8229 | case CMD_append: |
| 8230 | case CMD_change: |
| 8231 | case CMD_insert: |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 8232 | case CMD_t: |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 8233 | case CMD_xit: |
| 8234 | not_in_vim9(&ea); |
| 8235 | goto erret; |
| 8236 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 8237 | case CMD_SIZE: |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 8238 | if (cctx.ctx_skip != SKIP_YES) |
| 8239 | { |
| 8240 | semsg(_(e_invalid_command_str), ea.cmd); |
| 8241 | goto erret; |
| 8242 | } |
| 8243 | // We don't check for a next command here. |
| 8244 | line = (char_u *)""; |
| 8245 | break; |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 8246 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8247 | default: |
Bram Moolenaar | 5163fcc | 2020-08-27 23:37:09 +0200 | [diff] [blame] | 8248 | if (cctx.ctx_skip == SKIP_YES) |
| 8249 | { |
| 8250 | // We don't check for a next command here. |
| 8251 | line = (char_u *)""; |
| 8252 | } |
| 8253 | else |
| 8254 | { |
| 8255 | // Not recognized, execute with do_cmdline_cmd(). |
| 8256 | ea.arg = p; |
| 8257 | line = compile_exec(line, &ea, &cctx); |
| 8258 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8259 | break; |
| 8260 | } |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 8261 | nextline: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8262 | if (line == NULL) |
| 8263 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 8264 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8265 | |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 8266 | // Undo any command modifiers. |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 8267 | generate_undo_cmdmods(&cctx); |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 8268 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8269 | if (cctx.ctx_type_stack.ga_len < 0) |
| 8270 | { |
| 8271 | iemsg("Type stack underflow"); |
| 8272 | goto erret; |
| 8273 | } |
| 8274 | } |
| 8275 | |
| 8276 | if (cctx.ctx_scope != NULL) |
| 8277 | { |
| 8278 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 8279 | emsg(_(e_endif)); |
| 8280 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 8281 | emsg(_(e_endwhile)); |
| 8282 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 8283 | emsg(_(e_endfor)); |
| 8284 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 8285 | emsg(_(e_missing_rcurly)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8286 | goto erret; |
| 8287 | } |
| 8288 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8289 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8290 | { |
| 8291 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 8292 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 8293 | emsg(_(e_missing_return_statement)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8294 | goto erret; |
| 8295 | } |
| 8296 | |
| 8297 | // Return zero if there is no return at the end. |
Bram Moolenaar | 299f303 | 2021-01-08 20:53:09 +0100 | [diff] [blame] | 8298 | generate_instr(&cctx, ISN_RETURN_ZERO); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8299 | } |
| 8300 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 8301 | { |
| 8302 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8303 | + ufunc->uf_dfunc_idx; |
| 8304 | dfunc->df_deleted = FALSE; |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 8305 | dfunc->df_script_seq = current_sctx.sc_seq; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 8306 | dfunc->df_instr = instr->ga_data; |
| 8307 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 8308 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 8309 | dfunc->df_has_closure = cctx.ctx_has_closure; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 8310 | if (cctx.ctx_outer_used) |
| 8311 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8312 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 8313 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8314 | |
| 8315 | ret = OK; |
| 8316 | |
| 8317 | erret: |
| 8318 | if (ret == FAIL) |
| 8319 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8320 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 8321 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8322 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8323 | |
| 8324 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 8325 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8326 | ga_clear(instr); |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8327 | VIM_CLEAR(dfunc->df_name); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8328 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 8329 | // If using the last entry in the table and it was added above, we |
| 8330 | // might as well remove it. |
| 8331 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 8332 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 8333 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8334 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 8335 | ufunc->uf_dfunc_idx = 0; |
| 8336 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8337 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8338 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 8339 | while (cctx.ctx_scope != NULL) |
| 8340 | drop_scope(&cctx); |
| 8341 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8342 | // Don't execute this function body. |
| 8343 | ga_clear_strings(&ufunc->uf_lines); |
| 8344 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8345 | if (errormsg != NULL) |
| 8346 | emsg(errormsg); |
Bram Moolenaar | d66960b | 2020-10-30 20:46:26 +0100 | [diff] [blame] | 8347 | else if (did_emsg == did_emsg_before) |
Bram Moolenaar | e13bdec | 2020-10-16 23:16:47 +0200 | [diff] [blame] | 8348 | emsg(_(e_compiling_def_function_failed)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8349 | } |
| 8350 | |
| 8351 | current_sctx = save_current_sctx; |
Bram Moolenaar | f4e8cdd | 2020-10-12 22:07:13 +0200 | [diff] [blame] | 8352 | estack_compiling = save_estack_compiling; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 8353 | if (do_estack_push) |
| 8354 | estack_pop(); |
| 8355 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8356 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 8357 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8358 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 8359 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8360 | } |
| 8361 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 8362 | void |
| 8363 | set_function_type(ufunc_T *ufunc) |
| 8364 | { |
| 8365 | int varargs = ufunc->uf_va_name != NULL; |
| 8366 | int argcount = ufunc->uf_args.ga_len; |
| 8367 | |
| 8368 | // Create a type for the function, with the return type and any |
| 8369 | // argument types. |
| 8370 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 8371 | // The type is included in "tt_args". |
| 8372 | if (argcount > 0 || varargs) |
| 8373 | { |
| 8374 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 8375 | argcount, &ufunc->uf_type_list); |
| 8376 | // Add argument types to the function type. |
| 8377 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 8378 | argcount + varargs, |
| 8379 | &ufunc->uf_type_list) == FAIL) |
| 8380 | return; |
| 8381 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 8382 | ufunc->uf_func_type->tt_min_argcount = |
| 8383 | argcount - ufunc->uf_def_args.ga_len; |
| 8384 | if (ufunc->uf_arg_types == NULL) |
| 8385 | { |
| 8386 | int i; |
| 8387 | |
| 8388 | // lambda does not have argument types. |
| 8389 | for (i = 0; i < argcount; ++i) |
| 8390 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 8391 | } |
| 8392 | else |
| 8393 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 8394 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 8395 | if (varargs) |
| 8396 | { |
| 8397 | ufunc->uf_func_type->tt_args[argcount] = |
| 8398 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 8399 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 8400 | } |
| 8401 | } |
| 8402 | else |
| 8403 | // No arguments, can use a predefined type. |
| 8404 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 8405 | argcount, &ufunc->uf_type_list); |
| 8406 | } |
| 8407 | |
| 8408 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8409 | /* |
| 8410 | * Delete an instruction, free what it contains. |
| 8411 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8412 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8413 | delete_instr(isn_T *isn) |
| 8414 | { |
| 8415 | switch (isn->isn_type) |
| 8416 | { |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8417 | case ISN_DEF: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8418 | case ISN_EXEC: |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 8419 | case ISN_LOADAUTO: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8420 | case ISN_LOADB: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8421 | case ISN_LOADENV: |
| 8422 | case ISN_LOADG: |
| 8423 | case ISN_LOADOPT: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8424 | case ISN_LOADT: |
| 8425 | case ISN_LOADW: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8426 | case ISN_PUSHEXC: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8427 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8428 | case ISN_PUSHS: |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 8429 | case ISN_RANGE: |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 8430 | case ISN_STOREAUTO: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8431 | case ISN_STOREB: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 8432 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8433 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 8434 | case ISN_STORET: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8435 | case ISN_STOREW: |
| 8436 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8437 | vim_free(isn->isn_arg.string); |
| 8438 | break; |
| 8439 | |
| 8440 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 8441 | case ISN_STORES: |
| 8442 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8443 | break; |
| 8444 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 8445 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 8446 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 8447 | vim_free(isn->isn_arg.unlet.ul_name); |
| 8448 | break; |
| 8449 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8450 | case ISN_STOREOPT: |
| 8451 | vim_free(isn->isn_arg.storeopt.so_name); |
| 8452 | break; |
| 8453 | |
| 8454 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 8455 | blob_unref(isn->isn_arg.blob); |
| 8456 | break; |
| 8457 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8458 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 8459 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8460 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 8461 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8462 | break; |
| 8463 | |
| 8464 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 8465 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8466 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 8467 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8468 | break; |
| 8469 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8470 | case ISN_UCALL: |
| 8471 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 8472 | break; |
| 8473 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 8474 | case ISN_FUNCREF: |
| 8475 | { |
| 8476 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8477 | + isn->isn_arg.funcref.fr_func; |
Bram Moolenaar | 077a423 | 2020-12-22 18:33:27 +0100 | [diff] [blame] | 8478 | ufunc_T *ufunc = dfunc->df_ufunc; |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 8479 | |
Bram Moolenaar | 077a423 | 2020-12-22 18:33:27 +0100 | [diff] [blame] | 8480 | if (ufunc != NULL && func_name_refcount(ufunc->uf_name)) |
| 8481 | func_ptr_unref(ufunc); |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 8482 | } |
| 8483 | break; |
| 8484 | |
| 8485 | case ISN_DCALL: |
| 8486 | { |
| 8487 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8488 | + isn->isn_arg.dfunc.cdf_idx; |
| 8489 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 8490 | if (dfunc->df_ufunc != NULL |
| 8491 | && func_name_refcount(dfunc->df_ufunc->uf_name)) |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 8492 | func_ptr_unref(dfunc->df_ufunc); |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 8493 | } |
| 8494 | break; |
| 8495 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 8496 | case ISN_NEWFUNC: |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 8497 | { |
| 8498 | char_u *lambda = isn->isn_arg.newfunc.nf_lambda; |
| 8499 | ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL); |
| 8500 | |
| 8501 | if (ufunc != NULL) |
| 8502 | { |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8503 | unlink_def_function(ufunc); |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 8504 | func_ptr_unref(ufunc); |
| 8505 | } |
| 8506 | |
| 8507 | vim_free(lambda); |
| 8508 | vim_free(isn->isn_arg.newfunc.nf_global); |
| 8509 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 8510 | break; |
| 8511 | |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 8512 | case ISN_CHECKTYPE: |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 8513 | case ISN_SETTYPE: |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 8514 | free_type(isn->isn_arg.type.ct_type); |
| 8515 | break; |
| 8516 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 8517 | case ISN_CMDMOD: |
| 8518 | vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod |
| 8519 | ->cmod_filter_regmatch.regprog); |
| 8520 | vim_free(isn->isn_arg.cmdmod.cf_cmdmod); |
| 8521 | break; |
| 8522 | |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 8523 | case ISN_LOADSCRIPT: |
| 8524 | case ISN_STORESCRIPT: |
| 8525 | vim_free(isn->isn_arg.script.scriptref); |
| 8526 | break; |
| 8527 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8528 | case ISN_2BOOL: |
| 8529 | case ISN_2STRING: |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 8530 | case ISN_2STRING_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8531 | case ISN_ADDBLOB: |
| 8532 | case ISN_ADDLIST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8533 | case ISN_ANYINDEX: |
| 8534 | case ISN_ANYSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8535 | case ISN_BCALL: |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 8536 | case ISN_BLOBAPPEND: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8537 | case ISN_CATCH: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8538 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8539 | case ISN_CHECKNR: |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 8540 | case ISN_CMDMOD_REV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8541 | case ISN_COMPAREANY: |
| 8542 | case ISN_COMPAREBLOB: |
| 8543 | case ISN_COMPAREBOOL: |
| 8544 | case ISN_COMPAREDICT: |
| 8545 | case ISN_COMPAREFLOAT: |
| 8546 | case ISN_COMPAREFUNC: |
| 8547 | case ISN_COMPARELIST: |
| 8548 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8549 | case ISN_COMPARESPECIAL: |
| 8550 | case ISN_COMPARESTRING: |
| 8551 | case ISN_CONCAT: |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 8552 | case ISN_COND2BOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8553 | case ISN_DROP: |
| 8554 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 8555 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 8556 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8557 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 8558 | case ISN_EXECCONCAT: |
| 8559 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8560 | case ISN_FOR: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8561 | case ISN_GETITEM: |
| 8562 | case ISN_JUMP: |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 8563 | case ISN_LISTAPPEND: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 8564 | case ISN_LISTINDEX: |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 8565 | case ISN_LISTSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8566 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 8567 | case ISN_LOADBDICT: |
| 8568 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 8569 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8570 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 8571 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8572 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 8573 | case ISN_LOADWDICT: |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 8574 | case ISN_LOCKCONST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8575 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8576 | case ISN_NEGATENR: |
| 8577 | case ISN_NEWDICT: |
| 8578 | case ISN_NEWLIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8579 | case ISN_OPANY: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8580 | case ISN_OPFLOAT: |
| 8581 | case ISN_OPNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8582 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 8583 | case ISN_PCALL_END: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8584 | case ISN_PUSHBOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8585 | case ISN_PUSHF: |
| 8586 | case ISN_PUSHNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8587 | case ISN_PUSHSPEC: |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 8588 | case ISN_PUT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8589 | case ISN_RETURN: |
Bram Moolenaar | 299f303 | 2021-01-08 20:53:09 +0100 | [diff] [blame] | 8590 | case ISN_RETURN_ZERO: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8591 | case ISN_SHUFFLE: |
| 8592 | case ISN_SLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8593 | case ISN_STORE: |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 8594 | case ISN_STOREINDEX: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8595 | case ISN_STORENR: |
| 8596 | case ISN_STOREOUTER: |
| 8597 | case ISN_STOREREG: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8598 | case ISN_STOREV: |
| 8599 | case ISN_STRINDEX: |
| 8600 | case ISN_STRSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8601 | case ISN_THROW: |
| 8602 | case ISN_TRY: |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 8603 | case ISN_UNLETINDEX: |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 8604 | case ISN_UNPACK: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8605 | // nothing allocated |
| 8606 | break; |
| 8607 | } |
| 8608 | } |
| 8609 | |
| 8610 | /* |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8611 | * Free all instructions for "dfunc" except df_name. |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8612 | */ |
| 8613 | static void |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8614 | delete_def_function_contents(dfunc_T *dfunc, int mark_deleted) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8615 | { |
| 8616 | int idx; |
| 8617 | |
| 8618 | ga_clear(&dfunc->df_def_args_isn); |
| 8619 | |
| 8620 | if (dfunc->df_instr != NULL) |
| 8621 | { |
| 8622 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 8623 | delete_instr(dfunc->df_instr + idx); |
| 8624 | VIM_CLEAR(dfunc->df_instr); |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8625 | dfunc->df_instr = NULL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8626 | } |
| 8627 | |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8628 | if (mark_deleted) |
| 8629 | dfunc->df_deleted = TRUE; |
| 8630 | if (dfunc->df_ufunc != NULL) |
| 8631 | dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8632 | } |
| 8633 | |
| 8634 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8635 | * When a user function is deleted, clear the contents of any associated def |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8636 | * function, unless another user function still uses it. |
| 8637 | * The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8638 | */ |
| 8639 | void |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8640 | unlink_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8641 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8642 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8643 | { |
| 8644 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8645 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8646 | |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8647 | if (--dfunc->df_refcount <= 0) |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8648 | delete_def_function_contents(dfunc, TRUE); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8649 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8650 | ufunc->uf_dfunc_idx = 0; |
| 8651 | if (dfunc->df_ufunc == ufunc) |
| 8652 | dfunc->df_ufunc = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8653 | } |
| 8654 | } |
| 8655 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8656 | /* |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8657 | * Used when a user function refers to an existing dfunc. |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8658 | */ |
| 8659 | void |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8660 | link_def_function(ufunc_T *ufunc) |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8661 | { |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8662 | if (ufunc->uf_dfunc_idx > 0) |
| 8663 | { |
| 8664 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8665 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8666 | |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8667 | ++dfunc->df_refcount; |
| 8668 | } |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8669 | } |
| 8670 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8671 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8672 | /* |
| 8673 | * Free all functions defined with ":def". |
| 8674 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8675 | void |
| 8676 | free_def_functions(void) |
| 8677 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8678 | int idx; |
| 8679 | |
| 8680 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 8681 | { |
| 8682 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 8683 | |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8684 | delete_def_function_contents(dfunc, TRUE); |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8685 | vim_free(dfunc->df_name); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8686 | } |
| 8687 | |
| 8688 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8689 | } |
| 8690 | #endif |
| 8691 | |
| 8692 | |
| 8693 | #endif // FEAT_EVAL |