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 | */ |
| 881 | static 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, |
| 886 | cctx_T *cctx, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 887 | int silent, |
| 888 | int actual_is_const) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 889 | { |
Bram Moolenaar | 4ed124c | 2020-09-09 20:03:46 +0200 | [diff] [blame] | 890 | if (expected == &t_bool && actual != &t_bool |
| 891 | && (actual->tt_flags & TTFLAG_BOOL_OK)) |
| 892 | { |
| 893 | // Using "0", "1" or the result of an expression with "&&" or "||" as a |
| 894 | // boolean is OK but requires a conversion. |
| 895 | generate_2BOOL(cctx, FALSE); |
| 896 | return OK; |
| 897 | } |
| 898 | |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 899 | if (check_type(expected, actual, FALSE, 0) == OK) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 900 | return OK; |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 901 | |
| 902 | // 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] | 903 | // If it's a constant a runtime check makes no sense. |
| 904 | if (!actual_is_const && use_typecheck(actual, expected)) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 905 | { |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 906 | generate_TYPECHECK(cctx, expected, offset); |
| 907 | return OK; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 908 | } |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 909 | |
| 910 | if (!silent) |
| 911 | type_mismatch(expected, actual); |
| 912 | return FAIL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /* |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 916 | * Check that the top of the type stack has a type that can be used as a |
| 917 | * condition. Give an error and return FAIL if not. |
| 918 | */ |
| 919 | static int |
| 920 | bool_on_stack(cctx_T *cctx) |
| 921 | { |
| 922 | garray_T *stack = &cctx->ctx_type_stack; |
| 923 | type_T *type; |
| 924 | |
| 925 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 926 | if (type == &t_bool) |
| 927 | return OK; |
| 928 | |
| 929 | if (type == &t_any || type == &t_number) |
| 930 | // Number 0 and 1 are OK to use as a bool. "any" could also be a bool. |
| 931 | // This requires a runtime type check. |
| 932 | return generate_COND2BOOL(cctx); |
| 933 | |
| 934 | return need_type(type, &t_bool, -1, cctx, FALSE, FALSE); |
| 935 | } |
| 936 | |
| 937 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 938 | * Generate an ISN_PUSHNR instruction. |
| 939 | */ |
| 940 | static int |
| 941 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 942 | { |
| 943 | isn_T *isn; |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 944 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 945 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 946 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 947 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 948 | return FAIL; |
| 949 | isn->isn_arg.number = number; |
| 950 | |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 951 | if (number == 0 || number == 1) |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 952 | // A 0 or 1 number can also be used as a bool. |
Bram Moolenaar | 3868f59 | 2020-12-25 13:20:41 +0100 | [diff] [blame] | 953 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 954 | return OK; |
| 955 | } |
| 956 | |
| 957 | /* |
| 958 | * Generate an ISN_PUSHBOOL instruction. |
| 959 | */ |
| 960 | static int |
| 961 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 962 | { |
| 963 | isn_T *isn; |
| 964 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 965 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 966 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 967 | return FAIL; |
| 968 | isn->isn_arg.number = number; |
| 969 | |
| 970 | return OK; |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | * Generate an ISN_PUSHSPEC instruction. |
| 975 | */ |
| 976 | static int |
| 977 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 978 | { |
| 979 | isn_T *isn; |
| 980 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 981 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 982 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 983 | return FAIL; |
| 984 | isn->isn_arg.number = number; |
| 985 | |
| 986 | return OK; |
| 987 | } |
| 988 | |
| 989 | #ifdef FEAT_FLOAT |
| 990 | /* |
| 991 | * Generate an ISN_PUSHF instruction. |
| 992 | */ |
| 993 | static int |
| 994 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 995 | { |
| 996 | isn_T *isn; |
| 997 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 998 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 999 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 1000 | return FAIL; |
| 1001 | isn->isn_arg.fnumber = fnumber; |
| 1002 | |
| 1003 | return OK; |
| 1004 | } |
| 1005 | #endif |
| 1006 | |
| 1007 | /* |
| 1008 | * Generate an ISN_PUSHS instruction. |
| 1009 | * Consumes "str". |
| 1010 | */ |
| 1011 | static int |
| 1012 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 1013 | { |
| 1014 | isn_T *isn; |
| 1015 | |
Bram Moolenaar | 508b561 | 2021-01-02 18:17:26 +0100 | [diff] [blame] | 1016 | if (cctx->ctx_skip == SKIP_YES) |
| 1017 | { |
| 1018 | vim_free(str); |
| 1019 | return OK; |
| 1020 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1021 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 1022 | return FAIL; |
| 1023 | isn->isn_arg.string = str; |
| 1024 | |
| 1025 | return OK; |
| 1026 | } |
| 1027 | |
| 1028 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1029 | * Generate an ISN_PUSHCHANNEL instruction. |
| 1030 | * Consumes "channel". |
| 1031 | */ |
| 1032 | static int |
| 1033 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 1034 | { |
| 1035 | isn_T *isn; |
| 1036 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1037 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1038 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 1039 | return FAIL; |
| 1040 | isn->isn_arg.channel = channel; |
| 1041 | |
| 1042 | return OK; |
| 1043 | } |
| 1044 | |
| 1045 | /* |
| 1046 | * Generate an ISN_PUSHJOB instruction. |
| 1047 | * Consumes "job". |
| 1048 | */ |
| 1049 | static int |
| 1050 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 1051 | { |
| 1052 | isn_T *isn; |
| 1053 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1054 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1055 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1056 | return FAIL; |
| 1057 | isn->isn_arg.job = job; |
| 1058 | |
| 1059 | return OK; |
| 1060 | } |
| 1061 | |
| 1062 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1063 | * Generate an ISN_PUSHBLOB instruction. |
| 1064 | * Consumes "blob". |
| 1065 | */ |
| 1066 | static int |
| 1067 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1068 | { |
| 1069 | isn_T *isn; |
| 1070 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1071 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1072 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1073 | return FAIL; |
| 1074 | isn->isn_arg.blob = blob; |
| 1075 | |
| 1076 | return OK; |
| 1077 | } |
| 1078 | |
| 1079 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1080 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1081 | * Consumes "name". |
| 1082 | */ |
| 1083 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1084 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1085 | { |
| 1086 | isn_T *isn; |
| 1087 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1088 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1089 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1090 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 1091 | isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1092 | |
| 1093 | return OK; |
| 1094 | } |
| 1095 | |
| 1096 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1097 | * Generate an ISN_GETITEM instruction with "index". |
| 1098 | */ |
| 1099 | static int |
| 1100 | generate_GETITEM(cctx_T *cctx, int index) |
| 1101 | { |
| 1102 | isn_T *isn; |
| 1103 | garray_T *stack = &cctx->ctx_type_stack; |
| 1104 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1105 | type_T *item_type = &t_any; |
| 1106 | |
| 1107 | RETURN_OK_IF_SKIP(cctx); |
| 1108 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1109 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1110 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1111 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1112 | emsg(_(e_listreq)); |
| 1113 | return FAIL; |
| 1114 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1115 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1116 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1117 | return FAIL; |
| 1118 | isn->isn_arg.number = index; |
| 1119 | |
| 1120 | // add the item type to the type stack |
| 1121 | if (ga_grow(stack, 1) == FAIL) |
| 1122 | return FAIL; |
| 1123 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 1124 | ++stack->ga_len; |
| 1125 | return OK; |
| 1126 | } |
| 1127 | |
| 1128 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 1129 | * Generate an ISN_SLICE instruction with "count". |
| 1130 | */ |
| 1131 | static int |
| 1132 | generate_SLICE(cctx_T *cctx, int count) |
| 1133 | { |
| 1134 | isn_T *isn; |
| 1135 | |
| 1136 | RETURN_OK_IF_SKIP(cctx); |
| 1137 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1138 | return FAIL; |
| 1139 | isn->isn_arg.number = count; |
| 1140 | return OK; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1145 | */ |
| 1146 | static int |
| 1147 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1148 | { |
| 1149 | isn_T *isn; |
| 1150 | |
| 1151 | RETURN_OK_IF_SKIP(cctx); |
| 1152 | |
| 1153 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1154 | return FAIL; |
| 1155 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1156 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1157 | |
| 1158 | return OK; |
| 1159 | } |
| 1160 | |
| 1161 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1162 | * Generate an ISN_STORE instruction. |
| 1163 | */ |
| 1164 | static int |
| 1165 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1166 | { |
| 1167 | isn_T *isn; |
| 1168 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1169 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1170 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1171 | return FAIL; |
| 1172 | if (name != NULL) |
| 1173 | isn->isn_arg.string = vim_strsave(name); |
| 1174 | else |
| 1175 | isn->isn_arg.number = idx; |
| 1176 | |
| 1177 | return OK; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 1181 | * Generate an ISN_STOREOUTER instruction. |
| 1182 | */ |
| 1183 | static int |
| 1184 | generate_STOREOUTER(cctx_T *cctx, int idx, int level) |
| 1185 | { |
| 1186 | isn_T *isn; |
| 1187 | |
| 1188 | RETURN_OK_IF_SKIP(cctx); |
| 1189 | if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL) |
| 1190 | return FAIL; |
| 1191 | isn->isn_arg.outer.outer_idx = idx; |
| 1192 | isn->isn_arg.outer.outer_depth = level; |
| 1193 | |
| 1194 | return OK; |
| 1195 | } |
| 1196 | |
| 1197 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1198 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1199 | */ |
| 1200 | static int |
| 1201 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1202 | { |
| 1203 | isn_T *isn; |
| 1204 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1205 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1206 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1207 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1208 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1209 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1210 | |
| 1211 | return OK; |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * Generate an ISN_STOREOPT instruction |
| 1216 | */ |
| 1217 | static int |
| 1218 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1219 | { |
| 1220 | isn_T *isn; |
| 1221 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1222 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1c199f9 | 2020-08-07 21:28:34 +0200 | [diff] [blame] | 1223 | if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1224 | return FAIL; |
| 1225 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1226 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1227 | |
| 1228 | return OK; |
| 1229 | } |
| 1230 | |
| 1231 | /* |
| 1232 | * Generate an ISN_LOAD or similar instruction. |
| 1233 | */ |
| 1234 | static int |
| 1235 | generate_LOAD( |
| 1236 | cctx_T *cctx, |
| 1237 | isntype_T isn_type, |
| 1238 | int idx, |
| 1239 | char_u *name, |
| 1240 | type_T *type) |
| 1241 | { |
| 1242 | isn_T *isn; |
| 1243 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1244 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1245 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1246 | return FAIL; |
| 1247 | if (name != NULL) |
| 1248 | isn->isn_arg.string = vim_strsave(name); |
| 1249 | else |
| 1250 | isn->isn_arg.number = idx; |
| 1251 | |
| 1252 | return OK; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 1256 | * Generate an ISN_LOADOUTER instruction |
| 1257 | */ |
| 1258 | static int |
| 1259 | generate_LOADOUTER( |
| 1260 | cctx_T *cctx, |
| 1261 | int idx, |
| 1262 | int nesting, |
| 1263 | type_T *type) |
| 1264 | { |
| 1265 | isn_T *isn; |
| 1266 | |
| 1267 | RETURN_OK_IF_SKIP(cctx); |
| 1268 | if ((isn = generate_instr_type(cctx, ISN_LOADOUTER, type)) == NULL) |
| 1269 | return FAIL; |
| 1270 | isn->isn_arg.outer.outer_idx = idx; |
| 1271 | isn->isn_arg.outer.outer_depth = nesting; |
| 1272 | |
| 1273 | return OK; |
| 1274 | } |
| 1275 | |
| 1276 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1277 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1278 | */ |
| 1279 | static int |
| 1280 | generate_LOADV( |
| 1281 | cctx_T *cctx, |
| 1282 | char_u *name, |
| 1283 | int error) |
| 1284 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1285 | int di_flags; |
| 1286 | int vidx = find_vim_var(name, &di_flags); |
| 1287 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1288 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1289 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1290 | if (vidx < 0) |
| 1291 | { |
| 1292 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1293 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1294 | return FAIL; |
| 1295 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1296 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1297 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1298 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1302 | * Generate an ISN_UNLET instruction. |
| 1303 | */ |
| 1304 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1305 | 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] | 1306 | { |
| 1307 | isn_T *isn; |
| 1308 | |
| 1309 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1310 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1311 | return FAIL; |
| 1312 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1313 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1314 | |
| 1315 | return OK; |
| 1316 | } |
| 1317 | |
| 1318 | /* |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 1319 | * Generate an ISN_LOCKCONST instruction. |
| 1320 | */ |
| 1321 | static int |
| 1322 | generate_LOCKCONST(cctx_T *cctx) |
| 1323 | { |
| 1324 | isn_T *isn; |
| 1325 | |
| 1326 | RETURN_OK_IF_SKIP(cctx); |
| 1327 | if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL) |
| 1328 | return FAIL; |
| 1329 | return OK; |
| 1330 | } |
| 1331 | |
| 1332 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1333 | * Generate an ISN_LOADS instruction. |
| 1334 | */ |
| 1335 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1336 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1337 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1338 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1339 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1340 | int sid, |
| 1341 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1342 | { |
| 1343 | isn_T *isn; |
| 1344 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1345 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1346 | if (isn_type == ISN_LOADS) |
| 1347 | isn = generate_instr_type(cctx, isn_type, type); |
| 1348 | else |
| 1349 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1350 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1351 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1352 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1353 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1354 | |
| 1355 | return OK; |
| 1356 | } |
| 1357 | |
| 1358 | /* |
| 1359 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1360 | */ |
| 1361 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1362 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1363 | cctx_T *cctx, |
| 1364 | isntype_T isn_type, |
| 1365 | int sid, |
| 1366 | int idx, |
| 1367 | type_T *type) |
| 1368 | { |
| 1369 | isn_T *isn; |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 1370 | scriptref_T *sref; |
| 1371 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1372 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1373 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1374 | if (isn_type == ISN_LOADSCRIPT) |
| 1375 | isn = generate_instr_type(cctx, isn_type, type); |
| 1376 | else |
| 1377 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1378 | if (isn == NULL) |
| 1379 | return FAIL; |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 1380 | |
| 1381 | // This requires three arguments, which doesn't fit in an instruction, thus |
| 1382 | // we need to allocate a struct for this. |
| 1383 | sref = ALLOC_ONE(scriptref_T); |
| 1384 | if (sref == NULL) |
| 1385 | return FAIL; |
| 1386 | isn->isn_arg.script.scriptref = sref; |
| 1387 | sref->sref_sid = sid; |
| 1388 | sref->sref_idx = idx; |
| 1389 | sref->sref_seq = si->sn_script_seq; |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1390 | sref->sref_type = type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1391 | return OK; |
| 1392 | } |
| 1393 | |
| 1394 | /* |
| 1395 | * Generate an ISN_NEWLIST instruction. |
| 1396 | */ |
| 1397 | static int |
| 1398 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1399 | { |
| 1400 | isn_T *isn; |
| 1401 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1402 | type_T *type; |
| 1403 | type_T *member; |
| 1404 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1405 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1406 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1407 | return FAIL; |
| 1408 | isn->isn_arg.number = count; |
| 1409 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1410 | // get the member type from all the items on the stack. |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1411 | if (count == 0) |
| 1412 | member = &t_void; |
| 1413 | else |
| 1414 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1415 | ((type_T **)stack->ga_data) + stack->ga_len, count, 1, |
| 1416 | cctx->ctx_type_list); |
| 1417 | type = get_list_type(member, cctx->ctx_type_list); |
| 1418 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1419 | // drop the value types |
| 1420 | stack->ga_len -= count; |
| 1421 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1422 | // add the list type to the type stack |
| 1423 | if (ga_grow(stack, 1) == FAIL) |
| 1424 | return FAIL; |
| 1425 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1426 | ++stack->ga_len; |
| 1427 | |
| 1428 | return OK; |
| 1429 | } |
| 1430 | |
| 1431 | /* |
| 1432 | * Generate an ISN_NEWDICT instruction. |
| 1433 | */ |
| 1434 | static int |
| 1435 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1436 | { |
| 1437 | isn_T *isn; |
| 1438 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1439 | type_T *type; |
| 1440 | type_T *member; |
| 1441 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1442 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1443 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1444 | return FAIL; |
| 1445 | isn->isn_arg.number = count; |
| 1446 | |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1447 | if (count == 0) |
| 1448 | member = &t_void; |
| 1449 | else |
| 1450 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1451 | ((type_T **)stack->ga_data) + stack->ga_len, count, 2, |
| 1452 | cctx->ctx_type_list); |
| 1453 | type = get_dict_type(member, cctx->ctx_type_list); |
| 1454 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1455 | // drop the key and value types |
| 1456 | stack->ga_len -= 2 * count; |
| 1457 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1458 | // add the dict type to the type stack |
| 1459 | if (ga_grow(stack, 1) == FAIL) |
| 1460 | return FAIL; |
| 1461 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1462 | ++stack->ga_len; |
| 1463 | |
| 1464 | return OK; |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * Generate an ISN_FUNCREF instruction. |
| 1469 | */ |
| 1470 | static int |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1471 | generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1472 | { |
| 1473 | isn_T *isn; |
| 1474 | garray_T *stack = &cctx->ctx_type_stack; |
| 1475 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1476 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1477 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1478 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1479 | isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 1480 | cctx->ctx_has_closure = 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1481 | |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 1482 | // if the referenced function is a closure, it may use items further up in |
| 1483 | // the nested context, including this one. |
| 1484 | if (ufunc->uf_flags & FC_CLOSURE) |
| 1485 | cctx->ctx_ufunc->uf_flags |= FC_CLOSURE; |
| 1486 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1487 | if (ga_grow(stack, 1) == FAIL) |
| 1488 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1489 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 1490 | ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1491 | ++stack->ga_len; |
| 1492 | |
| 1493 | return OK; |
| 1494 | } |
| 1495 | |
| 1496 | /* |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1497 | * Generate an ISN_NEWFUNC instruction. |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 1498 | * "lambda_name" and "func_name" must be in allocated memory and will be |
| 1499 | * consumed. |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1500 | */ |
| 1501 | static int |
| 1502 | generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) |
| 1503 | { |
| 1504 | isn_T *isn; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1505 | |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 1506 | if (cctx->ctx_skip == SKIP_YES) |
| 1507 | { |
| 1508 | vim_free(lambda_name); |
| 1509 | vim_free(func_name); |
| 1510 | return OK; |
| 1511 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1512 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 1513 | { |
| 1514 | vim_free(lambda_name); |
| 1515 | vim_free(func_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1516 | return FAIL; |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 1517 | } |
| 1518 | isn->isn_arg.newfunc.nf_lambda = lambda_name; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1519 | isn->isn_arg.newfunc.nf_global = func_name; |
| 1520 | |
| 1521 | return OK; |
| 1522 | } |
| 1523 | |
| 1524 | /* |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 1525 | * Generate an ISN_DEF instruction: list functions |
| 1526 | */ |
| 1527 | static int |
| 1528 | generate_DEF(cctx_T *cctx, char_u *name, size_t len) |
| 1529 | { |
| 1530 | isn_T *isn; |
| 1531 | |
| 1532 | RETURN_OK_IF_SKIP(cctx); |
| 1533 | if ((isn = generate_instr(cctx, ISN_DEF)) == NULL) |
| 1534 | return FAIL; |
| 1535 | if (len > 0) |
| 1536 | { |
| 1537 | isn->isn_arg.string = vim_strnsave(name, len); |
| 1538 | if (isn->isn_arg.string == NULL) |
| 1539 | return FAIL; |
| 1540 | } |
| 1541 | return OK; |
| 1542 | } |
| 1543 | |
| 1544 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1545 | * Generate an ISN_JUMP instruction. |
| 1546 | */ |
| 1547 | static int |
| 1548 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1549 | { |
| 1550 | isn_T *isn; |
| 1551 | garray_T *stack = &cctx->ctx_type_stack; |
| 1552 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1553 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1554 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1555 | return FAIL; |
| 1556 | isn->isn_arg.jump.jump_when = when; |
| 1557 | isn->isn_arg.jump.jump_where = where; |
| 1558 | |
| 1559 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1560 | --stack->ga_len; |
| 1561 | |
| 1562 | return OK; |
| 1563 | } |
| 1564 | |
| 1565 | static int |
| 1566 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1567 | { |
| 1568 | isn_T *isn; |
| 1569 | garray_T *stack = &cctx->ctx_type_stack; |
| 1570 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1571 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1572 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1573 | return FAIL; |
| 1574 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1575 | |
| 1576 | if (ga_grow(stack, 1) == FAIL) |
| 1577 | return FAIL; |
| 1578 | // type doesn't matter, will be stored next |
| 1579 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1580 | ++stack->ga_len; |
| 1581 | |
| 1582 | return OK; |
| 1583 | } |
| 1584 | |
| 1585 | /* |
| 1586 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1587 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1588 | * Return FAIL if the number of arguments is wrong. |
| 1589 | */ |
| 1590 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1591 | 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] | 1592 | { |
| 1593 | isn_T *isn; |
| 1594 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1595 | int argoff; |
Bram Moolenaar | a1224cb | 2020-10-22 12:31:49 +0200 | [diff] [blame] | 1596 | type_T **argtypes = NULL; |
Bram Moolenaar | 75ab91f | 2021-01-10 22:42:50 +0100 | [diff] [blame] | 1597 | type_T *maptype = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1598 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1599 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1600 | argoff = check_internal_func(func_idx, argcount); |
| 1601 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1602 | return FAIL; |
| 1603 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1604 | if (method_call && argoff > 1) |
| 1605 | { |
| 1606 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1607 | return FAIL; |
| 1608 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1609 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1610 | } |
| 1611 | |
Bram Moolenaar | af7a906 | 2020-10-21 16:49:17 +0200 | [diff] [blame] | 1612 | if (argcount > 0) |
| 1613 | { |
| 1614 | // Check the types of the arguments. |
| 1615 | argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount; |
| 1616 | if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL) |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 1617 | return FAIL; |
Bram Moolenaar | 75ab91f | 2021-01-10 22:42:50 +0100 | [diff] [blame] | 1618 | if (internal_func_is_map(func_idx)) |
| 1619 | maptype = *argtypes; |
Bram Moolenaar | af7a906 | 2020-10-21 16:49:17 +0200 | [diff] [blame] | 1620 | } |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 1621 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1622 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1623 | return FAIL; |
| 1624 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1625 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1626 | |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 1627 | // Drop the argument types and push the return type. |
| 1628 | stack->ga_len -= argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1629 | if (ga_grow(stack, 1) == FAIL) |
| 1630 | return FAIL; |
| 1631 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1632 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 1633 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1634 | |
Bram Moolenaar | 75ab91f | 2021-01-10 22:42:50 +0100 | [diff] [blame] | 1635 | if (maptype != NULL && maptype->tt_member != NULL |
| 1636 | && maptype->tt_member != &t_any) |
| 1637 | // Check that map() didn't change the item types. |
| 1638 | generate_TYPECHECK(cctx, maptype, -1); |
| 1639 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1640 | return OK; |
| 1641 | } |
| 1642 | |
| 1643 | /* |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 1644 | * Generate an ISN_LISTAPPEND instruction. Works like add(). |
| 1645 | * Argument count is already checked. |
| 1646 | */ |
| 1647 | static int |
| 1648 | generate_LISTAPPEND(cctx_T *cctx) |
| 1649 | { |
| 1650 | garray_T *stack = &cctx->ctx_type_stack; |
| 1651 | type_T *list_type; |
| 1652 | type_T *item_type; |
| 1653 | type_T *expected; |
| 1654 | |
| 1655 | // Caller already checked that list_type is a list. |
| 1656 | list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 1657 | item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1658 | expected = list_type->tt_member; |
| 1659 | if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL) |
| 1660 | return FAIL; |
| 1661 | |
| 1662 | if (generate_instr(cctx, ISN_LISTAPPEND) == NULL) |
| 1663 | return FAIL; |
| 1664 | |
| 1665 | --stack->ga_len; // drop the argument |
| 1666 | return OK; |
| 1667 | } |
| 1668 | |
| 1669 | /* |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 1670 | * Generate an ISN_BLOBAPPEND instruction. Works like add(). |
| 1671 | * Argument count is already checked. |
| 1672 | */ |
| 1673 | static int |
| 1674 | generate_BLOBAPPEND(cctx_T *cctx) |
| 1675 | { |
| 1676 | garray_T *stack = &cctx->ctx_type_stack; |
| 1677 | type_T *item_type; |
| 1678 | |
| 1679 | // Caller already checked that blob_type is a blob. |
| 1680 | item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1681 | if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL) |
| 1682 | return FAIL; |
| 1683 | |
| 1684 | if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL) |
| 1685 | return FAIL; |
| 1686 | |
| 1687 | --stack->ga_len; // drop the argument |
| 1688 | return OK; |
| 1689 | } |
| 1690 | |
| 1691 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1692 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1693 | * Return FAIL if the number of arguments is wrong. |
| 1694 | */ |
| 1695 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1696 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1697 | { |
| 1698 | isn_T *isn; |
| 1699 | garray_T *stack = &cctx->ctx_type_stack; |
| 1700 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1701 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1702 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1703 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1704 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1705 | { |
Bram Moolenaar | b185a40 | 2020-09-18 22:42:00 +0200 | [diff] [blame] | 1706 | semsg(_(e_toomanyarg), printable_func_name(ufunc)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1707 | return FAIL; |
| 1708 | } |
| 1709 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1710 | { |
Bram Moolenaar | b185a40 | 2020-09-18 22:42:00 +0200 | [diff] [blame] | 1711 | semsg(_(e_toofewarg), printable_func_name(ufunc)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1712 | return FAIL; |
| 1713 | } |
| 1714 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1715 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1716 | { |
| 1717 | int i; |
| 1718 | |
| 1719 | for (i = 0; i < argcount; ++i) |
| 1720 | { |
| 1721 | type_T *expected; |
| 1722 | type_T *actual; |
| 1723 | |
| 1724 | if (i < regular_args) |
| 1725 | { |
| 1726 | if (ufunc->uf_arg_types == NULL) |
| 1727 | continue; |
| 1728 | expected = ufunc->uf_arg_types[i]; |
| 1729 | } |
Bram Moolenaar | 2f8cbc4 | 2020-09-16 17:22:59 +0200 | [diff] [blame] | 1730 | else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any) |
| 1731 | // possibly a lambda or "...: any" |
Bram Moolenaar | 79e8db9 | 2020-08-14 22:16:33 +0200 | [diff] [blame] | 1732 | expected = &t_any; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1733 | else |
| 1734 | expected = ufunc->uf_va_type->tt_member; |
| 1735 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 1736 | if (need_type(actual, expected, -argcount + i, cctx, |
| 1737 | TRUE, FALSE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1738 | { |
| 1739 | arg_type_mismatch(expected, actual, i + 1); |
| 1740 | return FAIL; |
| 1741 | } |
| 1742 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1743 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1744 | if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL) |
| 1745 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1746 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1747 | } |
| 1748 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1749 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1750 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1751 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1752 | return FAIL; |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 1753 | if (isn->isn_type == ISN_DCALL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1754 | { |
| 1755 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1756 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1757 | } |
| 1758 | else |
| 1759 | { |
| 1760 | // A user function may be deleted and redefined later, can't use the |
| 1761 | // ufunc pointer, need to look it up again at runtime. |
| 1762 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1763 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1764 | } |
| 1765 | |
| 1766 | stack->ga_len -= argcount; // drop the arguments |
| 1767 | if (ga_grow(stack, 1) == FAIL) |
| 1768 | return FAIL; |
| 1769 | // add return value |
| 1770 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1771 | ++stack->ga_len; |
| 1772 | |
| 1773 | return OK; |
| 1774 | } |
| 1775 | |
| 1776 | /* |
| 1777 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1778 | */ |
| 1779 | static int |
| 1780 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1781 | { |
| 1782 | isn_T *isn; |
| 1783 | garray_T *stack = &cctx->ctx_type_stack; |
| 1784 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1785 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1786 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1787 | return FAIL; |
| 1788 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1789 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1790 | |
| 1791 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1792 | if (ga_grow(stack, 1) == FAIL) |
| 1793 | return FAIL; |
| 1794 | // add return value |
| 1795 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1796 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1797 | |
| 1798 | return OK; |
| 1799 | } |
| 1800 | |
| 1801 | /* |
| 1802 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1803 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1804 | */ |
| 1805 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1806 | generate_PCALL( |
| 1807 | cctx_T *cctx, |
| 1808 | int argcount, |
| 1809 | char_u *name, |
| 1810 | type_T *type, |
| 1811 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1812 | { |
| 1813 | isn_T *isn; |
| 1814 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1815 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1816 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1817 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1818 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1819 | if (type->tt_type == VAR_ANY) |
| 1820 | ret_type = &t_any; |
| 1821 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1822 | { |
| 1823 | if (type->tt_argcount != -1) |
| 1824 | { |
| 1825 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1826 | |
| 1827 | if (argcount < type->tt_min_argcount - varargs) |
| 1828 | { |
Bram Moolenaar | 6cf7e3b | 2020-10-28 14:31:16 +0100 | [diff] [blame] | 1829 | semsg(_(e_toofewarg), name); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1830 | return FAIL; |
| 1831 | } |
| 1832 | if (!varargs && argcount > type->tt_argcount) |
| 1833 | { |
Bram Moolenaar | 6cf7e3b | 2020-10-28 14:31:16 +0100 | [diff] [blame] | 1834 | semsg(_(e_toomanyarg), name); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1835 | return FAIL; |
| 1836 | } |
Bram Moolenaar | b4d16cb | 2020-11-05 18:45:46 +0100 | [diff] [blame] | 1837 | if (type->tt_args != NULL) |
| 1838 | { |
| 1839 | int i; |
| 1840 | |
| 1841 | for (i = 0; i < argcount; ++i) |
| 1842 | { |
| 1843 | int offset = -argcount + i - 1; |
| 1844 | type_T *actual = ((type_T **)stack->ga_data)[ |
| 1845 | stack->ga_len + offset]; |
| 1846 | type_T *expected; |
| 1847 | |
Bram Moolenaar | 32b3f82 | 2021-01-06 21:59:39 +0100 | [diff] [blame] | 1848 | if (varargs && i >= type->tt_argcount - 1) |
Bram Moolenaar | b4d16cb | 2020-11-05 18:45:46 +0100 | [diff] [blame] | 1849 | expected = type->tt_args[ |
Bram Moolenaar | 32b3f82 | 2021-01-06 21:59:39 +0100 | [diff] [blame] | 1850 | type->tt_argcount - 1]->tt_member; |
Bram Moolenaar | b4d16cb | 2020-11-05 18:45:46 +0100 | [diff] [blame] | 1851 | else |
| 1852 | expected = type->tt_args[i]; |
| 1853 | if (need_type(actual, expected, offset, |
| 1854 | cctx, TRUE, FALSE) == FAIL) |
| 1855 | { |
| 1856 | arg_type_mismatch(expected, actual, i + 1); |
| 1857 | return FAIL; |
| 1858 | } |
| 1859 | } |
| 1860 | } |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1861 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1862 | ret_type = type->tt_member; |
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 | else |
| 1865 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1866 | semsg(_(e_not_callable_type_str), name); |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1867 | return FAIL; |
| 1868 | } |
| 1869 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1870 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1871 | return FAIL; |
| 1872 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1873 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1874 | |
| 1875 | stack->ga_len -= argcount; // drop the arguments |
| 1876 | |
| 1877 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1878 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1879 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1880 | // If partial is above the arguments it must be cleared and replaced with |
| 1881 | // the return value. |
| 1882 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1883 | return FAIL; |
| 1884 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1885 | return OK; |
| 1886 | } |
| 1887 | |
| 1888 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1889 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1890 | */ |
| 1891 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1892 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1893 | { |
| 1894 | isn_T *isn; |
| 1895 | garray_T *stack = &cctx->ctx_type_stack; |
| 1896 | type_T *type; |
| 1897 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1898 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1899 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1900 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1901 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1902 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1903 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1904 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1905 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1906 | { |
| 1907 | emsg(_(e_dictreq)); |
| 1908 | return FAIL; |
| 1909 | } |
| 1910 | // change dict type to dict member type |
| 1911 | if (type->tt_type == VAR_DICT) |
Bram Moolenaar | 31a11b9 | 2021-01-10 19:23:27 +0100 | [diff] [blame] | 1912 | { |
| 1913 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = |
| 1914 | type->tt_member == &t_unknown ? &t_any : type->tt_member; |
| 1915 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1916 | |
| 1917 | return OK; |
| 1918 | } |
| 1919 | |
| 1920 | /* |
| 1921 | * Generate an ISN_ECHO instruction. |
| 1922 | */ |
| 1923 | static int |
| 1924 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1925 | { |
| 1926 | isn_T *isn; |
| 1927 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1928 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1929 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1930 | return FAIL; |
| 1931 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1932 | isn->isn_arg.echo.echo_count = count; |
| 1933 | |
| 1934 | return OK; |
| 1935 | } |
| 1936 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1937 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1938 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1939 | */ |
| 1940 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1941 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1942 | { |
| 1943 | isn_T *isn; |
| 1944 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1945 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1946 | return FAIL; |
| 1947 | isn->isn_arg.number = count; |
| 1948 | |
| 1949 | return OK; |
| 1950 | } |
| 1951 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 1952 | /* |
| 1953 | * Generate an ISN_PUT instruction. |
| 1954 | */ |
| 1955 | static int |
| 1956 | generate_PUT(cctx_T *cctx, int regname, linenr_T lnum) |
| 1957 | { |
| 1958 | isn_T *isn; |
| 1959 | |
| 1960 | RETURN_OK_IF_SKIP(cctx); |
| 1961 | if ((isn = generate_instr(cctx, ISN_PUT)) == NULL) |
| 1962 | return FAIL; |
| 1963 | isn->isn_arg.put.put_regname = regname; |
| 1964 | isn->isn_arg.put.put_lnum = lnum; |
| 1965 | return OK; |
| 1966 | } |
| 1967 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1968 | static int |
| 1969 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1970 | { |
| 1971 | isn_T *isn; |
| 1972 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1973 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1974 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1975 | return FAIL; |
| 1976 | isn->isn_arg.string = vim_strsave(line); |
| 1977 | return OK; |
| 1978 | } |
| 1979 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1980 | static int |
| 1981 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1982 | { |
| 1983 | isn_T *isn; |
| 1984 | |
| 1985 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1986 | return FAIL; |
| 1987 | isn->isn_arg.number = count; |
| 1988 | return OK; |
| 1989 | } |
| 1990 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 1991 | /* |
| 1992 | * Generate ISN_RANGE. Consumes "range". Return OK/FAIL. |
| 1993 | */ |
| 1994 | static int |
| 1995 | generate_RANGE(cctx_T *cctx, char_u *range) |
| 1996 | { |
| 1997 | isn_T *isn; |
| 1998 | garray_T *stack = &cctx->ctx_type_stack; |
| 1999 | |
| 2000 | if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL) |
| 2001 | return FAIL; |
| 2002 | isn->isn_arg.string = range; |
| 2003 | |
| 2004 | if (ga_grow(stack, 1) == FAIL) |
| 2005 | return FAIL; |
| 2006 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_number; |
| 2007 | ++stack->ga_len; |
| 2008 | return OK; |
| 2009 | } |
| 2010 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 2011 | static int |
| 2012 | generate_UNPACK(cctx_T *cctx, int var_count, int semicolon) |
| 2013 | { |
| 2014 | isn_T *isn; |
| 2015 | |
| 2016 | RETURN_OK_IF_SKIP(cctx); |
| 2017 | if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL) |
| 2018 | return FAIL; |
| 2019 | isn->isn_arg.unpack.unp_count = var_count; |
| 2020 | isn->isn_arg.unpack.unp_semicolon = semicolon; |
| 2021 | return OK; |
| 2022 | } |
| 2023 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2024 | /* |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2025 | * Generate an instruction for any command modifiers. |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2026 | */ |
| 2027 | static int |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 2028 | generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod) |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2029 | { |
| 2030 | isn_T *isn; |
| 2031 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2032 | if (cmod->cmod_flags != 0 |
| 2033 | || cmod->cmod_split != 0 |
| 2034 | || cmod->cmod_verbose != 0 |
| 2035 | || cmod->cmod_tab != 0 |
| 2036 | || cmod->cmod_filter_regmatch.regprog != NULL) |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2037 | { |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2038 | cctx->ctx_has_cmdmod = TRUE; |
| 2039 | |
| 2040 | if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL) |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2041 | return FAIL; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2042 | isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T); |
| 2043 | if (isn->isn_arg.cmdmod.cf_cmdmod == NULL) |
| 2044 | return FAIL; |
| 2045 | mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T)); |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 2046 | // filter program now belongs to the instruction |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2047 | cmod->cmod_filter_regmatch.regprog = NULL; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2048 | } |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2049 | |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2050 | return OK; |
| 2051 | } |
| 2052 | |
| 2053 | static int |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 2054 | generate_undo_cmdmods(cctx_T *cctx) |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2055 | { |
Bram Moolenaar | f665e97 | 2020-12-05 19:17:16 +0100 | [diff] [blame] | 2056 | if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL) |
| 2057 | return FAIL; |
Bram Moolenaar | 7cd2422 | 2021-01-12 18:58:39 +0100 | [diff] [blame] | 2058 | cctx->ctx_has_cmdmod = FALSE; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2059 | return OK; |
| 2060 | } |
| 2061 | |
| 2062 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2063 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2064 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2065 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2066 | static lvar_T * |
Bram Moolenaar | e8211a3 | 2020-10-09 22:04:29 +0200 | [diff] [blame] | 2067 | reserve_local( |
| 2068 | cctx_T *cctx, |
| 2069 | char_u *name, |
| 2070 | size_t len, |
| 2071 | int isConst, |
| 2072 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2073 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2074 | lvar_T *lvar; |
| 2075 | |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2076 | if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2077 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2078 | emsg_namelen(_(e_str_is_used_as_argument), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2079 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2083 | return NULL; |
| 2084 | 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] | 2085 | CLEAR_POINTER(lvar); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2086 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2087 | // Every local variable uses the next entry on the stack. We could re-use |
| 2088 | // the last ones when leaving a scope, but then variables used in a closure |
| 2089 | // might get overwritten. To keep things simple do not re-use stack |
| 2090 | // entries. This is less efficient, but memory is cheap these days. |
| 2091 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 2092 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 2093 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2094 | lvar->lv_const = isConst; |
| 2095 | lvar->lv_type = type; |
| 2096 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2097 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2101 | * Remove local variables above "new_top". |
| 2102 | */ |
| 2103 | static void |
| 2104 | unwind_locals(cctx_T *cctx, int new_top) |
| 2105 | { |
| 2106 | if (cctx->ctx_locals.ga_len > new_top) |
| 2107 | { |
| 2108 | int idx; |
| 2109 | lvar_T *lvar; |
| 2110 | |
| 2111 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 2112 | { |
| 2113 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 2114 | vim_free(lvar->lv_name); |
| 2115 | } |
| 2116 | } |
| 2117 | cctx->ctx_locals.ga_len = new_top; |
| 2118 | } |
| 2119 | |
| 2120 | /* |
| 2121 | * Free all local variables. |
| 2122 | */ |
| 2123 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2124 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2125 | { |
| 2126 | unwind_locals(cctx, 0); |
| 2127 | ga_clear(&cctx->ctx_locals); |
| 2128 | } |
| 2129 | |
| 2130 | /* |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 2131 | * If "check_writable" is ASSIGN_CONST give an error if the variable was |
| 2132 | * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an |
| 2133 | * error if the variable was defined with :const. |
| 2134 | */ |
| 2135 | static int |
| 2136 | check_item_writable(svar_T *sv, int check_writable, char_u *name) |
| 2137 | { |
| 2138 | if ((check_writable == ASSIGN_CONST && sv->sv_const != 0) |
| 2139 | || (check_writable == ASSIGN_FINAL |
| 2140 | && sv->sv_const == ASSIGN_CONST)) |
| 2141 | { |
| 2142 | semsg(_(e_readonlyvar), name); |
| 2143 | return FAIL; |
| 2144 | } |
| 2145 | return OK; |
| 2146 | } |
| 2147 | |
| 2148 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2149 | * Find "name" in script-local items of script "sid". |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 2150 | * Pass "check_writable" to check_item_writable(). |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2151 | * Returns the index in "sn_var_vals" if found. |
| 2152 | * If found but not in "sn_var_vals" returns -1. |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 2153 | * If not found or the variable is not writable returns -2. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2154 | */ |
| 2155 | int |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2156 | 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] | 2157 | { |
| 2158 | hashtab_T *ht; |
| 2159 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2160 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2161 | svar_T *sv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2162 | int idx; |
| 2163 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2164 | if (!SCRIPT_ID_VALID(sid)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2165 | return -1; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2166 | if (sid == current_sctx.sc_sid) |
| 2167 | { |
Bram Moolenaar | 209f020 | 2020-10-15 13:57:56 +0200 | [diff] [blame] | 2168 | sallvar_T *sav = find_script_var(name, 0, cctx); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2169 | |
| 2170 | if (sav == NULL) |
| 2171 | return -2; |
| 2172 | idx = sav->sav_var_vals_idx; |
| 2173 | sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 2174 | if (check_item_writable(sv, check_writable, name) == FAIL) |
| 2175 | return -2; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2176 | return idx; |
| 2177 | } |
| 2178 | |
| 2179 | // First look the name up in the hashtable. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2180 | ht = &SCRIPT_VARS(sid); |
| 2181 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2182 | if (di == NULL) |
| 2183 | return -2; |
| 2184 | |
| 2185 | // Now find the svar_T index in sn_var_vals. |
| 2186 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2187 | { |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2188 | sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2189 | if (sv->sv_tv == &di->di_tv) |
| 2190 | { |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 2191 | if (check_item_writable(sv, check_writable, name) == FAIL) |
| 2192 | return -2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2193 | return idx; |
| 2194 | } |
| 2195 | } |
| 2196 | return -1; |
| 2197 | } |
| 2198 | |
| 2199 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2200 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2201 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2202 | */ |
| 2203 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2204 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2205 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2206 | int idx; |
| 2207 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2208 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2209 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2210 | if (cctx != NULL) |
| 2211 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2212 | { |
| 2213 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2214 | + idx; |
| 2215 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2216 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2217 | : STRLEN(import->imp_name) == len |
| 2218 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2219 | return import; |
| 2220 | } |
| 2221 | |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 2222 | return find_imported_in_script(name, len, current_sctx.sc_sid); |
| 2223 | } |
| 2224 | |
| 2225 | imported_T * |
| 2226 | find_imported_in_script(char_u *name, size_t len, int sid) |
| 2227 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2228 | scriptitem_T *si; |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 2229 | int idx; |
| 2230 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2231 | if (!SCRIPT_ID_VALID(sid)) |
| 2232 | return NULL; |
| 2233 | si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2234 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2235 | { |
| 2236 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2237 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2238 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2239 | : STRLEN(import->imp_name) == len |
| 2240 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2241 | return import; |
| 2242 | } |
| 2243 | return NULL; |
| 2244 | } |
| 2245 | |
| 2246 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2247 | * Free all imported variables. |
| 2248 | */ |
| 2249 | static void |
| 2250 | free_imported(cctx_T *cctx) |
| 2251 | { |
| 2252 | int idx; |
| 2253 | |
| 2254 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2255 | { |
| 2256 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2257 | |
| 2258 | vim_free(import->imp_name); |
| 2259 | } |
| 2260 | ga_clear(&cctx->ctx_imports); |
| 2261 | } |
| 2262 | |
| 2263 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2264 | * Return a pointer to the next line that isn't empty or only contains a |
| 2265 | * comment. Skips over white space. |
| 2266 | * Returns NULL if there is none. |
| 2267 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2268 | char_u * |
| 2269 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2270 | { |
| 2271 | int lnum = cctx->ctx_lnum; |
| 2272 | |
| 2273 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2274 | { |
| 2275 | 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] | 2276 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2277 | |
Bram Moolenaar | ba60cc4 | 2020-08-12 19:15:33 +0200 | [diff] [blame] | 2278 | // ignore NULLs inserted for continuation lines |
| 2279 | if (line != NULL) |
| 2280 | { |
| 2281 | p = skipwhite(line); |
| 2282 | if (*p != NUL && !vim9_comment_start(p)) |
| 2283 | return p; |
| 2284 | } |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2285 | } |
| 2286 | return NULL; |
| 2287 | } |
| 2288 | |
| 2289 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2290 | * Called when checking for a following operator at "arg". When the rest of |
| 2291 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2292 | * line return a pointer to it and set "nextp". |
| 2293 | * Otherwise skip over white space. |
| 2294 | */ |
| 2295 | static char_u * |
| 2296 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2297 | { |
| 2298 | char_u *p = skipwhite(arg); |
| 2299 | |
| 2300 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2301 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2302 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2303 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2304 | if (*nextp != NULL) |
| 2305 | return *nextp; |
| 2306 | } |
| 2307 | return p; |
| 2308 | } |
| 2309 | |
| 2310 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2311 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2312 | * 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] | 2313 | * Returns NULL when at the end. |
| 2314 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2315 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2316 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2317 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2318 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2319 | |
| 2320 | do |
| 2321 | { |
| 2322 | ++cctx->ctx_lnum; |
| 2323 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2324 | { |
| 2325 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2326 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2327 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2328 | 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] | 2329 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2330 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2331 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 2332 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2333 | return line; |
| 2334 | } |
| 2335 | |
| 2336 | /* |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 2337 | * Skip over white space at "whitep" and assign to "*arg". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2338 | * 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] | 2339 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2340 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2341 | */ |
| 2342 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2343 | 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] | 2344 | { |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 2345 | *arg = skipwhite(whitep); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2346 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2347 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2348 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2349 | |
| 2350 | if (next == NULL) |
| 2351 | return FAIL; |
| 2352 | *arg = skipwhite(next); |
| 2353 | } |
| 2354 | return OK; |
| 2355 | } |
| 2356 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2357 | /* |
| 2358 | * Idem, and give an error when failed. |
| 2359 | */ |
| 2360 | static int |
| 2361 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2362 | { |
| 2363 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2364 | { |
Bram Moolenaar | 8ff16e0 | 2020-12-07 21:49:52 +0100 | [diff] [blame] | 2365 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2366 | emsg(_(e_line_incomplete)); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2367 | return FAIL; |
| 2368 | } |
| 2369 | return OK; |
| 2370 | } |
| 2371 | |
| 2372 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2373 | // Structure passed between the compile_expr* functions to keep track of |
| 2374 | // constants that have been parsed but for which no code was produced yet. If |
| 2375 | // possible expressions on these constants are applied at compile time. If |
| 2376 | // that is not possible, the code to push the constants needs to be generated |
| 2377 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2378 | // Using 50 should be more than enough of 5 levels of (). |
| 2379 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2380 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2381 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2382 | int pp_used; // active entries in pp_tv[] |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2383 | int pp_is_const; // all generated code was constants, used for a |
| 2384 | // list or dict with constant members |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2385 | } ppconst_T; |
| 2386 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2387 | 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] | 2388 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2389 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2390 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2391 | /* |
| 2392 | * Generate a PUSH instruction for "tv". |
| 2393 | * "tv" will be consumed or cleared. |
| 2394 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2395 | */ |
| 2396 | static int |
| 2397 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2398 | { |
| 2399 | if (tv != NULL) |
| 2400 | { |
| 2401 | switch (tv->v_type) |
| 2402 | { |
| 2403 | case VAR_UNKNOWN: |
| 2404 | break; |
| 2405 | case VAR_BOOL: |
| 2406 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2407 | break; |
| 2408 | case VAR_SPECIAL: |
| 2409 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2410 | break; |
| 2411 | case VAR_NUMBER: |
| 2412 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2413 | break; |
| 2414 | #ifdef FEAT_FLOAT |
| 2415 | case VAR_FLOAT: |
| 2416 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2417 | break; |
| 2418 | #endif |
| 2419 | case VAR_BLOB: |
| 2420 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2421 | tv->vval.v_blob = NULL; |
| 2422 | break; |
| 2423 | case VAR_STRING: |
| 2424 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2425 | tv->vval.v_string = NULL; |
| 2426 | break; |
| 2427 | default: |
| 2428 | iemsg("constant type not supported"); |
| 2429 | clear_tv(tv); |
| 2430 | return FAIL; |
| 2431 | } |
| 2432 | tv->v_type = VAR_UNKNOWN; |
| 2433 | } |
| 2434 | return OK; |
| 2435 | } |
| 2436 | |
| 2437 | /* |
| 2438 | * Generate code for any ppconst entries. |
| 2439 | */ |
| 2440 | static int |
| 2441 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2442 | { |
| 2443 | int i; |
| 2444 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2445 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2446 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2447 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2448 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2449 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2450 | ret = FAIL; |
| 2451 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2452 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2453 | return ret; |
| 2454 | } |
| 2455 | |
| 2456 | /* |
| 2457 | * Clear ppconst constants. Used when failing. |
| 2458 | */ |
| 2459 | static void |
| 2460 | clear_ppconst(ppconst_T *ppconst) |
| 2461 | { |
| 2462 | int i; |
| 2463 | |
| 2464 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2465 | clear_tv(&ppconst->pp_tv[i]); |
| 2466 | ppconst->pp_used = 0; |
| 2467 | } |
| 2468 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2469 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2470 | * Generate an instruction to load script-local variable "name", without the |
| 2471 | * leading "s:". |
| 2472 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2473 | */ |
| 2474 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2475 | compile_load_scriptvar( |
| 2476 | cctx_T *cctx, |
| 2477 | char_u *name, // variable NUL terminated |
| 2478 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2479 | char_u **end, // end of variable |
| 2480 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2481 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2482 | scriptitem_T *si; |
| 2483 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2484 | imported_T *import; |
| 2485 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2486 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
| 2487 | return FAIL; |
| 2488 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 2489 | idx = get_script_item_idx(current_sctx.sc_sid, name, 0, cctx); |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2490 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2491 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2492 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2493 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2494 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2495 | } |
| 2496 | if (idx >= 0) |
| 2497 | { |
| 2498 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2499 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2500 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2501 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2502 | return OK; |
| 2503 | } |
| 2504 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2505 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2506 | if (import != NULL) |
| 2507 | { |
Bram Moolenaar | a629495 | 2020-12-27 13:39:50 +0100 | [diff] [blame] | 2508 | if (import->imp_flags & IMP_FLAGS_STAR) |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2509 | { |
| 2510 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2511 | char_u *exp_name; |
| 2512 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2513 | ufunc_T *ufunc; |
| 2514 | type_T *type; |
| 2515 | |
| 2516 | // Used "import * as Name", need to lookup the member. |
| 2517 | if (*p != '.') |
| 2518 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2519 | semsg(_(e_expected_dot_after_name_str), start); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2520 | return FAIL; |
| 2521 | } |
| 2522 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2523 | if (VIM_ISWHITE(*p)) |
| 2524 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2525 | emsg(_(e_no_white_space_allowed_after_dot)); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2526 | return FAIL; |
| 2527 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2528 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2529 | // isolate one name |
| 2530 | exp_name = p; |
| 2531 | while (eval_isnamec(*p)) |
| 2532 | ++p; |
| 2533 | cc = *p; |
| 2534 | *p = NUL; |
| 2535 | |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2536 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2537 | *p = cc; |
| 2538 | p = skipwhite(p); |
| 2539 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2540 | // TODO: what if it is a function? |
| 2541 | if (idx < 0) |
| 2542 | return FAIL; |
| 2543 | *end = p; |
| 2544 | |
| 2545 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2546 | import->imp_sid, |
| 2547 | idx, |
| 2548 | type); |
| 2549 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2550 | else if (import->imp_funcname != NULL) |
| 2551 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2552 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2553 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2554 | import->imp_sid, |
| 2555 | import->imp_var_vals_idx, |
| 2556 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2557 | return OK; |
| 2558 | } |
| 2559 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2560 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2561 | semsg(_(e_item_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2562 | return FAIL; |
| 2563 | } |
| 2564 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2565 | static int |
| 2566 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2567 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2568 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2569 | |
| 2570 | if (ufunc == NULL) |
| 2571 | return FAIL; |
| 2572 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2573 | // Need to compile any default values to get the argument types. |
| 2574 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2575 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2576 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2577 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2578 | } |
| 2579 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2580 | /* |
| 2581 | * Compile a variable name into a load instruction. |
| 2582 | * "end" points to just after the name. |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2583 | * "is_expr" is TRUE when evaluating an expression, might be a funcref. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2584 | * When "error" is FALSE do not give an error when not found. |
| 2585 | */ |
| 2586 | static int |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2587 | compile_load( |
| 2588 | char_u **arg, |
| 2589 | char_u *end_arg, |
| 2590 | cctx_T *cctx, |
| 2591 | int is_expr, |
| 2592 | int error) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2593 | { |
| 2594 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2595 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2596 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2597 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2598 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2599 | |
| 2600 | if (*(*arg + 1) == ':') |
| 2601 | { |
| 2602 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2603 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2604 | { |
| 2605 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2606 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2607 | switch (**arg) |
| 2608 | { |
| 2609 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2610 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2611 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2612 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2613 | default: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2614 | semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2615 | goto theend; |
| 2616 | } |
| 2617 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2618 | goto theend; |
| 2619 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2620 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2621 | else |
| 2622 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2623 | isntype_T isn_type = ISN_DROP; |
| 2624 | |
| 2625 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2626 | if (name == NULL) |
| 2627 | return FAIL; |
| 2628 | |
| 2629 | switch (**arg) |
| 2630 | { |
| 2631 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2632 | break; |
| 2633 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2634 | NULL, NULL, error); |
| 2635 | break; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2636 | case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL) |
| 2637 | isn_type = ISN_LOADG; |
| 2638 | else |
| 2639 | { |
| 2640 | isn_type = ISN_LOADAUTO; |
| 2641 | vim_free(name); |
| 2642 | name = vim_strnsave(*arg, end - *arg); |
| 2643 | if (name == NULL) |
| 2644 | return FAIL; |
| 2645 | } |
| 2646 | break; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2647 | case 'w': isn_type = ISN_LOADW; break; |
| 2648 | case 't': isn_type = ISN_LOADT; break; |
| 2649 | case 'b': isn_type = ISN_LOADB; break; |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 2650 | default: // cannot happen, just in case |
| 2651 | semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2652 | goto theend; |
| 2653 | } |
| 2654 | if (isn_type != ISN_DROP) |
| 2655 | { |
| 2656 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2657 | // variables can be defined later, thus we don't check if it |
| 2658 | // exists, give error at runtime. |
| 2659 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2660 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2661 | } |
| 2662 | } |
| 2663 | else |
| 2664 | { |
| 2665 | size_t len = end - *arg; |
| 2666 | int idx; |
| 2667 | int gen_load = FALSE; |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 2668 | int gen_load_outer = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2669 | |
| 2670 | name = vim_strnsave(*arg, end - *arg); |
| 2671 | if (name == NULL) |
| 2672 | return FAIL; |
| 2673 | |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2674 | if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2675 | { |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 2676 | if (gen_load_outer == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2677 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2678 | } |
| 2679 | else |
| 2680 | { |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 2681 | lvar_T lvar; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2682 | |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 2683 | if (lookup_local(*arg, len, &lvar, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2684 | { |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 2685 | type = lvar.lv_type; |
| 2686 | idx = lvar.lv_idx; |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 2687 | if (lvar.lv_from_outer != 0) |
| 2688 | gen_load_outer = lvar.lv_from_outer; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2689 | else |
| 2690 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2691 | } |
| 2692 | else |
| 2693 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2694 | // "var" can be script-local even without using "s:" if it |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2695 | // already exists in a Vim9 script or when it's imported. |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2696 | if (script_var_exists(*arg, len, TRUE, cctx) == OK |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2697 | || find_imported(name, 0, cctx) != NULL) |
| 2698 | res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2699 | |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2700 | // When evaluating an expression and the name starts with an |
| 2701 | // uppercase letter or "x:" it can be a user defined function. |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2702 | // TODO: this is just guessing |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2703 | if (res == FAIL && is_expr |
| 2704 | && (ASCII_ISUPPER(*name) || name[1] == ':')) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2705 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2706 | } |
| 2707 | } |
| 2708 | if (gen_load) |
| 2709 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 2710 | if (gen_load_outer > 0) |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2711 | { |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 2712 | res = generate_LOADOUTER(cctx, idx, gen_load_outer, type); |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2713 | cctx->ctx_outer_used = TRUE; |
| 2714 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | *arg = end; |
| 2718 | |
| 2719 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2720 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2721 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2722 | vim_free(name); |
| 2723 | return res; |
| 2724 | } |
| 2725 | |
| 2726 | /* |
| 2727 | * Compile the argument expressions. |
| 2728 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2729 | */ |
| 2730 | static int |
| 2731 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2732 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2733 | char_u *p = *arg; |
| 2734 | char_u *whitep = *arg; |
Bram Moolenaar | 10e4f12 | 2020-09-20 22:43:52 +0200 | [diff] [blame] | 2735 | int must_end = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2736 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2737 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2738 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2739 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2740 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2741 | if (*p == ')') |
| 2742 | { |
| 2743 | *arg = p + 1; |
| 2744 | return OK; |
| 2745 | } |
Bram Moolenaar | 10e4f12 | 2020-09-20 22:43:52 +0200 | [diff] [blame] | 2746 | if (must_end) |
| 2747 | { |
| 2748 | semsg(_(e_missing_comma_before_argument_str), p); |
| 2749 | return FAIL; |
| 2750 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2751 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2752 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2753 | return FAIL; |
| 2754 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2755 | |
| 2756 | if (*p != ',' && *skipwhite(p) == ',') |
| 2757 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2758 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2759 | p = skipwhite(p); |
| 2760 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2761 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2762 | { |
| 2763 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2764 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2765 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2766 | } |
Bram Moolenaar | 10e4f12 | 2020-09-20 22:43:52 +0200 | [diff] [blame] | 2767 | else |
| 2768 | must_end = TRUE; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2769 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2770 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2771 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2772 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2773 | emsg(_(e_missing_close)); |
| 2774 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2775 | } |
| 2776 | |
| 2777 | /* |
| 2778 | * Compile a function call: name(arg1, arg2) |
| 2779 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2780 | * "argcount_init" is 1 for "value->method()" |
| 2781 | * Instructions: |
| 2782 | * EVAL arg1 |
| 2783 | * EVAL arg2 |
| 2784 | * BCALL / DCALL / UCALL |
| 2785 | */ |
| 2786 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2787 | compile_call( |
| 2788 | char_u **arg, |
| 2789 | size_t varlen, |
| 2790 | cctx_T *cctx, |
| 2791 | ppconst_T *ppconst, |
| 2792 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2793 | { |
| 2794 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2795 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2796 | int argcount = argcount_init; |
| 2797 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2798 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2799 | char_u *tofree = NULL; |
| 2800 | int error = FCERR_NONE; |
Bram Moolenaar | b3a0194 | 2020-11-17 19:56:09 +0100 | [diff] [blame] | 2801 | ufunc_T *ufunc = NULL; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2802 | int res = FAIL; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2803 | int is_autoload; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2804 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2805 | // we can evaluate "has('name')" at compile time |
| 2806 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2807 | { |
| 2808 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2809 | typval_T argvars[2]; |
| 2810 | |
| 2811 | argvars[0].v_type = VAR_UNKNOWN; |
| 2812 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2813 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2814 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2815 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2816 | s = skipwhite(s); |
Bram Moolenaar | 8cebd43 | 2020-11-08 12:49:47 +0100 | [diff] [blame] | 2817 | if (*s == ')' && argvars[0].v_type == VAR_STRING |
| 2818 | && !dynamic_feature(argvars[0].vval.v_string)) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2819 | { |
| 2820 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2821 | |
| 2822 | *arg = s + 1; |
| 2823 | argvars[1].v_type = VAR_UNKNOWN; |
| 2824 | tv->v_type = VAR_NUMBER; |
| 2825 | tv->vval.v_number = 0; |
| 2826 | f_has(argvars, tv); |
| 2827 | clear_tv(&argvars[0]); |
| 2828 | ++ppconst->pp_used; |
| 2829 | return OK; |
| 2830 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2831 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2835 | return FAIL; |
| 2836 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2837 | if (varlen >= sizeof(namebuf)) |
| 2838 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2839 | semsg(_(e_name_too_long_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2840 | return FAIL; |
| 2841 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2842 | vim_strncpy(namebuf, *arg, varlen); |
| 2843 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2844 | |
| 2845 | *arg = skipwhite(*arg + varlen + 1); |
| 2846 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2847 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2848 | |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2849 | is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2850 | if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2851 | { |
| 2852 | int idx; |
| 2853 | |
| 2854 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2855 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2856 | if (idx >= 0) |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 2857 | { |
| 2858 | if (STRCMP(name, "add") == 0 && argcount == 2) |
| 2859 | { |
| 2860 | garray_T *stack = &cctx->ctx_type_stack; |
| 2861 | type_T *type = ((type_T **)stack->ga_data)[ |
| 2862 | stack->ga_len - 2]; |
| 2863 | |
Bram Moolenaar | e88c8e8 | 2020-11-01 17:03:37 +0100 | [diff] [blame] | 2864 | // add() can be compiled to instructions if we know the type |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 2865 | if (type->tt_type == VAR_LIST) |
| 2866 | { |
| 2867 | // inline "add(list, item)" so that the type can be checked |
| 2868 | res = generate_LISTAPPEND(cctx); |
| 2869 | idx = -1; |
| 2870 | } |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 2871 | else if (type->tt_type == VAR_BLOB) |
| 2872 | { |
| 2873 | // inline "add(blob, nr)" so that the type can be checked |
| 2874 | res = generate_BLOBAPPEND(cctx); |
| 2875 | idx = -1; |
| 2876 | } |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | if (idx >= 0) |
| 2880 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
| 2881 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2882 | else |
| 2883 | semsg(_(e_unknownfunc), namebuf); |
| 2884 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2885 | } |
| 2886 | |
Bram Moolenaar | 52bf81c | 2020-11-17 18:50:44 +0100 | [diff] [blame] | 2887 | // An argument or local variable can be a function reference, this |
| 2888 | // overrules a function name. |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 2889 | if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL |
Bram Moolenaar | 52bf81c | 2020-11-17 18:50:44 +0100 | [diff] [blame] | 2890 | && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2891 | { |
Bram Moolenaar | 52bf81c | 2020-11-17 18:50:44 +0100 | [diff] [blame] | 2892 | // If we can find the function by name generate the right call. |
| 2893 | // Skip global functions here, a local funcref takes precedence. |
| 2894 | ufunc = find_func(name, FALSE, cctx); |
| 2895 | if (ufunc != NULL && !func_is_global(ufunc)) |
| 2896 | { |
| 2897 | res = generate_CALL(cctx, ufunc, argcount); |
| 2898 | goto theend; |
| 2899 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2900 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2901 | |
| 2902 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2903 | // 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] | 2904 | // Not for eome#Func(), it will be loaded later. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2905 | p = namebuf; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2906 | if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2907 | && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2908 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2909 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 2910 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2911 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2912 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2913 | goto theend; |
| 2914 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2915 | |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2916 | // If we can find a global function by name generate the right call. |
| 2917 | if (ufunc != NULL) |
| 2918 | { |
| 2919 | res = generate_CALL(cctx, ufunc, argcount); |
| 2920 | goto theend; |
| 2921 | } |
| 2922 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2923 | // 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] | 2924 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2925 | if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload) |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2926 | res = generate_UCALL(cctx, name, argcount); |
| 2927 | else |
| 2928 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2929 | |
| 2930 | theend: |
| 2931 | vim_free(tofree); |
| 2932 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2936 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2937 | |
| 2938 | /* |
| 2939 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2940 | * does not recognize magic braces. |
Bram Moolenaar | bebaa0d | 2020-11-20 18:59:19 +0100 | [diff] [blame] | 2941 | * When "use_namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2942 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2943 | * valid name. |
| 2944 | */ |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 2945 | char_u * |
Bram Moolenaar | bebaa0d | 2020-11-20 18:59:19 +0100 | [diff] [blame] | 2946 | to_name_end(char_u *arg, int use_namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2947 | { |
| 2948 | char_u *p; |
| 2949 | |
| 2950 | // Quick check for valid starting character. |
| 2951 | if (!eval_isnamec1(*arg)) |
| 2952 | return arg; |
| 2953 | |
| 2954 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2955 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2956 | // and can be used in slice "[n:]". |
| 2957 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | bebaa0d | 2020-11-20 18:59:19 +0100 | [diff] [blame] | 2958 | || !use_namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2959 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2960 | break; |
| 2961 | return p; |
| 2962 | } |
| 2963 | |
| 2964 | /* |
| 2965 | * 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] | 2966 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2967 | */ |
| 2968 | char_u * |
| 2969 | to_name_const_end(char_u *arg) |
| 2970 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2971 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2972 | typval_T rettv; |
| 2973 | |
| 2974 | if (p == arg && *arg == '[') |
| 2975 | { |
| 2976 | |
| 2977 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2978 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2979 | p = arg; |
| 2980 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2981 | return p; |
| 2982 | } |
| 2983 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2984 | /* |
| 2985 | * parse a list: [expr, expr] |
| 2986 | * "*arg" points to the '['. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2987 | * ppconst->pp_is_const is set if all items are a constant. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2988 | */ |
| 2989 | static int |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2990 | compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2991 | { |
| 2992 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2993 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2994 | int count = 0; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2995 | int is_const; |
| 2996 | int is_all_const = TRUE; // reset when non-const encountered |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2997 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2998 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2999 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3000 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3001 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3002 | semsg(_(e_list_end), *arg); |
| 3003 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3004 | } |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 3005 | if (*p == ',') |
| 3006 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3007 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 3008 | return FAIL; |
| 3009 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3010 | if (*p == ']') |
| 3011 | { |
| 3012 | ++p; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3013 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 3014 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3015 | if (compile_expr0_ext(&p, cctx, &is_const) == FAIL) |
Bram Moolenaar | c1f0066 | 2020-10-03 13:41:53 +0200 | [diff] [blame] | 3016 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3017 | if (!is_const) |
| 3018 | is_all_const = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3019 | ++count; |
| 3020 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3021 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3022 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3023 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 3024 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3025 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 3026 | return FAIL; |
| 3027 | } |
| 3028 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3029 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3030 | p = skipwhite(p); |
| 3031 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3032 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3033 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3034 | ppconst->pp_is_const = is_all_const; |
| 3035 | return generate_NEWLIST(cctx, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | /* |
Bram Moolenaar | 2949cfd | 2020-12-31 21:28:47 +0100 | [diff] [blame] | 3039 | * Parse a lambda: "(arg, arg) => expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3040 | * "*arg" points to the '{'. |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 3041 | * 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] | 3042 | */ |
| 3043 | static int |
| 3044 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 3045 | { |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 3046 | int r; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3047 | typval_T rettv; |
| 3048 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3049 | evalarg_T evalarg; |
| 3050 | |
| 3051 | CLEAR_FIELD(evalarg); |
| 3052 | evalarg.eval_flags = EVAL_EVALUATE; |
| 3053 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3054 | |
| 3055 | // Get the funcref in "rettv". |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 3056 | r = get_lambda_tv(arg, &rettv, TRUE, &evalarg); |
| 3057 | if (r != OK) |
Bram Moolenaar | 2ea79ad | 2020-10-18 23:32:13 +0200 | [diff] [blame] | 3058 | { |
| 3059 | clear_evalarg(&evalarg, NULL); |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 3060 | return r; |
Bram Moolenaar | 2ea79ad | 2020-10-18 23:32:13 +0200 | [diff] [blame] | 3061 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3062 | |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 3063 | // "rettv" will now be a partial referencing the function. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3064 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3065 | ++ufunc->uf_refcount; |
| 3066 | clear_tv(&rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3067 | |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 3068 | // Compile the function into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 3069 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3070 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 3071 | clear_evalarg(&evalarg, NULL); |
| 3072 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 3073 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 3074 | { |
| 3075 | // The return type will now be known. |
| 3076 | set_function_type(ufunc); |
| 3077 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 3078 | // The function reference count will be 1. When the ISN_FUNCREF |
| 3079 | // instruction is deleted the reference count is decremented and the |
| 3080 | // function is freed. |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 3081 | return generate_FUNCREF(cctx, ufunc); |
| 3082 | } |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3083 | |
| 3084 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3085 | return FAIL; |
| 3086 | } |
| 3087 | |
| 3088 | /* |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3089 | * parse a dict: {key: val, [key]: val} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3090 | * "*arg" points to the '{'. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3091 | * 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] | 3092 | */ |
| 3093 | static int |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3094 | compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3095 | { |
| 3096 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3097 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3098 | int count = 0; |
| 3099 | dict_T *d = dict_alloc(); |
| 3100 | dictitem_T *item; |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3101 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3102 | char_u *p; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3103 | int is_const; |
| 3104 | int is_all_const = TRUE; // reset when non-const encountered |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3105 | |
| 3106 | if (d == NULL) |
| 3107 | return FAIL; |
Bram Moolenaar | d62d87d | 2021-01-04 17:40:12 +0100 | [diff] [blame] | 3108 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3109 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3110 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3111 | { |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3112 | char_u *key = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3113 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3114 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3115 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3116 | *arg = NULL; |
| 3117 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | if (**arg == '}') |
| 3121 | break; |
| 3122 | |
Bram Moolenaar | c5e6a71 | 2020-12-04 19:12:14 +0100 | [diff] [blame] | 3123 | if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3124 | { |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3125 | isn_T *isn; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3126 | |
Bram Moolenaar | c5e6a71 | 2020-12-04 19:12:14 +0100 | [diff] [blame] | 3127 | // {[expr]: value} uses an evaluated key. |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3128 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3129 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3130 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3131 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3132 | if (isn->isn_type == ISN_PUSHS) |
| 3133 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3134 | else |
| 3135 | { |
| 3136 | type_T *keytype = ((type_T **)stack->ga_data) |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3137 | [stack->ga_len - 1]; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3138 | if (need_type(keytype, &t_string, -1, cctx, |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3139 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3140 | return FAIL; |
| 3141 | } |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3142 | *arg = skipwhite(*arg); |
| 3143 | if (**arg != ']') |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3144 | { |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3145 | emsg(_(e_missing_matching_bracket_after_dict_key)); |
| 3146 | return FAIL; |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3147 | } |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3148 | ++*arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3149 | } |
Bram Moolenaar | c5e6a71 | 2020-12-04 19:12:14 +0100 | [diff] [blame] | 3150 | else |
| 3151 | { |
| 3152 | // {"name": value}, |
| 3153 | // {'name': value}, |
| 3154 | // {name: value} use "name" as a literal key |
| 3155 | key = get_literal_key(arg); |
| 3156 | if (key == NULL) |
| 3157 | return FAIL; |
| 3158 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3159 | return FAIL; |
| 3160 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3161 | |
| 3162 | // Check for duplicate keys, if using string keys. |
| 3163 | if (key != NULL) |
| 3164 | { |
| 3165 | item = dict_find(d, key, -1); |
| 3166 | if (item != NULL) |
| 3167 | { |
| 3168 | semsg(_(e_duplicate_key), key); |
| 3169 | goto failret; |
| 3170 | } |
| 3171 | item = dictitem_alloc(key); |
| 3172 | if (item != NULL) |
| 3173 | { |
| 3174 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3175 | item->di_tv.v_lock = 0; |
| 3176 | if (dict_add(d, item) == FAIL) |
| 3177 | dictitem_free(item); |
| 3178 | } |
| 3179 | } |
| 3180 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3181 | if (**arg != ':') |
| 3182 | { |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 3183 | if (*skipwhite(*arg) == ':') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3184 | semsg(_(e_no_white_space_allowed_before_str), ":"); |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 3185 | else |
| 3186 | semsg(_(e_missing_dict_colon), *arg); |
| 3187 | return FAIL; |
| 3188 | } |
| 3189 | whitep = *arg + 1; |
| 3190 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 3191 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3192 | semsg(_(e_white_space_required_after_str), ":"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3193 | return FAIL; |
| 3194 | } |
| 3195 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3196 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3197 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3198 | *arg = NULL; |
| 3199 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3200 | } |
| 3201 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3202 | if (compile_expr0_ext(arg, cctx, &is_const) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3203 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3204 | if (!is_const) |
| 3205 | is_all_const = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3206 | ++count; |
| 3207 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3208 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3209 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3210 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3211 | *arg = NULL; |
| 3212 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3213 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3214 | if (**arg == '}') |
| 3215 | break; |
| 3216 | if (**arg != ',') |
| 3217 | { |
| 3218 | semsg(_(e_missing_dict_comma), *arg); |
| 3219 | goto failret; |
| 3220 | } |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 3221 | if (IS_WHITE_OR_NUL(*whitep)) |
| 3222 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3223 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 3224 | return FAIL; |
| 3225 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3226 | whitep = *arg + 1; |
Bram Moolenaar | 9a13e18 | 2020-10-19 21:45:07 +0200 | [diff] [blame] | 3227 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 3228 | { |
| 3229 | semsg(_(e_white_space_required_after_str), ","); |
| 3230 | return FAIL; |
| 3231 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3232 | *arg = skipwhite(*arg + 1); |
| 3233 | } |
| 3234 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3235 | *arg = *arg + 1; |
| 3236 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3237 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3238 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3239 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3240 | *arg += STRLEN(*arg); |
| 3241 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3242 | dict_unref(d); |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3243 | ppconst->pp_is_const = is_all_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3244 | return generate_NEWDICT(cctx, count); |
| 3245 | |
| 3246 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3247 | if (*arg == NULL) |
Bram Moolenaar | 44aefff | 2020-10-05 19:23:59 +0200 | [diff] [blame] | 3248 | { |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3249 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 44aefff | 2020-10-05 19:23:59 +0200 | [diff] [blame] | 3250 | *arg = (char_u *)""; |
| 3251 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3252 | dict_unref(d); |
| 3253 | return FAIL; |
| 3254 | } |
| 3255 | |
| 3256 | /* |
| 3257 | * Compile "&option". |
| 3258 | */ |
| 3259 | static int |
| 3260 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3261 | { |
| 3262 | typval_T rettv; |
| 3263 | char_u *start = *arg; |
| 3264 | int ret; |
| 3265 | |
| 3266 | // parse the option and get the current value to get the type. |
| 3267 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3268 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3269 | if (ret == OK) |
| 3270 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3271 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | d5ea8f0 | 2021-01-01 14:49:15 +0100 | [diff] [blame] | 3272 | char_u *name = vim_strnsave(start, *arg - start); |
| 3273 | type_T *type = rettv.v_type == VAR_BOOL ? &t_bool |
| 3274 | : rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3275 | |
| 3276 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3277 | vim_free(name); |
| 3278 | } |
| 3279 | clear_tv(&rettv); |
| 3280 | |
| 3281 | return ret; |
| 3282 | } |
| 3283 | |
| 3284 | /* |
| 3285 | * Compile "$VAR". |
| 3286 | */ |
| 3287 | static int |
| 3288 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3289 | { |
| 3290 | char_u *start = *arg; |
| 3291 | int len; |
| 3292 | int ret; |
| 3293 | char_u *name; |
| 3294 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3295 | ++*arg; |
| 3296 | len = get_env_len(arg); |
| 3297 | if (len == 0) |
| 3298 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3299 | semsg(_(e_syntax_error_at_str), start - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3300 | return FAIL; |
| 3301 | } |
| 3302 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3303 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3304 | name = vim_strnsave(start, len + 1); |
| 3305 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3306 | vim_free(name); |
| 3307 | return ret; |
| 3308 | } |
| 3309 | |
| 3310 | /* |
| 3311 | * Compile "@r". |
| 3312 | */ |
| 3313 | static int |
| 3314 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3315 | { |
| 3316 | int ret; |
| 3317 | |
| 3318 | ++*arg; |
| 3319 | if (**arg == NUL) |
| 3320 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3321 | semsg(_(e_syntax_error_at_str), *arg - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3322 | return FAIL; |
| 3323 | } |
Bram Moolenaar | 7226e5b | 2020-08-02 17:33:26 +0200 | [diff] [blame] | 3324 | if (!valid_yank_reg(**arg, FALSE)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3325 | { |
| 3326 | emsg_invreg(**arg); |
| 3327 | return FAIL; |
| 3328 | } |
| 3329 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3330 | ++*arg; |
| 3331 | return ret; |
| 3332 | } |
| 3333 | |
| 3334 | /* |
| 3335 | * Apply leading '!', '-' and '+' to constant "rettv". |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3336 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3337 | */ |
| 3338 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3339 | 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] | 3340 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3341 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3342 | |
| 3343 | // this works from end to start |
| 3344 | while (p > start) |
| 3345 | { |
| 3346 | --p; |
| 3347 | if (*p == '-' || *p == '+') |
| 3348 | { |
| 3349 | // only '-' has an effect, for '+' we only check the type |
| 3350 | #ifdef FEAT_FLOAT |
| 3351 | if (rettv->v_type == VAR_FLOAT) |
| 3352 | { |
| 3353 | if (*p == '-') |
| 3354 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3355 | } |
| 3356 | else |
| 3357 | #endif |
| 3358 | { |
| 3359 | varnumber_T val; |
| 3360 | int error = FALSE; |
| 3361 | |
| 3362 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3363 | // here |
| 3364 | if (check_not_string(rettv) == FAIL) |
| 3365 | return FAIL; |
| 3366 | val = tv_get_number_chk(rettv, &error); |
| 3367 | clear_tv(rettv); |
| 3368 | if (error) |
| 3369 | return FAIL; |
| 3370 | if (*p == '-') |
| 3371 | val = -val; |
| 3372 | rettv->v_type = VAR_NUMBER; |
| 3373 | rettv->vval.v_number = val; |
| 3374 | } |
| 3375 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3376 | else if (numeric_only) |
| 3377 | { |
| 3378 | ++p; |
| 3379 | break; |
| 3380 | } |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3381 | else if (*p == '!') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3382 | { |
| 3383 | int v = tv2bool(rettv); |
| 3384 | |
| 3385 | // '!' is permissive in the type. |
| 3386 | clear_tv(rettv); |
| 3387 | rettv->v_type = VAR_BOOL; |
| 3388 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3389 | } |
| 3390 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3391 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3392 | return OK; |
| 3393 | } |
| 3394 | |
| 3395 | /* |
| 3396 | * Recognize v: variables that are constants and set "rettv". |
| 3397 | */ |
| 3398 | static void |
| 3399 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3400 | { |
| 3401 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3402 | { |
| 3403 | rettv->v_type = VAR_BOOL; |
| 3404 | rettv->vval.v_number = VVAL_TRUE; |
| 3405 | *arg += 6; |
| 3406 | } |
| 3407 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3408 | { |
| 3409 | rettv->v_type = VAR_BOOL; |
| 3410 | rettv->vval.v_number = VVAL_FALSE; |
| 3411 | *arg += 7; |
| 3412 | } |
| 3413 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3414 | { |
| 3415 | rettv->v_type = VAR_SPECIAL; |
| 3416 | rettv->vval.v_number = VVAL_NULL; |
| 3417 | *arg += 6; |
| 3418 | } |
| 3419 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3420 | { |
| 3421 | rettv->v_type = VAR_SPECIAL; |
| 3422 | rettv->vval.v_number = VVAL_NONE; |
| 3423 | *arg += 6; |
| 3424 | } |
| 3425 | } |
| 3426 | |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 3427 | exprtype_T |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3428 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3429 | { |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 3430 | exprtype_T type = EXPR_UNKNOWN; |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3431 | int i; |
| 3432 | |
| 3433 | switch (p[0]) |
| 3434 | { |
| 3435 | case '=': if (p[1] == '=') |
| 3436 | type = EXPR_EQUAL; |
| 3437 | else if (p[1] == '~') |
| 3438 | type = EXPR_MATCH; |
| 3439 | break; |
| 3440 | case '!': if (p[1] == '=') |
| 3441 | type = EXPR_NEQUAL; |
| 3442 | else if (p[1] == '~') |
| 3443 | type = EXPR_NOMATCH; |
| 3444 | break; |
| 3445 | case '>': if (p[1] != '=') |
| 3446 | { |
| 3447 | type = EXPR_GREATER; |
| 3448 | *len = 1; |
| 3449 | } |
| 3450 | else |
| 3451 | type = EXPR_GEQUAL; |
| 3452 | break; |
| 3453 | case '<': if (p[1] != '=') |
| 3454 | { |
| 3455 | type = EXPR_SMALLER; |
| 3456 | *len = 1; |
| 3457 | } |
| 3458 | else |
| 3459 | type = EXPR_SEQUAL; |
| 3460 | break; |
| 3461 | case 'i': if (p[1] == 's') |
| 3462 | { |
| 3463 | // "is" and "isnot"; but not a prefix of a name |
| 3464 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3465 | *len = 5; |
| 3466 | i = p[*len]; |
| 3467 | if (!isalnum(i) && i != '_') |
| 3468 | { |
| 3469 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3470 | *type_is = TRUE; |
| 3471 | } |
| 3472 | } |
| 3473 | break; |
| 3474 | } |
| 3475 | return type; |
| 3476 | } |
| 3477 | |
| 3478 | /* |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3479 | * Skip over an expression, ignoring most errors. |
| 3480 | */ |
| 3481 | static void |
| 3482 | skip_expr_cctx(char_u **arg, cctx_T *cctx) |
| 3483 | { |
| 3484 | evalarg_T evalarg; |
| 3485 | |
| 3486 | CLEAR_FIELD(evalarg); |
| 3487 | evalarg.eval_cctx = cctx; |
| 3488 | skip_expr(arg, &evalarg); |
| 3489 | } |
| 3490 | |
| 3491 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3492 | * Compile code to apply '-', '+' and '!'. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3493 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3494 | */ |
| 3495 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3496 | 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] | 3497 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3498 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3499 | |
| 3500 | // this works from end to start |
| 3501 | while (p > start) |
| 3502 | { |
| 3503 | --p; |
Bram Moolenaar | 79cdf80 | 2020-11-18 17:39:05 +0100 | [diff] [blame] | 3504 | while (VIM_ISWHITE(*p)) |
| 3505 | --p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3506 | if (*p == '-' || *p == '+') |
| 3507 | { |
| 3508 | int negate = *p == '-'; |
| 3509 | isn_T *isn; |
| 3510 | |
| 3511 | // TODO: check type |
| 3512 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3513 | { |
| 3514 | --p; |
| 3515 | if (*p == '-') |
| 3516 | negate = !negate; |
| 3517 | } |
| 3518 | // only '-' has an effect, for '+' we only check the type |
| 3519 | if (negate) |
| 3520 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3521 | else |
| 3522 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3523 | if (isn == NULL) |
| 3524 | return FAIL; |
| 3525 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3526 | else if (numeric_only) |
| 3527 | { |
| 3528 | ++p; |
| 3529 | break; |
| 3530 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3531 | else |
| 3532 | { |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3533 | int invert = *p == '!'; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3534 | |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3535 | while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1]))) |
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 | if (p[-1] == '!') |
| 3538 | invert = !invert; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3539 | --p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3540 | } |
| 3541 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3542 | return FAIL; |
| 3543 | } |
| 3544 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3545 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3546 | return OK; |
| 3547 | } |
| 3548 | |
| 3549 | /* |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3550 | * Compile "(expression)": recursive! |
| 3551 | * Return FAIL/OK. |
| 3552 | */ |
| 3553 | static int |
| 3554 | compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3555 | { |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3556 | int ret; |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3557 | |
| 3558 | *arg = skipwhite(*arg + 1); |
| 3559 | if (ppconst->pp_used <= PPSIZE - 10) |
| 3560 | { |
| 3561 | ret = compile_expr1(arg, cctx, ppconst); |
| 3562 | } |
| 3563 | else |
| 3564 | { |
| 3565 | // Not enough space in ppconst, flush constants. |
| 3566 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3567 | return FAIL; |
| 3568 | ret = compile_expr0(arg, cctx); |
| 3569 | } |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3570 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
| 3571 | return FAIL; |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3572 | if (**arg == ')') |
| 3573 | ++*arg; |
| 3574 | else if (ret == OK) |
| 3575 | { |
| 3576 | emsg(_(e_missing_close)); |
| 3577 | ret = FAIL; |
| 3578 | } |
| 3579 | return ret; |
| 3580 | } |
| 3581 | |
| 3582 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3583 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3584 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3585 | */ |
| 3586 | static int |
| 3587 | compile_subscript( |
| 3588 | char_u **arg, |
| 3589 | cctx_T *cctx, |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3590 | char_u *start_leader, |
| 3591 | char_u **end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3592 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3593 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3594 | char_u *name_start = *end_leader; |
| 3595 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3596 | for (;;) |
| 3597 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3598 | char_u *p = skipwhite(*arg); |
| 3599 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3600 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3601 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3602 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3603 | |
| 3604 | // If a following line starts with "->{" or "->X" advance to that |
| 3605 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3606 | // Also if a following line starts with ".x". |
| 3607 | if (next != NULL && |
| 3608 | ((next[0] == '-' && next[1] == '>' |
| 3609 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3610 | || (next[0] == '.' && eval_isdictc(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3611 | { |
| 3612 | next = next_line_from_context(cctx, TRUE); |
| 3613 | if (next == NULL) |
| 3614 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3615 | *arg = next; |
| 3616 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3617 | } |
| 3618 | } |
| 3619 | |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 3620 | // Do not skip over white space to find the "(", "execute 'x' ()" is |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 3621 | // not a function call. |
| 3622 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3623 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3624 | garray_T *stack = &cctx->ctx_type_stack; |
| 3625 | type_T *type; |
| 3626 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3627 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3628 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3629 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3630 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3631 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3632 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3633 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3634 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3635 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3636 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3637 | return FAIL; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3638 | if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3639 | return FAIL; |
| 3640 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3641 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3642 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3643 | char_u *pstart = p; |
| 3644 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3645 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3646 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3647 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3648 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3649 | // something->method() |
| 3650 | // Apply the '!', '-' and '+' first: |
| 3651 | // -1.0->func() works like (-1.0)->func() |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3652 | if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3653 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3654 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3655 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3656 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3657 | // No line break supported right after "->". |
Bram Moolenaar | 2949cfd | 2020-12-31 21:28:47 +0100 | [diff] [blame] | 3658 | if (**arg == '(') |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 3659 | { |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3660 | int argcount = 1; |
| 3661 | char_u *expr; |
| 3662 | garray_T *stack; |
| 3663 | type_T *type; |
| 3664 | |
| 3665 | // Funcref call: list->(Refs[2])(arg) |
| 3666 | // or lambda: list->((arg) => expr)(arg) |
| 3667 | // Fist compile the arguments. |
| 3668 | expr = *arg; |
| 3669 | *arg = skipwhite(*arg + 1); |
| 3670 | skip_expr_cctx(arg, cctx); |
| 3671 | *arg = skipwhite(*arg); |
| 3672 | if (**arg != ')') |
| 3673 | { |
| 3674 | semsg(_(e_missing_paren), *arg); |
| 3675 | return FAIL; |
| 3676 | } |
| 3677 | ++*arg; |
| 3678 | if (**arg != '(') |
| 3679 | { |
Bram Moolenaar | 2949cfd | 2020-12-31 21:28:47 +0100 | [diff] [blame] | 3680 | if (*skipwhite(*arg) == '(') |
| 3681 | emsg(_(e_nowhitespace)); |
| 3682 | else |
| 3683 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3684 | return FAIL; |
| 3685 | } |
| 3686 | |
| 3687 | *arg = skipwhite(*arg + 1); |
| 3688 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3689 | return FAIL; |
| 3690 | |
| 3691 | // Compile the function expression. |
| 3692 | if (compile_parenthesis(&expr, cctx, ppconst) == FAIL) |
| 3693 | return FAIL; |
| 3694 | |
| 3695 | stack = &cctx->ctx_type_stack; |
| 3696 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3697 | if (generate_PCALL(cctx, argcount, |
| 3698 | (char_u *)"[expression]", type, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3699 | return FAIL; |
| 3700 | } |
| 3701 | else |
| 3702 | { |
| 3703 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3704 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3705 | if (!eval_isnamec1(*p)) |
| 3706 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3707 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3708 | return FAIL; |
| 3709 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3710 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3711 | p += 2; |
Bram Moolenaar | c5da1fb | 2020-08-05 15:43:44 +0200 | [diff] [blame] | 3712 | for ( ; eval_isnamec(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3713 | ; |
| 3714 | if (*p != '(') |
| 3715 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3716 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3717 | return FAIL; |
| 3718 | } |
| 3719 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3720 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3721 | return FAIL; |
| 3722 | } |
| 3723 | } |
Bram Moolenaar | badd848 | 2020-07-31 22:38:17 +0200 | [diff] [blame] | 3724 | else if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3725 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3726 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3727 | type_T **typep; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3728 | type_T *valtype; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3729 | vartype_T vtype; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3730 | int is_slice = FALSE; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3731 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3732 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3733 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3734 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3735 | // TODO: blob index |
| 3736 | // TODO: more arguments |
| 3737 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3738 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3739 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3740 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3741 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3742 | ++p; |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3743 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3744 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3745 | if (**arg == ':') |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3746 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3747 | // missing first index is equal to zero |
| 3748 | generate_PUSHNR(cctx, 0); |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3749 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3750 | else |
| 3751 | { |
| 3752 | if (compile_expr0(arg, cctx) == FAIL) |
| 3753 | return FAIL; |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3754 | if (**arg == ':') |
| 3755 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 3756 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 3757 | ":", *arg); |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3758 | return FAIL; |
| 3759 | } |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3760 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3761 | return FAIL; |
| 3762 | *arg = skipwhite(*arg); |
| 3763 | } |
| 3764 | if (**arg == ':') |
| 3765 | { |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3766 | is_slice = TRUE; |
| 3767 | ++*arg; |
| 3768 | if (!IS_WHITE_OR_NUL(**arg) && **arg != ']') |
| 3769 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 3770 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 3771 | ":", *arg); |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3772 | return FAIL; |
| 3773 | } |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3774 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3775 | return FAIL; |
| 3776 | if (**arg == ']') |
| 3777 | // missing second index is equal to end of string |
| 3778 | generate_PUSHNR(cctx, -1); |
| 3779 | else |
| 3780 | { |
| 3781 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 3782 | return FAIL; |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3783 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3784 | return FAIL; |
| 3785 | *arg = skipwhite(*arg); |
| 3786 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3787 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3788 | |
| 3789 | if (**arg != ']') |
| 3790 | { |
| 3791 | emsg(_(e_missbrac)); |
| 3792 | return FAIL; |
| 3793 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3794 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3795 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3796 | // We can index a list and a dict. If we don't know the type |
| 3797 | // we can use the index value type. |
| 3798 | // TODO: If we don't know use an instruction to figure it out at |
| 3799 | // runtime. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3800 | typep = ((type_T **)stack->ga_data) + stack->ga_len |
| 3801 | - (is_slice ? 3 : 2); |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3802 | vtype = (*typep)->tt_type; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3803 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3804 | // If the index is a string, the variable must be a Dict. |
| 3805 | if (*typep == &t_any && valtype == &t_string) |
| 3806 | vtype = VAR_DICT; |
| 3807 | if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB) |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3808 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3809 | if (need_type(valtype, &t_number, -1, cctx, |
| 3810 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3811 | return FAIL; |
| 3812 | if (is_slice) |
| 3813 | { |
| 3814 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3815 | if (need_type(valtype, &t_number, -2, cctx, |
| 3816 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3817 | return FAIL; |
| 3818 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3819 | } |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3820 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3821 | if (vtype == VAR_DICT) |
| 3822 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3823 | if (is_slice) |
| 3824 | { |
| 3825 | emsg(_(e_cannot_slice_dictionary)); |
| 3826 | return FAIL; |
| 3827 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3828 | if ((*typep)->tt_type == VAR_DICT) |
Bram Moolenaar | 31a11b9 | 2021-01-10 19:23:27 +0100 | [diff] [blame] | 3829 | { |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3830 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 31a11b9 | 2021-01-10 19:23:27 +0100 | [diff] [blame] | 3831 | if (*typep == &t_unknown) |
| 3832 | // empty dict was used |
| 3833 | *typep = &t_any; |
| 3834 | } |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3835 | else |
| 3836 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3837 | if (need_type(*typep, &t_dict_any, -2, cctx, |
| 3838 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3839 | return FAIL; |
| 3840 | *typep = &t_any; |
| 3841 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3842 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3843 | return FAIL; |
| 3844 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3845 | return FAIL; |
| 3846 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3847 | else if (vtype == VAR_STRING) |
| 3848 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3849 | *typep = &t_string; |
| 3850 | if ((is_slice |
| 3851 | ? generate_instr_drop(cctx, ISN_STRSLICE, 2) |
| 3852 | : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL) |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3853 | return FAIL; |
| 3854 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3855 | else if (vtype == VAR_BLOB) |
| 3856 | { |
| 3857 | emsg("Sorry, blob index and slice not implemented yet"); |
| 3858 | return FAIL; |
| 3859 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3860 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3861 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3862 | if (is_slice) |
| 3863 | { |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3864 | if (generate_instr_drop(cctx, |
| 3865 | vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE, |
| 3866 | 2) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3867 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3868 | } |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3869 | else |
| 3870 | { |
| 3871 | if ((*typep)->tt_type == VAR_LIST) |
Bram Moolenaar | 31a11b9 | 2021-01-10 19:23:27 +0100 | [diff] [blame] | 3872 | { |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3873 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 31a11b9 | 2021-01-10 19:23:27 +0100 | [diff] [blame] | 3874 | if (*typep == &t_unknown) |
| 3875 | // empty list was used |
| 3876 | *typep = &t_any; |
| 3877 | } |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3878 | if (generate_instr_drop(cctx, |
| 3879 | vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, |
| 3880 | 1) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3881 | return FAIL; |
| 3882 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3883 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3884 | else |
| 3885 | { |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3886 | emsg(_(e_string_list_dict_or_blob_required)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3887 | return FAIL; |
| 3888 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3889 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3890 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3891 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3892 | // dictionary member: dict.name |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3893 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3894 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3895 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3896 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3897 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3898 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | c1f0066 | 2020-10-03 13:41:53 +0200 | [diff] [blame] | 3899 | { |
| 3900 | emsg(_(e_missing_name_after_dot)); |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3901 | return FAIL; |
Bram Moolenaar | c1f0066 | 2020-10-03 13:41:53 +0200 | [diff] [blame] | 3902 | } |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3903 | p = *arg; |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3904 | if (eval_isdictc(*p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3905 | while (eval_isnamec(*p)) |
| 3906 | MB_PTR_ADV(p); |
| 3907 | if (p == *arg) |
| 3908 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3909 | semsg(_(e_syntax_error_at_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3910 | return FAIL; |
| 3911 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3912 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3913 | return FAIL; |
| 3914 | *arg = p; |
| 3915 | } |
| 3916 | else |
| 3917 | break; |
| 3918 | } |
| 3919 | |
| 3920 | // TODO - see handle_subscript(): |
| 3921 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3922 | // Don't do this when "Func" is already a partial that was bound |
| 3923 | // explicitly (pt_auto is FALSE). |
| 3924 | |
| 3925 | return OK; |
| 3926 | } |
| 3927 | |
| 3928 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3929 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3930 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3931 | * |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3932 | * 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] | 3933 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3934 | * |
| 3935 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3936 | */ |
| 3937 | |
| 3938 | /* |
| 3939 | * number number constant |
| 3940 | * 0zFFFFFFFF Blob constant |
| 3941 | * "string" string constant |
| 3942 | * 'string' literal string constant |
| 3943 | * &option-name option value |
| 3944 | * @r register contents |
| 3945 | * identifier variable value |
| 3946 | * function() function call |
| 3947 | * $VAR environment variable |
| 3948 | * (expression) nested expression |
| 3949 | * [expr, expr] List |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3950 | * {key: val, [key]: val} Dictionary |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3951 | * |
| 3952 | * Also handle: |
| 3953 | * ! in front logical NOT |
| 3954 | * - in front unary minus |
| 3955 | * + in front unary plus (ignored) |
| 3956 | * trailing (arg) funcref/partial call |
| 3957 | * trailing [] subscript in String or List |
| 3958 | * trailing .name entry in Dictionary |
| 3959 | * trailing ->name() method call |
| 3960 | */ |
| 3961 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3962 | compile_expr7( |
| 3963 | char_u **arg, |
| 3964 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3965 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3966 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3967 | char_u *start_leader, *end_leader; |
| 3968 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3969 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3970 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3971 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3972 | ppconst->pp_is_const = FALSE; |
| 3973 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3974 | /* |
| 3975 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3976 | */ |
| 3977 | start_leader = *arg; |
Bram Moolenaar | b23279d | 2021-01-05 22:08:20 +0100 | [diff] [blame] | 3978 | if (eval_leader(arg, TRUE) == FAIL) |
| 3979 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3980 | end_leader = *arg; |
| 3981 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3982 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3983 | switch (**arg) |
| 3984 | { |
| 3985 | /* |
| 3986 | * Number constant. |
| 3987 | */ |
| 3988 | case '0': // also for blob starting with 0z |
| 3989 | case '1': |
| 3990 | case '2': |
| 3991 | case '3': |
| 3992 | case '4': |
| 3993 | case '5': |
| 3994 | case '6': |
| 3995 | case '7': |
| 3996 | case '8': |
| 3997 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3998 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3999 | return FAIL; |
Bram Moolenaar | 4301a72 | 2020-08-11 20:51:08 +0200 | [diff] [blame] | 4000 | // Apply "-" and "+" just before the number now, right to |
| 4001 | // left. Matters especially when "->" follows. Stops at |
| 4002 | // '!'. |
| 4003 | if (apply_leader(rettv, TRUE, |
| 4004 | start_leader, &end_leader) == FAIL) |
| 4005 | { |
| 4006 | clear_tv(rettv); |
| 4007 | return FAIL; |
| 4008 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4009 | break; |
| 4010 | |
| 4011 | /* |
| 4012 | * String constant: "string". |
| 4013 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4014 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4015 | return FAIL; |
| 4016 | break; |
| 4017 | |
| 4018 | /* |
| 4019 | * Literal string constant: 'str''ing'. |
| 4020 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 4021 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4022 | return FAIL; |
| 4023 | break; |
| 4024 | |
| 4025 | /* |
| 4026 | * Constant Vim variable. |
| 4027 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4028 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4029 | ret = NOTDONE; |
| 4030 | break; |
| 4031 | |
| 4032 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4033 | * "true" constant |
| 4034 | */ |
| 4035 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 4036 | && !eval_isnamec((*arg)[4])) |
| 4037 | { |
| 4038 | *arg += 4; |
| 4039 | rettv->v_type = VAR_BOOL; |
| 4040 | rettv->vval.v_number = VVAL_TRUE; |
| 4041 | } |
| 4042 | else |
| 4043 | ret = NOTDONE; |
| 4044 | break; |
| 4045 | |
| 4046 | /* |
| 4047 | * "false" constant |
| 4048 | */ |
| 4049 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 4050 | && !eval_isnamec((*arg)[5])) |
| 4051 | { |
| 4052 | *arg += 5; |
| 4053 | rettv->v_type = VAR_BOOL; |
| 4054 | rettv->vval.v_number = VVAL_FALSE; |
| 4055 | } |
| 4056 | else |
| 4057 | ret = NOTDONE; |
| 4058 | break; |
| 4059 | |
| 4060 | /* |
Bram Moolenaar | 6797782 | 2021-01-03 21:53:53 +0100 | [diff] [blame] | 4061 | * "null" constant |
| 4062 | */ |
| 4063 | case 'n': if (STRNCMP(*arg, "null", 4) == 0 |
| 4064 | && !eval_isnamec((*arg)[5])) |
| 4065 | { |
| 4066 | *arg += 4; |
| 4067 | rettv->v_type = VAR_SPECIAL; |
| 4068 | rettv->vval.v_number = VVAL_NULL; |
| 4069 | } |
| 4070 | else |
| 4071 | ret = NOTDONE; |
| 4072 | break; |
| 4073 | |
| 4074 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4075 | * List: [expr, expr] |
| 4076 | */ |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4077 | case '[': ret = compile_list(arg, cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4078 | break; |
| 4079 | |
| 4080 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4081 | * Dictionary: {'key': val, 'key': val} |
| 4082 | */ |
Bram Moolenaar | 2949cfd | 2020-12-31 21:28:47 +0100 | [diff] [blame] | 4083 | case '{': ret = compile_dict(arg, cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4084 | break; |
| 4085 | |
| 4086 | /* |
| 4087 | * Option value: &name |
| 4088 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 4089 | case '&': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4090 | return FAIL; |
| 4091 | ret = compile_get_option(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4092 | break; |
| 4093 | |
| 4094 | /* |
| 4095 | * Environment variable: $VAR. |
| 4096 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 4097 | case '$': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4098 | return FAIL; |
| 4099 | ret = compile_get_env(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4100 | break; |
| 4101 | |
| 4102 | /* |
| 4103 | * Register contents: @r. |
| 4104 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 4105 | case '@': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4106 | return FAIL; |
| 4107 | ret = compile_get_register(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4108 | break; |
| 4109 | /* |
| 4110 | * nested expression: (expression). |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 4111 | * lambda: (arg, arg) => expr |
| 4112 | * funcref: (arg, arg) => { statement } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4113 | */ |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 4114 | case '(': // if compile_lambda returns NOTDONE then it must be (expr) |
| 4115 | ret = compile_lambda(arg, cctx); |
| 4116 | if (ret == NOTDONE) |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 4117 | ret = compile_parenthesis(arg, cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4118 | break; |
| 4119 | |
| 4120 | default: ret = NOTDONE; |
| 4121 | break; |
| 4122 | } |
| 4123 | if (ret == FAIL) |
| 4124 | return FAIL; |
| 4125 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4126 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4127 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4128 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4129 | clear_tv(rettv); |
| 4130 | else |
| 4131 | // A constant expression can possibly be handled compile time, |
| 4132 | // return the value instead of generating code. |
| 4133 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4134 | } |
| 4135 | else if (ret == NOTDONE) |
| 4136 | { |
| 4137 | char_u *p; |
| 4138 | int r; |
| 4139 | |
| 4140 | if (!eval_isnamec1(**arg)) |
| 4141 | { |
Bram Moolenaar | e498429 | 2020-12-13 14:19:25 +0100 | [diff] [blame] | 4142 | if (ends_excmd(*skipwhite(*arg))) |
| 4143 | semsg(_(e_empty_expression_str), *arg); |
| 4144 | else |
| 4145 | semsg(_(e_name_expected_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4146 | return FAIL; |
| 4147 | } |
| 4148 | |
| 4149 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4150 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4151 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4152 | { |
| 4153 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 4154 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4155 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4156 | { |
| 4157 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4158 | return FAIL; |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 4159 | r = compile_load(arg, p, cctx, TRUE, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4160 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4161 | if (r == FAIL) |
| 4162 | return FAIL; |
| 4163 | } |
| 4164 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4165 | // Handle following "[]", ".member", etc. |
| 4166 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 4167 | if (compile_subscript(arg, cctx, start_leader, &end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4168 | ppconst) == FAIL) |
| 4169 | return FAIL; |
| 4170 | if (ppconst->pp_used > 0) |
| 4171 | { |
| 4172 | // apply the '!', '-' and '+' before the constant |
| 4173 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 4174 | if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4175 | return FAIL; |
| 4176 | return OK; |
| 4177 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 4178 | if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4179 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4180 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4181 | } |
| 4182 | |
| 4183 | /* |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4184 | * Give the "white on both sides" error, taking the operator from "p[len]". |
| 4185 | */ |
| 4186 | void |
| 4187 | error_white_both(char_u *op, int len) |
| 4188 | { |
| 4189 | char_u buf[10]; |
| 4190 | |
| 4191 | vim_strncpy(buf, op, len); |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 4192 | 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] | 4193 | } |
| 4194 | |
| 4195 | /* |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4196 | * <type>expr7: runtime type check / conversion |
| 4197 | */ |
| 4198 | static int |
| 4199 | compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 4200 | { |
| 4201 | type_T *want_type = NULL; |
| 4202 | |
| 4203 | // Recognize <type> |
| 4204 | if (**arg == '<' && eval_isnamec1((*arg)[1])) |
| 4205 | { |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4206 | ++*arg; |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4207 | want_type = parse_type(arg, cctx->ctx_type_list, TRUE); |
| 4208 | if (want_type == NULL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4209 | return FAIL; |
| 4210 | |
| 4211 | if (**arg != '>') |
| 4212 | { |
| 4213 | if (*skipwhite(*arg) == '>') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4214 | semsg(_(e_no_white_space_allowed_before_str), ">"); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4215 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4216 | emsg(_(e_missing_gt)); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4217 | return FAIL; |
| 4218 | } |
| 4219 | ++*arg; |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 4220 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4221 | return FAIL; |
| 4222 | } |
| 4223 | |
| 4224 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
| 4225 | return FAIL; |
| 4226 | |
| 4227 | if (want_type != NULL) |
| 4228 | { |
| 4229 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 4230 | type_T *actual; |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4231 | |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 4232 | generate_ppconst(cctx, ppconst); |
| 4233 | actual = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 4234 | if (check_type(want_type, actual, FALSE, 0) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4235 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4236 | if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4237 | return FAIL; |
| 4238 | } |
| 4239 | } |
| 4240 | |
| 4241 | return OK; |
| 4242 | } |
| 4243 | |
| 4244 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4245 | * * number multiplication |
| 4246 | * / number division |
| 4247 | * % number modulo |
| 4248 | */ |
| 4249 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4250 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4251 | { |
| 4252 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4253 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4254 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4255 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4256 | // get the first expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4257 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4258 | return FAIL; |
| 4259 | |
| 4260 | /* |
| 4261 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4262 | */ |
| 4263 | for (;;) |
| 4264 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4265 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4266 | if (*op != '*' && *op != '/' && *op != '%') |
| 4267 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4268 | if (next != NULL) |
| 4269 | { |
| 4270 | *arg = next_line_from_context(cctx, TRUE); |
| 4271 | op = skipwhite(*arg); |
| 4272 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4273 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4274 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4275 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4276 | error_white_both(op, 1); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4277 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4278 | } |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4279 | if (may_get_next_line_error(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4280 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4281 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4282 | // get the second expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4283 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4284 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4285 | |
| 4286 | if (ppconst->pp_used == ppconst_used + 2 |
| 4287 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4288 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4289 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4290 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4291 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4292 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4293 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4294 | // both are numbers: compute the result |
| 4295 | switch (*op) |
| 4296 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4297 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4298 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4299 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4300 | break; |
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; |
| 4303 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4304 | tv1->vval.v_number = res; |
| 4305 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4306 | } |
| 4307 | else |
| 4308 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4309 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4310 | generate_two_op(cctx, op); |
| 4311 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4312 | } |
| 4313 | |
| 4314 | return OK; |
| 4315 | } |
| 4316 | |
| 4317 | /* |
| 4318 | * + number addition |
| 4319 | * - number subtraction |
| 4320 | * .. string concatenation |
| 4321 | */ |
| 4322 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4323 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4324 | { |
| 4325 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4326 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4327 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4328 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4329 | |
| 4330 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4331 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4332 | return FAIL; |
| 4333 | |
| 4334 | /* |
| 4335 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4336 | */ |
| 4337 | for (;;) |
| 4338 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4339 | op = may_peek_next_line(cctx, *arg, &next); |
| 4340 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4341 | break; |
| 4342 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4343 | if (next != NULL) |
| 4344 | { |
| 4345 | *arg = next_line_from_context(cctx, TRUE); |
| 4346 | op = skipwhite(*arg); |
| 4347 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4348 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4349 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4350 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4351 | error_white_both(op, oplen); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4352 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4353 | } |
| 4354 | |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 4355 | if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4356 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4357 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4358 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4359 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4360 | return FAIL; |
| 4361 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4362 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4363 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4364 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4365 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4366 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4367 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4368 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4369 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4370 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4371 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4372 | // concat/subtract/add constant numbers |
| 4373 | if (*op == '+') |
| 4374 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4375 | else if (*op == '-') |
| 4376 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4377 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4378 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4379 | // concatenate constant strings |
| 4380 | char_u *s1 = tv1->vval.v_string; |
| 4381 | char_u *s2 = tv2->vval.v_string; |
| 4382 | size_t len1 = STRLEN(s1); |
| 4383 | |
| 4384 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4385 | if (tv1->vval.v_string == NULL) |
| 4386 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4387 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4388 | return FAIL; |
| 4389 | } |
| 4390 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4391 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4392 | vim_free(s1); |
| 4393 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4394 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4395 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4396 | } |
| 4397 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4398 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4399 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4400 | if (*op == '.') |
| 4401 | { |
| 4402 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4403 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4404 | return FAIL; |
| 4405 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4406 | } |
| 4407 | else |
| 4408 | generate_two_op(cctx, op); |
| 4409 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | return OK; |
| 4413 | } |
| 4414 | |
| 4415 | /* |
| 4416 | * expr5a == expr5b |
| 4417 | * expr5a =~ expr5b |
| 4418 | * expr5a != expr5b |
| 4419 | * expr5a !~ expr5b |
| 4420 | * expr5a > expr5b |
| 4421 | * expr5a >= expr5b |
| 4422 | * expr5a < expr5b |
| 4423 | * expr5a <= expr5b |
| 4424 | * expr5a is expr5b |
| 4425 | * expr5a isnot expr5b |
| 4426 | * |
| 4427 | * Produces instructions: |
| 4428 | * EVAL expr5a Push result of "expr5a" |
| 4429 | * EVAL expr5b Push result of "expr5b" |
| 4430 | * COMPARE one of the compare instructions |
| 4431 | */ |
| 4432 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4433 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4434 | { |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 4435 | exprtype_T type = EXPR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4436 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4437 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4438 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4439 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4440 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4441 | |
| 4442 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4443 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4444 | return FAIL; |
| 4445 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4446 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4447 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4448 | |
| 4449 | /* |
| 4450 | * If there is a comparative operator, use it. |
| 4451 | */ |
| 4452 | if (type != EXPR_UNKNOWN) |
| 4453 | { |
| 4454 | int ic = FALSE; // Default: do not ignore case |
| 4455 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4456 | if (next != NULL) |
| 4457 | { |
| 4458 | *arg = next_line_from_context(cctx, TRUE); |
| 4459 | p = skipwhite(*arg); |
| 4460 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4461 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4462 | { |
| 4463 | semsg(_(e_invexpr2), *arg); |
| 4464 | return FAIL; |
| 4465 | } |
| 4466 | // extra question mark appended: ignore case |
| 4467 | if (p[len] == '?') |
| 4468 | { |
| 4469 | ic = TRUE; |
| 4470 | ++len; |
| 4471 | } |
| 4472 | // extra '#' appended: match case (ignored) |
| 4473 | else if (p[len] == '#') |
| 4474 | ++len; |
| 4475 | // nothing appended: match case |
| 4476 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4477 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4478 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4479 | error_white_both(p, len); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4480 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | // get the second variable |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4484 | if (may_get_next_line_error(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4485 | return FAIL; |
| 4486 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4487 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4488 | return FAIL; |
| 4489 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4490 | if (ppconst->pp_used == ppconst_used + 2) |
| 4491 | { |
| 4492 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4493 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4494 | int ret; |
| 4495 | |
| 4496 | // Both sides are a constant, compute the result now. |
| 4497 | // First check for a valid combination of types, this is more |
| 4498 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4499 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4500 | ret = FAIL; |
| 4501 | else |
| 4502 | { |
| 4503 | ret = typval_compare(tv1, tv2, type, ic); |
| 4504 | tv1->v_type = VAR_BOOL; |
| 4505 | tv1->vval.v_number = tv1->vval.v_number |
| 4506 | ? VVAL_TRUE : VVAL_FALSE; |
| 4507 | clear_tv(tv2); |
| 4508 | --ppconst->pp_used; |
| 4509 | } |
| 4510 | return ret; |
| 4511 | } |
| 4512 | |
| 4513 | generate_ppconst(cctx, ppconst); |
| 4514 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4515 | } |
| 4516 | |
| 4517 | return OK; |
| 4518 | } |
| 4519 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4520 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4521 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4522 | /* |
| 4523 | * Compile || or &&. |
| 4524 | */ |
| 4525 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4526 | compile_and_or( |
| 4527 | char_u **arg, |
| 4528 | cctx_T *cctx, |
| 4529 | char *op, |
| 4530 | ppconst_T *ppconst, |
| 4531 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4532 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4533 | char_u *next; |
| 4534 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4535 | int opchar = *op; |
| 4536 | |
| 4537 | if (p[0] == opchar && p[1] == opchar) |
| 4538 | { |
| 4539 | garray_T *instr = &cctx->ctx_instr; |
| 4540 | garray_T end_ga; |
| 4541 | |
| 4542 | /* |
| 4543 | * Repeat until there is no following "||" or "&&" |
| 4544 | */ |
| 4545 | ga_init2(&end_ga, sizeof(int), 10); |
| 4546 | while (p[0] == opchar && p[1] == opchar) |
| 4547 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4548 | if (next != NULL) |
| 4549 | { |
| 4550 | *arg = next_line_from_context(cctx, TRUE); |
| 4551 | p = skipwhite(*arg); |
| 4552 | } |
| 4553 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4554 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4555 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 4556 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 4557 | op, *arg); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4558 | return FAIL; |
| 4559 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4560 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4561 | // TODO: use ppconst if the value is a constant and check |
| 4562 | // evaluating to bool |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4563 | generate_ppconst(cctx, ppconst); |
| 4564 | |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 4565 | // Every part must evaluate to a bool. |
| 4566 | if (bool_on_stack(cctx) == FAIL) |
| 4567 | { |
| 4568 | ga_clear(&end_ga); |
| 4569 | return FAIL; |
| 4570 | } |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4571 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4572 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4573 | { |
| 4574 | ga_clear(&end_ga); |
| 4575 | return FAIL; |
| 4576 | } |
| 4577 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4578 | ++end_ga.ga_len; |
| 4579 | generate_JUMP(cctx, opchar == '|' |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4580 | ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4581 | |
| 4582 | // eval the next expression |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4583 | if (may_get_next_line_error(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 8bb0f54 | 2020-12-06 16:03:55 +0100 | [diff] [blame] | 4584 | { |
| 4585 | ga_clear(&end_ga); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4586 | return FAIL; |
Bram Moolenaar | 8bb0f54 | 2020-12-06 16:03:55 +0100 | [diff] [blame] | 4587 | } |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4588 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4589 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4590 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4591 | { |
| 4592 | ga_clear(&end_ga); |
| 4593 | return FAIL; |
| 4594 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4595 | |
| 4596 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4597 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4598 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4599 | |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 4600 | // Every part must evaluate to a bool. |
| 4601 | if (bool_on_stack(cctx) == FAIL) |
| 4602 | { |
| 4603 | ga_clear(&end_ga); |
| 4604 | return FAIL; |
| 4605 | } |
| 4606 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4607 | // Fill in the end label in all jumps. |
| 4608 | while (end_ga.ga_len > 0) |
| 4609 | { |
| 4610 | isn_T *isn; |
| 4611 | |
| 4612 | --end_ga.ga_len; |
| 4613 | isn = ((isn_T *)instr->ga_data) |
| 4614 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4615 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4616 | } |
| 4617 | ga_clear(&end_ga); |
| 4618 | } |
| 4619 | |
| 4620 | return OK; |
| 4621 | } |
| 4622 | |
| 4623 | /* |
| 4624 | * expr4a && expr4a && expr4a logical AND |
| 4625 | * |
| 4626 | * Produces instructions: |
| 4627 | * EVAL expr4a Push result of "expr4a" |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 4628 | * COND2BOOL convert to bool if needed |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4629 | * JUMP_IF_COND_FALSE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4630 | * EVAL expr4b Push result of "expr4b" |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4631 | * JUMP_IF_COND_FALSE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4632 | * EVAL expr4c Push result of "expr4c" |
| 4633 | * end: |
| 4634 | */ |
| 4635 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4636 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4637 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4638 | int ppconst_used = ppconst->pp_used; |
| 4639 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4640 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4641 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4642 | return FAIL; |
| 4643 | |
| 4644 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4645 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | /* |
| 4649 | * expr3a || expr3b || expr3c logical OR |
| 4650 | * |
| 4651 | * Produces instructions: |
| 4652 | * EVAL expr3a Push result of "expr3a" |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 4653 | * COND2BOOL convert to bool if needed |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4654 | * JUMP_IF_COND_TRUE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4655 | * EVAL expr3b Push result of "expr3b" |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4656 | * JUMP_IF_COND_TRUE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4657 | * EVAL expr3c Push result of "expr3c" |
| 4658 | * end: |
| 4659 | */ |
| 4660 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4661 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4662 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4663 | int ppconst_used = ppconst->pp_used; |
| 4664 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4665 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4666 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4667 | return FAIL; |
| 4668 | |
| 4669 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4670 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4671 | } |
| 4672 | |
| 4673 | /* |
| 4674 | * Toplevel expression: expr2 ? expr1a : expr1b |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4675 | * Produces instructions: |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4676 | * EVAL expr2 Push result of "expr2" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4677 | * JUMP_IF_FALSE alt jump if false |
| 4678 | * EVAL expr1a |
| 4679 | * JUMP_ALWAYS end |
| 4680 | * alt: EVAL expr1b |
| 4681 | * end: |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4682 | * |
| 4683 | * Toplevel expression: expr2 ?? expr1 |
| 4684 | * Produces instructions: |
| 4685 | * EVAL expr2 Push result of "expr2" |
| 4686 | * JUMP_AND_KEEP_IF_TRUE end jump if true |
| 4687 | * EVAL expr1 |
| 4688 | * end: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4689 | */ |
| 4690 | static int |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 4691 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4692 | { |
| 4693 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4694 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4695 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4696 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 4697 | // Ignore all kinds of errors when not producing code. |
| 4698 | if (cctx->ctx_skip == SKIP_YES) |
| 4699 | { |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 4700 | skip_expr_cctx(arg, cctx); |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 4701 | return OK; |
| 4702 | } |
| 4703 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4704 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4705 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4706 | return FAIL; |
| 4707 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4708 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4709 | if (*p == '?') |
| 4710 | { |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4711 | int op_falsy = p[1] == '?'; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4712 | garray_T *instr = &cctx->ctx_instr; |
| 4713 | garray_T *stack = &cctx->ctx_type_stack; |
| 4714 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4715 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4716 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4717 | type_T *type1 = NULL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4718 | int has_const_expr = FALSE; |
| 4719 | int const_value = FALSE; |
| 4720 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4721 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4722 | if (next != NULL) |
| 4723 | { |
| 4724 | *arg = next_line_from_context(cctx, TRUE); |
| 4725 | p = skipwhite(*arg); |
| 4726 | } |
| 4727 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4728 | 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] | 4729 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 4730 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 4731 | op_falsy ? "??" : "?", *arg); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4732 | return FAIL; |
| 4733 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4734 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4735 | if (ppconst->pp_used == ppconst_used + 1) |
| 4736 | { |
| 4737 | // the condition is a constant, we know whether the ? or the : |
| 4738 | // expression is to be evaluated. |
| 4739 | has_const_expr = TRUE; |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 4740 | if (op_falsy) |
| 4741 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4742 | else |
| 4743 | { |
| 4744 | int error = FALSE; |
| 4745 | |
| 4746 | const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used], |
| 4747 | &error); |
| 4748 | if (error) |
| 4749 | return FAIL; |
| 4750 | } |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4751 | cctx->ctx_skip = save_skip == SKIP_YES || |
| 4752 | (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT; |
| 4753 | |
| 4754 | if (op_falsy && cctx->ctx_skip == SKIP_YES) |
| 4755 | // "left ?? right" and "left" is truthy: produce "left" |
| 4756 | generate_ppconst(cctx, ppconst); |
| 4757 | else |
| 4758 | { |
| 4759 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4760 | --ppconst->pp_used; |
| 4761 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4762 | } |
| 4763 | else |
| 4764 | { |
| 4765 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4766 | if (op_falsy) |
| 4767 | end_idx = instr->ga_len; |
| 4768 | generate_JUMP(cctx, op_falsy |
| 4769 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0); |
| 4770 | if (op_falsy) |
| 4771 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4772 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4773 | |
| 4774 | // evaluate the second expression; any type is accepted |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4775 | 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] | 4776 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4777 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4778 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4779 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4780 | if (!has_const_expr) |
| 4781 | { |
| 4782 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4783 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4784 | if (!op_falsy) |
| 4785 | { |
| 4786 | // remember the type and drop it |
| 4787 | --stack->ga_len; |
| 4788 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
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 | end_idx = instr->ga_len; |
| 4791 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4792 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4793 | // jump here from JUMP_IF_FALSE |
| 4794 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4795 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4796 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4797 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4798 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4799 | if (!op_falsy) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4800 | { |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4801 | // Check for the ":". |
| 4802 | p = may_peek_next_line(cctx, *arg, &next); |
| 4803 | if (*p != ':') |
| 4804 | { |
| 4805 | emsg(_(e_missing_colon)); |
| 4806 | return FAIL; |
| 4807 | } |
| 4808 | if (next != NULL) |
| 4809 | { |
| 4810 | *arg = next_line_from_context(cctx, TRUE); |
| 4811 | p = skipwhite(*arg); |
| 4812 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4813 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4814 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4815 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 4816 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 4817 | ":", p); |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4818 | return FAIL; |
| 4819 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4820 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4821 | // evaluate the third expression |
| 4822 | if (has_const_expr) |
| 4823 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4824 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4825 | if (may_get_next_line_error(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4826 | return FAIL; |
| 4827 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
| 4828 | return FAIL; |
| 4829 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4830 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4831 | if (!has_const_expr) |
| 4832 | { |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4833 | type_T **typep; |
| 4834 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4835 | generate_ppconst(cctx, ppconst); |
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 the types differ, the result has a more generic type. |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4838 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 1; |
| 4839 | common_type(type1, *typep, typep, cctx->ctx_type_list); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4840 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4841 | // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4842 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4843 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4844 | } |
| 4845 | |
| 4846 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4847 | } |
| 4848 | return OK; |
| 4849 | } |
| 4850 | |
| 4851 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4852 | * Toplevel expression. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4853 | * Sets "is_const" (if not NULL) to indicate the value is a constant. |
| 4854 | * Returns OK or FAIL. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4855 | */ |
| 4856 | static int |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4857 | compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4858 | { |
| 4859 | ppconst_T ppconst; |
| 4860 | |
| 4861 | CLEAR_FIELD(ppconst); |
| 4862 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4863 | { |
| 4864 | clear_ppconst(&ppconst); |
| 4865 | return FAIL; |
| 4866 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4867 | if (is_const != NULL) |
| 4868 | *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4869 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4870 | return FAIL; |
| 4871 | return OK; |
| 4872 | } |
| 4873 | |
| 4874 | /* |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4875 | * Toplevel expression. |
| 4876 | */ |
| 4877 | static int |
| 4878 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4879 | { |
| 4880 | return compile_expr0_ext(arg, cctx, NULL); |
| 4881 | } |
| 4882 | |
| 4883 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4884 | * compile "return [expr]" |
| 4885 | */ |
| 4886 | static char_u * |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4887 | compile_return(char_u *arg, int check_return_type, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4888 | { |
| 4889 | char_u *p = arg; |
| 4890 | garray_T *stack = &cctx->ctx_type_stack; |
| 4891 | type_T *stack_type; |
| 4892 | |
| 4893 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4894 | { |
| 4895 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4896 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4897 | return NULL; |
| 4898 | |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4899 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4900 | { |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4901 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 6b55377 | 2020-12-31 13:31:23 +0100 | [diff] [blame] | 4902 | if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL |
Bram Moolenaar | 328eac2 | 2021-01-07 19:23:08 +0100 | [diff] [blame] | 4903 | || cctx->ctx_ufunc->uf_ret_type == &t_unknown |
| 4904 | || cctx->ctx_ufunc->uf_ret_type == &t_any)) |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4905 | { |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4906 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4907 | } |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4908 | else |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4909 | { |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4910 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4911 | && stack_type->tt_type != VAR_VOID |
| 4912 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4913 | { |
| 4914 | emsg(_(e_returning_value_in_function_without_return_type)); |
| 4915 | return NULL; |
| 4916 | } |
| 4917 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4918 | cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4919 | return NULL; |
| 4920 | } |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4921 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4922 | } |
| 4923 | else |
| 4924 | { |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4925 | // "check_return_type" cannot be TRUE, only used for a lambda which |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4926 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4927 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4928 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4929 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4930 | emsg(_(e_missing_return_value)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4931 | return NULL; |
| 4932 | } |
| 4933 | |
| 4934 | // No argument, return zero. |
| 4935 | generate_PUSHNR(cctx, 0); |
| 4936 | } |
Bram Moolenaar | 7cd2422 | 2021-01-12 18:58:39 +0100 | [diff] [blame] | 4937 | |
| 4938 | // Undo any command modifiers. |
| 4939 | generate_undo_cmdmods(cctx); |
| 4940 | |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4941 | if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4942 | return NULL; |
| 4943 | |
| 4944 | // "return val | endif" is possible |
| 4945 | return skipwhite(p); |
| 4946 | } |
| 4947 | |
| 4948 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4949 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4950 | * Return a pointer to the line in allocated memory. |
| 4951 | * Return NULL for end-of-file or some error. |
| 4952 | */ |
| 4953 | static char_u * |
| 4954 | exarg_getline( |
| 4955 | int c UNUSED, |
| 4956 | void *cookie, |
| 4957 | int indent UNUSED, |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4958 | getline_opt_T options UNUSED) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4959 | { |
| 4960 | cctx_T *cctx = (cctx_T *)cookie; |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4961 | char_u *p; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4962 | |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4963 | for (;;) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4964 | { |
Bram Moolenaar | 2914a20 | 2020-09-27 18:24:03 +0200 | [diff] [blame] | 4965 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1) |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4966 | return NULL; |
| 4967 | ++cctx->ctx_lnum; |
| 4968 | p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]; |
| 4969 | // Comment lines result in NULL pointers, skip them. |
| 4970 | if (p != NULL) |
| 4971 | return vim_strsave(p); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4972 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | /* |
| 4976 | * Compile a nested :def command. |
| 4977 | */ |
| 4978 | static char_u * |
| 4979 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4980 | { |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4981 | int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4982 | char_u *name_start = eap->arg; |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4983 | char_u *name_end = to_name_end(eap->arg, TRUE); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4984 | char_u *lambda_name; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4985 | ufunc_T *ufunc; |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 4986 | int r = FAIL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4987 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 4988 | if (eap->forceit) |
Bram Moolenaar | 8b848ca | 2020-09-10 22:28:01 +0200 | [diff] [blame] | 4989 | { |
| 4990 | emsg(_(e_cannot_use_bang_with_nested_def)); |
| 4991 | return NULL; |
| 4992 | } |
| 4993 | |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 4994 | if (*name_start == '/') |
| 4995 | { |
| 4996 | name_end = skip_regexp(name_start + 1, '/', TRUE); |
| 4997 | if (*name_end == '/') |
| 4998 | ++name_end; |
| 4999 | eap->nextcmd = check_nextcmd(name_end); |
| 5000 | } |
| 5001 | if (name_end == name_start || *skipwhite(name_end) != '(') |
| 5002 | { |
| 5003 | if (!ends_excmd2(name_start, name_end)) |
| 5004 | { |
| 5005 | semsg(_(e_invalid_command_str), eap->cmd); |
| 5006 | return NULL; |
| 5007 | } |
| 5008 | |
| 5009 | // "def" or "def Name": list functions |
| 5010 | if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL) |
| 5011 | return NULL; |
| 5012 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5013 | } |
| 5014 | |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 5015 | // Only g:Func() can use a namespace. |
| 5016 | if (name_start[1] == ':' && !is_global) |
| 5017 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5018 | semsg(_(e_namespace_not_supported_str), name_start); |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 5019 | return NULL; |
| 5020 | } |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 5021 | if (check_defined(name_start, name_end - name_start, cctx) == FAIL) |
| 5022 | return NULL; |
| 5023 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5024 | eap->arg = name_end; |
| 5025 | eap->getline = exarg_getline; |
| 5026 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5027 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5028 | eap->forceit = FALSE; |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5029 | lambda_name = vim_strsave(get_lambda_name()); |
| 5030 | if (lambda_name == NULL) |
| 5031 | return NULL; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 5032 | ufunc = define_function(eap, lambda_name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5033 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 5034 | if (ufunc == NULL) |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5035 | { |
| 5036 | r = eap->skip ? OK : FAIL; |
| 5037 | goto theend; |
| 5038 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 5039 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 5040 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 4ee711f | 2020-09-23 18:51:11 +0200 | [diff] [blame] | 5041 | { |
| 5042 | func_ptr_unref(ufunc); |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5043 | goto theend; |
Bram Moolenaar | 4ee711f | 2020-09-23 18:51:11 +0200 | [diff] [blame] | 5044 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5045 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5046 | if (is_global) |
| 5047 | { |
| 5048 | char_u *func_name = vim_strnsave(name_start + 2, |
| 5049 | name_end - name_start - 2); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 5050 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5051 | if (func_name == NULL) |
| 5052 | r = FAIL; |
| 5053 | else |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5054 | { |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 5055 | r = generate_NEWFUNC(cctx, lambda_name, func_name); |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5056 | lambda_name = NULL; |
| 5057 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5058 | } |
| 5059 | else |
| 5060 | { |
| 5061 | // Define a local variable for the function reference. |
Bram Moolenaar | e8211a3 | 2020-10-09 22:04:29 +0200 | [diff] [blame] | 5062 | lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5063 | TRUE, ufunc->uf_func_type); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 5064 | int block_depth = cctx->ctx_ufunc->uf_block_depth; |
Bram Moolenaar | e8211a3 | 2020-10-09 22:04:29 +0200 | [diff] [blame] | 5065 | |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 5066 | if (lvar == NULL) |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5067 | goto theend; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 5068 | if (generate_FUNCREF(cctx, ufunc) == FAIL) |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5069 | goto theend; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5070 | r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 5071 | |
| 5072 | // copy over the block scope IDs |
| 5073 | if (block_depth > 0) |
| 5074 | { |
| 5075 | ufunc->uf_block_ids = ALLOC_MULT(int, block_depth); |
| 5076 | if (ufunc->uf_block_ids != NULL) |
| 5077 | { |
| 5078 | mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids, |
| 5079 | sizeof(int) * block_depth); |
| 5080 | ufunc->uf_block_depth = block_depth; |
| 5081 | } |
| 5082 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5083 | } |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 5084 | // TODO: warning for trailing text? |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 5085 | r = OK; |
| 5086 | |
| 5087 | theend: |
| 5088 | vim_free(lambda_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 5089 | return r == FAIL ? NULL : (char_u *)""; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5093 | * Return the length of an assignment operator, or zero if there isn't one. |
| 5094 | */ |
| 5095 | int |
| 5096 | assignment_len(char_u *p, int *heredoc) |
| 5097 | { |
| 5098 | if (*p == '=') |
| 5099 | { |
| 5100 | if (p[1] == '<' && p[2] == '<') |
| 5101 | { |
| 5102 | *heredoc = TRUE; |
| 5103 | return 3; |
| 5104 | } |
| 5105 | return 1; |
| 5106 | } |
| 5107 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 5108 | return 2; |
| 5109 | if (STRNCMP(p, "..=", 3) == 0) |
| 5110 | return 3; |
| 5111 | return 0; |
| 5112 | } |
| 5113 | |
| 5114 | // words that cannot be used as a variable |
| 5115 | static char *reserved[] = { |
| 5116 | "true", |
| 5117 | "false", |
Bram Moolenaar | 6797782 | 2021-01-03 21:53:53 +0100 | [diff] [blame] | 5118 | "null", |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5119 | NULL |
| 5120 | }; |
| 5121 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5122 | // Destination for an assignment or ":unlet" with an index. |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5123 | typedef enum { |
| 5124 | dest_local, |
| 5125 | dest_option, |
| 5126 | dest_env, |
| 5127 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 5128 | dest_buffer, |
| 5129 | dest_window, |
| 5130 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5131 | dest_vimvar, |
| 5132 | dest_script, |
| 5133 | dest_reg, |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5134 | dest_expr, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5135 | } assign_dest_T; |
| 5136 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5137 | // Used by compile_lhs() to store information about the LHS of an assignment |
| 5138 | // and one argument of ":unlet" with an index. |
| 5139 | typedef struct { |
| 5140 | assign_dest_T lhs_dest; // type of destination |
| 5141 | |
| 5142 | char_u *lhs_name; // allocated name including |
| 5143 | // "[expr]" or ".name". |
| 5144 | size_t lhs_varlen; // length of the variable without |
| 5145 | // "[expr]" or ".name" |
| 5146 | char_u *lhs_dest_end; // end of the destination, including |
| 5147 | // "[expr]" or ".name". |
| 5148 | |
| 5149 | int lhs_has_index; // has "[expr]" or ".name" |
| 5150 | |
| 5151 | int lhs_new_local; // create new local variable |
| 5152 | int lhs_opt_flags; // for when destination is an option |
| 5153 | int lhs_vimvaridx; // for when destination is a v:var |
| 5154 | |
| 5155 | lvar_T lhs_local_lvar; // used for existing local destination |
| 5156 | lvar_T lhs_arg_lvar; // used for argument destination |
| 5157 | lvar_T *lhs_lvar; // points to destination lvar |
| 5158 | int lhs_scriptvar_sid; |
| 5159 | int lhs_scriptvar_idx; |
| 5160 | |
| 5161 | int lhs_has_type; // type was specified |
| 5162 | type_T *lhs_type; |
| 5163 | type_T *lhs_member_type; |
| 5164 | } lhs_T; |
| 5165 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5166 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5167 | * Generate the load instruction for "name". |
| 5168 | */ |
| 5169 | static void |
| 5170 | generate_loadvar( |
| 5171 | cctx_T *cctx, |
| 5172 | assign_dest_T dest, |
| 5173 | char_u *name, |
| 5174 | lvar_T *lvar, |
| 5175 | type_T *type) |
| 5176 | { |
| 5177 | switch (dest) |
| 5178 | { |
| 5179 | case dest_option: |
| 5180 | // TODO: check the option exists |
| 5181 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 5182 | break; |
| 5183 | case dest_global: |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 5184 | if (vim_strchr(name, AUTOLOAD_CHAR) == NULL) |
| 5185 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 5186 | else |
| 5187 | generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5188 | break; |
| 5189 | case dest_buffer: |
| 5190 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 5191 | break; |
| 5192 | case dest_window: |
| 5193 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 5194 | break; |
| 5195 | case dest_tab: |
| 5196 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 5197 | break; |
| 5198 | case dest_script: |
| 5199 | compile_load_scriptvar(cctx, |
| 5200 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 5201 | break; |
| 5202 | case dest_env: |
| 5203 | // Include $ in the name here |
| 5204 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 5205 | break; |
| 5206 | case dest_reg: |
| 5207 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 5208 | break; |
| 5209 | case dest_vimvar: |
| 5210 | generate_LOADV(cctx, name + 2, TRUE); |
| 5211 | break; |
| 5212 | case dest_local: |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 5213 | if (lvar->lv_from_outer > 0) |
| 5214 | generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer, |
| 5215 | type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5216 | else |
| 5217 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 5218 | break; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5219 | case dest_expr: |
| 5220 | // list or dict value should already be on the stack. |
| 5221 | break; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5222 | } |
| 5223 | } |
| 5224 | |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5225 | /* |
| 5226 | * Skip over "[expr]" or ".member". |
| 5227 | * Does not check for any errors. |
| 5228 | */ |
| 5229 | static char_u * |
| 5230 | skip_index(char_u *start) |
| 5231 | { |
| 5232 | char_u *p = start; |
| 5233 | |
| 5234 | if (*p == '[') |
| 5235 | { |
| 5236 | p = skipwhite(p + 1); |
| 5237 | (void)skip_expr(&p, NULL); |
| 5238 | p = skipwhite(p); |
| 5239 | if (*p == ']') |
| 5240 | return p + 1; |
| 5241 | return p; |
| 5242 | } |
| 5243 | // if (*p == '.') |
| 5244 | return to_name_end(p + 1, TRUE); |
| 5245 | } |
| 5246 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5247 | void |
| 5248 | vim9_declare_error(char_u *name) |
| 5249 | { |
| 5250 | char *scope = ""; |
| 5251 | |
| 5252 | switch (*name) |
| 5253 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 5254 | case 'g': scope = _("global"); break; |
| 5255 | case 'b': scope = _("buffer"); break; |
| 5256 | case 'w': scope = _("window"); break; |
| 5257 | case 't': scope = _("tab"); break; |
| 5258 | case 'v': scope = "v:"; break; |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 5259 | case '$': semsg(_(e_cannot_declare_an_environment_variable), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5260 | return; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5261 | case '&': semsg(_(e_cannot_declare_an_option), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5262 | return; |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 5263 | case '@': semsg(_(e_cannot_declare_a_register_str), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5264 | return; |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 5265 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5266 | } |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 5267 | semsg(_(e_cannot_declare_a_scope_variable), scope, name); |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5268 | } |
| 5269 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5270 | /* |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5271 | * For one assignment figure out the type of destination. Return it in "dest". |
| 5272 | * When not recognized "dest" is not set. |
| 5273 | * For an option "opt_flags" is set. |
| 5274 | * For a v:var "vimvaridx" is set. |
| 5275 | * "type" is set to the destination type if known, unchanted otherwise. |
| 5276 | * Return FAIL if an error message was given. |
| 5277 | */ |
| 5278 | static int |
| 5279 | get_var_dest( |
| 5280 | char_u *name, |
| 5281 | assign_dest_T *dest, |
| 5282 | int cmdidx, |
| 5283 | int *opt_flags, |
| 5284 | int *vimvaridx, |
| 5285 | type_T **type, |
| 5286 | cctx_T *cctx) |
| 5287 | { |
| 5288 | char_u *p; |
| 5289 | |
| 5290 | if (*name == '&') |
| 5291 | { |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5292 | int cc; |
| 5293 | long numval; |
| 5294 | getoption_T opt_type; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5295 | |
| 5296 | *dest = dest_option; |
| 5297 | if (cmdidx == CMD_final || cmdidx == CMD_const) |
| 5298 | { |
| 5299 | emsg(_(e_const_option)); |
| 5300 | return FAIL; |
| 5301 | } |
| 5302 | p = name; |
| 5303 | p = find_option_end(&p, opt_flags); |
| 5304 | if (p == NULL) |
| 5305 | { |
| 5306 | // cannot happen? |
| 5307 | emsg(_(e_letunexp)); |
| 5308 | return FAIL; |
| 5309 | } |
| 5310 | cc = *p; |
| 5311 | *p = NUL; |
| 5312 | opt_type = get_option_value(skip_option_env_lead(name), |
| 5313 | &numval, NULL, *opt_flags); |
| 5314 | *p = cc; |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5315 | switch (opt_type) |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5316 | { |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5317 | case gov_unknown: |
| 5318 | semsg(_(e_unknown_option), name); |
| 5319 | return FAIL; |
| 5320 | case gov_string: |
| 5321 | case gov_hidden_string: |
| 5322 | *type = &t_string; |
| 5323 | break; |
| 5324 | case gov_bool: |
| 5325 | case gov_hidden_bool: |
| 5326 | *type = &t_bool; |
| 5327 | break; |
| 5328 | case gov_number: |
| 5329 | case gov_hidden_number: |
| 5330 | *type = &t_number; |
| 5331 | break; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5332 | } |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5333 | } |
| 5334 | else if (*name == '$') |
| 5335 | { |
| 5336 | *dest = dest_env; |
| 5337 | *type = &t_string; |
| 5338 | } |
| 5339 | else if (*name == '@') |
| 5340 | { |
| 5341 | if (!valid_yank_reg(name[1], FALSE) || name[1] == '.') |
| 5342 | { |
| 5343 | emsg_invreg(name[1]); |
| 5344 | return FAIL; |
| 5345 | } |
| 5346 | *dest = dest_reg; |
| 5347 | *type = &t_string; |
| 5348 | } |
| 5349 | else if (STRNCMP(name, "g:", 2) == 0) |
| 5350 | { |
| 5351 | *dest = dest_global; |
| 5352 | } |
| 5353 | else if (STRNCMP(name, "b:", 2) == 0) |
| 5354 | { |
| 5355 | *dest = dest_buffer; |
| 5356 | } |
| 5357 | else if (STRNCMP(name, "w:", 2) == 0) |
| 5358 | { |
| 5359 | *dest = dest_window; |
| 5360 | } |
| 5361 | else if (STRNCMP(name, "t:", 2) == 0) |
| 5362 | { |
| 5363 | *dest = dest_tab; |
| 5364 | } |
| 5365 | else if (STRNCMP(name, "v:", 2) == 0) |
| 5366 | { |
| 5367 | typval_T *vtv; |
| 5368 | int di_flags; |
| 5369 | |
| 5370 | *vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5371 | if (*vimvaridx < 0) |
| 5372 | { |
| 5373 | semsg(_(e_variable_not_found_str), name); |
| 5374 | return FAIL; |
| 5375 | } |
| 5376 | // We use the current value of "sandbox" here, is that OK? |
| 5377 | if (var_check_ro(di_flags, name, FALSE)) |
| 5378 | return FAIL; |
| 5379 | *dest = dest_vimvar; |
| 5380 | vtv = get_vim_var_tv(*vimvaridx); |
| 5381 | *type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
| 5382 | } |
| 5383 | return OK; |
| 5384 | } |
| 5385 | |
| 5386 | /* |
| 5387 | * Generate a STORE instruction for "dest", not being "dest_local". |
| 5388 | * Return FAIL when out of memory. |
| 5389 | */ |
| 5390 | static int |
| 5391 | generate_store_var( |
| 5392 | cctx_T *cctx, |
| 5393 | assign_dest_T dest, |
| 5394 | int opt_flags, |
| 5395 | int vimvaridx, |
| 5396 | int scriptvar_idx, |
| 5397 | int scriptvar_sid, |
| 5398 | type_T *type, |
| 5399 | char_u *name) |
| 5400 | { |
| 5401 | switch (dest) |
| 5402 | { |
| 5403 | case dest_option: |
| 5404 | return generate_STOREOPT(cctx, skip_option_env_lead(name), |
| 5405 | opt_flags); |
| 5406 | case dest_global: |
| 5407 | // include g: with the name, easier to execute that way |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 5408 | return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL |
| 5409 | ? ISN_STOREG : ISN_STOREAUTO, 0, name); |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5410 | case dest_buffer: |
| 5411 | // include b: with the name, easier to execute that way |
| 5412 | return generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5413 | case dest_window: |
| 5414 | // include w: with the name, easier to execute that way |
| 5415 | return generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5416 | case dest_tab: |
| 5417 | // include t: with the name, easier to execute that way |
| 5418 | return generate_STORE(cctx, ISN_STORET, 0, name); |
| 5419 | case dest_env: |
| 5420 | return generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5421 | case dest_reg: |
| 5422 | return generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5423 | case dest_vimvar: |
| 5424 | return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5425 | case dest_script: |
| 5426 | if (scriptvar_idx < 0) |
| 5427 | { |
| 5428 | char_u *name_s = name; |
Bram Moolenaar | 41d6196 | 2020-12-06 20:12:43 +0100 | [diff] [blame] | 5429 | int r; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5430 | |
Bram Moolenaar | 41d6196 | 2020-12-06 20:12:43 +0100 | [diff] [blame] | 5431 | // "s:" is included in the name. |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5432 | r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s, |
| 5433 | scriptvar_sid, type); |
| 5434 | if (name_s != name) |
| 5435 | vim_free(name_s); |
| 5436 | return r; |
| 5437 | } |
| 5438 | return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
| 5439 | scriptvar_sid, scriptvar_idx, type); |
| 5440 | case dest_local: |
| 5441 | case dest_expr: |
| 5442 | // cannot happen |
| 5443 | break; |
| 5444 | } |
| 5445 | return FAIL; |
| 5446 | } |
| 5447 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5448 | static int |
| 5449 | is_decl_command(int cmdidx) |
| 5450 | { |
| 5451 | return cmdidx == CMD_let || cmdidx == CMD_var |
| 5452 | || cmdidx == CMD_final || cmdidx == CMD_const; |
| 5453 | } |
| 5454 | |
| 5455 | /* |
| 5456 | * Figure out the LHS type and other properties for an assignment or one item |
| 5457 | * of ":unlet" with an index. |
| 5458 | * Returns OK or FAIL. |
| 5459 | */ |
| 5460 | static int |
| 5461 | compile_lhs( |
| 5462 | char_u *var_start, |
| 5463 | lhs_T *lhs, |
| 5464 | int cmdidx, |
| 5465 | int heredoc, |
| 5466 | int oplen, |
| 5467 | cctx_T *cctx) |
| 5468 | { |
| 5469 | char_u *var_end; |
| 5470 | int is_decl = is_decl_command(cmdidx); |
| 5471 | |
| 5472 | CLEAR_POINTER(lhs); |
| 5473 | lhs->lhs_dest = dest_local; |
| 5474 | lhs->lhs_vimvaridx = -1; |
| 5475 | lhs->lhs_scriptvar_idx = -1; |
| 5476 | |
| 5477 | // "dest_end" is the end of the destination, including "[expr]" or |
| 5478 | // ".name". |
| 5479 | // "var_end" is the end of the variable/option/etc. name. |
| 5480 | lhs->lhs_dest_end = skip_var_one(var_start, FALSE); |
| 5481 | if (*var_start == '@') |
| 5482 | var_end = var_start + 2; |
| 5483 | else |
| 5484 | { |
| 5485 | // skip over the leading "&", "&l:", "&g:" and "$" |
| 5486 | var_end = skip_option_env_lead(var_start); |
| 5487 | var_end = to_name_end(var_end, TRUE); |
| 5488 | } |
| 5489 | |
| 5490 | // "a: type" is declaring variable "a" with a type, not dict "a:". |
| 5491 | if (is_decl && lhs->lhs_dest_end == var_start + 2 |
| 5492 | && lhs->lhs_dest_end[-1] == ':') |
| 5493 | --lhs->lhs_dest_end; |
| 5494 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 5495 | --var_end; |
| 5496 | |
| 5497 | // compute the length of the destination without "[expr]" or ".name" |
| 5498 | lhs->lhs_varlen = var_end - var_start; |
| 5499 | lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen); |
| 5500 | if (lhs->lhs_name == NULL) |
| 5501 | return FAIL; |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 5502 | |
| 5503 | if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen) |
| 5504 | // Something follows after the variable: "var[idx]" or "var.key". |
| 5505 | lhs->lhs_has_index = TRUE; |
| 5506 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5507 | if (heredoc) |
| 5508 | lhs->lhs_type = &t_list_string; |
| 5509 | else |
| 5510 | lhs->lhs_type = &t_any; |
| 5511 | |
| 5512 | if (cctx->ctx_skip != SKIP_YES) |
| 5513 | { |
| 5514 | int declare_error = FALSE; |
| 5515 | |
| 5516 | if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx, |
| 5517 | &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx, |
| 5518 | &lhs->lhs_type, cctx) == FAIL) |
| 5519 | return FAIL; |
| 5520 | if (lhs->lhs_dest != dest_local) |
| 5521 | { |
| 5522 | // Specific kind of variable recognized. |
| 5523 | declare_error = is_decl; |
| 5524 | } |
| 5525 | else |
| 5526 | { |
| 5527 | int idx; |
| 5528 | |
| 5529 | // No specific kind of variable recognized, just a name. |
| 5530 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5531 | if (STRCMP(reserved[idx], lhs->lhs_name) == 0) |
| 5532 | { |
| 5533 | semsg(_(e_cannot_use_reserved_name), lhs->lhs_name); |
| 5534 | return FAIL; |
| 5535 | } |
| 5536 | |
| 5537 | |
| 5538 | if (lookup_local(var_start, lhs->lhs_varlen, |
| 5539 | &lhs->lhs_local_lvar, cctx) == OK) |
| 5540 | lhs->lhs_lvar = &lhs->lhs_local_lvar; |
| 5541 | else |
| 5542 | { |
| 5543 | CLEAR_FIELD(lhs->lhs_arg_lvar); |
| 5544 | if (arg_exists(var_start, lhs->lhs_varlen, |
| 5545 | &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type, |
| 5546 | &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK) |
| 5547 | { |
| 5548 | if (is_decl) |
| 5549 | { |
| 5550 | semsg(_(e_str_is_used_as_argument), lhs->lhs_name); |
| 5551 | return FAIL; |
| 5552 | } |
| 5553 | lhs->lhs_lvar = &lhs->lhs_arg_lvar; |
| 5554 | } |
| 5555 | } |
| 5556 | if (lhs->lhs_lvar != NULL) |
| 5557 | { |
| 5558 | if (is_decl) |
| 5559 | { |
| 5560 | semsg(_(e_variable_already_declared), lhs->lhs_name); |
| 5561 | return FAIL; |
| 5562 | } |
| 5563 | } |
| 5564 | else |
| 5565 | { |
| 5566 | int script_namespace = lhs->lhs_varlen > 1 |
| 5567 | && STRNCMP(var_start, "s:", 2) == 0; |
| 5568 | int script_var = (script_namespace |
| 5569 | ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2, |
| 5570 | FALSE, cctx) |
| 5571 | : script_var_exists(var_start, lhs->lhs_varlen, |
| 5572 | TRUE, cctx)) == OK; |
| 5573 | imported_T *import = |
| 5574 | find_imported(var_start, lhs->lhs_varlen, cctx); |
| 5575 | |
| 5576 | if (script_namespace || script_var || import != NULL) |
| 5577 | { |
| 5578 | char_u *rawname = lhs->lhs_name |
| 5579 | + (lhs->lhs_name[1] == ':' ? 2 : 0); |
| 5580 | |
| 5581 | if (is_decl) |
| 5582 | { |
| 5583 | if (script_namespace) |
| 5584 | semsg(_(e_cannot_declare_script_variable_in_function), |
| 5585 | lhs->lhs_name); |
| 5586 | else |
| 5587 | semsg(_(e_variable_already_declared_in_script), |
| 5588 | lhs->lhs_name); |
| 5589 | return FAIL; |
| 5590 | } |
| 5591 | else if (cctx->ctx_ufunc->uf_script_ctx_version |
| 5592 | == SCRIPT_VERSION_VIM9 |
| 5593 | && script_namespace |
| 5594 | && !script_var && import == NULL) |
| 5595 | { |
| 5596 | semsg(_(e_unknown_variable_str), lhs->lhs_name); |
| 5597 | return FAIL; |
| 5598 | } |
| 5599 | |
| 5600 | lhs->lhs_dest = dest_script; |
| 5601 | |
| 5602 | // existing script-local variables should have a type |
| 5603 | lhs->lhs_scriptvar_sid = current_sctx.sc_sid; |
| 5604 | if (import != NULL) |
| 5605 | lhs->lhs_scriptvar_sid = import->imp_sid; |
| 5606 | if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid)) |
| 5607 | { |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 5608 | // Check writable only when no index follows. |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5609 | lhs->lhs_scriptvar_idx = get_script_item_idx( |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 5610 | lhs->lhs_scriptvar_sid, rawname, |
| 5611 | lhs->lhs_has_index ? ASSIGN_FINAL : ASSIGN_CONST, |
| 5612 | cctx); |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5613 | if (lhs->lhs_scriptvar_idx >= 0) |
| 5614 | { |
| 5615 | scriptitem_T *si = SCRIPT_ITEM( |
| 5616 | lhs->lhs_scriptvar_sid); |
| 5617 | svar_T *sv = |
| 5618 | ((svar_T *)si->sn_var_vals.ga_data) |
| 5619 | + lhs->lhs_scriptvar_idx; |
| 5620 | lhs->lhs_type = sv->sv_type; |
| 5621 | } |
| 5622 | } |
| 5623 | } |
| 5624 | else if (check_defined(var_start, lhs->lhs_varlen, cctx) |
| 5625 | == FAIL) |
| 5626 | return FAIL; |
| 5627 | } |
| 5628 | } |
| 5629 | |
| 5630 | if (declare_error) |
| 5631 | { |
| 5632 | vim9_declare_error(lhs->lhs_name); |
| 5633 | return FAIL; |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | // handle "a:name" as a name, not index "name" on "a" |
| 5638 | if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':') |
| 5639 | var_end = lhs->lhs_dest_end; |
| 5640 | |
| 5641 | if (lhs->lhs_dest != dest_option) |
| 5642 | { |
| 5643 | if (is_decl && *var_end == ':') |
| 5644 | { |
| 5645 | char_u *p; |
| 5646 | |
| 5647 | // parse optional type: "let var: type = expr" |
| 5648 | if (!VIM_ISWHITE(var_end[1])) |
| 5649 | { |
| 5650 | semsg(_(e_white_space_required_after_str), ":"); |
| 5651 | return FAIL; |
| 5652 | } |
| 5653 | p = skipwhite(var_end + 1); |
| 5654 | lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE); |
| 5655 | if (lhs->lhs_type == NULL) |
| 5656 | return FAIL; |
| 5657 | lhs->lhs_has_type = TRUE; |
| 5658 | } |
| 5659 | else if (lhs->lhs_lvar != NULL) |
| 5660 | lhs->lhs_type = lhs->lhs_lvar->lv_type; |
| 5661 | } |
| 5662 | |
| 5663 | if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global |
| 5664 | && lhs->lhs_type->tt_type != VAR_STRING |
| 5665 | && lhs->lhs_type->tt_type != VAR_ANY) |
| 5666 | { |
| 5667 | emsg(_(e_can_only_concatenate_to_string)); |
| 5668 | return FAIL; |
| 5669 | } |
| 5670 | |
| 5671 | if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local |
| 5672 | && cctx->ctx_skip != SKIP_YES) |
| 5673 | { |
| 5674 | if (oplen > 1 && !heredoc) |
| 5675 | { |
| 5676 | // +=, /=, etc. require an existing variable |
| 5677 | semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name); |
| 5678 | return FAIL; |
| 5679 | } |
| 5680 | if (!is_decl) |
| 5681 | { |
| 5682 | semsg(_(e_unknown_variable_str), lhs->lhs_name); |
| 5683 | return FAIL; |
| 5684 | } |
| 5685 | |
| 5686 | // new local variable |
| 5687 | if ((lhs->lhs_type->tt_type == VAR_FUNC |
| 5688 | || lhs->lhs_type->tt_type == VAR_PARTIAL) |
| 5689 | && var_wrong_func_name(lhs->lhs_name, TRUE)) |
| 5690 | return FAIL; |
| 5691 | lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen, |
| 5692 | cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type); |
| 5693 | if (lhs->lhs_lvar == NULL) |
| 5694 | return FAIL; |
| 5695 | lhs->lhs_new_local = TRUE; |
| 5696 | } |
| 5697 | |
| 5698 | lhs->lhs_member_type = lhs->lhs_type; |
Bram Moolenaar | 0825175 | 2021-01-11 21:20:18 +0100 | [diff] [blame] | 5699 | if (lhs->lhs_has_index) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5700 | { |
| 5701 | // Something follows after the variable: "var[idx]" or "var.key". |
| 5702 | // TODO: should we also handle "->func()" here? |
| 5703 | if (is_decl) |
| 5704 | { |
| 5705 | emsg(_(e_cannot_use_index_when_declaring_variable)); |
| 5706 | return FAIL; |
| 5707 | } |
| 5708 | |
| 5709 | if (var_start[lhs->lhs_varlen] == '[' |
| 5710 | || var_start[lhs->lhs_varlen] == '.') |
| 5711 | { |
| 5712 | char_u *after = var_start + lhs->lhs_varlen; |
| 5713 | char_u *p; |
| 5714 | |
| 5715 | // Only the last index is used below, if there are others |
| 5716 | // before it generate code for the expression. Thus for |
| 5717 | // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index. |
| 5718 | for (;;) |
| 5719 | { |
| 5720 | p = skip_index(after); |
| 5721 | if (*p != '[' && *p != '.') |
| 5722 | break; |
| 5723 | after = p; |
| 5724 | } |
| 5725 | if (after > var_start + lhs->lhs_varlen) |
| 5726 | { |
| 5727 | lhs->lhs_varlen = after - var_start; |
| 5728 | lhs->lhs_dest = dest_expr; |
| 5729 | // We don't know the type before evaluating the expression, |
| 5730 | // use "any" until then. |
| 5731 | lhs->lhs_type = &t_any; |
| 5732 | } |
| 5733 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5734 | if (lhs->lhs_type->tt_member == NULL) |
| 5735 | lhs->lhs_member_type = &t_any; |
| 5736 | else |
| 5737 | lhs->lhs_member_type = lhs->lhs_type->tt_member; |
| 5738 | } |
| 5739 | else |
| 5740 | { |
| 5741 | semsg("Not supported yet: %s", var_start); |
| 5742 | return FAIL; |
| 5743 | } |
| 5744 | } |
| 5745 | return OK; |
| 5746 | } |
| 5747 | |
| 5748 | /* |
| 5749 | * Assignment to a list or dict member, or ":unlet" for the item, using the |
| 5750 | * information in "lhs". |
| 5751 | * Returns OK or FAIL. |
| 5752 | */ |
| 5753 | static int |
| 5754 | compile_assign_unlet( |
| 5755 | char_u *var_start, |
| 5756 | lhs_T *lhs, |
| 5757 | int is_assign, |
| 5758 | type_T *rhs_type, |
| 5759 | cctx_T *cctx) |
| 5760 | { |
| 5761 | char_u *p; |
| 5762 | int r; |
| 5763 | vartype_T dest_type; |
| 5764 | size_t varlen = lhs->lhs_varlen; |
| 5765 | garray_T *stack = &cctx->ctx_type_stack; |
| 5766 | |
| 5767 | // Compile the "idx" in "var[idx]" or "key" in "var.key". |
| 5768 | p = var_start + varlen; |
| 5769 | if (*p == '[') |
| 5770 | { |
| 5771 | p = skipwhite(p + 1); |
| 5772 | r = compile_expr0(&p, cctx); |
| 5773 | if (r == OK && *skipwhite(p) != ']') |
| 5774 | { |
| 5775 | // this should not happen |
| 5776 | emsg(_(e_missbrac)); |
| 5777 | r = FAIL; |
| 5778 | } |
| 5779 | } |
| 5780 | else // if (*p == '.') |
| 5781 | { |
| 5782 | char_u *key_end = to_name_end(p + 1, TRUE); |
| 5783 | char_u *key = vim_strnsave(p + 1, key_end - p - 1); |
| 5784 | |
| 5785 | r = generate_PUSHS(cctx, key); |
| 5786 | } |
| 5787 | if (r == FAIL) |
| 5788 | return FAIL; |
| 5789 | |
| 5790 | if (lhs->lhs_type == &t_any) |
| 5791 | { |
| 5792 | // Index on variable of unknown type: check at runtime. |
| 5793 | dest_type = VAR_ANY; |
| 5794 | } |
| 5795 | else |
| 5796 | { |
| 5797 | dest_type = lhs->lhs_type->tt_type; |
| 5798 | if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL) |
| 5799 | return FAIL; |
| 5800 | if (dest_type == VAR_LIST |
| 5801 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
| 5802 | != VAR_NUMBER) |
| 5803 | { |
| 5804 | emsg(_(e_number_exp)); |
| 5805 | return FAIL; |
| 5806 | } |
| 5807 | } |
| 5808 | |
| 5809 | // Load the dict or list. On the stack we then have: |
| 5810 | // - value (for assignment, not for :unlet) |
| 5811 | // - index |
| 5812 | // - variable |
| 5813 | if (lhs->lhs_dest == dest_expr) |
| 5814 | { |
| 5815 | int c = var_start[varlen]; |
| 5816 | |
| 5817 | // Evaluate "ll[expr]" of "ll[expr][idx]" |
| 5818 | p = var_start; |
| 5819 | var_start[varlen] = NUL; |
| 5820 | if (compile_expr0(&p, cctx) == OK && p != var_start + varlen) |
| 5821 | { |
| 5822 | // this should not happen |
| 5823 | emsg(_(e_missbrac)); |
| 5824 | return FAIL; |
| 5825 | } |
| 5826 | var_start[varlen] = c; |
| 5827 | |
| 5828 | lhs->lhs_type = stack->ga_len == 0 ? &t_void |
| 5829 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 5830 | // now we can properly check the type |
| 5831 | if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void |
| 5832 | && need_type(rhs_type, lhs->lhs_type->tt_member, -2, cctx, |
| 5833 | FALSE, FALSE) == FAIL) |
| 5834 | return FAIL; |
| 5835 | } |
| 5836 | else |
| 5837 | generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name, |
| 5838 | lhs->lhs_lvar, lhs->lhs_type); |
| 5839 | |
| 5840 | if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY) |
| 5841 | { |
| 5842 | if (is_assign) |
| 5843 | { |
| 5844 | isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3); |
| 5845 | |
| 5846 | if (isn == NULL) |
| 5847 | return FAIL; |
| 5848 | isn->isn_arg.vartype = dest_type; |
| 5849 | } |
| 5850 | else |
| 5851 | { |
| 5852 | if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL) |
| 5853 | return FAIL; |
| 5854 | } |
| 5855 | } |
| 5856 | else |
| 5857 | { |
| 5858 | emsg(_(e_indexable_type_required)); |
| 5859 | return FAIL; |
| 5860 | } |
| 5861 | |
| 5862 | return OK; |
| 5863 | } |
| 5864 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5865 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5866 | * Compile declaration and assignment: |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 5867 | * "let name" |
| 5868 | * "var name = expr" |
| 5869 | * "final name = expr" |
| 5870 | * "const name = expr" |
| 5871 | * "name = expr" |
| 5872 | * "arg" points to "name". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5873 | * Return NULL for an error. |
| 5874 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5875 | */ |
| 5876 | static char_u * |
| 5877 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 5878 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5879 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5880 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5881 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5882 | char_u *ret = NULL; |
| 5883 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5884 | int var_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5885 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5886 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5887 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5888 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5889 | int oplen = 0; |
| 5890 | int heredoc = FALSE; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5891 | type_T *rhs_type = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5892 | char_u *sp; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5893 | int is_decl = is_decl_command(cmdidx); |
| 5894 | lhs_T lhs; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5895 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5896 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 5897 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 5898 | if (p == NULL) |
| 5899 | return *arg == '[' ? arg : NULL; |
| 5900 | |
| 5901 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5902 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 5903 | // TODO: should we allow this, and figure out type inference from list |
| 5904 | // members? |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5905 | emsg(_(e_cannot_use_list_for_declaration)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5906 | return NULL; |
| 5907 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5908 | lhs.lhs_name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5909 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5910 | sp = p; |
| 5911 | p = skipwhite(p); |
| 5912 | op = p; |
| 5913 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5914 | |
| 5915 | if (var_count > 0 && oplen == 0) |
| 5916 | // can be something like "[1, 2]->func()" |
| 5917 | return arg; |
| 5918 | |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5919 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen]))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5920 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 5921 | error_white_both(op, oplen); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5922 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 5923 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5924 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5925 | if (heredoc) |
| 5926 | { |
| 5927 | list_T *l; |
| 5928 | listitem_T *li; |
| 5929 | |
| 5930 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5931 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5932 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 5933 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | c0e2901 | 2020-09-27 14:22:48 +0200 | [diff] [blame] | 5934 | if (l == NULL) |
| 5935 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5936 | |
Bram Moolenaar | 078269b | 2020-09-21 20:35:55 +0200 | [diff] [blame] | 5937 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5938 | { |
Bram Moolenaar | 078269b | 2020-09-21 20:35:55 +0200 | [diff] [blame] | 5939 | // Push each line and the create the list. |
| 5940 | FOR_ALL_LIST_ITEMS(l, li) |
| 5941 | { |
| 5942 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 5943 | li->li_tv.vval.v_string = NULL; |
| 5944 | } |
| 5945 | generate_NEWLIST(cctx, l->lv_len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5946 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5947 | list_free(l); |
| 5948 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5949 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5950 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5951 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5952 | { |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5953 | char_u *wp; |
| 5954 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5955 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 5956 | // list of variables below. |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5957 | // A line break may follow the "=". |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5958 | |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5959 | wp = op + oplen; |
Bram Moolenaar | 8ff16e0 | 2020-12-07 21:49:52 +0100 | [diff] [blame] | 5960 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5961 | return FAIL; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5962 | if (compile_expr0(&p, cctx) == FAIL) |
| 5963 | return NULL; |
| 5964 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5965 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5966 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5967 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5968 | type_T *stacktype; |
| 5969 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5970 | stacktype = stack->ga_len == 0 ? &t_void |
| 5971 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5972 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5973 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5974 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5975 | goto theend; |
| 5976 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 5977 | if (need_type(stacktype, &t_list_any, -1, cctx, |
| 5978 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5979 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5980 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5981 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 5982 | semicolon); |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5983 | if (stacktype->tt_member != NULL) |
| 5984 | rhs_type = stacktype->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5985 | } |
| 5986 | } |
| 5987 | |
| 5988 | /* |
| 5989 | * Loop over variables in "[var, var] = expr". |
| 5990 | * For "var = expr" and "let var: type" this is done only once. |
| 5991 | */ |
| 5992 | if (var_count > 0) |
| 5993 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 5994 | else |
| 5995 | var_start = arg; |
| 5996 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 5997 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5998 | int instr_count = -1; |
| 5999 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6000 | vim_free(lhs.lhs_name); |
| 6001 | |
| 6002 | /* |
| 6003 | * Figure out the LHS type and other properties. |
| 6004 | */ |
| 6005 | if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL) |
| 6006 | goto theend; |
| 6007 | |
| 6008 | if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar) |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 6009 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6010 | semsg(_(e_cannot_assign_to_argument), lhs.lhs_name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6011 | goto theend; |
| 6012 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6013 | if (!is_decl && lhs.lhs_lvar != NULL |
| 6014 | && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6015 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6016 | semsg(_(e_cannot_assign_to_constant), lhs.lhs_name); |
Bram Moolenaar | dbeecb2 | 2020-09-14 18:15:09 +0200 | [diff] [blame] | 6017 | goto theend; |
| 6018 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6019 | |
| 6020 | if (!heredoc) |
| 6021 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6022 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6023 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6024 | if (oplen > 0 && var_count == 0) |
| 6025 | { |
| 6026 | // skip over the "=" and the expression |
| 6027 | p = skipwhite(op + oplen); |
| 6028 | compile_expr0(&p, cctx); |
| 6029 | } |
| 6030 | } |
| 6031 | else if (oplen > 0) |
| 6032 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 6033 | int is_const = FALSE; |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 6034 | char_u *wp; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6035 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6036 | // For "var = expr" evaluate the expression. |
| 6037 | if (var_count == 0) |
| 6038 | { |
| 6039 | int r; |
| 6040 | |
| 6041 | // for "+=", "*=", "..=" etc. first load the current value |
| 6042 | if (*op != '=') |
| 6043 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6044 | generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name, |
| 6045 | lhs.lhs_lvar, lhs.lhs_type); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6046 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6047 | if (lhs.lhs_has_index) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6048 | { |
| 6049 | // TODO: get member from list or dict |
| 6050 | emsg("Index with operation not supported yet"); |
| 6051 | goto theend; |
| 6052 | } |
| 6053 | } |
| 6054 | |
| 6055 | // Compile the expression. Temporarily hide the new local |
| 6056 | // variable here, it is not available to this expression. |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6057 | if (lhs.lhs_new_local) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6058 | --cctx->ctx_locals.ga_len; |
| 6059 | instr_count = instr->ga_len; |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 6060 | wp = op + oplen; |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 6061 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
Bram Moolenaar | 21e5122 | 2020-12-04 12:43:29 +0100 | [diff] [blame] | 6062 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6063 | if (lhs.lhs_new_local) |
Bram Moolenaar | 21e5122 | 2020-12-04 12:43:29 +0100 | [diff] [blame] | 6064 | ++cctx->ctx_locals.ga_len; |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 6065 | goto theend; |
Bram Moolenaar | 21e5122 | 2020-12-04 12:43:29 +0100 | [diff] [blame] | 6066 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 6067 | r = compile_expr0_ext(&p, cctx, &is_const); |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6068 | if (lhs.lhs_new_local) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6069 | ++cctx->ctx_locals.ga_len; |
| 6070 | if (r == FAIL) |
| 6071 | goto theend; |
| 6072 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 6073 | else if (semicolon && var_idx == var_count - 1) |
| 6074 | { |
| 6075 | // For "[var; var] = expr" get the rest of the list |
| 6076 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 6077 | goto theend; |
| 6078 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6079 | else |
| 6080 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6081 | // For "[var, var] = expr" get the "var_idx" item from the |
| 6082 | // list. |
| 6083 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 6084 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6085 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6086 | |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6087 | rhs_type = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 6088 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6089 | if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6090 | { |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6091 | if ((rhs_type->tt_type == VAR_FUNC |
| 6092 | || rhs_type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6093 | && var_wrong_func_name(lhs.lhs_name, TRUE)) |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 6094 | goto theend; |
| 6095 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6096 | if (lhs.lhs_new_local && !lhs.lhs_has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6097 | { |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6098 | if (rhs_type->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6099 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6100 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6101 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6102 | } |
| 6103 | else |
| 6104 | { |
Bram Moolenaar | 04bdd57 | 2020-09-23 13:25:32 +0200 | [diff] [blame] | 6105 | // An empty list or dict has a &t_unknown member, |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6106 | // for a variable that implies &t_any. |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6107 | if (rhs_type == &t_list_empty) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6108 | lhs.lhs_lvar->lv_type = &t_list_any; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6109 | else if (rhs_type == &t_dict_empty) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6110 | lhs.lhs_lvar->lv_type = &t_dict_any; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6111 | else if (rhs_type == &t_unknown) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6112 | lhs.lhs_lvar->lv_type = &t_any; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6113 | else |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6114 | lhs.lhs_lvar->lv_type = rhs_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6115 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6116 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6117 | else if (*op == '=') |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6118 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6119 | type_T *use_type = lhs.lhs_lvar->lv_type; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6120 | |
Bram Moolenaar | 71abe48 | 2020-09-14 22:28:30 +0200 | [diff] [blame] | 6121 | // without operator check type here, otherwise below |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6122 | if (lhs.lhs_has_index) |
| 6123 | use_type = lhs.lhs_member_type; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6124 | if (need_type(rhs_type, use_type, -1, cctx, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 6125 | FALSE, is_const) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6126 | goto theend; |
| 6127 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6128 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6129 | else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type, |
| 6130 | -1, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6131 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6132 | } |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 6133 | else if (cmdidx == CMD_final) |
| 6134 | { |
| 6135 | emsg(_(e_final_requires_a_value)); |
| 6136 | goto theend; |
| 6137 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6138 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6139 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 6140 | emsg(_(e_const_requires_a_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6141 | goto theend; |
| 6142 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6143 | else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6144 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 6145 | emsg(_(e_type_or_initialization_required)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6146 | goto theend; |
| 6147 | } |
| 6148 | else |
| 6149 | { |
| 6150 | // variables are always initialized |
| 6151 | if (ga_grow(instr, 1) == FAIL) |
| 6152 | goto theend; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6153 | switch (lhs.lhs_member_type->tt_type) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6154 | { |
| 6155 | case VAR_BOOL: |
| 6156 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 6157 | break; |
| 6158 | case VAR_FLOAT: |
| 6159 | #ifdef FEAT_FLOAT |
| 6160 | generate_PUSHF(cctx, 0.0); |
| 6161 | #endif |
| 6162 | break; |
| 6163 | case VAR_STRING: |
| 6164 | generate_PUSHS(cctx, NULL); |
| 6165 | break; |
| 6166 | case VAR_BLOB: |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 6167 | generate_PUSHBLOB(cctx, blob_alloc()); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6168 | break; |
| 6169 | case VAR_FUNC: |
| 6170 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 6171 | break; |
| 6172 | case VAR_LIST: |
| 6173 | generate_NEWLIST(cctx, 0); |
| 6174 | break; |
| 6175 | case VAR_DICT: |
| 6176 | generate_NEWDICT(cctx, 0); |
| 6177 | break; |
| 6178 | case VAR_JOB: |
| 6179 | generate_PUSHJOB(cctx, NULL); |
| 6180 | break; |
| 6181 | case VAR_CHANNEL: |
| 6182 | generate_PUSHCHANNEL(cctx, NULL); |
| 6183 | break; |
| 6184 | case VAR_NUMBER: |
| 6185 | case VAR_UNKNOWN: |
| 6186 | case VAR_ANY: |
| 6187 | case VAR_PARTIAL: |
| 6188 | case VAR_VOID: |
| 6189 | case VAR_SPECIAL: // cannot happen |
| 6190 | generate_PUSHNR(cctx, 0); |
| 6191 | break; |
| 6192 | } |
| 6193 | } |
| 6194 | if (var_count == 0) |
| 6195 | end = p; |
| 6196 | } |
| 6197 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6198 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6199 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6200 | break; |
| 6201 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6202 | if (oplen > 0 && *op != '=') |
| 6203 | { |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6204 | type_T *expected; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6205 | type_T *stacktype; |
| 6206 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6207 | if (*op == '.') |
| 6208 | expected = &t_string; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6209 | else |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6210 | expected = lhs.lhs_member_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6211 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6212 | if ( |
| 6213 | #ifdef FEAT_FLOAT |
| 6214 | // If variable is float operation with number is OK. |
| 6215 | !(expected == &t_float && stacktype == &t_number) && |
| 6216 | #endif |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 6217 | need_type(stacktype, expected, -1, cctx, |
| 6218 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6219 | goto theend; |
| 6220 | |
| 6221 | if (*op == '.') |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 6222 | { |
| 6223 | if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL) |
| 6224 | goto theend; |
| 6225 | } |
| 6226 | else if (*op == '+') |
| 6227 | { |
| 6228 | if (generate_add_instr(cctx, |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6229 | operator_type(lhs.lhs_member_type, stacktype), |
| 6230 | lhs.lhs_member_type, stacktype) == FAIL) |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 6231 | goto theend; |
| 6232 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6233 | else if (generate_two_op(cctx, op) == FAIL) |
| 6234 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6235 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6236 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6237 | if (lhs.lhs_has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6238 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6239 | // Use the info in "lhs" to store the value at the index in the |
| 6240 | // list or dict. |
| 6241 | if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx) |
| 6242 | == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6243 | goto theend; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6244 | } |
| 6245 | else |
| 6246 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6247 | if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script |
| 6248 | || lhs.lhs_dest == dest_local)) |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 6249 | // ":const var": lock the value, but not referenced variables |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 6250 | generate_LOCKCONST(cctx); |
| 6251 | |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 6252 | if (is_decl |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6253 | && (lhs.lhs_type->tt_type == VAR_DICT |
| 6254 | || lhs.lhs_type->tt_type == VAR_LIST) |
| 6255 | && lhs.lhs_type->tt_member != NULL |
| 6256 | && lhs.lhs_type->tt_member != &t_any |
| 6257 | && lhs.lhs_type->tt_member != &t_unknown) |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 6258 | // Set the type in the list or dict, so that it can be checked, |
| 6259 | // also in legacy script. |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6260 | generate_SETTYPE(cctx, lhs.lhs_type); |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 6261 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6262 | if (lhs.lhs_dest != dest_local) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6263 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6264 | if (generate_store_var(cctx, lhs.lhs_dest, |
| 6265 | lhs.lhs_opt_flags, lhs.lhs_vimvaridx, |
| 6266 | lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid, |
| 6267 | lhs.lhs_type, lhs.lhs_name) == FAIL) |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6268 | goto theend; |
| 6269 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6270 | else if (lhs.lhs_lvar != NULL) |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6271 | { |
| 6272 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 6273 | + instr->ga_len - 1; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6274 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6275 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 6276 | // ISN_STORE into ISN_STORENR |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 6277 | if (lhs.lhs_lvar->lv_from_outer == 0 |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6278 | && instr->ga_len == instr_count + 1 |
| 6279 | && isn->isn_type == ISN_PUSHNR) |
| 6280 | { |
| 6281 | varnumber_T val = isn->isn_arg.number; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6282 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6283 | isn->isn_type = ISN_STORENR; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6284 | isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6285 | isn->isn_arg.storenr.stnr_val = val; |
| 6286 | if (stack->ga_len > 0) |
| 6287 | --stack->ga_len; |
| 6288 | } |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 6289 | else if (lhs.lhs_lvar->lv_from_outer > 0) |
| 6290 | generate_STOREOUTER(cctx, lhs.lhs_lvar->lv_idx, |
| 6291 | lhs.lhs_lvar->lv_from_outer); |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6292 | else |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6293 | generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6294 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6295 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6296 | |
| 6297 | if (var_idx + 1 < var_count) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6298 | var_start = skipwhite(lhs.lhs_dest_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6299 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6300 | |
| 6301 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 6302 | if (var_count > 0 && !semicolon) |
| 6303 | { |
Bram Moolenaar | ec79229 | 2020-12-13 21:26:56 +0100 | [diff] [blame] | 6304 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 6305 | goto theend; |
| 6306 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6307 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 6308 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6309 | |
| 6310 | theend: |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6311 | vim_free(lhs.lhs_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6312 | return ret; |
| 6313 | } |
| 6314 | |
| 6315 | /* |
Bram Moolenaar | 17126b1 | 2021-01-07 22:03:02 +0100 | [diff] [blame] | 6316 | * Check for an assignment at "eap->cmd", compile it if found. |
| 6317 | * Return NOTDONE if there is none, FAIL for failure, OK if done. |
| 6318 | */ |
| 6319 | static int |
| 6320 | may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx) |
| 6321 | { |
| 6322 | char_u *pskip; |
| 6323 | char_u *p; |
| 6324 | |
| 6325 | // Assuming the command starts with a variable or function name, |
| 6326 | // find what follows. |
| 6327 | // Skip over "var.member", "var[idx]" and the like. |
| 6328 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 6329 | pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@') |
| 6330 | ? eap->cmd + 1 : eap->cmd; |
| 6331 | p = to_name_end(pskip, TRUE); |
| 6332 | if (p > eap->cmd && *p != NUL) |
| 6333 | { |
| 6334 | char_u *var_end; |
| 6335 | int oplen; |
| 6336 | int heredoc; |
| 6337 | |
| 6338 | if (eap->cmd[0] == '@') |
| 6339 | var_end = eap->cmd + 2; |
| 6340 | else |
| 6341 | var_end = find_name_end(pskip, NULL, NULL, |
| 6342 | FNE_CHECK_START | FNE_INCL_BR); |
| 6343 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
| 6344 | if (oplen > 0) |
| 6345 | { |
| 6346 | size_t len = p - eap->cmd; |
| 6347 | |
| 6348 | // Recognize an assignment if we recognize the variable |
| 6349 | // name: |
| 6350 | // "g:var = expr" |
| 6351 | // "local = expr" where "local" is a local var. |
| 6352 | // "script = expr" where "script" is a script-local var. |
| 6353 | // "import = expr" where "import" is an imported var |
| 6354 | // "&opt = expr" |
| 6355 | // "$ENV = expr" |
| 6356 | // "@r = expr" |
| 6357 | if (*eap->cmd == '&' |
| 6358 | || *eap->cmd == '$' |
| 6359 | || *eap->cmd == '@' |
| 6360 | || ((len) > 2 && eap->cmd[1] == ':') |
| 6361 | || lookup_local(eap->cmd, len, NULL, cctx) == OK |
| 6362 | || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK |
| 6363 | || script_var_exists(eap->cmd, len, FALSE, cctx) == OK |
| 6364 | || find_imported(eap->cmd, len, cctx) != NULL) |
| 6365 | { |
| 6366 | *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx); |
| 6367 | if (*line == NULL || *line == eap->cmd) |
| 6368 | return FAIL; |
| 6369 | return OK; |
| 6370 | } |
| 6371 | } |
| 6372 | } |
| 6373 | |
| 6374 | if (*eap->cmd == '[') |
| 6375 | { |
| 6376 | // [var, var] = expr |
| 6377 | *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx); |
| 6378 | if (*line == NULL) |
| 6379 | return FAIL; |
| 6380 | if (*line != eap->cmd) |
| 6381 | return OK; |
| 6382 | } |
| 6383 | return NOTDONE; |
| 6384 | } |
| 6385 | |
| 6386 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6387 | * Check if "name" can be "unlet". |
| 6388 | */ |
| 6389 | int |
| 6390 | check_vim9_unlet(char_u *name) |
| 6391 | { |
| 6392 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 6393 | { |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 6394 | // "unlet s:var" is allowed in legacy script. |
| 6395 | if (*name == 's' && !script_is_vim9()) |
| 6396 | return OK; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6397 | semsg(_(e_cannot_unlet_str), name); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6398 | return FAIL; |
| 6399 | } |
| 6400 | return OK; |
| 6401 | } |
| 6402 | |
| 6403 | /* |
| 6404 | * Callback passed to ex_unletlock(). |
| 6405 | */ |
| 6406 | static int |
| 6407 | compile_unlet( |
| 6408 | lval_T *lvp, |
| 6409 | char_u *name_end, |
| 6410 | exarg_T *eap, |
| 6411 | int deep UNUSED, |
| 6412 | void *coookie) |
| 6413 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6414 | cctx_T *cctx = coookie; |
| 6415 | char_u *p = lvp->ll_name; |
| 6416 | int cc = *name_end; |
| 6417 | int ret = OK; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6418 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6419 | if (cctx->ctx_skip == SKIP_YES) |
| 6420 | return OK; |
| 6421 | |
| 6422 | *name_end = NUL; |
| 6423 | if (*p == '$') |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6424 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6425 | // :unlet $ENV_VAR |
| 6426 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 6427 | } |
| 6428 | else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL) |
| 6429 | { |
| 6430 | lhs_T lhs; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6431 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6432 | // This is similar to assigning: lookup the list/dict, compile the |
| 6433 | // idx/key. Then instead of storing the value unlet the item. |
| 6434 | // unlet {list}[idx] |
| 6435 | // unlet {dict}[key] dict.key |
| 6436 | // |
| 6437 | // Figure out the LHS type and other properties. |
| 6438 | // |
| 6439 | ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx); |
| 6440 | |
| 6441 | // : unlet an indexed item |
| 6442 | if (!lhs.lhs_has_index) |
Bram Moolenaar | 2ef951d | 2021-01-03 20:55:26 +0100 | [diff] [blame] | 6443 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6444 | iemsg("called compile_lhs() without an index"); |
| 6445 | ret = FAIL; |
| 6446 | } |
| 6447 | else |
| 6448 | { |
| 6449 | // Use the info in "lhs" to unlet the item at the index in the |
| 6450 | // list or dict. |
| 6451 | ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx); |
Bram Moolenaar | 2ef951d | 2021-01-03 20:55:26 +0100 | [diff] [blame] | 6452 | } |
| 6453 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6454 | vim_free(lhs.lhs_name); |
| 6455 | } |
| 6456 | else if (check_vim9_unlet(p) == FAIL) |
| 6457 | { |
| 6458 | ret = FAIL; |
| 6459 | } |
| 6460 | else |
| 6461 | { |
| 6462 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 6463 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6464 | } |
| 6465 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6466 | *name_end = cc; |
| 6467 | return ret; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6468 | } |
| 6469 | |
| 6470 | /* |
| 6471 | * compile "unlet var", "lock var" and "unlock var" |
| 6472 | * "arg" points to "var". |
| 6473 | */ |
| 6474 | static char_u * |
| 6475 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 6476 | { |
| 6477 | char_u *p = arg; |
| 6478 | |
| 6479 | if (eap->cmdidx != CMD_unlet) |
| 6480 | { |
| 6481 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 6482 | return NULL; |
| 6483 | } |
| 6484 | |
Bram Moolenaar | 2ef951d | 2021-01-03 20:55:26 +0100 | [diff] [blame] | 6485 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING, |
| 6486 | compile_unlet, cctx); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6487 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 6488 | } |
| 6489 | |
| 6490 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6491 | * Compile an :import command. |
| 6492 | */ |
| 6493 | static char_u * |
| 6494 | compile_import(char_u *arg, cctx_T *cctx) |
| 6495 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 6496 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6497 | } |
| 6498 | |
| 6499 | /* |
| 6500 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 6501 | */ |
| 6502 | static int |
| 6503 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 6504 | { |
| 6505 | garray_T *instr = &cctx->ctx_instr; |
| 6506 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 6507 | |
| 6508 | if (endlabel == NULL) |
| 6509 | return FAIL; |
| 6510 | endlabel->el_next = *el; |
| 6511 | *el = endlabel; |
| 6512 | endlabel->el_end_label = instr->ga_len; |
| 6513 | |
| 6514 | generate_JUMP(cctx, when, 0); |
| 6515 | return OK; |
| 6516 | } |
| 6517 | |
| 6518 | static void |
| 6519 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 6520 | { |
| 6521 | garray_T *instr = &cctx->ctx_instr; |
| 6522 | |
| 6523 | while (*el != NULL) |
| 6524 | { |
| 6525 | endlabel_T *cur = (*el); |
| 6526 | isn_T *isn; |
| 6527 | |
| 6528 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 6529 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6530 | *el = cur->el_next; |
| 6531 | vim_free(cur); |
| 6532 | } |
| 6533 | } |
| 6534 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6535 | static void |
| 6536 | compile_free_jump_to_end(endlabel_T **el) |
| 6537 | { |
| 6538 | while (*el != NULL) |
| 6539 | { |
| 6540 | endlabel_T *cur = (*el); |
| 6541 | |
| 6542 | *el = cur->el_next; |
| 6543 | vim_free(cur); |
| 6544 | } |
| 6545 | } |
| 6546 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6547 | /* |
| 6548 | * Create a new scope and set up the generic items. |
| 6549 | */ |
| 6550 | static scope_T * |
| 6551 | new_scope(cctx_T *cctx, scopetype_T type) |
| 6552 | { |
| 6553 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 6554 | |
| 6555 | if (scope == NULL) |
| 6556 | return NULL; |
| 6557 | scope->se_outer = cctx->ctx_scope; |
| 6558 | cctx->ctx_scope = scope; |
| 6559 | scope->se_type = type; |
| 6560 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 6561 | return scope; |
| 6562 | } |
| 6563 | |
| 6564 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6565 | * Free the current scope and go back to the outer scope. |
| 6566 | */ |
| 6567 | static void |
| 6568 | drop_scope(cctx_T *cctx) |
| 6569 | { |
| 6570 | scope_T *scope = cctx->ctx_scope; |
| 6571 | |
| 6572 | if (scope == NULL) |
| 6573 | { |
| 6574 | iemsg("calling drop_scope() without a scope"); |
| 6575 | return; |
| 6576 | } |
| 6577 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6578 | switch (scope->se_type) |
| 6579 | { |
| 6580 | case IF_SCOPE: |
| 6581 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 6582 | case FOR_SCOPE: |
| 6583 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 6584 | case WHILE_SCOPE: |
| 6585 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 6586 | case TRY_SCOPE: |
| 6587 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 6588 | case NO_SCOPE: |
| 6589 | case BLOCK_SCOPE: |
| 6590 | break; |
| 6591 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6592 | vim_free(scope); |
| 6593 | } |
| 6594 | |
| 6595 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6596 | * compile "if expr" |
| 6597 | * |
| 6598 | * "if expr" Produces instructions: |
| 6599 | * EVAL expr Push result of "expr" |
| 6600 | * JUMP_IF_FALSE end |
| 6601 | * ... body ... |
| 6602 | * end: |
| 6603 | * |
| 6604 | * "if expr | else" Produces instructions: |
| 6605 | * EVAL expr Push result of "expr" |
| 6606 | * JUMP_IF_FALSE else |
| 6607 | * ... body ... |
| 6608 | * JUMP_ALWAYS end |
| 6609 | * else: |
| 6610 | * ... body ... |
| 6611 | * end: |
| 6612 | * |
| 6613 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 6614 | * EVAL expr Push result of "expr" |
| 6615 | * JUMP_IF_FALSE elseif |
| 6616 | * ... body ... |
| 6617 | * JUMP_ALWAYS end |
| 6618 | * elseif: |
| 6619 | * EVAL expr Push result of "expr" |
| 6620 | * JUMP_IF_FALSE else |
| 6621 | * ... body ... |
| 6622 | * JUMP_ALWAYS end |
| 6623 | * else: |
| 6624 | * ... body ... |
| 6625 | * end: |
| 6626 | */ |
| 6627 | static char_u * |
| 6628 | compile_if(char_u *arg, cctx_T *cctx) |
| 6629 | { |
| 6630 | char_u *p = arg; |
| 6631 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6632 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6633 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6634 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6635 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6636 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6637 | CLEAR_FIELD(ppconst); |
| 6638 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6639 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6640 | clear_ppconst(&ppconst); |
| 6641 | return NULL; |
| 6642 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6643 | if (cctx->ctx_skip == SKIP_YES) |
| 6644 | clear_ppconst(&ppconst); |
| 6645 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6646 | { |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6647 | int error = FALSE; |
| 6648 | int v; |
| 6649 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6650 | // The expression results in a constant. |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6651 | v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); |
Bram Moolenaar | dda749c | 2020-10-04 17:24:29 +0200 | [diff] [blame] | 6652 | clear_ppconst(&ppconst); |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6653 | if (error) |
| 6654 | return NULL; |
| 6655 | cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6656 | } |
| 6657 | else |
| 6658 | { |
| 6659 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6660 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6661 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6662 | return NULL; |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6663 | if (bool_on_stack(cctx) == FAIL) |
| 6664 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6665 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6666 | |
| 6667 | scope = new_scope(cctx, IF_SCOPE); |
| 6668 | if (scope == NULL) |
| 6669 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6670 | scope->se_skip_save = skip_save; |
| 6671 | // "is_had_return" will be reset if any block does not end in :return |
| 6672 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6673 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6674 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6675 | { |
| 6676 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6677 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6678 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6679 | } |
| 6680 | else |
| 6681 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6682 | |
| 6683 | return p; |
| 6684 | } |
| 6685 | |
| 6686 | static char_u * |
| 6687 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 6688 | { |
| 6689 | char_u *p = arg; |
| 6690 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6691 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6692 | isn_T *isn; |
| 6693 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6694 | ppconst_T ppconst; |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 6695 | skip_T save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6696 | |
| 6697 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6698 | { |
| 6699 | emsg(_(e_elseif_without_if)); |
| 6700 | return NULL; |
| 6701 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6702 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6703 | if (!cctx->ctx_had_return) |
| 6704 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6705 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6706 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6707 | { |
| 6708 | 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] | 6709 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6710 | return NULL; |
| 6711 | // previous "if" or "elseif" jumps here |
| 6712 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6713 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6714 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6715 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6716 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6717 | CLEAR_FIELD(ppconst); |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 6718 | if (cctx->ctx_skip == SKIP_YES) |
| 6719 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6720 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6721 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6722 | clear_ppconst(&ppconst); |
| 6723 | return NULL; |
| 6724 | } |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 6725 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6726 | if (scope->se_skip_save == SKIP_YES) |
| 6727 | clear_ppconst(&ppconst); |
| 6728 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6729 | { |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6730 | int error = FALSE; |
| 6731 | int v; |
| 6732 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6733 | // The expression results in a constant. |
| 6734 | // TODO: how about nesting? |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6735 | v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); |
| 6736 | if (error) |
| 6737 | return NULL; |
| 6738 | cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6739 | clear_ppconst(&ppconst); |
| 6740 | scope->se_u.se_if.is_if_label = -1; |
| 6741 | } |
| 6742 | else |
| 6743 | { |
| 6744 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6745 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6746 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6747 | return NULL; |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6748 | if (bool_on_stack(cctx) == FAIL) |
| 6749 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6750 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6751 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6752 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6753 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6754 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6755 | |
| 6756 | return p; |
| 6757 | } |
| 6758 | |
| 6759 | static char_u * |
| 6760 | compile_else(char_u *arg, cctx_T *cctx) |
| 6761 | { |
| 6762 | char_u *p = arg; |
| 6763 | garray_T *instr = &cctx->ctx_instr; |
| 6764 | isn_T *isn; |
| 6765 | scope_T *scope = cctx->ctx_scope; |
| 6766 | |
| 6767 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6768 | { |
| 6769 | emsg(_(e_else_without_if)); |
| 6770 | return NULL; |
| 6771 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6772 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6773 | if (!cctx->ctx_had_return) |
| 6774 | scope->se_u.se_if.is_had_return = FALSE; |
| 6775 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6776 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6777 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6778 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6779 | // 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] | 6780 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
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 | if (!cctx->ctx_had_return |
| 6783 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6784 | JUMP_ALWAYS, cctx) == FAIL) |
| 6785 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6786 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6787 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6788 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6789 | { |
| 6790 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6791 | { |
| 6792 | // previous "if" or "elseif" jumps here |
| 6793 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6794 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6795 | scope->se_u.se_if.is_if_label = -1; |
| 6796 | } |
| 6797 | } |
| 6798 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6799 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6800 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6801 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6802 | |
| 6803 | return p; |
| 6804 | } |
| 6805 | |
| 6806 | static char_u * |
| 6807 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6808 | { |
| 6809 | scope_T *scope = cctx->ctx_scope; |
| 6810 | ifscope_T *ifscope; |
| 6811 | garray_T *instr = &cctx->ctx_instr; |
| 6812 | isn_T *isn; |
| 6813 | |
| 6814 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6815 | { |
| 6816 | emsg(_(e_endif_without_if)); |
| 6817 | return NULL; |
| 6818 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6819 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6820 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6821 | if (!cctx->ctx_had_return) |
| 6822 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6823 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6824 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6825 | { |
| 6826 | // previous "if" or "elseif" jumps here |
| 6827 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6828 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6829 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6830 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6831 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6832 | cctx->ctx_skip = scope->se_skip_save; |
| 6833 | |
| 6834 | // If all the blocks end in :return and there is an :else then the |
| 6835 | // had_return flag is set. |
| 6836 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6837 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6838 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6839 | return arg; |
| 6840 | } |
| 6841 | |
| 6842 | /* |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6843 | * Compile "for var in expr": |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6844 | * |
| 6845 | * Produces instructions: |
| 6846 | * PUSHNR -1 |
| 6847 | * STORE loop-idx Set index to -1 |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6848 | * EVAL expr result of "expr" on top of stack |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6849 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6850 | * - if beyond end, jump to "end" |
| 6851 | * - otherwise get item from list and push it |
| 6852 | * STORE var Store item in "var" |
| 6853 | * ... body ... |
| 6854 | * JUMP top Jump back to repeat |
| 6855 | * end: DROP Drop the result of "expr" |
| 6856 | * |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6857 | * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var": |
| 6858 | * UNPACK 2 Split item in 2 |
| 6859 | * STORE var1 Store item in "var1" |
| 6860 | * STORE var2 Store item in "var2" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6861 | */ |
| 6862 | static char_u * |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6863 | compile_for(char_u *arg_start, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6864 | { |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6865 | char_u *arg; |
| 6866 | char_u *arg_end; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6867 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6868 | char_u *p; |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6869 | char_u *wp; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6870 | int var_count = 0; |
| 6871 | int semicolon = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6872 | size_t varlen; |
| 6873 | garray_T *instr = &cctx->ctx_instr; |
| 6874 | garray_T *stack = &cctx->ctx_type_stack; |
| 6875 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6876 | lvar_T *loop_lvar; // loop iteration variable |
| 6877 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6878 | type_T *vartype; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6879 | type_T *item_type = &t_any; |
| 6880 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6881 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6882 | p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE); |
| 6883 | if (var_count == 0) |
| 6884 | var_count = 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6885 | |
| 6886 | // consume "in" |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6887 | wp = p; |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6888 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
| 6889 | return NULL; |
| 6890 | if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6891 | { |
| 6892 | emsg(_(e_missing_in)); |
| 6893 | return NULL; |
| 6894 | } |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6895 | wp = p + 2; |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6896 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
| 6897 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6898 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6899 | scope = new_scope(cctx, FOR_SCOPE); |
| 6900 | if (scope == NULL) |
| 6901 | return NULL; |
| 6902 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6903 | // Reserve a variable to store the loop iteration counter and initialize it |
| 6904 | // to -1. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6905 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6906 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6907 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6908 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6909 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6910 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6911 | } |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6912 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6913 | |
| 6914 | // compile "expr", it remains on the stack until "endfor" |
| 6915 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6916 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6917 | { |
| 6918 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6919 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6920 | } |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6921 | arg_end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6922 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6923 | // Now that we know the type of "var", check that it is a list, now or at |
| 6924 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6925 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 6926 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6927 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6928 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6929 | return NULL; |
| 6930 | } |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6931 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6932 | 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] | 6933 | { |
| 6934 | if (var_count == 1) |
| 6935 | item_type = vartype->tt_member; |
| 6936 | else if (vartype->tt_member->tt_type == VAR_LIST |
| 6937 | && vartype->tt_member->tt_member->tt_type != VAR_ANY) |
| 6938 | item_type = vartype->tt_member->tt_member; |
| 6939 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6940 | |
| 6941 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6942 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6943 | generate_FOR(cctx, loop_lvar->lv_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6944 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6945 | arg = arg_start; |
| 6946 | if (var_count > 1) |
| 6947 | { |
| 6948 | generate_UNPACK(cctx, var_count, semicolon); |
| 6949 | arg = skipwhite(arg + 1); // skip white after '[' |
| 6950 | |
| 6951 | // the list item is replaced by a number of items |
| 6952 | if (ga_grow(stack, var_count - 1) == FAIL) |
| 6953 | { |
| 6954 | drop_scope(cctx); |
| 6955 | return NULL; |
| 6956 | } |
| 6957 | --stack->ga_len; |
| 6958 | for (idx = 0; idx < var_count; ++idx) |
| 6959 | { |
| 6960 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 6961 | (semicolon && idx == 0) ? vartype : item_type; |
| 6962 | ++stack->ga_len; |
| 6963 | } |
| 6964 | } |
| 6965 | |
| 6966 | for (idx = 0; idx < var_count; ++idx) |
| 6967 | { |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6968 | assign_dest_T dest = dest_local; |
| 6969 | int opt_flags = 0; |
| 6970 | int vimvaridx = -1; |
| 6971 | type_T *type = &t_any; |
| 6972 | |
| 6973 | p = skip_var_one(arg, FALSE); |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6974 | varlen = p - arg; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6975 | name = vim_strnsave(arg, varlen); |
| 6976 | if (name == NULL) |
| 6977 | goto failed; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6978 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6979 | // TODO: script var not supported? |
| 6980 | if (get_var_dest(name, &dest, CMD_for, &opt_flags, |
| 6981 | &vimvaridx, &type, cctx) == FAIL) |
| 6982 | goto failed; |
| 6983 | if (dest != dest_local) |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6984 | { |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6985 | if (generate_store_var(cctx, dest, opt_flags, vimvaridx, |
| 6986 | 0, 0, type, name) == FAIL) |
| 6987 | goto failed; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6988 | } |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6989 | else |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6990 | { |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 6991 | if (lookup_local(arg, varlen, NULL, cctx) == OK) |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6992 | { |
| 6993 | semsg(_(e_variable_already_declared), arg); |
| 6994 | goto failed; |
| 6995 | } |
| 6996 | |
Bram Moolenaar | ea87069 | 2020-12-02 14:24:30 +0100 | [diff] [blame] | 6997 | if (STRNCMP(name, "s:", 2) == 0) |
| 6998 | { |
| 6999 | semsg(_(e_cannot_declare_script_variable_in_function), name); |
| 7000 | goto failed; |
| 7001 | } |
| 7002 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 7003 | // Reserve a variable to store "var". |
| 7004 | // TODO: check for type |
| 7005 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 7006 | if (var_lvar == NULL) |
| 7007 | // out of memory or used as an argument |
| 7008 | goto failed; |
| 7009 | |
| 7010 | if (semicolon && idx == var_count - 1) |
| 7011 | var_lvar->lv_type = vartype; |
| 7012 | else |
| 7013 | var_lvar->lv_type = item_type; |
| 7014 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
| 7015 | } |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 7016 | |
| 7017 | if (*p == ',' || *p == ';') |
| 7018 | ++p; |
| 7019 | arg = skipwhite(p); |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 7020 | vim_free(name); |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 7021 | } |
| 7022 | |
| 7023 | return arg_end; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 7024 | |
| 7025 | failed: |
| 7026 | vim_free(name); |
| 7027 | drop_scope(cctx); |
| 7028 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7029 | } |
| 7030 | |
| 7031 | /* |
| 7032 | * compile "endfor" |
| 7033 | */ |
| 7034 | static char_u * |
| 7035 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 7036 | { |
| 7037 | garray_T *instr = &cctx->ctx_instr; |
| 7038 | scope_T *scope = cctx->ctx_scope; |
| 7039 | forscope_T *forscope; |
| 7040 | isn_T *isn; |
| 7041 | |
| 7042 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 7043 | { |
| 7044 | emsg(_(e_for)); |
| 7045 | return NULL; |
| 7046 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7047 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7048 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7049 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7050 | |
| 7051 | // At end of ":for" scope jump back to the FOR instruction. |
| 7052 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 7053 | |
| 7054 | // Fill in the "end" label in the FOR statement so it can jump here |
| 7055 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 7056 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 7057 | |
| 7058 | // Fill in the "end" label any BREAK statements |
| 7059 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 7060 | |
| 7061 | // Below the ":for" scope drop the "expr" list from the stack. |
| 7062 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 7063 | return NULL; |
| 7064 | |
| 7065 | vim_free(scope); |
| 7066 | |
| 7067 | return arg; |
| 7068 | } |
| 7069 | |
| 7070 | /* |
| 7071 | * compile "while expr" |
| 7072 | * |
| 7073 | * Produces instructions: |
| 7074 | * top: EVAL expr Push result of "expr" |
| 7075 | * JUMP_IF_FALSE end jump if false |
| 7076 | * ... body ... |
| 7077 | * JUMP top Jump back to repeat |
| 7078 | * end: |
| 7079 | * |
| 7080 | */ |
| 7081 | static char_u * |
| 7082 | compile_while(char_u *arg, cctx_T *cctx) |
| 7083 | { |
| 7084 | char_u *p = arg; |
| 7085 | garray_T *instr = &cctx->ctx_instr; |
| 7086 | scope_T *scope; |
| 7087 | |
| 7088 | scope = new_scope(cctx, WHILE_SCOPE); |
| 7089 | if (scope == NULL) |
| 7090 | return NULL; |
| 7091 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7092 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7093 | |
| 7094 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7095 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7096 | return NULL; |
| 7097 | |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 7098 | if (bool_on_stack(cctx) == FAIL) |
| 7099 | return FAIL; |
| 7100 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7101 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7102 | 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] | 7103 | JUMP_IF_FALSE, cctx) == FAIL) |
| 7104 | return FAIL; |
| 7105 | |
| 7106 | return p; |
| 7107 | } |
| 7108 | |
| 7109 | /* |
| 7110 | * compile "endwhile" |
| 7111 | */ |
| 7112 | static char_u * |
| 7113 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 7114 | { |
| 7115 | scope_T *scope = cctx->ctx_scope; |
| 7116 | |
| 7117 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 7118 | { |
| 7119 | emsg(_(e_while)); |
| 7120 | return NULL; |
| 7121 | } |
| 7122 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7123 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7124 | |
| 7125 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7126 | 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] | 7127 | |
| 7128 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 7129 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7130 | 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] | 7131 | |
| 7132 | vim_free(scope); |
| 7133 | |
| 7134 | return arg; |
| 7135 | } |
| 7136 | |
| 7137 | /* |
| 7138 | * compile "continue" |
| 7139 | */ |
| 7140 | static char_u * |
| 7141 | compile_continue(char_u *arg, cctx_T *cctx) |
| 7142 | { |
| 7143 | scope_T *scope = cctx->ctx_scope; |
| 7144 | |
| 7145 | for (;;) |
| 7146 | { |
| 7147 | if (scope == NULL) |
| 7148 | { |
| 7149 | emsg(_(e_continue)); |
| 7150 | return NULL; |
| 7151 | } |
| 7152 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 7153 | break; |
| 7154 | scope = scope->se_outer; |
| 7155 | } |
| 7156 | |
| 7157 | // Jump back to the FOR or WHILE instruction. |
| 7158 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7159 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 7160 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7161 | return arg; |
| 7162 | } |
| 7163 | |
| 7164 | /* |
| 7165 | * compile "break" |
| 7166 | */ |
| 7167 | static char_u * |
| 7168 | compile_break(char_u *arg, cctx_T *cctx) |
| 7169 | { |
| 7170 | scope_T *scope = cctx->ctx_scope; |
| 7171 | endlabel_T **el; |
| 7172 | |
| 7173 | for (;;) |
| 7174 | { |
| 7175 | if (scope == NULL) |
| 7176 | { |
| 7177 | emsg(_(e_break)); |
| 7178 | return NULL; |
| 7179 | } |
| 7180 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 7181 | break; |
| 7182 | scope = scope->se_outer; |
| 7183 | } |
| 7184 | |
| 7185 | // Jump to the end of the FOR or WHILE loop. |
| 7186 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7187 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7188 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7189 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7190 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 7191 | return FAIL; |
| 7192 | |
| 7193 | return arg; |
| 7194 | } |
| 7195 | |
| 7196 | /* |
| 7197 | * compile "{" start of block |
| 7198 | */ |
| 7199 | static char_u * |
| 7200 | compile_block(char_u *arg, cctx_T *cctx) |
| 7201 | { |
| 7202 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 7203 | return NULL; |
| 7204 | return skipwhite(arg + 1); |
| 7205 | } |
| 7206 | |
| 7207 | /* |
| 7208 | * compile end of block: drop one scope |
| 7209 | */ |
| 7210 | static void |
| 7211 | compile_endblock(cctx_T *cctx) |
| 7212 | { |
| 7213 | scope_T *scope = cctx->ctx_scope; |
| 7214 | |
| 7215 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7216 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7217 | vim_free(scope); |
| 7218 | } |
| 7219 | |
| 7220 | /* |
| 7221 | * compile "try" |
| 7222 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 7223 | * finally. |
| 7224 | * Creates another scope for the "try" block itself. |
| 7225 | * TRY instruction sets up exception handling at runtime. |
| 7226 | * |
| 7227 | * "try" |
| 7228 | * TRY -> catch1, -> finally push trystack entry |
| 7229 | * ... try block |
| 7230 | * "throw {exception}" |
| 7231 | * EVAL {exception} |
| 7232 | * THROW create exception |
| 7233 | * ... try block |
| 7234 | * " catch {expr}" |
| 7235 | * JUMP -> finally |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 7236 | * catch1: PUSH exception |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7237 | * EVAL {expr} |
| 7238 | * MATCH |
| 7239 | * JUMP nomatch -> catch2 |
| 7240 | * CATCH remove exception |
| 7241 | * ... catch block |
| 7242 | * " catch" |
| 7243 | * JUMP -> finally |
| 7244 | * catch2: CATCH remove exception |
| 7245 | * ... catch block |
| 7246 | * " finally" |
| 7247 | * finally: |
| 7248 | * ... finally block |
| 7249 | * " endtry" |
| 7250 | * ENDTRY pop trystack entry, may rethrow |
| 7251 | */ |
| 7252 | static char_u * |
| 7253 | compile_try(char_u *arg, cctx_T *cctx) |
| 7254 | { |
| 7255 | garray_T *instr = &cctx->ctx_instr; |
| 7256 | scope_T *try_scope; |
| 7257 | scope_T *scope; |
| 7258 | |
| 7259 | // scope that holds the jumps that go to catch/finally/endtry |
| 7260 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 7261 | if (try_scope == NULL) |
| 7262 | return NULL; |
| 7263 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7264 | if (cctx->ctx_skip != SKIP_YES) |
| 7265 | { |
| 7266 | // "catch" is set when the first ":catch" is found. |
| 7267 | // "finally" is set when ":finally" or ":endtry" is found |
| 7268 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
| 7269 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 7270 | return NULL; |
| 7271 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7272 | |
| 7273 | // scope for the try block itself |
| 7274 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 7275 | if (scope == NULL) |
| 7276 | return NULL; |
| 7277 | |
| 7278 | return arg; |
| 7279 | } |
| 7280 | |
| 7281 | /* |
| 7282 | * compile "catch {expr}" |
| 7283 | */ |
| 7284 | static char_u * |
| 7285 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 7286 | { |
| 7287 | scope_T *scope = cctx->ctx_scope; |
| 7288 | garray_T *instr = &cctx->ctx_instr; |
| 7289 | char_u *p; |
| 7290 | isn_T *isn; |
| 7291 | |
| 7292 | // end block scope from :try or :catch |
| 7293 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 7294 | compile_endblock(cctx); |
| 7295 | scope = cctx->ctx_scope; |
| 7296 | |
| 7297 | // Error if not in a :try scope |
| 7298 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 7299 | { |
| 7300 | emsg(_(e_catch)); |
| 7301 | return NULL; |
| 7302 | } |
| 7303 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7304 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7305 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7306 | emsg(_(e_catch_unreachable_after_catch_all)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7307 | return NULL; |
| 7308 | } |
| 7309 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7310 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7311 | { |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7312 | // Jump from end of previous block to :finally or :endtry |
| 7313 | if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label, |
| 7314 | JUMP_ALWAYS, cctx) == FAIL) |
| 7315 | return NULL; |
| 7316 | |
| 7317 | // End :try or :catch scope: set value in ISN_TRY instruction |
| 7318 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
| 7319 | if (isn->isn_arg.try.try_catch == 0) |
| 7320 | isn->isn_arg.try.try_catch = instr->ga_len; |
| 7321 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 7322 | { |
| 7323 | // Previous catch without match jumps here |
| 7324 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 7325 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 7326 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7327 | } |
| 7328 | |
| 7329 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7330 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7331 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7332 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 7333 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7334 | } |
| 7335 | else |
| 7336 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7337 | char_u *end; |
| 7338 | char_u *pat; |
| 7339 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 7340 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 7341 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7342 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7343 | // Push v:exception, push {expr} and MATCH |
| 7344 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 7345 | |
Bram Moolenaar | d93a7fc | 2021-01-04 12:42:13 +0100 | [diff] [blame] | 7346 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7347 | if (*end != *p) |
| 7348 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 7349 | semsg(_(e_separator_mismatch_str), p); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7350 | vim_free(tofree); |
| 7351 | return FAIL; |
| 7352 | } |
| 7353 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 7354 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7355 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 7356 | len = (int)(end - tofree); |
| 7357 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7358 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 7359 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7360 | if (pat == NULL) |
| 7361 | return FAIL; |
| 7362 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 7363 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7364 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7365 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 7366 | return NULL; |
| 7367 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7368 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7369 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 7370 | return NULL; |
| 7371 | } |
| 7372 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7373 | if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7374 | return NULL; |
| 7375 | |
| 7376 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 7377 | return NULL; |
| 7378 | return p; |
| 7379 | } |
| 7380 | |
| 7381 | static char_u * |
| 7382 | compile_finally(char_u *arg, cctx_T *cctx) |
| 7383 | { |
| 7384 | scope_T *scope = cctx->ctx_scope; |
| 7385 | garray_T *instr = &cctx->ctx_instr; |
| 7386 | isn_T *isn; |
| 7387 | |
| 7388 | // end block scope from :try or :catch |
| 7389 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 7390 | compile_endblock(cctx); |
| 7391 | scope = cctx->ctx_scope; |
| 7392 | |
| 7393 | // Error if not in a :try scope |
| 7394 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 7395 | { |
| 7396 | emsg(_(e_finally)); |
| 7397 | return NULL; |
| 7398 | } |
| 7399 | |
| 7400 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7401 | 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] | 7402 | if (isn->isn_arg.try.try_finally != 0) |
| 7403 | { |
| 7404 | emsg(_(e_finally_dup)); |
| 7405 | return NULL; |
| 7406 | } |
| 7407 | |
| 7408 | // 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] | 7409 | 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] | 7410 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7411 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7412 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7413 | { |
| 7414 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7415 | 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] | 7416 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 7417 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7418 | } |
| 7419 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7420 | // TODO: set index in ts_finally_label jumps |
| 7421 | |
| 7422 | return arg; |
| 7423 | } |
| 7424 | |
| 7425 | static char_u * |
| 7426 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 7427 | { |
| 7428 | scope_T *scope = cctx->ctx_scope; |
| 7429 | garray_T *instr = &cctx->ctx_instr; |
| 7430 | isn_T *isn; |
| 7431 | |
| 7432 | // end block scope from :catch or :finally |
| 7433 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 7434 | compile_endblock(cctx); |
| 7435 | scope = cctx->ctx_scope; |
| 7436 | |
| 7437 | // Error if not in a :try scope |
| 7438 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 7439 | { |
| 7440 | if (scope == NULL) |
| 7441 | emsg(_(e_no_endtry)); |
| 7442 | else if (scope->se_type == WHILE_SCOPE) |
| 7443 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 7444 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7445 | emsg(_(e_endfor)); |
| 7446 | else |
| 7447 | emsg(_(e_endif)); |
| 7448 | return NULL; |
| 7449 | } |
| 7450 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7451 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7452 | { |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7453 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
| 7454 | if (isn->isn_arg.try.try_catch == 0 |
| 7455 | && isn->isn_arg.try.try_finally == 0) |
| 7456 | { |
| 7457 | emsg(_(e_missing_catch_or_finally)); |
| 7458 | return NULL; |
| 7459 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7460 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7461 | // Fill in the "end" label in jumps at the end of the blocks, if not |
| 7462 | // done by ":finally". |
| 7463 | 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] | 7464 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7465 | // End :catch or :finally scope: set value in ISN_TRY instruction |
| 7466 | if (isn->isn_arg.try.try_catch == 0) |
| 7467 | isn->isn_arg.try.try_catch = instr->ga_len; |
| 7468 | if (isn->isn_arg.try.try_finally == 0) |
| 7469 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 7470 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7471 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 7472 | { |
| 7473 | // Last catch without match jumps here |
| 7474 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 7475 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 7476 | } |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 7477 | } |
| 7478 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7479 | compile_endblock(cctx); |
| 7480 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7481 | if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7482 | return NULL; |
| 7483 | return arg; |
| 7484 | } |
| 7485 | |
| 7486 | /* |
| 7487 | * compile "throw {expr}" |
| 7488 | */ |
| 7489 | static char_u * |
| 7490 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 7491 | { |
| 7492 | char_u *p = skipwhite(arg); |
| 7493 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7494 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7495 | return NULL; |
Bram Moolenaar | 9e1d9e3 | 2021-01-11 20:17:34 +0100 | [diff] [blame] | 7496 | if (cctx->ctx_skip == SKIP_YES) |
| 7497 | return p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7498 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 7499 | return NULL; |
| 7500 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 7501 | return NULL; |
| 7502 | |
| 7503 | return p; |
| 7504 | } |
| 7505 | |
| 7506 | /* |
| 7507 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7508 | * compile "echomsg expr" |
| 7509 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7510 | * compile "execute expr" |
| 7511 | */ |
| 7512 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7513 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7514 | { |
| 7515 | char_u *p = arg; |
Bram Moolenaar | e498429 | 2020-12-13 14:19:25 +0100 | [diff] [blame] | 7516 | char_u *prev = arg; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7517 | int count = 0; |
| 7518 | |
| 7519 | for (;;) |
| 7520 | { |
Bram Moolenaar | e498429 | 2020-12-13 14:19:25 +0100 | [diff] [blame] | 7521 | if (ends_excmd2(prev, p)) |
| 7522 | break; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7523 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7524 | return NULL; |
| 7525 | ++count; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 7526 | prev = p; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7527 | p = skipwhite(p); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7528 | } |
| 7529 | |
Bram Moolenaar | e498429 | 2020-12-13 14:19:25 +0100 | [diff] [blame] | 7530 | if (count > 0) |
| 7531 | { |
| 7532 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 7533 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 7534 | else if (cmdidx == CMD_execute) |
| 7535 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 7536 | else if (cmdidx == CMD_echomsg) |
| 7537 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 7538 | else |
| 7539 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
| 7540 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7541 | return p; |
| 7542 | } |
| 7543 | |
| 7544 | /* |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 7545 | * 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] | 7546 | * instruction to compute it and return OK. |
| 7547 | * Otherwise return FAIL, the caller must deal with any range. |
| 7548 | */ |
| 7549 | static int |
| 7550 | compile_variable_range(exarg_T *eap, cctx_T *cctx) |
| 7551 | { |
| 7552 | char_u *range_end = skip_range(eap->cmd, TRUE, NULL); |
| 7553 | char_u *p = skipdigits(eap->cmd); |
| 7554 | |
| 7555 | if (p == range_end) |
| 7556 | return FAIL; |
| 7557 | return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd)); |
| 7558 | } |
| 7559 | |
| 7560 | /* |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7561 | * :put r |
| 7562 | * :put ={expr} |
| 7563 | */ |
| 7564 | static char_u * |
| 7565 | compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 7566 | { |
| 7567 | char_u *line = arg; |
| 7568 | linenr_T lnum; |
| 7569 | char *errormsg; |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 7570 | int above = eap->forceit; |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7571 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7572 | eap->regname = *line; |
| 7573 | |
| 7574 | if (eap->regname == '=') |
| 7575 | { |
| 7576 | char_u *p = line + 1; |
| 7577 | |
| 7578 | if (compile_expr0(&p, cctx) == FAIL) |
| 7579 | return NULL; |
| 7580 | line = p; |
| 7581 | } |
| 7582 | else if (eap->regname != NUL) |
| 7583 | ++line; |
| 7584 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 7585 | if (compile_variable_range(eap, cctx) == OK) |
| 7586 | { |
| 7587 | lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE; |
| 7588 | } |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7589 | else |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 7590 | { |
| 7591 | // Either no range or a number. |
| 7592 | // "errormsg" will not be set because the range is ADDR_LINES. |
| 7593 | if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL) |
Bram Moolenaar | 399ea81 | 2020-12-15 21:28:57 +0100 | [diff] [blame] | 7594 | // cannot happen |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 7595 | return NULL; |
| 7596 | if (eap->addr_count == 0) |
| 7597 | lnum = -1; |
| 7598 | else |
| 7599 | lnum = eap->line2; |
| 7600 | if (above) |
| 7601 | --lnum; |
| 7602 | } |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7603 | |
| 7604 | generate_PUT(cctx, eap->regname, lnum); |
| 7605 | return line; |
| 7606 | } |
| 7607 | |
| 7608 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7609 | * A command that is not compiled, execute with legacy code. |
| 7610 | */ |
| 7611 | static char_u * |
| 7612 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 7613 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 7614 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 7615 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7616 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7617 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7618 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7619 | goto theend; |
| 7620 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 7621 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7622 | { |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 7623 | long argt = eap->argt; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7624 | int usefilter = FALSE; |
| 7625 | |
| 7626 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 7627 | |
| 7628 | // If the command can be followed by a bar, find the bar and truncate |
| 7629 | // it, so that the following command can be compiled. |
| 7630 | // The '|' is overwritten with a NUL, it is put back below. |
| 7631 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 7632 | && *eap->arg == '!') |
| 7633 | // :w !filter or :r !filter or :r! filter |
| 7634 | usefilter = TRUE; |
| 7635 | if ((argt & EX_TRLBAR) && !usefilter) |
| 7636 | { |
Bram Moolenaar | b8a9296 | 2020-08-20 18:02:47 +0200 | [diff] [blame] | 7637 | eap->argt = argt; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7638 | separate_nextcmd(eap); |
| 7639 | if (eap->nextcmd != NULL) |
| 7640 | nextcmd = eap->nextcmd; |
| 7641 | } |
Bram Moolenaar | a11919f | 2021-01-02 19:44:56 +0100 | [diff] [blame] | 7642 | else if (eap->cmdidx == CMD_wincmd) |
| 7643 | { |
| 7644 | p = eap->arg; |
| 7645 | if (*p != NUL) |
| 7646 | ++p; |
| 7647 | if (*p == 'g' || *p == Ctrl_G) |
| 7648 | ++p; |
| 7649 | p = skipwhite(p); |
| 7650 | if (*p == '|') |
| 7651 | { |
| 7652 | *p = NUL; |
| 7653 | nextcmd = p + 1; |
| 7654 | } |
| 7655 | } |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7656 | } |
| 7657 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 7658 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 7659 | { |
| 7660 | // expand filename in "syntax include [@group] filename" |
| 7661 | has_expr = TRUE; |
| 7662 | eap->arg = skipwhite(eap->arg + 7); |
| 7663 | if (*eap->arg == '@') |
| 7664 | eap->arg = skiptowhite(eap->arg); |
| 7665 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7666 | |
Bram Moolenaar | 56ce9ea | 2020-12-25 18:35:29 +0100 | [diff] [blame] | 7667 | if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal) |
| 7668 | && STRLEN(eap->arg) > 4) |
| 7669 | { |
| 7670 | int delim = *eap->arg; |
| 7671 | |
Bram Moolenaar | d93a7fc | 2021-01-04 12:42:13 +0100 | [diff] [blame] | 7672 | p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL); |
Bram Moolenaar | 56ce9ea | 2020-12-25 18:35:29 +0100 | [diff] [blame] | 7673 | if (*p == delim) |
| 7674 | { |
| 7675 | eap->arg = p + 1; |
| 7676 | has_expr = TRUE; |
| 7677 | } |
| 7678 | } |
| 7679 | |
Bram Moolenaar | ecac591 | 2021-01-05 19:23:28 +0100 | [diff] [blame] | 7680 | if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed) |
| 7681 | { |
| 7682 | // TODO: should only expand when appropriate for the command |
| 7683 | eap->arg = skiptowhite(eap->arg); |
| 7684 | has_expr = TRUE; |
| 7685 | } |
| 7686 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 7687 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7688 | { |
| 7689 | int count = 0; |
| 7690 | char_u *start = skipwhite(line); |
| 7691 | |
| 7692 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 7693 | // PUSHS ":cmd xxx" |
| 7694 | // eval expr1 |
| 7695 | // PUSHS "yyy" |
| 7696 | // eval expr2 |
| 7697 | // PUSHS "zzz" |
| 7698 | // EXECCONCAT 5 |
| 7699 | for (;;) |
| 7700 | { |
| 7701 | if (p > start) |
| 7702 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 7703 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7704 | ++count; |
| 7705 | } |
| 7706 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7707 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7708 | return NULL; |
| 7709 | may_generate_2STRING(-1, cctx); |
| 7710 | ++count; |
| 7711 | p = skipwhite(p); |
| 7712 | if (*p != '`') |
| 7713 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7714 | emsg(_(e_missing_backtick)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7715 | return NULL; |
| 7716 | } |
| 7717 | start = p + 1; |
| 7718 | |
| 7719 | p = (char_u *)strstr((char *)start, "`="); |
| 7720 | if (p == NULL) |
| 7721 | { |
| 7722 | if (*skipwhite(start) != NUL) |
| 7723 | { |
| 7724 | generate_PUSHS(cctx, vim_strsave(start)); |
| 7725 | ++count; |
| 7726 | } |
| 7727 | break; |
| 7728 | } |
| 7729 | } |
| 7730 | generate_EXECCONCAT(cctx, count); |
| 7731 | } |
| 7732 | else |
| 7733 | generate_EXEC(cctx, line); |
| 7734 | |
| 7735 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7736 | if (*nextcmd != NUL) |
| 7737 | { |
| 7738 | // the parser expects a pointer to the bar, put it back |
| 7739 | --nextcmd; |
| 7740 | *nextcmd = '|'; |
| 7741 | } |
| 7742 | |
| 7743 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7744 | } |
| 7745 | |
| 7746 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7747 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7748 | * 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] | 7749 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7750 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7751 | add_def_function(ufunc_T *ufunc) |
| 7752 | { |
| 7753 | dfunc_T *dfunc; |
| 7754 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7755 | if (def_functions.ga_len == 0) |
| 7756 | { |
| 7757 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 7758 | // wasn't set. |
| 7759 | if (ga_grow(&def_functions, 1) == FAIL) |
| 7760 | return FAIL; |
| 7761 | ++def_functions.ga_len; |
| 7762 | } |
| 7763 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7764 | // Add the function to "def_functions". |
| 7765 | if (ga_grow(&def_functions, 1) == FAIL) |
| 7766 | return FAIL; |
| 7767 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 7768 | CLEAR_POINTER(dfunc); |
| 7769 | dfunc->df_idx = def_functions.ga_len; |
| 7770 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 7771 | dfunc->df_ufunc = ufunc; |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 7772 | dfunc->df_name = vim_strsave(ufunc->uf_name); |
| 7773 | ++dfunc->df_refcount; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7774 | ++def_functions.ga_len; |
| 7775 | return OK; |
| 7776 | } |
| 7777 | |
| 7778 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7779 | * After ex_function() has collected all the function lines: parse and compile |
| 7780 | * the lines into instructions. |
| 7781 | * Adds the function to "def_functions". |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 7782 | * When "check_return_type" is set then set ufunc->uf_ret_type to the type of |
| 7783 | * the return statement (used for lambda). When uf_ret_type is already set |
| 7784 | * then check that it matches. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7785 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7786 | * This can be used recursively through compile_lambda(), which may reallocate |
| 7787 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7788 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7789 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7790 | int |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 7791 | 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] | 7792 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7793 | char_u *line = NULL; |
| 7794 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7795 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7796 | cctx_T cctx; |
| 7797 | garray_T *instr; |
Bram Moolenaar | d66960b | 2020-10-30 20:46:26 +0100 | [diff] [blame] | 7798 | int did_emsg_before = did_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7799 | int ret = FAIL; |
| 7800 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | f4e8cdd | 2020-10-12 22:07:13 +0200 | [diff] [blame] | 7801 | int save_estack_compiling = estack_compiling; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7802 | int do_estack_push; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7803 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7804 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7805 | // When using a function that was compiled before: Free old instructions. |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 7806 | // The index is reused. Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7807 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7808 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7809 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7810 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 7811 | delete_def_function_contents(dfunc, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7812 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7813 | else |
| 7814 | { |
| 7815 | if (add_def_function(ufunc) == FAIL) |
| 7816 | return FAIL; |
| 7817 | new_def_function = TRUE; |
| 7818 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7819 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 7820 | ufunc->uf_def_status = UF_COMPILING; |
| 7821 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7822 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7823 | cctx.ctx_ufunc = ufunc; |
| 7824 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7825 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7826 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 7827 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 7828 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 7829 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 7830 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 7831 | instr = &cctx.ctx_instr; |
| 7832 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7833 | // Set the context to the function, it may be compiled when called from |
| 7834 | // another script. Set the script version to the most modern one. |
| 7835 | // The line number will be set in next_line_from_context(). |
| 7836 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7837 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 7838 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7839 | // Make sure error messages are OK. |
| 7840 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 7841 | if (do_estack_push) |
| 7842 | estack_push_ufunc(ufunc, 1); |
Bram Moolenaar | f4e8cdd | 2020-10-12 22:07:13 +0200 | [diff] [blame] | 7843 | estack_compiling = TRUE; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7844 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7845 | if (ufunc->uf_def_args.ga_len > 0) |
| 7846 | { |
| 7847 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7848 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7849 | int i; |
| 7850 | char_u *arg; |
| 7851 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7852 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7853 | |
| 7854 | // Produce instructions for the default values of optional arguments. |
| 7855 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 7856 | // where to start when the function is called, depending on the number |
| 7857 | // of arguments. |
| 7858 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 7859 | if (ufunc->uf_def_arg_idx == NULL) |
| 7860 | goto erret; |
| 7861 | for (i = 0; i < count; ++i) |
| 7862 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7863 | garray_T *stack = &cctx.ctx_type_stack; |
| 7864 | type_T *val_type; |
| 7865 | int arg_idx = first_def_arg + i; |
| 7866 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7867 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 7868 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7869 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7870 | goto erret; |
| 7871 | |
| 7872 | // If no type specified use the type of the default value. |
| 7873 | // Otherwise check that the default value type matches the |
| 7874 | // specified type. |
| 7875 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 7876 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7877 | { |
| 7878 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7879 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7880 | } |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 7881 | else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, |
| 7882 | TRUE, arg_idx + 1) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7883 | goto erret; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7884 | |
| 7885 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7886 | goto erret; |
| 7887 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7888 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7889 | |
| 7890 | if (did_set_arg_type) |
| 7891 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7892 | } |
| 7893 | |
| 7894 | /* |
| 7895 | * Loop over all the lines of the function and generate instructions. |
| 7896 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7897 | for (;;) |
| 7898 | { |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 7899 | exarg_T ea; |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 7900 | int starts_with_colon = FALSE; |
| 7901 | char_u *cmd; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 7902 | cmdmod_T local_cmdmod; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7903 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7904 | // Bail out on the first error to avoid a flood of errors and report |
| 7905 | // the right line number when inside try/catch. |
Bram Moolenaar | d66960b | 2020-10-30 20:46:26 +0100 | [diff] [blame] | 7906 | if (did_emsg_before != did_emsg) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7907 | goto erret; |
| 7908 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7909 | if (line != NULL && *line == '|') |
| 7910 | // the line continues after a '|' |
| 7911 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 7912 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7913 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 7914 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7915 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 7916 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7917 | goto erret; |
| 7918 | } |
| 7919 | else |
| 7920 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 7921 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 7922 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7923 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7924 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7925 | } |
| 7926 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7927 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7928 | ea.cmdlinep = &line; |
| 7929 | ea.cmd = skipwhite(line); |
| 7930 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7931 | // Some things can be recognized by the first character. |
| 7932 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7933 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7934 | case '#': |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 7935 | // "#" starts a comment |
| 7936 | line = (char_u *)""; |
| 7937 | continue; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7938 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7939 | case '}': |
| 7940 | { |
| 7941 | // "}" ends a block scope |
| 7942 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7943 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7944 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7945 | if (stype == BLOCK_SCOPE) |
| 7946 | { |
| 7947 | compile_endblock(&cctx); |
| 7948 | line = ea.cmd; |
| 7949 | } |
| 7950 | else |
| 7951 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7952 | emsg(_(e_using_rcurly_outside_if_block_scope)); |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7953 | goto erret; |
| 7954 | } |
| 7955 | if (line != NULL) |
| 7956 | line = skipwhite(ea.cmd + 1); |
| 7957 | continue; |
| 7958 | } |
| 7959 | |
| 7960 | case '{': |
| 7961 | // "{" starts a block scope |
| 7962 | // "{'a': 1}->func() is something else |
| 7963 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7964 | { |
| 7965 | line = compile_block(ea.cmd, &cctx); |
| 7966 | continue; |
| 7967 | } |
| 7968 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7969 | } |
| 7970 | |
| 7971 | /* |
| 7972 | * COMMAND MODIFIERS |
| 7973 | */ |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 7974 | cctx.ctx_has_cmdmod = FALSE; |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 7975 | if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE) |
| 7976 | == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7977 | { |
| 7978 | if (errormsg != NULL) |
| 7979 | goto erret; |
| 7980 | // empty line or comment |
| 7981 | line = (char_u *)""; |
| 7982 | continue; |
| 7983 | } |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 7984 | generate_cmdmods(&cctx, &local_cmdmod); |
| 7985 | undo_cmdmod(&local_cmdmod); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7986 | |
Bram Moolenaar | e88c8e8 | 2020-11-01 17:03:37 +0100 | [diff] [blame] | 7987 | // Check if there was a colon after the last command modifier or before |
| 7988 | // the current position. |
| 7989 | for (p = ea.cmd; p >= line; --p) |
| 7990 | { |
| 7991 | if (*p == ':') |
| 7992 | starts_with_colon = TRUE; |
| 7993 | if (p < ea.cmd && !VIM_ISWHITE(*p)) |
| 7994 | break; |
| 7995 | } |
| 7996 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7997 | // Skip ":call" to get to the function name. |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 7998 | p = ea.cmd; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7999 | if (checkforcmd(&ea.cmd, "call", 3)) |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 8000 | { |
| 8001 | if (*ea.cmd == '(') |
| 8002 | // not for "call()" |
| 8003 | ea.cmd = p; |
| 8004 | else |
| 8005 | ea.cmd = skipwhite(ea.cmd); |
| 8006 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8007 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 8008 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8009 | { |
Bram Moolenaar | 17126b1 | 2021-01-07 22:03:02 +0100 | [diff] [blame] | 8010 | int assign; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 8011 | |
Bram Moolenaar | 17126b1 | 2021-01-07 22:03:02 +0100 | [diff] [blame] | 8012 | // Check for assignment after command modifiers. |
| 8013 | assign = may_compile_assignment(&ea, &line, &cctx); |
| 8014 | if (assign == OK) |
| 8015 | goto nextline; |
| 8016 | if (assign == FAIL) |
| 8017 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8018 | } |
| 8019 | |
| 8020 | /* |
| 8021 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 8022 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8023 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 8024 | cmd = ea.cmd; |
Bram Moolenaar | 7c5ad34 | 2020-08-12 15:48:55 +0200 | [diff] [blame] | 8025 | if (*cmd != '\'' || starts_with_colon) |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 8026 | { |
Bram Moolenaar | 3bd8de4 | 2020-09-14 16:37:34 +0200 | [diff] [blame] | 8027 | ea.cmd = skip_range(ea.cmd, TRUE, NULL); |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 8028 | if (ea.cmd > cmd) |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 8029 | { |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 8030 | if (!starts_with_colon) |
| 8031 | { |
Bram Moolenaar | 6e2c2c5 | 2020-12-25 19:25:45 +0100 | [diff] [blame] | 8032 | semsg(_(e_colon_required_before_range_str), cmd); |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 8033 | goto erret; |
| 8034 | } |
| 8035 | if (ends_excmd2(line, ea.cmd)) |
| 8036 | { |
| 8037 | // A range without a command: jump to the line. |
| 8038 | // TODO: compile to a more efficient command, possibly |
| 8039 | // calling parse_cmd_address(). |
| 8040 | ea.cmdidx = CMD_SIZE; |
| 8041 | line = compile_exec(line, &ea, &cctx); |
| 8042 | goto nextline; |
| 8043 | } |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 8044 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 8045 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 8046 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 8047 | : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 8048 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8049 | |
Bram Moolenaar | d1510ee | 2021-01-04 16:15:58 +0100 | [diff] [blame] | 8050 | if (p == NULL) |
| 8051 | { |
| 8052 | if (cctx.ctx_skip != SKIP_YES) |
| 8053 | emsg(_(e_ambiguous_use_of_user_defined_command)); |
| 8054 | goto erret; |
| 8055 | } |
| 8056 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8057 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 8058 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 8059 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 8060 | { |
| 8061 | line += STRLEN(line); |
Bram Moolenaar | f665e97 | 2020-12-05 19:17:16 +0100 | [diff] [blame] | 8062 | goto nextline; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 8063 | } |
| 8064 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8065 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 8066 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8067 | { |
Bram Moolenaar | 52c124d | 2020-12-20 21:43:35 +0100 | [diff] [blame] | 8068 | // CMD_var cannot happen, compile_assignment() above would be |
| 8069 | // used. Most likely an assignment to a non-existing variable. |
| 8070 | semsg(_(e_command_not_recognized_str), ea.cmd); |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 8071 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8072 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8073 | } |
| 8074 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 8075 | if (cctx.ctx_had_return |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 8076 | && ea.cmdidx != CMD_elseif |
| 8077 | && ea.cmdidx != CMD_else |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8078 | && ea.cmdidx != CMD_endif |
| 8079 | && ea.cmdidx != CMD_endfor |
| 8080 | && ea.cmdidx != CMD_endwhile |
| 8081 | && ea.cmdidx != CMD_catch |
| 8082 | && ea.cmdidx != CMD_finally |
| 8083 | && ea.cmdidx != CMD_endtry) |
| 8084 | { |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 8085 | emsg(_(e_unreachable_code_after_return)); |
| 8086 | goto erret; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8087 | } |
| 8088 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 8089 | p = skipwhite(p); |
| 8090 | if (ea.cmdidx != CMD_SIZE |
| 8091 | && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read) |
| 8092 | { |
Bram Moolenaar | 9b123d8 | 2020-09-14 22:39:11 +0200 | [diff] [blame] | 8093 | if (ea.cmdidx >= 0) |
| 8094 | ea.argt = excmd_get_argt(ea.cmdidx); |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 8095 | if ((ea.argt & EX_BANG) && *p == '!') |
| 8096 | { |
| 8097 | ea.forceit = TRUE; |
| 8098 | p = skipwhite(p + 1); |
| 8099 | } |
| 8100 | } |
| 8101 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8102 | switch (ea.cmdidx) |
| 8103 | { |
| 8104 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 8105 | ea.arg = p; |
| 8106 | line = compile_nested_function(&ea, &cctx); |
| 8107 | break; |
| 8108 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8109 | case CMD_function: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 8110 | // TODO: should we allow this, e.g. to declare a global |
| 8111 | // function? |
| 8112 | emsg(_(e_cannot_use_function_inside_def)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8113 | goto erret; |
| 8114 | |
| 8115 | case CMD_return: |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 8116 | line = compile_return(p, check_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8117 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8118 | break; |
| 8119 | |
| 8120 | case CMD_let: |
Bram Moolenaar | c58f545 | 2020-10-21 20:58:52 +0200 | [diff] [blame] | 8121 | emsg(_(e_cannot_use_let_in_vim9_script)); |
| 8122 | break; |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 8123 | case CMD_var: |
| 8124 | case CMD_final: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8125 | case CMD_const: |
| 8126 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 8127 | if (line == p) |
| 8128 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8129 | break; |
| 8130 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 8131 | case CMD_unlet: |
| 8132 | case CMD_unlockvar: |
| 8133 | case CMD_lockvar: |
| 8134 | line = compile_unletlock(p, &ea, &cctx); |
| 8135 | break; |
| 8136 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8137 | case CMD_import: |
| 8138 | line = compile_import(p, &cctx); |
| 8139 | break; |
| 8140 | |
| 8141 | case CMD_if: |
| 8142 | line = compile_if(p, &cctx); |
| 8143 | break; |
| 8144 | case CMD_elseif: |
| 8145 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8146 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8147 | break; |
| 8148 | case CMD_else: |
| 8149 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8150 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8151 | break; |
| 8152 | case CMD_endif: |
| 8153 | line = compile_endif(p, &cctx); |
| 8154 | break; |
| 8155 | |
| 8156 | case CMD_while: |
| 8157 | line = compile_while(p, &cctx); |
| 8158 | break; |
| 8159 | case CMD_endwhile: |
| 8160 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8161 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8162 | break; |
| 8163 | |
| 8164 | case CMD_for: |
| 8165 | line = compile_for(p, &cctx); |
| 8166 | break; |
| 8167 | case CMD_endfor: |
| 8168 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8169 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8170 | break; |
| 8171 | case CMD_continue: |
| 8172 | line = compile_continue(p, &cctx); |
| 8173 | break; |
| 8174 | case CMD_break: |
| 8175 | line = compile_break(p, &cctx); |
| 8176 | break; |
| 8177 | |
| 8178 | case CMD_try: |
| 8179 | line = compile_try(p, &cctx); |
| 8180 | break; |
| 8181 | case CMD_catch: |
| 8182 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8183 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8184 | break; |
| 8185 | case CMD_finally: |
| 8186 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8187 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8188 | break; |
| 8189 | case CMD_endtry: |
| 8190 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8191 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8192 | break; |
| 8193 | case CMD_throw: |
| 8194 | line = compile_throw(p, &cctx); |
| 8195 | break; |
| 8196 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 8197 | case CMD_eval: |
| 8198 | if (compile_expr0(&p, &cctx) == FAIL) |
| 8199 | goto erret; |
| 8200 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 8201 | // drop the result |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 8202 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 8203 | |
| 8204 | line = skipwhite(p); |
| 8205 | break; |
| 8206 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8207 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8208 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 8209 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 8210 | case CMD_echomsg: |
| 8211 | case CMD_echoerr: |
| 8212 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 8213 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8214 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 8215 | case CMD_put: |
| 8216 | ea.cmd = cmd; |
| 8217 | line = compile_put(p, &ea, &cctx); |
| 8218 | break; |
| 8219 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 8220 | // TODO: any other commands with an expression argument? |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 8221 | |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 8222 | case CMD_append: |
| 8223 | case CMD_change: |
| 8224 | case CMD_insert: |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 8225 | case CMD_t: |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 8226 | case CMD_xit: |
| 8227 | not_in_vim9(&ea); |
| 8228 | goto erret; |
| 8229 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 8230 | case CMD_SIZE: |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 8231 | if (cctx.ctx_skip != SKIP_YES) |
| 8232 | { |
| 8233 | semsg(_(e_invalid_command_str), ea.cmd); |
| 8234 | goto erret; |
| 8235 | } |
| 8236 | // We don't check for a next command here. |
| 8237 | line = (char_u *)""; |
| 8238 | break; |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 8239 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8240 | default: |
Bram Moolenaar | 5163fcc | 2020-08-27 23:37:09 +0200 | [diff] [blame] | 8241 | if (cctx.ctx_skip == SKIP_YES) |
| 8242 | { |
| 8243 | // We don't check for a next command here. |
| 8244 | line = (char_u *)""; |
| 8245 | } |
| 8246 | else |
| 8247 | { |
| 8248 | // Not recognized, execute with do_cmdline_cmd(). |
| 8249 | ea.arg = p; |
| 8250 | line = compile_exec(line, &ea, &cctx); |
| 8251 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8252 | break; |
| 8253 | } |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 8254 | nextline: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8255 | if (line == NULL) |
| 8256 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 8257 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8258 | |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 8259 | // Undo any command modifiers. |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 8260 | generate_undo_cmdmods(&cctx); |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 8261 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8262 | if (cctx.ctx_type_stack.ga_len < 0) |
| 8263 | { |
| 8264 | iemsg("Type stack underflow"); |
| 8265 | goto erret; |
| 8266 | } |
| 8267 | } |
| 8268 | |
| 8269 | if (cctx.ctx_scope != NULL) |
| 8270 | { |
| 8271 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 8272 | emsg(_(e_endif)); |
| 8273 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 8274 | emsg(_(e_endwhile)); |
| 8275 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 8276 | emsg(_(e_endfor)); |
| 8277 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 8278 | emsg(_(e_missing_rcurly)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8279 | goto erret; |
| 8280 | } |
| 8281 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8282 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8283 | { |
| 8284 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 8285 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 8286 | emsg(_(e_missing_return_statement)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8287 | goto erret; |
| 8288 | } |
| 8289 | |
| 8290 | // Return zero if there is no return at the end. |
Bram Moolenaar | 299f303 | 2021-01-08 20:53:09 +0100 | [diff] [blame] | 8291 | generate_instr(&cctx, ISN_RETURN_ZERO); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8292 | } |
| 8293 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 8294 | { |
| 8295 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8296 | + ufunc->uf_dfunc_idx; |
| 8297 | dfunc->df_deleted = FALSE; |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 8298 | dfunc->df_script_seq = current_sctx.sc_seq; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 8299 | dfunc->df_instr = instr->ga_data; |
| 8300 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 8301 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 8302 | dfunc->df_has_closure = cctx.ctx_has_closure; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 8303 | if (cctx.ctx_outer_used) |
| 8304 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8305 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 8306 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8307 | |
| 8308 | ret = OK; |
| 8309 | |
| 8310 | erret: |
| 8311 | if (ret == FAIL) |
| 8312 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8313 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 8314 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8315 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8316 | |
| 8317 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 8318 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8319 | ga_clear(instr); |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8320 | VIM_CLEAR(dfunc->df_name); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8321 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 8322 | // If using the last entry in the table and it was added above, we |
| 8323 | // might as well remove it. |
| 8324 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 8325 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 8326 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8327 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 8328 | ufunc->uf_dfunc_idx = 0; |
| 8329 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8330 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8331 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 8332 | while (cctx.ctx_scope != NULL) |
| 8333 | drop_scope(&cctx); |
| 8334 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8335 | // Don't execute this function body. |
| 8336 | ga_clear_strings(&ufunc->uf_lines); |
| 8337 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8338 | if (errormsg != NULL) |
| 8339 | emsg(errormsg); |
Bram Moolenaar | d66960b | 2020-10-30 20:46:26 +0100 | [diff] [blame] | 8340 | else if (did_emsg == did_emsg_before) |
Bram Moolenaar | e13bdec | 2020-10-16 23:16:47 +0200 | [diff] [blame] | 8341 | emsg(_(e_compiling_def_function_failed)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8342 | } |
| 8343 | |
| 8344 | current_sctx = save_current_sctx; |
Bram Moolenaar | f4e8cdd | 2020-10-12 22:07:13 +0200 | [diff] [blame] | 8345 | estack_compiling = save_estack_compiling; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 8346 | if (do_estack_push) |
| 8347 | estack_pop(); |
| 8348 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8349 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 8350 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8351 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 8352 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8353 | } |
| 8354 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 8355 | void |
| 8356 | set_function_type(ufunc_T *ufunc) |
| 8357 | { |
| 8358 | int varargs = ufunc->uf_va_name != NULL; |
| 8359 | int argcount = ufunc->uf_args.ga_len; |
| 8360 | |
| 8361 | // Create a type for the function, with the return type and any |
| 8362 | // argument types. |
| 8363 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 8364 | // The type is included in "tt_args". |
| 8365 | if (argcount > 0 || varargs) |
| 8366 | { |
| 8367 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 8368 | argcount, &ufunc->uf_type_list); |
| 8369 | // Add argument types to the function type. |
| 8370 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 8371 | argcount + varargs, |
| 8372 | &ufunc->uf_type_list) == FAIL) |
| 8373 | return; |
| 8374 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 8375 | ufunc->uf_func_type->tt_min_argcount = |
| 8376 | argcount - ufunc->uf_def_args.ga_len; |
| 8377 | if (ufunc->uf_arg_types == NULL) |
| 8378 | { |
| 8379 | int i; |
| 8380 | |
| 8381 | // lambda does not have argument types. |
| 8382 | for (i = 0; i < argcount; ++i) |
| 8383 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 8384 | } |
| 8385 | else |
| 8386 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 8387 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 8388 | if (varargs) |
| 8389 | { |
| 8390 | ufunc->uf_func_type->tt_args[argcount] = |
| 8391 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 8392 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 8393 | } |
| 8394 | } |
| 8395 | else |
| 8396 | // No arguments, can use a predefined type. |
| 8397 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 8398 | argcount, &ufunc->uf_type_list); |
| 8399 | } |
| 8400 | |
| 8401 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8402 | /* |
| 8403 | * Delete an instruction, free what it contains. |
| 8404 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8405 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8406 | delete_instr(isn_T *isn) |
| 8407 | { |
| 8408 | switch (isn->isn_type) |
| 8409 | { |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8410 | case ISN_DEF: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8411 | case ISN_EXEC: |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 8412 | case ISN_LOADAUTO: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8413 | case ISN_LOADB: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8414 | case ISN_LOADENV: |
| 8415 | case ISN_LOADG: |
| 8416 | case ISN_LOADOPT: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8417 | case ISN_LOADT: |
| 8418 | case ISN_LOADW: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8419 | case ISN_PUSHEXC: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8420 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8421 | case ISN_PUSHS: |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 8422 | case ISN_RANGE: |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 8423 | case ISN_STOREAUTO: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8424 | case ISN_STOREB: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 8425 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8426 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 8427 | case ISN_STORET: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8428 | case ISN_STOREW: |
| 8429 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8430 | vim_free(isn->isn_arg.string); |
| 8431 | break; |
| 8432 | |
| 8433 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 8434 | case ISN_STORES: |
| 8435 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8436 | break; |
| 8437 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 8438 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 8439 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 8440 | vim_free(isn->isn_arg.unlet.ul_name); |
| 8441 | break; |
| 8442 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8443 | case ISN_STOREOPT: |
| 8444 | vim_free(isn->isn_arg.storeopt.so_name); |
| 8445 | break; |
| 8446 | |
| 8447 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 8448 | blob_unref(isn->isn_arg.blob); |
| 8449 | break; |
| 8450 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8451 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 8452 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8453 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 8454 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8455 | break; |
| 8456 | |
| 8457 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 8458 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8459 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 8460 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8461 | break; |
| 8462 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8463 | case ISN_UCALL: |
| 8464 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 8465 | break; |
| 8466 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 8467 | case ISN_FUNCREF: |
| 8468 | { |
| 8469 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8470 | + isn->isn_arg.funcref.fr_func; |
Bram Moolenaar | 077a423 | 2020-12-22 18:33:27 +0100 | [diff] [blame] | 8471 | ufunc_T *ufunc = dfunc->df_ufunc; |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 8472 | |
Bram Moolenaar | 077a423 | 2020-12-22 18:33:27 +0100 | [diff] [blame] | 8473 | if (ufunc != NULL && func_name_refcount(ufunc->uf_name)) |
| 8474 | func_ptr_unref(ufunc); |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 8475 | } |
| 8476 | break; |
| 8477 | |
| 8478 | case ISN_DCALL: |
| 8479 | { |
| 8480 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8481 | + isn->isn_arg.dfunc.cdf_idx; |
| 8482 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 8483 | if (dfunc->df_ufunc != NULL |
| 8484 | && func_name_refcount(dfunc->df_ufunc->uf_name)) |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 8485 | func_ptr_unref(dfunc->df_ufunc); |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 8486 | } |
| 8487 | break; |
| 8488 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 8489 | case ISN_NEWFUNC: |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 8490 | { |
| 8491 | char_u *lambda = isn->isn_arg.newfunc.nf_lambda; |
| 8492 | ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL); |
| 8493 | |
| 8494 | if (ufunc != NULL) |
| 8495 | { |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8496 | unlink_def_function(ufunc); |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 8497 | func_ptr_unref(ufunc); |
| 8498 | } |
| 8499 | |
| 8500 | vim_free(lambda); |
| 8501 | vim_free(isn->isn_arg.newfunc.nf_global); |
| 8502 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 8503 | break; |
| 8504 | |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 8505 | case ISN_CHECKTYPE: |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 8506 | case ISN_SETTYPE: |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 8507 | free_type(isn->isn_arg.type.ct_type); |
| 8508 | break; |
| 8509 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 8510 | case ISN_CMDMOD: |
| 8511 | vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod |
| 8512 | ->cmod_filter_regmatch.regprog); |
| 8513 | vim_free(isn->isn_arg.cmdmod.cf_cmdmod); |
| 8514 | break; |
| 8515 | |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 8516 | case ISN_LOADSCRIPT: |
| 8517 | case ISN_STORESCRIPT: |
| 8518 | vim_free(isn->isn_arg.script.scriptref); |
| 8519 | break; |
| 8520 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8521 | case ISN_2BOOL: |
| 8522 | case ISN_2STRING: |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 8523 | case ISN_2STRING_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8524 | case ISN_ADDBLOB: |
| 8525 | case ISN_ADDLIST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8526 | case ISN_ANYINDEX: |
| 8527 | case ISN_ANYSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8528 | case ISN_BCALL: |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 8529 | case ISN_BLOBAPPEND: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8530 | case ISN_CATCH: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8531 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8532 | case ISN_CHECKNR: |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 8533 | case ISN_CMDMOD_REV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8534 | case ISN_COMPAREANY: |
| 8535 | case ISN_COMPAREBLOB: |
| 8536 | case ISN_COMPAREBOOL: |
| 8537 | case ISN_COMPAREDICT: |
| 8538 | case ISN_COMPAREFLOAT: |
| 8539 | case ISN_COMPAREFUNC: |
| 8540 | case ISN_COMPARELIST: |
| 8541 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8542 | case ISN_COMPARESPECIAL: |
| 8543 | case ISN_COMPARESTRING: |
| 8544 | case ISN_CONCAT: |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 8545 | case ISN_COND2BOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8546 | case ISN_DROP: |
| 8547 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 8548 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 8549 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8550 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 8551 | case ISN_EXECCONCAT: |
| 8552 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8553 | case ISN_FOR: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8554 | case ISN_GETITEM: |
| 8555 | case ISN_JUMP: |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 8556 | case ISN_LISTAPPEND: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 8557 | case ISN_LISTINDEX: |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 8558 | case ISN_LISTSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8559 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 8560 | case ISN_LOADBDICT: |
| 8561 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 8562 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8563 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 8564 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8565 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 8566 | case ISN_LOADWDICT: |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 8567 | case ISN_LOCKCONST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8568 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8569 | case ISN_NEGATENR: |
| 8570 | case ISN_NEWDICT: |
| 8571 | case ISN_NEWLIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8572 | case ISN_OPANY: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8573 | case ISN_OPFLOAT: |
| 8574 | case ISN_OPNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8575 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 8576 | case ISN_PCALL_END: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8577 | case ISN_PUSHBOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8578 | case ISN_PUSHF: |
| 8579 | case ISN_PUSHNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8580 | case ISN_PUSHSPEC: |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 8581 | case ISN_PUT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8582 | case ISN_RETURN: |
Bram Moolenaar | 299f303 | 2021-01-08 20:53:09 +0100 | [diff] [blame] | 8583 | case ISN_RETURN_ZERO: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8584 | case ISN_SHUFFLE: |
| 8585 | case ISN_SLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8586 | case ISN_STORE: |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 8587 | case ISN_STOREINDEX: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8588 | case ISN_STORENR: |
| 8589 | case ISN_STOREOUTER: |
| 8590 | case ISN_STOREREG: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8591 | case ISN_STOREV: |
| 8592 | case ISN_STRINDEX: |
| 8593 | case ISN_STRSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8594 | case ISN_THROW: |
| 8595 | case ISN_TRY: |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 8596 | case ISN_UNLETINDEX: |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 8597 | case ISN_UNPACK: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8598 | // nothing allocated |
| 8599 | break; |
| 8600 | } |
| 8601 | } |
| 8602 | |
| 8603 | /* |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8604 | * Free all instructions for "dfunc" except df_name. |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8605 | */ |
| 8606 | static void |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8607 | delete_def_function_contents(dfunc_T *dfunc, int mark_deleted) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8608 | { |
| 8609 | int idx; |
| 8610 | |
| 8611 | ga_clear(&dfunc->df_def_args_isn); |
| 8612 | |
| 8613 | if (dfunc->df_instr != NULL) |
| 8614 | { |
| 8615 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 8616 | delete_instr(dfunc->df_instr + idx); |
| 8617 | VIM_CLEAR(dfunc->df_instr); |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8618 | dfunc->df_instr = NULL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8619 | } |
| 8620 | |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8621 | if (mark_deleted) |
| 8622 | dfunc->df_deleted = TRUE; |
| 8623 | if (dfunc->df_ufunc != NULL) |
| 8624 | dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8625 | } |
| 8626 | |
| 8627 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8628 | * 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] | 8629 | * function, unless another user function still uses it. |
| 8630 | * The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8631 | */ |
| 8632 | void |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8633 | unlink_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8634 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8635 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8636 | { |
| 8637 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8638 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8639 | |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8640 | if (--dfunc->df_refcount <= 0) |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8641 | delete_def_function_contents(dfunc, TRUE); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8642 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8643 | ufunc->uf_dfunc_idx = 0; |
| 8644 | if (dfunc->df_ufunc == ufunc) |
| 8645 | dfunc->df_ufunc = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8646 | } |
| 8647 | } |
| 8648 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8649 | /* |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8650 | * Used when a user function refers to an existing dfunc. |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8651 | */ |
| 8652 | void |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8653 | link_def_function(ufunc_T *ufunc) |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8654 | { |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8655 | if (ufunc->uf_dfunc_idx > 0) |
| 8656 | { |
| 8657 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8658 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8659 | |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8660 | ++dfunc->df_refcount; |
| 8661 | } |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8662 | } |
| 8663 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8664 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8665 | /* |
| 8666 | * Free all functions defined with ":def". |
| 8667 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8668 | void |
| 8669 | free_def_functions(void) |
| 8670 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8671 | int idx; |
| 8672 | |
| 8673 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 8674 | { |
| 8675 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 8676 | |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8677 | delete_def_function_contents(dfunc, TRUE); |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8678 | vim_free(dfunc->df_name); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8679 | } |
| 8680 | |
| 8681 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8682 | } |
| 8683 | #endif |
| 8684 | |
| 8685 | |
| 8686 | #endif // FEAT_EVAL |