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 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1113 | // get the member type from all the items on the stack. |
| 1114 | member = get_member_type_from_stack( |
| 1115 | ((type_T **)stack->ga_data) + stack->ga_len, count, 1, |
| 1116 | cctx->ctx_type_list); |
| 1117 | type = get_list_type(member, cctx->ctx_type_list); |
| 1118 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1119 | // drop the value types |
| 1120 | stack->ga_len -= count; |
| 1121 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1122 | // add the list type to the type stack |
| 1123 | if (ga_grow(stack, 1) == FAIL) |
| 1124 | return FAIL; |
| 1125 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1126 | ++stack->ga_len; |
| 1127 | |
| 1128 | return OK; |
| 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | * Generate an ISN_NEWDICT instruction. |
| 1133 | */ |
| 1134 | static int |
| 1135 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1136 | { |
| 1137 | isn_T *isn; |
| 1138 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1139 | type_T *type; |
| 1140 | type_T *member; |
| 1141 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1142 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1143 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1144 | return FAIL; |
| 1145 | isn->isn_arg.number = count; |
| 1146 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1147 | member = get_member_type_from_stack( |
| 1148 | ((type_T **)stack->ga_data) + stack->ga_len, count, 2, |
| 1149 | cctx->ctx_type_list); |
| 1150 | type = get_dict_type(member, cctx->ctx_type_list); |
| 1151 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1152 | // drop the key and value types |
| 1153 | stack->ga_len -= 2 * count; |
| 1154 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1155 | // add the dict type to the type stack |
| 1156 | if (ga_grow(stack, 1) == FAIL) |
| 1157 | return FAIL; |
| 1158 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1159 | ++stack->ga_len; |
| 1160 | |
| 1161 | return OK; |
| 1162 | } |
| 1163 | |
| 1164 | /* |
| 1165 | * Generate an ISN_FUNCREF instruction. |
| 1166 | */ |
| 1167 | static int |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1168 | generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1169 | { |
| 1170 | isn_T *isn; |
| 1171 | garray_T *stack = &cctx->ctx_type_stack; |
| 1172 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1173 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1174 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1175 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1176 | isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1177 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1178 | |
| 1179 | if (ga_grow(stack, 1) == FAIL) |
| 1180 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1181 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 1182 | ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1183 | ++stack->ga_len; |
| 1184 | |
| 1185 | return OK; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1189 | * Generate an ISN_NEWFUNC instruction. |
| 1190 | */ |
| 1191 | static int |
| 1192 | generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) |
| 1193 | { |
| 1194 | isn_T *isn; |
| 1195 | char_u *name; |
| 1196 | |
| 1197 | RETURN_OK_IF_SKIP(cctx); |
| 1198 | name = vim_strsave(lambda_name); |
| 1199 | if (name == NULL) |
| 1200 | return FAIL; |
| 1201 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
| 1202 | return FAIL; |
| 1203 | isn->isn_arg.newfunc.nf_lambda = name; |
| 1204 | isn->isn_arg.newfunc.nf_global = func_name; |
| 1205 | |
| 1206 | return OK; |
| 1207 | } |
| 1208 | |
| 1209 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1210 | * Generate an ISN_JUMP instruction. |
| 1211 | */ |
| 1212 | static int |
| 1213 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1214 | { |
| 1215 | isn_T *isn; |
| 1216 | garray_T *stack = &cctx->ctx_type_stack; |
| 1217 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1218 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1219 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1220 | return FAIL; |
| 1221 | isn->isn_arg.jump.jump_when = when; |
| 1222 | isn->isn_arg.jump.jump_where = where; |
| 1223 | |
| 1224 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1225 | --stack->ga_len; |
| 1226 | |
| 1227 | return OK; |
| 1228 | } |
| 1229 | |
| 1230 | static int |
| 1231 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1232 | { |
| 1233 | isn_T *isn; |
| 1234 | garray_T *stack = &cctx->ctx_type_stack; |
| 1235 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1236 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1237 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1238 | return FAIL; |
| 1239 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1240 | |
| 1241 | if (ga_grow(stack, 1) == FAIL) |
| 1242 | return FAIL; |
| 1243 | // type doesn't matter, will be stored next |
| 1244 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1245 | ++stack->ga_len; |
| 1246 | |
| 1247 | return OK; |
| 1248 | } |
| 1249 | |
| 1250 | /* |
| 1251 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1252 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1253 | * Return FAIL if the number of arguments is wrong. |
| 1254 | */ |
| 1255 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1256 | 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] | 1257 | { |
| 1258 | isn_T *isn; |
| 1259 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1260 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1261 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1262 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1263 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1264 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1265 | argoff = check_internal_func(func_idx, argcount); |
| 1266 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1267 | return FAIL; |
| 1268 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1269 | if (method_call && argoff > 1) |
| 1270 | { |
| 1271 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1272 | return FAIL; |
| 1273 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1274 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1275 | } |
| 1276 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1277 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1278 | return FAIL; |
| 1279 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1280 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1281 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1282 | for (i = 0; i < argcount; ++i) |
| 1283 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1284 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1285 | stack->ga_len -= argcount; // drop the arguments |
| 1286 | if (ga_grow(stack, 1) == FAIL) |
| 1287 | return FAIL; |
| 1288 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1289 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1290 | ++stack->ga_len; // add return value |
| 1291 | |
| 1292 | return OK; |
| 1293 | } |
| 1294 | |
| 1295 | /* |
| 1296 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1297 | * Return FAIL if the number of arguments is wrong. |
| 1298 | */ |
| 1299 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1300 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1301 | { |
| 1302 | isn_T *isn; |
| 1303 | garray_T *stack = &cctx->ctx_type_stack; |
| 1304 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1305 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1306 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1307 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1308 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1309 | { |
| 1310 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1311 | return FAIL; |
| 1312 | } |
| 1313 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1314 | { |
| 1315 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1316 | return FAIL; |
| 1317 | } |
| 1318 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1319 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1320 | { |
| 1321 | int i; |
| 1322 | |
| 1323 | for (i = 0; i < argcount; ++i) |
| 1324 | { |
| 1325 | type_T *expected; |
| 1326 | type_T *actual; |
| 1327 | |
| 1328 | if (i < regular_args) |
| 1329 | { |
| 1330 | if (ufunc->uf_arg_types == NULL) |
| 1331 | continue; |
| 1332 | expected = ufunc->uf_arg_types[i]; |
| 1333 | } |
| 1334 | else |
| 1335 | expected = ufunc->uf_va_type->tt_member; |
| 1336 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1337 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1338 | { |
| 1339 | arg_type_mismatch(expected, actual, i + 1); |
| 1340 | return FAIL; |
| 1341 | } |
| 1342 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1343 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1344 | if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL) |
| 1345 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1346 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1347 | } |
| 1348 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1349 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1350 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1351 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1352 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1353 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1354 | { |
| 1355 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1356 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1357 | } |
| 1358 | else |
| 1359 | { |
| 1360 | // A user function may be deleted and redefined later, can't use the |
| 1361 | // ufunc pointer, need to look it up again at runtime. |
| 1362 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1363 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1364 | } |
| 1365 | |
| 1366 | stack->ga_len -= argcount; // drop the arguments |
| 1367 | if (ga_grow(stack, 1) == FAIL) |
| 1368 | return FAIL; |
| 1369 | // add return value |
| 1370 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1371 | ++stack->ga_len; |
| 1372 | |
| 1373 | return OK; |
| 1374 | } |
| 1375 | |
| 1376 | /* |
| 1377 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1378 | */ |
| 1379 | static int |
| 1380 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1381 | { |
| 1382 | isn_T *isn; |
| 1383 | garray_T *stack = &cctx->ctx_type_stack; |
| 1384 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1385 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1386 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1387 | return FAIL; |
| 1388 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1389 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1390 | |
| 1391 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1392 | if (ga_grow(stack, 1) == FAIL) |
| 1393 | return FAIL; |
| 1394 | // add return value |
| 1395 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1396 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1397 | |
| 1398 | return OK; |
| 1399 | } |
| 1400 | |
| 1401 | /* |
| 1402 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1403 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1404 | */ |
| 1405 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1406 | generate_PCALL( |
| 1407 | cctx_T *cctx, |
| 1408 | int argcount, |
| 1409 | char_u *name, |
| 1410 | type_T *type, |
| 1411 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1412 | { |
| 1413 | isn_T *isn; |
| 1414 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1415 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1416 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1417 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1418 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1419 | if (type->tt_type == VAR_ANY) |
| 1420 | ret_type = &t_any; |
| 1421 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1422 | { |
| 1423 | if (type->tt_argcount != -1) |
| 1424 | { |
| 1425 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1426 | |
| 1427 | if (argcount < type->tt_min_argcount - varargs) |
| 1428 | { |
| 1429 | semsg(_(e_toofewarg), "[reference]"); |
| 1430 | return FAIL; |
| 1431 | } |
| 1432 | if (!varargs && argcount > type->tt_argcount) |
| 1433 | { |
| 1434 | semsg(_(e_toomanyarg), "[reference]"); |
| 1435 | return FAIL; |
| 1436 | } |
| 1437 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1438 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1439 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1440 | else |
| 1441 | { |
| 1442 | semsg(_("E1085: Not a callable type: %s"), name); |
| 1443 | return FAIL; |
| 1444 | } |
| 1445 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1446 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1447 | return FAIL; |
| 1448 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1449 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1450 | |
| 1451 | stack->ga_len -= argcount; // drop the arguments |
| 1452 | |
| 1453 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1454 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1455 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1456 | // If partial is above the arguments it must be cleared and replaced with |
| 1457 | // the return value. |
| 1458 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1459 | return FAIL; |
| 1460 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1461 | return OK; |
| 1462 | } |
| 1463 | |
| 1464 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1465 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1466 | */ |
| 1467 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1468 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1469 | { |
| 1470 | isn_T *isn; |
| 1471 | garray_T *stack = &cctx->ctx_type_stack; |
| 1472 | type_T *type; |
| 1473 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1474 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1475 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1476 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1477 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1478 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1479 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1480 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1481 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1482 | { |
| 1483 | emsg(_(e_dictreq)); |
| 1484 | return FAIL; |
| 1485 | } |
| 1486 | // change dict type to dict member type |
| 1487 | if (type->tt_type == VAR_DICT) |
| 1488 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1489 | |
| 1490 | return OK; |
| 1491 | } |
| 1492 | |
| 1493 | /* |
| 1494 | * Generate an ISN_ECHO instruction. |
| 1495 | */ |
| 1496 | static int |
| 1497 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1498 | { |
| 1499 | isn_T *isn; |
| 1500 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1501 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1502 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1503 | return FAIL; |
| 1504 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1505 | isn->isn_arg.echo.echo_count = count; |
| 1506 | |
| 1507 | return OK; |
| 1508 | } |
| 1509 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1510 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1511 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1512 | */ |
| 1513 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1514 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1515 | { |
| 1516 | isn_T *isn; |
| 1517 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1518 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1519 | return FAIL; |
| 1520 | isn->isn_arg.number = count; |
| 1521 | |
| 1522 | return OK; |
| 1523 | } |
| 1524 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1525 | static int |
| 1526 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1527 | { |
| 1528 | isn_T *isn; |
| 1529 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1530 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1531 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1532 | return FAIL; |
| 1533 | isn->isn_arg.string = vim_strsave(line); |
| 1534 | return OK; |
| 1535 | } |
| 1536 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1537 | static int |
| 1538 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1539 | { |
| 1540 | isn_T *isn; |
| 1541 | |
| 1542 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1543 | return FAIL; |
| 1544 | isn->isn_arg.number = count; |
| 1545 | return OK; |
| 1546 | } |
| 1547 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1548 | /* |
| 1549 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1550 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1551 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1552 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1553 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1554 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1555 | lvar_T *lvar; |
| 1556 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1557 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1558 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 1559 | emsg_namelen(_(e_used_as_arg), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1560 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1564 | return NULL; |
| 1565 | 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] | 1566 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1567 | // Every local variable uses the next entry on the stack. We could re-use |
| 1568 | // the last ones when leaving a scope, but then variables used in a closure |
| 1569 | // might get overwritten. To keep things simple do not re-use stack |
| 1570 | // entries. This is less efficient, but memory is cheap these days. |
| 1571 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1572 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1573 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1574 | lvar->lv_const = isConst; |
| 1575 | lvar->lv_type = type; |
| 1576 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1577 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1581 | * Remove local variables above "new_top". |
| 1582 | */ |
| 1583 | static void |
| 1584 | unwind_locals(cctx_T *cctx, int new_top) |
| 1585 | { |
| 1586 | if (cctx->ctx_locals.ga_len > new_top) |
| 1587 | { |
| 1588 | int idx; |
| 1589 | lvar_T *lvar; |
| 1590 | |
| 1591 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1592 | { |
| 1593 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1594 | vim_free(lvar->lv_name); |
| 1595 | } |
| 1596 | } |
| 1597 | cctx->ctx_locals.ga_len = new_top; |
| 1598 | } |
| 1599 | |
| 1600 | /* |
| 1601 | * Free all local variables. |
| 1602 | */ |
| 1603 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1604 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1605 | { |
| 1606 | unwind_locals(cctx, 0); |
| 1607 | ga_clear(&cctx->ctx_locals); |
| 1608 | } |
| 1609 | |
| 1610 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1611 | * Find "name" in script-local items of script "sid". |
| 1612 | * Returns the index in "sn_var_vals" if found. |
| 1613 | * If found but not in "sn_var_vals" returns -1. |
| 1614 | * If not found returns -2. |
| 1615 | */ |
| 1616 | int |
| 1617 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 1618 | { |
| 1619 | hashtab_T *ht; |
| 1620 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1621 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1622 | int idx; |
| 1623 | |
| 1624 | // First look the name up in the hashtable. |
| 1625 | if (sid <= 0 || sid > script_items.ga_len) |
| 1626 | return -1; |
| 1627 | ht = &SCRIPT_VARS(sid); |
| 1628 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 1629 | if (di == NULL) |
| 1630 | return -2; |
| 1631 | |
| 1632 | // Now find the svar_T index in sn_var_vals. |
| 1633 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 1634 | { |
| 1635 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1636 | |
| 1637 | if (sv->sv_tv == &di->di_tv) |
| 1638 | { |
| 1639 | if (check_writable && sv->sv_const) |
| 1640 | semsg(_(e_readonlyvar), name); |
| 1641 | return idx; |
| 1642 | } |
| 1643 | } |
| 1644 | return -1; |
| 1645 | } |
| 1646 | |
| 1647 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 1648 | * Find "name" in imported items of the current script or in "cctx" if not |
| 1649 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1650 | */ |
| 1651 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1652 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1653 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1654 | int idx; |
| 1655 | |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 1656 | if (current_sctx.sc_sid <= 0) |
| 1657 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1658 | if (cctx != NULL) |
| 1659 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1660 | { |
| 1661 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 1662 | + idx; |
| 1663 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1664 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1665 | : STRLEN(import->imp_name) == len |
| 1666 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1667 | return import; |
| 1668 | } |
| 1669 | |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 1670 | return find_imported_in_script(name, len, current_sctx.sc_sid); |
| 1671 | } |
| 1672 | |
| 1673 | imported_T * |
| 1674 | find_imported_in_script(char_u *name, size_t len, int sid) |
| 1675 | { |
| 1676 | scriptitem_T *si = SCRIPT_ITEM(sid); |
| 1677 | int idx; |
| 1678 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1679 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 1680 | { |
| 1681 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 1682 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1683 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1684 | : STRLEN(import->imp_name) == len |
| 1685 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1686 | return import; |
| 1687 | } |
| 1688 | return NULL; |
| 1689 | } |
| 1690 | |
| 1691 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1692 | * Free all imported variables. |
| 1693 | */ |
| 1694 | static void |
| 1695 | free_imported(cctx_T *cctx) |
| 1696 | { |
| 1697 | int idx; |
| 1698 | |
| 1699 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1700 | { |
| 1701 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 1702 | |
| 1703 | vim_free(import->imp_name); |
| 1704 | } |
| 1705 | ga_clear(&cctx->ctx_imports); |
| 1706 | } |
| 1707 | |
| 1708 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1709 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 1710 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 1711 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1712 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1713 | { |
| 1714 | return p[0] == '#' && p[1] != '{'; |
| 1715 | } |
| 1716 | |
| 1717 | /* |
| 1718 | * Return a pointer to the next line that isn't empty or only contains a |
| 1719 | * comment. Skips over white space. |
| 1720 | * Returns NULL if there is none. |
| 1721 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1722 | char_u * |
| 1723 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1724 | { |
| 1725 | int lnum = cctx->ctx_lnum; |
| 1726 | |
| 1727 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 1728 | { |
| 1729 | 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] | 1730 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1731 | |
Bram Moolenaar | acd4c5e | 2020-06-22 19:39:03 +0200 | [diff] [blame] | 1732 | if (line == NULL) |
| 1733 | break; |
| 1734 | p = skipwhite(line); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1735 | if (*p != NUL && !vim9_comment_start(p)) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1736 | return p; |
| 1737 | } |
| 1738 | return NULL; |
| 1739 | } |
| 1740 | |
| 1741 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1742 | * Called when checking for a following operator at "arg". When the rest of |
| 1743 | * the line is empty or only a comment, peek the next line. If there is a next |
| 1744 | * line return a pointer to it and set "nextp". |
| 1745 | * Otherwise skip over white space. |
| 1746 | */ |
| 1747 | static char_u * |
| 1748 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 1749 | { |
| 1750 | char_u *p = skipwhite(arg); |
| 1751 | |
| 1752 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1753 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1754 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1755 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1756 | if (*nextp != NULL) |
| 1757 | return *nextp; |
| 1758 | } |
| 1759 | return p; |
| 1760 | } |
| 1761 | |
| 1762 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1763 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1764 | * 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] | 1765 | * Returns NULL when at the end. |
| 1766 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1767 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1768 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1769 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1770 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1771 | |
| 1772 | do |
| 1773 | { |
| 1774 | ++cctx->ctx_lnum; |
| 1775 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1776 | { |
| 1777 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1778 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1779 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1780 | 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] | 1781 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 1782 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1783 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 1784 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1785 | return line; |
| 1786 | } |
| 1787 | |
| 1788 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1789 | * 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] | 1790 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1791 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 1792 | */ |
| 1793 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 1794 | 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] | 1795 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1796 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1797 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1798 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1799 | |
| 1800 | if (next == NULL) |
| 1801 | return FAIL; |
| 1802 | *arg = skipwhite(next); |
| 1803 | } |
| 1804 | return OK; |
| 1805 | } |
| 1806 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 1807 | /* |
| 1808 | * Idem, and give an error when failed. |
| 1809 | */ |
| 1810 | static int |
| 1811 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 1812 | { |
| 1813 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 1814 | { |
| 1815 | emsg(_("E1097: line incomplete")); |
| 1816 | return FAIL; |
| 1817 | } |
| 1818 | return OK; |
| 1819 | } |
| 1820 | |
| 1821 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1822 | // Structure passed between the compile_expr* functions to keep track of |
| 1823 | // constants that have been parsed but for which no code was produced yet. If |
| 1824 | // possible expressions on these constants are applied at compile time. If |
| 1825 | // that is not possible, the code to push the constants needs to be generated |
| 1826 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1827 | // Using 50 should be more than enough of 5 levels of (). |
| 1828 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1829 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1830 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1831 | int pp_used; // active entries in pp_tv[] |
| 1832 | } ppconst_T; |
| 1833 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1834 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 1835 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 1836 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1837 | /* |
| 1838 | * Generate a PUSH instruction for "tv". |
| 1839 | * "tv" will be consumed or cleared. |
| 1840 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 1841 | */ |
| 1842 | static int |
| 1843 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 1844 | { |
| 1845 | if (tv != NULL) |
| 1846 | { |
| 1847 | switch (tv->v_type) |
| 1848 | { |
| 1849 | case VAR_UNKNOWN: |
| 1850 | break; |
| 1851 | case VAR_BOOL: |
| 1852 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 1853 | break; |
| 1854 | case VAR_SPECIAL: |
| 1855 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 1856 | break; |
| 1857 | case VAR_NUMBER: |
| 1858 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 1859 | break; |
| 1860 | #ifdef FEAT_FLOAT |
| 1861 | case VAR_FLOAT: |
| 1862 | generate_PUSHF(cctx, tv->vval.v_float); |
| 1863 | break; |
| 1864 | #endif |
| 1865 | case VAR_BLOB: |
| 1866 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 1867 | tv->vval.v_blob = NULL; |
| 1868 | break; |
| 1869 | case VAR_STRING: |
| 1870 | generate_PUSHS(cctx, tv->vval.v_string); |
| 1871 | tv->vval.v_string = NULL; |
| 1872 | break; |
| 1873 | default: |
| 1874 | iemsg("constant type not supported"); |
| 1875 | clear_tv(tv); |
| 1876 | return FAIL; |
| 1877 | } |
| 1878 | tv->v_type = VAR_UNKNOWN; |
| 1879 | } |
| 1880 | return OK; |
| 1881 | } |
| 1882 | |
| 1883 | /* |
| 1884 | * Generate code for any ppconst entries. |
| 1885 | */ |
| 1886 | static int |
| 1887 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 1888 | { |
| 1889 | int i; |
| 1890 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1891 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1892 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 1893 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1894 | for (i = 0; i < ppconst->pp_used; ++i) |
| 1895 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 1896 | ret = FAIL; |
| 1897 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1898 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1899 | return ret; |
| 1900 | } |
| 1901 | |
| 1902 | /* |
| 1903 | * Clear ppconst constants. Used when failing. |
| 1904 | */ |
| 1905 | static void |
| 1906 | clear_ppconst(ppconst_T *ppconst) |
| 1907 | { |
| 1908 | int i; |
| 1909 | |
| 1910 | for (i = 0; i < ppconst->pp_used; ++i) |
| 1911 | clear_tv(&ppconst->pp_tv[i]); |
| 1912 | ppconst->pp_used = 0; |
| 1913 | } |
| 1914 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1915 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1916 | * Generate an instruction to load script-local variable "name", without the |
| 1917 | * leading "s:". |
| 1918 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1919 | */ |
| 1920 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1921 | compile_load_scriptvar( |
| 1922 | cctx_T *cctx, |
| 1923 | char_u *name, // variable NUL terminated |
| 1924 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 1925 | char_u **end, // end of variable |
| 1926 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1927 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1928 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1929 | int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
| 1930 | imported_T *import; |
| 1931 | |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 1932 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1933 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 1934 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1935 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 1936 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1937 | } |
| 1938 | if (idx >= 0) |
| 1939 | { |
| 1940 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1941 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1942 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1943 | current_sctx.sc_sid, idx, sv->sv_type); |
| 1944 | return OK; |
| 1945 | } |
| 1946 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1947 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1948 | if (import != NULL) |
| 1949 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1950 | if (import->imp_all) |
| 1951 | { |
| 1952 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 1953 | char_u *exp_name; |
| 1954 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1955 | ufunc_T *ufunc; |
| 1956 | type_T *type; |
| 1957 | |
| 1958 | // Used "import * as Name", need to lookup the member. |
| 1959 | if (*p != '.') |
| 1960 | { |
| 1961 | semsg(_("E1060: expected dot after name: %s"), start); |
| 1962 | return FAIL; |
| 1963 | } |
| 1964 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 1965 | if (VIM_ISWHITE(*p)) |
| 1966 | { |
| 1967 | emsg(_("E1074: no white space allowed after dot")); |
| 1968 | return FAIL; |
| 1969 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1970 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 1971 | // isolate one name |
| 1972 | exp_name = p; |
| 1973 | while (eval_isnamec(*p)) |
| 1974 | ++p; |
| 1975 | cc = *p; |
| 1976 | *p = NUL; |
| 1977 | |
| 1978 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 1979 | *p = cc; |
| 1980 | p = skipwhite(p); |
| 1981 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1982 | // TODO: what if it is a function? |
| 1983 | if (idx < 0) |
| 1984 | return FAIL; |
| 1985 | *end = p; |
| 1986 | |
| 1987 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 1988 | import->imp_sid, |
| 1989 | idx, |
| 1990 | type); |
| 1991 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 1992 | else if (import->imp_funcname != NULL) |
| 1993 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1994 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 1995 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 1996 | import->imp_sid, |
| 1997 | import->imp_var_vals_idx, |
| 1998 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1999 | return OK; |
| 2000 | } |
| 2001 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2002 | if (error) |
| 2003 | semsg(_("E1050: Item not found: %s"), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2004 | return FAIL; |
| 2005 | } |
| 2006 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2007 | static int |
| 2008 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2009 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2010 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2011 | |
| 2012 | if (ufunc == NULL) |
| 2013 | return FAIL; |
| 2014 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2015 | // Need to compile any default values to get the argument types. |
| 2016 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2017 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2018 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2019 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2022 | /* |
| 2023 | * Compile a variable name into a load instruction. |
| 2024 | * "end" points to just after the name. |
| 2025 | * When "error" is FALSE do not give an error when not found. |
| 2026 | */ |
| 2027 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2028 | 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] | 2029 | { |
| 2030 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2031 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2032 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2033 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2034 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2035 | |
| 2036 | if (*(*arg + 1) == ':') |
| 2037 | { |
| 2038 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2039 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2040 | { |
| 2041 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2042 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2043 | switch (**arg) |
| 2044 | { |
| 2045 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2046 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2047 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2048 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2049 | default: |
| 2050 | semsg(_(e_namespace), *arg); |
| 2051 | goto theend; |
| 2052 | } |
| 2053 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2054 | goto theend; |
| 2055 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2056 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2057 | else |
| 2058 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2059 | isntype_T isn_type = ISN_DROP; |
| 2060 | |
| 2061 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2062 | if (name == NULL) |
| 2063 | return FAIL; |
| 2064 | |
| 2065 | switch (**arg) |
| 2066 | { |
| 2067 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2068 | break; |
| 2069 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2070 | NULL, NULL, error); |
| 2071 | break; |
| 2072 | case 'g': isn_type = ISN_LOADG; break; |
| 2073 | case 'w': isn_type = ISN_LOADW; break; |
| 2074 | case 't': isn_type = ISN_LOADT; break; |
| 2075 | case 'b': isn_type = ISN_LOADB; break; |
| 2076 | default: semsg(_(e_namespace), *arg); |
| 2077 | goto theend; |
| 2078 | } |
| 2079 | if (isn_type != ISN_DROP) |
| 2080 | { |
| 2081 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2082 | // variables can be defined later, thus we don't check if it |
| 2083 | // exists, give error at runtime. |
| 2084 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2085 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | else |
| 2089 | { |
| 2090 | size_t len = end - *arg; |
| 2091 | int idx; |
| 2092 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2093 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2094 | |
| 2095 | name = vim_strnsave(*arg, end - *arg); |
| 2096 | if (name == NULL) |
| 2097 | return FAIL; |
| 2098 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2099 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2100 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2101 | if (!gen_load_outer) |
| 2102 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2103 | } |
| 2104 | else |
| 2105 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2106 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2107 | |
| 2108 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2109 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2110 | type = lvar->lv_type; |
| 2111 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2112 | if (lvar->lv_from_outer) |
| 2113 | gen_load_outer = TRUE; |
| 2114 | else |
| 2115 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2116 | } |
| 2117 | else |
| 2118 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2119 | // "var" can be script-local even without using "s:" if it |
| 2120 | // already exists. |
| 2121 | if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version |
| 2122 | == SCRIPT_VERSION_VIM9 |
| 2123 | || lookup_script(*arg, len) == OK) |
| 2124 | res = compile_load_scriptvar(cctx, name, *arg, &end, |
| 2125 | FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2126 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2127 | // When the name starts with an uppercase letter or "x:" it |
| 2128 | // can be a user defined function. |
| 2129 | if (res == FAIL && (ASCII_ISUPPER(*name) || name[1] == ':')) |
| 2130 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2131 | } |
| 2132 | } |
| 2133 | if (gen_load) |
| 2134 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2135 | if (gen_load_outer) |
| 2136 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | *arg = end; |
| 2140 | |
| 2141 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2142 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2143 | semsg(_(e_var_notfound), name); |
| 2144 | vim_free(name); |
| 2145 | return res; |
| 2146 | } |
| 2147 | |
| 2148 | /* |
| 2149 | * Compile the argument expressions. |
| 2150 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2151 | */ |
| 2152 | static int |
| 2153 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2154 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2155 | char_u *p = *arg; |
| 2156 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2157 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2158 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2159 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2160 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2161 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2162 | if (*p == ')') |
| 2163 | { |
| 2164 | *arg = p + 1; |
| 2165 | return OK; |
| 2166 | } |
| 2167 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2168 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2169 | return FAIL; |
| 2170 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2171 | |
| 2172 | if (*p != ',' && *skipwhite(p) == ',') |
| 2173 | { |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2174 | semsg(_(e_no_white_before), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2175 | p = skipwhite(p); |
| 2176 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2177 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2178 | { |
| 2179 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2180 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | d77a852 | 2020-04-03 21:59:57 +0200 | [diff] [blame] | 2181 | semsg(_(e_white_after), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2182 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2183 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2184 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2185 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2186 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2187 | emsg(_(e_missing_close)); |
| 2188 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | /* |
| 2192 | * Compile a function call: name(arg1, arg2) |
| 2193 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2194 | * "argcount_init" is 1 for "value->method()" |
| 2195 | * Instructions: |
| 2196 | * EVAL arg1 |
| 2197 | * EVAL arg2 |
| 2198 | * BCALL / DCALL / UCALL |
| 2199 | */ |
| 2200 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2201 | compile_call( |
| 2202 | char_u **arg, |
| 2203 | size_t varlen, |
| 2204 | cctx_T *cctx, |
| 2205 | ppconst_T *ppconst, |
| 2206 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2207 | { |
| 2208 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2209 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2210 | int argcount = argcount_init; |
| 2211 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2212 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2213 | char_u *tofree = NULL; |
| 2214 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2215 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2216 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2217 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2218 | // we can evaluate "has('name')" at compile time |
| 2219 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2220 | { |
| 2221 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2222 | typval_T argvars[2]; |
| 2223 | |
| 2224 | argvars[0].v_type = VAR_UNKNOWN; |
| 2225 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2226 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2227 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2228 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2229 | s = skipwhite(s); |
| 2230 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 2231 | { |
| 2232 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2233 | |
| 2234 | *arg = s + 1; |
| 2235 | argvars[1].v_type = VAR_UNKNOWN; |
| 2236 | tv->v_type = VAR_NUMBER; |
| 2237 | tv->vval.v_number = 0; |
| 2238 | f_has(argvars, tv); |
| 2239 | clear_tv(&argvars[0]); |
| 2240 | ++ppconst->pp_used; |
| 2241 | return OK; |
| 2242 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2243 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2247 | return FAIL; |
| 2248 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2249 | if (varlen >= sizeof(namebuf)) |
| 2250 | { |
| 2251 | semsg(_("E1011: name too long: %s"), name); |
| 2252 | return FAIL; |
| 2253 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2254 | vim_strncpy(namebuf, *arg, varlen); |
| 2255 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2256 | |
| 2257 | *arg = skipwhite(*arg + varlen + 1); |
| 2258 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2259 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2260 | |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2261 | if (ASCII_ISLOWER(*name) && name[1] != ':') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2262 | { |
| 2263 | int idx; |
| 2264 | |
| 2265 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2266 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2267 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 2268 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2269 | else |
| 2270 | semsg(_(e_unknownfunc), namebuf); |
| 2271 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2272 | } |
| 2273 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2274 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2275 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2276 | if (ufunc != NULL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2277 | { |
| 2278 | res = generate_CALL(cctx, ufunc, argcount); |
| 2279 | goto theend; |
| 2280 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2281 | |
| 2282 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2283 | // 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] | 2284 | p = namebuf; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2285 | if (STRNCMP(namebuf, "g:", 2) != 0 |
| 2286 | && compile_load(&p, namebuf + varlen, cctx, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2287 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2288 | garray_T *stack = &cctx->ctx_type_stack; |
| 2289 | type_T *type; |
| 2290 | |
| 2291 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2292 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2293 | goto theend; |
| 2294 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2295 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2296 | // 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] | 2297 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2298 | if (STRNCMP(namebuf, "g:", 2) == 0) |
| 2299 | res = generate_UCALL(cctx, name, argcount); |
| 2300 | else |
| 2301 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2302 | |
| 2303 | theend: |
| 2304 | vim_free(tofree); |
| 2305 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2309 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2310 | |
| 2311 | /* |
| 2312 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2313 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2314 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2315 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2316 | * valid name. |
| 2317 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2318 | static char_u * |
| 2319 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2320 | { |
| 2321 | char_u *p; |
| 2322 | |
| 2323 | // Quick check for valid starting character. |
| 2324 | if (!eval_isnamec1(*arg)) |
| 2325 | return arg; |
| 2326 | |
| 2327 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2328 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2329 | // and can be used in slice "[n:]". |
| 2330 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2331 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2332 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2333 | break; |
| 2334 | return p; |
| 2335 | } |
| 2336 | |
| 2337 | /* |
| 2338 | * 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] | 2339 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2340 | */ |
| 2341 | char_u * |
| 2342 | to_name_const_end(char_u *arg) |
| 2343 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2344 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2345 | typval_T rettv; |
| 2346 | |
| 2347 | if (p == arg && *arg == '[') |
| 2348 | { |
| 2349 | |
| 2350 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2351 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2352 | p = arg; |
| 2353 | } |
| 2354 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 2355 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2356 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2357 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 2358 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2359 | p = arg; |
| 2360 | } |
| 2361 | else if (p == arg && *arg == '{') |
| 2362 | { |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 2363 | int ret = get_lambda_tv(&p, &rettv, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2364 | |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2365 | // Can be "{x -> ret}()". |
| 2366 | // Can be "{'a': 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2367 | if (ret == NOTDONE) |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 2368 | ret = eval_dict(&p, &rettv, NULL, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2369 | if (ret != OK) |
| 2370 | p = arg; |
| 2371 | } |
| 2372 | |
| 2373 | return p; |
| 2374 | } |
| 2375 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2376 | /* |
| 2377 | * parse a list: [expr, expr] |
| 2378 | * "*arg" points to the '['. |
| 2379 | */ |
| 2380 | static int |
| 2381 | compile_list(char_u **arg, cctx_T *cctx) |
| 2382 | { |
| 2383 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2384 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2385 | int count = 0; |
| 2386 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2387 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2388 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2389 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2390 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2391 | semsg(_(e_list_end), *arg); |
| 2392 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2393 | } |
| 2394 | if (*p == ']') |
| 2395 | { |
| 2396 | ++p; |
| 2397 | // Allow for following comment, after at least one space. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2398 | if (VIM_ISWHITE(*p) && *skipwhite(p) == '#') |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2399 | p += STRLEN(p); |
| 2400 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2401 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2402 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2403 | break; |
| 2404 | ++count; |
| 2405 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2406 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2407 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2408 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 2409 | { |
| 2410 | semsg(_(e_white_after), ","); |
| 2411 | return FAIL; |
| 2412 | } |
| 2413 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2414 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2415 | p = skipwhite(p); |
| 2416 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2417 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2418 | |
| 2419 | generate_NEWLIST(cctx, count); |
| 2420 | return OK; |
| 2421 | } |
| 2422 | |
| 2423 | /* |
| 2424 | * parse a lambda: {arg, arg -> expr} |
| 2425 | * "*arg" points to the '{'. |
| 2426 | */ |
| 2427 | static int |
| 2428 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 2429 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2430 | typval_T rettv; |
| 2431 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2432 | evalarg_T evalarg; |
| 2433 | |
| 2434 | CLEAR_FIELD(evalarg); |
| 2435 | evalarg.eval_flags = EVAL_EVALUATE; |
| 2436 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2437 | |
| 2438 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2439 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2440 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2441 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2442 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2443 | ++ufunc->uf_refcount; |
| 2444 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2445 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2446 | |
| 2447 | // The function will have one line: "return {expr}". |
| 2448 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2449 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2450 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 2451 | clear_evalarg(&evalarg, NULL); |
| 2452 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 2453 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 2454 | { |
| 2455 | // The return type will now be known. |
| 2456 | set_function_type(ufunc); |
| 2457 | |
| 2458 | return generate_FUNCREF(cctx, ufunc); |
| 2459 | } |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2460 | |
| 2461 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2462 | return FAIL; |
| 2463 | } |
| 2464 | |
| 2465 | /* |
| 2466 | * Compile a lamda call: expr->{lambda}(args) |
| 2467 | * "arg" points to the "{". |
| 2468 | */ |
| 2469 | static int |
| 2470 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 2471 | { |
| 2472 | ufunc_T *ufunc; |
| 2473 | typval_T rettv; |
| 2474 | int argcount = 1; |
| 2475 | int ret = FAIL; |
| 2476 | |
| 2477 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 2478 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2479 | return FAIL; |
| 2480 | |
| 2481 | if (**arg != '(') |
| 2482 | { |
| 2483 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 2484 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2485 | else |
| 2486 | semsg(_(e_missing_paren), "lambda"); |
| 2487 | clear_tv(&rettv); |
| 2488 | return FAIL; |
| 2489 | } |
| 2490 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2491 | ufunc = rettv.vval.v_partial->pt_func; |
| 2492 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2493 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2494 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2495 | |
| 2496 | // The function will have one line: "return {expr}". |
| 2497 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2498 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2499 | |
| 2500 | // compile the arguments |
| 2501 | *arg = skipwhite(*arg + 1); |
| 2502 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 2503 | // call the compiled function |
| 2504 | ret = generate_CALL(cctx, ufunc, argcount); |
| 2505 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2506 | if (ret == FAIL) |
| 2507 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2508 | return ret; |
| 2509 | } |
| 2510 | |
| 2511 | /* |
| 2512 | * parse a dict: {'key': val} or #{key: val} |
| 2513 | * "*arg" points to the '{'. |
| 2514 | */ |
| 2515 | static int |
| 2516 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 2517 | { |
| 2518 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2519 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2520 | int count = 0; |
| 2521 | dict_T *d = dict_alloc(); |
| 2522 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2523 | char_u *whitep = *arg; |
| 2524 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2525 | |
| 2526 | if (d == NULL) |
| 2527 | return FAIL; |
| 2528 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2529 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2530 | { |
| 2531 | char_u *key = NULL; |
| 2532 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2533 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2534 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2535 | *arg = NULL; |
| 2536 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | if (**arg == '}') |
| 2540 | break; |
| 2541 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2542 | if (literal) |
| 2543 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2544 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2545 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2546 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2547 | { |
| 2548 | semsg(_("E1014: Invalid key: %s"), *arg); |
| 2549 | return FAIL; |
| 2550 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2551 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2552 | if (generate_PUSHS(cctx, key) == FAIL) |
| 2553 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2554 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2555 | } |
| 2556 | else |
| 2557 | { |
| 2558 | isn_T *isn; |
| 2559 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2560 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2561 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2562 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 2563 | if (isn->isn_type == ISN_PUSHS) |
| 2564 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2565 | else |
| 2566 | { |
| 2567 | type_T *keytype = ((type_T **)stack->ga_data) |
| 2568 | [stack->ga_len - 1]; |
| 2569 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 2570 | return FAIL; |
| 2571 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | // Check for duplicate keys, if using string keys. |
| 2575 | if (key != NULL) |
| 2576 | { |
| 2577 | item = dict_find(d, key, -1); |
| 2578 | if (item != NULL) |
| 2579 | { |
| 2580 | semsg(_(e_duplicate_key), key); |
| 2581 | goto failret; |
| 2582 | } |
| 2583 | item = dictitem_alloc(key); |
| 2584 | if (item != NULL) |
| 2585 | { |
| 2586 | item->di_tv.v_type = VAR_UNKNOWN; |
| 2587 | item->di_tv.v_lock = 0; |
| 2588 | if (dict_add(d, item) == FAIL) |
| 2589 | dictitem_free(item); |
| 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | *arg = skipwhite(*arg); |
| 2594 | if (**arg != ':') |
| 2595 | { |
| 2596 | semsg(_(e_missing_dict_colon), *arg); |
| 2597 | return FAIL; |
| 2598 | } |
| 2599 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2600 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2601 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2602 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2603 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2604 | *arg = NULL; |
| 2605 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2606 | } |
| 2607 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2608 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2609 | return FAIL; |
| 2610 | ++count; |
| 2611 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2612 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2613 | *arg = skipwhite(*arg); |
| 2614 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2615 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2616 | *arg = NULL; |
| 2617 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2618 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2619 | if (**arg == '}') |
| 2620 | break; |
| 2621 | if (**arg != ',') |
| 2622 | { |
| 2623 | semsg(_(e_missing_dict_comma), *arg); |
| 2624 | goto failret; |
| 2625 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2626 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2627 | *arg = skipwhite(*arg + 1); |
| 2628 | } |
| 2629 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2630 | *arg = *arg + 1; |
| 2631 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2632 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2633 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2634 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2635 | *arg += STRLEN(*arg); |
| 2636 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2637 | dict_unref(d); |
| 2638 | return generate_NEWDICT(cctx, count); |
| 2639 | |
| 2640 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2641 | if (*arg == NULL) |
| 2642 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2643 | dict_unref(d); |
| 2644 | return FAIL; |
| 2645 | } |
| 2646 | |
| 2647 | /* |
| 2648 | * Compile "&option". |
| 2649 | */ |
| 2650 | static int |
| 2651 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 2652 | { |
| 2653 | typval_T rettv; |
| 2654 | char_u *start = *arg; |
| 2655 | int ret; |
| 2656 | |
| 2657 | // parse the option and get the current value to get the type. |
| 2658 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2659 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2660 | if (ret == OK) |
| 2661 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2662 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2663 | char_u *name = vim_strnsave(start, *arg - start); |
| 2664 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 2665 | |
| 2666 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 2667 | vim_free(name); |
| 2668 | } |
| 2669 | clear_tv(&rettv); |
| 2670 | |
| 2671 | return ret; |
| 2672 | } |
| 2673 | |
| 2674 | /* |
| 2675 | * Compile "$VAR". |
| 2676 | */ |
| 2677 | static int |
| 2678 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 2679 | { |
| 2680 | char_u *start = *arg; |
| 2681 | int len; |
| 2682 | int ret; |
| 2683 | char_u *name; |
| 2684 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2685 | ++*arg; |
| 2686 | len = get_env_len(arg); |
| 2687 | if (len == 0) |
| 2688 | { |
| 2689 | semsg(_(e_syntax_at), start - 1); |
| 2690 | return FAIL; |
| 2691 | } |
| 2692 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2693 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2694 | name = vim_strnsave(start, len + 1); |
| 2695 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 2696 | vim_free(name); |
| 2697 | return ret; |
| 2698 | } |
| 2699 | |
| 2700 | /* |
| 2701 | * Compile "@r". |
| 2702 | */ |
| 2703 | static int |
| 2704 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 2705 | { |
| 2706 | int ret; |
| 2707 | |
| 2708 | ++*arg; |
| 2709 | if (**arg == NUL) |
| 2710 | { |
| 2711 | semsg(_(e_syntax_at), *arg - 1); |
| 2712 | return FAIL; |
| 2713 | } |
Bram Moolenaar | 7226e5b | 2020-08-02 17:33:26 +0200 | [diff] [blame] | 2714 | if (!valid_yank_reg(**arg, FALSE)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2715 | { |
| 2716 | emsg_invreg(**arg); |
| 2717 | return FAIL; |
| 2718 | } |
| 2719 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 2720 | ++*arg; |
| 2721 | return ret; |
| 2722 | } |
| 2723 | |
| 2724 | /* |
| 2725 | * Apply leading '!', '-' and '+' to constant "rettv". |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2726 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2727 | */ |
| 2728 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2729 | apply_leader(typval_T *rettv, int numeric_only, char_u *start, char_u **end) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2730 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2731 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2732 | |
| 2733 | // this works from end to start |
| 2734 | while (p > start) |
| 2735 | { |
| 2736 | --p; |
| 2737 | if (*p == '-' || *p == '+') |
| 2738 | { |
| 2739 | // only '-' has an effect, for '+' we only check the type |
| 2740 | #ifdef FEAT_FLOAT |
| 2741 | if (rettv->v_type == VAR_FLOAT) |
| 2742 | { |
| 2743 | if (*p == '-') |
| 2744 | rettv->vval.v_float = -rettv->vval.v_float; |
| 2745 | } |
| 2746 | else |
| 2747 | #endif |
| 2748 | { |
| 2749 | varnumber_T val; |
| 2750 | int error = FALSE; |
| 2751 | |
| 2752 | // tv_get_number_chk() accepts a string, but we don't want that |
| 2753 | // here |
| 2754 | if (check_not_string(rettv) == FAIL) |
| 2755 | return FAIL; |
| 2756 | val = tv_get_number_chk(rettv, &error); |
| 2757 | clear_tv(rettv); |
| 2758 | if (error) |
| 2759 | return FAIL; |
| 2760 | if (*p == '-') |
| 2761 | val = -val; |
| 2762 | rettv->v_type = VAR_NUMBER; |
| 2763 | rettv->vval.v_number = val; |
| 2764 | } |
| 2765 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2766 | else if (numeric_only) |
| 2767 | { |
| 2768 | ++p; |
| 2769 | break; |
| 2770 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2771 | else |
| 2772 | { |
| 2773 | int v = tv2bool(rettv); |
| 2774 | |
| 2775 | // '!' is permissive in the type. |
| 2776 | clear_tv(rettv); |
| 2777 | rettv->v_type = VAR_BOOL; |
| 2778 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 2779 | } |
| 2780 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2781 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2782 | return OK; |
| 2783 | } |
| 2784 | |
| 2785 | /* |
| 2786 | * Recognize v: variables that are constants and set "rettv". |
| 2787 | */ |
| 2788 | static void |
| 2789 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 2790 | { |
| 2791 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 2792 | { |
| 2793 | rettv->v_type = VAR_BOOL; |
| 2794 | rettv->vval.v_number = VVAL_TRUE; |
| 2795 | *arg += 6; |
| 2796 | } |
| 2797 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 2798 | { |
| 2799 | rettv->v_type = VAR_BOOL; |
| 2800 | rettv->vval.v_number = VVAL_FALSE; |
| 2801 | *arg += 7; |
| 2802 | } |
| 2803 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 2804 | { |
| 2805 | rettv->v_type = VAR_SPECIAL; |
| 2806 | rettv->vval.v_number = VVAL_NULL; |
| 2807 | *arg += 6; |
| 2808 | } |
| 2809 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 2810 | { |
| 2811 | rettv->v_type = VAR_SPECIAL; |
| 2812 | rettv->vval.v_number = VVAL_NONE; |
| 2813 | *arg += 6; |
| 2814 | } |
| 2815 | } |
| 2816 | |
Bram Moolenaar | 696ba23 | 2020-07-29 21:20:41 +0200 | [diff] [blame] | 2817 | exptype_T |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 2818 | get_compare_type(char_u *p, int *len, int *type_is) |
| 2819 | { |
| 2820 | exptype_T type = EXPR_UNKNOWN; |
| 2821 | int i; |
| 2822 | |
| 2823 | switch (p[0]) |
| 2824 | { |
| 2825 | case '=': if (p[1] == '=') |
| 2826 | type = EXPR_EQUAL; |
| 2827 | else if (p[1] == '~') |
| 2828 | type = EXPR_MATCH; |
| 2829 | break; |
| 2830 | case '!': if (p[1] == '=') |
| 2831 | type = EXPR_NEQUAL; |
| 2832 | else if (p[1] == '~') |
| 2833 | type = EXPR_NOMATCH; |
| 2834 | break; |
| 2835 | case '>': if (p[1] != '=') |
| 2836 | { |
| 2837 | type = EXPR_GREATER; |
| 2838 | *len = 1; |
| 2839 | } |
| 2840 | else |
| 2841 | type = EXPR_GEQUAL; |
| 2842 | break; |
| 2843 | case '<': if (p[1] != '=') |
| 2844 | { |
| 2845 | type = EXPR_SMALLER; |
| 2846 | *len = 1; |
| 2847 | } |
| 2848 | else |
| 2849 | type = EXPR_SEQUAL; |
| 2850 | break; |
| 2851 | case 'i': if (p[1] == 's') |
| 2852 | { |
| 2853 | // "is" and "isnot"; but not a prefix of a name |
| 2854 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 2855 | *len = 5; |
| 2856 | i = p[*len]; |
| 2857 | if (!isalnum(i) && i != '_') |
| 2858 | { |
| 2859 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 2860 | *type_is = TRUE; |
| 2861 | } |
| 2862 | } |
| 2863 | break; |
| 2864 | } |
| 2865 | return type; |
| 2866 | } |
| 2867 | |
| 2868 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2869 | * Compile code to apply '-', '+' and '!'. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2870 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2871 | */ |
| 2872 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2873 | compile_leader(cctx_T *cctx, int numeric_only, char_u *start, char_u **end) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2874 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2875 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2876 | |
| 2877 | // this works from end to start |
| 2878 | while (p > start) |
| 2879 | { |
| 2880 | --p; |
| 2881 | if (*p == '-' || *p == '+') |
| 2882 | { |
| 2883 | int negate = *p == '-'; |
| 2884 | isn_T *isn; |
| 2885 | |
| 2886 | // TODO: check type |
| 2887 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 2888 | { |
| 2889 | --p; |
| 2890 | if (*p == '-') |
| 2891 | negate = !negate; |
| 2892 | } |
| 2893 | // only '-' has an effect, for '+' we only check the type |
| 2894 | if (negate) |
| 2895 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 2896 | else |
| 2897 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 2898 | if (isn == NULL) |
| 2899 | return FAIL; |
| 2900 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2901 | else if (numeric_only) |
| 2902 | { |
| 2903 | ++p; |
| 2904 | break; |
| 2905 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2906 | else |
| 2907 | { |
| 2908 | int invert = TRUE; |
| 2909 | |
| 2910 | while (p > start && p[-1] == '!') |
| 2911 | { |
| 2912 | --p; |
| 2913 | invert = !invert; |
| 2914 | } |
| 2915 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 2916 | return FAIL; |
| 2917 | } |
| 2918 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2919 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2920 | return OK; |
| 2921 | } |
| 2922 | |
| 2923 | /* |
| 2924 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2925 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2926 | */ |
| 2927 | static int |
| 2928 | compile_subscript( |
| 2929 | char_u **arg, |
| 2930 | cctx_T *cctx, |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2931 | char_u *start_leader, |
| 2932 | char_u **end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 2933 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2934 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2935 | char_u *name_start = *end_leader; |
| 2936 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2937 | for (;;) |
| 2938 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2939 | char_u *p = skipwhite(*arg); |
| 2940 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2941 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2942 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2943 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2944 | |
| 2945 | // If a following line starts with "->{" or "->X" advance to that |
| 2946 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 2947 | // Also if a following line starts with ".x". |
| 2948 | if (next != NULL && |
| 2949 | ((next[0] == '-' && next[1] == '>' |
| 2950 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 2951 | || (next[0] == '.' && eval_isdictc(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2952 | { |
| 2953 | next = next_line_from_context(cctx, TRUE); |
| 2954 | if (next == NULL) |
| 2955 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2956 | *arg = next; |
| 2957 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2958 | } |
| 2959 | } |
| 2960 | |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 2961 | // Do not skip over white space to find the "(", "exeucte 'x' ()" is |
| 2962 | // not a function call. |
| 2963 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2964 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2965 | garray_T *stack = &cctx->ctx_type_stack; |
| 2966 | type_T *type; |
| 2967 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2968 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 2969 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 2970 | return FAIL; |
| 2971 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2972 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2973 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2974 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2975 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2976 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 2977 | return FAIL; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2978 | if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2979 | return FAIL; |
| 2980 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2981 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2982 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 2983 | char_u *pstart = p; |
| 2984 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 2985 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 2986 | return FAIL; |
| 2987 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2988 | // something->method() |
| 2989 | // Apply the '!', '-' and '+' first: |
| 2990 | // -1.0->func() works like (-1.0)->func() |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2991 | if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2992 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2993 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2994 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 2995 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 2996 | // No line break supported right after "->". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2997 | if (**arg == '{') |
| 2998 | { |
| 2999 | // lambda call: list->{lambda} |
| 3000 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3001 | return FAIL; |
| 3002 | } |
| 3003 | else |
| 3004 | { |
| 3005 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3006 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3007 | if (!eval_isnamec1(*p)) |
| 3008 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3009 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3010 | return FAIL; |
| 3011 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3012 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3013 | p += 2; |
Bram Moolenaar | c5da1fb | 2020-08-05 15:43:44 +0200 | [diff] [blame] | 3014 | for ( ; eval_isnamec(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3015 | ; |
| 3016 | if (*p != '(') |
| 3017 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3018 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3019 | return FAIL; |
| 3020 | } |
| 3021 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3022 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3023 | return FAIL; |
| 3024 | } |
| 3025 | } |
Bram Moolenaar | badd848 | 2020-07-31 22:38:17 +0200 | [diff] [blame] | 3026 | else if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3027 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3028 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3029 | type_T **typep; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3030 | vartype_T vtype; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3031 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3032 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3033 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3034 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3035 | // TODO: blob index |
| 3036 | // TODO: more arguments |
| 3037 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3038 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3039 | return FAIL; |
| 3040 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3041 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3042 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3043 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3044 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3045 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3046 | return FAIL; |
| 3047 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3048 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3049 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3050 | if (**arg != ']') |
| 3051 | { |
| 3052 | emsg(_(e_missbrac)); |
| 3053 | return FAIL; |
| 3054 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3055 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3056 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3057 | // We can index a list and a dict. If we don't know the type |
| 3058 | // we can use the index value type. |
| 3059 | // TODO: If we don't know use an instruction to figure it out at |
| 3060 | // runtime. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3061 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 2; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3062 | vtype = (*typep)->tt_type; |
| 3063 | if (*typep == &t_any) |
| 3064 | { |
| 3065 | type_T *valtype = ((type_T **)stack->ga_data) |
| 3066 | [stack->ga_len - 1]; |
| 3067 | if (valtype == &t_string) |
| 3068 | vtype = VAR_DICT; |
| 3069 | } |
| 3070 | if (vtype == VAR_DICT) |
| 3071 | { |
| 3072 | if ((*typep)->tt_type == VAR_DICT) |
| 3073 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3074 | else |
| 3075 | { |
| 3076 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3077 | return FAIL; |
| 3078 | *typep = &t_any; |
| 3079 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3080 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3081 | return FAIL; |
| 3082 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3083 | return FAIL; |
| 3084 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3085 | else if (vtype == VAR_STRING) |
| 3086 | { |
| 3087 | *typep = &t_number; |
| 3088 | if (generate_instr_drop(cctx, ISN_STRINDEX, 1) == FAIL) |
| 3089 | return FAIL; |
| 3090 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3091 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3092 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3093 | if ((*typep)->tt_type == VAR_LIST) |
| 3094 | *typep = (*typep)->tt_member; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3095 | if (generate_instr_drop(cctx, ISN_LISTINDEX, 1) == FAIL) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3096 | return FAIL; |
| 3097 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3098 | else |
| 3099 | { |
| 3100 | emsg(_(e_listdictblobreq)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3101 | return FAIL; |
| 3102 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3103 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3104 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3105 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3106 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3107 | return FAIL; |
| 3108 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3109 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3110 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3111 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3112 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3113 | p = *arg; |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3114 | if (eval_isdictc(*p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3115 | while (eval_isnamec(*p)) |
| 3116 | MB_PTR_ADV(p); |
| 3117 | if (p == *arg) |
| 3118 | { |
| 3119 | semsg(_(e_syntax_at), *arg); |
| 3120 | return FAIL; |
| 3121 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3122 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3123 | return FAIL; |
| 3124 | *arg = p; |
| 3125 | } |
| 3126 | else |
| 3127 | break; |
| 3128 | } |
| 3129 | |
| 3130 | // TODO - see handle_subscript(): |
| 3131 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3132 | // Don't do this when "Func" is already a partial that was bound |
| 3133 | // explicitly (pt_auto is FALSE). |
| 3134 | |
| 3135 | return OK; |
| 3136 | } |
| 3137 | |
| 3138 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3139 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3140 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3141 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3142 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3143 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3144 | * |
| 3145 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3146 | */ |
| 3147 | |
| 3148 | /* |
| 3149 | * number number constant |
| 3150 | * 0zFFFFFFFF Blob constant |
| 3151 | * "string" string constant |
| 3152 | * 'string' literal string constant |
| 3153 | * &option-name option value |
| 3154 | * @r register contents |
| 3155 | * identifier variable value |
| 3156 | * function() function call |
| 3157 | * $VAR environment variable |
| 3158 | * (expression) nested expression |
| 3159 | * [expr, expr] List |
| 3160 | * {key: val, key: val} Dictionary |
| 3161 | * #{key: val, key: val} Dictionary with literal keys |
| 3162 | * |
| 3163 | * Also handle: |
| 3164 | * ! in front logical NOT |
| 3165 | * - in front unary minus |
| 3166 | * + in front unary plus (ignored) |
| 3167 | * trailing (arg) funcref/partial call |
| 3168 | * trailing [] subscript in String or List |
| 3169 | * trailing .name entry in Dictionary |
| 3170 | * trailing ->name() method call |
| 3171 | */ |
| 3172 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3173 | compile_expr7( |
| 3174 | char_u **arg, |
| 3175 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3176 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3177 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3178 | char_u *start_leader, *end_leader; |
| 3179 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3180 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3181 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3182 | |
| 3183 | /* |
| 3184 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3185 | */ |
| 3186 | start_leader = *arg; |
| 3187 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3188 | *arg = skipwhite(*arg + 1); |
| 3189 | end_leader = *arg; |
| 3190 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3191 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3192 | switch (**arg) |
| 3193 | { |
| 3194 | /* |
| 3195 | * Number constant. |
| 3196 | */ |
| 3197 | case '0': // also for blob starting with 0z |
| 3198 | case '1': |
| 3199 | case '2': |
| 3200 | case '3': |
| 3201 | case '4': |
| 3202 | case '5': |
| 3203 | case '6': |
| 3204 | case '7': |
| 3205 | case '8': |
| 3206 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3207 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3208 | return FAIL; |
Bram Moolenaar | 4301a72 | 2020-08-11 20:51:08 +0200 | [diff] [blame] | 3209 | // Apply "-" and "+" just before the number now, right to |
| 3210 | // left. Matters especially when "->" follows. Stops at |
| 3211 | // '!'. |
| 3212 | if (apply_leader(rettv, TRUE, |
| 3213 | start_leader, &end_leader) == FAIL) |
| 3214 | { |
| 3215 | clear_tv(rettv); |
| 3216 | return FAIL; |
| 3217 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3218 | break; |
| 3219 | |
| 3220 | /* |
| 3221 | * String constant: "string". |
| 3222 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3223 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3224 | return FAIL; |
| 3225 | break; |
| 3226 | |
| 3227 | /* |
| 3228 | * Literal string constant: 'str''ing'. |
| 3229 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3230 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3231 | return FAIL; |
| 3232 | break; |
| 3233 | |
| 3234 | /* |
| 3235 | * Constant Vim variable. |
| 3236 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3237 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3238 | ret = NOTDONE; |
| 3239 | break; |
| 3240 | |
| 3241 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3242 | * "true" constant |
| 3243 | */ |
| 3244 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3245 | && !eval_isnamec((*arg)[4])) |
| 3246 | { |
| 3247 | *arg += 4; |
| 3248 | rettv->v_type = VAR_BOOL; |
| 3249 | rettv->vval.v_number = VVAL_TRUE; |
| 3250 | } |
| 3251 | else |
| 3252 | ret = NOTDONE; |
| 3253 | break; |
| 3254 | |
| 3255 | /* |
| 3256 | * "false" constant |
| 3257 | */ |
| 3258 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3259 | && !eval_isnamec((*arg)[5])) |
| 3260 | { |
| 3261 | *arg += 5; |
| 3262 | rettv->v_type = VAR_BOOL; |
| 3263 | rettv->vval.v_number = VVAL_FALSE; |
| 3264 | } |
| 3265 | else |
| 3266 | ret = NOTDONE; |
| 3267 | break; |
| 3268 | |
| 3269 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3270 | * List: [expr, expr] |
| 3271 | */ |
| 3272 | case '[': ret = compile_list(arg, cctx); |
| 3273 | break; |
| 3274 | |
| 3275 | /* |
| 3276 | * Dictionary: #{key: val, key: val} |
| 3277 | */ |
| 3278 | case '#': if ((*arg)[1] == '{') |
| 3279 | { |
| 3280 | ++*arg; |
| 3281 | ret = compile_dict(arg, cctx, TRUE); |
| 3282 | } |
| 3283 | else |
| 3284 | ret = NOTDONE; |
| 3285 | break; |
| 3286 | |
| 3287 | /* |
| 3288 | * Lambda: {arg, arg -> expr} |
| 3289 | * Dictionary: {'key': val, 'key': val} |
| 3290 | */ |
| 3291 | case '{': { |
| 3292 | char_u *start = skipwhite(*arg + 1); |
| 3293 | |
| 3294 | // Find out what comes after the arguments. |
| 3295 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3296 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3297 | if (ret != FAIL && *start == '>') |
| 3298 | ret = compile_lambda(arg, cctx); |
| 3299 | else |
| 3300 | ret = compile_dict(arg, cctx, FALSE); |
| 3301 | } |
| 3302 | break; |
| 3303 | |
| 3304 | /* |
| 3305 | * Option value: &name |
| 3306 | */ |
| 3307 | case '&': ret = compile_get_option(arg, cctx); |
| 3308 | break; |
| 3309 | |
| 3310 | /* |
| 3311 | * Environment variable: $VAR. |
| 3312 | */ |
| 3313 | case '$': ret = compile_get_env(arg, cctx); |
| 3314 | break; |
| 3315 | |
| 3316 | /* |
| 3317 | * Register contents: @r. |
| 3318 | */ |
| 3319 | case '@': ret = compile_get_register(arg, cctx); |
| 3320 | break; |
| 3321 | /* |
| 3322 | * nested expression: (expression). |
| 3323 | */ |
| 3324 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3325 | |
| 3326 | // recursive! |
| 3327 | if (ppconst->pp_used <= PPSIZE - 10) |
| 3328 | { |
| 3329 | ret = compile_expr1(arg, cctx, ppconst); |
| 3330 | } |
| 3331 | else |
| 3332 | { |
| 3333 | // Not enough space in ppconst, flush constants. |
| 3334 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3335 | return FAIL; |
| 3336 | ret = compile_expr0(arg, cctx); |
| 3337 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3338 | *arg = skipwhite(*arg); |
| 3339 | if (**arg == ')') |
| 3340 | ++*arg; |
| 3341 | else if (ret == OK) |
| 3342 | { |
| 3343 | emsg(_(e_missing_close)); |
| 3344 | ret = FAIL; |
| 3345 | } |
| 3346 | break; |
| 3347 | |
| 3348 | default: ret = NOTDONE; |
| 3349 | break; |
| 3350 | } |
| 3351 | if (ret == FAIL) |
| 3352 | return FAIL; |
| 3353 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3354 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3355 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3356 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3357 | clear_tv(rettv); |
| 3358 | else |
| 3359 | // A constant expression can possibly be handled compile time, |
| 3360 | // return the value instead of generating code. |
| 3361 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3362 | } |
| 3363 | else if (ret == NOTDONE) |
| 3364 | { |
| 3365 | char_u *p; |
| 3366 | int r; |
| 3367 | |
| 3368 | if (!eval_isnamec1(**arg)) |
| 3369 | { |
| 3370 | semsg(_("E1015: Name expected: %s"), *arg); |
| 3371 | return FAIL; |
| 3372 | } |
| 3373 | |
| 3374 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3375 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3376 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3377 | { |
| 3378 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 3379 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3380 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3381 | { |
| 3382 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3383 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3384 | r = compile_load(arg, p, cctx, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3385 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3386 | if (r == FAIL) |
| 3387 | return FAIL; |
| 3388 | } |
| 3389 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3390 | // Handle following "[]", ".member", etc. |
| 3391 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3392 | if (compile_subscript(arg, cctx, start_leader, &end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3393 | ppconst) == FAIL) |
| 3394 | return FAIL; |
| 3395 | if (ppconst->pp_used > 0) |
| 3396 | { |
| 3397 | // apply the '!', '-' and '+' before the constant |
| 3398 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3399 | if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3400 | return FAIL; |
| 3401 | return OK; |
| 3402 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3403 | if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3404 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3405 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | /* |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3409 | * Give the "white on both sides" error, taking the operator from "p[len]". |
| 3410 | */ |
| 3411 | void |
| 3412 | error_white_both(char_u *op, int len) |
| 3413 | { |
| 3414 | char_u buf[10]; |
| 3415 | |
| 3416 | vim_strncpy(buf, op, len); |
| 3417 | semsg(_(e_white_both), buf); |
| 3418 | } |
| 3419 | |
| 3420 | /* |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3421 | * <type>expr7: runtime type check / conversion |
| 3422 | */ |
| 3423 | static int |
| 3424 | compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3425 | { |
| 3426 | type_T *want_type = NULL; |
| 3427 | |
| 3428 | // Recognize <type> |
| 3429 | if (**arg == '<' && eval_isnamec1((*arg)[1])) |
| 3430 | { |
| 3431 | int called_emsg_before = called_emsg; |
| 3432 | |
| 3433 | ++*arg; |
| 3434 | want_type = parse_type(arg, cctx->ctx_type_list); |
| 3435 | if (called_emsg != called_emsg_before) |
| 3436 | return FAIL; |
| 3437 | |
| 3438 | if (**arg != '>') |
| 3439 | { |
| 3440 | if (*skipwhite(*arg) == '>') |
| 3441 | semsg(_(e_no_white_before), ">"); |
| 3442 | else |
| 3443 | emsg(_("E1104: Missing >")); |
| 3444 | return FAIL; |
| 3445 | } |
| 3446 | ++*arg; |
| 3447 | if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL) |
| 3448 | return FAIL; |
| 3449 | } |
| 3450 | |
| 3451 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
| 3452 | return FAIL; |
| 3453 | |
| 3454 | if (want_type != NULL) |
| 3455 | { |
| 3456 | garray_T *stack = &cctx->ctx_type_stack; |
| 3457 | type_T *actual = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3458 | |
| 3459 | if (check_type(want_type, actual, FALSE) == FAIL) |
| 3460 | { |
| 3461 | generate_ppconst(cctx, ppconst); |
| 3462 | if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL) |
| 3463 | return FAIL; |
| 3464 | } |
| 3465 | } |
| 3466 | |
| 3467 | return OK; |
| 3468 | } |
| 3469 | |
| 3470 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3471 | * * number multiplication |
| 3472 | * / number division |
| 3473 | * % number modulo |
| 3474 | */ |
| 3475 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3476 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3477 | { |
| 3478 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3479 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3480 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3481 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3482 | // get the first expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3483 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3484 | return FAIL; |
| 3485 | |
| 3486 | /* |
| 3487 | * Repeat computing, until no "*", "/" or "%" is following. |
| 3488 | */ |
| 3489 | for (;;) |
| 3490 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3491 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3492 | if (*op != '*' && *op != '/' && *op != '%') |
| 3493 | break; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3494 | if (next != NULL) |
| 3495 | { |
| 3496 | *arg = next_line_from_context(cctx, TRUE); |
| 3497 | op = skipwhite(*arg); |
| 3498 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3499 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3500 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3501 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3502 | error_white_both(op, 1); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3503 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3504 | } |
| 3505 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3506 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3507 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3508 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3509 | // get the second expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3510 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3511 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3512 | |
| 3513 | if (ppconst->pp_used == ppconst_used + 2 |
| 3514 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3515 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3516 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3517 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3518 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3519 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3520 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3521 | // both are numbers: compute the result |
| 3522 | switch (*op) |
| 3523 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3524 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3525 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3526 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3527 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3528 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3529 | break; |
| 3530 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3531 | tv1->vval.v_number = res; |
| 3532 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3533 | } |
| 3534 | else |
| 3535 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3536 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3537 | generate_two_op(cctx, op); |
| 3538 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | return OK; |
| 3542 | } |
| 3543 | |
| 3544 | /* |
| 3545 | * + number addition |
| 3546 | * - number subtraction |
| 3547 | * .. string concatenation |
| 3548 | */ |
| 3549 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3550 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3551 | { |
| 3552 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3553 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3554 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3555 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3556 | |
| 3557 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3558 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3559 | return FAIL; |
| 3560 | |
| 3561 | /* |
| 3562 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3563 | */ |
| 3564 | for (;;) |
| 3565 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3566 | op = may_peek_next_line(cctx, *arg, &next); |
| 3567 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3568 | break; |
| 3569 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3570 | if (next != NULL) |
| 3571 | { |
| 3572 | *arg = next_line_from_context(cctx, TRUE); |
| 3573 | op = skipwhite(*arg); |
| 3574 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3575 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3576 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3577 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3578 | error_white_both(op, oplen); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3579 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3583 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3584 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3585 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3586 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3587 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3588 | return FAIL; |
| 3589 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3590 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3591 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3592 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 3593 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 3594 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3595 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3596 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3597 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3598 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3599 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3600 | // concat/subtract/add constant numbers |
| 3601 | if (*op == '+') |
| 3602 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 3603 | else if (*op == '-') |
| 3604 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 3605 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3606 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3607 | // concatenate constant strings |
| 3608 | char_u *s1 = tv1->vval.v_string; |
| 3609 | char_u *s2 = tv2->vval.v_string; |
| 3610 | size_t len1 = STRLEN(s1); |
| 3611 | |
| 3612 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 3613 | if (tv1->vval.v_string == NULL) |
| 3614 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3615 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3616 | return FAIL; |
| 3617 | } |
| 3618 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 3619 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3620 | vim_free(s1); |
| 3621 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3622 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3623 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3624 | } |
| 3625 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3626 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3627 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3628 | if (*op == '.') |
| 3629 | { |
| 3630 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 3631 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 3632 | return FAIL; |
| 3633 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 3634 | } |
| 3635 | else |
| 3636 | generate_two_op(cctx, op); |
| 3637 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | return OK; |
| 3641 | } |
| 3642 | |
| 3643 | /* |
| 3644 | * expr5a == expr5b |
| 3645 | * expr5a =~ expr5b |
| 3646 | * expr5a != expr5b |
| 3647 | * expr5a !~ expr5b |
| 3648 | * expr5a > expr5b |
| 3649 | * expr5a >= expr5b |
| 3650 | * expr5a < expr5b |
| 3651 | * expr5a <= expr5b |
| 3652 | * expr5a is expr5b |
| 3653 | * expr5a isnot expr5b |
| 3654 | * |
| 3655 | * Produces instructions: |
| 3656 | * EVAL expr5a Push result of "expr5a" |
| 3657 | * EVAL expr5b Push result of "expr5b" |
| 3658 | * COMPARE one of the compare instructions |
| 3659 | */ |
| 3660 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3661 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3662 | { |
| 3663 | exptype_T type = EXPR_UNKNOWN; |
| 3664 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3665 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3666 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3667 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3668 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3669 | |
| 3670 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3671 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3672 | return FAIL; |
| 3673 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3674 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3675 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3676 | |
| 3677 | /* |
| 3678 | * If there is a comparative operator, use it. |
| 3679 | */ |
| 3680 | if (type != EXPR_UNKNOWN) |
| 3681 | { |
| 3682 | int ic = FALSE; // Default: do not ignore case |
| 3683 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3684 | if (next != NULL) |
| 3685 | { |
| 3686 | *arg = next_line_from_context(cctx, TRUE); |
| 3687 | p = skipwhite(*arg); |
| 3688 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3689 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 3690 | { |
| 3691 | semsg(_(e_invexpr2), *arg); |
| 3692 | return FAIL; |
| 3693 | } |
| 3694 | // extra question mark appended: ignore case |
| 3695 | if (p[len] == '?') |
| 3696 | { |
| 3697 | ic = TRUE; |
| 3698 | ++len; |
| 3699 | } |
| 3700 | // extra '#' appended: match case (ignored) |
| 3701 | else if (p[len] == '#') |
| 3702 | ++len; |
| 3703 | // nothing appended: match case |
| 3704 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3705 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3706 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3707 | error_white_both(p, len); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3708 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | // get the second variable |
| 3712 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3713 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3714 | return FAIL; |
| 3715 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3716 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3717 | return FAIL; |
| 3718 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3719 | if (ppconst->pp_used == ppconst_used + 2) |
| 3720 | { |
| 3721 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 3722 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3723 | int ret; |
| 3724 | |
| 3725 | // Both sides are a constant, compute the result now. |
| 3726 | // First check for a valid combination of types, this is more |
| 3727 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 3728 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3729 | ret = FAIL; |
| 3730 | else |
| 3731 | { |
| 3732 | ret = typval_compare(tv1, tv2, type, ic); |
| 3733 | tv1->v_type = VAR_BOOL; |
| 3734 | tv1->vval.v_number = tv1->vval.v_number |
| 3735 | ? VVAL_TRUE : VVAL_FALSE; |
| 3736 | clear_tv(tv2); |
| 3737 | --ppconst->pp_used; |
| 3738 | } |
| 3739 | return ret; |
| 3740 | } |
| 3741 | |
| 3742 | generate_ppconst(cctx, ppconst); |
| 3743 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | return OK; |
| 3747 | } |
| 3748 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 3749 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 3750 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3751 | /* |
| 3752 | * Compile || or &&. |
| 3753 | */ |
| 3754 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3755 | compile_and_or( |
| 3756 | char_u **arg, |
| 3757 | cctx_T *cctx, |
| 3758 | char *op, |
| 3759 | ppconst_T *ppconst, |
| 3760 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3761 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3762 | char_u *next; |
| 3763 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3764 | int opchar = *op; |
| 3765 | |
| 3766 | if (p[0] == opchar && p[1] == opchar) |
| 3767 | { |
| 3768 | garray_T *instr = &cctx->ctx_instr; |
| 3769 | garray_T end_ga; |
| 3770 | |
| 3771 | /* |
| 3772 | * Repeat until there is no following "||" or "&&" |
| 3773 | */ |
| 3774 | ga_init2(&end_ga, sizeof(int), 10); |
| 3775 | while (p[0] == opchar && p[1] == opchar) |
| 3776 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3777 | if (next != NULL) |
| 3778 | { |
| 3779 | *arg = next_line_from_context(cctx, TRUE); |
| 3780 | p = skipwhite(*arg); |
| 3781 | } |
| 3782 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3783 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 3784 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3785 | semsg(_(e_white_both), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3786 | return FAIL; |
| 3787 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3788 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3789 | // TODO: use ppconst if the value is a constant |
| 3790 | generate_ppconst(cctx, ppconst); |
| 3791 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3792 | if (ga_grow(&end_ga, 1) == FAIL) |
| 3793 | { |
| 3794 | ga_clear(&end_ga); |
| 3795 | return FAIL; |
| 3796 | } |
| 3797 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 3798 | ++end_ga.ga_len; |
| 3799 | generate_JUMP(cctx, opchar == '|' |
| 3800 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 3801 | |
| 3802 | // eval the next expression |
| 3803 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3804 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3805 | return FAIL; |
| 3806 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3807 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 3808 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3809 | { |
| 3810 | ga_clear(&end_ga); |
| 3811 | return FAIL; |
| 3812 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3813 | |
| 3814 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3815 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3816 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3817 | |
| 3818 | // Fill in the end label in all jumps. |
| 3819 | while (end_ga.ga_len > 0) |
| 3820 | { |
| 3821 | isn_T *isn; |
| 3822 | |
| 3823 | --end_ga.ga_len; |
| 3824 | isn = ((isn_T *)instr->ga_data) |
| 3825 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 3826 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3827 | } |
| 3828 | ga_clear(&end_ga); |
| 3829 | } |
| 3830 | |
| 3831 | return OK; |
| 3832 | } |
| 3833 | |
| 3834 | /* |
| 3835 | * expr4a && expr4a && expr4a logical AND |
| 3836 | * |
| 3837 | * Produces instructions: |
| 3838 | * EVAL expr4a Push result of "expr4a" |
| 3839 | * JUMP_AND_KEEP_IF_FALSE end |
| 3840 | * EVAL expr4b Push result of "expr4b" |
| 3841 | * JUMP_AND_KEEP_IF_FALSE end |
| 3842 | * EVAL expr4c Push result of "expr4c" |
| 3843 | * end: |
| 3844 | */ |
| 3845 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3846 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3847 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3848 | int ppconst_used = ppconst->pp_used; |
| 3849 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3850 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3851 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3852 | return FAIL; |
| 3853 | |
| 3854 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3855 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | /* |
| 3859 | * expr3a || expr3b || expr3c logical OR |
| 3860 | * |
| 3861 | * Produces instructions: |
| 3862 | * EVAL expr3a Push result of "expr3a" |
| 3863 | * JUMP_AND_KEEP_IF_TRUE end |
| 3864 | * EVAL expr3b Push result of "expr3b" |
| 3865 | * JUMP_AND_KEEP_IF_TRUE end |
| 3866 | * EVAL expr3c Push result of "expr3c" |
| 3867 | * end: |
| 3868 | */ |
| 3869 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3870 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3871 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3872 | int ppconst_used = ppconst->pp_used; |
| 3873 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3874 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3875 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3876 | return FAIL; |
| 3877 | |
| 3878 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3879 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | /* |
| 3883 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 3884 | * |
| 3885 | * Produces instructions: |
| 3886 | * EVAL expr2 Push result of "expr" |
| 3887 | * JUMP_IF_FALSE alt jump if false |
| 3888 | * EVAL expr1a |
| 3889 | * JUMP_ALWAYS end |
| 3890 | * alt: EVAL expr1b |
| 3891 | * end: |
| 3892 | */ |
| 3893 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3894 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3895 | { |
| 3896 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3897 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3898 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3899 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 3900 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3901 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3902 | return FAIL; |
| 3903 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3904 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3905 | if (*p == '?') |
| 3906 | { |
| 3907 | garray_T *instr = &cctx->ctx_instr; |
| 3908 | garray_T *stack = &cctx->ctx_type_stack; |
| 3909 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 3910 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3911 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 3912 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3913 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3914 | int has_const_expr = FALSE; |
| 3915 | int const_value = FALSE; |
| 3916 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3917 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3918 | if (next != NULL) |
| 3919 | { |
| 3920 | *arg = next_line_from_context(cctx, TRUE); |
| 3921 | p = skipwhite(*arg); |
| 3922 | } |
| 3923 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3924 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 3925 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3926 | semsg(_(e_white_both), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3927 | return FAIL; |
| 3928 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3929 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3930 | if (ppconst->pp_used == ppconst_used + 1) |
| 3931 | { |
| 3932 | // the condition is a constant, we know whether the ? or the : |
| 3933 | // expression is to be evaluated. |
| 3934 | has_const_expr = TRUE; |
| 3935 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 3936 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 3937 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3938 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 3939 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3940 | } |
| 3941 | else |
| 3942 | { |
| 3943 | generate_ppconst(cctx, ppconst); |
| 3944 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 3945 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3946 | |
| 3947 | // evaluate the second expression; any type is accepted |
| 3948 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3949 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3950 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3951 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 3952 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3953 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3954 | if (!has_const_expr) |
| 3955 | { |
| 3956 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3957 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3958 | // remember the type and drop it |
| 3959 | --stack->ga_len; |
| 3960 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3961 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3962 | end_idx = instr->ga_len; |
| 3963 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 3964 | |
| 3965 | // jump here from JUMP_IF_FALSE |
| 3966 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 3967 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 3968 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3969 | |
| 3970 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3971 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3972 | if (*p != ':') |
| 3973 | { |
| 3974 | emsg(_(e_missing_colon)); |
| 3975 | return FAIL; |
| 3976 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3977 | if (next != NULL) |
| 3978 | { |
| 3979 | *arg = next_line_from_context(cctx, TRUE); |
| 3980 | p = skipwhite(*arg); |
| 3981 | } |
| 3982 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3983 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 3984 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3985 | semsg(_(e_white_both), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3986 | return FAIL; |
| 3987 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3988 | |
| 3989 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3990 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3991 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 3992 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3993 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3994 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3995 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3996 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 3997 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3998 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3999 | if (!has_const_expr) |
| 4000 | { |
| 4001 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4002 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4003 | // If the types differ, the result has a more generic type. |
| 4004 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4005 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4006 | |
| 4007 | // jump here from JUMP_ALWAYS |
| 4008 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4009 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4010 | } |
| 4011 | |
| 4012 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4013 | } |
| 4014 | return OK; |
| 4015 | } |
| 4016 | |
| 4017 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4018 | * Toplevel expression. |
| 4019 | */ |
| 4020 | static int |
| 4021 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4022 | { |
| 4023 | ppconst_T ppconst; |
| 4024 | |
| 4025 | CLEAR_FIELD(ppconst); |
| 4026 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4027 | { |
| 4028 | clear_ppconst(&ppconst); |
| 4029 | return FAIL; |
| 4030 | } |
| 4031 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4032 | return FAIL; |
| 4033 | return OK; |
| 4034 | } |
| 4035 | |
| 4036 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4037 | * compile "return [expr]" |
| 4038 | */ |
| 4039 | static char_u * |
| 4040 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4041 | { |
| 4042 | char_u *p = arg; |
| 4043 | garray_T *stack = &cctx->ctx_type_stack; |
| 4044 | type_T *stack_type; |
| 4045 | |
| 4046 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4047 | { |
| 4048 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4049 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4050 | return NULL; |
| 4051 | |
| 4052 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4053 | if (set_return_type) |
| 4054 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4055 | else |
| 4056 | { |
| 4057 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4058 | && stack_type->tt_type != VAR_VOID |
| 4059 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4060 | { |
| 4061 | emsg(_("E1096: Returning a value in a function without a return type")); |
| 4062 | return NULL; |
| 4063 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4064 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4065 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4066 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4067 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4068 | } |
| 4069 | else |
| 4070 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4071 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4072 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4073 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4074 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4075 | { |
| 4076 | emsg(_("E1003: Missing return value")); |
| 4077 | return NULL; |
| 4078 | } |
| 4079 | |
| 4080 | // No argument, return zero. |
| 4081 | generate_PUSHNR(cctx, 0); |
| 4082 | } |
| 4083 | |
| 4084 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4085 | return NULL; |
| 4086 | |
| 4087 | // "return val | endif" is possible |
| 4088 | return skipwhite(p); |
| 4089 | } |
| 4090 | |
| 4091 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4092 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4093 | * Return a pointer to the line in allocated memory. |
| 4094 | * Return NULL for end-of-file or some error. |
| 4095 | */ |
| 4096 | static char_u * |
| 4097 | exarg_getline( |
| 4098 | int c UNUSED, |
| 4099 | void *cookie, |
| 4100 | int indent UNUSED, |
| 4101 | int do_concat UNUSED) |
| 4102 | { |
| 4103 | cctx_T *cctx = (cctx_T *)cookie; |
| 4104 | |
| 4105 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4106 | { |
| 4107 | iemsg("Heredoc got to end"); |
| 4108 | return NULL; |
| 4109 | } |
| 4110 | ++cctx->ctx_lnum; |
| 4111 | return vim_strsave(((char_u **)cctx->ctx_ufunc->uf_lines.ga_data) |
| 4112 | [cctx->ctx_lnum]); |
| 4113 | } |
| 4114 | |
| 4115 | /* |
| 4116 | * Compile a nested :def command. |
| 4117 | */ |
| 4118 | static char_u * |
| 4119 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4120 | { |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4121 | int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4122 | char_u *name_start = eap->arg; |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4123 | char_u *name_end = to_name_end(eap->arg, TRUE); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4124 | char_u *lambda_name; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4125 | lvar_T *lvar; |
| 4126 | ufunc_T *ufunc; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4127 | int r; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4128 | |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4129 | // Only g:Func() can use a namespace. |
| 4130 | if (name_start[1] == ':' && !is_global) |
| 4131 | { |
| 4132 | semsg(_(e_namespace), name_start); |
| 4133 | return NULL; |
| 4134 | } |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4135 | if (check_defined(name_start, name_end - name_start, cctx) == FAIL) |
| 4136 | return NULL; |
| 4137 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4138 | eap->arg = name_end; |
| 4139 | eap->getline = exarg_getline; |
| 4140 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4141 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4142 | eap->forceit = FALSE; |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4143 | lambda_name = get_lambda_name(); |
| 4144 | ufunc = def_function(eap, lambda_name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4145 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4146 | if (ufunc == NULL) |
| 4147 | return NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4148 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4149 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4150 | return NULL; |
| 4151 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4152 | if (is_global) |
| 4153 | { |
| 4154 | char_u *func_name = vim_strnsave(name_start + 2, |
| 4155 | name_end - name_start - 2); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4156 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4157 | if (func_name == NULL) |
| 4158 | r = FAIL; |
| 4159 | else |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4160 | r = generate_NEWFUNC(cctx, lambda_name, func_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4161 | } |
| 4162 | else |
| 4163 | { |
| 4164 | // Define a local variable for the function reference. |
| 4165 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
| 4166 | TRUE, ufunc->uf_func_type); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4167 | if (lvar == NULL) |
| 4168 | return NULL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 4169 | if (generate_FUNCREF(cctx, ufunc) == FAIL) |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4170 | return NULL; |
| 4171 | r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 4172 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4173 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4174 | // TODO: warning for trailing text? |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4175 | return r == FAIL ? NULL : (char_u *)""; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4179 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4180 | */ |
| 4181 | int |
| 4182 | assignment_len(char_u *p, int *heredoc) |
| 4183 | { |
| 4184 | if (*p == '=') |
| 4185 | { |
| 4186 | if (p[1] == '<' && p[2] == '<') |
| 4187 | { |
| 4188 | *heredoc = TRUE; |
| 4189 | return 3; |
| 4190 | } |
| 4191 | return 1; |
| 4192 | } |
| 4193 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4194 | return 2; |
| 4195 | if (STRNCMP(p, "..=", 3) == 0) |
| 4196 | return 3; |
| 4197 | return 0; |
| 4198 | } |
| 4199 | |
| 4200 | // words that cannot be used as a variable |
| 4201 | static char *reserved[] = { |
| 4202 | "true", |
| 4203 | "false", |
| 4204 | NULL |
| 4205 | }; |
| 4206 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4207 | typedef enum { |
| 4208 | dest_local, |
| 4209 | dest_option, |
| 4210 | dest_env, |
| 4211 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4212 | dest_buffer, |
| 4213 | dest_window, |
| 4214 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4215 | dest_vimvar, |
| 4216 | dest_script, |
| 4217 | dest_reg, |
| 4218 | } assign_dest_T; |
| 4219 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4220 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4221 | * Generate the load instruction for "name". |
| 4222 | */ |
| 4223 | static void |
| 4224 | generate_loadvar( |
| 4225 | cctx_T *cctx, |
| 4226 | assign_dest_T dest, |
| 4227 | char_u *name, |
| 4228 | lvar_T *lvar, |
| 4229 | type_T *type) |
| 4230 | { |
| 4231 | switch (dest) |
| 4232 | { |
| 4233 | case dest_option: |
| 4234 | // TODO: check the option exists |
| 4235 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4236 | break; |
| 4237 | case dest_global: |
| 4238 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4239 | break; |
| 4240 | case dest_buffer: |
| 4241 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4242 | break; |
| 4243 | case dest_window: |
| 4244 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4245 | break; |
| 4246 | case dest_tab: |
| 4247 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4248 | break; |
| 4249 | case dest_script: |
| 4250 | compile_load_scriptvar(cctx, |
| 4251 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4252 | break; |
| 4253 | case dest_env: |
| 4254 | // Include $ in the name here |
| 4255 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4256 | break; |
| 4257 | case dest_reg: |
| 4258 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4259 | break; |
| 4260 | case dest_vimvar: |
| 4261 | generate_LOADV(cctx, name + 2, TRUE); |
| 4262 | break; |
| 4263 | case dest_local: |
| 4264 | if (lvar->lv_from_outer) |
| 4265 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4266 | NULL, type); |
| 4267 | else |
| 4268 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4269 | break; |
| 4270 | } |
| 4271 | } |
| 4272 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4273 | void |
| 4274 | vim9_declare_error(char_u *name) |
| 4275 | { |
| 4276 | char *scope = ""; |
| 4277 | |
| 4278 | switch (*name) |
| 4279 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4280 | case 'g': scope = _("global"); break; |
| 4281 | case 'b': scope = _("buffer"); break; |
| 4282 | case 'w': scope = _("window"); break; |
| 4283 | case 't': scope = _("tab"); break; |
| 4284 | case 'v': scope = "v:"; break; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4285 | case '$': semsg(_(e_declare_env_var), name); |
| 4286 | return; |
| 4287 | case '&': semsg(_("E1052: Cannot declare an option: %s"), name); |
| 4288 | return; |
| 4289 | case '@': semsg(_("E1066: Cannot declare a register: %s"), name); |
| 4290 | return; |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4291 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4292 | } |
| 4293 | semsg(_(e_declare_var), scope, name); |
| 4294 | } |
| 4295 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4296 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4297 | * Compile declaration and assignment: |
| 4298 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4299 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4300 | * Return NULL for an error. |
| 4301 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4302 | */ |
| 4303 | static char_u * |
| 4304 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4305 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4306 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4307 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4308 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4309 | char_u *ret = NULL; |
| 4310 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4311 | int var_idx; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4312 | int scriptvar_sid = 0; |
| 4313 | int scriptvar_idx = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4314 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4315 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4316 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4317 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4318 | int oplen = 0; |
| 4319 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4320 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4321 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4322 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4323 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4324 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4325 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4326 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 4327 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 4328 | if (p == NULL) |
| 4329 | return *arg == '[' ? arg : NULL; |
| 4330 | |
| 4331 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4332 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 4333 | // TODO: should we allow this, and figure out type inference from list |
| 4334 | // members? |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4335 | emsg(_("E1092: Cannot use a list for a declaration")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4336 | return NULL; |
| 4337 | } |
| 4338 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4339 | sp = p; |
| 4340 | p = skipwhite(p); |
| 4341 | op = p; |
| 4342 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4343 | |
| 4344 | if (var_count > 0 && oplen == 0) |
| 4345 | // can be something like "[1, 2]->func()" |
| 4346 | return arg; |
| 4347 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4348 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4349 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4350 | error_white_both(op, oplen); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4351 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4352 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4353 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4354 | if (heredoc) |
| 4355 | { |
| 4356 | list_T *l; |
| 4357 | listitem_T *li; |
| 4358 | |
| 4359 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4360 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4361 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 4362 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4363 | |
| 4364 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 4365 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4366 | { |
| 4367 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4368 | li->li_tv.vval.v_string = NULL; |
| 4369 | } |
| 4370 | generate_NEWLIST(cctx, l->lv_len); |
| 4371 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4372 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4373 | list_free(l); |
| 4374 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4375 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4376 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4377 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4378 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4379 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 4380 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4381 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4382 | p = skipwhite(op + oplen); |
| 4383 | if (compile_expr0(&p, cctx) == FAIL) |
| 4384 | return NULL; |
| 4385 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4386 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4387 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4388 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4389 | type_T *stacktype; |
| 4390 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4391 | stacktype = stack->ga_len == 0 ? &t_void |
| 4392 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4393 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4394 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4395 | emsg(_(e_cannot_use_void)); |
| 4396 | goto theend; |
| 4397 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4398 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4399 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 4400 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4401 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 4402 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4403 | } |
| 4404 | } |
| 4405 | |
| 4406 | /* |
| 4407 | * Loop over variables in "[var, var] = expr". |
| 4408 | * For "var = expr" and "let var: type" this is done only once. |
| 4409 | */ |
| 4410 | if (var_count > 0) |
| 4411 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 4412 | else |
| 4413 | var_start = arg; |
| 4414 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 4415 | { |
| 4416 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 4417 | size_t varlen; |
| 4418 | int new_local = FALSE; |
| 4419 | int opt_type; |
| 4420 | int opt_flags = 0; |
| 4421 | assign_dest_T dest = dest_local; |
| 4422 | int vimvaridx = -1; |
| 4423 | lvar_T *lvar = NULL; |
| 4424 | lvar_T arg_lvar; |
| 4425 | int has_type = FALSE; |
| 4426 | int has_index = FALSE; |
| 4427 | int instr_count = -1; |
| 4428 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4429 | if (*var_start == '@') |
| 4430 | p = var_start + 2; |
| 4431 | else |
| 4432 | { |
| 4433 | p = (*var_start == '&' || *var_start == '$') |
| 4434 | ? var_start + 1 : var_start; |
| 4435 | p = to_name_end(p, TRUE); |
| 4436 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4437 | |
| 4438 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 4439 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 4440 | --var_end; |
| 4441 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 4442 | --p; |
| 4443 | |
| 4444 | varlen = p - var_start; |
| 4445 | vim_free(name); |
| 4446 | name = vim_strnsave(var_start, varlen); |
| 4447 | if (name == NULL) |
| 4448 | return NULL; |
| 4449 | if (!heredoc) |
| 4450 | type = &t_any; |
| 4451 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4452 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4453 | { |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4454 | int declare_error = FALSE; |
| 4455 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4456 | if (*var_start == '&') |
| 4457 | { |
| 4458 | int cc; |
| 4459 | long numval; |
| 4460 | |
| 4461 | dest = dest_option; |
| 4462 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4463 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4464 | emsg(_(e_const_option)); |
| 4465 | goto theend; |
| 4466 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4467 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4468 | p = var_start; |
| 4469 | p = find_option_end(&p, &opt_flags); |
| 4470 | if (p == NULL) |
| 4471 | { |
| 4472 | // cannot happen? |
| 4473 | emsg(_(e_letunexp)); |
| 4474 | goto theend; |
| 4475 | } |
| 4476 | cc = *p; |
| 4477 | *p = NUL; |
| 4478 | opt_type = get_option_value(var_start + 1, &numval, |
| 4479 | NULL, opt_flags); |
| 4480 | *p = cc; |
| 4481 | if (opt_type == -3) |
| 4482 | { |
| 4483 | semsg(_(e_unknown_option), var_start); |
| 4484 | goto theend; |
| 4485 | } |
| 4486 | if (opt_type == -2 || opt_type == 0) |
| 4487 | type = &t_string; |
| 4488 | else |
| 4489 | type = &t_number; // both number and boolean option |
| 4490 | } |
| 4491 | else if (*var_start == '$') |
| 4492 | { |
| 4493 | dest = dest_env; |
| 4494 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4495 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4496 | } |
| 4497 | else if (*var_start == '@') |
| 4498 | { |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4499 | if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.') |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4500 | { |
| 4501 | emsg_invreg(var_start[1]); |
| 4502 | goto theend; |
| 4503 | } |
| 4504 | dest = dest_reg; |
| 4505 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4506 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4507 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4508 | else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4509 | { |
| 4510 | dest = dest_global; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4511 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4512 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4513 | else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4514 | { |
| 4515 | dest = dest_buffer; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4516 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4517 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4518 | else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4519 | { |
| 4520 | dest = dest_window; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4521 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4522 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4523 | else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4524 | { |
| 4525 | dest = dest_tab; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4526 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4527 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4528 | else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4529 | { |
| 4530 | typval_T *vtv; |
| 4531 | int di_flags; |
| 4532 | |
| 4533 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 4534 | if (vimvaridx < 0) |
| 4535 | { |
| 4536 | semsg(_(e_var_notfound), var_start); |
| 4537 | goto theend; |
| 4538 | } |
| 4539 | // We use the current value of "sandbox" here, is that OK? |
| 4540 | if (var_check_ro(di_flags, name, FALSE)) |
| 4541 | goto theend; |
| 4542 | dest = dest_vimvar; |
| 4543 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 4544 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4545 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4546 | } |
| 4547 | else |
| 4548 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4549 | int idx; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4550 | |
| 4551 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 4552 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4553 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4554 | semsg(_("E1034: Cannot use reserved name %s"), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4555 | goto theend; |
| 4556 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4557 | |
| 4558 | lvar = lookup_local(var_start, varlen, cctx); |
| 4559 | if (lvar == NULL) |
| 4560 | { |
| 4561 | CLEAR_FIELD(arg_lvar); |
| 4562 | if (lookup_arg(var_start, varlen, |
| 4563 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 4564 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4565 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4566 | if (is_decl) |
| 4567 | { |
| 4568 | semsg(_(e_used_as_arg), name); |
| 4569 | goto theend; |
| 4570 | } |
| 4571 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4572 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4573 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4574 | if (lvar != NULL) |
| 4575 | { |
| 4576 | if (is_decl) |
| 4577 | { |
| 4578 | semsg(_("E1017: Variable already declared: %s"), name); |
| 4579 | goto theend; |
| 4580 | } |
| 4581 | else if (lvar->lv_const) |
| 4582 | { |
| 4583 | semsg(_("E1018: Cannot assign to a constant: %s"), |
| 4584 | name); |
| 4585 | goto theend; |
| 4586 | } |
| 4587 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4588 | else |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4589 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4590 | int script_namespace = varlen > 1 |
| 4591 | && STRNCMP(var_start, "s:", 2) == 0; |
| 4592 | int script_var = (script_namespace |
| 4593 | ? lookup_script(var_start + 2, varlen - 2) |
| 4594 | : lookup_script(var_start, varlen)) == OK; |
| 4595 | imported_T *import = |
| 4596 | find_imported(var_start, varlen, cctx); |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4597 | |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4598 | if (script_namespace || script_var || import != NULL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4599 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4600 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 4601 | |
| 4602 | if (is_decl) |
| 4603 | { |
| 4604 | if (script_namespace) |
| 4605 | semsg(_("E1101: Cannot declare a script variable in a function: %s"), |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4606 | name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4607 | else |
| 4608 | semsg(_("E1054: Variable already declared in the script: %s"), |
| 4609 | name); |
| 4610 | goto theend; |
| 4611 | } |
| 4612 | else if (cctx->ctx_ufunc->uf_script_ctx_version |
| 4613 | == SCRIPT_VERSION_VIM9 |
| 4614 | && script_namespace |
| 4615 | && !script_var && import == NULL) |
| 4616 | { |
| 4617 | semsg(_(e_unknown_var), name); |
| 4618 | goto theend; |
| 4619 | } |
| 4620 | |
| 4621 | dest = dest_script; |
| 4622 | |
| 4623 | // existing script-local variables should have a type |
| 4624 | scriptvar_sid = current_sctx.sc_sid; |
| 4625 | if (import != NULL) |
| 4626 | scriptvar_sid = import->imp_sid; |
| 4627 | scriptvar_idx = get_script_item_idx(scriptvar_sid, |
| 4628 | rawname, TRUE); |
| 4629 | if (scriptvar_idx >= 0) |
| 4630 | { |
| 4631 | scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid); |
| 4632 | svar_T *sv = |
| 4633 | ((svar_T *)si->sn_var_vals.ga_data) |
| 4634 | + scriptvar_idx; |
| 4635 | type = sv->sv_type; |
| 4636 | } |
| 4637 | } |
| 4638 | else if (name[1] == ':' && name[2] != NUL) |
| 4639 | { |
| 4640 | semsg(_("E1082: Cannot use a namespaced variable: %s"), |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4641 | name); |
| 4642 | goto theend; |
| 4643 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4644 | else if (!is_decl) |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4645 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4646 | semsg(_(e_unknown_var), name); |
| 4647 | goto theend; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4648 | } |
Bram Moolenaar | fa211f3 | 2020-08-07 22:00:26 +0200 | [diff] [blame] | 4649 | else if (check_defined(var_start, varlen, cctx) == FAIL) |
| 4650 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4651 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4652 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4653 | |
| 4654 | if (declare_error) |
| 4655 | { |
| 4656 | vim9_declare_error(name); |
| 4657 | goto theend; |
| 4658 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4659 | } |
| 4660 | |
| 4661 | // handle "a:name" as a name, not index "name" on "a" |
| 4662 | if (varlen > 1 || var_start[varlen] != ':') |
| 4663 | p = var_end; |
| 4664 | |
| 4665 | if (dest != dest_option) |
| 4666 | { |
| 4667 | if (is_decl && *p == ':') |
| 4668 | { |
| 4669 | // parse optional type: "let var: type = expr" |
| 4670 | if (!VIM_ISWHITE(p[1])) |
| 4671 | { |
| 4672 | semsg(_(e_white_after), ":"); |
| 4673 | goto theend; |
| 4674 | } |
| 4675 | p = skipwhite(p + 1); |
| 4676 | type = parse_type(&p, cctx->ctx_type_list); |
| 4677 | has_type = TRUE; |
| 4678 | } |
| 4679 | else if (lvar != NULL) |
| 4680 | type = lvar->lv_type; |
| 4681 | } |
| 4682 | |
| 4683 | if (oplen == 3 && !heredoc && dest != dest_global |
| 4684 | && type->tt_type != VAR_STRING |
| 4685 | && type->tt_type != VAR_ANY) |
| 4686 | { |
| 4687 | emsg(_("E1019: Can only concatenate to string")); |
| 4688 | goto theend; |
| 4689 | } |
| 4690 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4691 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4692 | { |
| 4693 | if (oplen > 1 && !heredoc) |
| 4694 | { |
| 4695 | // +=, /=, etc. require an existing variable |
| 4696 | semsg(_("E1020: cannot use an operator on a new variable: %s"), |
| 4697 | name); |
| 4698 | goto theend; |
| 4699 | } |
| 4700 | |
| 4701 | // new local variable |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 4702 | if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
| 4703 | && var_wrong_func_name(name, TRUE)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4704 | goto theend; |
| 4705 | lvar = reserve_local(cctx, var_start, varlen, |
| 4706 | cmdidx == CMD_const, type); |
| 4707 | if (lvar == NULL) |
| 4708 | goto theend; |
| 4709 | new_local = TRUE; |
| 4710 | } |
| 4711 | |
| 4712 | member_type = type; |
| 4713 | if (var_end > var_start + varlen) |
| 4714 | { |
| 4715 | // Something follows after the variable: "var[idx]". |
| 4716 | if (is_decl) |
| 4717 | { |
| 4718 | emsg(_("E1087: cannot use an index when declaring a variable")); |
| 4719 | goto theend; |
| 4720 | } |
| 4721 | |
| 4722 | if (var_start[varlen] == '[') |
| 4723 | { |
| 4724 | has_index = TRUE; |
| 4725 | if (type->tt_member == NULL) |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 4726 | member_type = &t_any; |
| 4727 | else |
| 4728 | member_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4729 | } |
| 4730 | else |
| 4731 | { |
| 4732 | semsg("Not supported yet: %s", var_start); |
| 4733 | goto theend; |
| 4734 | } |
| 4735 | } |
| 4736 | else if (lvar == &arg_lvar) |
| 4737 | { |
| 4738 | semsg(_("E1090: Cannot assign to argument %s"), name); |
| 4739 | goto theend; |
| 4740 | } |
| 4741 | |
| 4742 | if (!heredoc) |
| 4743 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4744 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4745 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4746 | if (oplen > 0 && var_count == 0) |
| 4747 | { |
| 4748 | // skip over the "=" and the expression |
| 4749 | p = skipwhite(op + oplen); |
| 4750 | compile_expr0(&p, cctx); |
| 4751 | } |
| 4752 | } |
| 4753 | else if (oplen > 0) |
| 4754 | { |
| 4755 | type_T *stacktype; |
| 4756 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4757 | // For "var = expr" evaluate the expression. |
| 4758 | if (var_count == 0) |
| 4759 | { |
| 4760 | int r; |
| 4761 | |
| 4762 | // for "+=", "*=", "..=" etc. first load the current value |
| 4763 | if (*op != '=') |
| 4764 | { |
| 4765 | generate_loadvar(cctx, dest, name, lvar, type); |
| 4766 | |
| 4767 | if (has_index) |
| 4768 | { |
| 4769 | // TODO: get member from list or dict |
| 4770 | emsg("Index with operation not supported yet"); |
| 4771 | goto theend; |
| 4772 | } |
| 4773 | } |
| 4774 | |
| 4775 | // Compile the expression. Temporarily hide the new local |
| 4776 | // variable here, it is not available to this expression. |
| 4777 | if (new_local) |
| 4778 | --cctx->ctx_locals.ga_len; |
| 4779 | instr_count = instr->ga_len; |
| 4780 | p = skipwhite(op + oplen); |
| 4781 | r = compile_expr0(&p, cctx); |
| 4782 | if (new_local) |
| 4783 | ++cctx->ctx_locals.ga_len; |
| 4784 | if (r == FAIL) |
| 4785 | goto theend; |
| 4786 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4787 | else if (semicolon && var_idx == var_count - 1) |
| 4788 | { |
| 4789 | // For "[var; var] = expr" get the rest of the list |
| 4790 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 4791 | goto theend; |
| 4792 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4793 | else |
| 4794 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4795 | // For "[var, var] = expr" get the "var_idx" item from the |
| 4796 | // list. |
| 4797 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 4798 | return FAIL; |
| 4799 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4800 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4801 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 4802 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4803 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4804 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4805 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4806 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4807 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4808 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4809 | emsg(_(e_cannot_use_void)); |
| 4810 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4811 | } |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 4812 | else if ((stacktype->tt_type == VAR_FUNC |
| 4813 | || stacktype->tt_type == VAR_PARTIAL) |
| 4814 | && var_wrong_func_name(name, TRUE)) |
| 4815 | { |
| 4816 | goto theend; |
| 4817 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4818 | else |
| 4819 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4820 | // An empty list or dict has a &t_void member, |
| 4821 | // for a variable that implies &t_any. |
| 4822 | if (stacktype == &t_list_empty) |
| 4823 | lvar->lv_type = &t_list_any; |
| 4824 | else if (stacktype == &t_dict_empty) |
| 4825 | lvar->lv_type = &t_dict_any; |
| 4826 | else |
| 4827 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4828 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4829 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4830 | else |
| 4831 | { |
| 4832 | type_T *use_type = lvar->lv_type; |
| 4833 | |
| 4834 | if (has_index) |
| 4835 | { |
| 4836 | use_type = use_type->tt_member; |
| 4837 | if (use_type == NULL) |
| 4838 | use_type = &t_void; |
| 4839 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4840 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4841 | == FAIL) |
| 4842 | goto theend; |
| 4843 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4844 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4845 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4846 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4847 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4848 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4849 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4850 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4851 | emsg(_(e_const_req_value)); |
| 4852 | goto theend; |
| 4853 | } |
| 4854 | else if (!has_type || dest == dest_option) |
| 4855 | { |
| 4856 | emsg(_(e_type_req)); |
| 4857 | goto theend; |
| 4858 | } |
| 4859 | else |
| 4860 | { |
| 4861 | // variables are always initialized |
| 4862 | if (ga_grow(instr, 1) == FAIL) |
| 4863 | goto theend; |
| 4864 | switch (member_type->tt_type) |
| 4865 | { |
| 4866 | case VAR_BOOL: |
| 4867 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 4868 | break; |
| 4869 | case VAR_FLOAT: |
| 4870 | #ifdef FEAT_FLOAT |
| 4871 | generate_PUSHF(cctx, 0.0); |
| 4872 | #endif |
| 4873 | break; |
| 4874 | case VAR_STRING: |
| 4875 | generate_PUSHS(cctx, NULL); |
| 4876 | break; |
| 4877 | case VAR_BLOB: |
| 4878 | generate_PUSHBLOB(cctx, NULL); |
| 4879 | break; |
| 4880 | case VAR_FUNC: |
| 4881 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 4882 | break; |
| 4883 | case VAR_LIST: |
| 4884 | generate_NEWLIST(cctx, 0); |
| 4885 | break; |
| 4886 | case VAR_DICT: |
| 4887 | generate_NEWDICT(cctx, 0); |
| 4888 | break; |
| 4889 | case VAR_JOB: |
| 4890 | generate_PUSHJOB(cctx, NULL); |
| 4891 | break; |
| 4892 | case VAR_CHANNEL: |
| 4893 | generate_PUSHCHANNEL(cctx, NULL); |
| 4894 | break; |
| 4895 | case VAR_NUMBER: |
| 4896 | case VAR_UNKNOWN: |
| 4897 | case VAR_ANY: |
| 4898 | case VAR_PARTIAL: |
| 4899 | case VAR_VOID: |
| 4900 | case VAR_SPECIAL: // cannot happen |
| 4901 | generate_PUSHNR(cctx, 0); |
| 4902 | break; |
| 4903 | } |
| 4904 | } |
| 4905 | if (var_count == 0) |
| 4906 | end = p; |
| 4907 | } |
| 4908 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4909 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4910 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4911 | break; |
| 4912 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4913 | if (oplen > 0 && *op != '=') |
| 4914 | { |
| 4915 | type_T *expected = &t_number; |
| 4916 | type_T *stacktype; |
| 4917 | |
| 4918 | // TODO: if type is known use float or any operation |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 4919 | // TODO: check operator matches variable type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4920 | |
| 4921 | if (*op == '.') |
| 4922 | expected = &t_string; |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 4923 | else if (*op == '+') |
| 4924 | expected = member_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4925 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4926 | if (need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4927 | goto theend; |
| 4928 | |
| 4929 | if (*op == '.') |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 4930 | { |
| 4931 | if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL) |
| 4932 | goto theend; |
| 4933 | } |
| 4934 | else if (*op == '+') |
| 4935 | { |
| 4936 | if (generate_add_instr(cctx, |
| 4937 | operator_type(member_type, stacktype), |
| 4938 | member_type, stacktype) == FAIL) |
| 4939 | goto theend; |
| 4940 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4941 | else |
| 4942 | { |
| 4943 | isn_T *isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 4944 | |
| 4945 | if (isn == NULL) |
| 4946 | goto theend; |
| 4947 | switch (*op) |
| 4948 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4949 | case '-': isn->isn_arg.op.op_type = EXPR_SUB; break; |
| 4950 | case '*': isn->isn_arg.op.op_type = EXPR_MULT; break; |
| 4951 | case '/': isn->isn_arg.op.op_type = EXPR_DIV; break; |
| 4952 | case '%': isn->isn_arg.op.op_type = EXPR_REM; break; |
| 4953 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4954 | } |
| 4955 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4956 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4957 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4958 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4959 | int r; |
| 4960 | |
| 4961 | // Compile the "idx" in "var[idx]". |
| 4962 | if (new_local) |
| 4963 | --cctx->ctx_locals.ga_len; |
| 4964 | p = skipwhite(var_start + varlen + 1); |
| 4965 | r = compile_expr0(&p, cctx); |
| 4966 | if (new_local) |
| 4967 | ++cctx->ctx_locals.ga_len; |
| 4968 | if (r == FAIL) |
| 4969 | goto theend; |
| 4970 | if (*skipwhite(p) != ']') |
| 4971 | { |
| 4972 | emsg(_(e_missbrac)); |
| 4973 | goto theend; |
| 4974 | } |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 4975 | if (type == &t_any) |
| 4976 | { |
| 4977 | type_T *idx_type = ((type_T **)stack->ga_data)[ |
| 4978 | stack->ga_len - 1]; |
| 4979 | // Index on variable of unknown type: guess the type from the |
| 4980 | // index type: number is dict, otherwise dict. |
| 4981 | // TODO: should do the assignment at runtime |
| 4982 | if (idx_type->tt_type == VAR_NUMBER) |
| 4983 | type = &t_list_any; |
| 4984 | else |
| 4985 | type = &t_dict_any; |
| 4986 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4987 | if (type->tt_type == VAR_DICT |
| 4988 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 4989 | goto theend; |
| 4990 | if (type->tt_type == VAR_LIST |
| 4991 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4992 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4993 | { |
| 4994 | emsg(_(e_number_exp)); |
| 4995 | goto theend; |
| 4996 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4997 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4998 | // Load the dict or list. On the stack we then have: |
| 4999 | // - value |
| 5000 | // - index |
| 5001 | // - variable |
| 5002 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5003 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5004 | if (type->tt_type == VAR_LIST) |
| 5005 | { |
| 5006 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5007 | return FAIL; |
| 5008 | } |
| 5009 | else if (type->tt_type == VAR_DICT) |
| 5010 | { |
| 5011 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5012 | return FAIL; |
| 5013 | } |
| 5014 | else |
| 5015 | { |
| 5016 | emsg(_(e_listreq)); |
| 5017 | goto theend; |
| 5018 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5019 | } |
| 5020 | else |
| 5021 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5022 | switch (dest) |
| 5023 | { |
| 5024 | case dest_option: |
| 5025 | generate_STOREOPT(cctx, name + 1, opt_flags); |
| 5026 | break; |
| 5027 | case dest_global: |
| 5028 | // include g: with the name, easier to execute that way |
| 5029 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5030 | break; |
| 5031 | case dest_buffer: |
| 5032 | // include b: with the name, easier to execute that way |
| 5033 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5034 | break; |
| 5035 | case dest_window: |
| 5036 | // include w: with the name, easier to execute that way |
| 5037 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5038 | break; |
| 5039 | case dest_tab: |
| 5040 | // include t: with the name, easier to execute that way |
| 5041 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5042 | break; |
| 5043 | case dest_env: |
| 5044 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5045 | break; |
| 5046 | case dest_reg: |
| 5047 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5048 | break; |
| 5049 | case dest_vimvar: |
| 5050 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5051 | break; |
| 5052 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5053 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5054 | if (scriptvar_idx < 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5055 | { |
| 5056 | char_u *name_s = name; |
| 5057 | |
| 5058 | // Include s: in the name for store_var() |
| 5059 | if (name[1] != ':') |
| 5060 | { |
| 5061 | int len = (int)STRLEN(name) + 3; |
| 5062 | |
| 5063 | name_s = alloc(len); |
| 5064 | if (name_s == NULL) |
| 5065 | name_s = name; |
| 5066 | else |
| 5067 | vim_snprintf((char *)name_s, len, |
| 5068 | "s:%s", name); |
| 5069 | } |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5070 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, |
| 5071 | scriptvar_sid, type); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5072 | if (name_s != name) |
| 5073 | vim_free(name_s); |
| 5074 | } |
| 5075 | else |
| 5076 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5077 | scriptvar_sid, scriptvar_idx, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5078 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5079 | break; |
| 5080 | case dest_local: |
| 5081 | if (lvar != NULL) |
| 5082 | { |
| 5083 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5084 | + instr->ga_len - 1; |
| 5085 | |
| 5086 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5087 | // ISN_STORE into ISN_STORENR |
| 5088 | if (!lvar->lv_from_outer |
| 5089 | && instr->ga_len == instr_count + 1 |
| 5090 | && isn->isn_type == ISN_PUSHNR) |
| 5091 | { |
| 5092 | varnumber_T val = isn->isn_arg.number; |
| 5093 | |
| 5094 | isn->isn_type = ISN_STORENR; |
| 5095 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5096 | isn->isn_arg.storenr.stnr_val = val; |
| 5097 | if (stack->ga_len > 0) |
| 5098 | --stack->ga_len; |
| 5099 | } |
| 5100 | else if (lvar->lv_from_outer) |
| 5101 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5102 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5103 | else |
| 5104 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5105 | } |
| 5106 | break; |
| 5107 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5108 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5109 | |
| 5110 | if (var_idx + 1 < var_count) |
| 5111 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5112 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5113 | |
| 5114 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5115 | if (var_count > 0 && !semicolon) |
| 5116 | { |
| 5117 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5118 | goto theend; |
| 5119 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5120 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5121 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5122 | |
| 5123 | theend: |
| 5124 | vim_free(name); |
| 5125 | return ret; |
| 5126 | } |
| 5127 | |
| 5128 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5129 | * Check if "name" can be "unlet". |
| 5130 | */ |
| 5131 | int |
| 5132 | check_vim9_unlet(char_u *name) |
| 5133 | { |
| 5134 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5135 | { |
| 5136 | semsg(_("E1081: Cannot unlet %s"), name); |
| 5137 | return FAIL; |
| 5138 | } |
| 5139 | return OK; |
| 5140 | } |
| 5141 | |
| 5142 | /* |
| 5143 | * Callback passed to ex_unletlock(). |
| 5144 | */ |
| 5145 | static int |
| 5146 | compile_unlet( |
| 5147 | lval_T *lvp, |
| 5148 | char_u *name_end, |
| 5149 | exarg_T *eap, |
| 5150 | int deep UNUSED, |
| 5151 | void *coookie) |
| 5152 | { |
| 5153 | cctx_T *cctx = coookie; |
| 5154 | |
| 5155 | if (lvp->ll_tv == NULL) |
| 5156 | { |
| 5157 | char_u *p = lvp->ll_name; |
| 5158 | int cc = *name_end; |
| 5159 | int ret = OK; |
| 5160 | |
| 5161 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5162 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5163 | if (*p == '$') |
| 5164 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5165 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5166 | ret = FAIL; |
| 5167 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5168 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5169 | |
| 5170 | *name_end = cc; |
| 5171 | return ret; |
| 5172 | } |
| 5173 | |
| 5174 | // TODO: unlet {list}[idx] |
| 5175 | // TODO: unlet {dict}[key] |
| 5176 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5177 | return FAIL; |
| 5178 | } |
| 5179 | |
| 5180 | /* |
| 5181 | * compile "unlet var", "lock var" and "unlock var" |
| 5182 | * "arg" points to "var". |
| 5183 | */ |
| 5184 | static char_u * |
| 5185 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5186 | { |
| 5187 | char_u *p = arg; |
| 5188 | |
| 5189 | if (eap->cmdidx != CMD_unlet) |
| 5190 | { |
| 5191 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5192 | return NULL; |
| 5193 | } |
| 5194 | |
| 5195 | if (*p == '!') |
| 5196 | { |
| 5197 | p = skipwhite(p + 1); |
| 5198 | eap->forceit = TRUE; |
| 5199 | } |
| 5200 | |
| 5201 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5202 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5203 | } |
| 5204 | |
| 5205 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5206 | * Compile an :import command. |
| 5207 | */ |
| 5208 | static char_u * |
| 5209 | compile_import(char_u *arg, cctx_T *cctx) |
| 5210 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5211 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5212 | } |
| 5213 | |
| 5214 | /* |
| 5215 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5216 | */ |
| 5217 | static int |
| 5218 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5219 | { |
| 5220 | garray_T *instr = &cctx->ctx_instr; |
| 5221 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5222 | |
| 5223 | if (endlabel == NULL) |
| 5224 | return FAIL; |
| 5225 | endlabel->el_next = *el; |
| 5226 | *el = endlabel; |
| 5227 | endlabel->el_end_label = instr->ga_len; |
| 5228 | |
| 5229 | generate_JUMP(cctx, when, 0); |
| 5230 | return OK; |
| 5231 | } |
| 5232 | |
| 5233 | static void |
| 5234 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5235 | { |
| 5236 | garray_T *instr = &cctx->ctx_instr; |
| 5237 | |
| 5238 | while (*el != NULL) |
| 5239 | { |
| 5240 | endlabel_T *cur = (*el); |
| 5241 | isn_T *isn; |
| 5242 | |
| 5243 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5244 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5245 | *el = cur->el_next; |
| 5246 | vim_free(cur); |
| 5247 | } |
| 5248 | } |
| 5249 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5250 | static void |
| 5251 | compile_free_jump_to_end(endlabel_T **el) |
| 5252 | { |
| 5253 | while (*el != NULL) |
| 5254 | { |
| 5255 | endlabel_T *cur = (*el); |
| 5256 | |
| 5257 | *el = cur->el_next; |
| 5258 | vim_free(cur); |
| 5259 | } |
| 5260 | } |
| 5261 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5262 | /* |
| 5263 | * Create a new scope and set up the generic items. |
| 5264 | */ |
| 5265 | static scope_T * |
| 5266 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5267 | { |
| 5268 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5269 | |
| 5270 | if (scope == NULL) |
| 5271 | return NULL; |
| 5272 | scope->se_outer = cctx->ctx_scope; |
| 5273 | cctx->ctx_scope = scope; |
| 5274 | scope->se_type = type; |
| 5275 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5276 | return scope; |
| 5277 | } |
| 5278 | |
| 5279 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5280 | * Free the current scope and go back to the outer scope. |
| 5281 | */ |
| 5282 | static void |
| 5283 | drop_scope(cctx_T *cctx) |
| 5284 | { |
| 5285 | scope_T *scope = cctx->ctx_scope; |
| 5286 | |
| 5287 | if (scope == NULL) |
| 5288 | { |
| 5289 | iemsg("calling drop_scope() without a scope"); |
| 5290 | return; |
| 5291 | } |
| 5292 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5293 | switch (scope->se_type) |
| 5294 | { |
| 5295 | case IF_SCOPE: |
| 5296 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5297 | case FOR_SCOPE: |
| 5298 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5299 | case WHILE_SCOPE: |
| 5300 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5301 | case TRY_SCOPE: |
| 5302 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5303 | case NO_SCOPE: |
| 5304 | case BLOCK_SCOPE: |
| 5305 | break; |
| 5306 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5307 | vim_free(scope); |
| 5308 | } |
| 5309 | |
| 5310 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5311 | * compile "if expr" |
| 5312 | * |
| 5313 | * "if expr" Produces instructions: |
| 5314 | * EVAL expr Push result of "expr" |
| 5315 | * JUMP_IF_FALSE end |
| 5316 | * ... body ... |
| 5317 | * end: |
| 5318 | * |
| 5319 | * "if expr | else" Produces instructions: |
| 5320 | * EVAL expr Push result of "expr" |
| 5321 | * JUMP_IF_FALSE else |
| 5322 | * ... body ... |
| 5323 | * JUMP_ALWAYS end |
| 5324 | * else: |
| 5325 | * ... body ... |
| 5326 | * end: |
| 5327 | * |
| 5328 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5329 | * EVAL expr Push result of "expr" |
| 5330 | * JUMP_IF_FALSE elseif |
| 5331 | * ... body ... |
| 5332 | * JUMP_ALWAYS end |
| 5333 | * elseif: |
| 5334 | * EVAL expr Push result of "expr" |
| 5335 | * JUMP_IF_FALSE else |
| 5336 | * ... body ... |
| 5337 | * JUMP_ALWAYS end |
| 5338 | * else: |
| 5339 | * ... body ... |
| 5340 | * end: |
| 5341 | */ |
| 5342 | static char_u * |
| 5343 | compile_if(char_u *arg, cctx_T *cctx) |
| 5344 | { |
| 5345 | char_u *p = arg; |
| 5346 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5347 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5348 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5349 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5350 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5351 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5352 | CLEAR_FIELD(ppconst); |
| 5353 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5354 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5355 | clear_ppconst(&ppconst); |
| 5356 | return NULL; |
| 5357 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5358 | if (cctx->ctx_skip == SKIP_YES) |
| 5359 | clear_ppconst(&ppconst); |
| 5360 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5361 | { |
| 5362 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5363 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5364 | clear_ppconst(&ppconst); |
| 5365 | } |
| 5366 | else |
| 5367 | { |
| 5368 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5369 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5370 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5371 | return NULL; |
| 5372 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5373 | |
| 5374 | scope = new_scope(cctx, IF_SCOPE); |
| 5375 | if (scope == NULL) |
| 5376 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5377 | scope->se_skip_save = skip_save; |
| 5378 | // "is_had_return" will be reset if any block does not end in :return |
| 5379 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5380 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5381 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5382 | { |
| 5383 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5384 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5385 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5386 | } |
| 5387 | else |
| 5388 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5389 | |
| 5390 | return p; |
| 5391 | } |
| 5392 | |
| 5393 | static char_u * |
| 5394 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 5395 | { |
| 5396 | char_u *p = arg; |
| 5397 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5398 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5399 | isn_T *isn; |
| 5400 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5401 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5402 | |
| 5403 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5404 | { |
| 5405 | emsg(_(e_elseif_without_if)); |
| 5406 | return NULL; |
| 5407 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5408 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5409 | if (!cctx->ctx_had_return) |
| 5410 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5411 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5412 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5413 | { |
| 5414 | 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] | 5415 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5416 | return NULL; |
| 5417 | // previous "if" or "elseif" jumps here |
| 5418 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5419 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5420 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5421 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5422 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5423 | CLEAR_FIELD(ppconst); |
| 5424 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5425 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5426 | clear_ppconst(&ppconst); |
| 5427 | return NULL; |
| 5428 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5429 | if (scope->se_skip_save == SKIP_YES) |
| 5430 | clear_ppconst(&ppconst); |
| 5431 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5432 | { |
| 5433 | // The expression results in a constant. |
| 5434 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5435 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5436 | clear_ppconst(&ppconst); |
| 5437 | scope->se_u.se_if.is_if_label = -1; |
| 5438 | } |
| 5439 | else |
| 5440 | { |
| 5441 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5442 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5443 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5444 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5445 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5446 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5447 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5448 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5449 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5450 | |
| 5451 | return p; |
| 5452 | } |
| 5453 | |
| 5454 | static char_u * |
| 5455 | compile_else(char_u *arg, cctx_T *cctx) |
| 5456 | { |
| 5457 | char_u *p = arg; |
| 5458 | garray_T *instr = &cctx->ctx_instr; |
| 5459 | isn_T *isn; |
| 5460 | scope_T *scope = cctx->ctx_scope; |
| 5461 | |
| 5462 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5463 | { |
| 5464 | emsg(_(e_else_without_if)); |
| 5465 | return NULL; |
| 5466 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5467 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5468 | if (!cctx->ctx_had_return) |
| 5469 | scope->se_u.se_if.is_had_return = FALSE; |
| 5470 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5471 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5472 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5473 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5474 | // 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] | 5475 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5476 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5477 | if (!cctx->ctx_had_return |
| 5478 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 5479 | JUMP_ALWAYS, cctx) == FAIL) |
| 5480 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5481 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5482 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5483 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5484 | { |
| 5485 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5486 | { |
| 5487 | // previous "if" or "elseif" jumps here |
| 5488 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5489 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5490 | scope->se_u.se_if.is_if_label = -1; |
| 5491 | } |
| 5492 | } |
| 5493 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5494 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5495 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 5496 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5497 | |
| 5498 | return p; |
| 5499 | } |
| 5500 | |
| 5501 | static char_u * |
| 5502 | compile_endif(char_u *arg, cctx_T *cctx) |
| 5503 | { |
| 5504 | scope_T *scope = cctx->ctx_scope; |
| 5505 | ifscope_T *ifscope; |
| 5506 | garray_T *instr = &cctx->ctx_instr; |
| 5507 | isn_T *isn; |
| 5508 | |
| 5509 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5510 | { |
| 5511 | emsg(_(e_endif_without_if)); |
| 5512 | return NULL; |
| 5513 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5514 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5515 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5516 | if (!cctx->ctx_had_return) |
| 5517 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5518 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5519 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5520 | { |
| 5521 | // previous "if" or "elseif" jumps here |
| 5522 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5523 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5524 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5525 | // Fill in the "end" label in jumps at the end of the blocks. |
| 5526 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5527 | cctx->ctx_skip = scope->se_skip_save; |
| 5528 | |
| 5529 | // If all the blocks end in :return and there is an :else then the |
| 5530 | // had_return flag is set. |
| 5531 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5532 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5533 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5534 | return arg; |
| 5535 | } |
| 5536 | |
| 5537 | /* |
| 5538 | * compile "for var in expr" |
| 5539 | * |
| 5540 | * Produces instructions: |
| 5541 | * PUSHNR -1 |
| 5542 | * STORE loop-idx Set index to -1 |
| 5543 | * EVAL expr Push result of "expr" |
| 5544 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 5545 | * - if beyond end, jump to "end" |
| 5546 | * - otherwise get item from list and push it |
| 5547 | * STORE var Store item in "var" |
| 5548 | * ... body ... |
| 5549 | * JUMP top Jump back to repeat |
| 5550 | * end: DROP Drop the result of "expr" |
| 5551 | * |
| 5552 | */ |
| 5553 | static char_u * |
| 5554 | compile_for(char_u *arg, cctx_T *cctx) |
| 5555 | { |
| 5556 | char_u *p; |
| 5557 | size_t varlen; |
| 5558 | garray_T *instr = &cctx->ctx_instr; |
| 5559 | garray_T *stack = &cctx->ctx_type_stack; |
| 5560 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5561 | lvar_T *loop_lvar; // loop iteration variable |
| 5562 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5563 | type_T *vartype; |
| 5564 | |
| 5565 | // TODO: list of variables: "for [key, value] in dict" |
| 5566 | // parse "var" |
| 5567 | for (p = arg; eval_isnamec1(*p); ++p) |
| 5568 | ; |
| 5569 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5570 | var_lvar = lookup_local(arg, varlen, cctx); |
| 5571 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5572 | { |
| 5573 | semsg(_("E1023: variable already defined: %s"), arg); |
| 5574 | return NULL; |
| 5575 | } |
| 5576 | |
| 5577 | // consume "in" |
| 5578 | p = skipwhite(p); |
| 5579 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 5580 | { |
| 5581 | emsg(_(e_missing_in)); |
| 5582 | return NULL; |
| 5583 | } |
| 5584 | p = skipwhite(p + 2); |
| 5585 | |
| 5586 | |
| 5587 | scope = new_scope(cctx, FOR_SCOPE); |
| 5588 | if (scope == NULL) |
| 5589 | return NULL; |
| 5590 | |
| 5591 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5592 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 5593 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5594 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5595 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5596 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5597 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5598 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5599 | |
| 5600 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5601 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 5602 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5603 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5604 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5605 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5606 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5607 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5608 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5609 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5610 | |
| 5611 | // compile "expr", it remains on the stack until "endfor" |
| 5612 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5613 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5614 | { |
| 5615 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5616 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5617 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5618 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5619 | // Now that we know the type of "var", check that it is a list, now or at |
| 5620 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5621 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5622 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5623 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5624 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5625 | return NULL; |
| 5626 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5627 | 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] | 5628 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5629 | |
| 5630 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5631 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5632 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5633 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 5634 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5635 | |
| 5636 | return arg; |
| 5637 | } |
| 5638 | |
| 5639 | /* |
| 5640 | * compile "endfor" |
| 5641 | */ |
| 5642 | static char_u * |
| 5643 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 5644 | { |
| 5645 | garray_T *instr = &cctx->ctx_instr; |
| 5646 | scope_T *scope = cctx->ctx_scope; |
| 5647 | forscope_T *forscope; |
| 5648 | isn_T *isn; |
| 5649 | |
| 5650 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 5651 | { |
| 5652 | emsg(_(e_for)); |
| 5653 | return NULL; |
| 5654 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5655 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5656 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5657 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5658 | |
| 5659 | // At end of ":for" scope jump back to the FOR instruction. |
| 5660 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 5661 | |
| 5662 | // Fill in the "end" label in the FOR statement so it can jump here |
| 5663 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 5664 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 5665 | |
| 5666 | // Fill in the "end" label any BREAK statements |
| 5667 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 5668 | |
| 5669 | // Below the ":for" scope drop the "expr" list from the stack. |
| 5670 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5671 | return NULL; |
| 5672 | |
| 5673 | vim_free(scope); |
| 5674 | |
| 5675 | return arg; |
| 5676 | } |
| 5677 | |
| 5678 | /* |
| 5679 | * compile "while expr" |
| 5680 | * |
| 5681 | * Produces instructions: |
| 5682 | * top: EVAL expr Push result of "expr" |
| 5683 | * JUMP_IF_FALSE end jump if false |
| 5684 | * ... body ... |
| 5685 | * JUMP top Jump back to repeat |
| 5686 | * end: |
| 5687 | * |
| 5688 | */ |
| 5689 | static char_u * |
| 5690 | compile_while(char_u *arg, cctx_T *cctx) |
| 5691 | { |
| 5692 | char_u *p = arg; |
| 5693 | garray_T *instr = &cctx->ctx_instr; |
| 5694 | scope_T *scope; |
| 5695 | |
| 5696 | scope = new_scope(cctx, WHILE_SCOPE); |
| 5697 | if (scope == NULL) |
| 5698 | return NULL; |
| 5699 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5700 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5701 | |
| 5702 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5703 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5704 | return NULL; |
| 5705 | |
| 5706 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5707 | 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] | 5708 | JUMP_IF_FALSE, cctx) == FAIL) |
| 5709 | return FAIL; |
| 5710 | |
| 5711 | return p; |
| 5712 | } |
| 5713 | |
| 5714 | /* |
| 5715 | * compile "endwhile" |
| 5716 | */ |
| 5717 | static char_u * |
| 5718 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 5719 | { |
| 5720 | scope_T *scope = cctx->ctx_scope; |
| 5721 | |
| 5722 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 5723 | { |
| 5724 | emsg(_(e_while)); |
| 5725 | return NULL; |
| 5726 | } |
| 5727 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5728 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5729 | |
| 5730 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5731 | 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] | 5732 | |
| 5733 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 5734 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5735 | 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] | 5736 | |
| 5737 | vim_free(scope); |
| 5738 | |
| 5739 | return arg; |
| 5740 | } |
| 5741 | |
| 5742 | /* |
| 5743 | * compile "continue" |
| 5744 | */ |
| 5745 | static char_u * |
| 5746 | compile_continue(char_u *arg, cctx_T *cctx) |
| 5747 | { |
| 5748 | scope_T *scope = cctx->ctx_scope; |
| 5749 | |
| 5750 | for (;;) |
| 5751 | { |
| 5752 | if (scope == NULL) |
| 5753 | { |
| 5754 | emsg(_(e_continue)); |
| 5755 | return NULL; |
| 5756 | } |
| 5757 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5758 | break; |
| 5759 | scope = scope->se_outer; |
| 5760 | } |
| 5761 | |
| 5762 | // Jump back to the FOR or WHILE instruction. |
| 5763 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5764 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 5765 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5766 | return arg; |
| 5767 | } |
| 5768 | |
| 5769 | /* |
| 5770 | * compile "break" |
| 5771 | */ |
| 5772 | static char_u * |
| 5773 | compile_break(char_u *arg, cctx_T *cctx) |
| 5774 | { |
| 5775 | scope_T *scope = cctx->ctx_scope; |
| 5776 | endlabel_T **el; |
| 5777 | |
| 5778 | for (;;) |
| 5779 | { |
| 5780 | if (scope == NULL) |
| 5781 | { |
| 5782 | emsg(_(e_break)); |
| 5783 | return NULL; |
| 5784 | } |
| 5785 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5786 | break; |
| 5787 | scope = scope->se_outer; |
| 5788 | } |
| 5789 | |
| 5790 | // Jump to the end of the FOR or WHILE loop. |
| 5791 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5792 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5793 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5794 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5795 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 5796 | return FAIL; |
| 5797 | |
| 5798 | return arg; |
| 5799 | } |
| 5800 | |
| 5801 | /* |
| 5802 | * compile "{" start of block |
| 5803 | */ |
| 5804 | static char_u * |
| 5805 | compile_block(char_u *arg, cctx_T *cctx) |
| 5806 | { |
| 5807 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 5808 | return NULL; |
| 5809 | return skipwhite(arg + 1); |
| 5810 | } |
| 5811 | |
| 5812 | /* |
| 5813 | * compile end of block: drop one scope |
| 5814 | */ |
| 5815 | static void |
| 5816 | compile_endblock(cctx_T *cctx) |
| 5817 | { |
| 5818 | scope_T *scope = cctx->ctx_scope; |
| 5819 | |
| 5820 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5821 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5822 | vim_free(scope); |
| 5823 | } |
| 5824 | |
| 5825 | /* |
| 5826 | * compile "try" |
| 5827 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 5828 | * finally. |
| 5829 | * Creates another scope for the "try" block itself. |
| 5830 | * TRY instruction sets up exception handling at runtime. |
| 5831 | * |
| 5832 | * "try" |
| 5833 | * TRY -> catch1, -> finally push trystack entry |
| 5834 | * ... try block |
| 5835 | * "throw {exception}" |
| 5836 | * EVAL {exception} |
| 5837 | * THROW create exception |
| 5838 | * ... try block |
| 5839 | * " catch {expr}" |
| 5840 | * JUMP -> finally |
| 5841 | * catch1: PUSH exeception |
| 5842 | * EVAL {expr} |
| 5843 | * MATCH |
| 5844 | * JUMP nomatch -> catch2 |
| 5845 | * CATCH remove exception |
| 5846 | * ... catch block |
| 5847 | * " catch" |
| 5848 | * JUMP -> finally |
| 5849 | * catch2: CATCH remove exception |
| 5850 | * ... catch block |
| 5851 | * " finally" |
| 5852 | * finally: |
| 5853 | * ... finally block |
| 5854 | * " endtry" |
| 5855 | * ENDTRY pop trystack entry, may rethrow |
| 5856 | */ |
| 5857 | static char_u * |
| 5858 | compile_try(char_u *arg, cctx_T *cctx) |
| 5859 | { |
| 5860 | garray_T *instr = &cctx->ctx_instr; |
| 5861 | scope_T *try_scope; |
| 5862 | scope_T *scope; |
| 5863 | |
| 5864 | // scope that holds the jumps that go to catch/finally/endtry |
| 5865 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 5866 | if (try_scope == NULL) |
| 5867 | return NULL; |
| 5868 | |
| 5869 | // "catch" is set when the first ":catch" is found. |
| 5870 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5871 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5872 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 5873 | return NULL; |
| 5874 | |
| 5875 | // scope for the try block itself |
| 5876 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 5877 | if (scope == NULL) |
| 5878 | return NULL; |
| 5879 | |
| 5880 | return arg; |
| 5881 | } |
| 5882 | |
| 5883 | /* |
| 5884 | * compile "catch {expr}" |
| 5885 | */ |
| 5886 | static char_u * |
| 5887 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 5888 | { |
| 5889 | scope_T *scope = cctx->ctx_scope; |
| 5890 | garray_T *instr = &cctx->ctx_instr; |
| 5891 | char_u *p; |
| 5892 | isn_T *isn; |
| 5893 | |
| 5894 | // end block scope from :try or :catch |
| 5895 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5896 | compile_endblock(cctx); |
| 5897 | scope = cctx->ctx_scope; |
| 5898 | |
| 5899 | // Error if not in a :try scope |
| 5900 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 5901 | { |
| 5902 | emsg(_(e_catch)); |
| 5903 | return NULL; |
| 5904 | } |
| 5905 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5906 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5907 | { |
| 5908 | emsg(_("E1033: catch unreachable after catch-all")); |
| 5909 | return NULL; |
| 5910 | } |
| 5911 | |
| 5912 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5913 | 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] | 5914 | JUMP_ALWAYS, cctx) == FAIL) |
| 5915 | return NULL; |
| 5916 | |
| 5917 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5918 | 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] | 5919 | if (isn->isn_arg.try.try_catch == 0) |
| 5920 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5921 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5922 | { |
| 5923 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5924 | 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] | 5925 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5926 | } |
| 5927 | |
| 5928 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 5929 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5930 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5931 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 5932 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5933 | } |
| 5934 | else |
| 5935 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5936 | char_u *end; |
| 5937 | char_u *pat; |
| 5938 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5939 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 5940 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5941 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5942 | // Push v:exception, push {expr} and MATCH |
| 5943 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 5944 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5945 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5946 | if (*end != *p) |
| 5947 | { |
| 5948 | semsg(_("E1067: Separator mismatch: %s"), p); |
| 5949 | vim_free(tofree); |
| 5950 | return FAIL; |
| 5951 | } |
| 5952 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 5953 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5954 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5955 | len = (int)(end - tofree); |
| 5956 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5957 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 5958 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 5959 | if (pat == NULL) |
| 5960 | return FAIL; |
| 5961 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 5962 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5963 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5964 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 5965 | return NULL; |
| 5966 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5967 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5968 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 5969 | return NULL; |
| 5970 | } |
| 5971 | |
| 5972 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 5973 | return NULL; |
| 5974 | |
| 5975 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 5976 | return NULL; |
| 5977 | return p; |
| 5978 | } |
| 5979 | |
| 5980 | static char_u * |
| 5981 | compile_finally(char_u *arg, cctx_T *cctx) |
| 5982 | { |
| 5983 | scope_T *scope = cctx->ctx_scope; |
| 5984 | garray_T *instr = &cctx->ctx_instr; |
| 5985 | isn_T *isn; |
| 5986 | |
| 5987 | // end block scope from :try or :catch |
| 5988 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 5989 | compile_endblock(cctx); |
| 5990 | scope = cctx->ctx_scope; |
| 5991 | |
| 5992 | // Error if not in a :try scope |
| 5993 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 5994 | { |
| 5995 | emsg(_(e_finally)); |
| 5996 | return NULL; |
| 5997 | } |
| 5998 | |
| 5999 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6000 | 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] | 6001 | if (isn->isn_arg.try.try_finally != 0) |
| 6002 | { |
| 6003 | emsg(_(e_finally_dup)); |
| 6004 | return NULL; |
| 6005 | } |
| 6006 | |
| 6007 | // 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] | 6008 | 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] | 6009 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6010 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6011 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6012 | { |
| 6013 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6014 | 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] | 6015 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6016 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6017 | } |
| 6018 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6019 | // TODO: set index in ts_finally_label jumps |
| 6020 | |
| 6021 | return arg; |
| 6022 | } |
| 6023 | |
| 6024 | static char_u * |
| 6025 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6026 | { |
| 6027 | scope_T *scope = cctx->ctx_scope; |
| 6028 | garray_T *instr = &cctx->ctx_instr; |
| 6029 | isn_T *isn; |
| 6030 | |
| 6031 | // end block scope from :catch or :finally |
| 6032 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6033 | compile_endblock(cctx); |
| 6034 | scope = cctx->ctx_scope; |
| 6035 | |
| 6036 | // Error if not in a :try scope |
| 6037 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6038 | { |
| 6039 | if (scope == NULL) |
| 6040 | emsg(_(e_no_endtry)); |
| 6041 | else if (scope->se_type == WHILE_SCOPE) |
| 6042 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6043 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6044 | emsg(_(e_endfor)); |
| 6045 | else |
| 6046 | emsg(_(e_endif)); |
| 6047 | return NULL; |
| 6048 | } |
| 6049 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6050 | 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] | 6051 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6052 | { |
| 6053 | emsg(_("E1032: missing :catch or :finally")); |
| 6054 | return NULL; |
| 6055 | } |
| 6056 | |
| 6057 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6058 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6059 | 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] | 6060 | |
| 6061 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6062 | if (isn->isn_arg.try.try_catch == 0) |
| 6063 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6064 | if (isn->isn_arg.try.try_finally == 0) |
| 6065 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6066 | |
| 6067 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6068 | { |
| 6069 | // Last catch without match jumps here |
| 6070 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6071 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6072 | } |
| 6073 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6074 | compile_endblock(cctx); |
| 6075 | |
| 6076 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6077 | return NULL; |
| 6078 | return arg; |
| 6079 | } |
| 6080 | |
| 6081 | /* |
| 6082 | * compile "throw {expr}" |
| 6083 | */ |
| 6084 | static char_u * |
| 6085 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6086 | { |
| 6087 | char_u *p = skipwhite(arg); |
| 6088 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6089 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6090 | return NULL; |
| 6091 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6092 | return NULL; |
| 6093 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6094 | return NULL; |
| 6095 | |
| 6096 | return p; |
| 6097 | } |
| 6098 | |
| 6099 | /* |
| 6100 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6101 | * compile "echomsg expr" |
| 6102 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6103 | * compile "execute expr" |
| 6104 | */ |
| 6105 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6106 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6107 | { |
| 6108 | char_u *p = arg; |
| 6109 | int count = 0; |
| 6110 | |
| 6111 | for (;;) |
| 6112 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6113 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6114 | return NULL; |
| 6115 | ++count; |
| 6116 | p = skipwhite(p); |
| 6117 | if (ends_excmd(*p)) |
| 6118 | break; |
| 6119 | } |
| 6120 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6121 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6122 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6123 | else if (cmdidx == CMD_execute) |
| 6124 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6125 | else if (cmdidx == CMD_echomsg) |
| 6126 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6127 | else |
| 6128 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6129 | return p; |
| 6130 | } |
| 6131 | |
| 6132 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6133 | * A command that is not compiled, execute with legacy code. |
| 6134 | */ |
| 6135 | static char_u * |
| 6136 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6137 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6138 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6139 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6140 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6141 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6142 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6143 | goto theend; |
| 6144 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6145 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6146 | { |
| 6147 | long argt = excmd_get_argt(eap->cmdidx); |
| 6148 | int usefilter = FALSE; |
| 6149 | |
| 6150 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6151 | |
| 6152 | // If the command can be followed by a bar, find the bar and truncate |
| 6153 | // it, so that the following command can be compiled. |
| 6154 | // The '|' is overwritten with a NUL, it is put back below. |
| 6155 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6156 | && *eap->arg == '!') |
| 6157 | // :w !filter or :r !filter or :r! filter |
| 6158 | usefilter = TRUE; |
| 6159 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6160 | { |
| 6161 | separate_nextcmd(eap); |
| 6162 | if (eap->nextcmd != NULL) |
| 6163 | nextcmd = eap->nextcmd; |
| 6164 | } |
| 6165 | } |
| 6166 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6167 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6168 | { |
| 6169 | // expand filename in "syntax include [@group] filename" |
| 6170 | has_expr = TRUE; |
| 6171 | eap->arg = skipwhite(eap->arg + 7); |
| 6172 | if (*eap->arg == '@') |
| 6173 | eap->arg = skiptowhite(eap->arg); |
| 6174 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6175 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6176 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6177 | { |
| 6178 | int count = 0; |
| 6179 | char_u *start = skipwhite(line); |
| 6180 | |
| 6181 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6182 | // PUSHS ":cmd xxx" |
| 6183 | // eval expr1 |
| 6184 | // PUSHS "yyy" |
| 6185 | // eval expr2 |
| 6186 | // PUSHS "zzz" |
| 6187 | // EXECCONCAT 5 |
| 6188 | for (;;) |
| 6189 | { |
| 6190 | if (p > start) |
| 6191 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6192 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6193 | ++count; |
| 6194 | } |
| 6195 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6196 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6197 | return NULL; |
| 6198 | may_generate_2STRING(-1, cctx); |
| 6199 | ++count; |
| 6200 | p = skipwhite(p); |
| 6201 | if (*p != '`') |
| 6202 | { |
| 6203 | emsg(_("E1083: missing backtick")); |
| 6204 | return NULL; |
| 6205 | } |
| 6206 | start = p + 1; |
| 6207 | |
| 6208 | p = (char_u *)strstr((char *)start, "`="); |
| 6209 | if (p == NULL) |
| 6210 | { |
| 6211 | if (*skipwhite(start) != NUL) |
| 6212 | { |
| 6213 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6214 | ++count; |
| 6215 | } |
| 6216 | break; |
| 6217 | } |
| 6218 | } |
| 6219 | generate_EXECCONCAT(cctx, count); |
| 6220 | } |
| 6221 | else |
| 6222 | generate_EXEC(cctx, line); |
| 6223 | |
| 6224 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6225 | if (*nextcmd != NUL) |
| 6226 | { |
| 6227 | // the parser expects a pointer to the bar, put it back |
| 6228 | --nextcmd; |
| 6229 | *nextcmd = '|'; |
| 6230 | } |
| 6231 | |
| 6232 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6233 | } |
| 6234 | |
| 6235 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6236 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6237 | * 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] | 6238 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6239 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6240 | add_def_function(ufunc_T *ufunc) |
| 6241 | { |
| 6242 | dfunc_T *dfunc; |
| 6243 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6244 | if (def_functions.ga_len == 0) |
| 6245 | { |
| 6246 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6247 | // wasn't set. |
| 6248 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6249 | return FAIL; |
| 6250 | ++def_functions.ga_len; |
| 6251 | } |
| 6252 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6253 | // Add the function to "def_functions". |
| 6254 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6255 | return FAIL; |
| 6256 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6257 | CLEAR_POINTER(dfunc); |
| 6258 | dfunc->df_idx = def_functions.ga_len; |
| 6259 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6260 | dfunc->df_ufunc = ufunc; |
| 6261 | ++def_functions.ga_len; |
| 6262 | return OK; |
| 6263 | } |
| 6264 | |
| 6265 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6266 | * After ex_function() has collected all the function lines: parse and compile |
| 6267 | * the lines into instructions. |
| 6268 | * Adds the function to "def_functions". |
| 6269 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6270 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6271 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6272 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6273 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6274 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6275 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6276 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6277 | 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] | 6278 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6279 | char_u *line = NULL; |
| 6280 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6281 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6282 | cctx_T cctx; |
| 6283 | garray_T *instr; |
| 6284 | int called_emsg_before = called_emsg; |
| 6285 | int ret = FAIL; |
| 6286 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6287 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6288 | int emsg_before = called_emsg; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6289 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6290 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6291 | // When using a function that was compiled before: Free old instructions. |
| 6292 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6293 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6294 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6295 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6296 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6297 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6298 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6299 | else |
| 6300 | { |
| 6301 | if (add_def_function(ufunc) == FAIL) |
| 6302 | return FAIL; |
| 6303 | new_def_function = TRUE; |
| 6304 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6305 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6306 | ufunc->uf_def_status = UF_COMPILING; |
| 6307 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6308 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6309 | cctx.ctx_ufunc = ufunc; |
| 6310 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6311 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6312 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6313 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6314 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6315 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6316 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6317 | instr = &cctx.ctx_instr; |
| 6318 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6319 | // Set the context to the function, it may be compiled when called from |
| 6320 | // another script. Set the script version to the most modern one. |
| 6321 | // The line number will be set in next_line_from_context(). |
| 6322 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6323 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6324 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6325 | // Make sure error messages are OK. |
| 6326 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6327 | if (do_estack_push) |
| 6328 | estack_push_ufunc(ufunc, 1); |
| 6329 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6330 | if (ufunc->uf_def_args.ga_len > 0) |
| 6331 | { |
| 6332 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6333 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6334 | int i; |
| 6335 | char_u *arg; |
| 6336 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6337 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6338 | |
| 6339 | // Produce instructions for the default values of optional arguments. |
| 6340 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6341 | // where to start when the function is called, depending on the number |
| 6342 | // of arguments. |
| 6343 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6344 | if (ufunc->uf_def_arg_idx == NULL) |
| 6345 | goto erret; |
| 6346 | for (i = 0; i < count; ++i) |
| 6347 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6348 | garray_T *stack = &cctx.ctx_type_stack; |
| 6349 | type_T *val_type; |
| 6350 | int arg_idx = first_def_arg + i; |
| 6351 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6352 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6353 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6354 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6355 | goto erret; |
| 6356 | |
| 6357 | // If no type specified use the type of the default value. |
| 6358 | // Otherwise check that the default value type matches the |
| 6359 | // specified type. |
| 6360 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6361 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6362 | { |
| 6363 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6364 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6365 | } |
Bram Moolenaar | e30f64b | 2020-07-15 19:48:20 +0200 | [diff] [blame] | 6366 | 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] | 6367 | == FAIL) |
| 6368 | { |
| 6369 | arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, |
| 6370 | arg_idx + 1); |
| 6371 | goto erret; |
| 6372 | } |
| 6373 | |
| 6374 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6375 | goto erret; |
| 6376 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6377 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6378 | |
| 6379 | if (did_set_arg_type) |
| 6380 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6381 | } |
| 6382 | |
| 6383 | /* |
| 6384 | * Loop over all the lines of the function and generate instructions. |
| 6385 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6386 | for (;;) |
| 6387 | { |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6388 | exarg_T ea; |
| 6389 | cmdmod_T save_cmdmod; |
| 6390 | int starts_with_colon = FALSE; |
| 6391 | char_u *cmd; |
| 6392 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6393 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6394 | // Bail out on the first error to avoid a flood of errors and report |
| 6395 | // the right line number when inside try/catch. |
| 6396 | if (emsg_before != called_emsg) |
| 6397 | goto erret; |
| 6398 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6399 | if (line != NULL && *line == '|') |
| 6400 | // the line continues after a '|' |
| 6401 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 6402 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6403 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 6404 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6405 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 6406 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6407 | goto erret; |
| 6408 | } |
| 6409 | else |
| 6410 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 6411 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 6412 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6413 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6414 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6415 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6416 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6417 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6418 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6419 | ea.cmdlinep = &line; |
| 6420 | ea.cmd = skipwhite(line); |
| 6421 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6422 | // Some things can be recognized by the first character. |
| 6423 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6424 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6425 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6426 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6427 | if (ea.cmd[1] != '{') |
| 6428 | { |
| 6429 | line = (char_u *)""; |
| 6430 | continue; |
| 6431 | } |
| 6432 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6433 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6434 | case '}': |
| 6435 | { |
| 6436 | // "}" ends a block scope |
| 6437 | scopetype_T stype = cctx.ctx_scope == NULL |
| 6438 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6439 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6440 | if (stype == BLOCK_SCOPE) |
| 6441 | { |
| 6442 | compile_endblock(&cctx); |
| 6443 | line = ea.cmd; |
| 6444 | } |
| 6445 | else |
| 6446 | { |
| 6447 | emsg(_("E1025: using } outside of a block scope")); |
| 6448 | goto erret; |
| 6449 | } |
| 6450 | if (line != NULL) |
| 6451 | line = skipwhite(ea.cmd + 1); |
| 6452 | continue; |
| 6453 | } |
| 6454 | |
| 6455 | case '{': |
| 6456 | // "{" starts a block scope |
| 6457 | // "{'a': 1}->func() is something else |
| 6458 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 6459 | { |
| 6460 | line = compile_block(ea.cmd, &cctx); |
| 6461 | continue; |
| 6462 | } |
| 6463 | break; |
| 6464 | |
| 6465 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6466 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6467 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6468 | } |
| 6469 | |
| 6470 | /* |
| 6471 | * COMMAND MODIFIERS |
| 6472 | */ |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6473 | save_cmdmod = cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6474 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 6475 | { |
| 6476 | if (errormsg != NULL) |
| 6477 | goto erret; |
| 6478 | // empty line or comment |
| 6479 | line = (char_u *)""; |
| 6480 | continue; |
| 6481 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 6482 | // TODO: use modifiers in the command |
| 6483 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6484 | cmdmod = save_cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6485 | |
| 6486 | // Skip ":call" to get to the function name. |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame^] | 6487 | p = ea.cmd; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6488 | if (checkforcmd(&ea.cmd, "call", 3)) |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame^] | 6489 | { |
| 6490 | if (*ea.cmd == '(') |
| 6491 | // not for "call()" |
| 6492 | ea.cmd = p; |
| 6493 | else |
| 6494 | ea.cmd = skipwhite(ea.cmd); |
| 6495 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6496 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6497 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6498 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6499 | char_u *pskip; |
| 6500 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6501 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6502 | // find what follows. |
| 6503 | // Skip over "var.member", "var[idx]" and the like. |
| 6504 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 6505 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6506 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6507 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6508 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6509 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6510 | char_u *var_end; |
| 6511 | int oplen; |
| 6512 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6513 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 6514 | if (ea.cmd[0] == '@') |
| 6515 | var_end = ea.cmd + 2; |
| 6516 | else |
| 6517 | var_end = find_name_end(pskip, NULL, NULL, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6518 | FNE_CHECK_START | FNE_INCL_BR); |
| 6519 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6520 | if (oplen > 0) |
| 6521 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6522 | size_t len = p - ea.cmd; |
| 6523 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6524 | // Recognize an assignment if we recognize the variable |
| 6525 | // name: |
| 6526 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 6527 | // "local = expr" where "local" is a local var. |
| 6528 | // "script = expr" where "script" is a script-local var. |
| 6529 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6530 | // "&opt = expr" |
| 6531 | // "$ENV = expr" |
| 6532 | // "@r = expr" |
| 6533 | if (*ea.cmd == '&' |
| 6534 | || *ea.cmd == '$' |
| 6535 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6536 | || ((len) > 2 && ea.cmd[1] == ':') |
| 6537 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 6538 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 6539 | NULL, &cctx) == OK |
| 6540 | || lookup_script(ea.cmd, len) == OK |
| 6541 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6542 | { |
| 6543 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6544 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6545 | goto erret; |
| 6546 | continue; |
| 6547 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6548 | } |
| 6549 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6550 | |
| 6551 | if (*ea.cmd == '[') |
| 6552 | { |
| 6553 | // [var, var] = expr |
| 6554 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 6555 | if (line == NULL) |
| 6556 | goto erret; |
| 6557 | if (line != ea.cmd) |
| 6558 | continue; |
| 6559 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6560 | } |
| 6561 | |
| 6562 | /* |
| 6563 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6564 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6565 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6566 | cmd = ea.cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6567 | if (*cmd != '\'') |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6568 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6569 | ea.cmd = skip_range(ea.cmd, NULL); |
| 6570 | if (ea.cmd > cmd && !starts_with_colon) |
| 6571 | { |
| 6572 | emsg(_(e_colon_required)); |
| 6573 | goto erret; |
| 6574 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6575 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6576 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6577 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6578 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6579 | |
| 6580 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 6581 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6582 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6583 | { |
| 6584 | line += STRLEN(line); |
| 6585 | continue; |
| 6586 | } |
| 6587 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6588 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6589 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6590 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6591 | // CMD_let cannot happen, compile_assignment() above is used |
| 6592 | iemsg("Command from find_ex_command() not handled"); |
| 6593 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6594 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6595 | } |
| 6596 | |
| 6597 | p = skipwhite(p); |
| 6598 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6599 | if (cctx.ctx_skip == SKIP_YES |
Bram Moolenaar | 7e38003 | 2020-06-19 22:35:44 +0200 | [diff] [blame] | 6600 | && ea.cmdidx != CMD_if |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6601 | && ea.cmdidx != CMD_elseif |
| 6602 | && ea.cmdidx != CMD_else |
| 6603 | && ea.cmdidx != CMD_endif) |
| 6604 | { |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6605 | line = (char_u *)""; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6606 | continue; |
| 6607 | } |
| 6608 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6609 | if (ea.cmdidx != CMD_elseif |
| 6610 | && ea.cmdidx != CMD_else |
| 6611 | && ea.cmdidx != CMD_endif |
| 6612 | && ea.cmdidx != CMD_endfor |
| 6613 | && ea.cmdidx != CMD_endwhile |
| 6614 | && ea.cmdidx != CMD_catch |
| 6615 | && ea.cmdidx != CMD_finally |
| 6616 | && ea.cmdidx != CMD_endtry) |
| 6617 | { |
| 6618 | if (cctx.ctx_had_return) |
| 6619 | { |
| 6620 | emsg(_("E1095: Unreachable code after :return")); |
| 6621 | goto erret; |
| 6622 | } |
| 6623 | } |
| 6624 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6625 | switch (ea.cmdidx) |
| 6626 | { |
| 6627 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6628 | ea.arg = p; |
| 6629 | line = compile_nested_function(&ea, &cctx); |
| 6630 | break; |
| 6631 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6632 | case CMD_function: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6633 | emsg(_("E1086: Cannot use :function inside :def")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6634 | goto erret; |
| 6635 | |
| 6636 | case CMD_return: |
| 6637 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6638 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6639 | break; |
| 6640 | |
| 6641 | case CMD_let: |
| 6642 | case CMD_const: |
| 6643 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6644 | if (line == p) |
| 6645 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6646 | break; |
| 6647 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6648 | case CMD_unlet: |
| 6649 | case CMD_unlockvar: |
| 6650 | case CMD_lockvar: |
| 6651 | line = compile_unletlock(p, &ea, &cctx); |
| 6652 | break; |
| 6653 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6654 | case CMD_import: |
| 6655 | line = compile_import(p, &cctx); |
| 6656 | break; |
| 6657 | |
| 6658 | case CMD_if: |
| 6659 | line = compile_if(p, &cctx); |
| 6660 | break; |
| 6661 | case CMD_elseif: |
| 6662 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6663 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6664 | break; |
| 6665 | case CMD_else: |
| 6666 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6667 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6668 | break; |
| 6669 | case CMD_endif: |
| 6670 | line = compile_endif(p, &cctx); |
| 6671 | break; |
| 6672 | |
| 6673 | case CMD_while: |
| 6674 | line = compile_while(p, &cctx); |
| 6675 | break; |
| 6676 | case CMD_endwhile: |
| 6677 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6678 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6679 | break; |
| 6680 | |
| 6681 | case CMD_for: |
| 6682 | line = compile_for(p, &cctx); |
| 6683 | break; |
| 6684 | case CMD_endfor: |
| 6685 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6686 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6687 | break; |
| 6688 | case CMD_continue: |
| 6689 | line = compile_continue(p, &cctx); |
| 6690 | break; |
| 6691 | case CMD_break: |
| 6692 | line = compile_break(p, &cctx); |
| 6693 | break; |
| 6694 | |
| 6695 | case CMD_try: |
| 6696 | line = compile_try(p, &cctx); |
| 6697 | break; |
| 6698 | case CMD_catch: |
| 6699 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6700 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6701 | break; |
| 6702 | case CMD_finally: |
| 6703 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6704 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6705 | break; |
| 6706 | case CMD_endtry: |
| 6707 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6708 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6709 | break; |
| 6710 | case CMD_throw: |
| 6711 | line = compile_throw(p, &cctx); |
| 6712 | break; |
| 6713 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6714 | case CMD_eval: |
| 6715 | if (compile_expr0(&p, &cctx) == FAIL) |
| 6716 | goto erret; |
| 6717 | |
| 6718 | // drop the return value |
| 6719 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 6720 | |
| 6721 | line = skipwhite(p); |
| 6722 | break; |
| 6723 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6724 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6725 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6726 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6727 | case CMD_echomsg: |
| 6728 | case CMD_echoerr: |
| 6729 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6730 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6731 | |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6732 | // TODO: other commands with an expression argument |
| 6733 | |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 6734 | case CMD_append: |
| 6735 | case CMD_change: |
| 6736 | case CMD_insert: |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 6737 | case CMD_t: |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 6738 | case CMD_xit: |
| 6739 | not_in_vim9(&ea); |
| 6740 | goto erret; |
| 6741 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 6742 | case CMD_SIZE: |
| 6743 | semsg(_("E476: Invalid command: %s"), ea.cmd); |
| 6744 | goto erret; |
| 6745 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6746 | default: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6747 | // Not recognized, execute with do_cmdline_cmd(). |
| 6748 | ea.arg = p; |
| 6749 | line = compile_exec(line, &ea, &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6750 | break; |
| 6751 | } |
| 6752 | if (line == NULL) |
| 6753 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6754 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6755 | |
| 6756 | if (cctx.ctx_type_stack.ga_len < 0) |
| 6757 | { |
| 6758 | iemsg("Type stack underflow"); |
| 6759 | goto erret; |
| 6760 | } |
| 6761 | } |
| 6762 | |
| 6763 | if (cctx.ctx_scope != NULL) |
| 6764 | { |
| 6765 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 6766 | emsg(_(e_endif)); |
| 6767 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 6768 | emsg(_(e_endwhile)); |
| 6769 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 6770 | emsg(_(e_endfor)); |
| 6771 | else |
| 6772 | emsg(_("E1026: Missing }")); |
| 6773 | goto erret; |
| 6774 | } |
| 6775 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6776 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6777 | { |
| 6778 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 6779 | { |
| 6780 | emsg(_("E1027: Missing return statement")); |
| 6781 | goto erret; |
| 6782 | } |
| 6783 | |
| 6784 | // Return zero if there is no return at the end. |
| 6785 | generate_PUSHNR(&cctx, 0); |
| 6786 | generate_instr(&cctx, ISN_RETURN); |
| 6787 | } |
| 6788 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6789 | { |
| 6790 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6791 | + ufunc->uf_dfunc_idx; |
| 6792 | dfunc->df_deleted = FALSE; |
| 6793 | dfunc->df_instr = instr->ga_data; |
| 6794 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6795 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 6796 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6797 | if (cctx.ctx_outer_used) |
| 6798 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6799 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6800 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6801 | |
| 6802 | ret = OK; |
| 6803 | |
| 6804 | erret: |
| 6805 | if (ret == FAIL) |
| 6806 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6807 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6808 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6809 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6810 | |
| 6811 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 6812 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6813 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6814 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6815 | // If using the last entry in the table and it was added above, we |
| 6816 | // might as well remove it. |
| 6817 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 6818 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6819 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6820 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6821 | ufunc->uf_dfunc_idx = 0; |
| 6822 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6823 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6824 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 6825 | while (cctx.ctx_scope != NULL) |
| 6826 | drop_scope(&cctx); |
| 6827 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6828 | // Don't execute this function body. |
| 6829 | ga_clear_strings(&ufunc->uf_lines); |
| 6830 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6831 | if (errormsg != NULL) |
| 6832 | emsg(errormsg); |
| 6833 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 6834 | emsg(_("E1028: compile_def_function failed")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6835 | } |
| 6836 | |
| 6837 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6838 | if (do_estack_push) |
| 6839 | estack_pop(); |
| 6840 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6841 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6842 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6843 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6844 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6845 | } |
| 6846 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 6847 | void |
| 6848 | set_function_type(ufunc_T *ufunc) |
| 6849 | { |
| 6850 | int varargs = ufunc->uf_va_name != NULL; |
| 6851 | int argcount = ufunc->uf_args.ga_len; |
| 6852 | |
| 6853 | // Create a type for the function, with the return type and any |
| 6854 | // argument types. |
| 6855 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 6856 | // The type is included in "tt_args". |
| 6857 | if (argcount > 0 || varargs) |
| 6858 | { |
| 6859 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 6860 | argcount, &ufunc->uf_type_list); |
| 6861 | // Add argument types to the function type. |
| 6862 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 6863 | argcount + varargs, |
| 6864 | &ufunc->uf_type_list) == FAIL) |
| 6865 | return; |
| 6866 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 6867 | ufunc->uf_func_type->tt_min_argcount = |
| 6868 | argcount - ufunc->uf_def_args.ga_len; |
| 6869 | if (ufunc->uf_arg_types == NULL) |
| 6870 | { |
| 6871 | int i; |
| 6872 | |
| 6873 | // lambda does not have argument types. |
| 6874 | for (i = 0; i < argcount; ++i) |
| 6875 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 6876 | } |
| 6877 | else |
| 6878 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 6879 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 6880 | if (varargs) |
| 6881 | { |
| 6882 | ufunc->uf_func_type->tt_args[argcount] = |
| 6883 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 6884 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 6885 | } |
| 6886 | } |
| 6887 | else |
| 6888 | // No arguments, can use a predefined type. |
| 6889 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 6890 | argcount, &ufunc->uf_type_list); |
| 6891 | } |
| 6892 | |
| 6893 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6894 | /* |
| 6895 | * Delete an instruction, free what it contains. |
| 6896 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6897 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6898 | delete_instr(isn_T *isn) |
| 6899 | { |
| 6900 | switch (isn->isn_type) |
| 6901 | { |
| 6902 | case ISN_EXEC: |
| 6903 | case ISN_LOADENV: |
| 6904 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 6905 | case ISN_LOADB: |
| 6906 | case ISN_LOADW: |
| 6907 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6908 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6909 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6910 | case ISN_PUSHEXC: |
| 6911 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6912 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6913 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 6914 | case ISN_STOREB: |
| 6915 | case ISN_STOREW: |
| 6916 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6917 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6918 | vim_free(isn->isn_arg.string); |
| 6919 | break; |
| 6920 | |
| 6921 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 6922 | case ISN_STORES: |
| 6923 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6924 | break; |
| 6925 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6926 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 6927 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6928 | vim_free(isn->isn_arg.unlet.ul_name); |
| 6929 | break; |
| 6930 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6931 | case ISN_STOREOPT: |
| 6932 | vim_free(isn->isn_arg.storeopt.so_name); |
| 6933 | break; |
| 6934 | |
| 6935 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 6936 | blob_unref(isn->isn_arg.blob); |
| 6937 | break; |
| 6938 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6939 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6940 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6941 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6942 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6943 | break; |
| 6944 | |
| 6945 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6946 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6947 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 6948 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6949 | break; |
| 6950 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6951 | case ISN_UCALL: |
| 6952 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 6953 | break; |
| 6954 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 6955 | case ISN_FUNCREF: |
| 6956 | { |
| 6957 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6958 | + isn->isn_arg.funcref.fr_func; |
| 6959 | func_ptr_unref(dfunc->df_ufunc); |
| 6960 | } |
| 6961 | break; |
| 6962 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 6963 | case ISN_NEWFUNC: |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 6964 | { |
| 6965 | char_u *lambda = isn->isn_arg.newfunc.nf_lambda; |
| 6966 | ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL); |
| 6967 | |
| 6968 | if (ufunc != NULL) |
| 6969 | { |
| 6970 | // Clear uf_dfunc_idx so that the function is deleted. |
| 6971 | clear_def_function(ufunc); |
| 6972 | ufunc->uf_dfunc_idx = 0; |
| 6973 | func_ptr_unref(ufunc); |
| 6974 | } |
| 6975 | |
| 6976 | vim_free(lambda); |
| 6977 | vim_free(isn->isn_arg.newfunc.nf_global); |
| 6978 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 6979 | break; |
| 6980 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6981 | case ISN_2BOOL: |
| 6982 | case ISN_2STRING: |
| 6983 | case ISN_ADDBLOB: |
| 6984 | case ISN_ADDLIST: |
| 6985 | case ISN_BCALL: |
| 6986 | case ISN_CATCH: |
| 6987 | case ISN_CHECKNR: |
| 6988 | case ISN_CHECKTYPE: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 6989 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6990 | case ISN_COMPAREANY: |
| 6991 | case ISN_COMPAREBLOB: |
| 6992 | case ISN_COMPAREBOOL: |
| 6993 | case ISN_COMPAREDICT: |
| 6994 | case ISN_COMPAREFLOAT: |
| 6995 | case ISN_COMPAREFUNC: |
| 6996 | case ISN_COMPARELIST: |
| 6997 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6998 | case ISN_COMPARESPECIAL: |
| 6999 | case ISN_COMPARESTRING: |
| 7000 | case ISN_CONCAT: |
| 7001 | case ISN_DCALL: |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 7002 | case ISN_SHUFFLE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7003 | case ISN_DROP: |
| 7004 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7005 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7006 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7007 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7008 | case ISN_EXECCONCAT: |
| 7009 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7010 | case ISN_FOR: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7011 | case ISN_LISTINDEX: |
| 7012 | case ISN_STRINDEX: |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 7013 | case ISN_GETITEM: |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 7014 | case ISN_SLICE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7015 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7016 | case ISN_JUMP: |
| 7017 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7018 | case ISN_LOADBDICT: |
| 7019 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7020 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7021 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7022 | case ISN_LOADSCRIPT: |
| 7023 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7024 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7025 | case ISN_LOADWDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7026 | case ISN_NEGATENR: |
| 7027 | case ISN_NEWDICT: |
| 7028 | case ISN_NEWLIST: |
| 7029 | case ISN_OPNR: |
| 7030 | case ISN_OPFLOAT: |
| 7031 | case ISN_OPANY: |
| 7032 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7033 | case ISN_PCALL_END: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7034 | case ISN_PUSHF: |
| 7035 | case ISN_PUSHNR: |
| 7036 | case ISN_PUSHBOOL: |
| 7037 | case ISN_PUSHSPEC: |
| 7038 | case ISN_RETURN: |
| 7039 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 7040 | case ISN_STOREOUTER: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7041 | case ISN_STOREV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7042 | case ISN_STORENR: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7043 | case ISN_STOREREG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7044 | case ISN_STORESCRIPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7045 | case ISN_STOREDICT: |
| 7046 | case ISN_STORELIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7047 | case ISN_THROW: |
| 7048 | case ISN_TRY: |
| 7049 | // nothing allocated |
| 7050 | break; |
| 7051 | } |
| 7052 | } |
| 7053 | |
| 7054 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7055 | * Free all instructions for "dfunc". |
| 7056 | */ |
| 7057 | static void |
| 7058 | delete_def_function_contents(dfunc_T *dfunc) |
| 7059 | { |
| 7060 | int idx; |
| 7061 | |
| 7062 | ga_clear(&dfunc->df_def_args_isn); |
| 7063 | |
| 7064 | if (dfunc->df_instr != NULL) |
| 7065 | { |
| 7066 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7067 | delete_instr(dfunc->df_instr + idx); |
| 7068 | VIM_CLEAR(dfunc->df_instr); |
| 7069 | } |
| 7070 | |
| 7071 | dfunc->df_deleted = TRUE; |
| 7072 | } |
| 7073 | |
| 7074 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7075 | * When a user function is deleted, clear the contents of any associated def |
| 7076 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7077 | */ |
| 7078 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7079 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7080 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7081 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7082 | { |
| 7083 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7084 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7085 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7086 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7087 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7088 | } |
| 7089 | } |
| 7090 | |
| 7091 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7092 | /* |
| 7093 | * Free all functions defined with ":def". |
| 7094 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7095 | void |
| 7096 | free_def_functions(void) |
| 7097 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7098 | int idx; |
| 7099 | |
| 7100 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7101 | { |
| 7102 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7103 | |
| 7104 | delete_def_function_contents(dfunc); |
| 7105 | } |
| 7106 | |
| 7107 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7108 | } |
| 7109 | #endif |
| 7110 | |
| 7111 | |
| 7112 | #endif // FEAT_EVAL |