Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9compile.c: :def and dealing with instructions |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | #ifdef VMS |
| 20 | # include <float.h> |
| 21 | #endif |
| 22 | |
| 23 | #define DEFINE_VIM9_GLOBALS |
| 24 | #include "vim9.h" |
| 25 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 26 | // values for ctx_skip |
| 27 | typedef enum { |
| 28 | SKIP_NOT, // condition is a constant, produce code |
| 29 | SKIP_YES, // condition is a constant, do NOT produce code |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 30 | SKIP_UNKNOWN // condition is not a constant, produce code |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 31 | } skip_T; |
| 32 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Chain of jump instructions where the end label needs to be set. |
| 35 | */ |
| 36 | typedef struct endlabel_S endlabel_T; |
| 37 | struct endlabel_S { |
| 38 | endlabel_T *el_next; // chain end_label locations |
| 39 | int el_end_label; // instruction idx where to set end |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * info specific for the scope of :if / elseif / else |
| 44 | */ |
| 45 | typedef struct { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 46 | int is_seen_else; |
| 47 | int is_had_return; // every block ends in :return |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 48 | int is_if_label; // instruction idx at IF or ELSEIF |
| 49 | endlabel_T *is_end_label; // instructions to set end label |
| 50 | } ifscope_T; |
| 51 | |
| 52 | /* |
| 53 | * info specific for the scope of :while |
| 54 | */ |
| 55 | typedef struct { |
| 56 | int ws_top_label; // instruction idx at WHILE |
| 57 | endlabel_T *ws_end_label; // instructions to set end |
| 58 | } whilescope_T; |
| 59 | |
| 60 | /* |
| 61 | * info specific for the scope of :for |
| 62 | */ |
| 63 | typedef struct { |
| 64 | int fs_top_label; // instruction idx at FOR |
| 65 | endlabel_T *fs_end_label; // break instructions |
| 66 | } forscope_T; |
| 67 | |
| 68 | /* |
| 69 | * info specific for the scope of :try |
| 70 | */ |
| 71 | typedef struct { |
| 72 | int ts_try_label; // instruction idx at TRY |
| 73 | endlabel_T *ts_end_label; // jump to :finally or :endtry |
| 74 | int ts_catch_label; // instruction idx of last CATCH |
| 75 | int ts_caught_all; // "catch" without argument encountered |
| 76 | } tryscope_T; |
| 77 | |
| 78 | typedef enum { |
| 79 | NO_SCOPE, |
| 80 | IF_SCOPE, |
| 81 | WHILE_SCOPE, |
| 82 | FOR_SCOPE, |
| 83 | TRY_SCOPE, |
| 84 | BLOCK_SCOPE |
| 85 | } scopetype_T; |
| 86 | |
| 87 | /* |
| 88 | * Info for one scope, pointed to by "ctx_scope". |
| 89 | */ |
| 90 | typedef struct scope_S scope_T; |
| 91 | struct scope_S { |
| 92 | scope_T *se_outer; // scope containing this one |
| 93 | scopetype_T se_type; |
| 94 | int se_local_count; // ctx_locals.ga_len before scope |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 95 | skip_T se_skip_save; // ctx_skip before the block |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 96 | union { |
| 97 | ifscope_T se_if; |
| 98 | whilescope_T se_while; |
| 99 | forscope_T se_for; |
| 100 | tryscope_T se_try; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 101 | } se_u; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Entry for "ctx_locals". Used for arguments and local variables. |
| 106 | */ |
| 107 | typedef struct { |
| 108 | char_u *lv_name; |
| 109 | type_T *lv_type; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 110 | int lv_idx; // index of the variable on the stack |
| 111 | int lv_from_outer; // when TRUE using ctx_outer scope |
| 112 | int lv_const; // when TRUE cannot be assigned to |
| 113 | int lv_arg; // when TRUE this is an argument |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 114 | } lvar_T; |
| 115 | |
| 116 | /* |
| 117 | * Context for compiling lines of Vim script. |
| 118 | * Stores info about the local variables and condition stack. |
| 119 | */ |
| 120 | struct cctx_S { |
| 121 | ufunc_T *ctx_ufunc; // current function |
| 122 | int ctx_lnum; // line number in current function |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 123 | char_u *ctx_line_start; // start of current line or NULL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 124 | garray_T ctx_instr; // generated instructions |
| 125 | |
| 126 | garray_T ctx_locals; // currently visible local variables |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 127 | int ctx_locals_count; // total number of local variables |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 128 | |
Bram Moolenaar | 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". |
| 152 | * "lvar->lv_from_outer" is set accordingly. |
| 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; |
| 175 | lvar->lv_from_outer = FALSE; |
| 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; |
| 189 | lvar->lv_from_outer = TRUE; |
| 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 | { |
| 261 | *gen_load_outer = TRUE; |
| 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; |
| 477 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 478 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 479 | switch ((*type)->tt_type) |
| 480 | { |
| 481 | // nothing to be done |
| 482 | case VAR_STRING: return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 483 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 484 | // conversion possible |
| 485 | case VAR_SPECIAL: |
| 486 | case VAR_BOOL: |
| 487 | case VAR_NUMBER: |
| 488 | case VAR_FLOAT: |
| 489 | break; |
| 490 | |
| 491 | // conversion possible (with runtime check) |
| 492 | case VAR_ANY: |
| 493 | case VAR_UNKNOWN: |
| 494 | isntype = ISN_2STRING_ANY; |
| 495 | break; |
| 496 | |
| 497 | // conversion not possible |
| 498 | case VAR_VOID: |
| 499 | case VAR_BLOB: |
| 500 | case VAR_FUNC: |
| 501 | case VAR_PARTIAL: |
| 502 | case VAR_LIST: |
| 503 | case VAR_DICT: |
| 504 | case VAR_JOB: |
| 505 | case VAR_CHANNEL: |
| 506 | to_string_error((*type)->tt_type); |
| 507 | return FAIL; |
| 508 | } |
| 509 | |
| 510 | *type = &t_string; |
| 511 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 512 | return FAIL; |
| 513 | isn->isn_arg.number = offset; |
| 514 | |
| 515 | return OK; |
| 516 | } |
| 517 | |
| 518 | static int |
| 519 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 520 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 521 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 522 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 523 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 524 | { |
| 525 | if (*op == '+') |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 526 | emsg(_(e_wrong_argument_type_for_plus)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 527 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 528 | semsg(_(e_char_requires_number_or_float_arguments), *op); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 529 | return FAIL; |
| 530 | } |
| 531 | return OK; |
| 532 | } |
| 533 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 534 | static int |
| 535 | generate_add_instr( |
| 536 | cctx_T *cctx, |
| 537 | vartype_T vartype, |
| 538 | type_T *type1, |
| 539 | type_T *type2) |
| 540 | { |
Bram Moolenaar | 399ea81 | 2020-12-15 21:28:57 +0100 | [diff] [blame] | 541 | garray_T *stack = &cctx->ctx_type_stack; |
| 542 | isn_T *isn = generate_instr_drop(cctx, |
| 543 | vartype == VAR_NUMBER ? ISN_OPNR |
| 544 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 545 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 546 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 399ea81 | 2020-12-15 21:28:57 +0100 | [diff] [blame] | 547 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 548 | #endif |
Bram Moolenaar | 399ea81 | 2020-12-15 21:28:57 +0100 | [diff] [blame] | 549 | : ISN_OPANY, 1); |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 550 | |
| 551 | if (vartype != VAR_LIST && vartype != VAR_BLOB |
| 552 | && type1->tt_type != VAR_ANY |
| 553 | && type2->tt_type != VAR_ANY |
| 554 | && check_number_or_float( |
| 555 | type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL) |
| 556 | return FAIL; |
| 557 | |
| 558 | if (isn != NULL) |
| 559 | isn->isn_arg.op.op_type = EXPR_ADD; |
Bram Moolenaar | 399ea81 | 2020-12-15 21:28:57 +0100 | [diff] [blame] | 560 | |
| 561 | // When concatenating two lists with different member types the member type |
| 562 | // becomes "any". |
| 563 | if (vartype == VAR_LIST |
| 564 | && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST |
| 565 | && type1->tt_member != type2->tt_member) |
| 566 | (((type_T **)stack->ga_data)[stack->ga_len - 1]) = &t_list_any; |
| 567 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 568 | return isn == NULL ? FAIL : OK; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Get the type to use for an instruction for an operation on "type1" and |
| 573 | * "type2". If they are matching use a type-specific instruction. Otherwise |
| 574 | * fall back to runtime type checking. |
| 575 | */ |
| 576 | static vartype_T |
| 577 | operator_type(type_T *type1, type_T *type2) |
| 578 | { |
| 579 | if (type1->tt_type == type2->tt_type |
| 580 | && (type1->tt_type == VAR_NUMBER |
| 581 | || type1->tt_type == VAR_LIST |
| 582 | #ifdef FEAT_FLOAT |
| 583 | || type1->tt_type == VAR_FLOAT |
| 584 | #endif |
| 585 | || type1->tt_type == VAR_BLOB)) |
| 586 | return type1->tt_type; |
| 587 | return VAR_ANY; |
| 588 | } |
| 589 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 590 | /* |
| 591 | * Generate an instruction with two arguments. The instruction depends on the |
| 592 | * type of the arguments. |
| 593 | */ |
| 594 | static int |
| 595 | generate_two_op(cctx_T *cctx, char_u *op) |
| 596 | { |
| 597 | garray_T *stack = &cctx->ctx_type_stack; |
| 598 | type_T *type1; |
| 599 | type_T *type2; |
| 600 | vartype_T vartype; |
| 601 | isn_T *isn; |
| 602 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 603 | RETURN_OK_IF_SKIP(cctx); |
| 604 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 605 | // Get the known type of the two items on the stack. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 606 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 607 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 608 | vartype = operator_type(type1, type2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 609 | |
| 610 | switch (*op) |
| 611 | { |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 612 | case '+': |
| 613 | if (generate_add_instr(cctx, vartype, type1, type2) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 614 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 615 | break; |
| 616 | |
| 617 | case '-': |
| 618 | case '*': |
| 619 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 620 | op) == FAIL) |
| 621 | return FAIL; |
| 622 | if (vartype == VAR_NUMBER) |
| 623 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 624 | #ifdef FEAT_FLOAT |
| 625 | else if (vartype == VAR_FLOAT) |
| 626 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 627 | #endif |
| 628 | else |
| 629 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 630 | if (isn != NULL) |
| 631 | isn->isn_arg.op.op_type = *op == '*' |
| 632 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 633 | break; |
| 634 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 635 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 636 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 637 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 638 | && type2->tt_type != VAR_NUMBER)) |
| 639 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 640 | emsg(_(e_percent_requires_number_arguments)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 641 | return FAIL; |
| 642 | } |
| 643 | isn = generate_instr_drop(cctx, |
| 644 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 645 | if (isn != NULL) |
| 646 | isn->isn_arg.op.op_type = EXPR_REM; |
| 647 | break; |
| 648 | } |
| 649 | |
| 650 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 651 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 652 | { |
| 653 | type_T *type = &t_any; |
| 654 | |
| 655 | #ifdef FEAT_FLOAT |
| 656 | // float+number and number+float results in float |
| 657 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 658 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 659 | type = &t_float; |
| 660 | #endif |
| 661 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 662 | } |
| 663 | |
| 664 | return OK; |
| 665 | } |
| 666 | |
| 667 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 668 | * Get the instruction to use for comparing "type1" with "type2" |
| 669 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 670 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 671 | static isntype_T |
| 672 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 673 | { |
| 674 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 675 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 676 | if (type1 == VAR_UNKNOWN) |
| 677 | type1 = VAR_ANY; |
| 678 | if (type2 == VAR_UNKNOWN) |
| 679 | type2 = VAR_ANY; |
| 680 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 681 | if (type1 == type2) |
| 682 | { |
| 683 | switch (type1) |
| 684 | { |
| 685 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 686 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 687 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 688 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 689 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 690 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 691 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 692 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 693 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 694 | default: isntype = ISN_COMPAREANY; break; |
| 695 | } |
| 696 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 697 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 698 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 699 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 700 | isntype = ISN_COMPAREANY; |
| 701 | |
| 702 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 703 | && (isntype == ISN_COMPAREBOOL |
| 704 | || isntype == ISN_COMPARESPECIAL |
| 705 | || isntype == ISN_COMPARENR |
| 706 | || isntype == ISN_COMPAREFLOAT)) |
| 707 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 708 | semsg(_(e_cannot_use_str_with_str), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 709 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 710 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 711 | } |
| 712 | if (isntype == ISN_DROP |
| 713 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 714 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 715 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 716 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 717 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 718 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 719 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 720 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 721 | semsg(_(e_cannot_compare_str_with_str), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 722 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 723 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 724 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 725 | return isntype; |
| 726 | } |
| 727 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 728 | int |
| 729 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 730 | { |
| 731 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 732 | return FAIL; |
| 733 | return OK; |
| 734 | } |
| 735 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 736 | /* |
| 737 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 738 | */ |
| 739 | static int |
| 740 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 741 | { |
| 742 | isntype_T isntype; |
| 743 | isn_T *isn; |
| 744 | garray_T *stack = &cctx->ctx_type_stack; |
| 745 | vartype_T type1; |
| 746 | vartype_T type2; |
| 747 | |
| 748 | RETURN_OK_IF_SKIP(cctx); |
| 749 | |
| 750 | // Get the known type of the two items on the stack. If they are matching |
| 751 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 752 | // checking. |
| 753 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 754 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 755 | isntype = get_compare_isn(exptype, type1, type2); |
| 756 | if (isntype == ISN_DROP) |
| 757 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 758 | |
| 759 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 760 | return FAIL; |
| 761 | isn->isn_arg.op.op_type = exptype; |
| 762 | isn->isn_arg.op.op_ic = ic; |
| 763 | |
| 764 | // takes two arguments, puts one bool back |
| 765 | if (stack->ga_len >= 2) |
| 766 | { |
| 767 | --stack->ga_len; |
| 768 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 769 | } |
| 770 | |
| 771 | return OK; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Generate an ISN_2BOOL instruction. |
| 776 | */ |
| 777 | static int |
| 778 | generate_2BOOL(cctx_T *cctx, int invert) |
| 779 | { |
| 780 | isn_T *isn; |
| 781 | garray_T *stack = &cctx->ctx_type_stack; |
| 782 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 783 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 784 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 785 | return FAIL; |
| 786 | isn->isn_arg.number = invert; |
| 787 | |
| 788 | // type becomes bool |
| 789 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 790 | |
| 791 | return OK; |
| 792 | } |
| 793 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 794 | /* |
| 795 | * Generate an ISN_COND2BOOL instruction. |
| 796 | */ |
| 797 | static int |
| 798 | generate_COND2BOOL(cctx_T *cctx) |
| 799 | { |
| 800 | isn_T *isn; |
| 801 | garray_T *stack = &cctx->ctx_type_stack; |
| 802 | |
| 803 | RETURN_OK_IF_SKIP(cctx); |
| 804 | if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL) |
| 805 | return FAIL; |
| 806 | |
| 807 | // type becomes bool |
| 808 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 809 | |
| 810 | return OK; |
| 811 | } |
| 812 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 813 | static int |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 814 | generate_TYPECHECK( |
| 815 | cctx_T *cctx, |
| 816 | type_T *expected, |
| 817 | int offset) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 818 | { |
| 819 | isn_T *isn; |
| 820 | garray_T *stack = &cctx->ctx_type_stack; |
| 821 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 822 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 823 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 824 | return FAIL; |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 825 | isn->isn_arg.type.ct_type = alloc_type(expected); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 826 | isn->isn_arg.type.ct_off = offset; |
| 827 | |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 828 | // type becomes expected |
| 829 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 830 | |
| 831 | return OK; |
| 832 | } |
| 833 | |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 834 | static int |
| 835 | generate_SETTYPE( |
| 836 | cctx_T *cctx, |
| 837 | type_T *expected) |
| 838 | { |
| 839 | isn_T *isn; |
| 840 | |
| 841 | RETURN_OK_IF_SKIP(cctx); |
| 842 | if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL) |
| 843 | return FAIL; |
| 844 | isn->isn_arg.type.ct_type = alloc_type(expected); |
| 845 | return OK; |
| 846 | } |
| 847 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 848 | /* |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 849 | * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be |
| 850 | * used. Return FALSE if the types will never match. |
| 851 | */ |
Bram Moolenaar | 193f620 | 2020-11-16 20:08:35 +0100 | [diff] [blame] | 852 | int |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 853 | use_typecheck(type_T *actual, type_T *expected) |
| 854 | { |
| 855 | if (actual->tt_type == VAR_ANY |
| 856 | || actual->tt_type == VAR_UNKNOWN |
| 857 | || (actual->tt_type == VAR_FUNC |
| 858 | && (expected->tt_type == VAR_FUNC |
| 859 | || expected->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 328eac2 | 2021-01-07 19:23:08 +0100 | [diff] [blame] | 860 | && (actual->tt_member == &t_any || actual->tt_argcount < 0) |
| 861 | && ((actual->tt_member == &t_void) |
| 862 | == (expected->tt_member == &t_void)))) |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 863 | return TRUE; |
| 864 | if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT) |
| 865 | && actual->tt_type == expected->tt_type) |
| 866 | // This takes care of a nested list or dict. |
| 867 | return use_typecheck(actual->tt_member, expected->tt_member); |
| 868 | return FALSE; |
| 869 | } |
| 870 | |
| 871 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 872 | * Check that |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 873 | * - "actual" matches "expected" type or |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 874 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 875 | * - return FAIL. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 876 | * If "actual_is_const" is TRUE then the type won't change at runtime, do not |
| 877 | * generate a TYPECHECK. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 878 | */ |
| 879 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 880 | need_type( |
| 881 | type_T *actual, |
| 882 | type_T *expected, |
| 883 | int offset, |
| 884 | cctx_T *cctx, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 885 | int silent, |
| 886 | int actual_is_const) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 887 | { |
Bram Moolenaar | 4ed124c | 2020-09-09 20:03:46 +0200 | [diff] [blame] | 888 | if (expected == &t_bool && actual != &t_bool |
| 889 | && (actual->tt_flags & TTFLAG_BOOL_OK)) |
| 890 | { |
| 891 | // Using "0", "1" or the result of an expression with "&&" or "||" as a |
| 892 | // boolean is OK but requires a conversion. |
| 893 | generate_2BOOL(cctx, FALSE); |
| 894 | return OK; |
| 895 | } |
| 896 | |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 897 | if (check_type(expected, actual, FALSE, 0) == OK) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 898 | return OK; |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 899 | |
| 900 | // 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] | 901 | // If it's a constant a runtime check makes no sense. |
| 902 | if (!actual_is_const && use_typecheck(actual, expected)) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 903 | { |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 904 | generate_TYPECHECK(cctx, expected, offset); |
| 905 | return OK; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 906 | } |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 907 | |
| 908 | if (!silent) |
| 909 | type_mismatch(expected, actual); |
| 910 | return FAIL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | /* |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 914 | * Check that the top of the type stack has a type that can be used as a |
| 915 | * condition. Give an error and return FAIL if not. |
| 916 | */ |
| 917 | static int |
| 918 | bool_on_stack(cctx_T *cctx) |
| 919 | { |
| 920 | garray_T *stack = &cctx->ctx_type_stack; |
| 921 | type_T *type; |
| 922 | |
| 923 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 924 | if (type == &t_bool) |
| 925 | return OK; |
| 926 | |
| 927 | if (type == &t_any || type == &t_number) |
| 928 | // Number 0 and 1 are OK to use as a bool. "any" could also be a bool. |
| 929 | // This requires a runtime type check. |
| 930 | return generate_COND2BOOL(cctx); |
| 931 | |
| 932 | return need_type(type, &t_bool, -1, cctx, FALSE, FALSE); |
| 933 | } |
| 934 | |
| 935 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 936 | * Generate an ISN_PUSHNR instruction. |
| 937 | */ |
| 938 | static int |
| 939 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 940 | { |
| 941 | isn_T *isn; |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 942 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 943 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 944 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 945 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 946 | return FAIL; |
| 947 | isn->isn_arg.number = number; |
| 948 | |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 949 | if (number == 0 || number == 1) |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 950 | // A 0 or 1 number can also be used as a bool. |
Bram Moolenaar | 3868f59 | 2020-12-25 13:20:41 +0100 | [diff] [blame] | 951 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_number_bool; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 952 | return OK; |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Generate an ISN_PUSHBOOL instruction. |
| 957 | */ |
| 958 | static int |
| 959 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 960 | { |
| 961 | isn_T *isn; |
| 962 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 963 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 964 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 965 | return FAIL; |
| 966 | isn->isn_arg.number = number; |
| 967 | |
| 968 | return OK; |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | * Generate an ISN_PUSHSPEC instruction. |
| 973 | */ |
| 974 | static int |
| 975 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 976 | { |
| 977 | isn_T *isn; |
| 978 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 979 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 980 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 981 | return FAIL; |
| 982 | isn->isn_arg.number = number; |
| 983 | |
| 984 | return OK; |
| 985 | } |
| 986 | |
| 987 | #ifdef FEAT_FLOAT |
| 988 | /* |
| 989 | * Generate an ISN_PUSHF instruction. |
| 990 | */ |
| 991 | static int |
| 992 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 993 | { |
| 994 | isn_T *isn; |
| 995 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 996 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 997 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 998 | return FAIL; |
| 999 | isn->isn_arg.fnumber = fnumber; |
| 1000 | |
| 1001 | return OK; |
| 1002 | } |
| 1003 | #endif |
| 1004 | |
| 1005 | /* |
| 1006 | * Generate an ISN_PUSHS instruction. |
| 1007 | * Consumes "str". |
| 1008 | */ |
| 1009 | static int |
| 1010 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 1011 | { |
| 1012 | isn_T *isn; |
| 1013 | |
Bram Moolenaar | 508b561 | 2021-01-02 18:17:26 +0100 | [diff] [blame] | 1014 | if (cctx->ctx_skip == SKIP_YES) |
| 1015 | { |
| 1016 | vim_free(str); |
| 1017 | return OK; |
| 1018 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1019 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 1020 | return FAIL; |
| 1021 | isn->isn_arg.string = str; |
| 1022 | |
| 1023 | return OK; |
| 1024 | } |
| 1025 | |
| 1026 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1027 | * Generate an ISN_PUSHCHANNEL instruction. |
| 1028 | * Consumes "channel". |
| 1029 | */ |
| 1030 | static int |
| 1031 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 1032 | { |
| 1033 | isn_T *isn; |
| 1034 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1035 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1036 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 1037 | return FAIL; |
| 1038 | isn->isn_arg.channel = channel; |
| 1039 | |
| 1040 | return OK; |
| 1041 | } |
| 1042 | |
| 1043 | /* |
| 1044 | * Generate an ISN_PUSHJOB instruction. |
| 1045 | * Consumes "job". |
| 1046 | */ |
| 1047 | static int |
| 1048 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 1049 | { |
| 1050 | isn_T *isn; |
| 1051 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1052 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1053 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1054 | return FAIL; |
| 1055 | isn->isn_arg.job = job; |
| 1056 | |
| 1057 | return OK; |
| 1058 | } |
| 1059 | |
| 1060 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1061 | * Generate an ISN_PUSHBLOB instruction. |
| 1062 | * Consumes "blob". |
| 1063 | */ |
| 1064 | static int |
| 1065 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1066 | { |
| 1067 | isn_T *isn; |
| 1068 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1069 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1070 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1071 | return FAIL; |
| 1072 | isn->isn_arg.blob = blob; |
| 1073 | |
| 1074 | return OK; |
| 1075 | } |
| 1076 | |
| 1077 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1078 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1079 | * Consumes "name". |
| 1080 | */ |
| 1081 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1082 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1083 | { |
| 1084 | isn_T *isn; |
| 1085 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1086 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1087 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1088 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 1089 | isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1090 | |
| 1091 | return OK; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1095 | * Generate an ISN_GETITEM instruction with "index". |
| 1096 | */ |
| 1097 | static int |
| 1098 | generate_GETITEM(cctx_T *cctx, int index) |
| 1099 | { |
| 1100 | isn_T *isn; |
| 1101 | garray_T *stack = &cctx->ctx_type_stack; |
| 1102 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1103 | type_T *item_type = &t_any; |
| 1104 | |
| 1105 | RETURN_OK_IF_SKIP(cctx); |
| 1106 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1107 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1108 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1109 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1110 | emsg(_(e_listreq)); |
| 1111 | return FAIL; |
| 1112 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1113 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1114 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1115 | return FAIL; |
| 1116 | isn->isn_arg.number = index; |
| 1117 | |
| 1118 | // add the item type to the type stack |
| 1119 | if (ga_grow(stack, 1) == FAIL) |
| 1120 | return FAIL; |
| 1121 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 1122 | ++stack->ga_len; |
| 1123 | return OK; |
| 1124 | } |
| 1125 | |
| 1126 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 1127 | * Generate an ISN_SLICE instruction with "count". |
| 1128 | */ |
| 1129 | static int |
| 1130 | generate_SLICE(cctx_T *cctx, int count) |
| 1131 | { |
| 1132 | isn_T *isn; |
| 1133 | |
| 1134 | RETURN_OK_IF_SKIP(cctx); |
| 1135 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1136 | return FAIL; |
| 1137 | isn->isn_arg.number = count; |
| 1138 | return OK; |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1143 | */ |
| 1144 | static int |
| 1145 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1146 | { |
| 1147 | isn_T *isn; |
| 1148 | |
| 1149 | RETURN_OK_IF_SKIP(cctx); |
| 1150 | |
| 1151 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1152 | return FAIL; |
| 1153 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1154 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1155 | |
| 1156 | return OK; |
| 1157 | } |
| 1158 | |
| 1159 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1160 | * Generate an ISN_STORE instruction. |
| 1161 | */ |
| 1162 | static int |
| 1163 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1164 | { |
| 1165 | isn_T *isn; |
| 1166 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1167 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1168 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1169 | return FAIL; |
| 1170 | if (name != NULL) |
| 1171 | isn->isn_arg.string = vim_strsave(name); |
| 1172 | else |
| 1173 | isn->isn_arg.number = idx; |
| 1174 | |
| 1175 | return OK; |
| 1176 | } |
| 1177 | |
| 1178 | /* |
| 1179 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1180 | */ |
| 1181 | static int |
| 1182 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1183 | { |
| 1184 | isn_T *isn; |
| 1185 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1186 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1187 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1188 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1189 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1190 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1191 | |
| 1192 | return OK; |
| 1193 | } |
| 1194 | |
| 1195 | /* |
| 1196 | * Generate an ISN_STOREOPT instruction |
| 1197 | */ |
| 1198 | static int |
| 1199 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1200 | { |
| 1201 | isn_T *isn; |
| 1202 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1203 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1c199f9 | 2020-08-07 21:28:34 +0200 | [diff] [blame] | 1204 | if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1205 | return FAIL; |
| 1206 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1207 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1208 | |
| 1209 | return OK; |
| 1210 | } |
| 1211 | |
| 1212 | /* |
| 1213 | * Generate an ISN_LOAD or similar instruction. |
| 1214 | */ |
| 1215 | static int |
| 1216 | generate_LOAD( |
| 1217 | cctx_T *cctx, |
| 1218 | isntype_T isn_type, |
| 1219 | int idx, |
| 1220 | char_u *name, |
| 1221 | type_T *type) |
| 1222 | { |
| 1223 | isn_T *isn; |
| 1224 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1225 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1226 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1227 | return FAIL; |
| 1228 | if (name != NULL) |
| 1229 | isn->isn_arg.string = vim_strsave(name); |
| 1230 | else |
| 1231 | isn->isn_arg.number = idx; |
| 1232 | |
| 1233 | return OK; |
| 1234 | } |
| 1235 | |
| 1236 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1237 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1238 | */ |
| 1239 | static int |
| 1240 | generate_LOADV( |
| 1241 | cctx_T *cctx, |
| 1242 | char_u *name, |
| 1243 | int error) |
| 1244 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1245 | int di_flags; |
| 1246 | int vidx = find_vim_var(name, &di_flags); |
| 1247 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1248 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1249 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1250 | if (vidx < 0) |
| 1251 | { |
| 1252 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1253 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1254 | return FAIL; |
| 1255 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1256 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1257 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1258 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1262 | * Generate an ISN_UNLET instruction. |
| 1263 | */ |
| 1264 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1265 | 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] | 1266 | { |
| 1267 | isn_T *isn; |
| 1268 | |
| 1269 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1270 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1271 | return FAIL; |
| 1272 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1273 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1274 | |
| 1275 | return OK; |
| 1276 | } |
| 1277 | |
| 1278 | /* |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 1279 | * Generate an ISN_LOCKCONST instruction. |
| 1280 | */ |
| 1281 | static int |
| 1282 | generate_LOCKCONST(cctx_T *cctx) |
| 1283 | { |
| 1284 | isn_T *isn; |
| 1285 | |
| 1286 | RETURN_OK_IF_SKIP(cctx); |
| 1287 | if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL) |
| 1288 | return FAIL; |
| 1289 | return OK; |
| 1290 | } |
| 1291 | |
| 1292 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1293 | * Generate an ISN_LOADS instruction. |
| 1294 | */ |
| 1295 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1296 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1297 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1298 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1299 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1300 | int sid, |
| 1301 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1302 | { |
| 1303 | isn_T *isn; |
| 1304 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1305 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1306 | if (isn_type == ISN_LOADS) |
| 1307 | isn = generate_instr_type(cctx, isn_type, type); |
| 1308 | else |
| 1309 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1310 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1311 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1312 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1313 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1314 | |
| 1315 | return OK; |
| 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1320 | */ |
| 1321 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1322 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1323 | cctx_T *cctx, |
| 1324 | isntype_T isn_type, |
| 1325 | int sid, |
| 1326 | int idx, |
| 1327 | type_T *type) |
| 1328 | { |
| 1329 | isn_T *isn; |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 1330 | scriptref_T *sref; |
| 1331 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1332 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1333 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1334 | if (isn_type == ISN_LOADSCRIPT) |
| 1335 | isn = generate_instr_type(cctx, isn_type, type); |
| 1336 | else |
| 1337 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1338 | if (isn == NULL) |
| 1339 | return FAIL; |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 1340 | |
| 1341 | // This requires three arguments, which doesn't fit in an instruction, thus |
| 1342 | // we need to allocate a struct for this. |
| 1343 | sref = ALLOC_ONE(scriptref_T); |
| 1344 | if (sref == NULL) |
| 1345 | return FAIL; |
| 1346 | isn->isn_arg.script.scriptref = sref; |
| 1347 | sref->sref_sid = sid; |
| 1348 | sref->sref_idx = idx; |
| 1349 | sref->sref_seq = si->sn_script_seq; |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1350 | sref->sref_type = type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1351 | return OK; |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * Generate an ISN_NEWLIST instruction. |
| 1356 | */ |
| 1357 | static int |
| 1358 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1359 | { |
| 1360 | isn_T *isn; |
| 1361 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1362 | type_T *type; |
| 1363 | type_T *member; |
| 1364 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1365 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1366 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1367 | return FAIL; |
| 1368 | isn->isn_arg.number = count; |
| 1369 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1370 | // get the member type from all the items on the stack. |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1371 | if (count == 0) |
| 1372 | member = &t_void; |
| 1373 | else |
| 1374 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1375 | ((type_T **)stack->ga_data) + stack->ga_len, count, 1, |
| 1376 | cctx->ctx_type_list); |
| 1377 | type = get_list_type(member, cctx->ctx_type_list); |
| 1378 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1379 | // drop the value types |
| 1380 | stack->ga_len -= count; |
| 1381 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1382 | // add the list type to the type stack |
| 1383 | if (ga_grow(stack, 1) == FAIL) |
| 1384 | return FAIL; |
| 1385 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1386 | ++stack->ga_len; |
| 1387 | |
| 1388 | return OK; |
| 1389 | } |
| 1390 | |
| 1391 | /* |
| 1392 | * Generate an ISN_NEWDICT instruction. |
| 1393 | */ |
| 1394 | static int |
| 1395 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1396 | { |
| 1397 | isn_T *isn; |
| 1398 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1399 | type_T *type; |
| 1400 | type_T *member; |
| 1401 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1402 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1403 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1404 | return FAIL; |
| 1405 | isn->isn_arg.number = count; |
| 1406 | |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1407 | if (count == 0) |
| 1408 | member = &t_void; |
| 1409 | else |
| 1410 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1411 | ((type_T **)stack->ga_data) + stack->ga_len, count, 2, |
| 1412 | cctx->ctx_type_list); |
| 1413 | type = get_dict_type(member, cctx->ctx_type_list); |
| 1414 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1415 | // drop the key and value types |
| 1416 | stack->ga_len -= 2 * count; |
| 1417 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1418 | // add the dict type to the type stack |
| 1419 | if (ga_grow(stack, 1) == FAIL) |
| 1420 | return FAIL; |
| 1421 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1422 | ++stack->ga_len; |
| 1423 | |
| 1424 | return OK; |
| 1425 | } |
| 1426 | |
| 1427 | /* |
| 1428 | * Generate an ISN_FUNCREF instruction. |
| 1429 | */ |
| 1430 | static int |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1431 | generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1432 | { |
| 1433 | isn_T *isn; |
| 1434 | garray_T *stack = &cctx->ctx_type_stack; |
| 1435 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1436 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1437 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1438 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1439 | isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 1440 | cctx->ctx_has_closure = 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1441 | |
| 1442 | if (ga_grow(stack, 1) == FAIL) |
| 1443 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1444 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 1445 | ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1446 | ++stack->ga_len; |
| 1447 | |
| 1448 | return OK; |
| 1449 | } |
| 1450 | |
| 1451 | /* |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1452 | * Generate an ISN_NEWFUNC instruction. |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 1453 | * "lambda_name" and "func_name" must be in allocated memory and will be |
| 1454 | * consumed. |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1455 | */ |
| 1456 | static int |
| 1457 | generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) |
| 1458 | { |
| 1459 | isn_T *isn; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1460 | |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 1461 | if (cctx->ctx_skip == SKIP_YES) |
| 1462 | { |
| 1463 | vim_free(lambda_name); |
| 1464 | vim_free(func_name); |
| 1465 | return OK; |
| 1466 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1467 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 1468 | { |
| 1469 | vim_free(lambda_name); |
| 1470 | vim_free(func_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1471 | return FAIL; |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 1472 | } |
| 1473 | isn->isn_arg.newfunc.nf_lambda = lambda_name; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1474 | isn->isn_arg.newfunc.nf_global = func_name; |
| 1475 | |
| 1476 | return OK; |
| 1477 | } |
| 1478 | |
| 1479 | /* |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 1480 | * Generate an ISN_DEF instruction: list functions |
| 1481 | */ |
| 1482 | static int |
| 1483 | generate_DEF(cctx_T *cctx, char_u *name, size_t len) |
| 1484 | { |
| 1485 | isn_T *isn; |
| 1486 | |
| 1487 | RETURN_OK_IF_SKIP(cctx); |
| 1488 | if ((isn = generate_instr(cctx, ISN_DEF)) == NULL) |
| 1489 | return FAIL; |
| 1490 | if (len > 0) |
| 1491 | { |
| 1492 | isn->isn_arg.string = vim_strnsave(name, len); |
| 1493 | if (isn->isn_arg.string == NULL) |
| 1494 | return FAIL; |
| 1495 | } |
| 1496 | return OK; |
| 1497 | } |
| 1498 | |
| 1499 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1500 | * Generate an ISN_JUMP instruction. |
| 1501 | */ |
| 1502 | static int |
| 1503 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1504 | { |
| 1505 | isn_T *isn; |
| 1506 | garray_T *stack = &cctx->ctx_type_stack; |
| 1507 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1508 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1509 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1510 | return FAIL; |
| 1511 | isn->isn_arg.jump.jump_when = when; |
| 1512 | isn->isn_arg.jump.jump_where = where; |
| 1513 | |
| 1514 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1515 | --stack->ga_len; |
| 1516 | |
| 1517 | return OK; |
| 1518 | } |
| 1519 | |
| 1520 | static int |
| 1521 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1522 | { |
| 1523 | isn_T *isn; |
| 1524 | garray_T *stack = &cctx->ctx_type_stack; |
| 1525 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1526 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1527 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1528 | return FAIL; |
| 1529 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1530 | |
| 1531 | if (ga_grow(stack, 1) == FAIL) |
| 1532 | return FAIL; |
| 1533 | // type doesn't matter, will be stored next |
| 1534 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1535 | ++stack->ga_len; |
| 1536 | |
| 1537 | return OK; |
| 1538 | } |
| 1539 | |
| 1540 | /* |
| 1541 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1542 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1543 | * Return FAIL if the number of arguments is wrong. |
| 1544 | */ |
| 1545 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1546 | 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] | 1547 | { |
| 1548 | isn_T *isn; |
| 1549 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1550 | int argoff; |
Bram Moolenaar | a1224cb | 2020-10-22 12:31:49 +0200 | [diff] [blame] | 1551 | type_T **argtypes = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1552 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1553 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1554 | argoff = check_internal_func(func_idx, argcount); |
| 1555 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1556 | return FAIL; |
| 1557 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1558 | if (method_call && argoff > 1) |
| 1559 | { |
| 1560 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1561 | return FAIL; |
| 1562 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1563 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1564 | } |
| 1565 | |
Bram Moolenaar | af7a906 | 2020-10-21 16:49:17 +0200 | [diff] [blame] | 1566 | if (argcount > 0) |
| 1567 | { |
| 1568 | // Check the types of the arguments. |
| 1569 | argtypes = ((type_T **)stack->ga_data) + stack->ga_len - argcount; |
| 1570 | if (internal_func_check_arg_types(argtypes, func_idx, argcount) == FAIL) |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 1571 | return FAIL; |
Bram Moolenaar | af7a906 | 2020-10-21 16:49:17 +0200 | [diff] [blame] | 1572 | } |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 1573 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1574 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1575 | return FAIL; |
| 1576 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1577 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1578 | |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 1579 | // Drop the argument types and push the return type. |
| 1580 | stack->ga_len -= argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1581 | if (ga_grow(stack, 1) == FAIL) |
| 1582 | return FAIL; |
| 1583 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1584 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 94738d8 | 2020-10-21 14:25:07 +0200 | [diff] [blame] | 1585 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1586 | |
| 1587 | return OK; |
| 1588 | } |
| 1589 | |
| 1590 | /* |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 1591 | * Generate an ISN_LISTAPPEND instruction. Works like add(). |
| 1592 | * Argument count is already checked. |
| 1593 | */ |
| 1594 | static int |
| 1595 | generate_LISTAPPEND(cctx_T *cctx) |
| 1596 | { |
| 1597 | garray_T *stack = &cctx->ctx_type_stack; |
| 1598 | type_T *list_type; |
| 1599 | type_T *item_type; |
| 1600 | type_T *expected; |
| 1601 | |
| 1602 | // Caller already checked that list_type is a list. |
| 1603 | list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 1604 | item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1605 | expected = list_type->tt_member; |
| 1606 | if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL) |
| 1607 | return FAIL; |
| 1608 | |
| 1609 | if (generate_instr(cctx, ISN_LISTAPPEND) == NULL) |
| 1610 | return FAIL; |
| 1611 | |
| 1612 | --stack->ga_len; // drop the argument |
| 1613 | return OK; |
| 1614 | } |
| 1615 | |
| 1616 | /* |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 1617 | * Generate an ISN_BLOBAPPEND instruction. Works like add(). |
| 1618 | * Argument count is already checked. |
| 1619 | */ |
| 1620 | static int |
| 1621 | generate_BLOBAPPEND(cctx_T *cctx) |
| 1622 | { |
| 1623 | garray_T *stack = &cctx->ctx_type_stack; |
| 1624 | type_T *item_type; |
| 1625 | |
| 1626 | // Caller already checked that blob_type is a blob. |
| 1627 | item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1628 | if (need_type(item_type, &t_number, -1, cctx, FALSE, FALSE) == FAIL) |
| 1629 | return FAIL; |
| 1630 | |
| 1631 | if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL) |
| 1632 | return FAIL; |
| 1633 | |
| 1634 | --stack->ga_len; // drop the argument |
| 1635 | return OK; |
| 1636 | } |
| 1637 | |
| 1638 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1639 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1640 | * Return FAIL if the number of arguments is wrong. |
| 1641 | */ |
| 1642 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1643 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1644 | { |
| 1645 | isn_T *isn; |
| 1646 | garray_T *stack = &cctx->ctx_type_stack; |
| 1647 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1648 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1649 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1650 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1651 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1652 | { |
Bram Moolenaar | b185a40 | 2020-09-18 22:42:00 +0200 | [diff] [blame] | 1653 | semsg(_(e_toomanyarg), printable_func_name(ufunc)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1654 | return FAIL; |
| 1655 | } |
| 1656 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1657 | { |
Bram Moolenaar | b185a40 | 2020-09-18 22:42:00 +0200 | [diff] [blame] | 1658 | semsg(_(e_toofewarg), printable_func_name(ufunc)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1659 | return FAIL; |
| 1660 | } |
| 1661 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1662 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1663 | { |
| 1664 | int i; |
| 1665 | |
| 1666 | for (i = 0; i < argcount; ++i) |
| 1667 | { |
| 1668 | type_T *expected; |
| 1669 | type_T *actual; |
| 1670 | |
| 1671 | if (i < regular_args) |
| 1672 | { |
| 1673 | if (ufunc->uf_arg_types == NULL) |
| 1674 | continue; |
| 1675 | expected = ufunc->uf_arg_types[i]; |
| 1676 | } |
Bram Moolenaar | 2f8cbc4 | 2020-09-16 17:22:59 +0200 | [diff] [blame] | 1677 | else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any) |
| 1678 | // possibly a lambda or "...: any" |
Bram Moolenaar | 79e8db9 | 2020-08-14 22:16:33 +0200 | [diff] [blame] | 1679 | expected = &t_any; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1680 | else |
| 1681 | expected = ufunc->uf_va_type->tt_member; |
| 1682 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 1683 | if (need_type(actual, expected, -argcount + i, cctx, |
| 1684 | TRUE, FALSE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1685 | { |
| 1686 | arg_type_mismatch(expected, actual, i + 1); |
| 1687 | return FAIL; |
| 1688 | } |
| 1689 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1690 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1691 | if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL) |
| 1692 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1693 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1694 | } |
| 1695 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1696 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1697 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1698 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1699 | return FAIL; |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 1700 | if (isn->isn_type == ISN_DCALL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1701 | { |
| 1702 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1703 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1704 | } |
| 1705 | else |
| 1706 | { |
| 1707 | // A user function may be deleted and redefined later, can't use the |
| 1708 | // ufunc pointer, need to look it up again at runtime. |
| 1709 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1710 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1711 | } |
| 1712 | |
| 1713 | stack->ga_len -= argcount; // drop the arguments |
| 1714 | if (ga_grow(stack, 1) == FAIL) |
| 1715 | return FAIL; |
| 1716 | // add return value |
| 1717 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1718 | ++stack->ga_len; |
| 1719 | |
| 1720 | return OK; |
| 1721 | } |
| 1722 | |
| 1723 | /* |
| 1724 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1725 | */ |
| 1726 | static int |
| 1727 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1728 | { |
| 1729 | isn_T *isn; |
| 1730 | garray_T *stack = &cctx->ctx_type_stack; |
| 1731 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1732 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1733 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1734 | return FAIL; |
| 1735 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1736 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1737 | |
| 1738 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1739 | if (ga_grow(stack, 1) == FAIL) |
| 1740 | return FAIL; |
| 1741 | // add return value |
| 1742 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1743 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1744 | |
| 1745 | return OK; |
| 1746 | } |
| 1747 | |
| 1748 | /* |
| 1749 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1750 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1751 | */ |
| 1752 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1753 | generate_PCALL( |
| 1754 | cctx_T *cctx, |
| 1755 | int argcount, |
| 1756 | char_u *name, |
| 1757 | type_T *type, |
| 1758 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1759 | { |
| 1760 | isn_T *isn; |
| 1761 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1762 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1763 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1764 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1765 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1766 | if (type->tt_type == VAR_ANY) |
| 1767 | ret_type = &t_any; |
| 1768 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1769 | { |
| 1770 | if (type->tt_argcount != -1) |
| 1771 | { |
| 1772 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1773 | |
| 1774 | if (argcount < type->tt_min_argcount - varargs) |
| 1775 | { |
Bram Moolenaar | 6cf7e3b | 2020-10-28 14:31:16 +0100 | [diff] [blame] | 1776 | semsg(_(e_toofewarg), name); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1777 | return FAIL; |
| 1778 | } |
| 1779 | if (!varargs && argcount > type->tt_argcount) |
| 1780 | { |
Bram Moolenaar | 6cf7e3b | 2020-10-28 14:31:16 +0100 | [diff] [blame] | 1781 | semsg(_(e_toomanyarg), name); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1782 | return FAIL; |
| 1783 | } |
Bram Moolenaar | b4d16cb | 2020-11-05 18:45:46 +0100 | [diff] [blame] | 1784 | if (type->tt_args != NULL) |
| 1785 | { |
| 1786 | int i; |
| 1787 | |
| 1788 | for (i = 0; i < argcount; ++i) |
| 1789 | { |
| 1790 | int offset = -argcount + i - 1; |
| 1791 | type_T *actual = ((type_T **)stack->ga_data)[ |
| 1792 | stack->ga_len + offset]; |
| 1793 | type_T *expected; |
| 1794 | |
Bram Moolenaar | 32b3f82 | 2021-01-06 21:59:39 +0100 | [diff] [blame] | 1795 | if (varargs && i >= type->tt_argcount - 1) |
Bram Moolenaar | b4d16cb | 2020-11-05 18:45:46 +0100 | [diff] [blame] | 1796 | expected = type->tt_args[ |
Bram Moolenaar | 32b3f82 | 2021-01-06 21:59:39 +0100 | [diff] [blame] | 1797 | type->tt_argcount - 1]->tt_member; |
Bram Moolenaar | b4d16cb | 2020-11-05 18:45:46 +0100 | [diff] [blame] | 1798 | else |
| 1799 | expected = type->tt_args[i]; |
| 1800 | if (need_type(actual, expected, offset, |
| 1801 | cctx, TRUE, FALSE) == FAIL) |
| 1802 | { |
| 1803 | arg_type_mismatch(expected, actual, i + 1); |
| 1804 | return FAIL; |
| 1805 | } |
| 1806 | } |
| 1807 | } |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1808 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1809 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1810 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1811 | else |
| 1812 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1813 | semsg(_(e_not_callable_type_str), name); |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1814 | return FAIL; |
| 1815 | } |
| 1816 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1817 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1818 | return FAIL; |
| 1819 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1820 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1821 | |
| 1822 | stack->ga_len -= argcount; // drop the arguments |
| 1823 | |
| 1824 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1825 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1826 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1827 | // If partial is above the arguments it must be cleared and replaced with |
| 1828 | // the return value. |
| 1829 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1830 | return FAIL; |
| 1831 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1832 | return OK; |
| 1833 | } |
| 1834 | |
| 1835 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1836 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1837 | */ |
| 1838 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1839 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1840 | { |
| 1841 | isn_T *isn; |
| 1842 | garray_T *stack = &cctx->ctx_type_stack; |
| 1843 | type_T *type; |
| 1844 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1845 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1846 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1847 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1848 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1849 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1850 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1851 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1852 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1853 | { |
| 1854 | emsg(_(e_dictreq)); |
| 1855 | return FAIL; |
| 1856 | } |
| 1857 | // change dict type to dict member type |
| 1858 | if (type->tt_type == VAR_DICT) |
| 1859 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1860 | |
| 1861 | return OK; |
| 1862 | } |
| 1863 | |
| 1864 | /* |
| 1865 | * Generate an ISN_ECHO instruction. |
| 1866 | */ |
| 1867 | static int |
| 1868 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1869 | { |
| 1870 | isn_T *isn; |
| 1871 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1872 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1873 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1874 | return FAIL; |
| 1875 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1876 | isn->isn_arg.echo.echo_count = count; |
| 1877 | |
| 1878 | return OK; |
| 1879 | } |
| 1880 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1881 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1882 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1883 | */ |
| 1884 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1885 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1886 | { |
| 1887 | isn_T *isn; |
| 1888 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1889 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1890 | return FAIL; |
| 1891 | isn->isn_arg.number = count; |
| 1892 | |
| 1893 | return OK; |
| 1894 | } |
| 1895 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 1896 | /* |
| 1897 | * Generate an ISN_PUT instruction. |
| 1898 | */ |
| 1899 | static int |
| 1900 | generate_PUT(cctx_T *cctx, int regname, linenr_T lnum) |
| 1901 | { |
| 1902 | isn_T *isn; |
| 1903 | |
| 1904 | RETURN_OK_IF_SKIP(cctx); |
| 1905 | if ((isn = generate_instr(cctx, ISN_PUT)) == NULL) |
| 1906 | return FAIL; |
| 1907 | isn->isn_arg.put.put_regname = regname; |
| 1908 | isn->isn_arg.put.put_lnum = lnum; |
| 1909 | return OK; |
| 1910 | } |
| 1911 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1912 | static int |
| 1913 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1914 | { |
| 1915 | isn_T *isn; |
| 1916 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1917 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1918 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1919 | return FAIL; |
| 1920 | isn->isn_arg.string = vim_strsave(line); |
| 1921 | return OK; |
| 1922 | } |
| 1923 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1924 | static int |
| 1925 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1926 | { |
| 1927 | isn_T *isn; |
| 1928 | |
| 1929 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1930 | return FAIL; |
| 1931 | isn->isn_arg.number = count; |
| 1932 | return OK; |
| 1933 | } |
| 1934 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 1935 | /* |
| 1936 | * Generate ISN_RANGE. Consumes "range". Return OK/FAIL. |
| 1937 | */ |
| 1938 | static int |
| 1939 | generate_RANGE(cctx_T *cctx, char_u *range) |
| 1940 | { |
| 1941 | isn_T *isn; |
| 1942 | garray_T *stack = &cctx->ctx_type_stack; |
| 1943 | |
| 1944 | if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL) |
| 1945 | return FAIL; |
| 1946 | isn->isn_arg.string = range; |
| 1947 | |
| 1948 | if (ga_grow(stack, 1) == FAIL) |
| 1949 | return FAIL; |
| 1950 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_number; |
| 1951 | ++stack->ga_len; |
| 1952 | return OK; |
| 1953 | } |
| 1954 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 1955 | static int |
| 1956 | generate_UNPACK(cctx_T *cctx, int var_count, int semicolon) |
| 1957 | { |
| 1958 | isn_T *isn; |
| 1959 | |
| 1960 | RETURN_OK_IF_SKIP(cctx); |
| 1961 | if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL) |
| 1962 | return FAIL; |
| 1963 | isn->isn_arg.unpack.unp_count = var_count; |
| 1964 | isn->isn_arg.unpack.unp_semicolon = semicolon; |
| 1965 | return OK; |
| 1966 | } |
| 1967 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1968 | /* |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1969 | * Generate an instruction for any command modifiers. |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 1970 | */ |
| 1971 | static int |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 1972 | generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod) |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 1973 | { |
| 1974 | isn_T *isn; |
| 1975 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1976 | if (cmod->cmod_flags != 0 |
| 1977 | || cmod->cmod_split != 0 |
| 1978 | || cmod->cmod_verbose != 0 |
| 1979 | || cmod->cmod_tab != 0 |
| 1980 | || cmod->cmod_filter_regmatch.regprog != NULL) |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 1981 | { |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1982 | cctx->ctx_has_cmdmod = TRUE; |
| 1983 | |
| 1984 | if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL) |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 1985 | return FAIL; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1986 | isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T); |
| 1987 | if (isn->isn_arg.cmdmod.cf_cmdmod == NULL) |
| 1988 | return FAIL; |
| 1989 | mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T)); |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 1990 | // filter program now belongs to the instruction |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1991 | cmod->cmod_filter_regmatch.regprog = NULL; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 1992 | } |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1993 | |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 1994 | return OK; |
| 1995 | } |
| 1996 | |
| 1997 | static int |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1998 | generate_undo_cmdmods(cctx_T *cctx) |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 1999 | { |
Bram Moolenaar | f665e97 | 2020-12-05 19:17:16 +0100 | [diff] [blame] | 2000 | if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL) |
| 2001 | return FAIL; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 2002 | return OK; |
| 2003 | } |
| 2004 | |
| 2005 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2006 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2007 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2008 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2009 | static lvar_T * |
Bram Moolenaar | e8211a3 | 2020-10-09 22:04:29 +0200 | [diff] [blame] | 2010 | reserve_local( |
| 2011 | cctx_T *cctx, |
| 2012 | char_u *name, |
| 2013 | size_t len, |
| 2014 | int isConst, |
| 2015 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2016 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2017 | lvar_T *lvar; |
| 2018 | |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2019 | if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2020 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2021 | emsg_namelen(_(e_str_is_used_as_argument), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2022 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2026 | return NULL; |
| 2027 | 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] | 2028 | CLEAR_POINTER(lvar); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2029 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2030 | // Every local variable uses the next entry on the stack. We could re-use |
| 2031 | // the last ones when leaving a scope, but then variables used in a closure |
| 2032 | // might get overwritten. To keep things simple do not re-use stack |
| 2033 | // entries. This is less efficient, but memory is cheap these days. |
| 2034 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 2035 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 2036 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2037 | lvar->lv_const = isConst; |
| 2038 | lvar->lv_type = type; |
| 2039 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2040 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2044 | * Remove local variables above "new_top". |
| 2045 | */ |
| 2046 | static void |
| 2047 | unwind_locals(cctx_T *cctx, int new_top) |
| 2048 | { |
| 2049 | if (cctx->ctx_locals.ga_len > new_top) |
| 2050 | { |
| 2051 | int idx; |
| 2052 | lvar_T *lvar; |
| 2053 | |
| 2054 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 2055 | { |
| 2056 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 2057 | vim_free(lvar->lv_name); |
| 2058 | } |
| 2059 | } |
| 2060 | cctx->ctx_locals.ga_len = new_top; |
| 2061 | } |
| 2062 | |
| 2063 | /* |
| 2064 | * Free all local variables. |
| 2065 | */ |
| 2066 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2067 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2068 | { |
| 2069 | unwind_locals(cctx, 0); |
| 2070 | ga_clear(&cctx->ctx_locals); |
| 2071 | } |
| 2072 | |
| 2073 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2074 | * Find "name" in script-local items of script "sid". |
| 2075 | * Returns the index in "sn_var_vals" if found. |
| 2076 | * If found but not in "sn_var_vals" returns -1. |
| 2077 | * If not found returns -2. |
| 2078 | */ |
| 2079 | int |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2080 | 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] | 2081 | { |
| 2082 | hashtab_T *ht; |
| 2083 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2084 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2085 | svar_T *sv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2086 | int idx; |
| 2087 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2088 | if (!SCRIPT_ID_VALID(sid)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2089 | return -1; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2090 | if (sid == current_sctx.sc_sid) |
| 2091 | { |
Bram Moolenaar | 209f020 | 2020-10-15 13:57:56 +0200 | [diff] [blame] | 2092 | sallvar_T *sav = find_script_var(name, 0, cctx); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2093 | |
| 2094 | if (sav == NULL) |
| 2095 | return -2; |
| 2096 | idx = sav->sav_var_vals_idx; |
| 2097 | sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2098 | if (check_writable && sv->sv_const) |
| 2099 | semsg(_(e_readonlyvar), name); |
| 2100 | return idx; |
| 2101 | } |
| 2102 | |
| 2103 | // First look the name up in the hashtable. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2104 | ht = &SCRIPT_VARS(sid); |
| 2105 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 2106 | if (di == NULL) |
| 2107 | return -2; |
| 2108 | |
| 2109 | // Now find the svar_T index in sn_var_vals. |
| 2110 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 2111 | { |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2112 | sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2113 | if (sv->sv_tv == &di->di_tv) |
| 2114 | { |
| 2115 | if (check_writable && sv->sv_const) |
| 2116 | semsg(_(e_readonlyvar), name); |
| 2117 | return idx; |
| 2118 | } |
| 2119 | } |
| 2120 | return -1; |
| 2121 | } |
| 2122 | |
| 2123 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 2124 | * Find "name" in imported items of the current script or in "cctx" if not |
| 2125 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2126 | */ |
| 2127 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2128 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2129 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2130 | int idx; |
| 2131 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2132 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 2133 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2134 | if (cctx != NULL) |
| 2135 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2136 | { |
| 2137 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 2138 | + idx; |
| 2139 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2140 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2141 | : STRLEN(import->imp_name) == len |
| 2142 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2143 | return import; |
| 2144 | } |
| 2145 | |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 2146 | return find_imported_in_script(name, len, current_sctx.sc_sid); |
| 2147 | } |
| 2148 | |
| 2149 | imported_T * |
| 2150 | find_imported_in_script(char_u *name, size_t len, int sid) |
| 2151 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2152 | scriptitem_T *si; |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 2153 | int idx; |
| 2154 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2155 | if (!SCRIPT_ID_VALID(sid)) |
| 2156 | return NULL; |
| 2157 | si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2158 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 2159 | { |
| 2160 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 2161 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2162 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 2163 | : STRLEN(import->imp_name) == len |
| 2164 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2165 | return import; |
| 2166 | } |
| 2167 | return NULL; |
| 2168 | } |
| 2169 | |
| 2170 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2171 | * Free all imported variables. |
| 2172 | */ |
| 2173 | static void |
| 2174 | free_imported(cctx_T *cctx) |
| 2175 | { |
| 2176 | int idx; |
| 2177 | |
| 2178 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 2179 | { |
| 2180 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 2181 | |
| 2182 | vim_free(import->imp_name); |
| 2183 | } |
| 2184 | ga_clear(&cctx->ctx_imports); |
| 2185 | } |
| 2186 | |
| 2187 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2188 | * Return a pointer to the next line that isn't empty or only contains a |
| 2189 | * comment. Skips over white space. |
| 2190 | * Returns NULL if there is none. |
| 2191 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2192 | char_u * |
| 2193 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2194 | { |
| 2195 | int lnum = cctx->ctx_lnum; |
| 2196 | |
| 2197 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 2198 | { |
| 2199 | 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] | 2200 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2201 | |
Bram Moolenaar | ba60cc4 | 2020-08-12 19:15:33 +0200 | [diff] [blame] | 2202 | // ignore NULLs inserted for continuation lines |
| 2203 | if (line != NULL) |
| 2204 | { |
| 2205 | p = skipwhite(line); |
| 2206 | if (*p != NUL && !vim9_comment_start(p)) |
| 2207 | return p; |
| 2208 | } |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2209 | } |
| 2210 | return NULL; |
| 2211 | } |
| 2212 | |
| 2213 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2214 | * Called when checking for a following operator at "arg". When the rest of |
| 2215 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2216 | * line return a pointer to it and set "nextp". |
| 2217 | * Otherwise skip over white space. |
| 2218 | */ |
| 2219 | static char_u * |
| 2220 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2221 | { |
| 2222 | char_u *p = skipwhite(arg); |
| 2223 | |
| 2224 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2225 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2226 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2227 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2228 | if (*nextp != NULL) |
| 2229 | return *nextp; |
| 2230 | } |
| 2231 | return p; |
| 2232 | } |
| 2233 | |
| 2234 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2235 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2236 | * 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] | 2237 | * Returns NULL when at the end. |
| 2238 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2239 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2240 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2241 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2242 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2243 | |
| 2244 | do |
| 2245 | { |
| 2246 | ++cctx->ctx_lnum; |
| 2247 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2248 | { |
| 2249 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2250 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2251 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2252 | 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] | 2253 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2254 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2255 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 2256 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2257 | return line; |
| 2258 | } |
| 2259 | |
| 2260 | /* |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 2261 | * Skip over white space at "whitep" and assign to "*arg". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2262 | * 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] | 2263 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2264 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2265 | */ |
| 2266 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2267 | 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] | 2268 | { |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 2269 | *arg = skipwhite(whitep); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2270 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2271 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2272 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2273 | |
| 2274 | if (next == NULL) |
| 2275 | return FAIL; |
| 2276 | *arg = skipwhite(next); |
| 2277 | } |
| 2278 | return OK; |
| 2279 | } |
| 2280 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2281 | /* |
| 2282 | * Idem, and give an error when failed. |
| 2283 | */ |
| 2284 | static int |
| 2285 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2286 | { |
| 2287 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2288 | { |
Bram Moolenaar | 8ff16e0 | 2020-12-07 21:49:52 +0100 | [diff] [blame] | 2289 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2290 | emsg(_(e_line_incomplete)); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2291 | return FAIL; |
| 2292 | } |
| 2293 | return OK; |
| 2294 | } |
| 2295 | |
| 2296 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2297 | // Structure passed between the compile_expr* functions to keep track of |
| 2298 | // constants that have been parsed but for which no code was produced yet. If |
| 2299 | // possible expressions on these constants are applied at compile time. If |
| 2300 | // that is not possible, the code to push the constants needs to be generated |
| 2301 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2302 | // Using 50 should be more than enough of 5 levels of (). |
| 2303 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2304 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2305 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2306 | int pp_used; // active entries in pp_tv[] |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2307 | int pp_is_const; // all generated code was constants, used for a |
| 2308 | // list or dict with constant members |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2309 | } ppconst_T; |
| 2310 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2311 | 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] | 2312 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2313 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2314 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2315 | /* |
| 2316 | * Generate a PUSH instruction for "tv". |
| 2317 | * "tv" will be consumed or cleared. |
| 2318 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2319 | */ |
| 2320 | static int |
| 2321 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2322 | { |
| 2323 | if (tv != NULL) |
| 2324 | { |
| 2325 | switch (tv->v_type) |
| 2326 | { |
| 2327 | case VAR_UNKNOWN: |
| 2328 | break; |
| 2329 | case VAR_BOOL: |
| 2330 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2331 | break; |
| 2332 | case VAR_SPECIAL: |
| 2333 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2334 | break; |
| 2335 | case VAR_NUMBER: |
| 2336 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2337 | break; |
| 2338 | #ifdef FEAT_FLOAT |
| 2339 | case VAR_FLOAT: |
| 2340 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2341 | break; |
| 2342 | #endif |
| 2343 | case VAR_BLOB: |
| 2344 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2345 | tv->vval.v_blob = NULL; |
| 2346 | break; |
| 2347 | case VAR_STRING: |
| 2348 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2349 | tv->vval.v_string = NULL; |
| 2350 | break; |
| 2351 | default: |
| 2352 | iemsg("constant type not supported"); |
| 2353 | clear_tv(tv); |
| 2354 | return FAIL; |
| 2355 | } |
| 2356 | tv->v_type = VAR_UNKNOWN; |
| 2357 | } |
| 2358 | return OK; |
| 2359 | } |
| 2360 | |
| 2361 | /* |
| 2362 | * Generate code for any ppconst entries. |
| 2363 | */ |
| 2364 | static int |
| 2365 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2366 | { |
| 2367 | int i; |
| 2368 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2369 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2370 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2371 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2372 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2373 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2374 | ret = FAIL; |
| 2375 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2376 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2377 | return ret; |
| 2378 | } |
| 2379 | |
| 2380 | /* |
| 2381 | * Clear ppconst constants. Used when failing. |
| 2382 | */ |
| 2383 | static void |
| 2384 | clear_ppconst(ppconst_T *ppconst) |
| 2385 | { |
| 2386 | int i; |
| 2387 | |
| 2388 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2389 | clear_tv(&ppconst->pp_tv[i]); |
| 2390 | ppconst->pp_used = 0; |
| 2391 | } |
| 2392 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2393 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2394 | * Generate an instruction to load script-local variable "name", without the |
| 2395 | * leading "s:". |
| 2396 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2397 | */ |
| 2398 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2399 | compile_load_scriptvar( |
| 2400 | cctx_T *cctx, |
| 2401 | char_u *name, // variable NUL terminated |
| 2402 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2403 | char_u **end, // end of variable |
| 2404 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2405 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2406 | scriptitem_T *si; |
| 2407 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2408 | imported_T *import; |
| 2409 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2410 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
| 2411 | return FAIL; |
| 2412 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2413 | idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx); |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2414 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2415 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2416 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2417 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2418 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2419 | } |
| 2420 | if (idx >= 0) |
| 2421 | { |
| 2422 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2423 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2424 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2425 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2426 | return OK; |
| 2427 | } |
| 2428 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2429 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2430 | if (import != NULL) |
| 2431 | { |
Bram Moolenaar | a629495 | 2020-12-27 13:39:50 +0100 | [diff] [blame] | 2432 | if (import->imp_flags & IMP_FLAGS_STAR) |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2433 | { |
| 2434 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2435 | char_u *exp_name; |
| 2436 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2437 | ufunc_T *ufunc; |
| 2438 | type_T *type; |
| 2439 | |
| 2440 | // Used "import * as Name", need to lookup the member. |
| 2441 | if (*p != '.') |
| 2442 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2443 | semsg(_(e_expected_dot_after_name_str), start); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2444 | return FAIL; |
| 2445 | } |
| 2446 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2447 | if (VIM_ISWHITE(*p)) |
| 2448 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2449 | emsg(_(e_no_white_space_allowed_after_dot)); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2450 | return FAIL; |
| 2451 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2452 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2453 | // isolate one name |
| 2454 | exp_name = p; |
| 2455 | while (eval_isnamec(*p)) |
| 2456 | ++p; |
| 2457 | cc = *p; |
| 2458 | *p = NUL; |
| 2459 | |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2460 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2461 | *p = cc; |
| 2462 | p = skipwhite(p); |
| 2463 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2464 | // TODO: what if it is a function? |
| 2465 | if (idx < 0) |
| 2466 | return FAIL; |
| 2467 | *end = p; |
| 2468 | |
| 2469 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2470 | import->imp_sid, |
| 2471 | idx, |
| 2472 | type); |
| 2473 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2474 | else if (import->imp_funcname != NULL) |
| 2475 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2476 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2477 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2478 | import->imp_sid, |
| 2479 | import->imp_var_vals_idx, |
| 2480 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2481 | return OK; |
| 2482 | } |
| 2483 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2484 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2485 | semsg(_(e_item_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2486 | return FAIL; |
| 2487 | } |
| 2488 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2489 | static int |
| 2490 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2491 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2492 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2493 | |
| 2494 | if (ufunc == NULL) |
| 2495 | return FAIL; |
| 2496 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2497 | // Need to compile any default values to get the argument types. |
| 2498 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2499 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2500 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2501 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2502 | } |
| 2503 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2504 | /* |
| 2505 | * Compile a variable name into a load instruction. |
| 2506 | * "end" points to just after the name. |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2507 | * "is_expr" is TRUE when evaluating an expression, might be a funcref. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2508 | * When "error" is FALSE do not give an error when not found. |
| 2509 | */ |
| 2510 | static int |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2511 | compile_load( |
| 2512 | char_u **arg, |
| 2513 | char_u *end_arg, |
| 2514 | cctx_T *cctx, |
| 2515 | int is_expr, |
| 2516 | int error) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2517 | { |
| 2518 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2519 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2520 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2521 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2522 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2523 | |
| 2524 | if (*(*arg + 1) == ':') |
| 2525 | { |
| 2526 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2527 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2528 | { |
| 2529 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2530 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2531 | switch (**arg) |
| 2532 | { |
| 2533 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2534 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2535 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2536 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2537 | default: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2538 | semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2539 | goto theend; |
| 2540 | } |
| 2541 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2542 | goto theend; |
| 2543 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2544 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2545 | else |
| 2546 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2547 | isntype_T isn_type = ISN_DROP; |
| 2548 | |
| 2549 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2550 | if (name == NULL) |
| 2551 | return FAIL; |
| 2552 | |
| 2553 | switch (**arg) |
| 2554 | { |
| 2555 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2556 | break; |
| 2557 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2558 | NULL, NULL, error); |
| 2559 | break; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2560 | case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL) |
| 2561 | isn_type = ISN_LOADG; |
| 2562 | else |
| 2563 | { |
| 2564 | isn_type = ISN_LOADAUTO; |
| 2565 | vim_free(name); |
| 2566 | name = vim_strnsave(*arg, end - *arg); |
| 2567 | if (name == NULL) |
| 2568 | return FAIL; |
| 2569 | } |
| 2570 | break; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2571 | case 'w': isn_type = ISN_LOADW; break; |
| 2572 | case 't': isn_type = ISN_LOADT; break; |
| 2573 | case 'b': isn_type = ISN_LOADB; break; |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 2574 | default: // cannot happen, just in case |
| 2575 | semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2576 | goto theend; |
| 2577 | } |
| 2578 | if (isn_type != ISN_DROP) |
| 2579 | { |
| 2580 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2581 | // variables can be defined later, thus we don't check if it |
| 2582 | // exists, give error at runtime. |
| 2583 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2584 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2585 | } |
| 2586 | } |
| 2587 | else |
| 2588 | { |
| 2589 | size_t len = end - *arg; |
| 2590 | int idx; |
| 2591 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2592 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2593 | |
| 2594 | name = vim_strnsave(*arg, end - *arg); |
| 2595 | if (name == NULL) |
| 2596 | return FAIL; |
| 2597 | |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2598 | if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2599 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2600 | if (!gen_load_outer) |
| 2601 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2602 | } |
| 2603 | else |
| 2604 | { |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 2605 | lvar_T lvar; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2606 | |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 2607 | if (lookup_local(*arg, len, &lvar, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2608 | { |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 2609 | type = lvar.lv_type; |
| 2610 | idx = lvar.lv_idx; |
| 2611 | if (lvar.lv_from_outer) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2612 | gen_load_outer = TRUE; |
| 2613 | else |
| 2614 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2615 | } |
| 2616 | else |
| 2617 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2618 | // "var" can be script-local even without using "s:" if it |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2619 | // already exists in a Vim9 script or when it's imported. |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2620 | if (script_var_exists(*arg, len, TRUE, cctx) == OK |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2621 | || find_imported(name, 0, cctx) != NULL) |
| 2622 | res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2623 | |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2624 | // When evaluating an expression and the name starts with an |
| 2625 | // uppercase letter or "x:" it can be a user defined function. |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2626 | // TODO: this is just guessing |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2627 | if (res == FAIL && is_expr |
| 2628 | && (ASCII_ISUPPER(*name) || name[1] == ':')) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2629 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2630 | } |
| 2631 | } |
| 2632 | if (gen_load) |
| 2633 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2634 | if (gen_load_outer) |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2635 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2636 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2637 | cctx->ctx_outer_used = TRUE; |
| 2638 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | *arg = end; |
| 2642 | |
| 2643 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2644 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2645 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2646 | vim_free(name); |
| 2647 | return res; |
| 2648 | } |
| 2649 | |
| 2650 | /* |
| 2651 | * Compile the argument expressions. |
| 2652 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2653 | */ |
| 2654 | static int |
| 2655 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2656 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2657 | char_u *p = *arg; |
| 2658 | char_u *whitep = *arg; |
Bram Moolenaar | 10e4f12 | 2020-09-20 22:43:52 +0200 | [diff] [blame] | 2659 | int must_end = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2660 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2661 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2662 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2663 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2664 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2665 | if (*p == ')') |
| 2666 | { |
| 2667 | *arg = p + 1; |
| 2668 | return OK; |
| 2669 | } |
Bram Moolenaar | 10e4f12 | 2020-09-20 22:43:52 +0200 | [diff] [blame] | 2670 | if (must_end) |
| 2671 | { |
| 2672 | semsg(_(e_missing_comma_before_argument_str), p); |
| 2673 | return FAIL; |
| 2674 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2675 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2676 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2677 | return FAIL; |
| 2678 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2679 | |
| 2680 | if (*p != ',' && *skipwhite(p) == ',') |
| 2681 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2682 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2683 | p = skipwhite(p); |
| 2684 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2685 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2686 | { |
| 2687 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2688 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2689 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2690 | } |
Bram Moolenaar | 10e4f12 | 2020-09-20 22:43:52 +0200 | [diff] [blame] | 2691 | else |
| 2692 | must_end = TRUE; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2693 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2694 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2695 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2696 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2697 | emsg(_(e_missing_close)); |
| 2698 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | /* |
| 2702 | * Compile a function call: name(arg1, arg2) |
| 2703 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2704 | * "argcount_init" is 1 for "value->method()" |
| 2705 | * Instructions: |
| 2706 | * EVAL arg1 |
| 2707 | * EVAL arg2 |
| 2708 | * BCALL / DCALL / UCALL |
| 2709 | */ |
| 2710 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2711 | compile_call( |
| 2712 | char_u **arg, |
| 2713 | size_t varlen, |
| 2714 | cctx_T *cctx, |
| 2715 | ppconst_T *ppconst, |
| 2716 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2717 | { |
| 2718 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2719 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2720 | int argcount = argcount_init; |
| 2721 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2722 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2723 | char_u *tofree = NULL; |
| 2724 | int error = FCERR_NONE; |
Bram Moolenaar | b3a0194 | 2020-11-17 19:56:09 +0100 | [diff] [blame] | 2725 | ufunc_T *ufunc = NULL; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2726 | int res = FAIL; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2727 | int is_autoload; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2728 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2729 | // we can evaluate "has('name')" at compile time |
| 2730 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2731 | { |
| 2732 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2733 | typval_T argvars[2]; |
| 2734 | |
| 2735 | argvars[0].v_type = VAR_UNKNOWN; |
| 2736 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2737 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2738 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2739 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2740 | s = skipwhite(s); |
Bram Moolenaar | 8cebd43 | 2020-11-08 12:49:47 +0100 | [diff] [blame] | 2741 | if (*s == ')' && argvars[0].v_type == VAR_STRING |
| 2742 | && !dynamic_feature(argvars[0].vval.v_string)) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2743 | { |
| 2744 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2745 | |
| 2746 | *arg = s + 1; |
| 2747 | argvars[1].v_type = VAR_UNKNOWN; |
| 2748 | tv->v_type = VAR_NUMBER; |
| 2749 | tv->vval.v_number = 0; |
| 2750 | f_has(argvars, tv); |
| 2751 | clear_tv(&argvars[0]); |
| 2752 | ++ppconst->pp_used; |
| 2753 | return OK; |
| 2754 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2755 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2759 | return FAIL; |
| 2760 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2761 | if (varlen >= sizeof(namebuf)) |
| 2762 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2763 | semsg(_(e_name_too_long_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2764 | return FAIL; |
| 2765 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2766 | vim_strncpy(namebuf, *arg, varlen); |
| 2767 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2768 | |
| 2769 | *arg = skipwhite(*arg + varlen + 1); |
| 2770 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2771 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2772 | |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2773 | is_autoload = vim_strchr(name, AUTOLOAD_CHAR) != NULL; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2774 | if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2775 | { |
| 2776 | int idx; |
| 2777 | |
| 2778 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2779 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2780 | if (idx >= 0) |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 2781 | { |
| 2782 | if (STRCMP(name, "add") == 0 && argcount == 2) |
| 2783 | { |
| 2784 | garray_T *stack = &cctx->ctx_type_stack; |
| 2785 | type_T *type = ((type_T **)stack->ga_data)[ |
| 2786 | stack->ga_len - 2]; |
| 2787 | |
Bram Moolenaar | e88c8e8 | 2020-11-01 17:03:37 +0100 | [diff] [blame] | 2788 | // add() can be compiled to instructions if we know the type |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 2789 | if (type->tt_type == VAR_LIST) |
| 2790 | { |
| 2791 | // inline "add(list, item)" so that the type can be checked |
| 2792 | res = generate_LISTAPPEND(cctx); |
| 2793 | idx = -1; |
| 2794 | } |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 2795 | else if (type->tt_type == VAR_BLOB) |
| 2796 | { |
| 2797 | // inline "add(blob, nr)" so that the type can be checked |
| 2798 | res = generate_BLOBAPPEND(cctx); |
| 2799 | idx = -1; |
| 2800 | } |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | if (idx >= 0) |
| 2804 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
| 2805 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2806 | else |
| 2807 | semsg(_(e_unknownfunc), namebuf); |
| 2808 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2809 | } |
| 2810 | |
Bram Moolenaar | 52bf81c | 2020-11-17 18:50:44 +0100 | [diff] [blame] | 2811 | // An argument or local variable can be a function reference, this |
| 2812 | // overrules a function name. |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 2813 | if (lookup_local(namebuf, varlen, NULL, cctx) == FAIL |
Bram Moolenaar | 52bf81c | 2020-11-17 18:50:44 +0100 | [diff] [blame] | 2814 | && arg_exists(namebuf, varlen, NULL, NULL, NULL, cctx) != OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2815 | { |
Bram Moolenaar | 52bf81c | 2020-11-17 18:50:44 +0100 | [diff] [blame] | 2816 | // If we can find the function by name generate the right call. |
| 2817 | // Skip global functions here, a local funcref takes precedence. |
| 2818 | ufunc = find_func(name, FALSE, cctx); |
| 2819 | if (ufunc != NULL && !func_is_global(ufunc)) |
| 2820 | { |
| 2821 | res = generate_CALL(cctx, ufunc, argcount); |
| 2822 | goto theend; |
| 2823 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2824 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2825 | |
| 2826 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2827 | // 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] | 2828 | // Not for eome#Func(), it will be loaded later. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2829 | p = namebuf; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2830 | if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2831 | && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2832 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2833 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 2834 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2835 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2836 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2837 | goto theend; |
| 2838 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2839 | |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2840 | // If we can find a global function by name generate the right call. |
| 2841 | if (ufunc != NULL) |
| 2842 | { |
| 2843 | res = generate_CALL(cctx, ufunc, argcount); |
| 2844 | goto theend; |
| 2845 | } |
| 2846 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2847 | // 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] | 2848 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2849 | if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload) |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2850 | res = generate_UCALL(cctx, name, argcount); |
| 2851 | else |
| 2852 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2853 | |
| 2854 | theend: |
| 2855 | vim_free(tofree); |
| 2856 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2857 | } |
| 2858 | |
| 2859 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2860 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2861 | |
| 2862 | /* |
| 2863 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2864 | * does not recognize magic braces. |
Bram Moolenaar | bebaa0d | 2020-11-20 18:59:19 +0100 | [diff] [blame] | 2865 | * When "use_namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2866 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2867 | * valid name. |
| 2868 | */ |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 2869 | char_u * |
Bram Moolenaar | bebaa0d | 2020-11-20 18:59:19 +0100 | [diff] [blame] | 2870 | to_name_end(char_u *arg, int use_namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2871 | { |
| 2872 | char_u *p; |
| 2873 | |
| 2874 | // Quick check for valid starting character. |
| 2875 | if (!eval_isnamec1(*arg)) |
| 2876 | return arg; |
| 2877 | |
| 2878 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2879 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2880 | // and can be used in slice "[n:]". |
| 2881 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | bebaa0d | 2020-11-20 18:59:19 +0100 | [diff] [blame] | 2882 | || !use_namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2883 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2884 | break; |
| 2885 | return p; |
| 2886 | } |
| 2887 | |
| 2888 | /* |
| 2889 | * 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] | 2890 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2891 | */ |
| 2892 | char_u * |
| 2893 | to_name_const_end(char_u *arg) |
| 2894 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2895 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2896 | typval_T rettv; |
| 2897 | |
| 2898 | if (p == arg && *arg == '[') |
| 2899 | { |
| 2900 | |
| 2901 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2902 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2903 | p = arg; |
| 2904 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2905 | return p; |
| 2906 | } |
| 2907 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2908 | /* |
| 2909 | * parse a list: [expr, expr] |
| 2910 | * "*arg" points to the '['. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2911 | * ppconst->pp_is_const is set if all items are a constant. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2912 | */ |
| 2913 | static int |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2914 | compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2915 | { |
| 2916 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2917 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2918 | int count = 0; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2919 | int is_const; |
| 2920 | int is_all_const = TRUE; // reset when non-const encountered |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2921 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2922 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2923 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2924 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2925 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2926 | semsg(_(e_list_end), *arg); |
| 2927 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2928 | } |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2929 | if (*p == ',') |
| 2930 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2931 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2932 | return FAIL; |
| 2933 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2934 | if (*p == ']') |
| 2935 | { |
| 2936 | ++p; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2937 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2938 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2939 | if (compile_expr0_ext(&p, cctx, &is_const) == FAIL) |
Bram Moolenaar | c1f0066 | 2020-10-03 13:41:53 +0200 | [diff] [blame] | 2940 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2941 | if (!is_const) |
| 2942 | is_all_const = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2943 | ++count; |
| 2944 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2945 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2946 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2947 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 2948 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2949 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2950 | return FAIL; |
| 2951 | } |
| 2952 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2953 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2954 | p = skipwhite(p); |
| 2955 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2956 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2957 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2958 | ppconst->pp_is_const = is_all_const; |
| 2959 | return generate_NEWLIST(cctx, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | /* |
Bram Moolenaar | 2949cfd | 2020-12-31 21:28:47 +0100 | [diff] [blame] | 2963 | * Parse a lambda: "(arg, arg) => expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2964 | * "*arg" points to the '{'. |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 2965 | * 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] | 2966 | */ |
| 2967 | static int |
| 2968 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 2969 | { |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 2970 | int r; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2971 | typval_T rettv; |
| 2972 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2973 | evalarg_T evalarg; |
| 2974 | |
| 2975 | CLEAR_FIELD(evalarg); |
| 2976 | evalarg.eval_flags = EVAL_EVALUATE; |
| 2977 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2978 | |
| 2979 | // Get the funcref in "rettv". |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 2980 | r = get_lambda_tv(arg, &rettv, TRUE, &evalarg); |
| 2981 | if (r != OK) |
Bram Moolenaar | 2ea79ad | 2020-10-18 23:32:13 +0200 | [diff] [blame] | 2982 | { |
| 2983 | clear_evalarg(&evalarg, NULL); |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 2984 | return r; |
Bram Moolenaar | 2ea79ad | 2020-10-18 23:32:13 +0200 | [diff] [blame] | 2985 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2986 | |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 2987 | // "rettv" will now be a partial referencing the function. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2988 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2989 | ++ufunc->uf_refcount; |
| 2990 | clear_tv(&rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2991 | |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 2992 | // Compile the function into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2993 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2994 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 2995 | clear_evalarg(&evalarg, NULL); |
| 2996 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 2997 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 2998 | { |
| 2999 | // The return type will now be known. |
| 3000 | set_function_type(ufunc); |
| 3001 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 3002 | // The function reference count will be 1. When the ISN_FUNCREF |
| 3003 | // instruction is deleted the reference count is decremented and the |
| 3004 | // function is freed. |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 3005 | return generate_FUNCREF(cctx, ufunc); |
| 3006 | } |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3007 | |
| 3008 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3009 | return FAIL; |
| 3010 | } |
| 3011 | |
| 3012 | /* |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3013 | * parse a dict: {key: val, [key]: val} |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3014 | * "*arg" points to the '{'. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3015 | * 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] | 3016 | */ |
| 3017 | static int |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3018 | compile_dict(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3019 | { |
| 3020 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3021 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3022 | int count = 0; |
| 3023 | dict_T *d = dict_alloc(); |
| 3024 | dictitem_T *item; |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3025 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3026 | char_u *p; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3027 | int is_const; |
| 3028 | int is_all_const = TRUE; // reset when non-const encountered |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3029 | |
| 3030 | if (d == NULL) |
| 3031 | return FAIL; |
Bram Moolenaar | d62d87d | 2021-01-04 17:40:12 +0100 | [diff] [blame] | 3032 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3033 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3034 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3035 | { |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3036 | char_u *key = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3037 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3038 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3039 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3040 | *arg = NULL; |
| 3041 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | if (**arg == '}') |
| 3045 | break; |
| 3046 | |
Bram Moolenaar | c5e6a71 | 2020-12-04 19:12:14 +0100 | [diff] [blame] | 3047 | if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3048 | { |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3049 | isn_T *isn; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3050 | |
Bram Moolenaar | c5e6a71 | 2020-12-04 19:12:14 +0100 | [diff] [blame] | 3051 | // {[expr]: value} uses an evaluated key. |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3052 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3053 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3054 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3055 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 3056 | if (isn->isn_type == ISN_PUSHS) |
| 3057 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3058 | else |
| 3059 | { |
| 3060 | type_T *keytype = ((type_T **)stack->ga_data) |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3061 | [stack->ga_len - 1]; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3062 | if (need_type(keytype, &t_string, -1, cctx, |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3063 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 3064 | return FAIL; |
| 3065 | } |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3066 | *arg = skipwhite(*arg); |
| 3067 | if (**arg != ']') |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3068 | { |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3069 | emsg(_(e_missing_matching_bracket_after_dict_key)); |
| 3070 | return FAIL; |
Bram Moolenaar | 2bede17 | 2020-11-19 18:53:18 +0100 | [diff] [blame] | 3071 | } |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3072 | ++*arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3073 | } |
Bram Moolenaar | c5e6a71 | 2020-12-04 19:12:14 +0100 | [diff] [blame] | 3074 | else |
| 3075 | { |
| 3076 | // {"name": value}, |
| 3077 | // {'name': value}, |
| 3078 | // {name: value} use "name" as a literal key |
| 3079 | key = get_literal_key(arg); |
| 3080 | if (key == NULL) |
| 3081 | return FAIL; |
| 3082 | if (generate_PUSHS(cctx, key) == FAIL) |
| 3083 | return FAIL; |
| 3084 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3085 | |
| 3086 | // Check for duplicate keys, if using string keys. |
| 3087 | if (key != NULL) |
| 3088 | { |
| 3089 | item = dict_find(d, key, -1); |
| 3090 | if (item != NULL) |
| 3091 | { |
| 3092 | semsg(_(e_duplicate_key), key); |
| 3093 | goto failret; |
| 3094 | } |
| 3095 | item = dictitem_alloc(key); |
| 3096 | if (item != NULL) |
| 3097 | { |
| 3098 | item->di_tv.v_type = VAR_UNKNOWN; |
| 3099 | item->di_tv.v_lock = 0; |
| 3100 | if (dict_add(d, item) == FAIL) |
| 3101 | dictitem_free(item); |
| 3102 | } |
| 3103 | } |
| 3104 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3105 | if (**arg != ':') |
| 3106 | { |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 3107 | if (*skipwhite(*arg) == ':') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3108 | semsg(_(e_no_white_space_allowed_before_str), ":"); |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 3109 | else |
| 3110 | semsg(_(e_missing_dict_colon), *arg); |
| 3111 | return FAIL; |
| 3112 | } |
| 3113 | whitep = *arg + 1; |
| 3114 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 3115 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3116 | semsg(_(e_white_space_required_after_str), ":"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3117 | return FAIL; |
| 3118 | } |
| 3119 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3120 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3121 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3122 | *arg = NULL; |
| 3123 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3124 | } |
| 3125 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3126 | if (compile_expr0_ext(arg, cctx, &is_const) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3127 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3128 | if (!is_const) |
| 3129 | is_all_const = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3130 | ++count; |
| 3131 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3132 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3133 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3134 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3135 | *arg = NULL; |
| 3136 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3137 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3138 | if (**arg == '}') |
| 3139 | break; |
| 3140 | if (**arg != ',') |
| 3141 | { |
| 3142 | semsg(_(e_missing_dict_comma), *arg); |
| 3143 | goto failret; |
| 3144 | } |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 3145 | if (IS_WHITE_OR_NUL(*whitep)) |
| 3146 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3147 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 3148 | return FAIL; |
| 3149 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3150 | whitep = *arg + 1; |
Bram Moolenaar | 9a13e18 | 2020-10-19 21:45:07 +0200 | [diff] [blame] | 3151 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 3152 | { |
| 3153 | semsg(_(e_white_space_required_after_str), ","); |
| 3154 | return FAIL; |
| 3155 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3156 | *arg = skipwhite(*arg + 1); |
| 3157 | } |
| 3158 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3159 | *arg = *arg + 1; |
| 3160 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3161 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3162 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3163 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3164 | *arg += STRLEN(*arg); |
| 3165 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3166 | dict_unref(d); |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3167 | ppconst->pp_is_const = is_all_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3168 | return generate_NEWDICT(cctx, count); |
| 3169 | |
| 3170 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3171 | if (*arg == NULL) |
Bram Moolenaar | 44aefff | 2020-10-05 19:23:59 +0200 | [diff] [blame] | 3172 | { |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 3173 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 44aefff | 2020-10-05 19:23:59 +0200 | [diff] [blame] | 3174 | *arg = (char_u *)""; |
| 3175 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3176 | dict_unref(d); |
| 3177 | return FAIL; |
| 3178 | } |
| 3179 | |
| 3180 | /* |
| 3181 | * Compile "&option". |
| 3182 | */ |
| 3183 | static int |
| 3184 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3185 | { |
| 3186 | typval_T rettv; |
| 3187 | char_u *start = *arg; |
| 3188 | int ret; |
| 3189 | |
| 3190 | // parse the option and get the current value to get the type. |
| 3191 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3192 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3193 | if (ret == OK) |
| 3194 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3195 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | d5ea8f0 | 2021-01-01 14:49:15 +0100 | [diff] [blame] | 3196 | char_u *name = vim_strnsave(start, *arg - start); |
| 3197 | type_T *type = rettv.v_type == VAR_BOOL ? &t_bool |
| 3198 | : rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3199 | |
| 3200 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3201 | vim_free(name); |
| 3202 | } |
| 3203 | clear_tv(&rettv); |
| 3204 | |
| 3205 | return ret; |
| 3206 | } |
| 3207 | |
| 3208 | /* |
| 3209 | * Compile "$VAR". |
| 3210 | */ |
| 3211 | static int |
| 3212 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3213 | { |
| 3214 | char_u *start = *arg; |
| 3215 | int len; |
| 3216 | int ret; |
| 3217 | char_u *name; |
| 3218 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3219 | ++*arg; |
| 3220 | len = get_env_len(arg); |
| 3221 | if (len == 0) |
| 3222 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3223 | semsg(_(e_syntax_error_at_str), start - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3224 | return FAIL; |
| 3225 | } |
| 3226 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3227 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3228 | name = vim_strnsave(start, len + 1); |
| 3229 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3230 | vim_free(name); |
| 3231 | return ret; |
| 3232 | } |
| 3233 | |
| 3234 | /* |
| 3235 | * Compile "@r". |
| 3236 | */ |
| 3237 | static int |
| 3238 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3239 | { |
| 3240 | int ret; |
| 3241 | |
| 3242 | ++*arg; |
| 3243 | if (**arg == NUL) |
| 3244 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3245 | semsg(_(e_syntax_error_at_str), *arg - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3246 | return FAIL; |
| 3247 | } |
Bram Moolenaar | 7226e5b | 2020-08-02 17:33:26 +0200 | [diff] [blame] | 3248 | if (!valid_yank_reg(**arg, FALSE)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3249 | { |
| 3250 | emsg_invreg(**arg); |
| 3251 | return FAIL; |
| 3252 | } |
| 3253 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3254 | ++*arg; |
| 3255 | return ret; |
| 3256 | } |
| 3257 | |
| 3258 | /* |
| 3259 | * Apply leading '!', '-' and '+' to constant "rettv". |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3260 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3261 | */ |
| 3262 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3263 | 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] | 3264 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3265 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3266 | |
| 3267 | // this works from end to start |
| 3268 | while (p > start) |
| 3269 | { |
| 3270 | --p; |
| 3271 | if (*p == '-' || *p == '+') |
| 3272 | { |
| 3273 | // only '-' has an effect, for '+' we only check the type |
| 3274 | #ifdef FEAT_FLOAT |
| 3275 | if (rettv->v_type == VAR_FLOAT) |
| 3276 | { |
| 3277 | if (*p == '-') |
| 3278 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3279 | } |
| 3280 | else |
| 3281 | #endif |
| 3282 | { |
| 3283 | varnumber_T val; |
| 3284 | int error = FALSE; |
| 3285 | |
| 3286 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3287 | // here |
| 3288 | if (check_not_string(rettv) == FAIL) |
| 3289 | return FAIL; |
| 3290 | val = tv_get_number_chk(rettv, &error); |
| 3291 | clear_tv(rettv); |
| 3292 | if (error) |
| 3293 | return FAIL; |
| 3294 | if (*p == '-') |
| 3295 | val = -val; |
| 3296 | rettv->v_type = VAR_NUMBER; |
| 3297 | rettv->vval.v_number = val; |
| 3298 | } |
| 3299 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3300 | else if (numeric_only) |
| 3301 | { |
| 3302 | ++p; |
| 3303 | break; |
| 3304 | } |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3305 | else if (*p == '!') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3306 | { |
| 3307 | int v = tv2bool(rettv); |
| 3308 | |
| 3309 | // '!' is permissive in the type. |
| 3310 | clear_tv(rettv); |
| 3311 | rettv->v_type = VAR_BOOL; |
| 3312 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3313 | } |
| 3314 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3315 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3316 | return OK; |
| 3317 | } |
| 3318 | |
| 3319 | /* |
| 3320 | * Recognize v: variables that are constants and set "rettv". |
| 3321 | */ |
| 3322 | static void |
| 3323 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3324 | { |
| 3325 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3326 | { |
| 3327 | rettv->v_type = VAR_BOOL; |
| 3328 | rettv->vval.v_number = VVAL_TRUE; |
| 3329 | *arg += 6; |
| 3330 | } |
| 3331 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3332 | { |
| 3333 | rettv->v_type = VAR_BOOL; |
| 3334 | rettv->vval.v_number = VVAL_FALSE; |
| 3335 | *arg += 7; |
| 3336 | } |
| 3337 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3338 | { |
| 3339 | rettv->v_type = VAR_SPECIAL; |
| 3340 | rettv->vval.v_number = VVAL_NULL; |
| 3341 | *arg += 6; |
| 3342 | } |
| 3343 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3344 | { |
| 3345 | rettv->v_type = VAR_SPECIAL; |
| 3346 | rettv->vval.v_number = VVAL_NONE; |
| 3347 | *arg += 6; |
| 3348 | } |
| 3349 | } |
| 3350 | |
Bram Moolenaar | 696ba23 | 2020-07-29 21:20:41 +0200 | [diff] [blame] | 3351 | exptype_T |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3352 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3353 | { |
| 3354 | exptype_T type = EXPR_UNKNOWN; |
| 3355 | int i; |
| 3356 | |
| 3357 | switch (p[0]) |
| 3358 | { |
| 3359 | case '=': if (p[1] == '=') |
| 3360 | type = EXPR_EQUAL; |
| 3361 | else if (p[1] == '~') |
| 3362 | type = EXPR_MATCH; |
| 3363 | break; |
| 3364 | case '!': if (p[1] == '=') |
| 3365 | type = EXPR_NEQUAL; |
| 3366 | else if (p[1] == '~') |
| 3367 | type = EXPR_NOMATCH; |
| 3368 | break; |
| 3369 | case '>': if (p[1] != '=') |
| 3370 | { |
| 3371 | type = EXPR_GREATER; |
| 3372 | *len = 1; |
| 3373 | } |
| 3374 | else |
| 3375 | type = EXPR_GEQUAL; |
| 3376 | break; |
| 3377 | case '<': if (p[1] != '=') |
| 3378 | { |
| 3379 | type = EXPR_SMALLER; |
| 3380 | *len = 1; |
| 3381 | } |
| 3382 | else |
| 3383 | type = EXPR_SEQUAL; |
| 3384 | break; |
| 3385 | case 'i': if (p[1] == 's') |
| 3386 | { |
| 3387 | // "is" and "isnot"; but not a prefix of a name |
| 3388 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3389 | *len = 5; |
| 3390 | i = p[*len]; |
| 3391 | if (!isalnum(i) && i != '_') |
| 3392 | { |
| 3393 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3394 | *type_is = TRUE; |
| 3395 | } |
| 3396 | } |
| 3397 | break; |
| 3398 | } |
| 3399 | return type; |
| 3400 | } |
| 3401 | |
| 3402 | /* |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3403 | * Skip over an expression, ignoring most errors. |
| 3404 | */ |
| 3405 | static void |
| 3406 | skip_expr_cctx(char_u **arg, cctx_T *cctx) |
| 3407 | { |
| 3408 | evalarg_T evalarg; |
| 3409 | |
| 3410 | CLEAR_FIELD(evalarg); |
| 3411 | evalarg.eval_cctx = cctx; |
| 3412 | skip_expr(arg, &evalarg); |
| 3413 | } |
| 3414 | |
| 3415 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3416 | * Compile code to apply '-', '+' and '!'. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3417 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3418 | */ |
| 3419 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3420 | 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] | 3421 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3422 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3423 | |
| 3424 | // this works from end to start |
| 3425 | while (p > start) |
| 3426 | { |
| 3427 | --p; |
Bram Moolenaar | 79cdf80 | 2020-11-18 17:39:05 +0100 | [diff] [blame] | 3428 | while (VIM_ISWHITE(*p)) |
| 3429 | --p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3430 | if (*p == '-' || *p == '+') |
| 3431 | { |
| 3432 | int negate = *p == '-'; |
| 3433 | isn_T *isn; |
| 3434 | |
| 3435 | // TODO: check type |
| 3436 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3437 | { |
| 3438 | --p; |
| 3439 | if (*p == '-') |
| 3440 | negate = !negate; |
| 3441 | } |
| 3442 | // only '-' has an effect, for '+' we only check the type |
| 3443 | if (negate) |
| 3444 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3445 | else |
| 3446 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3447 | if (isn == NULL) |
| 3448 | return FAIL; |
| 3449 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3450 | else if (numeric_only) |
| 3451 | { |
| 3452 | ++p; |
| 3453 | break; |
| 3454 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3455 | else |
| 3456 | { |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3457 | int invert = *p == '!'; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3458 | |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3459 | while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1]))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3460 | { |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3461 | if (p[-1] == '!') |
| 3462 | invert = !invert; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3463 | --p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3464 | } |
| 3465 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3466 | return FAIL; |
| 3467 | } |
| 3468 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3469 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3470 | return OK; |
| 3471 | } |
| 3472 | |
| 3473 | /* |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3474 | * Compile "(expression)": recursive! |
| 3475 | * Return FAIL/OK. |
| 3476 | */ |
| 3477 | static int |
| 3478 | compile_parenthesis(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3479 | { |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3480 | int ret; |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3481 | |
| 3482 | *arg = skipwhite(*arg + 1); |
| 3483 | if (ppconst->pp_used <= PPSIZE - 10) |
| 3484 | { |
| 3485 | ret = compile_expr1(arg, cctx, ppconst); |
| 3486 | } |
| 3487 | else |
| 3488 | { |
| 3489 | // Not enough space in ppconst, flush constants. |
| 3490 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3491 | return FAIL; |
| 3492 | ret = compile_expr0(arg, cctx); |
| 3493 | } |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3494 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
| 3495 | return FAIL; |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3496 | if (**arg == ')') |
| 3497 | ++*arg; |
| 3498 | else if (ret == OK) |
| 3499 | { |
| 3500 | emsg(_(e_missing_close)); |
| 3501 | ret = FAIL; |
| 3502 | } |
| 3503 | return ret; |
| 3504 | } |
| 3505 | |
| 3506 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3507 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3508 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3509 | */ |
| 3510 | static int |
| 3511 | compile_subscript( |
| 3512 | char_u **arg, |
| 3513 | cctx_T *cctx, |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3514 | char_u *start_leader, |
| 3515 | char_u **end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3516 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3517 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3518 | char_u *name_start = *end_leader; |
| 3519 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3520 | for (;;) |
| 3521 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3522 | char_u *p = skipwhite(*arg); |
| 3523 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3524 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3525 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3526 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3527 | |
| 3528 | // If a following line starts with "->{" or "->X" advance to that |
| 3529 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3530 | // Also if a following line starts with ".x". |
| 3531 | if (next != NULL && |
| 3532 | ((next[0] == '-' && next[1] == '>' |
| 3533 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3534 | || (next[0] == '.' && eval_isdictc(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3535 | { |
| 3536 | next = next_line_from_context(cctx, TRUE); |
| 3537 | if (next == NULL) |
| 3538 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3539 | *arg = next; |
| 3540 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3541 | } |
| 3542 | } |
| 3543 | |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 3544 | // Do not skip over white space to find the "(", "execute 'x' ()" is |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 3545 | // not a function call. |
| 3546 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3547 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3548 | garray_T *stack = &cctx->ctx_type_stack; |
| 3549 | type_T *type; |
| 3550 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3551 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3552 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3553 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3554 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3555 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3556 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3557 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3558 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3559 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3560 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3561 | return FAIL; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3562 | if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3563 | return FAIL; |
| 3564 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3565 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3566 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3567 | char_u *pstart = p; |
| 3568 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3569 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3570 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3571 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3572 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3573 | // something->method() |
| 3574 | // Apply the '!', '-' and '+' first: |
| 3575 | // -1.0->func() works like (-1.0)->func() |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3576 | if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3577 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3578 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3579 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3580 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3581 | // No line break supported right after "->". |
Bram Moolenaar | 2949cfd | 2020-12-31 21:28:47 +0100 | [diff] [blame] | 3582 | if (**arg == '(') |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 3583 | { |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3584 | int argcount = 1; |
| 3585 | char_u *expr; |
| 3586 | garray_T *stack; |
| 3587 | type_T *type; |
| 3588 | |
| 3589 | // Funcref call: list->(Refs[2])(arg) |
| 3590 | // or lambda: list->((arg) => expr)(arg) |
| 3591 | // Fist compile the arguments. |
| 3592 | expr = *arg; |
| 3593 | *arg = skipwhite(*arg + 1); |
| 3594 | skip_expr_cctx(arg, cctx); |
| 3595 | *arg = skipwhite(*arg); |
| 3596 | if (**arg != ')') |
| 3597 | { |
| 3598 | semsg(_(e_missing_paren), *arg); |
| 3599 | return FAIL; |
| 3600 | } |
| 3601 | ++*arg; |
| 3602 | if (**arg != '(') |
| 3603 | { |
Bram Moolenaar | 2949cfd | 2020-12-31 21:28:47 +0100 | [diff] [blame] | 3604 | if (*skipwhite(*arg) == '(') |
| 3605 | emsg(_(e_nowhitespace)); |
| 3606 | else |
| 3607 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 3608 | return FAIL; |
| 3609 | } |
| 3610 | |
| 3611 | *arg = skipwhite(*arg + 1); |
| 3612 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3613 | return FAIL; |
| 3614 | |
| 3615 | // Compile the function expression. |
| 3616 | if (compile_parenthesis(&expr, cctx, ppconst) == FAIL) |
| 3617 | return FAIL; |
| 3618 | |
| 3619 | stack = &cctx->ctx_type_stack; |
| 3620 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3621 | if (generate_PCALL(cctx, argcount, |
| 3622 | (char_u *)"[expression]", type, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3623 | return FAIL; |
| 3624 | } |
| 3625 | else |
| 3626 | { |
| 3627 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3628 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3629 | if (!eval_isnamec1(*p)) |
| 3630 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3631 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3632 | return FAIL; |
| 3633 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3634 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3635 | p += 2; |
Bram Moolenaar | c5da1fb | 2020-08-05 15:43:44 +0200 | [diff] [blame] | 3636 | for ( ; eval_isnamec(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3637 | ; |
| 3638 | if (*p != '(') |
| 3639 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3640 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3641 | return FAIL; |
| 3642 | } |
| 3643 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3644 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3645 | return FAIL; |
| 3646 | } |
| 3647 | } |
Bram Moolenaar | badd848 | 2020-07-31 22:38:17 +0200 | [diff] [blame] | 3648 | else if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3649 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3650 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3651 | type_T **typep; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3652 | type_T *valtype; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3653 | vartype_T vtype; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3654 | int is_slice = FALSE; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3655 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3656 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3657 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3658 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3659 | // TODO: blob index |
| 3660 | // TODO: more arguments |
| 3661 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3662 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3663 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3664 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3665 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3666 | ++p; |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3667 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3668 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3669 | if (**arg == ':') |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3670 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3671 | // missing first index is equal to zero |
| 3672 | generate_PUSHNR(cctx, 0); |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3673 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3674 | else |
| 3675 | { |
| 3676 | if (compile_expr0(arg, cctx) == FAIL) |
| 3677 | return FAIL; |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3678 | if (**arg == ':') |
| 3679 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 3680 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 3681 | ":", *arg); |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3682 | return FAIL; |
| 3683 | } |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3684 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3685 | return FAIL; |
| 3686 | *arg = skipwhite(*arg); |
| 3687 | } |
| 3688 | if (**arg == ':') |
| 3689 | { |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3690 | is_slice = TRUE; |
| 3691 | ++*arg; |
| 3692 | if (!IS_WHITE_OR_NUL(**arg) && **arg != ']') |
| 3693 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 3694 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 3695 | ":", *arg); |
Bram Moolenaar | de4f95b | 2020-12-30 20:39:21 +0100 | [diff] [blame] | 3696 | return FAIL; |
| 3697 | } |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3698 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3699 | return FAIL; |
| 3700 | if (**arg == ']') |
| 3701 | // missing second index is equal to end of string |
| 3702 | generate_PUSHNR(cctx, -1); |
| 3703 | else |
| 3704 | { |
| 3705 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 3706 | return FAIL; |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 3707 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3708 | return FAIL; |
| 3709 | *arg = skipwhite(*arg); |
| 3710 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3711 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3712 | |
| 3713 | if (**arg != ']') |
| 3714 | { |
| 3715 | emsg(_(e_missbrac)); |
| 3716 | return FAIL; |
| 3717 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3718 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3719 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3720 | // We can index a list and a dict. If we don't know the type |
| 3721 | // we can use the index value type. |
| 3722 | // TODO: If we don't know use an instruction to figure it out at |
| 3723 | // runtime. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3724 | typep = ((type_T **)stack->ga_data) + stack->ga_len |
| 3725 | - (is_slice ? 3 : 2); |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3726 | vtype = (*typep)->tt_type; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3727 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3728 | // If the index is a string, the variable must be a Dict. |
| 3729 | if (*typep == &t_any && valtype == &t_string) |
| 3730 | vtype = VAR_DICT; |
| 3731 | if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB) |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3732 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3733 | if (need_type(valtype, &t_number, -1, cctx, |
| 3734 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3735 | return FAIL; |
| 3736 | if (is_slice) |
| 3737 | { |
| 3738 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3739 | if (need_type(valtype, &t_number, -2, cctx, |
| 3740 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3741 | return FAIL; |
| 3742 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3743 | } |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3744 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3745 | if (vtype == VAR_DICT) |
| 3746 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3747 | if (is_slice) |
| 3748 | { |
| 3749 | emsg(_(e_cannot_slice_dictionary)); |
| 3750 | return FAIL; |
| 3751 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3752 | if ((*typep)->tt_type == VAR_DICT) |
| 3753 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3754 | else |
| 3755 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3756 | if (need_type(*typep, &t_dict_any, -2, cctx, |
| 3757 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3758 | return FAIL; |
| 3759 | *typep = &t_any; |
| 3760 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3761 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3762 | return FAIL; |
| 3763 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3764 | return FAIL; |
| 3765 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3766 | else if (vtype == VAR_STRING) |
| 3767 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3768 | *typep = &t_string; |
| 3769 | if ((is_slice |
| 3770 | ? generate_instr_drop(cctx, ISN_STRSLICE, 2) |
| 3771 | : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL) |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3772 | return FAIL; |
| 3773 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3774 | else if (vtype == VAR_BLOB) |
| 3775 | { |
| 3776 | emsg("Sorry, blob index and slice not implemented yet"); |
| 3777 | return FAIL; |
| 3778 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3779 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3780 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3781 | if (is_slice) |
| 3782 | { |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3783 | if (generate_instr_drop(cctx, |
| 3784 | vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE, |
| 3785 | 2) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3786 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3787 | } |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3788 | else |
| 3789 | { |
| 3790 | if ((*typep)->tt_type == VAR_LIST) |
| 3791 | *typep = (*typep)->tt_member; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3792 | if (generate_instr_drop(cctx, |
| 3793 | vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, |
| 3794 | 1) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3795 | return FAIL; |
| 3796 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3797 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3798 | else |
| 3799 | { |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3800 | emsg(_(e_string_list_dict_or_blob_required)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3801 | return FAIL; |
| 3802 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3803 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3804 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3805 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3806 | // dictionary member: dict.name |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3807 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3808 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3809 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3810 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3811 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3812 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | c1f0066 | 2020-10-03 13:41:53 +0200 | [diff] [blame] | 3813 | { |
| 3814 | emsg(_(e_missing_name_after_dot)); |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3815 | return FAIL; |
Bram Moolenaar | c1f0066 | 2020-10-03 13:41:53 +0200 | [diff] [blame] | 3816 | } |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3817 | p = *arg; |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3818 | if (eval_isdictc(*p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3819 | while (eval_isnamec(*p)) |
| 3820 | MB_PTR_ADV(p); |
| 3821 | if (p == *arg) |
| 3822 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3823 | semsg(_(e_syntax_error_at_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3824 | return FAIL; |
| 3825 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3826 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3827 | return FAIL; |
| 3828 | *arg = p; |
| 3829 | } |
| 3830 | else |
| 3831 | break; |
| 3832 | } |
| 3833 | |
| 3834 | // TODO - see handle_subscript(): |
| 3835 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3836 | // Don't do this when "Func" is already a partial that was bound |
| 3837 | // explicitly (pt_auto is FALSE). |
| 3838 | |
| 3839 | return OK; |
| 3840 | } |
| 3841 | |
| 3842 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3843 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3844 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3845 | * |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3846 | * 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] | 3847 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3848 | * |
| 3849 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3850 | */ |
| 3851 | |
| 3852 | /* |
| 3853 | * number number constant |
| 3854 | * 0zFFFFFFFF Blob constant |
| 3855 | * "string" string constant |
| 3856 | * 'string' literal string constant |
| 3857 | * &option-name option value |
| 3858 | * @r register contents |
| 3859 | * identifier variable value |
| 3860 | * function() function call |
| 3861 | * $VAR environment variable |
| 3862 | * (expression) nested expression |
| 3863 | * [expr, expr] List |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 3864 | * {key: val, [key]: val} Dictionary |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3865 | * |
| 3866 | * Also handle: |
| 3867 | * ! in front logical NOT |
| 3868 | * - in front unary minus |
| 3869 | * + in front unary plus (ignored) |
| 3870 | * trailing (arg) funcref/partial call |
| 3871 | * trailing [] subscript in String or List |
| 3872 | * trailing .name entry in Dictionary |
| 3873 | * trailing ->name() method call |
| 3874 | */ |
| 3875 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3876 | compile_expr7( |
| 3877 | char_u **arg, |
| 3878 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3879 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3880 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3881 | char_u *start_leader, *end_leader; |
| 3882 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3883 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3884 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3885 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3886 | ppconst->pp_is_const = FALSE; |
| 3887 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3888 | /* |
| 3889 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3890 | */ |
| 3891 | start_leader = *arg; |
Bram Moolenaar | b23279d | 2021-01-05 22:08:20 +0100 | [diff] [blame] | 3892 | if (eval_leader(arg, TRUE) == FAIL) |
| 3893 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3894 | end_leader = *arg; |
| 3895 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3896 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3897 | switch (**arg) |
| 3898 | { |
| 3899 | /* |
| 3900 | * Number constant. |
| 3901 | */ |
| 3902 | case '0': // also for blob starting with 0z |
| 3903 | case '1': |
| 3904 | case '2': |
| 3905 | case '3': |
| 3906 | case '4': |
| 3907 | case '5': |
| 3908 | case '6': |
| 3909 | case '7': |
| 3910 | case '8': |
| 3911 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3912 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3913 | return FAIL; |
Bram Moolenaar | 4301a72 | 2020-08-11 20:51:08 +0200 | [diff] [blame] | 3914 | // Apply "-" and "+" just before the number now, right to |
| 3915 | // left. Matters especially when "->" follows. Stops at |
| 3916 | // '!'. |
| 3917 | if (apply_leader(rettv, TRUE, |
| 3918 | start_leader, &end_leader) == FAIL) |
| 3919 | { |
| 3920 | clear_tv(rettv); |
| 3921 | return FAIL; |
| 3922 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3923 | break; |
| 3924 | |
| 3925 | /* |
| 3926 | * String constant: "string". |
| 3927 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3928 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3929 | return FAIL; |
| 3930 | break; |
| 3931 | |
| 3932 | /* |
| 3933 | * Literal string constant: 'str''ing'. |
| 3934 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3935 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3936 | return FAIL; |
| 3937 | break; |
| 3938 | |
| 3939 | /* |
| 3940 | * Constant Vim variable. |
| 3941 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3942 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3943 | ret = NOTDONE; |
| 3944 | break; |
| 3945 | |
| 3946 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3947 | * "true" constant |
| 3948 | */ |
| 3949 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3950 | && !eval_isnamec((*arg)[4])) |
| 3951 | { |
| 3952 | *arg += 4; |
| 3953 | rettv->v_type = VAR_BOOL; |
| 3954 | rettv->vval.v_number = VVAL_TRUE; |
| 3955 | } |
| 3956 | else |
| 3957 | ret = NOTDONE; |
| 3958 | break; |
| 3959 | |
| 3960 | /* |
| 3961 | * "false" constant |
| 3962 | */ |
| 3963 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3964 | && !eval_isnamec((*arg)[5])) |
| 3965 | { |
| 3966 | *arg += 5; |
| 3967 | rettv->v_type = VAR_BOOL; |
| 3968 | rettv->vval.v_number = VVAL_FALSE; |
| 3969 | } |
| 3970 | else |
| 3971 | ret = NOTDONE; |
| 3972 | break; |
| 3973 | |
| 3974 | /* |
Bram Moolenaar | 6797782 | 2021-01-03 21:53:53 +0100 | [diff] [blame] | 3975 | * "null" constant |
| 3976 | */ |
| 3977 | case 'n': if (STRNCMP(*arg, "null", 4) == 0 |
| 3978 | && !eval_isnamec((*arg)[5])) |
| 3979 | { |
| 3980 | *arg += 4; |
| 3981 | rettv->v_type = VAR_SPECIAL; |
| 3982 | rettv->vval.v_number = VVAL_NULL; |
| 3983 | } |
| 3984 | else |
| 3985 | ret = NOTDONE; |
| 3986 | break; |
| 3987 | |
| 3988 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3989 | * List: [expr, expr] |
| 3990 | */ |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3991 | case '[': ret = compile_list(arg, cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3992 | break; |
| 3993 | |
| 3994 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3995 | * Dictionary: {'key': val, 'key': val} |
| 3996 | */ |
Bram Moolenaar | 2949cfd | 2020-12-31 21:28:47 +0100 | [diff] [blame] | 3997 | case '{': ret = compile_dict(arg, cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3998 | break; |
| 3999 | |
| 4000 | /* |
| 4001 | * Option value: &name |
| 4002 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 4003 | case '&': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4004 | return FAIL; |
| 4005 | ret = compile_get_option(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4006 | break; |
| 4007 | |
| 4008 | /* |
| 4009 | * Environment variable: $VAR. |
| 4010 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 4011 | case '$': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4012 | return FAIL; |
| 4013 | ret = compile_get_env(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4014 | break; |
| 4015 | |
| 4016 | /* |
| 4017 | * Register contents: @r. |
| 4018 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 4019 | case '@': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4020 | return FAIL; |
| 4021 | ret = compile_get_register(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4022 | break; |
| 4023 | /* |
| 4024 | * nested expression: (expression). |
Bram Moolenaar | 65c4415 | 2020-12-24 15:14:01 +0100 | [diff] [blame] | 4025 | * lambda: (arg, arg) => expr |
| 4026 | * funcref: (arg, arg) => { statement } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4027 | */ |
Bram Moolenaar | e462f52 | 2020-12-27 14:43:30 +0100 | [diff] [blame] | 4028 | case '(': // if compile_lambda returns NOTDONE then it must be (expr) |
| 4029 | ret = compile_lambda(arg, cctx); |
| 4030 | if (ret == NOTDONE) |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 4031 | ret = compile_parenthesis(arg, cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4032 | break; |
| 4033 | |
| 4034 | default: ret = NOTDONE; |
| 4035 | break; |
| 4036 | } |
| 4037 | if (ret == FAIL) |
| 4038 | return FAIL; |
| 4039 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 4040 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4041 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4042 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4043 | clear_tv(rettv); |
| 4044 | else |
| 4045 | // A constant expression can possibly be handled compile time, |
| 4046 | // return the value instead of generating code. |
| 4047 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4048 | } |
| 4049 | else if (ret == NOTDONE) |
| 4050 | { |
| 4051 | char_u *p; |
| 4052 | int r; |
| 4053 | |
| 4054 | if (!eval_isnamec1(**arg)) |
| 4055 | { |
Bram Moolenaar | e498429 | 2020-12-13 14:19:25 +0100 | [diff] [blame] | 4056 | if (ends_excmd(*skipwhite(*arg))) |
| 4057 | semsg(_(e_empty_expression_str), *arg); |
| 4058 | else |
| 4059 | semsg(_(e_name_expected_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4060 | return FAIL; |
| 4061 | } |
| 4062 | |
| 4063 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 4064 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4065 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4066 | { |
| 4067 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 4068 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4069 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4070 | { |
| 4071 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 4072 | return FAIL; |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 4073 | r = compile_load(arg, p, cctx, TRUE, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4074 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4075 | if (r == FAIL) |
| 4076 | return FAIL; |
| 4077 | } |
| 4078 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4079 | // Handle following "[]", ".member", etc. |
| 4080 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 4081 | if (compile_subscript(arg, cctx, start_leader, &end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4082 | ppconst) == FAIL) |
| 4083 | return FAIL; |
| 4084 | if (ppconst->pp_used > 0) |
| 4085 | { |
| 4086 | // apply the '!', '-' and '+' before the constant |
| 4087 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 4088 | if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4089 | return FAIL; |
| 4090 | return OK; |
| 4091 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 4092 | if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4093 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 4094 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | /* |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4098 | * Give the "white on both sides" error, taking the operator from "p[len]". |
| 4099 | */ |
| 4100 | void |
| 4101 | error_white_both(char_u *op, int len) |
| 4102 | { |
| 4103 | char_u buf[10]; |
| 4104 | |
| 4105 | vim_strncpy(buf, op, len); |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 4106 | 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] | 4107 | } |
| 4108 | |
| 4109 | /* |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4110 | * <type>expr7: runtime type check / conversion |
| 4111 | */ |
| 4112 | static int |
| 4113 | compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 4114 | { |
| 4115 | type_T *want_type = NULL; |
| 4116 | |
| 4117 | // Recognize <type> |
| 4118 | if (**arg == '<' && eval_isnamec1((*arg)[1])) |
| 4119 | { |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4120 | ++*arg; |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4121 | want_type = parse_type(arg, cctx->ctx_type_list, TRUE); |
| 4122 | if (want_type == NULL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4123 | return FAIL; |
| 4124 | |
| 4125 | if (**arg != '>') |
| 4126 | { |
| 4127 | if (*skipwhite(*arg) == '>') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4128 | semsg(_(e_no_white_space_allowed_before_str), ">"); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4129 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4130 | emsg(_(e_missing_gt)); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4131 | return FAIL; |
| 4132 | } |
| 4133 | ++*arg; |
Bram Moolenaar | 5afd081 | 2021-01-03 18:33:13 +0100 | [diff] [blame] | 4134 | if (may_get_next_line_error(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4135 | return FAIL; |
| 4136 | } |
| 4137 | |
| 4138 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
| 4139 | return FAIL; |
| 4140 | |
| 4141 | if (want_type != NULL) |
| 4142 | { |
| 4143 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 4144 | type_T *actual; |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4145 | |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 4146 | generate_ppconst(cctx, ppconst); |
| 4147 | actual = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 4148 | if (check_type(want_type, actual, FALSE, 0) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4149 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4150 | if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4151 | return FAIL; |
| 4152 | } |
| 4153 | } |
| 4154 | |
| 4155 | return OK; |
| 4156 | } |
| 4157 | |
| 4158 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4159 | * * number multiplication |
| 4160 | * / number division |
| 4161 | * % number modulo |
| 4162 | */ |
| 4163 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4164 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4165 | { |
| 4166 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4167 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4168 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4169 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4170 | // get the first expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4171 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4172 | return FAIL; |
| 4173 | |
| 4174 | /* |
| 4175 | * Repeat computing, until no "*", "/" or "%" is following. |
| 4176 | */ |
| 4177 | for (;;) |
| 4178 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4179 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4180 | if (*op != '*' && *op != '/' && *op != '%') |
| 4181 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4182 | if (next != NULL) |
| 4183 | { |
| 4184 | *arg = next_line_from_context(cctx, TRUE); |
| 4185 | op = skipwhite(*arg); |
| 4186 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4187 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4188 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4189 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4190 | error_white_both(op, 1); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4191 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4192 | } |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4193 | if (may_get_next_line_error(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4194 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4195 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4196 | // get the second expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 4197 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4198 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4199 | |
| 4200 | if (ppconst->pp_used == ppconst_used + 2 |
| 4201 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4202 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4203 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4204 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4205 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4206 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4207 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4208 | // both are numbers: compute the result |
| 4209 | switch (*op) |
| 4210 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4211 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4212 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4213 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4214 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4215 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4216 | break; |
| 4217 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4218 | tv1->vval.v_number = res; |
| 4219 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4220 | } |
| 4221 | else |
| 4222 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4223 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4224 | generate_two_op(cctx, op); |
| 4225 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4226 | } |
| 4227 | |
| 4228 | return OK; |
| 4229 | } |
| 4230 | |
| 4231 | /* |
| 4232 | * + number addition |
| 4233 | * - number subtraction |
| 4234 | * .. string concatenation |
| 4235 | */ |
| 4236 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4237 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4238 | { |
| 4239 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4240 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4241 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4242 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4243 | |
| 4244 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4245 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4246 | return FAIL; |
| 4247 | |
| 4248 | /* |
| 4249 | * Repeat computing, until no "+", "-" or ".." is following. |
| 4250 | */ |
| 4251 | for (;;) |
| 4252 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4253 | op = may_peek_next_line(cctx, *arg, &next); |
| 4254 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4255 | break; |
| 4256 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4257 | if (next != NULL) |
| 4258 | { |
| 4259 | *arg = next_line_from_context(cctx, TRUE); |
| 4260 | op = skipwhite(*arg); |
| 4261 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4262 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4263 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4264 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4265 | error_white_both(op, oplen); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4266 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4267 | } |
| 4268 | |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 4269 | if (may_get_next_line_error(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4270 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4271 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4272 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4273 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4274 | return FAIL; |
| 4275 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4276 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4277 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4278 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4279 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4280 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4281 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4282 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4283 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4284 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4285 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4286 | // concat/subtract/add constant numbers |
| 4287 | if (*op == '+') |
| 4288 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4289 | else if (*op == '-') |
| 4290 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4291 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4292 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4293 | // concatenate constant strings |
| 4294 | char_u *s1 = tv1->vval.v_string; |
| 4295 | char_u *s2 = tv2->vval.v_string; |
| 4296 | size_t len1 = STRLEN(s1); |
| 4297 | |
| 4298 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4299 | if (tv1->vval.v_string == NULL) |
| 4300 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4301 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4302 | return FAIL; |
| 4303 | } |
| 4304 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4305 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4306 | vim_free(s1); |
| 4307 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4308 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4309 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4310 | } |
| 4311 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4312 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4313 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4314 | if (*op == '.') |
| 4315 | { |
| 4316 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4317 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4318 | return FAIL; |
| 4319 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4320 | } |
| 4321 | else |
| 4322 | generate_two_op(cctx, op); |
| 4323 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4324 | } |
| 4325 | |
| 4326 | return OK; |
| 4327 | } |
| 4328 | |
| 4329 | /* |
| 4330 | * expr5a == expr5b |
| 4331 | * expr5a =~ expr5b |
| 4332 | * expr5a != expr5b |
| 4333 | * expr5a !~ expr5b |
| 4334 | * expr5a > expr5b |
| 4335 | * expr5a >= expr5b |
| 4336 | * expr5a < expr5b |
| 4337 | * expr5a <= expr5b |
| 4338 | * expr5a is expr5b |
| 4339 | * expr5a isnot expr5b |
| 4340 | * |
| 4341 | * Produces instructions: |
| 4342 | * EVAL expr5a Push result of "expr5a" |
| 4343 | * EVAL expr5b Push result of "expr5b" |
| 4344 | * COMPARE one of the compare instructions |
| 4345 | */ |
| 4346 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4347 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4348 | { |
| 4349 | exptype_T type = EXPR_UNKNOWN; |
| 4350 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4351 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4352 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4353 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4354 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4355 | |
| 4356 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4357 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4358 | return FAIL; |
| 4359 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4360 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4361 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4362 | |
| 4363 | /* |
| 4364 | * If there is a comparative operator, use it. |
| 4365 | */ |
| 4366 | if (type != EXPR_UNKNOWN) |
| 4367 | { |
| 4368 | int ic = FALSE; // Default: do not ignore case |
| 4369 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4370 | if (next != NULL) |
| 4371 | { |
| 4372 | *arg = next_line_from_context(cctx, TRUE); |
| 4373 | p = skipwhite(*arg); |
| 4374 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4375 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4376 | { |
| 4377 | semsg(_(e_invexpr2), *arg); |
| 4378 | return FAIL; |
| 4379 | } |
| 4380 | // extra question mark appended: ignore case |
| 4381 | if (p[len] == '?') |
| 4382 | { |
| 4383 | ic = TRUE; |
| 4384 | ++len; |
| 4385 | } |
| 4386 | // extra '#' appended: match case (ignored) |
| 4387 | else if (p[len] == '#') |
| 4388 | ++len; |
| 4389 | // nothing appended: match case |
| 4390 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4391 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4392 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4393 | error_white_both(p, len); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4394 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | // get the second variable |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4398 | if (may_get_next_line_error(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4399 | return FAIL; |
| 4400 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4401 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4402 | return FAIL; |
| 4403 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4404 | if (ppconst->pp_used == ppconst_used + 2) |
| 4405 | { |
| 4406 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4407 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4408 | int ret; |
| 4409 | |
| 4410 | // Both sides are a constant, compute the result now. |
| 4411 | // First check for a valid combination of types, this is more |
| 4412 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4413 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4414 | ret = FAIL; |
| 4415 | else |
| 4416 | { |
| 4417 | ret = typval_compare(tv1, tv2, type, ic); |
| 4418 | tv1->v_type = VAR_BOOL; |
| 4419 | tv1->vval.v_number = tv1->vval.v_number |
| 4420 | ? VVAL_TRUE : VVAL_FALSE; |
| 4421 | clear_tv(tv2); |
| 4422 | --ppconst->pp_used; |
| 4423 | } |
| 4424 | return ret; |
| 4425 | } |
| 4426 | |
| 4427 | generate_ppconst(cctx, ppconst); |
| 4428 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4429 | } |
| 4430 | |
| 4431 | return OK; |
| 4432 | } |
| 4433 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4434 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4435 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4436 | /* |
| 4437 | * Compile || or &&. |
| 4438 | */ |
| 4439 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4440 | compile_and_or( |
| 4441 | char_u **arg, |
| 4442 | cctx_T *cctx, |
| 4443 | char *op, |
| 4444 | ppconst_T *ppconst, |
| 4445 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4446 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4447 | char_u *next; |
| 4448 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4449 | int opchar = *op; |
| 4450 | |
| 4451 | if (p[0] == opchar && p[1] == opchar) |
| 4452 | { |
| 4453 | garray_T *instr = &cctx->ctx_instr; |
| 4454 | garray_T end_ga; |
| 4455 | |
| 4456 | /* |
| 4457 | * Repeat until there is no following "||" or "&&" |
| 4458 | */ |
| 4459 | ga_init2(&end_ga, sizeof(int), 10); |
| 4460 | while (p[0] == opchar && p[1] == opchar) |
| 4461 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4462 | if (next != NULL) |
| 4463 | { |
| 4464 | *arg = next_line_from_context(cctx, TRUE); |
| 4465 | p = skipwhite(*arg); |
| 4466 | } |
| 4467 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4468 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4469 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 4470 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 4471 | op, *arg); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4472 | return FAIL; |
| 4473 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4474 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4475 | // TODO: use ppconst if the value is a constant and check |
| 4476 | // evaluating to bool |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4477 | generate_ppconst(cctx, ppconst); |
| 4478 | |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 4479 | // Every part must evaluate to a bool. |
| 4480 | if (bool_on_stack(cctx) == FAIL) |
| 4481 | { |
| 4482 | ga_clear(&end_ga); |
| 4483 | return FAIL; |
| 4484 | } |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4485 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4486 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4487 | { |
| 4488 | ga_clear(&end_ga); |
| 4489 | return FAIL; |
| 4490 | } |
| 4491 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4492 | ++end_ga.ga_len; |
| 4493 | generate_JUMP(cctx, opchar == '|' |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4494 | ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4495 | |
| 4496 | // eval the next expression |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4497 | if (may_get_next_line_error(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 8bb0f54 | 2020-12-06 16:03:55 +0100 | [diff] [blame] | 4498 | { |
| 4499 | ga_clear(&end_ga); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4500 | return FAIL; |
Bram Moolenaar | 8bb0f54 | 2020-12-06 16:03:55 +0100 | [diff] [blame] | 4501 | } |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4502 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4503 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4504 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4505 | { |
| 4506 | ga_clear(&end_ga); |
| 4507 | return FAIL; |
| 4508 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4509 | |
| 4510 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4511 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4512 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4513 | |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 4514 | // Every part must evaluate to a bool. |
| 4515 | if (bool_on_stack(cctx) == FAIL) |
| 4516 | { |
| 4517 | ga_clear(&end_ga); |
| 4518 | return FAIL; |
| 4519 | } |
| 4520 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4521 | // Fill in the end label in all jumps. |
| 4522 | while (end_ga.ga_len > 0) |
| 4523 | { |
| 4524 | isn_T *isn; |
| 4525 | |
| 4526 | --end_ga.ga_len; |
| 4527 | isn = ((isn_T *)instr->ga_data) |
| 4528 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4529 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4530 | } |
| 4531 | ga_clear(&end_ga); |
| 4532 | } |
| 4533 | |
| 4534 | return OK; |
| 4535 | } |
| 4536 | |
| 4537 | /* |
| 4538 | * expr4a && expr4a && expr4a logical AND |
| 4539 | * |
| 4540 | * Produces instructions: |
| 4541 | * EVAL expr4a Push result of "expr4a" |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 4542 | * COND2BOOL convert to bool if needed |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4543 | * JUMP_IF_COND_FALSE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4544 | * EVAL expr4b Push result of "expr4b" |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4545 | * JUMP_IF_COND_FALSE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4546 | * EVAL expr4c Push result of "expr4c" |
| 4547 | * end: |
| 4548 | */ |
| 4549 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4550 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4551 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4552 | int ppconst_used = ppconst->pp_used; |
| 4553 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4554 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4555 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4556 | return FAIL; |
| 4557 | |
| 4558 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4559 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | /* |
| 4563 | * expr3a || expr3b || expr3c logical OR |
| 4564 | * |
| 4565 | * Produces instructions: |
| 4566 | * EVAL expr3a Push result of "expr3a" |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 4567 | * COND2BOOL convert to bool if needed |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4568 | * JUMP_IF_COND_TRUE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4569 | * EVAL expr3b Push result of "expr3b" |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4570 | * JUMP_IF_COND_TRUE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4571 | * EVAL expr3c Push result of "expr3c" |
| 4572 | * end: |
| 4573 | */ |
| 4574 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4575 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4576 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4577 | int ppconst_used = ppconst->pp_used; |
| 4578 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4579 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4580 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4581 | return FAIL; |
| 4582 | |
| 4583 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4584 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4585 | } |
| 4586 | |
| 4587 | /* |
| 4588 | * Toplevel expression: expr2 ? expr1a : expr1b |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4589 | * Produces instructions: |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4590 | * EVAL expr2 Push result of "expr2" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4591 | * JUMP_IF_FALSE alt jump if false |
| 4592 | * EVAL expr1a |
| 4593 | * JUMP_ALWAYS end |
| 4594 | * alt: EVAL expr1b |
| 4595 | * end: |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4596 | * |
| 4597 | * Toplevel expression: expr2 ?? expr1 |
| 4598 | * Produces instructions: |
| 4599 | * EVAL expr2 Push result of "expr2" |
| 4600 | * JUMP_AND_KEEP_IF_TRUE end jump if true |
| 4601 | * EVAL expr1 |
| 4602 | * end: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4603 | */ |
| 4604 | static int |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 4605 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4606 | { |
| 4607 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4608 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4609 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4610 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 4611 | // Ignore all kinds of errors when not producing code. |
| 4612 | if (cctx->ctx_skip == SKIP_YES) |
| 4613 | { |
Bram Moolenaar | 7e36820 | 2020-12-25 21:56:57 +0100 | [diff] [blame] | 4614 | skip_expr_cctx(arg, cctx); |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 4615 | return OK; |
| 4616 | } |
| 4617 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4618 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4619 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4620 | return FAIL; |
| 4621 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4622 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4623 | if (*p == '?') |
| 4624 | { |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4625 | int op_falsy = p[1] == '?'; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4626 | garray_T *instr = &cctx->ctx_instr; |
| 4627 | garray_T *stack = &cctx->ctx_type_stack; |
| 4628 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4629 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4630 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4631 | type_T *type1 = NULL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4632 | int has_const_expr = FALSE; |
| 4633 | int const_value = FALSE; |
| 4634 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4635 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4636 | if (next != NULL) |
| 4637 | { |
| 4638 | *arg = next_line_from_context(cctx, TRUE); |
| 4639 | p = skipwhite(*arg); |
| 4640 | } |
| 4641 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4642 | 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] | 4643 | { |
Bram Moolenaar | e7a73e0 | 2021-01-01 19:17:55 +0100 | [diff] [blame] | 4644 | semsg(_(e_white_space_required_before_and_after_str_at_str), |
| 4645 | op_falsy ? "??" : "?", *arg); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4646 | return FAIL; |
| 4647 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4648 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4649 | if (ppconst->pp_used == ppconst_used + 1) |
| 4650 | { |
| 4651 | // the condition is a constant, we know whether the ? or the : |
| 4652 | // expression is to be evaluated. |
| 4653 | has_const_expr = TRUE; |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 4654 | if (op_falsy) |
| 4655 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4656 | else |
| 4657 | { |
| 4658 | int error = FALSE; |
| 4659 | |
| 4660 | const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used], |
| 4661 | &error); |
| 4662 | if (error) |
| 4663 | return FAIL; |
| 4664 | } |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4665 | cctx->ctx_skip = save_skip == SKIP_YES || |
| 4666 | (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT; |
| 4667 | |
| 4668 | if (op_falsy && cctx->ctx_skip == SKIP_YES) |
| 4669 | // "left ?? right" and "left" is truthy: produce "left" |
| 4670 | generate_ppconst(cctx, ppconst); |
| 4671 | else |
| 4672 | { |
| 4673 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4674 | --ppconst->pp_used; |
| 4675 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4676 | } |
| 4677 | else |
| 4678 | { |
| 4679 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4680 | if (op_falsy) |
| 4681 | end_idx = instr->ga_len; |
| 4682 | generate_JUMP(cctx, op_falsy |
| 4683 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0); |
| 4684 | if (op_falsy) |
| 4685 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4686 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4687 | |
| 4688 | // evaluate the second expression; any type is accepted |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4689 | 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] | 4690 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4691 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4692 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4693 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4694 | if (!has_const_expr) |
| 4695 | { |
| 4696 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4697 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4698 | if (!op_falsy) |
| 4699 | { |
| 4700 | // remember the type and drop it |
| 4701 | --stack->ga_len; |
| 4702 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4703 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4704 | end_idx = instr->ga_len; |
| 4705 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4706 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4707 | // jump here from JUMP_IF_FALSE |
| 4708 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4709 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4710 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4711 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4712 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4713 | if (!op_falsy) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4714 | { |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4715 | // Check for the ":". |
| 4716 | p = may_peek_next_line(cctx, *arg, &next); |
| 4717 | if (*p != ':') |
| 4718 | { |
| 4719 | emsg(_(e_missing_colon)); |
| 4720 | return FAIL; |
| 4721 | } |
| 4722 | if (next != NULL) |
| 4723 | { |
| 4724 | *arg = next_line_from_context(cctx, TRUE); |
| 4725 | p = skipwhite(*arg); |
| 4726 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 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])) |
| 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 | ":", p); |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4732 | return FAIL; |
| 4733 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4734 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4735 | // evaluate the third expression |
| 4736 | if (has_const_expr) |
| 4737 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4738 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 918a424 | 2020-12-06 14:37:08 +0100 | [diff] [blame] | 4739 | if (may_get_next_line_error(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4740 | return FAIL; |
| 4741 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
| 4742 | return FAIL; |
| 4743 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4744 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4745 | if (!has_const_expr) |
| 4746 | { |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4747 | type_T **typep; |
| 4748 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4749 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4750 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4751 | // If the types differ, the result has a more generic type. |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4752 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 1; |
| 4753 | common_type(type1, *typep, typep, cctx->ctx_type_list); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4754 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4755 | // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4756 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4757 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4758 | } |
| 4759 | |
| 4760 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4761 | } |
| 4762 | return OK; |
| 4763 | } |
| 4764 | |
| 4765 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4766 | * Toplevel expression. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4767 | * Sets "is_const" (if not NULL) to indicate the value is a constant. |
| 4768 | * Returns OK or FAIL. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4769 | */ |
| 4770 | static int |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4771 | compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4772 | { |
| 4773 | ppconst_T ppconst; |
| 4774 | |
| 4775 | CLEAR_FIELD(ppconst); |
| 4776 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4777 | { |
| 4778 | clear_ppconst(&ppconst); |
| 4779 | return FAIL; |
| 4780 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4781 | if (is_const != NULL) |
| 4782 | *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4783 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4784 | return FAIL; |
| 4785 | return OK; |
| 4786 | } |
| 4787 | |
| 4788 | /* |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4789 | * Toplevel expression. |
| 4790 | */ |
| 4791 | static int |
| 4792 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4793 | { |
| 4794 | return compile_expr0_ext(arg, cctx, NULL); |
| 4795 | } |
| 4796 | |
| 4797 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4798 | * compile "return [expr]" |
| 4799 | */ |
| 4800 | static char_u * |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4801 | compile_return(char_u *arg, int check_return_type, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4802 | { |
| 4803 | char_u *p = arg; |
| 4804 | garray_T *stack = &cctx->ctx_type_stack; |
| 4805 | type_T *stack_type; |
| 4806 | |
| 4807 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4808 | { |
| 4809 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4810 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4811 | return NULL; |
| 4812 | |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4813 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4814 | { |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4815 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 6b55377 | 2020-12-31 13:31:23 +0100 | [diff] [blame] | 4816 | if (check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL |
Bram Moolenaar | 328eac2 | 2021-01-07 19:23:08 +0100 | [diff] [blame] | 4817 | || cctx->ctx_ufunc->uf_ret_type == &t_unknown |
| 4818 | || cctx->ctx_ufunc->uf_ret_type == &t_any)) |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4819 | { |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4820 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4821 | } |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4822 | else |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4823 | { |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4824 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4825 | && stack_type->tt_type != VAR_VOID |
| 4826 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4827 | { |
| 4828 | emsg(_(e_returning_value_in_function_without_return_type)); |
| 4829 | return NULL; |
| 4830 | } |
| 4831 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4832 | cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4833 | return NULL; |
| 4834 | } |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4835 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4836 | } |
| 4837 | else |
| 4838 | { |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 4839 | // "check_return_type" cannot be TRUE, only used for a lambda which |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4840 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4841 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4842 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4843 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4844 | emsg(_(e_missing_return_value)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4845 | return NULL; |
| 4846 | } |
| 4847 | |
| 4848 | // No argument, return zero. |
| 4849 | generate_PUSHNR(cctx, 0); |
| 4850 | } |
Bram Moolenaar | 8e02faf | 2020-11-18 16:35:02 +0100 | [diff] [blame] | 4851 | if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4852 | return NULL; |
| 4853 | |
| 4854 | // "return val | endif" is possible |
| 4855 | return skipwhite(p); |
| 4856 | } |
| 4857 | |
| 4858 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4859 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4860 | * Return a pointer to the line in allocated memory. |
| 4861 | * Return NULL for end-of-file or some error. |
| 4862 | */ |
| 4863 | static char_u * |
| 4864 | exarg_getline( |
| 4865 | int c UNUSED, |
| 4866 | void *cookie, |
| 4867 | int indent UNUSED, |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4868 | getline_opt_T options UNUSED) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4869 | { |
| 4870 | cctx_T *cctx = (cctx_T *)cookie; |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4871 | char_u *p; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4872 | |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4873 | for (;;) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4874 | { |
Bram Moolenaar | 2914a20 | 2020-09-27 18:24:03 +0200 | [diff] [blame] | 4875 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1) |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4876 | return NULL; |
| 4877 | ++cctx->ctx_lnum; |
| 4878 | p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]; |
| 4879 | // Comment lines result in NULL pointers, skip them. |
| 4880 | if (p != NULL) |
| 4881 | return vim_strsave(p); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4882 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4883 | } |
| 4884 | |
| 4885 | /* |
| 4886 | * Compile a nested :def command. |
| 4887 | */ |
| 4888 | static char_u * |
| 4889 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4890 | { |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4891 | int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4892 | char_u *name_start = eap->arg; |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4893 | char_u *name_end = to_name_end(eap->arg, TRUE); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4894 | char_u *lambda_name; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4895 | ufunc_T *ufunc; |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 4896 | int r = FAIL; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4897 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 4898 | if (eap->forceit) |
Bram Moolenaar | 8b848ca | 2020-09-10 22:28:01 +0200 | [diff] [blame] | 4899 | { |
| 4900 | emsg(_(e_cannot_use_bang_with_nested_def)); |
| 4901 | return NULL; |
| 4902 | } |
| 4903 | |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 4904 | if (*name_start == '/') |
| 4905 | { |
| 4906 | name_end = skip_regexp(name_start + 1, '/', TRUE); |
| 4907 | if (*name_end == '/') |
| 4908 | ++name_end; |
| 4909 | eap->nextcmd = check_nextcmd(name_end); |
| 4910 | } |
| 4911 | if (name_end == name_start || *skipwhite(name_end) != '(') |
| 4912 | { |
| 4913 | if (!ends_excmd2(name_start, name_end)) |
| 4914 | { |
| 4915 | semsg(_(e_invalid_command_str), eap->cmd); |
| 4916 | return NULL; |
| 4917 | } |
| 4918 | |
| 4919 | // "def" or "def Name": list functions |
| 4920 | if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL) |
| 4921 | return NULL; |
| 4922 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 4923 | } |
| 4924 | |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4925 | // Only g:Func() can use a namespace. |
| 4926 | if (name_start[1] == ':' && !is_global) |
| 4927 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4928 | semsg(_(e_namespace_not_supported_str), name_start); |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4929 | return NULL; |
| 4930 | } |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4931 | if (check_defined(name_start, name_end - name_start, cctx) == FAIL) |
| 4932 | return NULL; |
| 4933 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4934 | eap->arg = name_end; |
| 4935 | eap->getline = exarg_getline; |
| 4936 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4937 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4938 | eap->forceit = FALSE; |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 4939 | lambda_name = vim_strsave(get_lambda_name()); |
| 4940 | if (lambda_name == NULL) |
| 4941 | return NULL; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 4942 | ufunc = define_function(eap, lambda_name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4943 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4944 | if (ufunc == NULL) |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 4945 | { |
| 4946 | r = eap->skip ? OK : FAIL; |
| 4947 | goto theend; |
| 4948 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4949 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4950 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 4ee711f | 2020-09-23 18:51:11 +0200 | [diff] [blame] | 4951 | { |
| 4952 | func_ptr_unref(ufunc); |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 4953 | goto theend; |
Bram Moolenaar | 4ee711f | 2020-09-23 18:51:11 +0200 | [diff] [blame] | 4954 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4955 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4956 | if (is_global) |
| 4957 | { |
| 4958 | char_u *func_name = vim_strnsave(name_start + 2, |
| 4959 | name_end - name_start - 2); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4960 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4961 | if (func_name == NULL) |
| 4962 | r = FAIL; |
| 4963 | else |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 4964 | { |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4965 | r = generate_NEWFUNC(cctx, lambda_name, func_name); |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 4966 | lambda_name = NULL; |
| 4967 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4968 | } |
| 4969 | else |
| 4970 | { |
| 4971 | // Define a local variable for the function reference. |
Bram Moolenaar | e8211a3 | 2020-10-09 22:04:29 +0200 | [diff] [blame] | 4972 | lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4973 | TRUE, ufunc->uf_func_type); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 4974 | int block_depth = cctx->ctx_ufunc->uf_block_depth; |
Bram Moolenaar | e8211a3 | 2020-10-09 22:04:29 +0200 | [diff] [blame] | 4975 | |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4976 | if (lvar == NULL) |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 4977 | goto theend; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 4978 | if (generate_FUNCREF(cctx, ufunc) == FAIL) |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 4979 | goto theend; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4980 | r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 4981 | |
| 4982 | // copy over the block scope IDs |
| 4983 | if (block_depth > 0) |
| 4984 | { |
| 4985 | ufunc->uf_block_ids = ALLOC_MULT(int, block_depth); |
| 4986 | if (ufunc->uf_block_ids != NULL) |
| 4987 | { |
| 4988 | mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids, |
| 4989 | sizeof(int) * block_depth); |
| 4990 | ufunc->uf_block_depth = block_depth; |
| 4991 | } |
| 4992 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4993 | } |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4994 | // TODO: warning for trailing text? |
Bram Moolenaar | 58a52f2 | 2020-12-22 18:56:55 +0100 | [diff] [blame] | 4995 | r = OK; |
| 4996 | |
| 4997 | theend: |
| 4998 | vim_free(lambda_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4999 | return r == FAIL ? NULL : (char_u *)""; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5003 | * Return the length of an assignment operator, or zero if there isn't one. |
| 5004 | */ |
| 5005 | int |
| 5006 | assignment_len(char_u *p, int *heredoc) |
| 5007 | { |
| 5008 | if (*p == '=') |
| 5009 | { |
| 5010 | if (p[1] == '<' && p[2] == '<') |
| 5011 | { |
| 5012 | *heredoc = TRUE; |
| 5013 | return 3; |
| 5014 | } |
| 5015 | return 1; |
| 5016 | } |
| 5017 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 5018 | return 2; |
| 5019 | if (STRNCMP(p, "..=", 3) == 0) |
| 5020 | return 3; |
| 5021 | return 0; |
| 5022 | } |
| 5023 | |
| 5024 | // words that cannot be used as a variable |
| 5025 | static char *reserved[] = { |
| 5026 | "true", |
| 5027 | "false", |
Bram Moolenaar | 6797782 | 2021-01-03 21:53:53 +0100 | [diff] [blame] | 5028 | "null", |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5029 | NULL |
| 5030 | }; |
| 5031 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5032 | // Destination for an assignment or ":unlet" with an index. |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5033 | typedef enum { |
| 5034 | dest_local, |
| 5035 | dest_option, |
| 5036 | dest_env, |
| 5037 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 5038 | dest_buffer, |
| 5039 | dest_window, |
| 5040 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5041 | dest_vimvar, |
| 5042 | dest_script, |
| 5043 | dest_reg, |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5044 | dest_expr, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 5045 | } assign_dest_T; |
| 5046 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5047 | // Used by compile_lhs() to store information about the LHS of an assignment |
| 5048 | // and one argument of ":unlet" with an index. |
| 5049 | typedef struct { |
| 5050 | assign_dest_T lhs_dest; // type of destination |
| 5051 | |
| 5052 | char_u *lhs_name; // allocated name including |
| 5053 | // "[expr]" or ".name". |
| 5054 | size_t lhs_varlen; // length of the variable without |
| 5055 | // "[expr]" or ".name" |
| 5056 | char_u *lhs_dest_end; // end of the destination, including |
| 5057 | // "[expr]" or ".name". |
| 5058 | |
| 5059 | int lhs_has_index; // has "[expr]" or ".name" |
| 5060 | |
| 5061 | int lhs_new_local; // create new local variable |
| 5062 | int lhs_opt_flags; // for when destination is an option |
| 5063 | int lhs_vimvaridx; // for when destination is a v:var |
| 5064 | |
| 5065 | lvar_T lhs_local_lvar; // used for existing local destination |
| 5066 | lvar_T lhs_arg_lvar; // used for argument destination |
| 5067 | lvar_T *lhs_lvar; // points to destination lvar |
| 5068 | int lhs_scriptvar_sid; |
| 5069 | int lhs_scriptvar_idx; |
| 5070 | |
| 5071 | int lhs_has_type; // type was specified |
| 5072 | type_T *lhs_type; |
| 5073 | type_T *lhs_member_type; |
| 5074 | } lhs_T; |
| 5075 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5076 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5077 | * Generate the load instruction for "name". |
| 5078 | */ |
| 5079 | static void |
| 5080 | generate_loadvar( |
| 5081 | cctx_T *cctx, |
| 5082 | assign_dest_T dest, |
| 5083 | char_u *name, |
| 5084 | lvar_T *lvar, |
| 5085 | type_T *type) |
| 5086 | { |
| 5087 | switch (dest) |
| 5088 | { |
| 5089 | case dest_option: |
| 5090 | // TODO: check the option exists |
| 5091 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 5092 | break; |
| 5093 | case dest_global: |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 5094 | if (vim_strchr(name, AUTOLOAD_CHAR) == NULL) |
| 5095 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 5096 | else |
| 5097 | generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5098 | break; |
| 5099 | case dest_buffer: |
| 5100 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 5101 | break; |
| 5102 | case dest_window: |
| 5103 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 5104 | break; |
| 5105 | case dest_tab: |
| 5106 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 5107 | break; |
| 5108 | case dest_script: |
| 5109 | compile_load_scriptvar(cctx, |
| 5110 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 5111 | break; |
| 5112 | case dest_env: |
| 5113 | // Include $ in the name here |
| 5114 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 5115 | break; |
| 5116 | case dest_reg: |
| 5117 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 5118 | break; |
| 5119 | case dest_vimvar: |
| 5120 | generate_LOADV(cctx, name + 2, TRUE); |
| 5121 | break; |
| 5122 | case dest_local: |
| 5123 | if (lvar->lv_from_outer) |
| 5124 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 5125 | NULL, type); |
| 5126 | else |
| 5127 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 5128 | break; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5129 | case dest_expr: |
| 5130 | // list or dict value should already be on the stack. |
| 5131 | break; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5132 | } |
| 5133 | } |
| 5134 | |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5135 | /* |
| 5136 | * Skip over "[expr]" or ".member". |
| 5137 | * Does not check for any errors. |
| 5138 | */ |
| 5139 | static char_u * |
| 5140 | skip_index(char_u *start) |
| 5141 | { |
| 5142 | char_u *p = start; |
| 5143 | |
| 5144 | if (*p == '[') |
| 5145 | { |
| 5146 | p = skipwhite(p + 1); |
| 5147 | (void)skip_expr(&p, NULL); |
| 5148 | p = skipwhite(p); |
| 5149 | if (*p == ']') |
| 5150 | return p + 1; |
| 5151 | return p; |
| 5152 | } |
| 5153 | // if (*p == '.') |
| 5154 | return to_name_end(p + 1, TRUE); |
| 5155 | } |
| 5156 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5157 | void |
| 5158 | vim9_declare_error(char_u *name) |
| 5159 | { |
| 5160 | char *scope = ""; |
| 5161 | |
| 5162 | switch (*name) |
| 5163 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 5164 | case 'g': scope = _("global"); break; |
| 5165 | case 'b': scope = _("buffer"); break; |
| 5166 | case 'w': scope = _("window"); break; |
| 5167 | case 't': scope = _("tab"); break; |
| 5168 | case 'v': scope = "v:"; break; |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 5169 | case '$': semsg(_(e_cannot_declare_an_environment_variable), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5170 | return; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5171 | case '&': semsg(_(e_cannot_declare_an_option), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5172 | return; |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 5173 | case '@': semsg(_(e_cannot_declare_a_register_str), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5174 | return; |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 5175 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5176 | } |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 5177 | semsg(_(e_cannot_declare_a_scope_variable), scope, name); |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 5178 | } |
| 5179 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5180 | /* |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5181 | * For one assignment figure out the type of destination. Return it in "dest". |
| 5182 | * When not recognized "dest" is not set. |
| 5183 | * For an option "opt_flags" is set. |
| 5184 | * For a v:var "vimvaridx" is set. |
| 5185 | * "type" is set to the destination type if known, unchanted otherwise. |
| 5186 | * Return FAIL if an error message was given. |
| 5187 | */ |
| 5188 | static int |
| 5189 | get_var_dest( |
| 5190 | char_u *name, |
| 5191 | assign_dest_T *dest, |
| 5192 | int cmdidx, |
| 5193 | int *opt_flags, |
| 5194 | int *vimvaridx, |
| 5195 | type_T **type, |
| 5196 | cctx_T *cctx) |
| 5197 | { |
| 5198 | char_u *p; |
| 5199 | |
| 5200 | if (*name == '&') |
| 5201 | { |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5202 | int cc; |
| 5203 | long numval; |
| 5204 | getoption_T opt_type; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5205 | |
| 5206 | *dest = dest_option; |
| 5207 | if (cmdidx == CMD_final || cmdidx == CMD_const) |
| 5208 | { |
| 5209 | emsg(_(e_const_option)); |
| 5210 | return FAIL; |
| 5211 | } |
| 5212 | p = name; |
| 5213 | p = find_option_end(&p, opt_flags); |
| 5214 | if (p == NULL) |
| 5215 | { |
| 5216 | // cannot happen? |
| 5217 | emsg(_(e_letunexp)); |
| 5218 | return FAIL; |
| 5219 | } |
| 5220 | cc = *p; |
| 5221 | *p = NUL; |
| 5222 | opt_type = get_option_value(skip_option_env_lead(name), |
| 5223 | &numval, NULL, *opt_flags); |
| 5224 | *p = cc; |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5225 | switch (opt_type) |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5226 | { |
Bram Moolenaar | dd1f426 | 2020-12-31 17:41:01 +0100 | [diff] [blame] | 5227 | case gov_unknown: |
| 5228 | semsg(_(e_unknown_option), name); |
| 5229 | return FAIL; |
| 5230 | case gov_string: |
| 5231 | case gov_hidden_string: |
| 5232 | *type = &t_string; |
| 5233 | break; |
| 5234 | case gov_bool: |
| 5235 | case gov_hidden_bool: |
| 5236 | *type = &t_bool; |
| 5237 | break; |
| 5238 | case gov_number: |
| 5239 | case gov_hidden_number: |
| 5240 | *type = &t_number; |
| 5241 | break; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5242 | } |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5243 | } |
| 5244 | else if (*name == '$') |
| 5245 | { |
| 5246 | *dest = dest_env; |
| 5247 | *type = &t_string; |
| 5248 | } |
| 5249 | else if (*name == '@') |
| 5250 | { |
| 5251 | if (!valid_yank_reg(name[1], FALSE) || name[1] == '.') |
| 5252 | { |
| 5253 | emsg_invreg(name[1]); |
| 5254 | return FAIL; |
| 5255 | } |
| 5256 | *dest = dest_reg; |
| 5257 | *type = &t_string; |
| 5258 | } |
| 5259 | else if (STRNCMP(name, "g:", 2) == 0) |
| 5260 | { |
| 5261 | *dest = dest_global; |
| 5262 | } |
| 5263 | else if (STRNCMP(name, "b:", 2) == 0) |
| 5264 | { |
| 5265 | *dest = dest_buffer; |
| 5266 | } |
| 5267 | else if (STRNCMP(name, "w:", 2) == 0) |
| 5268 | { |
| 5269 | *dest = dest_window; |
| 5270 | } |
| 5271 | else if (STRNCMP(name, "t:", 2) == 0) |
| 5272 | { |
| 5273 | *dest = dest_tab; |
| 5274 | } |
| 5275 | else if (STRNCMP(name, "v:", 2) == 0) |
| 5276 | { |
| 5277 | typval_T *vtv; |
| 5278 | int di_flags; |
| 5279 | |
| 5280 | *vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5281 | if (*vimvaridx < 0) |
| 5282 | { |
| 5283 | semsg(_(e_variable_not_found_str), name); |
| 5284 | return FAIL; |
| 5285 | } |
| 5286 | // We use the current value of "sandbox" here, is that OK? |
| 5287 | if (var_check_ro(di_flags, name, FALSE)) |
| 5288 | return FAIL; |
| 5289 | *dest = dest_vimvar; |
| 5290 | vtv = get_vim_var_tv(*vimvaridx); |
| 5291 | *type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
| 5292 | } |
| 5293 | return OK; |
| 5294 | } |
| 5295 | |
| 5296 | /* |
| 5297 | * Generate a STORE instruction for "dest", not being "dest_local". |
| 5298 | * Return FAIL when out of memory. |
| 5299 | */ |
| 5300 | static int |
| 5301 | generate_store_var( |
| 5302 | cctx_T *cctx, |
| 5303 | assign_dest_T dest, |
| 5304 | int opt_flags, |
| 5305 | int vimvaridx, |
| 5306 | int scriptvar_idx, |
| 5307 | int scriptvar_sid, |
| 5308 | type_T *type, |
| 5309 | char_u *name) |
| 5310 | { |
| 5311 | switch (dest) |
| 5312 | { |
| 5313 | case dest_option: |
| 5314 | return generate_STOREOPT(cctx, skip_option_env_lead(name), |
| 5315 | opt_flags); |
| 5316 | case dest_global: |
| 5317 | // include g: with the name, easier to execute that way |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 5318 | return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL |
| 5319 | ? ISN_STOREG : ISN_STOREAUTO, 0, name); |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5320 | case dest_buffer: |
| 5321 | // include b: with the name, easier to execute that way |
| 5322 | return generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5323 | case dest_window: |
| 5324 | // include w: with the name, easier to execute that way |
| 5325 | return generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5326 | case dest_tab: |
| 5327 | // include t: with the name, easier to execute that way |
| 5328 | return generate_STORE(cctx, ISN_STORET, 0, name); |
| 5329 | case dest_env: |
| 5330 | return generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5331 | case dest_reg: |
| 5332 | return generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5333 | case dest_vimvar: |
| 5334 | return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5335 | case dest_script: |
| 5336 | if (scriptvar_idx < 0) |
| 5337 | { |
| 5338 | char_u *name_s = name; |
Bram Moolenaar | 41d6196 | 2020-12-06 20:12:43 +0100 | [diff] [blame] | 5339 | int r; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5340 | |
Bram Moolenaar | 41d6196 | 2020-12-06 20:12:43 +0100 | [diff] [blame] | 5341 | // "s:" is included in the name. |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5342 | r = generate_OLDSCRIPT(cctx, ISN_STORES, name_s, |
| 5343 | scriptvar_sid, type); |
| 5344 | if (name_s != name) |
| 5345 | vim_free(name_s); |
| 5346 | return r; |
| 5347 | } |
| 5348 | return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
| 5349 | scriptvar_sid, scriptvar_idx, type); |
| 5350 | case dest_local: |
| 5351 | case dest_expr: |
| 5352 | // cannot happen |
| 5353 | break; |
| 5354 | } |
| 5355 | return FAIL; |
| 5356 | } |
| 5357 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5358 | static int |
| 5359 | is_decl_command(int cmdidx) |
| 5360 | { |
| 5361 | return cmdidx == CMD_let || cmdidx == CMD_var |
| 5362 | || cmdidx == CMD_final || cmdidx == CMD_const; |
| 5363 | } |
| 5364 | |
| 5365 | /* |
| 5366 | * Figure out the LHS type and other properties for an assignment or one item |
| 5367 | * of ":unlet" with an index. |
| 5368 | * Returns OK or FAIL. |
| 5369 | */ |
| 5370 | static int |
| 5371 | compile_lhs( |
| 5372 | char_u *var_start, |
| 5373 | lhs_T *lhs, |
| 5374 | int cmdidx, |
| 5375 | int heredoc, |
| 5376 | int oplen, |
| 5377 | cctx_T *cctx) |
| 5378 | { |
| 5379 | char_u *var_end; |
| 5380 | int is_decl = is_decl_command(cmdidx); |
| 5381 | |
| 5382 | CLEAR_POINTER(lhs); |
| 5383 | lhs->lhs_dest = dest_local; |
| 5384 | lhs->lhs_vimvaridx = -1; |
| 5385 | lhs->lhs_scriptvar_idx = -1; |
| 5386 | |
| 5387 | // "dest_end" is the end of the destination, including "[expr]" or |
| 5388 | // ".name". |
| 5389 | // "var_end" is the end of the variable/option/etc. name. |
| 5390 | lhs->lhs_dest_end = skip_var_one(var_start, FALSE); |
| 5391 | if (*var_start == '@') |
| 5392 | var_end = var_start + 2; |
| 5393 | else |
| 5394 | { |
| 5395 | // skip over the leading "&", "&l:", "&g:" and "$" |
| 5396 | var_end = skip_option_env_lead(var_start); |
| 5397 | var_end = to_name_end(var_end, TRUE); |
| 5398 | } |
| 5399 | |
| 5400 | // "a: type" is declaring variable "a" with a type, not dict "a:". |
| 5401 | if (is_decl && lhs->lhs_dest_end == var_start + 2 |
| 5402 | && lhs->lhs_dest_end[-1] == ':') |
| 5403 | --lhs->lhs_dest_end; |
| 5404 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 5405 | --var_end; |
| 5406 | |
| 5407 | // compute the length of the destination without "[expr]" or ".name" |
| 5408 | lhs->lhs_varlen = var_end - var_start; |
| 5409 | lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen); |
| 5410 | if (lhs->lhs_name == NULL) |
| 5411 | return FAIL; |
| 5412 | if (heredoc) |
| 5413 | lhs->lhs_type = &t_list_string; |
| 5414 | else |
| 5415 | lhs->lhs_type = &t_any; |
| 5416 | |
| 5417 | if (cctx->ctx_skip != SKIP_YES) |
| 5418 | { |
| 5419 | int declare_error = FALSE; |
| 5420 | |
| 5421 | if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx, |
| 5422 | &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx, |
| 5423 | &lhs->lhs_type, cctx) == FAIL) |
| 5424 | return FAIL; |
| 5425 | if (lhs->lhs_dest != dest_local) |
| 5426 | { |
| 5427 | // Specific kind of variable recognized. |
| 5428 | declare_error = is_decl; |
| 5429 | } |
| 5430 | else |
| 5431 | { |
| 5432 | int idx; |
| 5433 | |
| 5434 | // No specific kind of variable recognized, just a name. |
| 5435 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5436 | if (STRCMP(reserved[idx], lhs->lhs_name) == 0) |
| 5437 | { |
| 5438 | semsg(_(e_cannot_use_reserved_name), lhs->lhs_name); |
| 5439 | return FAIL; |
| 5440 | } |
| 5441 | |
| 5442 | |
| 5443 | if (lookup_local(var_start, lhs->lhs_varlen, |
| 5444 | &lhs->lhs_local_lvar, cctx) == OK) |
| 5445 | lhs->lhs_lvar = &lhs->lhs_local_lvar; |
| 5446 | else |
| 5447 | { |
| 5448 | CLEAR_FIELD(lhs->lhs_arg_lvar); |
| 5449 | if (arg_exists(var_start, lhs->lhs_varlen, |
| 5450 | &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type, |
| 5451 | &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK) |
| 5452 | { |
| 5453 | if (is_decl) |
| 5454 | { |
| 5455 | semsg(_(e_str_is_used_as_argument), lhs->lhs_name); |
| 5456 | return FAIL; |
| 5457 | } |
| 5458 | lhs->lhs_lvar = &lhs->lhs_arg_lvar; |
| 5459 | } |
| 5460 | } |
| 5461 | if (lhs->lhs_lvar != NULL) |
| 5462 | { |
| 5463 | if (is_decl) |
| 5464 | { |
| 5465 | semsg(_(e_variable_already_declared), lhs->lhs_name); |
| 5466 | return FAIL; |
| 5467 | } |
| 5468 | } |
| 5469 | else |
| 5470 | { |
| 5471 | int script_namespace = lhs->lhs_varlen > 1 |
| 5472 | && STRNCMP(var_start, "s:", 2) == 0; |
| 5473 | int script_var = (script_namespace |
| 5474 | ? script_var_exists(var_start + 2, lhs->lhs_varlen - 2, |
| 5475 | FALSE, cctx) |
| 5476 | : script_var_exists(var_start, lhs->lhs_varlen, |
| 5477 | TRUE, cctx)) == OK; |
| 5478 | imported_T *import = |
| 5479 | find_imported(var_start, lhs->lhs_varlen, cctx); |
| 5480 | |
| 5481 | if (script_namespace || script_var || import != NULL) |
| 5482 | { |
| 5483 | char_u *rawname = lhs->lhs_name |
| 5484 | + (lhs->lhs_name[1] == ':' ? 2 : 0); |
| 5485 | |
| 5486 | if (is_decl) |
| 5487 | { |
| 5488 | if (script_namespace) |
| 5489 | semsg(_(e_cannot_declare_script_variable_in_function), |
| 5490 | lhs->lhs_name); |
| 5491 | else |
| 5492 | semsg(_(e_variable_already_declared_in_script), |
| 5493 | lhs->lhs_name); |
| 5494 | return FAIL; |
| 5495 | } |
| 5496 | else if (cctx->ctx_ufunc->uf_script_ctx_version |
| 5497 | == SCRIPT_VERSION_VIM9 |
| 5498 | && script_namespace |
| 5499 | && !script_var && import == NULL) |
| 5500 | { |
| 5501 | semsg(_(e_unknown_variable_str), lhs->lhs_name); |
| 5502 | return FAIL; |
| 5503 | } |
| 5504 | |
| 5505 | lhs->lhs_dest = dest_script; |
| 5506 | |
| 5507 | // existing script-local variables should have a type |
| 5508 | lhs->lhs_scriptvar_sid = current_sctx.sc_sid; |
| 5509 | if (import != NULL) |
| 5510 | lhs->lhs_scriptvar_sid = import->imp_sid; |
| 5511 | if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid)) |
| 5512 | { |
| 5513 | lhs->lhs_scriptvar_idx = get_script_item_idx( |
| 5514 | lhs->lhs_scriptvar_sid, |
| 5515 | rawname, TRUE, cctx); |
| 5516 | if (lhs->lhs_scriptvar_idx >= 0) |
| 5517 | { |
| 5518 | scriptitem_T *si = SCRIPT_ITEM( |
| 5519 | lhs->lhs_scriptvar_sid); |
| 5520 | svar_T *sv = |
| 5521 | ((svar_T *)si->sn_var_vals.ga_data) |
| 5522 | + lhs->lhs_scriptvar_idx; |
| 5523 | lhs->lhs_type = sv->sv_type; |
| 5524 | } |
| 5525 | } |
| 5526 | } |
| 5527 | else if (check_defined(var_start, lhs->lhs_varlen, cctx) |
| 5528 | == FAIL) |
| 5529 | return FAIL; |
| 5530 | } |
| 5531 | } |
| 5532 | |
| 5533 | if (declare_error) |
| 5534 | { |
| 5535 | vim9_declare_error(lhs->lhs_name); |
| 5536 | return FAIL; |
| 5537 | } |
| 5538 | } |
| 5539 | |
| 5540 | // handle "a:name" as a name, not index "name" on "a" |
| 5541 | if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':') |
| 5542 | var_end = lhs->lhs_dest_end; |
| 5543 | |
| 5544 | if (lhs->lhs_dest != dest_option) |
| 5545 | { |
| 5546 | if (is_decl && *var_end == ':') |
| 5547 | { |
| 5548 | char_u *p; |
| 5549 | |
| 5550 | // parse optional type: "let var: type = expr" |
| 5551 | if (!VIM_ISWHITE(var_end[1])) |
| 5552 | { |
| 5553 | semsg(_(e_white_space_required_after_str), ":"); |
| 5554 | return FAIL; |
| 5555 | } |
| 5556 | p = skipwhite(var_end + 1); |
| 5557 | lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE); |
| 5558 | if (lhs->lhs_type == NULL) |
| 5559 | return FAIL; |
| 5560 | lhs->lhs_has_type = TRUE; |
| 5561 | } |
| 5562 | else if (lhs->lhs_lvar != NULL) |
| 5563 | lhs->lhs_type = lhs->lhs_lvar->lv_type; |
| 5564 | } |
| 5565 | |
| 5566 | if (oplen == 3 && !heredoc && lhs->lhs_dest != dest_global |
| 5567 | && lhs->lhs_type->tt_type != VAR_STRING |
| 5568 | && lhs->lhs_type->tt_type != VAR_ANY) |
| 5569 | { |
| 5570 | emsg(_(e_can_only_concatenate_to_string)); |
| 5571 | return FAIL; |
| 5572 | } |
| 5573 | |
| 5574 | if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local |
| 5575 | && cctx->ctx_skip != SKIP_YES) |
| 5576 | { |
| 5577 | if (oplen > 1 && !heredoc) |
| 5578 | { |
| 5579 | // +=, /=, etc. require an existing variable |
| 5580 | semsg(_(e_cannot_use_operator_on_new_variable), lhs->lhs_name); |
| 5581 | return FAIL; |
| 5582 | } |
| 5583 | if (!is_decl) |
| 5584 | { |
| 5585 | semsg(_(e_unknown_variable_str), lhs->lhs_name); |
| 5586 | return FAIL; |
| 5587 | } |
| 5588 | |
| 5589 | // new local variable |
| 5590 | if ((lhs->lhs_type->tt_type == VAR_FUNC |
| 5591 | || lhs->lhs_type->tt_type == VAR_PARTIAL) |
| 5592 | && var_wrong_func_name(lhs->lhs_name, TRUE)) |
| 5593 | return FAIL; |
| 5594 | lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen, |
| 5595 | cmdidx == CMD_final || cmdidx == CMD_const, lhs->lhs_type); |
| 5596 | if (lhs->lhs_lvar == NULL) |
| 5597 | return FAIL; |
| 5598 | lhs->lhs_new_local = TRUE; |
| 5599 | } |
| 5600 | |
| 5601 | lhs->lhs_member_type = lhs->lhs_type; |
| 5602 | if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen) |
| 5603 | { |
| 5604 | // Something follows after the variable: "var[idx]" or "var.key". |
| 5605 | // TODO: should we also handle "->func()" here? |
| 5606 | if (is_decl) |
| 5607 | { |
| 5608 | emsg(_(e_cannot_use_index_when_declaring_variable)); |
| 5609 | return FAIL; |
| 5610 | } |
| 5611 | |
| 5612 | if (var_start[lhs->lhs_varlen] == '[' |
| 5613 | || var_start[lhs->lhs_varlen] == '.') |
| 5614 | { |
| 5615 | char_u *after = var_start + lhs->lhs_varlen; |
| 5616 | char_u *p; |
| 5617 | |
| 5618 | // Only the last index is used below, if there are others |
| 5619 | // before it generate code for the expression. Thus for |
| 5620 | // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index. |
| 5621 | for (;;) |
| 5622 | { |
| 5623 | p = skip_index(after); |
| 5624 | if (*p != '[' && *p != '.') |
| 5625 | break; |
| 5626 | after = p; |
| 5627 | } |
| 5628 | if (after > var_start + lhs->lhs_varlen) |
| 5629 | { |
| 5630 | lhs->lhs_varlen = after - var_start; |
| 5631 | lhs->lhs_dest = dest_expr; |
| 5632 | // We don't know the type before evaluating the expression, |
| 5633 | // use "any" until then. |
| 5634 | lhs->lhs_type = &t_any; |
| 5635 | } |
| 5636 | |
| 5637 | lhs->lhs_has_index = TRUE; |
| 5638 | if (lhs->lhs_type->tt_member == NULL) |
| 5639 | lhs->lhs_member_type = &t_any; |
| 5640 | else |
| 5641 | lhs->lhs_member_type = lhs->lhs_type->tt_member; |
| 5642 | } |
| 5643 | else |
| 5644 | { |
| 5645 | semsg("Not supported yet: %s", var_start); |
| 5646 | return FAIL; |
| 5647 | } |
| 5648 | } |
| 5649 | return OK; |
| 5650 | } |
| 5651 | |
| 5652 | /* |
| 5653 | * Assignment to a list or dict member, or ":unlet" for the item, using the |
| 5654 | * information in "lhs". |
| 5655 | * Returns OK or FAIL. |
| 5656 | */ |
| 5657 | static int |
| 5658 | compile_assign_unlet( |
| 5659 | char_u *var_start, |
| 5660 | lhs_T *lhs, |
| 5661 | int is_assign, |
| 5662 | type_T *rhs_type, |
| 5663 | cctx_T *cctx) |
| 5664 | { |
| 5665 | char_u *p; |
| 5666 | int r; |
| 5667 | vartype_T dest_type; |
| 5668 | size_t varlen = lhs->lhs_varlen; |
| 5669 | garray_T *stack = &cctx->ctx_type_stack; |
| 5670 | |
| 5671 | // Compile the "idx" in "var[idx]" or "key" in "var.key". |
| 5672 | p = var_start + varlen; |
| 5673 | if (*p == '[') |
| 5674 | { |
| 5675 | p = skipwhite(p + 1); |
| 5676 | r = compile_expr0(&p, cctx); |
| 5677 | if (r == OK && *skipwhite(p) != ']') |
| 5678 | { |
| 5679 | // this should not happen |
| 5680 | emsg(_(e_missbrac)); |
| 5681 | r = FAIL; |
| 5682 | } |
| 5683 | } |
| 5684 | else // if (*p == '.') |
| 5685 | { |
| 5686 | char_u *key_end = to_name_end(p + 1, TRUE); |
| 5687 | char_u *key = vim_strnsave(p + 1, key_end - p - 1); |
| 5688 | |
| 5689 | r = generate_PUSHS(cctx, key); |
| 5690 | } |
| 5691 | if (r == FAIL) |
| 5692 | return FAIL; |
| 5693 | |
| 5694 | if (lhs->lhs_type == &t_any) |
| 5695 | { |
| 5696 | // Index on variable of unknown type: check at runtime. |
| 5697 | dest_type = VAR_ANY; |
| 5698 | } |
| 5699 | else |
| 5700 | { |
| 5701 | dest_type = lhs->lhs_type->tt_type; |
| 5702 | if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL) |
| 5703 | return FAIL; |
| 5704 | if (dest_type == VAR_LIST |
| 5705 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
| 5706 | != VAR_NUMBER) |
| 5707 | { |
| 5708 | emsg(_(e_number_exp)); |
| 5709 | return FAIL; |
| 5710 | } |
| 5711 | } |
| 5712 | |
| 5713 | // Load the dict or list. On the stack we then have: |
| 5714 | // - value (for assignment, not for :unlet) |
| 5715 | // - index |
| 5716 | // - variable |
| 5717 | if (lhs->lhs_dest == dest_expr) |
| 5718 | { |
| 5719 | int c = var_start[varlen]; |
| 5720 | |
| 5721 | // Evaluate "ll[expr]" of "ll[expr][idx]" |
| 5722 | p = var_start; |
| 5723 | var_start[varlen] = NUL; |
| 5724 | if (compile_expr0(&p, cctx) == OK && p != var_start + varlen) |
| 5725 | { |
| 5726 | // this should not happen |
| 5727 | emsg(_(e_missbrac)); |
| 5728 | return FAIL; |
| 5729 | } |
| 5730 | var_start[varlen] = c; |
| 5731 | |
| 5732 | lhs->lhs_type = stack->ga_len == 0 ? &t_void |
| 5733 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 5734 | // now we can properly check the type |
| 5735 | if (lhs->lhs_type->tt_member != NULL && rhs_type != &t_void |
| 5736 | && need_type(rhs_type, lhs->lhs_type->tt_member, -2, cctx, |
| 5737 | FALSE, FALSE) == FAIL) |
| 5738 | return FAIL; |
| 5739 | } |
| 5740 | else |
| 5741 | generate_loadvar(cctx, lhs->lhs_dest, lhs->lhs_name, |
| 5742 | lhs->lhs_lvar, lhs->lhs_type); |
| 5743 | |
| 5744 | if (dest_type == VAR_LIST || dest_type == VAR_DICT || dest_type == VAR_ANY) |
| 5745 | { |
| 5746 | if (is_assign) |
| 5747 | { |
| 5748 | isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3); |
| 5749 | |
| 5750 | if (isn == NULL) |
| 5751 | return FAIL; |
| 5752 | isn->isn_arg.vartype = dest_type; |
| 5753 | } |
| 5754 | else |
| 5755 | { |
| 5756 | if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL) |
| 5757 | return FAIL; |
| 5758 | } |
| 5759 | } |
| 5760 | else |
| 5761 | { |
| 5762 | emsg(_(e_indexable_type_required)); |
| 5763 | return FAIL; |
| 5764 | } |
| 5765 | |
| 5766 | return OK; |
| 5767 | } |
| 5768 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 5769 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5770 | * Compile declaration and assignment: |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 5771 | * "let name" |
| 5772 | * "var name = expr" |
| 5773 | * "final name = expr" |
| 5774 | * "const name = expr" |
| 5775 | * "name = expr" |
| 5776 | * "arg" points to "name". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5777 | * Return NULL for an error. |
| 5778 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5779 | */ |
| 5780 | static char_u * |
| 5781 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 5782 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5783 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5784 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5785 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5786 | char_u *ret = NULL; |
| 5787 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5788 | int var_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5789 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5790 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5791 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5792 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5793 | int oplen = 0; |
| 5794 | int heredoc = FALSE; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5795 | type_T *rhs_type = &t_any; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5796 | char_u *sp; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5797 | int is_decl = is_decl_command(cmdidx); |
| 5798 | lhs_T lhs; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5799 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5800 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 5801 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 5802 | if (p == NULL) |
| 5803 | return *arg == '[' ? arg : NULL; |
| 5804 | |
| 5805 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5806 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 5807 | // TODO: should we allow this, and figure out type inference from list |
| 5808 | // members? |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5809 | emsg(_(e_cannot_use_list_for_declaration)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5810 | return NULL; |
| 5811 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5812 | lhs.lhs_name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5813 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5814 | sp = p; |
| 5815 | p = skipwhite(p); |
| 5816 | op = p; |
| 5817 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5818 | |
| 5819 | if (var_count > 0 && oplen == 0) |
| 5820 | // can be something like "[1, 2]->func()" |
| 5821 | return arg; |
| 5822 | |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5823 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !IS_WHITE_OR_NUL(op[oplen]))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5824 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 5825 | error_white_both(op, oplen); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5826 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 5827 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5828 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5829 | if (heredoc) |
| 5830 | { |
| 5831 | list_T *l; |
| 5832 | listitem_T *li; |
| 5833 | |
| 5834 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 5835 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5836 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 5837 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | c0e2901 | 2020-09-27 14:22:48 +0200 | [diff] [blame] | 5838 | if (l == NULL) |
| 5839 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5840 | |
Bram Moolenaar | 078269b | 2020-09-21 20:35:55 +0200 | [diff] [blame] | 5841 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5842 | { |
Bram Moolenaar | 078269b | 2020-09-21 20:35:55 +0200 | [diff] [blame] | 5843 | // Push each line and the create the list. |
| 5844 | FOR_ALL_LIST_ITEMS(l, li) |
| 5845 | { |
| 5846 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 5847 | li->li_tv.vval.v_string = NULL; |
| 5848 | } |
| 5849 | generate_NEWLIST(cctx, l->lv_len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5850 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5851 | list_free(l); |
| 5852 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5853 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5854 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5855 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5856 | { |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5857 | char_u *wp; |
| 5858 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5859 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 5860 | // list of variables below. |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5861 | // A line break may follow the "=". |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 5862 | |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5863 | wp = op + oplen; |
Bram Moolenaar | 8ff16e0 | 2020-12-07 21:49:52 +0100 | [diff] [blame] | 5864 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
Bram Moolenaar | 004d9b0 | 2020-11-30 21:40:03 +0100 | [diff] [blame] | 5865 | return FAIL; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5866 | if (compile_expr0(&p, cctx) == FAIL) |
| 5867 | return NULL; |
| 5868 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5869 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5870 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5871 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5872 | type_T *stacktype; |
| 5873 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5874 | stacktype = stack->ga_len == 0 ? &t_void |
| 5875 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5876 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5877 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5878 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5879 | goto theend; |
| 5880 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 5881 | if (need_type(stacktype, &t_list_any, -1, cctx, |
| 5882 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5883 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5884 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5885 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 5886 | semicolon); |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5887 | if (stacktype->tt_member != NULL) |
| 5888 | rhs_type = stacktype->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5889 | } |
| 5890 | } |
| 5891 | |
| 5892 | /* |
| 5893 | * Loop over variables in "[var, var] = expr". |
| 5894 | * For "var = expr" and "let var: type" this is done only once. |
| 5895 | */ |
| 5896 | if (var_count > 0) |
| 5897 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 5898 | else |
| 5899 | var_start = arg; |
| 5900 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 5901 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5902 | int instr_count = -1; |
| 5903 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5904 | vim_free(lhs.lhs_name); |
| 5905 | |
| 5906 | /* |
| 5907 | * Figure out the LHS type and other properties. |
| 5908 | */ |
| 5909 | if (compile_lhs(var_start, &lhs, cmdidx, heredoc, oplen, cctx) == FAIL) |
| 5910 | goto theend; |
| 5911 | |
| 5912 | if (!lhs.lhs_has_index && lhs.lhs_lvar == &lhs.lhs_arg_lvar) |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 5913 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5914 | semsg(_(e_cannot_assign_to_argument), lhs.lhs_name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5915 | goto theend; |
| 5916 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5917 | if (!is_decl && lhs.lhs_lvar != NULL |
| 5918 | && lhs.lhs_lvar->lv_const && !lhs.lhs_has_index) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5919 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5920 | semsg(_(e_cannot_assign_to_constant), lhs.lhs_name); |
Bram Moolenaar | dbeecb2 | 2020-09-14 18:15:09 +0200 | [diff] [blame] | 5921 | goto theend; |
| 5922 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5923 | |
| 5924 | if (!heredoc) |
| 5925 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5926 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5927 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5928 | if (oplen > 0 && var_count == 0) |
| 5929 | { |
| 5930 | // skip over the "=" and the expression |
| 5931 | p = skipwhite(op + oplen); |
| 5932 | compile_expr0(&p, cctx); |
| 5933 | } |
| 5934 | } |
| 5935 | else if (oplen > 0) |
| 5936 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 5937 | int is_const = FALSE; |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 5938 | char_u *wp; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5939 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5940 | // For "var = expr" evaluate the expression. |
| 5941 | if (var_count == 0) |
| 5942 | { |
| 5943 | int r; |
| 5944 | |
| 5945 | // for "+=", "*=", "..=" etc. first load the current value |
| 5946 | if (*op != '=') |
| 5947 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5948 | generate_loadvar(cctx, lhs.lhs_dest, lhs.lhs_name, |
| 5949 | lhs.lhs_lvar, lhs.lhs_type); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5950 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5951 | if (lhs.lhs_has_index) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5952 | { |
| 5953 | // TODO: get member from list or dict |
| 5954 | emsg("Index with operation not supported yet"); |
| 5955 | goto theend; |
| 5956 | } |
| 5957 | } |
| 5958 | |
| 5959 | // Compile the expression. Temporarily hide the new local |
| 5960 | // variable here, it is not available to this expression. |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5961 | if (lhs.lhs_new_local) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5962 | --cctx->ctx_locals.ga_len; |
| 5963 | instr_count = instr->ga_len; |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 5964 | wp = op + oplen; |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 5965 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
Bram Moolenaar | 21e5122 | 2020-12-04 12:43:29 +0100 | [diff] [blame] | 5966 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5967 | if (lhs.lhs_new_local) |
Bram Moolenaar | 21e5122 | 2020-12-04 12:43:29 +0100 | [diff] [blame] | 5968 | ++cctx->ctx_locals.ga_len; |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 5969 | goto theend; |
Bram Moolenaar | 21e5122 | 2020-12-04 12:43:29 +0100 | [diff] [blame] | 5970 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 5971 | r = compile_expr0_ext(&p, cctx, &is_const); |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5972 | if (lhs.lhs_new_local) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5973 | ++cctx->ctx_locals.ga_len; |
| 5974 | if (r == FAIL) |
| 5975 | goto theend; |
| 5976 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5977 | else if (semicolon && var_idx == var_count - 1) |
| 5978 | { |
| 5979 | // For "[var; var] = expr" get the rest of the list |
| 5980 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5981 | goto theend; |
| 5982 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5983 | else |
| 5984 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5985 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5986 | // list. |
| 5987 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
Bram Moolenaar | 7f76494 | 2020-12-02 15:11:18 +0100 | [diff] [blame] | 5988 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5989 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5990 | |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5991 | rhs_type = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5992 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5993 | if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5994 | { |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 5995 | if ((rhs_type->tt_type == VAR_FUNC |
| 5996 | || rhs_type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 5997 | && var_wrong_func_name(lhs.lhs_name, TRUE)) |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 5998 | goto theend; |
| 5999 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6000 | if (lhs.lhs_new_local && !lhs.lhs_has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6001 | { |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6002 | if (rhs_type->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6003 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6004 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6005 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6006 | } |
| 6007 | else |
| 6008 | { |
Bram Moolenaar | 04bdd57 | 2020-09-23 13:25:32 +0200 | [diff] [blame] | 6009 | // An empty list or dict has a &t_unknown member, |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6010 | // for a variable that implies &t_any. |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6011 | if (rhs_type == &t_list_empty) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6012 | lhs.lhs_lvar->lv_type = &t_list_any; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6013 | else if (rhs_type == &t_dict_empty) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6014 | lhs.lhs_lvar->lv_type = &t_dict_any; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6015 | else if (rhs_type == &t_unknown) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6016 | lhs.lhs_lvar->lv_type = &t_any; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6017 | else |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6018 | lhs.lhs_lvar->lv_type = rhs_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6019 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6020 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6021 | else if (*op == '=') |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6022 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6023 | type_T *use_type = lhs.lhs_lvar->lv_type; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6024 | |
Bram Moolenaar | 71abe48 | 2020-09-14 22:28:30 +0200 | [diff] [blame] | 6025 | // without operator check type here, otherwise below |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6026 | if (lhs.lhs_has_index) |
| 6027 | use_type = lhs.lhs_member_type; |
Bram Moolenaar | dc234ca | 2020-11-28 18:52:33 +0100 | [diff] [blame] | 6028 | if (need_type(rhs_type, use_type, -1, cctx, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 6029 | FALSE, is_const) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6030 | goto theend; |
| 6031 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6032 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6033 | else if (*p != '=' && need_type(rhs_type, lhs.lhs_member_type, |
| 6034 | -1, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6035 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6036 | } |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 6037 | else if (cmdidx == CMD_final) |
| 6038 | { |
| 6039 | emsg(_(e_final_requires_a_value)); |
| 6040 | goto theend; |
| 6041 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6042 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6043 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 6044 | emsg(_(e_const_requires_a_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6045 | goto theend; |
| 6046 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6047 | else if (!lhs.lhs_has_type || lhs.lhs_dest == dest_option) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6048 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 6049 | emsg(_(e_type_or_initialization_required)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6050 | goto theend; |
| 6051 | } |
| 6052 | else |
| 6053 | { |
| 6054 | // variables are always initialized |
| 6055 | if (ga_grow(instr, 1) == FAIL) |
| 6056 | goto theend; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6057 | switch (lhs.lhs_member_type->tt_type) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6058 | { |
| 6059 | case VAR_BOOL: |
| 6060 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 6061 | break; |
| 6062 | case VAR_FLOAT: |
| 6063 | #ifdef FEAT_FLOAT |
| 6064 | generate_PUSHF(cctx, 0.0); |
| 6065 | #endif |
| 6066 | break; |
| 6067 | case VAR_STRING: |
| 6068 | generate_PUSHS(cctx, NULL); |
| 6069 | break; |
| 6070 | case VAR_BLOB: |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 6071 | generate_PUSHBLOB(cctx, blob_alloc()); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6072 | break; |
| 6073 | case VAR_FUNC: |
| 6074 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 6075 | break; |
| 6076 | case VAR_LIST: |
| 6077 | generate_NEWLIST(cctx, 0); |
| 6078 | break; |
| 6079 | case VAR_DICT: |
| 6080 | generate_NEWDICT(cctx, 0); |
| 6081 | break; |
| 6082 | case VAR_JOB: |
| 6083 | generate_PUSHJOB(cctx, NULL); |
| 6084 | break; |
| 6085 | case VAR_CHANNEL: |
| 6086 | generate_PUSHCHANNEL(cctx, NULL); |
| 6087 | break; |
| 6088 | case VAR_NUMBER: |
| 6089 | case VAR_UNKNOWN: |
| 6090 | case VAR_ANY: |
| 6091 | case VAR_PARTIAL: |
| 6092 | case VAR_VOID: |
| 6093 | case VAR_SPECIAL: // cannot happen |
| 6094 | generate_PUSHNR(cctx, 0); |
| 6095 | break; |
| 6096 | } |
| 6097 | } |
| 6098 | if (var_count == 0) |
| 6099 | end = p; |
| 6100 | } |
| 6101 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6102 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6103 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 6104 | break; |
| 6105 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6106 | if (oplen > 0 && *op != '=') |
| 6107 | { |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6108 | type_T *expected; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6109 | type_T *stacktype; |
| 6110 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6111 | if (*op == '.') |
| 6112 | expected = &t_string; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6113 | else |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6114 | expected = lhs.lhs_member_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6115 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6116 | if ( |
| 6117 | #ifdef FEAT_FLOAT |
| 6118 | // If variable is float operation with number is OK. |
| 6119 | !(expected == &t_float && stacktype == &t_number) && |
| 6120 | #endif |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 6121 | need_type(stacktype, expected, -1, cctx, |
| 6122 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6123 | goto theend; |
| 6124 | |
| 6125 | if (*op == '.') |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 6126 | { |
| 6127 | if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL) |
| 6128 | goto theend; |
| 6129 | } |
| 6130 | else if (*op == '+') |
| 6131 | { |
| 6132 | if (generate_add_instr(cctx, |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6133 | operator_type(lhs.lhs_member_type, stacktype), |
| 6134 | lhs.lhs_member_type, stacktype) == FAIL) |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 6135 | goto theend; |
| 6136 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 6137 | else if (generate_two_op(cctx, op) == FAIL) |
| 6138 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6139 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6140 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6141 | if (lhs.lhs_has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6142 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6143 | // Use the info in "lhs" to store the value at the index in the |
| 6144 | // list or dict. |
| 6145 | if (compile_assign_unlet(var_start, &lhs, TRUE, rhs_type, cctx) |
| 6146 | == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6147 | goto theend; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6148 | } |
| 6149 | else |
| 6150 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6151 | if (is_decl && cmdidx == CMD_const && (lhs.lhs_dest == dest_script |
| 6152 | || lhs.lhs_dest == dest_local)) |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 6153 | // ":const var": lock the value, but not referenced variables |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 6154 | generate_LOCKCONST(cctx); |
| 6155 | |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 6156 | if (is_decl |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6157 | && (lhs.lhs_type->tt_type == VAR_DICT |
| 6158 | || lhs.lhs_type->tt_type == VAR_LIST) |
| 6159 | && lhs.lhs_type->tt_member != NULL |
| 6160 | && lhs.lhs_type->tt_member != &t_any |
| 6161 | && lhs.lhs_type->tt_member != &t_unknown) |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 6162 | // Set the type in the list or dict, so that it can be checked, |
| 6163 | // also in legacy script. |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6164 | generate_SETTYPE(cctx, lhs.lhs_type); |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 6165 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6166 | if (lhs.lhs_dest != dest_local) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6167 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6168 | if (generate_store_var(cctx, lhs.lhs_dest, |
| 6169 | lhs.lhs_opt_flags, lhs.lhs_vimvaridx, |
| 6170 | lhs.lhs_scriptvar_idx, lhs.lhs_scriptvar_sid, |
| 6171 | lhs.lhs_type, lhs.lhs_name) == FAIL) |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6172 | goto theend; |
| 6173 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6174 | else if (lhs.lhs_lvar != NULL) |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6175 | { |
| 6176 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 6177 | + instr->ga_len - 1; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6178 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6179 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 6180 | // ISN_STORE into ISN_STORENR |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6181 | if (!lhs.lhs_lvar->lv_from_outer |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6182 | && instr->ga_len == instr_count + 1 |
| 6183 | && isn->isn_type == ISN_PUSHNR) |
| 6184 | { |
| 6185 | varnumber_T val = isn->isn_arg.number; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6186 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6187 | isn->isn_type = ISN_STORENR; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6188 | isn->isn_arg.storenr.stnr_idx = lhs.lhs_lvar->lv_idx; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6189 | isn->isn_arg.storenr.stnr_val = val; |
| 6190 | if (stack->ga_len > 0) |
| 6191 | --stack->ga_len; |
| 6192 | } |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6193 | else if (lhs.lhs_lvar->lv_from_outer) |
| 6194 | generate_STORE(cctx, ISN_STOREOUTER, |
| 6195 | lhs.lhs_lvar->lv_idx, NULL); |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6196 | else |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6197 | generate_STORE(cctx, ISN_STORE, lhs.lhs_lvar->lv_idx, NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6198 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6199 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6200 | |
| 6201 | if (var_idx + 1 < var_count) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6202 | var_start = skipwhite(lhs.lhs_dest_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6203 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6204 | |
| 6205 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 6206 | if (var_count > 0 && !semicolon) |
| 6207 | { |
Bram Moolenaar | ec79229 | 2020-12-13 21:26:56 +0100 | [diff] [blame] | 6208 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 6209 | goto theend; |
| 6210 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6211 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 6212 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6213 | |
| 6214 | theend: |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6215 | vim_free(lhs.lhs_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6216 | return ret; |
| 6217 | } |
| 6218 | |
| 6219 | /* |
Bram Moolenaar | 17126b1 | 2021-01-07 22:03:02 +0100 | [diff] [blame] | 6220 | * Check for an assignment at "eap->cmd", compile it if found. |
| 6221 | * Return NOTDONE if there is none, FAIL for failure, OK if done. |
| 6222 | */ |
| 6223 | static int |
| 6224 | may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx) |
| 6225 | { |
| 6226 | char_u *pskip; |
| 6227 | char_u *p; |
| 6228 | |
| 6229 | // Assuming the command starts with a variable or function name, |
| 6230 | // find what follows. |
| 6231 | // Skip over "var.member", "var[idx]" and the like. |
| 6232 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 6233 | pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@') |
| 6234 | ? eap->cmd + 1 : eap->cmd; |
| 6235 | p = to_name_end(pskip, TRUE); |
| 6236 | if (p > eap->cmd && *p != NUL) |
| 6237 | { |
| 6238 | char_u *var_end; |
| 6239 | int oplen; |
| 6240 | int heredoc; |
| 6241 | |
| 6242 | if (eap->cmd[0] == '@') |
| 6243 | var_end = eap->cmd + 2; |
| 6244 | else |
| 6245 | var_end = find_name_end(pskip, NULL, NULL, |
| 6246 | FNE_CHECK_START | FNE_INCL_BR); |
| 6247 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
| 6248 | if (oplen > 0) |
| 6249 | { |
| 6250 | size_t len = p - eap->cmd; |
| 6251 | |
| 6252 | // Recognize an assignment if we recognize the variable |
| 6253 | // name: |
| 6254 | // "g:var = expr" |
| 6255 | // "local = expr" where "local" is a local var. |
| 6256 | // "script = expr" where "script" is a script-local var. |
| 6257 | // "import = expr" where "import" is an imported var |
| 6258 | // "&opt = expr" |
| 6259 | // "$ENV = expr" |
| 6260 | // "@r = expr" |
| 6261 | if (*eap->cmd == '&' |
| 6262 | || *eap->cmd == '$' |
| 6263 | || *eap->cmd == '@' |
| 6264 | || ((len) > 2 && eap->cmd[1] == ':') |
| 6265 | || lookup_local(eap->cmd, len, NULL, cctx) == OK |
| 6266 | || arg_exists(eap->cmd, len, NULL, NULL, NULL, cctx) == OK |
| 6267 | || script_var_exists(eap->cmd, len, FALSE, cctx) == OK |
| 6268 | || find_imported(eap->cmd, len, cctx) != NULL) |
| 6269 | { |
| 6270 | *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx); |
| 6271 | if (*line == NULL || *line == eap->cmd) |
| 6272 | return FAIL; |
| 6273 | return OK; |
| 6274 | } |
| 6275 | } |
| 6276 | } |
| 6277 | |
| 6278 | if (*eap->cmd == '[') |
| 6279 | { |
| 6280 | // [var, var] = expr |
| 6281 | *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx); |
| 6282 | if (*line == NULL) |
| 6283 | return FAIL; |
| 6284 | if (*line != eap->cmd) |
| 6285 | return OK; |
| 6286 | } |
| 6287 | return NOTDONE; |
| 6288 | } |
| 6289 | |
| 6290 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6291 | * Check if "name" can be "unlet". |
| 6292 | */ |
| 6293 | int |
| 6294 | check_vim9_unlet(char_u *name) |
| 6295 | { |
| 6296 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 6297 | { |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 6298 | // "unlet s:var" is allowed in legacy script. |
| 6299 | if (*name == 's' && !script_is_vim9()) |
| 6300 | return OK; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6301 | semsg(_(e_cannot_unlet_str), name); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6302 | return FAIL; |
| 6303 | } |
| 6304 | return OK; |
| 6305 | } |
| 6306 | |
| 6307 | /* |
| 6308 | * Callback passed to ex_unletlock(). |
| 6309 | */ |
| 6310 | static int |
| 6311 | compile_unlet( |
| 6312 | lval_T *lvp, |
| 6313 | char_u *name_end, |
| 6314 | exarg_T *eap, |
| 6315 | int deep UNUSED, |
| 6316 | void *coookie) |
| 6317 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6318 | cctx_T *cctx = coookie; |
| 6319 | char_u *p = lvp->ll_name; |
| 6320 | int cc = *name_end; |
| 6321 | int ret = OK; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6322 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6323 | if (cctx->ctx_skip == SKIP_YES) |
| 6324 | return OK; |
| 6325 | |
| 6326 | *name_end = NUL; |
| 6327 | if (*p == '$') |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6328 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6329 | // :unlet $ENV_VAR |
| 6330 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 6331 | } |
| 6332 | else if (vim_strchr(p, '.') != NULL || vim_strchr(p, '[') != NULL) |
| 6333 | { |
| 6334 | lhs_T lhs; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6335 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6336 | // This is similar to assigning: lookup the list/dict, compile the |
| 6337 | // idx/key. Then instead of storing the value unlet the item. |
| 6338 | // unlet {list}[idx] |
| 6339 | // unlet {dict}[key] dict.key |
| 6340 | // |
| 6341 | // Figure out the LHS type and other properties. |
| 6342 | // |
| 6343 | ret = compile_lhs(p, &lhs, CMD_unlet, FALSE, 0, cctx); |
| 6344 | |
| 6345 | // : unlet an indexed item |
| 6346 | if (!lhs.lhs_has_index) |
Bram Moolenaar | 2ef951d | 2021-01-03 20:55:26 +0100 | [diff] [blame] | 6347 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6348 | iemsg("called compile_lhs() without an index"); |
| 6349 | ret = FAIL; |
| 6350 | } |
| 6351 | else |
| 6352 | { |
| 6353 | // Use the info in "lhs" to unlet the item at the index in the |
| 6354 | // list or dict. |
| 6355 | ret = compile_assign_unlet(p, &lhs, FALSE, &t_void, cctx); |
Bram Moolenaar | 2ef951d | 2021-01-03 20:55:26 +0100 | [diff] [blame] | 6356 | } |
| 6357 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6358 | vim_free(lhs.lhs_name); |
| 6359 | } |
| 6360 | else if (check_vim9_unlet(p) == FAIL) |
| 6361 | { |
| 6362 | ret = FAIL; |
| 6363 | } |
| 6364 | else |
| 6365 | { |
| 6366 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 6367 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6368 | } |
| 6369 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 6370 | *name_end = cc; |
| 6371 | return ret; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6372 | } |
| 6373 | |
| 6374 | /* |
| 6375 | * compile "unlet var", "lock var" and "unlock var" |
| 6376 | * "arg" points to "var". |
| 6377 | */ |
| 6378 | static char_u * |
| 6379 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 6380 | { |
| 6381 | char_u *p = arg; |
| 6382 | |
| 6383 | if (eap->cmdidx != CMD_unlet) |
| 6384 | { |
| 6385 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 6386 | return NULL; |
| 6387 | } |
| 6388 | |
Bram Moolenaar | 2ef951d | 2021-01-03 20:55:26 +0100 | [diff] [blame] | 6389 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING, |
| 6390 | compile_unlet, cctx); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6391 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 6392 | } |
| 6393 | |
| 6394 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6395 | * Compile an :import command. |
| 6396 | */ |
| 6397 | static char_u * |
| 6398 | compile_import(char_u *arg, cctx_T *cctx) |
| 6399 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 6400 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6401 | } |
| 6402 | |
| 6403 | /* |
| 6404 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 6405 | */ |
| 6406 | static int |
| 6407 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 6408 | { |
| 6409 | garray_T *instr = &cctx->ctx_instr; |
| 6410 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 6411 | |
| 6412 | if (endlabel == NULL) |
| 6413 | return FAIL; |
| 6414 | endlabel->el_next = *el; |
| 6415 | *el = endlabel; |
| 6416 | endlabel->el_end_label = instr->ga_len; |
| 6417 | |
| 6418 | generate_JUMP(cctx, when, 0); |
| 6419 | return OK; |
| 6420 | } |
| 6421 | |
| 6422 | static void |
| 6423 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 6424 | { |
| 6425 | garray_T *instr = &cctx->ctx_instr; |
| 6426 | |
| 6427 | while (*el != NULL) |
| 6428 | { |
| 6429 | endlabel_T *cur = (*el); |
| 6430 | isn_T *isn; |
| 6431 | |
| 6432 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 6433 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6434 | *el = cur->el_next; |
| 6435 | vim_free(cur); |
| 6436 | } |
| 6437 | } |
| 6438 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6439 | static void |
| 6440 | compile_free_jump_to_end(endlabel_T **el) |
| 6441 | { |
| 6442 | while (*el != NULL) |
| 6443 | { |
| 6444 | endlabel_T *cur = (*el); |
| 6445 | |
| 6446 | *el = cur->el_next; |
| 6447 | vim_free(cur); |
| 6448 | } |
| 6449 | } |
| 6450 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6451 | /* |
| 6452 | * Create a new scope and set up the generic items. |
| 6453 | */ |
| 6454 | static scope_T * |
| 6455 | new_scope(cctx_T *cctx, scopetype_T type) |
| 6456 | { |
| 6457 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 6458 | |
| 6459 | if (scope == NULL) |
| 6460 | return NULL; |
| 6461 | scope->se_outer = cctx->ctx_scope; |
| 6462 | cctx->ctx_scope = scope; |
| 6463 | scope->se_type = type; |
| 6464 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 6465 | return scope; |
| 6466 | } |
| 6467 | |
| 6468 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6469 | * Free the current scope and go back to the outer scope. |
| 6470 | */ |
| 6471 | static void |
| 6472 | drop_scope(cctx_T *cctx) |
| 6473 | { |
| 6474 | scope_T *scope = cctx->ctx_scope; |
| 6475 | |
| 6476 | if (scope == NULL) |
| 6477 | { |
| 6478 | iemsg("calling drop_scope() without a scope"); |
| 6479 | return; |
| 6480 | } |
| 6481 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6482 | switch (scope->se_type) |
| 6483 | { |
| 6484 | case IF_SCOPE: |
| 6485 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 6486 | case FOR_SCOPE: |
| 6487 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 6488 | case WHILE_SCOPE: |
| 6489 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 6490 | case TRY_SCOPE: |
| 6491 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 6492 | case NO_SCOPE: |
| 6493 | case BLOCK_SCOPE: |
| 6494 | break; |
| 6495 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6496 | vim_free(scope); |
| 6497 | } |
| 6498 | |
| 6499 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6500 | * compile "if expr" |
| 6501 | * |
| 6502 | * "if expr" Produces instructions: |
| 6503 | * EVAL expr Push result of "expr" |
| 6504 | * JUMP_IF_FALSE end |
| 6505 | * ... body ... |
| 6506 | * end: |
| 6507 | * |
| 6508 | * "if expr | else" Produces instructions: |
| 6509 | * EVAL expr Push result of "expr" |
| 6510 | * JUMP_IF_FALSE else |
| 6511 | * ... body ... |
| 6512 | * JUMP_ALWAYS end |
| 6513 | * else: |
| 6514 | * ... body ... |
| 6515 | * end: |
| 6516 | * |
| 6517 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 6518 | * EVAL expr Push result of "expr" |
| 6519 | * JUMP_IF_FALSE elseif |
| 6520 | * ... body ... |
| 6521 | * JUMP_ALWAYS end |
| 6522 | * elseif: |
| 6523 | * EVAL expr Push result of "expr" |
| 6524 | * JUMP_IF_FALSE else |
| 6525 | * ... body ... |
| 6526 | * JUMP_ALWAYS end |
| 6527 | * else: |
| 6528 | * ... body ... |
| 6529 | * end: |
| 6530 | */ |
| 6531 | static char_u * |
| 6532 | compile_if(char_u *arg, cctx_T *cctx) |
| 6533 | { |
| 6534 | char_u *p = arg; |
| 6535 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6536 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6537 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6538 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6539 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6540 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6541 | CLEAR_FIELD(ppconst); |
| 6542 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6543 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6544 | clear_ppconst(&ppconst); |
| 6545 | return NULL; |
| 6546 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6547 | if (cctx->ctx_skip == SKIP_YES) |
| 6548 | clear_ppconst(&ppconst); |
| 6549 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6550 | { |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6551 | int error = FALSE; |
| 6552 | int v; |
| 6553 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6554 | // The expression results in a constant. |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6555 | v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); |
Bram Moolenaar | dda749c | 2020-10-04 17:24:29 +0200 | [diff] [blame] | 6556 | clear_ppconst(&ppconst); |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6557 | if (error) |
| 6558 | return NULL; |
| 6559 | cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6560 | } |
| 6561 | else |
| 6562 | { |
| 6563 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6564 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6565 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6566 | return NULL; |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6567 | if (bool_on_stack(cctx) == FAIL) |
| 6568 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6569 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6570 | |
| 6571 | scope = new_scope(cctx, IF_SCOPE); |
| 6572 | if (scope == NULL) |
| 6573 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6574 | scope->se_skip_save = skip_save; |
| 6575 | // "is_had_return" will be reset if any block does not end in :return |
| 6576 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6577 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6578 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6579 | { |
| 6580 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6581 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6582 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6583 | } |
| 6584 | else |
| 6585 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6586 | |
| 6587 | return p; |
| 6588 | } |
| 6589 | |
| 6590 | static char_u * |
| 6591 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 6592 | { |
| 6593 | char_u *p = arg; |
| 6594 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6595 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6596 | isn_T *isn; |
| 6597 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6598 | ppconst_T ppconst; |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 6599 | skip_T save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6600 | |
| 6601 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6602 | { |
| 6603 | emsg(_(e_elseif_without_if)); |
| 6604 | return NULL; |
| 6605 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6606 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6607 | if (!cctx->ctx_had_return) |
| 6608 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6609 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6610 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6611 | { |
| 6612 | 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] | 6613 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6614 | return NULL; |
| 6615 | // previous "if" or "elseif" jumps here |
| 6616 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6617 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6618 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6619 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6620 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6621 | CLEAR_FIELD(ppconst); |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 6622 | if (cctx->ctx_skip == SKIP_YES) |
| 6623 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6624 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6625 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6626 | clear_ppconst(&ppconst); |
| 6627 | return NULL; |
| 6628 | } |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 6629 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6630 | if (scope->se_skip_save == SKIP_YES) |
| 6631 | clear_ppconst(&ppconst); |
| 6632 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6633 | { |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6634 | int error = FALSE; |
| 6635 | int v; |
| 6636 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6637 | // The expression results in a constant. |
| 6638 | // TODO: how about nesting? |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6639 | v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); |
| 6640 | if (error) |
| 6641 | return NULL; |
| 6642 | cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6643 | clear_ppconst(&ppconst); |
| 6644 | scope->se_u.se_if.is_if_label = -1; |
| 6645 | } |
| 6646 | else |
| 6647 | { |
| 6648 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6649 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6650 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6651 | return NULL; |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6652 | if (bool_on_stack(cctx) == FAIL) |
| 6653 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6654 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6655 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6656 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6657 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6658 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6659 | |
| 6660 | return p; |
| 6661 | } |
| 6662 | |
| 6663 | static char_u * |
| 6664 | compile_else(char_u *arg, cctx_T *cctx) |
| 6665 | { |
| 6666 | char_u *p = arg; |
| 6667 | garray_T *instr = &cctx->ctx_instr; |
| 6668 | isn_T *isn; |
| 6669 | scope_T *scope = cctx->ctx_scope; |
| 6670 | |
| 6671 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6672 | { |
| 6673 | emsg(_(e_else_without_if)); |
| 6674 | return NULL; |
| 6675 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6676 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6677 | if (!cctx->ctx_had_return) |
| 6678 | scope->se_u.se_if.is_had_return = FALSE; |
| 6679 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6680 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6681 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6682 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6683 | // 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] | 6684 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6685 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6686 | if (!cctx->ctx_had_return |
| 6687 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6688 | JUMP_ALWAYS, cctx) == FAIL) |
| 6689 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6690 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6691 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6692 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6693 | { |
| 6694 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6695 | { |
| 6696 | // previous "if" or "elseif" jumps here |
| 6697 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6698 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6699 | scope->se_u.se_if.is_if_label = -1; |
| 6700 | } |
| 6701 | } |
| 6702 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6703 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6704 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6705 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6706 | |
| 6707 | return p; |
| 6708 | } |
| 6709 | |
| 6710 | static char_u * |
| 6711 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6712 | { |
| 6713 | scope_T *scope = cctx->ctx_scope; |
| 6714 | ifscope_T *ifscope; |
| 6715 | garray_T *instr = &cctx->ctx_instr; |
| 6716 | isn_T *isn; |
| 6717 | |
| 6718 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6719 | { |
| 6720 | emsg(_(e_endif_without_if)); |
| 6721 | return NULL; |
| 6722 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6723 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6724 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6725 | if (!cctx->ctx_had_return) |
| 6726 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6727 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6728 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6729 | { |
| 6730 | // previous "if" or "elseif" jumps here |
| 6731 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6732 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6733 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6734 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6735 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6736 | cctx->ctx_skip = scope->se_skip_save; |
| 6737 | |
| 6738 | // If all the blocks end in :return and there is an :else then the |
| 6739 | // had_return flag is set. |
| 6740 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6741 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6742 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6743 | return arg; |
| 6744 | } |
| 6745 | |
| 6746 | /* |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6747 | * Compile "for var in expr": |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6748 | * |
| 6749 | * Produces instructions: |
| 6750 | * PUSHNR -1 |
| 6751 | * STORE loop-idx Set index to -1 |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6752 | * EVAL expr result of "expr" on top of stack |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6753 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6754 | * - if beyond end, jump to "end" |
| 6755 | * - otherwise get item from list and push it |
| 6756 | * STORE var Store item in "var" |
| 6757 | * ... body ... |
| 6758 | * JUMP top Jump back to repeat |
| 6759 | * end: DROP Drop the result of "expr" |
| 6760 | * |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6761 | * Compile "for [var1, var2] in expr" - as above, but instead of "STORE var": |
| 6762 | * UNPACK 2 Split item in 2 |
| 6763 | * STORE var1 Store item in "var1" |
| 6764 | * STORE var2 Store item in "var2" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6765 | */ |
| 6766 | static char_u * |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6767 | compile_for(char_u *arg_start, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6768 | { |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6769 | char_u *arg; |
| 6770 | char_u *arg_end; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6771 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6772 | char_u *p; |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6773 | char_u *wp; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6774 | int var_count = 0; |
| 6775 | int semicolon = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6776 | size_t varlen; |
| 6777 | garray_T *instr = &cctx->ctx_instr; |
| 6778 | garray_T *stack = &cctx->ctx_type_stack; |
| 6779 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6780 | lvar_T *loop_lvar; // loop iteration variable |
| 6781 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6782 | type_T *vartype; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6783 | type_T *item_type = &t_any; |
| 6784 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6785 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6786 | p = skip_var_list(arg_start, TRUE, &var_count, &semicolon, FALSE); |
| 6787 | if (var_count == 0) |
| 6788 | var_count = 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6789 | |
| 6790 | // consume "in" |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6791 | wp = p; |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6792 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
| 6793 | return NULL; |
| 6794 | if (STRNCMP(p, "in", 2) != 0 || !IS_WHITE_OR_NUL(p[2])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6795 | { |
| 6796 | emsg(_(e_missing_in)); |
| 6797 | return NULL; |
| 6798 | } |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6799 | wp = p + 2; |
Bram Moolenaar | 38bd8de | 2020-12-02 13:23:36 +0100 | [diff] [blame] | 6800 | if (may_get_next_line_error(wp, &p, cctx) == FAIL) |
| 6801 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6802 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6803 | scope = new_scope(cctx, FOR_SCOPE); |
| 6804 | if (scope == NULL) |
| 6805 | return NULL; |
| 6806 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6807 | // Reserve a variable to store the loop iteration counter and initialize it |
| 6808 | // to -1. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6809 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6810 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6811 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6812 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6813 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6814 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6815 | } |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6816 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6817 | |
| 6818 | // compile "expr", it remains on the stack until "endfor" |
| 6819 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6820 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6821 | { |
| 6822 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6823 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6824 | } |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6825 | arg_end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6826 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6827 | // Now that we know the type of "var", check that it is a list, now or at |
| 6828 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6829 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 6830 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6831 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6832 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6833 | return NULL; |
| 6834 | } |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6835 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6836 | 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] | 6837 | { |
| 6838 | if (var_count == 1) |
| 6839 | item_type = vartype->tt_member; |
| 6840 | else if (vartype->tt_member->tt_type == VAR_LIST |
| 6841 | && vartype->tt_member->tt_member->tt_type != VAR_ANY) |
| 6842 | item_type = vartype->tt_member->tt_member; |
| 6843 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6844 | |
| 6845 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6846 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6847 | generate_FOR(cctx, loop_lvar->lv_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6848 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6849 | arg = arg_start; |
| 6850 | if (var_count > 1) |
| 6851 | { |
| 6852 | generate_UNPACK(cctx, var_count, semicolon); |
| 6853 | arg = skipwhite(arg + 1); // skip white after '[' |
| 6854 | |
| 6855 | // the list item is replaced by a number of items |
| 6856 | if (ga_grow(stack, var_count - 1) == FAIL) |
| 6857 | { |
| 6858 | drop_scope(cctx); |
| 6859 | return NULL; |
| 6860 | } |
| 6861 | --stack->ga_len; |
| 6862 | for (idx = 0; idx < var_count; ++idx) |
| 6863 | { |
| 6864 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 6865 | (semicolon && idx == 0) ? vartype : item_type; |
| 6866 | ++stack->ga_len; |
| 6867 | } |
| 6868 | } |
| 6869 | |
| 6870 | for (idx = 0; idx < var_count; ++idx) |
| 6871 | { |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6872 | assign_dest_T dest = dest_local; |
| 6873 | int opt_flags = 0; |
| 6874 | int vimvaridx = -1; |
| 6875 | type_T *type = &t_any; |
| 6876 | |
| 6877 | p = skip_var_one(arg, FALSE); |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6878 | varlen = p - arg; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6879 | name = vim_strnsave(arg, varlen); |
| 6880 | if (name == NULL) |
| 6881 | goto failed; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6882 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6883 | // TODO: script var not supported? |
| 6884 | if (get_var_dest(name, &dest, CMD_for, &opt_flags, |
| 6885 | &vimvaridx, &type, cctx) == FAIL) |
| 6886 | goto failed; |
| 6887 | if (dest != dest_local) |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6888 | { |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6889 | if (generate_store_var(cctx, dest, opt_flags, vimvaridx, |
| 6890 | 0, 0, type, name) == FAIL) |
| 6891 | goto failed; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6892 | } |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6893 | else |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6894 | { |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 6895 | if (lookup_local(arg, varlen, NULL, cctx) == OK) |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6896 | { |
| 6897 | semsg(_(e_variable_already_declared), arg); |
| 6898 | goto failed; |
| 6899 | } |
| 6900 | |
Bram Moolenaar | ea87069 | 2020-12-02 14:24:30 +0100 | [diff] [blame] | 6901 | if (STRNCMP(name, "s:", 2) == 0) |
| 6902 | { |
| 6903 | semsg(_(e_cannot_declare_script_variable_in_function), name); |
| 6904 | goto failed; |
| 6905 | } |
| 6906 | |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6907 | // Reserve a variable to store "var". |
| 6908 | // TODO: check for type |
| 6909 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 6910 | if (var_lvar == NULL) |
| 6911 | // out of memory or used as an argument |
| 6912 | goto failed; |
| 6913 | |
| 6914 | if (semicolon && idx == var_count - 1) |
| 6915 | var_lvar->lv_type = vartype; |
| 6916 | else |
| 6917 | var_lvar->lv_type = item_type; |
| 6918 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
| 6919 | } |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6920 | |
| 6921 | if (*p == ',' || *p == ';') |
| 6922 | ++p; |
| 6923 | arg = skipwhite(p); |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6924 | vim_free(name); |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 6925 | } |
| 6926 | |
| 6927 | return arg_end; |
Bram Moolenaar | 4b8a065 | 2020-12-01 16:30:44 +0100 | [diff] [blame] | 6928 | |
| 6929 | failed: |
| 6930 | vim_free(name); |
| 6931 | drop_scope(cctx); |
| 6932 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6933 | } |
| 6934 | |
| 6935 | /* |
| 6936 | * compile "endfor" |
| 6937 | */ |
| 6938 | static char_u * |
| 6939 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 6940 | { |
| 6941 | garray_T *instr = &cctx->ctx_instr; |
| 6942 | scope_T *scope = cctx->ctx_scope; |
| 6943 | forscope_T *forscope; |
| 6944 | isn_T *isn; |
| 6945 | |
| 6946 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 6947 | { |
| 6948 | emsg(_(e_for)); |
| 6949 | return NULL; |
| 6950 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6951 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6952 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6953 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6954 | |
| 6955 | // At end of ":for" scope jump back to the FOR instruction. |
| 6956 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 6957 | |
| 6958 | // Fill in the "end" label in the FOR statement so it can jump here |
| 6959 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 6960 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 6961 | |
| 6962 | // Fill in the "end" label any BREAK statements |
| 6963 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 6964 | |
| 6965 | // Below the ":for" scope drop the "expr" list from the stack. |
| 6966 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 6967 | return NULL; |
| 6968 | |
| 6969 | vim_free(scope); |
| 6970 | |
| 6971 | return arg; |
| 6972 | } |
| 6973 | |
| 6974 | /* |
| 6975 | * compile "while expr" |
| 6976 | * |
| 6977 | * Produces instructions: |
| 6978 | * top: EVAL expr Push result of "expr" |
| 6979 | * JUMP_IF_FALSE end jump if false |
| 6980 | * ... body ... |
| 6981 | * JUMP top Jump back to repeat |
| 6982 | * end: |
| 6983 | * |
| 6984 | */ |
| 6985 | static char_u * |
| 6986 | compile_while(char_u *arg, cctx_T *cctx) |
| 6987 | { |
| 6988 | char_u *p = arg; |
| 6989 | garray_T *instr = &cctx->ctx_instr; |
| 6990 | scope_T *scope; |
| 6991 | |
| 6992 | scope = new_scope(cctx, WHILE_SCOPE); |
| 6993 | if (scope == NULL) |
| 6994 | return NULL; |
| 6995 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6996 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6997 | |
| 6998 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6999 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7000 | return NULL; |
| 7001 | |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 7002 | if (bool_on_stack(cctx) == FAIL) |
| 7003 | return FAIL; |
| 7004 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7005 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7006 | 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] | 7007 | JUMP_IF_FALSE, cctx) == FAIL) |
| 7008 | return FAIL; |
| 7009 | |
| 7010 | return p; |
| 7011 | } |
| 7012 | |
| 7013 | /* |
| 7014 | * compile "endwhile" |
| 7015 | */ |
| 7016 | static char_u * |
| 7017 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 7018 | { |
| 7019 | scope_T *scope = cctx->ctx_scope; |
| 7020 | |
| 7021 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 7022 | { |
| 7023 | emsg(_(e_while)); |
| 7024 | return NULL; |
| 7025 | } |
| 7026 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7027 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7028 | |
| 7029 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7030 | 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] | 7031 | |
| 7032 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 7033 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7034 | 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] | 7035 | |
| 7036 | vim_free(scope); |
| 7037 | |
| 7038 | return arg; |
| 7039 | } |
| 7040 | |
| 7041 | /* |
| 7042 | * compile "continue" |
| 7043 | */ |
| 7044 | static char_u * |
| 7045 | compile_continue(char_u *arg, cctx_T *cctx) |
| 7046 | { |
| 7047 | scope_T *scope = cctx->ctx_scope; |
| 7048 | |
| 7049 | for (;;) |
| 7050 | { |
| 7051 | if (scope == NULL) |
| 7052 | { |
| 7053 | emsg(_(e_continue)); |
| 7054 | return NULL; |
| 7055 | } |
| 7056 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 7057 | break; |
| 7058 | scope = scope->se_outer; |
| 7059 | } |
| 7060 | |
| 7061 | // Jump back to the FOR or WHILE instruction. |
| 7062 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7063 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 7064 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7065 | return arg; |
| 7066 | } |
| 7067 | |
| 7068 | /* |
| 7069 | * compile "break" |
| 7070 | */ |
| 7071 | static char_u * |
| 7072 | compile_break(char_u *arg, cctx_T *cctx) |
| 7073 | { |
| 7074 | scope_T *scope = cctx->ctx_scope; |
| 7075 | endlabel_T **el; |
| 7076 | |
| 7077 | for (;;) |
| 7078 | { |
| 7079 | if (scope == NULL) |
| 7080 | { |
| 7081 | emsg(_(e_break)); |
| 7082 | return NULL; |
| 7083 | } |
| 7084 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 7085 | break; |
| 7086 | scope = scope->se_outer; |
| 7087 | } |
| 7088 | |
| 7089 | // Jump to the end of the FOR or WHILE loop. |
| 7090 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7091 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7092 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7093 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7094 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 7095 | return FAIL; |
| 7096 | |
| 7097 | return arg; |
| 7098 | } |
| 7099 | |
| 7100 | /* |
| 7101 | * compile "{" start of block |
| 7102 | */ |
| 7103 | static char_u * |
| 7104 | compile_block(char_u *arg, cctx_T *cctx) |
| 7105 | { |
| 7106 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 7107 | return NULL; |
| 7108 | return skipwhite(arg + 1); |
| 7109 | } |
| 7110 | |
| 7111 | /* |
| 7112 | * compile end of block: drop one scope |
| 7113 | */ |
| 7114 | static void |
| 7115 | compile_endblock(cctx_T *cctx) |
| 7116 | { |
| 7117 | scope_T *scope = cctx->ctx_scope; |
| 7118 | |
| 7119 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7120 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7121 | vim_free(scope); |
| 7122 | } |
| 7123 | |
| 7124 | /* |
| 7125 | * compile "try" |
| 7126 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 7127 | * finally. |
| 7128 | * Creates another scope for the "try" block itself. |
| 7129 | * TRY instruction sets up exception handling at runtime. |
| 7130 | * |
| 7131 | * "try" |
| 7132 | * TRY -> catch1, -> finally push trystack entry |
| 7133 | * ... try block |
| 7134 | * "throw {exception}" |
| 7135 | * EVAL {exception} |
| 7136 | * THROW create exception |
| 7137 | * ... try block |
| 7138 | * " catch {expr}" |
| 7139 | * JUMP -> finally |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 7140 | * catch1: PUSH exception |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7141 | * EVAL {expr} |
| 7142 | * MATCH |
| 7143 | * JUMP nomatch -> catch2 |
| 7144 | * CATCH remove exception |
| 7145 | * ... catch block |
| 7146 | * " catch" |
| 7147 | * JUMP -> finally |
| 7148 | * catch2: CATCH remove exception |
| 7149 | * ... catch block |
| 7150 | * " finally" |
| 7151 | * finally: |
| 7152 | * ... finally block |
| 7153 | * " endtry" |
| 7154 | * ENDTRY pop trystack entry, may rethrow |
| 7155 | */ |
| 7156 | static char_u * |
| 7157 | compile_try(char_u *arg, cctx_T *cctx) |
| 7158 | { |
| 7159 | garray_T *instr = &cctx->ctx_instr; |
| 7160 | scope_T *try_scope; |
| 7161 | scope_T *scope; |
| 7162 | |
| 7163 | // scope that holds the jumps that go to catch/finally/endtry |
| 7164 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 7165 | if (try_scope == NULL) |
| 7166 | return NULL; |
| 7167 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7168 | if (cctx->ctx_skip != SKIP_YES) |
| 7169 | { |
| 7170 | // "catch" is set when the first ":catch" is found. |
| 7171 | // "finally" is set when ":finally" or ":endtry" is found |
| 7172 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
| 7173 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 7174 | return NULL; |
| 7175 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7176 | |
| 7177 | // scope for the try block itself |
| 7178 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 7179 | if (scope == NULL) |
| 7180 | return NULL; |
| 7181 | |
| 7182 | return arg; |
| 7183 | } |
| 7184 | |
| 7185 | /* |
| 7186 | * compile "catch {expr}" |
| 7187 | */ |
| 7188 | static char_u * |
| 7189 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 7190 | { |
| 7191 | scope_T *scope = cctx->ctx_scope; |
| 7192 | garray_T *instr = &cctx->ctx_instr; |
| 7193 | char_u *p; |
| 7194 | isn_T *isn; |
| 7195 | |
| 7196 | // end block scope from :try or :catch |
| 7197 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 7198 | compile_endblock(cctx); |
| 7199 | scope = cctx->ctx_scope; |
| 7200 | |
| 7201 | // Error if not in a :try scope |
| 7202 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 7203 | { |
| 7204 | emsg(_(e_catch)); |
| 7205 | return NULL; |
| 7206 | } |
| 7207 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7208 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7209 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7210 | emsg(_(e_catch_unreachable_after_catch_all)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7211 | return NULL; |
| 7212 | } |
| 7213 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7214 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7215 | { |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7216 | // Jump from end of previous block to :finally or :endtry |
| 7217 | if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label, |
| 7218 | JUMP_ALWAYS, cctx) == FAIL) |
| 7219 | return NULL; |
| 7220 | |
| 7221 | // End :try or :catch scope: set value in ISN_TRY instruction |
| 7222 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
| 7223 | if (isn->isn_arg.try.try_catch == 0) |
| 7224 | isn->isn_arg.try.try_catch = instr->ga_len; |
| 7225 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 7226 | { |
| 7227 | // Previous catch without match jumps here |
| 7228 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 7229 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 7230 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7231 | } |
| 7232 | |
| 7233 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7234 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7235 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7236 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 7237 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7238 | } |
| 7239 | else |
| 7240 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7241 | char_u *end; |
| 7242 | char_u *pat; |
| 7243 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 7244 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 7245 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7246 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7247 | // Push v:exception, push {expr} and MATCH |
| 7248 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 7249 | |
Bram Moolenaar | d93a7fc | 2021-01-04 12:42:13 +0100 | [diff] [blame] | 7250 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped, NULL); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7251 | if (*end != *p) |
| 7252 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 7253 | semsg(_(e_separator_mismatch_str), p); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7254 | vim_free(tofree); |
| 7255 | return FAIL; |
| 7256 | } |
| 7257 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 7258 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7259 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 7260 | len = (int)(end - tofree); |
| 7261 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7262 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 7263 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 7264 | if (pat == NULL) |
| 7265 | return FAIL; |
| 7266 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 7267 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7268 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7269 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 7270 | return NULL; |
| 7271 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7272 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7273 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 7274 | return NULL; |
| 7275 | } |
| 7276 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7277 | if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_CATCH) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7278 | return NULL; |
| 7279 | |
| 7280 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 7281 | return NULL; |
| 7282 | return p; |
| 7283 | } |
| 7284 | |
| 7285 | static char_u * |
| 7286 | compile_finally(char_u *arg, cctx_T *cctx) |
| 7287 | { |
| 7288 | scope_T *scope = cctx->ctx_scope; |
| 7289 | garray_T *instr = &cctx->ctx_instr; |
| 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_finally)); |
| 7301 | return NULL; |
| 7302 | } |
| 7303 | |
| 7304 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7305 | 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] | 7306 | if (isn->isn_arg.try.try_finally != 0) |
| 7307 | { |
| 7308 | emsg(_(e_finally_dup)); |
| 7309 | return NULL; |
| 7310 | } |
| 7311 | |
| 7312 | // 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] | 7313 | 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] | 7314 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7315 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7316 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7317 | { |
| 7318 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 7319 | 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] | 7320 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 7321 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7322 | } |
| 7323 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7324 | // TODO: set index in ts_finally_label jumps |
| 7325 | |
| 7326 | return arg; |
| 7327 | } |
| 7328 | |
| 7329 | static char_u * |
| 7330 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 7331 | { |
| 7332 | scope_T *scope = cctx->ctx_scope; |
| 7333 | garray_T *instr = &cctx->ctx_instr; |
| 7334 | isn_T *isn; |
| 7335 | |
| 7336 | // end block scope from :catch or :finally |
| 7337 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 7338 | compile_endblock(cctx); |
| 7339 | scope = cctx->ctx_scope; |
| 7340 | |
| 7341 | // Error if not in a :try scope |
| 7342 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 7343 | { |
| 7344 | if (scope == NULL) |
| 7345 | emsg(_(e_no_endtry)); |
| 7346 | else if (scope->se_type == WHILE_SCOPE) |
| 7347 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 7348 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7349 | emsg(_(e_endfor)); |
| 7350 | else |
| 7351 | emsg(_(e_endif)); |
| 7352 | return NULL; |
| 7353 | } |
| 7354 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7355 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7356 | { |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7357 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_try_label; |
| 7358 | if (isn->isn_arg.try.try_catch == 0 |
| 7359 | && isn->isn_arg.try.try_finally == 0) |
| 7360 | { |
| 7361 | emsg(_(e_missing_catch_or_finally)); |
| 7362 | return NULL; |
| 7363 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7364 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7365 | // Fill in the "end" label in jumps at the end of the blocks, if not |
| 7366 | // done by ":finally". |
| 7367 | 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] | 7368 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7369 | // End :catch or :finally scope: set value in ISN_TRY instruction |
| 7370 | if (isn->isn_arg.try.try_catch == 0) |
| 7371 | isn->isn_arg.try.try_catch = instr->ga_len; |
| 7372 | if (isn->isn_arg.try.try_finally == 0) |
| 7373 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 7374 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7375 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 7376 | { |
| 7377 | // Last catch without match jumps here |
| 7378 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 7379 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 7380 | } |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 7381 | } |
| 7382 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7383 | compile_endblock(cctx); |
| 7384 | |
Bram Moolenaar | 69f7050 | 2021-01-01 16:10:46 +0100 | [diff] [blame] | 7385 | if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_ENDTRY) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7386 | return NULL; |
| 7387 | return arg; |
| 7388 | } |
| 7389 | |
| 7390 | /* |
| 7391 | * compile "throw {expr}" |
| 7392 | */ |
| 7393 | static char_u * |
| 7394 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 7395 | { |
| 7396 | char_u *p = skipwhite(arg); |
| 7397 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7398 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7399 | return NULL; |
| 7400 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 7401 | return NULL; |
| 7402 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 7403 | return NULL; |
| 7404 | |
| 7405 | return p; |
| 7406 | } |
| 7407 | |
| 7408 | /* |
| 7409 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7410 | * compile "echomsg expr" |
| 7411 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7412 | * compile "execute expr" |
| 7413 | */ |
| 7414 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7415 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7416 | { |
| 7417 | char_u *p = arg; |
Bram Moolenaar | e498429 | 2020-12-13 14:19:25 +0100 | [diff] [blame] | 7418 | char_u *prev = arg; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7419 | int count = 0; |
| 7420 | |
| 7421 | for (;;) |
| 7422 | { |
Bram Moolenaar | e498429 | 2020-12-13 14:19:25 +0100 | [diff] [blame] | 7423 | if (ends_excmd2(prev, p)) |
| 7424 | break; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7425 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7426 | return NULL; |
| 7427 | ++count; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 7428 | prev = p; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7429 | p = skipwhite(p); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7430 | } |
| 7431 | |
Bram Moolenaar | e498429 | 2020-12-13 14:19:25 +0100 | [diff] [blame] | 7432 | if (count > 0) |
| 7433 | { |
| 7434 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 7435 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 7436 | else if (cmdidx == CMD_execute) |
| 7437 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 7438 | else if (cmdidx == CMD_echomsg) |
| 7439 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 7440 | else |
| 7441 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
| 7442 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7443 | return p; |
| 7444 | } |
| 7445 | |
| 7446 | /* |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 7447 | * 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] | 7448 | * instruction to compute it and return OK. |
| 7449 | * Otherwise return FAIL, the caller must deal with any range. |
| 7450 | */ |
| 7451 | static int |
| 7452 | compile_variable_range(exarg_T *eap, cctx_T *cctx) |
| 7453 | { |
| 7454 | char_u *range_end = skip_range(eap->cmd, TRUE, NULL); |
| 7455 | char_u *p = skipdigits(eap->cmd); |
| 7456 | |
| 7457 | if (p == range_end) |
| 7458 | return FAIL; |
| 7459 | return generate_RANGE(cctx, vim_strnsave(eap->cmd, range_end - eap->cmd)); |
| 7460 | } |
| 7461 | |
| 7462 | /* |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7463 | * :put r |
| 7464 | * :put ={expr} |
| 7465 | */ |
| 7466 | static char_u * |
| 7467 | compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 7468 | { |
| 7469 | char_u *line = arg; |
| 7470 | linenr_T lnum; |
| 7471 | char *errormsg; |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 7472 | int above = eap->forceit; |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7473 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7474 | eap->regname = *line; |
| 7475 | |
| 7476 | if (eap->regname == '=') |
| 7477 | { |
| 7478 | char_u *p = line + 1; |
| 7479 | |
| 7480 | if (compile_expr0(&p, cctx) == FAIL) |
| 7481 | return NULL; |
| 7482 | line = p; |
| 7483 | } |
| 7484 | else if (eap->regname != NUL) |
| 7485 | ++line; |
| 7486 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 7487 | if (compile_variable_range(eap, cctx) == OK) |
| 7488 | { |
| 7489 | lnum = above ? LNUM_VARIABLE_RANGE_ABOVE : LNUM_VARIABLE_RANGE; |
| 7490 | } |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7491 | else |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 7492 | { |
| 7493 | // Either no range or a number. |
| 7494 | // "errormsg" will not be set because the range is ADDR_LINES. |
| 7495 | if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL) |
Bram Moolenaar | 399ea81 | 2020-12-15 21:28:57 +0100 | [diff] [blame] | 7496 | // cannot happen |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 7497 | return NULL; |
| 7498 | if (eap->addr_count == 0) |
| 7499 | lnum = -1; |
| 7500 | else |
| 7501 | lnum = eap->line2; |
| 7502 | if (above) |
| 7503 | --lnum; |
| 7504 | } |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7505 | |
| 7506 | generate_PUT(cctx, eap->regname, lnum); |
| 7507 | return line; |
| 7508 | } |
| 7509 | |
| 7510 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7511 | * A command that is not compiled, execute with legacy code. |
| 7512 | */ |
| 7513 | static char_u * |
| 7514 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 7515 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 7516 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 7517 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7518 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7519 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7520 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7521 | goto theend; |
| 7522 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 7523 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7524 | { |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 7525 | long argt = eap->argt; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7526 | int usefilter = FALSE; |
| 7527 | |
| 7528 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 7529 | |
| 7530 | // If the command can be followed by a bar, find the bar and truncate |
| 7531 | // it, so that the following command can be compiled. |
| 7532 | // The '|' is overwritten with a NUL, it is put back below. |
| 7533 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 7534 | && *eap->arg == '!') |
| 7535 | // :w !filter or :r !filter or :r! filter |
| 7536 | usefilter = TRUE; |
| 7537 | if ((argt & EX_TRLBAR) && !usefilter) |
| 7538 | { |
Bram Moolenaar | b8a9296 | 2020-08-20 18:02:47 +0200 | [diff] [blame] | 7539 | eap->argt = argt; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7540 | separate_nextcmd(eap); |
| 7541 | if (eap->nextcmd != NULL) |
| 7542 | nextcmd = eap->nextcmd; |
| 7543 | } |
Bram Moolenaar | a11919f | 2021-01-02 19:44:56 +0100 | [diff] [blame] | 7544 | else if (eap->cmdidx == CMD_wincmd) |
| 7545 | { |
| 7546 | p = eap->arg; |
| 7547 | if (*p != NUL) |
| 7548 | ++p; |
| 7549 | if (*p == 'g' || *p == Ctrl_G) |
| 7550 | ++p; |
| 7551 | p = skipwhite(p); |
| 7552 | if (*p == '|') |
| 7553 | { |
| 7554 | *p = NUL; |
| 7555 | nextcmd = p + 1; |
| 7556 | } |
| 7557 | } |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7558 | } |
| 7559 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 7560 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 7561 | { |
| 7562 | // expand filename in "syntax include [@group] filename" |
| 7563 | has_expr = TRUE; |
| 7564 | eap->arg = skipwhite(eap->arg + 7); |
| 7565 | if (*eap->arg == '@') |
| 7566 | eap->arg = skiptowhite(eap->arg); |
| 7567 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7568 | |
Bram Moolenaar | 56ce9ea | 2020-12-25 18:35:29 +0100 | [diff] [blame] | 7569 | if ((eap->cmdidx == CMD_global || eap->cmdidx == CMD_vglobal) |
| 7570 | && STRLEN(eap->arg) > 4) |
| 7571 | { |
| 7572 | int delim = *eap->arg; |
| 7573 | |
Bram Moolenaar | d93a7fc | 2021-01-04 12:42:13 +0100 | [diff] [blame] | 7574 | p = skip_regexp_ex(eap->arg + 1, delim, TRUE, NULL, NULL, NULL); |
Bram Moolenaar | 56ce9ea | 2020-12-25 18:35:29 +0100 | [diff] [blame] | 7575 | if (*p == delim) |
| 7576 | { |
| 7577 | eap->arg = p + 1; |
| 7578 | has_expr = TRUE; |
| 7579 | } |
| 7580 | } |
| 7581 | |
Bram Moolenaar | ecac591 | 2021-01-05 19:23:28 +0100 | [diff] [blame] | 7582 | if (eap->cmdidx == CMD_folddoopen || eap->cmdidx == CMD_folddoclosed) |
| 7583 | { |
| 7584 | // TODO: should only expand when appropriate for the command |
| 7585 | eap->arg = skiptowhite(eap->arg); |
| 7586 | has_expr = TRUE; |
| 7587 | } |
| 7588 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 7589 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7590 | { |
| 7591 | int count = 0; |
| 7592 | char_u *start = skipwhite(line); |
| 7593 | |
| 7594 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 7595 | // PUSHS ":cmd xxx" |
| 7596 | // eval expr1 |
| 7597 | // PUSHS "yyy" |
| 7598 | // eval expr2 |
| 7599 | // PUSHS "zzz" |
| 7600 | // EXECCONCAT 5 |
| 7601 | for (;;) |
| 7602 | { |
| 7603 | if (p > start) |
| 7604 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 7605 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7606 | ++count; |
| 7607 | } |
| 7608 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7609 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7610 | return NULL; |
| 7611 | may_generate_2STRING(-1, cctx); |
| 7612 | ++count; |
| 7613 | p = skipwhite(p); |
| 7614 | if (*p != '`') |
| 7615 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7616 | emsg(_(e_missing_backtick)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7617 | return NULL; |
| 7618 | } |
| 7619 | start = p + 1; |
| 7620 | |
| 7621 | p = (char_u *)strstr((char *)start, "`="); |
| 7622 | if (p == NULL) |
| 7623 | { |
| 7624 | if (*skipwhite(start) != NUL) |
| 7625 | { |
| 7626 | generate_PUSHS(cctx, vim_strsave(start)); |
| 7627 | ++count; |
| 7628 | } |
| 7629 | break; |
| 7630 | } |
| 7631 | } |
| 7632 | generate_EXECCONCAT(cctx, count); |
| 7633 | } |
| 7634 | else |
| 7635 | generate_EXEC(cctx, line); |
| 7636 | |
| 7637 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 7638 | if (*nextcmd != NUL) |
| 7639 | { |
| 7640 | // the parser expects a pointer to the bar, put it back |
| 7641 | --nextcmd; |
| 7642 | *nextcmd = '|'; |
| 7643 | } |
| 7644 | |
| 7645 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7646 | } |
| 7647 | |
| 7648 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7649 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7650 | * 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] | 7651 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7652 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7653 | add_def_function(ufunc_T *ufunc) |
| 7654 | { |
| 7655 | dfunc_T *dfunc; |
| 7656 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7657 | if (def_functions.ga_len == 0) |
| 7658 | { |
| 7659 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 7660 | // wasn't set. |
| 7661 | if (ga_grow(&def_functions, 1) == FAIL) |
| 7662 | return FAIL; |
| 7663 | ++def_functions.ga_len; |
| 7664 | } |
| 7665 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7666 | // Add the function to "def_functions". |
| 7667 | if (ga_grow(&def_functions, 1) == FAIL) |
| 7668 | return FAIL; |
| 7669 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 7670 | CLEAR_POINTER(dfunc); |
| 7671 | dfunc->df_idx = def_functions.ga_len; |
| 7672 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 7673 | dfunc->df_ufunc = ufunc; |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 7674 | dfunc->df_name = vim_strsave(ufunc->uf_name); |
| 7675 | ++dfunc->df_refcount; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7676 | ++def_functions.ga_len; |
| 7677 | return OK; |
| 7678 | } |
| 7679 | |
| 7680 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7681 | * After ex_function() has collected all the function lines: parse and compile |
| 7682 | * the lines into instructions. |
| 7683 | * Adds the function to "def_functions". |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 7684 | * When "check_return_type" is set then set ufunc->uf_ret_type to the type of |
| 7685 | * the return statement (used for lambda). When uf_ret_type is already set |
| 7686 | * then check that it matches. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7687 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7688 | * This can be used recursively through compile_lambda(), which may reallocate |
| 7689 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7690 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7691 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7692 | int |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 7693 | 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] | 7694 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7695 | char_u *line = NULL; |
| 7696 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7697 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7698 | cctx_T cctx; |
| 7699 | garray_T *instr; |
Bram Moolenaar | d66960b | 2020-10-30 20:46:26 +0100 | [diff] [blame] | 7700 | int did_emsg_before = did_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7701 | int ret = FAIL; |
| 7702 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | f4e8cdd | 2020-10-12 22:07:13 +0200 | [diff] [blame] | 7703 | int save_estack_compiling = estack_compiling; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7704 | int do_estack_push; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7705 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7706 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7707 | // When using a function that was compiled before: Free old instructions. |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 7708 | // The index is reused. Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7709 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7710 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 7711 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7712 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 7713 | delete_def_function_contents(dfunc, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7714 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7715 | else |
| 7716 | { |
| 7717 | if (add_def_function(ufunc) == FAIL) |
| 7718 | return FAIL; |
| 7719 | new_def_function = TRUE; |
| 7720 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7721 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 7722 | ufunc->uf_def_status = UF_COMPILING; |
| 7723 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7724 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7725 | cctx.ctx_ufunc = ufunc; |
| 7726 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7727 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7728 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 7729 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 7730 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 7731 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 7732 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 7733 | instr = &cctx.ctx_instr; |
| 7734 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7735 | // Set the context to the function, it may be compiled when called from |
| 7736 | // another script. Set the script version to the most modern one. |
| 7737 | // The line number will be set in next_line_from_context(). |
| 7738 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7739 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 7740 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7741 | // Make sure error messages are OK. |
| 7742 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 7743 | if (do_estack_push) |
| 7744 | estack_push_ufunc(ufunc, 1); |
Bram Moolenaar | f4e8cdd | 2020-10-12 22:07:13 +0200 | [diff] [blame] | 7745 | estack_compiling = TRUE; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7746 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7747 | if (ufunc->uf_def_args.ga_len > 0) |
| 7748 | { |
| 7749 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7750 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7751 | int i; |
| 7752 | char_u *arg; |
| 7753 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7754 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7755 | |
| 7756 | // Produce instructions for the default values of optional arguments. |
| 7757 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 7758 | // where to start when the function is called, depending on the number |
| 7759 | // of arguments. |
| 7760 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 7761 | if (ufunc->uf_def_arg_idx == NULL) |
| 7762 | goto erret; |
| 7763 | for (i = 0; i < count; ++i) |
| 7764 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7765 | garray_T *stack = &cctx.ctx_type_stack; |
| 7766 | type_T *val_type; |
| 7767 | int arg_idx = first_def_arg + i; |
| 7768 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7769 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 7770 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 7771 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7772 | goto erret; |
| 7773 | |
| 7774 | // If no type specified use the type of the default value. |
| 7775 | // Otherwise check that the default value type matches the |
| 7776 | // specified type. |
| 7777 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 7778 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7779 | { |
| 7780 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7781 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7782 | } |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 7783 | else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, |
| 7784 | TRUE, arg_idx + 1) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7785 | goto erret; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7786 | |
| 7787 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7788 | goto erret; |
| 7789 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7790 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7791 | |
| 7792 | if (did_set_arg_type) |
| 7793 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7794 | } |
| 7795 | |
| 7796 | /* |
| 7797 | * Loop over all the lines of the function and generate instructions. |
| 7798 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7799 | for (;;) |
| 7800 | { |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 7801 | exarg_T ea; |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 7802 | int starts_with_colon = FALSE; |
| 7803 | char_u *cmd; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 7804 | cmdmod_T local_cmdmod; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7805 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7806 | // Bail out on the first error to avoid a flood of errors and report |
| 7807 | // the right line number when inside try/catch. |
Bram Moolenaar | d66960b | 2020-10-30 20:46:26 +0100 | [diff] [blame] | 7808 | if (did_emsg_before != did_emsg) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7809 | goto erret; |
| 7810 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7811 | if (line != NULL && *line == '|') |
| 7812 | // the line continues after a '|' |
| 7813 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 7814 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7815 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 7816 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7817 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 7818 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7819 | goto erret; |
| 7820 | } |
| 7821 | else |
| 7822 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 7823 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 7824 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7825 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7826 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7827 | } |
| 7828 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7829 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7830 | ea.cmdlinep = &line; |
| 7831 | ea.cmd = skipwhite(line); |
| 7832 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7833 | // Some things can be recognized by the first character. |
| 7834 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7835 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7836 | case '#': |
Bram Moolenaar | e0de171 | 2020-12-02 17:36:54 +0100 | [diff] [blame] | 7837 | // "#" starts a comment |
| 7838 | line = (char_u *)""; |
| 7839 | continue; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7840 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7841 | case '}': |
| 7842 | { |
| 7843 | // "}" ends a block scope |
| 7844 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7845 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7846 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7847 | if (stype == BLOCK_SCOPE) |
| 7848 | { |
| 7849 | compile_endblock(&cctx); |
| 7850 | line = ea.cmd; |
| 7851 | } |
| 7852 | else |
| 7853 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7854 | emsg(_(e_using_rcurly_outside_if_block_scope)); |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7855 | goto erret; |
| 7856 | } |
| 7857 | if (line != NULL) |
| 7858 | line = skipwhite(ea.cmd + 1); |
| 7859 | continue; |
| 7860 | } |
| 7861 | |
| 7862 | case '{': |
| 7863 | // "{" starts a block scope |
| 7864 | // "{'a': 1}->func() is something else |
| 7865 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7866 | { |
| 7867 | line = compile_block(ea.cmd, &cctx); |
| 7868 | continue; |
| 7869 | } |
| 7870 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7871 | } |
| 7872 | |
| 7873 | /* |
| 7874 | * COMMAND MODIFIERS |
| 7875 | */ |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 7876 | cctx.ctx_has_cmdmod = FALSE; |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 7877 | if (parse_command_modifiers(&ea, &errormsg, &local_cmdmod, FALSE) |
| 7878 | == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7879 | { |
| 7880 | if (errormsg != NULL) |
| 7881 | goto erret; |
| 7882 | // empty line or comment |
| 7883 | line = (char_u *)""; |
| 7884 | continue; |
| 7885 | } |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 7886 | generate_cmdmods(&cctx, &local_cmdmod); |
| 7887 | undo_cmdmod(&local_cmdmod); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7888 | |
Bram Moolenaar | e88c8e8 | 2020-11-01 17:03:37 +0100 | [diff] [blame] | 7889 | // Check if there was a colon after the last command modifier or before |
| 7890 | // the current position. |
| 7891 | for (p = ea.cmd; p >= line; --p) |
| 7892 | { |
| 7893 | if (*p == ':') |
| 7894 | starts_with_colon = TRUE; |
| 7895 | if (p < ea.cmd && !VIM_ISWHITE(*p)) |
| 7896 | break; |
| 7897 | } |
| 7898 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7899 | // Skip ":call" to get to the function name. |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 7900 | p = ea.cmd; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7901 | if (checkforcmd(&ea.cmd, "call", 3)) |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 7902 | { |
| 7903 | if (*ea.cmd == '(') |
| 7904 | // not for "call()" |
| 7905 | ea.cmd = p; |
| 7906 | else |
| 7907 | ea.cmd = skipwhite(ea.cmd); |
| 7908 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7909 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7910 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7911 | { |
Bram Moolenaar | 17126b1 | 2021-01-07 22:03:02 +0100 | [diff] [blame] | 7912 | int assign; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7913 | |
Bram Moolenaar | 17126b1 | 2021-01-07 22:03:02 +0100 | [diff] [blame] | 7914 | // Check for assignment after command modifiers. |
| 7915 | assign = may_compile_assignment(&ea, &line, &cctx); |
| 7916 | if (assign == OK) |
| 7917 | goto nextline; |
| 7918 | if (assign == FAIL) |
| 7919 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7920 | } |
| 7921 | |
| 7922 | /* |
| 7923 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7924 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7925 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7926 | cmd = ea.cmd; |
Bram Moolenaar | 7c5ad34 | 2020-08-12 15:48:55 +0200 | [diff] [blame] | 7927 | if (*cmd != '\'' || starts_with_colon) |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7928 | { |
Bram Moolenaar | 3bd8de4 | 2020-09-14 16:37:34 +0200 | [diff] [blame] | 7929 | ea.cmd = skip_range(ea.cmd, TRUE, NULL); |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 7930 | if (ea.cmd > cmd) |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7931 | { |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 7932 | if (!starts_with_colon) |
| 7933 | { |
Bram Moolenaar | 6e2c2c5 | 2020-12-25 19:25:45 +0100 | [diff] [blame] | 7934 | semsg(_(e_colon_required_before_range_str), cmd); |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 7935 | goto erret; |
| 7936 | } |
| 7937 | if (ends_excmd2(line, ea.cmd)) |
| 7938 | { |
| 7939 | // A range without a command: jump to the line. |
| 7940 | // TODO: compile to a more efficient command, possibly |
| 7941 | // calling parse_cmd_address(). |
| 7942 | ea.cmdidx = CMD_SIZE; |
| 7943 | line = compile_exec(line, &ea, &cctx); |
| 7944 | goto nextline; |
| 7945 | } |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7946 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7947 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7948 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | 709664c | 2020-12-12 14:33:41 +0100 | [diff] [blame] | 7949 | : (int (*)(char_u *, size_t, void *, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7950 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7951 | |
Bram Moolenaar | d1510ee | 2021-01-04 16:15:58 +0100 | [diff] [blame] | 7952 | if (p == NULL) |
| 7953 | { |
| 7954 | if (cctx.ctx_skip != SKIP_YES) |
| 7955 | emsg(_(e_ambiguous_use_of_user_defined_command)); |
| 7956 | goto erret; |
| 7957 | } |
| 7958 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7959 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 7960 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7961 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7962 | { |
| 7963 | line += STRLEN(line); |
Bram Moolenaar | f665e97 | 2020-12-05 19:17:16 +0100 | [diff] [blame] | 7964 | goto nextline; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7965 | } |
| 7966 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7967 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7968 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7969 | { |
Bram Moolenaar | 52c124d | 2020-12-20 21:43:35 +0100 | [diff] [blame] | 7970 | // CMD_var cannot happen, compile_assignment() above would be |
| 7971 | // used. Most likely an assignment to a non-existing variable. |
| 7972 | semsg(_(e_command_not_recognized_str), ea.cmd); |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7973 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7974 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7975 | } |
| 7976 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 7977 | if (cctx.ctx_had_return |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7978 | && ea.cmdidx != CMD_elseif |
| 7979 | && ea.cmdidx != CMD_else |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7980 | && ea.cmdidx != CMD_endif |
| 7981 | && ea.cmdidx != CMD_endfor |
| 7982 | && ea.cmdidx != CMD_endwhile |
| 7983 | && ea.cmdidx != CMD_catch |
| 7984 | && ea.cmdidx != CMD_finally |
| 7985 | && ea.cmdidx != CMD_endtry) |
| 7986 | { |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 7987 | emsg(_(e_unreachable_code_after_return)); |
| 7988 | goto erret; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7989 | } |
| 7990 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 7991 | p = skipwhite(p); |
| 7992 | if (ea.cmdidx != CMD_SIZE |
| 7993 | && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read) |
| 7994 | { |
Bram Moolenaar | 9b123d8 | 2020-09-14 22:39:11 +0200 | [diff] [blame] | 7995 | if (ea.cmdidx >= 0) |
| 7996 | ea.argt = excmd_get_argt(ea.cmdidx); |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 7997 | if ((ea.argt & EX_BANG) && *p == '!') |
| 7998 | { |
| 7999 | ea.forceit = TRUE; |
| 8000 | p = skipwhite(p + 1); |
| 8001 | } |
| 8002 | } |
| 8003 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8004 | switch (ea.cmdidx) |
| 8005 | { |
| 8006 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 8007 | ea.arg = p; |
| 8008 | line = compile_nested_function(&ea, &cctx); |
| 8009 | break; |
| 8010 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8011 | case CMD_function: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 8012 | // TODO: should we allow this, e.g. to declare a global |
| 8013 | // function? |
| 8014 | emsg(_(e_cannot_use_function_inside_def)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8015 | goto erret; |
| 8016 | |
| 8017 | case CMD_return: |
Bram Moolenaar | 9e68c32 | 2020-12-25 12:38:04 +0100 | [diff] [blame] | 8018 | line = compile_return(p, check_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8019 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8020 | break; |
| 8021 | |
| 8022 | case CMD_let: |
Bram Moolenaar | c58f545 | 2020-10-21 20:58:52 +0200 | [diff] [blame] | 8023 | emsg(_(e_cannot_use_let_in_vim9_script)); |
| 8024 | break; |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 8025 | case CMD_var: |
| 8026 | case CMD_final: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8027 | case CMD_const: |
| 8028 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 8029 | if (line == p) |
| 8030 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8031 | break; |
| 8032 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 8033 | case CMD_unlet: |
| 8034 | case CMD_unlockvar: |
| 8035 | case CMD_lockvar: |
| 8036 | line = compile_unletlock(p, &ea, &cctx); |
| 8037 | break; |
| 8038 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8039 | case CMD_import: |
| 8040 | line = compile_import(p, &cctx); |
| 8041 | break; |
| 8042 | |
| 8043 | case CMD_if: |
| 8044 | line = compile_if(p, &cctx); |
| 8045 | break; |
| 8046 | case CMD_elseif: |
| 8047 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8048 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8049 | break; |
| 8050 | case CMD_else: |
| 8051 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8052 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8053 | break; |
| 8054 | case CMD_endif: |
| 8055 | line = compile_endif(p, &cctx); |
| 8056 | break; |
| 8057 | |
| 8058 | case CMD_while: |
| 8059 | line = compile_while(p, &cctx); |
| 8060 | break; |
| 8061 | case CMD_endwhile: |
| 8062 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8063 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8064 | break; |
| 8065 | |
| 8066 | case CMD_for: |
| 8067 | line = compile_for(p, &cctx); |
| 8068 | break; |
| 8069 | case CMD_endfor: |
| 8070 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8071 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8072 | break; |
| 8073 | case CMD_continue: |
| 8074 | line = compile_continue(p, &cctx); |
| 8075 | break; |
| 8076 | case CMD_break: |
| 8077 | line = compile_break(p, &cctx); |
| 8078 | break; |
| 8079 | |
| 8080 | case CMD_try: |
| 8081 | line = compile_try(p, &cctx); |
| 8082 | break; |
| 8083 | case CMD_catch: |
| 8084 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8085 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8086 | break; |
| 8087 | case CMD_finally: |
| 8088 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8089 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8090 | break; |
| 8091 | case CMD_endtry: |
| 8092 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8093 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8094 | break; |
| 8095 | case CMD_throw: |
| 8096 | line = compile_throw(p, &cctx); |
| 8097 | break; |
| 8098 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 8099 | case CMD_eval: |
| 8100 | if (compile_expr0(&p, &cctx) == FAIL) |
| 8101 | goto erret; |
| 8102 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 8103 | // drop the result |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 8104 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 8105 | |
| 8106 | line = skipwhite(p); |
| 8107 | break; |
| 8108 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8109 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8110 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 8111 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 8112 | case CMD_echomsg: |
| 8113 | case CMD_echoerr: |
| 8114 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 8115 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8116 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 8117 | case CMD_put: |
| 8118 | ea.cmd = cmd; |
| 8119 | line = compile_put(p, &ea, &cctx); |
| 8120 | break; |
| 8121 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 8122 | // TODO: any other commands with an expression argument? |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 8123 | |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 8124 | case CMD_append: |
| 8125 | case CMD_change: |
| 8126 | case CMD_insert: |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 8127 | case CMD_t: |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 8128 | case CMD_xit: |
| 8129 | not_in_vim9(&ea); |
| 8130 | goto erret; |
| 8131 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 8132 | case CMD_SIZE: |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 8133 | if (cctx.ctx_skip != SKIP_YES) |
| 8134 | { |
| 8135 | semsg(_(e_invalid_command_str), ea.cmd); |
| 8136 | goto erret; |
| 8137 | } |
| 8138 | // We don't check for a next command here. |
| 8139 | line = (char_u *)""; |
| 8140 | break; |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 8141 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8142 | default: |
Bram Moolenaar | 5163fcc | 2020-08-27 23:37:09 +0200 | [diff] [blame] | 8143 | if (cctx.ctx_skip == SKIP_YES) |
| 8144 | { |
| 8145 | // We don't check for a next command here. |
| 8146 | line = (char_u *)""; |
| 8147 | } |
| 8148 | else |
| 8149 | { |
| 8150 | // Not recognized, execute with do_cmdline_cmd(). |
| 8151 | ea.arg = p; |
| 8152 | line = compile_exec(line, &ea, &cctx); |
| 8153 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8154 | break; |
| 8155 | } |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 8156 | nextline: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8157 | if (line == NULL) |
| 8158 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 8159 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8160 | |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 8161 | // Undo any command modifiers. |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 8162 | generate_undo_cmdmods(&cctx); |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 8163 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8164 | if (cctx.ctx_type_stack.ga_len < 0) |
| 8165 | { |
| 8166 | iemsg("Type stack underflow"); |
| 8167 | goto erret; |
| 8168 | } |
| 8169 | } |
| 8170 | |
| 8171 | if (cctx.ctx_scope != NULL) |
| 8172 | { |
| 8173 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 8174 | emsg(_(e_endif)); |
| 8175 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 8176 | emsg(_(e_endwhile)); |
| 8177 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 8178 | emsg(_(e_endfor)); |
| 8179 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 8180 | emsg(_(e_missing_rcurly)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8181 | goto erret; |
| 8182 | } |
| 8183 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 8184 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8185 | { |
| 8186 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 8187 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 8188 | emsg(_(e_missing_return_statement)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8189 | goto erret; |
| 8190 | } |
| 8191 | |
| 8192 | // Return zero if there is no return at the end. |
| 8193 | generate_PUSHNR(&cctx, 0); |
| 8194 | generate_instr(&cctx, ISN_RETURN); |
| 8195 | } |
| 8196 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 8197 | { |
| 8198 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8199 | + ufunc->uf_dfunc_idx; |
| 8200 | dfunc->df_deleted = FALSE; |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 8201 | dfunc->df_script_seq = current_sctx.sc_seq; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 8202 | dfunc->df_instr = instr->ga_data; |
| 8203 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 8204 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 8205 | dfunc->df_has_closure = cctx.ctx_has_closure; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 8206 | if (cctx.ctx_outer_used) |
| 8207 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8208 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 8209 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8210 | |
| 8211 | ret = OK; |
| 8212 | |
| 8213 | erret: |
| 8214 | if (ret == FAIL) |
| 8215 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8216 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 8217 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8218 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8219 | |
| 8220 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 8221 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8222 | ga_clear(instr); |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8223 | VIM_CLEAR(dfunc->df_name); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8224 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 8225 | // If using the last entry in the table and it was added above, we |
| 8226 | // might as well remove it. |
| 8227 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 8228 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 8229 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8230 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 8231 | ufunc->uf_dfunc_idx = 0; |
| 8232 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8233 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8234 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 8235 | while (cctx.ctx_scope != NULL) |
| 8236 | drop_scope(&cctx); |
| 8237 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8238 | // Don't execute this function body. |
| 8239 | ga_clear_strings(&ufunc->uf_lines); |
| 8240 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8241 | if (errormsg != NULL) |
| 8242 | emsg(errormsg); |
Bram Moolenaar | d66960b | 2020-10-30 20:46:26 +0100 | [diff] [blame] | 8243 | else if (did_emsg == did_emsg_before) |
Bram Moolenaar | e13bdec | 2020-10-16 23:16:47 +0200 | [diff] [blame] | 8244 | emsg(_(e_compiling_def_function_failed)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8245 | } |
| 8246 | |
| 8247 | current_sctx = save_current_sctx; |
Bram Moolenaar | f4e8cdd | 2020-10-12 22:07:13 +0200 | [diff] [blame] | 8248 | estack_compiling = save_estack_compiling; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 8249 | if (do_estack_push) |
| 8250 | estack_pop(); |
| 8251 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8252 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 8253 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8254 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 8255 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8256 | } |
| 8257 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 8258 | void |
| 8259 | set_function_type(ufunc_T *ufunc) |
| 8260 | { |
| 8261 | int varargs = ufunc->uf_va_name != NULL; |
| 8262 | int argcount = ufunc->uf_args.ga_len; |
| 8263 | |
| 8264 | // Create a type for the function, with the return type and any |
| 8265 | // argument types. |
| 8266 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 8267 | // The type is included in "tt_args". |
| 8268 | if (argcount > 0 || varargs) |
| 8269 | { |
| 8270 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 8271 | argcount, &ufunc->uf_type_list); |
| 8272 | // Add argument types to the function type. |
| 8273 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 8274 | argcount + varargs, |
| 8275 | &ufunc->uf_type_list) == FAIL) |
| 8276 | return; |
| 8277 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 8278 | ufunc->uf_func_type->tt_min_argcount = |
| 8279 | argcount - ufunc->uf_def_args.ga_len; |
| 8280 | if (ufunc->uf_arg_types == NULL) |
| 8281 | { |
| 8282 | int i; |
| 8283 | |
| 8284 | // lambda does not have argument types. |
| 8285 | for (i = 0; i < argcount; ++i) |
| 8286 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 8287 | } |
| 8288 | else |
| 8289 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 8290 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 8291 | if (varargs) |
| 8292 | { |
| 8293 | ufunc->uf_func_type->tt_args[argcount] = |
| 8294 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 8295 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 8296 | } |
| 8297 | } |
| 8298 | else |
| 8299 | // No arguments, can use a predefined type. |
| 8300 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 8301 | argcount, &ufunc->uf_type_list); |
| 8302 | } |
| 8303 | |
| 8304 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8305 | /* |
| 8306 | * Delete an instruction, free what it contains. |
| 8307 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8308 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8309 | delete_instr(isn_T *isn) |
| 8310 | { |
| 8311 | switch (isn->isn_type) |
| 8312 | { |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8313 | case ISN_DEF: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8314 | case ISN_EXEC: |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 8315 | case ISN_LOADAUTO: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8316 | case ISN_LOADB: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8317 | case ISN_LOADENV: |
| 8318 | case ISN_LOADG: |
| 8319 | case ISN_LOADOPT: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8320 | case ISN_LOADT: |
| 8321 | case ISN_LOADW: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8322 | case ISN_PUSHEXC: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8323 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8324 | case ISN_PUSHS: |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 8325 | case ISN_RANGE: |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 8326 | case ISN_STOREAUTO: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8327 | case ISN_STOREB: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 8328 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8329 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 8330 | case ISN_STORET: |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 8331 | case ISN_STOREW: |
| 8332 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8333 | vim_free(isn->isn_arg.string); |
| 8334 | break; |
| 8335 | |
| 8336 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 8337 | case ISN_STORES: |
| 8338 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8339 | break; |
| 8340 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 8341 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 8342 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 8343 | vim_free(isn->isn_arg.unlet.ul_name); |
| 8344 | break; |
| 8345 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8346 | case ISN_STOREOPT: |
| 8347 | vim_free(isn->isn_arg.storeopt.so_name); |
| 8348 | break; |
| 8349 | |
| 8350 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 8351 | blob_unref(isn->isn_arg.blob); |
| 8352 | break; |
| 8353 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8354 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 8355 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8356 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 8357 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8358 | break; |
| 8359 | |
| 8360 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 8361 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8362 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 8363 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 8364 | break; |
| 8365 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8366 | case ISN_UCALL: |
| 8367 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 8368 | break; |
| 8369 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 8370 | case ISN_FUNCREF: |
| 8371 | { |
| 8372 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8373 | + isn->isn_arg.funcref.fr_func; |
Bram Moolenaar | 077a423 | 2020-12-22 18:33:27 +0100 | [diff] [blame] | 8374 | ufunc_T *ufunc = dfunc->df_ufunc; |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 8375 | |
Bram Moolenaar | 077a423 | 2020-12-22 18:33:27 +0100 | [diff] [blame] | 8376 | if (ufunc != NULL && func_name_refcount(ufunc->uf_name)) |
| 8377 | func_ptr_unref(ufunc); |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 8378 | } |
| 8379 | break; |
| 8380 | |
| 8381 | case ISN_DCALL: |
| 8382 | { |
| 8383 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8384 | + isn->isn_arg.dfunc.cdf_idx; |
| 8385 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 8386 | if (dfunc->df_ufunc != NULL |
| 8387 | && func_name_refcount(dfunc->df_ufunc->uf_name)) |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 8388 | func_ptr_unref(dfunc->df_ufunc); |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 8389 | } |
| 8390 | break; |
| 8391 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 8392 | case ISN_NEWFUNC: |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 8393 | { |
| 8394 | char_u *lambda = isn->isn_arg.newfunc.nf_lambda; |
| 8395 | ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL); |
| 8396 | |
| 8397 | if (ufunc != NULL) |
| 8398 | { |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8399 | unlink_def_function(ufunc); |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 8400 | func_ptr_unref(ufunc); |
| 8401 | } |
| 8402 | |
| 8403 | vim_free(lambda); |
| 8404 | vim_free(isn->isn_arg.newfunc.nf_global); |
| 8405 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 8406 | break; |
| 8407 | |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 8408 | case ISN_CHECKTYPE: |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 8409 | case ISN_SETTYPE: |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 8410 | free_type(isn->isn_arg.type.ct_type); |
| 8411 | break; |
| 8412 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 8413 | case ISN_CMDMOD: |
| 8414 | vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod |
| 8415 | ->cmod_filter_regmatch.regprog); |
| 8416 | vim_free(isn->isn_arg.cmdmod.cf_cmdmod); |
| 8417 | break; |
| 8418 | |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 8419 | case ISN_LOADSCRIPT: |
| 8420 | case ISN_STORESCRIPT: |
| 8421 | vim_free(isn->isn_arg.script.scriptref); |
| 8422 | break; |
| 8423 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8424 | case ISN_2BOOL: |
| 8425 | case ISN_2STRING: |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 8426 | case ISN_2STRING_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8427 | case ISN_ADDBLOB: |
| 8428 | case ISN_ADDLIST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8429 | case ISN_ANYINDEX: |
| 8430 | case ISN_ANYSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8431 | case ISN_BCALL: |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 8432 | case ISN_BLOBAPPEND: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8433 | case ISN_CATCH: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8434 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8435 | case ISN_CHECKNR: |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 8436 | case ISN_CMDMOD_REV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8437 | case ISN_COMPAREANY: |
| 8438 | case ISN_COMPAREBLOB: |
| 8439 | case ISN_COMPAREBOOL: |
| 8440 | case ISN_COMPAREDICT: |
| 8441 | case ISN_COMPAREFLOAT: |
| 8442 | case ISN_COMPAREFUNC: |
| 8443 | case ISN_COMPARELIST: |
| 8444 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8445 | case ISN_COMPARESPECIAL: |
| 8446 | case ISN_COMPARESTRING: |
| 8447 | case ISN_CONCAT: |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 8448 | case ISN_COND2BOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8449 | case ISN_DROP: |
| 8450 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 8451 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 8452 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8453 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 8454 | case ISN_EXECCONCAT: |
| 8455 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8456 | case ISN_FOR: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8457 | case ISN_GETITEM: |
| 8458 | case ISN_JUMP: |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 8459 | case ISN_LISTAPPEND: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 8460 | case ISN_LISTINDEX: |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 8461 | case ISN_LISTSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8462 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 8463 | case ISN_LOADBDICT: |
| 8464 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 8465 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8466 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 8467 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8468 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 8469 | case ISN_LOADWDICT: |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 8470 | case ISN_LOCKCONST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8471 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8472 | case ISN_NEGATENR: |
| 8473 | case ISN_NEWDICT: |
| 8474 | case ISN_NEWLIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8475 | case ISN_OPANY: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8476 | case ISN_OPFLOAT: |
| 8477 | case ISN_OPNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8478 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 8479 | case ISN_PCALL_END: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8480 | case ISN_PUSHBOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8481 | case ISN_PUSHF: |
| 8482 | case ISN_PUSHNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8483 | case ISN_PUSHSPEC: |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 8484 | case ISN_PUT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8485 | case ISN_RETURN: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8486 | case ISN_SHUFFLE: |
| 8487 | case ISN_SLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8488 | case ISN_STORE: |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 8489 | case ISN_STOREINDEX: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8490 | case ISN_STORENR: |
| 8491 | case ISN_STOREOUTER: |
| 8492 | case ISN_STOREREG: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 8493 | case ISN_STOREV: |
| 8494 | case ISN_STRINDEX: |
| 8495 | case ISN_STRSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8496 | case ISN_THROW: |
| 8497 | case ISN_TRY: |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 8498 | case ISN_UNLETINDEX: |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 8499 | case ISN_UNPACK: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8500 | // nothing allocated |
| 8501 | break; |
| 8502 | } |
| 8503 | } |
| 8504 | |
| 8505 | /* |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8506 | * Free all instructions for "dfunc" except df_name. |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8507 | */ |
| 8508 | static void |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8509 | delete_def_function_contents(dfunc_T *dfunc, int mark_deleted) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8510 | { |
| 8511 | int idx; |
| 8512 | |
| 8513 | ga_clear(&dfunc->df_def_args_isn); |
| 8514 | |
| 8515 | if (dfunc->df_instr != NULL) |
| 8516 | { |
| 8517 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 8518 | delete_instr(dfunc->df_instr + idx); |
| 8519 | VIM_CLEAR(dfunc->df_instr); |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8520 | dfunc->df_instr = NULL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8521 | } |
| 8522 | |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8523 | if (mark_deleted) |
| 8524 | dfunc->df_deleted = TRUE; |
| 8525 | if (dfunc->df_ufunc != NULL) |
| 8526 | dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8527 | } |
| 8528 | |
| 8529 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8530 | * 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] | 8531 | * function, unless another user function still uses it. |
| 8532 | * The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8533 | */ |
| 8534 | void |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8535 | unlink_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8536 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8537 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8538 | { |
| 8539 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8540 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8541 | |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8542 | if (--dfunc->df_refcount <= 0) |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8543 | delete_def_function_contents(dfunc, TRUE); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 8544 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8545 | ufunc->uf_dfunc_idx = 0; |
| 8546 | if (dfunc->df_ufunc == ufunc) |
| 8547 | dfunc->df_ufunc = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8548 | } |
| 8549 | } |
| 8550 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8551 | /* |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8552 | * Used when a user function refers to an existing dfunc. |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8553 | */ |
| 8554 | void |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8555 | link_def_function(ufunc_T *ufunc) |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8556 | { |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8557 | if (ufunc->uf_dfunc_idx > 0) |
| 8558 | { |
| 8559 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 8560 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8561 | |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8562 | ++dfunc->df_refcount; |
| 8563 | } |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 8564 | } |
| 8565 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8566 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8567 | /* |
| 8568 | * Free all functions defined with ":def". |
| 8569 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8570 | void |
| 8571 | free_def_functions(void) |
| 8572 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8573 | int idx; |
| 8574 | |
| 8575 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 8576 | { |
| 8577 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 8578 | |
Bram Moolenaar | cdc40c4 | 2020-12-26 17:43:08 +0100 | [diff] [blame] | 8579 | delete_def_function_contents(dfunc, TRUE); |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 8580 | vim_free(dfunc->df_name); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 8581 | } |
| 8582 | |
| 8583 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8584 | } |
| 8585 | #endif |
| 8586 | |
| 8587 | |
| 8588 | #endif // FEAT_EVAL |