Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9compile.c: :def and dealing with instructions |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | #ifdef VMS |
| 20 | # include <float.h> |
| 21 | #endif |
| 22 | |
| 23 | #define DEFINE_VIM9_GLOBALS |
| 24 | #include "vim9.h" |
| 25 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 26 | // values for ctx_skip |
| 27 | typedef enum { |
| 28 | SKIP_NOT, // condition is a constant, produce code |
| 29 | SKIP_YES, // condition is a constant, do NOT produce code |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 30 | SKIP_UNKNOWN // condition is not a constant, produce code |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 31 | } skip_T; |
| 32 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Chain of jump instructions where the end label needs to be set. |
| 35 | */ |
| 36 | typedef struct endlabel_S endlabel_T; |
| 37 | struct endlabel_S { |
| 38 | endlabel_T *el_next; // chain end_label locations |
| 39 | int el_end_label; // instruction idx where to set end |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * info specific for the scope of :if / elseif / else |
| 44 | */ |
| 45 | typedef struct { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 46 | int is_seen_else; |
| 47 | int is_had_return; // every block ends in :return |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 48 | int is_if_label; // instruction idx at IF or ELSEIF |
| 49 | endlabel_T *is_end_label; // instructions to set end label |
| 50 | } ifscope_T; |
| 51 | |
| 52 | /* |
| 53 | * info specific for the scope of :while |
| 54 | */ |
| 55 | typedef struct { |
| 56 | int ws_top_label; // instruction idx at WHILE |
| 57 | endlabel_T *ws_end_label; // instructions to set end |
| 58 | } whilescope_T; |
| 59 | |
| 60 | /* |
| 61 | * info specific for the scope of :for |
| 62 | */ |
| 63 | typedef struct { |
| 64 | int fs_top_label; // instruction idx at FOR |
| 65 | endlabel_T *fs_end_label; // break instructions |
| 66 | } forscope_T; |
| 67 | |
| 68 | /* |
| 69 | * info specific for the scope of :try |
| 70 | */ |
| 71 | typedef struct { |
| 72 | int ts_try_label; // instruction idx at TRY |
| 73 | endlabel_T *ts_end_label; // jump to :finally or :endtry |
| 74 | int ts_catch_label; // instruction idx of last CATCH |
| 75 | int ts_caught_all; // "catch" without argument encountered |
| 76 | } tryscope_T; |
| 77 | |
| 78 | typedef enum { |
| 79 | NO_SCOPE, |
| 80 | IF_SCOPE, |
| 81 | WHILE_SCOPE, |
| 82 | FOR_SCOPE, |
| 83 | TRY_SCOPE, |
| 84 | BLOCK_SCOPE |
| 85 | } scopetype_T; |
| 86 | |
| 87 | /* |
| 88 | * Info for one scope, pointed to by "ctx_scope". |
| 89 | */ |
| 90 | typedef struct scope_S scope_T; |
| 91 | struct scope_S { |
| 92 | scope_T *se_outer; // scope containing this one |
| 93 | scopetype_T se_type; |
| 94 | int se_local_count; // ctx_locals.ga_len before scope |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 95 | skip_T se_skip_save; // ctx_skip before the block |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 96 | union { |
| 97 | ifscope_T se_if; |
| 98 | whilescope_T se_while; |
| 99 | forscope_T se_for; |
| 100 | tryscope_T se_try; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 101 | } se_u; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Entry for "ctx_locals". Used for arguments and local variables. |
| 106 | */ |
| 107 | typedef struct { |
| 108 | char_u *lv_name; |
| 109 | type_T *lv_type; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 110 | int lv_idx; // index of the variable on the stack |
| 111 | int lv_from_outer; // when TRUE using ctx_outer scope |
| 112 | int lv_const; // when TRUE cannot be assigned to |
| 113 | int lv_arg; // when TRUE this is an argument |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 114 | } lvar_T; |
| 115 | |
| 116 | /* |
| 117 | * Context for compiling lines of Vim script. |
| 118 | * Stores info about the local variables and condition stack. |
| 119 | */ |
| 120 | struct cctx_S { |
| 121 | ufunc_T *ctx_ufunc; // current function |
| 122 | int ctx_lnum; // line number in current function |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 123 | char_u *ctx_line_start; // start of current line or NULL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 124 | garray_T ctx_instr; // generated instructions |
| 125 | |
| 126 | garray_T ctx_locals; // currently visible local variables |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 127 | int ctx_locals_count; // total number of local variables |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 128 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 129 | int ctx_closure_count; // number of closures created in the |
| 130 | // function |
| 131 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 132 | garray_T ctx_imports; // imported items |
| 133 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 134 | skip_T ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 135 | scope_T *ctx_scope; // current scope, NULL at toplevel |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 136 | int ctx_had_return; // last seen statement was "return" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 137 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 138 | cctx_T *ctx_outer; // outer scope for lambda or nested |
| 139 | // function |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 140 | int ctx_outer_used; // var in ctx_outer was used |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 141 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 142 | garray_T ctx_type_stack; // type of each item on the stack |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 143 | garray_T *ctx_type_list; // list of pointers to allocated types |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static char e_var_notfound[] = N_("E1001: variable not found: %s"); |
| 147 | static char e_syntax_at[] = N_("E1002: Syntax error at %s"); |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 148 | static char e_used_as_arg[] = N_("E1006: %s is used as an argument"); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 149 | static char e_cannot_use_void[] = N_("E1031: Cannot use void value"); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 150 | static char e_namespace[] = N_("E1075: Namespace not supported: %s"); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 151 | static char e_unknown_var[] = N_("E1089: unknown variable: %s"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 152 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 153 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 154 | |
| 155 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 156 | * Lookup variable "name" in the local scope and return it. |
| 157 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 158 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 159 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 160 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 161 | { |
| 162 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 163 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 164 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 165 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 166 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 167 | |
| 168 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 169 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 170 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 171 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 172 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 173 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 174 | { |
| 175 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 176 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 177 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 178 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 179 | |
| 180 | // Find local in outer function scope. |
| 181 | if (cctx->ctx_outer != NULL) |
| 182 | { |
| 183 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 184 | if (lvar != NULL) |
| 185 | { |
| 186 | // TODO: are there situations we should not mark the outer scope as |
| 187 | // used? |
| 188 | cctx->ctx_outer_used = TRUE; |
| 189 | lvar->lv_from_outer = TRUE; |
| 190 | return lvar; |
| 191 | } |
| 192 | } |
| 193 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 194 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 198 | * Lookup an argument in the current function and an enclosing function. |
| 199 | * Returns the argument index in "idxp" |
| 200 | * Returns the argument type in "type" |
| 201 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 202 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 203 | */ |
| 204 | static int |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 205 | lookup_arg( |
| 206 | char_u *name, |
| 207 | size_t len, |
| 208 | int *idxp, |
| 209 | type_T **type, |
| 210 | int *gen_load_outer, |
| 211 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 212 | { |
| 213 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 214 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 215 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 216 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 217 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 218 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 219 | { |
| 220 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 221 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 222 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 223 | { |
| 224 | if (idxp != NULL) |
| 225 | { |
| 226 | // Arguments are located above the frame pointer. One further |
| 227 | // if there is a vararg argument |
| 228 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 229 | + STACK_FRAME_SIZE) |
| 230 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 231 | |
| 232 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 233 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 234 | else |
| 235 | *type = &t_any; |
| 236 | } |
| 237 | return OK; |
| 238 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 239 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 240 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 241 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 242 | if (va_name != NULL |
| 243 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 244 | { |
| 245 | if (idxp != NULL) |
| 246 | { |
| 247 | // varargs is always the last argument |
| 248 | *idxp = -STACK_FRAME_SIZE - 1; |
| 249 | *type = cctx->ctx_ufunc->uf_va_type; |
| 250 | } |
| 251 | return OK; |
| 252 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 253 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 254 | if (cctx->ctx_outer != NULL) |
| 255 | { |
| 256 | // Lookup the name for an argument of the outer function. |
| 257 | if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
| 258 | == OK) |
| 259 | { |
| 260 | *gen_load_outer = TRUE; |
| 261 | return OK; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Lookup a variable in the current script. |
| 270 | * Returns OK or FAIL. |
| 271 | */ |
| 272 | static int |
| 273 | lookup_script(char_u *name, size_t len) |
| 274 | { |
| 275 | int cc; |
| 276 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 277 | dictitem_T *di; |
| 278 | |
| 279 | cc = name[len]; |
| 280 | name[len] = NUL; |
| 281 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 282 | name[len] = cc; |
| 283 | return di == NULL ? FAIL: OK; |
| 284 | } |
| 285 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 286 | /* |
| 287 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 288 | * compilation context "cctx". |
| 289 | * Return FAIL and give an error if it defined. |
| 290 | */ |
| 291 | int |
Bram Moolenaar | cbb6bdc | 2020-07-06 21:53:17 +0200 | [diff] [blame] | 292 | check_defined(char_u *p, size_t len, cctx_T *cctx) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 293 | { |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 294 | int c = p[len]; |
| 295 | |
| 296 | p[len] = NUL; |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 297 | if (lookup_script(p, len) == OK |
| 298 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 299 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 300 | || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK)) |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 301 | || find_imported(p, len, cctx) != NULL |
| 302 | || find_func_even_dead(p, FALSE, cctx) != NULL) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 303 | { |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 304 | p[len] = c; |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 305 | semsg(_(e_name_already_defined), p); |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 306 | return FAIL; |
| 307 | } |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 308 | p[len] = c; |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 309 | return OK; |
| 310 | } |
| 311 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 312 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 313 | ///////////////////////////////////////////////////////////////////// |
| 314 | // Following generate_ functions expect the caller to call ga_grow(). |
| 315 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 316 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 317 | #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] | 318 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 319 | /* |
| 320 | * Generate an instruction without arguments. |
| 321 | * Returns a pointer to the new instruction, NULL if failed. |
| 322 | */ |
| 323 | static isn_T * |
| 324 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 325 | { |
| 326 | garray_T *instr = &cctx->ctx_instr; |
| 327 | isn_T *isn; |
| 328 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 329 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 330 | if (ga_grow(instr, 1) == FAIL) |
| 331 | return NULL; |
| 332 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 333 | isn->isn_type = isn_type; |
| 334 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 335 | ++instr->ga_len; |
| 336 | |
| 337 | return isn; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Generate an instruction without arguments. |
| 342 | * "drop" will be removed from the stack. |
| 343 | * Returns a pointer to the new instruction, NULL if failed. |
| 344 | */ |
| 345 | static isn_T * |
| 346 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 347 | { |
| 348 | garray_T *stack = &cctx->ctx_type_stack; |
| 349 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 350 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 351 | stack->ga_len -= drop; |
| 352 | return generate_instr(cctx, isn_type); |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 357 | */ |
| 358 | static isn_T * |
| 359 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 360 | { |
| 361 | isn_T *isn; |
| 362 | garray_T *stack = &cctx->ctx_type_stack; |
| 363 | |
| 364 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 365 | return NULL; |
| 366 | |
| 367 | if (ga_grow(stack, 1) == FAIL) |
| 368 | return NULL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 369 | ((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] | 370 | ++stack->ga_len; |
| 371 | |
| 372 | return isn; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * 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] | 377 | * But only for simple types. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 378 | */ |
| 379 | static int |
| 380 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 381 | { |
| 382 | isn_T *isn; |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 383 | isntype_T isntype = ISN_2STRING; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 384 | garray_T *stack = &cctx->ctx_type_stack; |
| 385 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 386 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 387 | switch ((*type)->tt_type) |
| 388 | { |
| 389 | // nothing to be done |
| 390 | case VAR_STRING: return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 391 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 392 | // conversion possible |
| 393 | case VAR_SPECIAL: |
| 394 | case VAR_BOOL: |
| 395 | case VAR_NUMBER: |
| 396 | case VAR_FLOAT: |
| 397 | break; |
| 398 | |
| 399 | // conversion possible (with runtime check) |
| 400 | case VAR_ANY: |
| 401 | case VAR_UNKNOWN: |
| 402 | isntype = ISN_2STRING_ANY; |
| 403 | break; |
| 404 | |
| 405 | // conversion not possible |
| 406 | case VAR_VOID: |
| 407 | case VAR_BLOB: |
| 408 | case VAR_FUNC: |
| 409 | case VAR_PARTIAL: |
| 410 | case VAR_LIST: |
| 411 | case VAR_DICT: |
| 412 | case VAR_JOB: |
| 413 | case VAR_CHANNEL: |
| 414 | to_string_error((*type)->tt_type); |
| 415 | return FAIL; |
| 416 | } |
| 417 | |
| 418 | *type = &t_string; |
| 419 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 420 | return FAIL; |
| 421 | isn->isn_arg.number = offset; |
| 422 | |
| 423 | return OK; |
| 424 | } |
| 425 | |
| 426 | static int |
| 427 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 428 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 429 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 430 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 431 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 432 | { |
| 433 | if (*op == '+') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 434 | emsg(_("E1051: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 435 | else |
| 436 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 437 | return FAIL; |
| 438 | } |
| 439 | return OK; |
| 440 | } |
| 441 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 442 | static int |
| 443 | generate_add_instr( |
| 444 | cctx_T *cctx, |
| 445 | vartype_T vartype, |
| 446 | type_T *type1, |
| 447 | type_T *type2) |
| 448 | { |
| 449 | isn_T *isn = generate_instr_drop(cctx, |
| 450 | vartype == VAR_NUMBER ? ISN_OPNR |
| 451 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 452 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 453 | #ifdef FEAT_FLOAT |
| 454 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 455 | #endif |
| 456 | : ISN_OPANY, 1); |
| 457 | |
| 458 | if (vartype != VAR_LIST && vartype != VAR_BLOB |
| 459 | && type1->tt_type != VAR_ANY |
| 460 | && type2->tt_type != VAR_ANY |
| 461 | && check_number_or_float( |
| 462 | type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL) |
| 463 | return FAIL; |
| 464 | |
| 465 | if (isn != NULL) |
| 466 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 467 | return isn == NULL ? FAIL : OK; |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * Get the type to use for an instruction for an operation on "type1" and |
| 472 | * "type2". If they are matching use a type-specific instruction. Otherwise |
| 473 | * fall back to runtime type checking. |
| 474 | */ |
| 475 | static vartype_T |
| 476 | operator_type(type_T *type1, type_T *type2) |
| 477 | { |
| 478 | if (type1->tt_type == type2->tt_type |
| 479 | && (type1->tt_type == VAR_NUMBER |
| 480 | || type1->tt_type == VAR_LIST |
| 481 | #ifdef FEAT_FLOAT |
| 482 | || type1->tt_type == VAR_FLOAT |
| 483 | #endif |
| 484 | || type1->tt_type == VAR_BLOB)) |
| 485 | return type1->tt_type; |
| 486 | return VAR_ANY; |
| 487 | } |
| 488 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 489 | /* |
| 490 | * Generate an instruction with two arguments. The instruction depends on the |
| 491 | * type of the arguments. |
| 492 | */ |
| 493 | static int |
| 494 | generate_two_op(cctx_T *cctx, char_u *op) |
| 495 | { |
| 496 | garray_T *stack = &cctx->ctx_type_stack; |
| 497 | type_T *type1; |
| 498 | type_T *type2; |
| 499 | vartype_T vartype; |
| 500 | isn_T *isn; |
| 501 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 502 | RETURN_OK_IF_SKIP(cctx); |
| 503 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 504 | // Get the known type of the two items on the stack. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 505 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 506 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 507 | vartype = operator_type(type1, type2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 508 | |
| 509 | switch (*op) |
| 510 | { |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 511 | case '+': |
| 512 | if (generate_add_instr(cctx, vartype, type1, type2) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 513 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 514 | break; |
| 515 | |
| 516 | case '-': |
| 517 | case '*': |
| 518 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 519 | op) == FAIL) |
| 520 | return FAIL; |
| 521 | if (vartype == VAR_NUMBER) |
| 522 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 523 | #ifdef FEAT_FLOAT |
| 524 | else if (vartype == VAR_FLOAT) |
| 525 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 526 | #endif |
| 527 | else |
| 528 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 529 | if (isn != NULL) |
| 530 | isn->isn_arg.op.op_type = *op == '*' |
| 531 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 532 | break; |
| 533 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 534 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 535 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 536 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 537 | && type2->tt_type != VAR_NUMBER)) |
| 538 | { |
| 539 | emsg(_("E1035: % requires number arguments")); |
| 540 | return FAIL; |
| 541 | } |
| 542 | isn = generate_instr_drop(cctx, |
| 543 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 544 | if (isn != NULL) |
| 545 | isn->isn_arg.op.op_type = EXPR_REM; |
| 546 | break; |
| 547 | } |
| 548 | |
| 549 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 550 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 551 | { |
| 552 | type_T *type = &t_any; |
| 553 | |
| 554 | #ifdef FEAT_FLOAT |
| 555 | // float+number and number+float results in float |
| 556 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 557 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 558 | type = &t_float; |
| 559 | #endif |
| 560 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 561 | } |
| 562 | |
| 563 | return OK; |
| 564 | } |
| 565 | |
| 566 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 567 | * Get the instruction to use for comparing "type1" with "type2" |
| 568 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 569 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 570 | static isntype_T |
| 571 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 572 | { |
| 573 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 574 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 575 | if (type1 == VAR_UNKNOWN) |
| 576 | type1 = VAR_ANY; |
| 577 | if (type2 == VAR_UNKNOWN) |
| 578 | type2 = VAR_ANY; |
| 579 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 580 | if (type1 == type2) |
| 581 | { |
| 582 | switch (type1) |
| 583 | { |
| 584 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 585 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 586 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 587 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 588 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 589 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 590 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 591 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 592 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 593 | default: isntype = ISN_COMPAREANY; break; |
| 594 | } |
| 595 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 596 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 597 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 598 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 599 | isntype = ISN_COMPAREANY; |
| 600 | |
| 601 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 602 | && (isntype == ISN_COMPAREBOOL |
| 603 | || isntype == ISN_COMPARESPECIAL |
| 604 | || isntype == ISN_COMPARENR |
| 605 | || isntype == ISN_COMPAREFLOAT)) |
| 606 | { |
| 607 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 608 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 609 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 610 | } |
| 611 | if (isntype == ISN_DROP |
| 612 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 613 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 614 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 615 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 616 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 617 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 618 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 619 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 620 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 621 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 622 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 623 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 624 | return isntype; |
| 625 | } |
| 626 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 627 | int |
| 628 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 629 | { |
| 630 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 631 | return FAIL; |
| 632 | return OK; |
| 633 | } |
| 634 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 635 | /* |
| 636 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 637 | */ |
| 638 | static int |
| 639 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 640 | { |
| 641 | isntype_T isntype; |
| 642 | isn_T *isn; |
| 643 | garray_T *stack = &cctx->ctx_type_stack; |
| 644 | vartype_T type1; |
| 645 | vartype_T type2; |
| 646 | |
| 647 | RETURN_OK_IF_SKIP(cctx); |
| 648 | |
| 649 | // Get the known type of the two items on the stack. If they are matching |
| 650 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 651 | // checking. |
| 652 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 653 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 654 | isntype = get_compare_isn(exptype, type1, type2); |
| 655 | if (isntype == ISN_DROP) |
| 656 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 657 | |
| 658 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 659 | return FAIL; |
| 660 | isn->isn_arg.op.op_type = exptype; |
| 661 | isn->isn_arg.op.op_ic = ic; |
| 662 | |
| 663 | // takes two arguments, puts one bool back |
| 664 | if (stack->ga_len >= 2) |
| 665 | { |
| 666 | --stack->ga_len; |
| 667 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 668 | } |
| 669 | |
| 670 | return OK; |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | * Generate an ISN_2BOOL instruction. |
| 675 | */ |
| 676 | static int |
| 677 | generate_2BOOL(cctx_T *cctx, int invert) |
| 678 | { |
| 679 | isn_T *isn; |
| 680 | garray_T *stack = &cctx->ctx_type_stack; |
| 681 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 682 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 683 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 684 | return FAIL; |
| 685 | isn->isn_arg.number = invert; |
| 686 | |
| 687 | // type becomes bool |
| 688 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 689 | |
| 690 | return OK; |
| 691 | } |
| 692 | |
| 693 | static int |
| 694 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 695 | { |
| 696 | isn_T *isn; |
| 697 | garray_T *stack = &cctx->ctx_type_stack; |
| 698 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 699 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 700 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 701 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 702 | // TODO: whole type, e.g. for a function also arg and return types |
| 703 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 704 | isn->isn_arg.type.ct_off = offset; |
| 705 | |
| 706 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 707 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 708 | |
| 709 | return OK; |
| 710 | } |
| 711 | |
| 712 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 713 | * Check that |
| 714 | * - "actual" is "expected" type or |
| 715 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 716 | * - return FAIL. |
| 717 | */ |
| 718 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 719 | need_type( |
| 720 | type_T *actual, |
| 721 | type_T *expected, |
| 722 | int offset, |
| 723 | cctx_T *cctx, |
| 724 | int silent) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 725 | { |
| 726 | if (check_type(expected, actual, FALSE) == OK) |
| 727 | return OK; |
| 728 | if (actual->tt_type != VAR_ANY |
| 729 | && actual->tt_type != VAR_UNKNOWN |
| 730 | && !(actual->tt_type == VAR_FUNC |
| 731 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 732 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 733 | if (!silent) |
| 734 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 735 | return FAIL; |
| 736 | } |
| 737 | generate_TYPECHECK(cctx, expected, offset); |
| 738 | return OK; |
| 739 | } |
| 740 | |
| 741 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 742 | * Generate an ISN_PUSHNR instruction. |
| 743 | */ |
| 744 | static int |
| 745 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 746 | { |
| 747 | isn_T *isn; |
| 748 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 749 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 750 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 751 | return FAIL; |
| 752 | isn->isn_arg.number = number; |
| 753 | |
| 754 | return OK; |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * Generate an ISN_PUSHBOOL instruction. |
| 759 | */ |
| 760 | static int |
| 761 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 762 | { |
| 763 | isn_T *isn; |
| 764 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 765 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 766 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 767 | return FAIL; |
| 768 | isn->isn_arg.number = number; |
| 769 | |
| 770 | return OK; |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Generate an ISN_PUSHSPEC instruction. |
| 775 | */ |
| 776 | static int |
| 777 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 778 | { |
| 779 | isn_T *isn; |
| 780 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 781 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 782 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 783 | return FAIL; |
| 784 | isn->isn_arg.number = number; |
| 785 | |
| 786 | return OK; |
| 787 | } |
| 788 | |
| 789 | #ifdef FEAT_FLOAT |
| 790 | /* |
| 791 | * Generate an ISN_PUSHF instruction. |
| 792 | */ |
| 793 | static int |
| 794 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 795 | { |
| 796 | isn_T *isn; |
| 797 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 798 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 799 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 800 | return FAIL; |
| 801 | isn->isn_arg.fnumber = fnumber; |
| 802 | |
| 803 | return OK; |
| 804 | } |
| 805 | #endif |
| 806 | |
| 807 | /* |
| 808 | * Generate an ISN_PUSHS instruction. |
| 809 | * Consumes "str". |
| 810 | */ |
| 811 | static int |
| 812 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 813 | { |
| 814 | isn_T *isn; |
| 815 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 816 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 817 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 818 | return FAIL; |
| 819 | isn->isn_arg.string = str; |
| 820 | |
| 821 | return OK; |
| 822 | } |
| 823 | |
| 824 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 825 | * Generate an ISN_PUSHCHANNEL instruction. |
| 826 | * Consumes "channel". |
| 827 | */ |
| 828 | static int |
| 829 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 830 | { |
| 831 | isn_T *isn; |
| 832 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 833 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 834 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 835 | return FAIL; |
| 836 | isn->isn_arg.channel = channel; |
| 837 | |
| 838 | return OK; |
| 839 | } |
| 840 | |
| 841 | /* |
| 842 | * Generate an ISN_PUSHJOB instruction. |
| 843 | * Consumes "job". |
| 844 | */ |
| 845 | static int |
| 846 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 847 | { |
| 848 | isn_T *isn; |
| 849 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 850 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 851 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 852 | return FAIL; |
| 853 | isn->isn_arg.job = job; |
| 854 | |
| 855 | return OK; |
| 856 | } |
| 857 | |
| 858 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 859 | * Generate an ISN_PUSHBLOB instruction. |
| 860 | * Consumes "blob". |
| 861 | */ |
| 862 | static int |
| 863 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 864 | { |
| 865 | isn_T *isn; |
| 866 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 867 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 868 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 869 | return FAIL; |
| 870 | isn->isn_arg.blob = blob; |
| 871 | |
| 872 | return OK; |
| 873 | } |
| 874 | |
| 875 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 876 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 877 | * Consumes "name". |
| 878 | */ |
| 879 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 880 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 881 | { |
| 882 | isn_T *isn; |
| 883 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 884 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 885 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 886 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 887 | isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 888 | |
| 889 | return OK; |
| 890 | } |
| 891 | |
| 892 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 893 | * Generate an ISN_GETITEM instruction with "index". |
| 894 | */ |
| 895 | static int |
| 896 | generate_GETITEM(cctx_T *cctx, int index) |
| 897 | { |
| 898 | isn_T *isn; |
| 899 | garray_T *stack = &cctx->ctx_type_stack; |
| 900 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 901 | type_T *item_type = &t_any; |
| 902 | |
| 903 | RETURN_OK_IF_SKIP(cctx); |
| 904 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 905 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 906 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 907 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 908 | emsg(_(e_listreq)); |
| 909 | return FAIL; |
| 910 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 911 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 912 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 913 | return FAIL; |
| 914 | isn->isn_arg.number = index; |
| 915 | |
| 916 | // add the item type to the type stack |
| 917 | if (ga_grow(stack, 1) == FAIL) |
| 918 | return FAIL; |
| 919 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 920 | ++stack->ga_len; |
| 921 | return OK; |
| 922 | } |
| 923 | |
| 924 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 925 | * Generate an ISN_SLICE instruction with "count". |
| 926 | */ |
| 927 | static int |
| 928 | generate_SLICE(cctx_T *cctx, int count) |
| 929 | { |
| 930 | isn_T *isn; |
| 931 | |
| 932 | RETURN_OK_IF_SKIP(cctx); |
| 933 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 934 | return FAIL; |
| 935 | isn->isn_arg.number = count; |
| 936 | return OK; |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 941 | */ |
| 942 | static int |
| 943 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 944 | { |
| 945 | isn_T *isn; |
| 946 | |
| 947 | RETURN_OK_IF_SKIP(cctx); |
| 948 | |
| 949 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 950 | return FAIL; |
| 951 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 952 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 953 | |
| 954 | return OK; |
| 955 | } |
| 956 | |
| 957 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 958 | * Generate an ISN_STORE instruction. |
| 959 | */ |
| 960 | static int |
| 961 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 962 | { |
| 963 | isn_T *isn; |
| 964 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 965 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 966 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 967 | return FAIL; |
| 968 | if (name != NULL) |
| 969 | isn->isn_arg.string = vim_strsave(name); |
| 970 | else |
| 971 | isn->isn_arg.number = idx; |
| 972 | |
| 973 | return OK; |
| 974 | } |
| 975 | |
| 976 | /* |
| 977 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 978 | */ |
| 979 | static int |
| 980 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 981 | { |
| 982 | isn_T *isn; |
| 983 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 984 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 985 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 986 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 987 | isn->isn_arg.storenr.stnr_idx = idx; |
| 988 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 989 | |
| 990 | return OK; |
| 991 | } |
| 992 | |
| 993 | /* |
| 994 | * Generate an ISN_STOREOPT instruction |
| 995 | */ |
| 996 | static int |
| 997 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 998 | { |
| 999 | isn_T *isn; |
| 1000 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1001 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1c199f9 | 2020-08-07 21:28:34 +0200 | [diff] [blame] | 1002 | if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1003 | return FAIL; |
| 1004 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1005 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1006 | |
| 1007 | return OK; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * Generate an ISN_LOAD or similar instruction. |
| 1012 | */ |
| 1013 | static int |
| 1014 | generate_LOAD( |
| 1015 | cctx_T *cctx, |
| 1016 | isntype_T isn_type, |
| 1017 | int idx, |
| 1018 | char_u *name, |
| 1019 | type_T *type) |
| 1020 | { |
| 1021 | isn_T *isn; |
| 1022 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1023 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1024 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1025 | return FAIL; |
| 1026 | if (name != NULL) |
| 1027 | isn->isn_arg.string = vim_strsave(name); |
| 1028 | else |
| 1029 | isn->isn_arg.number = idx; |
| 1030 | |
| 1031 | return OK; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1035 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1036 | */ |
| 1037 | static int |
| 1038 | generate_LOADV( |
| 1039 | cctx_T *cctx, |
| 1040 | char_u *name, |
| 1041 | int error) |
| 1042 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1043 | int di_flags; |
| 1044 | int vidx = find_vim_var(name, &di_flags); |
| 1045 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1046 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1047 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1048 | if (vidx < 0) |
| 1049 | { |
| 1050 | if (error) |
| 1051 | semsg(_(e_var_notfound), name); |
| 1052 | return FAIL; |
| 1053 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1054 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1055 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1056 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1060 | * Generate an ISN_UNLET instruction. |
| 1061 | */ |
| 1062 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1063 | 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] | 1064 | { |
| 1065 | isn_T *isn; |
| 1066 | |
| 1067 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1068 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1069 | return FAIL; |
| 1070 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1071 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1072 | |
| 1073 | return OK; |
| 1074 | } |
| 1075 | |
| 1076 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1077 | * Generate an ISN_LOADS instruction. |
| 1078 | */ |
| 1079 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1080 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1081 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1082 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1083 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1084 | int sid, |
| 1085 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1086 | { |
| 1087 | isn_T *isn; |
| 1088 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1089 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1090 | if (isn_type == ISN_LOADS) |
| 1091 | isn = generate_instr_type(cctx, isn_type, type); |
| 1092 | else |
| 1093 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1094 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1095 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1096 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1097 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1098 | |
| 1099 | return OK; |
| 1100 | } |
| 1101 | |
| 1102 | /* |
| 1103 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1104 | */ |
| 1105 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1106 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1107 | cctx_T *cctx, |
| 1108 | isntype_T isn_type, |
| 1109 | int sid, |
| 1110 | int idx, |
| 1111 | type_T *type) |
| 1112 | { |
| 1113 | isn_T *isn; |
| 1114 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1115 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1116 | if (isn_type == ISN_LOADSCRIPT) |
| 1117 | isn = generate_instr_type(cctx, isn_type, type); |
| 1118 | else |
| 1119 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1120 | if (isn == NULL) |
| 1121 | return FAIL; |
| 1122 | isn->isn_arg.script.script_sid = sid; |
| 1123 | isn->isn_arg.script.script_idx = idx; |
| 1124 | return OK; |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * Generate an ISN_NEWLIST instruction. |
| 1129 | */ |
| 1130 | static int |
| 1131 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1132 | { |
| 1133 | isn_T *isn; |
| 1134 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1135 | type_T *type; |
| 1136 | type_T *member; |
| 1137 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1138 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1139 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1140 | return FAIL; |
| 1141 | isn->isn_arg.number = count; |
| 1142 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1143 | // get the member type from all the items on the stack. |
| 1144 | member = get_member_type_from_stack( |
| 1145 | ((type_T **)stack->ga_data) + stack->ga_len, count, 1, |
| 1146 | cctx->ctx_type_list); |
| 1147 | type = get_list_type(member, cctx->ctx_type_list); |
| 1148 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1149 | // drop the value types |
| 1150 | stack->ga_len -= count; |
| 1151 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1152 | // add the list type to the type stack |
| 1153 | if (ga_grow(stack, 1) == FAIL) |
| 1154 | return FAIL; |
| 1155 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1156 | ++stack->ga_len; |
| 1157 | |
| 1158 | return OK; |
| 1159 | } |
| 1160 | |
| 1161 | /* |
| 1162 | * Generate an ISN_NEWDICT instruction. |
| 1163 | */ |
| 1164 | static int |
| 1165 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1166 | { |
| 1167 | isn_T *isn; |
| 1168 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1169 | type_T *type; |
| 1170 | type_T *member; |
| 1171 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1172 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1173 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1174 | return FAIL; |
| 1175 | isn->isn_arg.number = count; |
| 1176 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1177 | member = get_member_type_from_stack( |
| 1178 | ((type_T **)stack->ga_data) + stack->ga_len, count, 2, |
| 1179 | cctx->ctx_type_list); |
| 1180 | type = get_dict_type(member, cctx->ctx_type_list); |
| 1181 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1182 | // drop the key and value types |
| 1183 | stack->ga_len -= 2 * count; |
| 1184 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1185 | // add the dict type to the type stack |
| 1186 | if (ga_grow(stack, 1) == FAIL) |
| 1187 | return FAIL; |
| 1188 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1189 | ++stack->ga_len; |
| 1190 | |
| 1191 | return OK; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * Generate an ISN_FUNCREF instruction. |
| 1196 | */ |
| 1197 | static int |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1198 | generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1199 | { |
| 1200 | isn_T *isn; |
| 1201 | garray_T *stack = &cctx->ctx_type_stack; |
| 1202 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1203 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1204 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1205 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1206 | isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1207 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1208 | |
| 1209 | if (ga_grow(stack, 1) == FAIL) |
| 1210 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1211 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 1212 | ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1213 | ++stack->ga_len; |
| 1214 | |
| 1215 | return OK; |
| 1216 | } |
| 1217 | |
| 1218 | /* |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1219 | * Generate an ISN_NEWFUNC instruction. |
| 1220 | */ |
| 1221 | static int |
| 1222 | generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) |
| 1223 | { |
| 1224 | isn_T *isn; |
| 1225 | char_u *name; |
| 1226 | |
| 1227 | RETURN_OK_IF_SKIP(cctx); |
| 1228 | name = vim_strsave(lambda_name); |
| 1229 | if (name == NULL) |
| 1230 | return FAIL; |
| 1231 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
| 1232 | return FAIL; |
| 1233 | isn->isn_arg.newfunc.nf_lambda = name; |
| 1234 | isn->isn_arg.newfunc.nf_global = func_name; |
| 1235 | |
| 1236 | return OK; |
| 1237 | } |
| 1238 | |
| 1239 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1240 | * Generate an ISN_JUMP instruction. |
| 1241 | */ |
| 1242 | static int |
| 1243 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1244 | { |
| 1245 | isn_T *isn; |
| 1246 | garray_T *stack = &cctx->ctx_type_stack; |
| 1247 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1248 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1249 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1250 | return FAIL; |
| 1251 | isn->isn_arg.jump.jump_when = when; |
| 1252 | isn->isn_arg.jump.jump_where = where; |
| 1253 | |
| 1254 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1255 | --stack->ga_len; |
| 1256 | |
| 1257 | return OK; |
| 1258 | } |
| 1259 | |
| 1260 | static int |
| 1261 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1262 | { |
| 1263 | isn_T *isn; |
| 1264 | garray_T *stack = &cctx->ctx_type_stack; |
| 1265 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1266 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1267 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1268 | return FAIL; |
| 1269 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1270 | |
| 1271 | if (ga_grow(stack, 1) == FAIL) |
| 1272 | return FAIL; |
| 1273 | // type doesn't matter, will be stored next |
| 1274 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1275 | ++stack->ga_len; |
| 1276 | |
| 1277 | return OK; |
| 1278 | } |
| 1279 | |
| 1280 | /* |
| 1281 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1282 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1283 | * Return FAIL if the number of arguments is wrong. |
| 1284 | */ |
| 1285 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1286 | 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] | 1287 | { |
| 1288 | isn_T *isn; |
| 1289 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1290 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1291 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1292 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1293 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1294 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1295 | argoff = check_internal_func(func_idx, argcount); |
| 1296 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1297 | return FAIL; |
| 1298 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1299 | if (method_call && argoff > 1) |
| 1300 | { |
| 1301 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1302 | return FAIL; |
| 1303 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1304 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1305 | } |
| 1306 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1307 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1308 | return FAIL; |
| 1309 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1310 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1311 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1312 | for (i = 0; i < argcount; ++i) |
| 1313 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1314 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1315 | stack->ga_len -= argcount; // drop the arguments |
| 1316 | if (ga_grow(stack, 1) == FAIL) |
| 1317 | return FAIL; |
| 1318 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1319 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1320 | ++stack->ga_len; // add return value |
| 1321 | |
| 1322 | return OK; |
| 1323 | } |
| 1324 | |
| 1325 | /* |
| 1326 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1327 | * Return FAIL if the number of arguments is wrong. |
| 1328 | */ |
| 1329 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1330 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1331 | { |
| 1332 | isn_T *isn; |
| 1333 | garray_T *stack = &cctx->ctx_type_stack; |
| 1334 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1335 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1336 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1337 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1338 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1339 | { |
| 1340 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1341 | return FAIL; |
| 1342 | } |
| 1343 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1344 | { |
| 1345 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1346 | return FAIL; |
| 1347 | } |
| 1348 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1349 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1350 | { |
| 1351 | int i; |
| 1352 | |
| 1353 | for (i = 0; i < argcount; ++i) |
| 1354 | { |
| 1355 | type_T *expected; |
| 1356 | type_T *actual; |
| 1357 | |
| 1358 | if (i < regular_args) |
| 1359 | { |
| 1360 | if (ufunc->uf_arg_types == NULL) |
| 1361 | continue; |
| 1362 | expected = ufunc->uf_arg_types[i]; |
| 1363 | } |
| 1364 | else |
| 1365 | expected = ufunc->uf_va_type->tt_member; |
| 1366 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1367 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1368 | { |
| 1369 | arg_type_mismatch(expected, actual, i + 1); |
| 1370 | return FAIL; |
| 1371 | } |
| 1372 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1373 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1374 | if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL) |
| 1375 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1376 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1377 | } |
| 1378 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1379 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1380 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1381 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1382 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1383 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1384 | { |
| 1385 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1386 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1387 | } |
| 1388 | else |
| 1389 | { |
| 1390 | // A user function may be deleted and redefined later, can't use the |
| 1391 | // ufunc pointer, need to look it up again at runtime. |
| 1392 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1393 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1394 | } |
| 1395 | |
| 1396 | stack->ga_len -= argcount; // drop the arguments |
| 1397 | if (ga_grow(stack, 1) == FAIL) |
| 1398 | return FAIL; |
| 1399 | // add return value |
| 1400 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1401 | ++stack->ga_len; |
| 1402 | |
| 1403 | return OK; |
| 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1408 | */ |
| 1409 | static int |
| 1410 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1411 | { |
| 1412 | isn_T *isn; |
| 1413 | garray_T *stack = &cctx->ctx_type_stack; |
| 1414 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1415 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1416 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1417 | return FAIL; |
| 1418 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1419 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1420 | |
| 1421 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1422 | if (ga_grow(stack, 1) == FAIL) |
| 1423 | return FAIL; |
| 1424 | // add return value |
| 1425 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1426 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1427 | |
| 1428 | return OK; |
| 1429 | } |
| 1430 | |
| 1431 | /* |
| 1432 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1433 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1434 | */ |
| 1435 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1436 | generate_PCALL( |
| 1437 | cctx_T *cctx, |
| 1438 | int argcount, |
| 1439 | char_u *name, |
| 1440 | type_T *type, |
| 1441 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1442 | { |
| 1443 | isn_T *isn; |
| 1444 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1445 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1446 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1447 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1448 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1449 | if (type->tt_type == VAR_ANY) |
| 1450 | ret_type = &t_any; |
| 1451 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1452 | { |
| 1453 | if (type->tt_argcount != -1) |
| 1454 | { |
| 1455 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1456 | |
| 1457 | if (argcount < type->tt_min_argcount - varargs) |
| 1458 | { |
| 1459 | semsg(_(e_toofewarg), "[reference]"); |
| 1460 | return FAIL; |
| 1461 | } |
| 1462 | if (!varargs && argcount > type->tt_argcount) |
| 1463 | { |
| 1464 | semsg(_(e_toomanyarg), "[reference]"); |
| 1465 | return FAIL; |
| 1466 | } |
| 1467 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1468 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1469 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1470 | else |
| 1471 | { |
| 1472 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1473 | return FAIL; |
| 1474 | } |
| 1475 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1476 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1477 | return FAIL; |
| 1478 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1479 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1480 | |
| 1481 | stack->ga_len -= argcount; // drop the arguments |
| 1482 | |
| 1483 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1484 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1485 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1486 | // If partial is above the arguments it must be cleared and replaced with |
| 1487 | // the return value. |
| 1488 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1489 | return FAIL; |
| 1490 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1491 | return OK; |
| 1492 | } |
| 1493 | |
| 1494 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1495 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1496 | */ |
| 1497 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1498 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1499 | { |
| 1500 | isn_T *isn; |
| 1501 | garray_T *stack = &cctx->ctx_type_stack; |
| 1502 | type_T *type; |
| 1503 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1504 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1505 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1506 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1507 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1508 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1509 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1510 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1511 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1512 | { |
| 1513 | emsg(_(e_dictreq)); |
| 1514 | return FAIL; |
| 1515 | } |
| 1516 | // change dict type to dict member type |
| 1517 | if (type->tt_type == VAR_DICT) |
| 1518 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1519 | |
| 1520 | return OK; |
| 1521 | } |
| 1522 | |
| 1523 | /* |
| 1524 | * Generate an ISN_ECHO instruction. |
| 1525 | */ |
| 1526 | static int |
| 1527 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1528 | { |
| 1529 | isn_T *isn; |
| 1530 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1531 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1532 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1533 | return FAIL; |
| 1534 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1535 | isn->isn_arg.echo.echo_count = count; |
| 1536 | |
| 1537 | return OK; |
| 1538 | } |
| 1539 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1540 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1541 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1542 | */ |
| 1543 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1544 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1545 | { |
| 1546 | isn_T *isn; |
| 1547 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1548 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1549 | return FAIL; |
| 1550 | isn->isn_arg.number = count; |
| 1551 | |
| 1552 | return OK; |
| 1553 | } |
| 1554 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1555 | static int |
| 1556 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1557 | { |
| 1558 | isn_T *isn; |
| 1559 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1560 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1561 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1562 | return FAIL; |
| 1563 | isn->isn_arg.string = vim_strsave(line); |
| 1564 | return OK; |
| 1565 | } |
| 1566 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1567 | static int |
| 1568 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1569 | { |
| 1570 | isn_T *isn; |
| 1571 | |
| 1572 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1573 | return FAIL; |
| 1574 | isn->isn_arg.number = count; |
| 1575 | return OK; |
| 1576 | } |
| 1577 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1578 | /* |
| 1579 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1580 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1581 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1582 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1583 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1584 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1585 | lvar_T *lvar; |
| 1586 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1587 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1588 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1589 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1590 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1594 | return NULL; |
| 1595 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1596 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1597 | // Every local variable uses the next entry on the stack. We could re-use |
| 1598 | // the last ones when leaving a scope, but then variables used in a closure |
| 1599 | // might get overwritten. To keep things simple do not re-use stack |
| 1600 | // entries. This is less efficient, but memory is cheap these days. |
| 1601 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1602 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1603 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1604 | lvar->lv_const = isConst; |
| 1605 | lvar->lv_type = type; |
| 1606 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1607 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1611 | * Remove local variables above "new_top". |
| 1612 | */ |
| 1613 | static void |
| 1614 | unwind_locals(cctx_T *cctx, int new_top) |
| 1615 | { |
| 1616 | if (cctx->ctx_locals.ga_len > new_top) |
| 1617 | { |
| 1618 | int idx; |
| 1619 | lvar_T *lvar; |
| 1620 | |
| 1621 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1622 | { |
| 1623 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1624 | vim_free(lvar->lv_name); |
| 1625 | } |
| 1626 | } |
| 1627 | cctx->ctx_locals.ga_len = new_top; |
| 1628 | } |
| 1629 | |
| 1630 | /* |
| 1631 | * Free all local variables. |
| 1632 | */ |
| 1633 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1634 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1635 | { |
| 1636 | unwind_locals(cctx, 0); |
| 1637 | ga_clear(&cctx->ctx_locals); |
| 1638 | } |
| 1639 | |
| 1640 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1641 | * Find "name" in script-local items of script "sid". |
| 1642 | * Returns the index in "sn_var_vals" if found. |
| 1643 | * If found but not in "sn_var_vals" returns -1. |
| 1644 | * If not found returns -2. |
| 1645 | */ |
| 1646 | int |
| 1647 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 1648 | { |
| 1649 | hashtab_T *ht; |
| 1650 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1651 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1652 | int idx; |
| 1653 | |
| 1654 | // First look the name up in the hashtable. |
| 1655 | if (sid <= 0 || sid > script_items.ga_len) |
| 1656 | return -1; |
| 1657 | ht = &SCRIPT_VARS(sid); |
| 1658 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 1659 | if (di == NULL) |
| 1660 | return -2; |
| 1661 | |
| 1662 | // Now find the svar_T index in sn_var_vals. |
| 1663 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 1664 | { |
| 1665 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1666 | |
| 1667 | if (sv->sv_tv == &di->di_tv) |
| 1668 | { |
| 1669 | if (check_writable && sv->sv_const) |
| 1670 | semsg(_(e_readonlyvar), name); |
| 1671 | return idx; |
| 1672 | } |
| 1673 | } |
| 1674 | return -1; |
| 1675 | } |
| 1676 | |
| 1677 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 1678 | * Find "name" in imported items of the current script or in "cctx" if not |
| 1679 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1680 | */ |
| 1681 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1682 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1683 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1684 | int idx; |
| 1685 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 1686 | if (current_sctx.sc_sid <= 0) |
| 1687 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1688 | if (cctx != NULL) |
| 1689 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1690 | { |
| 1691 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 1692 | + idx; |
| 1693 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1694 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1695 | : STRLEN(import->imp_name) == len |
| 1696 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1697 | return import; |
| 1698 | } |
| 1699 | |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 1700 | return find_imported_in_script(name, len, current_sctx.sc_sid); |
| 1701 | } |
| 1702 | |
| 1703 | imported_T * |
| 1704 | find_imported_in_script(char_u *name, size_t len, int sid) |
| 1705 | { |
| 1706 | scriptitem_T *si = SCRIPT_ITEM(sid); |
| 1707 | int idx; |
| 1708 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1709 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 1710 | { |
| 1711 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 1712 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1713 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1714 | : STRLEN(import->imp_name) == len |
| 1715 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1716 | return import; |
| 1717 | } |
| 1718 | return NULL; |
| 1719 | } |
| 1720 | |
| 1721 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1722 | * Free all imported variables. |
| 1723 | */ |
| 1724 | static void |
| 1725 | free_imported(cctx_T *cctx) |
| 1726 | { |
| 1727 | int idx; |
| 1728 | |
| 1729 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1730 | { |
| 1731 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 1732 | |
| 1733 | vim_free(import->imp_name); |
| 1734 | } |
| 1735 | ga_clear(&cctx->ctx_imports); |
| 1736 | } |
| 1737 | |
| 1738 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1739 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 1740 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 1741 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1742 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1743 | { |
| 1744 | return p[0] == '#' && p[1] != '{'; |
| 1745 | } |
| 1746 | |
| 1747 | /* |
| 1748 | * Return a pointer to the next line that isn't empty or only contains a |
| 1749 | * comment. Skips over white space. |
| 1750 | * Returns NULL if there is none. |
| 1751 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1752 | char_u * |
| 1753 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1754 | { |
| 1755 | int lnum = cctx->ctx_lnum; |
| 1756 | |
| 1757 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 1758 | { |
| 1759 | 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] | 1760 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1761 | |
Bram Moolenaar | ba60cc4 | 2020-08-12 19:15:33 +0200 | [diff] [blame] | 1762 | // ignore NULLs inserted for continuation lines |
| 1763 | if (line != NULL) |
| 1764 | { |
| 1765 | p = skipwhite(line); |
| 1766 | if (*p != NUL && !vim9_comment_start(p)) |
| 1767 | return p; |
| 1768 | } |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1769 | } |
| 1770 | return NULL; |
| 1771 | } |
| 1772 | |
| 1773 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1774 | * Called when checking for a following operator at "arg". When the rest of |
| 1775 | * the line is empty or only a comment, peek the next line. If there is a next |
| 1776 | * line return a pointer to it and set "nextp". |
| 1777 | * Otherwise skip over white space. |
| 1778 | */ |
| 1779 | static char_u * |
| 1780 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 1781 | { |
| 1782 | char_u *p = skipwhite(arg); |
| 1783 | |
| 1784 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1785 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1786 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1787 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1788 | if (*nextp != NULL) |
| 1789 | return *nextp; |
| 1790 | } |
| 1791 | return p; |
| 1792 | } |
| 1793 | |
| 1794 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1795 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1796 | * 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] | 1797 | * Returns NULL when at the end. |
| 1798 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1799 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1800 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1801 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1802 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1803 | |
| 1804 | do |
| 1805 | { |
| 1806 | ++cctx->ctx_lnum; |
| 1807 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1808 | { |
| 1809 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1810 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1811 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1812 | 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] | 1813 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 1814 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1815 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 1816 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1817 | return line; |
| 1818 | } |
| 1819 | |
| 1820 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1821 | * 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] | 1822 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1823 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 1824 | */ |
| 1825 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 1826 | 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] | 1827 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1828 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1829 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1830 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1831 | |
| 1832 | if (next == NULL) |
| 1833 | return FAIL; |
| 1834 | *arg = skipwhite(next); |
| 1835 | } |
| 1836 | return OK; |
| 1837 | } |
| 1838 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 1839 | /* |
| 1840 | * Idem, and give an error when failed. |
| 1841 | */ |
| 1842 | static int |
| 1843 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 1844 | { |
| 1845 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 1846 | { |
| 1847 | emsg(_("E1097: line incomplete")); |
| 1848 | return FAIL; |
| 1849 | } |
| 1850 | return OK; |
| 1851 | } |
| 1852 | |
| 1853 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1854 | // Structure passed between the compile_expr* functions to keep track of |
| 1855 | // constants that have been parsed but for which no code was produced yet. If |
| 1856 | // possible expressions on these constants are applied at compile time. If |
| 1857 | // that is not possible, the code to push the constants needs to be generated |
| 1858 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1859 | // Using 50 should be more than enough of 5 levels of (). |
| 1860 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1861 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1862 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1863 | int pp_used; // active entries in pp_tv[] |
| 1864 | } ppconst_T; |
| 1865 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1866 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 1867 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 1868 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1869 | /* |
| 1870 | * Generate a PUSH instruction for "tv". |
| 1871 | * "tv" will be consumed or cleared. |
| 1872 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 1873 | */ |
| 1874 | static int |
| 1875 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 1876 | { |
| 1877 | if (tv != NULL) |
| 1878 | { |
| 1879 | switch (tv->v_type) |
| 1880 | { |
| 1881 | case VAR_UNKNOWN: |
| 1882 | break; |
| 1883 | case VAR_BOOL: |
| 1884 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 1885 | break; |
| 1886 | case VAR_SPECIAL: |
| 1887 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 1888 | break; |
| 1889 | case VAR_NUMBER: |
| 1890 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 1891 | break; |
| 1892 | #ifdef FEAT_FLOAT |
| 1893 | case VAR_FLOAT: |
| 1894 | generate_PUSHF(cctx, tv->vval.v_float); |
| 1895 | break; |
| 1896 | #endif |
| 1897 | case VAR_BLOB: |
| 1898 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 1899 | tv->vval.v_blob = NULL; |
| 1900 | break; |
| 1901 | case VAR_STRING: |
| 1902 | generate_PUSHS(cctx, tv->vval.v_string); |
| 1903 | tv->vval.v_string = NULL; |
| 1904 | break; |
| 1905 | default: |
| 1906 | iemsg("constant type not supported"); |
| 1907 | clear_tv(tv); |
| 1908 | return FAIL; |
| 1909 | } |
| 1910 | tv->v_type = VAR_UNKNOWN; |
| 1911 | } |
| 1912 | return OK; |
| 1913 | } |
| 1914 | |
| 1915 | /* |
| 1916 | * Generate code for any ppconst entries. |
| 1917 | */ |
| 1918 | static int |
| 1919 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 1920 | { |
| 1921 | int i; |
| 1922 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1923 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1924 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 1925 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1926 | for (i = 0; i < ppconst->pp_used; ++i) |
| 1927 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 1928 | ret = FAIL; |
| 1929 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1930 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1931 | return ret; |
| 1932 | } |
| 1933 | |
| 1934 | /* |
| 1935 | * Clear ppconst constants. Used when failing. |
| 1936 | */ |
| 1937 | static void |
| 1938 | clear_ppconst(ppconst_T *ppconst) |
| 1939 | { |
| 1940 | int i; |
| 1941 | |
| 1942 | for (i = 0; i < ppconst->pp_used; ++i) |
| 1943 | clear_tv(&ppconst->pp_tv[i]); |
| 1944 | ppconst->pp_used = 0; |
| 1945 | } |
| 1946 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1947 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1948 | * Generate an instruction to load script-local variable "name", without the |
| 1949 | * leading "s:". |
| 1950 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1951 | */ |
| 1952 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1953 | compile_load_scriptvar( |
| 1954 | cctx_T *cctx, |
| 1955 | char_u *name, // variable NUL terminated |
| 1956 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 1957 | char_u **end, // end of variable |
| 1958 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1959 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1960 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1961 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 1962 | imported_T *import; |
| 1963 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 1964 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1965 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 1966 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1967 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 1968 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1969 | } |
| 1970 | if (idx >= 0) |
| 1971 | { |
| 1972 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1973 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1974 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1975 | current_sctx.sc_sid, idx, sv->sv_type); |
| 1976 | return OK; |
| 1977 | } |
| 1978 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1979 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1980 | if (import != NULL) |
| 1981 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1982 | if (import->imp_all) |
| 1983 | { |
| 1984 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 1985 | char_u *exp_name; |
| 1986 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1987 | ufunc_T *ufunc; |
| 1988 | type_T *type; |
| 1989 | |
| 1990 | // Used "import * as Name", need to lookup the member. |
| 1991 | if (*p != '.') |
| 1992 | { |
| 1993 | semsg(_("E1060: expected dot after name: %s"), start); |
| 1994 | return FAIL; |
| 1995 | } |
| 1996 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1997 | if (VIM_ISWHITE(*p)) |
| 1998 | { |
| 1999 | emsg(_("E1074: no white space allowed after dot")); |
| 2000 | return FAIL; |
| 2001 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2002 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2003 | // isolate one name |
| 2004 | exp_name = p; |
| 2005 | while (eval_isnamec(*p)) |
| 2006 | ++p; |
| 2007 | cc = *p; |
| 2008 | *p = NUL; |
| 2009 | |
| 2010 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2011 | *p = cc; |
| 2012 | p = skipwhite(p); |
| 2013 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2014 | // TODO: what if it is a function? |
| 2015 | if (idx < 0) |
| 2016 | return FAIL; |
| 2017 | *end = p; |
| 2018 | |
| 2019 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2020 | import->imp_sid, |
| 2021 | idx, |
| 2022 | type); |
| 2023 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2024 | else if (import->imp_funcname != NULL) |
| 2025 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2026 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2027 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2028 | import->imp_sid, |
| 2029 | import->imp_var_vals_idx, |
| 2030 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2031 | return OK; |
| 2032 | } |
| 2033 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2034 | if (error) |
| 2035 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2036 | return FAIL; |
| 2037 | } |
| 2038 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2039 | static int |
| 2040 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2041 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2042 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2043 | |
| 2044 | if (ufunc == NULL) |
| 2045 | return FAIL; |
| 2046 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2047 | // Need to compile any default values to get the argument types. |
| 2048 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2049 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2050 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2051 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2052 | } |
| 2053 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2054 | /* |
| 2055 | * Compile a variable name into a load instruction. |
| 2056 | * "end" points to just after the name. |
| 2057 | * When "error" is FALSE do not give an error when not found. |
| 2058 | */ |
| 2059 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2060 | compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2061 | { |
| 2062 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2063 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2064 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2065 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2066 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2067 | |
| 2068 | if (*(*arg + 1) == ':') |
| 2069 | { |
| 2070 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2071 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2072 | { |
| 2073 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2074 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2075 | switch (**arg) |
| 2076 | { |
| 2077 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2078 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2079 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2080 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2081 | default: |
| 2082 | semsg(_(e_namespace), *arg); |
| 2083 | goto theend; |
| 2084 | } |
| 2085 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2086 | goto theend; |
| 2087 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2088 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2089 | else |
| 2090 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2091 | isntype_T isn_type = ISN_DROP; |
| 2092 | |
| 2093 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2094 | if (name == NULL) |
| 2095 | return FAIL; |
| 2096 | |
| 2097 | switch (**arg) |
| 2098 | { |
| 2099 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2100 | break; |
| 2101 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2102 | NULL, NULL, error); |
| 2103 | break; |
| 2104 | case 'g': isn_type = ISN_LOADG; break; |
| 2105 | case 'w': isn_type = ISN_LOADW; break; |
| 2106 | case 't': isn_type = ISN_LOADT; break; |
| 2107 | case 'b': isn_type = ISN_LOADB; break; |
| 2108 | default: semsg(_(e_namespace), *arg); |
| 2109 | goto theend; |
| 2110 | } |
| 2111 | if (isn_type != ISN_DROP) |
| 2112 | { |
| 2113 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2114 | // variables can be defined later, thus we don't check if it |
| 2115 | // exists, give error at runtime. |
| 2116 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2117 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2118 | } |
| 2119 | } |
| 2120 | else |
| 2121 | { |
| 2122 | size_t len = end - *arg; |
| 2123 | int idx; |
| 2124 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2125 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2126 | |
| 2127 | name = vim_strnsave(*arg, end - *arg); |
| 2128 | if (name == NULL) |
| 2129 | return FAIL; |
| 2130 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2131 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2132 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2133 | if (!gen_load_outer) |
| 2134 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2135 | } |
| 2136 | else |
| 2137 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2138 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2139 | |
| 2140 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2141 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2142 | type = lvar->lv_type; |
| 2143 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2144 | if (lvar->lv_from_outer) |
| 2145 | gen_load_outer = TRUE; |
| 2146 | else |
| 2147 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2148 | } |
| 2149 | else |
| 2150 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2151 | // "var" can be script-local even without using "s:" if it |
| 2152 | // already exists. |
| 2153 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2154 | == SCRIPT_VERSION_VIM9 |
| 2155 | || lookup_script(*arg, len) == OK) |
| 2156 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2157 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2158 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2159 | // When the name starts with an uppercase letter or "x:" it |
| 2160 | // can be a user defined function. |
| 2161 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2162 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2163 | } |
| 2164 | } |
| 2165 | if (gen_load) |
| 2166 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2167 | if (gen_load_outer) |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2168 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2169 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2170 | cctx->ctx_outer_used = TRUE; |
| 2171 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | *arg = end; |
| 2175 | |
| 2176 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2177 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2178 | semsg(_(e_var_notfound), name); |
| 2179 | vim_free(name); |
| 2180 | return res; |
| 2181 | } |
| 2182 | |
| 2183 | /* |
| 2184 | * Compile the argument expressions. |
| 2185 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2186 | */ |
| 2187 | static int |
| 2188 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2189 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2190 | char_u *p = *arg; |
| 2191 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2192 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2193 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2194 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2195 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2196 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2197 | if (*p == ')') |
| 2198 | { |
| 2199 | *arg = p + 1; |
| 2200 | return OK; |
| 2201 | } |
| 2202 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2203 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2204 | return FAIL; |
| 2205 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2206 | |
| 2207 | if (*p != ',' && *skipwhite(p) == ',') |
| 2208 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 2209 | semsg(_(e_no_white_space_allowed_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2210 | p = skipwhite(p); |
| 2211 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2212 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2213 | { |
| 2214 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2215 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 2216 | semsg(_(e_white_space_required_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2217 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2218 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2219 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2220 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2221 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2222 | emsg(_(e_missing_close)); |
| 2223 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | /* |
| 2227 | * Compile a function call: name(arg1, arg2) |
| 2228 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2229 | * "argcount_init" is 1 for "value->method()" |
| 2230 | * Instructions: |
| 2231 | * EVAL arg1 |
| 2232 | * EVAL arg2 |
| 2233 | * BCALL / DCALL / UCALL |
| 2234 | */ |
| 2235 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2236 | compile_call( |
| 2237 | char_u **arg, |
| 2238 | size_t varlen, |
| 2239 | cctx_T *cctx, |
| 2240 | ppconst_T *ppconst, |
| 2241 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2242 | { |
| 2243 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2244 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2245 | int argcount = argcount_init; |
| 2246 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2247 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2248 | char_u *tofree = NULL; |
| 2249 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2250 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2251 | int res = FAIL; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2252 | int is_autoload; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2253 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2254 | // we can evaluate "has('name')" at compile time |
| 2255 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2256 | { |
| 2257 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2258 | typval_T argvars[2]; |
| 2259 | |
| 2260 | argvars[0].v_type = VAR_UNKNOWN; |
| 2261 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2262 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2263 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2264 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2265 | s = skipwhite(s); |
| 2266 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 2267 | { |
| 2268 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2269 | |
| 2270 | *arg = s + 1; |
| 2271 | argvars[1].v_type = VAR_UNKNOWN; |
| 2272 | tv->v_type = VAR_NUMBER; |
| 2273 | tv->vval.v_number = 0; |
| 2274 | f_has(argvars, tv); |
| 2275 | clear_tv(&argvars[0]); |
| 2276 | ++ppconst->pp_used; |
| 2277 | return OK; |
| 2278 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2279 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2283 | return FAIL; |
| 2284 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2285 | if (varlen >= sizeof(namebuf)) |
| 2286 | { |
| 2287 | semsg(_("E1011: name too long: %s"), name); |
| 2288 | return FAIL; |
| 2289 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2290 | vim_strncpy(namebuf, *arg, varlen); |
| 2291 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2292 | |
| 2293 | *arg = skipwhite(*arg + varlen + 1); |
| 2294 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2295 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2296 | |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2297 | is_autoload = vim_strchr(name, '#') != NULL; |
| 2298 | if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2299 | { |
| 2300 | int idx; |
| 2301 | |
| 2302 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2303 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2304 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 2305 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2306 | else |
| 2307 | semsg(_(e_unknownfunc), namebuf); |
| 2308 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2309 | } |
| 2310 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2311 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2312 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2313 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2314 | { |
| 2315 | res = generate_CALL(cctx, ufunc, argcount); |
| 2316 | goto theend; |
| 2317 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2318 | |
| 2319 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2320 | // 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] | 2321 | // Not for eome#Func(), it will be loaded later. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2322 | p = namebuf; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2323 | if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2324 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2325 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2326 | garray_T *stack = &cctx->ctx_type_stack; |
| 2327 | type_T *type; |
| 2328 | |
| 2329 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2330 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2331 | goto theend; |
| 2332 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2333 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2334 | // 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] | 2335 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2336 | if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload) |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2337 | res = generate_UCALL(cctx, name, argcount); |
| 2338 | else |
| 2339 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2340 | |
| 2341 | theend: |
| 2342 | vim_free(tofree); |
| 2343 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2347 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2348 | |
| 2349 | /* |
| 2350 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2351 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2352 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2353 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2354 | * valid name. |
| 2355 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2356 | static char_u * |
| 2357 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2358 | { |
| 2359 | char_u *p; |
| 2360 | |
| 2361 | // Quick check for valid starting character. |
| 2362 | if (!eval_isnamec1(*arg)) |
| 2363 | return arg; |
| 2364 | |
| 2365 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2366 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2367 | // and can be used in slice "[n:]". |
| 2368 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2369 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2370 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2371 | break; |
| 2372 | return p; |
| 2373 | } |
| 2374 | |
| 2375 | /* |
| 2376 | * 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] | 2377 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2378 | */ |
| 2379 | char_u * |
| 2380 | to_name_const_end(char_u *arg) |
| 2381 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2382 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2383 | typval_T rettv; |
| 2384 | |
| 2385 | if (p == arg && *arg == '[') |
| 2386 | { |
| 2387 | |
| 2388 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2389 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2390 | p = arg; |
| 2391 | } |
| 2392 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 2393 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2394 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2395 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 2396 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2397 | p = arg; |
| 2398 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2399 | |
| 2400 | return p; |
| 2401 | } |
| 2402 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2403 | /* |
| 2404 | * parse a list: [expr, expr] |
| 2405 | * "*arg" points to the '['. |
| 2406 | */ |
| 2407 | static int |
| 2408 | compile_list(char_u **arg, cctx_T *cctx) |
| 2409 | { |
| 2410 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2411 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2412 | int count = 0; |
| 2413 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2414 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2415 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2416 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2417 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2418 | semsg(_(e_list_end), *arg); |
| 2419 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2420 | } |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2421 | if (*p == ',') |
| 2422 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 2423 | semsg(_(e_no_white_space_allowed_before), ","); |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2424 | return FAIL; |
| 2425 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2426 | if (*p == ']') |
| 2427 | { |
| 2428 | ++p; |
| 2429 | // Allow for following comment, after at least one space. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2430 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '#') |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2431 | p += STRLEN(p); |
| 2432 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2433 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2434 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2435 | break; |
| 2436 | ++count; |
| 2437 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2438 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2439 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2440 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 2441 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 2442 | semsg(_(e_white_space_required_after), ","); |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2443 | return FAIL; |
| 2444 | } |
| 2445 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2446 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2447 | p = skipwhite(p); |
| 2448 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2449 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2450 | |
| 2451 | generate_NEWLIST(cctx, count); |
| 2452 | return OK; |
| 2453 | } |
| 2454 | |
| 2455 | /* |
| 2456 | * parse a lambda: {arg, arg -> expr} |
| 2457 | * "*arg" points to the '{'. |
| 2458 | */ |
| 2459 | static int |
| 2460 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 2461 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2462 | typval_T rettv; |
| 2463 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2464 | evalarg_T evalarg; |
| 2465 | |
| 2466 | CLEAR_FIELD(evalarg); |
| 2467 | evalarg.eval_flags = EVAL_EVALUATE; |
| 2468 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2469 | |
| 2470 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2471 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2472 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2473 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2474 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2475 | ++ufunc->uf_refcount; |
| 2476 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2477 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2478 | |
| 2479 | // The function will have one line: "return {expr}". |
| 2480 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2481 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2482 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 2483 | clear_evalarg(&evalarg, NULL); |
| 2484 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 2485 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 2486 | { |
| 2487 | // The return type will now be known. |
| 2488 | set_function_type(ufunc); |
| 2489 | |
| 2490 | return generate_FUNCREF(cctx, ufunc); |
| 2491 | } |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2492 | |
| 2493 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2494 | return FAIL; |
| 2495 | } |
| 2496 | |
| 2497 | /* |
| 2498 | * Compile a lamda call: expr->{lambda}(args) |
| 2499 | * "arg" points to the "{". |
| 2500 | */ |
| 2501 | static int |
| 2502 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 2503 | { |
| 2504 | ufunc_T *ufunc; |
| 2505 | typval_T rettv; |
| 2506 | int argcount = 1; |
| 2507 | int ret = FAIL; |
| 2508 | |
| 2509 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 2510 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2511 | return FAIL; |
| 2512 | |
| 2513 | if (**arg != '(') |
| 2514 | { |
| 2515 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 2516 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2517 | else |
| 2518 | semsg(_(e_missing_paren), "lambda"); |
| 2519 | clear_tv(&rettv); |
| 2520 | return FAIL; |
| 2521 | } |
| 2522 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2523 | ufunc = rettv.vval.v_partial->pt_func; |
| 2524 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2525 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2526 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2527 | |
| 2528 | // The function will have one line: "return {expr}". |
| 2529 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2530 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2531 | |
| 2532 | // compile the arguments |
| 2533 | *arg = skipwhite(*arg + 1); |
| 2534 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 2535 | // call the compiled function |
| 2536 | ret = generate_CALL(cctx, ufunc, argcount); |
| 2537 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2538 | if (ret == FAIL) |
| 2539 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2540 | return ret; |
| 2541 | } |
| 2542 | |
| 2543 | /* |
| 2544 | * parse a dict: {'key': val} or #{key: val} |
| 2545 | * "*arg" points to the '{'. |
| 2546 | */ |
| 2547 | static int |
| 2548 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 2549 | { |
| 2550 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2551 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2552 | int count = 0; |
| 2553 | dict_T *d = dict_alloc(); |
| 2554 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2555 | char_u *whitep = *arg; |
| 2556 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2557 | |
| 2558 | if (d == NULL) |
| 2559 | return FAIL; |
| 2560 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2561 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2562 | { |
| 2563 | char_u *key = NULL; |
| 2564 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2565 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2566 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2567 | *arg = NULL; |
| 2568 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2569 | } |
| 2570 | |
| 2571 | if (**arg == '}') |
| 2572 | break; |
| 2573 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2574 | if (literal) |
| 2575 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2576 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2577 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2578 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2579 | { |
| 2580 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 2581 | return FAIL; |
| 2582 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2583 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2584 | if (generate_PUSHS(cctx, key) == FAIL) |
| 2585 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2586 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2587 | } |
| 2588 | else |
| 2589 | { |
| 2590 | isn_T *isn; |
| 2591 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2592 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2593 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2594 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 2595 | if (isn->isn_type == ISN_PUSHS) |
| 2596 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2597 | else |
| 2598 | { |
| 2599 | type_T *keytype = ((type_T **)stack->ga_data) |
| 2600 | [stack->ga_len - 1]; |
| 2601 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 2602 | return FAIL; |
| 2603 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | // Check for duplicate keys, if using string keys. |
| 2607 | if (key != NULL) |
| 2608 | { |
| 2609 | item = dict_find(d, key, -1); |
| 2610 | if (item != NULL) |
| 2611 | { |
| 2612 | semsg(_(e_duplicate_key), key); |
| 2613 | goto failret; |
| 2614 | } |
| 2615 | item = dictitem_alloc(key); |
| 2616 | if (item != NULL) |
| 2617 | { |
| 2618 | item->di_tv.v_type = VAR_UNKNOWN; |
| 2619 | item->di_tv.v_lock = 0; |
| 2620 | if (dict_add(d, item) == FAIL) |
| 2621 | dictitem_free(item); |
| 2622 | } |
| 2623 | } |
| 2624 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2625 | if (**arg != ':') |
| 2626 | { |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 2627 | if (*skipwhite(*arg) == ':') |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 2628 | semsg(_(e_no_white_space_allowed_before), ":"); |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 2629 | else |
| 2630 | semsg(_(e_missing_dict_colon), *arg); |
| 2631 | return FAIL; |
| 2632 | } |
| 2633 | whitep = *arg + 1; |
| 2634 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 2635 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 2636 | semsg(_(e_white_space_required_after), ":"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2637 | return FAIL; |
| 2638 | } |
| 2639 | |
| 2640 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2641 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2642 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2643 | *arg = NULL; |
| 2644 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2645 | } |
| 2646 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2647 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2648 | return FAIL; |
| 2649 | ++count; |
| 2650 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2651 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2652 | *arg = skipwhite(*arg); |
| 2653 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2654 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2655 | *arg = NULL; |
| 2656 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2657 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2658 | if (**arg == '}') |
| 2659 | break; |
| 2660 | if (**arg != ',') |
| 2661 | { |
| 2662 | semsg(_(e_missing_dict_comma), *arg); |
| 2663 | goto failret; |
| 2664 | } |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 2665 | if (IS_WHITE_OR_NUL(*whitep)) |
| 2666 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 2667 | semsg(_(e_no_white_space_allowed_before), ","); |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 2668 | return FAIL; |
| 2669 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2670 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2671 | *arg = skipwhite(*arg + 1); |
| 2672 | } |
| 2673 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2674 | *arg = *arg + 1; |
| 2675 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2676 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2677 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2678 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2679 | *arg += STRLEN(*arg); |
| 2680 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2681 | dict_unref(d); |
| 2682 | return generate_NEWDICT(cctx, count); |
| 2683 | |
| 2684 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2685 | if (*arg == NULL) |
| 2686 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2687 | dict_unref(d); |
| 2688 | return FAIL; |
| 2689 | } |
| 2690 | |
| 2691 | /* |
| 2692 | * Compile "&option". |
| 2693 | */ |
| 2694 | static int |
| 2695 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 2696 | { |
| 2697 | typval_T rettv; |
| 2698 | char_u *start = *arg; |
| 2699 | int ret; |
| 2700 | |
| 2701 | // parse the option and get the current value to get the type. |
| 2702 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2703 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2704 | if (ret == OK) |
| 2705 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2706 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2707 | char_u *name = vim_strnsave(start, *arg - start); |
| 2708 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 2709 | |
| 2710 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 2711 | vim_free(name); |
| 2712 | } |
| 2713 | clear_tv(&rettv); |
| 2714 | |
| 2715 | return ret; |
| 2716 | } |
| 2717 | |
| 2718 | /* |
| 2719 | * Compile "$VAR". |
| 2720 | */ |
| 2721 | static int |
| 2722 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 2723 | { |
| 2724 | char_u *start = *arg; |
| 2725 | int len; |
| 2726 | int ret; |
| 2727 | char_u *name; |
| 2728 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2729 | ++*arg; |
| 2730 | len = get_env_len(arg); |
| 2731 | if (len == 0) |
| 2732 | { |
| 2733 | semsg(_(e_syntax_at), start - 1); |
| 2734 | return FAIL; |
| 2735 | } |
| 2736 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2737 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2738 | name = vim_strnsave(start, len + 1); |
| 2739 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 2740 | vim_free(name); |
| 2741 | return ret; |
| 2742 | } |
| 2743 | |
| 2744 | /* |
| 2745 | * Compile "@r". |
| 2746 | */ |
| 2747 | static int |
| 2748 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 2749 | { |
| 2750 | int ret; |
| 2751 | |
| 2752 | ++*arg; |
| 2753 | if (**arg == NUL) |
| 2754 | { |
| 2755 | semsg(_(e_syntax_at), *arg - 1); |
| 2756 | return FAIL; |
| 2757 | } |
Bram Moolenaar | 7226e5b | 2020-08-02 17:33:26 +0200 | [diff] [blame] | 2758 | if (!valid_yank_reg(**arg, FALSE)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2759 | { |
| 2760 | emsg_invreg(**arg); |
| 2761 | return FAIL; |
| 2762 | } |
| 2763 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 2764 | ++*arg; |
| 2765 | return ret; |
| 2766 | } |
| 2767 | |
| 2768 | /* |
| 2769 | * Apply leading '!', '-' and '+' to constant "rettv". |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2770 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2771 | */ |
| 2772 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2773 | 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] | 2774 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2775 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2776 | |
| 2777 | // this works from end to start |
| 2778 | while (p > start) |
| 2779 | { |
| 2780 | --p; |
| 2781 | if (*p == '-' || *p == '+') |
| 2782 | { |
| 2783 | // only '-' has an effect, for '+' we only check the type |
| 2784 | #ifdef FEAT_FLOAT |
| 2785 | if (rettv->v_type == VAR_FLOAT) |
| 2786 | { |
| 2787 | if (*p == '-') |
| 2788 | rettv->vval.v_float = -rettv->vval.v_float; |
| 2789 | } |
| 2790 | else |
| 2791 | #endif |
| 2792 | { |
| 2793 | varnumber_T val; |
| 2794 | int error = FALSE; |
| 2795 | |
| 2796 | // tv_get_number_chk() accepts a string, but we don't want that |
| 2797 | // here |
| 2798 | if (check_not_string(rettv) == FAIL) |
| 2799 | return FAIL; |
| 2800 | val = tv_get_number_chk(rettv, &error); |
| 2801 | clear_tv(rettv); |
| 2802 | if (error) |
| 2803 | return FAIL; |
| 2804 | if (*p == '-') |
| 2805 | val = -val; |
| 2806 | rettv->v_type = VAR_NUMBER; |
| 2807 | rettv->vval.v_number = val; |
| 2808 | } |
| 2809 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2810 | else if (numeric_only) |
| 2811 | { |
| 2812 | ++p; |
| 2813 | break; |
| 2814 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2815 | else |
| 2816 | { |
| 2817 | int v = tv2bool(rettv); |
| 2818 | |
| 2819 | // '!' is permissive in the type. |
| 2820 | clear_tv(rettv); |
| 2821 | rettv->v_type = VAR_BOOL; |
| 2822 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 2823 | } |
| 2824 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2825 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2826 | return OK; |
| 2827 | } |
| 2828 | |
| 2829 | /* |
| 2830 | * Recognize v: variables that are constants and set "rettv". |
| 2831 | */ |
| 2832 | static void |
| 2833 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 2834 | { |
| 2835 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 2836 | { |
| 2837 | rettv->v_type = VAR_BOOL; |
| 2838 | rettv->vval.v_number = VVAL_TRUE; |
| 2839 | *arg += 6; |
| 2840 | } |
| 2841 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 2842 | { |
| 2843 | rettv->v_type = VAR_BOOL; |
| 2844 | rettv->vval.v_number = VVAL_FALSE; |
| 2845 | *arg += 7; |
| 2846 | } |
| 2847 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 2848 | { |
| 2849 | rettv->v_type = VAR_SPECIAL; |
| 2850 | rettv->vval.v_number = VVAL_NULL; |
| 2851 | *arg += 6; |
| 2852 | } |
| 2853 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 2854 | { |
| 2855 | rettv->v_type = VAR_SPECIAL; |
| 2856 | rettv->vval.v_number = VVAL_NONE; |
| 2857 | *arg += 6; |
| 2858 | } |
| 2859 | } |
| 2860 | |
Bram Moolenaar | 696ba23 | 2020-07-29 21:20:41 +0200 | [diff] [blame] | 2861 | exptype_T |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 2862 | get_compare_type(char_u *p, int *len, int *type_is) |
| 2863 | { |
| 2864 | exptype_T type = EXPR_UNKNOWN; |
| 2865 | int i; |
| 2866 | |
| 2867 | switch (p[0]) |
| 2868 | { |
| 2869 | case '=': if (p[1] == '=') |
| 2870 | type = EXPR_EQUAL; |
| 2871 | else if (p[1] == '~') |
| 2872 | type = EXPR_MATCH; |
| 2873 | break; |
| 2874 | case '!': if (p[1] == '=') |
| 2875 | type = EXPR_NEQUAL; |
| 2876 | else if (p[1] == '~') |
| 2877 | type = EXPR_NOMATCH; |
| 2878 | break; |
| 2879 | case '>': if (p[1] != '=') |
| 2880 | { |
| 2881 | type = EXPR_GREATER; |
| 2882 | *len = 1; |
| 2883 | } |
| 2884 | else |
| 2885 | type = EXPR_GEQUAL; |
| 2886 | break; |
| 2887 | case '<': if (p[1] != '=') |
| 2888 | { |
| 2889 | type = EXPR_SMALLER; |
| 2890 | *len = 1; |
| 2891 | } |
| 2892 | else |
| 2893 | type = EXPR_SEQUAL; |
| 2894 | break; |
| 2895 | case 'i': if (p[1] == 's') |
| 2896 | { |
| 2897 | // "is" and "isnot"; but not a prefix of a name |
| 2898 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 2899 | *len = 5; |
| 2900 | i = p[*len]; |
| 2901 | if (!isalnum(i) && i != '_') |
| 2902 | { |
| 2903 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 2904 | *type_is = TRUE; |
| 2905 | } |
| 2906 | } |
| 2907 | break; |
| 2908 | } |
| 2909 | return type; |
| 2910 | } |
| 2911 | |
| 2912 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2913 | * Compile code to apply '-', '+' and '!'. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2914 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2915 | */ |
| 2916 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2917 | 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] | 2918 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2919 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2920 | |
| 2921 | // this works from end to start |
| 2922 | while (p > start) |
| 2923 | { |
| 2924 | --p; |
| 2925 | if (*p == '-' || *p == '+') |
| 2926 | { |
| 2927 | int negate = *p == '-'; |
| 2928 | isn_T *isn; |
| 2929 | |
| 2930 | // TODO: check type |
| 2931 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 2932 | { |
| 2933 | --p; |
| 2934 | if (*p == '-') |
| 2935 | negate = !negate; |
| 2936 | } |
| 2937 | // only '-' has an effect, for '+' we only check the type |
| 2938 | if (negate) |
| 2939 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 2940 | else |
| 2941 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 2942 | if (isn == NULL) |
| 2943 | return FAIL; |
| 2944 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2945 | else if (numeric_only) |
| 2946 | { |
| 2947 | ++p; |
| 2948 | break; |
| 2949 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2950 | else |
| 2951 | { |
| 2952 | int invert = TRUE; |
| 2953 | |
| 2954 | while (p > start && p[-1] == '!') |
| 2955 | { |
| 2956 | --p; |
| 2957 | invert = !invert; |
| 2958 | } |
| 2959 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 2960 | return FAIL; |
| 2961 | } |
| 2962 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2963 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2964 | return OK; |
| 2965 | } |
| 2966 | |
| 2967 | /* |
| 2968 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2969 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2970 | */ |
| 2971 | static int |
| 2972 | compile_subscript( |
| 2973 | char_u **arg, |
| 2974 | cctx_T *cctx, |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2975 | char_u *start_leader, |
| 2976 | char_u **end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 2977 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2978 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2979 | char_u *name_start = *end_leader; |
| 2980 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2981 | for (;;) |
| 2982 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2983 | char_u *p = skipwhite(*arg); |
| 2984 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2985 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2986 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2987 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2988 | |
| 2989 | // If a following line starts with "->{" or "->X" advance to that |
| 2990 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2991 | // Also if a following line starts with ".x". |
| 2992 | if (next != NULL && |
| 2993 | ((next[0] == '-' && next[1] == '>' |
| 2994 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 2995 | || (next[0] == '.' && eval_isdictc(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2996 | { |
| 2997 | next = next_line_from_context(cctx, TRUE); |
| 2998 | if (next == NULL) |
| 2999 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3000 | *arg = next; |
| 3001 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3002 | } |
| 3003 | } |
| 3004 | |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 3005 | // Do not skip over white space to find the "(", "exeucte 'x' ()" is |
| 3006 | // not a function call. |
| 3007 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3008 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3009 | garray_T *stack = &cctx->ctx_type_stack; |
| 3010 | type_T *type; |
| 3011 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3012 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3013 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3014 | return FAIL; |
| 3015 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3016 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3017 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3018 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3019 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3020 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3021 | return FAIL; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3022 | if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3023 | return FAIL; |
| 3024 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3025 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3026 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3027 | char_u *pstart = p; |
| 3028 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3029 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3030 | return FAIL; |
| 3031 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3032 | // something->method() |
| 3033 | // Apply the '!', '-' and '+' first: |
| 3034 | // -1.0->func() works like (-1.0)->func() |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3035 | if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3036 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3037 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3038 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3039 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3040 | // No line break supported right after "->". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3041 | if (**arg == '{') |
| 3042 | { |
| 3043 | // lambda call: list->{lambda} |
| 3044 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3045 | return FAIL; |
| 3046 | } |
| 3047 | else |
| 3048 | { |
| 3049 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3050 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3051 | if (!eval_isnamec1(*p)) |
| 3052 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3053 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3054 | return FAIL; |
| 3055 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3056 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3057 | p += 2; |
Bram Moolenaar | c5da1fb | 2020-08-05 15:43:44 +0200 | [diff] [blame] | 3058 | for ( ; eval_isnamec(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3059 | ; |
| 3060 | if (*p != '(') |
| 3061 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3062 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3063 | return FAIL; |
| 3064 | } |
| 3065 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3066 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3067 | return FAIL; |
| 3068 | } |
| 3069 | } |
Bram Moolenaar | badd848 | 2020-07-31 22:38:17 +0200 | [diff] [blame] | 3070 | else if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3071 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3072 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3073 | type_T **typep; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3074 | vartype_T vtype; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3075 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3076 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3077 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3078 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3079 | // TODO: blob index |
| 3080 | // TODO: more arguments |
| 3081 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3082 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3083 | return FAIL; |
| 3084 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3085 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3086 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3087 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3088 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3089 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3090 | return FAIL; |
| 3091 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3092 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3093 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3094 | if (**arg != ']') |
| 3095 | { |
| 3096 | emsg(_(e_missbrac)); |
| 3097 | return FAIL; |
| 3098 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3099 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3100 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3101 | // We can index a list and a dict. If we don't know the type |
| 3102 | // we can use the index value type. |
| 3103 | // TODO: If we don't know use an instruction to figure it out at |
| 3104 | // runtime. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3105 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3106 | vtype = (*typep)->tt_type; |
| 3107 | if (*typep == &t_any) |
| 3108 | { |
| 3109 | type_T *valtype = ((type_T **)stack->ga_data) |
| 3110 | [stack->ga_len - 1]; |
| 3111 | if (valtype == &t_string) |
| 3112 | vtype = VAR_DICT; |
| 3113 | } |
| 3114 | if (vtype == VAR_DICT) |
| 3115 | { |
| 3116 | if ((*typep)->tt_type == VAR_DICT) |
| 3117 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3118 | else |
| 3119 | { |
| 3120 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3121 | return FAIL; |
| 3122 | *typep = &t_any; |
| 3123 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3124 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3125 | return FAIL; |
| 3126 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3127 | return FAIL; |
| 3128 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3129 | else if (vtype == VAR_STRING) |
| 3130 | { |
| 3131 | *typep = &t_number; |
| 3132 | if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL) |
| 3133 | return FAIL; |
| 3134 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3135 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3136 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3137 | if ((*typep)->tt_type == VAR_LIST) |
| 3138 | *typep = (*typep)->tt_member; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3139 | if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3140 | return FAIL; |
| 3141 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3142 | else |
| 3143 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 3144 | emsg(_(e_list_dict_or_blob_required)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3145 | return FAIL; |
| 3146 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3147 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3148 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3149 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3150 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3151 | return FAIL; |
| 3152 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3153 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3154 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3155 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3156 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3157 | p = *arg; |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3158 | if (eval_isdictc(*p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3159 | while (eval_isnamec(*p)) |
| 3160 | MB_PTR_ADV(p); |
| 3161 | if (p == *arg) |
| 3162 | { |
| 3163 | semsg(_(e_syntax_at), *arg); |
| 3164 | return FAIL; |
| 3165 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3166 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3167 | return FAIL; |
| 3168 | *arg = p; |
| 3169 | } |
| 3170 | else |
| 3171 | break; |
| 3172 | } |
| 3173 | |
| 3174 | // TODO - see handle_subscript(): |
| 3175 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3176 | // Don't do this when "Func" is already a partial that was bound |
| 3177 | // explicitly (pt_auto is FALSE). |
| 3178 | |
| 3179 | return OK; |
| 3180 | } |
| 3181 | |
| 3182 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3183 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3184 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3185 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3186 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3187 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3188 | * |
| 3189 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3190 | */ |
| 3191 | |
| 3192 | /* |
| 3193 | * number number constant |
| 3194 | * 0zFFFFFFFF Blob constant |
| 3195 | * "string" string constant |
| 3196 | * 'string' literal string constant |
| 3197 | * &option-name option value |
| 3198 | * @r register contents |
| 3199 | * identifier variable value |
| 3200 | * function() function call |
| 3201 | * $VAR environment variable |
| 3202 | * (expression) nested expression |
| 3203 | * [expr, expr] List |
| 3204 | * {key: val, key: val} Dictionary |
| 3205 | * #{key: val, key: val} Dictionary with literal keys |
| 3206 | * |
| 3207 | * Also handle: |
| 3208 | * ! in front logical NOT |
| 3209 | * - in front unary minus |
| 3210 | * + in front unary plus (ignored) |
| 3211 | * trailing (arg) funcref/partial call |
| 3212 | * trailing [] subscript in String or List |
| 3213 | * trailing .name entry in Dictionary |
| 3214 | * trailing ->name() method call |
| 3215 | */ |
| 3216 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3217 | compile_expr7( |
| 3218 | char_u **arg, |
| 3219 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3220 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3221 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3222 | char_u *start_leader, *end_leader; |
| 3223 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3224 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3225 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3226 | |
| 3227 | /* |
| 3228 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3229 | */ |
| 3230 | start_leader = *arg; |
| 3231 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3232 | *arg = skipwhite(*arg + 1); |
| 3233 | end_leader = *arg; |
| 3234 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3235 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3236 | switch (**arg) |
| 3237 | { |
| 3238 | /* |
| 3239 | * Number constant. |
| 3240 | */ |
| 3241 | case '0': // also for blob starting with 0z |
| 3242 | case '1': |
| 3243 | case '2': |
| 3244 | case '3': |
| 3245 | case '4': |
| 3246 | case '5': |
| 3247 | case '6': |
| 3248 | case '7': |
| 3249 | case '8': |
| 3250 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3251 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3252 | return FAIL; |
Bram Moolenaar | 4301a72 | 2020-08-11 20:51:08 +0200 | [diff] [blame] | 3253 | // Apply "-" and "+" just before the number now, right to |
| 3254 | // left. Matters especially when "->" follows. Stops at |
| 3255 | // '!'. |
| 3256 | if (apply_leader(rettv, TRUE, |
| 3257 | start_leader, &end_leader) == FAIL) |
| 3258 | { |
| 3259 | clear_tv(rettv); |
| 3260 | return FAIL; |
| 3261 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3262 | break; |
| 3263 | |
| 3264 | /* |
| 3265 | * String constant: "string". |
| 3266 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3267 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3268 | return FAIL; |
| 3269 | break; |
| 3270 | |
| 3271 | /* |
| 3272 | * Literal string constant: 'str''ing'. |
| 3273 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3274 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3275 | return FAIL; |
| 3276 | break; |
| 3277 | |
| 3278 | /* |
| 3279 | * Constant Vim variable. |
| 3280 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3281 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3282 | ret = NOTDONE; |
| 3283 | break; |
| 3284 | |
| 3285 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3286 | * "true" constant |
| 3287 | */ |
| 3288 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3289 | && !eval_isnamec((*arg)[4])) |
| 3290 | { |
| 3291 | *arg += 4; |
| 3292 | rettv->v_type = VAR_BOOL; |
| 3293 | rettv->vval.v_number = VVAL_TRUE; |
| 3294 | } |
| 3295 | else |
| 3296 | ret = NOTDONE; |
| 3297 | break; |
| 3298 | |
| 3299 | /* |
| 3300 | * "false" constant |
| 3301 | */ |
| 3302 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3303 | && !eval_isnamec((*arg)[5])) |
| 3304 | { |
| 3305 | *arg += 5; |
| 3306 | rettv->v_type = VAR_BOOL; |
| 3307 | rettv->vval.v_number = VVAL_FALSE; |
| 3308 | } |
| 3309 | else |
| 3310 | ret = NOTDONE; |
| 3311 | break; |
| 3312 | |
| 3313 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3314 | * List: [expr, expr] |
| 3315 | */ |
| 3316 | case '[': ret = compile_list(arg, cctx); |
| 3317 | break; |
| 3318 | |
| 3319 | /* |
| 3320 | * Dictionary: #{key: val, key: val} |
| 3321 | */ |
| 3322 | case '#': if ((*arg)[1] == '{') |
| 3323 | { |
| 3324 | ++*arg; |
| 3325 | ret = compile_dict(arg, cctx, TRUE); |
| 3326 | } |
| 3327 | else |
| 3328 | ret = NOTDONE; |
| 3329 | break; |
| 3330 | |
| 3331 | /* |
| 3332 | * Lambda: {arg, arg -> expr} |
| 3333 | * Dictionary: {'key': val, 'key': val} |
| 3334 | */ |
| 3335 | case '{': { |
| 3336 | char_u *start = skipwhite(*arg + 1); |
| 3337 | |
| 3338 | // Find out what comes after the arguments. |
| 3339 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3340 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3341 | if (ret != FAIL && *start == '>') |
| 3342 | ret = compile_lambda(arg, cctx); |
| 3343 | else |
| 3344 | ret = compile_dict(arg, cctx, FALSE); |
| 3345 | } |
| 3346 | break; |
| 3347 | |
| 3348 | /* |
| 3349 | * Option value: &name |
| 3350 | */ |
| 3351 | case '&': ret = compile_get_option(arg, cctx); |
| 3352 | break; |
| 3353 | |
| 3354 | /* |
| 3355 | * Environment variable: $VAR. |
| 3356 | */ |
| 3357 | case '$': ret = compile_get_env(arg, cctx); |
| 3358 | break; |
| 3359 | |
| 3360 | /* |
| 3361 | * Register contents: @r. |
| 3362 | */ |
| 3363 | case '@': ret = compile_get_register(arg, cctx); |
| 3364 | break; |
| 3365 | /* |
| 3366 | * nested expression: (expression). |
| 3367 | */ |
| 3368 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3369 | |
| 3370 | // recursive! |
| 3371 | if (ppconst->pp_used <= PPSIZE - 10) |
| 3372 | { |
| 3373 | ret = compile_expr1(arg, cctx, ppconst); |
| 3374 | } |
| 3375 | else |
| 3376 | { |
| 3377 | // Not enough space in ppconst, flush constants. |
| 3378 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3379 | return FAIL; |
| 3380 | ret = compile_expr0(arg, cctx); |
| 3381 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3382 | *arg = skipwhite(*arg); |
| 3383 | if (**arg == ')') |
| 3384 | ++*arg; |
| 3385 | else if (ret == OK) |
| 3386 | { |
| 3387 | emsg(_(e_missing_close)); |
| 3388 | ret = FAIL; |
| 3389 | } |
| 3390 | break; |
| 3391 | |
| 3392 | default: ret = NOTDONE; |
| 3393 | break; |
| 3394 | } |
| 3395 | if (ret == FAIL) |
| 3396 | return FAIL; |
| 3397 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3398 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3399 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3400 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3401 | clear_tv(rettv); |
| 3402 | else |
| 3403 | // A constant expression can possibly be handled compile time, |
| 3404 | // return the value instead of generating code. |
| 3405 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3406 | } |
| 3407 | else if (ret == NOTDONE) |
| 3408 | { |
| 3409 | char_u *p; |
| 3410 | int r; |
| 3411 | |
| 3412 | if (!eval_isnamec1(**arg)) |
| 3413 | { |
| 3414 | semsg(_("E1015: Name expected: %s"), *arg); |
| 3415 | return FAIL; |
| 3416 | } |
| 3417 | |
| 3418 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3419 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3420 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3421 | { |
| 3422 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 3423 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3424 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3425 | { |
| 3426 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3427 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3428 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3429 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3430 | if (r == FAIL) |
| 3431 | return FAIL; |
| 3432 | } |
| 3433 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3434 | // Handle following "[]", ".member", etc. |
| 3435 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3436 | if (compile_subscript(arg, cctx, start_leader, &end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3437 | ppconst) == FAIL) |
| 3438 | return FAIL; |
| 3439 | if (ppconst->pp_used > 0) |
| 3440 | { |
| 3441 | // apply the '!', '-' and '+' before the constant |
| 3442 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3443 | if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3444 | return FAIL; |
| 3445 | return OK; |
| 3446 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3447 | if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3448 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3449 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | /* |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3453 | * Give the "white on both sides" error, taking the operator from "p[len]". |
| 3454 | */ |
| 3455 | void |
| 3456 | error_white_both(char_u *op, int len) |
| 3457 | { |
| 3458 | char_u buf[10]; |
| 3459 | |
| 3460 | vim_strncpy(buf, op, len); |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 3461 | semsg(_(e_white_space_required_before_and_after), buf); |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | /* |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3465 | * <type>expr7: runtime type check / conversion |
| 3466 | */ |
| 3467 | static int |
| 3468 | compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3469 | { |
| 3470 | type_T *want_type = NULL; |
| 3471 | |
| 3472 | // Recognize <type> |
| 3473 | if (**arg == '<' && eval_isnamec1((*arg)[1])) |
| 3474 | { |
| 3475 | int called_emsg_before = called_emsg; |
| 3476 | |
| 3477 | ++*arg; |
| 3478 | want_type = parse_type(arg, cctx->ctx_type_list); |
| 3479 | if (called_emsg != called_emsg_before) |
| 3480 | return FAIL; |
| 3481 | |
| 3482 | if (**arg != '>') |
| 3483 | { |
| 3484 | if (*skipwhite(*arg) == '>') |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 3485 | semsg(_(e_no_white_space_allowed_before), ">"); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3486 | else |
| 3487 | emsg(_("E1104: Missing >")); |
| 3488 | return FAIL; |
| 3489 | } |
| 3490 | ++*arg; |
| 3491 | if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL) |
| 3492 | return FAIL; |
| 3493 | } |
| 3494 | |
| 3495 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
| 3496 | return FAIL; |
| 3497 | |
| 3498 | if (want_type != NULL) |
| 3499 | { |
| 3500 | garray_T *stack = &cctx->ctx_type_stack; |
| 3501 | type_T *actual = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3502 | |
| 3503 | if (check_type(want_type, actual, FALSE) == FAIL) |
| 3504 | { |
| 3505 | generate_ppconst(cctx, ppconst); |
| 3506 | if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL) |
| 3507 | return FAIL; |
| 3508 | } |
| 3509 | } |
| 3510 | |
| 3511 | return OK; |
| 3512 | } |
| 3513 | |
| 3514 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3515 | * * number multiplication |
| 3516 | * / number division |
| 3517 | * % number modulo |
| 3518 | */ |
| 3519 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3520 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3521 | { |
| 3522 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3523 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3524 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3525 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3526 | // get the first expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3527 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3528 | return FAIL; |
| 3529 | |
| 3530 | /* |
| 3531 | * Repeat computing, until no "*", "/" or "%" is following. |
| 3532 | */ |
| 3533 | for (;;) |
| 3534 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3535 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3536 | if (*op != '*' && *op != '/' && *op != '%') |
| 3537 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3538 | if (next != NULL) |
| 3539 | { |
| 3540 | *arg = next_line_from_context(cctx, TRUE); |
| 3541 | op = skipwhite(*arg); |
| 3542 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3543 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3544 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3545 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3546 | error_white_both(op, 1); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3547 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3548 | } |
| 3549 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3550 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3551 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3552 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3553 | // get the second expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3554 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3555 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3556 | |
| 3557 | if (ppconst->pp_used == ppconst_used + 2 |
| 3558 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3559 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3560 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3561 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3562 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3563 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3564 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3565 | // both are numbers: compute the result |
| 3566 | switch (*op) |
| 3567 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3568 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3569 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3570 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3571 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3572 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3573 | break; |
| 3574 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3575 | tv1->vval.v_number = res; |
| 3576 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3577 | } |
| 3578 | else |
| 3579 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3580 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3581 | generate_two_op(cctx, op); |
| 3582 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | return OK; |
| 3586 | } |
| 3587 | |
| 3588 | /* |
| 3589 | * + number addition |
| 3590 | * - number subtraction |
| 3591 | * .. string concatenation |
| 3592 | */ |
| 3593 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3594 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3595 | { |
| 3596 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3597 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3598 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3599 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3600 | |
| 3601 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3602 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3603 | return FAIL; |
| 3604 | |
| 3605 | /* |
| 3606 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3607 | */ |
| 3608 | for (;;) |
| 3609 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3610 | op = may_peek_next_line(cctx, *arg, &next); |
| 3611 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3612 | break; |
| 3613 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3614 | if (next != NULL) |
| 3615 | { |
| 3616 | *arg = next_line_from_context(cctx, TRUE); |
| 3617 | op = skipwhite(*arg); |
| 3618 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3619 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3620 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3621 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3622 | error_white_both(op, oplen); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3623 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3627 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3628 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3629 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3630 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3631 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3632 | return FAIL; |
| 3633 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3634 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3635 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3636 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 3637 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 3638 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3639 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3640 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3641 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3642 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3643 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3644 | // concat/subtract/add constant numbers |
| 3645 | if (*op == '+') |
| 3646 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 3647 | else if (*op == '-') |
| 3648 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 3649 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3650 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3651 | // concatenate constant strings |
| 3652 | char_u *s1 = tv1->vval.v_string; |
| 3653 | char_u *s2 = tv2->vval.v_string; |
| 3654 | size_t len1 = STRLEN(s1); |
| 3655 | |
| 3656 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 3657 | if (tv1->vval.v_string == NULL) |
| 3658 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3659 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3660 | return FAIL; |
| 3661 | } |
| 3662 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 3663 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3664 | vim_free(s1); |
| 3665 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3666 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3667 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3668 | } |
| 3669 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3670 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3671 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3672 | if (*op == '.') |
| 3673 | { |
| 3674 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 3675 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 3676 | return FAIL; |
| 3677 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 3678 | } |
| 3679 | else |
| 3680 | generate_two_op(cctx, op); |
| 3681 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3682 | } |
| 3683 | |
| 3684 | return OK; |
| 3685 | } |
| 3686 | |
| 3687 | /* |
| 3688 | * expr5a == expr5b |
| 3689 | * expr5a =~ expr5b |
| 3690 | * expr5a != expr5b |
| 3691 | * expr5a !~ expr5b |
| 3692 | * expr5a > expr5b |
| 3693 | * expr5a >= expr5b |
| 3694 | * expr5a < expr5b |
| 3695 | * expr5a <= expr5b |
| 3696 | * expr5a is expr5b |
| 3697 | * expr5a isnot expr5b |
| 3698 | * |
| 3699 | * Produces instructions: |
| 3700 | * EVAL expr5a Push result of "expr5a" |
| 3701 | * EVAL expr5b Push result of "expr5b" |
| 3702 | * COMPARE one of the compare instructions |
| 3703 | */ |
| 3704 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3705 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3706 | { |
| 3707 | exptype_T type = EXPR_UNKNOWN; |
| 3708 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3709 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3710 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3711 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3712 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3713 | |
| 3714 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3715 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3716 | return FAIL; |
| 3717 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3718 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3719 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3720 | |
| 3721 | /* |
| 3722 | * If there is a comparative operator, use it. |
| 3723 | */ |
| 3724 | if (type != EXPR_UNKNOWN) |
| 3725 | { |
| 3726 | int ic = FALSE; // Default: do not ignore case |
| 3727 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3728 | if (next != NULL) |
| 3729 | { |
| 3730 | *arg = next_line_from_context(cctx, TRUE); |
| 3731 | p = skipwhite(*arg); |
| 3732 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3733 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 3734 | { |
| 3735 | semsg(_(e_invexpr2), *arg); |
| 3736 | return FAIL; |
| 3737 | } |
| 3738 | // extra question mark appended: ignore case |
| 3739 | if (p[len] == '?') |
| 3740 | { |
| 3741 | ic = TRUE; |
| 3742 | ++len; |
| 3743 | } |
| 3744 | // extra '#' appended: match case (ignored) |
| 3745 | else if (p[len] == '#') |
| 3746 | ++len; |
| 3747 | // nothing appended: match case |
| 3748 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3749 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3750 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3751 | error_white_both(p, len); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3752 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3753 | } |
| 3754 | |
| 3755 | // get the second variable |
| 3756 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3757 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3758 | return FAIL; |
| 3759 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3760 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3761 | return FAIL; |
| 3762 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3763 | if (ppconst->pp_used == ppconst_used + 2) |
| 3764 | { |
| 3765 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 3766 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3767 | int ret; |
| 3768 | |
| 3769 | // Both sides are a constant, compute the result now. |
| 3770 | // First check for a valid combination of types, this is more |
| 3771 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 3772 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3773 | ret = FAIL; |
| 3774 | else |
| 3775 | { |
| 3776 | ret = typval_compare(tv1, tv2, type, ic); |
| 3777 | tv1->v_type = VAR_BOOL; |
| 3778 | tv1->vval.v_number = tv1->vval.v_number |
| 3779 | ? VVAL_TRUE : VVAL_FALSE; |
| 3780 | clear_tv(tv2); |
| 3781 | --ppconst->pp_used; |
| 3782 | } |
| 3783 | return ret; |
| 3784 | } |
| 3785 | |
| 3786 | generate_ppconst(cctx, ppconst); |
| 3787 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3788 | } |
| 3789 | |
| 3790 | return OK; |
| 3791 | } |
| 3792 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 3793 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 3794 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3795 | /* |
| 3796 | * Compile || or &&. |
| 3797 | */ |
| 3798 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3799 | compile_and_or( |
| 3800 | char_u **arg, |
| 3801 | cctx_T *cctx, |
| 3802 | char *op, |
| 3803 | ppconst_T *ppconst, |
| 3804 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3805 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3806 | char_u *next; |
| 3807 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3808 | int opchar = *op; |
| 3809 | |
| 3810 | if (p[0] == opchar && p[1] == opchar) |
| 3811 | { |
| 3812 | garray_T *instr = &cctx->ctx_instr; |
| 3813 | garray_T end_ga; |
| 3814 | |
| 3815 | /* |
| 3816 | * Repeat until there is no following "||" or "&&" |
| 3817 | */ |
| 3818 | ga_init2(&end_ga, sizeof(int), 10); |
| 3819 | while (p[0] == opchar && p[1] == opchar) |
| 3820 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3821 | if (next != NULL) |
| 3822 | { |
| 3823 | *arg = next_line_from_context(cctx, TRUE); |
| 3824 | p = skipwhite(*arg); |
| 3825 | } |
| 3826 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3827 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 3828 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 3829 | semsg(_(e_white_space_required_before_and_after), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3830 | return FAIL; |
| 3831 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3832 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3833 | // TODO: use ppconst if the value is a constant |
| 3834 | generate_ppconst(cctx, ppconst); |
| 3835 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3836 | if (ga_grow(&end_ga, 1) == FAIL) |
| 3837 | { |
| 3838 | ga_clear(&end_ga); |
| 3839 | return FAIL; |
| 3840 | } |
| 3841 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 3842 | ++end_ga.ga_len; |
| 3843 | generate_JUMP(cctx, opchar == '|' |
| 3844 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 3845 | |
| 3846 | // eval the next expression |
| 3847 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3848 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3849 | return FAIL; |
| 3850 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3851 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 3852 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3853 | { |
| 3854 | ga_clear(&end_ga); |
| 3855 | return FAIL; |
| 3856 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3857 | |
| 3858 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3859 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3860 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3861 | |
| 3862 | // Fill in the end label in all jumps. |
| 3863 | while (end_ga.ga_len > 0) |
| 3864 | { |
| 3865 | isn_T *isn; |
| 3866 | |
| 3867 | --end_ga.ga_len; |
| 3868 | isn = ((isn_T *)instr->ga_data) |
| 3869 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 3870 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3871 | } |
| 3872 | ga_clear(&end_ga); |
| 3873 | } |
| 3874 | |
| 3875 | return OK; |
| 3876 | } |
| 3877 | |
| 3878 | /* |
| 3879 | * expr4a && expr4a && expr4a logical AND |
| 3880 | * |
| 3881 | * Produces instructions: |
| 3882 | * EVAL expr4a Push result of "expr4a" |
| 3883 | * JUMP_AND_KEEP_IF_FALSE end |
| 3884 | * EVAL expr4b Push result of "expr4b" |
| 3885 | * JUMP_AND_KEEP_IF_FALSE end |
| 3886 | * EVAL expr4c Push result of "expr4c" |
| 3887 | * end: |
| 3888 | */ |
| 3889 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3890 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3891 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3892 | int ppconst_used = ppconst->pp_used; |
| 3893 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3894 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3895 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3896 | return FAIL; |
| 3897 | |
| 3898 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3899 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | /* |
| 3903 | * expr3a || expr3b || expr3c logical OR |
| 3904 | * |
| 3905 | * Produces instructions: |
| 3906 | * EVAL expr3a Push result of "expr3a" |
| 3907 | * JUMP_AND_KEEP_IF_TRUE end |
| 3908 | * EVAL expr3b Push result of "expr3b" |
| 3909 | * JUMP_AND_KEEP_IF_TRUE end |
| 3910 | * EVAL expr3c Push result of "expr3c" |
| 3911 | * end: |
| 3912 | */ |
| 3913 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3914 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3915 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3916 | int ppconst_used = ppconst->pp_used; |
| 3917 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3918 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3919 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3920 | return FAIL; |
| 3921 | |
| 3922 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3923 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3924 | } |
| 3925 | |
| 3926 | /* |
| 3927 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 3928 | * |
| 3929 | * Produces instructions: |
| 3930 | * EVAL expr2 Push result of "expr" |
| 3931 | * JUMP_IF_FALSE alt jump if false |
| 3932 | * EVAL expr1a |
| 3933 | * JUMP_ALWAYS end |
| 3934 | * alt: EVAL expr1b |
| 3935 | * end: |
| 3936 | */ |
| 3937 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3938 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3939 | { |
| 3940 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3941 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3942 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3943 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3944 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3945 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3946 | return FAIL; |
| 3947 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3948 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3949 | if (*p == '?') |
| 3950 | { |
| 3951 | garray_T *instr = &cctx->ctx_instr; |
| 3952 | garray_T *stack = &cctx->ctx_type_stack; |
| 3953 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 3954 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3955 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 3956 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3957 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3958 | int has_const_expr = FALSE; |
| 3959 | int const_value = FALSE; |
| 3960 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3961 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3962 | if (next != NULL) |
| 3963 | { |
| 3964 | *arg = next_line_from_context(cctx, TRUE); |
| 3965 | p = skipwhite(*arg); |
| 3966 | } |
| 3967 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3968 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 3969 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 3970 | semsg(_(e_white_space_required_before_and_after), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3971 | return FAIL; |
| 3972 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3973 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3974 | if (ppconst->pp_used == ppconst_used + 1) |
| 3975 | { |
| 3976 | // the condition is a constant, we know whether the ? or the : |
| 3977 | // expression is to be evaluated. |
| 3978 | has_const_expr = TRUE; |
| 3979 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 3980 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 3981 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3982 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 3983 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3984 | } |
| 3985 | else |
| 3986 | { |
| 3987 | generate_ppconst(cctx, ppconst); |
| 3988 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 3989 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3990 | |
| 3991 | // evaluate the second expression; any type is accepted |
| 3992 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3993 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3994 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3995 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 3996 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3997 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3998 | if (!has_const_expr) |
| 3999 | { |
| 4000 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4001 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4002 | // remember the type and drop it |
| 4003 | --stack->ga_len; |
| 4004 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4005 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4006 | end_idx = instr->ga_len; |
| 4007 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4008 | |
| 4009 | // jump here from JUMP_IF_FALSE |
| 4010 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4011 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4012 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4013 | |
| 4014 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4015 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4016 | if (*p != ':') |
| 4017 | { |
| 4018 | emsg(_(e_missing_colon)); |
| 4019 | return FAIL; |
| 4020 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4021 | if (next != NULL) |
| 4022 | { |
| 4023 | *arg = next_line_from_context(cctx, TRUE); |
| 4024 | p = skipwhite(*arg); |
| 4025 | } |
| 4026 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4027 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4028 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4029 | semsg(_(e_white_space_required_before_and_after), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4030 | return FAIL; |
| 4031 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4032 | |
| 4033 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4034 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4035 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4036 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4037 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4038 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4039 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4040 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4041 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4042 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4043 | if (!has_const_expr) |
| 4044 | { |
| 4045 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4046 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4047 | // If the types differ, the result has a more generic type. |
| 4048 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4049 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4050 | |
| 4051 | // jump here from JUMP_ALWAYS |
| 4052 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4053 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4054 | } |
| 4055 | |
| 4056 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4057 | } |
| 4058 | return OK; |
| 4059 | } |
| 4060 | |
| 4061 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4062 | * Toplevel expression. |
| 4063 | */ |
| 4064 | static int |
| 4065 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4066 | { |
| 4067 | ppconst_T ppconst; |
| 4068 | |
| 4069 | CLEAR_FIELD(ppconst); |
| 4070 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4071 | { |
| 4072 | clear_ppconst(&ppconst); |
| 4073 | return FAIL; |
| 4074 | } |
| 4075 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4076 | return FAIL; |
| 4077 | return OK; |
| 4078 | } |
| 4079 | |
| 4080 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4081 | * compile "return [expr]" |
| 4082 | */ |
| 4083 | static char_u * |
| 4084 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4085 | { |
| 4086 | char_u *p = arg; |
| 4087 | garray_T *stack = &cctx->ctx_type_stack; |
| 4088 | type_T *stack_type; |
| 4089 | |
| 4090 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4091 | { |
| 4092 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4093 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4094 | return NULL; |
| 4095 | |
| 4096 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4097 | if (set_return_type) |
| 4098 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4099 | else |
| 4100 | { |
| 4101 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4102 | && stack_type->tt_type != VAR_VOID |
| 4103 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4104 | { |
| 4105 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4106 | return NULL; |
| 4107 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4108 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4109 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4110 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4111 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4112 | } |
| 4113 | else |
| 4114 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4115 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4116 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4117 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4118 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4119 | { |
| 4120 | emsg(_("E1003: Missing return value")); |
| 4121 | return NULL; |
| 4122 | } |
| 4123 | |
| 4124 | // No argument, return zero. |
| 4125 | generate_PUSHNR(cctx, 0); |
| 4126 | } |
| 4127 | |
| 4128 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4129 | return NULL; |
| 4130 | |
| 4131 | // "return val | endif" is possible |
| 4132 | return skipwhite(p); |
| 4133 | } |
| 4134 | |
| 4135 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4136 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4137 | * Return a pointer to the line in allocated memory. |
| 4138 | * Return NULL for end-of-file or some error. |
| 4139 | */ |
| 4140 | static char_u * |
| 4141 | exarg_getline( |
| 4142 | int c UNUSED, |
| 4143 | void *cookie, |
| 4144 | int indent UNUSED, |
| 4145 | int do_concat UNUSED) |
| 4146 | { |
| 4147 | cctx_T *cctx = (cctx_T *)cookie; |
| 4148 | |
| 4149 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4150 | { |
| 4151 | iemsg("Heredoc got to end"); |
| 4152 | return NULL; |
| 4153 | } |
| 4154 | ++cctx->ctx_lnum; |
| 4155 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4156 | [cctx->ctx_lnum]); |
| 4157 | } |
| 4158 | |
| 4159 | /* |
| 4160 | * Compile a nested :def command. |
| 4161 | */ |
| 4162 | static char_u * |
| 4163 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4164 | { |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4165 | int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4166 | char_u *name_start = eap->arg; |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4167 | char_u *name_end = to_name_end(eap->arg, TRUE); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4168 | char_u *lambda_name; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4169 | lvar_T *lvar; |
| 4170 | ufunc_T *ufunc; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4171 | int r; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4172 | |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4173 | // Only g:Func() can use a namespace. |
| 4174 | if (name_start[1] == ':' && !is_global) |
| 4175 | { |
| 4176 | semsg(_(e_namespace), name_start); |
| 4177 | return NULL; |
| 4178 | } |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4179 | if (check_defined(name_start, name_end - name_start, cctx) == FAIL) |
| 4180 | return NULL; |
| 4181 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4182 | eap->arg = name_end; |
| 4183 | eap->getline = exarg_getline; |
| 4184 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4185 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4186 | eap->forceit = FALSE; |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4187 | lambda_name = get_lambda_name(); |
| 4188 | ufunc = def_function(eap, lambda_name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4189 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4190 | if (ufunc == NULL) |
| 4191 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4192 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4193 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4194 | return NULL; |
| 4195 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4196 | if (is_global) |
| 4197 | { |
| 4198 | char_u *func_name = vim_strnsave(name_start + 2, |
| 4199 | name_end - name_start - 2); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4200 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4201 | if (func_name == NULL) |
| 4202 | r = FAIL; |
| 4203 | else |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4204 | r = generate_NEWFUNC(cctx, lambda_name, func_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4205 | } |
| 4206 | else |
| 4207 | { |
| 4208 | // Define a local variable for the function reference. |
| 4209 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
| 4210 | TRUE, ufunc->uf_func_type); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4211 | if (lvar == NULL) |
| 4212 | return NULL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 4213 | if (generate_FUNCREF(cctx, ufunc) == FAIL) |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4214 | return NULL; |
| 4215 | r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 4216 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4217 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4218 | // TODO: warning for trailing text? |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4219 | return r == FAIL ? NULL : (char_u *)""; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4223 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4224 | */ |
| 4225 | int |
| 4226 | assignment_len(char_u *p, int *heredoc) |
| 4227 | { |
| 4228 | if (*p == '=') |
| 4229 | { |
| 4230 | if (p[1] == '<' && p[2] == '<') |
| 4231 | { |
| 4232 | *heredoc = TRUE; |
| 4233 | return 3; |
| 4234 | } |
| 4235 | return 1; |
| 4236 | } |
| 4237 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4238 | return 2; |
| 4239 | if (STRNCMP(p, "..=", 3) == 0) |
| 4240 | return 3; |
| 4241 | return 0; |
| 4242 | } |
| 4243 | |
| 4244 | // words that cannot be used as a variable |
| 4245 | static char *reserved[] = { |
| 4246 | "true", |
| 4247 | "false", |
| 4248 | NULL |
| 4249 | }; |
| 4250 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4251 | typedef enum { |
| 4252 | dest_local, |
| 4253 | dest_option, |
| 4254 | dest_env, |
| 4255 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4256 | dest_buffer, |
| 4257 | dest_window, |
| 4258 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4259 | dest_vimvar, |
| 4260 | dest_script, |
| 4261 | dest_reg, |
| 4262 | } assign_dest_T; |
| 4263 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4264 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4265 | * Generate the load instruction for "name". |
| 4266 | */ |
| 4267 | static void |
| 4268 | generate_loadvar( |
| 4269 | cctx_T *cctx, |
| 4270 | assign_dest_T dest, |
| 4271 | char_u *name, |
| 4272 | lvar_T *lvar, |
| 4273 | type_T *type) |
| 4274 | { |
| 4275 | switch (dest) |
| 4276 | { |
| 4277 | case dest_option: |
| 4278 | // TODO: check the option exists |
| 4279 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4280 | break; |
| 4281 | case dest_global: |
| 4282 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4283 | break; |
| 4284 | case dest_buffer: |
| 4285 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4286 | break; |
| 4287 | case dest_window: |
| 4288 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4289 | break; |
| 4290 | case dest_tab: |
| 4291 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4292 | break; |
| 4293 | case dest_script: |
| 4294 | compile_load_scriptvar(cctx, |
| 4295 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4296 | break; |
| 4297 | case dest_env: |
| 4298 | // Include $ in the name here |
| 4299 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4300 | break; |
| 4301 | case dest_reg: |
| 4302 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4303 | break; |
| 4304 | case dest_vimvar: |
| 4305 | generate_LOADV(cctx, name + 2, TRUE); |
| 4306 | break; |
| 4307 | case dest_local: |
| 4308 | if (lvar->lv_from_outer) |
| 4309 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4310 | NULL, type); |
| 4311 | else |
| 4312 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4313 | break; |
| 4314 | } |
| 4315 | } |
| 4316 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4317 | void |
| 4318 | vim9_declare_error(char_u *name) |
| 4319 | { |
| 4320 | char *scope = ""; |
| 4321 | |
| 4322 | switch (*name) |
| 4323 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4324 | case 'g': scope = _("global"); break; |
| 4325 | case 'b': scope = _("buffer"); break; |
| 4326 | case 'w': scope = _("window"); break; |
| 4327 | case 't': scope = _("tab"); break; |
| 4328 | case 'v': scope = "v:"; break; |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4329 | case '$': semsg(_(e_cannot_declare_an_environment_variable), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4330 | return; |
| 4331 | case '&': semsg(_("E1052: Cannot declare an option: %s"), name); |
| 4332 | return; |
| 4333 | case '@': semsg(_("E1066: Cannot declare a register: %s"), name); |
| 4334 | return; |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4335 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4336 | } |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4337 | semsg(_(e_cannot_declare_a_scope_variable), scope, name); |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4338 | } |
| 4339 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4340 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4341 | * Compile declaration and assignment: |
| 4342 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4343 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4344 | * Return NULL for an error. |
| 4345 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4346 | */ |
| 4347 | static char_u * |
| 4348 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4349 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4350 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4351 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4352 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4353 | char_u *ret = NULL; |
| 4354 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4355 | int var_idx; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4356 | int scriptvar_sid = 0; |
| 4357 | int scriptvar_idx = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4358 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4359 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4360 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4361 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4362 | int oplen = 0; |
| 4363 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4364 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4365 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4366 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4367 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4368 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4369 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4370 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 4371 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 4372 | if (p == NULL) |
| 4373 | return *arg == '[' ? arg : NULL; |
| 4374 | |
| 4375 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4376 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 4377 | // TODO: should we allow this, and figure out type inference from list |
| 4378 | // members? |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4379 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4380 | return NULL; |
| 4381 | } |
| 4382 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4383 | sp = p; |
| 4384 | p = skipwhite(p); |
| 4385 | op = p; |
| 4386 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4387 | |
| 4388 | if (var_count > 0 && oplen == 0) |
| 4389 | // can be something like "[1, 2]->func()" |
| 4390 | return arg; |
| 4391 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4392 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4393 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4394 | error_white_both(op, oplen); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4395 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4396 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4397 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4398 | if (heredoc) |
| 4399 | { |
| 4400 | list_T *l; |
| 4401 | listitem_T *li; |
| 4402 | |
| 4403 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4404 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4405 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 4406 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4407 | |
| 4408 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 4409 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4410 | { |
| 4411 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4412 | li->li_tv.vval.v_string = NULL; |
| 4413 | } |
| 4414 | generate_NEWLIST(cctx, l->lv_len); |
| 4415 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4416 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4417 | list_free(l); |
| 4418 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4419 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4420 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4421 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4422 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4423 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 4424 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4425 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4426 | p = skipwhite(op + oplen); |
| 4427 | if (compile_expr0(&p, cctx) == FAIL) |
| 4428 | return NULL; |
| 4429 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4430 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4431 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4432 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4433 | type_T *stacktype; |
| 4434 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4435 | stacktype = stack->ga_len == 0 ? &t_void |
| 4436 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4437 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4438 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4439 | emsg(_(e_cannot_use_void)); |
| 4440 | goto theend; |
| 4441 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4442 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4443 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 4444 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4445 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 4446 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4447 | } |
| 4448 | } |
| 4449 | |
| 4450 | /* |
| 4451 | * Loop over variables in "[var, var] = expr". |
| 4452 | * For "var = expr" and "let var: type" this is done only once. |
| 4453 | */ |
| 4454 | if (var_count > 0) |
| 4455 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 4456 | else |
| 4457 | var_start = arg; |
| 4458 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 4459 | { |
| 4460 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 4461 | size_t varlen; |
| 4462 | int new_local = FALSE; |
| 4463 | int opt_type; |
| 4464 | int opt_flags = 0; |
| 4465 | assign_dest_T dest = dest_local; |
| 4466 | int vimvaridx = -1; |
| 4467 | lvar_T *lvar = NULL; |
| 4468 | lvar_T arg_lvar; |
| 4469 | int has_type = FALSE; |
| 4470 | int has_index = FALSE; |
| 4471 | int instr_count = -1; |
| 4472 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4473 | if (*var_start == '@') |
| 4474 | p = var_start + 2; |
| 4475 | else |
| 4476 | { |
| 4477 | p = (*var_start == '&' || *var_start == '$') |
| 4478 | ? var_start + 1 : var_start; |
| 4479 | p = to_name_end(p, TRUE); |
| 4480 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4481 | |
| 4482 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 4483 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 4484 | --var_end; |
| 4485 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 4486 | --p; |
| 4487 | |
| 4488 | varlen = p - var_start; |
| 4489 | vim_free(name); |
| 4490 | name = vim_strnsave(var_start, varlen); |
| 4491 | if (name == NULL) |
| 4492 | return NULL; |
| 4493 | if (!heredoc) |
| 4494 | type = &t_any; |
| 4495 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4496 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4497 | { |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4498 | int declare_error = FALSE; |
| 4499 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4500 | if (*var_start == '&') |
| 4501 | { |
| 4502 | int cc; |
| 4503 | long numval; |
| 4504 | |
| 4505 | dest = dest_option; |
| 4506 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4507 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4508 | emsg(_(e_const_option)); |
| 4509 | goto theend; |
| 4510 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4511 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4512 | p = var_start; |
| 4513 | p = find_option_end(&p, &opt_flags); |
| 4514 | if (p == NULL) |
| 4515 | { |
| 4516 | // cannot happen? |
| 4517 | emsg(_(e_letunexp)); |
| 4518 | goto theend; |
| 4519 | } |
| 4520 | cc = *p; |
| 4521 | *p = NUL; |
| 4522 | opt_type = get_option_value(var_start + 1, &numval, |
| 4523 | NULL, opt_flags); |
| 4524 | *p = cc; |
| 4525 | if (opt_type == -3) |
| 4526 | { |
| 4527 | semsg(_(e_unknown_option), var_start); |
| 4528 | goto theend; |
| 4529 | } |
| 4530 | if (opt_type == -2 || opt_type == 0) |
| 4531 | type = &t_string; |
| 4532 | else |
| 4533 | type = &t_number; // both number and boolean option |
| 4534 | } |
| 4535 | else if (*var_start == '$') |
| 4536 | { |
| 4537 | dest = dest_env; |
| 4538 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4539 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4540 | } |
| 4541 | else if (*var_start == '@') |
| 4542 | { |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4543 | if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.') |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4544 | { |
| 4545 | emsg_invreg(var_start[1]); |
| 4546 | goto theend; |
| 4547 | } |
| 4548 | dest = dest_reg; |
| 4549 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4550 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4551 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4552 | else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4553 | { |
| 4554 | dest = dest_global; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4555 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4556 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4557 | else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4558 | { |
| 4559 | dest = dest_buffer; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4560 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4561 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4562 | else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4563 | { |
| 4564 | dest = dest_window; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4565 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4566 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4567 | else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4568 | { |
| 4569 | dest = dest_tab; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4570 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4571 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4572 | else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4573 | { |
| 4574 | typval_T *vtv; |
| 4575 | int di_flags; |
| 4576 | |
| 4577 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 4578 | if (vimvaridx < 0) |
| 4579 | { |
| 4580 | semsg(_(e_var_notfound), var_start); |
| 4581 | goto theend; |
| 4582 | } |
| 4583 | // We use the current value of "sandbox" here, is that OK? |
| 4584 | if (var_check_ro(di_flags, name, FALSE)) |
| 4585 | goto theend; |
| 4586 | dest = dest_vimvar; |
| 4587 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 4588 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4589 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4590 | } |
| 4591 | else |
| 4592 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4593 | int idx; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4594 | |
| 4595 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 4596 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4597 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4598 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4599 | goto theend; |
| 4600 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4601 | |
| 4602 | lvar = lookup_local(var_start, varlen, cctx); |
| 4603 | if (lvar == NULL) |
| 4604 | { |
| 4605 | CLEAR_FIELD(arg_lvar); |
| 4606 | if (lookup_arg(var_start, varlen, |
| 4607 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 4608 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4609 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4610 | if (is_decl) |
| 4611 | { |
| 4612 | semsg(_(e_used_as_arg), name); |
| 4613 | goto theend; |
| 4614 | } |
| 4615 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4616 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4617 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4618 | if (lvar != NULL) |
| 4619 | { |
| 4620 | if (is_decl) |
| 4621 | { |
| 4622 | semsg(_("E1017: Variable already declared: %s"), name); |
| 4623 | goto theend; |
| 4624 | } |
| 4625 | else if (lvar->lv_const) |
| 4626 | { |
| 4627 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 4628 | name); |
| 4629 | goto theend; |
| 4630 | } |
| 4631 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4632 | else |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4633 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4634 | int script_namespace = varlen > 1 |
| 4635 | && STRNCMP(var_start, "s:", 2) == 0; |
| 4636 | int script_var = (script_namespace |
| 4637 | ? lookup_script(var_start + 2, varlen - 2) |
| 4638 | : lookup_script(var_start, varlen)) == OK; |
| 4639 | imported_T *import = |
| 4640 | find_imported(var_start, varlen, cctx); |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4641 | |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4642 | if (script_namespace || script_var || import != NULL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4643 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4644 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 4645 | |
| 4646 | if (is_decl) |
| 4647 | { |
| 4648 | if (script_namespace) |
| 4649 | semsg(_("E1101: Cannot declare a script variable in a function: %s"), |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4650 | name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4651 | else |
| 4652 | semsg(_("E1054: Variable already declared in the script: %s"), |
| 4653 | name); |
| 4654 | goto theend; |
| 4655 | } |
| 4656 | else if (cctx->ctx_ufunc->uf_script_ctx_version |
| 4657 | == SCRIPT_VERSION_VIM9 |
| 4658 | && script_namespace |
| 4659 | && !script_var && import == NULL) |
| 4660 | { |
| 4661 | semsg(_(e_unknown_var), name); |
| 4662 | goto theend; |
| 4663 | } |
| 4664 | |
| 4665 | dest = dest_script; |
| 4666 | |
| 4667 | // existing script-local variables should have a type |
| 4668 | scriptvar_sid = current_sctx.sc_sid; |
| 4669 | if (import != NULL) |
| 4670 | scriptvar_sid = import->imp_sid; |
| 4671 | scriptvar_idx = get_script_item_idx(scriptvar_sid, |
| 4672 | rawname, TRUE); |
| 4673 | if (scriptvar_idx >= 0) |
| 4674 | { |
| 4675 | scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid); |
| 4676 | svar_T *sv = |
| 4677 | ((svar_T *)si->sn_var_vals.ga_data) |
| 4678 | + scriptvar_idx; |
| 4679 | type = sv->sv_type; |
| 4680 | } |
| 4681 | } |
| 4682 | else if (name[1] == ':' && name[2] != NUL) |
| 4683 | { |
| 4684 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4685 | name); |
| 4686 | goto theend; |
| 4687 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4688 | else if (!is_decl) |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4689 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4690 | semsg(_(e_unknown_var), name); |
| 4691 | goto theend; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4692 | } |
Bram Moolenaar | fa211f3 | 2020-08-07 22:00:26 +0200 | [diff] [blame] | 4693 | else if (check_defined(var_start, varlen, cctx) == FAIL) |
| 4694 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4695 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4696 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4697 | |
| 4698 | if (declare_error) |
| 4699 | { |
| 4700 | vim9_declare_error(name); |
| 4701 | goto theend; |
| 4702 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | // handle "a:name" as a name, not index "name" on "a" |
| 4706 | if (varlen > 1 || var_start[varlen] != ':') |
| 4707 | p = var_end; |
| 4708 | |
| 4709 | if (dest != dest_option) |
| 4710 | { |
| 4711 | if (is_decl && *p == ':') |
| 4712 | { |
| 4713 | // parse optional type: "let var: type = expr" |
| 4714 | if (!VIM_ISWHITE(p[1])) |
| 4715 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4716 | semsg(_(e_white_space_required_after), ":"); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4717 | goto theend; |
| 4718 | } |
| 4719 | p = skipwhite(p + 1); |
| 4720 | type = parse_type(&p, cctx->ctx_type_list); |
| 4721 | has_type = TRUE; |
| 4722 | } |
| 4723 | else if (lvar != NULL) |
| 4724 | type = lvar->lv_type; |
| 4725 | } |
| 4726 | |
| 4727 | if (oplen == 3 && !heredoc && dest != dest_global |
| 4728 | && type->tt_type != VAR_STRING |
| 4729 | && type->tt_type != VAR_ANY) |
| 4730 | { |
| 4731 | emsg(_("E1019: Can only concatenate to string")); |
| 4732 | goto theend; |
| 4733 | } |
| 4734 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4735 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4736 | { |
| 4737 | if (oplen > 1 && !heredoc) |
| 4738 | { |
| 4739 | // +=, /=, etc. require an existing variable |
| 4740 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 4741 | name); |
| 4742 | goto theend; |
| 4743 | } |
| 4744 | |
| 4745 | // new local variable |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 4746 | if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
| 4747 | && var_wrong_func_name(name, TRUE)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4748 | goto theend; |
| 4749 | lvar = reserve_local(cctx, var_start, varlen, |
| 4750 | cmdidx == CMD_const, type); |
| 4751 | if (lvar == NULL) |
| 4752 | goto theend; |
| 4753 | new_local = TRUE; |
| 4754 | } |
| 4755 | |
| 4756 | member_type = type; |
| 4757 | if (var_end > var_start + varlen) |
| 4758 | { |
| 4759 | // Something follows after the variable: "var[idx]". |
| 4760 | if (is_decl) |
| 4761 | { |
| 4762 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 4763 | goto theend; |
| 4764 | } |
| 4765 | |
| 4766 | if (var_start[varlen] == '[') |
| 4767 | { |
| 4768 | has_index = TRUE; |
| 4769 | if (type->tt_member == NULL) |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 4770 | member_type = &t_any; |
| 4771 | else |
| 4772 | member_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4773 | } |
| 4774 | else |
| 4775 | { |
| 4776 | semsg("Not supported yet: %s", var_start); |
| 4777 | goto theend; |
| 4778 | } |
| 4779 | } |
| 4780 | else if (lvar == &arg_lvar) |
| 4781 | { |
| 4782 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 4783 | goto theend; |
| 4784 | } |
| 4785 | |
| 4786 | if (!heredoc) |
| 4787 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4788 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4789 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4790 | if (oplen > 0 && var_count == 0) |
| 4791 | { |
| 4792 | // skip over the "=" and the expression |
| 4793 | p = skipwhite(op + oplen); |
| 4794 | compile_expr0(&p, cctx); |
| 4795 | } |
| 4796 | } |
| 4797 | else if (oplen > 0) |
| 4798 | { |
| 4799 | type_T *stacktype; |
| 4800 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4801 | // For "var = expr" evaluate the expression. |
| 4802 | if (var_count == 0) |
| 4803 | { |
| 4804 | int r; |
| 4805 | |
| 4806 | // for "+=", "*=", "..=" etc. first load the current value |
| 4807 | if (*op != '=') |
| 4808 | { |
| 4809 | generate_loadvar(cctx, dest, name, lvar, type); |
| 4810 | |
| 4811 | if (has_index) |
| 4812 | { |
| 4813 | // TODO: get member from list or dict |
| 4814 | emsg("Index with operation not supported yet"); |
| 4815 | goto theend; |
| 4816 | } |
| 4817 | } |
| 4818 | |
| 4819 | // Compile the expression. Temporarily hide the new local |
| 4820 | // variable here, it is not available to this expression. |
| 4821 | if (new_local) |
| 4822 | --cctx->ctx_locals.ga_len; |
| 4823 | instr_count = instr->ga_len; |
| 4824 | p = skipwhite(op + oplen); |
| 4825 | r = compile_expr0(&p, cctx); |
| 4826 | if (new_local) |
| 4827 | ++cctx->ctx_locals.ga_len; |
| 4828 | if (r == FAIL) |
| 4829 | goto theend; |
| 4830 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4831 | else if (semicolon && var_idx == var_count - 1) |
| 4832 | { |
| 4833 | // For "[var; var] = expr" get the rest of the list |
| 4834 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 4835 | goto theend; |
| 4836 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4837 | else |
| 4838 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4839 | // For "[var, var] = expr" get the "var_idx" item from the |
| 4840 | // list. |
| 4841 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 4842 | return FAIL; |
| 4843 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4844 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4845 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 4846 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4847 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4848 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4849 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4850 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4851 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4852 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4853 | emsg(_(e_cannot_use_void)); |
| 4854 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4855 | } |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 4856 | else if ((stacktype->tt_type == VAR_FUNC |
| 4857 | || stacktype->tt_type == VAR_PARTIAL) |
| 4858 | && var_wrong_func_name(name, TRUE)) |
| 4859 | { |
| 4860 | goto theend; |
| 4861 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4862 | else |
| 4863 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4864 | // An empty list or dict has a &t_void member, |
| 4865 | // for a variable that implies &t_any. |
| 4866 | if (stacktype == &t_list_empty) |
| 4867 | lvar->lv_type = &t_list_any; |
| 4868 | else if (stacktype == &t_dict_empty) |
| 4869 | lvar->lv_type = &t_dict_any; |
| 4870 | else |
| 4871 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4872 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4873 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4874 | else |
| 4875 | { |
| 4876 | type_T *use_type = lvar->lv_type; |
| 4877 | |
| 4878 | if (has_index) |
| 4879 | { |
| 4880 | use_type = use_type->tt_member; |
| 4881 | if (use_type == NULL) |
| 4882 | use_type = &t_void; |
| 4883 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4884 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4885 | == FAIL) |
| 4886 | goto theend; |
| 4887 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4888 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4889 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4890 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4891 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4892 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4893 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4894 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4895 | emsg(_(e_const_requires_a_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4896 | goto theend; |
| 4897 | } |
| 4898 | else if (!has_type || dest == dest_option) |
| 4899 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4900 | emsg(_(e_type_or_initialization_required)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4901 | goto theend; |
| 4902 | } |
| 4903 | else |
| 4904 | { |
| 4905 | // variables are always initialized |
| 4906 | if (ga_grow(instr, 1) == FAIL) |
| 4907 | goto theend; |
| 4908 | switch (member_type->tt_type) |
| 4909 | { |
| 4910 | case VAR_BOOL: |
| 4911 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 4912 | break; |
| 4913 | case VAR_FLOAT: |
| 4914 | #ifdef FEAT_FLOAT |
| 4915 | generate_PUSHF(cctx, 0.0); |
| 4916 | #endif |
| 4917 | break; |
| 4918 | case VAR_STRING: |
| 4919 | generate_PUSHS(cctx, NULL); |
| 4920 | break; |
| 4921 | case VAR_BLOB: |
| 4922 | generate_PUSHBLOB(cctx, NULL); |
| 4923 | break; |
| 4924 | case VAR_FUNC: |
| 4925 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 4926 | break; |
| 4927 | case VAR_LIST: |
| 4928 | generate_NEWLIST(cctx, 0); |
| 4929 | break; |
| 4930 | case VAR_DICT: |
| 4931 | generate_NEWDICT(cctx, 0); |
| 4932 | break; |
| 4933 | case VAR_JOB: |
| 4934 | generate_PUSHJOB(cctx, NULL); |
| 4935 | break; |
| 4936 | case VAR_CHANNEL: |
| 4937 | generate_PUSHCHANNEL(cctx, NULL); |
| 4938 | break; |
| 4939 | case VAR_NUMBER: |
| 4940 | case VAR_UNKNOWN: |
| 4941 | case VAR_ANY: |
| 4942 | case VAR_PARTIAL: |
| 4943 | case VAR_VOID: |
| 4944 | case VAR_SPECIAL: // cannot happen |
| 4945 | generate_PUSHNR(cctx, 0); |
| 4946 | break; |
| 4947 | } |
| 4948 | } |
| 4949 | if (var_count == 0) |
| 4950 | end = p; |
| 4951 | } |
| 4952 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4953 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4954 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4955 | break; |
| 4956 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4957 | if (oplen > 0 && *op != '=') |
| 4958 | { |
| 4959 | type_T *expected = &t_number; |
| 4960 | type_T *stacktype; |
| 4961 | |
| 4962 | // TODO: if type is known use float or any operation |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 4963 | // TODO: check operator matches variable type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4964 | |
| 4965 | if (*op == '.') |
| 4966 | expected = &t_string; |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 4967 | else if (*op == '+') |
| 4968 | expected = member_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4969 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4970 | if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4971 | goto theend; |
| 4972 | |
| 4973 | if (*op == '.') |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 4974 | { |
| 4975 | if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL) |
| 4976 | goto theend; |
| 4977 | } |
| 4978 | else if (*op == '+') |
| 4979 | { |
| 4980 | if (generate_add_instr(cctx, |
| 4981 | operator_type(member_type, stacktype), |
| 4982 | member_type, stacktype) == FAIL) |
| 4983 | goto theend; |
| 4984 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4985 | else |
| 4986 | { |
| 4987 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 4988 | |
| 4989 | if (isn == NULL) |
| 4990 | goto theend; |
| 4991 | switch (*op) |
| 4992 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4993 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 4994 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 4995 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 4996 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 4997 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4998 | } |
| 4999 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5000 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5001 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5002 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5003 | int r; |
| 5004 | |
| 5005 | // Compile the "idx" in "var[idx]". |
| 5006 | if (new_local) |
| 5007 | --cctx->ctx_locals.ga_len; |
| 5008 | p = skipwhite(var_start + varlen + 1); |
| 5009 | r = compile_expr0(&p, cctx); |
| 5010 | if (new_local) |
| 5011 | ++cctx->ctx_locals.ga_len; |
| 5012 | if (r == FAIL) |
| 5013 | goto theend; |
| 5014 | if (*skipwhite(p) != ']') |
| 5015 | { |
| 5016 | emsg(_(e_missbrac)); |
| 5017 | goto theend; |
| 5018 | } |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 5019 | if (type == &t_any) |
| 5020 | { |
| 5021 | type_T *idx_type = ((type_T **)stack->ga_data)[ |
| 5022 | stack->ga_len - 1]; |
| 5023 | // Index on variable of unknown type: guess the type from the |
| 5024 | // index type: number is dict, otherwise dict. |
| 5025 | // TODO: should do the assignment at runtime |
| 5026 | if (idx_type->tt_type == VAR_NUMBER) |
| 5027 | type = &t_list_any; |
| 5028 | else |
| 5029 | type = &t_dict_any; |
| 5030 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5031 | if (type->tt_type == VAR_DICT |
| 5032 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5033 | goto theend; |
| 5034 | if (type->tt_type == VAR_LIST |
| 5035 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5036 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5037 | { |
| 5038 | emsg(_(e_number_exp)); |
| 5039 | goto theend; |
| 5040 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5041 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5042 | // Load the dict or list. On the stack we then have: |
| 5043 | // - value |
| 5044 | // - index |
| 5045 | // - variable |
| 5046 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5047 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5048 | if (type->tt_type == VAR_LIST) |
| 5049 | { |
| 5050 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5051 | return FAIL; |
| 5052 | } |
| 5053 | else if (type->tt_type == VAR_DICT) |
| 5054 | { |
| 5055 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5056 | return FAIL; |
| 5057 | } |
| 5058 | else |
| 5059 | { |
| 5060 | emsg(_(e_listreq)); |
| 5061 | goto theend; |
| 5062 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5063 | } |
| 5064 | else |
| 5065 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5066 | switch (dest) |
| 5067 | { |
| 5068 | case dest_option: |
| 5069 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5070 | break; |
| 5071 | case dest_global: |
| 5072 | // include g: with the name, easier to execute that way |
| 5073 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5074 | break; |
| 5075 | case dest_buffer: |
| 5076 | // include b: with the name, easier to execute that way |
| 5077 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5078 | break; |
| 5079 | case dest_window: |
| 5080 | // include w: with the name, easier to execute that way |
| 5081 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5082 | break; |
| 5083 | case dest_tab: |
| 5084 | // include t: with the name, easier to execute that way |
| 5085 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5086 | break; |
| 5087 | case dest_env: |
| 5088 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5089 | break; |
| 5090 | case dest_reg: |
| 5091 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5092 | break; |
| 5093 | case dest_vimvar: |
| 5094 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5095 | break; |
| 5096 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5097 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5098 | if (scriptvar_idx < 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5099 | { |
| 5100 | char_u *name_s = name; |
| 5101 | |
| 5102 | // Include s: in the name for store_var() |
| 5103 | if (name[1] != ':') |
| 5104 | { |
| 5105 | int len = (int)STRLEN(name) + 3; |
| 5106 | |
| 5107 | name_s = alloc(len); |
| 5108 | if (name_s == NULL) |
| 5109 | name_s = name; |
| 5110 | else |
| 5111 | vim_snprintf((char *)name_s, len, |
| 5112 | "s:%s", name); |
| 5113 | } |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5114 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, |
| 5115 | scriptvar_sid, type); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5116 | if (name_s != name) |
| 5117 | vim_free(name_s); |
| 5118 | } |
| 5119 | else |
| 5120 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5121 | scriptvar_sid, scriptvar_idx, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5122 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5123 | break; |
| 5124 | case dest_local: |
| 5125 | if (lvar != NULL) |
| 5126 | { |
| 5127 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5128 | + instr->ga_len - 1; |
| 5129 | |
| 5130 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5131 | // ISN_STORE into ISN_STORENR |
| 5132 | if (!lvar->lv_from_outer |
| 5133 | && instr->ga_len == instr_count + 1 |
| 5134 | && isn->isn_type == ISN_PUSHNR) |
| 5135 | { |
| 5136 | varnumber_T val = isn->isn_arg.number; |
| 5137 | |
| 5138 | isn->isn_type = ISN_STORENR; |
| 5139 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5140 | isn->isn_arg.storenr.stnr_val = val; |
| 5141 | if (stack->ga_len > 0) |
| 5142 | --stack->ga_len; |
| 5143 | } |
| 5144 | else if (lvar->lv_from_outer) |
| 5145 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5146 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5147 | else |
| 5148 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5149 | } |
| 5150 | break; |
| 5151 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5152 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5153 | |
| 5154 | if (var_idx + 1 < var_count) |
| 5155 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5156 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5157 | |
| 5158 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5159 | if (var_count > 0 && !semicolon) |
| 5160 | { |
| 5161 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5162 | goto theend; |
| 5163 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5164 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5165 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5166 | |
| 5167 | theend: |
| 5168 | vim_free(name); |
| 5169 | return ret; |
| 5170 | } |
| 5171 | |
| 5172 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5173 | * Check if "name" can be "unlet". |
| 5174 | */ |
| 5175 | int |
| 5176 | check_vim9_unlet(char_u *name) |
| 5177 | { |
| 5178 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5179 | { |
| 5180 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5181 | return FAIL; |
| 5182 | } |
| 5183 | return OK; |
| 5184 | } |
| 5185 | |
| 5186 | /* |
| 5187 | * Callback passed to ex_unletlock(). |
| 5188 | */ |
| 5189 | static int |
| 5190 | compile_unlet( |
| 5191 | lval_T *lvp, |
| 5192 | char_u *name_end, |
| 5193 | exarg_T *eap, |
| 5194 | int deep UNUSED, |
| 5195 | void *coookie) |
| 5196 | { |
| 5197 | cctx_T *cctx = coookie; |
| 5198 | |
| 5199 | if (lvp->ll_tv == NULL) |
| 5200 | { |
| 5201 | char_u *p = lvp->ll_name; |
| 5202 | int cc = *name_end; |
| 5203 | int ret = OK; |
| 5204 | |
| 5205 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5206 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5207 | if (*p == '$') |
| 5208 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5209 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5210 | ret = FAIL; |
| 5211 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5212 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5213 | |
| 5214 | *name_end = cc; |
| 5215 | return ret; |
| 5216 | } |
| 5217 | |
| 5218 | // TODO: unlet {list}[idx] |
| 5219 | // TODO: unlet {dict}[key] |
| 5220 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5221 | return FAIL; |
| 5222 | } |
| 5223 | |
| 5224 | /* |
| 5225 | * compile "unlet var", "lock var" and "unlock var" |
| 5226 | * "arg" points to "var". |
| 5227 | */ |
| 5228 | static char_u * |
| 5229 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5230 | { |
| 5231 | char_u *p = arg; |
| 5232 | |
| 5233 | if (eap->cmdidx != CMD_unlet) |
| 5234 | { |
| 5235 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5236 | return NULL; |
| 5237 | } |
| 5238 | |
| 5239 | if (*p == '!') |
| 5240 | { |
| 5241 | p = skipwhite(p + 1); |
| 5242 | eap->forceit = TRUE; |
| 5243 | } |
| 5244 | |
| 5245 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5246 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5247 | } |
| 5248 | |
| 5249 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5250 | * Compile an :import command. |
| 5251 | */ |
| 5252 | static char_u * |
| 5253 | compile_import(char_u *arg, cctx_T *cctx) |
| 5254 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5255 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5256 | } |
| 5257 | |
| 5258 | /* |
| 5259 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5260 | */ |
| 5261 | static int |
| 5262 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5263 | { |
| 5264 | garray_T *instr = &cctx->ctx_instr; |
| 5265 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5266 | |
| 5267 | if (endlabel == NULL) |
| 5268 | return FAIL; |
| 5269 | endlabel->el_next = *el; |
| 5270 | *el = endlabel; |
| 5271 | endlabel->el_end_label = instr->ga_len; |
| 5272 | |
| 5273 | generate_JUMP(cctx, when, 0); |
| 5274 | return OK; |
| 5275 | } |
| 5276 | |
| 5277 | static void |
| 5278 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5279 | { |
| 5280 | garray_T *instr = &cctx->ctx_instr; |
| 5281 | |
| 5282 | while (*el != NULL) |
| 5283 | { |
| 5284 | endlabel_T *cur = (*el); |
| 5285 | isn_T *isn; |
| 5286 | |
| 5287 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5288 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5289 | *el = cur->el_next; |
| 5290 | vim_free(cur); |
| 5291 | } |
| 5292 | } |
| 5293 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5294 | static void |
| 5295 | compile_free_jump_to_end(endlabel_T **el) |
| 5296 | { |
| 5297 | while (*el != NULL) |
| 5298 | { |
| 5299 | endlabel_T *cur = (*el); |
| 5300 | |
| 5301 | *el = cur->el_next; |
| 5302 | vim_free(cur); |
| 5303 | } |
| 5304 | } |
| 5305 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5306 | /* |
| 5307 | * Create a new scope and set up the generic items. |
| 5308 | */ |
| 5309 | static scope_T * |
| 5310 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5311 | { |
| 5312 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5313 | |
| 5314 | if (scope == NULL) |
| 5315 | return NULL; |
| 5316 | scope->se_outer = cctx->ctx_scope; |
| 5317 | cctx->ctx_scope = scope; |
| 5318 | scope->se_type = type; |
| 5319 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5320 | return scope; |
| 5321 | } |
| 5322 | |
| 5323 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5324 | * Free the current scope and go back to the outer scope. |
| 5325 | */ |
| 5326 | static void |
| 5327 | drop_scope(cctx_T *cctx) |
| 5328 | { |
| 5329 | scope_T *scope = cctx->ctx_scope; |
| 5330 | |
| 5331 | if (scope == NULL) |
| 5332 | { |
| 5333 | iemsg("calling drop_scope() without a scope"); |
| 5334 | return; |
| 5335 | } |
| 5336 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5337 | switch (scope->se_type) |
| 5338 | { |
| 5339 | case IF_SCOPE: |
| 5340 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5341 | case FOR_SCOPE: |
| 5342 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5343 | case WHILE_SCOPE: |
| 5344 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5345 | case TRY_SCOPE: |
| 5346 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5347 | case NO_SCOPE: |
| 5348 | case BLOCK_SCOPE: |
| 5349 | break; |
| 5350 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5351 | vim_free(scope); |
| 5352 | } |
| 5353 | |
| 5354 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5355 | * compile "if expr" |
| 5356 | * |
| 5357 | * "if expr" Produces instructions: |
| 5358 | * EVAL expr Push result of "expr" |
| 5359 | * JUMP_IF_FALSE end |
| 5360 | * ... body ... |
| 5361 | * end: |
| 5362 | * |
| 5363 | * "if expr | else" Produces instructions: |
| 5364 | * EVAL expr Push result of "expr" |
| 5365 | * JUMP_IF_FALSE else |
| 5366 | * ... body ... |
| 5367 | * JUMP_ALWAYS end |
| 5368 | * else: |
| 5369 | * ... body ... |
| 5370 | * end: |
| 5371 | * |
| 5372 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5373 | * EVAL expr Push result of "expr" |
| 5374 | * JUMP_IF_FALSE elseif |
| 5375 | * ... body ... |
| 5376 | * JUMP_ALWAYS end |
| 5377 | * elseif: |
| 5378 | * EVAL expr Push result of "expr" |
| 5379 | * JUMP_IF_FALSE else |
| 5380 | * ... body ... |
| 5381 | * JUMP_ALWAYS end |
| 5382 | * else: |
| 5383 | * ... body ... |
| 5384 | * end: |
| 5385 | */ |
| 5386 | static char_u * |
| 5387 | compile_if(char_u *arg, cctx_T *cctx) |
| 5388 | { |
| 5389 | char_u *p = arg; |
| 5390 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5391 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5392 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5393 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5394 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5395 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5396 | CLEAR_FIELD(ppconst); |
| 5397 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5398 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5399 | clear_ppconst(&ppconst); |
| 5400 | return NULL; |
| 5401 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5402 | if (cctx->ctx_skip == SKIP_YES) |
| 5403 | clear_ppconst(&ppconst); |
| 5404 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5405 | { |
| 5406 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5407 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5408 | clear_ppconst(&ppconst); |
| 5409 | } |
| 5410 | else |
| 5411 | { |
| 5412 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5413 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5414 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5415 | return NULL; |
| 5416 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5417 | |
| 5418 | scope = new_scope(cctx, IF_SCOPE); |
| 5419 | if (scope == NULL) |
| 5420 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5421 | scope->se_skip_save = skip_save; |
| 5422 | // "is_had_return" will be reset if any block does not end in :return |
| 5423 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5424 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5425 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5426 | { |
| 5427 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5428 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5429 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5430 | } |
| 5431 | else |
| 5432 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5433 | |
| 5434 | return p; |
| 5435 | } |
| 5436 | |
| 5437 | static char_u * |
| 5438 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 5439 | { |
| 5440 | char_u *p = arg; |
| 5441 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5442 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5443 | isn_T *isn; |
| 5444 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5445 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5446 | |
| 5447 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5448 | { |
| 5449 | emsg(_(e_elseif_without_if)); |
| 5450 | return NULL; |
| 5451 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5452 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5453 | if (!cctx->ctx_had_return) |
| 5454 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5455 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5456 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5457 | { |
| 5458 | 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] | 5459 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5460 | return NULL; |
| 5461 | // previous "if" or "elseif" jumps here |
| 5462 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5463 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5464 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5465 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5466 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5467 | CLEAR_FIELD(ppconst); |
| 5468 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5469 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5470 | clear_ppconst(&ppconst); |
| 5471 | return NULL; |
| 5472 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5473 | if (scope->se_skip_save == SKIP_YES) |
| 5474 | clear_ppconst(&ppconst); |
| 5475 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5476 | { |
| 5477 | // The expression results in a constant. |
| 5478 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5479 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5480 | clear_ppconst(&ppconst); |
| 5481 | scope->se_u.se_if.is_if_label = -1; |
| 5482 | } |
| 5483 | else |
| 5484 | { |
| 5485 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5486 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5487 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5488 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5489 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5490 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5491 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5492 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5493 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5494 | |
| 5495 | return p; |
| 5496 | } |
| 5497 | |
| 5498 | static char_u * |
| 5499 | compile_else(char_u *arg, cctx_T *cctx) |
| 5500 | { |
| 5501 | char_u *p = arg; |
| 5502 | garray_T *instr = &cctx->ctx_instr; |
| 5503 | isn_T *isn; |
| 5504 | scope_T *scope = cctx->ctx_scope; |
| 5505 | |
| 5506 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5507 | { |
| 5508 | emsg(_(e_else_without_if)); |
| 5509 | return NULL; |
| 5510 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5511 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5512 | if (!cctx->ctx_had_return) |
| 5513 | scope->se_u.se_if.is_had_return = FALSE; |
| 5514 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5515 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5516 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5517 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5518 | // 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] | 5519 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5520 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5521 | if (!cctx->ctx_had_return |
| 5522 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 5523 | JUMP_ALWAYS, cctx) == FAIL) |
| 5524 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5525 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5526 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5527 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5528 | { |
| 5529 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5530 | { |
| 5531 | // previous "if" or "elseif" jumps here |
| 5532 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5533 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5534 | scope->se_u.se_if.is_if_label = -1; |
| 5535 | } |
| 5536 | } |
| 5537 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5538 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5539 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 5540 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5541 | |
| 5542 | return p; |
| 5543 | } |
| 5544 | |
| 5545 | static char_u * |
| 5546 | compile_endif(char_u *arg, cctx_T *cctx) |
| 5547 | { |
| 5548 | scope_T *scope = cctx->ctx_scope; |
| 5549 | ifscope_T *ifscope; |
| 5550 | garray_T *instr = &cctx->ctx_instr; |
| 5551 | isn_T *isn; |
| 5552 | |
| 5553 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5554 | { |
| 5555 | emsg(_(e_endif_without_if)); |
| 5556 | return NULL; |
| 5557 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5558 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5559 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5560 | if (!cctx->ctx_had_return) |
| 5561 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5562 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5563 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5564 | { |
| 5565 | // previous "if" or "elseif" jumps here |
| 5566 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5567 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5568 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5569 | // Fill in the "end" label in jumps at the end of the blocks. |
| 5570 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5571 | cctx->ctx_skip = scope->se_skip_save; |
| 5572 | |
| 5573 | // If all the blocks end in :return and there is an :else then the |
| 5574 | // had_return flag is set. |
| 5575 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5576 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5577 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5578 | return arg; |
| 5579 | } |
| 5580 | |
| 5581 | /* |
| 5582 | * compile "for var in expr" |
| 5583 | * |
| 5584 | * Produces instructions: |
| 5585 | * PUSHNR -1 |
| 5586 | * STORE loop-idx Set index to -1 |
| 5587 | * EVAL expr Push result of "expr" |
| 5588 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 5589 | * - if beyond end, jump to "end" |
| 5590 | * - otherwise get item from list and push it |
| 5591 | * STORE var Store item in "var" |
| 5592 | * ... body ... |
| 5593 | * JUMP top Jump back to repeat |
| 5594 | * end: DROP Drop the result of "expr" |
| 5595 | * |
| 5596 | */ |
| 5597 | static char_u * |
| 5598 | compile_for(char_u *arg, cctx_T *cctx) |
| 5599 | { |
| 5600 | char_u *p; |
| 5601 | size_t varlen; |
| 5602 | garray_T *instr = &cctx->ctx_instr; |
| 5603 | garray_T *stack = &cctx->ctx_type_stack; |
| 5604 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5605 | lvar_T *loop_lvar; // loop iteration variable |
| 5606 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5607 | type_T *vartype; |
| 5608 | |
| 5609 | // TODO: list of variables: "for [key, value] in dict" |
| 5610 | // parse "var" |
| 5611 | for (p = arg; eval_isnamec1(*p); ++p) |
| 5612 | ; |
| 5613 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5614 | var_lvar = lookup_local(arg, varlen, cctx); |
| 5615 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5616 | { |
| 5617 | semsg(_("E1023: variable already defined: %s"), arg); |
| 5618 | return NULL; |
| 5619 | } |
| 5620 | |
| 5621 | // consume "in" |
| 5622 | p = skipwhite(p); |
| 5623 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 5624 | { |
| 5625 | emsg(_(e_missing_in)); |
| 5626 | return NULL; |
| 5627 | } |
| 5628 | p = skipwhite(p + 2); |
| 5629 | |
| 5630 | |
| 5631 | scope = new_scope(cctx, FOR_SCOPE); |
| 5632 | if (scope == NULL) |
| 5633 | return NULL; |
| 5634 | |
| 5635 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5636 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 5637 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5638 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5639 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5640 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5641 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5642 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5643 | |
| 5644 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5645 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 5646 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5647 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5648 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5649 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5650 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5651 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5652 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5653 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5654 | |
| 5655 | // compile "expr", it remains on the stack until "endfor" |
| 5656 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5657 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5658 | { |
| 5659 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5660 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5661 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5662 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5663 | // Now that we know the type of "var", check that it is a list, now or at |
| 5664 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5665 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5666 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5667 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5668 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5669 | return NULL; |
| 5670 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5671 | 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] | 5672 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5673 | |
| 5674 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5675 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5676 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5677 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 5678 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5679 | |
| 5680 | return arg; |
| 5681 | } |
| 5682 | |
| 5683 | /* |
| 5684 | * compile "endfor" |
| 5685 | */ |
| 5686 | static char_u * |
| 5687 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 5688 | { |
| 5689 | garray_T *instr = &cctx->ctx_instr; |
| 5690 | scope_T *scope = cctx->ctx_scope; |
| 5691 | forscope_T *forscope; |
| 5692 | isn_T *isn; |
| 5693 | |
| 5694 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 5695 | { |
| 5696 | emsg(_(e_for)); |
| 5697 | return NULL; |
| 5698 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5699 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5700 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5701 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5702 | |
| 5703 | // At end of ":for" scope jump back to the FOR instruction. |
| 5704 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 5705 | |
| 5706 | // Fill in the "end" label in the FOR statement so it can jump here |
| 5707 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 5708 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 5709 | |
| 5710 | // Fill in the "end" label any BREAK statements |
| 5711 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 5712 | |
| 5713 | // Below the ":for" scope drop the "expr" list from the stack. |
| 5714 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5715 | return NULL; |
| 5716 | |
| 5717 | vim_free(scope); |
| 5718 | |
| 5719 | return arg; |
| 5720 | } |
| 5721 | |
| 5722 | /* |
| 5723 | * compile "while expr" |
| 5724 | * |
| 5725 | * Produces instructions: |
| 5726 | * top: EVAL expr Push result of "expr" |
| 5727 | * JUMP_IF_FALSE end jump if false |
| 5728 | * ... body ... |
| 5729 | * JUMP top Jump back to repeat |
| 5730 | * end: |
| 5731 | * |
| 5732 | */ |
| 5733 | static char_u * |
| 5734 | compile_while(char_u *arg, cctx_T *cctx) |
| 5735 | { |
| 5736 | char_u *p = arg; |
| 5737 | garray_T *instr = &cctx->ctx_instr; |
| 5738 | scope_T *scope; |
| 5739 | |
| 5740 | scope = new_scope(cctx, WHILE_SCOPE); |
| 5741 | if (scope == NULL) |
| 5742 | return NULL; |
| 5743 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5744 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5745 | |
| 5746 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5747 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5748 | return NULL; |
| 5749 | |
| 5750 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5751 | 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] | 5752 | JUMP_IF_FALSE, cctx) == FAIL) |
| 5753 | return FAIL; |
| 5754 | |
| 5755 | return p; |
| 5756 | } |
| 5757 | |
| 5758 | /* |
| 5759 | * compile "endwhile" |
| 5760 | */ |
| 5761 | static char_u * |
| 5762 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 5763 | { |
| 5764 | scope_T *scope = cctx->ctx_scope; |
| 5765 | |
| 5766 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 5767 | { |
| 5768 | emsg(_(e_while)); |
| 5769 | return NULL; |
| 5770 | } |
| 5771 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5772 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5773 | |
| 5774 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5775 | 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] | 5776 | |
| 5777 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 5778 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5779 | 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] | 5780 | |
| 5781 | vim_free(scope); |
| 5782 | |
| 5783 | return arg; |
| 5784 | } |
| 5785 | |
| 5786 | /* |
| 5787 | * compile "continue" |
| 5788 | */ |
| 5789 | static char_u * |
| 5790 | compile_continue(char_u *arg, cctx_T *cctx) |
| 5791 | { |
| 5792 | scope_T *scope = cctx->ctx_scope; |
| 5793 | |
| 5794 | for (;;) |
| 5795 | { |
| 5796 | if (scope == NULL) |
| 5797 | { |
| 5798 | emsg(_(e_continue)); |
| 5799 | return NULL; |
| 5800 | } |
| 5801 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5802 | break; |
| 5803 | scope = scope->se_outer; |
| 5804 | } |
| 5805 | |
| 5806 | // Jump back to the FOR or WHILE instruction. |
| 5807 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5808 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 5809 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5810 | return arg; |
| 5811 | } |
| 5812 | |
| 5813 | /* |
| 5814 | * compile "break" |
| 5815 | */ |
| 5816 | static char_u * |
| 5817 | compile_break(char_u *arg, cctx_T *cctx) |
| 5818 | { |
| 5819 | scope_T *scope = cctx->ctx_scope; |
| 5820 | endlabel_T **el; |
| 5821 | |
| 5822 | for (;;) |
| 5823 | { |
| 5824 | if (scope == NULL) |
| 5825 | { |
| 5826 | emsg(_(e_break)); |
| 5827 | return NULL; |
| 5828 | } |
| 5829 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5830 | break; |
| 5831 | scope = scope->se_outer; |
| 5832 | } |
| 5833 | |
| 5834 | // Jump to the end of the FOR or WHILE loop. |
| 5835 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5836 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5837 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5838 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5839 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 5840 | return FAIL; |
| 5841 | |
| 5842 | return arg; |
| 5843 | } |
| 5844 | |
| 5845 | /* |
| 5846 | * compile "{" start of block |
| 5847 | */ |
| 5848 | static char_u * |
| 5849 | compile_block(char_u *arg, cctx_T *cctx) |
| 5850 | { |
| 5851 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 5852 | return NULL; |
| 5853 | return skipwhite(arg + 1); |
| 5854 | } |
| 5855 | |
| 5856 | /* |
| 5857 | * compile end of block: drop one scope |
| 5858 | */ |
| 5859 | static void |
| 5860 | compile_endblock(cctx_T *cctx) |
| 5861 | { |
| 5862 | scope_T *scope = cctx->ctx_scope; |
| 5863 | |
| 5864 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5865 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5866 | vim_free(scope); |
| 5867 | } |
| 5868 | |
| 5869 | /* |
| 5870 | * compile "try" |
| 5871 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 5872 | * finally. |
| 5873 | * Creates another scope for the "try" block itself. |
| 5874 | * TRY instruction sets up exception handling at runtime. |
| 5875 | * |
| 5876 | * "try" |
| 5877 | * TRY -> catch1, -> finally push trystack entry |
| 5878 | * ... try block |
| 5879 | * "throw {exception}" |
| 5880 | * EVAL {exception} |
| 5881 | * THROW create exception |
| 5882 | * ... try block |
| 5883 | * " catch {expr}" |
| 5884 | * JUMP -> finally |
| 5885 | * catch1: PUSH exeception |
| 5886 | * EVAL {expr} |
| 5887 | * MATCH |
| 5888 | * JUMP nomatch -> catch2 |
| 5889 | * CATCH remove exception |
| 5890 | * ... catch block |
| 5891 | * " catch" |
| 5892 | * JUMP -> finally |
| 5893 | * catch2: CATCH remove exception |
| 5894 | * ... catch block |
| 5895 | * " finally" |
| 5896 | * finally: |
| 5897 | * ... finally block |
| 5898 | * " endtry" |
| 5899 | * ENDTRY pop trystack entry, may rethrow |
| 5900 | */ |
| 5901 | static char_u * |
| 5902 | compile_try(char_u *arg, cctx_T *cctx) |
| 5903 | { |
| 5904 | garray_T *instr = &cctx->ctx_instr; |
| 5905 | scope_T *try_scope; |
| 5906 | scope_T *scope; |
| 5907 | |
| 5908 | // scope that holds the jumps that go to catch/finally/endtry |
| 5909 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 5910 | if (try_scope == NULL) |
| 5911 | return NULL; |
| 5912 | |
| 5913 | // "catch" is set when the first ":catch" is found. |
| 5914 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5915 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5916 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 5917 | return NULL; |
| 5918 | |
| 5919 | // scope for the try block itself |
| 5920 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 5921 | if (scope == NULL) |
| 5922 | return NULL; |
| 5923 | |
| 5924 | return arg; |
| 5925 | } |
| 5926 | |
| 5927 | /* |
| 5928 | * compile "catch {expr}" |
| 5929 | */ |
| 5930 | static char_u * |
| 5931 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 5932 | { |
| 5933 | scope_T *scope = cctx->ctx_scope; |
| 5934 | garray_T *instr = &cctx->ctx_instr; |
| 5935 | char_u *p; |
| 5936 | isn_T *isn; |
| 5937 | |
| 5938 | // end block scope from :try or :catch |
| 5939 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5940 | compile_endblock(cctx); |
| 5941 | scope = cctx->ctx_scope; |
| 5942 | |
| 5943 | // Error if not in a :try scope |
| 5944 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 5945 | { |
| 5946 | emsg(_(e_catch)); |
| 5947 | return NULL; |
| 5948 | } |
| 5949 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5950 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5951 | { |
| 5952 | emsg(_("E1033: catch unreachable after catch-all")); |
| 5953 | return NULL; |
| 5954 | } |
| 5955 | |
| 5956 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5957 | 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] | 5958 | JUMP_ALWAYS, cctx) == FAIL) |
| 5959 | return NULL; |
| 5960 | |
| 5961 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5962 | 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] | 5963 | if (isn->isn_arg.try.try_catch == 0) |
| 5964 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5965 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5966 | { |
| 5967 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5968 | 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] | 5969 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5970 | } |
| 5971 | |
| 5972 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 5973 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5974 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5975 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 5976 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5977 | } |
| 5978 | else |
| 5979 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5980 | char_u *end; |
| 5981 | char_u *pat; |
| 5982 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5983 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 5984 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5985 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5986 | // Push v:exception, push {expr} and MATCH |
| 5987 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 5988 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5989 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5990 | if (*end != *p) |
| 5991 | { |
| 5992 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 5993 | vim_free(tofree); |
| 5994 | return FAIL; |
| 5995 | } |
| 5996 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 5997 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5998 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5999 | len = (int)(end - tofree); |
| 6000 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6001 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6002 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6003 | if (pat == NULL) |
| 6004 | return FAIL; |
| 6005 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6006 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6007 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6008 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6009 | return NULL; |
| 6010 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6011 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6012 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6013 | return NULL; |
| 6014 | } |
| 6015 | |
| 6016 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6017 | return NULL; |
| 6018 | |
| 6019 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6020 | return NULL; |
| 6021 | return p; |
| 6022 | } |
| 6023 | |
| 6024 | static char_u * |
| 6025 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6026 | { |
| 6027 | scope_T *scope = cctx->ctx_scope; |
| 6028 | garray_T *instr = &cctx->ctx_instr; |
| 6029 | isn_T *isn; |
| 6030 | |
| 6031 | // end block scope from :try or :catch |
| 6032 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6033 | compile_endblock(cctx); |
| 6034 | scope = cctx->ctx_scope; |
| 6035 | |
| 6036 | // Error if not in a :try scope |
| 6037 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6038 | { |
| 6039 | emsg(_(e_finally)); |
| 6040 | return NULL; |
| 6041 | } |
| 6042 | |
| 6043 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6044 | 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] | 6045 | if (isn->isn_arg.try.try_finally != 0) |
| 6046 | { |
| 6047 | emsg(_(e_finally_dup)); |
| 6048 | return NULL; |
| 6049 | } |
| 6050 | |
| 6051 | // 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] | 6052 | 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] | 6053 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6054 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6055 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6056 | { |
| 6057 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6058 | 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] | 6059 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6060 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6061 | } |
| 6062 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6063 | // TODO: set index in ts_finally_label jumps |
| 6064 | |
| 6065 | return arg; |
| 6066 | } |
| 6067 | |
| 6068 | static char_u * |
| 6069 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6070 | { |
| 6071 | scope_T *scope = cctx->ctx_scope; |
| 6072 | garray_T *instr = &cctx->ctx_instr; |
| 6073 | isn_T *isn; |
| 6074 | |
| 6075 | // end block scope from :catch or :finally |
| 6076 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6077 | compile_endblock(cctx); |
| 6078 | scope = cctx->ctx_scope; |
| 6079 | |
| 6080 | // Error if not in a :try scope |
| 6081 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6082 | { |
| 6083 | if (scope == NULL) |
| 6084 | emsg(_(e_no_endtry)); |
| 6085 | else if (scope->se_type == WHILE_SCOPE) |
| 6086 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6087 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6088 | emsg(_(e_endfor)); |
| 6089 | else |
| 6090 | emsg(_(e_endif)); |
| 6091 | return NULL; |
| 6092 | } |
| 6093 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6094 | 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] | 6095 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6096 | { |
| 6097 | emsg(_("E1032: missing :catch or :finally")); |
| 6098 | return NULL; |
| 6099 | } |
| 6100 | |
| 6101 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6102 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6103 | 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] | 6104 | |
| 6105 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6106 | if (isn->isn_arg.try.try_catch == 0) |
| 6107 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6108 | if (isn->isn_arg.try.try_finally == 0) |
| 6109 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6110 | |
| 6111 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6112 | { |
| 6113 | // Last catch without match jumps here |
| 6114 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6115 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6116 | } |
| 6117 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6118 | compile_endblock(cctx); |
| 6119 | |
| 6120 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6121 | return NULL; |
| 6122 | return arg; |
| 6123 | } |
| 6124 | |
| 6125 | /* |
| 6126 | * compile "throw {expr}" |
| 6127 | */ |
| 6128 | static char_u * |
| 6129 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6130 | { |
| 6131 | char_u *p = skipwhite(arg); |
| 6132 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6133 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6134 | return NULL; |
| 6135 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6136 | return NULL; |
| 6137 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6138 | return NULL; |
| 6139 | |
| 6140 | return p; |
| 6141 | } |
| 6142 | |
| 6143 | /* |
| 6144 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6145 | * compile "echomsg expr" |
| 6146 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6147 | * compile "execute expr" |
| 6148 | */ |
| 6149 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6150 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6151 | { |
| 6152 | char_u *p = arg; |
| 6153 | int count = 0; |
| 6154 | |
| 6155 | for (;;) |
| 6156 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6157 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6158 | return NULL; |
| 6159 | ++count; |
| 6160 | p = skipwhite(p); |
| 6161 | if (ends_excmd(*p)) |
| 6162 | break; |
| 6163 | } |
| 6164 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6165 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6166 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6167 | else if (cmdidx == CMD_execute) |
| 6168 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6169 | else if (cmdidx == CMD_echomsg) |
| 6170 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6171 | else |
| 6172 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6173 | return p; |
| 6174 | } |
| 6175 | |
| 6176 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6177 | * A command that is not compiled, execute with legacy code. |
| 6178 | */ |
| 6179 | static char_u * |
| 6180 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6181 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6182 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6183 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6184 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6185 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6186 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6187 | goto theend; |
| 6188 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6189 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6190 | { |
| 6191 | long argt = excmd_get_argt(eap->cmdidx); |
| 6192 | int usefilter = FALSE; |
| 6193 | |
| 6194 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6195 | |
| 6196 | // If the command can be followed by a bar, find the bar and truncate |
| 6197 | // it, so that the following command can be compiled. |
| 6198 | // The '|' is overwritten with a NUL, it is put back below. |
| 6199 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6200 | && *eap->arg == '!') |
| 6201 | // :w !filter or :r !filter or :r! filter |
| 6202 | usefilter = TRUE; |
| 6203 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6204 | { |
| 6205 | separate_nextcmd(eap); |
| 6206 | if (eap->nextcmd != NULL) |
| 6207 | nextcmd = eap->nextcmd; |
| 6208 | } |
| 6209 | } |
| 6210 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6211 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6212 | { |
| 6213 | // expand filename in "syntax include [@group] filename" |
| 6214 | has_expr = TRUE; |
| 6215 | eap->arg = skipwhite(eap->arg + 7); |
| 6216 | if (*eap->arg == '@') |
| 6217 | eap->arg = skiptowhite(eap->arg); |
| 6218 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6219 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6220 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6221 | { |
| 6222 | int count = 0; |
| 6223 | char_u *start = skipwhite(line); |
| 6224 | |
| 6225 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6226 | // PUSHS ":cmd xxx" |
| 6227 | // eval expr1 |
| 6228 | // PUSHS "yyy" |
| 6229 | // eval expr2 |
| 6230 | // PUSHS "zzz" |
| 6231 | // EXECCONCAT 5 |
| 6232 | for (;;) |
| 6233 | { |
| 6234 | if (p > start) |
| 6235 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6236 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6237 | ++count; |
| 6238 | } |
| 6239 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6240 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6241 | return NULL; |
| 6242 | may_generate_2STRING(-1, cctx); |
| 6243 | ++count; |
| 6244 | p = skipwhite(p); |
| 6245 | if (*p != '`') |
| 6246 | { |
| 6247 | emsg(_("E1083: missing backtick")); |
| 6248 | return NULL; |
| 6249 | } |
| 6250 | start = p + 1; |
| 6251 | |
| 6252 | p = (char_u *)strstr((char *)start, "`="); |
| 6253 | if (p == NULL) |
| 6254 | { |
| 6255 | if (*skipwhite(start) != NUL) |
| 6256 | { |
| 6257 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6258 | ++count; |
| 6259 | } |
| 6260 | break; |
| 6261 | } |
| 6262 | } |
| 6263 | generate_EXECCONCAT(cctx, count); |
| 6264 | } |
| 6265 | else |
| 6266 | generate_EXEC(cctx, line); |
| 6267 | |
| 6268 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6269 | if (*nextcmd != NUL) |
| 6270 | { |
| 6271 | // the parser expects a pointer to the bar, put it back |
| 6272 | --nextcmd; |
| 6273 | *nextcmd = '|'; |
| 6274 | } |
| 6275 | |
| 6276 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6277 | } |
| 6278 | |
| 6279 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6280 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6281 | * 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] | 6282 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6283 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6284 | add_def_function(ufunc_T *ufunc) |
| 6285 | { |
| 6286 | dfunc_T *dfunc; |
| 6287 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6288 | if (def_functions.ga_len == 0) |
| 6289 | { |
| 6290 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6291 | // wasn't set. |
| 6292 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6293 | return FAIL; |
| 6294 | ++def_functions.ga_len; |
| 6295 | } |
| 6296 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6297 | // Add the function to "def_functions". |
| 6298 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6299 | return FAIL; |
| 6300 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6301 | CLEAR_POINTER(dfunc); |
| 6302 | dfunc->df_idx = def_functions.ga_len; |
| 6303 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6304 | dfunc->df_ufunc = ufunc; |
| 6305 | ++def_functions.ga_len; |
| 6306 | return OK; |
| 6307 | } |
| 6308 | |
| 6309 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6310 | * After ex_function() has collected all the function lines: parse and compile |
| 6311 | * the lines into instructions. |
| 6312 | * Adds the function to "def_functions". |
| 6313 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6314 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6315 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6316 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6317 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6318 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6319 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6320 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6321 | 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] | 6322 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6323 | char_u *line = NULL; |
| 6324 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6325 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6326 | cctx_T cctx; |
| 6327 | garray_T *instr; |
| 6328 | int called_emsg_before = called_emsg; |
| 6329 | int ret = FAIL; |
| 6330 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6331 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6332 | int emsg_before = called_emsg; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6333 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6334 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6335 | // When using a function that was compiled before: Free old instructions. |
| 6336 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6337 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6338 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6339 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6340 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6341 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6342 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6343 | else |
| 6344 | { |
| 6345 | if (add_def_function(ufunc) == FAIL) |
| 6346 | return FAIL; |
| 6347 | new_def_function = TRUE; |
| 6348 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6349 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6350 | ufunc->uf_def_status = UF_COMPILING; |
| 6351 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6352 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6353 | cctx.ctx_ufunc = ufunc; |
| 6354 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6355 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6356 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6357 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6358 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6359 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6360 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6361 | instr = &cctx.ctx_instr; |
| 6362 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6363 | // Set the context to the function, it may be compiled when called from |
| 6364 | // another script. Set the script version to the most modern one. |
| 6365 | // The line number will be set in next_line_from_context(). |
| 6366 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6367 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6368 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6369 | // Make sure error messages are OK. |
| 6370 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6371 | if (do_estack_push) |
| 6372 | estack_push_ufunc(ufunc, 1); |
| 6373 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6374 | if (ufunc->uf_def_args.ga_len > 0) |
| 6375 | { |
| 6376 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6377 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6378 | int i; |
| 6379 | char_u *arg; |
| 6380 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6381 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6382 | |
| 6383 | // Produce instructions for the default values of optional arguments. |
| 6384 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6385 | // where to start when the function is called, depending on the number |
| 6386 | // of arguments. |
| 6387 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6388 | if (ufunc->uf_def_arg_idx == NULL) |
| 6389 | goto erret; |
| 6390 | for (i = 0; i < count; ++i) |
| 6391 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6392 | garray_T *stack = &cctx.ctx_type_stack; |
| 6393 | type_T *val_type; |
| 6394 | int arg_idx = first_def_arg + i; |
| 6395 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6396 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6397 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6398 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6399 | goto erret; |
| 6400 | |
| 6401 | // If no type specified use the type of the default value. |
| 6402 | // Otherwise check that the default value type matches the |
| 6403 | // specified type. |
| 6404 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6405 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6406 | { |
| 6407 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6408 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6409 | } |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 6410 | else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, FALSE) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6411 | == FAIL) |
| 6412 | { |
| 6413 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 6414 | arg_idx + 1); |
| 6415 | goto erret; |
| 6416 | } |
| 6417 | |
| 6418 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6419 | goto erret; |
| 6420 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6421 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6422 | |
| 6423 | if (did_set_arg_type) |
| 6424 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6425 | } |
| 6426 | |
| 6427 | /* |
| 6428 | * Loop over all the lines of the function and generate instructions. |
| 6429 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6430 | for (;;) |
| 6431 | { |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6432 | exarg_T ea; |
| 6433 | cmdmod_T save_cmdmod; |
| 6434 | int starts_with_colon = FALSE; |
| 6435 | char_u *cmd; |
| 6436 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6437 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6438 | // Bail out on the first error to avoid a flood of errors and report |
| 6439 | // the right line number when inside try/catch. |
| 6440 | if (emsg_before != called_emsg) |
| 6441 | goto erret; |
| 6442 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6443 | if (line != NULL && *line == '|') |
| 6444 | // the line continues after a '|' |
| 6445 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 6446 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6447 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 6448 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6449 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 6450 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6451 | goto erret; |
| 6452 | } |
| 6453 | else |
| 6454 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 6455 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 6456 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6457 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6458 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6459 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6460 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6461 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6462 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6463 | ea.cmdlinep = &line; |
| 6464 | ea.cmd = skipwhite(line); |
| 6465 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6466 | // Some things can be recognized by the first character. |
| 6467 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6468 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6469 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6470 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6471 | if (ea.cmd[1] != '{') |
| 6472 | { |
| 6473 | line = (char_u *)""; |
| 6474 | continue; |
| 6475 | } |
| 6476 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6477 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6478 | case '}': |
| 6479 | { |
| 6480 | // "}" ends a block scope |
| 6481 | scopetype_T stype = cctx.ctx_scope == NULL |
| 6482 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6483 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6484 | if (stype == BLOCK_SCOPE) |
| 6485 | { |
| 6486 | compile_endblock(&cctx); |
| 6487 | line = ea.cmd; |
| 6488 | } |
| 6489 | else |
| 6490 | { |
| 6491 | emsg(_("E1025: using } outside of a block scope")); |
| 6492 | goto erret; |
| 6493 | } |
| 6494 | if (line != NULL) |
| 6495 | line = skipwhite(ea.cmd + 1); |
| 6496 | continue; |
| 6497 | } |
| 6498 | |
| 6499 | case '{': |
| 6500 | // "{" starts a block scope |
| 6501 | // "{'a': 1}->func() is something else |
| 6502 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 6503 | { |
| 6504 | line = compile_block(ea.cmd, &cctx); |
| 6505 | continue; |
| 6506 | } |
| 6507 | break; |
| 6508 | |
| 6509 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6510 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6511 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6512 | } |
| 6513 | |
| 6514 | /* |
| 6515 | * COMMAND MODIFIERS |
| 6516 | */ |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6517 | save_cmdmod = cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6518 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 6519 | { |
| 6520 | if (errormsg != NULL) |
| 6521 | goto erret; |
| 6522 | // empty line or comment |
| 6523 | line = (char_u *)""; |
| 6524 | continue; |
| 6525 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 6526 | // TODO: use modifiers in the command |
| 6527 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6528 | cmdmod = save_cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6529 | |
| 6530 | // Skip ":call" to get to the function name. |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 6531 | p = ea.cmd; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6532 | if (checkforcmd(&ea.cmd, "call", 3)) |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 6533 | { |
| 6534 | if (*ea.cmd == '(') |
| 6535 | // not for "call()" |
| 6536 | ea.cmd = p; |
| 6537 | else |
| 6538 | ea.cmd = skipwhite(ea.cmd); |
| 6539 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6540 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6541 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6542 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6543 | char_u *pskip; |
| 6544 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6545 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6546 | // find what follows. |
| 6547 | // Skip over "var.member", "var[idx]" and the like. |
| 6548 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 6549 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6550 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6551 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6552 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6553 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6554 | char_u *var_end; |
| 6555 | int oplen; |
| 6556 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6557 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 6558 | if (ea.cmd[0] == '@') |
| 6559 | var_end = ea.cmd + 2; |
| 6560 | else |
| 6561 | var_end = find_name_end(pskip, NULL, NULL, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6562 | FNE_CHECK_START | FNE_INCL_BR); |
| 6563 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6564 | if (oplen > 0) |
| 6565 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6566 | size_t len = p - ea.cmd; |
| 6567 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6568 | // Recognize an assignment if we recognize the variable |
| 6569 | // name: |
| 6570 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 6571 | // "local = expr" where "local" is a local var. |
| 6572 | // "script = expr" where "script" is a script-local var. |
| 6573 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6574 | // "&opt = expr" |
| 6575 | // "$ENV = expr" |
| 6576 | // "@r = expr" |
| 6577 | if (*ea.cmd == '&' |
| 6578 | || *ea.cmd == '$' |
| 6579 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6580 | || ((len) > 2 && ea.cmd[1] == ':') |
| 6581 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 6582 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 6583 | NULL, &cctx) == OK |
| 6584 | || lookup_script(ea.cmd, len) == OK |
| 6585 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6586 | { |
| 6587 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6588 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6589 | goto erret; |
| 6590 | continue; |
| 6591 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6592 | } |
| 6593 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6594 | |
| 6595 | if (*ea.cmd == '[') |
| 6596 | { |
| 6597 | // [var, var] = expr |
| 6598 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 6599 | if (line == NULL) |
| 6600 | goto erret; |
| 6601 | if (line != ea.cmd) |
| 6602 | continue; |
| 6603 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6604 | } |
| 6605 | |
| 6606 | /* |
| 6607 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6608 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6609 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6610 | cmd = ea.cmd; |
Bram Moolenaar | 7c5ad34 | 2020-08-12 15:48:55 +0200 | [diff] [blame] | 6611 | if (*cmd != '\'' || starts_with_colon) |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6612 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6613 | ea.cmd = skip_range(ea.cmd, NULL); |
| 6614 | if (ea.cmd > cmd && !starts_with_colon) |
| 6615 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 6616 | emsg(_(e_colon_required_before_a_range)); |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6617 | goto erret; |
| 6618 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6619 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6620 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6621 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6622 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6623 | |
| 6624 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 6625 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6626 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6627 | { |
| 6628 | line += STRLEN(line); |
| 6629 | continue; |
| 6630 | } |
| 6631 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6632 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6633 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6634 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6635 | // CMD_let cannot happen, compile_assignment() above is used |
| 6636 | iemsg("Command from find_ex_command() not handled"); |
| 6637 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6638 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6639 | } |
| 6640 | |
| 6641 | p = skipwhite(p); |
| 6642 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6643 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 6644 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6645 | && ea.cmdidx != CMD_elseif |
| 6646 | && ea.cmdidx != CMD_else |
| 6647 | && ea.cmdidx != CMD_endif) |
| 6648 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6649 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6650 | continue; |
| 6651 | } |
| 6652 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6653 | if (ea.cmdidx != CMD_elseif |
| 6654 | && ea.cmdidx != CMD_else |
| 6655 | && ea.cmdidx != CMD_endif |
| 6656 | && ea.cmdidx != CMD_endfor |
| 6657 | && ea.cmdidx != CMD_endwhile |
| 6658 | && ea.cmdidx != CMD_catch |
| 6659 | && ea.cmdidx != CMD_finally |
| 6660 | && ea.cmdidx != CMD_endtry) |
| 6661 | { |
| 6662 | if (cctx.ctx_had_return) |
| 6663 | { |
| 6664 | emsg(_("E1095: Unreachable code after :return")); |
| 6665 | goto erret; |
| 6666 | } |
| 6667 | } |
| 6668 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6669 | switch (ea.cmdidx) |
| 6670 | { |
| 6671 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6672 | ea.arg = p; |
| 6673 | line = compile_nested_function(&ea, &cctx); |
| 6674 | break; |
| 6675 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6676 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6677 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6678 | goto erret; |
| 6679 | |
| 6680 | case CMD_return: |
| 6681 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6682 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6683 | break; |
| 6684 | |
| 6685 | case CMD_let: |
| 6686 | case CMD_const: |
| 6687 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6688 | if (line == p) |
| 6689 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6690 | break; |
| 6691 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6692 | case CMD_unlet: |
| 6693 | case CMD_unlockvar: |
| 6694 | case CMD_lockvar: |
| 6695 | line = compile_unletlock(p, &ea, &cctx); |
| 6696 | break; |
| 6697 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6698 | case CMD_import: |
| 6699 | line = compile_import(p, &cctx); |
| 6700 | break; |
| 6701 | |
| 6702 | case CMD_if: |
| 6703 | line = compile_if(p, &cctx); |
| 6704 | break; |
| 6705 | case CMD_elseif: |
| 6706 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6707 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6708 | break; |
| 6709 | case CMD_else: |
| 6710 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6711 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6712 | break; |
| 6713 | case CMD_endif: |
| 6714 | line = compile_endif(p, &cctx); |
| 6715 | break; |
| 6716 | |
| 6717 | case CMD_while: |
| 6718 | line = compile_while(p, &cctx); |
| 6719 | break; |
| 6720 | case CMD_endwhile: |
| 6721 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6722 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6723 | break; |
| 6724 | |
| 6725 | case CMD_for: |
| 6726 | line = compile_for(p, &cctx); |
| 6727 | break; |
| 6728 | case CMD_endfor: |
| 6729 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6730 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6731 | break; |
| 6732 | case CMD_continue: |
| 6733 | line = compile_continue(p, &cctx); |
| 6734 | break; |
| 6735 | case CMD_break: |
| 6736 | line = compile_break(p, &cctx); |
| 6737 | break; |
| 6738 | |
| 6739 | case CMD_try: |
| 6740 | line = compile_try(p, &cctx); |
| 6741 | break; |
| 6742 | case CMD_catch: |
| 6743 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6744 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6745 | break; |
| 6746 | case CMD_finally: |
| 6747 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6748 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6749 | break; |
| 6750 | case CMD_endtry: |
| 6751 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6752 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6753 | break; |
| 6754 | case CMD_throw: |
| 6755 | line = compile_throw(p, &cctx); |
| 6756 | break; |
| 6757 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6758 | case CMD_eval: |
| 6759 | if (compile_expr0(&p, &cctx) == FAIL) |
| 6760 | goto erret; |
| 6761 | |
| 6762 | // drop the return value |
| 6763 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 6764 | |
| 6765 | line = skipwhite(p); |
| 6766 | break; |
| 6767 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6768 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6769 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6770 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6771 | case CMD_echomsg: |
| 6772 | case CMD_echoerr: |
| 6773 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6774 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6775 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6776 | // TODO: other commands with an expression argument |
| 6777 | |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 6778 | case CMD_append: |
| 6779 | case CMD_change: |
| 6780 | case CMD_insert: |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 6781 | case CMD_t: |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 6782 | case CMD_xit: |
| 6783 | not_in_vim9(&ea); |
| 6784 | goto erret; |
| 6785 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 6786 | case CMD_SIZE: |
| 6787 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 6788 | goto erret; |
| 6789 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6790 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6791 | // Not recognized, execute with do_cmdline_cmd(). |
| 6792 | ea.arg = p; |
| 6793 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6794 | break; |
| 6795 | } |
| 6796 | if (line == NULL) |
| 6797 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6798 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6799 | |
| 6800 | if (cctx.ctx_type_stack.ga_len < 0) |
| 6801 | { |
| 6802 | iemsg("Type stack underflow"); |
| 6803 | goto erret; |
| 6804 | } |
| 6805 | } |
| 6806 | |
| 6807 | if (cctx.ctx_scope != NULL) |
| 6808 | { |
| 6809 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 6810 | emsg(_(e_endif)); |
| 6811 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 6812 | emsg(_(e_endwhile)); |
| 6813 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 6814 | emsg(_(e_endfor)); |
| 6815 | else |
| 6816 | emsg(_("E1026: Missing }")); |
| 6817 | goto erret; |
| 6818 | } |
| 6819 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6820 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6821 | { |
| 6822 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 6823 | { |
| 6824 | emsg(_("E1027: Missing return statement")); |
| 6825 | goto erret; |
| 6826 | } |
| 6827 | |
| 6828 | // Return zero if there is no return at the end. |
| 6829 | generate_PUSHNR(&cctx, 0); |
| 6830 | generate_instr(&cctx, ISN_RETURN); |
| 6831 | } |
| 6832 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6833 | { |
| 6834 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6835 | + ufunc->uf_dfunc_idx; |
| 6836 | dfunc->df_deleted = FALSE; |
| 6837 | dfunc->df_instr = instr->ga_data; |
| 6838 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6839 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 6840 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6841 | if (cctx.ctx_outer_used) |
| 6842 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6843 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6844 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6845 | |
| 6846 | ret = OK; |
| 6847 | |
| 6848 | erret: |
| 6849 | if (ret == FAIL) |
| 6850 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6851 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6852 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6853 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6854 | |
| 6855 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 6856 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6857 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6858 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6859 | // If using the last entry in the table and it was added above, we |
| 6860 | // might as well remove it. |
| 6861 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 6862 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6863 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6864 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6865 | ufunc->uf_dfunc_idx = 0; |
| 6866 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6867 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6868 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6869 | while (cctx.ctx_scope != NULL) |
| 6870 | drop_scope(&cctx); |
| 6871 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6872 | // Don't execute this function body. |
| 6873 | ga_clear_strings(&ufunc->uf_lines); |
| 6874 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6875 | if (errormsg != NULL) |
| 6876 | emsg(errormsg); |
| 6877 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 6878 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6879 | } |
| 6880 | |
| 6881 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6882 | if (do_estack_push) |
| 6883 | estack_pop(); |
| 6884 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6885 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6886 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6887 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6888 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6889 | } |
| 6890 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 6891 | void |
| 6892 | set_function_type(ufunc_T *ufunc) |
| 6893 | { |
| 6894 | int varargs = ufunc->uf_va_name != NULL; |
| 6895 | int argcount = ufunc->uf_args.ga_len; |
| 6896 | |
| 6897 | // Create a type for the function, with the return type and any |
| 6898 | // argument types. |
| 6899 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 6900 | // The type is included in "tt_args". |
| 6901 | if (argcount > 0 || varargs) |
| 6902 | { |
| 6903 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 6904 | argcount, &ufunc->uf_type_list); |
| 6905 | // Add argument types to the function type. |
| 6906 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 6907 | argcount + varargs, |
| 6908 | &ufunc->uf_type_list) == FAIL) |
| 6909 | return; |
| 6910 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 6911 | ufunc->uf_func_type->tt_min_argcount = |
| 6912 | argcount - ufunc->uf_def_args.ga_len; |
| 6913 | if (ufunc->uf_arg_types == NULL) |
| 6914 | { |
| 6915 | int i; |
| 6916 | |
| 6917 | // lambda does not have argument types. |
| 6918 | for (i = 0; i < argcount; ++i) |
| 6919 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 6920 | } |
| 6921 | else |
| 6922 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 6923 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 6924 | if (varargs) |
| 6925 | { |
| 6926 | ufunc->uf_func_type->tt_args[argcount] = |
| 6927 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 6928 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 6929 | } |
| 6930 | } |
| 6931 | else |
| 6932 | // No arguments, can use a predefined type. |
| 6933 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 6934 | argcount, &ufunc->uf_type_list); |
| 6935 | } |
| 6936 | |
| 6937 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6938 | /* |
| 6939 | * Delete an instruction, free what it contains. |
| 6940 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6941 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6942 | delete_instr(isn_T *isn) |
| 6943 | { |
| 6944 | switch (isn->isn_type) |
| 6945 | { |
| 6946 | case ISN_EXEC: |
| 6947 | case ISN_LOADENV: |
| 6948 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 6949 | case ISN_LOADB: |
| 6950 | case ISN_LOADW: |
| 6951 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6952 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6953 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6954 | case ISN_PUSHEXC: |
| 6955 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6956 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6957 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 6958 | case ISN_STOREB: |
| 6959 | case ISN_STOREW: |
| 6960 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6961 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6962 | vim_free(isn->isn_arg.string); |
| 6963 | break; |
| 6964 | |
| 6965 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6966 | case ISN_STORES: |
| 6967 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6968 | break; |
| 6969 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6970 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 6971 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6972 | vim_free(isn->isn_arg.unlet.ul_name); |
| 6973 | break; |
| 6974 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6975 | case ISN_STOREOPT: |
| 6976 | vim_free(isn->isn_arg.storeopt.so_name); |
| 6977 | break; |
| 6978 | |
| 6979 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 6980 | blob_unref(isn->isn_arg.blob); |
| 6981 | break; |
| 6982 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6983 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6984 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6985 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6986 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6987 | break; |
| 6988 | |
| 6989 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6990 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6991 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6992 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6993 | break; |
| 6994 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6995 | case ISN_UCALL: |
| 6996 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 6997 | break; |
| 6998 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 6999 | case ISN_FUNCREF: |
| 7000 | { |
| 7001 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7002 | + isn->isn_arg.funcref.fr_func; |
| 7003 | func_ptr_unref(dfunc->df_ufunc); |
| 7004 | } |
| 7005 | break; |
| 7006 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7007 | case ISN_NEWFUNC: |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 7008 | { |
| 7009 | char_u *lambda = isn->isn_arg.newfunc.nf_lambda; |
| 7010 | ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL); |
| 7011 | |
| 7012 | if (ufunc != NULL) |
| 7013 | { |
| 7014 | // Clear uf_dfunc_idx so that the function is deleted. |
| 7015 | clear_def_function(ufunc); |
| 7016 | ufunc->uf_dfunc_idx = 0; |
| 7017 | func_ptr_unref(ufunc); |
| 7018 | } |
| 7019 | |
| 7020 | vim_free(lambda); |
| 7021 | vim_free(isn->isn_arg.newfunc.nf_global); |
| 7022 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7023 | break; |
| 7024 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7025 | case ISN_2BOOL: |
| 7026 | case ISN_2STRING: |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 7027 | case ISN_2STRING_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7028 | case ISN_ADDBLOB: |
| 7029 | case ISN_ADDLIST: |
| 7030 | case ISN_BCALL: |
| 7031 | case ISN_CATCH: |
| 7032 | case ISN_CHECKNR: |
| 7033 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7034 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7035 | case ISN_COMPAREANY: |
| 7036 | case ISN_COMPAREBLOB: |
| 7037 | case ISN_COMPAREBOOL: |
| 7038 | case ISN_COMPAREDICT: |
| 7039 | case ISN_COMPAREFLOAT: |
| 7040 | case ISN_COMPAREFUNC: |
| 7041 | case ISN_COMPARELIST: |
| 7042 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7043 | case ISN_COMPARESPECIAL: |
| 7044 | case ISN_COMPARESTRING: |
| 7045 | case ISN_CONCAT: |
| 7046 | case ISN_DCALL: |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 7047 | case ISN_SHUFFLE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7048 | case ISN_DROP: |
| 7049 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7050 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7051 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7052 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7053 | case ISN_EXECCONCAT: |
| 7054 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7055 | case ISN_FOR: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7056 | case ISN_LISTINDEX: |
| 7057 | case ISN_STRINDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7058 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7059 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7060 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7061 | case ISN_JUMP: |
| 7062 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7063 | case ISN_LOADBDICT: |
| 7064 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7065 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7066 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7067 | case ISN_LOADSCRIPT: |
| 7068 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7069 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7070 | case ISN_LOADWDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7071 | case ISN_NEGATENR: |
| 7072 | case ISN_NEWDICT: |
| 7073 | case ISN_NEWLIST: |
| 7074 | case ISN_OPNR: |
| 7075 | case ISN_OPFLOAT: |
| 7076 | case ISN_OPANY: |
| 7077 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7078 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7079 | case ISN_PUSHF: |
| 7080 | case ISN_PUSHNR: |
| 7081 | case ISN_PUSHBOOL: |
| 7082 | case ISN_PUSHSPEC: |
| 7083 | case ISN_RETURN: |
| 7084 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 7085 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7086 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7087 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7088 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7089 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7090 | case ISN_STOREDICT: |
| 7091 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7092 | case ISN_THROW: |
| 7093 | case ISN_TRY: |
| 7094 | // nothing allocated |
| 7095 | break; |
| 7096 | } |
| 7097 | } |
| 7098 | |
| 7099 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7100 | * Free all instructions for "dfunc". |
| 7101 | */ |
| 7102 | static void |
| 7103 | delete_def_function_contents(dfunc_T *dfunc) |
| 7104 | { |
| 7105 | int idx; |
| 7106 | |
| 7107 | ga_clear(&dfunc->df_def_args_isn); |
| 7108 | |
| 7109 | if (dfunc->df_instr != NULL) |
| 7110 | { |
| 7111 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7112 | delete_instr(dfunc->df_instr + idx); |
| 7113 | VIM_CLEAR(dfunc->df_instr); |
| 7114 | } |
| 7115 | |
| 7116 | dfunc->df_deleted = TRUE; |
| 7117 | } |
| 7118 | |
| 7119 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7120 | * When a user function is deleted, clear the contents of any associated def |
| 7121 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7122 | */ |
| 7123 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7124 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7125 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7126 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7127 | { |
| 7128 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7129 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7130 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7131 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7132 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7133 | } |
| 7134 | } |
| 7135 | |
| 7136 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7137 | /* |
| 7138 | * Free all functions defined with ":def". |
| 7139 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7140 | void |
| 7141 | free_def_functions(void) |
| 7142 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7143 | int idx; |
| 7144 | |
| 7145 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7146 | { |
| 7147 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7148 | |
| 7149 | delete_def_function_contents(dfunc); |
| 7150 | } |
| 7151 | |
| 7152 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7153 | } |
| 7154 | #endif |
| 7155 | |
| 7156 | |
| 7157 | #endif // FEAT_EVAL |