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 | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 305 | semsg(_(e_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. |
| 377 | */ |
| 378 | static int |
| 379 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 380 | { |
| 381 | isn_T *isn; |
| 382 | garray_T *stack = &cctx->ctx_type_stack; |
| 383 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 384 | |
| 385 | if ((*type)->tt_type == VAR_STRING) |
| 386 | return OK; |
| 387 | *type = &t_string; |
| 388 | |
| 389 | if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL) |
| 390 | return FAIL; |
| 391 | isn->isn_arg.number = offset; |
| 392 | |
| 393 | return OK; |
| 394 | } |
| 395 | |
| 396 | static int |
| 397 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 398 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 399 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 400 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 401 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 402 | { |
| 403 | if (*op == '+') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 404 | emsg(_("E1051: wrong argument type for +")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 405 | else |
| 406 | semsg(_("E1036: %c requires number or float arguments"), *op); |
| 407 | return FAIL; |
| 408 | } |
| 409 | return OK; |
| 410 | } |
| 411 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 412 | static int |
| 413 | generate_add_instr( |
| 414 | cctx_T *cctx, |
| 415 | vartype_T vartype, |
| 416 | type_T *type1, |
| 417 | type_T *type2) |
| 418 | { |
| 419 | isn_T *isn = generate_instr_drop(cctx, |
| 420 | vartype == VAR_NUMBER ? ISN_OPNR |
| 421 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 422 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 423 | #ifdef FEAT_FLOAT |
| 424 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 425 | #endif |
| 426 | : ISN_OPANY, 1); |
| 427 | |
| 428 | if (vartype != VAR_LIST && vartype != VAR_BLOB |
| 429 | && type1->tt_type != VAR_ANY |
| 430 | && type2->tt_type != VAR_ANY |
| 431 | && check_number_or_float( |
| 432 | type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL) |
| 433 | return FAIL; |
| 434 | |
| 435 | if (isn != NULL) |
| 436 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 437 | return isn == NULL ? FAIL : OK; |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * Get the type to use for an instruction for an operation on "type1" and |
| 442 | * "type2". If they are matching use a type-specific instruction. Otherwise |
| 443 | * fall back to runtime type checking. |
| 444 | */ |
| 445 | static vartype_T |
| 446 | operator_type(type_T *type1, type_T *type2) |
| 447 | { |
| 448 | if (type1->tt_type == type2->tt_type |
| 449 | && (type1->tt_type == VAR_NUMBER |
| 450 | || type1->tt_type == VAR_LIST |
| 451 | #ifdef FEAT_FLOAT |
| 452 | || type1->tt_type == VAR_FLOAT |
| 453 | #endif |
| 454 | || type1->tt_type == VAR_BLOB)) |
| 455 | return type1->tt_type; |
| 456 | return VAR_ANY; |
| 457 | } |
| 458 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 459 | /* |
| 460 | * Generate an instruction with two arguments. The instruction depends on the |
| 461 | * type of the arguments. |
| 462 | */ |
| 463 | static int |
| 464 | generate_two_op(cctx_T *cctx, char_u *op) |
| 465 | { |
| 466 | garray_T *stack = &cctx->ctx_type_stack; |
| 467 | type_T *type1; |
| 468 | type_T *type2; |
| 469 | vartype_T vartype; |
| 470 | isn_T *isn; |
| 471 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 472 | RETURN_OK_IF_SKIP(cctx); |
| 473 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 474 | // Get the known type of the two items on the stack. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 475 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 476 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 477 | vartype = operator_type(type1, type2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 478 | |
| 479 | switch (*op) |
| 480 | { |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 481 | case '+': |
| 482 | if (generate_add_instr(cctx, vartype, type1, type2) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 483 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 484 | break; |
| 485 | |
| 486 | case '-': |
| 487 | case '*': |
| 488 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 489 | op) == FAIL) |
| 490 | return FAIL; |
| 491 | if (vartype == VAR_NUMBER) |
| 492 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 493 | #ifdef FEAT_FLOAT |
| 494 | else if (vartype == VAR_FLOAT) |
| 495 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 496 | #endif |
| 497 | else |
| 498 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 499 | if (isn != NULL) |
| 500 | isn->isn_arg.op.op_type = *op == '*' |
| 501 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 502 | break; |
| 503 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 504 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 505 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 506 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 507 | && type2->tt_type != VAR_NUMBER)) |
| 508 | { |
| 509 | emsg(_("E1035: % requires number arguments")); |
| 510 | return FAIL; |
| 511 | } |
| 512 | isn = generate_instr_drop(cctx, |
| 513 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 514 | if (isn != NULL) |
| 515 | isn->isn_arg.op.op_type = EXPR_REM; |
| 516 | break; |
| 517 | } |
| 518 | |
| 519 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 520 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 521 | { |
| 522 | type_T *type = &t_any; |
| 523 | |
| 524 | #ifdef FEAT_FLOAT |
| 525 | // float+number and number+float results in float |
| 526 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 527 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 528 | type = &t_float; |
| 529 | #endif |
| 530 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 531 | } |
| 532 | |
| 533 | return OK; |
| 534 | } |
| 535 | |
| 536 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 537 | * Get the instruction to use for comparing "type1" with "type2" |
| 538 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 539 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 540 | static isntype_T |
| 541 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 542 | { |
| 543 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 544 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 545 | if (type1 == VAR_UNKNOWN) |
| 546 | type1 = VAR_ANY; |
| 547 | if (type2 == VAR_UNKNOWN) |
| 548 | type2 = VAR_ANY; |
| 549 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 550 | if (type1 == type2) |
| 551 | { |
| 552 | switch (type1) |
| 553 | { |
| 554 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 555 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 556 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 557 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 558 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 559 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 560 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 561 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 562 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 563 | default: isntype = ISN_COMPAREANY; break; |
| 564 | } |
| 565 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 566 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 567 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 568 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 569 | isntype = ISN_COMPAREANY; |
| 570 | |
| 571 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 572 | && (isntype == ISN_COMPAREBOOL |
| 573 | || isntype == ISN_COMPARESPECIAL |
| 574 | || isntype == ISN_COMPARENR |
| 575 | || isntype == ISN_COMPAREFLOAT)) |
| 576 | { |
| 577 | semsg(_("E1037: Cannot use \"%s\" with %s"), |
| 578 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 579 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 580 | } |
| 581 | if (isntype == ISN_DROP |
| 582 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 583 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 584 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 585 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 586 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 587 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 588 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 589 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 590 | semsg(_("E1072: Cannot compare %s with %s"), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 591 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 592 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 593 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 594 | return isntype; |
| 595 | } |
| 596 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 597 | int |
| 598 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 599 | { |
| 600 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 601 | return FAIL; |
| 602 | return OK; |
| 603 | } |
| 604 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 605 | /* |
| 606 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 607 | */ |
| 608 | static int |
| 609 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 610 | { |
| 611 | isntype_T isntype; |
| 612 | isn_T *isn; |
| 613 | garray_T *stack = &cctx->ctx_type_stack; |
| 614 | vartype_T type1; |
| 615 | vartype_T type2; |
| 616 | |
| 617 | RETURN_OK_IF_SKIP(cctx); |
| 618 | |
| 619 | // Get the known type of the two items on the stack. If they are matching |
| 620 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 621 | // checking. |
| 622 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 623 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 624 | isntype = get_compare_isn(exptype, type1, type2); |
| 625 | if (isntype == ISN_DROP) |
| 626 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 627 | |
| 628 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 629 | return FAIL; |
| 630 | isn->isn_arg.op.op_type = exptype; |
| 631 | isn->isn_arg.op.op_ic = ic; |
| 632 | |
| 633 | // takes two arguments, puts one bool back |
| 634 | if (stack->ga_len >= 2) |
| 635 | { |
| 636 | --stack->ga_len; |
| 637 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 638 | } |
| 639 | |
| 640 | return OK; |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Generate an ISN_2BOOL instruction. |
| 645 | */ |
| 646 | static int |
| 647 | generate_2BOOL(cctx_T *cctx, int invert) |
| 648 | { |
| 649 | isn_T *isn; |
| 650 | garray_T *stack = &cctx->ctx_type_stack; |
| 651 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 652 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 653 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 654 | return FAIL; |
| 655 | isn->isn_arg.number = invert; |
| 656 | |
| 657 | // type becomes bool |
| 658 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 659 | |
| 660 | return OK; |
| 661 | } |
| 662 | |
| 663 | static int |
| 664 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 665 | { |
| 666 | isn_T *isn; |
| 667 | garray_T *stack = &cctx->ctx_type_stack; |
| 668 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 669 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 670 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 671 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 672 | // TODO: whole type, e.g. for a function also arg and return types |
| 673 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 674 | isn->isn_arg.type.ct_off = offset; |
| 675 | |
| 676 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 677 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 678 | |
| 679 | return OK; |
| 680 | } |
| 681 | |
| 682 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 683 | * Check that |
| 684 | * - "actual" is "expected" type or |
| 685 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 686 | * - return FAIL. |
| 687 | */ |
| 688 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 689 | need_type( |
| 690 | type_T *actual, |
| 691 | type_T *expected, |
| 692 | int offset, |
| 693 | cctx_T *cctx, |
| 694 | int silent) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 695 | { |
| 696 | if (check_type(expected, actual, FALSE) == OK) |
| 697 | return OK; |
| 698 | if (actual->tt_type != VAR_ANY |
| 699 | && actual->tt_type != VAR_UNKNOWN |
| 700 | && !(actual->tt_type == VAR_FUNC |
| 701 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 702 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 703 | if (!silent) |
| 704 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 705 | return FAIL; |
| 706 | } |
| 707 | generate_TYPECHECK(cctx, expected, offset); |
| 708 | return OK; |
| 709 | } |
| 710 | |
| 711 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 712 | * Generate an ISN_PUSHNR instruction. |
| 713 | */ |
| 714 | static int |
| 715 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 716 | { |
| 717 | isn_T *isn; |
| 718 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 719 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 720 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 721 | return FAIL; |
| 722 | isn->isn_arg.number = number; |
| 723 | |
| 724 | return OK; |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * Generate an ISN_PUSHBOOL instruction. |
| 729 | */ |
| 730 | static int |
| 731 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 732 | { |
| 733 | isn_T *isn; |
| 734 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 735 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 736 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 737 | return FAIL; |
| 738 | isn->isn_arg.number = number; |
| 739 | |
| 740 | return OK; |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * Generate an ISN_PUSHSPEC instruction. |
| 745 | */ |
| 746 | static int |
| 747 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 748 | { |
| 749 | isn_T *isn; |
| 750 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 751 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 752 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 753 | return FAIL; |
| 754 | isn->isn_arg.number = number; |
| 755 | |
| 756 | return OK; |
| 757 | } |
| 758 | |
| 759 | #ifdef FEAT_FLOAT |
| 760 | /* |
| 761 | * Generate an ISN_PUSHF instruction. |
| 762 | */ |
| 763 | static int |
| 764 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 765 | { |
| 766 | isn_T *isn; |
| 767 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 768 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 769 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 770 | return FAIL; |
| 771 | isn->isn_arg.fnumber = fnumber; |
| 772 | |
| 773 | return OK; |
| 774 | } |
| 775 | #endif |
| 776 | |
| 777 | /* |
| 778 | * Generate an ISN_PUSHS instruction. |
| 779 | * Consumes "str". |
| 780 | */ |
| 781 | static int |
| 782 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 783 | { |
| 784 | isn_T *isn; |
| 785 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 786 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 787 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 788 | return FAIL; |
| 789 | isn->isn_arg.string = str; |
| 790 | |
| 791 | return OK; |
| 792 | } |
| 793 | |
| 794 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 795 | * Generate an ISN_PUSHCHANNEL instruction. |
| 796 | * Consumes "channel". |
| 797 | */ |
| 798 | static int |
| 799 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 800 | { |
| 801 | isn_T *isn; |
| 802 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 803 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 804 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 805 | return FAIL; |
| 806 | isn->isn_arg.channel = channel; |
| 807 | |
| 808 | return OK; |
| 809 | } |
| 810 | |
| 811 | /* |
| 812 | * Generate an ISN_PUSHJOB instruction. |
| 813 | * Consumes "job". |
| 814 | */ |
| 815 | static int |
| 816 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 817 | { |
| 818 | isn_T *isn; |
| 819 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 820 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 821 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 822 | return FAIL; |
| 823 | isn->isn_arg.job = job; |
| 824 | |
| 825 | return OK; |
| 826 | } |
| 827 | |
| 828 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 829 | * Generate an ISN_PUSHBLOB instruction. |
| 830 | * Consumes "blob". |
| 831 | */ |
| 832 | static int |
| 833 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 834 | { |
| 835 | isn_T *isn; |
| 836 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 837 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 838 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 839 | return FAIL; |
| 840 | isn->isn_arg.blob = blob; |
| 841 | |
| 842 | return OK; |
| 843 | } |
| 844 | |
| 845 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 846 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 847 | * Consumes "name". |
| 848 | */ |
| 849 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 850 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 851 | { |
| 852 | isn_T *isn; |
| 853 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 854 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 855 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 856 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 857 | isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 858 | |
| 859 | return OK; |
| 860 | } |
| 861 | |
| 862 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 863 | * Generate an ISN_GETITEM instruction with "index". |
| 864 | */ |
| 865 | static int |
| 866 | generate_GETITEM(cctx_T *cctx, int index) |
| 867 | { |
| 868 | isn_T *isn; |
| 869 | garray_T *stack = &cctx->ctx_type_stack; |
| 870 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 871 | type_T *item_type = &t_any; |
| 872 | |
| 873 | RETURN_OK_IF_SKIP(cctx); |
| 874 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 875 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 876 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 877 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 878 | emsg(_(e_listreq)); |
| 879 | return FAIL; |
| 880 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 881 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 882 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 883 | return FAIL; |
| 884 | isn->isn_arg.number = index; |
| 885 | |
| 886 | // add the item type to the type stack |
| 887 | if (ga_grow(stack, 1) == FAIL) |
| 888 | return FAIL; |
| 889 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 890 | ++stack->ga_len; |
| 891 | return OK; |
| 892 | } |
| 893 | |
| 894 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 895 | * Generate an ISN_SLICE instruction with "count". |
| 896 | */ |
| 897 | static int |
| 898 | generate_SLICE(cctx_T *cctx, int count) |
| 899 | { |
| 900 | isn_T *isn; |
| 901 | |
| 902 | RETURN_OK_IF_SKIP(cctx); |
| 903 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 904 | return FAIL; |
| 905 | isn->isn_arg.number = count; |
| 906 | return OK; |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 911 | */ |
| 912 | static int |
| 913 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 914 | { |
| 915 | isn_T *isn; |
| 916 | |
| 917 | RETURN_OK_IF_SKIP(cctx); |
| 918 | |
| 919 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 920 | return FAIL; |
| 921 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 922 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 923 | |
| 924 | return OK; |
| 925 | } |
| 926 | |
| 927 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 928 | * Generate an ISN_STORE instruction. |
| 929 | */ |
| 930 | static int |
| 931 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 932 | { |
| 933 | isn_T *isn; |
| 934 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 935 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 936 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 937 | return FAIL; |
| 938 | if (name != NULL) |
| 939 | isn->isn_arg.string = vim_strsave(name); |
| 940 | else |
| 941 | isn->isn_arg.number = idx; |
| 942 | |
| 943 | return OK; |
| 944 | } |
| 945 | |
| 946 | /* |
| 947 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 948 | */ |
| 949 | static int |
| 950 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 951 | { |
| 952 | isn_T *isn; |
| 953 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 954 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 955 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 956 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 957 | isn->isn_arg.storenr.stnr_idx = idx; |
| 958 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 959 | |
| 960 | return OK; |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | * Generate an ISN_STOREOPT instruction |
| 965 | */ |
| 966 | static int |
| 967 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 968 | { |
| 969 | isn_T *isn; |
| 970 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 971 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1c199f9 | 2020-08-07 21:28:34 +0200 | [diff] [blame] | 972 | if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 973 | return FAIL; |
| 974 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 975 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 976 | |
| 977 | return OK; |
| 978 | } |
| 979 | |
| 980 | /* |
| 981 | * Generate an ISN_LOAD or similar instruction. |
| 982 | */ |
| 983 | static int |
| 984 | generate_LOAD( |
| 985 | cctx_T *cctx, |
| 986 | isntype_T isn_type, |
| 987 | int idx, |
| 988 | char_u *name, |
| 989 | type_T *type) |
| 990 | { |
| 991 | isn_T *isn; |
| 992 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 993 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 994 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 995 | return FAIL; |
| 996 | if (name != NULL) |
| 997 | isn->isn_arg.string = vim_strsave(name); |
| 998 | else |
| 999 | isn->isn_arg.number = idx; |
| 1000 | |
| 1001 | return OK; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1005 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1006 | */ |
| 1007 | static int |
| 1008 | generate_LOADV( |
| 1009 | cctx_T *cctx, |
| 1010 | char_u *name, |
| 1011 | int error) |
| 1012 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1013 | int di_flags; |
| 1014 | int vidx = find_vim_var(name, &di_flags); |
| 1015 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1016 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1017 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1018 | if (vidx < 0) |
| 1019 | { |
| 1020 | if (error) |
| 1021 | semsg(_(e_var_notfound), name); |
| 1022 | return FAIL; |
| 1023 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1024 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1025 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1026 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1030 | * Generate an ISN_UNLET instruction. |
| 1031 | */ |
| 1032 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1033 | 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] | 1034 | { |
| 1035 | isn_T *isn; |
| 1036 | |
| 1037 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1038 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1039 | return FAIL; |
| 1040 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1041 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1042 | |
| 1043 | return OK; |
| 1044 | } |
| 1045 | |
| 1046 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1047 | * Generate an ISN_LOADS instruction. |
| 1048 | */ |
| 1049 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1050 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1051 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1052 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1053 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1054 | int sid, |
| 1055 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1056 | { |
| 1057 | isn_T *isn; |
| 1058 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1059 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1060 | if (isn_type == ISN_LOADS) |
| 1061 | isn = generate_instr_type(cctx, isn_type, type); |
| 1062 | else |
| 1063 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1064 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1065 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1066 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1067 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1068 | |
| 1069 | return OK; |
| 1070 | } |
| 1071 | |
| 1072 | /* |
| 1073 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1074 | */ |
| 1075 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1076 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1077 | cctx_T *cctx, |
| 1078 | isntype_T isn_type, |
| 1079 | int sid, |
| 1080 | int idx, |
| 1081 | type_T *type) |
| 1082 | { |
| 1083 | isn_T *isn; |
| 1084 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1085 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1086 | if (isn_type == ISN_LOADSCRIPT) |
| 1087 | isn = generate_instr_type(cctx, isn_type, type); |
| 1088 | else |
| 1089 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1090 | if (isn == NULL) |
| 1091 | return FAIL; |
| 1092 | isn->isn_arg.script.script_sid = sid; |
| 1093 | isn->isn_arg.script.script_idx = idx; |
| 1094 | return OK; |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * Generate an ISN_NEWLIST instruction. |
| 1099 | */ |
| 1100 | static int |
| 1101 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1102 | { |
| 1103 | isn_T *isn; |
| 1104 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1105 | type_T *type; |
| 1106 | type_T *member; |
| 1107 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1108 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1109 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1110 | return FAIL; |
| 1111 | isn->isn_arg.number = count; |
| 1112 | |
| 1113 | // drop the value types |
| 1114 | stack->ga_len -= count; |
| 1115 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1116 | // Use the first value type for the list member type. Use "any" for an |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1117 | // empty list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1118 | if (count > 0) |
| 1119 | member = ((type_T **)stack->ga_data)[stack->ga_len]; |
| 1120 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1121 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1122 | type = get_list_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1123 | |
| 1124 | // add the list type to the type stack |
| 1125 | if (ga_grow(stack, 1) == FAIL) |
| 1126 | return FAIL; |
| 1127 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1128 | ++stack->ga_len; |
| 1129 | |
| 1130 | return OK; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * Generate an ISN_NEWDICT instruction. |
| 1135 | */ |
| 1136 | static int |
| 1137 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1138 | { |
| 1139 | isn_T *isn; |
| 1140 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1141 | type_T *type; |
| 1142 | type_T *member; |
| 1143 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1144 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1145 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1146 | return FAIL; |
| 1147 | isn->isn_arg.number = count; |
| 1148 | |
| 1149 | // drop the key and value types |
| 1150 | stack->ga_len -= 2 * count; |
| 1151 | |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1152 | // Use the first value type for the list member type. Use "void" for an |
| 1153 | // empty dict. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1154 | if (count > 0) |
| 1155 | member = ((type_T **)stack->ga_data)[stack->ga_len + 1]; |
| 1156 | else |
Bram Moolenaar | 436472f | 2020-02-20 22:54:43 +0100 | [diff] [blame] | 1157 | member = &t_void; |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 1158 | type = get_dict_type(member, cctx->ctx_type_list); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1159 | |
| 1160 | // add the dict type to the type stack |
| 1161 | if (ga_grow(stack, 1) == FAIL) |
| 1162 | return FAIL; |
| 1163 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1164 | ++stack->ga_len; |
| 1165 | |
| 1166 | return OK; |
| 1167 | } |
| 1168 | |
| 1169 | /* |
| 1170 | * Generate an ISN_FUNCREF instruction. |
| 1171 | */ |
| 1172 | static int |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1173 | generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1174 | { |
| 1175 | isn_T *isn; |
| 1176 | garray_T *stack = &cctx->ctx_type_stack; |
| 1177 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1178 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1179 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1180 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1181 | isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1182 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1183 | |
| 1184 | if (ga_grow(stack, 1) == FAIL) |
| 1185 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1186 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 1187 | ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1188 | ++stack->ga_len; |
| 1189 | |
| 1190 | return OK; |
| 1191 | } |
| 1192 | |
| 1193 | /* |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1194 | * Generate an ISN_NEWFUNC instruction. |
| 1195 | */ |
| 1196 | static int |
| 1197 | generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) |
| 1198 | { |
| 1199 | isn_T *isn; |
| 1200 | char_u *name; |
| 1201 | |
| 1202 | RETURN_OK_IF_SKIP(cctx); |
| 1203 | name = vim_strsave(lambda_name); |
| 1204 | if (name == NULL) |
| 1205 | return FAIL; |
| 1206 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
| 1207 | return FAIL; |
| 1208 | isn->isn_arg.newfunc.nf_lambda = name; |
| 1209 | isn->isn_arg.newfunc.nf_global = func_name; |
| 1210 | |
| 1211 | return OK; |
| 1212 | } |
| 1213 | |
| 1214 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1215 | * Generate an ISN_JUMP instruction. |
| 1216 | */ |
| 1217 | static int |
| 1218 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1219 | { |
| 1220 | isn_T *isn; |
| 1221 | garray_T *stack = &cctx->ctx_type_stack; |
| 1222 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1223 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1224 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1225 | return FAIL; |
| 1226 | isn->isn_arg.jump.jump_when = when; |
| 1227 | isn->isn_arg.jump.jump_where = where; |
| 1228 | |
| 1229 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1230 | --stack->ga_len; |
| 1231 | |
| 1232 | return OK; |
| 1233 | } |
| 1234 | |
| 1235 | static int |
| 1236 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1237 | { |
| 1238 | isn_T *isn; |
| 1239 | garray_T *stack = &cctx->ctx_type_stack; |
| 1240 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1241 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1242 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1243 | return FAIL; |
| 1244 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1245 | |
| 1246 | if (ga_grow(stack, 1) == FAIL) |
| 1247 | return FAIL; |
| 1248 | // type doesn't matter, will be stored next |
| 1249 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1250 | ++stack->ga_len; |
| 1251 | |
| 1252 | return OK; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1257 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1258 | * Return FAIL if the number of arguments is wrong. |
| 1259 | */ |
| 1260 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1261 | 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] | 1262 | { |
| 1263 | isn_T *isn; |
| 1264 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1265 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1266 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1267 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1268 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1269 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1270 | argoff = check_internal_func(func_idx, argcount); |
| 1271 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1272 | return FAIL; |
| 1273 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1274 | if (method_call && argoff > 1) |
| 1275 | { |
| 1276 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1277 | return FAIL; |
| 1278 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1279 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1280 | } |
| 1281 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1282 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1283 | return FAIL; |
| 1284 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1285 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1286 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1287 | for (i = 0; i < argcount; ++i) |
| 1288 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1289 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1290 | stack->ga_len -= argcount; // drop the arguments |
| 1291 | if (ga_grow(stack, 1) == FAIL) |
| 1292 | return FAIL; |
| 1293 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1294 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1295 | ++stack->ga_len; // add return value |
| 1296 | |
| 1297 | return OK; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1302 | * Return FAIL if the number of arguments is wrong. |
| 1303 | */ |
| 1304 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1305 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1306 | { |
| 1307 | isn_T *isn; |
| 1308 | garray_T *stack = &cctx->ctx_type_stack; |
| 1309 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1310 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1311 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1312 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1313 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1314 | { |
| 1315 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1316 | return FAIL; |
| 1317 | } |
| 1318 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1319 | { |
| 1320 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1321 | return FAIL; |
| 1322 | } |
| 1323 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1324 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1325 | { |
| 1326 | int i; |
| 1327 | |
| 1328 | for (i = 0; i < argcount; ++i) |
| 1329 | { |
| 1330 | type_T *expected; |
| 1331 | type_T *actual; |
| 1332 | |
| 1333 | if (i < regular_args) |
| 1334 | { |
| 1335 | if (ufunc->uf_arg_types == NULL) |
| 1336 | continue; |
| 1337 | expected = ufunc->uf_arg_types[i]; |
| 1338 | } |
| 1339 | else |
| 1340 | expected = ufunc->uf_va_type->tt_member; |
| 1341 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1342 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1343 | { |
| 1344 | arg_type_mismatch(expected, actual, i + 1); |
| 1345 | return FAIL; |
| 1346 | } |
| 1347 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1348 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1349 | if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL) |
| 1350 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1351 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1352 | } |
| 1353 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1354 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1355 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1356 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1357 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1358 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1359 | { |
| 1360 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1361 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1362 | } |
| 1363 | else |
| 1364 | { |
| 1365 | // A user function may be deleted and redefined later, can't use the |
| 1366 | // ufunc pointer, need to look it up again at runtime. |
| 1367 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1368 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1369 | } |
| 1370 | |
| 1371 | stack->ga_len -= argcount; // drop the arguments |
| 1372 | if (ga_grow(stack, 1) == FAIL) |
| 1373 | return FAIL; |
| 1374 | // add return value |
| 1375 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1376 | ++stack->ga_len; |
| 1377 | |
| 1378 | return OK; |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1383 | */ |
| 1384 | static int |
| 1385 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1386 | { |
| 1387 | isn_T *isn; |
| 1388 | garray_T *stack = &cctx->ctx_type_stack; |
| 1389 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1390 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1391 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1392 | return FAIL; |
| 1393 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1394 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1395 | |
| 1396 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1397 | if (ga_grow(stack, 1) == FAIL) |
| 1398 | return FAIL; |
| 1399 | // add return value |
| 1400 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1401 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1402 | |
| 1403 | return OK; |
| 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1408 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1409 | */ |
| 1410 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1411 | generate_PCALL( |
| 1412 | cctx_T *cctx, |
| 1413 | int argcount, |
| 1414 | char_u *name, |
| 1415 | type_T *type, |
| 1416 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1417 | { |
| 1418 | isn_T *isn; |
| 1419 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1420 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1421 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1422 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1423 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1424 | if (type->tt_type == VAR_ANY) |
| 1425 | ret_type = &t_any; |
| 1426 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1427 | { |
| 1428 | if (type->tt_argcount != -1) |
| 1429 | { |
| 1430 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1431 | |
| 1432 | if (argcount < type->tt_min_argcount - varargs) |
| 1433 | { |
| 1434 | semsg(_(e_toofewarg), "[reference]"); |
| 1435 | return FAIL; |
| 1436 | } |
| 1437 | if (!varargs && argcount > type->tt_argcount) |
| 1438 | { |
| 1439 | semsg(_(e_toomanyarg), "[reference]"); |
| 1440 | return FAIL; |
| 1441 | } |
| 1442 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1443 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1444 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1445 | else |
| 1446 | { |
| 1447 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1448 | return FAIL; |
| 1449 | } |
| 1450 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1451 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1452 | return FAIL; |
| 1453 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1454 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1455 | |
| 1456 | stack->ga_len -= argcount; // drop the arguments |
| 1457 | |
| 1458 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1459 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1460 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1461 | // If partial is above the arguments it must be cleared and replaced with |
| 1462 | // the return value. |
| 1463 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1464 | return FAIL; |
| 1465 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1466 | return OK; |
| 1467 | } |
| 1468 | |
| 1469 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1470 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1471 | */ |
| 1472 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1473 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1474 | { |
| 1475 | isn_T *isn; |
| 1476 | garray_T *stack = &cctx->ctx_type_stack; |
| 1477 | type_T *type; |
| 1478 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1479 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1480 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1481 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1482 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1483 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1484 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1485 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1486 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1487 | { |
| 1488 | emsg(_(e_dictreq)); |
| 1489 | return FAIL; |
| 1490 | } |
| 1491 | // change dict type to dict member type |
| 1492 | if (type->tt_type == VAR_DICT) |
| 1493 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1494 | |
| 1495 | return OK; |
| 1496 | } |
| 1497 | |
| 1498 | /* |
| 1499 | * Generate an ISN_ECHO instruction. |
| 1500 | */ |
| 1501 | static int |
| 1502 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1503 | { |
| 1504 | isn_T *isn; |
| 1505 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1506 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1507 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1508 | return FAIL; |
| 1509 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1510 | isn->isn_arg.echo.echo_count = count; |
| 1511 | |
| 1512 | return OK; |
| 1513 | } |
| 1514 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1515 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1516 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1517 | */ |
| 1518 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1519 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1520 | { |
| 1521 | isn_T *isn; |
| 1522 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1523 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1524 | return FAIL; |
| 1525 | isn->isn_arg.number = count; |
| 1526 | |
| 1527 | return OK; |
| 1528 | } |
| 1529 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1530 | static int |
| 1531 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1532 | { |
| 1533 | isn_T *isn; |
| 1534 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1535 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1536 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1537 | return FAIL; |
| 1538 | isn->isn_arg.string = vim_strsave(line); |
| 1539 | return OK; |
| 1540 | } |
| 1541 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1542 | static int |
| 1543 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1544 | { |
| 1545 | isn_T *isn; |
| 1546 | |
| 1547 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1548 | return FAIL; |
| 1549 | isn->isn_arg.number = count; |
| 1550 | return OK; |
| 1551 | } |
| 1552 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1553 | /* |
| 1554 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1555 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1556 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1557 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1558 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1559 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1560 | lvar_T *lvar; |
| 1561 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1562 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1563 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1564 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1565 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1569 | return NULL; |
| 1570 | 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] | 1571 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1572 | // Every local variable uses the next entry on the stack. We could re-use |
| 1573 | // the last ones when leaving a scope, but then variables used in a closure |
| 1574 | // might get overwritten. To keep things simple do not re-use stack |
| 1575 | // entries. This is less efficient, but memory is cheap these days. |
| 1576 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1577 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1578 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1579 | lvar->lv_const = isConst; |
| 1580 | lvar->lv_type = type; |
| 1581 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1582 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1586 | * Remove local variables above "new_top". |
| 1587 | */ |
| 1588 | static void |
| 1589 | unwind_locals(cctx_T *cctx, int new_top) |
| 1590 | { |
| 1591 | if (cctx->ctx_locals.ga_len > new_top) |
| 1592 | { |
| 1593 | int idx; |
| 1594 | lvar_T *lvar; |
| 1595 | |
| 1596 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1597 | { |
| 1598 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1599 | vim_free(lvar->lv_name); |
| 1600 | } |
| 1601 | } |
| 1602 | cctx->ctx_locals.ga_len = new_top; |
| 1603 | } |
| 1604 | |
| 1605 | /* |
| 1606 | * Free all local variables. |
| 1607 | */ |
| 1608 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1609 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1610 | { |
| 1611 | unwind_locals(cctx, 0); |
| 1612 | ga_clear(&cctx->ctx_locals); |
| 1613 | } |
| 1614 | |
| 1615 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1616 | * Find "name" in script-local items of script "sid". |
| 1617 | * Returns the index in "sn_var_vals" if found. |
| 1618 | * If found but not in "sn_var_vals" returns -1. |
| 1619 | * If not found returns -2. |
| 1620 | */ |
| 1621 | int |
| 1622 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 1623 | { |
| 1624 | hashtab_T *ht; |
| 1625 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1626 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1627 | int idx; |
| 1628 | |
| 1629 | // First look the name up in the hashtable. |
| 1630 | if (sid <= 0 || sid > script_items.ga_len) |
| 1631 | return -1; |
| 1632 | ht = &SCRIPT_VARS(sid); |
| 1633 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 1634 | if (di == NULL) |
| 1635 | return -2; |
| 1636 | |
| 1637 | // Now find the svar_T index in sn_var_vals. |
| 1638 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 1639 | { |
| 1640 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1641 | |
| 1642 | if (sv->sv_tv == &di->di_tv) |
| 1643 | { |
| 1644 | if (check_writable && sv->sv_const) |
| 1645 | semsg(_(e_readonlyvar), name); |
| 1646 | return idx; |
| 1647 | } |
| 1648 | } |
| 1649 | return -1; |
| 1650 | } |
| 1651 | |
| 1652 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 1653 | * Find "name" in imported items of the current script or in "cctx" if not |
| 1654 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1655 | */ |
| 1656 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1657 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1658 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1659 | int idx; |
| 1660 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 1661 | if (current_sctx.sc_sid <= 0) |
| 1662 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1663 | if (cctx != NULL) |
| 1664 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1665 | { |
| 1666 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 1667 | + idx; |
| 1668 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1669 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1670 | : STRLEN(import->imp_name) == len |
| 1671 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1672 | return import; |
| 1673 | } |
| 1674 | |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 1675 | return find_imported_in_script(name, len, current_sctx.sc_sid); |
| 1676 | } |
| 1677 | |
| 1678 | imported_T * |
| 1679 | find_imported_in_script(char_u *name, size_t len, int sid) |
| 1680 | { |
| 1681 | scriptitem_T *si = SCRIPT_ITEM(sid); |
| 1682 | int idx; |
| 1683 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1684 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 1685 | { |
| 1686 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 1687 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1688 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1689 | : STRLEN(import->imp_name) == len |
| 1690 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1691 | return import; |
| 1692 | } |
| 1693 | return NULL; |
| 1694 | } |
| 1695 | |
| 1696 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1697 | * Free all imported variables. |
| 1698 | */ |
| 1699 | static void |
| 1700 | free_imported(cctx_T *cctx) |
| 1701 | { |
| 1702 | int idx; |
| 1703 | |
| 1704 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1705 | { |
| 1706 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 1707 | |
| 1708 | vim_free(import->imp_name); |
| 1709 | } |
| 1710 | ga_clear(&cctx->ctx_imports); |
| 1711 | } |
| 1712 | |
| 1713 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1714 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 1715 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 1716 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1717 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1718 | { |
| 1719 | return p[0] == '#' && p[1] != '{'; |
| 1720 | } |
| 1721 | |
| 1722 | /* |
| 1723 | * Return a pointer to the next line that isn't empty or only contains a |
| 1724 | * comment. Skips over white space. |
| 1725 | * Returns NULL if there is none. |
| 1726 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1727 | char_u * |
| 1728 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1729 | { |
| 1730 | int lnum = cctx->ctx_lnum; |
| 1731 | |
| 1732 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 1733 | { |
| 1734 | 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] | 1735 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1736 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 1737 | if (line == NULL) |
| 1738 | break; |
| 1739 | p = skipwhite(line); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1740 | if (*p != NUL && !vim9_comment_start(p)) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1741 | return p; |
| 1742 | } |
| 1743 | return NULL; |
| 1744 | } |
| 1745 | |
| 1746 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1747 | * Called when checking for a following operator at "arg". When the rest of |
| 1748 | * the line is empty or only a comment, peek the next line. If there is a next |
| 1749 | * line return a pointer to it and set "nextp". |
| 1750 | * Otherwise skip over white space. |
| 1751 | */ |
| 1752 | static char_u * |
| 1753 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 1754 | { |
| 1755 | char_u *p = skipwhite(arg); |
| 1756 | |
| 1757 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1758 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1759 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1760 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1761 | if (*nextp != NULL) |
| 1762 | return *nextp; |
| 1763 | } |
| 1764 | return p; |
| 1765 | } |
| 1766 | |
| 1767 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1768 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1769 | * 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] | 1770 | * Returns NULL when at the end. |
| 1771 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1772 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1773 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1774 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1775 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1776 | |
| 1777 | do |
| 1778 | { |
| 1779 | ++cctx->ctx_lnum; |
| 1780 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1781 | { |
| 1782 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1783 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1784 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1785 | 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] | 1786 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 1787 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1788 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 1789 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1790 | return line; |
| 1791 | } |
| 1792 | |
| 1793 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1794 | * 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] | 1795 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1796 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 1797 | */ |
| 1798 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 1799 | 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] | 1800 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1801 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1802 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1803 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1804 | |
| 1805 | if (next == NULL) |
| 1806 | return FAIL; |
| 1807 | *arg = skipwhite(next); |
| 1808 | } |
| 1809 | return OK; |
| 1810 | } |
| 1811 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 1812 | /* |
| 1813 | * Idem, and give an error when failed. |
| 1814 | */ |
| 1815 | static int |
| 1816 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 1817 | { |
| 1818 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 1819 | { |
| 1820 | emsg(_("E1097: line incomplete")); |
| 1821 | return FAIL; |
| 1822 | } |
| 1823 | return OK; |
| 1824 | } |
| 1825 | |
| 1826 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1827 | // Structure passed between the compile_expr* functions to keep track of |
| 1828 | // constants that have been parsed but for which no code was produced yet. If |
| 1829 | // possible expressions on these constants are applied at compile time. If |
| 1830 | // that is not possible, the code to push the constants needs to be generated |
| 1831 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1832 | // Using 50 should be more than enough of 5 levels of (). |
| 1833 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1834 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1835 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1836 | int pp_used; // active entries in pp_tv[] |
| 1837 | } ppconst_T; |
| 1838 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1839 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 1840 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 1841 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1842 | /* |
| 1843 | * Generate a PUSH instruction for "tv". |
| 1844 | * "tv" will be consumed or cleared. |
| 1845 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 1846 | */ |
| 1847 | static int |
| 1848 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 1849 | { |
| 1850 | if (tv != NULL) |
| 1851 | { |
| 1852 | switch (tv->v_type) |
| 1853 | { |
| 1854 | case VAR_UNKNOWN: |
| 1855 | break; |
| 1856 | case VAR_BOOL: |
| 1857 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 1858 | break; |
| 1859 | case VAR_SPECIAL: |
| 1860 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 1861 | break; |
| 1862 | case VAR_NUMBER: |
| 1863 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 1864 | break; |
| 1865 | #ifdef FEAT_FLOAT |
| 1866 | case VAR_FLOAT: |
| 1867 | generate_PUSHF(cctx, tv->vval.v_float); |
| 1868 | break; |
| 1869 | #endif |
| 1870 | case VAR_BLOB: |
| 1871 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 1872 | tv->vval.v_blob = NULL; |
| 1873 | break; |
| 1874 | case VAR_STRING: |
| 1875 | generate_PUSHS(cctx, tv->vval.v_string); |
| 1876 | tv->vval.v_string = NULL; |
| 1877 | break; |
| 1878 | default: |
| 1879 | iemsg("constant type not supported"); |
| 1880 | clear_tv(tv); |
| 1881 | return FAIL; |
| 1882 | } |
| 1883 | tv->v_type = VAR_UNKNOWN; |
| 1884 | } |
| 1885 | return OK; |
| 1886 | } |
| 1887 | |
| 1888 | /* |
| 1889 | * Generate code for any ppconst entries. |
| 1890 | */ |
| 1891 | static int |
| 1892 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 1893 | { |
| 1894 | int i; |
| 1895 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1896 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1897 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 1898 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1899 | for (i = 0; i < ppconst->pp_used; ++i) |
| 1900 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 1901 | ret = FAIL; |
| 1902 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1903 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1904 | return ret; |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * Clear ppconst constants. Used when failing. |
| 1909 | */ |
| 1910 | static void |
| 1911 | clear_ppconst(ppconst_T *ppconst) |
| 1912 | { |
| 1913 | int i; |
| 1914 | |
| 1915 | for (i = 0; i < ppconst->pp_used; ++i) |
| 1916 | clear_tv(&ppconst->pp_tv[i]); |
| 1917 | ppconst->pp_used = 0; |
| 1918 | } |
| 1919 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1920 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1921 | * Generate an instruction to load script-local variable "name", without the |
| 1922 | * leading "s:". |
| 1923 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1924 | */ |
| 1925 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1926 | compile_load_scriptvar( |
| 1927 | cctx_T *cctx, |
| 1928 | char_u *name, // variable NUL terminated |
| 1929 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 1930 | char_u **end, // end of variable |
| 1931 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1932 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1933 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1934 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 1935 | imported_T *import; |
| 1936 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 1937 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1938 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 1939 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1940 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 1941 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1942 | } |
| 1943 | if (idx >= 0) |
| 1944 | { |
| 1945 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1946 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1947 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1948 | current_sctx.sc_sid, idx, sv->sv_type); |
| 1949 | return OK; |
| 1950 | } |
| 1951 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1952 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1953 | if (import != NULL) |
| 1954 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1955 | if (import->imp_all) |
| 1956 | { |
| 1957 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 1958 | char_u *exp_name; |
| 1959 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1960 | ufunc_T *ufunc; |
| 1961 | type_T *type; |
| 1962 | |
| 1963 | // Used "import * as Name", need to lookup the member. |
| 1964 | if (*p != '.') |
| 1965 | { |
| 1966 | semsg(_("E1060: expected dot after name: %s"), start); |
| 1967 | return FAIL; |
| 1968 | } |
| 1969 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1970 | if (VIM_ISWHITE(*p)) |
| 1971 | { |
| 1972 | emsg(_("E1074: no white space allowed after dot")); |
| 1973 | return FAIL; |
| 1974 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1975 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 1976 | // isolate one name |
| 1977 | exp_name = p; |
| 1978 | while (eval_isnamec(*p)) |
| 1979 | ++p; |
| 1980 | cc = *p; |
| 1981 | *p = NUL; |
| 1982 | |
| 1983 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 1984 | *p = cc; |
| 1985 | p = skipwhite(p); |
| 1986 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1987 | // TODO: what if it is a function? |
| 1988 | if (idx < 0) |
| 1989 | return FAIL; |
| 1990 | *end = p; |
| 1991 | |
| 1992 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 1993 | import->imp_sid, |
| 1994 | idx, |
| 1995 | type); |
| 1996 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 1997 | else if (import->imp_funcname != NULL) |
| 1998 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1999 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2000 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2001 | import->imp_sid, |
| 2002 | import->imp_var_vals_idx, |
| 2003 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2004 | return OK; |
| 2005 | } |
| 2006 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2007 | if (error) |
| 2008 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2009 | return FAIL; |
| 2010 | } |
| 2011 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2012 | static int |
| 2013 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2014 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2015 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2016 | |
| 2017 | if (ufunc == NULL) |
| 2018 | return FAIL; |
| 2019 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2020 | // Need to compile any default values to get the argument types. |
| 2021 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2022 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2023 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2024 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2025 | } |
| 2026 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2027 | /* |
| 2028 | * Compile a variable name into a load instruction. |
| 2029 | * "end" points to just after the name. |
| 2030 | * When "error" is FALSE do not give an error when not found. |
| 2031 | */ |
| 2032 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2033 | 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] | 2034 | { |
| 2035 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2036 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2037 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2038 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2039 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2040 | |
| 2041 | if (*(*arg + 1) == ':') |
| 2042 | { |
| 2043 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2044 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2045 | { |
| 2046 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2047 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2048 | switch (**arg) |
| 2049 | { |
| 2050 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2051 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2052 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2053 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2054 | default: |
| 2055 | semsg(_(e_namespace), *arg); |
| 2056 | goto theend; |
| 2057 | } |
| 2058 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2059 | goto theend; |
| 2060 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2061 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2062 | else |
| 2063 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2064 | isntype_T isn_type = ISN_DROP; |
| 2065 | |
| 2066 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2067 | if (name == NULL) |
| 2068 | return FAIL; |
| 2069 | |
| 2070 | switch (**arg) |
| 2071 | { |
| 2072 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2073 | break; |
| 2074 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2075 | NULL, NULL, error); |
| 2076 | break; |
| 2077 | case 'g': isn_type = ISN_LOADG; break; |
| 2078 | case 'w': isn_type = ISN_LOADW; break; |
| 2079 | case 't': isn_type = ISN_LOADT; break; |
| 2080 | case 'b': isn_type = ISN_LOADB; break; |
| 2081 | default: semsg(_(e_namespace), *arg); |
| 2082 | goto theend; |
| 2083 | } |
| 2084 | if (isn_type != ISN_DROP) |
| 2085 | { |
| 2086 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2087 | // variables can be defined later, thus we don't check if it |
| 2088 | // exists, give error at runtime. |
| 2089 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2090 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2091 | } |
| 2092 | } |
| 2093 | else |
| 2094 | { |
| 2095 | size_t len = end - *arg; |
| 2096 | int idx; |
| 2097 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2098 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2099 | |
| 2100 | name = vim_strnsave(*arg, end - *arg); |
| 2101 | if (name == NULL) |
| 2102 | return FAIL; |
| 2103 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2104 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2105 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2106 | if (!gen_load_outer) |
| 2107 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2108 | } |
| 2109 | else |
| 2110 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2111 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2112 | |
| 2113 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2114 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2115 | type = lvar->lv_type; |
| 2116 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2117 | if (lvar->lv_from_outer) |
| 2118 | gen_load_outer = TRUE; |
| 2119 | else |
| 2120 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2121 | } |
| 2122 | else |
| 2123 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2124 | // "var" can be script-local even without using "s:" if it |
| 2125 | // already exists. |
| 2126 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2127 | == SCRIPT_VERSION_VIM9 |
| 2128 | || lookup_script(*arg, len) == OK) |
| 2129 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2130 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2131 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2132 | // When the name starts with an uppercase letter or "x:" it |
| 2133 | // can be a user defined function. |
| 2134 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2135 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2136 | } |
| 2137 | } |
| 2138 | if (gen_load) |
| 2139 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2140 | if (gen_load_outer) |
| 2141 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | *arg = end; |
| 2145 | |
| 2146 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2147 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2148 | semsg(_(e_var_notfound), name); |
| 2149 | vim_free(name); |
| 2150 | return res; |
| 2151 | } |
| 2152 | |
| 2153 | /* |
| 2154 | * Compile the argument expressions. |
| 2155 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2156 | */ |
| 2157 | static int |
| 2158 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2159 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2160 | char_u *p = *arg; |
| 2161 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2162 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2163 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2164 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2165 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2166 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2167 | if (*p == ')') |
| 2168 | { |
| 2169 | *arg = p + 1; |
| 2170 | return OK; |
| 2171 | } |
| 2172 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2173 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2174 | return FAIL; |
| 2175 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2176 | |
| 2177 | if (*p != ',' && *skipwhite(p) == ',') |
| 2178 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2179 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2180 | p = skipwhite(p); |
| 2181 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2182 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2183 | { |
| 2184 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2185 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2186 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2187 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2188 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2189 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2190 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2191 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2192 | emsg(_(e_missing_close)); |
| 2193 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | /* |
| 2197 | * Compile a function call: name(arg1, arg2) |
| 2198 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2199 | * "argcount_init" is 1 for "value->method()" |
| 2200 | * Instructions: |
| 2201 | * EVAL arg1 |
| 2202 | * EVAL arg2 |
| 2203 | * BCALL / DCALL / UCALL |
| 2204 | */ |
| 2205 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2206 | compile_call( |
| 2207 | char_u **arg, |
| 2208 | size_t varlen, |
| 2209 | cctx_T *cctx, |
| 2210 | ppconst_T *ppconst, |
| 2211 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2212 | { |
| 2213 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2214 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2215 | int argcount = argcount_init; |
| 2216 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2217 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2218 | char_u *tofree = NULL; |
| 2219 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2220 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2221 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2222 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2223 | // we can evaluate "has('name')" at compile time |
| 2224 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2225 | { |
| 2226 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2227 | typval_T argvars[2]; |
| 2228 | |
| 2229 | argvars[0].v_type = VAR_UNKNOWN; |
| 2230 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2231 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2232 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2233 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2234 | s = skipwhite(s); |
| 2235 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 2236 | { |
| 2237 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2238 | |
| 2239 | *arg = s + 1; |
| 2240 | argvars[1].v_type = VAR_UNKNOWN; |
| 2241 | tv->v_type = VAR_NUMBER; |
| 2242 | tv->vval.v_number = 0; |
| 2243 | f_has(argvars, tv); |
| 2244 | clear_tv(&argvars[0]); |
| 2245 | ++ppconst->pp_used; |
| 2246 | return OK; |
| 2247 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2248 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2252 | return FAIL; |
| 2253 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2254 | if (varlen >= sizeof(namebuf)) |
| 2255 | { |
| 2256 | semsg(_("E1011: name too long: %s"), name); |
| 2257 | return FAIL; |
| 2258 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2259 | vim_strncpy(namebuf, *arg, varlen); |
| 2260 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2261 | |
| 2262 | *arg = skipwhite(*arg + varlen + 1); |
| 2263 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2264 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2265 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2266 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2267 | { |
| 2268 | int idx; |
| 2269 | |
| 2270 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2271 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2272 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 2273 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2274 | else |
| 2275 | semsg(_(e_unknownfunc), namebuf); |
| 2276 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2277 | } |
| 2278 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2279 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2280 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2281 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2282 | { |
| 2283 | res = generate_CALL(cctx, ufunc, argcount); |
| 2284 | goto theend; |
| 2285 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2286 | |
| 2287 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2288 | // Not for g:Func(), we don't know if it is a variable or not. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2289 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2290 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 2291 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2292 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2293 | garray_T *stack = &cctx->ctx_type_stack; |
| 2294 | type_T *type; |
| 2295 | |
| 2296 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2297 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2298 | goto theend; |
| 2299 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2300 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2301 | // 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] | 2302 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2303 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 2304 | res = generate_UCALL(cctx, name, argcount); |
| 2305 | else |
| 2306 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2307 | |
| 2308 | theend: |
| 2309 | vim_free(tofree); |
| 2310 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2314 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2315 | |
| 2316 | /* |
| 2317 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2318 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2319 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2320 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2321 | * valid name. |
| 2322 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2323 | static char_u * |
| 2324 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2325 | { |
| 2326 | char_u *p; |
| 2327 | |
| 2328 | // Quick check for valid starting character. |
| 2329 | if (!eval_isnamec1(*arg)) |
| 2330 | return arg; |
| 2331 | |
| 2332 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2333 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2334 | // and can be used in slice "[n:]". |
| 2335 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2336 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2337 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2338 | break; |
| 2339 | return p; |
| 2340 | } |
| 2341 | |
| 2342 | /* |
| 2343 | * 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] | 2344 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2345 | */ |
| 2346 | char_u * |
| 2347 | to_name_const_end(char_u *arg) |
| 2348 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2349 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2350 | typval_T rettv; |
| 2351 | |
| 2352 | if (p == arg && *arg == '[') |
| 2353 | { |
| 2354 | |
| 2355 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2356 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2357 | p = arg; |
| 2358 | } |
| 2359 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 2360 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2361 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2362 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 2363 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2364 | p = arg; |
| 2365 | } |
| 2366 | else if (p == arg && *arg == '{') |
| 2367 | { |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 2368 | int ret = get_lambda_tv(&p, &rettv, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2369 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2370 | // Can be "{x -> ret}()". |
| 2371 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2372 | if (ret == NOTDONE) |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 2373 | ret = eval_dict(&p, &rettv, NULL, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2374 | if (ret != OK) |
| 2375 | p = arg; |
| 2376 | } |
| 2377 | |
| 2378 | return p; |
| 2379 | } |
| 2380 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2381 | /* |
| 2382 | * parse a list: [expr, expr] |
| 2383 | * "*arg" points to the '['. |
| 2384 | */ |
| 2385 | static int |
| 2386 | compile_list(char_u **arg, cctx_T *cctx) |
| 2387 | { |
| 2388 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2389 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2390 | int count = 0; |
| 2391 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2392 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2393 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2394 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2395 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2396 | semsg(_(e_list_end), *arg); |
| 2397 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2398 | } |
| 2399 | if (*p == ']') |
| 2400 | { |
| 2401 | ++p; |
| 2402 | // Allow for following comment, after at least one space. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2403 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '#') |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2404 | p += STRLEN(p); |
| 2405 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2406 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2407 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2408 | break; |
| 2409 | ++count; |
| 2410 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2411 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2412 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2413 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 2414 | { |
| 2415 | semsg(_(e_white_after), ","); |
| 2416 | return FAIL; |
| 2417 | } |
| 2418 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2419 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2420 | p = skipwhite(p); |
| 2421 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2422 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2423 | |
| 2424 | generate_NEWLIST(cctx, count); |
| 2425 | return OK; |
| 2426 | } |
| 2427 | |
| 2428 | /* |
| 2429 | * parse a lambda: {arg, arg -> expr} |
| 2430 | * "*arg" points to the '{'. |
| 2431 | */ |
| 2432 | static int |
| 2433 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 2434 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2435 | typval_T rettv; |
| 2436 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2437 | evalarg_T evalarg; |
| 2438 | |
| 2439 | CLEAR_FIELD(evalarg); |
| 2440 | evalarg.eval_flags = EVAL_EVALUATE; |
| 2441 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2442 | |
| 2443 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2444 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2445 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2446 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2447 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2448 | ++ufunc->uf_refcount; |
| 2449 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2450 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2451 | |
| 2452 | // The function will have one line: "return {expr}". |
| 2453 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2454 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2455 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 2456 | clear_evalarg(&evalarg, NULL); |
| 2457 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 2458 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 2459 | { |
| 2460 | // The return type will now be known. |
| 2461 | set_function_type(ufunc); |
| 2462 | |
| 2463 | return generate_FUNCREF(cctx, ufunc); |
| 2464 | } |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2465 | |
| 2466 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2467 | return FAIL; |
| 2468 | } |
| 2469 | |
| 2470 | /* |
| 2471 | * Compile a lamda call: expr->{lambda}(args) |
| 2472 | * "arg" points to the "{". |
| 2473 | */ |
| 2474 | static int |
| 2475 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 2476 | { |
| 2477 | ufunc_T *ufunc; |
| 2478 | typval_T rettv; |
| 2479 | int argcount = 1; |
| 2480 | int ret = FAIL; |
| 2481 | |
| 2482 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 2483 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2484 | return FAIL; |
| 2485 | |
| 2486 | if (**arg != '(') |
| 2487 | { |
| 2488 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 2489 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2490 | else |
| 2491 | semsg(_(e_missing_paren), "lambda"); |
| 2492 | clear_tv(&rettv); |
| 2493 | return FAIL; |
| 2494 | } |
| 2495 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2496 | ufunc = rettv.vval.v_partial->pt_func; |
| 2497 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2498 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2499 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2500 | |
| 2501 | // The function will have one line: "return {expr}". |
| 2502 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2503 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2504 | |
| 2505 | // compile the arguments |
| 2506 | *arg = skipwhite(*arg + 1); |
| 2507 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 2508 | // call the compiled function |
| 2509 | ret = generate_CALL(cctx, ufunc, argcount); |
| 2510 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2511 | if (ret == FAIL) |
| 2512 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2513 | return ret; |
| 2514 | } |
| 2515 | |
| 2516 | /* |
| 2517 | * parse a dict: {'key': val} or #{key: val} |
| 2518 | * "*arg" points to the '{'. |
| 2519 | */ |
| 2520 | static int |
| 2521 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 2522 | { |
| 2523 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2524 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2525 | int count = 0; |
| 2526 | dict_T *d = dict_alloc(); |
| 2527 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2528 | char_u *whitep = *arg; |
| 2529 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2530 | |
| 2531 | if (d == NULL) |
| 2532 | return FAIL; |
| 2533 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2534 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2535 | { |
| 2536 | char_u *key = NULL; |
| 2537 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2538 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2539 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2540 | *arg = NULL; |
| 2541 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | if (**arg == '}') |
| 2545 | break; |
| 2546 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2547 | if (literal) |
| 2548 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2549 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2550 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2551 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2552 | { |
| 2553 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 2554 | return FAIL; |
| 2555 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2556 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2557 | if (generate_PUSHS(cctx, key) == FAIL) |
| 2558 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2559 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2560 | } |
| 2561 | else |
| 2562 | { |
| 2563 | isn_T *isn; |
| 2564 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2565 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2566 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2567 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 2568 | if (isn->isn_type == ISN_PUSHS) |
| 2569 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2570 | else |
| 2571 | { |
| 2572 | type_T *keytype = ((type_T **)stack->ga_data) |
| 2573 | [stack->ga_len - 1]; |
| 2574 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 2575 | return FAIL; |
| 2576 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | // Check for duplicate keys, if using string keys. |
| 2580 | if (key != NULL) |
| 2581 | { |
| 2582 | item = dict_find(d, key, -1); |
| 2583 | if (item != NULL) |
| 2584 | { |
| 2585 | semsg(_(e_duplicate_key), key); |
| 2586 | goto failret; |
| 2587 | } |
| 2588 | item = dictitem_alloc(key); |
| 2589 | if (item != NULL) |
| 2590 | { |
| 2591 | item->di_tv.v_type = VAR_UNKNOWN; |
| 2592 | item->di_tv.v_lock = 0; |
| 2593 | if (dict_add(d, item) == FAIL) |
| 2594 | dictitem_free(item); |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | *arg = skipwhite(*arg); |
| 2599 | if (**arg != ':') |
| 2600 | { |
| 2601 | semsg(_(e_missing_dict_colon), *arg); |
| 2602 | return FAIL; |
| 2603 | } |
| 2604 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2605 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2606 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2607 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2608 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2609 | *arg = NULL; |
| 2610 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2611 | } |
| 2612 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2613 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2614 | return FAIL; |
| 2615 | ++count; |
| 2616 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2617 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2618 | *arg = skipwhite(*arg); |
| 2619 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2620 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2621 | *arg = NULL; |
| 2622 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2623 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2624 | if (**arg == '}') |
| 2625 | break; |
| 2626 | if (**arg != ',') |
| 2627 | { |
| 2628 | semsg(_(e_missing_dict_comma), *arg); |
| 2629 | goto failret; |
| 2630 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2631 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2632 | *arg = skipwhite(*arg + 1); |
| 2633 | } |
| 2634 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2635 | *arg = *arg + 1; |
| 2636 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2637 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2638 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2639 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2640 | *arg += STRLEN(*arg); |
| 2641 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2642 | dict_unref(d); |
| 2643 | return generate_NEWDICT(cctx, count); |
| 2644 | |
| 2645 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2646 | if (*arg == NULL) |
| 2647 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2648 | dict_unref(d); |
| 2649 | return FAIL; |
| 2650 | } |
| 2651 | |
| 2652 | /* |
| 2653 | * Compile "&option". |
| 2654 | */ |
| 2655 | static int |
| 2656 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 2657 | { |
| 2658 | typval_T rettv; |
| 2659 | char_u *start = *arg; |
| 2660 | int ret; |
| 2661 | |
| 2662 | // parse the option and get the current value to get the type. |
| 2663 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2664 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2665 | if (ret == OK) |
| 2666 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2667 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2668 | char_u *name = vim_strnsave(start, *arg - start); |
| 2669 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 2670 | |
| 2671 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 2672 | vim_free(name); |
| 2673 | } |
| 2674 | clear_tv(&rettv); |
| 2675 | |
| 2676 | return ret; |
| 2677 | } |
| 2678 | |
| 2679 | /* |
| 2680 | * Compile "$VAR". |
| 2681 | */ |
| 2682 | static int |
| 2683 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 2684 | { |
| 2685 | char_u *start = *arg; |
| 2686 | int len; |
| 2687 | int ret; |
| 2688 | char_u *name; |
| 2689 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2690 | ++*arg; |
| 2691 | len = get_env_len(arg); |
| 2692 | if (len == 0) |
| 2693 | { |
| 2694 | semsg(_(e_syntax_at), start - 1); |
| 2695 | return FAIL; |
| 2696 | } |
| 2697 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2698 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2699 | name = vim_strnsave(start, len + 1); |
| 2700 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 2701 | vim_free(name); |
| 2702 | return ret; |
| 2703 | } |
| 2704 | |
| 2705 | /* |
| 2706 | * Compile "@r". |
| 2707 | */ |
| 2708 | static int |
| 2709 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 2710 | { |
| 2711 | int ret; |
| 2712 | |
| 2713 | ++*arg; |
| 2714 | if (**arg == NUL) |
| 2715 | { |
| 2716 | semsg(_(e_syntax_at), *arg - 1); |
| 2717 | return FAIL; |
| 2718 | } |
Bram Moolenaar | 7226e5b | 2020-08-02 17:33:26 +0200 | [diff] [blame] | 2719 | if (!valid_yank_reg(**arg, FALSE)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2720 | { |
| 2721 | emsg_invreg(**arg); |
| 2722 | return FAIL; |
| 2723 | } |
| 2724 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 2725 | ++*arg; |
| 2726 | return ret; |
| 2727 | } |
| 2728 | |
| 2729 | /* |
| 2730 | * Apply leading '!', '-' and '+' to constant "rettv". |
| 2731 | */ |
| 2732 | static int |
| 2733 | apply_leader(typval_T *rettv, char_u *start, char_u *end) |
| 2734 | { |
| 2735 | char_u *p = end; |
| 2736 | |
| 2737 | // this works from end to start |
| 2738 | while (p > start) |
| 2739 | { |
| 2740 | --p; |
| 2741 | if (*p == '-' || *p == '+') |
| 2742 | { |
| 2743 | // only '-' has an effect, for '+' we only check the type |
| 2744 | #ifdef FEAT_FLOAT |
| 2745 | if (rettv->v_type == VAR_FLOAT) |
| 2746 | { |
| 2747 | if (*p == '-') |
| 2748 | rettv->vval.v_float = -rettv->vval.v_float; |
| 2749 | } |
| 2750 | else |
| 2751 | #endif |
| 2752 | { |
| 2753 | varnumber_T val; |
| 2754 | int error = FALSE; |
| 2755 | |
| 2756 | // tv_get_number_chk() accepts a string, but we don't want that |
| 2757 | // here |
| 2758 | if (check_not_string(rettv) == FAIL) |
| 2759 | return FAIL; |
| 2760 | val = tv_get_number_chk(rettv, &error); |
| 2761 | clear_tv(rettv); |
| 2762 | if (error) |
| 2763 | return FAIL; |
| 2764 | if (*p == '-') |
| 2765 | val = -val; |
| 2766 | rettv->v_type = VAR_NUMBER; |
| 2767 | rettv->vval.v_number = val; |
| 2768 | } |
| 2769 | } |
| 2770 | else |
| 2771 | { |
| 2772 | int v = tv2bool(rettv); |
| 2773 | |
| 2774 | // '!' is permissive in the type. |
| 2775 | clear_tv(rettv); |
| 2776 | rettv->v_type = VAR_BOOL; |
| 2777 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 2778 | } |
| 2779 | } |
| 2780 | return OK; |
| 2781 | } |
| 2782 | |
| 2783 | /* |
| 2784 | * Recognize v: variables that are constants and set "rettv". |
| 2785 | */ |
| 2786 | static void |
| 2787 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 2788 | { |
| 2789 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 2790 | { |
| 2791 | rettv->v_type = VAR_BOOL; |
| 2792 | rettv->vval.v_number = VVAL_TRUE; |
| 2793 | *arg += 6; |
| 2794 | } |
| 2795 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 2796 | { |
| 2797 | rettv->v_type = VAR_BOOL; |
| 2798 | rettv->vval.v_number = VVAL_FALSE; |
| 2799 | *arg += 7; |
| 2800 | } |
| 2801 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 2802 | { |
| 2803 | rettv->v_type = VAR_SPECIAL; |
| 2804 | rettv->vval.v_number = VVAL_NULL; |
| 2805 | *arg += 6; |
| 2806 | } |
| 2807 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 2808 | { |
| 2809 | rettv->v_type = VAR_SPECIAL; |
| 2810 | rettv->vval.v_number = VVAL_NONE; |
| 2811 | *arg += 6; |
| 2812 | } |
| 2813 | } |
| 2814 | |
Bram Moolenaar | 696ba23 | 2020-07-29 21:20:41 +0200 | [diff] [blame] | 2815 | exptype_T |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 2816 | get_compare_type(char_u *p, int *len, int *type_is) |
| 2817 | { |
| 2818 | exptype_T type = EXPR_UNKNOWN; |
| 2819 | int i; |
| 2820 | |
| 2821 | switch (p[0]) |
| 2822 | { |
| 2823 | case '=': if (p[1] == '=') |
| 2824 | type = EXPR_EQUAL; |
| 2825 | else if (p[1] == '~') |
| 2826 | type = EXPR_MATCH; |
| 2827 | break; |
| 2828 | case '!': if (p[1] == '=') |
| 2829 | type = EXPR_NEQUAL; |
| 2830 | else if (p[1] == '~') |
| 2831 | type = EXPR_NOMATCH; |
| 2832 | break; |
| 2833 | case '>': if (p[1] != '=') |
| 2834 | { |
| 2835 | type = EXPR_GREATER; |
| 2836 | *len = 1; |
| 2837 | } |
| 2838 | else |
| 2839 | type = EXPR_GEQUAL; |
| 2840 | break; |
| 2841 | case '<': if (p[1] != '=') |
| 2842 | { |
| 2843 | type = EXPR_SMALLER; |
| 2844 | *len = 1; |
| 2845 | } |
| 2846 | else |
| 2847 | type = EXPR_SEQUAL; |
| 2848 | break; |
| 2849 | case 'i': if (p[1] == 's') |
| 2850 | { |
| 2851 | // "is" and "isnot"; but not a prefix of a name |
| 2852 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 2853 | *len = 5; |
| 2854 | i = p[*len]; |
| 2855 | if (!isalnum(i) && i != '_') |
| 2856 | { |
| 2857 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 2858 | *type_is = TRUE; |
| 2859 | } |
| 2860 | } |
| 2861 | break; |
| 2862 | } |
| 2863 | return type; |
| 2864 | } |
| 2865 | |
| 2866 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2867 | * Compile code to apply '-', '+' and '!'. |
| 2868 | */ |
| 2869 | static int |
| 2870 | compile_leader(cctx_T *cctx, char_u *start, char_u *end) |
| 2871 | { |
| 2872 | char_u *p = end; |
| 2873 | |
| 2874 | // this works from end to start |
| 2875 | while (p > start) |
| 2876 | { |
| 2877 | --p; |
| 2878 | if (*p == '-' || *p == '+') |
| 2879 | { |
| 2880 | int negate = *p == '-'; |
| 2881 | isn_T *isn; |
| 2882 | |
| 2883 | // TODO: check type |
| 2884 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 2885 | { |
| 2886 | --p; |
| 2887 | if (*p == '-') |
| 2888 | negate = !negate; |
| 2889 | } |
| 2890 | // only '-' has an effect, for '+' we only check the type |
| 2891 | if (negate) |
| 2892 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 2893 | else |
| 2894 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 2895 | if (isn == NULL) |
| 2896 | return FAIL; |
| 2897 | } |
| 2898 | else |
| 2899 | { |
| 2900 | int invert = TRUE; |
| 2901 | |
| 2902 | while (p > start && p[-1] == '!') |
| 2903 | { |
| 2904 | --p; |
| 2905 | invert = !invert; |
| 2906 | } |
| 2907 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 2908 | return FAIL; |
| 2909 | } |
| 2910 | } |
| 2911 | return OK; |
| 2912 | } |
| 2913 | |
| 2914 | /* |
| 2915 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2916 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2917 | */ |
| 2918 | static int |
| 2919 | compile_subscript( |
| 2920 | char_u **arg, |
| 2921 | cctx_T *cctx, |
| 2922 | char_u **start_leader, |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 2923 | char_u *end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 2924 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2925 | { |
| 2926 | for (;;) |
| 2927 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2928 | char_u *p = skipwhite(*arg); |
| 2929 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2930 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2931 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2932 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2933 | |
| 2934 | // If a following line starts with "->{" or "->X" advance to that |
| 2935 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2936 | // Also if a following line starts with ".x". |
| 2937 | if (next != NULL && |
| 2938 | ((next[0] == '-' && next[1] == '>' |
| 2939 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 2940 | || (next[0] == '.' && eval_isdictc(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2941 | { |
| 2942 | next = next_line_from_context(cctx, TRUE); |
| 2943 | if (next == NULL) |
| 2944 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2945 | *arg = next; |
| 2946 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2947 | } |
| 2948 | } |
| 2949 | |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 2950 | // Do not skip over white space to find the "(", "exeucte 'x' ()" is |
| 2951 | // not a function call. |
| 2952 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2953 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2954 | garray_T *stack = &cctx->ctx_type_stack; |
| 2955 | type_T *type; |
| 2956 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2957 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 2958 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 2959 | return FAIL; |
| 2960 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2961 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2962 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2963 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2964 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2965 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 2966 | return FAIL; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2967 | if (generate_PCALL(cctx, argcount, end_leader, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2968 | return FAIL; |
| 2969 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2970 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2971 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 2972 | char_u *pstart = p; |
| 2973 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 2974 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 2975 | return FAIL; |
| 2976 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2977 | // something->method() |
| 2978 | // Apply the '!', '-' and '+' first: |
| 2979 | // -1.0->func() works like (-1.0)->func() |
| 2980 | if (compile_leader(cctx, *start_leader, end_leader) == FAIL) |
| 2981 | return FAIL; |
| 2982 | *start_leader = end_leader; // don't apply again later |
| 2983 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2984 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 2985 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 2986 | // No line break supported right after "->". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2987 | if (**arg == '{') |
| 2988 | { |
| 2989 | // lambda call: list->{lambda} |
| 2990 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 2991 | return FAIL; |
| 2992 | } |
| 2993 | else |
| 2994 | { |
| 2995 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 2996 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 2997 | if (!eval_isnamec1(*p)) |
| 2998 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 2999 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3000 | return FAIL; |
| 3001 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3002 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3003 | p += 2; |
Bram Moolenaar | c5da1fb | 2020-08-05 15:43:44 +0200 | [diff] [blame] | 3004 | for ( ; eval_isnamec(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3005 | ; |
| 3006 | if (*p != '(') |
| 3007 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3008 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3009 | return FAIL; |
| 3010 | } |
| 3011 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3012 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3013 | return FAIL; |
| 3014 | } |
| 3015 | } |
Bram Moolenaar | badd848 | 2020-07-31 22:38:17 +0200 | [diff] [blame] | 3016 | else if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3017 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3018 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3019 | type_T **typep; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3020 | vartype_T vtype; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3021 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3022 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3023 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3024 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3025 | // TODO: blob index |
| 3026 | // TODO: more arguments |
| 3027 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3028 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3029 | return FAIL; |
| 3030 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3031 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3032 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3033 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3034 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3035 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3036 | return FAIL; |
| 3037 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3038 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3039 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3040 | if (**arg != ']') |
| 3041 | { |
| 3042 | emsg(_(e_missbrac)); |
| 3043 | return FAIL; |
| 3044 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3045 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3046 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3047 | // We can index a list and a dict. If we don't know the type |
| 3048 | // we can use the index value type. |
| 3049 | // TODO: If we don't know use an instruction to figure it out at |
| 3050 | // runtime. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3051 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3052 | vtype = (*typep)->tt_type; |
| 3053 | if (*typep == &t_any) |
| 3054 | { |
| 3055 | type_T *valtype = ((type_T **)stack->ga_data) |
| 3056 | [stack->ga_len - 1]; |
| 3057 | if (valtype == &t_string) |
| 3058 | vtype = VAR_DICT; |
| 3059 | } |
| 3060 | if (vtype == VAR_DICT) |
| 3061 | { |
| 3062 | if ((*typep)->tt_type == VAR_DICT) |
| 3063 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3064 | else |
| 3065 | { |
| 3066 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3067 | return FAIL; |
| 3068 | *typep = &t_any; |
| 3069 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3070 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3071 | return FAIL; |
| 3072 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3073 | return FAIL; |
| 3074 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3075 | else if (vtype == VAR_STRING) |
| 3076 | { |
| 3077 | *typep = &t_number; |
| 3078 | if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL) |
| 3079 | return FAIL; |
| 3080 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3081 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3082 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3083 | if ((*typep)->tt_type == VAR_LIST) |
| 3084 | *typep = (*typep)->tt_member; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3085 | if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3086 | return FAIL; |
| 3087 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3088 | else |
| 3089 | { |
| 3090 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3091 | return FAIL; |
| 3092 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3093 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3094 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3095 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3096 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3097 | return FAIL; |
| 3098 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3099 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3100 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3101 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3102 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3103 | p = *arg; |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3104 | if (eval_isdictc(*p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3105 | while (eval_isnamec(*p)) |
| 3106 | MB_PTR_ADV(p); |
| 3107 | if (p == *arg) |
| 3108 | { |
| 3109 | semsg(_(e_syntax_at), *arg); |
| 3110 | return FAIL; |
| 3111 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3112 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3113 | return FAIL; |
| 3114 | *arg = p; |
| 3115 | } |
| 3116 | else |
| 3117 | break; |
| 3118 | } |
| 3119 | |
| 3120 | // TODO - see handle_subscript(): |
| 3121 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3122 | // Don't do this when "Func" is already a partial that was bound |
| 3123 | // explicitly (pt_auto is FALSE). |
| 3124 | |
| 3125 | return OK; |
| 3126 | } |
| 3127 | |
| 3128 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3129 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3130 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3131 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3132 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3133 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3134 | * |
| 3135 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3136 | */ |
| 3137 | |
| 3138 | /* |
| 3139 | * number number constant |
| 3140 | * 0zFFFFFFFF Blob constant |
| 3141 | * "string" string constant |
| 3142 | * 'string' literal string constant |
| 3143 | * &option-name option value |
| 3144 | * @r register contents |
| 3145 | * identifier variable value |
| 3146 | * function() function call |
| 3147 | * $VAR environment variable |
| 3148 | * (expression) nested expression |
| 3149 | * [expr, expr] List |
| 3150 | * {key: val, key: val} Dictionary |
| 3151 | * #{key: val, key: val} Dictionary with literal keys |
| 3152 | * |
| 3153 | * Also handle: |
| 3154 | * ! in front logical NOT |
| 3155 | * - in front unary minus |
| 3156 | * + in front unary plus (ignored) |
| 3157 | * trailing (arg) funcref/partial call |
| 3158 | * trailing [] subscript in String or List |
| 3159 | * trailing .name entry in Dictionary |
| 3160 | * trailing ->name() method call |
| 3161 | */ |
| 3162 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3163 | compile_expr7( |
| 3164 | char_u **arg, |
| 3165 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3166 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3167 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3168 | char_u *start_leader, *end_leader; |
| 3169 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3170 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3171 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3172 | |
| 3173 | /* |
| 3174 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3175 | */ |
| 3176 | start_leader = *arg; |
| 3177 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3178 | *arg = skipwhite(*arg + 1); |
| 3179 | end_leader = *arg; |
| 3180 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3181 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3182 | switch (**arg) |
| 3183 | { |
| 3184 | /* |
| 3185 | * Number constant. |
| 3186 | */ |
| 3187 | case '0': // also for blob starting with 0z |
| 3188 | case '1': |
| 3189 | case '2': |
| 3190 | case '3': |
| 3191 | case '4': |
| 3192 | case '5': |
| 3193 | case '6': |
| 3194 | case '7': |
| 3195 | case '8': |
| 3196 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3197 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3198 | return FAIL; |
| 3199 | break; |
| 3200 | |
| 3201 | /* |
| 3202 | * String constant: "string". |
| 3203 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3204 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3205 | return FAIL; |
| 3206 | break; |
| 3207 | |
| 3208 | /* |
| 3209 | * Literal string constant: 'str''ing'. |
| 3210 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3211 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3212 | return FAIL; |
| 3213 | break; |
| 3214 | |
| 3215 | /* |
| 3216 | * Constant Vim variable. |
| 3217 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3218 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3219 | ret = NOTDONE; |
| 3220 | break; |
| 3221 | |
| 3222 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3223 | * "true" constant |
| 3224 | */ |
| 3225 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3226 | && !eval_isnamec((*arg)[4])) |
| 3227 | { |
| 3228 | *arg += 4; |
| 3229 | rettv->v_type = VAR_BOOL; |
| 3230 | rettv->vval.v_number = VVAL_TRUE; |
| 3231 | } |
| 3232 | else |
| 3233 | ret = NOTDONE; |
| 3234 | break; |
| 3235 | |
| 3236 | /* |
| 3237 | * "false" constant |
| 3238 | */ |
| 3239 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3240 | && !eval_isnamec((*arg)[5])) |
| 3241 | { |
| 3242 | *arg += 5; |
| 3243 | rettv->v_type = VAR_BOOL; |
| 3244 | rettv->vval.v_number = VVAL_FALSE; |
| 3245 | } |
| 3246 | else |
| 3247 | ret = NOTDONE; |
| 3248 | break; |
| 3249 | |
| 3250 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3251 | * List: [expr, expr] |
| 3252 | */ |
| 3253 | case '[': ret = compile_list(arg, cctx); |
| 3254 | break; |
| 3255 | |
| 3256 | /* |
| 3257 | * Dictionary: #{key: val, key: val} |
| 3258 | */ |
| 3259 | case '#': if ((*arg)[1] == '{') |
| 3260 | { |
| 3261 | ++*arg; |
| 3262 | ret = compile_dict(arg, cctx, TRUE); |
| 3263 | } |
| 3264 | else |
| 3265 | ret = NOTDONE; |
| 3266 | break; |
| 3267 | |
| 3268 | /* |
| 3269 | * Lambda: {arg, arg -> expr} |
| 3270 | * Dictionary: {'key': val, 'key': val} |
| 3271 | */ |
| 3272 | case '{': { |
| 3273 | char_u *start = skipwhite(*arg + 1); |
| 3274 | |
| 3275 | // Find out what comes after the arguments. |
| 3276 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3277 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3278 | if (ret != FAIL && *start == '>') |
| 3279 | ret = compile_lambda(arg, cctx); |
| 3280 | else |
| 3281 | ret = compile_dict(arg, cctx, FALSE); |
| 3282 | } |
| 3283 | break; |
| 3284 | |
| 3285 | /* |
| 3286 | * Option value: &name |
| 3287 | */ |
| 3288 | case '&': ret = compile_get_option(arg, cctx); |
| 3289 | break; |
| 3290 | |
| 3291 | /* |
| 3292 | * Environment variable: $VAR. |
| 3293 | */ |
| 3294 | case '$': ret = compile_get_env(arg, cctx); |
| 3295 | break; |
| 3296 | |
| 3297 | /* |
| 3298 | * Register contents: @r. |
| 3299 | */ |
| 3300 | case '@': ret = compile_get_register(arg, cctx); |
| 3301 | break; |
| 3302 | /* |
| 3303 | * nested expression: (expression). |
| 3304 | */ |
| 3305 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3306 | |
| 3307 | // recursive! |
| 3308 | if (ppconst->pp_used <= PPSIZE - 10) |
| 3309 | { |
| 3310 | ret = compile_expr1(arg, cctx, ppconst); |
| 3311 | } |
| 3312 | else |
| 3313 | { |
| 3314 | // Not enough space in ppconst, flush constants. |
| 3315 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3316 | return FAIL; |
| 3317 | ret = compile_expr0(arg, cctx); |
| 3318 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3319 | *arg = skipwhite(*arg); |
| 3320 | if (**arg == ')') |
| 3321 | ++*arg; |
| 3322 | else if (ret == OK) |
| 3323 | { |
| 3324 | emsg(_(e_missing_close)); |
| 3325 | ret = FAIL; |
| 3326 | } |
| 3327 | break; |
| 3328 | |
| 3329 | default: ret = NOTDONE; |
| 3330 | break; |
| 3331 | } |
| 3332 | if (ret == FAIL) |
| 3333 | return FAIL; |
| 3334 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3335 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3336 | { |
| 3337 | // apply the '!', '-' and '+' before the constant |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3338 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3339 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3340 | clear_tv(rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3341 | return FAIL; |
| 3342 | } |
| 3343 | start_leader = end_leader; // don't apply again below |
| 3344 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3345 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3346 | clear_tv(rettv); |
| 3347 | else |
| 3348 | // A constant expression can possibly be handled compile time, |
| 3349 | // return the value instead of generating code. |
| 3350 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3351 | } |
| 3352 | else if (ret == NOTDONE) |
| 3353 | { |
| 3354 | char_u *p; |
| 3355 | int r; |
| 3356 | |
| 3357 | if (!eval_isnamec1(**arg)) |
| 3358 | { |
| 3359 | semsg(_("E1015: Name expected: %s"), *arg); |
| 3360 | return FAIL; |
| 3361 | } |
| 3362 | |
| 3363 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3364 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3365 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3366 | { |
| 3367 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 3368 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3369 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3370 | { |
| 3371 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3372 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3373 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3374 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3375 | if (r == FAIL) |
| 3376 | return FAIL; |
| 3377 | } |
| 3378 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3379 | // Handle following "[]", ".member", etc. |
| 3380 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3381 | if (compile_subscript(arg, cctx, &start_leader, end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3382 | ppconst) == FAIL) |
| 3383 | return FAIL; |
| 3384 | if (ppconst->pp_used > 0) |
| 3385 | { |
| 3386 | // apply the '!', '-' and '+' before the constant |
| 3387 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3388 | if (apply_leader(rettv, start_leader, end_leader) == FAIL) |
| 3389 | return FAIL; |
| 3390 | return OK; |
| 3391 | } |
| 3392 | if (compile_leader(cctx, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3393 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3394 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | /* |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3398 | * Give the "white on both sides" error, taking the operator from "p[len]". |
| 3399 | */ |
| 3400 | void |
| 3401 | error_white_both(char_u *op, int len) |
| 3402 | { |
| 3403 | char_u buf[10]; |
| 3404 | |
| 3405 | vim_strncpy(buf, op, len); |
| 3406 | semsg(_(e_white_both), buf); |
| 3407 | } |
| 3408 | |
| 3409 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3410 | * * number multiplication |
| 3411 | * / number division |
| 3412 | * % number modulo |
| 3413 | */ |
| 3414 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3415 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3416 | { |
| 3417 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3418 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3419 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3420 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3421 | // get the first expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3422 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3423 | return FAIL; |
| 3424 | |
| 3425 | /* |
| 3426 | * Repeat computing, until no "*", "/" or "%" is following. |
| 3427 | */ |
| 3428 | for (;;) |
| 3429 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3430 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3431 | if (*op != '*' && *op != '/' && *op != '%') |
| 3432 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3433 | if (next != NULL) |
| 3434 | { |
| 3435 | *arg = next_line_from_context(cctx, TRUE); |
| 3436 | op = skipwhite(*arg); |
| 3437 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3438 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3439 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3440 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3441 | error_white_both(op, 1); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3442 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3443 | } |
| 3444 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3445 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3446 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3447 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3448 | // get the second expression |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3449 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3450 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3451 | |
| 3452 | if (ppconst->pp_used == ppconst_used + 2 |
| 3453 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3454 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3455 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3456 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3457 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3458 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3459 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3460 | // both are numbers: compute the result |
| 3461 | switch (*op) |
| 3462 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3463 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3464 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3465 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3466 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3467 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3468 | break; |
| 3469 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3470 | tv1->vval.v_number = res; |
| 3471 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3472 | } |
| 3473 | else |
| 3474 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3475 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3476 | generate_two_op(cctx, op); |
| 3477 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | return OK; |
| 3481 | } |
| 3482 | |
| 3483 | /* |
| 3484 | * + number addition |
| 3485 | * - number subtraction |
| 3486 | * .. string concatenation |
| 3487 | */ |
| 3488 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3489 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3490 | { |
| 3491 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3492 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3493 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3494 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3495 | |
| 3496 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3497 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3498 | return FAIL; |
| 3499 | |
| 3500 | /* |
| 3501 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3502 | */ |
| 3503 | for (;;) |
| 3504 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3505 | op = may_peek_next_line(cctx, *arg, &next); |
| 3506 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3507 | break; |
| 3508 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3509 | if (next != NULL) |
| 3510 | { |
| 3511 | *arg = next_line_from_context(cctx, TRUE); |
| 3512 | op = skipwhite(*arg); |
| 3513 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3514 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3515 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3516 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3517 | error_white_both(op, oplen); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3518 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3522 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3523 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3524 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3525 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3526 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3527 | return FAIL; |
| 3528 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3529 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3530 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3531 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 3532 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 3533 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3534 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3535 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3536 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3537 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3538 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3539 | // concat/subtract/add constant numbers |
| 3540 | if (*op == '+') |
| 3541 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 3542 | else if (*op == '-') |
| 3543 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 3544 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3545 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3546 | // concatenate constant strings |
| 3547 | char_u *s1 = tv1->vval.v_string; |
| 3548 | char_u *s2 = tv2->vval.v_string; |
| 3549 | size_t len1 = STRLEN(s1); |
| 3550 | |
| 3551 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 3552 | if (tv1->vval.v_string == NULL) |
| 3553 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3554 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3555 | return FAIL; |
| 3556 | } |
| 3557 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 3558 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3559 | vim_free(s1); |
| 3560 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3561 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3562 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3563 | } |
| 3564 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3565 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3566 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3567 | if (*op == '.') |
| 3568 | { |
| 3569 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 3570 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 3571 | return FAIL; |
| 3572 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 3573 | } |
| 3574 | else |
| 3575 | generate_two_op(cctx, op); |
| 3576 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | return OK; |
| 3580 | } |
| 3581 | |
| 3582 | /* |
| 3583 | * expr5a == expr5b |
| 3584 | * expr5a =~ expr5b |
| 3585 | * expr5a != expr5b |
| 3586 | * expr5a !~ expr5b |
| 3587 | * expr5a > expr5b |
| 3588 | * expr5a >= expr5b |
| 3589 | * expr5a < expr5b |
| 3590 | * expr5a <= expr5b |
| 3591 | * expr5a is expr5b |
| 3592 | * expr5a isnot expr5b |
| 3593 | * |
| 3594 | * Produces instructions: |
| 3595 | * EVAL expr5a Push result of "expr5a" |
| 3596 | * EVAL expr5b Push result of "expr5b" |
| 3597 | * COMPARE one of the compare instructions |
| 3598 | */ |
| 3599 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3600 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3601 | { |
| 3602 | exptype_T type = EXPR_UNKNOWN; |
| 3603 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3604 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3605 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3606 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3607 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3608 | |
| 3609 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3610 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3611 | return FAIL; |
| 3612 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3613 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3614 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3615 | |
| 3616 | /* |
| 3617 | * If there is a comparative operator, use it. |
| 3618 | */ |
| 3619 | if (type != EXPR_UNKNOWN) |
| 3620 | { |
| 3621 | int ic = FALSE; // Default: do not ignore case |
| 3622 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3623 | if (next != NULL) |
| 3624 | { |
| 3625 | *arg = next_line_from_context(cctx, TRUE); |
| 3626 | p = skipwhite(*arg); |
| 3627 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3628 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 3629 | { |
| 3630 | semsg(_(e_invexpr2), *arg); |
| 3631 | return FAIL; |
| 3632 | } |
| 3633 | // extra question mark appended: ignore case |
| 3634 | if (p[len] == '?') |
| 3635 | { |
| 3636 | ic = TRUE; |
| 3637 | ++len; |
| 3638 | } |
| 3639 | // extra '#' appended: match case (ignored) |
| 3640 | else if (p[len] == '#') |
| 3641 | ++len; |
| 3642 | // nothing appended: match case |
| 3643 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3644 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3645 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3646 | error_white_both(p, len); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3647 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3648 | } |
| 3649 | |
| 3650 | // get the second variable |
| 3651 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3652 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3653 | return FAIL; |
| 3654 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3655 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3656 | return FAIL; |
| 3657 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3658 | if (ppconst->pp_used == ppconst_used + 2) |
| 3659 | { |
| 3660 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 3661 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3662 | int ret; |
| 3663 | |
| 3664 | // Both sides are a constant, compute the result now. |
| 3665 | // First check for a valid combination of types, this is more |
| 3666 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 3667 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3668 | ret = FAIL; |
| 3669 | else |
| 3670 | { |
| 3671 | ret = typval_compare(tv1, tv2, type, ic); |
| 3672 | tv1->v_type = VAR_BOOL; |
| 3673 | tv1->vval.v_number = tv1->vval.v_number |
| 3674 | ? VVAL_TRUE : VVAL_FALSE; |
| 3675 | clear_tv(tv2); |
| 3676 | --ppconst->pp_used; |
| 3677 | } |
| 3678 | return ret; |
| 3679 | } |
| 3680 | |
| 3681 | generate_ppconst(cctx, ppconst); |
| 3682 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | return OK; |
| 3686 | } |
| 3687 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 3688 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 3689 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3690 | /* |
| 3691 | * Compile || or &&. |
| 3692 | */ |
| 3693 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3694 | compile_and_or( |
| 3695 | char_u **arg, |
| 3696 | cctx_T *cctx, |
| 3697 | char *op, |
| 3698 | ppconst_T *ppconst, |
| 3699 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3700 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3701 | char_u *next; |
| 3702 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3703 | int opchar = *op; |
| 3704 | |
| 3705 | if (p[0] == opchar && p[1] == opchar) |
| 3706 | { |
| 3707 | garray_T *instr = &cctx->ctx_instr; |
| 3708 | garray_T end_ga; |
| 3709 | |
| 3710 | /* |
| 3711 | * Repeat until there is no following "||" or "&&" |
| 3712 | */ |
| 3713 | ga_init2(&end_ga, sizeof(int), 10); |
| 3714 | while (p[0] == opchar && p[1] == opchar) |
| 3715 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3716 | if (next != NULL) |
| 3717 | { |
| 3718 | *arg = next_line_from_context(cctx, TRUE); |
| 3719 | p = skipwhite(*arg); |
| 3720 | } |
| 3721 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3722 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 3723 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3724 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3725 | return FAIL; |
| 3726 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3727 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3728 | // TODO: use ppconst if the value is a constant |
| 3729 | generate_ppconst(cctx, ppconst); |
| 3730 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3731 | if (ga_grow(&end_ga, 1) == FAIL) |
| 3732 | { |
| 3733 | ga_clear(&end_ga); |
| 3734 | return FAIL; |
| 3735 | } |
| 3736 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 3737 | ++end_ga.ga_len; |
| 3738 | generate_JUMP(cctx, opchar == '|' |
| 3739 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 3740 | |
| 3741 | // eval the next expression |
| 3742 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3743 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3744 | return FAIL; |
| 3745 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3746 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 3747 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3748 | { |
| 3749 | ga_clear(&end_ga); |
| 3750 | return FAIL; |
| 3751 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3752 | |
| 3753 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3754 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3755 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3756 | |
| 3757 | // Fill in the end label in all jumps. |
| 3758 | while (end_ga.ga_len > 0) |
| 3759 | { |
| 3760 | isn_T *isn; |
| 3761 | |
| 3762 | --end_ga.ga_len; |
| 3763 | isn = ((isn_T *)instr->ga_data) |
| 3764 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 3765 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3766 | } |
| 3767 | ga_clear(&end_ga); |
| 3768 | } |
| 3769 | |
| 3770 | return OK; |
| 3771 | } |
| 3772 | |
| 3773 | /* |
| 3774 | * expr4a && expr4a && expr4a logical AND |
| 3775 | * |
| 3776 | * Produces instructions: |
| 3777 | * EVAL expr4a Push result of "expr4a" |
| 3778 | * JUMP_AND_KEEP_IF_FALSE end |
| 3779 | * EVAL expr4b Push result of "expr4b" |
| 3780 | * JUMP_AND_KEEP_IF_FALSE end |
| 3781 | * EVAL expr4c Push result of "expr4c" |
| 3782 | * end: |
| 3783 | */ |
| 3784 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3785 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3786 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3787 | int ppconst_used = ppconst->pp_used; |
| 3788 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3789 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3790 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3791 | return FAIL; |
| 3792 | |
| 3793 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3794 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3795 | } |
| 3796 | |
| 3797 | /* |
| 3798 | * expr3a || expr3b || expr3c logical OR |
| 3799 | * |
| 3800 | * Produces instructions: |
| 3801 | * EVAL expr3a Push result of "expr3a" |
| 3802 | * JUMP_AND_KEEP_IF_TRUE end |
| 3803 | * EVAL expr3b Push result of "expr3b" |
| 3804 | * JUMP_AND_KEEP_IF_TRUE end |
| 3805 | * EVAL expr3c Push result of "expr3c" |
| 3806 | * end: |
| 3807 | */ |
| 3808 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3809 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3810 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3811 | int ppconst_used = ppconst->pp_used; |
| 3812 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3813 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3814 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3815 | return FAIL; |
| 3816 | |
| 3817 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3818 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | /* |
| 3822 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 3823 | * |
| 3824 | * Produces instructions: |
| 3825 | * EVAL expr2 Push result of "expr" |
| 3826 | * JUMP_IF_FALSE alt jump if false |
| 3827 | * EVAL expr1a |
| 3828 | * JUMP_ALWAYS end |
| 3829 | * alt: EVAL expr1b |
| 3830 | * end: |
| 3831 | */ |
| 3832 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3833 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3834 | { |
| 3835 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3836 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3837 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3838 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3839 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3840 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3841 | return FAIL; |
| 3842 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3843 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3844 | if (*p == '?') |
| 3845 | { |
| 3846 | garray_T *instr = &cctx->ctx_instr; |
| 3847 | garray_T *stack = &cctx->ctx_type_stack; |
| 3848 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 3849 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3850 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 3851 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3852 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3853 | int has_const_expr = FALSE; |
| 3854 | int const_value = FALSE; |
| 3855 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3856 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3857 | if (next != NULL) |
| 3858 | { |
| 3859 | *arg = next_line_from_context(cctx, TRUE); |
| 3860 | p = skipwhite(*arg); |
| 3861 | } |
| 3862 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3863 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 3864 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3865 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3866 | return FAIL; |
| 3867 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3868 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3869 | if (ppconst->pp_used == ppconst_used + 1) |
| 3870 | { |
| 3871 | // the condition is a constant, we know whether the ? or the : |
| 3872 | // expression is to be evaluated. |
| 3873 | has_const_expr = TRUE; |
| 3874 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 3875 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 3876 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3877 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 3878 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3879 | } |
| 3880 | else |
| 3881 | { |
| 3882 | generate_ppconst(cctx, ppconst); |
| 3883 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 3884 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3885 | |
| 3886 | // evaluate the second expression; any type is accepted |
| 3887 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3888 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3889 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3890 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 3891 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3892 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3893 | if (!has_const_expr) |
| 3894 | { |
| 3895 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3896 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3897 | // remember the type and drop it |
| 3898 | --stack->ga_len; |
| 3899 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3900 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3901 | end_idx = instr->ga_len; |
| 3902 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 3903 | |
| 3904 | // jump here from JUMP_IF_FALSE |
| 3905 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 3906 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3907 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3908 | |
| 3909 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3910 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3911 | if (*p != ':') |
| 3912 | { |
| 3913 | emsg(_(e_missing_colon)); |
| 3914 | return FAIL; |
| 3915 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3916 | if (next != NULL) |
| 3917 | { |
| 3918 | *arg = next_line_from_context(cctx, TRUE); |
| 3919 | p = skipwhite(*arg); |
| 3920 | } |
| 3921 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3922 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 3923 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3924 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3925 | return FAIL; |
| 3926 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3927 | |
| 3928 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3929 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3930 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 3931 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3932 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3933 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3934 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3935 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 3936 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3937 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3938 | if (!has_const_expr) |
| 3939 | { |
| 3940 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3941 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3942 | // If the types differ, the result has a more generic type. |
| 3943 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3944 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 3945 | |
| 3946 | // jump here from JUMP_ALWAYS |
| 3947 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 3948 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3949 | } |
| 3950 | |
| 3951 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3952 | } |
| 3953 | return OK; |
| 3954 | } |
| 3955 | |
| 3956 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3957 | * Toplevel expression. |
| 3958 | */ |
| 3959 | static int |
| 3960 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 3961 | { |
| 3962 | ppconst_T ppconst; |
| 3963 | |
| 3964 | CLEAR_FIELD(ppconst); |
| 3965 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 3966 | { |
| 3967 | clear_ppconst(&ppconst); |
| 3968 | return FAIL; |
| 3969 | } |
| 3970 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 3971 | return FAIL; |
| 3972 | return OK; |
| 3973 | } |
| 3974 | |
| 3975 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3976 | * compile "return [expr]" |
| 3977 | */ |
| 3978 | static char_u * |
| 3979 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 3980 | { |
| 3981 | char_u *p = arg; |
| 3982 | garray_T *stack = &cctx->ctx_type_stack; |
| 3983 | type_T *stack_type; |
| 3984 | |
| 3985 | if (*p != NUL && *p != '|' && *p != '\n') |
| 3986 | { |
| 3987 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3988 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3989 | return NULL; |
| 3990 | |
| 3991 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3992 | if (set_return_type) |
| 3993 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 3994 | else |
| 3995 | { |
| 3996 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 3997 | && stack_type->tt_type != VAR_VOID |
| 3998 | && stack_type->tt_type != VAR_UNKNOWN) |
| 3999 | { |
| 4000 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4001 | return NULL; |
| 4002 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4003 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4004 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4005 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4006 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4007 | } |
| 4008 | else |
| 4009 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4010 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4011 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4012 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4013 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4014 | { |
| 4015 | emsg(_("E1003: Missing return value")); |
| 4016 | return NULL; |
| 4017 | } |
| 4018 | |
| 4019 | // No argument, return zero. |
| 4020 | generate_PUSHNR(cctx, 0); |
| 4021 | } |
| 4022 | |
| 4023 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4024 | return NULL; |
| 4025 | |
| 4026 | // "return val | endif" is possible |
| 4027 | return skipwhite(p); |
| 4028 | } |
| 4029 | |
| 4030 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4031 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4032 | * Return a pointer to the line in allocated memory. |
| 4033 | * Return NULL for end-of-file or some error. |
| 4034 | */ |
| 4035 | static char_u * |
| 4036 | exarg_getline( |
| 4037 | int c UNUSED, |
| 4038 | void *cookie, |
| 4039 | int indent UNUSED, |
| 4040 | int do_concat UNUSED) |
| 4041 | { |
| 4042 | cctx_T *cctx = (cctx_T *)cookie; |
| 4043 | |
| 4044 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4045 | { |
| 4046 | iemsg("Heredoc got to end"); |
| 4047 | return NULL; |
| 4048 | } |
| 4049 | ++cctx->ctx_lnum; |
| 4050 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4051 | [cctx->ctx_lnum]); |
| 4052 | } |
| 4053 | |
| 4054 | /* |
| 4055 | * Compile a nested :def command. |
| 4056 | */ |
| 4057 | static char_u * |
| 4058 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4059 | { |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4060 | int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4061 | char_u *name_start = eap->arg; |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4062 | char_u *name_end = to_name_end(eap->arg, TRUE); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4063 | char_u *lambda_name; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4064 | lvar_T *lvar; |
| 4065 | ufunc_T *ufunc; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4066 | int r; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4067 | |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4068 | // Only g:Func() can use a namespace. |
| 4069 | if (name_start[1] == ':' && !is_global) |
| 4070 | { |
| 4071 | semsg(_(e_namespace), name_start); |
| 4072 | return NULL; |
| 4073 | } |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4074 | if (check_defined(name_start, name_end - name_start, cctx) == FAIL) |
| 4075 | return NULL; |
| 4076 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4077 | eap->arg = name_end; |
| 4078 | eap->getline = exarg_getline; |
| 4079 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4080 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4081 | eap->forceit = FALSE; |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4082 | lambda_name = get_lambda_name(); |
| 4083 | ufunc = def_function(eap, lambda_name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4084 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4085 | if (ufunc == NULL) |
| 4086 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4087 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4088 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4089 | return NULL; |
| 4090 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4091 | if (is_global) |
| 4092 | { |
| 4093 | char_u *func_name = vim_strnsave(name_start + 2, |
| 4094 | name_end - name_start - 2); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4095 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4096 | if (func_name == NULL) |
| 4097 | r = FAIL; |
| 4098 | else |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4099 | r = generate_NEWFUNC(cctx, lambda_name, func_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4100 | } |
| 4101 | else |
| 4102 | { |
| 4103 | // Define a local variable for the function reference. |
| 4104 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
| 4105 | TRUE, ufunc->uf_func_type); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4106 | if (lvar == NULL) |
| 4107 | return NULL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 4108 | if (generate_FUNCREF(cctx, ufunc) == FAIL) |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4109 | return NULL; |
| 4110 | r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 4111 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4112 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4113 | // TODO: warning for trailing text? |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4114 | return r == FAIL ? NULL : (char_u *)""; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4118 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4119 | */ |
| 4120 | int |
| 4121 | assignment_len(char_u *p, int *heredoc) |
| 4122 | { |
| 4123 | if (*p == '=') |
| 4124 | { |
| 4125 | if (p[1] == '<' && p[2] == '<') |
| 4126 | { |
| 4127 | *heredoc = TRUE; |
| 4128 | return 3; |
| 4129 | } |
| 4130 | return 1; |
| 4131 | } |
| 4132 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4133 | return 2; |
| 4134 | if (STRNCMP(p, "..=", 3) == 0) |
| 4135 | return 3; |
| 4136 | return 0; |
| 4137 | } |
| 4138 | |
| 4139 | // words that cannot be used as a variable |
| 4140 | static char *reserved[] = { |
| 4141 | "true", |
| 4142 | "false", |
| 4143 | NULL |
| 4144 | }; |
| 4145 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4146 | typedef enum { |
| 4147 | dest_local, |
| 4148 | dest_option, |
| 4149 | dest_env, |
| 4150 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4151 | dest_buffer, |
| 4152 | dest_window, |
| 4153 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4154 | dest_vimvar, |
| 4155 | dest_script, |
| 4156 | dest_reg, |
| 4157 | } assign_dest_T; |
| 4158 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4159 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4160 | * Generate the load instruction for "name". |
| 4161 | */ |
| 4162 | static void |
| 4163 | generate_loadvar( |
| 4164 | cctx_T *cctx, |
| 4165 | assign_dest_T dest, |
| 4166 | char_u *name, |
| 4167 | lvar_T *lvar, |
| 4168 | type_T *type) |
| 4169 | { |
| 4170 | switch (dest) |
| 4171 | { |
| 4172 | case dest_option: |
| 4173 | // TODO: check the option exists |
| 4174 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4175 | break; |
| 4176 | case dest_global: |
| 4177 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4178 | break; |
| 4179 | case dest_buffer: |
| 4180 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4181 | break; |
| 4182 | case dest_window: |
| 4183 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4184 | break; |
| 4185 | case dest_tab: |
| 4186 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4187 | break; |
| 4188 | case dest_script: |
| 4189 | compile_load_scriptvar(cctx, |
| 4190 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4191 | break; |
| 4192 | case dest_env: |
| 4193 | // Include $ in the name here |
| 4194 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4195 | break; |
| 4196 | case dest_reg: |
| 4197 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4198 | break; |
| 4199 | case dest_vimvar: |
| 4200 | generate_LOADV(cctx, name + 2, TRUE); |
| 4201 | break; |
| 4202 | case dest_local: |
| 4203 | if (lvar->lv_from_outer) |
| 4204 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4205 | NULL, type); |
| 4206 | else |
| 4207 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4208 | break; |
| 4209 | } |
| 4210 | } |
| 4211 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4212 | void |
| 4213 | vim9_declare_error(char_u *name) |
| 4214 | { |
| 4215 | char *scope = ""; |
| 4216 | |
| 4217 | switch (*name) |
| 4218 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4219 | case 'g': scope = _("global"); break; |
| 4220 | case 'b': scope = _("buffer"); break; |
| 4221 | case 'w': scope = _("window"); break; |
| 4222 | case 't': scope = _("tab"); break; |
| 4223 | case 'v': scope = "v:"; break; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4224 | case '$': semsg(_(e_declare_env_var), name); |
| 4225 | return; |
| 4226 | case '&': semsg(_("E1052: Cannot declare an option: %s"), name); |
| 4227 | return; |
| 4228 | case '@': semsg(_("E1066: Cannot declare a register: %s"), name); |
| 4229 | return; |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4230 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4231 | } |
| 4232 | semsg(_(e_declare_var), scope, name); |
| 4233 | } |
| 4234 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4235 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4236 | * Compile declaration and assignment: |
| 4237 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4238 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4239 | * Return NULL for an error. |
| 4240 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4241 | */ |
| 4242 | static char_u * |
| 4243 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4244 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4245 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4246 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4247 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4248 | char_u *ret = NULL; |
| 4249 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4250 | int var_idx; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4251 | int scriptvar_sid = 0; |
| 4252 | int scriptvar_idx = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4253 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4254 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4255 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4256 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4257 | int oplen = 0; |
| 4258 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4259 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4260 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4261 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4262 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4263 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4264 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4265 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 4266 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 4267 | if (p == NULL) |
| 4268 | return *arg == '[' ? arg : NULL; |
| 4269 | |
| 4270 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4271 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 4272 | // TODO: should we allow this, and figure out type inference from list |
| 4273 | // members? |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4274 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4275 | return NULL; |
| 4276 | } |
| 4277 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4278 | sp = p; |
| 4279 | p = skipwhite(p); |
| 4280 | op = p; |
| 4281 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4282 | |
| 4283 | if (var_count > 0 && oplen == 0) |
| 4284 | // can be something like "[1, 2]->func()" |
| 4285 | return arg; |
| 4286 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4287 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4288 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4289 | error_white_both(op, oplen); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4290 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4291 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4292 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4293 | if (heredoc) |
| 4294 | { |
| 4295 | list_T *l; |
| 4296 | listitem_T *li; |
| 4297 | |
| 4298 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4299 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4300 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 4301 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4302 | |
| 4303 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 4304 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4305 | { |
| 4306 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4307 | li->li_tv.vval.v_string = NULL; |
| 4308 | } |
| 4309 | generate_NEWLIST(cctx, l->lv_len); |
| 4310 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4311 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4312 | list_free(l); |
| 4313 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4314 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4315 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4316 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4317 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4318 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 4319 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4320 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4321 | p = skipwhite(op + oplen); |
| 4322 | if (compile_expr0(&p, cctx) == FAIL) |
| 4323 | return NULL; |
| 4324 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4325 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4326 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4327 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4328 | type_T *stacktype; |
| 4329 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4330 | stacktype = stack->ga_len == 0 ? &t_void |
| 4331 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4332 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4333 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4334 | emsg(_(e_cannot_use_void)); |
| 4335 | goto theend; |
| 4336 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4337 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4338 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 4339 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4340 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 4341 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4342 | } |
| 4343 | } |
| 4344 | |
| 4345 | /* |
| 4346 | * Loop over variables in "[var, var] = expr". |
| 4347 | * For "var = expr" and "let var: type" this is done only once. |
| 4348 | */ |
| 4349 | if (var_count > 0) |
| 4350 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 4351 | else |
| 4352 | var_start = arg; |
| 4353 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 4354 | { |
| 4355 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 4356 | size_t varlen; |
| 4357 | int new_local = FALSE; |
| 4358 | int opt_type; |
| 4359 | int opt_flags = 0; |
| 4360 | assign_dest_T dest = dest_local; |
| 4361 | int vimvaridx = -1; |
| 4362 | lvar_T *lvar = NULL; |
| 4363 | lvar_T arg_lvar; |
| 4364 | int has_type = FALSE; |
| 4365 | int has_index = FALSE; |
| 4366 | int instr_count = -1; |
| 4367 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4368 | if (*var_start == '@') |
| 4369 | p = var_start + 2; |
| 4370 | else |
| 4371 | { |
| 4372 | p = (*var_start == '&' || *var_start == '$') |
| 4373 | ? var_start + 1 : var_start; |
| 4374 | p = to_name_end(p, TRUE); |
| 4375 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4376 | |
| 4377 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 4378 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 4379 | --var_end; |
| 4380 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 4381 | --p; |
| 4382 | |
| 4383 | varlen = p - var_start; |
| 4384 | vim_free(name); |
| 4385 | name = vim_strnsave(var_start, varlen); |
| 4386 | if (name == NULL) |
| 4387 | return NULL; |
| 4388 | if (!heredoc) |
| 4389 | type = &t_any; |
| 4390 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4391 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4392 | { |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4393 | int declare_error = FALSE; |
| 4394 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4395 | if (*var_start == '&') |
| 4396 | { |
| 4397 | int cc; |
| 4398 | long numval; |
| 4399 | |
| 4400 | dest = dest_option; |
| 4401 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4402 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4403 | emsg(_(e_const_option)); |
| 4404 | goto theend; |
| 4405 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4406 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4407 | p = var_start; |
| 4408 | p = find_option_end(&p, &opt_flags); |
| 4409 | if (p == NULL) |
| 4410 | { |
| 4411 | // cannot happen? |
| 4412 | emsg(_(e_letunexp)); |
| 4413 | goto theend; |
| 4414 | } |
| 4415 | cc = *p; |
| 4416 | *p = NUL; |
| 4417 | opt_type = get_option_value(var_start + 1, &numval, |
| 4418 | NULL, opt_flags); |
| 4419 | *p = cc; |
| 4420 | if (opt_type == -3) |
| 4421 | { |
| 4422 | semsg(_(e_unknown_option), var_start); |
| 4423 | goto theend; |
| 4424 | } |
| 4425 | if (opt_type == -2 || opt_type == 0) |
| 4426 | type = &t_string; |
| 4427 | else |
| 4428 | type = &t_number; // both number and boolean option |
| 4429 | } |
| 4430 | else if (*var_start == '$') |
| 4431 | { |
| 4432 | dest = dest_env; |
| 4433 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4434 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4435 | } |
| 4436 | else if (*var_start == '@') |
| 4437 | { |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4438 | if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.') |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4439 | { |
| 4440 | emsg_invreg(var_start[1]); |
| 4441 | goto theend; |
| 4442 | } |
| 4443 | dest = dest_reg; |
| 4444 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4445 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4446 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4447 | else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4448 | { |
| 4449 | dest = dest_global; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4450 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4451 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4452 | else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4453 | { |
| 4454 | dest = dest_buffer; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4455 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4456 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4457 | else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4458 | { |
| 4459 | dest = dest_window; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4460 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4461 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4462 | else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4463 | { |
| 4464 | dest = dest_tab; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4465 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4466 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4467 | else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4468 | { |
| 4469 | typval_T *vtv; |
| 4470 | int di_flags; |
| 4471 | |
| 4472 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 4473 | if (vimvaridx < 0) |
| 4474 | { |
| 4475 | semsg(_(e_var_notfound), var_start); |
| 4476 | goto theend; |
| 4477 | } |
| 4478 | // We use the current value of "sandbox" here, is that OK? |
| 4479 | if (var_check_ro(di_flags, name, FALSE)) |
| 4480 | goto theend; |
| 4481 | dest = dest_vimvar; |
| 4482 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 4483 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4484 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4485 | } |
| 4486 | else |
| 4487 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4488 | int idx; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4489 | |
| 4490 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 4491 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4492 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4493 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4494 | goto theend; |
| 4495 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4496 | |
| 4497 | lvar = lookup_local(var_start, varlen, cctx); |
| 4498 | if (lvar == NULL) |
| 4499 | { |
| 4500 | CLEAR_FIELD(arg_lvar); |
| 4501 | if (lookup_arg(var_start, varlen, |
| 4502 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 4503 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4504 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4505 | if (is_decl) |
| 4506 | { |
| 4507 | semsg(_(e_used_as_arg), name); |
| 4508 | goto theend; |
| 4509 | } |
| 4510 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4511 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4512 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4513 | if (lvar != NULL) |
| 4514 | { |
| 4515 | if (is_decl) |
| 4516 | { |
| 4517 | semsg(_("E1017: Variable already declared: %s"), name); |
| 4518 | goto theend; |
| 4519 | } |
| 4520 | else if (lvar->lv_const) |
| 4521 | { |
| 4522 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 4523 | name); |
| 4524 | goto theend; |
| 4525 | } |
| 4526 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4527 | else |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4528 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4529 | int script_namespace = varlen > 1 |
| 4530 | && STRNCMP(var_start, "s:", 2) == 0; |
| 4531 | int script_var = (script_namespace |
| 4532 | ? lookup_script(var_start + 2, varlen - 2) |
| 4533 | : lookup_script(var_start, varlen)) == OK; |
| 4534 | imported_T *import = |
| 4535 | find_imported(var_start, varlen, cctx); |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4536 | |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4537 | if (script_namespace || script_var || import != NULL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4538 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4539 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 4540 | |
| 4541 | if (is_decl) |
| 4542 | { |
| 4543 | if (script_namespace) |
| 4544 | semsg(_("E1101: Cannot declare a script variable in a function: %s"), |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4545 | name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4546 | else |
| 4547 | semsg(_("E1054: Variable already declared in the script: %s"), |
| 4548 | name); |
| 4549 | goto theend; |
| 4550 | } |
| 4551 | else if (cctx->ctx_ufunc->uf_script_ctx_version |
| 4552 | == SCRIPT_VERSION_VIM9 |
| 4553 | && script_namespace |
| 4554 | && !script_var && import == NULL) |
| 4555 | { |
| 4556 | semsg(_(e_unknown_var), name); |
| 4557 | goto theend; |
| 4558 | } |
| 4559 | |
| 4560 | dest = dest_script; |
| 4561 | |
| 4562 | // existing script-local variables should have a type |
| 4563 | scriptvar_sid = current_sctx.sc_sid; |
| 4564 | if (import != NULL) |
| 4565 | scriptvar_sid = import->imp_sid; |
| 4566 | scriptvar_idx = get_script_item_idx(scriptvar_sid, |
| 4567 | rawname, TRUE); |
| 4568 | if (scriptvar_idx >= 0) |
| 4569 | { |
| 4570 | scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid); |
| 4571 | svar_T *sv = |
| 4572 | ((svar_T *)si->sn_var_vals.ga_data) |
| 4573 | + scriptvar_idx; |
| 4574 | type = sv->sv_type; |
| 4575 | } |
| 4576 | } |
| 4577 | else if (name[1] == ':' && name[2] != NUL) |
| 4578 | { |
| 4579 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4580 | name); |
| 4581 | goto theend; |
| 4582 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4583 | else if (!is_decl) |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4584 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4585 | semsg(_(e_unknown_var), name); |
| 4586 | goto theend; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4587 | } |
Bram Moolenaar | fa211f3 | 2020-08-07 22:00:26 +0200 | [diff] [blame] | 4588 | else if (check_defined(var_start, varlen, cctx) == FAIL) |
| 4589 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4590 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4591 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4592 | |
| 4593 | if (declare_error) |
| 4594 | { |
| 4595 | vim9_declare_error(name); |
| 4596 | goto theend; |
| 4597 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4598 | } |
| 4599 | |
| 4600 | // handle "a:name" as a name, not index "name" on "a" |
| 4601 | if (varlen > 1 || var_start[varlen] != ':') |
| 4602 | p = var_end; |
| 4603 | |
| 4604 | if (dest != dest_option) |
| 4605 | { |
| 4606 | if (is_decl && *p == ':') |
| 4607 | { |
| 4608 | // parse optional type: "let var: type = expr" |
| 4609 | if (!VIM_ISWHITE(p[1])) |
| 4610 | { |
| 4611 | semsg(_(e_white_after), ":"); |
| 4612 | goto theend; |
| 4613 | } |
| 4614 | p = skipwhite(p + 1); |
| 4615 | type = parse_type(&p, cctx->ctx_type_list); |
| 4616 | has_type = TRUE; |
| 4617 | } |
| 4618 | else if (lvar != NULL) |
| 4619 | type = lvar->lv_type; |
| 4620 | } |
| 4621 | |
| 4622 | if (oplen == 3 && !heredoc && dest != dest_global |
| 4623 | && type->tt_type != VAR_STRING |
| 4624 | && type->tt_type != VAR_ANY) |
| 4625 | { |
| 4626 | emsg(_("E1019: Can only concatenate to string")); |
| 4627 | goto theend; |
| 4628 | } |
| 4629 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4630 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4631 | { |
| 4632 | if (oplen > 1 && !heredoc) |
| 4633 | { |
| 4634 | // +=, /=, etc. require an existing variable |
| 4635 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 4636 | name); |
| 4637 | goto theend; |
| 4638 | } |
| 4639 | |
| 4640 | // new local variable |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 4641 | if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
| 4642 | && var_wrong_func_name(name, TRUE)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4643 | goto theend; |
| 4644 | lvar = reserve_local(cctx, var_start, varlen, |
| 4645 | cmdidx == CMD_const, type); |
| 4646 | if (lvar == NULL) |
| 4647 | goto theend; |
| 4648 | new_local = TRUE; |
| 4649 | } |
| 4650 | |
| 4651 | member_type = type; |
| 4652 | if (var_end > var_start + varlen) |
| 4653 | { |
| 4654 | // Something follows after the variable: "var[idx]". |
| 4655 | if (is_decl) |
| 4656 | { |
| 4657 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 4658 | goto theend; |
| 4659 | } |
| 4660 | |
| 4661 | if (var_start[varlen] == '[') |
| 4662 | { |
| 4663 | has_index = TRUE; |
| 4664 | if (type->tt_member == NULL) |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 4665 | member_type = &t_any; |
| 4666 | else |
| 4667 | member_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4668 | } |
| 4669 | else |
| 4670 | { |
| 4671 | semsg("Not supported yet: %s", var_start); |
| 4672 | goto theend; |
| 4673 | } |
| 4674 | } |
| 4675 | else if (lvar == &arg_lvar) |
| 4676 | { |
| 4677 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 4678 | goto theend; |
| 4679 | } |
| 4680 | |
| 4681 | if (!heredoc) |
| 4682 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4683 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4684 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4685 | if (oplen > 0 && var_count == 0) |
| 4686 | { |
| 4687 | // skip over the "=" and the expression |
| 4688 | p = skipwhite(op + oplen); |
| 4689 | compile_expr0(&p, cctx); |
| 4690 | } |
| 4691 | } |
| 4692 | else if (oplen > 0) |
| 4693 | { |
| 4694 | type_T *stacktype; |
| 4695 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4696 | // For "var = expr" evaluate the expression. |
| 4697 | if (var_count == 0) |
| 4698 | { |
| 4699 | int r; |
| 4700 | |
| 4701 | // for "+=", "*=", "..=" etc. first load the current value |
| 4702 | if (*op != '=') |
| 4703 | { |
| 4704 | generate_loadvar(cctx, dest, name, lvar, type); |
| 4705 | |
| 4706 | if (has_index) |
| 4707 | { |
| 4708 | // TODO: get member from list or dict |
| 4709 | emsg("Index with operation not supported yet"); |
| 4710 | goto theend; |
| 4711 | } |
| 4712 | } |
| 4713 | |
| 4714 | // Compile the expression. Temporarily hide the new local |
| 4715 | // variable here, it is not available to this expression. |
| 4716 | if (new_local) |
| 4717 | --cctx->ctx_locals.ga_len; |
| 4718 | instr_count = instr->ga_len; |
| 4719 | p = skipwhite(op + oplen); |
| 4720 | r = compile_expr0(&p, cctx); |
| 4721 | if (new_local) |
| 4722 | ++cctx->ctx_locals.ga_len; |
| 4723 | if (r == FAIL) |
| 4724 | goto theend; |
| 4725 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4726 | else if (semicolon && var_idx == var_count - 1) |
| 4727 | { |
| 4728 | // For "[var; var] = expr" get the rest of the list |
| 4729 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 4730 | goto theend; |
| 4731 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4732 | else |
| 4733 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4734 | // For "[var, var] = expr" get the "var_idx" item from the |
| 4735 | // list. |
| 4736 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 4737 | return FAIL; |
| 4738 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4739 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4740 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 4741 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4742 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4743 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4744 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4745 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4746 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4747 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4748 | emsg(_(e_cannot_use_void)); |
| 4749 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4750 | } |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 4751 | else if ((stacktype->tt_type == VAR_FUNC |
| 4752 | || stacktype->tt_type == VAR_PARTIAL) |
| 4753 | && var_wrong_func_name(name, TRUE)) |
| 4754 | { |
| 4755 | goto theend; |
| 4756 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4757 | else |
| 4758 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4759 | // An empty list or dict has a &t_void member, |
| 4760 | // for a variable that implies &t_any. |
| 4761 | if (stacktype == &t_list_empty) |
| 4762 | lvar->lv_type = &t_list_any; |
| 4763 | else if (stacktype == &t_dict_empty) |
| 4764 | lvar->lv_type = &t_dict_any; |
| 4765 | else |
| 4766 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4767 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4768 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4769 | else |
| 4770 | { |
| 4771 | type_T *use_type = lvar->lv_type; |
| 4772 | |
| 4773 | if (has_index) |
| 4774 | { |
| 4775 | use_type = use_type->tt_member; |
| 4776 | if (use_type == NULL) |
| 4777 | use_type = &t_void; |
| 4778 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4779 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4780 | == FAIL) |
| 4781 | goto theend; |
| 4782 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4783 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4784 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4785 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4786 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4787 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4788 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4789 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4790 | emsg(_(e_const_req_value)); |
| 4791 | goto theend; |
| 4792 | } |
| 4793 | else if (!has_type || dest == dest_option) |
| 4794 | { |
| 4795 | emsg(_(e_type_req)); |
| 4796 | goto theend; |
| 4797 | } |
| 4798 | else |
| 4799 | { |
| 4800 | // variables are always initialized |
| 4801 | if (ga_grow(instr, 1) == FAIL) |
| 4802 | goto theend; |
| 4803 | switch (member_type->tt_type) |
| 4804 | { |
| 4805 | case VAR_BOOL: |
| 4806 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 4807 | break; |
| 4808 | case VAR_FLOAT: |
| 4809 | #ifdef FEAT_FLOAT |
| 4810 | generate_PUSHF(cctx, 0.0); |
| 4811 | #endif |
| 4812 | break; |
| 4813 | case VAR_STRING: |
| 4814 | generate_PUSHS(cctx, NULL); |
| 4815 | break; |
| 4816 | case VAR_BLOB: |
| 4817 | generate_PUSHBLOB(cctx, NULL); |
| 4818 | break; |
| 4819 | case VAR_FUNC: |
| 4820 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 4821 | break; |
| 4822 | case VAR_LIST: |
| 4823 | generate_NEWLIST(cctx, 0); |
| 4824 | break; |
| 4825 | case VAR_DICT: |
| 4826 | generate_NEWDICT(cctx, 0); |
| 4827 | break; |
| 4828 | case VAR_JOB: |
| 4829 | generate_PUSHJOB(cctx, NULL); |
| 4830 | break; |
| 4831 | case VAR_CHANNEL: |
| 4832 | generate_PUSHCHANNEL(cctx, NULL); |
| 4833 | break; |
| 4834 | case VAR_NUMBER: |
| 4835 | case VAR_UNKNOWN: |
| 4836 | case VAR_ANY: |
| 4837 | case VAR_PARTIAL: |
| 4838 | case VAR_VOID: |
| 4839 | case VAR_SPECIAL: // cannot happen |
| 4840 | generate_PUSHNR(cctx, 0); |
| 4841 | break; |
| 4842 | } |
| 4843 | } |
| 4844 | if (var_count == 0) |
| 4845 | end = p; |
| 4846 | } |
| 4847 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4848 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4849 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4850 | break; |
| 4851 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4852 | if (oplen > 0 && *op != '=') |
| 4853 | { |
| 4854 | type_T *expected = &t_number; |
| 4855 | type_T *stacktype; |
| 4856 | |
| 4857 | // TODO: if type is known use float or any operation |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 4858 | // TODO: check operator matches variable type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4859 | |
| 4860 | if (*op == '.') |
| 4861 | expected = &t_string; |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 4862 | else if (*op == '+') |
| 4863 | expected = member_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4864 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4865 | if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4866 | goto theend; |
| 4867 | |
| 4868 | if (*op == '.') |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 4869 | { |
| 4870 | if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL) |
| 4871 | goto theend; |
| 4872 | } |
| 4873 | else if (*op == '+') |
| 4874 | { |
| 4875 | if (generate_add_instr(cctx, |
| 4876 | operator_type(member_type, stacktype), |
| 4877 | member_type, stacktype) == FAIL) |
| 4878 | goto theend; |
| 4879 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4880 | else |
| 4881 | { |
| 4882 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 4883 | |
| 4884 | if (isn == NULL) |
| 4885 | goto theend; |
| 4886 | switch (*op) |
| 4887 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4888 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 4889 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 4890 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 4891 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 4892 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4893 | } |
| 4894 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4895 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4896 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4897 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4898 | int r; |
| 4899 | |
| 4900 | // Compile the "idx" in "var[idx]". |
| 4901 | if (new_local) |
| 4902 | --cctx->ctx_locals.ga_len; |
| 4903 | p = skipwhite(var_start + varlen + 1); |
| 4904 | r = compile_expr0(&p, cctx); |
| 4905 | if (new_local) |
| 4906 | ++cctx->ctx_locals.ga_len; |
| 4907 | if (r == FAIL) |
| 4908 | goto theend; |
| 4909 | if (*skipwhite(p) != ']') |
| 4910 | { |
| 4911 | emsg(_(e_missbrac)); |
| 4912 | goto theend; |
| 4913 | } |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 4914 | if (type == &t_any) |
| 4915 | { |
| 4916 | type_T *idx_type = ((type_T **)stack->ga_data)[ |
| 4917 | stack->ga_len - 1]; |
| 4918 | // Index on variable of unknown type: guess the type from the |
| 4919 | // index type: number is dict, otherwise dict. |
| 4920 | // TODO: should do the assignment at runtime |
| 4921 | if (idx_type->tt_type == VAR_NUMBER) |
| 4922 | type = &t_list_any; |
| 4923 | else |
| 4924 | type = &t_dict_any; |
| 4925 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4926 | if (type->tt_type == VAR_DICT |
| 4927 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 4928 | goto theend; |
| 4929 | if (type->tt_type == VAR_LIST |
| 4930 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4931 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4932 | { |
| 4933 | emsg(_(e_number_exp)); |
| 4934 | goto theend; |
| 4935 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4936 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4937 | // Load the dict or list. On the stack we then have: |
| 4938 | // - value |
| 4939 | // - index |
| 4940 | // - variable |
| 4941 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4942 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4943 | if (type->tt_type == VAR_LIST) |
| 4944 | { |
| 4945 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 4946 | return FAIL; |
| 4947 | } |
| 4948 | else if (type->tt_type == VAR_DICT) |
| 4949 | { |
| 4950 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 4951 | return FAIL; |
| 4952 | } |
| 4953 | else |
| 4954 | { |
| 4955 | emsg(_(e_listreq)); |
| 4956 | goto theend; |
| 4957 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4958 | } |
| 4959 | else |
| 4960 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4961 | switch (dest) |
| 4962 | { |
| 4963 | case dest_option: |
| 4964 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 4965 | break; |
| 4966 | case dest_global: |
| 4967 | // include g: with the name, easier to execute that way |
| 4968 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 4969 | break; |
| 4970 | case dest_buffer: |
| 4971 | // include b: with the name, easier to execute that way |
| 4972 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 4973 | break; |
| 4974 | case dest_window: |
| 4975 | // include w: with the name, easier to execute that way |
| 4976 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 4977 | break; |
| 4978 | case dest_tab: |
| 4979 | // include t: with the name, easier to execute that way |
| 4980 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 4981 | break; |
| 4982 | case dest_env: |
| 4983 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 4984 | break; |
| 4985 | case dest_reg: |
| 4986 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 4987 | break; |
| 4988 | case dest_vimvar: |
| 4989 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 4990 | break; |
| 4991 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4992 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4993 | if (scriptvar_idx < 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4994 | { |
| 4995 | char_u *name_s = name; |
| 4996 | |
| 4997 | // Include s: in the name for store_var() |
| 4998 | if (name[1] != ':') |
| 4999 | { |
| 5000 | int len = (int)STRLEN(name) + 3; |
| 5001 | |
| 5002 | name_s = alloc(len); |
| 5003 | if (name_s == NULL) |
| 5004 | name_s = name; |
| 5005 | else |
| 5006 | vim_snprintf((char *)name_s, len, |
| 5007 | "s:%s", name); |
| 5008 | } |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5009 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, |
| 5010 | scriptvar_sid, type); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5011 | if (name_s != name) |
| 5012 | vim_free(name_s); |
| 5013 | } |
| 5014 | else |
| 5015 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5016 | scriptvar_sid, scriptvar_idx, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5017 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5018 | break; |
| 5019 | case dest_local: |
| 5020 | if (lvar != NULL) |
| 5021 | { |
| 5022 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5023 | + instr->ga_len - 1; |
| 5024 | |
| 5025 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5026 | // ISN_STORE into ISN_STORENR |
| 5027 | if (!lvar->lv_from_outer |
| 5028 | && instr->ga_len == instr_count + 1 |
| 5029 | && isn->isn_type == ISN_PUSHNR) |
| 5030 | { |
| 5031 | varnumber_T val = isn->isn_arg.number; |
| 5032 | |
| 5033 | isn->isn_type = ISN_STORENR; |
| 5034 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5035 | isn->isn_arg.storenr.stnr_val = val; |
| 5036 | if (stack->ga_len > 0) |
| 5037 | --stack->ga_len; |
| 5038 | } |
| 5039 | else if (lvar->lv_from_outer) |
| 5040 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5041 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5042 | else |
| 5043 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5044 | } |
| 5045 | break; |
| 5046 | } |
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 | |
| 5049 | if (var_idx + 1 < var_count) |
| 5050 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5051 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5052 | |
| 5053 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5054 | if (var_count > 0 && !semicolon) |
| 5055 | { |
| 5056 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5057 | goto theend; |
| 5058 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5059 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5060 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5061 | |
| 5062 | theend: |
| 5063 | vim_free(name); |
| 5064 | return ret; |
| 5065 | } |
| 5066 | |
| 5067 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5068 | * Check if "name" can be "unlet". |
| 5069 | */ |
| 5070 | int |
| 5071 | check_vim9_unlet(char_u *name) |
| 5072 | { |
| 5073 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5074 | { |
| 5075 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5076 | return FAIL; |
| 5077 | } |
| 5078 | return OK; |
| 5079 | } |
| 5080 | |
| 5081 | /* |
| 5082 | * Callback passed to ex_unletlock(). |
| 5083 | */ |
| 5084 | static int |
| 5085 | compile_unlet( |
| 5086 | lval_T *lvp, |
| 5087 | char_u *name_end, |
| 5088 | exarg_T *eap, |
| 5089 | int deep UNUSED, |
| 5090 | void *coookie) |
| 5091 | { |
| 5092 | cctx_T *cctx = coookie; |
| 5093 | |
| 5094 | if (lvp->ll_tv == NULL) |
| 5095 | { |
| 5096 | char_u *p = lvp->ll_name; |
| 5097 | int cc = *name_end; |
| 5098 | int ret = OK; |
| 5099 | |
| 5100 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5101 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5102 | if (*p == '$') |
| 5103 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5104 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5105 | ret = FAIL; |
| 5106 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5107 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5108 | |
| 5109 | *name_end = cc; |
| 5110 | return ret; |
| 5111 | } |
| 5112 | |
| 5113 | // TODO: unlet {list}[idx] |
| 5114 | // TODO: unlet {dict}[key] |
| 5115 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5116 | return FAIL; |
| 5117 | } |
| 5118 | |
| 5119 | /* |
| 5120 | * compile "unlet var", "lock var" and "unlock var" |
| 5121 | * "arg" points to "var". |
| 5122 | */ |
| 5123 | static char_u * |
| 5124 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5125 | { |
| 5126 | char_u *p = arg; |
| 5127 | |
| 5128 | if (eap->cmdidx != CMD_unlet) |
| 5129 | { |
| 5130 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5131 | return NULL; |
| 5132 | } |
| 5133 | |
| 5134 | if (*p == '!') |
| 5135 | { |
| 5136 | p = skipwhite(p + 1); |
| 5137 | eap->forceit = TRUE; |
| 5138 | } |
| 5139 | |
| 5140 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5141 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5142 | } |
| 5143 | |
| 5144 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5145 | * Compile an :import command. |
| 5146 | */ |
| 5147 | static char_u * |
| 5148 | compile_import(char_u *arg, cctx_T *cctx) |
| 5149 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5150 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | /* |
| 5154 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5155 | */ |
| 5156 | static int |
| 5157 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5158 | { |
| 5159 | garray_T *instr = &cctx->ctx_instr; |
| 5160 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5161 | |
| 5162 | if (endlabel == NULL) |
| 5163 | return FAIL; |
| 5164 | endlabel->el_next = *el; |
| 5165 | *el = endlabel; |
| 5166 | endlabel->el_end_label = instr->ga_len; |
| 5167 | |
| 5168 | generate_JUMP(cctx, when, 0); |
| 5169 | return OK; |
| 5170 | } |
| 5171 | |
| 5172 | static void |
| 5173 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5174 | { |
| 5175 | garray_T *instr = &cctx->ctx_instr; |
| 5176 | |
| 5177 | while (*el != NULL) |
| 5178 | { |
| 5179 | endlabel_T *cur = (*el); |
| 5180 | isn_T *isn; |
| 5181 | |
| 5182 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5183 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5184 | *el = cur->el_next; |
| 5185 | vim_free(cur); |
| 5186 | } |
| 5187 | } |
| 5188 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5189 | static void |
| 5190 | compile_free_jump_to_end(endlabel_T **el) |
| 5191 | { |
| 5192 | while (*el != NULL) |
| 5193 | { |
| 5194 | endlabel_T *cur = (*el); |
| 5195 | |
| 5196 | *el = cur->el_next; |
| 5197 | vim_free(cur); |
| 5198 | } |
| 5199 | } |
| 5200 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5201 | /* |
| 5202 | * Create a new scope and set up the generic items. |
| 5203 | */ |
| 5204 | static scope_T * |
| 5205 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5206 | { |
| 5207 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5208 | |
| 5209 | if (scope == NULL) |
| 5210 | return NULL; |
| 5211 | scope->se_outer = cctx->ctx_scope; |
| 5212 | cctx->ctx_scope = scope; |
| 5213 | scope->se_type = type; |
| 5214 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5215 | return scope; |
| 5216 | } |
| 5217 | |
| 5218 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5219 | * Free the current scope and go back to the outer scope. |
| 5220 | */ |
| 5221 | static void |
| 5222 | drop_scope(cctx_T *cctx) |
| 5223 | { |
| 5224 | scope_T *scope = cctx->ctx_scope; |
| 5225 | |
| 5226 | if (scope == NULL) |
| 5227 | { |
| 5228 | iemsg("calling drop_scope() without a scope"); |
| 5229 | return; |
| 5230 | } |
| 5231 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5232 | switch (scope->se_type) |
| 5233 | { |
| 5234 | case IF_SCOPE: |
| 5235 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5236 | case FOR_SCOPE: |
| 5237 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5238 | case WHILE_SCOPE: |
| 5239 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5240 | case TRY_SCOPE: |
| 5241 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5242 | case NO_SCOPE: |
| 5243 | case BLOCK_SCOPE: |
| 5244 | break; |
| 5245 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5246 | vim_free(scope); |
| 5247 | } |
| 5248 | |
| 5249 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5250 | * compile "if expr" |
| 5251 | * |
| 5252 | * "if expr" Produces instructions: |
| 5253 | * EVAL expr Push result of "expr" |
| 5254 | * JUMP_IF_FALSE end |
| 5255 | * ... body ... |
| 5256 | * end: |
| 5257 | * |
| 5258 | * "if expr | else" Produces instructions: |
| 5259 | * EVAL expr Push result of "expr" |
| 5260 | * JUMP_IF_FALSE else |
| 5261 | * ... body ... |
| 5262 | * JUMP_ALWAYS end |
| 5263 | * else: |
| 5264 | * ... body ... |
| 5265 | * end: |
| 5266 | * |
| 5267 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5268 | * EVAL expr Push result of "expr" |
| 5269 | * JUMP_IF_FALSE elseif |
| 5270 | * ... body ... |
| 5271 | * JUMP_ALWAYS end |
| 5272 | * elseif: |
| 5273 | * EVAL expr Push result of "expr" |
| 5274 | * JUMP_IF_FALSE else |
| 5275 | * ... body ... |
| 5276 | * JUMP_ALWAYS end |
| 5277 | * else: |
| 5278 | * ... body ... |
| 5279 | * end: |
| 5280 | */ |
| 5281 | static char_u * |
| 5282 | compile_if(char_u *arg, cctx_T *cctx) |
| 5283 | { |
| 5284 | char_u *p = arg; |
| 5285 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5286 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5287 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5288 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5289 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5290 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5291 | CLEAR_FIELD(ppconst); |
| 5292 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5293 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5294 | clear_ppconst(&ppconst); |
| 5295 | return NULL; |
| 5296 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5297 | if (cctx->ctx_skip == SKIP_YES) |
| 5298 | clear_ppconst(&ppconst); |
| 5299 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5300 | { |
| 5301 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5302 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5303 | clear_ppconst(&ppconst); |
| 5304 | } |
| 5305 | else |
| 5306 | { |
| 5307 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5308 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5309 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5310 | return NULL; |
| 5311 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5312 | |
| 5313 | scope = new_scope(cctx, IF_SCOPE); |
| 5314 | if (scope == NULL) |
| 5315 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5316 | scope->se_skip_save = skip_save; |
| 5317 | // "is_had_return" will be reset if any block does not end in :return |
| 5318 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5319 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5320 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5321 | { |
| 5322 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5323 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5324 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5325 | } |
| 5326 | else |
| 5327 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5328 | |
| 5329 | return p; |
| 5330 | } |
| 5331 | |
| 5332 | static char_u * |
| 5333 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 5334 | { |
| 5335 | char_u *p = arg; |
| 5336 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5337 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5338 | isn_T *isn; |
| 5339 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5340 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5341 | |
| 5342 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5343 | { |
| 5344 | emsg(_(e_elseif_without_if)); |
| 5345 | return NULL; |
| 5346 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5347 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5348 | if (!cctx->ctx_had_return) |
| 5349 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5350 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5351 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5352 | { |
| 5353 | 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] | 5354 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5355 | return NULL; |
| 5356 | // previous "if" or "elseif" jumps here |
| 5357 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5358 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5359 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5360 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5361 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5362 | CLEAR_FIELD(ppconst); |
| 5363 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5364 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5365 | clear_ppconst(&ppconst); |
| 5366 | return NULL; |
| 5367 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5368 | if (scope->se_skip_save == SKIP_YES) |
| 5369 | clear_ppconst(&ppconst); |
| 5370 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5371 | { |
| 5372 | // The expression results in a constant. |
| 5373 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5374 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5375 | clear_ppconst(&ppconst); |
| 5376 | scope->se_u.se_if.is_if_label = -1; |
| 5377 | } |
| 5378 | else |
| 5379 | { |
| 5380 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5381 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5382 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5383 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5384 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5385 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5386 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5387 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5388 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5389 | |
| 5390 | return p; |
| 5391 | } |
| 5392 | |
| 5393 | static char_u * |
| 5394 | compile_else(char_u *arg, cctx_T *cctx) |
| 5395 | { |
| 5396 | char_u *p = arg; |
| 5397 | garray_T *instr = &cctx->ctx_instr; |
| 5398 | isn_T *isn; |
| 5399 | scope_T *scope = cctx->ctx_scope; |
| 5400 | |
| 5401 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5402 | { |
| 5403 | emsg(_(e_else_without_if)); |
| 5404 | return NULL; |
| 5405 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5406 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5407 | if (!cctx->ctx_had_return) |
| 5408 | scope->se_u.se_if.is_had_return = FALSE; |
| 5409 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5410 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5411 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5412 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5413 | // 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] | 5414 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5415 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5416 | if (!cctx->ctx_had_return |
| 5417 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 5418 | JUMP_ALWAYS, cctx) == FAIL) |
| 5419 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5420 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5421 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5422 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5423 | { |
| 5424 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5425 | { |
| 5426 | // previous "if" or "elseif" jumps here |
| 5427 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5428 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5429 | scope->se_u.se_if.is_if_label = -1; |
| 5430 | } |
| 5431 | } |
| 5432 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5433 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5434 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 5435 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5436 | |
| 5437 | return p; |
| 5438 | } |
| 5439 | |
| 5440 | static char_u * |
| 5441 | compile_endif(char_u *arg, cctx_T *cctx) |
| 5442 | { |
| 5443 | scope_T *scope = cctx->ctx_scope; |
| 5444 | ifscope_T *ifscope; |
| 5445 | garray_T *instr = &cctx->ctx_instr; |
| 5446 | isn_T *isn; |
| 5447 | |
| 5448 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5449 | { |
| 5450 | emsg(_(e_endif_without_if)); |
| 5451 | return NULL; |
| 5452 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5453 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5454 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5455 | if (!cctx->ctx_had_return) |
| 5456 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5457 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5458 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5459 | { |
| 5460 | // previous "if" or "elseif" jumps here |
| 5461 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5462 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5463 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5464 | // Fill in the "end" label in jumps at the end of the blocks. |
| 5465 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5466 | cctx->ctx_skip = scope->se_skip_save; |
| 5467 | |
| 5468 | // If all the blocks end in :return and there is an :else then the |
| 5469 | // had_return flag is set. |
| 5470 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5471 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5472 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5473 | return arg; |
| 5474 | } |
| 5475 | |
| 5476 | /* |
| 5477 | * compile "for var in expr" |
| 5478 | * |
| 5479 | * Produces instructions: |
| 5480 | * PUSHNR -1 |
| 5481 | * STORE loop-idx Set index to -1 |
| 5482 | * EVAL expr Push result of "expr" |
| 5483 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 5484 | * - if beyond end, jump to "end" |
| 5485 | * - otherwise get item from list and push it |
| 5486 | * STORE var Store item in "var" |
| 5487 | * ... body ... |
| 5488 | * JUMP top Jump back to repeat |
| 5489 | * end: DROP Drop the result of "expr" |
| 5490 | * |
| 5491 | */ |
| 5492 | static char_u * |
| 5493 | compile_for(char_u *arg, cctx_T *cctx) |
| 5494 | { |
| 5495 | char_u *p; |
| 5496 | size_t varlen; |
| 5497 | garray_T *instr = &cctx->ctx_instr; |
| 5498 | garray_T *stack = &cctx->ctx_type_stack; |
| 5499 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5500 | lvar_T *loop_lvar; // loop iteration variable |
| 5501 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5502 | type_T *vartype; |
| 5503 | |
| 5504 | // TODO: list of variables: "for [key, value] in dict" |
| 5505 | // parse "var" |
| 5506 | for (p = arg; eval_isnamec1(*p); ++p) |
| 5507 | ; |
| 5508 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5509 | var_lvar = lookup_local(arg, varlen, cctx); |
| 5510 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5511 | { |
| 5512 | semsg(_("E1023: variable already defined: %s"), arg); |
| 5513 | return NULL; |
| 5514 | } |
| 5515 | |
| 5516 | // consume "in" |
| 5517 | p = skipwhite(p); |
| 5518 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 5519 | { |
| 5520 | emsg(_(e_missing_in)); |
| 5521 | return NULL; |
| 5522 | } |
| 5523 | p = skipwhite(p + 2); |
| 5524 | |
| 5525 | |
| 5526 | scope = new_scope(cctx, FOR_SCOPE); |
| 5527 | if (scope == NULL) |
| 5528 | return NULL; |
| 5529 | |
| 5530 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5531 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 5532 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5533 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5534 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5535 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5536 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5537 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5538 | |
| 5539 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5540 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 5541 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5542 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5543 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5544 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5545 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5546 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5547 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5548 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5549 | |
| 5550 | // compile "expr", it remains on the stack until "endfor" |
| 5551 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5552 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5553 | { |
| 5554 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5555 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5556 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5557 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5558 | // Now that we know the type of "var", check that it is a list, now or at |
| 5559 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5560 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5561 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5562 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5563 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5564 | return NULL; |
| 5565 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5566 | 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] | 5567 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5568 | |
| 5569 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5570 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5571 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5572 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 5573 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5574 | |
| 5575 | return arg; |
| 5576 | } |
| 5577 | |
| 5578 | /* |
| 5579 | * compile "endfor" |
| 5580 | */ |
| 5581 | static char_u * |
| 5582 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 5583 | { |
| 5584 | garray_T *instr = &cctx->ctx_instr; |
| 5585 | scope_T *scope = cctx->ctx_scope; |
| 5586 | forscope_T *forscope; |
| 5587 | isn_T *isn; |
| 5588 | |
| 5589 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 5590 | { |
| 5591 | emsg(_(e_for)); |
| 5592 | return NULL; |
| 5593 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5594 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5595 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5596 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5597 | |
| 5598 | // At end of ":for" scope jump back to the FOR instruction. |
| 5599 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 5600 | |
| 5601 | // Fill in the "end" label in the FOR statement so it can jump here |
| 5602 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 5603 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 5604 | |
| 5605 | // Fill in the "end" label any BREAK statements |
| 5606 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 5607 | |
| 5608 | // Below the ":for" scope drop the "expr" list from the stack. |
| 5609 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5610 | return NULL; |
| 5611 | |
| 5612 | vim_free(scope); |
| 5613 | |
| 5614 | return arg; |
| 5615 | } |
| 5616 | |
| 5617 | /* |
| 5618 | * compile "while expr" |
| 5619 | * |
| 5620 | * Produces instructions: |
| 5621 | * top: EVAL expr Push result of "expr" |
| 5622 | * JUMP_IF_FALSE end jump if false |
| 5623 | * ... body ... |
| 5624 | * JUMP top Jump back to repeat |
| 5625 | * end: |
| 5626 | * |
| 5627 | */ |
| 5628 | static char_u * |
| 5629 | compile_while(char_u *arg, cctx_T *cctx) |
| 5630 | { |
| 5631 | char_u *p = arg; |
| 5632 | garray_T *instr = &cctx->ctx_instr; |
| 5633 | scope_T *scope; |
| 5634 | |
| 5635 | scope = new_scope(cctx, WHILE_SCOPE); |
| 5636 | if (scope == NULL) |
| 5637 | return NULL; |
| 5638 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5639 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5640 | |
| 5641 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5642 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5643 | return NULL; |
| 5644 | |
| 5645 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5646 | 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] | 5647 | JUMP_IF_FALSE, cctx) == FAIL) |
| 5648 | return FAIL; |
| 5649 | |
| 5650 | return p; |
| 5651 | } |
| 5652 | |
| 5653 | /* |
| 5654 | * compile "endwhile" |
| 5655 | */ |
| 5656 | static char_u * |
| 5657 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 5658 | { |
| 5659 | scope_T *scope = cctx->ctx_scope; |
| 5660 | |
| 5661 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 5662 | { |
| 5663 | emsg(_(e_while)); |
| 5664 | return NULL; |
| 5665 | } |
| 5666 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5667 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5668 | |
| 5669 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5670 | 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] | 5671 | |
| 5672 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 5673 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5674 | 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] | 5675 | |
| 5676 | vim_free(scope); |
| 5677 | |
| 5678 | return arg; |
| 5679 | } |
| 5680 | |
| 5681 | /* |
| 5682 | * compile "continue" |
| 5683 | */ |
| 5684 | static char_u * |
| 5685 | compile_continue(char_u *arg, cctx_T *cctx) |
| 5686 | { |
| 5687 | scope_T *scope = cctx->ctx_scope; |
| 5688 | |
| 5689 | for (;;) |
| 5690 | { |
| 5691 | if (scope == NULL) |
| 5692 | { |
| 5693 | emsg(_(e_continue)); |
| 5694 | return NULL; |
| 5695 | } |
| 5696 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5697 | break; |
| 5698 | scope = scope->se_outer; |
| 5699 | } |
| 5700 | |
| 5701 | // Jump back to the FOR or WHILE instruction. |
| 5702 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5703 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 5704 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5705 | return arg; |
| 5706 | } |
| 5707 | |
| 5708 | /* |
| 5709 | * compile "break" |
| 5710 | */ |
| 5711 | static char_u * |
| 5712 | compile_break(char_u *arg, cctx_T *cctx) |
| 5713 | { |
| 5714 | scope_T *scope = cctx->ctx_scope; |
| 5715 | endlabel_T **el; |
| 5716 | |
| 5717 | for (;;) |
| 5718 | { |
| 5719 | if (scope == NULL) |
| 5720 | { |
| 5721 | emsg(_(e_break)); |
| 5722 | return NULL; |
| 5723 | } |
| 5724 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5725 | break; |
| 5726 | scope = scope->se_outer; |
| 5727 | } |
| 5728 | |
| 5729 | // Jump to the end of the FOR or WHILE loop. |
| 5730 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5731 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5732 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5733 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5734 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 5735 | return FAIL; |
| 5736 | |
| 5737 | return arg; |
| 5738 | } |
| 5739 | |
| 5740 | /* |
| 5741 | * compile "{" start of block |
| 5742 | */ |
| 5743 | static char_u * |
| 5744 | compile_block(char_u *arg, cctx_T *cctx) |
| 5745 | { |
| 5746 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 5747 | return NULL; |
| 5748 | return skipwhite(arg + 1); |
| 5749 | } |
| 5750 | |
| 5751 | /* |
| 5752 | * compile end of block: drop one scope |
| 5753 | */ |
| 5754 | static void |
| 5755 | compile_endblock(cctx_T *cctx) |
| 5756 | { |
| 5757 | scope_T *scope = cctx->ctx_scope; |
| 5758 | |
| 5759 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5760 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5761 | vim_free(scope); |
| 5762 | } |
| 5763 | |
| 5764 | /* |
| 5765 | * compile "try" |
| 5766 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 5767 | * finally. |
| 5768 | * Creates another scope for the "try" block itself. |
| 5769 | * TRY instruction sets up exception handling at runtime. |
| 5770 | * |
| 5771 | * "try" |
| 5772 | * TRY -> catch1, -> finally push trystack entry |
| 5773 | * ... try block |
| 5774 | * "throw {exception}" |
| 5775 | * EVAL {exception} |
| 5776 | * THROW create exception |
| 5777 | * ... try block |
| 5778 | * " catch {expr}" |
| 5779 | * JUMP -> finally |
| 5780 | * catch1: PUSH exeception |
| 5781 | * EVAL {expr} |
| 5782 | * MATCH |
| 5783 | * JUMP nomatch -> catch2 |
| 5784 | * CATCH remove exception |
| 5785 | * ... catch block |
| 5786 | * " catch" |
| 5787 | * JUMP -> finally |
| 5788 | * catch2: CATCH remove exception |
| 5789 | * ... catch block |
| 5790 | * " finally" |
| 5791 | * finally: |
| 5792 | * ... finally block |
| 5793 | * " endtry" |
| 5794 | * ENDTRY pop trystack entry, may rethrow |
| 5795 | */ |
| 5796 | static char_u * |
| 5797 | compile_try(char_u *arg, cctx_T *cctx) |
| 5798 | { |
| 5799 | garray_T *instr = &cctx->ctx_instr; |
| 5800 | scope_T *try_scope; |
| 5801 | scope_T *scope; |
| 5802 | |
| 5803 | // scope that holds the jumps that go to catch/finally/endtry |
| 5804 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 5805 | if (try_scope == NULL) |
| 5806 | return NULL; |
| 5807 | |
| 5808 | // "catch" is set when the first ":catch" is found. |
| 5809 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5810 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5811 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 5812 | return NULL; |
| 5813 | |
| 5814 | // scope for the try block itself |
| 5815 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 5816 | if (scope == NULL) |
| 5817 | return NULL; |
| 5818 | |
| 5819 | return arg; |
| 5820 | } |
| 5821 | |
| 5822 | /* |
| 5823 | * compile "catch {expr}" |
| 5824 | */ |
| 5825 | static char_u * |
| 5826 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 5827 | { |
| 5828 | scope_T *scope = cctx->ctx_scope; |
| 5829 | garray_T *instr = &cctx->ctx_instr; |
| 5830 | char_u *p; |
| 5831 | isn_T *isn; |
| 5832 | |
| 5833 | // end block scope from :try or :catch |
| 5834 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5835 | compile_endblock(cctx); |
| 5836 | scope = cctx->ctx_scope; |
| 5837 | |
| 5838 | // Error if not in a :try scope |
| 5839 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 5840 | { |
| 5841 | emsg(_(e_catch)); |
| 5842 | return NULL; |
| 5843 | } |
| 5844 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5845 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5846 | { |
| 5847 | emsg(_("E1033: catch unreachable after catch-all")); |
| 5848 | return NULL; |
| 5849 | } |
| 5850 | |
| 5851 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5852 | 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] | 5853 | JUMP_ALWAYS, cctx) == FAIL) |
| 5854 | return NULL; |
| 5855 | |
| 5856 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5857 | 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] | 5858 | if (isn->isn_arg.try.try_catch == 0) |
| 5859 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5860 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5861 | { |
| 5862 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5863 | 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] | 5864 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5865 | } |
| 5866 | |
| 5867 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 5868 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5869 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5870 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 5871 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5872 | } |
| 5873 | else |
| 5874 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5875 | char_u *end; |
| 5876 | char_u *pat; |
| 5877 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5878 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 5879 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5880 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5881 | // Push v:exception, push {expr} and MATCH |
| 5882 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 5883 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5884 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5885 | if (*end != *p) |
| 5886 | { |
| 5887 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 5888 | vim_free(tofree); |
| 5889 | return FAIL; |
| 5890 | } |
| 5891 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 5892 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5893 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5894 | len = (int)(end - tofree); |
| 5895 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5896 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5897 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5898 | if (pat == NULL) |
| 5899 | return FAIL; |
| 5900 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 5901 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5902 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5903 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 5904 | return NULL; |
| 5905 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5906 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5907 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 5908 | return NULL; |
| 5909 | } |
| 5910 | |
| 5911 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 5912 | return NULL; |
| 5913 | |
| 5914 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 5915 | return NULL; |
| 5916 | return p; |
| 5917 | } |
| 5918 | |
| 5919 | static char_u * |
| 5920 | compile_finally(char_u *arg, cctx_T *cctx) |
| 5921 | { |
| 5922 | scope_T *scope = cctx->ctx_scope; |
| 5923 | garray_T *instr = &cctx->ctx_instr; |
| 5924 | isn_T *isn; |
| 5925 | |
| 5926 | // end block scope from :try or :catch |
| 5927 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5928 | compile_endblock(cctx); |
| 5929 | scope = cctx->ctx_scope; |
| 5930 | |
| 5931 | // Error if not in a :try scope |
| 5932 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 5933 | { |
| 5934 | emsg(_(e_finally)); |
| 5935 | return NULL; |
| 5936 | } |
| 5937 | |
| 5938 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5939 | 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] | 5940 | if (isn->isn_arg.try.try_finally != 0) |
| 5941 | { |
| 5942 | emsg(_(e_finally_dup)); |
| 5943 | return NULL; |
| 5944 | } |
| 5945 | |
| 5946 | // 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] | 5947 | 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] | 5948 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 5949 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5950 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5951 | { |
| 5952 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5953 | 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] | 5954 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 5955 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5956 | } |
| 5957 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5958 | // TODO: set index in ts_finally_label jumps |
| 5959 | |
| 5960 | return arg; |
| 5961 | } |
| 5962 | |
| 5963 | static char_u * |
| 5964 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 5965 | { |
| 5966 | scope_T *scope = cctx->ctx_scope; |
| 5967 | garray_T *instr = &cctx->ctx_instr; |
| 5968 | isn_T *isn; |
| 5969 | |
| 5970 | // end block scope from :catch or :finally |
| 5971 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5972 | compile_endblock(cctx); |
| 5973 | scope = cctx->ctx_scope; |
| 5974 | |
| 5975 | // Error if not in a :try scope |
| 5976 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 5977 | { |
| 5978 | if (scope == NULL) |
| 5979 | emsg(_(e_no_endtry)); |
| 5980 | else if (scope->se_type == WHILE_SCOPE) |
| 5981 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 5982 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5983 | emsg(_(e_endfor)); |
| 5984 | else |
| 5985 | emsg(_(e_endif)); |
| 5986 | return NULL; |
| 5987 | } |
| 5988 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5989 | 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] | 5990 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 5991 | { |
| 5992 | emsg(_("E1032: missing :catch or :finally")); |
| 5993 | return NULL; |
| 5994 | } |
| 5995 | |
| 5996 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 5997 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5998 | 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] | 5999 | |
| 6000 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6001 | if (isn->isn_arg.try.try_catch == 0) |
| 6002 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6003 | if (isn->isn_arg.try.try_finally == 0) |
| 6004 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6005 | |
| 6006 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6007 | { |
| 6008 | // Last catch without match jumps here |
| 6009 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6010 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6011 | } |
| 6012 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6013 | compile_endblock(cctx); |
| 6014 | |
| 6015 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6016 | return NULL; |
| 6017 | return arg; |
| 6018 | } |
| 6019 | |
| 6020 | /* |
| 6021 | * compile "throw {expr}" |
| 6022 | */ |
| 6023 | static char_u * |
| 6024 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6025 | { |
| 6026 | char_u *p = skipwhite(arg); |
| 6027 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6028 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6029 | return NULL; |
| 6030 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6031 | return NULL; |
| 6032 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6033 | return NULL; |
| 6034 | |
| 6035 | return p; |
| 6036 | } |
| 6037 | |
| 6038 | /* |
| 6039 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6040 | * compile "echomsg expr" |
| 6041 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6042 | * compile "execute expr" |
| 6043 | */ |
| 6044 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6045 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6046 | { |
| 6047 | char_u *p = arg; |
| 6048 | int count = 0; |
| 6049 | |
| 6050 | for (;;) |
| 6051 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6052 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6053 | return NULL; |
| 6054 | ++count; |
| 6055 | p = skipwhite(p); |
| 6056 | if (ends_excmd(*p)) |
| 6057 | break; |
| 6058 | } |
| 6059 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6060 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6061 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6062 | else if (cmdidx == CMD_execute) |
| 6063 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6064 | else if (cmdidx == CMD_echomsg) |
| 6065 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6066 | else |
| 6067 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6068 | return p; |
| 6069 | } |
| 6070 | |
| 6071 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6072 | * A command that is not compiled, execute with legacy code. |
| 6073 | */ |
| 6074 | static char_u * |
| 6075 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6076 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6077 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6078 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6079 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6080 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6081 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6082 | goto theend; |
| 6083 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6084 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6085 | { |
| 6086 | long argt = excmd_get_argt(eap->cmdidx); |
| 6087 | int usefilter = FALSE; |
| 6088 | |
| 6089 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6090 | |
| 6091 | // If the command can be followed by a bar, find the bar and truncate |
| 6092 | // it, so that the following command can be compiled. |
| 6093 | // The '|' is overwritten with a NUL, it is put back below. |
| 6094 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6095 | && *eap->arg == '!') |
| 6096 | // :w !filter or :r !filter or :r! filter |
| 6097 | usefilter = TRUE; |
| 6098 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6099 | { |
| 6100 | separate_nextcmd(eap); |
| 6101 | if (eap->nextcmd != NULL) |
| 6102 | nextcmd = eap->nextcmd; |
| 6103 | } |
| 6104 | } |
| 6105 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6106 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6107 | { |
| 6108 | // expand filename in "syntax include [@group] filename" |
| 6109 | has_expr = TRUE; |
| 6110 | eap->arg = skipwhite(eap->arg + 7); |
| 6111 | if (*eap->arg == '@') |
| 6112 | eap->arg = skiptowhite(eap->arg); |
| 6113 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6114 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6115 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6116 | { |
| 6117 | int count = 0; |
| 6118 | char_u *start = skipwhite(line); |
| 6119 | |
| 6120 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6121 | // PUSHS ":cmd xxx" |
| 6122 | // eval expr1 |
| 6123 | // PUSHS "yyy" |
| 6124 | // eval expr2 |
| 6125 | // PUSHS "zzz" |
| 6126 | // EXECCONCAT 5 |
| 6127 | for (;;) |
| 6128 | { |
| 6129 | if (p > start) |
| 6130 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6131 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6132 | ++count; |
| 6133 | } |
| 6134 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6135 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6136 | return NULL; |
| 6137 | may_generate_2STRING(-1, cctx); |
| 6138 | ++count; |
| 6139 | p = skipwhite(p); |
| 6140 | if (*p != '`') |
| 6141 | { |
| 6142 | emsg(_("E1083: missing backtick")); |
| 6143 | return NULL; |
| 6144 | } |
| 6145 | start = p + 1; |
| 6146 | |
| 6147 | p = (char_u *)strstr((char *)start, "`="); |
| 6148 | if (p == NULL) |
| 6149 | { |
| 6150 | if (*skipwhite(start) != NUL) |
| 6151 | { |
| 6152 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6153 | ++count; |
| 6154 | } |
| 6155 | break; |
| 6156 | } |
| 6157 | } |
| 6158 | generate_EXECCONCAT(cctx, count); |
| 6159 | } |
| 6160 | else |
| 6161 | generate_EXEC(cctx, line); |
| 6162 | |
| 6163 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6164 | if (*nextcmd != NUL) |
| 6165 | { |
| 6166 | // the parser expects a pointer to the bar, put it back |
| 6167 | --nextcmd; |
| 6168 | *nextcmd = '|'; |
| 6169 | } |
| 6170 | |
| 6171 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6172 | } |
| 6173 | |
| 6174 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6175 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6176 | * 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] | 6177 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6178 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6179 | add_def_function(ufunc_T *ufunc) |
| 6180 | { |
| 6181 | dfunc_T *dfunc; |
| 6182 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6183 | if (def_functions.ga_len == 0) |
| 6184 | { |
| 6185 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6186 | // wasn't set. |
| 6187 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6188 | return FAIL; |
| 6189 | ++def_functions.ga_len; |
| 6190 | } |
| 6191 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6192 | // Add the function to "def_functions". |
| 6193 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6194 | return FAIL; |
| 6195 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6196 | CLEAR_POINTER(dfunc); |
| 6197 | dfunc->df_idx = def_functions.ga_len; |
| 6198 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6199 | dfunc->df_ufunc = ufunc; |
| 6200 | ++def_functions.ga_len; |
| 6201 | return OK; |
| 6202 | } |
| 6203 | |
| 6204 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6205 | * After ex_function() has collected all the function lines: parse and compile |
| 6206 | * the lines into instructions. |
| 6207 | * Adds the function to "def_functions". |
| 6208 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6209 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6210 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6211 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6212 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6213 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6214 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6215 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6216 | 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] | 6217 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6218 | char_u *line = NULL; |
| 6219 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6220 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6221 | cctx_T cctx; |
| 6222 | garray_T *instr; |
| 6223 | int called_emsg_before = called_emsg; |
| 6224 | int ret = FAIL; |
| 6225 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6226 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6227 | int emsg_before = called_emsg; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6228 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6229 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6230 | // When using a function that was compiled before: Free old instructions. |
| 6231 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6232 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6233 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6234 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6235 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6236 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6237 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6238 | else |
| 6239 | { |
| 6240 | if (add_def_function(ufunc) == FAIL) |
| 6241 | return FAIL; |
| 6242 | new_def_function = TRUE; |
| 6243 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6244 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6245 | ufunc->uf_def_status = UF_COMPILING; |
| 6246 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6247 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6248 | cctx.ctx_ufunc = ufunc; |
| 6249 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6250 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6251 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6252 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6253 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6254 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6255 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6256 | instr = &cctx.ctx_instr; |
| 6257 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6258 | // Set the context to the function, it may be compiled when called from |
| 6259 | // another script. Set the script version to the most modern one. |
| 6260 | // The line number will be set in next_line_from_context(). |
| 6261 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6262 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6263 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6264 | // Make sure error messages are OK. |
| 6265 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6266 | if (do_estack_push) |
| 6267 | estack_push_ufunc(ufunc, 1); |
| 6268 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6269 | if (ufunc->uf_def_args.ga_len > 0) |
| 6270 | { |
| 6271 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6272 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6273 | int i; |
| 6274 | char_u *arg; |
| 6275 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6276 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6277 | |
| 6278 | // Produce instructions for the default values of optional arguments. |
| 6279 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6280 | // where to start when the function is called, depending on the number |
| 6281 | // of arguments. |
| 6282 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6283 | if (ufunc->uf_def_arg_idx == NULL) |
| 6284 | goto erret; |
| 6285 | for (i = 0; i < count; ++i) |
| 6286 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6287 | garray_T *stack = &cctx.ctx_type_stack; |
| 6288 | type_T *val_type; |
| 6289 | int arg_idx = first_def_arg + i; |
| 6290 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6291 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6292 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6293 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6294 | goto erret; |
| 6295 | |
| 6296 | // If no type specified use the type of the default value. |
| 6297 | // Otherwise check that the default value type matches the |
| 6298 | // specified type. |
| 6299 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6300 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6301 | { |
| 6302 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6303 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6304 | } |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 6305 | 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] | 6306 | == FAIL) |
| 6307 | { |
| 6308 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 6309 | arg_idx + 1); |
| 6310 | goto erret; |
| 6311 | } |
| 6312 | |
| 6313 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6314 | goto erret; |
| 6315 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6316 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6317 | |
| 6318 | if (did_set_arg_type) |
| 6319 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6320 | } |
| 6321 | |
| 6322 | /* |
| 6323 | * Loop over all the lines of the function and generate instructions. |
| 6324 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6325 | for (;;) |
| 6326 | { |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6327 | exarg_T ea; |
| 6328 | cmdmod_T save_cmdmod; |
| 6329 | int starts_with_colon = FALSE; |
| 6330 | char_u *cmd; |
| 6331 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6332 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6333 | // Bail out on the first error to avoid a flood of errors and report |
| 6334 | // the right line number when inside try/catch. |
| 6335 | if (emsg_before != called_emsg) |
| 6336 | goto erret; |
| 6337 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6338 | if (line != NULL && *line == '|') |
| 6339 | // the line continues after a '|' |
| 6340 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 6341 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6342 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 6343 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6344 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 6345 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6346 | goto erret; |
| 6347 | } |
| 6348 | else |
| 6349 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 6350 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 6351 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6352 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6353 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6354 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6355 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6356 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6357 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6358 | ea.cmdlinep = &line; |
| 6359 | ea.cmd = skipwhite(line); |
| 6360 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6361 | // Some things can be recognized by the first character. |
| 6362 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6363 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6364 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6365 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6366 | if (ea.cmd[1] != '{') |
| 6367 | { |
| 6368 | line = (char_u *)""; |
| 6369 | continue; |
| 6370 | } |
| 6371 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6372 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6373 | case '}': |
| 6374 | { |
| 6375 | // "}" ends a block scope |
| 6376 | scopetype_T stype = cctx.ctx_scope == NULL |
| 6377 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6378 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6379 | if (stype == BLOCK_SCOPE) |
| 6380 | { |
| 6381 | compile_endblock(&cctx); |
| 6382 | line = ea.cmd; |
| 6383 | } |
| 6384 | else |
| 6385 | { |
| 6386 | emsg(_("E1025: using } outside of a block scope")); |
| 6387 | goto erret; |
| 6388 | } |
| 6389 | if (line != NULL) |
| 6390 | line = skipwhite(ea.cmd + 1); |
| 6391 | continue; |
| 6392 | } |
| 6393 | |
| 6394 | case '{': |
| 6395 | // "{" starts a block scope |
| 6396 | // "{'a': 1}->func() is something else |
| 6397 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 6398 | { |
| 6399 | line = compile_block(ea.cmd, &cctx); |
| 6400 | continue; |
| 6401 | } |
| 6402 | break; |
| 6403 | |
| 6404 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6405 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6406 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6407 | } |
| 6408 | |
| 6409 | /* |
| 6410 | * COMMAND MODIFIERS |
| 6411 | */ |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6412 | save_cmdmod = cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6413 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 6414 | { |
| 6415 | if (errormsg != NULL) |
| 6416 | goto erret; |
| 6417 | // empty line or comment |
| 6418 | line = (char_u *)""; |
| 6419 | continue; |
| 6420 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 6421 | // TODO: use modifiers in the command |
| 6422 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6423 | cmdmod = save_cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6424 | |
| 6425 | // Skip ":call" to get to the function name. |
| 6426 | if (checkforcmd(&ea.cmd, "call", 3)) |
| 6427 | ea.cmd = skipwhite(ea.cmd); |
| 6428 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6429 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6430 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6431 | char_u *pskip; |
| 6432 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6433 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6434 | // find what follows. |
| 6435 | // Skip over "var.member", "var[idx]" and the like. |
| 6436 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 6437 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6438 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6439 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6440 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6441 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6442 | char_u *var_end; |
| 6443 | int oplen; |
| 6444 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6445 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 6446 | if (ea.cmd[0] == '@') |
| 6447 | var_end = ea.cmd + 2; |
| 6448 | else |
| 6449 | var_end = find_name_end(pskip, NULL, NULL, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6450 | FNE_CHECK_START | FNE_INCL_BR); |
| 6451 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6452 | if (oplen > 0) |
| 6453 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6454 | size_t len = p - ea.cmd; |
| 6455 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6456 | // Recognize an assignment if we recognize the variable |
| 6457 | // name: |
| 6458 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 6459 | // "local = expr" where "local" is a local var. |
| 6460 | // "script = expr" where "script" is a script-local var. |
| 6461 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6462 | // "&opt = expr" |
| 6463 | // "$ENV = expr" |
| 6464 | // "@r = expr" |
| 6465 | if (*ea.cmd == '&' |
| 6466 | || *ea.cmd == '$' |
| 6467 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6468 | || ((len) > 2 && ea.cmd[1] == ':') |
| 6469 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 6470 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 6471 | NULL, &cctx) == OK |
| 6472 | || lookup_script(ea.cmd, len) == OK |
| 6473 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6474 | { |
| 6475 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6476 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6477 | goto erret; |
| 6478 | continue; |
| 6479 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6480 | } |
| 6481 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6482 | |
| 6483 | if (*ea.cmd == '[') |
| 6484 | { |
| 6485 | // [var, var] = expr |
| 6486 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 6487 | if (line == NULL) |
| 6488 | goto erret; |
| 6489 | if (line != ea.cmd) |
| 6490 | continue; |
| 6491 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6492 | } |
| 6493 | |
| 6494 | /* |
| 6495 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6496 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6497 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6498 | cmd = ea.cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6499 | if (*cmd != '\'') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6500 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6501 | ea.cmd = skip_range(ea.cmd, NULL); |
| 6502 | if (ea.cmd > cmd && !starts_with_colon) |
| 6503 | { |
| 6504 | emsg(_(e_colon_required)); |
| 6505 | goto erret; |
| 6506 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6507 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6508 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6509 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6510 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6511 | |
| 6512 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 6513 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6514 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6515 | { |
| 6516 | line += STRLEN(line); |
| 6517 | continue; |
| 6518 | } |
| 6519 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6520 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6521 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6522 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6523 | // CMD_let cannot happen, compile_assignment() above is used |
| 6524 | iemsg("Command from find_ex_command() not handled"); |
| 6525 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6526 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6527 | } |
| 6528 | |
| 6529 | p = skipwhite(p); |
| 6530 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6531 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 6532 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6533 | && ea.cmdidx != CMD_elseif |
| 6534 | && ea.cmdidx != CMD_else |
| 6535 | && ea.cmdidx != CMD_endif) |
| 6536 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6537 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6538 | continue; |
| 6539 | } |
| 6540 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6541 | if (ea.cmdidx != CMD_elseif |
| 6542 | && ea.cmdidx != CMD_else |
| 6543 | && ea.cmdidx != CMD_endif |
| 6544 | && ea.cmdidx != CMD_endfor |
| 6545 | && ea.cmdidx != CMD_endwhile |
| 6546 | && ea.cmdidx != CMD_catch |
| 6547 | && ea.cmdidx != CMD_finally |
| 6548 | && ea.cmdidx != CMD_endtry) |
| 6549 | { |
| 6550 | if (cctx.ctx_had_return) |
| 6551 | { |
| 6552 | emsg(_("E1095: Unreachable code after :return")); |
| 6553 | goto erret; |
| 6554 | } |
| 6555 | } |
| 6556 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6557 | switch (ea.cmdidx) |
| 6558 | { |
| 6559 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6560 | ea.arg = p; |
| 6561 | line = compile_nested_function(&ea, &cctx); |
| 6562 | break; |
| 6563 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6564 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6565 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6566 | goto erret; |
| 6567 | |
| 6568 | case CMD_return: |
| 6569 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6570 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6571 | break; |
| 6572 | |
| 6573 | case CMD_let: |
| 6574 | case CMD_const: |
| 6575 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6576 | if (line == p) |
| 6577 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6578 | break; |
| 6579 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6580 | case CMD_unlet: |
| 6581 | case CMD_unlockvar: |
| 6582 | case CMD_lockvar: |
| 6583 | line = compile_unletlock(p, &ea, &cctx); |
| 6584 | break; |
| 6585 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6586 | case CMD_import: |
| 6587 | line = compile_import(p, &cctx); |
| 6588 | break; |
| 6589 | |
| 6590 | case CMD_if: |
| 6591 | line = compile_if(p, &cctx); |
| 6592 | break; |
| 6593 | case CMD_elseif: |
| 6594 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6595 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6596 | break; |
| 6597 | case CMD_else: |
| 6598 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6599 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6600 | break; |
| 6601 | case CMD_endif: |
| 6602 | line = compile_endif(p, &cctx); |
| 6603 | break; |
| 6604 | |
| 6605 | case CMD_while: |
| 6606 | line = compile_while(p, &cctx); |
| 6607 | break; |
| 6608 | case CMD_endwhile: |
| 6609 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6610 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6611 | break; |
| 6612 | |
| 6613 | case CMD_for: |
| 6614 | line = compile_for(p, &cctx); |
| 6615 | break; |
| 6616 | case CMD_endfor: |
| 6617 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6618 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6619 | break; |
| 6620 | case CMD_continue: |
| 6621 | line = compile_continue(p, &cctx); |
| 6622 | break; |
| 6623 | case CMD_break: |
| 6624 | line = compile_break(p, &cctx); |
| 6625 | break; |
| 6626 | |
| 6627 | case CMD_try: |
| 6628 | line = compile_try(p, &cctx); |
| 6629 | break; |
| 6630 | case CMD_catch: |
| 6631 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6632 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6633 | break; |
| 6634 | case CMD_finally: |
| 6635 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6636 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6637 | break; |
| 6638 | case CMD_endtry: |
| 6639 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6640 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6641 | break; |
| 6642 | case CMD_throw: |
| 6643 | line = compile_throw(p, &cctx); |
| 6644 | break; |
| 6645 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6646 | case CMD_eval: |
| 6647 | if (compile_expr0(&p, &cctx) == FAIL) |
| 6648 | goto erret; |
| 6649 | |
| 6650 | // drop the return value |
| 6651 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 6652 | |
| 6653 | line = skipwhite(p); |
| 6654 | break; |
| 6655 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6656 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6657 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6658 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6659 | case CMD_echomsg: |
| 6660 | case CMD_echoerr: |
| 6661 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6662 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6663 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6664 | // TODO: other commands with an expression argument |
| 6665 | |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 6666 | case CMD_append: |
| 6667 | case CMD_change: |
| 6668 | case CMD_insert: |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 6669 | case CMD_t: |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 6670 | case CMD_xit: |
| 6671 | not_in_vim9(&ea); |
| 6672 | goto erret; |
| 6673 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 6674 | case CMD_SIZE: |
| 6675 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 6676 | goto erret; |
| 6677 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6678 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6679 | // Not recognized, execute with do_cmdline_cmd(). |
| 6680 | ea.arg = p; |
| 6681 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6682 | break; |
| 6683 | } |
| 6684 | if (line == NULL) |
| 6685 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6686 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6687 | |
| 6688 | if (cctx.ctx_type_stack.ga_len < 0) |
| 6689 | { |
| 6690 | iemsg("Type stack underflow"); |
| 6691 | goto erret; |
| 6692 | } |
| 6693 | } |
| 6694 | |
| 6695 | if (cctx.ctx_scope != NULL) |
| 6696 | { |
| 6697 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 6698 | emsg(_(e_endif)); |
| 6699 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 6700 | emsg(_(e_endwhile)); |
| 6701 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 6702 | emsg(_(e_endfor)); |
| 6703 | else |
| 6704 | emsg(_("E1026: Missing }")); |
| 6705 | goto erret; |
| 6706 | } |
| 6707 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6708 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6709 | { |
| 6710 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 6711 | { |
| 6712 | emsg(_("E1027: Missing return statement")); |
| 6713 | goto erret; |
| 6714 | } |
| 6715 | |
| 6716 | // Return zero if there is no return at the end. |
| 6717 | generate_PUSHNR(&cctx, 0); |
| 6718 | generate_instr(&cctx, ISN_RETURN); |
| 6719 | } |
| 6720 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6721 | { |
| 6722 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6723 | + ufunc->uf_dfunc_idx; |
| 6724 | dfunc->df_deleted = FALSE; |
| 6725 | dfunc->df_instr = instr->ga_data; |
| 6726 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6727 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 6728 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6729 | if (cctx.ctx_outer_used) |
| 6730 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6731 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6732 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6733 | |
| 6734 | ret = OK; |
| 6735 | |
| 6736 | erret: |
| 6737 | if (ret == FAIL) |
| 6738 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6739 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6740 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6741 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6742 | |
| 6743 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 6744 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6745 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6746 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6747 | // If using the last entry in the table and it was added above, we |
| 6748 | // might as well remove it. |
| 6749 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 6750 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6751 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6752 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6753 | ufunc->uf_dfunc_idx = 0; |
| 6754 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6755 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6756 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6757 | while (cctx.ctx_scope != NULL) |
| 6758 | drop_scope(&cctx); |
| 6759 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6760 | // Don't execute this function body. |
| 6761 | ga_clear_strings(&ufunc->uf_lines); |
| 6762 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6763 | if (errormsg != NULL) |
| 6764 | emsg(errormsg); |
| 6765 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 6766 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6767 | } |
| 6768 | |
| 6769 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6770 | if (do_estack_push) |
| 6771 | estack_pop(); |
| 6772 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6773 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6774 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6775 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6776 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6777 | } |
| 6778 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 6779 | void |
| 6780 | set_function_type(ufunc_T *ufunc) |
| 6781 | { |
| 6782 | int varargs = ufunc->uf_va_name != NULL; |
| 6783 | int argcount = ufunc->uf_args.ga_len; |
| 6784 | |
| 6785 | // Create a type for the function, with the return type and any |
| 6786 | // argument types. |
| 6787 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 6788 | // The type is included in "tt_args". |
| 6789 | if (argcount > 0 || varargs) |
| 6790 | { |
| 6791 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 6792 | argcount, &ufunc->uf_type_list); |
| 6793 | // Add argument types to the function type. |
| 6794 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 6795 | argcount + varargs, |
| 6796 | &ufunc->uf_type_list) == FAIL) |
| 6797 | return; |
| 6798 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 6799 | ufunc->uf_func_type->tt_min_argcount = |
| 6800 | argcount - ufunc->uf_def_args.ga_len; |
| 6801 | if (ufunc->uf_arg_types == NULL) |
| 6802 | { |
| 6803 | int i; |
| 6804 | |
| 6805 | // lambda does not have argument types. |
| 6806 | for (i = 0; i < argcount; ++i) |
| 6807 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 6808 | } |
| 6809 | else |
| 6810 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 6811 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 6812 | if (varargs) |
| 6813 | { |
| 6814 | ufunc->uf_func_type->tt_args[argcount] = |
| 6815 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 6816 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 6817 | } |
| 6818 | } |
| 6819 | else |
| 6820 | // No arguments, can use a predefined type. |
| 6821 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 6822 | argcount, &ufunc->uf_type_list); |
| 6823 | } |
| 6824 | |
| 6825 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6826 | /* |
| 6827 | * Delete an instruction, free what it contains. |
| 6828 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6829 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6830 | delete_instr(isn_T *isn) |
| 6831 | { |
| 6832 | switch (isn->isn_type) |
| 6833 | { |
| 6834 | case ISN_EXEC: |
| 6835 | case ISN_LOADENV: |
| 6836 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 6837 | case ISN_LOADB: |
| 6838 | case ISN_LOADW: |
| 6839 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6840 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6841 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6842 | case ISN_PUSHEXC: |
| 6843 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6844 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6845 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 6846 | case ISN_STOREB: |
| 6847 | case ISN_STOREW: |
| 6848 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6849 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6850 | vim_free(isn->isn_arg.string); |
| 6851 | break; |
| 6852 | |
| 6853 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6854 | case ISN_STORES: |
| 6855 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6856 | break; |
| 6857 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6858 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 6859 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6860 | vim_free(isn->isn_arg.unlet.ul_name); |
| 6861 | break; |
| 6862 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6863 | case ISN_STOREOPT: |
| 6864 | vim_free(isn->isn_arg.storeopt.so_name); |
| 6865 | break; |
| 6866 | |
| 6867 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 6868 | blob_unref(isn->isn_arg.blob); |
| 6869 | break; |
| 6870 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6871 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6872 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6873 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6874 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6875 | break; |
| 6876 | |
| 6877 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6878 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6879 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6880 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6881 | break; |
| 6882 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6883 | case ISN_UCALL: |
| 6884 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 6885 | break; |
| 6886 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 6887 | case ISN_FUNCREF: |
| 6888 | { |
| 6889 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6890 | + isn->isn_arg.funcref.fr_func; |
| 6891 | func_ptr_unref(dfunc->df_ufunc); |
| 6892 | } |
| 6893 | break; |
| 6894 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 6895 | case ISN_NEWFUNC: |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 6896 | { |
| 6897 | char_u *lambda = isn->isn_arg.newfunc.nf_lambda; |
| 6898 | ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL); |
| 6899 | |
| 6900 | if (ufunc != NULL) |
| 6901 | { |
| 6902 | // Clear uf_dfunc_idx so that the function is deleted. |
| 6903 | clear_def_function(ufunc); |
| 6904 | ufunc->uf_dfunc_idx = 0; |
| 6905 | func_ptr_unref(ufunc); |
| 6906 | } |
| 6907 | |
| 6908 | vim_free(lambda); |
| 6909 | vim_free(isn->isn_arg.newfunc.nf_global); |
| 6910 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 6911 | break; |
| 6912 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6913 | case ISN_2BOOL: |
| 6914 | case ISN_2STRING: |
| 6915 | case ISN_ADDBLOB: |
| 6916 | case ISN_ADDLIST: |
| 6917 | case ISN_BCALL: |
| 6918 | case ISN_CATCH: |
| 6919 | case ISN_CHECKNR: |
| 6920 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 6921 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6922 | case ISN_COMPAREANY: |
| 6923 | case ISN_COMPAREBLOB: |
| 6924 | case ISN_COMPAREBOOL: |
| 6925 | case ISN_COMPAREDICT: |
| 6926 | case ISN_COMPAREFLOAT: |
| 6927 | case ISN_COMPAREFUNC: |
| 6928 | case ISN_COMPARELIST: |
| 6929 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6930 | case ISN_COMPARESPECIAL: |
| 6931 | case ISN_COMPARESTRING: |
| 6932 | case ISN_CONCAT: |
| 6933 | case ISN_DCALL: |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 6934 | case ISN_SHUFFLE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6935 | case ISN_DROP: |
| 6936 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6937 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6938 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6939 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6940 | case ISN_EXECCONCAT: |
| 6941 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6942 | case ISN_FOR: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 6943 | case ISN_LISTINDEX: |
| 6944 | case ISN_STRINDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6945 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 6946 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6947 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6948 | case ISN_JUMP: |
| 6949 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 6950 | case ISN_LOADBDICT: |
| 6951 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6952 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6953 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 6954 | case ISN_LOADSCRIPT: |
| 6955 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6956 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 6957 | case ISN_LOADWDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6958 | case ISN_NEGATENR: |
| 6959 | case ISN_NEWDICT: |
| 6960 | case ISN_NEWLIST: |
| 6961 | case ISN_OPNR: |
| 6962 | case ISN_OPFLOAT: |
| 6963 | case ISN_OPANY: |
| 6964 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6965 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6966 | case ISN_PUSHF: |
| 6967 | case ISN_PUSHNR: |
| 6968 | case ISN_PUSHBOOL: |
| 6969 | case ISN_PUSHSPEC: |
| 6970 | case ISN_RETURN: |
| 6971 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 6972 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6973 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6974 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6975 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6976 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6977 | case ISN_STOREDICT: |
| 6978 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6979 | case ISN_THROW: |
| 6980 | case ISN_TRY: |
| 6981 | // nothing allocated |
| 6982 | break; |
| 6983 | } |
| 6984 | } |
| 6985 | |
| 6986 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6987 | * Free all instructions for "dfunc". |
| 6988 | */ |
| 6989 | static void |
| 6990 | delete_def_function_contents(dfunc_T *dfunc) |
| 6991 | { |
| 6992 | int idx; |
| 6993 | |
| 6994 | ga_clear(&dfunc->df_def_args_isn); |
| 6995 | |
| 6996 | if (dfunc->df_instr != NULL) |
| 6997 | { |
| 6998 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 6999 | delete_instr(dfunc->df_instr + idx); |
| 7000 | VIM_CLEAR(dfunc->df_instr); |
| 7001 | } |
| 7002 | |
| 7003 | dfunc->df_deleted = TRUE; |
| 7004 | } |
| 7005 | |
| 7006 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7007 | * When a user function is deleted, clear the contents of any associated def |
| 7008 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7009 | */ |
| 7010 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7011 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7012 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7013 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7014 | { |
| 7015 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7016 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7017 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7018 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7019 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7020 | } |
| 7021 | } |
| 7022 | |
| 7023 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7024 | /* |
| 7025 | * Free all functions defined with ":def". |
| 7026 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7027 | void |
| 7028 | free_def_functions(void) |
| 7029 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7030 | int idx; |
| 7031 | |
| 7032 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7033 | { |
| 7034 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7035 | |
| 7036 | delete_def_function_contents(dfunc); |
| 7037 | } |
| 7038 | |
| 7039 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7040 | } |
| 7041 | #endif |
| 7042 | |
| 7043 | |
| 7044 | #endif // FEAT_EVAL |