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 | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 144 | }; |
| 145 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 146 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 147 | |
| 148 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 149 | * Lookup variable "name" in the local scope and return it. |
| 150 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 151 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 152 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 153 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 154 | { |
| 155 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 156 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 157 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 158 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 159 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 160 | |
| 161 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 162 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 163 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 164 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 165 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 166 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 167 | { |
| 168 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 169 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 170 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 171 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 172 | |
| 173 | // Find local in outer function scope. |
| 174 | if (cctx->ctx_outer != NULL) |
| 175 | { |
| 176 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 177 | if (lvar != NULL) |
| 178 | { |
| 179 | // TODO: are there situations we should not mark the outer scope as |
| 180 | // used? |
| 181 | cctx->ctx_outer_used = TRUE; |
| 182 | lvar->lv_from_outer = TRUE; |
| 183 | return lvar; |
| 184 | } |
| 185 | } |
| 186 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 187 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 191 | * Lookup an argument in the current function and an enclosing function. |
| 192 | * Returns the argument index in "idxp" |
| 193 | * Returns the argument type in "type" |
| 194 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 195 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 196 | */ |
| 197 | static int |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 198 | arg_exists( |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 199 | char_u *name, |
| 200 | size_t len, |
| 201 | int *idxp, |
| 202 | type_T **type, |
| 203 | int *gen_load_outer, |
| 204 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 205 | { |
| 206 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 207 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 208 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 209 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 210 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 211 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 212 | { |
| 213 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 214 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 215 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 216 | { |
| 217 | if (idxp != NULL) |
| 218 | { |
| 219 | // Arguments are located above the frame pointer. One further |
| 220 | // if there is a vararg argument |
| 221 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 222 | + STACK_FRAME_SIZE) |
| 223 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 224 | |
| 225 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 226 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 227 | else |
| 228 | *type = &t_any; |
| 229 | } |
| 230 | return OK; |
| 231 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 232 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 233 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 234 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 235 | if (va_name != NULL |
| 236 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 237 | { |
| 238 | if (idxp != NULL) |
| 239 | { |
| 240 | // varargs is always the last argument |
| 241 | *idxp = -STACK_FRAME_SIZE - 1; |
| 242 | *type = cctx->ctx_ufunc->uf_va_type; |
| 243 | } |
| 244 | return OK; |
| 245 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 246 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 247 | if (cctx->ctx_outer != NULL) |
| 248 | { |
| 249 | // Lookup the name for an argument of the outer function. |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 250 | 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] | 251 | == OK) |
| 252 | { |
| 253 | *gen_load_outer = TRUE; |
| 254 | return OK; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 262 | * Lookup a script-local variable in the current script, possibly defined in a |
| 263 | * block that contains the function "cctx->ctx_ufunc". |
| 264 | * "cctx" is NULL at the script level. |
| 265 | * if "len" is <= 0 "name" must be NUL terminated. |
| 266 | * Return NULL when not found. |
| 267 | */ |
| 268 | static sallvar_T * |
| 269 | find_script_var(char_u *name, size_t len, cctx_T *cctx) |
| 270 | { |
| 271 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
| 272 | hashitem_T *hi; |
| 273 | int cc; |
| 274 | sallvar_T *sav; |
| 275 | ufunc_T *ufunc; |
| 276 | |
| 277 | // Find the list of all script variables with the right name. |
| 278 | if (len > 0) |
| 279 | { |
| 280 | cc = name[len]; |
| 281 | name[len] = NUL; |
| 282 | } |
| 283 | hi = hash_find(&si->sn_all_vars.dv_hashtab, name); |
| 284 | if (len > 0) |
| 285 | name[len] = cc; |
| 286 | if (HASHITEM_EMPTY(hi)) |
| 287 | return NULL; |
| 288 | |
| 289 | sav = HI2SAV(hi); |
| 290 | if (sav->sav_block_id == 0 || cctx == NULL) |
| 291 | // variable defined in the script scope or not in a function. |
| 292 | return sav; |
| 293 | |
| 294 | // Go over the variables with this name and find one that was visible |
| 295 | // from the function. |
| 296 | ufunc = cctx->ctx_ufunc; |
| 297 | while (sav != NULL) |
| 298 | { |
| 299 | int idx; |
| 300 | |
| 301 | // Go over the blocks that this function was defined in. If the |
| 302 | // variable block ID matches it was visible to the function. |
| 303 | for (idx = 0; idx < ufunc->uf_block_depth; ++idx) |
| 304 | if (ufunc->uf_block_ids[idx] == sav->sav_block_id) |
| 305 | return sav; |
| 306 | sav = sav->sav_next; |
| 307 | } |
| 308 | |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | /* |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 313 | * Returnd TRUE if the script context is Vim9 script. |
| 314 | */ |
| 315 | static int |
| 316 | script_is_vim9() |
| 317 | { |
| 318 | return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9; |
| 319 | } |
| 320 | |
| 321 | /* |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 322 | * Lookup a variable (without s: prefix) in the current script. |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 323 | * If "vim9script" is TRUE the script must be Vim9 script. Used for "var" |
| 324 | * without "s:". |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 325 | * "cctx" is NULL at the script level. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 326 | * Returns OK or FAIL. |
| 327 | */ |
| 328 | static int |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 329 | 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] | 330 | { |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 331 | int is_vim9_script; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 332 | |
Bram Moolenaar | 9c4f552 | 2020-09-25 21:47:28 +0200 | [diff] [blame] | 333 | if (current_sctx.sc_sid <= 0) |
| 334 | return FAIL; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 335 | is_vim9_script = script_is_vim9(); |
| 336 | if (vim9script && !is_vim9_script) |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 337 | return FAIL; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 338 | if (is_vim9_script) |
| 339 | { |
| 340 | // Check script variables that were visible where the function was |
| 341 | // defined. |
| 342 | if (find_script_var(name, len, cctx) != NULL) |
| 343 | return OK; |
| 344 | } |
| 345 | else |
| 346 | { |
| 347 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 348 | dictitem_T *di; |
| 349 | int cc; |
| 350 | |
| 351 | // Check script variables that are currently visible |
| 352 | cc = name[len]; |
| 353 | name[len] = NUL; |
| 354 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 355 | name[len] = cc; |
| 356 | if (di != NULL) |
| 357 | return OK; |
| 358 | } |
| 359 | |
| 360 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 361 | } |
| 362 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 363 | /* |
| 364 | * 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] | 365 | * compilation context "cctx". "cctx" is NULL at the script level. |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 366 | * Does not check the global namespace. |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 367 | * Return FAIL and give an error if it defined. |
| 368 | */ |
| 369 | int |
Bram Moolenaar | cbb6bdc | 2020-07-06 21:53:17 +0200 | [diff] [blame] | 370 | check_defined(char_u *p, size_t len, cctx_T *cctx) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 371 | { |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 372 | int c = p[len]; |
| 373 | ufunc_T *ufunc = NULL; |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 374 | |
| 375 | p[len] = NUL; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 376 | if (script_var_exists(p, len, FALSE, cctx) == OK |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 377 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 378 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 379 | || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK)) |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 380 | || find_imported(p, len, cctx) != NULL |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 381 | || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 382 | { |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 383 | // A local or script-local function can shadow a global function. |
| 384 | if (ufunc == NULL || !func_is_global(ufunc) |
| 385 | || (p[0] == 'g' && p[1] == ':')) |
| 386 | { |
| 387 | p[len] = c; |
| 388 | semsg(_(e_name_already_defined_str), p); |
| 389 | return FAIL; |
| 390 | } |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 391 | } |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 392 | p[len] = c; |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 393 | return OK; |
| 394 | } |
| 395 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 396 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 397 | ///////////////////////////////////////////////////////////////////// |
| 398 | // Following generate_ functions expect the caller to call ga_grow(). |
| 399 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 400 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 401 | #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] | 402 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 403 | /* |
| 404 | * Generate an instruction without arguments. |
| 405 | * Returns a pointer to the new instruction, NULL if failed. |
| 406 | */ |
| 407 | static isn_T * |
| 408 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 409 | { |
| 410 | garray_T *instr = &cctx->ctx_instr; |
| 411 | isn_T *isn; |
| 412 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 413 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 414 | if (ga_grow(instr, 1) == FAIL) |
| 415 | return NULL; |
| 416 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 417 | isn->isn_type = isn_type; |
| 418 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 419 | ++instr->ga_len; |
| 420 | |
| 421 | return isn; |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Generate an instruction without arguments. |
| 426 | * "drop" will be removed from the stack. |
| 427 | * Returns a pointer to the new instruction, NULL if failed. |
| 428 | */ |
| 429 | static isn_T * |
| 430 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 431 | { |
| 432 | garray_T *stack = &cctx->ctx_type_stack; |
| 433 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 434 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 435 | stack->ga_len -= drop; |
| 436 | return generate_instr(cctx, isn_type); |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 441 | */ |
| 442 | static isn_T * |
| 443 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 444 | { |
| 445 | isn_T *isn; |
| 446 | garray_T *stack = &cctx->ctx_type_stack; |
| 447 | |
| 448 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 449 | return NULL; |
| 450 | |
| 451 | if (ga_grow(stack, 1) == FAIL) |
| 452 | return NULL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 453 | ((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] | 454 | ++stack->ga_len; |
| 455 | |
| 456 | return isn; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * 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] | 461 | * But only for simple types. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 462 | */ |
| 463 | static int |
| 464 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 465 | { |
| 466 | isn_T *isn; |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 467 | isntype_T isntype = ISN_2STRING; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 468 | garray_T *stack = &cctx->ctx_type_stack; |
| 469 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 470 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 471 | switch ((*type)->tt_type) |
| 472 | { |
| 473 | // nothing to be done |
| 474 | case VAR_STRING: return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 475 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 476 | // conversion possible |
| 477 | case VAR_SPECIAL: |
| 478 | case VAR_BOOL: |
| 479 | case VAR_NUMBER: |
| 480 | case VAR_FLOAT: |
| 481 | break; |
| 482 | |
| 483 | // conversion possible (with runtime check) |
| 484 | case VAR_ANY: |
| 485 | case VAR_UNKNOWN: |
| 486 | isntype = ISN_2STRING_ANY; |
| 487 | break; |
| 488 | |
| 489 | // conversion not possible |
| 490 | case VAR_VOID: |
| 491 | case VAR_BLOB: |
| 492 | case VAR_FUNC: |
| 493 | case VAR_PARTIAL: |
| 494 | case VAR_LIST: |
| 495 | case VAR_DICT: |
| 496 | case VAR_JOB: |
| 497 | case VAR_CHANNEL: |
| 498 | to_string_error((*type)->tt_type); |
| 499 | return FAIL; |
| 500 | } |
| 501 | |
| 502 | *type = &t_string; |
| 503 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 504 | return FAIL; |
| 505 | isn->isn_arg.number = offset; |
| 506 | |
| 507 | return OK; |
| 508 | } |
| 509 | |
| 510 | static int |
| 511 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 512 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 513 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 514 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 515 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 516 | { |
| 517 | if (*op == '+') |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 518 | emsg(_(e_wrong_argument_type_for_plus)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 519 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 520 | semsg(_(e_char_requires_number_or_float_arguments), *op); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 521 | return FAIL; |
| 522 | } |
| 523 | return OK; |
| 524 | } |
| 525 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 526 | static int |
| 527 | generate_add_instr( |
| 528 | cctx_T *cctx, |
| 529 | vartype_T vartype, |
| 530 | type_T *type1, |
| 531 | type_T *type2) |
| 532 | { |
| 533 | isn_T *isn = generate_instr_drop(cctx, |
| 534 | vartype == VAR_NUMBER ? ISN_OPNR |
| 535 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 536 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 537 | #ifdef FEAT_FLOAT |
| 538 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 539 | #endif |
| 540 | : ISN_OPANY, 1); |
| 541 | |
| 542 | if (vartype != VAR_LIST && vartype != VAR_BLOB |
| 543 | && type1->tt_type != VAR_ANY |
| 544 | && type2->tt_type != VAR_ANY |
| 545 | && check_number_or_float( |
| 546 | type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL) |
| 547 | return FAIL; |
| 548 | |
| 549 | if (isn != NULL) |
| 550 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 551 | return isn == NULL ? FAIL : OK; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * Get the type to use for an instruction for an operation on "type1" and |
| 556 | * "type2". If they are matching use a type-specific instruction. Otherwise |
| 557 | * fall back to runtime type checking. |
| 558 | */ |
| 559 | static vartype_T |
| 560 | operator_type(type_T *type1, type_T *type2) |
| 561 | { |
| 562 | if (type1->tt_type == type2->tt_type |
| 563 | && (type1->tt_type == VAR_NUMBER |
| 564 | || type1->tt_type == VAR_LIST |
| 565 | #ifdef FEAT_FLOAT |
| 566 | || type1->tt_type == VAR_FLOAT |
| 567 | #endif |
| 568 | || type1->tt_type == VAR_BLOB)) |
| 569 | return type1->tt_type; |
| 570 | return VAR_ANY; |
| 571 | } |
| 572 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 573 | /* |
| 574 | * Generate an instruction with two arguments. The instruction depends on the |
| 575 | * type of the arguments. |
| 576 | */ |
| 577 | static int |
| 578 | generate_two_op(cctx_T *cctx, char_u *op) |
| 579 | { |
| 580 | garray_T *stack = &cctx->ctx_type_stack; |
| 581 | type_T *type1; |
| 582 | type_T *type2; |
| 583 | vartype_T vartype; |
| 584 | isn_T *isn; |
| 585 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 586 | RETURN_OK_IF_SKIP(cctx); |
| 587 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 588 | // Get the known type of the two items on the stack. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 589 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 590 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 591 | vartype = operator_type(type1, type2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 592 | |
| 593 | switch (*op) |
| 594 | { |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 595 | case '+': |
| 596 | if (generate_add_instr(cctx, vartype, type1, type2) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 597 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 598 | break; |
| 599 | |
| 600 | case '-': |
| 601 | case '*': |
| 602 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 603 | op) == FAIL) |
| 604 | return FAIL; |
| 605 | if (vartype == VAR_NUMBER) |
| 606 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 607 | #ifdef FEAT_FLOAT |
| 608 | else if (vartype == VAR_FLOAT) |
| 609 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 610 | #endif |
| 611 | else |
| 612 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 613 | if (isn != NULL) |
| 614 | isn->isn_arg.op.op_type = *op == '*' |
| 615 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 616 | break; |
| 617 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 618 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 619 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 620 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 621 | && type2->tt_type != VAR_NUMBER)) |
| 622 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 623 | emsg(_(e_percent_requires_number_arguments)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 624 | return FAIL; |
| 625 | } |
| 626 | isn = generate_instr_drop(cctx, |
| 627 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 628 | if (isn != NULL) |
| 629 | isn->isn_arg.op.op_type = EXPR_REM; |
| 630 | break; |
| 631 | } |
| 632 | |
| 633 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 634 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 635 | { |
| 636 | type_T *type = &t_any; |
| 637 | |
| 638 | #ifdef FEAT_FLOAT |
| 639 | // float+number and number+float results in float |
| 640 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 641 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 642 | type = &t_float; |
| 643 | #endif |
| 644 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 645 | } |
| 646 | |
| 647 | return OK; |
| 648 | } |
| 649 | |
| 650 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 651 | * Get the instruction to use for comparing "type1" with "type2" |
| 652 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 653 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 654 | static isntype_T |
| 655 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 656 | { |
| 657 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 658 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 659 | if (type1 == VAR_UNKNOWN) |
| 660 | type1 = VAR_ANY; |
| 661 | if (type2 == VAR_UNKNOWN) |
| 662 | type2 = VAR_ANY; |
| 663 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 664 | if (type1 == type2) |
| 665 | { |
| 666 | switch (type1) |
| 667 | { |
| 668 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 669 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 670 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 671 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 672 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 673 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 674 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 675 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 676 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 677 | default: isntype = ISN_COMPAREANY; break; |
| 678 | } |
| 679 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 680 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 681 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 682 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 683 | isntype = ISN_COMPAREANY; |
| 684 | |
| 685 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 686 | && (isntype == ISN_COMPAREBOOL |
| 687 | || isntype == ISN_COMPARESPECIAL |
| 688 | || isntype == ISN_COMPARENR |
| 689 | || isntype == ISN_COMPAREFLOAT)) |
| 690 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 691 | semsg(_(e_cannot_use_str_with_str), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 692 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 693 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 694 | } |
| 695 | if (isntype == ISN_DROP |
| 696 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 697 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 698 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 699 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 700 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 701 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 702 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 703 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 704 | semsg(_(e_cannot_compare_str_with_str), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 705 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 706 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 707 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 708 | return isntype; |
| 709 | } |
| 710 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 711 | int |
| 712 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 713 | { |
| 714 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 715 | return FAIL; |
| 716 | return OK; |
| 717 | } |
| 718 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 719 | /* |
| 720 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 721 | */ |
| 722 | static int |
| 723 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 724 | { |
| 725 | isntype_T isntype; |
| 726 | isn_T *isn; |
| 727 | garray_T *stack = &cctx->ctx_type_stack; |
| 728 | vartype_T type1; |
| 729 | vartype_T type2; |
| 730 | |
| 731 | RETURN_OK_IF_SKIP(cctx); |
| 732 | |
| 733 | // Get the known type of the two items on the stack. If they are matching |
| 734 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 735 | // checking. |
| 736 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 737 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 738 | isntype = get_compare_isn(exptype, type1, type2); |
| 739 | if (isntype == ISN_DROP) |
| 740 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 741 | |
| 742 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 743 | return FAIL; |
| 744 | isn->isn_arg.op.op_type = exptype; |
| 745 | isn->isn_arg.op.op_ic = ic; |
| 746 | |
| 747 | // takes two arguments, puts one bool back |
| 748 | if (stack->ga_len >= 2) |
| 749 | { |
| 750 | --stack->ga_len; |
| 751 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 752 | } |
| 753 | |
| 754 | return OK; |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * Generate an ISN_2BOOL instruction. |
| 759 | */ |
| 760 | static int |
| 761 | generate_2BOOL(cctx_T *cctx, int invert) |
| 762 | { |
| 763 | isn_T *isn; |
| 764 | garray_T *stack = &cctx->ctx_type_stack; |
| 765 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 766 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 767 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 768 | return FAIL; |
| 769 | isn->isn_arg.number = invert; |
| 770 | |
| 771 | // type becomes bool |
| 772 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 773 | |
| 774 | return OK; |
| 775 | } |
| 776 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 777 | /* |
| 778 | * Generate an ISN_COND2BOOL instruction. |
| 779 | */ |
| 780 | static int |
| 781 | generate_COND2BOOL(cctx_T *cctx) |
| 782 | { |
| 783 | isn_T *isn; |
| 784 | garray_T *stack = &cctx->ctx_type_stack; |
| 785 | |
| 786 | RETURN_OK_IF_SKIP(cctx); |
| 787 | if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL) |
| 788 | return FAIL; |
| 789 | |
| 790 | // type becomes bool |
| 791 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 792 | |
| 793 | return OK; |
| 794 | } |
| 795 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 796 | static int |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 797 | generate_TYPECHECK( |
| 798 | cctx_T *cctx, |
| 799 | type_T *expected, |
| 800 | int offset) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 801 | { |
| 802 | isn_T *isn; |
| 803 | garray_T *stack = &cctx->ctx_type_stack; |
| 804 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 805 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 806 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 807 | return FAIL; |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 808 | isn->isn_arg.type.ct_type = alloc_type(expected); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 809 | isn->isn_arg.type.ct_off = offset; |
| 810 | |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 811 | // type becomes expected |
| 812 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = expected; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 813 | |
| 814 | return OK; |
| 815 | } |
| 816 | |
| 817 | /* |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 818 | * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be |
| 819 | * used. Return FALSE if the types will never match. |
| 820 | */ |
| 821 | static int |
| 822 | use_typecheck(type_T *actual, type_T *expected) |
| 823 | { |
| 824 | if (actual->tt_type == VAR_ANY |
| 825 | || actual->tt_type == VAR_UNKNOWN |
| 826 | || (actual->tt_type == VAR_FUNC |
| 827 | && (expected->tt_type == VAR_FUNC |
| 828 | || expected->tt_type == VAR_PARTIAL) |
| 829 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 830 | return TRUE; |
| 831 | if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT) |
| 832 | && actual->tt_type == expected->tt_type) |
| 833 | // This takes care of a nested list or dict. |
| 834 | return use_typecheck(actual->tt_member, expected->tt_member); |
| 835 | return FALSE; |
| 836 | } |
| 837 | |
| 838 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 839 | * Check that |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 840 | * - "actual" matches "expected" type or |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 841 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 842 | * - return FAIL. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 843 | * If "actual_is_const" is TRUE then the type won't change at runtime, do not |
| 844 | * generate a TYPECHECK. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 845 | */ |
| 846 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 847 | need_type( |
| 848 | type_T *actual, |
| 849 | type_T *expected, |
| 850 | int offset, |
| 851 | cctx_T *cctx, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 852 | int silent, |
| 853 | int actual_is_const) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 854 | { |
Bram Moolenaar | 4ed124c | 2020-09-09 20:03:46 +0200 | [diff] [blame] | 855 | if (expected == &t_bool && actual != &t_bool |
| 856 | && (actual->tt_flags & TTFLAG_BOOL_OK)) |
| 857 | { |
| 858 | // Using "0", "1" or the result of an expression with "&&" or "||" as a |
| 859 | // boolean is OK but requires a conversion. |
| 860 | generate_2BOOL(cctx, FALSE); |
| 861 | return OK; |
| 862 | } |
| 863 | |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 864 | if (check_type(expected, actual, FALSE, 0) == OK) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 865 | return OK; |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 866 | |
| 867 | // 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] | 868 | // If it's a constant a runtime check makes no sense. |
| 869 | if (!actual_is_const && use_typecheck(actual, expected)) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 870 | { |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 871 | generate_TYPECHECK(cctx, expected, offset); |
| 872 | return OK; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 873 | } |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 874 | |
| 875 | if (!silent) |
| 876 | type_mismatch(expected, actual); |
| 877 | return FAIL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 881 | * Generate an ISN_PUSHNR instruction. |
| 882 | */ |
| 883 | static int |
| 884 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 885 | { |
| 886 | isn_T *isn; |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 887 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 888 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 889 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 890 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 891 | return FAIL; |
| 892 | isn->isn_arg.number = number; |
| 893 | |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 894 | if (number == 0 || number == 1) |
| 895 | { |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 896 | type_T *type = get_type_ptr(cctx->ctx_type_list); |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 897 | |
| 898 | // A 0 or 1 number can also be used as a bool. |
| 899 | if (type != NULL) |
| 900 | { |
| 901 | type->tt_type = VAR_NUMBER; |
| 902 | type->tt_flags = TTFLAG_BOOL_OK; |
| 903 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 904 | } |
| 905 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 906 | return OK; |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * Generate an ISN_PUSHBOOL instruction. |
| 911 | */ |
| 912 | static int |
| 913 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 914 | { |
| 915 | isn_T *isn; |
| 916 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 917 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 918 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 919 | return FAIL; |
| 920 | isn->isn_arg.number = number; |
| 921 | |
| 922 | return OK; |
| 923 | } |
| 924 | |
| 925 | /* |
| 926 | * Generate an ISN_PUSHSPEC instruction. |
| 927 | */ |
| 928 | static int |
| 929 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 930 | { |
| 931 | isn_T *isn; |
| 932 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 933 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 934 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 935 | return FAIL; |
| 936 | isn->isn_arg.number = number; |
| 937 | |
| 938 | return OK; |
| 939 | } |
| 940 | |
| 941 | #ifdef FEAT_FLOAT |
| 942 | /* |
| 943 | * Generate an ISN_PUSHF instruction. |
| 944 | */ |
| 945 | static int |
| 946 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 947 | { |
| 948 | isn_T *isn; |
| 949 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 950 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 951 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 952 | return FAIL; |
| 953 | isn->isn_arg.fnumber = fnumber; |
| 954 | |
| 955 | return OK; |
| 956 | } |
| 957 | #endif |
| 958 | |
| 959 | /* |
| 960 | * Generate an ISN_PUSHS instruction. |
| 961 | * Consumes "str". |
| 962 | */ |
| 963 | static int |
| 964 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 965 | { |
| 966 | isn_T *isn; |
| 967 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 968 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 969 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 970 | return FAIL; |
| 971 | isn->isn_arg.string = str; |
| 972 | |
| 973 | return OK; |
| 974 | } |
| 975 | |
| 976 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 977 | * Generate an ISN_PUSHCHANNEL instruction. |
| 978 | * Consumes "channel". |
| 979 | */ |
| 980 | static int |
| 981 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 982 | { |
| 983 | isn_T *isn; |
| 984 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 985 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 986 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 987 | return FAIL; |
| 988 | isn->isn_arg.channel = channel; |
| 989 | |
| 990 | return OK; |
| 991 | } |
| 992 | |
| 993 | /* |
| 994 | * Generate an ISN_PUSHJOB instruction. |
| 995 | * Consumes "job". |
| 996 | */ |
| 997 | static int |
| 998 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 999 | { |
| 1000 | isn_T *isn; |
| 1001 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1002 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 1003 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1004 | return FAIL; |
| 1005 | isn->isn_arg.job = job; |
| 1006 | |
| 1007 | return OK; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1011 | * Generate an ISN_PUSHBLOB instruction. |
| 1012 | * Consumes "blob". |
| 1013 | */ |
| 1014 | static int |
| 1015 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 1016 | { |
| 1017 | isn_T *isn; |
| 1018 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1019 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1020 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 1021 | return FAIL; |
| 1022 | isn->isn_arg.blob = blob; |
| 1023 | |
| 1024 | return OK; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1028 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 1029 | * Consumes "name". |
| 1030 | */ |
| 1031 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1032 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1033 | { |
| 1034 | isn_T *isn; |
| 1035 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1036 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1037 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1038 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 1039 | isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1040 | |
| 1041 | return OK; |
| 1042 | } |
| 1043 | |
| 1044 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1045 | * Generate an ISN_GETITEM instruction with "index". |
| 1046 | */ |
| 1047 | static int |
| 1048 | generate_GETITEM(cctx_T *cctx, int index) |
| 1049 | { |
| 1050 | isn_T *isn; |
| 1051 | garray_T *stack = &cctx->ctx_type_stack; |
| 1052 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1053 | type_T *item_type = &t_any; |
| 1054 | |
| 1055 | RETURN_OK_IF_SKIP(cctx); |
| 1056 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1057 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1058 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1059 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1060 | emsg(_(e_listreq)); |
| 1061 | return FAIL; |
| 1062 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 1063 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 1064 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 1065 | return FAIL; |
| 1066 | isn->isn_arg.number = index; |
| 1067 | |
| 1068 | // add the item type to the type stack |
| 1069 | if (ga_grow(stack, 1) == FAIL) |
| 1070 | return FAIL; |
| 1071 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 1072 | ++stack->ga_len; |
| 1073 | return OK; |
| 1074 | } |
| 1075 | |
| 1076 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 1077 | * Generate an ISN_SLICE instruction with "count". |
| 1078 | */ |
| 1079 | static int |
| 1080 | generate_SLICE(cctx_T *cctx, int count) |
| 1081 | { |
| 1082 | isn_T *isn; |
| 1083 | |
| 1084 | RETURN_OK_IF_SKIP(cctx); |
| 1085 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 1086 | return FAIL; |
| 1087 | isn->isn_arg.number = count; |
| 1088 | return OK; |
| 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 1093 | */ |
| 1094 | static int |
| 1095 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 1096 | { |
| 1097 | isn_T *isn; |
| 1098 | |
| 1099 | RETURN_OK_IF_SKIP(cctx); |
| 1100 | |
| 1101 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 1102 | return FAIL; |
| 1103 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 1104 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 1105 | |
| 1106 | return OK; |
| 1107 | } |
| 1108 | |
| 1109 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1110 | * Generate an ISN_STORE instruction. |
| 1111 | */ |
| 1112 | static int |
| 1113 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 1114 | { |
| 1115 | isn_T *isn; |
| 1116 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1117 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1118 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1119 | return FAIL; |
| 1120 | if (name != NULL) |
| 1121 | isn->isn_arg.string = vim_strsave(name); |
| 1122 | else |
| 1123 | isn->isn_arg.number = idx; |
| 1124 | |
| 1125 | return OK; |
| 1126 | } |
| 1127 | |
| 1128 | /* |
| 1129 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1130 | */ |
| 1131 | static int |
| 1132 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1133 | { |
| 1134 | isn_T *isn; |
| 1135 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1136 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1137 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1138 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1139 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1140 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1141 | |
| 1142 | return OK; |
| 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * Generate an ISN_STOREOPT instruction |
| 1147 | */ |
| 1148 | static int |
| 1149 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1150 | { |
| 1151 | isn_T *isn; |
| 1152 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1153 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1c199f9 | 2020-08-07 21:28:34 +0200 | [diff] [blame] | 1154 | if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1155 | return FAIL; |
| 1156 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1157 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1158 | |
| 1159 | return OK; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * Generate an ISN_LOAD or similar instruction. |
| 1164 | */ |
| 1165 | static int |
| 1166 | generate_LOAD( |
| 1167 | cctx_T *cctx, |
| 1168 | isntype_T isn_type, |
| 1169 | int idx, |
| 1170 | char_u *name, |
| 1171 | type_T *type) |
| 1172 | { |
| 1173 | isn_T *isn; |
| 1174 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1175 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1176 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1177 | return FAIL; |
| 1178 | if (name != NULL) |
| 1179 | isn->isn_arg.string = vim_strsave(name); |
| 1180 | else |
| 1181 | isn->isn_arg.number = idx; |
| 1182 | |
| 1183 | return OK; |
| 1184 | } |
| 1185 | |
| 1186 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1187 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1188 | */ |
| 1189 | static int |
| 1190 | generate_LOADV( |
| 1191 | cctx_T *cctx, |
| 1192 | char_u *name, |
| 1193 | int error) |
| 1194 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1195 | int di_flags; |
| 1196 | int vidx = find_vim_var(name, &di_flags); |
| 1197 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1198 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1199 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1200 | if (vidx < 0) |
| 1201 | { |
| 1202 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1203 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1204 | return FAIL; |
| 1205 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1206 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1207 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1208 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1212 | * Generate an ISN_UNLET instruction. |
| 1213 | */ |
| 1214 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1215 | 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] | 1216 | { |
| 1217 | isn_T *isn; |
| 1218 | |
| 1219 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1220 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1221 | return FAIL; |
| 1222 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1223 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1224 | |
| 1225 | return OK; |
| 1226 | } |
| 1227 | |
| 1228 | /* |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 1229 | * Generate an ISN_LOCKCONST instruction. |
| 1230 | */ |
| 1231 | static int |
| 1232 | generate_LOCKCONST(cctx_T *cctx) |
| 1233 | { |
| 1234 | isn_T *isn; |
| 1235 | |
| 1236 | RETURN_OK_IF_SKIP(cctx); |
| 1237 | if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL) |
| 1238 | return FAIL; |
| 1239 | return OK; |
| 1240 | } |
| 1241 | |
| 1242 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1243 | * Generate an ISN_LOADS instruction. |
| 1244 | */ |
| 1245 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1246 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1247 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1248 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1249 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1250 | int sid, |
| 1251 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1252 | { |
| 1253 | isn_T *isn; |
| 1254 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1255 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1256 | if (isn_type == ISN_LOADS) |
| 1257 | isn = generate_instr_type(cctx, isn_type, type); |
| 1258 | else |
| 1259 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1260 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1261 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1262 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1263 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1264 | |
| 1265 | return OK; |
| 1266 | } |
| 1267 | |
| 1268 | /* |
| 1269 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1270 | */ |
| 1271 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1272 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1273 | cctx_T *cctx, |
| 1274 | isntype_T isn_type, |
| 1275 | int sid, |
| 1276 | int idx, |
| 1277 | type_T *type) |
| 1278 | { |
| 1279 | isn_T *isn; |
| 1280 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1281 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1282 | if (isn_type == ISN_LOADSCRIPT) |
| 1283 | isn = generate_instr_type(cctx, isn_type, type); |
| 1284 | else |
| 1285 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1286 | if (isn == NULL) |
| 1287 | return FAIL; |
| 1288 | isn->isn_arg.script.script_sid = sid; |
| 1289 | isn->isn_arg.script.script_idx = idx; |
| 1290 | return OK; |
| 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * Generate an ISN_NEWLIST instruction. |
| 1295 | */ |
| 1296 | static int |
| 1297 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1298 | { |
| 1299 | isn_T *isn; |
| 1300 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1301 | type_T *type; |
| 1302 | type_T *member; |
| 1303 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1304 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1305 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1306 | return FAIL; |
| 1307 | isn->isn_arg.number = count; |
| 1308 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1309 | // get the member type from all the items on the stack. |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1310 | if (count == 0) |
| 1311 | member = &t_void; |
| 1312 | else |
| 1313 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1314 | ((type_T **)stack->ga_data) + stack->ga_len, count, 1, |
| 1315 | cctx->ctx_type_list); |
| 1316 | type = get_list_type(member, cctx->ctx_type_list); |
| 1317 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1318 | // drop the value types |
| 1319 | stack->ga_len -= count; |
| 1320 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1321 | // add the list type to the type stack |
| 1322 | if (ga_grow(stack, 1) == FAIL) |
| 1323 | return FAIL; |
| 1324 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1325 | ++stack->ga_len; |
| 1326 | |
| 1327 | return OK; |
| 1328 | } |
| 1329 | |
| 1330 | /* |
| 1331 | * Generate an ISN_NEWDICT instruction. |
| 1332 | */ |
| 1333 | static int |
| 1334 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1335 | { |
| 1336 | isn_T *isn; |
| 1337 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1338 | type_T *type; |
| 1339 | type_T *member; |
| 1340 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1341 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1342 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1343 | return FAIL; |
| 1344 | isn->isn_arg.number = count; |
| 1345 | |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1346 | if (count == 0) |
| 1347 | member = &t_void; |
| 1348 | else |
| 1349 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1350 | ((type_T **)stack->ga_data) + stack->ga_len, count, 2, |
| 1351 | cctx->ctx_type_list); |
| 1352 | type = get_dict_type(member, cctx->ctx_type_list); |
| 1353 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1354 | // drop the key and value types |
| 1355 | stack->ga_len -= 2 * count; |
| 1356 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1357 | // add the dict type to the type stack |
| 1358 | if (ga_grow(stack, 1) == FAIL) |
| 1359 | return FAIL; |
| 1360 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1361 | ++stack->ga_len; |
| 1362 | |
| 1363 | return OK; |
| 1364 | } |
| 1365 | |
| 1366 | /* |
| 1367 | * Generate an ISN_FUNCREF instruction. |
| 1368 | */ |
| 1369 | static int |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1370 | generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1371 | { |
| 1372 | isn_T *isn; |
| 1373 | garray_T *stack = &cctx->ctx_type_stack; |
| 1374 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1375 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1376 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1377 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1378 | isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 1379 | cctx->ctx_has_closure = 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1380 | |
| 1381 | if (ga_grow(stack, 1) == FAIL) |
| 1382 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1383 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 1384 | ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1385 | ++stack->ga_len; |
| 1386 | |
| 1387 | return OK; |
| 1388 | } |
| 1389 | |
| 1390 | /* |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1391 | * Generate an ISN_NEWFUNC instruction. |
| 1392 | */ |
| 1393 | static int |
| 1394 | generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) |
| 1395 | { |
| 1396 | isn_T *isn; |
| 1397 | char_u *name; |
| 1398 | |
| 1399 | RETURN_OK_IF_SKIP(cctx); |
| 1400 | name = vim_strsave(lambda_name); |
| 1401 | if (name == NULL) |
| 1402 | return FAIL; |
| 1403 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
| 1404 | return FAIL; |
| 1405 | isn->isn_arg.newfunc.nf_lambda = name; |
| 1406 | isn->isn_arg.newfunc.nf_global = func_name; |
| 1407 | |
| 1408 | return OK; |
| 1409 | } |
| 1410 | |
| 1411 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1412 | * Generate an ISN_JUMP instruction. |
| 1413 | */ |
| 1414 | static int |
| 1415 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1416 | { |
| 1417 | isn_T *isn; |
| 1418 | garray_T *stack = &cctx->ctx_type_stack; |
| 1419 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1420 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1421 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1422 | return FAIL; |
| 1423 | isn->isn_arg.jump.jump_when = when; |
| 1424 | isn->isn_arg.jump.jump_where = where; |
| 1425 | |
| 1426 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1427 | --stack->ga_len; |
| 1428 | |
| 1429 | return OK; |
| 1430 | } |
| 1431 | |
| 1432 | static int |
| 1433 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1434 | { |
| 1435 | isn_T *isn; |
| 1436 | garray_T *stack = &cctx->ctx_type_stack; |
| 1437 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1438 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1439 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1440 | return FAIL; |
| 1441 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1442 | |
| 1443 | if (ga_grow(stack, 1) == FAIL) |
| 1444 | return FAIL; |
| 1445 | // type doesn't matter, will be stored next |
| 1446 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1447 | ++stack->ga_len; |
| 1448 | |
| 1449 | return OK; |
| 1450 | } |
| 1451 | |
| 1452 | /* |
| 1453 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1454 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1455 | * Return FAIL if the number of arguments is wrong. |
| 1456 | */ |
| 1457 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1458 | 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] | 1459 | { |
| 1460 | isn_T *isn; |
| 1461 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1462 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1463 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1464 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1465 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1466 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1467 | argoff = check_internal_func(func_idx, argcount); |
| 1468 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1469 | return FAIL; |
| 1470 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1471 | if (method_call && argoff > 1) |
| 1472 | { |
| 1473 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1474 | return FAIL; |
| 1475 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1476 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1477 | } |
| 1478 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1479 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1480 | return FAIL; |
| 1481 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1482 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1483 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1484 | for (i = 0; i < argcount; ++i) |
| 1485 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1486 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1487 | stack->ga_len -= argcount; // drop the arguments |
| 1488 | if (ga_grow(stack, 1) == FAIL) |
| 1489 | return FAIL; |
| 1490 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1491 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1492 | ++stack->ga_len; // add return value |
| 1493 | |
| 1494 | return OK; |
| 1495 | } |
| 1496 | |
| 1497 | /* |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 1498 | * Generate an ISN_LISTAPPEND instruction. Works like add(). |
| 1499 | * Argument count is already checked. |
| 1500 | */ |
| 1501 | static int |
| 1502 | generate_LISTAPPEND(cctx_T *cctx) |
| 1503 | { |
| 1504 | garray_T *stack = &cctx->ctx_type_stack; |
| 1505 | type_T *list_type; |
| 1506 | type_T *item_type; |
| 1507 | type_T *expected; |
| 1508 | |
| 1509 | // Caller already checked that list_type is a list. |
| 1510 | list_type = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 1511 | item_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 1512 | expected = list_type->tt_member; |
| 1513 | if (need_type(item_type, expected, -1, cctx, FALSE, FALSE) == FAIL) |
| 1514 | return FAIL; |
| 1515 | |
| 1516 | if (generate_instr(cctx, ISN_LISTAPPEND) == NULL) |
| 1517 | return FAIL; |
| 1518 | |
| 1519 | --stack->ga_len; // drop the argument |
| 1520 | return OK; |
| 1521 | } |
| 1522 | |
| 1523 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1524 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1525 | * Return FAIL if the number of arguments is wrong. |
| 1526 | */ |
| 1527 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1528 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1529 | { |
| 1530 | isn_T *isn; |
| 1531 | garray_T *stack = &cctx->ctx_type_stack; |
| 1532 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1533 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1534 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1535 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1536 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1537 | { |
Bram Moolenaar | b185a40 | 2020-09-18 22:42:00 +0200 | [diff] [blame] | 1538 | semsg(_(e_toomanyarg), printable_func_name(ufunc)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1539 | return FAIL; |
| 1540 | } |
| 1541 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1542 | { |
Bram Moolenaar | b185a40 | 2020-09-18 22:42:00 +0200 | [diff] [blame] | 1543 | semsg(_(e_toofewarg), printable_func_name(ufunc)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1544 | return FAIL; |
| 1545 | } |
| 1546 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1547 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1548 | { |
| 1549 | int i; |
| 1550 | |
| 1551 | for (i = 0; i < argcount; ++i) |
| 1552 | { |
| 1553 | type_T *expected; |
| 1554 | type_T *actual; |
| 1555 | |
| 1556 | if (i < regular_args) |
| 1557 | { |
| 1558 | if (ufunc->uf_arg_types == NULL) |
| 1559 | continue; |
| 1560 | expected = ufunc->uf_arg_types[i]; |
| 1561 | } |
Bram Moolenaar | 2f8cbc4 | 2020-09-16 17:22:59 +0200 | [diff] [blame] | 1562 | else if (ufunc->uf_va_type == NULL || ufunc->uf_va_type == &t_any) |
| 1563 | // possibly a lambda or "...: any" |
Bram Moolenaar | 79e8db9 | 2020-08-14 22:16:33 +0200 | [diff] [blame] | 1564 | expected = &t_any; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1565 | else |
| 1566 | expected = ufunc->uf_va_type->tt_member; |
| 1567 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 1568 | if (need_type(actual, expected, -argcount + i, cctx, |
| 1569 | TRUE, FALSE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1570 | { |
| 1571 | arg_type_mismatch(expected, actual, i + 1); |
| 1572 | return FAIL; |
| 1573 | } |
| 1574 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1575 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1576 | if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL) |
| 1577 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1578 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1579 | } |
| 1580 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1581 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1582 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1583 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1584 | return FAIL; |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 1585 | if (isn->isn_type == ISN_DCALL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1586 | { |
| 1587 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1588 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1589 | } |
| 1590 | else |
| 1591 | { |
| 1592 | // A user function may be deleted and redefined later, can't use the |
| 1593 | // ufunc pointer, need to look it up again at runtime. |
| 1594 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1595 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1596 | } |
| 1597 | |
| 1598 | stack->ga_len -= argcount; // drop the arguments |
| 1599 | if (ga_grow(stack, 1) == FAIL) |
| 1600 | return FAIL; |
| 1601 | // add return value |
| 1602 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1603 | ++stack->ga_len; |
| 1604 | |
| 1605 | return OK; |
| 1606 | } |
| 1607 | |
| 1608 | /* |
| 1609 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1610 | */ |
| 1611 | static int |
| 1612 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1613 | { |
| 1614 | isn_T *isn; |
| 1615 | garray_T *stack = &cctx->ctx_type_stack; |
| 1616 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1617 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1618 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1619 | return FAIL; |
| 1620 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1621 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1622 | |
| 1623 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1624 | if (ga_grow(stack, 1) == FAIL) |
| 1625 | return FAIL; |
| 1626 | // add return value |
| 1627 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1628 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1629 | |
| 1630 | return OK; |
| 1631 | } |
| 1632 | |
| 1633 | /* |
| 1634 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1635 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1636 | */ |
| 1637 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1638 | generate_PCALL( |
| 1639 | cctx_T *cctx, |
| 1640 | int argcount, |
| 1641 | char_u *name, |
| 1642 | type_T *type, |
| 1643 | int at_top) |
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; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1647 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1648 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1649 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1650 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1651 | if (type->tt_type == VAR_ANY) |
| 1652 | ret_type = &t_any; |
| 1653 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1654 | { |
| 1655 | if (type->tt_argcount != -1) |
| 1656 | { |
| 1657 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1658 | |
| 1659 | if (argcount < type->tt_min_argcount - varargs) |
| 1660 | { |
| 1661 | semsg(_(e_toofewarg), "[reference]"); |
| 1662 | return FAIL; |
| 1663 | } |
| 1664 | if (!varargs && argcount > type->tt_argcount) |
| 1665 | { |
| 1666 | semsg(_(e_toomanyarg), "[reference]"); |
| 1667 | return FAIL; |
| 1668 | } |
| 1669 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1670 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1671 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1672 | else |
| 1673 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1674 | semsg(_(e_not_callable_type_str), name); |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1675 | return FAIL; |
| 1676 | } |
| 1677 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1678 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1679 | return FAIL; |
| 1680 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1681 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1682 | |
| 1683 | stack->ga_len -= argcount; // drop the arguments |
| 1684 | |
| 1685 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1686 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1687 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1688 | // If partial is above the arguments it must be cleared and replaced with |
| 1689 | // the return value. |
| 1690 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1691 | return FAIL; |
| 1692 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1693 | return OK; |
| 1694 | } |
| 1695 | |
| 1696 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1697 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1698 | */ |
| 1699 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1700 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1701 | { |
| 1702 | isn_T *isn; |
| 1703 | garray_T *stack = &cctx->ctx_type_stack; |
| 1704 | type_T *type; |
| 1705 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1706 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1707 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1708 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1709 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1710 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1711 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1712 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1713 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1714 | { |
| 1715 | emsg(_(e_dictreq)); |
| 1716 | return FAIL; |
| 1717 | } |
| 1718 | // change dict type to dict member type |
| 1719 | if (type->tt_type == VAR_DICT) |
| 1720 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1721 | |
| 1722 | return OK; |
| 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | * Generate an ISN_ECHO instruction. |
| 1727 | */ |
| 1728 | static int |
| 1729 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1730 | { |
| 1731 | isn_T *isn; |
| 1732 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1733 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1734 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1735 | return FAIL; |
| 1736 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1737 | isn->isn_arg.echo.echo_count = count; |
| 1738 | |
| 1739 | return OK; |
| 1740 | } |
| 1741 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1742 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1743 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1744 | */ |
| 1745 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1746 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1747 | { |
| 1748 | isn_T *isn; |
| 1749 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1750 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1751 | return FAIL; |
| 1752 | isn->isn_arg.number = count; |
| 1753 | |
| 1754 | return OK; |
| 1755 | } |
| 1756 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 1757 | /* |
| 1758 | * Generate an ISN_PUT instruction. |
| 1759 | */ |
| 1760 | static int |
| 1761 | generate_PUT(cctx_T *cctx, int regname, linenr_T lnum) |
| 1762 | { |
| 1763 | isn_T *isn; |
| 1764 | |
| 1765 | RETURN_OK_IF_SKIP(cctx); |
| 1766 | if ((isn = generate_instr(cctx, ISN_PUT)) == NULL) |
| 1767 | return FAIL; |
| 1768 | isn->isn_arg.put.put_regname = regname; |
| 1769 | isn->isn_arg.put.put_lnum = lnum; |
| 1770 | return OK; |
| 1771 | } |
| 1772 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1773 | static int |
| 1774 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1775 | { |
| 1776 | isn_T *isn; |
| 1777 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1778 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1779 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1780 | return FAIL; |
| 1781 | isn->isn_arg.string = vim_strsave(line); |
| 1782 | return OK; |
| 1783 | } |
| 1784 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1785 | static int |
| 1786 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1787 | { |
| 1788 | isn_T *isn; |
| 1789 | |
| 1790 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1791 | return FAIL; |
| 1792 | isn->isn_arg.number = count; |
| 1793 | return OK; |
| 1794 | } |
| 1795 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1796 | /* |
| 1797 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1798 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1799 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1800 | static lvar_T * |
Bram Moolenaar | e8211a3 | 2020-10-09 22:04:29 +0200 | [diff] [blame] | 1801 | reserve_local( |
| 1802 | cctx_T *cctx, |
| 1803 | char_u *name, |
| 1804 | size_t len, |
| 1805 | int isConst, |
| 1806 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1807 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1808 | lvar_T *lvar; |
| 1809 | |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 1810 | if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1811 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1812 | emsg_namelen(_(e_str_is_used_as_argument), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1813 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1817 | return NULL; |
| 1818 | 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] | 1819 | CLEAR_POINTER(lvar); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1820 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1821 | // Every local variable uses the next entry on the stack. We could re-use |
| 1822 | // the last ones when leaving a scope, but then variables used in a closure |
| 1823 | // might get overwritten. To keep things simple do not re-use stack |
| 1824 | // entries. This is less efficient, but memory is cheap these days. |
| 1825 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1826 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1827 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1828 | lvar->lv_const = isConst; |
| 1829 | lvar->lv_type = type; |
| 1830 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1831 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1835 | * Remove local variables above "new_top". |
| 1836 | */ |
| 1837 | static void |
| 1838 | unwind_locals(cctx_T *cctx, int new_top) |
| 1839 | { |
| 1840 | if (cctx->ctx_locals.ga_len > new_top) |
| 1841 | { |
| 1842 | int idx; |
| 1843 | lvar_T *lvar; |
| 1844 | |
| 1845 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1846 | { |
| 1847 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1848 | vim_free(lvar->lv_name); |
| 1849 | } |
| 1850 | } |
| 1851 | cctx->ctx_locals.ga_len = new_top; |
| 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | * Free all local variables. |
| 1856 | */ |
| 1857 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1858 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1859 | { |
| 1860 | unwind_locals(cctx, 0); |
| 1861 | ga_clear(&cctx->ctx_locals); |
| 1862 | } |
| 1863 | |
| 1864 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1865 | * Find "name" in script-local items of script "sid". |
| 1866 | * Returns the index in "sn_var_vals" if found. |
| 1867 | * If found but not in "sn_var_vals" returns -1. |
| 1868 | * If not found returns -2. |
| 1869 | */ |
| 1870 | int |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 1871 | 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] | 1872 | { |
| 1873 | hashtab_T *ht; |
| 1874 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1875 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 1876 | svar_T *sv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1877 | int idx; |
| 1878 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1879 | if (!SCRIPT_ID_VALID(sid)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1880 | return -1; |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 1881 | if (sid == current_sctx.sc_sid) |
| 1882 | { |
Bram Moolenaar | 209f020 | 2020-10-15 13:57:56 +0200 | [diff] [blame] | 1883 | sallvar_T *sav = find_script_var(name, 0, cctx); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 1884 | |
| 1885 | if (sav == NULL) |
| 1886 | return -2; |
| 1887 | idx = sav->sav_var_vals_idx; |
| 1888 | sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1889 | if (check_writable && sv->sv_const) |
| 1890 | semsg(_(e_readonlyvar), name); |
| 1891 | return idx; |
| 1892 | } |
| 1893 | |
| 1894 | // First look the name up in the hashtable. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1895 | ht = &SCRIPT_VARS(sid); |
| 1896 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 1897 | if (di == NULL) |
| 1898 | return -2; |
| 1899 | |
| 1900 | // Now find the svar_T index in sn_var_vals. |
| 1901 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 1902 | { |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 1903 | sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1904 | if (sv->sv_tv == &di->di_tv) |
| 1905 | { |
| 1906 | if (check_writable && sv->sv_const) |
| 1907 | semsg(_(e_readonlyvar), name); |
| 1908 | return idx; |
| 1909 | } |
| 1910 | } |
| 1911 | return -1; |
| 1912 | } |
| 1913 | |
| 1914 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 1915 | * Find "name" in imported items of the current script or in "cctx" if not |
| 1916 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1917 | */ |
| 1918 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1919 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1920 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1921 | int idx; |
| 1922 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1923 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 1924 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1925 | if (cctx != NULL) |
| 1926 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1927 | { |
| 1928 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 1929 | + idx; |
| 1930 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1931 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1932 | : STRLEN(import->imp_name) == len |
| 1933 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1934 | return import; |
| 1935 | } |
| 1936 | |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 1937 | return find_imported_in_script(name, len, current_sctx.sc_sid); |
| 1938 | } |
| 1939 | |
| 1940 | imported_T * |
| 1941 | find_imported_in_script(char_u *name, size_t len, int sid) |
| 1942 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1943 | scriptitem_T *si; |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 1944 | int idx; |
| 1945 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1946 | if (!SCRIPT_ID_VALID(sid)) |
| 1947 | return NULL; |
| 1948 | si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1949 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 1950 | { |
| 1951 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 1952 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1953 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1954 | : STRLEN(import->imp_name) == len |
| 1955 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1956 | return import; |
| 1957 | } |
| 1958 | return NULL; |
| 1959 | } |
| 1960 | |
| 1961 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1962 | * Free all imported variables. |
| 1963 | */ |
| 1964 | static void |
| 1965 | free_imported(cctx_T *cctx) |
| 1966 | { |
| 1967 | int idx; |
| 1968 | |
| 1969 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1970 | { |
| 1971 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 1972 | |
| 1973 | vim_free(import->imp_name); |
| 1974 | } |
| 1975 | ga_clear(&cctx->ctx_imports); |
| 1976 | } |
| 1977 | |
| 1978 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1979 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 1980 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 1981 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1982 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1983 | { |
| 1984 | return p[0] == '#' && p[1] != '{'; |
| 1985 | } |
| 1986 | |
| 1987 | /* |
| 1988 | * Return a pointer to the next line that isn't empty or only contains a |
| 1989 | * comment. Skips over white space. |
| 1990 | * Returns NULL if there is none. |
| 1991 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1992 | char_u * |
| 1993 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1994 | { |
| 1995 | int lnum = cctx->ctx_lnum; |
| 1996 | |
| 1997 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 1998 | { |
| 1999 | 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] | 2000 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2001 | |
Bram Moolenaar | ba60cc4 | 2020-08-12 19:15:33 +0200 | [diff] [blame] | 2002 | // ignore NULLs inserted for continuation lines |
| 2003 | if (line != NULL) |
| 2004 | { |
| 2005 | p = skipwhite(line); |
| 2006 | if (*p != NUL && !vim9_comment_start(p)) |
| 2007 | return p; |
| 2008 | } |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2009 | } |
| 2010 | return NULL; |
| 2011 | } |
| 2012 | |
| 2013 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2014 | * Called when checking for a following operator at "arg". When the rest of |
| 2015 | * the line is empty or only a comment, peek the next line. If there is a next |
| 2016 | * line return a pointer to it and set "nextp". |
| 2017 | * Otherwise skip over white space. |
| 2018 | */ |
| 2019 | static char_u * |
| 2020 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 2021 | { |
| 2022 | char_u *p = skipwhite(arg); |
| 2023 | |
| 2024 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2025 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2026 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2027 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 2028 | if (*nextp != NULL) |
| 2029 | return *nextp; |
| 2030 | } |
| 2031 | return p; |
| 2032 | } |
| 2033 | |
| 2034 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2035 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2036 | * 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] | 2037 | * Returns NULL when at the end. |
| 2038 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2039 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2040 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2041 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2042 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2043 | |
| 2044 | do |
| 2045 | { |
| 2046 | ++cctx->ctx_lnum; |
| 2047 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2048 | { |
| 2049 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2050 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 2051 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2052 | 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] | 2053 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 2054 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2055 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 2056 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2057 | return line; |
| 2058 | } |
| 2059 | |
| 2060 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2061 | * 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] | 2062 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2063 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 2064 | */ |
| 2065 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2066 | 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] | 2067 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2068 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2069 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2070 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2071 | |
| 2072 | if (next == NULL) |
| 2073 | return FAIL; |
| 2074 | *arg = skipwhite(next); |
| 2075 | } |
| 2076 | return OK; |
| 2077 | } |
| 2078 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2079 | /* |
| 2080 | * Idem, and give an error when failed. |
| 2081 | */ |
| 2082 | static int |
| 2083 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 2084 | { |
| 2085 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 2086 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2087 | emsg(_(e_line_incomplete)); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2088 | return FAIL; |
| 2089 | } |
| 2090 | return OK; |
| 2091 | } |
| 2092 | |
| 2093 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2094 | // Structure passed between the compile_expr* functions to keep track of |
| 2095 | // constants that have been parsed but for which no code was produced yet. If |
| 2096 | // possible expressions on these constants are applied at compile time. If |
| 2097 | // that is not possible, the code to push the constants needs to be generated |
| 2098 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2099 | // Using 50 should be more than enough of 5 levels of (). |
| 2100 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2101 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 2102 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2103 | int pp_used; // active entries in pp_tv[] |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2104 | int pp_is_const; // all generated code was constants, used for a |
| 2105 | // list or dict with constant members |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2106 | } ppconst_T; |
| 2107 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2108 | 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] | 2109 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 2110 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 2111 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2112 | /* |
| 2113 | * Generate a PUSH instruction for "tv". |
| 2114 | * "tv" will be consumed or cleared. |
| 2115 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 2116 | */ |
| 2117 | static int |
| 2118 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 2119 | { |
| 2120 | if (tv != NULL) |
| 2121 | { |
| 2122 | switch (tv->v_type) |
| 2123 | { |
| 2124 | case VAR_UNKNOWN: |
| 2125 | break; |
| 2126 | case VAR_BOOL: |
| 2127 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 2128 | break; |
| 2129 | case VAR_SPECIAL: |
| 2130 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 2131 | break; |
| 2132 | case VAR_NUMBER: |
| 2133 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 2134 | break; |
| 2135 | #ifdef FEAT_FLOAT |
| 2136 | case VAR_FLOAT: |
| 2137 | generate_PUSHF(cctx, tv->vval.v_float); |
| 2138 | break; |
| 2139 | #endif |
| 2140 | case VAR_BLOB: |
| 2141 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 2142 | tv->vval.v_blob = NULL; |
| 2143 | break; |
| 2144 | case VAR_STRING: |
| 2145 | generate_PUSHS(cctx, tv->vval.v_string); |
| 2146 | tv->vval.v_string = NULL; |
| 2147 | break; |
| 2148 | default: |
| 2149 | iemsg("constant type not supported"); |
| 2150 | clear_tv(tv); |
| 2151 | return FAIL; |
| 2152 | } |
| 2153 | tv->v_type = VAR_UNKNOWN; |
| 2154 | } |
| 2155 | return OK; |
| 2156 | } |
| 2157 | |
| 2158 | /* |
| 2159 | * Generate code for any ppconst entries. |
| 2160 | */ |
| 2161 | static int |
| 2162 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 2163 | { |
| 2164 | int i; |
| 2165 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2166 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2167 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 2168 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2169 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2170 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 2171 | ret = FAIL; |
| 2172 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2173 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2174 | return ret; |
| 2175 | } |
| 2176 | |
| 2177 | /* |
| 2178 | * Clear ppconst constants. Used when failing. |
| 2179 | */ |
| 2180 | static void |
| 2181 | clear_ppconst(ppconst_T *ppconst) |
| 2182 | { |
| 2183 | int i; |
| 2184 | |
| 2185 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2186 | clear_tv(&ppconst->pp_tv[i]); |
| 2187 | ppconst->pp_used = 0; |
| 2188 | } |
| 2189 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2190 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2191 | * Generate an instruction to load script-local variable "name", without the |
| 2192 | * leading "s:". |
| 2193 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2194 | */ |
| 2195 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2196 | compile_load_scriptvar( |
| 2197 | cctx_T *cctx, |
| 2198 | char_u *name, // variable NUL terminated |
| 2199 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2200 | char_u **end, // end of variable |
| 2201 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2202 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2203 | scriptitem_T *si; |
| 2204 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2205 | imported_T *import; |
| 2206 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2207 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
| 2208 | return FAIL; |
| 2209 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2210 | idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE, cctx); |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2211 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2212 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2213 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2214 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2215 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2216 | } |
| 2217 | if (idx >= 0) |
| 2218 | { |
| 2219 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2220 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2221 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2222 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2223 | return OK; |
| 2224 | } |
| 2225 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2226 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2227 | if (import != NULL) |
| 2228 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2229 | if (import->imp_all) |
| 2230 | { |
| 2231 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2232 | char_u *exp_name; |
| 2233 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2234 | ufunc_T *ufunc; |
| 2235 | type_T *type; |
| 2236 | |
| 2237 | // Used "import * as Name", need to lookup the member. |
| 2238 | if (*p != '.') |
| 2239 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2240 | semsg(_(e_expected_dot_after_name_str), start); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2241 | return FAIL; |
| 2242 | } |
| 2243 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2244 | if (VIM_ISWHITE(*p)) |
| 2245 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2246 | emsg(_(e_no_white_space_allowed_after_dot)); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2247 | return FAIL; |
| 2248 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2249 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2250 | // isolate one name |
| 2251 | exp_name = p; |
| 2252 | while (eval_isnamec(*p)) |
| 2253 | ++p; |
| 2254 | cc = *p; |
| 2255 | *p = NUL; |
| 2256 | |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2257 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type, cctx); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2258 | *p = cc; |
| 2259 | p = skipwhite(p); |
| 2260 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2261 | // TODO: what if it is a function? |
| 2262 | if (idx < 0) |
| 2263 | return FAIL; |
| 2264 | *end = p; |
| 2265 | |
| 2266 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2267 | import->imp_sid, |
| 2268 | idx, |
| 2269 | type); |
| 2270 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2271 | else if (import->imp_funcname != NULL) |
| 2272 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2273 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2274 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2275 | import->imp_sid, |
| 2276 | import->imp_var_vals_idx, |
| 2277 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2278 | return OK; |
| 2279 | } |
| 2280 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2281 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2282 | semsg(_(e_item_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2283 | return FAIL; |
| 2284 | } |
| 2285 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2286 | static int |
| 2287 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2288 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2289 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2290 | |
| 2291 | if (ufunc == NULL) |
| 2292 | return FAIL; |
| 2293 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2294 | // Need to compile any default values to get the argument types. |
| 2295 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2296 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2297 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2298 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2299 | } |
| 2300 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2301 | /* |
| 2302 | * Compile a variable name into a load instruction. |
| 2303 | * "end" points to just after the name. |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2304 | * "is_expr" is TRUE when evaluating an expression, might be a funcref. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2305 | * When "error" is FALSE do not give an error when not found. |
| 2306 | */ |
| 2307 | static int |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2308 | compile_load( |
| 2309 | char_u **arg, |
| 2310 | char_u *end_arg, |
| 2311 | cctx_T *cctx, |
| 2312 | int is_expr, |
| 2313 | int error) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2314 | { |
| 2315 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2316 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2317 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2318 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2319 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2320 | |
| 2321 | if (*(*arg + 1) == ':') |
| 2322 | { |
| 2323 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2324 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2325 | { |
| 2326 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2327 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2328 | switch (**arg) |
| 2329 | { |
| 2330 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2331 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2332 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2333 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2334 | default: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2335 | semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2336 | goto theend; |
| 2337 | } |
| 2338 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2339 | goto theend; |
| 2340 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2341 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2342 | else |
| 2343 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2344 | isntype_T isn_type = ISN_DROP; |
| 2345 | |
| 2346 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2347 | if (name == NULL) |
| 2348 | return FAIL; |
| 2349 | |
| 2350 | switch (**arg) |
| 2351 | { |
| 2352 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2353 | break; |
| 2354 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2355 | NULL, NULL, error); |
| 2356 | break; |
| 2357 | case 'g': isn_type = ISN_LOADG; break; |
| 2358 | case 'w': isn_type = ISN_LOADW; break; |
| 2359 | case 't': isn_type = ISN_LOADT; break; |
| 2360 | case 'b': isn_type = ISN_LOADB; break; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2361 | default: semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2362 | goto theend; |
| 2363 | } |
| 2364 | if (isn_type != ISN_DROP) |
| 2365 | { |
| 2366 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2367 | // variables can be defined later, thus we don't check if it |
| 2368 | // exists, give error at runtime. |
| 2369 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2370 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2371 | } |
| 2372 | } |
| 2373 | else |
| 2374 | { |
| 2375 | size_t len = end - *arg; |
| 2376 | int idx; |
| 2377 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2378 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2379 | |
| 2380 | name = vim_strnsave(*arg, end - *arg); |
| 2381 | if (name == NULL) |
| 2382 | return FAIL; |
| 2383 | |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2384 | if (arg_exists(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2385 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2386 | if (!gen_load_outer) |
| 2387 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2388 | } |
| 2389 | else |
| 2390 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2391 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2392 | |
| 2393 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2394 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2395 | type = lvar->lv_type; |
| 2396 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2397 | if (lvar->lv_from_outer) |
| 2398 | gen_load_outer = TRUE; |
| 2399 | else |
| 2400 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2401 | } |
| 2402 | else |
| 2403 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2404 | // "var" can be script-local even without using "s:" if it |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2405 | // already exists in a Vim9 script or when it's imported. |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 2406 | if (script_var_exists(*arg, len, TRUE, cctx) == OK |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2407 | || find_imported(name, 0, cctx) != NULL) |
| 2408 | res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2409 | |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2410 | // When evaluating an expression and the name starts with an |
| 2411 | // uppercase letter or "x:" it can be a user defined function. |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2412 | // TODO: this is just guessing |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2413 | if (res == FAIL && is_expr |
| 2414 | && (ASCII_ISUPPER(*name) || name[1] == ':')) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2415 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2416 | } |
| 2417 | } |
| 2418 | if (gen_load) |
| 2419 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2420 | if (gen_load_outer) |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2421 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2422 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2423 | cctx->ctx_outer_used = TRUE; |
| 2424 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | *arg = end; |
| 2428 | |
| 2429 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2430 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2431 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2432 | vim_free(name); |
| 2433 | return res; |
| 2434 | } |
| 2435 | |
| 2436 | /* |
| 2437 | * Compile the argument expressions. |
| 2438 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2439 | */ |
| 2440 | static int |
| 2441 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2442 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2443 | char_u *p = *arg; |
| 2444 | char_u *whitep = *arg; |
Bram Moolenaar | 10e4f12 | 2020-09-20 22:43:52 +0200 | [diff] [blame] | 2445 | int must_end = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2446 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2447 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2448 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2449 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2450 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2451 | if (*p == ')') |
| 2452 | { |
| 2453 | *arg = p + 1; |
| 2454 | return OK; |
| 2455 | } |
Bram Moolenaar | 10e4f12 | 2020-09-20 22:43:52 +0200 | [diff] [blame] | 2456 | if (must_end) |
| 2457 | { |
| 2458 | semsg(_(e_missing_comma_before_argument_str), p); |
| 2459 | return FAIL; |
| 2460 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2461 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2462 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2463 | return FAIL; |
| 2464 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2465 | |
| 2466 | if (*p != ',' && *skipwhite(p) == ',') |
| 2467 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2468 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2469 | p = skipwhite(p); |
| 2470 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2471 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2472 | { |
| 2473 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2474 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2475 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2476 | } |
Bram Moolenaar | 10e4f12 | 2020-09-20 22:43:52 +0200 | [diff] [blame] | 2477 | else |
| 2478 | must_end = TRUE; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2479 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2480 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2481 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2482 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2483 | emsg(_(e_missing_close)); |
| 2484 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | /* |
| 2488 | * Compile a function call: name(arg1, arg2) |
| 2489 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2490 | * "argcount_init" is 1 for "value->method()" |
| 2491 | * Instructions: |
| 2492 | * EVAL arg1 |
| 2493 | * EVAL arg2 |
| 2494 | * BCALL / DCALL / UCALL |
| 2495 | */ |
| 2496 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2497 | compile_call( |
| 2498 | char_u **arg, |
| 2499 | size_t varlen, |
| 2500 | cctx_T *cctx, |
| 2501 | ppconst_T *ppconst, |
| 2502 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2503 | { |
| 2504 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2505 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2506 | int argcount = argcount_init; |
| 2507 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2508 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2509 | char_u *tofree = NULL; |
| 2510 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2511 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2512 | int res = FAIL; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2513 | int is_autoload; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2514 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2515 | // we can evaluate "has('name')" at compile time |
| 2516 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2517 | { |
| 2518 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2519 | typval_T argvars[2]; |
| 2520 | |
| 2521 | argvars[0].v_type = VAR_UNKNOWN; |
| 2522 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2523 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2524 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2525 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2526 | s = skipwhite(s); |
| 2527 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 2528 | { |
| 2529 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2530 | |
| 2531 | *arg = s + 1; |
| 2532 | argvars[1].v_type = VAR_UNKNOWN; |
| 2533 | tv->v_type = VAR_NUMBER; |
| 2534 | tv->vval.v_number = 0; |
| 2535 | f_has(argvars, tv); |
| 2536 | clear_tv(&argvars[0]); |
| 2537 | ++ppconst->pp_used; |
| 2538 | return OK; |
| 2539 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2540 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2544 | return FAIL; |
| 2545 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2546 | if (varlen >= sizeof(namebuf)) |
| 2547 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2548 | semsg(_(e_name_too_long_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2549 | return FAIL; |
| 2550 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2551 | vim_strncpy(namebuf, *arg, varlen); |
| 2552 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2553 | |
| 2554 | *arg = skipwhite(*arg + varlen + 1); |
| 2555 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2556 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2557 | |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2558 | is_autoload = vim_strchr(name, '#') != NULL; |
| 2559 | if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2560 | { |
| 2561 | int idx; |
| 2562 | |
| 2563 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2564 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2565 | if (idx >= 0) |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 2566 | { |
| 2567 | if (STRCMP(name, "add") == 0 && argcount == 2) |
| 2568 | { |
| 2569 | garray_T *stack = &cctx->ctx_type_stack; |
| 2570 | type_T *type = ((type_T **)stack->ga_data)[ |
| 2571 | stack->ga_len - 2]; |
| 2572 | |
| 2573 | // TODO: also check for VAR_BLOB |
| 2574 | if (type->tt_type == VAR_LIST) |
| 2575 | { |
| 2576 | // inline "add(list, item)" so that the type can be checked |
| 2577 | res = generate_LISTAPPEND(cctx); |
| 2578 | idx = -1; |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | if (idx >= 0) |
| 2583 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
| 2584 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2585 | else |
| 2586 | semsg(_(e_unknownfunc), namebuf); |
| 2587 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2588 | } |
| 2589 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2590 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2591 | // Skip global functions here, a local funcref takes precedence. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2592 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2593 | if (ufunc != NULL && !func_is_global(ufunc)) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2594 | { |
| 2595 | res = generate_CALL(cctx, ufunc, argcount); |
| 2596 | goto theend; |
| 2597 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2598 | |
| 2599 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2600 | // 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] | 2601 | // Not for eome#Func(), it will be loaded later. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2602 | p = namebuf; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2603 | if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2604 | && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2605 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2606 | garray_T *stack = &cctx->ctx_type_stack; |
| 2607 | type_T *type; |
| 2608 | |
| 2609 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2610 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2611 | goto theend; |
| 2612 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2613 | |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2614 | // If we can find a global function by name generate the right call. |
| 2615 | if (ufunc != NULL) |
| 2616 | { |
| 2617 | res = generate_CALL(cctx, ufunc, argcount); |
| 2618 | goto theend; |
| 2619 | } |
| 2620 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2621 | // 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] | 2622 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2623 | if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload) |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2624 | res = generate_UCALL(cctx, name, argcount); |
| 2625 | else |
| 2626 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2627 | |
| 2628 | theend: |
| 2629 | vim_free(tofree); |
| 2630 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2634 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2635 | |
| 2636 | /* |
| 2637 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2638 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2639 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2640 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2641 | * valid name. |
| 2642 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2643 | static char_u * |
| 2644 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2645 | { |
| 2646 | char_u *p; |
| 2647 | |
| 2648 | // Quick check for valid starting character. |
| 2649 | if (!eval_isnamec1(*arg)) |
| 2650 | return arg; |
| 2651 | |
| 2652 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2653 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2654 | // and can be used in slice "[n:]". |
| 2655 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2656 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2657 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2658 | break; |
| 2659 | return p; |
| 2660 | } |
| 2661 | |
| 2662 | /* |
| 2663 | * 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] | 2664 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2665 | */ |
| 2666 | char_u * |
| 2667 | to_name_const_end(char_u *arg) |
| 2668 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2669 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2670 | typval_T rettv; |
| 2671 | |
| 2672 | if (p == arg && *arg == '[') |
| 2673 | { |
| 2674 | |
| 2675 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2676 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2677 | p = arg; |
| 2678 | } |
| 2679 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 2680 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2681 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2682 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 2683 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2684 | p = arg; |
| 2685 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2686 | |
| 2687 | return p; |
| 2688 | } |
| 2689 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2690 | /* |
| 2691 | * parse a list: [expr, expr] |
| 2692 | * "*arg" points to the '['. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2693 | * ppconst->pp_is_const is set if all items are a constant. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2694 | */ |
| 2695 | static int |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2696 | compile_list(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2697 | { |
| 2698 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2699 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2700 | int count = 0; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2701 | int is_const; |
| 2702 | int is_all_const = TRUE; // reset when non-const encountered |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2703 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2704 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2705 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2706 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2707 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2708 | semsg(_(e_list_end), *arg); |
| 2709 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2710 | } |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2711 | if (*p == ',') |
| 2712 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2713 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2714 | return FAIL; |
| 2715 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2716 | if (*p == ']') |
| 2717 | { |
| 2718 | ++p; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2719 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2720 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2721 | if (compile_expr0_ext(&p, cctx, &is_const) == FAIL) |
Bram Moolenaar | c1f0066 | 2020-10-03 13:41:53 +0200 | [diff] [blame] | 2722 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2723 | if (!is_const) |
| 2724 | is_all_const = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2725 | ++count; |
| 2726 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2727 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2728 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2729 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 2730 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2731 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2732 | return FAIL; |
| 2733 | } |
| 2734 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2735 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2736 | p = skipwhite(p); |
| 2737 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2738 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2739 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2740 | ppconst->pp_is_const = is_all_const; |
| 2741 | return generate_NEWLIST(cctx, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | /* |
| 2745 | * parse a lambda: {arg, arg -> expr} |
| 2746 | * "*arg" points to the '{'. |
| 2747 | */ |
| 2748 | static int |
| 2749 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 2750 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2751 | typval_T rettv; |
| 2752 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2753 | evalarg_T evalarg; |
| 2754 | |
| 2755 | CLEAR_FIELD(evalarg); |
| 2756 | evalarg.eval_flags = EVAL_EVALUATE; |
| 2757 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2758 | |
| 2759 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2760 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 2ea79ad | 2020-10-18 23:32:13 +0200 | [diff] [blame] | 2761 | { |
| 2762 | clear_evalarg(&evalarg, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2763 | return FAIL; |
Bram Moolenaar | 2ea79ad | 2020-10-18 23:32:13 +0200 | [diff] [blame] | 2764 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2765 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2766 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2767 | ++ufunc->uf_refcount; |
| 2768 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2769 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2770 | |
| 2771 | // The function will have one line: "return {expr}". |
| 2772 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2773 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2774 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 2775 | clear_evalarg(&evalarg, NULL); |
| 2776 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 2777 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 2778 | { |
| 2779 | // The return type will now be known. |
| 2780 | set_function_type(ufunc); |
| 2781 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 2782 | // The function reference count will be 1. When the ISN_FUNCREF |
| 2783 | // instruction is deleted the reference count is decremented and the |
| 2784 | // function is freed. |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 2785 | return generate_FUNCREF(cctx, ufunc); |
| 2786 | } |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2787 | |
| 2788 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2789 | return FAIL; |
| 2790 | } |
| 2791 | |
| 2792 | /* |
| 2793 | * Compile a lamda call: expr->{lambda}(args) |
| 2794 | * "arg" points to the "{". |
| 2795 | */ |
| 2796 | static int |
| 2797 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 2798 | { |
| 2799 | ufunc_T *ufunc; |
| 2800 | typval_T rettv; |
| 2801 | int argcount = 1; |
| 2802 | int ret = FAIL; |
| 2803 | |
| 2804 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 2805 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2806 | return FAIL; |
| 2807 | |
| 2808 | if (**arg != '(') |
| 2809 | { |
| 2810 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 2811 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2812 | else |
| 2813 | semsg(_(e_missing_paren), "lambda"); |
| 2814 | clear_tv(&rettv); |
| 2815 | return FAIL; |
| 2816 | } |
| 2817 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2818 | ufunc = rettv.vval.v_partial->pt_func; |
| 2819 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2820 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2821 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2822 | |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 2823 | // The function will have one line: "return {expr}". Compile it into |
| 2824 | // instructions so that we get any errors right now. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2825 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2826 | |
| 2827 | // compile the arguments |
| 2828 | *arg = skipwhite(*arg + 1); |
| 2829 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 2830 | // call the compiled function |
| 2831 | ret = generate_CALL(cctx, ufunc, argcount); |
| 2832 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2833 | if (ret == FAIL) |
| 2834 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2835 | return ret; |
| 2836 | } |
| 2837 | |
| 2838 | /* |
| 2839 | * parse a dict: {'key': val} or #{key: val} |
| 2840 | * "*arg" points to the '{'. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2841 | * 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] | 2842 | */ |
| 2843 | static int |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2844 | compile_dict(char_u **arg, cctx_T *cctx, int literal, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2845 | { |
| 2846 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2847 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2848 | int count = 0; |
| 2849 | dict_T *d = dict_alloc(); |
| 2850 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2851 | char_u *whitep = *arg; |
| 2852 | char_u *p; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2853 | int is_const; |
| 2854 | int is_all_const = TRUE; // reset when non-const encountered |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2855 | |
| 2856 | if (d == NULL) |
| 2857 | return FAIL; |
| 2858 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2859 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2860 | { |
| 2861 | char_u *key = NULL; |
| 2862 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2863 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2864 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2865 | *arg = NULL; |
| 2866 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | if (**arg == '}') |
| 2870 | break; |
| 2871 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2872 | if (literal) |
| 2873 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2874 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2875 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2876 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2877 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2878 | semsg(_(e_invalid_key_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2879 | return FAIL; |
| 2880 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2881 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2882 | if (generate_PUSHS(cctx, key) == FAIL) |
| 2883 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2884 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2885 | } |
| 2886 | else |
| 2887 | { |
| 2888 | isn_T *isn; |
| 2889 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2890 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2891 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2892 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 2893 | if (isn->isn_type == ISN_PUSHS) |
| 2894 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2895 | else |
| 2896 | { |
| 2897 | type_T *keytype = ((type_T **)stack->ga_data) |
| 2898 | [stack->ga_len - 1]; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2899 | if (need_type(keytype, &t_string, -1, cctx, |
| 2900 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2901 | return FAIL; |
| 2902 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | // Check for duplicate keys, if using string keys. |
| 2906 | if (key != NULL) |
| 2907 | { |
| 2908 | item = dict_find(d, key, -1); |
| 2909 | if (item != NULL) |
| 2910 | { |
| 2911 | semsg(_(e_duplicate_key), key); |
| 2912 | goto failret; |
| 2913 | } |
| 2914 | item = dictitem_alloc(key); |
| 2915 | if (item != NULL) |
| 2916 | { |
| 2917 | item->di_tv.v_type = VAR_UNKNOWN; |
| 2918 | item->di_tv.v_lock = 0; |
| 2919 | if (dict_add(d, item) == FAIL) |
| 2920 | dictitem_free(item); |
| 2921 | } |
| 2922 | } |
| 2923 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2924 | if (**arg != ':') |
| 2925 | { |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 2926 | if (*skipwhite(*arg) == ':') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2927 | semsg(_(e_no_white_space_allowed_before_str), ":"); |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 2928 | else |
| 2929 | semsg(_(e_missing_dict_colon), *arg); |
| 2930 | return FAIL; |
| 2931 | } |
| 2932 | whitep = *arg + 1; |
| 2933 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 2934 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2935 | semsg(_(e_white_space_required_after_str), ":"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2936 | return FAIL; |
| 2937 | } |
| 2938 | |
| 2939 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2940 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2941 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2942 | *arg = NULL; |
| 2943 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2944 | } |
| 2945 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2946 | if (compile_expr0_ext(arg, cctx, &is_const) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2947 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2948 | if (!is_const) |
| 2949 | is_all_const = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2950 | ++count; |
| 2951 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2952 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2953 | *arg = skipwhite(*arg); |
| 2954 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2955 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2956 | *arg = NULL; |
| 2957 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2958 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2959 | if (**arg == '}') |
| 2960 | break; |
| 2961 | if (**arg != ',') |
| 2962 | { |
| 2963 | semsg(_(e_missing_dict_comma), *arg); |
| 2964 | goto failret; |
| 2965 | } |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 2966 | if (IS_WHITE_OR_NUL(*whitep)) |
| 2967 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2968 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 2969 | return FAIL; |
| 2970 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2971 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2972 | *arg = skipwhite(*arg + 1); |
| 2973 | } |
| 2974 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2975 | *arg = *arg + 1; |
| 2976 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2977 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2978 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2979 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2980 | *arg += STRLEN(*arg); |
| 2981 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2982 | dict_unref(d); |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 2983 | ppconst->pp_is_const = is_all_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2984 | return generate_NEWDICT(cctx, count); |
| 2985 | |
| 2986 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2987 | if (*arg == NULL) |
Bram Moolenaar | 44aefff | 2020-10-05 19:23:59 +0200 | [diff] [blame] | 2988 | { |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2989 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 44aefff | 2020-10-05 19:23:59 +0200 | [diff] [blame] | 2990 | *arg = (char_u *)""; |
| 2991 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2992 | dict_unref(d); |
| 2993 | return FAIL; |
| 2994 | } |
| 2995 | |
| 2996 | /* |
| 2997 | * Compile "&option". |
| 2998 | */ |
| 2999 | static int |
| 3000 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 3001 | { |
| 3002 | typval_T rettv; |
| 3003 | char_u *start = *arg; |
| 3004 | int ret; |
| 3005 | |
| 3006 | // parse the option and get the current value to get the type. |
| 3007 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3008 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3009 | if (ret == OK) |
| 3010 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3011 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3012 | char_u *name = vim_strnsave(start, *arg - start); |
| 3013 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 3014 | |
| 3015 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 3016 | vim_free(name); |
| 3017 | } |
| 3018 | clear_tv(&rettv); |
| 3019 | |
| 3020 | return ret; |
| 3021 | } |
| 3022 | |
| 3023 | /* |
| 3024 | * Compile "$VAR". |
| 3025 | */ |
| 3026 | static int |
| 3027 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 3028 | { |
| 3029 | char_u *start = *arg; |
| 3030 | int len; |
| 3031 | int ret; |
| 3032 | char_u *name; |
| 3033 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3034 | ++*arg; |
| 3035 | len = get_env_len(arg); |
| 3036 | if (len == 0) |
| 3037 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3038 | semsg(_(e_syntax_error_at_str), start - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3039 | return FAIL; |
| 3040 | } |
| 3041 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3042 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3043 | name = vim_strnsave(start, len + 1); |
| 3044 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 3045 | vim_free(name); |
| 3046 | return ret; |
| 3047 | } |
| 3048 | |
| 3049 | /* |
| 3050 | * Compile "@r". |
| 3051 | */ |
| 3052 | static int |
| 3053 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 3054 | { |
| 3055 | int ret; |
| 3056 | |
| 3057 | ++*arg; |
| 3058 | if (**arg == NUL) |
| 3059 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3060 | semsg(_(e_syntax_error_at_str), *arg - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3061 | return FAIL; |
| 3062 | } |
Bram Moolenaar | 7226e5b | 2020-08-02 17:33:26 +0200 | [diff] [blame] | 3063 | if (!valid_yank_reg(**arg, FALSE)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3064 | { |
| 3065 | emsg_invreg(**arg); |
| 3066 | return FAIL; |
| 3067 | } |
| 3068 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 3069 | ++*arg; |
| 3070 | return ret; |
| 3071 | } |
| 3072 | |
| 3073 | /* |
| 3074 | * Apply leading '!', '-' and '+' to constant "rettv". |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3075 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3076 | */ |
| 3077 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3078 | 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] | 3079 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3080 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3081 | |
| 3082 | // this works from end to start |
| 3083 | while (p > start) |
| 3084 | { |
| 3085 | --p; |
| 3086 | if (*p == '-' || *p == '+') |
| 3087 | { |
| 3088 | // only '-' has an effect, for '+' we only check the type |
| 3089 | #ifdef FEAT_FLOAT |
| 3090 | if (rettv->v_type == VAR_FLOAT) |
| 3091 | { |
| 3092 | if (*p == '-') |
| 3093 | rettv->vval.v_float = -rettv->vval.v_float; |
| 3094 | } |
| 3095 | else |
| 3096 | #endif |
| 3097 | { |
| 3098 | varnumber_T val; |
| 3099 | int error = FALSE; |
| 3100 | |
| 3101 | // tv_get_number_chk() accepts a string, but we don't want that |
| 3102 | // here |
| 3103 | if (check_not_string(rettv) == FAIL) |
| 3104 | return FAIL; |
| 3105 | val = tv_get_number_chk(rettv, &error); |
| 3106 | clear_tv(rettv); |
| 3107 | if (error) |
| 3108 | return FAIL; |
| 3109 | if (*p == '-') |
| 3110 | val = -val; |
| 3111 | rettv->v_type = VAR_NUMBER; |
| 3112 | rettv->vval.v_number = val; |
| 3113 | } |
| 3114 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3115 | else if (numeric_only) |
| 3116 | { |
| 3117 | ++p; |
| 3118 | break; |
| 3119 | } |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3120 | else if (*p == '!') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3121 | { |
| 3122 | int v = tv2bool(rettv); |
| 3123 | |
| 3124 | // '!' is permissive in the type. |
| 3125 | clear_tv(rettv); |
| 3126 | rettv->v_type = VAR_BOOL; |
| 3127 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 3128 | } |
| 3129 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3130 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3131 | return OK; |
| 3132 | } |
| 3133 | |
| 3134 | /* |
| 3135 | * Recognize v: variables that are constants and set "rettv". |
| 3136 | */ |
| 3137 | static void |
| 3138 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 3139 | { |
| 3140 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 3141 | { |
| 3142 | rettv->v_type = VAR_BOOL; |
| 3143 | rettv->vval.v_number = VVAL_TRUE; |
| 3144 | *arg += 6; |
| 3145 | } |
| 3146 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 3147 | { |
| 3148 | rettv->v_type = VAR_BOOL; |
| 3149 | rettv->vval.v_number = VVAL_FALSE; |
| 3150 | *arg += 7; |
| 3151 | } |
| 3152 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 3153 | { |
| 3154 | rettv->v_type = VAR_SPECIAL; |
| 3155 | rettv->vval.v_number = VVAL_NULL; |
| 3156 | *arg += 6; |
| 3157 | } |
| 3158 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 3159 | { |
| 3160 | rettv->v_type = VAR_SPECIAL; |
| 3161 | rettv->vval.v_number = VVAL_NONE; |
| 3162 | *arg += 6; |
| 3163 | } |
| 3164 | } |
| 3165 | |
Bram Moolenaar | 696ba23 | 2020-07-29 21:20:41 +0200 | [diff] [blame] | 3166 | exptype_T |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3167 | get_compare_type(char_u *p, int *len, int *type_is) |
| 3168 | { |
| 3169 | exptype_T type = EXPR_UNKNOWN; |
| 3170 | int i; |
| 3171 | |
| 3172 | switch (p[0]) |
| 3173 | { |
| 3174 | case '=': if (p[1] == '=') |
| 3175 | type = EXPR_EQUAL; |
| 3176 | else if (p[1] == '~') |
| 3177 | type = EXPR_MATCH; |
| 3178 | break; |
| 3179 | case '!': if (p[1] == '=') |
| 3180 | type = EXPR_NEQUAL; |
| 3181 | else if (p[1] == '~') |
| 3182 | type = EXPR_NOMATCH; |
| 3183 | break; |
| 3184 | case '>': if (p[1] != '=') |
| 3185 | { |
| 3186 | type = EXPR_GREATER; |
| 3187 | *len = 1; |
| 3188 | } |
| 3189 | else |
| 3190 | type = EXPR_GEQUAL; |
| 3191 | break; |
| 3192 | case '<': if (p[1] != '=') |
| 3193 | { |
| 3194 | type = EXPR_SMALLER; |
| 3195 | *len = 1; |
| 3196 | } |
| 3197 | else |
| 3198 | type = EXPR_SEQUAL; |
| 3199 | break; |
| 3200 | case 'i': if (p[1] == 's') |
| 3201 | { |
| 3202 | // "is" and "isnot"; but not a prefix of a name |
| 3203 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 3204 | *len = 5; |
| 3205 | i = p[*len]; |
| 3206 | if (!isalnum(i) && i != '_') |
| 3207 | { |
| 3208 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 3209 | *type_is = TRUE; |
| 3210 | } |
| 3211 | } |
| 3212 | break; |
| 3213 | } |
| 3214 | return type; |
| 3215 | } |
| 3216 | |
| 3217 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3218 | * Compile code to apply '-', '+' and '!'. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3219 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3220 | */ |
| 3221 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3222 | 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] | 3223 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3224 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3225 | |
| 3226 | // this works from end to start |
| 3227 | while (p > start) |
| 3228 | { |
| 3229 | --p; |
| 3230 | if (*p == '-' || *p == '+') |
| 3231 | { |
| 3232 | int negate = *p == '-'; |
| 3233 | isn_T *isn; |
| 3234 | |
| 3235 | // TODO: check type |
| 3236 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3237 | { |
| 3238 | --p; |
| 3239 | if (*p == '-') |
| 3240 | negate = !negate; |
| 3241 | } |
| 3242 | // only '-' has an effect, for '+' we only check the type |
| 3243 | if (negate) |
| 3244 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3245 | else |
| 3246 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3247 | if (isn == NULL) |
| 3248 | return FAIL; |
| 3249 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3250 | else if (numeric_only) |
| 3251 | { |
| 3252 | ++p; |
| 3253 | break; |
| 3254 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3255 | else |
| 3256 | { |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3257 | int invert = *p == '!'; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3258 | |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3259 | while (p > start && (p[-1] == '!' || VIM_ISWHITE(p[-1]))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3260 | { |
Bram Moolenaar | 27491cd | 2020-10-15 21:54:56 +0200 | [diff] [blame] | 3261 | if (p[-1] == '!') |
| 3262 | invert = !invert; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3263 | --p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3264 | } |
| 3265 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3266 | return FAIL; |
| 3267 | } |
| 3268 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3269 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3270 | return OK; |
| 3271 | } |
| 3272 | |
| 3273 | /* |
| 3274 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3275 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3276 | */ |
| 3277 | static int |
| 3278 | compile_subscript( |
| 3279 | char_u **arg, |
| 3280 | cctx_T *cctx, |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3281 | char_u *start_leader, |
| 3282 | char_u **end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3283 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3284 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3285 | char_u *name_start = *end_leader; |
| 3286 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3287 | for (;;) |
| 3288 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3289 | char_u *p = skipwhite(*arg); |
| 3290 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3291 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3292 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3293 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3294 | |
| 3295 | // If a following line starts with "->{" or "->X" advance to that |
| 3296 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3297 | // Also if a following line starts with ".x". |
| 3298 | if (next != NULL && |
| 3299 | ((next[0] == '-' && next[1] == '>' |
| 3300 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3301 | || (next[0] == '.' && eval_isdictc(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3302 | { |
| 3303 | next = next_line_from_context(cctx, TRUE); |
| 3304 | if (next == NULL) |
| 3305 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3306 | *arg = next; |
| 3307 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3308 | } |
| 3309 | } |
| 3310 | |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 3311 | // Do not skip over white space to find the "(", "exeucte 'x' ()" is |
| 3312 | // not a function call. |
| 3313 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3314 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3315 | garray_T *stack = &cctx->ctx_type_stack; |
| 3316 | type_T *type; |
| 3317 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3318 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3319 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3320 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3321 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3322 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3323 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3324 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3325 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3326 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3327 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3328 | return FAIL; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3329 | if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3330 | return FAIL; |
| 3331 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3332 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3333 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3334 | char_u *pstart = p; |
| 3335 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3336 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3337 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3338 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3339 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3340 | // something->method() |
| 3341 | // Apply the '!', '-' and '+' first: |
| 3342 | // -1.0->func() works like (-1.0)->func() |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3343 | if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3344 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3345 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3346 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3347 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3348 | // No line break supported right after "->". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3349 | if (**arg == '{') |
| 3350 | { |
| 3351 | // lambda call: list->{lambda} |
| 3352 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3353 | return FAIL; |
| 3354 | } |
| 3355 | else |
| 3356 | { |
| 3357 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3358 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3359 | if (!eval_isnamec1(*p)) |
| 3360 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3361 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3362 | return FAIL; |
| 3363 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3364 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3365 | p += 2; |
Bram Moolenaar | c5da1fb | 2020-08-05 15:43:44 +0200 | [diff] [blame] | 3366 | for ( ; eval_isnamec(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3367 | ; |
| 3368 | if (*p != '(') |
| 3369 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3370 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3371 | return FAIL; |
| 3372 | } |
| 3373 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3374 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3375 | return FAIL; |
| 3376 | } |
| 3377 | } |
Bram Moolenaar | badd848 | 2020-07-31 22:38:17 +0200 | [diff] [blame] | 3378 | else if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3379 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3380 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3381 | type_T **typep; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3382 | type_T *valtype; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3383 | vartype_T vtype; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3384 | int is_slice = FALSE; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3385 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3386 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3387 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3388 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3389 | // TODO: blob index |
| 3390 | // TODO: more arguments |
| 3391 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3392 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3393 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3394 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3395 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3396 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3397 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3398 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3399 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3400 | if (**arg == ':') |
| 3401 | // missing first index is equal to zero |
| 3402 | generate_PUSHNR(cctx, 0); |
| 3403 | else |
| 3404 | { |
| 3405 | if (compile_expr0(arg, cctx) == FAIL) |
| 3406 | return FAIL; |
| 3407 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3408 | return FAIL; |
| 3409 | *arg = skipwhite(*arg); |
| 3410 | } |
| 3411 | if (**arg == ':') |
| 3412 | { |
| 3413 | *arg = skipwhite(*arg + 1); |
| 3414 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3415 | return FAIL; |
| 3416 | if (**arg == ']') |
| 3417 | // missing second index is equal to end of string |
| 3418 | generate_PUSHNR(cctx, -1); |
| 3419 | else |
| 3420 | { |
| 3421 | if (compile_expr0(arg, cctx) == FAIL) |
| 3422 | return FAIL; |
| 3423 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3424 | return FAIL; |
| 3425 | *arg = skipwhite(*arg); |
| 3426 | } |
| 3427 | is_slice = TRUE; |
| 3428 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3429 | |
| 3430 | if (**arg != ']') |
| 3431 | { |
| 3432 | emsg(_(e_missbrac)); |
| 3433 | return FAIL; |
| 3434 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3435 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3436 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3437 | // We can index a list and a dict. If we don't know the type |
| 3438 | // we can use the index value type. |
| 3439 | // TODO: If we don't know use an instruction to figure it out at |
| 3440 | // runtime. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3441 | typep = ((type_T **)stack->ga_data) + stack->ga_len |
| 3442 | - (is_slice ? 3 : 2); |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3443 | vtype = (*typep)->tt_type; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3444 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3445 | // If the index is a string, the variable must be a Dict. |
| 3446 | if (*typep == &t_any && valtype == &t_string) |
| 3447 | vtype = VAR_DICT; |
| 3448 | if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB) |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3449 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3450 | if (need_type(valtype, &t_number, -1, cctx, |
| 3451 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3452 | return FAIL; |
| 3453 | if (is_slice) |
| 3454 | { |
| 3455 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3456 | if (need_type(valtype, &t_number, -2, cctx, |
| 3457 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3458 | return FAIL; |
| 3459 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3460 | } |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3461 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3462 | if (vtype == VAR_DICT) |
| 3463 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3464 | if (is_slice) |
| 3465 | { |
| 3466 | emsg(_(e_cannot_slice_dictionary)); |
| 3467 | return FAIL; |
| 3468 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3469 | if ((*typep)->tt_type == VAR_DICT) |
| 3470 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3471 | else |
| 3472 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3473 | if (need_type(*typep, &t_dict_any, -2, cctx, |
| 3474 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3475 | return FAIL; |
| 3476 | *typep = &t_any; |
| 3477 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3478 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3479 | return FAIL; |
| 3480 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3481 | return FAIL; |
| 3482 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3483 | else if (vtype == VAR_STRING) |
| 3484 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3485 | *typep = &t_string; |
| 3486 | if ((is_slice |
| 3487 | ? generate_instr_drop(cctx, ISN_STRSLICE, 2) |
| 3488 | : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL) |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3489 | return FAIL; |
| 3490 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3491 | else if (vtype == VAR_BLOB) |
| 3492 | { |
| 3493 | emsg("Sorry, blob index and slice not implemented yet"); |
| 3494 | return FAIL; |
| 3495 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3496 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3497 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3498 | if (is_slice) |
| 3499 | { |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3500 | if (generate_instr_drop(cctx, |
| 3501 | vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE, |
| 3502 | 2) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3503 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3504 | } |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3505 | else |
| 3506 | { |
| 3507 | if ((*typep)->tt_type == VAR_LIST) |
| 3508 | *typep = (*typep)->tt_member; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3509 | if (generate_instr_drop(cctx, |
| 3510 | vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, |
| 3511 | 1) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3512 | return FAIL; |
| 3513 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3514 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3515 | else |
| 3516 | { |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3517 | emsg(_(e_string_list_dict_or_blob_required)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3518 | return FAIL; |
| 3519 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3520 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3521 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3522 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3523 | // dictionary member: dict.name |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3524 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3525 | return FAIL; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3526 | ppconst->pp_is_const = FALSE; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3527 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3528 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3529 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
Bram Moolenaar | c1f0066 | 2020-10-03 13:41:53 +0200 | [diff] [blame] | 3530 | { |
| 3531 | emsg(_(e_missing_name_after_dot)); |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3532 | return FAIL; |
Bram Moolenaar | c1f0066 | 2020-10-03 13:41:53 +0200 | [diff] [blame] | 3533 | } |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3534 | p = *arg; |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3535 | if (eval_isdictc(*p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3536 | while (eval_isnamec(*p)) |
| 3537 | MB_PTR_ADV(p); |
| 3538 | if (p == *arg) |
| 3539 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3540 | semsg(_(e_syntax_error_at_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3541 | return FAIL; |
| 3542 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3543 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3544 | return FAIL; |
| 3545 | *arg = p; |
| 3546 | } |
| 3547 | else |
| 3548 | break; |
| 3549 | } |
| 3550 | |
| 3551 | // TODO - see handle_subscript(): |
| 3552 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3553 | // Don't do this when "Func" is already a partial that was bound |
| 3554 | // explicitly (pt_auto is FALSE). |
| 3555 | |
| 3556 | return OK; |
| 3557 | } |
| 3558 | |
| 3559 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3560 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3561 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3562 | * |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3563 | * 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] | 3564 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3565 | * |
| 3566 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3567 | */ |
| 3568 | |
| 3569 | /* |
| 3570 | * number number constant |
| 3571 | * 0zFFFFFFFF Blob constant |
| 3572 | * "string" string constant |
| 3573 | * 'string' literal string constant |
| 3574 | * &option-name option value |
| 3575 | * @r register contents |
| 3576 | * identifier variable value |
| 3577 | * function() function call |
| 3578 | * $VAR environment variable |
| 3579 | * (expression) nested expression |
| 3580 | * [expr, expr] List |
| 3581 | * {key: val, key: val} Dictionary |
| 3582 | * #{key: val, key: val} Dictionary with literal keys |
| 3583 | * |
| 3584 | * Also handle: |
| 3585 | * ! in front logical NOT |
| 3586 | * - in front unary minus |
| 3587 | * + in front unary plus (ignored) |
| 3588 | * trailing (arg) funcref/partial call |
| 3589 | * trailing [] subscript in String or List |
| 3590 | * trailing .name entry in Dictionary |
| 3591 | * trailing ->name() method call |
| 3592 | */ |
| 3593 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3594 | compile_expr7( |
| 3595 | char_u **arg, |
| 3596 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3597 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3598 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3599 | char_u *start_leader, *end_leader; |
| 3600 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3601 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3602 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3603 | |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3604 | ppconst->pp_is_const = FALSE; |
| 3605 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3606 | /* |
| 3607 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3608 | */ |
| 3609 | start_leader = *arg; |
| 3610 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3611 | *arg = skipwhite(*arg + 1); |
| 3612 | end_leader = *arg; |
| 3613 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3614 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3615 | switch (**arg) |
| 3616 | { |
| 3617 | /* |
| 3618 | * Number constant. |
| 3619 | */ |
| 3620 | case '0': // also for blob starting with 0z |
| 3621 | case '1': |
| 3622 | case '2': |
| 3623 | case '3': |
| 3624 | case '4': |
| 3625 | case '5': |
| 3626 | case '6': |
| 3627 | case '7': |
| 3628 | case '8': |
| 3629 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3630 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3631 | return FAIL; |
Bram Moolenaar | 4301a72 | 2020-08-11 20:51:08 +0200 | [diff] [blame] | 3632 | // Apply "-" and "+" just before the number now, right to |
| 3633 | // left. Matters especially when "->" follows. Stops at |
| 3634 | // '!'. |
| 3635 | if (apply_leader(rettv, TRUE, |
| 3636 | start_leader, &end_leader) == FAIL) |
| 3637 | { |
| 3638 | clear_tv(rettv); |
| 3639 | return FAIL; |
| 3640 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3641 | break; |
| 3642 | |
| 3643 | /* |
| 3644 | * String constant: "string". |
| 3645 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3646 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3647 | return FAIL; |
| 3648 | break; |
| 3649 | |
| 3650 | /* |
| 3651 | * Literal string constant: 'str''ing'. |
| 3652 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3653 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3654 | return FAIL; |
| 3655 | break; |
| 3656 | |
| 3657 | /* |
| 3658 | * Constant Vim variable. |
| 3659 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3660 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3661 | ret = NOTDONE; |
| 3662 | break; |
| 3663 | |
| 3664 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3665 | * "true" constant |
| 3666 | */ |
| 3667 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3668 | && !eval_isnamec((*arg)[4])) |
| 3669 | { |
| 3670 | *arg += 4; |
| 3671 | rettv->v_type = VAR_BOOL; |
| 3672 | rettv->vval.v_number = VVAL_TRUE; |
| 3673 | } |
| 3674 | else |
| 3675 | ret = NOTDONE; |
| 3676 | break; |
| 3677 | |
| 3678 | /* |
| 3679 | * "false" constant |
| 3680 | */ |
| 3681 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3682 | && !eval_isnamec((*arg)[5])) |
| 3683 | { |
| 3684 | *arg += 5; |
| 3685 | rettv->v_type = VAR_BOOL; |
| 3686 | rettv->vval.v_number = VVAL_FALSE; |
| 3687 | } |
| 3688 | else |
| 3689 | ret = NOTDONE; |
| 3690 | break; |
| 3691 | |
| 3692 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3693 | * List: [expr, expr] |
| 3694 | */ |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3695 | case '[': ret = compile_list(arg, cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3696 | break; |
| 3697 | |
| 3698 | /* |
| 3699 | * Dictionary: #{key: val, key: val} |
| 3700 | */ |
| 3701 | case '#': if ((*arg)[1] == '{') |
| 3702 | { |
| 3703 | ++*arg; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3704 | ret = compile_dict(arg, cctx, TRUE, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3705 | } |
| 3706 | else |
| 3707 | ret = NOTDONE; |
| 3708 | break; |
| 3709 | |
| 3710 | /* |
| 3711 | * Lambda: {arg, arg -> expr} |
| 3712 | * Dictionary: {'key': val, 'key': val} |
| 3713 | */ |
| 3714 | case '{': { |
| 3715 | char_u *start = skipwhite(*arg + 1); |
| 3716 | |
| 3717 | // Find out what comes after the arguments. |
| 3718 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3719 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3720 | if (ret != FAIL && *start == '>') |
| 3721 | ret = compile_lambda(arg, cctx); |
| 3722 | else |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3723 | ret = compile_dict(arg, cctx, FALSE, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3724 | } |
| 3725 | break; |
| 3726 | |
| 3727 | /* |
| 3728 | * Option value: &name |
| 3729 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3730 | case '&': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3731 | return FAIL; |
| 3732 | ret = compile_get_option(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3733 | break; |
| 3734 | |
| 3735 | /* |
| 3736 | * Environment variable: $VAR. |
| 3737 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3738 | case '$': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3739 | return FAIL; |
| 3740 | ret = compile_get_env(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3741 | break; |
| 3742 | |
| 3743 | /* |
| 3744 | * Register contents: @r. |
| 3745 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3746 | case '@': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3747 | return FAIL; |
| 3748 | ret = compile_get_register(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3749 | break; |
| 3750 | /* |
| 3751 | * nested expression: (expression). |
| 3752 | */ |
| 3753 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3754 | |
| 3755 | // recursive! |
| 3756 | if (ppconst->pp_used <= PPSIZE - 10) |
| 3757 | { |
| 3758 | ret = compile_expr1(arg, cctx, ppconst); |
| 3759 | } |
| 3760 | else |
| 3761 | { |
| 3762 | // Not enough space in ppconst, flush constants. |
| 3763 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3764 | return FAIL; |
| 3765 | ret = compile_expr0(arg, cctx); |
| 3766 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3767 | *arg = skipwhite(*arg); |
| 3768 | if (**arg == ')') |
| 3769 | ++*arg; |
| 3770 | else if (ret == OK) |
| 3771 | { |
| 3772 | emsg(_(e_missing_close)); |
| 3773 | ret = FAIL; |
| 3774 | } |
| 3775 | break; |
| 3776 | |
| 3777 | default: ret = NOTDONE; |
| 3778 | break; |
| 3779 | } |
| 3780 | if (ret == FAIL) |
| 3781 | return FAIL; |
| 3782 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3783 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3784 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3785 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3786 | clear_tv(rettv); |
| 3787 | else |
| 3788 | // A constant expression can possibly be handled compile time, |
| 3789 | // return the value instead of generating code. |
| 3790 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3791 | } |
| 3792 | else if (ret == NOTDONE) |
| 3793 | { |
| 3794 | char_u *p; |
| 3795 | int r; |
| 3796 | |
| 3797 | if (!eval_isnamec1(**arg)) |
| 3798 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3799 | semsg(_(e_name_expected), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3800 | return FAIL; |
| 3801 | } |
| 3802 | |
| 3803 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3804 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3805 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3806 | { |
| 3807 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 3808 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3809 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3810 | { |
| 3811 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3812 | return FAIL; |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 3813 | r = compile_load(arg, p, cctx, TRUE, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3814 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3815 | if (r == FAIL) |
| 3816 | return FAIL; |
| 3817 | } |
| 3818 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3819 | // Handle following "[]", ".member", etc. |
| 3820 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3821 | if (compile_subscript(arg, cctx, start_leader, &end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3822 | ppconst) == FAIL) |
| 3823 | return FAIL; |
| 3824 | if (ppconst->pp_used > 0) |
| 3825 | { |
| 3826 | // apply the '!', '-' and '+' before the constant |
| 3827 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3828 | if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3829 | return FAIL; |
| 3830 | return OK; |
| 3831 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3832 | if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3833 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3834 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | /* |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3838 | * Give the "white on both sides" error, taking the operator from "p[len]". |
| 3839 | */ |
| 3840 | void |
| 3841 | error_white_both(char_u *op, int len) |
| 3842 | { |
| 3843 | char_u buf[10]; |
| 3844 | |
| 3845 | vim_strncpy(buf, op, len); |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3846 | semsg(_(e_white_space_required_before_and_after_str), buf); |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | /* |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3850 | * <type>expr7: runtime type check / conversion |
| 3851 | */ |
| 3852 | static int |
| 3853 | compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3854 | { |
| 3855 | type_T *want_type = NULL; |
| 3856 | |
| 3857 | // Recognize <type> |
| 3858 | if (**arg == '<' && eval_isnamec1((*arg)[1])) |
| 3859 | { |
| 3860 | int called_emsg_before = called_emsg; |
| 3861 | |
| 3862 | ++*arg; |
| 3863 | want_type = parse_type(arg, cctx->ctx_type_list); |
| 3864 | if (called_emsg != called_emsg_before) |
| 3865 | return FAIL; |
| 3866 | |
| 3867 | if (**arg != '>') |
| 3868 | { |
| 3869 | if (*skipwhite(*arg) == '>') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3870 | semsg(_(e_no_white_space_allowed_before_str), ">"); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3871 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3872 | emsg(_(e_missing_gt)); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3873 | return FAIL; |
| 3874 | } |
| 3875 | ++*arg; |
| 3876 | if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL) |
| 3877 | return FAIL; |
| 3878 | } |
| 3879 | |
| 3880 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
| 3881 | return FAIL; |
| 3882 | |
| 3883 | if (want_type != NULL) |
| 3884 | { |
| 3885 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 3886 | type_T *actual; |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3887 | |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 3888 | generate_ppconst(cctx, ppconst); |
| 3889 | actual = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 3890 | if (check_type(want_type, actual, FALSE, 0) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3891 | { |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 3892 | if (need_type(actual, want_type, -1, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3893 | return FAIL; |
| 3894 | } |
| 3895 | } |
| 3896 | |
| 3897 | return OK; |
| 3898 | } |
| 3899 | |
| 3900 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3901 | * * number multiplication |
| 3902 | * / number division |
| 3903 | * % number modulo |
| 3904 | */ |
| 3905 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3906 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3907 | { |
| 3908 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3909 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3910 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3911 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3912 | // get the first expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3913 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3914 | return FAIL; |
| 3915 | |
| 3916 | /* |
| 3917 | * Repeat computing, until no "*", "/" or "%" is following. |
| 3918 | */ |
| 3919 | for (;;) |
| 3920 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3921 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3922 | if (*op != '*' && *op != '/' && *op != '%') |
| 3923 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3924 | if (next != NULL) |
| 3925 | { |
| 3926 | *arg = next_line_from_context(cctx, TRUE); |
| 3927 | op = skipwhite(*arg); |
| 3928 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3929 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3930 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3931 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3932 | error_white_both(op, 1); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3933 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3934 | } |
| 3935 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3936 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3937 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3938 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3939 | // get the second expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3940 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3941 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3942 | |
| 3943 | if (ppconst->pp_used == ppconst_used + 2 |
| 3944 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3945 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3946 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3947 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3948 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3949 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3950 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3951 | // both are numbers: compute the result |
| 3952 | switch (*op) |
| 3953 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3954 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3955 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3956 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3957 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3958 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3959 | break; |
| 3960 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3961 | tv1->vval.v_number = res; |
| 3962 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3963 | } |
| 3964 | else |
| 3965 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3966 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3967 | generate_two_op(cctx, op); |
| 3968 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3969 | } |
| 3970 | |
| 3971 | return OK; |
| 3972 | } |
| 3973 | |
| 3974 | /* |
| 3975 | * + number addition |
| 3976 | * - number subtraction |
| 3977 | * .. string concatenation |
| 3978 | */ |
| 3979 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3980 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3981 | { |
| 3982 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3983 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3984 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3985 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3986 | |
| 3987 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3988 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3989 | return FAIL; |
| 3990 | |
| 3991 | /* |
| 3992 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3993 | */ |
| 3994 | for (;;) |
| 3995 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3996 | op = may_peek_next_line(cctx, *arg, &next); |
| 3997 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3998 | break; |
| 3999 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4000 | if (next != NULL) |
| 4001 | { |
| 4002 | *arg = next_line_from_context(cctx, TRUE); |
| 4003 | op = skipwhite(*arg); |
| 4004 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4005 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4006 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4007 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4008 | error_white_both(op, oplen); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4009 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4010 | } |
| 4011 | |
| 4012 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4013 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4014 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4015 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4016 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4017 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4018 | return FAIL; |
| 4019 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4020 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4021 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4022 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 4023 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 4024 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 4025 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4026 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4027 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 4028 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4029 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4030 | // concat/subtract/add constant numbers |
| 4031 | if (*op == '+') |
| 4032 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 4033 | else if (*op == '-') |
| 4034 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 4035 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4036 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4037 | // concatenate constant strings |
| 4038 | char_u *s1 = tv1->vval.v_string; |
| 4039 | char_u *s2 = tv2->vval.v_string; |
| 4040 | size_t len1 = STRLEN(s1); |
| 4041 | |
| 4042 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 4043 | if (tv1->vval.v_string == NULL) |
| 4044 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4045 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 4046 | return FAIL; |
| 4047 | } |
| 4048 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 4049 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4050 | vim_free(s1); |
| 4051 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4052 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4053 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4054 | } |
| 4055 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4056 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4057 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 4058 | if (*op == '.') |
| 4059 | { |
| 4060 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 4061 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 4062 | return FAIL; |
| 4063 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 4064 | } |
| 4065 | else |
| 4066 | generate_two_op(cctx, op); |
| 4067 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4068 | } |
| 4069 | |
| 4070 | return OK; |
| 4071 | } |
| 4072 | |
| 4073 | /* |
| 4074 | * expr5a == expr5b |
| 4075 | * expr5a =~ expr5b |
| 4076 | * expr5a != expr5b |
| 4077 | * expr5a !~ expr5b |
| 4078 | * expr5a > expr5b |
| 4079 | * expr5a >= expr5b |
| 4080 | * expr5a < expr5b |
| 4081 | * expr5a <= expr5b |
| 4082 | * expr5a is expr5b |
| 4083 | * expr5a isnot expr5b |
| 4084 | * |
| 4085 | * Produces instructions: |
| 4086 | * EVAL expr5a Push result of "expr5a" |
| 4087 | * EVAL expr5b Push result of "expr5b" |
| 4088 | * COMPARE one of the compare instructions |
| 4089 | */ |
| 4090 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4091 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4092 | { |
| 4093 | exptype_T type = EXPR_UNKNOWN; |
| 4094 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4095 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4096 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4097 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4098 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4099 | |
| 4100 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4101 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4102 | return FAIL; |
| 4103 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4104 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 4105 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4106 | |
| 4107 | /* |
| 4108 | * If there is a comparative operator, use it. |
| 4109 | */ |
| 4110 | if (type != EXPR_UNKNOWN) |
| 4111 | { |
| 4112 | int ic = FALSE; // Default: do not ignore case |
| 4113 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4114 | if (next != NULL) |
| 4115 | { |
| 4116 | *arg = next_line_from_context(cctx, TRUE); |
| 4117 | p = skipwhite(*arg); |
| 4118 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4119 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 4120 | { |
| 4121 | semsg(_(e_invexpr2), *arg); |
| 4122 | return FAIL; |
| 4123 | } |
| 4124 | // extra question mark appended: ignore case |
| 4125 | if (p[len] == '?') |
| 4126 | { |
| 4127 | ic = TRUE; |
| 4128 | ++len; |
| 4129 | } |
| 4130 | // extra '#' appended: match case (ignored) |
| 4131 | else if (p[len] == '#') |
| 4132 | ++len; |
| 4133 | // nothing appended: match case |
| 4134 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4135 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4136 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4137 | error_white_both(p, len); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4138 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | // get the second variable |
| 4142 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4143 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4144 | return FAIL; |
| 4145 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4146 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4147 | return FAIL; |
| 4148 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4149 | if (ppconst->pp_used == ppconst_used + 2) |
| 4150 | { |
| 4151 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 4152 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 4153 | int ret; |
| 4154 | |
| 4155 | // Both sides are a constant, compute the result now. |
| 4156 | // First check for a valid combination of types, this is more |
| 4157 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 4158 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4159 | ret = FAIL; |
| 4160 | else |
| 4161 | { |
| 4162 | ret = typval_compare(tv1, tv2, type, ic); |
| 4163 | tv1->v_type = VAR_BOOL; |
| 4164 | tv1->vval.v_number = tv1->vval.v_number |
| 4165 | ? VVAL_TRUE : VVAL_FALSE; |
| 4166 | clear_tv(tv2); |
| 4167 | --ppconst->pp_used; |
| 4168 | } |
| 4169 | return ret; |
| 4170 | } |
| 4171 | |
| 4172 | generate_ppconst(cctx, ppconst); |
| 4173 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4174 | } |
| 4175 | |
| 4176 | return OK; |
| 4177 | } |
| 4178 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 4179 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 4180 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4181 | /* |
| 4182 | * Compile || or &&. |
| 4183 | */ |
| 4184 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4185 | compile_and_or( |
| 4186 | char_u **arg, |
| 4187 | cctx_T *cctx, |
| 4188 | char *op, |
| 4189 | ppconst_T *ppconst, |
| 4190 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4191 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4192 | char_u *next; |
| 4193 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4194 | int opchar = *op; |
| 4195 | |
| 4196 | if (p[0] == opchar && p[1] == opchar) |
| 4197 | { |
| 4198 | garray_T *instr = &cctx->ctx_instr; |
| 4199 | garray_T end_ga; |
Bram Moolenaar | 4ed124c | 2020-09-09 20:03:46 +0200 | [diff] [blame] | 4200 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4201 | int all_bool_values = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4202 | |
| 4203 | /* |
| 4204 | * Repeat until there is no following "||" or "&&" |
| 4205 | */ |
| 4206 | ga_init2(&end_ga, sizeof(int), 10); |
| 4207 | while (p[0] == opchar && p[1] == opchar) |
| 4208 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4209 | if (next != NULL) |
| 4210 | { |
| 4211 | *arg = next_line_from_context(cctx, TRUE); |
| 4212 | p = skipwhite(*arg); |
| 4213 | } |
| 4214 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4215 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 4216 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4217 | semsg(_(e_white_space_required_before_and_after_str), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4218 | return FAIL; |
| 4219 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4220 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4221 | // TODO: use ppconst if the value is a constant and check |
| 4222 | // evaluating to bool |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4223 | generate_ppconst(cctx, ppconst); |
| 4224 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4225 | if (((type_T **)stack->ga_data)[stack->ga_len - 1] != &t_bool) |
| 4226 | all_bool_values = FALSE; |
| 4227 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4228 | if (ga_grow(&end_ga, 1) == FAIL) |
| 4229 | { |
| 4230 | ga_clear(&end_ga); |
| 4231 | return FAIL; |
| 4232 | } |
| 4233 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 4234 | ++end_ga.ga_len; |
| 4235 | generate_JUMP(cctx, opchar == '|' |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4236 | ? JUMP_IF_COND_TRUE : JUMP_IF_COND_FALSE, 0); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4237 | |
| 4238 | // eval the next expression |
| 4239 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4240 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4241 | return FAIL; |
| 4242 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4243 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4244 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4245 | { |
| 4246 | ga_clear(&end_ga); |
| 4247 | return FAIL; |
| 4248 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4249 | |
| 4250 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4251 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4252 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4253 | |
| 4254 | // Fill in the end label in all jumps. |
| 4255 | while (end_ga.ga_len > 0) |
| 4256 | { |
| 4257 | isn_T *isn; |
| 4258 | |
| 4259 | --end_ga.ga_len; |
| 4260 | isn = ((isn_T *)instr->ga_data) |
| 4261 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4262 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4263 | } |
| 4264 | ga_clear(&end_ga); |
Bram Moolenaar | 4ed124c | 2020-09-09 20:03:46 +0200 | [diff] [blame] | 4265 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4266 | // The resulting type is converted to bool if needed. |
| 4267 | if (!all_bool_values) |
| 4268 | generate_COND2BOOL(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4269 | } |
| 4270 | |
| 4271 | return OK; |
| 4272 | } |
| 4273 | |
| 4274 | /* |
| 4275 | * expr4a && expr4a && expr4a logical AND |
| 4276 | * |
| 4277 | * Produces instructions: |
| 4278 | * EVAL expr4a Push result of "expr4a" |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4279 | * JUMP_IF_COND_FALSE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4280 | * EVAL expr4b Push result of "expr4b" |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4281 | * JUMP_IF_COND_FALSE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4282 | * EVAL expr4c Push result of "expr4c" |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4283 | * COND2BOOL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4284 | * end: |
| 4285 | */ |
| 4286 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4287 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4288 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4289 | int ppconst_used = ppconst->pp_used; |
| 4290 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4291 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4292 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4293 | return FAIL; |
| 4294 | |
| 4295 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4296 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | /* |
| 4300 | * expr3a || expr3b || expr3c logical OR |
| 4301 | * |
| 4302 | * Produces instructions: |
| 4303 | * EVAL expr3a Push result of "expr3a" |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4304 | * JUMP_IF_COND_TRUE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4305 | * EVAL expr3b Push result of "expr3b" |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4306 | * JUMP_IF_COND_TRUE end |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4307 | * EVAL expr3c Push result of "expr3c" |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 4308 | * COND2BOOL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4309 | * end: |
| 4310 | */ |
| 4311 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4312 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4313 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4314 | int ppconst_used = ppconst->pp_used; |
| 4315 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4316 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4317 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4318 | return FAIL; |
| 4319 | |
| 4320 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4321 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4322 | } |
| 4323 | |
| 4324 | /* |
| 4325 | * Toplevel expression: expr2 ? expr1a : expr1b |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4326 | * Produces instructions: |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4327 | * EVAL expr2 Push result of "expr2" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4328 | * JUMP_IF_FALSE alt jump if false |
| 4329 | * EVAL expr1a |
| 4330 | * JUMP_ALWAYS end |
| 4331 | * alt: EVAL expr1b |
| 4332 | * end: |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4333 | * |
| 4334 | * Toplevel expression: expr2 ?? expr1 |
| 4335 | * Produces instructions: |
| 4336 | * EVAL expr2 Push result of "expr2" |
| 4337 | * JUMP_AND_KEEP_IF_TRUE end jump if true |
| 4338 | * EVAL expr1 |
| 4339 | * end: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4340 | */ |
| 4341 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4342 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4343 | { |
| 4344 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4345 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4346 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4347 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 4348 | // Ignore all kinds of errors when not producing code. |
| 4349 | if (cctx->ctx_skip == SKIP_YES) |
| 4350 | { |
| 4351 | skip_expr(arg); |
| 4352 | return OK; |
| 4353 | } |
| 4354 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4355 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4356 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4357 | return FAIL; |
| 4358 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4359 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4360 | if (*p == '?') |
| 4361 | { |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4362 | int op_falsy = p[1] == '?'; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4363 | garray_T *instr = &cctx->ctx_instr; |
| 4364 | garray_T *stack = &cctx->ctx_type_stack; |
| 4365 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4366 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4367 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4368 | type_T *type1 = NULL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4369 | int has_const_expr = FALSE; |
| 4370 | int const_value = FALSE; |
| 4371 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4372 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4373 | if (next != NULL) |
| 4374 | { |
| 4375 | *arg = next_line_from_context(cctx, TRUE); |
| 4376 | p = skipwhite(*arg); |
| 4377 | } |
| 4378 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4379 | 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] | 4380 | { |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4381 | semsg(_(e_white_space_required_before_and_after_str), |
| 4382 | op_falsy ? "??" : "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4383 | return FAIL; |
| 4384 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4385 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4386 | if (ppconst->pp_used == ppconst_used + 1) |
| 4387 | { |
| 4388 | // the condition is a constant, we know whether the ? or the : |
| 4389 | // expression is to be evaluated. |
| 4390 | has_const_expr = TRUE; |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 4391 | if (op_falsy) |
| 4392 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4393 | else |
| 4394 | { |
| 4395 | int error = FALSE; |
| 4396 | |
| 4397 | const_value = tv_get_bool_chk(&ppconst->pp_tv[ppconst_used], |
| 4398 | &error); |
| 4399 | if (error) |
| 4400 | return FAIL; |
| 4401 | } |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4402 | cctx->ctx_skip = save_skip == SKIP_YES || |
| 4403 | (op_falsy ? const_value : !const_value) ? SKIP_YES : SKIP_NOT; |
| 4404 | |
| 4405 | if (op_falsy && cctx->ctx_skip == SKIP_YES) |
| 4406 | // "left ?? right" and "left" is truthy: produce "left" |
| 4407 | generate_ppconst(cctx, ppconst); |
| 4408 | else |
| 4409 | { |
| 4410 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4411 | --ppconst->pp_used; |
| 4412 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4413 | } |
| 4414 | else |
| 4415 | { |
| 4416 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4417 | if (op_falsy) |
| 4418 | end_idx = instr->ga_len; |
| 4419 | generate_JUMP(cctx, op_falsy |
| 4420 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_IF_FALSE, 0); |
| 4421 | if (op_falsy) |
| 4422 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4423 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4424 | |
| 4425 | // evaluate the second expression; any type is accepted |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4426 | *arg = skipwhite(p + 1 + op_falsy); |
| 4427 | if (may_get_next_line(p + 1 + op_falsy, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4428 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4429 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4430 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4431 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4432 | if (!has_const_expr) |
| 4433 | { |
| 4434 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4435 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4436 | if (!op_falsy) |
| 4437 | { |
| 4438 | // remember the type and drop it |
| 4439 | --stack->ga_len; |
| 4440 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4441 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4442 | end_idx = instr->ga_len; |
| 4443 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4444 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4445 | // jump here from JUMP_IF_FALSE |
| 4446 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4447 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4448 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4449 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4450 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4451 | if (!op_falsy) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4452 | { |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4453 | // Check for the ":". |
| 4454 | p = may_peek_next_line(cctx, *arg, &next); |
| 4455 | if (*p != ':') |
| 4456 | { |
| 4457 | emsg(_(e_missing_colon)); |
| 4458 | return FAIL; |
| 4459 | } |
| 4460 | if (next != NULL) |
| 4461 | { |
| 4462 | *arg = next_line_from_context(cctx, TRUE); |
| 4463 | p = skipwhite(*arg); |
| 4464 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4465 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4466 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4467 | { |
| 4468 | semsg(_(e_white_space_required_before_and_after_str), ":"); |
| 4469 | return FAIL; |
| 4470 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4471 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4472 | // evaluate the third expression |
| 4473 | if (has_const_expr) |
| 4474 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4475 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4476 | *arg = skipwhite(p + 1); |
| 4477 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
| 4478 | return FAIL; |
| 4479 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
| 4480 | return FAIL; |
| 4481 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4482 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4483 | if (!has_const_expr) |
| 4484 | { |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4485 | type_T **typep; |
| 4486 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4487 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4488 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4489 | // If the types differ, the result has a more generic type. |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4490 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 1; |
| 4491 | common_type(type1, *typep, typep, cctx->ctx_type_list); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4492 | |
Bram Moolenaar | 92f26c2 | 2020-10-03 20:17:30 +0200 | [diff] [blame] | 4493 | // jump here from JUMP_ALWAYS or JUMP_AND_KEEP_IF_TRUE |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4494 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4495 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4496 | } |
| 4497 | |
| 4498 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4499 | } |
| 4500 | return OK; |
| 4501 | } |
| 4502 | |
| 4503 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4504 | * Toplevel expression. |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4505 | * Sets "is_const" (if not NULL) to indicate the value is a constant. |
| 4506 | * Returns OK or FAIL. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4507 | */ |
| 4508 | static int |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4509 | compile_expr0_ext(char_u **arg, cctx_T *cctx, int *is_const) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4510 | { |
| 4511 | ppconst_T ppconst; |
| 4512 | |
| 4513 | CLEAR_FIELD(ppconst); |
| 4514 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4515 | { |
| 4516 | clear_ppconst(&ppconst); |
| 4517 | return FAIL; |
| 4518 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4519 | if (is_const != NULL) |
| 4520 | *is_const = ppconst.pp_used > 0 || ppconst.pp_is_const; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4521 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4522 | return FAIL; |
| 4523 | return OK; |
| 4524 | } |
| 4525 | |
| 4526 | /* |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4527 | * Toplevel expression. |
| 4528 | */ |
| 4529 | static int |
| 4530 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4531 | { |
| 4532 | return compile_expr0_ext(arg, cctx, NULL); |
| 4533 | } |
| 4534 | |
| 4535 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4536 | * compile "return [expr]" |
| 4537 | */ |
| 4538 | static char_u * |
| 4539 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4540 | { |
| 4541 | char_u *p = arg; |
| 4542 | garray_T *stack = &cctx->ctx_type_stack; |
| 4543 | type_T *stack_type; |
| 4544 | |
| 4545 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4546 | { |
| 4547 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4548 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4549 | return NULL; |
| 4550 | |
| 4551 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4552 | if (set_return_type) |
| 4553 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4554 | else |
| 4555 | { |
| 4556 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4557 | && stack_type->tt_type != VAR_VOID |
| 4558 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4559 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4560 | emsg(_(e_returning_value_in_function_without_return_type)); |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4561 | return NULL; |
| 4562 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4563 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4564 | cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 3bd8de4 | 2020-09-14 16:37:34 +0200 | [diff] [blame] | 4565 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4566 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4567 | } |
| 4568 | else |
| 4569 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4570 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4571 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4572 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4573 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4574 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4575 | emsg(_(e_missing_return_value)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4576 | return NULL; |
| 4577 | } |
| 4578 | |
| 4579 | // No argument, return zero. |
| 4580 | generate_PUSHNR(cctx, 0); |
| 4581 | } |
| 4582 | |
| 4583 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4584 | return NULL; |
| 4585 | |
| 4586 | // "return val | endif" is possible |
| 4587 | return skipwhite(p); |
| 4588 | } |
| 4589 | |
| 4590 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4591 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4592 | * Return a pointer to the line in allocated memory. |
| 4593 | * Return NULL for end-of-file or some error. |
| 4594 | */ |
| 4595 | static char_u * |
| 4596 | exarg_getline( |
| 4597 | int c UNUSED, |
| 4598 | void *cookie, |
| 4599 | int indent UNUSED, |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4600 | getline_opt_T options UNUSED) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4601 | { |
| 4602 | cctx_T *cctx = (cctx_T *)cookie; |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4603 | char_u *p; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4604 | |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4605 | for (;;) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4606 | { |
Bram Moolenaar | 2914a20 | 2020-09-27 18:24:03 +0200 | [diff] [blame] | 4607 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1) |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4608 | return NULL; |
| 4609 | ++cctx->ctx_lnum; |
| 4610 | p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]; |
| 4611 | // Comment lines result in NULL pointers, skip them. |
| 4612 | if (p != NULL) |
| 4613 | return vim_strsave(p); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4614 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | /* |
| 4618 | * Compile a nested :def command. |
| 4619 | */ |
| 4620 | static char_u * |
| 4621 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4622 | { |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4623 | int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4624 | char_u *name_start = eap->arg; |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4625 | char_u *name_end = to_name_end(eap->arg, TRUE); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4626 | char_u *lambda_name; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4627 | ufunc_T *ufunc; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4628 | int r; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4629 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 4630 | if (eap->forceit) |
Bram Moolenaar | 8b848ca | 2020-09-10 22:28:01 +0200 | [diff] [blame] | 4631 | { |
| 4632 | emsg(_(e_cannot_use_bang_with_nested_def)); |
| 4633 | return NULL; |
| 4634 | } |
| 4635 | |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4636 | // Only g:Func() can use a namespace. |
| 4637 | if (name_start[1] == ':' && !is_global) |
| 4638 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4639 | semsg(_(e_namespace_not_supported_str), name_start); |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4640 | return NULL; |
| 4641 | } |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4642 | if (check_defined(name_start, name_end - name_start, cctx) == FAIL) |
| 4643 | return NULL; |
| 4644 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4645 | eap->arg = name_end; |
| 4646 | eap->getline = exarg_getline; |
| 4647 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4648 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4649 | eap->forceit = FALSE; |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4650 | lambda_name = get_lambda_name(); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 4651 | ufunc = define_function(eap, lambda_name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4652 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4653 | if (ufunc == NULL) |
Bram Moolenaar | 7a3330f | 2020-08-27 23:57:57 +0200 | [diff] [blame] | 4654 | return eap->skip ? (char_u *)"" : NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4655 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4656 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 4ee711f | 2020-09-23 18:51:11 +0200 | [diff] [blame] | 4657 | { |
| 4658 | func_ptr_unref(ufunc); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4659 | return NULL; |
Bram Moolenaar | 4ee711f | 2020-09-23 18:51:11 +0200 | [diff] [blame] | 4660 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4661 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4662 | if (is_global) |
| 4663 | { |
| 4664 | char_u *func_name = vim_strnsave(name_start + 2, |
| 4665 | name_end - name_start - 2); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4666 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4667 | if (func_name == NULL) |
| 4668 | r = FAIL; |
| 4669 | else |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4670 | r = generate_NEWFUNC(cctx, lambda_name, func_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4671 | } |
| 4672 | else |
| 4673 | { |
| 4674 | // Define a local variable for the function reference. |
Bram Moolenaar | e8211a3 | 2020-10-09 22:04:29 +0200 | [diff] [blame] | 4675 | lvar_T *lvar = reserve_local(cctx, name_start, name_end - name_start, |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4676 | TRUE, ufunc->uf_func_type); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 4677 | int block_depth = cctx->ctx_ufunc->uf_block_depth; |
Bram Moolenaar | e8211a3 | 2020-10-09 22:04:29 +0200 | [diff] [blame] | 4678 | |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4679 | if (lvar == NULL) |
| 4680 | return NULL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 4681 | if (generate_FUNCREF(cctx, ufunc) == FAIL) |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4682 | return NULL; |
| 4683 | r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 4684 | |
| 4685 | // copy over the block scope IDs |
| 4686 | if (block_depth > 0) |
| 4687 | { |
| 4688 | ufunc->uf_block_ids = ALLOC_MULT(int, block_depth); |
| 4689 | if (ufunc->uf_block_ids != NULL) |
| 4690 | { |
| 4691 | mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids, |
| 4692 | sizeof(int) * block_depth); |
| 4693 | ufunc->uf_block_depth = block_depth; |
| 4694 | } |
| 4695 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4696 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4697 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4698 | // TODO: warning for trailing text? |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4699 | return r == FAIL ? NULL : (char_u *)""; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4700 | } |
| 4701 | |
| 4702 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4703 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4704 | */ |
| 4705 | int |
| 4706 | assignment_len(char_u *p, int *heredoc) |
| 4707 | { |
| 4708 | if (*p == '=') |
| 4709 | { |
| 4710 | if (p[1] == '<' && p[2] == '<') |
| 4711 | { |
| 4712 | *heredoc = TRUE; |
| 4713 | return 3; |
| 4714 | } |
| 4715 | return 1; |
| 4716 | } |
| 4717 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4718 | return 2; |
| 4719 | if (STRNCMP(p, "..=", 3) == 0) |
| 4720 | return 3; |
| 4721 | return 0; |
| 4722 | } |
| 4723 | |
| 4724 | // words that cannot be used as a variable |
| 4725 | static char *reserved[] = { |
| 4726 | "true", |
| 4727 | "false", |
| 4728 | NULL |
| 4729 | }; |
| 4730 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4731 | typedef enum { |
| 4732 | dest_local, |
| 4733 | dest_option, |
| 4734 | dest_env, |
| 4735 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4736 | dest_buffer, |
| 4737 | dest_window, |
| 4738 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4739 | dest_vimvar, |
| 4740 | dest_script, |
| 4741 | dest_reg, |
| 4742 | } assign_dest_T; |
| 4743 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4744 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4745 | * Generate the load instruction for "name". |
| 4746 | */ |
| 4747 | static void |
| 4748 | generate_loadvar( |
| 4749 | cctx_T *cctx, |
| 4750 | assign_dest_T dest, |
| 4751 | char_u *name, |
| 4752 | lvar_T *lvar, |
| 4753 | type_T *type) |
| 4754 | { |
| 4755 | switch (dest) |
| 4756 | { |
| 4757 | case dest_option: |
| 4758 | // TODO: check the option exists |
| 4759 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4760 | break; |
| 4761 | case dest_global: |
| 4762 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4763 | break; |
| 4764 | case dest_buffer: |
| 4765 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4766 | break; |
| 4767 | case dest_window: |
| 4768 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4769 | break; |
| 4770 | case dest_tab: |
| 4771 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4772 | break; |
| 4773 | case dest_script: |
| 4774 | compile_load_scriptvar(cctx, |
| 4775 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4776 | break; |
| 4777 | case dest_env: |
| 4778 | // Include $ in the name here |
| 4779 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4780 | break; |
| 4781 | case dest_reg: |
| 4782 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4783 | break; |
| 4784 | case dest_vimvar: |
| 4785 | generate_LOADV(cctx, name + 2, TRUE); |
| 4786 | break; |
| 4787 | case dest_local: |
| 4788 | if (lvar->lv_from_outer) |
| 4789 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4790 | NULL, type); |
| 4791 | else |
| 4792 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4793 | break; |
| 4794 | } |
| 4795 | } |
| 4796 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4797 | void |
| 4798 | vim9_declare_error(char_u *name) |
| 4799 | { |
| 4800 | char *scope = ""; |
| 4801 | |
| 4802 | switch (*name) |
| 4803 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4804 | case 'g': scope = _("global"); break; |
| 4805 | case 'b': scope = _("buffer"); break; |
| 4806 | case 'w': scope = _("window"); break; |
| 4807 | case 't': scope = _("tab"); break; |
| 4808 | case 'v': scope = "v:"; break; |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4809 | case '$': semsg(_(e_cannot_declare_an_environment_variable), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4810 | return; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4811 | case '&': semsg(_(e_cannot_declare_an_option), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4812 | return; |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4813 | case '@': semsg(_(e_cannot_declare_a_register_str), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4814 | return; |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4815 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4816 | } |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4817 | semsg(_(e_cannot_declare_a_scope_variable), scope, name); |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4818 | } |
| 4819 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4820 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4821 | * Compile declaration and assignment: |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 4822 | * "let name" |
| 4823 | * "var name = expr" |
| 4824 | * "final name = expr" |
| 4825 | * "const name = expr" |
| 4826 | * "name = expr" |
| 4827 | * "arg" points to "name". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4828 | * Return NULL for an error. |
| 4829 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4830 | */ |
| 4831 | static char_u * |
| 4832 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4833 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4834 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4835 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4836 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4837 | char_u *ret = NULL; |
| 4838 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4839 | int var_idx; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4840 | int scriptvar_sid = 0; |
| 4841 | int scriptvar_idx = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4842 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4843 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4844 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4845 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4846 | int oplen = 0; |
| 4847 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4848 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4849 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4850 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4851 | char_u *sp; |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 4852 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_var |
| 4853 | || cmdidx == CMD_final || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4854 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4855 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 4856 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 4857 | if (p == NULL) |
| 4858 | return *arg == '[' ? arg : NULL; |
| 4859 | |
| 4860 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4861 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 4862 | // TODO: should we allow this, and figure out type inference from list |
| 4863 | // members? |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4864 | emsg(_(e_cannot_use_list_for_declaration)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4865 | return NULL; |
| 4866 | } |
| 4867 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4868 | sp = p; |
| 4869 | p = skipwhite(p); |
| 4870 | op = p; |
| 4871 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4872 | |
| 4873 | if (var_count > 0 && oplen == 0) |
| 4874 | // can be something like "[1, 2]->func()" |
| 4875 | return arg; |
| 4876 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4877 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4878 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4879 | error_white_both(op, oplen); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4880 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4881 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4882 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4883 | if (heredoc) |
| 4884 | { |
| 4885 | list_T *l; |
| 4886 | listitem_T *li; |
| 4887 | |
| 4888 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4889 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4890 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 4891 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | c0e2901 | 2020-09-27 14:22:48 +0200 | [diff] [blame] | 4892 | if (l == NULL) |
| 4893 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4894 | |
Bram Moolenaar | 078269b | 2020-09-21 20:35:55 +0200 | [diff] [blame] | 4895 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4896 | { |
Bram Moolenaar | 078269b | 2020-09-21 20:35:55 +0200 | [diff] [blame] | 4897 | // Push each line and the create the list. |
| 4898 | FOR_ALL_LIST_ITEMS(l, li) |
| 4899 | { |
| 4900 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4901 | li->li_tv.vval.v_string = NULL; |
| 4902 | } |
| 4903 | generate_NEWLIST(cctx, l->lv_len); |
| 4904 | type = &t_list_string; |
| 4905 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4906 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4907 | list_free(l); |
| 4908 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4909 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4910 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4911 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4912 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4913 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 4914 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4915 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4916 | p = skipwhite(op + oplen); |
| 4917 | if (compile_expr0(&p, cctx) == FAIL) |
| 4918 | return NULL; |
| 4919 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4920 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4921 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4922 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4923 | type_T *stacktype; |
| 4924 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4925 | stacktype = stack->ga_len == 0 ? &t_void |
| 4926 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4927 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4928 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4929 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4930 | goto theend; |
| 4931 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 4932 | if (need_type(stacktype, &t_list_any, -1, cctx, |
| 4933 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4934 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 4935 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4936 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 4937 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4938 | } |
| 4939 | } |
| 4940 | |
| 4941 | /* |
| 4942 | * Loop over variables in "[var, var] = expr". |
| 4943 | * For "var = expr" and "let var: type" this is done only once. |
| 4944 | */ |
| 4945 | if (var_count > 0) |
| 4946 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 4947 | else |
| 4948 | var_start = arg; |
| 4949 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 4950 | { |
| 4951 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 4952 | size_t varlen; |
| 4953 | int new_local = FALSE; |
| 4954 | int opt_type; |
| 4955 | int opt_flags = 0; |
| 4956 | assign_dest_T dest = dest_local; |
| 4957 | int vimvaridx = -1; |
| 4958 | lvar_T *lvar = NULL; |
| 4959 | lvar_T arg_lvar; |
| 4960 | int has_type = FALSE; |
| 4961 | int has_index = FALSE; |
| 4962 | int instr_count = -1; |
| 4963 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4964 | if (*var_start == '@') |
| 4965 | p = var_start + 2; |
| 4966 | else |
| 4967 | { |
Bram Moolenaar | 2e80095 | 2020-08-23 19:34:48 +0200 | [diff] [blame] | 4968 | // skip over the leading "&", "&l:", "&g:" and "$" |
| 4969 | p = skip_option_env_lead(var_start); |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4970 | p = to_name_end(p, TRUE); |
| 4971 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4972 | |
| 4973 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 4974 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 4975 | --var_end; |
| 4976 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 4977 | --p; |
| 4978 | |
| 4979 | varlen = p - var_start; |
| 4980 | vim_free(name); |
| 4981 | name = vim_strnsave(var_start, varlen); |
| 4982 | if (name == NULL) |
| 4983 | return NULL; |
| 4984 | if (!heredoc) |
| 4985 | type = &t_any; |
| 4986 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4987 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4988 | { |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4989 | int declare_error = FALSE; |
| 4990 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4991 | if (*var_start == '&') |
| 4992 | { |
| 4993 | int cc; |
| 4994 | long numval; |
| 4995 | |
| 4996 | dest = dest_option; |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 4997 | if (cmdidx == CMD_final || cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4998 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4999 | emsg(_(e_const_option)); |
| 5000 | goto theend; |
| 5001 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5002 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5003 | p = var_start; |
| 5004 | p = find_option_end(&p, &opt_flags); |
| 5005 | if (p == NULL) |
| 5006 | { |
| 5007 | // cannot happen? |
| 5008 | emsg(_(e_letunexp)); |
| 5009 | goto theend; |
| 5010 | } |
| 5011 | cc = *p; |
| 5012 | *p = NUL; |
Bram Moolenaar | 2e80095 | 2020-08-23 19:34:48 +0200 | [diff] [blame] | 5013 | opt_type = get_option_value(skip_option_env_lead(var_start), |
| 5014 | &numval, NULL, opt_flags); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5015 | *p = cc; |
| 5016 | if (opt_type == -3) |
| 5017 | { |
| 5018 | semsg(_(e_unknown_option), var_start); |
| 5019 | goto theend; |
| 5020 | } |
| 5021 | if (opt_type == -2 || opt_type == 0) |
| 5022 | type = &t_string; |
| 5023 | else |
| 5024 | type = &t_number; // both number and boolean option |
| 5025 | } |
| 5026 | else if (*var_start == '$') |
| 5027 | { |
| 5028 | dest = dest_env; |
| 5029 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5030 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5031 | } |
| 5032 | else if (*var_start == '@') |
| 5033 | { |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 5034 | if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.') |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5035 | { |
| 5036 | emsg_invreg(var_start[1]); |
| 5037 | goto theend; |
| 5038 | } |
| 5039 | dest = dest_reg; |
| 5040 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5041 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5042 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 5043 | else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5044 | { |
| 5045 | dest = dest_global; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5046 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5047 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 5048 | else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5049 | { |
| 5050 | dest = dest_buffer; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5051 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5052 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 5053 | else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5054 | { |
| 5055 | dest = dest_window; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5056 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5057 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 5058 | else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5059 | { |
| 5060 | dest = dest_tab; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5061 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5062 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 5063 | else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5064 | { |
| 5065 | typval_T *vtv; |
| 5066 | int di_flags; |
| 5067 | |
| 5068 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 5069 | if (vimvaridx < 0) |
| 5070 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5071 | semsg(_(e_variable_not_found_str), var_start); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5072 | goto theend; |
| 5073 | } |
| 5074 | // We use the current value of "sandbox" here, is that OK? |
| 5075 | if (var_check_ro(di_flags, name, FALSE)) |
| 5076 | goto theend; |
| 5077 | dest = dest_vimvar; |
| 5078 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 5079 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5080 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5081 | } |
| 5082 | else |
| 5083 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5084 | int idx; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5085 | |
| 5086 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 5087 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5088 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5089 | semsg(_(e_cannot_use_reserved_name), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5090 | goto theend; |
| 5091 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5092 | |
| 5093 | lvar = lookup_local(var_start, varlen, cctx); |
| 5094 | if (lvar == NULL) |
| 5095 | { |
| 5096 | CLEAR_FIELD(arg_lvar); |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 5097 | if (arg_exists(var_start, varlen, |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5098 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 5099 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5100 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5101 | if (is_decl) |
| 5102 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5103 | semsg(_(e_str_is_used_as_argument), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5104 | goto theend; |
| 5105 | } |
| 5106 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 5107 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5108 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5109 | if (lvar != NULL) |
| 5110 | { |
| 5111 | if (is_decl) |
| 5112 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5113 | semsg(_(e_variable_already_declared), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5114 | goto theend; |
| 5115 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5116 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 5117 | else |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5118 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 5119 | int script_namespace = varlen > 1 |
| 5120 | && STRNCMP(var_start, "s:", 2) == 0; |
| 5121 | int script_var = (script_namespace |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 5122 | ? script_var_exists(var_start + 2, varlen - 2, |
| 5123 | FALSE, cctx) |
| 5124 | : script_var_exists(var_start, varlen, |
| 5125 | TRUE, cctx)) == OK; |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 5126 | imported_T *import = |
| 5127 | find_imported(var_start, varlen, cctx); |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5128 | |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 5129 | if (script_namespace || script_var || import != NULL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5130 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 5131 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 5132 | |
| 5133 | if (is_decl) |
| 5134 | { |
| 5135 | if (script_namespace) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5136 | semsg(_(e_cannot_declare_script_variable_in_function), |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 5137 | name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 5138 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5139 | semsg(_(e_variable_already_declared_in_script), |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 5140 | name); |
| 5141 | goto theend; |
| 5142 | } |
| 5143 | else if (cctx->ctx_ufunc->uf_script_ctx_version |
| 5144 | == SCRIPT_VERSION_VIM9 |
| 5145 | && script_namespace |
| 5146 | && !script_var && import == NULL) |
| 5147 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5148 | semsg(_(e_unknown_variable_str), name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 5149 | goto theend; |
| 5150 | } |
| 5151 | |
| 5152 | dest = dest_script; |
| 5153 | |
| 5154 | // existing script-local variables should have a type |
| 5155 | scriptvar_sid = current_sctx.sc_sid; |
| 5156 | if (import != NULL) |
| 5157 | scriptvar_sid = import->imp_sid; |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 5158 | if (SCRIPT_ID_VALID(scriptvar_sid)) |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 5159 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 5160 | scriptvar_idx = get_script_item_idx(scriptvar_sid, |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 5161 | rawname, TRUE, cctx); |
Bram Moolenaar | 06f9c69 | 2020-09-27 21:27:40 +0200 | [diff] [blame] | 5162 | if (scriptvar_idx >= 0) |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 5163 | { |
| 5164 | scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid); |
| 5165 | svar_T *sv = |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 5166 | ((svar_T *)si->sn_var_vals.ga_data) |
| 5167 | + scriptvar_idx; |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 5168 | type = sv->sv_type; |
| 5169 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 5170 | } |
| 5171 | } |
| 5172 | else if (name[1] == ':' && name[2] != NUL) |
| 5173 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5174 | semsg(_(e_cannot_use_namespaced_variable), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5175 | goto theend; |
| 5176 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 5177 | else if (!is_decl) |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5178 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5179 | semsg(_(e_unknown_variable_str), name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 5180 | goto theend; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5181 | } |
Bram Moolenaar | fa211f3 | 2020-08-07 22:00:26 +0200 | [diff] [blame] | 5182 | else if (check_defined(var_start, varlen, cctx) == FAIL) |
| 5183 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5184 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5185 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 5186 | |
| 5187 | if (declare_error) |
| 5188 | { |
| 5189 | vim9_declare_error(name); |
| 5190 | goto theend; |
| 5191 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5192 | } |
| 5193 | |
| 5194 | // handle "a:name" as a name, not index "name" on "a" |
| 5195 | if (varlen > 1 || var_start[varlen] != ':') |
| 5196 | p = var_end; |
| 5197 | |
| 5198 | if (dest != dest_option) |
| 5199 | { |
| 5200 | if (is_decl && *p == ':') |
| 5201 | { |
| 5202 | // parse optional type: "let var: type = expr" |
| 5203 | if (!VIM_ISWHITE(p[1])) |
| 5204 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 5205 | semsg(_(e_white_space_required_after_str), ":"); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5206 | goto theend; |
| 5207 | } |
| 5208 | p = skipwhite(p + 1); |
| 5209 | type = parse_type(&p, cctx->ctx_type_list); |
| 5210 | has_type = TRUE; |
| 5211 | } |
| 5212 | else if (lvar != NULL) |
| 5213 | type = lvar->lv_type; |
| 5214 | } |
| 5215 | |
| 5216 | if (oplen == 3 && !heredoc && dest != dest_global |
| 5217 | && type->tt_type != VAR_STRING |
| 5218 | && type->tt_type != VAR_ANY) |
| 5219 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5220 | emsg(_(e_can_only_concatenate_to_string)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5221 | goto theend; |
| 5222 | } |
| 5223 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5224 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5225 | { |
| 5226 | if (oplen > 1 && !heredoc) |
| 5227 | { |
| 5228 | // +=, /=, etc. require an existing variable |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5229 | semsg(_(e_cannot_use_operator_on_new_variable), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5230 | goto theend; |
| 5231 | } |
| 5232 | |
| 5233 | // new local variable |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 5234 | if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
| 5235 | && var_wrong_func_name(name, TRUE)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5236 | goto theend; |
| 5237 | lvar = reserve_local(cctx, var_start, varlen, |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 5238 | cmdidx == CMD_final || cmdidx == CMD_const, type); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5239 | if (lvar == NULL) |
| 5240 | goto theend; |
| 5241 | new_local = TRUE; |
| 5242 | } |
| 5243 | |
| 5244 | member_type = type; |
| 5245 | if (var_end > var_start + varlen) |
| 5246 | { |
| 5247 | // Something follows after the variable: "var[idx]". |
| 5248 | if (is_decl) |
| 5249 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5250 | emsg(_(e_cannot_use_index_when_declaring_variable)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5251 | goto theend; |
| 5252 | } |
| 5253 | |
| 5254 | if (var_start[varlen] == '[') |
| 5255 | { |
| 5256 | has_index = TRUE; |
| 5257 | if (type->tt_member == NULL) |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 5258 | member_type = &t_any; |
| 5259 | else |
| 5260 | member_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5261 | } |
| 5262 | else |
| 5263 | { |
| 5264 | semsg("Not supported yet: %s", var_start); |
| 5265 | goto theend; |
| 5266 | } |
| 5267 | } |
| 5268 | else if (lvar == &arg_lvar) |
| 5269 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5270 | semsg(_(e_cannot_assign_to_argument), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5271 | goto theend; |
| 5272 | } |
Bram Moolenaar | dbeecb2 | 2020-09-14 18:15:09 +0200 | [diff] [blame] | 5273 | if (!is_decl && lvar != NULL && lvar->lv_const && !has_index) |
| 5274 | { |
| 5275 | semsg(_(e_cannot_assign_to_constant), name); |
| 5276 | goto theend; |
| 5277 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5278 | |
| 5279 | if (!heredoc) |
| 5280 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5281 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5282 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5283 | if (oplen > 0 && var_count == 0) |
| 5284 | { |
| 5285 | // skip over the "=" and the expression |
| 5286 | p = skipwhite(op + oplen); |
| 5287 | compile_expr0(&p, cctx); |
| 5288 | } |
| 5289 | } |
| 5290 | else if (oplen > 0) |
| 5291 | { |
| 5292 | type_T *stacktype; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 5293 | int is_const = FALSE; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5294 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5295 | // For "var = expr" evaluate the expression. |
| 5296 | if (var_count == 0) |
| 5297 | { |
| 5298 | int r; |
| 5299 | |
| 5300 | // for "+=", "*=", "..=" etc. first load the current value |
| 5301 | if (*op != '=') |
| 5302 | { |
| 5303 | generate_loadvar(cctx, dest, name, lvar, type); |
| 5304 | |
| 5305 | if (has_index) |
| 5306 | { |
| 5307 | // TODO: get member from list or dict |
| 5308 | emsg("Index with operation not supported yet"); |
| 5309 | goto theend; |
| 5310 | } |
| 5311 | } |
| 5312 | |
| 5313 | // Compile the expression. Temporarily hide the new local |
| 5314 | // variable here, it is not available to this expression. |
| 5315 | if (new_local) |
| 5316 | --cctx->ctx_locals.ga_len; |
| 5317 | instr_count = instr->ga_len; |
| 5318 | p = skipwhite(op + oplen); |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 5319 | r = compile_expr0_ext(&p, cctx, &is_const); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5320 | if (new_local) |
| 5321 | ++cctx->ctx_locals.ga_len; |
| 5322 | if (r == FAIL) |
| 5323 | goto theend; |
| 5324 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5325 | else if (semicolon && var_idx == var_count - 1) |
| 5326 | { |
| 5327 | // For "[var; var] = expr" get the rest of the list |
| 5328 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5329 | goto theend; |
| 5330 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5331 | else |
| 5332 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5333 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5334 | // list. |
| 5335 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 5336 | return FAIL; |
| 5337 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5338 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5339 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5340 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5341 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5342 | { |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 5343 | if ((stacktype->tt_type == VAR_FUNC |
| 5344 | || stacktype->tt_type == VAR_PARTIAL) |
| 5345 | && var_wrong_func_name(name, TRUE)) |
| 5346 | goto theend; |
| 5347 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5348 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5349 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5350 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5351 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5352 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5353 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5354 | } |
| 5355 | else |
| 5356 | { |
Bram Moolenaar | 04bdd57 | 2020-09-23 13:25:32 +0200 | [diff] [blame] | 5357 | // An empty list or dict has a &t_unknown member, |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5358 | // for a variable that implies &t_any. |
| 5359 | if (stacktype == &t_list_empty) |
| 5360 | lvar->lv_type = &t_list_any; |
| 5361 | else if (stacktype == &t_dict_empty) |
| 5362 | lvar->lv_type = &t_dict_any; |
Bram Moolenaar | 04bdd57 | 2020-09-23 13:25:32 +0200 | [diff] [blame] | 5363 | else if (stacktype == &t_unknown) |
| 5364 | lvar->lv_type = &t_any; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5365 | else |
| 5366 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5367 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5368 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5369 | else if (*op == '=') |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5370 | { |
| 5371 | type_T *use_type = lvar->lv_type; |
| 5372 | |
Bram Moolenaar | 71abe48 | 2020-09-14 22:28:30 +0200 | [diff] [blame] | 5373 | // without operator check type here, otherwise below |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5374 | if (has_index) |
| 5375 | { |
| 5376 | use_type = use_type->tt_member; |
| 5377 | if (use_type == NULL) |
Bram Moolenaar | 71abe48 | 2020-09-14 22:28:30 +0200 | [diff] [blame] | 5378 | // could be indexing "any" |
| 5379 | use_type = &t_any; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5380 | } |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 5381 | if (need_type(stacktype, use_type, -1, cctx, |
| 5382 | FALSE, is_const) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5383 | goto theend; |
| 5384 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5385 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5386 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 5387 | cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5388 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5389 | } |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 5390 | else if (cmdidx == CMD_final) |
| 5391 | { |
| 5392 | emsg(_(e_final_requires_a_value)); |
| 5393 | goto theend; |
| 5394 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5395 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5396 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 5397 | emsg(_(e_const_requires_a_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5398 | goto theend; |
| 5399 | } |
| 5400 | else if (!has_type || dest == dest_option) |
| 5401 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 5402 | emsg(_(e_type_or_initialization_required)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5403 | goto theend; |
| 5404 | } |
| 5405 | else |
| 5406 | { |
| 5407 | // variables are always initialized |
| 5408 | if (ga_grow(instr, 1) == FAIL) |
| 5409 | goto theend; |
| 5410 | switch (member_type->tt_type) |
| 5411 | { |
| 5412 | case VAR_BOOL: |
| 5413 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5414 | break; |
| 5415 | case VAR_FLOAT: |
| 5416 | #ifdef FEAT_FLOAT |
| 5417 | generate_PUSHF(cctx, 0.0); |
| 5418 | #endif |
| 5419 | break; |
| 5420 | case VAR_STRING: |
| 5421 | generate_PUSHS(cctx, NULL); |
| 5422 | break; |
| 5423 | case VAR_BLOB: |
| 5424 | generate_PUSHBLOB(cctx, NULL); |
| 5425 | break; |
| 5426 | case VAR_FUNC: |
| 5427 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5428 | break; |
| 5429 | case VAR_LIST: |
| 5430 | generate_NEWLIST(cctx, 0); |
| 5431 | break; |
| 5432 | case VAR_DICT: |
| 5433 | generate_NEWDICT(cctx, 0); |
| 5434 | break; |
| 5435 | case VAR_JOB: |
| 5436 | generate_PUSHJOB(cctx, NULL); |
| 5437 | break; |
| 5438 | case VAR_CHANNEL: |
| 5439 | generate_PUSHCHANNEL(cctx, NULL); |
| 5440 | break; |
| 5441 | case VAR_NUMBER: |
| 5442 | case VAR_UNKNOWN: |
| 5443 | case VAR_ANY: |
| 5444 | case VAR_PARTIAL: |
| 5445 | case VAR_VOID: |
| 5446 | case VAR_SPECIAL: // cannot happen |
| 5447 | generate_PUSHNR(cctx, 0); |
| 5448 | break; |
| 5449 | } |
| 5450 | } |
| 5451 | if (var_count == 0) |
| 5452 | end = p; |
| 5453 | } |
| 5454 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5455 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5456 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5457 | break; |
| 5458 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5459 | if (oplen > 0 && *op != '=') |
| 5460 | { |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5461 | type_T *expected; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5462 | type_T *stacktype; |
| 5463 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5464 | if (*op == '.') |
| 5465 | expected = &t_string; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5466 | else |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 5467 | expected = member_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5468 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5469 | if ( |
| 5470 | #ifdef FEAT_FLOAT |
| 5471 | // If variable is float operation with number is OK. |
| 5472 | !(expected == &t_float && stacktype == &t_number) && |
| 5473 | #endif |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 5474 | need_type(stacktype, expected, -1, cctx, |
| 5475 | FALSE, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5476 | goto theend; |
| 5477 | |
| 5478 | if (*op == '.') |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 5479 | { |
| 5480 | if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL) |
| 5481 | goto theend; |
| 5482 | } |
| 5483 | else if (*op == '+') |
| 5484 | { |
| 5485 | if (generate_add_instr(cctx, |
| 5486 | operator_type(member_type, stacktype), |
| 5487 | member_type, stacktype) == FAIL) |
| 5488 | goto theend; |
| 5489 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5490 | else if (generate_two_op(cctx, op) == FAIL) |
| 5491 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5492 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5493 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5494 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5495 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5496 | int r; |
| 5497 | |
| 5498 | // Compile the "idx" in "var[idx]". |
| 5499 | if (new_local) |
| 5500 | --cctx->ctx_locals.ga_len; |
| 5501 | p = skipwhite(var_start + varlen + 1); |
| 5502 | r = compile_expr0(&p, cctx); |
| 5503 | if (new_local) |
| 5504 | ++cctx->ctx_locals.ga_len; |
| 5505 | if (r == FAIL) |
| 5506 | goto theend; |
| 5507 | if (*skipwhite(p) != ']') |
| 5508 | { |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 5509 | // this should not happen |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5510 | emsg(_(e_missbrac)); |
| 5511 | goto theend; |
| 5512 | } |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 5513 | if (type == &t_any) |
| 5514 | { |
| 5515 | type_T *idx_type = ((type_T **)stack->ga_data)[ |
| 5516 | stack->ga_len - 1]; |
| 5517 | // Index on variable of unknown type: guess the type from the |
| 5518 | // index type: number is dict, otherwise dict. |
| 5519 | // TODO: should do the assignment at runtime |
| 5520 | if (idx_type->tt_type == VAR_NUMBER) |
| 5521 | type = &t_list_any; |
| 5522 | else |
| 5523 | type = &t_dict_any; |
| 5524 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5525 | if (type->tt_type == VAR_DICT |
| 5526 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5527 | goto theend; |
| 5528 | if (type->tt_type == VAR_LIST |
| 5529 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5530 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5531 | { |
| 5532 | emsg(_(e_number_exp)); |
| 5533 | goto theend; |
| 5534 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5535 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5536 | // Load the dict or list. On the stack we then have: |
| 5537 | // - value |
| 5538 | // - index |
| 5539 | // - variable |
| 5540 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5541 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5542 | if (type->tt_type == VAR_LIST) |
| 5543 | { |
| 5544 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5545 | return FAIL; |
| 5546 | } |
| 5547 | else if (type->tt_type == VAR_DICT) |
| 5548 | { |
| 5549 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5550 | return FAIL; |
| 5551 | } |
| 5552 | else |
| 5553 | { |
| 5554 | emsg(_(e_listreq)); |
| 5555 | goto theend; |
| 5556 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5557 | } |
| 5558 | else |
| 5559 | { |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 5560 | if (is_decl && cmdidx == CMD_const |
| 5561 | && (dest == dest_script || dest == dest_local)) |
| 5562 | // ":const var": lock the value, but not referenced variables |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 5563 | generate_LOCKCONST(cctx); |
| 5564 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5565 | switch (dest) |
| 5566 | { |
| 5567 | case dest_option: |
Bram Moolenaar | 2e80095 | 2020-08-23 19:34:48 +0200 | [diff] [blame] | 5568 | generate_STOREOPT(cctx, skip_option_env_lead(name), |
| 5569 | opt_flags); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5570 | break; |
| 5571 | case dest_global: |
| 5572 | // include g: with the name, easier to execute that way |
| 5573 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5574 | break; |
| 5575 | case dest_buffer: |
| 5576 | // include b: with the name, easier to execute that way |
| 5577 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5578 | break; |
| 5579 | case dest_window: |
| 5580 | // include w: with the name, easier to execute that way |
| 5581 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5582 | break; |
| 5583 | case dest_tab: |
| 5584 | // include t: with the name, easier to execute that way |
| 5585 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5586 | break; |
| 5587 | case dest_env: |
| 5588 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5589 | break; |
| 5590 | case dest_reg: |
| 5591 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5592 | break; |
| 5593 | case dest_vimvar: |
| 5594 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5595 | break; |
| 5596 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5597 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5598 | if (scriptvar_idx < 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5599 | { |
| 5600 | char_u *name_s = name; |
| 5601 | |
| 5602 | // Include s: in the name for store_var() |
| 5603 | if (name[1] != ':') |
| 5604 | { |
| 5605 | int len = (int)STRLEN(name) + 3; |
| 5606 | |
| 5607 | name_s = alloc(len); |
| 5608 | if (name_s == NULL) |
| 5609 | name_s = name; |
| 5610 | else |
| 5611 | vim_snprintf((char *)name_s, len, |
| 5612 | "s:%s", name); |
| 5613 | } |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5614 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, |
| 5615 | scriptvar_sid, type); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5616 | if (name_s != name) |
| 5617 | vim_free(name_s); |
| 5618 | } |
| 5619 | else |
| 5620 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5621 | scriptvar_sid, scriptvar_idx, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5622 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5623 | break; |
| 5624 | case dest_local: |
| 5625 | if (lvar != NULL) |
| 5626 | { |
| 5627 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5628 | + instr->ga_len - 1; |
| 5629 | |
| 5630 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5631 | // ISN_STORE into ISN_STORENR |
| 5632 | if (!lvar->lv_from_outer |
| 5633 | && instr->ga_len == instr_count + 1 |
| 5634 | && isn->isn_type == ISN_PUSHNR) |
| 5635 | { |
| 5636 | varnumber_T val = isn->isn_arg.number; |
| 5637 | |
| 5638 | isn->isn_type = ISN_STORENR; |
| 5639 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5640 | isn->isn_arg.storenr.stnr_val = val; |
| 5641 | if (stack->ga_len > 0) |
| 5642 | --stack->ga_len; |
| 5643 | } |
| 5644 | else if (lvar->lv_from_outer) |
| 5645 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5646 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5647 | else |
| 5648 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5649 | } |
| 5650 | break; |
| 5651 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5652 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5653 | |
| 5654 | if (var_idx + 1 < var_count) |
| 5655 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5656 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5657 | |
| 5658 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5659 | if (var_count > 0 && !semicolon) |
| 5660 | { |
| 5661 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5662 | goto theend; |
| 5663 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5664 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5665 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5666 | |
| 5667 | theend: |
| 5668 | vim_free(name); |
| 5669 | return ret; |
| 5670 | } |
| 5671 | |
| 5672 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5673 | * Check if "name" can be "unlet". |
| 5674 | */ |
| 5675 | int |
| 5676 | check_vim9_unlet(char_u *name) |
| 5677 | { |
| 5678 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5679 | { |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 5680 | // "unlet s:var" is allowed in legacy script. |
| 5681 | if (*name == 's' && !script_is_vim9()) |
| 5682 | return OK; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5683 | semsg(_(e_cannot_unlet_str), name); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5684 | return FAIL; |
| 5685 | } |
| 5686 | return OK; |
| 5687 | } |
| 5688 | |
| 5689 | /* |
| 5690 | * Callback passed to ex_unletlock(). |
| 5691 | */ |
| 5692 | static int |
| 5693 | compile_unlet( |
| 5694 | lval_T *lvp, |
| 5695 | char_u *name_end, |
| 5696 | exarg_T *eap, |
| 5697 | int deep UNUSED, |
| 5698 | void *coookie) |
| 5699 | { |
| 5700 | cctx_T *cctx = coookie; |
| 5701 | |
| 5702 | if (lvp->ll_tv == NULL) |
| 5703 | { |
| 5704 | char_u *p = lvp->ll_name; |
| 5705 | int cc = *name_end; |
| 5706 | int ret = OK; |
| 5707 | |
| 5708 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5709 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5710 | if (*p == '$') |
| 5711 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5712 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5713 | ret = FAIL; |
| 5714 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5715 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5716 | |
| 5717 | *name_end = cc; |
| 5718 | return ret; |
| 5719 | } |
| 5720 | |
| 5721 | // TODO: unlet {list}[idx] |
| 5722 | // TODO: unlet {dict}[key] |
| 5723 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5724 | return FAIL; |
| 5725 | } |
| 5726 | |
| 5727 | /* |
| 5728 | * compile "unlet var", "lock var" and "unlock var" |
| 5729 | * "arg" points to "var". |
| 5730 | */ |
| 5731 | static char_u * |
| 5732 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5733 | { |
| 5734 | char_u *p = arg; |
| 5735 | |
| 5736 | if (eap->cmdidx != CMD_unlet) |
| 5737 | { |
| 5738 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5739 | return NULL; |
| 5740 | } |
| 5741 | |
| 5742 | if (*p == '!') |
| 5743 | { |
| 5744 | p = skipwhite(p + 1); |
| 5745 | eap->forceit = TRUE; |
| 5746 | } |
| 5747 | |
| 5748 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5749 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5750 | } |
| 5751 | |
| 5752 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5753 | * Compile an :import command. |
| 5754 | */ |
| 5755 | static char_u * |
| 5756 | compile_import(char_u *arg, cctx_T *cctx) |
| 5757 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5758 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5759 | } |
| 5760 | |
| 5761 | /* |
| 5762 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5763 | */ |
| 5764 | static int |
| 5765 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5766 | { |
| 5767 | garray_T *instr = &cctx->ctx_instr; |
| 5768 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5769 | |
| 5770 | if (endlabel == NULL) |
| 5771 | return FAIL; |
| 5772 | endlabel->el_next = *el; |
| 5773 | *el = endlabel; |
| 5774 | endlabel->el_end_label = instr->ga_len; |
| 5775 | |
| 5776 | generate_JUMP(cctx, when, 0); |
| 5777 | return OK; |
| 5778 | } |
| 5779 | |
| 5780 | static void |
| 5781 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5782 | { |
| 5783 | garray_T *instr = &cctx->ctx_instr; |
| 5784 | |
| 5785 | while (*el != NULL) |
| 5786 | { |
| 5787 | endlabel_T *cur = (*el); |
| 5788 | isn_T *isn; |
| 5789 | |
| 5790 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5791 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5792 | *el = cur->el_next; |
| 5793 | vim_free(cur); |
| 5794 | } |
| 5795 | } |
| 5796 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5797 | static void |
| 5798 | compile_free_jump_to_end(endlabel_T **el) |
| 5799 | { |
| 5800 | while (*el != NULL) |
| 5801 | { |
| 5802 | endlabel_T *cur = (*el); |
| 5803 | |
| 5804 | *el = cur->el_next; |
| 5805 | vim_free(cur); |
| 5806 | } |
| 5807 | } |
| 5808 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5809 | /* |
| 5810 | * Create a new scope and set up the generic items. |
| 5811 | */ |
| 5812 | static scope_T * |
| 5813 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5814 | { |
| 5815 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5816 | |
| 5817 | if (scope == NULL) |
| 5818 | return NULL; |
| 5819 | scope->se_outer = cctx->ctx_scope; |
| 5820 | cctx->ctx_scope = scope; |
| 5821 | scope->se_type = type; |
| 5822 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5823 | return scope; |
| 5824 | } |
| 5825 | |
| 5826 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5827 | * Free the current scope and go back to the outer scope. |
| 5828 | */ |
| 5829 | static void |
| 5830 | drop_scope(cctx_T *cctx) |
| 5831 | { |
| 5832 | scope_T *scope = cctx->ctx_scope; |
| 5833 | |
| 5834 | if (scope == NULL) |
| 5835 | { |
| 5836 | iemsg("calling drop_scope() without a scope"); |
| 5837 | return; |
| 5838 | } |
| 5839 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5840 | switch (scope->se_type) |
| 5841 | { |
| 5842 | case IF_SCOPE: |
| 5843 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5844 | case FOR_SCOPE: |
| 5845 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5846 | case WHILE_SCOPE: |
| 5847 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5848 | case TRY_SCOPE: |
| 5849 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5850 | case NO_SCOPE: |
| 5851 | case BLOCK_SCOPE: |
| 5852 | break; |
| 5853 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5854 | vim_free(scope); |
| 5855 | } |
| 5856 | |
| 5857 | /* |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 5858 | * Check that the top of the type stack has a type that can be used as a |
| 5859 | * condition. Give an error and return FAIL if not. |
| 5860 | */ |
| 5861 | static int |
| 5862 | bool_on_stack(cctx_T *cctx) |
| 5863 | { |
| 5864 | garray_T *stack = &cctx->ctx_type_stack; |
| 5865 | type_T *type; |
| 5866 | |
| 5867 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 5868 | if (type != &t_bool && type != &t_number && type != &t_any |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 5869 | && need_type(type, &t_bool, -1, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 5870 | return FAIL; |
| 5871 | return OK; |
| 5872 | } |
| 5873 | |
| 5874 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5875 | * compile "if expr" |
| 5876 | * |
| 5877 | * "if expr" Produces instructions: |
| 5878 | * EVAL expr Push result of "expr" |
| 5879 | * JUMP_IF_FALSE end |
| 5880 | * ... body ... |
| 5881 | * end: |
| 5882 | * |
| 5883 | * "if expr | else" Produces instructions: |
| 5884 | * EVAL expr Push result of "expr" |
| 5885 | * JUMP_IF_FALSE else |
| 5886 | * ... body ... |
| 5887 | * JUMP_ALWAYS end |
| 5888 | * else: |
| 5889 | * ... body ... |
| 5890 | * end: |
| 5891 | * |
| 5892 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5893 | * EVAL expr Push result of "expr" |
| 5894 | * JUMP_IF_FALSE elseif |
| 5895 | * ... body ... |
| 5896 | * JUMP_ALWAYS end |
| 5897 | * elseif: |
| 5898 | * EVAL expr Push result of "expr" |
| 5899 | * JUMP_IF_FALSE else |
| 5900 | * ... body ... |
| 5901 | * JUMP_ALWAYS end |
| 5902 | * else: |
| 5903 | * ... body ... |
| 5904 | * end: |
| 5905 | */ |
| 5906 | static char_u * |
| 5907 | compile_if(char_u *arg, cctx_T *cctx) |
| 5908 | { |
| 5909 | char_u *p = arg; |
| 5910 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5911 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5912 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5913 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5914 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5915 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5916 | CLEAR_FIELD(ppconst); |
| 5917 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5918 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5919 | clear_ppconst(&ppconst); |
| 5920 | return NULL; |
| 5921 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5922 | if (cctx->ctx_skip == SKIP_YES) |
| 5923 | clear_ppconst(&ppconst); |
| 5924 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5925 | { |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 5926 | int error = FALSE; |
| 5927 | int v; |
| 5928 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5929 | // The expression results in a constant. |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 5930 | v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); |
Bram Moolenaar | dda749c | 2020-10-04 17:24:29 +0200 | [diff] [blame] | 5931 | clear_ppconst(&ppconst); |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 5932 | if (error) |
| 5933 | return NULL; |
| 5934 | cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5935 | } |
| 5936 | else |
| 5937 | { |
| 5938 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5939 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5940 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5941 | return NULL; |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 5942 | if (bool_on_stack(cctx) == FAIL) |
| 5943 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5944 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5945 | |
| 5946 | scope = new_scope(cctx, IF_SCOPE); |
| 5947 | if (scope == NULL) |
| 5948 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5949 | scope->se_skip_save = skip_save; |
| 5950 | // "is_had_return" will be reset if any block does not end in :return |
| 5951 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5952 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5953 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5954 | { |
| 5955 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5956 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5957 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5958 | } |
| 5959 | else |
| 5960 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5961 | |
| 5962 | return p; |
| 5963 | } |
| 5964 | |
| 5965 | static char_u * |
| 5966 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 5967 | { |
| 5968 | char_u *p = arg; |
| 5969 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5970 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5971 | isn_T *isn; |
| 5972 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5973 | ppconst_T ppconst; |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 5974 | skip_T save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5975 | |
| 5976 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5977 | { |
| 5978 | emsg(_(e_elseif_without_if)); |
| 5979 | return NULL; |
| 5980 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5981 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5982 | if (!cctx->ctx_had_return) |
| 5983 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5984 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5985 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5986 | { |
| 5987 | 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] | 5988 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5989 | return NULL; |
| 5990 | // previous "if" or "elseif" jumps here |
| 5991 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5992 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5993 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5994 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5995 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5996 | CLEAR_FIELD(ppconst); |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 5997 | if (cctx->ctx_skip == SKIP_YES) |
| 5998 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5999 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6000 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6001 | clear_ppconst(&ppconst); |
| 6002 | return NULL; |
| 6003 | } |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 6004 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6005 | if (scope->se_skip_save == SKIP_YES) |
| 6006 | clear_ppconst(&ppconst); |
| 6007 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6008 | { |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6009 | int error = FALSE; |
| 6010 | int v; |
| 6011 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6012 | // The expression results in a constant. |
| 6013 | // TODO: how about nesting? |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6014 | v = tv_get_bool_chk(&ppconst.pp_tv[0], &error); |
| 6015 | if (error) |
| 6016 | return NULL; |
| 6017 | cctx->ctx_skip = v ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6018 | clear_ppconst(&ppconst); |
| 6019 | scope->se_u.se_if.is_if_label = -1; |
| 6020 | } |
| 6021 | else |
| 6022 | { |
| 6023 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6024 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 6025 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6026 | return NULL; |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6027 | if (bool_on_stack(cctx) == FAIL) |
| 6028 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6029 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6030 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 6031 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 6032 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 6033 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6034 | |
| 6035 | return p; |
| 6036 | } |
| 6037 | |
| 6038 | static char_u * |
| 6039 | compile_else(char_u *arg, cctx_T *cctx) |
| 6040 | { |
| 6041 | char_u *p = arg; |
| 6042 | garray_T *instr = &cctx->ctx_instr; |
| 6043 | isn_T *isn; |
| 6044 | scope_T *scope = cctx->ctx_scope; |
| 6045 | |
| 6046 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6047 | { |
| 6048 | emsg(_(e_else_without_if)); |
| 6049 | return NULL; |
| 6050 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6051 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6052 | if (!cctx->ctx_had_return) |
| 6053 | scope->se_u.se_if.is_had_return = FALSE; |
| 6054 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6055 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6056 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6057 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6058 | // 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] | 6059 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6060 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6061 | if (!cctx->ctx_had_return |
| 6062 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 6063 | JUMP_ALWAYS, cctx) == FAIL) |
| 6064 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6065 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6066 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6067 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6068 | { |
| 6069 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6070 | { |
| 6071 | // previous "if" or "elseif" jumps here |
| 6072 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6073 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6074 | scope->se_u.se_if.is_if_label = -1; |
| 6075 | } |
| 6076 | } |
| 6077 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 6078 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6079 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 6080 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6081 | |
| 6082 | return p; |
| 6083 | } |
| 6084 | |
| 6085 | static char_u * |
| 6086 | compile_endif(char_u *arg, cctx_T *cctx) |
| 6087 | { |
| 6088 | scope_T *scope = cctx->ctx_scope; |
| 6089 | ifscope_T *ifscope; |
| 6090 | garray_T *instr = &cctx->ctx_instr; |
| 6091 | isn_T *isn; |
| 6092 | |
| 6093 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 6094 | { |
| 6095 | emsg(_(e_endif_without_if)); |
| 6096 | return NULL; |
| 6097 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6098 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6099 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6100 | if (!cctx->ctx_had_return) |
| 6101 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6102 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6103 | if (scope->se_u.se_if.is_if_label >= 0) |
| 6104 | { |
| 6105 | // previous "if" or "elseif" jumps here |
| 6106 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 6107 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6108 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6109 | // Fill in the "end" label in jumps at the end of the blocks. |
| 6110 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6111 | cctx->ctx_skip = scope->se_skip_save; |
| 6112 | |
| 6113 | // If all the blocks end in :return and there is an :else then the |
| 6114 | // had_return flag is set. |
| 6115 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6116 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6117 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6118 | return arg; |
| 6119 | } |
| 6120 | |
| 6121 | /* |
| 6122 | * compile "for var in expr" |
| 6123 | * |
| 6124 | * Produces instructions: |
| 6125 | * PUSHNR -1 |
| 6126 | * STORE loop-idx Set index to -1 |
| 6127 | * EVAL expr Push result of "expr" |
| 6128 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 6129 | * - if beyond end, jump to "end" |
| 6130 | * - otherwise get item from list and push it |
| 6131 | * STORE var Store item in "var" |
| 6132 | * ... body ... |
| 6133 | * JUMP top Jump back to repeat |
| 6134 | * end: DROP Drop the result of "expr" |
| 6135 | * |
| 6136 | */ |
| 6137 | static char_u * |
| 6138 | compile_for(char_u *arg, cctx_T *cctx) |
| 6139 | { |
| 6140 | char_u *p; |
| 6141 | size_t varlen; |
| 6142 | garray_T *instr = &cctx->ctx_instr; |
| 6143 | garray_T *stack = &cctx->ctx_type_stack; |
| 6144 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6145 | lvar_T *loop_lvar; // loop iteration variable |
| 6146 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6147 | type_T *vartype; |
| 6148 | |
| 6149 | // TODO: list of variables: "for [key, value] in dict" |
| 6150 | // parse "var" |
| 6151 | for (p = arg; eval_isnamec1(*p); ++p) |
| 6152 | ; |
| 6153 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6154 | var_lvar = lookup_local(arg, varlen, cctx); |
| 6155 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6156 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6157 | semsg(_(e_variable_already_declared), arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6158 | return NULL; |
| 6159 | } |
| 6160 | |
| 6161 | // consume "in" |
| 6162 | p = skipwhite(p); |
| 6163 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 6164 | { |
| 6165 | emsg(_(e_missing_in)); |
| 6166 | return NULL; |
| 6167 | } |
| 6168 | p = skipwhite(p + 2); |
| 6169 | |
| 6170 | |
| 6171 | scope = new_scope(cctx, FOR_SCOPE); |
| 6172 | if (scope == NULL) |
| 6173 | return NULL; |
| 6174 | |
| 6175 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6176 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 6177 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6178 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6179 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6180 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6181 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6182 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6183 | |
| 6184 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6185 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 6186 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6187 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6188 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6189 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6190 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6191 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6192 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6193 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6194 | |
| 6195 | // compile "expr", it remains on the stack until "endfor" |
| 6196 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6197 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6198 | { |
| 6199 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6200 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6201 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6202 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6203 | // Now that we know the type of "var", check that it is a list, now or at |
| 6204 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6205 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 334a8b4 | 2020-10-19 16:07:42 +0200 | [diff] [blame] | 6206 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6207 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 6208 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6209 | return NULL; |
| 6210 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 6211 | if (vartype->tt_type == VAR_LIST && vartype->tt_member->tt_type != VAR_ANY) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6212 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6213 | |
| 6214 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6215 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6216 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6217 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 6218 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6219 | |
| 6220 | return arg; |
| 6221 | } |
| 6222 | |
| 6223 | /* |
| 6224 | * compile "endfor" |
| 6225 | */ |
| 6226 | static char_u * |
| 6227 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 6228 | { |
| 6229 | garray_T *instr = &cctx->ctx_instr; |
| 6230 | scope_T *scope = cctx->ctx_scope; |
| 6231 | forscope_T *forscope; |
| 6232 | isn_T *isn; |
| 6233 | |
| 6234 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 6235 | { |
| 6236 | emsg(_(e_for)); |
| 6237 | return NULL; |
| 6238 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6239 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6240 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6241 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6242 | |
| 6243 | // At end of ":for" scope jump back to the FOR instruction. |
| 6244 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 6245 | |
| 6246 | // Fill in the "end" label in the FOR statement so it can jump here |
| 6247 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 6248 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 6249 | |
| 6250 | // Fill in the "end" label any BREAK statements |
| 6251 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 6252 | |
| 6253 | // Below the ":for" scope drop the "expr" list from the stack. |
| 6254 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 6255 | return NULL; |
| 6256 | |
| 6257 | vim_free(scope); |
| 6258 | |
| 6259 | return arg; |
| 6260 | } |
| 6261 | |
| 6262 | /* |
| 6263 | * compile "while expr" |
| 6264 | * |
| 6265 | * Produces instructions: |
| 6266 | * top: EVAL expr Push result of "expr" |
| 6267 | * JUMP_IF_FALSE end jump if false |
| 6268 | * ... body ... |
| 6269 | * JUMP top Jump back to repeat |
| 6270 | * end: |
| 6271 | * |
| 6272 | */ |
| 6273 | static char_u * |
| 6274 | compile_while(char_u *arg, cctx_T *cctx) |
| 6275 | { |
| 6276 | char_u *p = arg; |
| 6277 | garray_T *instr = &cctx->ctx_instr; |
| 6278 | scope_T *scope; |
| 6279 | |
| 6280 | scope = new_scope(cctx, WHILE_SCOPE); |
| 6281 | if (scope == NULL) |
| 6282 | return NULL; |
| 6283 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6284 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6285 | |
| 6286 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6287 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6288 | return NULL; |
| 6289 | |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 6290 | if (bool_on_stack(cctx) == FAIL) |
| 6291 | return FAIL; |
| 6292 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6293 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6294 | 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] | 6295 | JUMP_IF_FALSE, cctx) == FAIL) |
| 6296 | return FAIL; |
| 6297 | |
| 6298 | return p; |
| 6299 | } |
| 6300 | |
| 6301 | /* |
| 6302 | * compile "endwhile" |
| 6303 | */ |
| 6304 | static char_u * |
| 6305 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 6306 | { |
| 6307 | scope_T *scope = cctx->ctx_scope; |
| 6308 | |
| 6309 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 6310 | { |
| 6311 | emsg(_(e_while)); |
| 6312 | return NULL; |
| 6313 | } |
| 6314 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6315 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6316 | |
| 6317 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6318 | 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] | 6319 | |
| 6320 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 6321 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6322 | 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] | 6323 | |
| 6324 | vim_free(scope); |
| 6325 | |
| 6326 | return arg; |
| 6327 | } |
| 6328 | |
| 6329 | /* |
| 6330 | * compile "continue" |
| 6331 | */ |
| 6332 | static char_u * |
| 6333 | compile_continue(char_u *arg, cctx_T *cctx) |
| 6334 | { |
| 6335 | scope_T *scope = cctx->ctx_scope; |
| 6336 | |
| 6337 | for (;;) |
| 6338 | { |
| 6339 | if (scope == NULL) |
| 6340 | { |
| 6341 | emsg(_(e_continue)); |
| 6342 | return NULL; |
| 6343 | } |
| 6344 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6345 | break; |
| 6346 | scope = scope->se_outer; |
| 6347 | } |
| 6348 | |
| 6349 | // Jump back to the FOR or WHILE instruction. |
| 6350 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6351 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 6352 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6353 | return arg; |
| 6354 | } |
| 6355 | |
| 6356 | /* |
| 6357 | * compile "break" |
| 6358 | */ |
| 6359 | static char_u * |
| 6360 | compile_break(char_u *arg, cctx_T *cctx) |
| 6361 | { |
| 6362 | scope_T *scope = cctx->ctx_scope; |
| 6363 | endlabel_T **el; |
| 6364 | |
| 6365 | for (;;) |
| 6366 | { |
| 6367 | if (scope == NULL) |
| 6368 | { |
| 6369 | emsg(_(e_break)); |
| 6370 | return NULL; |
| 6371 | } |
| 6372 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6373 | break; |
| 6374 | scope = scope->se_outer; |
| 6375 | } |
| 6376 | |
| 6377 | // Jump to the end of the FOR or WHILE loop. |
| 6378 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6379 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6380 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6381 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6382 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 6383 | return FAIL; |
| 6384 | |
| 6385 | return arg; |
| 6386 | } |
| 6387 | |
| 6388 | /* |
| 6389 | * compile "{" start of block |
| 6390 | */ |
| 6391 | static char_u * |
| 6392 | compile_block(char_u *arg, cctx_T *cctx) |
| 6393 | { |
| 6394 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6395 | return NULL; |
| 6396 | return skipwhite(arg + 1); |
| 6397 | } |
| 6398 | |
| 6399 | /* |
| 6400 | * compile end of block: drop one scope |
| 6401 | */ |
| 6402 | static void |
| 6403 | compile_endblock(cctx_T *cctx) |
| 6404 | { |
| 6405 | scope_T *scope = cctx->ctx_scope; |
| 6406 | |
| 6407 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6408 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6409 | vim_free(scope); |
| 6410 | } |
| 6411 | |
| 6412 | /* |
| 6413 | * compile "try" |
| 6414 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6415 | * finally. |
| 6416 | * Creates another scope for the "try" block itself. |
| 6417 | * TRY instruction sets up exception handling at runtime. |
| 6418 | * |
| 6419 | * "try" |
| 6420 | * TRY -> catch1, -> finally push trystack entry |
| 6421 | * ... try block |
| 6422 | * "throw {exception}" |
| 6423 | * EVAL {exception} |
| 6424 | * THROW create exception |
| 6425 | * ... try block |
| 6426 | * " catch {expr}" |
| 6427 | * JUMP -> finally |
| 6428 | * catch1: PUSH exeception |
| 6429 | * EVAL {expr} |
| 6430 | * MATCH |
| 6431 | * JUMP nomatch -> catch2 |
| 6432 | * CATCH remove exception |
| 6433 | * ... catch block |
| 6434 | * " catch" |
| 6435 | * JUMP -> finally |
| 6436 | * catch2: CATCH remove exception |
| 6437 | * ... catch block |
| 6438 | * " finally" |
| 6439 | * finally: |
| 6440 | * ... finally block |
| 6441 | * " endtry" |
| 6442 | * ENDTRY pop trystack entry, may rethrow |
| 6443 | */ |
| 6444 | static char_u * |
| 6445 | compile_try(char_u *arg, cctx_T *cctx) |
| 6446 | { |
| 6447 | garray_T *instr = &cctx->ctx_instr; |
| 6448 | scope_T *try_scope; |
| 6449 | scope_T *scope; |
| 6450 | |
| 6451 | // scope that holds the jumps that go to catch/finally/endtry |
| 6452 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6453 | if (try_scope == NULL) |
| 6454 | return NULL; |
| 6455 | |
| 6456 | // "catch" is set when the first ":catch" is found. |
| 6457 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6458 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6459 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6460 | return NULL; |
| 6461 | |
| 6462 | // scope for the try block itself |
| 6463 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6464 | if (scope == NULL) |
| 6465 | return NULL; |
| 6466 | |
| 6467 | return arg; |
| 6468 | } |
| 6469 | |
| 6470 | /* |
| 6471 | * compile "catch {expr}" |
| 6472 | */ |
| 6473 | static char_u * |
| 6474 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6475 | { |
| 6476 | scope_T *scope = cctx->ctx_scope; |
| 6477 | garray_T *instr = &cctx->ctx_instr; |
| 6478 | char_u *p; |
| 6479 | isn_T *isn; |
| 6480 | |
| 6481 | // end block scope from :try or :catch |
| 6482 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6483 | compile_endblock(cctx); |
| 6484 | scope = cctx->ctx_scope; |
| 6485 | |
| 6486 | // Error if not in a :try scope |
| 6487 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6488 | { |
| 6489 | emsg(_(e_catch)); |
| 6490 | return NULL; |
| 6491 | } |
| 6492 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6493 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6494 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6495 | emsg(_(e_catch_unreachable_after_catch_all)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6496 | return NULL; |
| 6497 | } |
| 6498 | |
| 6499 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6500 | if (compile_jump_to_end(&scope->se_u.se_try.ts_end_label, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6501 | JUMP_ALWAYS, cctx) == FAIL) |
| 6502 | return NULL; |
| 6503 | |
| 6504 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6505 | 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] | 6506 | if (isn->isn_arg.try.try_catch == 0) |
| 6507 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6508 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6509 | { |
| 6510 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6511 | 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] | 6512 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6513 | } |
| 6514 | |
| 6515 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6516 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6517 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6518 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6519 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6520 | } |
| 6521 | else |
| 6522 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6523 | char_u *end; |
| 6524 | char_u *pat; |
| 6525 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6526 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6527 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6528 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6529 | // Push v:exception, push {expr} and MATCH |
| 6530 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6531 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6532 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6533 | if (*end != *p) |
| 6534 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 6535 | semsg(_(e_separator_mismatch_str), p); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6536 | vim_free(tofree); |
| 6537 | return FAIL; |
| 6538 | } |
| 6539 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6540 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6541 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6542 | len = (int)(end - tofree); |
| 6543 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6544 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6545 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6546 | if (pat == NULL) |
| 6547 | return FAIL; |
| 6548 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6549 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6550 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6551 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6552 | return NULL; |
| 6553 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6554 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6555 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6556 | return NULL; |
| 6557 | } |
| 6558 | |
| 6559 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6560 | return NULL; |
| 6561 | |
| 6562 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6563 | return NULL; |
| 6564 | return p; |
| 6565 | } |
| 6566 | |
| 6567 | static char_u * |
| 6568 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6569 | { |
| 6570 | scope_T *scope = cctx->ctx_scope; |
| 6571 | garray_T *instr = &cctx->ctx_instr; |
| 6572 | isn_T *isn; |
| 6573 | |
| 6574 | // end block scope from :try or :catch |
| 6575 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6576 | compile_endblock(cctx); |
| 6577 | scope = cctx->ctx_scope; |
| 6578 | |
| 6579 | // Error if not in a :try scope |
| 6580 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6581 | { |
| 6582 | emsg(_(e_finally)); |
| 6583 | return NULL; |
| 6584 | } |
| 6585 | |
| 6586 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6587 | 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] | 6588 | if (isn->isn_arg.try.try_finally != 0) |
| 6589 | { |
| 6590 | emsg(_(e_finally_dup)); |
| 6591 | return NULL; |
| 6592 | } |
| 6593 | |
| 6594 | // 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] | 6595 | 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] | 6596 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6597 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6598 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6599 | { |
| 6600 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6601 | 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] | 6602 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6603 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6604 | } |
| 6605 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6606 | // TODO: set index in ts_finally_label jumps |
| 6607 | |
| 6608 | return arg; |
| 6609 | } |
| 6610 | |
| 6611 | static char_u * |
| 6612 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6613 | { |
| 6614 | scope_T *scope = cctx->ctx_scope; |
| 6615 | garray_T *instr = &cctx->ctx_instr; |
| 6616 | isn_T *isn; |
| 6617 | |
| 6618 | // end block scope from :catch or :finally |
| 6619 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6620 | compile_endblock(cctx); |
| 6621 | scope = cctx->ctx_scope; |
| 6622 | |
| 6623 | // Error if not in a :try scope |
| 6624 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6625 | { |
| 6626 | if (scope == NULL) |
| 6627 | emsg(_(e_no_endtry)); |
| 6628 | else if (scope->se_type == WHILE_SCOPE) |
| 6629 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6630 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6631 | emsg(_(e_endfor)); |
| 6632 | else |
| 6633 | emsg(_(e_endif)); |
| 6634 | return NULL; |
| 6635 | } |
| 6636 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6637 | 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] | 6638 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6639 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6640 | emsg(_(e_missing_catch_or_finally)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6641 | return NULL; |
| 6642 | } |
| 6643 | |
| 6644 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6645 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6646 | 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] | 6647 | |
| 6648 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6649 | if (isn->isn_arg.try.try_catch == 0) |
| 6650 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6651 | if (isn->isn_arg.try.try_finally == 0) |
| 6652 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6653 | |
| 6654 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6655 | { |
| 6656 | // Last catch without match jumps here |
| 6657 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6658 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6659 | } |
| 6660 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6661 | compile_endblock(cctx); |
| 6662 | |
| 6663 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6664 | return NULL; |
| 6665 | return arg; |
| 6666 | } |
| 6667 | |
| 6668 | /* |
| 6669 | * compile "throw {expr}" |
| 6670 | */ |
| 6671 | static char_u * |
| 6672 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6673 | { |
| 6674 | char_u *p = skipwhite(arg); |
| 6675 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6676 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6677 | return NULL; |
| 6678 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6679 | return NULL; |
| 6680 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6681 | return NULL; |
| 6682 | |
| 6683 | return p; |
| 6684 | } |
| 6685 | |
| 6686 | /* |
| 6687 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6688 | * compile "echomsg expr" |
| 6689 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6690 | * compile "execute expr" |
| 6691 | */ |
| 6692 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6693 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6694 | { |
| 6695 | char_u *p = arg; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6696 | char_u *prev; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6697 | int count = 0; |
| 6698 | |
| 6699 | for (;;) |
| 6700 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6701 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6702 | return NULL; |
| 6703 | ++count; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6704 | prev = p; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6705 | p = skipwhite(p); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6706 | if (ends_excmd2(prev, p)) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6707 | break; |
| 6708 | } |
| 6709 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6710 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6711 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6712 | else if (cmdidx == CMD_execute) |
| 6713 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6714 | else if (cmdidx == CMD_echomsg) |
| 6715 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6716 | else |
| 6717 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6718 | return p; |
| 6719 | } |
| 6720 | |
| 6721 | /* |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 6722 | * :put r |
| 6723 | * :put ={expr} |
| 6724 | */ |
| 6725 | static char_u * |
| 6726 | compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 6727 | { |
| 6728 | char_u *line = arg; |
| 6729 | linenr_T lnum; |
| 6730 | char *errormsg; |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 6731 | int above = eap->forceit; |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 6732 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 6733 | eap->regname = *line; |
| 6734 | |
| 6735 | if (eap->regname == '=') |
| 6736 | { |
| 6737 | char_u *p = line + 1; |
| 6738 | |
| 6739 | if (compile_expr0(&p, cctx) == FAIL) |
| 6740 | return NULL; |
| 6741 | line = p; |
| 6742 | } |
| 6743 | else if (eap->regname != NUL) |
| 6744 | ++line; |
| 6745 | |
Bram Moolenaar | e13bdec | 2020-10-16 23:16:47 +0200 | [diff] [blame] | 6746 | // "errormsg" will not be set because the range is ADDR_LINES. |
| 6747 | // TODO: if the range contains something like "$" or "." need to evaluate |
| 6748 | // at runtime |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 6749 | if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL) |
| 6750 | return NULL; |
| 6751 | if (eap->addr_count == 0) |
| 6752 | lnum = -1; |
| 6753 | else |
| 6754 | lnum = eap->line2; |
| 6755 | if (above) |
| 6756 | --lnum; |
| 6757 | |
| 6758 | generate_PUT(cctx, eap->regname, lnum); |
| 6759 | return line; |
| 6760 | } |
| 6761 | |
| 6762 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6763 | * A command that is not compiled, execute with legacy code. |
| 6764 | */ |
| 6765 | static char_u * |
| 6766 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6767 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6768 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6769 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6770 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6771 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6772 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6773 | goto theend; |
| 6774 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6775 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6776 | { |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 6777 | long argt = eap->argt; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6778 | int usefilter = FALSE; |
| 6779 | |
| 6780 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6781 | |
| 6782 | // If the command can be followed by a bar, find the bar and truncate |
| 6783 | // it, so that the following command can be compiled. |
| 6784 | // The '|' is overwritten with a NUL, it is put back below. |
| 6785 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6786 | && *eap->arg == '!') |
| 6787 | // :w !filter or :r !filter or :r! filter |
| 6788 | usefilter = TRUE; |
| 6789 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6790 | { |
Bram Moolenaar | b8a9296 | 2020-08-20 18:02:47 +0200 | [diff] [blame] | 6791 | eap->argt = argt; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6792 | separate_nextcmd(eap); |
| 6793 | if (eap->nextcmd != NULL) |
| 6794 | nextcmd = eap->nextcmd; |
| 6795 | } |
| 6796 | } |
| 6797 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6798 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6799 | { |
| 6800 | // expand filename in "syntax include [@group] filename" |
| 6801 | has_expr = TRUE; |
| 6802 | eap->arg = skipwhite(eap->arg + 7); |
| 6803 | if (*eap->arg == '@') |
| 6804 | eap->arg = skiptowhite(eap->arg); |
| 6805 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6806 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6807 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6808 | { |
| 6809 | int count = 0; |
| 6810 | char_u *start = skipwhite(line); |
| 6811 | |
| 6812 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6813 | // PUSHS ":cmd xxx" |
| 6814 | // eval expr1 |
| 6815 | // PUSHS "yyy" |
| 6816 | // eval expr2 |
| 6817 | // PUSHS "zzz" |
| 6818 | // EXECCONCAT 5 |
| 6819 | for (;;) |
| 6820 | { |
| 6821 | if (p > start) |
| 6822 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6823 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6824 | ++count; |
| 6825 | } |
| 6826 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6827 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6828 | return NULL; |
| 6829 | may_generate_2STRING(-1, cctx); |
| 6830 | ++count; |
| 6831 | p = skipwhite(p); |
| 6832 | if (*p != '`') |
| 6833 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6834 | emsg(_(e_missing_backtick)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6835 | return NULL; |
| 6836 | } |
| 6837 | start = p + 1; |
| 6838 | |
| 6839 | p = (char_u *)strstr((char *)start, "`="); |
| 6840 | if (p == NULL) |
| 6841 | { |
| 6842 | if (*skipwhite(start) != NUL) |
| 6843 | { |
| 6844 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6845 | ++count; |
| 6846 | } |
| 6847 | break; |
| 6848 | } |
| 6849 | } |
| 6850 | generate_EXECCONCAT(cctx, count); |
| 6851 | } |
| 6852 | else |
| 6853 | generate_EXEC(cctx, line); |
| 6854 | |
| 6855 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6856 | if (*nextcmd != NUL) |
| 6857 | { |
| 6858 | // the parser expects a pointer to the bar, put it back |
| 6859 | --nextcmd; |
| 6860 | *nextcmd = '|'; |
| 6861 | } |
| 6862 | |
| 6863 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6864 | } |
| 6865 | |
| 6866 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6867 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6868 | * 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] | 6869 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6870 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6871 | add_def_function(ufunc_T *ufunc) |
| 6872 | { |
| 6873 | dfunc_T *dfunc; |
| 6874 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6875 | if (def_functions.ga_len == 0) |
| 6876 | { |
| 6877 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6878 | // wasn't set. |
| 6879 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6880 | return FAIL; |
| 6881 | ++def_functions.ga_len; |
| 6882 | } |
| 6883 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6884 | // Add the function to "def_functions". |
| 6885 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6886 | return FAIL; |
| 6887 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6888 | CLEAR_POINTER(dfunc); |
| 6889 | dfunc->df_idx = def_functions.ga_len; |
| 6890 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6891 | dfunc->df_ufunc = ufunc; |
| 6892 | ++def_functions.ga_len; |
| 6893 | return OK; |
| 6894 | } |
| 6895 | |
| 6896 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6897 | * After ex_function() has collected all the function lines: parse and compile |
| 6898 | * the lines into instructions. |
| 6899 | * Adds the function to "def_functions". |
| 6900 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6901 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6902 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6903 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6904 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6905 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6906 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6907 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6908 | compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6909 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6910 | char_u *line = NULL; |
| 6911 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6912 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6913 | cctx_T cctx; |
| 6914 | garray_T *instr; |
| 6915 | int called_emsg_before = called_emsg; |
| 6916 | int ret = FAIL; |
| 6917 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | f4e8cdd | 2020-10-12 22:07:13 +0200 | [diff] [blame] | 6918 | int save_estack_compiling = estack_compiling; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6919 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6920 | int emsg_before = called_emsg; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6921 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6922 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6923 | // When using a function that was compiled before: Free old instructions. |
| 6924 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6925 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6926 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6927 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6928 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6929 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6930 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6931 | else |
| 6932 | { |
| 6933 | if (add_def_function(ufunc) == FAIL) |
| 6934 | return FAIL; |
| 6935 | new_def_function = TRUE; |
| 6936 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6937 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6938 | ufunc->uf_def_status = UF_COMPILING; |
| 6939 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6940 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6941 | cctx.ctx_ufunc = ufunc; |
| 6942 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6943 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6944 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6945 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6946 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6947 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6948 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6949 | instr = &cctx.ctx_instr; |
| 6950 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6951 | // Set the context to the function, it may be compiled when called from |
| 6952 | // another script. Set the script version to the most modern one. |
| 6953 | // The line number will be set in next_line_from_context(). |
| 6954 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6955 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6956 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6957 | // Make sure error messages are OK. |
| 6958 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6959 | if (do_estack_push) |
| 6960 | estack_push_ufunc(ufunc, 1); |
Bram Moolenaar | f4e8cdd | 2020-10-12 22:07:13 +0200 | [diff] [blame] | 6961 | estack_compiling = TRUE; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6962 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6963 | if (ufunc->uf_def_args.ga_len > 0) |
| 6964 | { |
| 6965 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6966 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6967 | int i; |
| 6968 | char_u *arg; |
| 6969 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6970 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6971 | |
| 6972 | // Produce instructions for the default values of optional arguments. |
| 6973 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6974 | // where to start when the function is called, depending on the number |
| 6975 | // of arguments. |
| 6976 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6977 | if (ufunc->uf_def_arg_idx == NULL) |
| 6978 | goto erret; |
| 6979 | for (i = 0; i < count; ++i) |
| 6980 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6981 | garray_T *stack = &cctx.ctx_type_stack; |
| 6982 | type_T *val_type; |
| 6983 | int arg_idx = first_def_arg + i; |
| 6984 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6985 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6986 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6987 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6988 | goto erret; |
| 6989 | |
| 6990 | // If no type specified use the type of the default value. |
| 6991 | // Otherwise check that the default value type matches the |
| 6992 | // specified type. |
| 6993 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6994 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6995 | { |
| 6996 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6997 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6998 | } |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 6999 | else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, |
| 7000 | TRUE, arg_idx + 1) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7001 | goto erret; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 7002 | |
| 7003 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7004 | goto erret; |
| 7005 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7006 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 7007 | |
| 7008 | if (did_set_arg_type) |
| 7009 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 7010 | } |
| 7011 | |
| 7012 | /* |
| 7013 | * Loop over all the lines of the function and generate instructions. |
| 7014 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7015 | for (;;) |
| 7016 | { |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 7017 | exarg_T ea; |
| 7018 | cmdmod_T save_cmdmod; |
| 7019 | int starts_with_colon = FALSE; |
| 7020 | char_u *cmd; |
| 7021 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7022 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 7023 | // Bail out on the first error to avoid a flood of errors and report |
| 7024 | // the right line number when inside try/catch. |
| 7025 | if (emsg_before != called_emsg) |
| 7026 | goto erret; |
| 7027 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7028 | if (line != NULL && *line == '|') |
| 7029 | // the line continues after a '|' |
| 7030 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 7031 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 7032 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 7033 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7034 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 7035 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7036 | goto erret; |
| 7037 | } |
| 7038 | else |
| 7039 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 7040 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 7041 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7042 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7043 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7044 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7045 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7046 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 7047 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7048 | ea.cmdlinep = &line; |
| 7049 | ea.cmd = skipwhite(line); |
| 7050 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7051 | // Some things can be recognized by the first character. |
| 7052 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7053 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7054 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7055 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7056 | if (ea.cmd[1] != '{') |
| 7057 | { |
| 7058 | line = (char_u *)""; |
| 7059 | continue; |
| 7060 | } |
| 7061 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7062 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7063 | case '}': |
| 7064 | { |
| 7065 | // "}" ends a block scope |
| 7066 | scopetype_T stype = cctx.ctx_scope == NULL |
| 7067 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7068 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7069 | if (stype == BLOCK_SCOPE) |
| 7070 | { |
| 7071 | compile_endblock(&cctx); |
| 7072 | line = ea.cmd; |
| 7073 | } |
| 7074 | else |
| 7075 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7076 | emsg(_(e_using_rcurly_outside_if_block_scope)); |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7077 | goto erret; |
| 7078 | } |
| 7079 | if (line != NULL) |
| 7080 | line = skipwhite(ea.cmd + 1); |
| 7081 | continue; |
| 7082 | } |
| 7083 | |
| 7084 | case '{': |
| 7085 | // "{" starts a block scope |
| 7086 | // "{'a': 1}->func() is something else |
| 7087 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 7088 | { |
| 7089 | line = compile_block(ea.cmd, &cctx); |
| 7090 | continue; |
| 7091 | } |
| 7092 | break; |
| 7093 | |
| 7094 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7095 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 7096 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7097 | } |
| 7098 | |
| 7099 | /* |
| 7100 | * COMMAND MODIFIERS |
| 7101 | */ |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 7102 | save_cmdmod = cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7103 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 7104 | { |
| 7105 | if (errormsg != NULL) |
| 7106 | goto erret; |
| 7107 | // empty line or comment |
| 7108 | line = (char_u *)""; |
| 7109 | continue; |
| 7110 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 7111 | // TODO: use modifiers in the command |
| 7112 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 7113 | cmdmod = save_cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7114 | |
| 7115 | // Skip ":call" to get to the function name. |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 7116 | p = ea.cmd; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7117 | if (checkforcmd(&ea.cmd, "call", 3)) |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 7118 | { |
| 7119 | if (*ea.cmd == '(') |
| 7120 | // not for "call()" |
| 7121 | ea.cmd = p; |
| 7122 | else |
| 7123 | ea.cmd = skipwhite(ea.cmd); |
| 7124 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7125 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7126 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7127 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7128 | char_u *pskip; |
| 7129 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7130 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7131 | // find what follows. |
| 7132 | // Skip over "var.member", "var[idx]" and the like. |
| 7133 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 7134 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7135 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7136 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7137 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7138 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7139 | char_u *var_end; |
| 7140 | int oplen; |
| 7141 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7142 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 7143 | if (ea.cmd[0] == '@') |
| 7144 | var_end = ea.cmd + 2; |
| 7145 | else |
| 7146 | var_end = find_name_end(pskip, NULL, NULL, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7147 | FNE_CHECK_START | FNE_INCL_BR); |
| 7148 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7149 | if (oplen > 0) |
| 7150 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7151 | size_t len = p - ea.cmd; |
| 7152 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7153 | // Recognize an assignment if we recognize the variable |
| 7154 | // name: |
| 7155 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 7156 | // "local = expr" where "local" is a local var. |
| 7157 | // "script = expr" where "script" is a script-local var. |
| 7158 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7159 | // "&opt = expr" |
| 7160 | // "$ENV = expr" |
| 7161 | // "@r = expr" |
| 7162 | if (*ea.cmd == '&' |
| 7163 | || *ea.cmd == '$' |
| 7164 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7165 | || ((len) > 2 && ea.cmd[1] == ':') |
| 7166 | || lookup_local(ea.cmd, len, &cctx) != NULL |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 7167 | || arg_exists(ea.cmd, len, NULL, NULL, |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7168 | NULL, &cctx) == OK |
Bram Moolenaar | fbbcd00 | 2020-10-15 12:46:44 +0200 | [diff] [blame] | 7169 | || script_var_exists(ea.cmd, len, |
| 7170 | FALSE, &cctx) == OK |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 7171 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7172 | { |
| 7173 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7174 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7175 | goto erret; |
| 7176 | continue; |
| 7177 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7178 | } |
| 7179 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7180 | |
| 7181 | if (*ea.cmd == '[') |
| 7182 | { |
| 7183 | // [var, var] = expr |
| 7184 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 7185 | if (line == NULL) |
| 7186 | goto erret; |
| 7187 | if (line != ea.cmd) |
| 7188 | continue; |
| 7189 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7190 | } |
| 7191 | |
| 7192 | /* |
| 7193 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7194 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7195 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7196 | cmd = ea.cmd; |
Bram Moolenaar | 7c5ad34 | 2020-08-12 15:48:55 +0200 | [diff] [blame] | 7197 | if (*cmd != '\'' || starts_with_colon) |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7198 | { |
Bram Moolenaar | 3bd8de4 | 2020-09-14 16:37:34 +0200 | [diff] [blame] | 7199 | ea.cmd = skip_range(ea.cmd, TRUE, NULL); |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 7200 | if (ea.cmd > cmd) |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7201 | { |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 7202 | if (!starts_with_colon) |
| 7203 | { |
| 7204 | emsg(_(e_colon_required_before_a_range)); |
| 7205 | goto erret; |
| 7206 | } |
| 7207 | if (ends_excmd2(line, ea.cmd)) |
| 7208 | { |
| 7209 | // A range without a command: jump to the line. |
| 7210 | // TODO: compile to a more efficient command, possibly |
| 7211 | // calling parse_cmd_address(). |
| 7212 | ea.cmdidx = CMD_SIZE; |
| 7213 | line = compile_exec(line, &ea, &cctx); |
| 7214 | goto nextline; |
| 7215 | } |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 7216 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7217 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7218 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7219 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 7220 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7221 | |
| 7222 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 7223 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 7224 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7225 | { |
| 7226 | line += STRLEN(line); |
| 7227 | continue; |
| 7228 | } |
| 7229 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7230 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7231 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7232 | { |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 7233 | // CMD_var cannot happen, compile_assignment() above is used |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7234 | iemsg("Command from find_ex_command() not handled"); |
| 7235 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7236 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7237 | } |
| 7238 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 7239 | if (cctx.ctx_had_return |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 7240 | && ea.cmdidx != CMD_elseif |
| 7241 | && ea.cmdidx != CMD_else |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7242 | && ea.cmdidx != CMD_endif |
| 7243 | && ea.cmdidx != CMD_endfor |
| 7244 | && ea.cmdidx != CMD_endwhile |
| 7245 | && ea.cmdidx != CMD_catch |
| 7246 | && ea.cmdidx != CMD_finally |
| 7247 | && ea.cmdidx != CMD_endtry) |
| 7248 | { |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 7249 | emsg(_(e_unreachable_code_after_return)); |
| 7250 | goto erret; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7251 | } |
| 7252 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 7253 | p = skipwhite(p); |
| 7254 | if (ea.cmdidx != CMD_SIZE |
| 7255 | && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read) |
| 7256 | { |
Bram Moolenaar | 9b123d8 | 2020-09-14 22:39:11 +0200 | [diff] [blame] | 7257 | if (ea.cmdidx >= 0) |
| 7258 | ea.argt = excmd_get_argt(ea.cmdidx); |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 7259 | if ((ea.argt & EX_BANG) && *p == '!') |
| 7260 | { |
| 7261 | ea.forceit = TRUE; |
| 7262 | p = skipwhite(p + 1); |
| 7263 | } |
| 7264 | } |
| 7265 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7266 | switch (ea.cmdidx) |
| 7267 | { |
| 7268 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 7269 | ea.arg = p; |
| 7270 | line = compile_nested_function(&ea, &cctx); |
| 7271 | break; |
| 7272 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7273 | case CMD_function: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7274 | // TODO: should we allow this, e.g. to declare a global |
| 7275 | // function? |
| 7276 | emsg(_(e_cannot_use_function_inside_def)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7277 | goto erret; |
| 7278 | |
| 7279 | case CMD_return: |
| 7280 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7281 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7282 | break; |
| 7283 | |
| 7284 | case CMD_let: |
Bram Moolenaar | cfcd011 | 2020-09-27 15:19:27 +0200 | [diff] [blame] | 7285 | if (get_vim_var_nr(VV_DISALLOW_LET)) |
| 7286 | { |
| 7287 | emsg(_(e_cannot_use_let_in_vim9_script)); |
| 7288 | break; |
| 7289 | } |
| 7290 | // FALLTHROUGH |
Bram Moolenaar | 30fd820 | 2020-09-26 15:09:30 +0200 | [diff] [blame] | 7291 | case CMD_var: |
| 7292 | case CMD_final: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7293 | case CMD_const: |
| 7294 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7295 | if (line == p) |
| 7296 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7297 | break; |
| 7298 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7299 | case CMD_unlet: |
| 7300 | case CMD_unlockvar: |
| 7301 | case CMD_lockvar: |
| 7302 | line = compile_unletlock(p, &ea, &cctx); |
| 7303 | break; |
| 7304 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7305 | case CMD_import: |
| 7306 | line = compile_import(p, &cctx); |
| 7307 | break; |
| 7308 | |
| 7309 | case CMD_if: |
| 7310 | line = compile_if(p, &cctx); |
| 7311 | break; |
| 7312 | case CMD_elseif: |
| 7313 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7314 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7315 | break; |
| 7316 | case CMD_else: |
| 7317 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7318 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7319 | break; |
| 7320 | case CMD_endif: |
| 7321 | line = compile_endif(p, &cctx); |
| 7322 | break; |
| 7323 | |
| 7324 | case CMD_while: |
| 7325 | line = compile_while(p, &cctx); |
| 7326 | break; |
| 7327 | case CMD_endwhile: |
| 7328 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7329 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7330 | break; |
| 7331 | |
| 7332 | case CMD_for: |
| 7333 | line = compile_for(p, &cctx); |
| 7334 | break; |
| 7335 | case CMD_endfor: |
| 7336 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7337 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7338 | break; |
| 7339 | case CMD_continue: |
| 7340 | line = compile_continue(p, &cctx); |
| 7341 | break; |
| 7342 | case CMD_break: |
| 7343 | line = compile_break(p, &cctx); |
| 7344 | break; |
| 7345 | |
| 7346 | case CMD_try: |
| 7347 | line = compile_try(p, &cctx); |
| 7348 | break; |
| 7349 | case CMD_catch: |
| 7350 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7351 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7352 | break; |
| 7353 | case CMD_finally: |
| 7354 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7355 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7356 | break; |
| 7357 | case CMD_endtry: |
| 7358 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7359 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7360 | break; |
| 7361 | case CMD_throw: |
| 7362 | line = compile_throw(p, &cctx); |
| 7363 | break; |
| 7364 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7365 | case CMD_eval: |
| 7366 | if (compile_expr0(&p, &cctx) == FAIL) |
| 7367 | goto erret; |
| 7368 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 7369 | // drop the result |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 7370 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 7371 | |
| 7372 | line = skipwhite(p); |
| 7373 | break; |
| 7374 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7375 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7376 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7377 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7378 | case CMD_echomsg: |
| 7379 | case CMD_echoerr: |
| 7380 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 7381 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7382 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7383 | case CMD_put: |
| 7384 | ea.cmd = cmd; |
| 7385 | line = compile_put(p, &ea, &cctx); |
| 7386 | break; |
| 7387 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 7388 | // TODO: any other commands with an expression argument? |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7389 | |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 7390 | case CMD_append: |
| 7391 | case CMD_change: |
| 7392 | case CMD_insert: |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 7393 | case CMD_t: |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 7394 | case CMD_xit: |
| 7395 | not_in_vim9(&ea); |
| 7396 | goto erret; |
| 7397 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7398 | case CMD_SIZE: |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 7399 | if (cctx.ctx_skip != SKIP_YES) |
| 7400 | { |
| 7401 | semsg(_(e_invalid_command_str), ea.cmd); |
| 7402 | goto erret; |
| 7403 | } |
| 7404 | // We don't check for a next command here. |
| 7405 | line = (char_u *)""; |
| 7406 | break; |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7407 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7408 | default: |
Bram Moolenaar | 5163fcc | 2020-08-27 23:37:09 +0200 | [diff] [blame] | 7409 | if (cctx.ctx_skip == SKIP_YES) |
| 7410 | { |
| 7411 | // We don't check for a next command here. |
| 7412 | line = (char_u *)""; |
| 7413 | } |
| 7414 | else |
| 7415 | { |
| 7416 | // Not recognized, execute with do_cmdline_cmd(). |
| 7417 | ea.arg = p; |
| 7418 | line = compile_exec(line, &ea, &cctx); |
| 7419 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7420 | break; |
| 7421 | } |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 7422 | nextline: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7423 | if (line == NULL) |
| 7424 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7425 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7426 | |
| 7427 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7428 | { |
| 7429 | iemsg("Type stack underflow"); |
| 7430 | goto erret; |
| 7431 | } |
| 7432 | } |
| 7433 | |
| 7434 | if (cctx.ctx_scope != NULL) |
| 7435 | { |
| 7436 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7437 | emsg(_(e_endif)); |
| 7438 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7439 | emsg(_(e_endwhile)); |
| 7440 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7441 | emsg(_(e_endfor)); |
| 7442 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7443 | emsg(_(e_missing_rcurly)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7444 | goto erret; |
| 7445 | } |
| 7446 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7447 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7448 | { |
| 7449 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7450 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7451 | emsg(_(e_missing_return_statement)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7452 | goto erret; |
| 7453 | } |
| 7454 | |
| 7455 | // Return zero if there is no return at the end. |
| 7456 | generate_PUSHNR(&cctx, 0); |
| 7457 | generate_instr(&cctx, ISN_RETURN); |
| 7458 | } |
| 7459 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7460 | { |
| 7461 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7462 | + ufunc->uf_dfunc_idx; |
| 7463 | dfunc->df_deleted = FALSE; |
| 7464 | dfunc->df_instr = instr->ga_data; |
| 7465 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7466 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 7467 | dfunc->df_has_closure = cctx.ctx_has_closure; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7468 | if (cctx.ctx_outer_used) |
| 7469 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7470 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7471 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7472 | |
| 7473 | ret = OK; |
| 7474 | |
| 7475 | erret: |
| 7476 | if (ret == FAIL) |
| 7477 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7478 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7479 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7480 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7481 | |
| 7482 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7483 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7484 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7485 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7486 | // If using the last entry in the table and it was added above, we |
| 7487 | // might as well remove it. |
| 7488 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7489 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7490 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7491 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7492 | ufunc->uf_dfunc_idx = 0; |
| 7493 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7494 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7495 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7496 | while (cctx.ctx_scope != NULL) |
| 7497 | drop_scope(&cctx); |
| 7498 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7499 | // Don't execute this function body. |
| 7500 | ga_clear_strings(&ufunc->uf_lines); |
| 7501 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7502 | if (errormsg != NULL) |
| 7503 | emsg(errormsg); |
| 7504 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | e13bdec | 2020-10-16 23:16:47 +0200 | [diff] [blame] | 7505 | emsg(_(e_compiling_def_function_failed)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7506 | } |
| 7507 | |
| 7508 | current_sctx = save_current_sctx; |
Bram Moolenaar | f4e8cdd | 2020-10-12 22:07:13 +0200 | [diff] [blame] | 7509 | estack_compiling = save_estack_compiling; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7510 | if (do_estack_push) |
| 7511 | estack_pop(); |
| 7512 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7513 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7514 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7515 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7516 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7517 | } |
| 7518 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7519 | void |
| 7520 | set_function_type(ufunc_T *ufunc) |
| 7521 | { |
| 7522 | int varargs = ufunc->uf_va_name != NULL; |
| 7523 | int argcount = ufunc->uf_args.ga_len; |
| 7524 | |
| 7525 | // Create a type for the function, with the return type and any |
| 7526 | // argument types. |
| 7527 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7528 | // The type is included in "tt_args". |
| 7529 | if (argcount > 0 || varargs) |
| 7530 | { |
| 7531 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7532 | argcount, &ufunc->uf_type_list); |
| 7533 | // Add argument types to the function type. |
| 7534 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7535 | argcount + varargs, |
| 7536 | &ufunc->uf_type_list) == FAIL) |
| 7537 | return; |
| 7538 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7539 | ufunc->uf_func_type->tt_min_argcount = |
| 7540 | argcount - ufunc->uf_def_args.ga_len; |
| 7541 | if (ufunc->uf_arg_types == NULL) |
| 7542 | { |
| 7543 | int i; |
| 7544 | |
| 7545 | // lambda does not have argument types. |
| 7546 | for (i = 0; i < argcount; ++i) |
| 7547 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7548 | } |
| 7549 | else |
| 7550 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7551 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7552 | if (varargs) |
| 7553 | { |
| 7554 | ufunc->uf_func_type->tt_args[argcount] = |
| 7555 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7556 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7557 | } |
| 7558 | } |
| 7559 | else |
| 7560 | // No arguments, can use a predefined type. |
| 7561 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7562 | argcount, &ufunc->uf_type_list); |
| 7563 | } |
| 7564 | |
| 7565 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7566 | /* |
| 7567 | * Delete an instruction, free what it contains. |
| 7568 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7569 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7570 | delete_instr(isn_T *isn) |
| 7571 | { |
| 7572 | switch (isn->isn_type) |
| 7573 | { |
| 7574 | case ISN_EXEC: |
| 7575 | case ISN_LOADENV: |
| 7576 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7577 | case ISN_LOADB: |
| 7578 | case ISN_LOADW: |
| 7579 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7580 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7581 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7582 | case ISN_PUSHEXC: |
| 7583 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7584 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7585 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7586 | case ISN_STOREB: |
| 7587 | case ISN_STOREW: |
| 7588 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7589 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7590 | vim_free(isn->isn_arg.string); |
| 7591 | break; |
| 7592 | |
| 7593 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7594 | case ISN_STORES: |
| 7595 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7596 | break; |
| 7597 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7598 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7599 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7600 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7601 | break; |
| 7602 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7603 | case ISN_STOREOPT: |
| 7604 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7605 | break; |
| 7606 | |
| 7607 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7608 | blob_unref(isn->isn_arg.blob); |
| 7609 | break; |
| 7610 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7611 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7612 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7613 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7614 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7615 | break; |
| 7616 | |
| 7617 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7618 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7619 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7620 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7621 | break; |
| 7622 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7623 | case ISN_UCALL: |
| 7624 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7625 | break; |
| 7626 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7627 | case ISN_FUNCREF: |
| 7628 | { |
| 7629 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7630 | + isn->isn_arg.funcref.fr_func; |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 7631 | |
| 7632 | if (func_name_refcount(dfunc->df_ufunc->uf_name)) |
| 7633 | func_ptr_unref(dfunc->df_ufunc); |
| 7634 | } |
| 7635 | break; |
| 7636 | |
| 7637 | case ISN_DCALL: |
| 7638 | { |
| 7639 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7640 | + isn->isn_arg.dfunc.cdf_idx; |
| 7641 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 7642 | if (dfunc->df_ufunc != NULL |
| 7643 | && func_name_refcount(dfunc->df_ufunc->uf_name)) |
Bram Moolenaar | a05e524 | 2020-09-19 18:19:19 +0200 | [diff] [blame] | 7644 | func_ptr_unref(dfunc->df_ufunc); |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7645 | } |
| 7646 | break; |
| 7647 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7648 | case ISN_NEWFUNC: |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 7649 | { |
| 7650 | char_u *lambda = isn->isn_arg.newfunc.nf_lambda; |
| 7651 | ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL); |
| 7652 | |
| 7653 | if (ufunc != NULL) |
| 7654 | { |
| 7655 | // Clear uf_dfunc_idx so that the function is deleted. |
| 7656 | clear_def_function(ufunc); |
| 7657 | ufunc->uf_dfunc_idx = 0; |
| 7658 | func_ptr_unref(ufunc); |
| 7659 | } |
| 7660 | |
| 7661 | vim_free(lambda); |
| 7662 | vim_free(isn->isn_arg.newfunc.nf_global); |
| 7663 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7664 | break; |
| 7665 | |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 7666 | case ISN_CHECKTYPE: |
| 7667 | free_type(isn->isn_arg.type.ct_type); |
| 7668 | break; |
| 7669 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7670 | case ISN_2BOOL: |
| 7671 | case ISN_2STRING: |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 7672 | case ISN_2STRING_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7673 | case ISN_ADDBLOB: |
| 7674 | case ISN_ADDLIST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7675 | case ISN_ANYINDEX: |
| 7676 | case ISN_ANYSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7677 | case ISN_BCALL: |
| 7678 | case ISN_CATCH: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7679 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7680 | case ISN_CHECKNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7681 | case ISN_COMPAREANY: |
| 7682 | case ISN_COMPAREBLOB: |
| 7683 | case ISN_COMPAREBOOL: |
| 7684 | case ISN_COMPAREDICT: |
| 7685 | case ISN_COMPAREFLOAT: |
| 7686 | case ISN_COMPAREFUNC: |
| 7687 | case ISN_COMPARELIST: |
| 7688 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7689 | case ISN_COMPARESPECIAL: |
| 7690 | case ISN_COMPARESTRING: |
| 7691 | case ISN_CONCAT: |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 7692 | case ISN_COND2BOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7693 | case ISN_DROP: |
| 7694 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7695 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7696 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7697 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7698 | case ISN_EXECCONCAT: |
| 7699 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7700 | case ISN_FOR: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7701 | case ISN_GETITEM: |
| 7702 | case ISN_JUMP: |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 7703 | case ISN_LISTAPPEND: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7704 | case ISN_LISTINDEX: |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 7705 | case ISN_LISTSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7706 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7707 | case ISN_LOADBDICT: |
| 7708 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7709 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7710 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7711 | case ISN_LOADSCRIPT: |
| 7712 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7713 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7714 | case ISN_LOADWDICT: |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 7715 | case ISN_LOCKCONST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7716 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7717 | case ISN_NEGATENR: |
| 7718 | case ISN_NEWDICT: |
| 7719 | case ISN_NEWLIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7720 | case ISN_OPANY: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7721 | case ISN_OPFLOAT: |
| 7722 | case ISN_OPNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7723 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7724 | case ISN_PCALL_END: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7725 | case ISN_PUSHBOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7726 | case ISN_PUSHF: |
| 7727 | case ISN_PUSHNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7728 | case ISN_PUSHSPEC: |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7729 | case ISN_PUT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7730 | case ISN_RETURN: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7731 | case ISN_SHUFFLE: |
| 7732 | case ISN_SLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7733 | case ISN_STORE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7734 | case ISN_STOREDICT: |
| 7735 | case ISN_STORELIST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7736 | case ISN_STORENR: |
| 7737 | case ISN_STOREOUTER: |
| 7738 | case ISN_STOREREG: |
| 7739 | case ISN_STORESCRIPT: |
| 7740 | case ISN_STOREV: |
| 7741 | case ISN_STRINDEX: |
| 7742 | case ISN_STRSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7743 | case ISN_THROW: |
| 7744 | case ISN_TRY: |
| 7745 | // nothing allocated |
| 7746 | break; |
| 7747 | } |
| 7748 | } |
| 7749 | |
| 7750 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7751 | * Free all instructions for "dfunc". |
| 7752 | */ |
| 7753 | static void |
| 7754 | delete_def_function_contents(dfunc_T *dfunc) |
| 7755 | { |
| 7756 | int idx; |
| 7757 | |
| 7758 | ga_clear(&dfunc->df_def_args_isn); |
| 7759 | |
| 7760 | if (dfunc->df_instr != NULL) |
| 7761 | { |
| 7762 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7763 | delete_instr(dfunc->df_instr + idx); |
| 7764 | VIM_CLEAR(dfunc->df_instr); |
| 7765 | } |
| 7766 | |
| 7767 | dfunc->df_deleted = TRUE; |
| 7768 | } |
| 7769 | |
| 7770 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7771 | * When a user function is deleted, clear the contents of any associated def |
| 7772 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7773 | */ |
| 7774 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7775 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7776 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7777 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7778 | { |
| 7779 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7780 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7781 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7782 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7783 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7784 | } |
| 7785 | } |
| 7786 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 7787 | /* |
| 7788 | * Used when a user function is about to be deleted: remove the pointer to it. |
| 7789 | * The entry in def_functions is then unused. |
| 7790 | */ |
| 7791 | void |
| 7792 | unlink_def_function(ufunc_T *ufunc) |
| 7793 | { |
| 7794 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; |
| 7795 | |
| 7796 | dfunc->df_ufunc = NULL; |
| 7797 | } |
| 7798 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7799 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7800 | /* |
| 7801 | * Free all functions defined with ":def". |
| 7802 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7803 | void |
| 7804 | free_def_functions(void) |
| 7805 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7806 | int idx; |
| 7807 | |
| 7808 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7809 | { |
| 7810 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7811 | |
| 7812 | delete_def_function_contents(dfunc); |
| 7813 | } |
| 7814 | |
| 7815 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7816 | } |
| 7817 | #endif |
| 7818 | |
| 7819 | |
| 7820 | #endif // FEAT_EVAL |