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 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 146 | static void delete_def_function_contents(dfunc_T *dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 147 | |
| 148 | /* |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 149 | * Lookup variable "name" in the local scope and return it. |
| 150 | * Return NULL if not found. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 151 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 152 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 153 | lookup_local(char_u *name, size_t len, cctx_T *cctx) |
| 154 | { |
| 155 | int idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 156 | lvar_T *lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 157 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 158 | if (len == 0) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 159 | return NULL; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 160 | |
| 161 | // Find local in current function scope. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 162 | for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx) |
| 163 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 164 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 165 | if (STRNCMP(name, lvar->lv_name, len) == 0 |
| 166 | && STRLEN(lvar->lv_name) == len) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 167 | { |
| 168 | lvar->lv_from_outer = FALSE; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 169 | return lvar; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 170 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 171 | } |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 172 | |
| 173 | // Find local in outer function scope. |
| 174 | if (cctx->ctx_outer != NULL) |
| 175 | { |
| 176 | lvar = lookup_local(name, len, cctx->ctx_outer); |
| 177 | if (lvar != NULL) |
| 178 | { |
| 179 | // TODO: are there situations we should not mark the outer scope as |
| 180 | // used? |
| 181 | cctx->ctx_outer_used = TRUE; |
| 182 | lvar->lv_from_outer = TRUE; |
| 183 | return lvar; |
| 184 | } |
| 185 | } |
| 186 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 187 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 191 | * Lookup an argument in the current function and an enclosing function. |
| 192 | * Returns the argument index in "idxp" |
| 193 | * Returns the argument type in "type" |
| 194 | * Sets "gen_load_outer" to TRUE if found in outer scope. |
| 195 | * Returns OK when found, FAIL otherwise. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 196 | */ |
| 197 | static int |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 198 | lookup_arg( |
| 199 | char_u *name, |
| 200 | size_t len, |
| 201 | int *idxp, |
| 202 | type_T **type, |
| 203 | int *gen_load_outer, |
| 204 | cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 205 | { |
| 206 | int idx; |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 207 | char_u *va_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 208 | |
Bram Moolenaar | ae8d2de | 2020-02-13 21:42:24 +0100 | [diff] [blame] | 209 | if (len == 0) |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 210 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 211 | for (idx = 0; idx < cctx->ctx_ufunc->uf_args.ga_len; ++idx) |
| 212 | { |
| 213 | char_u *arg = FUNCARG(cctx->ctx_ufunc, idx); |
| 214 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 215 | if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL) |
| 216 | { |
| 217 | if (idxp != NULL) |
| 218 | { |
| 219 | // Arguments are located above the frame pointer. One further |
| 220 | // if there is a vararg argument |
| 221 | *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len |
| 222 | + STACK_FRAME_SIZE) |
| 223 | + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0); |
| 224 | |
| 225 | if (cctx->ctx_ufunc->uf_arg_types != NULL) |
| 226 | *type = cctx->ctx_ufunc->uf_arg_types[idx]; |
| 227 | else |
| 228 | *type = &t_any; |
| 229 | } |
| 230 | return OK; |
| 231 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 232 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 233 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 234 | va_name = cctx->ctx_ufunc->uf_va_name; |
| 235 | if (va_name != NULL |
| 236 | && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL) |
| 237 | { |
| 238 | if (idxp != NULL) |
| 239 | { |
| 240 | // varargs is always the last argument |
| 241 | *idxp = -STACK_FRAME_SIZE - 1; |
| 242 | *type = cctx->ctx_ufunc->uf_va_type; |
| 243 | } |
| 244 | return OK; |
| 245 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 246 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 247 | if (cctx->ctx_outer != NULL) |
| 248 | { |
| 249 | // Lookup the name for an argument of the outer function. |
| 250 | if (lookup_arg(name, len, idxp, type, gen_load_outer, cctx->ctx_outer) |
| 251 | == OK) |
| 252 | { |
| 253 | *gen_load_outer = TRUE; |
| 254 | return OK; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 262 | * Returnd TRUE if the script context is Vim9 script. |
| 263 | */ |
| 264 | static int |
| 265 | script_is_vim9() |
| 266 | { |
| 267 | return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9; |
| 268 | } |
| 269 | |
| 270 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 271 | * Lookup a variable in the current script. |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 272 | * If "vim9script" is TRUE the script must be Vim9 script. Used for "var" |
| 273 | * without "s:". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 274 | * Returns OK or FAIL. |
| 275 | */ |
| 276 | static int |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 277 | lookup_script(char_u *name, size_t len, int vim9script) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 278 | { |
| 279 | int cc; |
| 280 | hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid); |
| 281 | dictitem_T *di; |
| 282 | |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 283 | if (vim9script && !script_is_vim9()) |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 284 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 285 | cc = name[len]; |
| 286 | name[len] = NUL; |
| 287 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 288 | name[len] = cc; |
| 289 | return di == NULL ? FAIL: OK; |
| 290 | } |
| 291 | |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 292 | /* |
| 293 | * Check if "p[len]" is already defined, either in script "import_sid" or in |
| 294 | * compilation context "cctx". |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 295 | * Does not check the global namespace. |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 296 | * Return FAIL and give an error if it defined. |
| 297 | */ |
| 298 | int |
Bram Moolenaar | cbb6bdc | 2020-07-06 21:53:17 +0200 | [diff] [blame] | 299 | check_defined(char_u *p, size_t len, cctx_T *cctx) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 300 | { |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 301 | int c = p[len]; |
| 302 | ufunc_T *ufunc = NULL; |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 303 | |
| 304 | p[len] = NUL; |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 305 | if (lookup_script(p, len, FALSE) == OK |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 306 | || (cctx != NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 307 | && (lookup_local(p, len, cctx) != NULL |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 308 | || lookup_arg(p, len, NULL, NULL, NULL, cctx) == OK)) |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 309 | || find_imported(p, len, cctx) != NULL |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 310 | || (ufunc = find_func_even_dead(p, FALSE, cctx)) != NULL) |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 311 | { |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 312 | // A local or script-local function can shadow a global function. |
| 313 | if (ufunc == NULL || !func_is_global(ufunc) |
| 314 | || (p[0] == 'g' && p[1] == ':')) |
| 315 | { |
| 316 | p[len] = c; |
| 317 | semsg(_(e_name_already_defined_str), p); |
| 318 | return FAIL; |
| 319 | } |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 320 | } |
Bram Moolenaar | ad486a0 | 2020-08-01 23:22:18 +0200 | [diff] [blame] | 321 | p[len] = c; |
Bram Moolenaar | 5269bd2 | 2020-03-09 19:25:27 +0100 | [diff] [blame] | 322 | return OK; |
| 323 | } |
| 324 | |
Bram Moolenaar | 65b9545 | 2020-07-19 14:03:09 +0200 | [diff] [blame] | 325 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 326 | ///////////////////////////////////////////////////////////////////// |
| 327 | // Following generate_ functions expect the caller to call ga_grow(). |
| 328 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 329 | #define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL |
| 330 | #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] | 331 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 332 | /* |
| 333 | * Generate an instruction without arguments. |
| 334 | * Returns a pointer to the new instruction, NULL if failed. |
| 335 | */ |
| 336 | static isn_T * |
| 337 | generate_instr(cctx_T *cctx, isntype_T isn_type) |
| 338 | { |
| 339 | garray_T *instr = &cctx->ctx_instr; |
| 340 | isn_T *isn; |
| 341 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 342 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 343 | if (ga_grow(instr, 1) == FAIL) |
| 344 | return NULL; |
| 345 | isn = ((isn_T *)instr->ga_data) + instr->ga_len; |
| 346 | isn->isn_type = isn_type; |
| 347 | isn->isn_lnum = cctx->ctx_lnum + 1; |
| 348 | ++instr->ga_len; |
| 349 | |
| 350 | return isn; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Generate an instruction without arguments. |
| 355 | * "drop" will be removed from the stack. |
| 356 | * Returns a pointer to the new instruction, NULL if failed. |
| 357 | */ |
| 358 | static isn_T * |
| 359 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 360 | { |
| 361 | garray_T *stack = &cctx->ctx_type_stack; |
| 362 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 363 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 364 | stack->ga_len -= drop; |
| 365 | return generate_instr(cctx, isn_type); |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Generate instruction "isn_type" and put "type" on the type stack. |
| 370 | */ |
| 371 | static isn_T * |
| 372 | generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type) |
| 373 | { |
| 374 | isn_T *isn; |
| 375 | garray_T *stack = &cctx->ctx_type_stack; |
| 376 | |
| 377 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 378 | return NULL; |
| 379 | |
| 380 | if (ga_grow(stack, 1) == FAIL) |
| 381 | return NULL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 382 | ((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] | 383 | ++stack->ga_len; |
| 384 | |
| 385 | return isn; |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 390 | * But only for simple types. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 391 | */ |
| 392 | static int |
| 393 | may_generate_2STRING(int offset, cctx_T *cctx) |
| 394 | { |
| 395 | isn_T *isn; |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 396 | isntype_T isntype = ISN_2STRING; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 397 | garray_T *stack = &cctx->ctx_type_stack; |
| 398 | type_T **type = ((type_T **)stack->ga_data) + stack->ga_len + offset; |
| 399 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 400 | switch ((*type)->tt_type) |
| 401 | { |
| 402 | // nothing to be done |
| 403 | case VAR_STRING: return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 404 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 405 | // conversion possible |
| 406 | case VAR_SPECIAL: |
| 407 | case VAR_BOOL: |
| 408 | case VAR_NUMBER: |
| 409 | case VAR_FLOAT: |
| 410 | break; |
| 411 | |
| 412 | // conversion possible (with runtime check) |
| 413 | case VAR_ANY: |
| 414 | case VAR_UNKNOWN: |
| 415 | isntype = ISN_2STRING_ANY; |
| 416 | break; |
| 417 | |
| 418 | // conversion not possible |
| 419 | case VAR_VOID: |
| 420 | case VAR_BLOB: |
| 421 | case VAR_FUNC: |
| 422 | case VAR_PARTIAL: |
| 423 | case VAR_LIST: |
| 424 | case VAR_DICT: |
| 425 | case VAR_JOB: |
| 426 | case VAR_CHANNEL: |
| 427 | to_string_error((*type)->tt_type); |
| 428 | return FAIL; |
| 429 | } |
| 430 | |
| 431 | *type = &t_string; |
| 432 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 433 | return FAIL; |
| 434 | isn->isn_arg.number = offset; |
| 435 | |
| 436 | return OK; |
| 437 | } |
| 438 | |
| 439 | static int |
| 440 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 441 | { |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 442 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT || type1 == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 443 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 444 | || type2 == VAR_ANY))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 445 | { |
| 446 | if (*op == '+') |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 447 | emsg(_(e_wrong_argument_type_for_plus)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 448 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 449 | semsg(_(e_char_requires_number_or_float_arguments), *op); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 450 | return FAIL; |
| 451 | } |
| 452 | return OK; |
| 453 | } |
| 454 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 455 | static int |
| 456 | generate_add_instr( |
| 457 | cctx_T *cctx, |
| 458 | vartype_T vartype, |
| 459 | type_T *type1, |
| 460 | type_T *type2) |
| 461 | { |
| 462 | isn_T *isn = generate_instr_drop(cctx, |
| 463 | vartype == VAR_NUMBER ? ISN_OPNR |
| 464 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 465 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 466 | #ifdef FEAT_FLOAT |
| 467 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 468 | #endif |
| 469 | : ISN_OPANY, 1); |
| 470 | |
| 471 | if (vartype != VAR_LIST && vartype != VAR_BLOB |
| 472 | && type1->tt_type != VAR_ANY |
| 473 | && type2->tt_type != VAR_ANY |
| 474 | && check_number_or_float( |
| 475 | type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL) |
| 476 | return FAIL; |
| 477 | |
| 478 | if (isn != NULL) |
| 479 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 480 | return isn == NULL ? FAIL : OK; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Get the type to use for an instruction for an operation on "type1" and |
| 485 | * "type2". If they are matching use a type-specific instruction. Otherwise |
| 486 | * fall back to runtime type checking. |
| 487 | */ |
| 488 | static vartype_T |
| 489 | operator_type(type_T *type1, type_T *type2) |
| 490 | { |
| 491 | if (type1->tt_type == type2->tt_type |
| 492 | && (type1->tt_type == VAR_NUMBER |
| 493 | || type1->tt_type == VAR_LIST |
| 494 | #ifdef FEAT_FLOAT |
| 495 | || type1->tt_type == VAR_FLOAT |
| 496 | #endif |
| 497 | || type1->tt_type == VAR_BLOB)) |
| 498 | return type1->tt_type; |
| 499 | return VAR_ANY; |
| 500 | } |
| 501 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 502 | /* |
| 503 | * Generate an instruction with two arguments. The instruction depends on the |
| 504 | * type of the arguments. |
| 505 | */ |
| 506 | static int |
| 507 | generate_two_op(cctx_T *cctx, char_u *op) |
| 508 | { |
| 509 | garray_T *stack = &cctx->ctx_type_stack; |
| 510 | type_T *type1; |
| 511 | type_T *type2; |
| 512 | vartype_T vartype; |
| 513 | isn_T *isn; |
| 514 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 515 | RETURN_OK_IF_SKIP(cctx); |
| 516 | |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 517 | // Get the known type of the two items on the stack. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 518 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 519 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 520 | vartype = operator_type(type1, type2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 521 | |
| 522 | switch (*op) |
| 523 | { |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 524 | case '+': |
| 525 | if (generate_add_instr(cctx, vartype, type1, type2) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 526 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 527 | break; |
| 528 | |
| 529 | case '-': |
| 530 | case '*': |
| 531 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 532 | op) == FAIL) |
| 533 | return FAIL; |
| 534 | if (vartype == VAR_NUMBER) |
| 535 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 536 | #ifdef FEAT_FLOAT |
| 537 | else if (vartype == VAR_FLOAT) |
| 538 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 539 | #endif |
| 540 | else |
| 541 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 542 | if (isn != NULL) |
| 543 | isn->isn_arg.op.op_type = *op == '*' |
| 544 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 545 | break; |
| 546 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 547 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 548 | && type1->tt_type != VAR_NUMBER) |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 549 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 550 | && type2->tt_type != VAR_NUMBER)) |
| 551 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 552 | emsg(_(e_percent_requires_number_arguments)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 553 | return FAIL; |
| 554 | } |
| 555 | isn = generate_instr_drop(cctx, |
| 556 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 557 | if (isn != NULL) |
| 558 | isn->isn_arg.op.op_type = EXPR_REM; |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | // correct type of result |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 563 | if (vartype == VAR_ANY) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 564 | { |
| 565 | type_T *type = &t_any; |
| 566 | |
| 567 | #ifdef FEAT_FLOAT |
| 568 | // float+number and number+float results in float |
| 569 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 570 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 571 | type = &t_float; |
| 572 | #endif |
| 573 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 574 | } |
| 575 | |
| 576 | return OK; |
| 577 | } |
| 578 | |
| 579 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 580 | * Get the instruction to use for comparing "type1" with "type2" |
| 581 | * Return ISN_DROP when failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 582 | */ |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 583 | static isntype_T |
| 584 | get_compare_isn(exptype_T exptype, vartype_T type1, vartype_T type2) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 585 | { |
| 586 | isntype_T isntype = ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 587 | |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 588 | if (type1 == VAR_UNKNOWN) |
| 589 | type1 = VAR_ANY; |
| 590 | if (type2 == VAR_UNKNOWN) |
| 591 | type2 = VAR_ANY; |
| 592 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 593 | if (type1 == type2) |
| 594 | { |
| 595 | switch (type1) |
| 596 | { |
| 597 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 598 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 599 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 600 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 601 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 602 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 603 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 604 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 605 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 606 | default: isntype = ISN_COMPAREANY; break; |
| 607 | } |
| 608 | } |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 609 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 610 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 611 | && (type2 == VAR_NUMBER || type2 ==VAR_FLOAT))) |
| 612 | isntype = ISN_COMPAREANY; |
| 613 | |
| 614 | if ((exptype == EXPR_IS || exptype == EXPR_ISNOT) |
| 615 | && (isntype == ISN_COMPAREBOOL |
| 616 | || isntype == ISN_COMPARESPECIAL |
| 617 | || isntype == ISN_COMPARENR |
| 618 | || isntype == ISN_COMPAREFLOAT)) |
| 619 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 620 | semsg(_(e_cannot_use_str_with_str), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 621 | exptype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 622 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 623 | } |
| 624 | if (isntype == ISN_DROP |
| 625 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 626 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 627 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 628 | || ((exptype != EXPR_EQUAL && exptype != EXPR_NEQUAL |
| 629 | && exptype != EXPR_IS && exptype != EXPR_ISNOT |
| 630 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 631 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 632 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 633 | semsg(_(e_cannot_compare_str_with_str), |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 634 | vartype_name(type1), vartype_name(type2)); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 635 | return ISN_DROP; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 636 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 637 | return isntype; |
| 638 | } |
| 639 | |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 640 | int |
| 641 | check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2) |
| 642 | { |
| 643 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 644 | return FAIL; |
| 645 | return OK; |
| 646 | } |
| 647 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 648 | /* |
| 649 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 650 | */ |
| 651 | static int |
| 652 | generate_COMPARE(cctx_T *cctx, exptype_T exptype, int ic) |
| 653 | { |
| 654 | isntype_T isntype; |
| 655 | isn_T *isn; |
| 656 | garray_T *stack = &cctx->ctx_type_stack; |
| 657 | vartype_T type1; |
| 658 | vartype_T type2; |
| 659 | |
| 660 | RETURN_OK_IF_SKIP(cctx); |
| 661 | |
| 662 | // Get the known type of the two items on the stack. If they are matching |
| 663 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 664 | // checking. |
| 665 | type1 = ((type_T **)stack->ga_data)[stack->ga_len - 2]->tt_type; |
| 666 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type; |
| 667 | isntype = get_compare_isn(exptype, type1, type2); |
| 668 | if (isntype == ISN_DROP) |
| 669 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 670 | |
| 671 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 672 | return FAIL; |
| 673 | isn->isn_arg.op.op_type = exptype; |
| 674 | isn->isn_arg.op.op_ic = ic; |
| 675 | |
| 676 | // takes two arguments, puts one bool back |
| 677 | if (stack->ga_len >= 2) |
| 678 | { |
| 679 | --stack->ga_len; |
| 680 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 681 | } |
| 682 | |
| 683 | return OK; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Generate an ISN_2BOOL instruction. |
| 688 | */ |
| 689 | static int |
| 690 | generate_2BOOL(cctx_T *cctx, int invert) |
| 691 | { |
| 692 | isn_T *isn; |
| 693 | garray_T *stack = &cctx->ctx_type_stack; |
| 694 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 695 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 696 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 697 | return FAIL; |
| 698 | isn->isn_arg.number = invert; |
| 699 | |
| 700 | // type becomes bool |
| 701 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = &t_bool; |
| 702 | |
| 703 | return OK; |
| 704 | } |
| 705 | |
| 706 | static int |
| 707 | generate_TYPECHECK(cctx_T *cctx, type_T *vartype, int offset) |
| 708 | { |
| 709 | isn_T *isn; |
| 710 | garray_T *stack = &cctx->ctx_type_stack; |
| 711 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 712 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 713 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 714 | return FAIL; |
Bram Moolenaar | 939b5db | 2020-04-28 22:49:08 +0200 | [diff] [blame] | 715 | // TODO: whole type, e.g. for a function also arg and return types |
| 716 | isn->isn_arg.type.ct_type = vartype->tt_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 717 | isn->isn_arg.type.ct_off = offset; |
| 718 | |
| 719 | // type becomes vartype |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 720 | ((type_T **)stack->ga_data)[stack->ga_len + offset] = vartype; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 721 | |
| 722 | return OK; |
| 723 | } |
| 724 | |
| 725 | /* |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 726 | * Check that |
| 727 | * - "actual" is "expected" type or |
| 728 | * - "actual" is a type that can be "expected" type: add a runtime check; or |
| 729 | * - return FAIL. |
| 730 | */ |
| 731 | static int |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 732 | need_type( |
| 733 | type_T *actual, |
| 734 | type_T *expected, |
| 735 | int offset, |
| 736 | cctx_T *cctx, |
| 737 | int silent) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 738 | { |
Bram Moolenaar | 4ed124c | 2020-09-09 20:03:46 +0200 | [diff] [blame] | 739 | if (expected == &t_bool && actual != &t_bool |
| 740 | && (actual->tt_flags & TTFLAG_BOOL_OK)) |
| 741 | { |
| 742 | // Using "0", "1" or the result of an expression with "&&" or "||" as a |
| 743 | // boolean is OK but requires a conversion. |
| 744 | generate_2BOOL(cctx, FALSE); |
| 745 | return OK; |
| 746 | } |
| 747 | |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 748 | if (check_type(expected, actual, FALSE, 0) == OK) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 749 | return OK; |
| 750 | if (actual->tt_type != VAR_ANY |
| 751 | && actual->tt_type != VAR_UNKNOWN |
| 752 | && !(actual->tt_type == VAR_FUNC |
| 753 | && (actual->tt_member == &t_any || actual->tt_argcount < 0))) |
| 754 | { |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 755 | if (!silent) |
| 756 | type_mismatch(expected, actual); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 757 | return FAIL; |
| 758 | } |
| 759 | generate_TYPECHECK(cctx, expected, offset); |
| 760 | return OK; |
| 761 | } |
| 762 | |
| 763 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 764 | * Generate an ISN_PUSHNR instruction. |
| 765 | */ |
| 766 | static int |
| 767 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 768 | { |
| 769 | isn_T *isn; |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 770 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 771 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 772 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 773 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 774 | return FAIL; |
| 775 | isn->isn_arg.number = number; |
| 776 | |
Bram Moolenaar | 29a86ff | 2020-09-09 14:55:31 +0200 | [diff] [blame] | 777 | if (number == 0 || number == 1) |
| 778 | { |
| 779 | type_T *type = alloc_type(cctx->ctx_type_list); |
| 780 | |
| 781 | // A 0 or 1 number can also be used as a bool. |
| 782 | if (type != NULL) |
| 783 | { |
| 784 | type->tt_type = VAR_NUMBER; |
| 785 | type->tt_flags = TTFLAG_BOOL_OK; |
| 786 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type; |
| 787 | } |
| 788 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 789 | return OK; |
| 790 | } |
| 791 | |
| 792 | /* |
| 793 | * Generate an ISN_PUSHBOOL instruction. |
| 794 | */ |
| 795 | static int |
| 796 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 797 | { |
| 798 | isn_T *isn; |
| 799 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 800 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 801 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 802 | return FAIL; |
| 803 | isn->isn_arg.number = number; |
| 804 | |
| 805 | return OK; |
| 806 | } |
| 807 | |
| 808 | /* |
| 809 | * Generate an ISN_PUSHSPEC instruction. |
| 810 | */ |
| 811 | static int |
| 812 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 813 | { |
| 814 | isn_T *isn; |
| 815 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 816 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 817 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 818 | return FAIL; |
| 819 | isn->isn_arg.number = number; |
| 820 | |
| 821 | return OK; |
| 822 | } |
| 823 | |
| 824 | #ifdef FEAT_FLOAT |
| 825 | /* |
| 826 | * Generate an ISN_PUSHF instruction. |
| 827 | */ |
| 828 | static int |
| 829 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 830 | { |
| 831 | isn_T *isn; |
| 832 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 833 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 834 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 835 | return FAIL; |
| 836 | isn->isn_arg.fnumber = fnumber; |
| 837 | |
| 838 | return OK; |
| 839 | } |
| 840 | #endif |
| 841 | |
| 842 | /* |
| 843 | * Generate an ISN_PUSHS instruction. |
| 844 | * Consumes "str". |
| 845 | */ |
| 846 | static int |
| 847 | generate_PUSHS(cctx_T *cctx, char_u *str) |
| 848 | { |
| 849 | isn_T *isn; |
| 850 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 851 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 852 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 853 | return FAIL; |
| 854 | isn->isn_arg.string = str; |
| 855 | |
| 856 | return OK; |
| 857 | } |
| 858 | |
| 859 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 860 | * Generate an ISN_PUSHCHANNEL instruction. |
| 861 | * Consumes "channel". |
| 862 | */ |
| 863 | static int |
| 864 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 865 | { |
| 866 | isn_T *isn; |
| 867 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 868 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 869 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 870 | return FAIL; |
| 871 | isn->isn_arg.channel = channel; |
| 872 | |
| 873 | return OK; |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * Generate an ISN_PUSHJOB instruction. |
| 878 | * Consumes "job". |
| 879 | */ |
| 880 | static int |
| 881 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 882 | { |
| 883 | isn_T *isn; |
| 884 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 885 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 886 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 887 | return FAIL; |
| 888 | isn->isn_arg.job = job; |
| 889 | |
| 890 | return OK; |
| 891 | } |
| 892 | |
| 893 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 894 | * Generate an ISN_PUSHBLOB instruction. |
| 895 | * Consumes "blob". |
| 896 | */ |
| 897 | static int |
| 898 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 899 | { |
| 900 | isn_T *isn; |
| 901 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 902 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 903 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 904 | return FAIL; |
| 905 | isn->isn_arg.blob = blob; |
| 906 | |
| 907 | return OK; |
| 908 | } |
| 909 | |
| 910 | /* |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 911 | * Generate an ISN_PUSHFUNC instruction with name "name". |
| 912 | * Consumes "name". |
| 913 | */ |
| 914 | static int |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 915 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 916 | { |
| 917 | isn_T *isn; |
| 918 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 919 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 920 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 921 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 922 | isn->isn_arg.string = name == NULL ? NULL : vim_strsave(name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 923 | |
| 924 | return OK; |
| 925 | } |
| 926 | |
| 927 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 928 | * Generate an ISN_GETITEM instruction with "index". |
| 929 | */ |
| 930 | static int |
| 931 | generate_GETITEM(cctx_T *cctx, int index) |
| 932 | { |
| 933 | isn_T *isn; |
| 934 | garray_T *stack = &cctx->ctx_type_stack; |
| 935 | type_T *type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 936 | type_T *item_type = &t_any; |
| 937 | |
| 938 | RETURN_OK_IF_SKIP(cctx); |
| 939 | |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 940 | if (type->tt_type != VAR_LIST) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 941 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 942 | // cannot happen, caller has checked the type |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 943 | emsg(_(e_listreq)); |
| 944 | return FAIL; |
| 945 | } |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 946 | item_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 947 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 948 | return FAIL; |
| 949 | isn->isn_arg.number = index; |
| 950 | |
| 951 | // add the item type to the type stack |
| 952 | if (ga_grow(stack, 1) == FAIL) |
| 953 | return FAIL; |
| 954 | ((type_T **)stack->ga_data)[stack->ga_len] = item_type; |
| 955 | ++stack->ga_len; |
| 956 | return OK; |
| 957 | } |
| 958 | |
| 959 | /* |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 960 | * Generate an ISN_SLICE instruction with "count". |
| 961 | */ |
| 962 | static int |
| 963 | generate_SLICE(cctx_T *cctx, int count) |
| 964 | { |
| 965 | isn_T *isn; |
| 966 | |
| 967 | RETURN_OK_IF_SKIP(cctx); |
| 968 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 969 | return FAIL; |
| 970 | isn->isn_arg.number = count; |
| 971 | return OK; |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 976 | */ |
| 977 | static int |
| 978 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 979 | { |
| 980 | isn_T *isn; |
| 981 | |
| 982 | RETURN_OK_IF_SKIP(cctx); |
| 983 | |
| 984 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 985 | return FAIL; |
| 986 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 987 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 988 | |
| 989 | return OK; |
| 990 | } |
| 991 | |
| 992 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 993 | * Generate an ISN_STORE instruction. |
| 994 | */ |
| 995 | static int |
| 996 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 997 | { |
| 998 | isn_T *isn; |
| 999 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1000 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1001 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 1002 | return FAIL; |
| 1003 | if (name != NULL) |
| 1004 | isn->isn_arg.string = vim_strsave(name); |
| 1005 | else |
| 1006 | isn->isn_arg.number = idx; |
| 1007 | |
| 1008 | return OK; |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 1013 | */ |
| 1014 | static int |
| 1015 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 1016 | { |
| 1017 | isn_T *isn; |
| 1018 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1019 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1020 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 1021 | return FAIL; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1022 | isn->isn_arg.storenr.stnr_idx = idx; |
| 1023 | isn->isn_arg.storenr.stnr_val = value; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1024 | |
| 1025 | return OK; |
| 1026 | } |
| 1027 | |
| 1028 | /* |
| 1029 | * Generate an ISN_STOREOPT instruction |
| 1030 | */ |
| 1031 | static int |
| 1032 | generate_STOREOPT(cctx_T *cctx, char_u *name, int opt_flags) |
| 1033 | { |
| 1034 | isn_T *isn; |
| 1035 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1036 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1c199f9 | 2020-08-07 21:28:34 +0200 | [diff] [blame] | 1037 | if ((isn = generate_instr_drop(cctx, ISN_STOREOPT, 1)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1038 | return FAIL; |
| 1039 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 1040 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 1041 | |
| 1042 | return OK; |
| 1043 | } |
| 1044 | |
| 1045 | /* |
| 1046 | * Generate an ISN_LOAD or similar instruction. |
| 1047 | */ |
| 1048 | static int |
| 1049 | generate_LOAD( |
| 1050 | cctx_T *cctx, |
| 1051 | isntype_T isn_type, |
| 1052 | int idx, |
| 1053 | char_u *name, |
| 1054 | type_T *type) |
| 1055 | { |
| 1056 | isn_T *isn; |
| 1057 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1058 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1059 | if ((isn = generate_instr_type(cctx, isn_type, type)) == NULL) |
| 1060 | return FAIL; |
| 1061 | if (name != NULL) |
| 1062 | isn->isn_arg.string = vim_strsave(name); |
| 1063 | else |
| 1064 | isn->isn_arg.number = idx; |
| 1065 | |
| 1066 | return OK; |
| 1067 | } |
| 1068 | |
| 1069 | /* |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1070 | * Generate an ISN_LOADV instruction for v:var. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1071 | */ |
| 1072 | static int |
| 1073 | generate_LOADV( |
| 1074 | cctx_T *cctx, |
| 1075 | char_u *name, |
| 1076 | int error) |
| 1077 | { |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1078 | int di_flags; |
| 1079 | int vidx = find_vim_var(name, &di_flags); |
| 1080 | type_T *type; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1081 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1082 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1083 | if (vidx < 0) |
| 1084 | { |
| 1085 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1086 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1087 | return FAIL; |
| 1088 | } |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 1089 | type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1090 | |
Bram Moolenaar | 5da356e | 2020-04-09 19:34:43 +0200 | [diff] [blame] | 1091 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1095 | * Generate an ISN_UNLET instruction. |
| 1096 | */ |
| 1097 | static int |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1098 | 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] | 1099 | { |
| 1100 | isn_T *isn; |
| 1101 | |
| 1102 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1103 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1104 | return FAIL; |
| 1105 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 1106 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 1107 | |
| 1108 | return OK; |
| 1109 | } |
| 1110 | |
| 1111 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1112 | * Generate an ISN_LOADS instruction. |
| 1113 | */ |
| 1114 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1115 | generate_OLDSCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1116 | cctx_T *cctx, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1117 | isntype_T isn_type, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1118 | char_u *name, |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1119 | int sid, |
| 1120 | type_T *type) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1121 | { |
| 1122 | isn_T *isn; |
| 1123 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1124 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1125 | if (isn_type == ISN_LOADS) |
| 1126 | isn = generate_instr_type(cctx, isn_type, type); |
| 1127 | else |
| 1128 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1129 | if (isn == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1130 | return FAIL; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1131 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1132 | isn->isn_arg.loadstore.ls_sid = sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1133 | |
| 1134 | return OK; |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1139 | */ |
| 1140 | static int |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1141 | generate_VIM9SCRIPT( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1142 | cctx_T *cctx, |
| 1143 | isntype_T isn_type, |
| 1144 | int sid, |
| 1145 | int idx, |
| 1146 | type_T *type) |
| 1147 | { |
| 1148 | isn_T *isn; |
| 1149 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1150 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1151 | if (isn_type == ISN_LOADSCRIPT) |
| 1152 | isn = generate_instr_type(cctx, isn_type, type); |
| 1153 | else |
| 1154 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1155 | if (isn == NULL) |
| 1156 | return FAIL; |
| 1157 | isn->isn_arg.script.script_sid = sid; |
| 1158 | isn->isn_arg.script.script_idx = idx; |
| 1159 | return OK; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * Generate an ISN_NEWLIST instruction. |
| 1164 | */ |
| 1165 | static int |
| 1166 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1167 | { |
| 1168 | isn_T *isn; |
| 1169 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1170 | type_T *type; |
| 1171 | type_T *member; |
| 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_NEWLIST)) == NULL) |
| 1175 | return FAIL; |
| 1176 | isn->isn_arg.number = count; |
| 1177 | |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1178 | // get the member type from all the items on the stack. |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1179 | if (count == 0) |
| 1180 | member = &t_void; |
| 1181 | else |
| 1182 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1183 | ((type_T **)stack->ga_data) + stack->ga_len, count, 1, |
| 1184 | cctx->ctx_type_list); |
| 1185 | type = get_list_type(member, cctx->ctx_type_list); |
| 1186 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1187 | // drop the value types |
| 1188 | stack->ga_len -= count; |
| 1189 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1190 | // add the list type to the type stack |
| 1191 | if (ga_grow(stack, 1) == FAIL) |
| 1192 | return FAIL; |
| 1193 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1194 | ++stack->ga_len; |
| 1195 | |
| 1196 | return OK; |
| 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * Generate an ISN_NEWDICT instruction. |
| 1201 | */ |
| 1202 | static int |
| 1203 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1204 | { |
| 1205 | isn_T *isn; |
| 1206 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1207 | type_T *type; |
| 1208 | type_T *member; |
| 1209 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1210 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1211 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1212 | return FAIL; |
| 1213 | isn->isn_arg.number = count; |
| 1214 | |
Bram Moolenaar | 9c2b066 | 2020-09-01 19:56:15 +0200 | [diff] [blame] | 1215 | if (count == 0) |
| 1216 | member = &t_void; |
| 1217 | else |
| 1218 | member = get_member_type_from_stack( |
Bram Moolenaar | 127542b | 2020-08-09 17:22:04 +0200 | [diff] [blame] | 1219 | ((type_T **)stack->ga_data) + stack->ga_len, count, 2, |
| 1220 | cctx->ctx_type_list); |
| 1221 | type = get_dict_type(member, cctx->ctx_type_list); |
| 1222 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1223 | // drop the key and value types |
| 1224 | stack->ga_len -= 2 * count; |
| 1225 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1226 | // add the dict type to the type stack |
| 1227 | if (ga_grow(stack, 1) == FAIL) |
| 1228 | return FAIL; |
| 1229 | ((type_T **)stack->ga_data)[stack->ga_len] = type; |
| 1230 | ++stack->ga_len; |
| 1231 | |
| 1232 | return OK; |
| 1233 | } |
| 1234 | |
| 1235 | /* |
| 1236 | * Generate an ISN_FUNCREF instruction. |
| 1237 | */ |
| 1238 | static int |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1239 | generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1240 | { |
| 1241 | isn_T *isn; |
| 1242 | garray_T *stack = &cctx->ctx_type_stack; |
| 1243 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1244 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1245 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1246 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1247 | isn->isn_arg.funcref.fr_func = ufunc->uf_dfunc_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1248 | isn->isn_arg.funcref.fr_var_idx = cctx->ctx_closure_count++; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1249 | |
| 1250 | if (ga_grow(stack, 1) == FAIL) |
| 1251 | return FAIL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1252 | ((type_T **)stack->ga_data)[stack->ga_len] = |
| 1253 | ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1254 | ++stack->ga_len; |
| 1255 | |
| 1256 | return OK; |
| 1257 | } |
| 1258 | |
| 1259 | /* |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 1260 | * Generate an ISN_NEWFUNC instruction. |
| 1261 | */ |
| 1262 | static int |
| 1263 | generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) |
| 1264 | { |
| 1265 | isn_T *isn; |
| 1266 | char_u *name; |
| 1267 | |
| 1268 | RETURN_OK_IF_SKIP(cctx); |
| 1269 | name = vim_strsave(lambda_name); |
| 1270 | if (name == NULL) |
| 1271 | return FAIL; |
| 1272 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
| 1273 | return FAIL; |
| 1274 | isn->isn_arg.newfunc.nf_lambda = name; |
| 1275 | isn->isn_arg.newfunc.nf_global = func_name; |
| 1276 | |
| 1277 | return OK; |
| 1278 | } |
| 1279 | |
| 1280 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1281 | * Generate an ISN_JUMP instruction. |
| 1282 | */ |
| 1283 | static int |
| 1284 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1285 | { |
| 1286 | isn_T *isn; |
| 1287 | garray_T *stack = &cctx->ctx_type_stack; |
| 1288 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1289 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1290 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1291 | return FAIL; |
| 1292 | isn->isn_arg.jump.jump_when = when; |
| 1293 | isn->isn_arg.jump.jump_where = where; |
| 1294 | |
| 1295 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1296 | --stack->ga_len; |
| 1297 | |
| 1298 | return OK; |
| 1299 | } |
| 1300 | |
| 1301 | static int |
| 1302 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1303 | { |
| 1304 | isn_T *isn; |
| 1305 | garray_T *stack = &cctx->ctx_type_stack; |
| 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 ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1309 | return FAIL; |
| 1310 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1311 | |
| 1312 | if (ga_grow(stack, 1) == FAIL) |
| 1313 | return FAIL; |
| 1314 | // type doesn't matter, will be stored next |
| 1315 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1316 | ++stack->ga_len; |
| 1317 | |
| 1318 | return OK; |
| 1319 | } |
| 1320 | |
| 1321 | /* |
| 1322 | * Generate an ISN_BCALL instruction. |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1323 | * "method_call" is TRUE for "value->method()" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1324 | * Return FAIL if the number of arguments is wrong. |
| 1325 | */ |
| 1326 | static int |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1327 | 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] | 1328 | { |
| 1329 | isn_T *isn; |
| 1330 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1331 | int argoff; |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1332 | type_T *argtypes[MAX_FUNC_ARGS]; |
| 1333 | int i; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1334 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1335 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1336 | argoff = check_internal_func(func_idx, argcount); |
| 1337 | if (argoff < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1338 | return FAIL; |
| 1339 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 1340 | if (method_call && argoff > 1) |
| 1341 | { |
| 1342 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1343 | return FAIL; |
| 1344 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1345 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1346 | } |
| 1347 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1348 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1349 | return FAIL; |
| 1350 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1351 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1352 | |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1353 | for (i = 0; i < argcount; ++i) |
| 1354 | argtypes[i] = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
| 1355 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1356 | stack->ga_len -= argcount; // drop the arguments |
| 1357 | if (ga_grow(stack, 1) == FAIL) |
| 1358 | return FAIL; |
| 1359 | ((type_T **)stack->ga_data)[stack->ga_len] = |
Bram Moolenaar | fbdd08e | 2020-03-01 14:04:46 +0100 | [diff] [blame] | 1360 | internal_func_ret_type(func_idx, argcount, argtypes); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1361 | ++stack->ga_len; // add return value |
| 1362 | |
| 1363 | return OK; |
| 1364 | } |
| 1365 | |
| 1366 | /* |
| 1367 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1368 | * Return FAIL if the number of arguments is wrong. |
| 1369 | */ |
| 1370 | static int |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1371 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1372 | { |
| 1373 | isn_T *isn; |
| 1374 | garray_T *stack = &cctx->ctx_type_stack; |
| 1375 | int regular_args = ufunc->uf_args.ga_len; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1376 | int argcount = pushed_argcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1377 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1378 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1379 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1380 | { |
| 1381 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 1382 | return FAIL; |
| 1383 | } |
| 1384 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1385 | { |
| 1386 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 1387 | return FAIL; |
| 1388 | } |
| 1389 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1390 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1391 | { |
| 1392 | int i; |
| 1393 | |
| 1394 | for (i = 0; i < argcount; ++i) |
| 1395 | { |
| 1396 | type_T *expected; |
| 1397 | type_T *actual; |
| 1398 | |
| 1399 | if (i < regular_args) |
| 1400 | { |
| 1401 | if (ufunc->uf_arg_types == NULL) |
| 1402 | continue; |
| 1403 | expected = ufunc->uf_arg_types[i]; |
| 1404 | } |
Bram Moolenaar | 79e8db9 | 2020-08-14 22:16:33 +0200 | [diff] [blame] | 1405 | else if (ufunc->uf_va_type == NULL) |
| 1406 | // possibly a lambda |
| 1407 | expected = &t_any; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1408 | else |
| 1409 | expected = ufunc->uf_va_type->tt_member; |
| 1410 | actual = ((type_T **)stack->ga_data)[stack->ga_len - argcount + i]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1411 | if (need_type(actual, expected, -argcount + i, cctx, TRUE) == FAIL) |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1412 | { |
| 1413 | arg_type_mismatch(expected, actual, i + 1); |
| 1414 | return FAIL; |
| 1415 | } |
| 1416 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1417 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 1418 | if (compile_def_function(ufunc, ufunc->uf_ret_type == NULL, NULL) |
| 1419 | == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1420 | return FAIL; |
Bram Moolenaar | 0b76b42 | 2020-04-07 22:05:08 +0200 | [diff] [blame] | 1421 | } |
| 1422 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1423 | if ((isn = generate_instr(cctx, |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1424 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 1425 | : ISN_UCALL)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1426 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 1427 | if (ufunc->uf_def_status != UF_NOT_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1428 | { |
| 1429 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1430 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1431 | } |
| 1432 | else |
| 1433 | { |
| 1434 | // A user function may be deleted and redefined later, can't use the |
| 1435 | // ufunc pointer, need to look it up again at runtime. |
| 1436 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1437 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1438 | } |
| 1439 | |
| 1440 | stack->ga_len -= argcount; // drop the arguments |
| 1441 | if (ga_grow(stack, 1) == FAIL) |
| 1442 | return FAIL; |
| 1443 | // add return value |
| 1444 | ((type_T **)stack->ga_data)[stack->ga_len] = ufunc->uf_ret_type; |
| 1445 | ++stack->ga_len; |
| 1446 | |
| 1447 | return OK; |
| 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1452 | */ |
| 1453 | static int |
| 1454 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1455 | { |
| 1456 | isn_T *isn; |
| 1457 | garray_T *stack = &cctx->ctx_type_stack; |
| 1458 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1459 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1460 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1461 | return FAIL; |
| 1462 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1463 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1464 | |
| 1465 | stack->ga_len -= argcount; // drop the arguments |
Bram Moolenaar | 26e117e | 2020-02-04 21:24:15 +0100 | [diff] [blame] | 1466 | if (ga_grow(stack, 1) == FAIL) |
| 1467 | return FAIL; |
| 1468 | // add return value |
| 1469 | ((type_T **)stack->ga_data)[stack->ga_len] = &t_any; |
| 1470 | ++stack->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1471 | |
| 1472 | return OK; |
| 1473 | } |
| 1474 | |
| 1475 | /* |
| 1476 | * Generate an ISN_PCALL instruction. |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1477 | * "type" is the type of the FuncRef. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1478 | */ |
| 1479 | static int |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1480 | generate_PCALL( |
| 1481 | cctx_T *cctx, |
| 1482 | int argcount, |
| 1483 | char_u *name, |
| 1484 | type_T *type, |
| 1485 | int at_top) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1486 | { |
| 1487 | isn_T *isn; |
| 1488 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1489 | type_T *ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1490 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1491 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1492 | |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1493 | if (type->tt_type == VAR_ANY) |
| 1494 | ret_type = &t_any; |
| 1495 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1496 | { |
| 1497 | if (type->tt_argcount != -1) |
| 1498 | { |
| 1499 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1500 | |
| 1501 | if (argcount < type->tt_min_argcount - varargs) |
| 1502 | { |
| 1503 | semsg(_(e_toofewarg), "[reference]"); |
| 1504 | return FAIL; |
| 1505 | } |
| 1506 | if (!varargs && argcount > type->tt_argcount) |
| 1507 | { |
| 1508 | semsg(_(e_toomanyarg), "[reference]"); |
| 1509 | return FAIL; |
| 1510 | } |
| 1511 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1512 | ret_type = type->tt_member; |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 1513 | } |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1514 | else |
| 1515 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1516 | semsg(_(e_not_callable_type_str), name); |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1517 | return FAIL; |
| 1518 | } |
| 1519 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1520 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1521 | return FAIL; |
| 1522 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1523 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1524 | |
| 1525 | stack->ga_len -= argcount; // drop the arguments |
| 1526 | |
| 1527 | // drop the funcref/partial, get back the return value |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 1528 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = ret_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1529 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1530 | // If partial is above the arguments it must be cleared and replaced with |
| 1531 | // the return value. |
| 1532 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1533 | return FAIL; |
| 1534 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1535 | return OK; |
| 1536 | } |
| 1537 | |
| 1538 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1539 | * Generate an ISN_STRINGMEMBER instruction. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1540 | */ |
| 1541 | static int |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1542 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1543 | { |
| 1544 | isn_T *isn; |
| 1545 | garray_T *stack = &cctx->ctx_type_stack; |
| 1546 | type_T *type; |
| 1547 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1548 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1549 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1550 | return FAIL; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1551 | isn->isn_arg.string = vim_strnsave(name, len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1552 | |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1553 | // check for dict type |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1554 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 0062c2d | 2020-02-20 22:14:31 +0100 | [diff] [blame] | 1555 | if (type->tt_type != VAR_DICT && type != &t_any) |
| 1556 | { |
| 1557 | emsg(_(e_dictreq)); |
| 1558 | return FAIL; |
| 1559 | } |
| 1560 | // change dict type to dict member type |
| 1561 | if (type->tt_type == VAR_DICT) |
| 1562 | ((type_T **)stack->ga_data)[stack->ga_len - 1] = type->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1563 | |
| 1564 | return OK; |
| 1565 | } |
| 1566 | |
| 1567 | /* |
| 1568 | * Generate an ISN_ECHO instruction. |
| 1569 | */ |
| 1570 | static int |
| 1571 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1572 | { |
| 1573 | isn_T *isn; |
| 1574 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1575 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1576 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1577 | return FAIL; |
| 1578 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1579 | isn->isn_arg.echo.echo_count = count; |
| 1580 | |
| 1581 | return OK; |
| 1582 | } |
| 1583 | |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1584 | /* |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1585 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1586 | */ |
| 1587 | static int |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1588 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1589 | { |
| 1590 | isn_T *isn; |
| 1591 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1592 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1593 | return FAIL; |
| 1594 | isn->isn_arg.number = count; |
| 1595 | |
| 1596 | return OK; |
| 1597 | } |
| 1598 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 1599 | /* |
| 1600 | * Generate an ISN_PUT instruction. |
| 1601 | */ |
| 1602 | static int |
| 1603 | generate_PUT(cctx_T *cctx, int regname, linenr_T lnum) |
| 1604 | { |
| 1605 | isn_T *isn; |
| 1606 | |
| 1607 | RETURN_OK_IF_SKIP(cctx); |
| 1608 | if ((isn = generate_instr(cctx, ISN_PUT)) == NULL) |
| 1609 | return FAIL; |
| 1610 | isn->isn_arg.put.put_regname = regname; |
| 1611 | isn->isn_arg.put.put_lnum = lnum; |
| 1612 | return OK; |
| 1613 | } |
| 1614 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1615 | static int |
| 1616 | generate_EXEC(cctx_T *cctx, char_u *line) |
| 1617 | { |
| 1618 | isn_T *isn; |
| 1619 | |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 1620 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1621 | if ((isn = generate_instr(cctx, ISN_EXEC)) == NULL) |
| 1622 | return FAIL; |
| 1623 | isn->isn_arg.string = vim_strsave(line); |
| 1624 | return OK; |
| 1625 | } |
| 1626 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1627 | static int |
| 1628 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1629 | { |
| 1630 | isn_T *isn; |
| 1631 | |
| 1632 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1633 | return FAIL; |
| 1634 | isn->isn_arg.number = count; |
| 1635 | return OK; |
| 1636 | } |
| 1637 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1638 | /* |
| 1639 | * Reserve space for a local variable. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1640 | * Return the variable or NULL if it failed. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1641 | */ |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1642 | static lvar_T * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1643 | reserve_local(cctx_T *cctx, char_u *name, size_t len, int isConst, type_T *type) |
| 1644 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1645 | lvar_T *lvar; |
| 1646 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 1647 | if (lookup_arg(name, len, NULL, NULL, NULL, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1648 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1649 | emsg_namelen(_(e_str_is_used_as_argument), name, (int)len); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1650 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | if (ga_grow(&cctx->ctx_locals, 1) == FAIL) |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1654 | return NULL; |
| 1655 | 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] | 1656 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1657 | // Every local variable uses the next entry on the stack. We could re-use |
| 1658 | // the last ones when leaving a scope, but then variables used in a closure |
| 1659 | // might get overwritten. To keep things simple do not re-use stack |
| 1660 | // entries. This is less efficient, but memory is cheap these days. |
| 1661 | lvar->lv_idx = cctx->ctx_locals_count++; |
| 1662 | |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1663 | lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1664 | lvar->lv_const = isConst; |
| 1665 | lvar->lv_type = type; |
| 1666 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1667 | return lvar; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1671 | * Remove local variables above "new_top". |
| 1672 | */ |
| 1673 | static void |
| 1674 | unwind_locals(cctx_T *cctx, int new_top) |
| 1675 | { |
| 1676 | if (cctx->ctx_locals.ga_len > new_top) |
| 1677 | { |
| 1678 | int idx; |
| 1679 | lvar_T *lvar; |
| 1680 | |
| 1681 | for (idx = new_top; idx < cctx->ctx_locals.ga_len; ++idx) |
| 1682 | { |
| 1683 | lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx; |
| 1684 | vim_free(lvar->lv_name); |
| 1685 | } |
| 1686 | } |
| 1687 | cctx->ctx_locals.ga_len = new_top; |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * Free all local variables. |
| 1692 | */ |
| 1693 | static void |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 1694 | free_locals(cctx_T *cctx) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1695 | { |
| 1696 | unwind_locals(cctx, 0); |
| 1697 | ga_clear(&cctx->ctx_locals); |
| 1698 | } |
| 1699 | |
| 1700 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1701 | * Find "name" in script-local items of script "sid". |
| 1702 | * Returns the index in "sn_var_vals" if found. |
| 1703 | * If found but not in "sn_var_vals" returns -1. |
| 1704 | * If not found returns -2. |
| 1705 | */ |
| 1706 | int |
| 1707 | get_script_item_idx(int sid, char_u *name, int check_writable) |
| 1708 | { |
| 1709 | hashtab_T *ht; |
| 1710 | dictitem_T *di; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1711 | scriptitem_T *si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1712 | int idx; |
| 1713 | |
| 1714 | // First look the name up in the hashtable. |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1715 | if (!SCRIPT_ID_VALID(sid)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1716 | return -1; |
| 1717 | ht = &SCRIPT_VARS(sid); |
| 1718 | di = find_var_in_ht(ht, 0, name, TRUE); |
| 1719 | if (di == NULL) |
| 1720 | return -2; |
| 1721 | |
| 1722 | // Now find the svar_T index in sn_var_vals. |
| 1723 | for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx) |
| 1724 | { |
| 1725 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 1726 | |
| 1727 | if (sv->sv_tv == &di->di_tv) |
| 1728 | { |
| 1729 | if (check_writable && sv->sv_const) |
| 1730 | semsg(_(e_readonlyvar), name); |
| 1731 | return idx; |
| 1732 | } |
| 1733 | } |
| 1734 | return -1; |
| 1735 | } |
| 1736 | |
| 1737 | /* |
Bram Moolenaar | c82a5b5 | 2020-06-13 18:09:19 +0200 | [diff] [blame] | 1738 | * Find "name" in imported items of the current script or in "cctx" if not |
| 1739 | * NULL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1740 | */ |
| 1741 | imported_T * |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1742 | find_imported(char_u *name, size_t len, cctx_T *cctx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1743 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1744 | int idx; |
| 1745 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1746 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
Bram Moolenaar | 8e6cbb7 | 2020-07-01 14:38:12 +0200 | [diff] [blame] | 1747 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1748 | if (cctx != NULL) |
| 1749 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1750 | { |
| 1751 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) |
| 1752 | + idx; |
| 1753 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1754 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1755 | : STRLEN(import->imp_name) == len |
| 1756 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1757 | return import; |
| 1758 | } |
| 1759 | |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 1760 | return find_imported_in_script(name, len, current_sctx.sc_sid); |
| 1761 | } |
| 1762 | |
| 1763 | imported_T * |
| 1764 | find_imported_in_script(char_u *name, size_t len, int sid) |
| 1765 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1766 | scriptitem_T *si; |
Bram Moolenaar | efa9444 | 2020-08-08 22:16:00 +0200 | [diff] [blame] | 1767 | int idx; |
| 1768 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1769 | if (!SCRIPT_ID_VALID(sid)) |
| 1770 | return NULL; |
| 1771 | si = SCRIPT_ITEM(sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1772 | for (idx = 0; idx < si->sn_imports.ga_len; ++idx) |
| 1773 | { |
| 1774 | imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; |
| 1775 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 1776 | if (len == 0 ? STRCMP(name, import->imp_name) == 0 |
| 1777 | : STRLEN(import->imp_name) == len |
| 1778 | && STRNCMP(name, import->imp_name, len) == 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1779 | return import; |
| 1780 | } |
| 1781 | return NULL; |
| 1782 | } |
| 1783 | |
| 1784 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1785 | * Free all imported variables. |
| 1786 | */ |
| 1787 | static void |
| 1788 | free_imported(cctx_T *cctx) |
| 1789 | { |
| 1790 | int idx; |
| 1791 | |
| 1792 | for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) |
| 1793 | { |
| 1794 | imported_T *import = ((imported_T *)cctx->ctx_imports.ga_data) + idx; |
| 1795 | |
| 1796 | vim_free(import->imp_name); |
| 1797 | } |
| 1798 | ga_clear(&cctx->ctx_imports); |
| 1799 | } |
| 1800 | |
| 1801 | /* |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1802 | * Return TRUE if "p" points at a "#" but not at "#{". |
| 1803 | */ |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 1804 | int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1805 | vim9_comment_start(char_u *p) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1806 | { |
| 1807 | return p[0] == '#' && p[1] != '{'; |
| 1808 | } |
| 1809 | |
| 1810 | /* |
| 1811 | * Return a pointer to the next line that isn't empty or only contains a |
| 1812 | * comment. Skips over white space. |
| 1813 | * Returns NULL if there is none. |
| 1814 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1815 | char_u * |
| 1816 | peek_next_line_from_context(cctx_T *cctx) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1817 | { |
| 1818 | int lnum = cctx->ctx_lnum; |
| 1819 | |
| 1820 | while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len) |
| 1821 | { |
| 1822 | 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] | 1823 | char_u *p; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1824 | |
Bram Moolenaar | ba60cc4 | 2020-08-12 19:15:33 +0200 | [diff] [blame] | 1825 | // ignore NULLs inserted for continuation lines |
| 1826 | if (line != NULL) |
| 1827 | { |
| 1828 | p = skipwhite(line); |
| 1829 | if (*p != NUL && !vim9_comment_start(p)) |
| 1830 | return p; |
| 1831 | } |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1832 | } |
| 1833 | return NULL; |
| 1834 | } |
| 1835 | |
| 1836 | /* |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1837 | * Called when checking for a following operator at "arg". When the rest of |
| 1838 | * the line is empty or only a comment, peek the next line. If there is a next |
| 1839 | * line return a pointer to it and set "nextp". |
| 1840 | * Otherwise skip over white space. |
| 1841 | */ |
| 1842 | static char_u * |
| 1843 | may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp) |
| 1844 | { |
| 1845 | char_u *p = skipwhite(arg); |
| 1846 | |
| 1847 | *nextp = NULL; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1848 | if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p))) |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1849 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1850 | *nextp = peek_next_line_from_context(cctx); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 1851 | if (*nextp != NULL) |
| 1852 | return *nextp; |
| 1853 | } |
| 1854 | return p; |
| 1855 | } |
| 1856 | |
| 1857 | /* |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1858 | * Get the next line of the function from "cctx". |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1859 | * 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] | 1860 | * Returns NULL when at the end. |
| 1861 | */ |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 1862 | char_u * |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1863 | next_line_from_context(cctx_T *cctx, int skip_comment) |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1864 | { |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1865 | char_u *line; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1866 | |
| 1867 | do |
| 1868 | { |
| 1869 | ++cctx->ctx_lnum; |
| 1870 | if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1871 | { |
| 1872 | line = NULL; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1873 | break; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 1874 | } |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1875 | 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] | 1876 | cctx->ctx_line_start = line; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 1877 | SOURCING_LNUM = cctx->ctx_lnum + 1; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1878 | } while (line == NULL || *skipwhite(line) == NUL |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 1879 | || (skip_comment && vim9_comment_start(skipwhite(line)))); |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 1880 | return line; |
| 1881 | } |
| 1882 | |
| 1883 | /* |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1884 | * 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] | 1885 | * Also when "whitep" points to white space and "*arg" is on a "#". |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1886 | * Return FAIL if beyond the last line, "*arg" is unmodified then. |
| 1887 | */ |
| 1888 | static int |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 1889 | 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] | 1890 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1891 | if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg))) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1892 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 1893 | char_u *next = next_line_from_context(cctx, TRUE); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 1894 | |
| 1895 | if (next == NULL) |
| 1896 | return FAIL; |
| 1897 | *arg = skipwhite(next); |
| 1898 | } |
| 1899 | return OK; |
| 1900 | } |
| 1901 | |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 1902 | /* |
| 1903 | * Idem, and give an error when failed. |
| 1904 | */ |
| 1905 | static int |
| 1906 | may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx) |
| 1907 | { |
| 1908 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
| 1909 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1910 | emsg(_(e_line_incomplete)); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 1911 | return FAIL; |
| 1912 | } |
| 1913 | return OK; |
| 1914 | } |
| 1915 | |
| 1916 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1917 | // Structure passed between the compile_expr* functions to keep track of |
| 1918 | // constants that have been parsed but for which no code was produced yet. If |
| 1919 | // possible expressions on these constants are applied at compile time. If |
| 1920 | // that is not possible, the code to push the constants needs to be generated |
| 1921 | // before other instructions. |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1922 | // Using 50 should be more than enough of 5 levels of (). |
| 1923 | #define PPSIZE 50 |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1924 | typedef struct { |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1925 | typval_T pp_tv[PPSIZE]; // stack of ppconst constants |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1926 | int pp_used; // active entries in pp_tv[] |
| 1927 | } ppconst_T; |
| 1928 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 1929 | static int compile_expr0(char_u **arg, cctx_T *cctx); |
| 1930 | static int compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 1931 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1932 | /* |
| 1933 | * Generate a PUSH instruction for "tv". |
| 1934 | * "tv" will be consumed or cleared. |
| 1935 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 1936 | */ |
| 1937 | static int |
| 1938 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 1939 | { |
| 1940 | if (tv != NULL) |
| 1941 | { |
| 1942 | switch (tv->v_type) |
| 1943 | { |
| 1944 | case VAR_UNKNOWN: |
| 1945 | break; |
| 1946 | case VAR_BOOL: |
| 1947 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 1948 | break; |
| 1949 | case VAR_SPECIAL: |
| 1950 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 1951 | break; |
| 1952 | case VAR_NUMBER: |
| 1953 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 1954 | break; |
| 1955 | #ifdef FEAT_FLOAT |
| 1956 | case VAR_FLOAT: |
| 1957 | generate_PUSHF(cctx, tv->vval.v_float); |
| 1958 | break; |
| 1959 | #endif |
| 1960 | case VAR_BLOB: |
| 1961 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 1962 | tv->vval.v_blob = NULL; |
| 1963 | break; |
| 1964 | case VAR_STRING: |
| 1965 | generate_PUSHS(cctx, tv->vval.v_string); |
| 1966 | tv->vval.v_string = NULL; |
| 1967 | break; |
| 1968 | default: |
| 1969 | iemsg("constant type not supported"); |
| 1970 | clear_tv(tv); |
| 1971 | return FAIL; |
| 1972 | } |
| 1973 | tv->v_type = VAR_UNKNOWN; |
| 1974 | } |
| 1975 | return OK; |
| 1976 | } |
| 1977 | |
| 1978 | /* |
| 1979 | * Generate code for any ppconst entries. |
| 1980 | */ |
| 1981 | static int |
| 1982 | generate_ppconst(cctx_T *cctx, ppconst_T *ppconst) |
| 1983 | { |
| 1984 | int i; |
| 1985 | int ret = OK; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1986 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1987 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 1988 | cctx->ctx_skip = SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1989 | for (i = 0; i < ppconst->pp_used; ++i) |
| 1990 | if (generate_tv_PUSH(cctx, &ppconst->pp_tv[i]) == FAIL) |
| 1991 | ret = FAIL; |
| 1992 | ppconst->pp_used = 0; |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 1993 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 1994 | return ret; |
| 1995 | } |
| 1996 | |
| 1997 | /* |
| 1998 | * Clear ppconst constants. Used when failing. |
| 1999 | */ |
| 2000 | static void |
| 2001 | clear_ppconst(ppconst_T *ppconst) |
| 2002 | { |
| 2003 | int i; |
| 2004 | |
| 2005 | for (i = 0; i < ppconst->pp_used; ++i) |
| 2006 | clear_tv(&ppconst->pp_tv[i]); |
| 2007 | ppconst->pp_used = 0; |
| 2008 | } |
| 2009 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 2010 | /* |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2011 | * Generate an instruction to load script-local variable "name", without the |
| 2012 | * leading "s:". |
| 2013 | * Also finds imported variables. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2014 | */ |
| 2015 | static int |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2016 | compile_load_scriptvar( |
| 2017 | cctx_T *cctx, |
| 2018 | char_u *name, // variable NUL terminated |
| 2019 | char_u *start, // start of variable |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2020 | char_u **end, // end of variable |
| 2021 | int error) // when TRUE may give error |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2022 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2023 | scriptitem_T *si; |
| 2024 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2025 | imported_T *import; |
| 2026 | |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 2027 | if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) |
| 2028 | return FAIL; |
| 2029 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
| 2030 | idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE); |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2031 | if (idx == -1 || si->sn_version != SCRIPT_VERSION_VIM9) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2032 | { |
Bram Moolenaar | fd1823e | 2020-02-19 20:23:11 +0100 | [diff] [blame] | 2033 | // variable is not in sn_var_vals: old style script. |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2034 | return generate_OLDSCRIPT(cctx, ISN_LOADS, name, current_sctx.sc_sid, |
| 2035 | &t_any); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2036 | } |
| 2037 | if (idx >= 0) |
| 2038 | { |
| 2039 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx; |
| 2040 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2041 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2042 | current_sctx.sc_sid, idx, sv->sv_type); |
| 2043 | return OK; |
| 2044 | } |
| 2045 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 2046 | import = find_imported(name, 0, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2047 | if (import != NULL) |
| 2048 | { |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2049 | if (import->imp_all) |
| 2050 | { |
| 2051 | char_u *p = skipwhite(*end); |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2052 | char_u *exp_name; |
| 2053 | int cc; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2054 | ufunc_T *ufunc; |
| 2055 | type_T *type; |
| 2056 | |
| 2057 | // Used "import * as Name", need to lookup the member. |
| 2058 | if (*p != '.') |
| 2059 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2060 | semsg(_(e_expected_dot_after_name_str), start); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2061 | return FAIL; |
| 2062 | } |
| 2063 | ++p; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2064 | if (VIM_ISWHITE(*p)) |
| 2065 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2066 | emsg(_(e_no_white_space_allowed_after_dot)); |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2067 | return FAIL; |
| 2068 | } |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2069 | |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 2070 | // isolate one name |
| 2071 | exp_name = p; |
| 2072 | while (eval_isnamec(*p)) |
| 2073 | ++p; |
| 2074 | cc = *p; |
| 2075 | *p = NUL; |
| 2076 | |
| 2077 | idx = find_exported(import->imp_sid, exp_name, &ufunc, &type); |
| 2078 | *p = cc; |
| 2079 | p = skipwhite(p); |
| 2080 | |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2081 | // TODO: what if it is a function? |
| 2082 | if (idx < 0) |
| 2083 | return FAIL; |
| 2084 | *end = p; |
| 2085 | |
| 2086 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2087 | import->imp_sid, |
| 2088 | idx, |
| 2089 | type); |
| 2090 | } |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2091 | else if (import->imp_funcname != NULL) |
| 2092 | generate_PUSHFUNC(cctx, import->imp_funcname, import->imp_type); |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2093 | else |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2094 | generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT, |
| 2095 | import->imp_sid, |
| 2096 | import->imp_var_vals_idx, |
| 2097 | import->imp_type); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2098 | return OK; |
| 2099 | } |
| 2100 | |
Bram Moolenaar | b35efa5 | 2020-02-26 20:15:18 +0100 | [diff] [blame] | 2101 | if (error) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2102 | semsg(_(e_item_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2103 | return FAIL; |
| 2104 | } |
| 2105 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2106 | static int |
| 2107 | generate_funcref(cctx_T *cctx, char_u *name) |
| 2108 | { |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2109 | ufunc_T *ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2110 | |
| 2111 | if (ufunc == NULL) |
| 2112 | return FAIL; |
| 2113 | |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 2114 | // Need to compile any default values to get the argument types. |
| 2115 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED) |
| 2116 | if (compile_def_function(ufunc, TRUE, NULL) == FAIL) |
| 2117 | return FAIL; |
Bram Moolenaar | 40f4f7a | 2020-07-23 22:41:43 +0200 | [diff] [blame] | 2118 | return generate_PUSHFUNC(cctx, ufunc->uf_name, ufunc->uf_func_type); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2119 | } |
| 2120 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2121 | /* |
| 2122 | * Compile a variable name into a load instruction. |
| 2123 | * "end" points to just after the name. |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2124 | * "is_expr" is TRUE when evaluating an expression, might be a funcref. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2125 | * When "error" is FALSE do not give an error when not found. |
| 2126 | */ |
| 2127 | static int |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2128 | compile_load( |
| 2129 | char_u **arg, |
| 2130 | char_u *end_arg, |
| 2131 | cctx_T *cctx, |
| 2132 | int is_expr, |
| 2133 | int error) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2134 | { |
| 2135 | type_T *type; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2136 | char_u *name = NULL; |
Bram Moolenaar | f2d5c24 | 2020-02-23 21:25:54 +0100 | [diff] [blame] | 2137 | char_u *end = end_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2138 | int res = FAIL; |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2139 | int prev_called_emsg = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2140 | |
| 2141 | if (*(*arg + 1) == ':') |
| 2142 | { |
| 2143 | // load namespaced variable |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2144 | if (end <= *arg + 2) |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2145 | { |
| 2146 | isntype_T isn_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2147 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2148 | switch (**arg) |
| 2149 | { |
| 2150 | case 'g': isn_type = ISN_LOADGDICT; break; |
| 2151 | case 'w': isn_type = ISN_LOADWDICT; break; |
| 2152 | case 't': isn_type = ISN_LOADTDICT; break; |
| 2153 | case 'b': isn_type = ISN_LOADBDICT; break; |
| 2154 | default: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2155 | semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2156 | goto theend; |
| 2157 | } |
| 2158 | if (generate_instr_type(cctx, isn_type, &t_dict_any) == NULL) |
| 2159 | goto theend; |
| 2160 | res = OK; |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2161 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2162 | else |
| 2163 | { |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2164 | isntype_T isn_type = ISN_DROP; |
| 2165 | |
| 2166 | name = vim_strnsave(*arg + 2, end - (*arg + 2)); |
| 2167 | if (name == NULL) |
| 2168 | return FAIL; |
| 2169 | |
| 2170 | switch (**arg) |
| 2171 | { |
| 2172 | case 'v': res = generate_LOADV(cctx, name, error); |
| 2173 | break; |
| 2174 | case 's': res = compile_load_scriptvar(cctx, name, |
| 2175 | NULL, NULL, error); |
| 2176 | break; |
| 2177 | case 'g': isn_type = ISN_LOADG; break; |
| 2178 | case 'w': isn_type = ISN_LOADW; break; |
| 2179 | case 't': isn_type = ISN_LOADT; break; |
| 2180 | case 'b': isn_type = ISN_LOADB; break; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2181 | default: semsg(_(e_namespace_not_supported_str), *arg); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2182 | goto theend; |
| 2183 | } |
| 2184 | if (isn_type != ISN_DROP) |
| 2185 | { |
| 2186 | // Global, Buffer-local, Window-local and Tabpage-local |
| 2187 | // variables can be defined later, thus we don't check if it |
| 2188 | // exists, give error at runtime. |
| 2189 | res = generate_LOAD(cctx, isn_type, 0, name, &t_any); |
| 2190 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2191 | } |
| 2192 | } |
| 2193 | else |
| 2194 | { |
| 2195 | size_t len = end - *arg; |
| 2196 | int idx; |
| 2197 | int gen_load = FALSE; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2198 | int gen_load_outer = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2199 | |
| 2200 | name = vim_strnsave(*arg, end - *arg); |
| 2201 | if (name == NULL) |
| 2202 | return FAIL; |
| 2203 | |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2204 | if (lookup_arg(*arg, len, &idx, &type, &gen_load_outer, cctx) == OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2205 | { |
Bram Moolenaar | 2fd4cd7 | 2020-05-03 22:30:49 +0200 | [diff] [blame] | 2206 | if (!gen_load_outer) |
| 2207 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2208 | } |
| 2209 | else |
| 2210 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2211 | lvar_T *lvar = lookup_local(*arg, len, cctx); |
| 2212 | |
| 2213 | if (lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2214 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 2215 | type = lvar->lv_type; |
| 2216 | idx = lvar->lv_idx; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2217 | if (lvar->lv_from_outer) |
| 2218 | gen_load_outer = TRUE; |
| 2219 | else |
| 2220 | gen_load = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2221 | } |
| 2222 | else |
| 2223 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2224 | // "var" can be script-local even without using "s:" if it |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2225 | // already exists in a Vim9 script or when it's imported. |
| 2226 | if (lookup_script(*arg, len, TRUE) == OK |
| 2227 | || find_imported(name, 0, cctx) != NULL) |
| 2228 | res = compile_load_scriptvar(cctx, name, *arg, &end, FALSE); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2229 | |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2230 | // When evaluating an expression and the name starts with an |
| 2231 | // uppercase letter or "x:" it can be a user defined function. |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 2232 | // TODO: this is just guessing |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2233 | if (res == FAIL && is_expr |
| 2234 | && (ASCII_ISUPPER(*name) || name[1] == ':')) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2235 | res = generate_funcref(cctx, name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2236 | } |
| 2237 | } |
| 2238 | if (gen_load) |
| 2239 | res = generate_LOAD(cctx, ISN_LOAD, idx, NULL, type); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2240 | if (gen_load_outer) |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2241 | { |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2242 | res = generate_LOAD(cctx, ISN_LOADOUTER, idx, NULL, type); |
Bram Moolenaar | fd77748 | 2020-08-12 19:42:01 +0200 | [diff] [blame] | 2243 | cctx->ctx_outer_used = TRUE; |
| 2244 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | *arg = end; |
| 2248 | |
| 2249 | theend: |
Bram Moolenaar | 599c89c | 2020-03-28 14:53:20 +0100 | [diff] [blame] | 2250 | if (res == FAIL && error && called_emsg == prev_called_emsg) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2251 | semsg(_(e_variable_not_found_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2252 | vim_free(name); |
| 2253 | return res; |
| 2254 | } |
| 2255 | |
| 2256 | /* |
| 2257 | * Compile the argument expressions. |
| 2258 | * "arg" points to just after the "(" and is advanced to after the ")" |
| 2259 | */ |
| 2260 | static int |
| 2261 | compile_arguments(char_u **arg, cctx_T *cctx, int *argcount) |
| 2262 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2263 | char_u *p = *arg; |
| 2264 | char_u *whitep = *arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2265 | |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2266 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2267 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2268 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
| 2269 | goto failret; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2270 | if (*p == ')') |
| 2271 | { |
| 2272 | *arg = p + 1; |
| 2273 | return OK; |
| 2274 | } |
| 2275 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2276 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2277 | return FAIL; |
| 2278 | ++*argcount; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2279 | |
| 2280 | if (*p != ',' && *skipwhite(p) == ',') |
| 2281 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2282 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2283 | p = skipwhite(p); |
| 2284 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2285 | if (*p == ',') |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2286 | { |
| 2287 | ++p; |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2288 | if (*p != NUL && !VIM_ISWHITE(*p)) |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2289 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2290 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2291 | whitep = p; |
Bram Moolenaar | 38a5f51 | 2020-02-19 12:40:39 +0100 | [diff] [blame] | 2292 | p = skipwhite(p); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2293 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2294 | failret: |
Bram Moolenaar | e6085c5 | 2020-04-12 20:19:16 +0200 | [diff] [blame] | 2295 | emsg(_(e_missing_close)); |
| 2296 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | /* |
| 2300 | * Compile a function call: name(arg1, arg2) |
| 2301 | * "arg" points to "name", "arg + varlen" to the "(". |
| 2302 | * "argcount_init" is 1 for "value->method()" |
| 2303 | * Instructions: |
| 2304 | * EVAL arg1 |
| 2305 | * EVAL arg2 |
| 2306 | * BCALL / DCALL / UCALL |
| 2307 | */ |
| 2308 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2309 | compile_call( |
| 2310 | char_u **arg, |
| 2311 | size_t varlen, |
| 2312 | cctx_T *cctx, |
| 2313 | ppconst_T *ppconst, |
| 2314 | int argcount_init) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2315 | { |
| 2316 | char_u *name = *arg; |
Bram Moolenaar | 0b76ad5 | 2020-01-31 21:20:51 +0100 | [diff] [blame] | 2317 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2318 | int argcount = argcount_init; |
| 2319 | char_u namebuf[100]; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2320 | char_u fname_buf[FLEN_FIXED + 1]; |
| 2321 | char_u *tofree = NULL; |
| 2322 | int error = FCERR_NONE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2323 | ufunc_T *ufunc; |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2324 | int res = FAIL; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2325 | int is_autoload; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2326 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2327 | // we can evaluate "has('name')" at compile time |
| 2328 | if (varlen == 3 && STRNCMP(*arg, "has", 3) == 0) |
| 2329 | { |
| 2330 | char_u *s = skipwhite(*arg + varlen + 1); |
| 2331 | typval_T argvars[2]; |
| 2332 | |
| 2333 | argvars[0].v_type = VAR_UNKNOWN; |
| 2334 | if (*s == '"') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2335 | (void)eval_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2336 | else if (*s == '\'') |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2337 | (void)eval_lit_string(&s, &argvars[0], TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2338 | s = skipwhite(s); |
| 2339 | if (*s == ')' && argvars[0].v_type == VAR_STRING) |
| 2340 | { |
| 2341 | typval_T *tv = &ppconst->pp_tv[ppconst->pp_used]; |
| 2342 | |
| 2343 | *arg = s + 1; |
| 2344 | argvars[1].v_type = VAR_UNKNOWN; |
| 2345 | tv->v_type = VAR_NUMBER; |
| 2346 | tv->vval.v_number = 0; |
| 2347 | f_has(argvars, tv); |
| 2348 | clear_tv(&argvars[0]); |
| 2349 | ++ppconst->pp_used; |
| 2350 | return OK; |
| 2351 | } |
Bram Moolenaar | 497f76b | 2020-05-09 16:44:22 +0200 | [diff] [blame] | 2352 | clear_tv(&argvars[0]); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 2356 | return FAIL; |
| 2357 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2358 | if (varlen >= sizeof(namebuf)) |
| 2359 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2360 | semsg(_(e_name_too_long_str), name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2361 | return FAIL; |
| 2362 | } |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2363 | vim_strncpy(namebuf, *arg, varlen); |
| 2364 | name = fname_trans_sid(namebuf, fname_buf, &tofree, &error); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2365 | |
| 2366 | *arg = skipwhite(*arg + varlen + 1); |
| 2367 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2368 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2369 | |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2370 | is_autoload = vim_strchr(name, '#') != NULL; |
| 2371 | if (ASCII_ISLOWER(*name) && name[1] != ':' && !is_autoload) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2372 | { |
| 2373 | int idx; |
| 2374 | |
| 2375 | // builtin function |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2376 | idx = find_internal_func(name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2377 | if (idx >= 0) |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 2378 | res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2379 | else |
| 2380 | semsg(_(e_unknownfunc), namebuf); |
| 2381 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2382 | } |
| 2383 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2384 | // If we can find the function by name generate the right call. |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2385 | // Skip global functions here, a local funcref takes precedence. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2386 | ufunc = find_func(name, FALSE, cctx); |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2387 | if (ufunc != NULL && !func_is_global(ufunc)) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2388 | { |
| 2389 | res = generate_CALL(cctx, ufunc, argcount); |
| 2390 | goto theend; |
| 2391 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2392 | |
| 2393 | // If the name is a variable, load it and use PCALL. |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2394 | // Not for g:Func(), we don't know if it is a variable or not. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2395 | // Not for eome#Func(), it will be loaded later. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2396 | p = namebuf; |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2397 | if (STRNCMP(namebuf, "g:", 2) != 0 && !is_autoload |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2398 | && compile_load(&p, namebuf + varlen, cctx, FALSE, FALSE) == OK) |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2399 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 2400 | garray_T *stack = &cctx->ctx_type_stack; |
| 2401 | type_T *type; |
| 2402 | |
| 2403 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 2404 | res = generate_PCALL(cctx, argcount, namebuf, type, FALSE); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2405 | goto theend; |
| 2406 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2407 | |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 2408 | // If we can find a global function by name generate the right call. |
| 2409 | if (ufunc != NULL) |
| 2410 | { |
| 2411 | res = generate_CALL(cctx, ufunc, argcount); |
| 2412 | goto theend; |
| 2413 | } |
| 2414 | |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2415 | // 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] | 2416 | // runtime. Also handles a FuncRef at runtime. |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2417 | if (STRNCMP(namebuf, "g:", 2) == 0 || is_autoload) |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 2418 | res = generate_UCALL(cctx, name, argcount); |
| 2419 | else |
| 2420 | semsg(_(e_unknownfunc), namebuf); |
Bram Moolenaar | 5cab73f | 2020-02-06 19:25:19 +0100 | [diff] [blame] | 2421 | |
| 2422 | theend: |
| 2423 | vim_free(tofree); |
| 2424 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | // like NAMESPACE_CHAR but with 'a' and 'l'. |
| 2428 | #define VIM9_NAMESPACE_CHAR (char_u *)"bgstvw" |
| 2429 | |
| 2430 | /* |
| 2431 | * Find the end of a variable or function name. Unlike find_name_end() this |
| 2432 | * does not recognize magic braces. |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2433 | * When "namespace" is TRUE recognize "b:", "s:", etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2434 | * Return a pointer to just after the name. Equal to "arg" if there is no |
| 2435 | * valid name. |
| 2436 | */ |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2437 | static char_u * |
| 2438 | to_name_end(char_u *arg, int namespace) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2439 | { |
| 2440 | char_u *p; |
| 2441 | |
| 2442 | // Quick check for valid starting character. |
| 2443 | if (!eval_isnamec1(*arg)) |
| 2444 | return arg; |
| 2445 | |
| 2446 | for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) |
| 2447 | // Include a namespace such as "s:var" and "v:var". But "n:" is not |
| 2448 | // and can be used in slice "[n:]". |
| 2449 | if (*p == ':' && (p != arg + 1 |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2450 | || !namespace |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2451 | || vim_strchr(VIM9_NAMESPACE_CHAR, *arg) == NULL)) |
| 2452 | break; |
| 2453 | return p; |
| 2454 | } |
| 2455 | |
| 2456 | /* |
| 2457 | * 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] | 2458 | * This intentionally does not handle line continuation. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2459 | */ |
| 2460 | char_u * |
| 2461 | to_name_const_end(char_u *arg) |
| 2462 | { |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 2463 | char_u *p = to_name_end(arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2464 | typval_T rettv; |
| 2465 | |
| 2466 | if (p == arg && *arg == '[') |
| 2467 | { |
| 2468 | |
| 2469 | // Can be "[1, 2, 3]->Func()". |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2470 | if (eval_list(&p, &rettv, NULL, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2471 | p = arg; |
| 2472 | } |
| 2473 | else if (p == arg && *arg == '#' && arg[1] == '{') |
| 2474 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 2475 | // Can be "#{a: 1}->Func()". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2476 | ++p; |
Bram Moolenaar | 8ea9390 | 2020-06-27 14:11:53 +0200 | [diff] [blame] | 2477 | if (eval_dict(&p, &rettv, NULL, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2478 | p = arg; |
| 2479 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2480 | |
| 2481 | return p; |
| 2482 | } |
| 2483 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2484 | /* |
| 2485 | * parse a list: [expr, expr] |
| 2486 | * "*arg" points to the '['. |
| 2487 | */ |
| 2488 | static int |
| 2489 | compile_list(char_u **arg, cctx_T *cctx) |
| 2490 | { |
| 2491 | char_u *p = skipwhite(*arg + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2492 | char_u *whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2493 | int count = 0; |
| 2494 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2495 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2496 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2497 | if (may_get_next_line(whitep, &p, cctx) == FAIL) |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2498 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2499 | semsg(_(e_list_end), *arg); |
| 2500 | return FAIL; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2501 | } |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2502 | if (*p == ',') |
| 2503 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2504 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | db19921 | 2020-08-12 18:01:53 +0200 | [diff] [blame] | 2505 | return FAIL; |
| 2506 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2507 | if (*p == ']') |
| 2508 | { |
| 2509 | ++p; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2510 | break; |
Bram Moolenaar | a30590d | 2020-03-28 22:06:23 +0100 | [diff] [blame] | 2511 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2512 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2513 | break; |
| 2514 | ++count; |
| 2515 | if (*p == ',') |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2516 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2517 | ++p; |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2518 | if (*p != ']' && !IS_WHITE_OR_NUL(*p)) |
| 2519 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2520 | semsg(_(e_white_space_required_after_str), ","); |
Bram Moolenaar | 6b7a0a8 | 2020-07-08 18:38:08 +0200 | [diff] [blame] | 2521 | return FAIL; |
| 2522 | } |
| 2523 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2524 | whitep = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2525 | p = skipwhite(p); |
| 2526 | } |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2527 | *arg = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2528 | |
| 2529 | generate_NEWLIST(cctx, count); |
| 2530 | return OK; |
| 2531 | } |
| 2532 | |
| 2533 | /* |
| 2534 | * parse a lambda: {arg, arg -> expr} |
| 2535 | * "*arg" points to the '{'. |
| 2536 | */ |
| 2537 | static int |
| 2538 | compile_lambda(char_u **arg, cctx_T *cctx) |
| 2539 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2540 | typval_T rettv; |
| 2541 | ufunc_T *ufunc; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2542 | evalarg_T evalarg; |
| 2543 | |
| 2544 | CLEAR_FIELD(evalarg); |
| 2545 | evalarg.eval_flags = EVAL_EVALUATE; |
| 2546 | evalarg.eval_cctx = cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2547 | |
| 2548 | // Get the funcref in "rettv". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 2549 | if (get_lambda_tv(arg, &rettv, &evalarg) != OK) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2550 | return FAIL; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2551 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2552 | ufunc = rettv.vval.v_partial->pt_func; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2553 | ++ufunc->uf_refcount; |
| 2554 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2555 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2556 | |
| 2557 | // The function will have one line: "return {expr}". |
| 2558 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2559 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2560 | |
Bram Moolenaar | 8e2730a | 2020-07-08 22:01:49 +0200 | [diff] [blame] | 2561 | clear_evalarg(&evalarg, NULL); |
| 2562 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 2563 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 2564 | { |
| 2565 | // The return type will now be known. |
| 2566 | set_function_type(ufunc); |
| 2567 | |
| 2568 | return generate_FUNCREF(cctx, ufunc); |
| 2569 | } |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2570 | |
| 2571 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2572 | return FAIL; |
| 2573 | } |
| 2574 | |
| 2575 | /* |
| 2576 | * Compile a lamda call: expr->{lambda}(args) |
| 2577 | * "arg" points to the "{". |
| 2578 | */ |
| 2579 | static int |
| 2580 | compile_lambda_call(char_u **arg, cctx_T *cctx) |
| 2581 | { |
| 2582 | ufunc_T *ufunc; |
| 2583 | typval_T rettv; |
| 2584 | int argcount = 1; |
| 2585 | int ret = FAIL; |
| 2586 | |
| 2587 | // Get the funcref in "rettv". |
Bram Moolenaar | e40fbc2 | 2020-06-27 18:06:45 +0200 | [diff] [blame] | 2588 | if (get_lambda_tv(arg, &rettv, &EVALARG_EVALUATE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2589 | return FAIL; |
| 2590 | |
| 2591 | if (**arg != '(') |
| 2592 | { |
| 2593 | if (*skipwhite(*arg) == '(') |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 2594 | emsg(_(e_nowhitespace)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2595 | else |
| 2596 | semsg(_(e_missing_paren), "lambda"); |
| 2597 | clear_tv(&rettv); |
| 2598 | return FAIL; |
| 2599 | } |
| 2600 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2601 | ufunc = rettv.vval.v_partial->pt_func; |
| 2602 | ++ufunc->uf_refcount; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2603 | clear_tv(&rettv); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2604 | ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2605 | |
| 2606 | // The function will have one line: "return {expr}". |
| 2607 | // Compile it into instructions. |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2608 | compile_def_function(ufunc, TRUE, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2609 | |
| 2610 | // compile the arguments |
| 2611 | *arg = skipwhite(*arg + 1); |
| 2612 | if (compile_arguments(arg, cctx, &argcount) == OK) |
| 2613 | // call the compiled function |
| 2614 | ret = generate_CALL(cctx, ufunc, argcount); |
| 2615 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2616 | if (ret == FAIL) |
| 2617 | func_ptr_unref(ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2618 | return ret; |
| 2619 | } |
| 2620 | |
| 2621 | /* |
| 2622 | * parse a dict: {'key': val} or #{key: val} |
| 2623 | * "*arg" points to the '{'. |
| 2624 | */ |
| 2625 | static int |
| 2626 | compile_dict(char_u **arg, cctx_T *cctx, int literal) |
| 2627 | { |
| 2628 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2629 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2630 | int count = 0; |
| 2631 | dict_T *d = dict_alloc(); |
| 2632 | dictitem_T *item; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2633 | char_u *whitep = *arg; |
| 2634 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2635 | |
| 2636 | if (d == NULL) |
| 2637 | return FAIL; |
| 2638 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2639 | for (;;) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2640 | { |
| 2641 | char_u *key = NULL; |
| 2642 | |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2643 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2644 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2645 | *arg = NULL; |
| 2646 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | if (**arg == '}') |
| 2650 | break; |
| 2651 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2652 | if (literal) |
| 2653 | { |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2654 | char_u *end = to_name_end(*arg, !literal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2655 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2656 | if (end == *arg) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2657 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2658 | semsg(_(e_invalid_key_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2659 | return FAIL; |
| 2660 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2661 | key = vim_strnsave(*arg, end - *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2662 | if (generate_PUSHS(cctx, key) == FAIL) |
| 2663 | return FAIL; |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2664 | *arg = end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2665 | } |
| 2666 | else |
| 2667 | { |
| 2668 | isn_T *isn; |
| 2669 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2670 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2671 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2672 | isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 2673 | if (isn->isn_type == ISN_PUSHS) |
| 2674 | key = isn->isn_arg.string; |
Bram Moolenaar | f1a2368 | 2020-07-13 18:55:48 +0200 | [diff] [blame] | 2675 | else |
| 2676 | { |
| 2677 | type_T *keytype = ((type_T **)stack->ga_data) |
| 2678 | [stack->ga_len - 1]; |
| 2679 | if (need_type(keytype, &t_string, -1, cctx, FALSE) == FAIL) |
| 2680 | return FAIL; |
| 2681 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | // Check for duplicate keys, if using string keys. |
| 2685 | if (key != NULL) |
| 2686 | { |
| 2687 | item = dict_find(d, key, -1); |
| 2688 | if (item != NULL) |
| 2689 | { |
| 2690 | semsg(_(e_duplicate_key), key); |
| 2691 | goto failret; |
| 2692 | } |
| 2693 | item = dictitem_alloc(key); |
| 2694 | if (item != NULL) |
| 2695 | { |
| 2696 | item->di_tv.v_type = VAR_UNKNOWN; |
| 2697 | item->di_tv.v_lock = 0; |
| 2698 | if (dict_add(d, item) == FAIL) |
| 2699 | dictitem_free(item); |
| 2700 | } |
| 2701 | } |
| 2702 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2703 | if (**arg != ':') |
| 2704 | { |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 2705 | if (*skipwhite(*arg) == ':') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2706 | semsg(_(e_no_white_space_allowed_before_str), ":"); |
Bram Moolenaar | 17a836c | 2020-08-12 17:35:58 +0200 | [diff] [blame] | 2707 | else |
| 2708 | semsg(_(e_missing_dict_colon), *arg); |
| 2709 | return FAIL; |
| 2710 | } |
| 2711 | whitep = *arg + 1; |
| 2712 | if (!IS_WHITE_OR_NUL(*whitep)) |
| 2713 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2714 | semsg(_(e_white_space_required_after_str), ":"); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2715 | return FAIL; |
| 2716 | } |
| 2717 | |
| 2718 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2719 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2720 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2721 | *arg = NULL; |
| 2722 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2723 | } |
| 2724 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 2725 | if (compile_expr0(arg, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2726 | return FAIL; |
| 2727 | ++count; |
| 2728 | |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2729 | whitep = *arg; |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2730 | *arg = skipwhite(*arg); |
| 2731 | if (may_get_next_line(whitep, arg, cctx) == FAIL) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2732 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 2733 | *arg = NULL; |
| 2734 | goto failret; |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2735 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2736 | if (**arg == '}') |
| 2737 | break; |
| 2738 | if (**arg != ',') |
| 2739 | { |
| 2740 | semsg(_(e_missing_dict_comma), *arg); |
| 2741 | goto failret; |
| 2742 | } |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 2743 | if (IS_WHITE_OR_NUL(*whitep)) |
| 2744 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 2745 | semsg(_(e_no_white_space_allowed_before_str), ","); |
Bram Moolenaar | c3d6e8a | 2020-08-12 18:34:28 +0200 | [diff] [blame] | 2746 | return FAIL; |
| 2747 | } |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2748 | whitep = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2749 | *arg = skipwhite(*arg + 1); |
| 2750 | } |
| 2751 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2752 | *arg = *arg + 1; |
| 2753 | |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2754 | // Allow for following comment, after at least one space. |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 2755 | p = skipwhite(*arg); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2756 | if (VIM_ISWHITE(**arg) && vim9_comment_start(p)) |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2757 | *arg += STRLEN(*arg); |
| 2758 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2759 | dict_unref(d); |
| 2760 | return generate_NEWDICT(cctx, count); |
| 2761 | |
| 2762 | failret: |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 2763 | if (*arg == NULL) |
| 2764 | semsg(_(e_missing_dict_end), _("[end of lines]")); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2765 | dict_unref(d); |
| 2766 | return FAIL; |
| 2767 | } |
| 2768 | |
| 2769 | /* |
| 2770 | * Compile "&option". |
| 2771 | */ |
| 2772 | static int |
| 2773 | compile_get_option(char_u **arg, cctx_T *cctx) |
| 2774 | { |
| 2775 | typval_T rettv; |
| 2776 | char_u *start = *arg; |
| 2777 | int ret; |
| 2778 | |
| 2779 | // parse the option and get the current value to get the type. |
| 2780 | rettv.v_type = VAR_UNKNOWN; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2781 | ret = eval_option(arg, &rettv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2782 | if (ret == OK) |
| 2783 | { |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2784 | // include the '&' in the name, eval_option() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2785 | char_u *name = vim_strnsave(start, *arg - start); |
| 2786 | type_T *type = rettv.v_type == VAR_NUMBER ? &t_number : &t_string; |
| 2787 | |
| 2788 | ret = generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 2789 | vim_free(name); |
| 2790 | } |
| 2791 | clear_tv(&rettv); |
| 2792 | |
| 2793 | return ret; |
| 2794 | } |
| 2795 | |
| 2796 | /* |
| 2797 | * Compile "$VAR". |
| 2798 | */ |
| 2799 | static int |
| 2800 | compile_get_env(char_u **arg, cctx_T *cctx) |
| 2801 | { |
| 2802 | char_u *start = *arg; |
| 2803 | int len; |
| 2804 | int ret; |
| 2805 | char_u *name; |
| 2806 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2807 | ++*arg; |
| 2808 | len = get_env_len(arg); |
| 2809 | if (len == 0) |
| 2810 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2811 | semsg(_(e_syntax_error_at_str), start - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2812 | return FAIL; |
| 2813 | } |
| 2814 | |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 2815 | // include the '$' in the name, eval_env_var() expects it. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2816 | name = vim_strnsave(start, len + 1); |
| 2817 | ret = generate_LOAD(cctx, ISN_LOADENV, 0, name, &t_string); |
| 2818 | vim_free(name); |
| 2819 | return ret; |
| 2820 | } |
| 2821 | |
| 2822 | /* |
| 2823 | * Compile "@r". |
| 2824 | */ |
| 2825 | static int |
| 2826 | compile_get_register(char_u **arg, cctx_T *cctx) |
| 2827 | { |
| 2828 | int ret; |
| 2829 | |
| 2830 | ++*arg; |
| 2831 | if (**arg == NUL) |
| 2832 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2833 | semsg(_(e_syntax_error_at_str), *arg - 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2834 | return FAIL; |
| 2835 | } |
Bram Moolenaar | 7226e5b | 2020-08-02 17:33:26 +0200 | [diff] [blame] | 2836 | if (!valid_yank_reg(**arg, FALSE)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2837 | { |
| 2838 | emsg_invreg(**arg); |
| 2839 | return FAIL; |
| 2840 | } |
| 2841 | ret = generate_LOAD(cctx, ISN_LOADREG, **arg, NULL, &t_string); |
| 2842 | ++*arg; |
| 2843 | return ret; |
| 2844 | } |
| 2845 | |
| 2846 | /* |
| 2847 | * Apply leading '!', '-' and '+' to constant "rettv". |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2848 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2849 | */ |
| 2850 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2851 | 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] | 2852 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2853 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2854 | |
| 2855 | // this works from end to start |
| 2856 | while (p > start) |
| 2857 | { |
| 2858 | --p; |
| 2859 | if (*p == '-' || *p == '+') |
| 2860 | { |
| 2861 | // only '-' has an effect, for '+' we only check the type |
| 2862 | #ifdef FEAT_FLOAT |
| 2863 | if (rettv->v_type == VAR_FLOAT) |
| 2864 | { |
| 2865 | if (*p == '-') |
| 2866 | rettv->vval.v_float = -rettv->vval.v_float; |
| 2867 | } |
| 2868 | else |
| 2869 | #endif |
| 2870 | { |
| 2871 | varnumber_T val; |
| 2872 | int error = FALSE; |
| 2873 | |
| 2874 | // tv_get_number_chk() accepts a string, but we don't want that |
| 2875 | // here |
| 2876 | if (check_not_string(rettv) == FAIL) |
| 2877 | return FAIL; |
| 2878 | val = tv_get_number_chk(rettv, &error); |
| 2879 | clear_tv(rettv); |
| 2880 | if (error) |
| 2881 | return FAIL; |
| 2882 | if (*p == '-') |
| 2883 | val = -val; |
| 2884 | rettv->v_type = VAR_NUMBER; |
| 2885 | rettv->vval.v_number = val; |
| 2886 | } |
| 2887 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2888 | else if (numeric_only) |
| 2889 | { |
| 2890 | ++p; |
| 2891 | break; |
| 2892 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2893 | else |
| 2894 | { |
| 2895 | int v = tv2bool(rettv); |
| 2896 | |
| 2897 | // '!' is permissive in the type. |
| 2898 | clear_tv(rettv); |
| 2899 | rettv->v_type = VAR_BOOL; |
| 2900 | rettv->vval.v_number = v ? VVAL_FALSE : VVAL_TRUE; |
| 2901 | } |
| 2902 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2903 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2904 | return OK; |
| 2905 | } |
| 2906 | |
| 2907 | /* |
| 2908 | * Recognize v: variables that are constants and set "rettv". |
| 2909 | */ |
| 2910 | static void |
| 2911 | get_vim_constant(char_u **arg, typval_T *rettv) |
| 2912 | { |
| 2913 | if (STRNCMP(*arg, "v:true", 6) == 0) |
| 2914 | { |
| 2915 | rettv->v_type = VAR_BOOL; |
| 2916 | rettv->vval.v_number = VVAL_TRUE; |
| 2917 | *arg += 6; |
| 2918 | } |
| 2919 | else if (STRNCMP(*arg, "v:false", 7) == 0) |
| 2920 | { |
| 2921 | rettv->v_type = VAR_BOOL; |
| 2922 | rettv->vval.v_number = VVAL_FALSE; |
| 2923 | *arg += 7; |
| 2924 | } |
| 2925 | else if (STRNCMP(*arg, "v:null", 6) == 0) |
| 2926 | { |
| 2927 | rettv->v_type = VAR_SPECIAL; |
| 2928 | rettv->vval.v_number = VVAL_NULL; |
| 2929 | *arg += 6; |
| 2930 | } |
| 2931 | else if (STRNCMP(*arg, "v:none", 6) == 0) |
| 2932 | { |
| 2933 | rettv->v_type = VAR_SPECIAL; |
| 2934 | rettv->vval.v_number = VVAL_NONE; |
| 2935 | *arg += 6; |
| 2936 | } |
| 2937 | } |
| 2938 | |
Bram Moolenaar | 696ba23 | 2020-07-29 21:20:41 +0200 | [diff] [blame] | 2939 | exptype_T |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 2940 | get_compare_type(char_u *p, int *len, int *type_is) |
| 2941 | { |
| 2942 | exptype_T type = EXPR_UNKNOWN; |
| 2943 | int i; |
| 2944 | |
| 2945 | switch (p[0]) |
| 2946 | { |
| 2947 | case '=': if (p[1] == '=') |
| 2948 | type = EXPR_EQUAL; |
| 2949 | else if (p[1] == '~') |
| 2950 | type = EXPR_MATCH; |
| 2951 | break; |
| 2952 | case '!': if (p[1] == '=') |
| 2953 | type = EXPR_NEQUAL; |
| 2954 | else if (p[1] == '~') |
| 2955 | type = EXPR_NOMATCH; |
| 2956 | break; |
| 2957 | case '>': if (p[1] != '=') |
| 2958 | { |
| 2959 | type = EXPR_GREATER; |
| 2960 | *len = 1; |
| 2961 | } |
| 2962 | else |
| 2963 | type = EXPR_GEQUAL; |
| 2964 | break; |
| 2965 | case '<': if (p[1] != '=') |
| 2966 | { |
| 2967 | type = EXPR_SMALLER; |
| 2968 | *len = 1; |
| 2969 | } |
| 2970 | else |
| 2971 | type = EXPR_SEQUAL; |
| 2972 | break; |
| 2973 | case 'i': if (p[1] == 's') |
| 2974 | { |
| 2975 | // "is" and "isnot"; but not a prefix of a name |
| 2976 | if (p[2] == 'n' && p[3] == 'o' && p[4] == 't') |
| 2977 | *len = 5; |
| 2978 | i = p[*len]; |
| 2979 | if (!isalnum(i) && i != '_') |
| 2980 | { |
| 2981 | type = *len == 2 ? EXPR_IS : EXPR_ISNOT; |
| 2982 | *type_is = TRUE; |
| 2983 | } |
| 2984 | } |
| 2985 | break; |
| 2986 | } |
| 2987 | return type; |
| 2988 | } |
| 2989 | |
| 2990 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2991 | * Compile code to apply '-', '+' and '!'. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2992 | * When "numeric_only" is TRUE do not apply '!'. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2993 | */ |
| 2994 | static int |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2995 | 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] | 2996 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 2997 | char_u *p = *end; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2998 | |
| 2999 | // this works from end to start |
| 3000 | while (p > start) |
| 3001 | { |
| 3002 | --p; |
| 3003 | if (*p == '-' || *p == '+') |
| 3004 | { |
| 3005 | int negate = *p == '-'; |
| 3006 | isn_T *isn; |
| 3007 | |
| 3008 | // TODO: check type |
| 3009 | while (p > start && (p[-1] == '-' || p[-1] == '+')) |
| 3010 | { |
| 3011 | --p; |
| 3012 | if (*p == '-') |
| 3013 | negate = !negate; |
| 3014 | } |
| 3015 | // only '-' has an effect, for '+' we only check the type |
| 3016 | if (negate) |
| 3017 | isn = generate_instr(cctx, ISN_NEGATENR); |
| 3018 | else |
| 3019 | isn = generate_instr(cctx, ISN_CHECKNR); |
| 3020 | if (isn == NULL) |
| 3021 | return FAIL; |
| 3022 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3023 | else if (numeric_only) |
| 3024 | { |
| 3025 | ++p; |
| 3026 | break; |
| 3027 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3028 | else |
| 3029 | { |
| 3030 | int invert = TRUE; |
| 3031 | |
| 3032 | while (p > start && p[-1] == '!') |
| 3033 | { |
| 3034 | --p; |
| 3035 | invert = !invert; |
| 3036 | } |
| 3037 | if (generate_2BOOL(cctx, invert) == FAIL) |
| 3038 | return FAIL; |
| 3039 | } |
| 3040 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3041 | *end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3042 | return OK; |
| 3043 | } |
| 3044 | |
| 3045 | /* |
| 3046 | * Compile whatever comes after "name" or "name()". |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3047 | * Advances "*arg" only when something was recognized. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3048 | */ |
| 3049 | static int |
| 3050 | compile_subscript( |
| 3051 | char_u **arg, |
| 3052 | cctx_T *cctx, |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3053 | char_u *start_leader, |
| 3054 | char_u **end_leader, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3055 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3056 | { |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3057 | char_u *name_start = *end_leader; |
| 3058 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3059 | for (;;) |
| 3060 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3061 | char_u *p = skipwhite(*arg); |
| 3062 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 3063 | if (*p == NUL || (VIM_ISWHITE(**arg) && vim9_comment_start(p))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3064 | { |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3065 | char_u *next = peek_next_line_from_context(cctx); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3066 | |
| 3067 | // If a following line starts with "->{" or "->X" advance to that |
| 3068 | // line, so that a line break before "->" is allowed. |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3069 | // Also if a following line starts with ".x". |
| 3070 | if (next != NULL && |
| 3071 | ((next[0] == '-' && next[1] == '>' |
| 3072 | && (next[2] == '{' || ASCII_ISALPHA(next[2]))) |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3073 | || (next[0] == '.' && eval_isdictc(next[1])))) |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3074 | { |
| 3075 | next = next_line_from_context(cctx, TRUE); |
| 3076 | if (next == NULL) |
| 3077 | return FAIL; |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3078 | *arg = next; |
| 3079 | p = skipwhite(*arg); |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 3080 | } |
| 3081 | } |
| 3082 | |
Bram Moolenaar | 2d6b20d | 2020-07-25 19:30:59 +0200 | [diff] [blame] | 3083 | // Do not skip over white space to find the "(", "exeucte 'x' ()" is |
| 3084 | // not a function call. |
| 3085 | if (**arg == '(') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3086 | { |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3087 | garray_T *stack = &cctx->ctx_type_stack; |
| 3088 | type_T *type; |
| 3089 | int argcount = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3090 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3091 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3092 | return FAIL; |
| 3093 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3094 | // funcref(arg) |
Bram Moolenaar | a0a9f43 | 2020-04-28 21:29:34 +0200 | [diff] [blame] | 3095 | type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3096 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3097 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3098 | if (compile_arguments(arg, cctx, &argcount) == FAIL) |
| 3099 | return FAIL; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3100 | if (generate_PCALL(cctx, argcount, name_start, type, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3101 | return FAIL; |
| 3102 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3103 | else if (*p == '-' && p[1] == '>') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3104 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3105 | char_u *pstart = p; |
| 3106 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3107 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3108 | return FAIL; |
| 3109 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3110 | // something->method() |
| 3111 | // Apply the '!', '-' and '+' first: |
| 3112 | // -1.0->func() works like (-1.0)->func() |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3113 | if (compile_leader(cctx, TRUE, start_leader, end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3114 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3115 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3116 | p += 2; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3117 | *arg = skipwhite(p); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3118 | // No line break supported right after "->". |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3119 | if (**arg == '{') |
| 3120 | { |
| 3121 | // lambda call: list->{lambda} |
| 3122 | if (compile_lambda_call(arg, cctx) == FAIL) |
| 3123 | return FAIL; |
| 3124 | } |
| 3125 | else |
| 3126 | { |
| 3127 | // method call: list->method() |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3128 | p = *arg; |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3129 | if (!eval_isnamec1(*p)) |
| 3130 | { |
Bram Moolenaar | 637cd7d | 2020-07-23 19:06:23 +0200 | [diff] [blame] | 3131 | semsg(_(e_trailing_arg), pstart); |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 3132 | return FAIL; |
| 3133 | } |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3134 | if (ASCII_ISALPHA(*p) && p[1] == ':') |
| 3135 | p += 2; |
Bram Moolenaar | c5da1fb | 2020-08-05 15:43:44 +0200 | [diff] [blame] | 3136 | for ( ; eval_isnamec(*p); ++p) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3137 | ; |
| 3138 | if (*p != '(') |
| 3139 | { |
Bram Moolenaar | 0b37a2f | 2020-03-29 21:38:15 +0200 | [diff] [blame] | 3140 | semsg(_(e_missing_paren), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3141 | return FAIL; |
| 3142 | } |
| 3143 | // TODO: base value may not be the first argument |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3144 | if (compile_call(arg, p - *arg, cctx, ppconst, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3145 | return FAIL; |
| 3146 | } |
| 3147 | } |
Bram Moolenaar | badd848 | 2020-07-31 22:38:17 +0200 | [diff] [blame] | 3148 | else if (**arg == '[') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3149 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3150 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3151 | type_T **typep; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3152 | type_T *valtype; |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3153 | vartype_T vtype; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3154 | int is_slice = FALSE; |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3155 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3156 | // list index: list[123] |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 3157 | // dict member: dict[key] |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3158 | // string index: text[123] |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3159 | // TODO: blob index |
| 3160 | // TODO: more arguments |
| 3161 | // TODO: recognize list or dict at runtime |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3162 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3163 | return FAIL; |
| 3164 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3165 | ++p; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3166 | *arg = skipwhite(p); |
Bram Moolenaar | a7eedf3 | 2020-07-10 21:50:41 +0200 | [diff] [blame] | 3167 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3168 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3169 | if (**arg == ':') |
| 3170 | // missing first index is equal to zero |
| 3171 | generate_PUSHNR(cctx, 0); |
| 3172 | else |
| 3173 | { |
| 3174 | if (compile_expr0(arg, cctx) == FAIL) |
| 3175 | return FAIL; |
| 3176 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3177 | return FAIL; |
| 3178 | *arg = skipwhite(*arg); |
| 3179 | } |
| 3180 | if (**arg == ':') |
| 3181 | { |
| 3182 | *arg = skipwhite(*arg + 1); |
| 3183 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3184 | return FAIL; |
| 3185 | if (**arg == ']') |
| 3186 | // missing second index is equal to end of string |
| 3187 | generate_PUSHNR(cctx, -1); |
| 3188 | else |
| 3189 | { |
| 3190 | if (compile_expr0(arg, cctx) == FAIL) |
| 3191 | return FAIL; |
| 3192 | if (may_get_next_line_error(p, arg, cctx) == FAIL) |
| 3193 | return FAIL; |
| 3194 | *arg = skipwhite(*arg); |
| 3195 | } |
| 3196 | is_slice = TRUE; |
| 3197 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3198 | |
| 3199 | if (**arg != ']') |
| 3200 | { |
| 3201 | emsg(_(e_missbrac)); |
| 3202 | return FAIL; |
| 3203 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 3204 | *arg = *arg + 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3205 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3206 | // We can index a list and a dict. If we don't know the type |
| 3207 | // we can use the index value type. |
| 3208 | // TODO: If we don't know use an instruction to figure it out at |
| 3209 | // runtime. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3210 | typep = ((type_T **)stack->ga_data) + stack->ga_len |
| 3211 | - (is_slice ? 3 : 2); |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3212 | vtype = (*typep)->tt_type; |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3213 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 3214 | // If the index is a string, the variable must be a Dict. |
| 3215 | if (*typep == &t_any && valtype == &t_string) |
| 3216 | vtype = VAR_DICT; |
| 3217 | if (vtype == VAR_STRING || vtype == VAR_LIST || vtype == VAR_BLOB) |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3218 | { |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3219 | if (need_type(valtype, &t_number, -1, cctx, FALSE) == FAIL) |
| 3220 | return FAIL; |
| 3221 | if (is_slice) |
| 3222 | { |
| 3223 | valtype = ((type_T **)stack->ga_data)[stack->ga_len - 2]; |
| 3224 | if (need_type(valtype, &t_number, -2, cctx, FALSE) == FAIL) |
| 3225 | return FAIL; |
| 3226 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3227 | } |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3228 | |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3229 | if (vtype == VAR_DICT) |
| 3230 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3231 | if (is_slice) |
| 3232 | { |
| 3233 | emsg(_(e_cannot_slice_dictionary)); |
| 3234 | return FAIL; |
| 3235 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3236 | if ((*typep)->tt_type == VAR_DICT) |
| 3237 | *typep = (*typep)->tt_member; |
Bram Moolenaar | 7892b95 | 2020-07-20 22:09:34 +0200 | [diff] [blame] | 3238 | else |
| 3239 | { |
| 3240 | if (need_type(*typep, &t_dict_any, -2, cctx, FALSE) == FAIL) |
| 3241 | return FAIL; |
| 3242 | *typep = &t_any; |
| 3243 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3244 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 3245 | return FAIL; |
| 3246 | if (generate_instr_drop(cctx, ISN_MEMBER, 1) == FAIL) |
| 3247 | return FAIL; |
| 3248 | } |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3249 | else if (vtype == VAR_STRING) |
| 3250 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3251 | *typep = &t_string; |
| 3252 | if ((is_slice |
| 3253 | ? generate_instr_drop(cctx, ISN_STRSLICE, 2) |
| 3254 | : generate_instr_drop(cctx, ISN_STRINDEX, 1)) == FAIL) |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3255 | return FAIL; |
| 3256 | } |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3257 | else if (vtype == VAR_BLOB) |
| 3258 | { |
| 3259 | emsg("Sorry, blob index and slice not implemented yet"); |
| 3260 | return FAIL; |
| 3261 | } |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 3262 | else if (vtype == VAR_LIST || *typep == &t_any) |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3263 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3264 | if (is_slice) |
| 3265 | { |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3266 | if (generate_instr_drop(cctx, |
| 3267 | vtype == VAR_LIST ? ISN_LISTSLICE : ISN_ANYSLICE, |
| 3268 | 2) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3269 | return FAIL; |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3270 | } |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3271 | else |
| 3272 | { |
| 3273 | if ((*typep)->tt_type == VAR_LIST) |
| 3274 | *typep = (*typep)->tt_member; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3275 | if (generate_instr_drop(cctx, |
| 3276 | vtype == VAR_LIST ? ISN_LISTINDEX : ISN_ANYINDEX, |
| 3277 | 1) == FAIL) |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3278 | return FAIL; |
| 3279 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3280 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3281 | else |
| 3282 | { |
Bram Moolenaar | 56acb09 | 2020-08-16 14:48:19 +0200 | [diff] [blame] | 3283 | emsg(_(e_string_list_dict_or_blob_required)); |
Bram Moolenaar | b13af50 | 2020-02-17 21:12:08 +0100 | [diff] [blame] | 3284 | return FAIL; |
| 3285 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3286 | } |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3287 | else if (*p == '.' && p[1] != '.') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3288 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3289 | if (generate_ppconst(cctx, ppconst) == FAIL) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3290 | return FAIL; |
| 3291 | |
Bram Moolenaar | 7a4b898 | 2020-07-08 17:36:21 +0200 | [diff] [blame] | 3292 | *arg = p + 1; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3293 | if (may_get_next_line(*arg, arg, cctx) == FAIL) |
| 3294 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3295 | // dictionary member: dict.name |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 3296 | p = *arg; |
Bram Moolenaar | b13ab99 | 2020-07-27 21:43:28 +0200 | [diff] [blame] | 3297 | if (eval_isdictc(*p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3298 | while (eval_isnamec(*p)) |
| 3299 | MB_PTR_ADV(p); |
| 3300 | if (p == *arg) |
| 3301 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3302 | semsg(_(e_syntax_error_at_str), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3303 | return FAIL; |
| 3304 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3305 | if (generate_STRINGMEMBER(cctx, *arg, p - *arg) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3306 | return FAIL; |
| 3307 | *arg = p; |
| 3308 | } |
| 3309 | else |
| 3310 | break; |
| 3311 | } |
| 3312 | |
| 3313 | // TODO - see handle_subscript(): |
| 3314 | // Turn "dict.Func" into a partial for "Func" bound to "dict". |
| 3315 | // Don't do this when "Func" is already a partial that was bound |
| 3316 | // explicitly (pt_auto is FALSE). |
| 3317 | |
| 3318 | return OK; |
| 3319 | } |
| 3320 | |
| 3321 | /* |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3322 | * Compile an expression at "*arg" and add instructions to "cctx->ctx_instr". |
| 3323 | * "arg" is advanced until after the expression, skipping white space. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3324 | * |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3325 | * If the value is a constant "ppconst->pp_ret" will be set. |
| 3326 | * Before instructions are generated, any values in "ppconst" will generated. |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3327 | * |
| 3328 | * This is the compiling equivalent of eval1(), eval2(), etc. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3329 | */ |
| 3330 | |
| 3331 | /* |
| 3332 | * number number constant |
| 3333 | * 0zFFFFFFFF Blob constant |
| 3334 | * "string" string constant |
| 3335 | * 'string' literal string constant |
| 3336 | * &option-name option value |
| 3337 | * @r register contents |
| 3338 | * identifier variable value |
| 3339 | * function() function call |
| 3340 | * $VAR environment variable |
| 3341 | * (expression) nested expression |
| 3342 | * [expr, expr] List |
| 3343 | * {key: val, key: val} Dictionary |
| 3344 | * #{key: val, key: val} Dictionary with literal keys |
| 3345 | * |
| 3346 | * Also handle: |
| 3347 | * ! in front logical NOT |
| 3348 | * - in front unary minus |
| 3349 | * + in front unary plus (ignored) |
| 3350 | * trailing (arg) funcref/partial call |
| 3351 | * trailing [] subscript in String or List |
| 3352 | * trailing .name entry in Dictionary |
| 3353 | * trailing ->name() method call |
| 3354 | */ |
| 3355 | static int |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3356 | compile_expr7( |
| 3357 | char_u **arg, |
| 3358 | cctx_T *cctx, |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3359 | ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3360 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3361 | char_u *start_leader, *end_leader; |
| 3362 | int ret = OK; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3363 | typval_T *rettv = &ppconst->pp_tv[ppconst->pp_used]; |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3364 | int used_before = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3365 | |
| 3366 | /* |
| 3367 | * Skip '!', '-' and '+' characters. They are handled later. |
| 3368 | */ |
| 3369 | start_leader = *arg; |
| 3370 | while (**arg == '!' || **arg == '-' || **arg == '+') |
| 3371 | *arg = skipwhite(*arg + 1); |
| 3372 | end_leader = *arg; |
| 3373 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3374 | rettv->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3375 | switch (**arg) |
| 3376 | { |
| 3377 | /* |
| 3378 | * Number constant. |
| 3379 | */ |
| 3380 | case '0': // also for blob starting with 0z |
| 3381 | case '1': |
| 3382 | case '2': |
| 3383 | case '3': |
| 3384 | case '4': |
| 3385 | case '5': |
| 3386 | case '6': |
| 3387 | case '7': |
| 3388 | case '8': |
| 3389 | case '9': |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3390 | case '.': if (eval_number(arg, rettv, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3391 | return FAIL; |
Bram Moolenaar | 4301a72 | 2020-08-11 20:51:08 +0200 | [diff] [blame] | 3392 | // Apply "-" and "+" just before the number now, right to |
| 3393 | // left. Matters especially when "->" follows. Stops at |
| 3394 | // '!'. |
| 3395 | if (apply_leader(rettv, TRUE, |
| 3396 | start_leader, &end_leader) == FAIL) |
| 3397 | { |
| 3398 | clear_tv(rettv); |
| 3399 | return FAIL; |
| 3400 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3401 | break; |
| 3402 | |
| 3403 | /* |
| 3404 | * String constant: "string". |
| 3405 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3406 | case '"': if (eval_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3407 | return FAIL; |
| 3408 | break; |
| 3409 | |
| 3410 | /* |
| 3411 | * Literal string constant: 'str''ing'. |
| 3412 | */ |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 3413 | case '\'': if (eval_lit_string(arg, rettv, TRUE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3414 | return FAIL; |
| 3415 | break; |
| 3416 | |
| 3417 | /* |
| 3418 | * Constant Vim variable. |
| 3419 | */ |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3420 | case 'v': get_vim_constant(arg, rettv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3421 | ret = NOTDONE; |
| 3422 | break; |
| 3423 | |
| 3424 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3425 | * "true" constant |
| 3426 | */ |
| 3427 | case 't': if (STRNCMP(*arg, "true", 4) == 0 |
| 3428 | && !eval_isnamec((*arg)[4])) |
| 3429 | { |
| 3430 | *arg += 4; |
| 3431 | rettv->v_type = VAR_BOOL; |
| 3432 | rettv->vval.v_number = VVAL_TRUE; |
| 3433 | } |
| 3434 | else |
| 3435 | ret = NOTDONE; |
| 3436 | break; |
| 3437 | |
| 3438 | /* |
| 3439 | * "false" constant |
| 3440 | */ |
| 3441 | case 'f': if (STRNCMP(*arg, "false", 5) == 0 |
| 3442 | && !eval_isnamec((*arg)[5])) |
| 3443 | { |
| 3444 | *arg += 5; |
| 3445 | rettv->v_type = VAR_BOOL; |
| 3446 | rettv->vval.v_number = VVAL_FALSE; |
| 3447 | } |
| 3448 | else |
| 3449 | ret = NOTDONE; |
| 3450 | break; |
| 3451 | |
| 3452 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3453 | * List: [expr, expr] |
| 3454 | */ |
| 3455 | case '[': ret = compile_list(arg, cctx); |
| 3456 | break; |
| 3457 | |
| 3458 | /* |
| 3459 | * Dictionary: #{key: val, key: val} |
| 3460 | */ |
| 3461 | case '#': if ((*arg)[1] == '{') |
| 3462 | { |
| 3463 | ++*arg; |
| 3464 | ret = compile_dict(arg, cctx, TRUE); |
| 3465 | } |
| 3466 | else |
| 3467 | ret = NOTDONE; |
| 3468 | break; |
| 3469 | |
| 3470 | /* |
| 3471 | * Lambda: {arg, arg -> expr} |
| 3472 | * Dictionary: {'key': val, 'key': val} |
| 3473 | */ |
| 3474 | case '{': { |
| 3475 | char_u *start = skipwhite(*arg + 1); |
| 3476 | |
| 3477 | // Find out what comes after the arguments. |
| 3478 | ret = get_function_args(&start, '-', NULL, |
Bram Moolenaar | 5e774c7 | 2020-04-12 21:53:00 +0200 | [diff] [blame] | 3479 | NULL, NULL, NULL, TRUE, NULL, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3480 | if (ret != FAIL && *start == '>') |
| 3481 | ret = compile_lambda(arg, cctx); |
| 3482 | else |
| 3483 | ret = compile_dict(arg, cctx, FALSE); |
| 3484 | } |
| 3485 | break; |
| 3486 | |
| 3487 | /* |
| 3488 | * Option value: &name |
| 3489 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3490 | case '&': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3491 | return FAIL; |
| 3492 | ret = compile_get_option(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3493 | break; |
| 3494 | |
| 3495 | /* |
| 3496 | * Environment variable: $VAR. |
| 3497 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3498 | case '$': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3499 | return FAIL; |
| 3500 | ret = compile_get_env(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3501 | break; |
| 3502 | |
| 3503 | /* |
| 3504 | * Register contents: @r. |
| 3505 | */ |
Bram Moolenaar | 3fc7128 | 2020-08-21 20:43:17 +0200 | [diff] [blame] | 3506 | case '@': if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3507 | return FAIL; |
| 3508 | ret = compile_get_register(arg, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3509 | break; |
| 3510 | /* |
| 3511 | * nested expression: (expression). |
| 3512 | */ |
| 3513 | case '(': *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3514 | |
| 3515 | // recursive! |
| 3516 | if (ppconst->pp_used <= PPSIZE - 10) |
| 3517 | { |
| 3518 | ret = compile_expr1(arg, cctx, ppconst); |
| 3519 | } |
| 3520 | else |
| 3521 | { |
| 3522 | // Not enough space in ppconst, flush constants. |
| 3523 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3524 | return FAIL; |
| 3525 | ret = compile_expr0(arg, cctx); |
| 3526 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3527 | *arg = skipwhite(*arg); |
| 3528 | if (**arg == ')') |
| 3529 | ++*arg; |
| 3530 | else if (ret == OK) |
| 3531 | { |
| 3532 | emsg(_(e_missing_close)); |
| 3533 | ret = FAIL; |
| 3534 | } |
| 3535 | break; |
| 3536 | |
| 3537 | default: ret = NOTDONE; |
| 3538 | break; |
| 3539 | } |
| 3540 | if (ret == FAIL) |
| 3541 | return FAIL; |
| 3542 | |
Bram Moolenaar | 1c74721 | 2020-05-09 18:28:34 +0200 | [diff] [blame] | 3543 | if (rettv->v_type != VAR_UNKNOWN && used_before == ppconst->pp_used) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3544 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 3545 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3546 | clear_tv(rettv); |
| 3547 | else |
| 3548 | // A constant expression can possibly be handled compile time, |
| 3549 | // return the value instead of generating code. |
| 3550 | ++ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3551 | } |
| 3552 | else if (ret == NOTDONE) |
| 3553 | { |
| 3554 | char_u *p; |
| 3555 | int r; |
| 3556 | |
| 3557 | if (!eval_isnamec1(**arg)) |
| 3558 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3559 | semsg(_(e_name_expected), *arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3560 | return FAIL; |
| 3561 | } |
| 3562 | |
| 3563 | // "name" or "name()" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 3564 | p = to_name_end(*arg, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3565 | if (*p == '(') |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3566 | { |
| 3567 | r = compile_call(arg, p - *arg, cctx, ppconst, 0); |
| 3568 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3569 | else |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3570 | { |
| 3571 | if (generate_ppconst(cctx, ppconst) == FAIL) |
| 3572 | return FAIL; |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 3573 | r = compile_load(arg, p, cctx, TRUE, TRUE); |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3574 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3575 | if (r == FAIL) |
| 3576 | return FAIL; |
| 3577 | } |
| 3578 | |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3579 | // Handle following "[]", ".member", etc. |
| 3580 | // Then deal with prefixed '-', '+' and '!', if not done already. |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3581 | if (compile_subscript(arg, cctx, start_leader, &end_leader, |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3582 | ppconst) == FAIL) |
| 3583 | return FAIL; |
| 3584 | if (ppconst->pp_used > 0) |
| 3585 | { |
| 3586 | // apply the '!', '-' and '+' before the constant |
| 3587 | rettv = &ppconst->pp_tv[ppconst->pp_used - 1]; |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3588 | if (apply_leader(rettv, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3589 | return FAIL; |
| 3590 | return OK; |
| 3591 | } |
Bram Moolenaar | 59eccb9 | 2020-08-10 23:09:37 +0200 | [diff] [blame] | 3592 | if (compile_leader(cctx, FALSE, start_leader, &end_leader) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3593 | return FAIL; |
Bram Moolenaar | 5c2fe64 | 2020-05-07 23:20:21 +0200 | [diff] [blame] | 3594 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3595 | } |
| 3596 | |
| 3597 | /* |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3598 | * Give the "white on both sides" error, taking the operator from "p[len]". |
| 3599 | */ |
| 3600 | void |
| 3601 | error_white_both(char_u *op, int len) |
| 3602 | { |
| 3603 | char_u buf[10]; |
| 3604 | |
| 3605 | vim_strncpy(buf, op, len); |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3606 | semsg(_(e_white_space_required_before_and_after_str), buf); |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | /* |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3610 | * <type>expr7: runtime type check / conversion |
| 3611 | */ |
| 3612 | static int |
| 3613 | compile_expr7t(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
| 3614 | { |
| 3615 | type_T *want_type = NULL; |
| 3616 | |
| 3617 | // Recognize <type> |
| 3618 | if (**arg == '<' && eval_isnamec1((*arg)[1])) |
| 3619 | { |
| 3620 | int called_emsg_before = called_emsg; |
| 3621 | |
| 3622 | ++*arg; |
| 3623 | want_type = parse_type(arg, cctx->ctx_type_list); |
| 3624 | if (called_emsg != called_emsg_before) |
| 3625 | return FAIL; |
| 3626 | |
| 3627 | if (**arg != '>') |
| 3628 | { |
| 3629 | if (*skipwhite(*arg) == '>') |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3630 | semsg(_(e_no_white_space_allowed_before_str), ">"); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3631 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3632 | emsg(_(e_missing_gt)); |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3633 | return FAIL; |
| 3634 | } |
| 3635 | ++*arg; |
| 3636 | if (may_get_next_line_error(*arg - 1, arg, cctx) == FAIL) |
| 3637 | return FAIL; |
| 3638 | } |
| 3639 | |
| 3640 | if (compile_expr7(arg, cctx, ppconst) == FAIL) |
| 3641 | return FAIL; |
| 3642 | |
| 3643 | if (want_type != NULL) |
| 3644 | { |
| 3645 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 3646 | type_T *actual; |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3647 | |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 3648 | generate_ppconst(cctx, ppconst); |
| 3649 | actual = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 3650 | if (check_type(want_type, actual, FALSE, 0) == FAIL) |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3651 | { |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3652 | if (need_type(actual, want_type, -1, cctx, FALSE) == FAIL) |
| 3653 | return FAIL; |
| 3654 | } |
| 3655 | } |
| 3656 | |
| 3657 | return OK; |
| 3658 | } |
| 3659 | |
| 3660 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3661 | * * number multiplication |
| 3662 | * / number division |
| 3663 | * % number modulo |
| 3664 | */ |
| 3665 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3666 | compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3667 | { |
| 3668 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3669 | char_u *next; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3670 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3671 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3672 | // get the first expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3673 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3674 | return FAIL; |
| 3675 | |
| 3676 | /* |
| 3677 | * Repeat computing, until no "*", "/" or "%" is following. |
| 3678 | */ |
| 3679 | for (;;) |
| 3680 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3681 | op = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3682 | if (*op != '*' && *op != '/' && *op != '%') |
| 3683 | break; |
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 | op = skipwhite(*arg); |
| 3688 | } |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3689 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3690 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3691 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3692 | error_white_both(op, 1); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3693 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3694 | } |
| 3695 | *arg = skipwhite(op + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3696 | if (may_get_next_line(op + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3697 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3698 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3699 | // get the second expression |
Bram Moolenaar | 64d662d | 2020-08-09 19:02:50 +0200 | [diff] [blame] | 3700 | if (compile_expr7t(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3701 | return FAIL; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3702 | |
| 3703 | if (ppconst->pp_used == ppconst_used + 2 |
| 3704 | && ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3705 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER) |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3706 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3707 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3708 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3709 | varnumber_T res = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3710 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3711 | // both are numbers: compute the result |
| 3712 | switch (*op) |
| 3713 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3714 | case '*': res = tv1->vval.v_number * tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3715 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3716 | case '/': res = tv1->vval.v_number / tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3717 | break; |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3718 | case '%': res = tv1->vval.v_number % tv2->vval.v_number; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3719 | break; |
| 3720 | } |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3721 | tv1->vval.v_number = res; |
| 3722 | --ppconst->pp_used; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3723 | } |
| 3724 | else |
| 3725 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3726 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3727 | generate_two_op(cctx, op); |
| 3728 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | return OK; |
| 3732 | } |
| 3733 | |
| 3734 | /* |
| 3735 | * + number addition |
| 3736 | * - number subtraction |
| 3737 | * .. string concatenation |
| 3738 | */ |
| 3739 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3740 | compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3741 | { |
| 3742 | char_u *op; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3743 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3744 | int oplen; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3745 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3746 | |
| 3747 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3748 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3749 | return FAIL; |
| 3750 | |
| 3751 | /* |
| 3752 | * Repeat computing, until no "+", "-" or ".." is following. |
| 3753 | */ |
| 3754 | for (;;) |
| 3755 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3756 | op = may_peek_next_line(cctx, *arg, &next); |
| 3757 | if (*op != '+' && *op != '-' && !(*op == '.' && *(op + 1) == '.')) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3758 | break; |
| 3759 | oplen = (*op == '.' ? 2 : 1); |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3760 | if (next != NULL) |
| 3761 | { |
| 3762 | *arg = next_line_from_context(cctx, TRUE); |
| 3763 | op = skipwhite(*arg); |
| 3764 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3765 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3766 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3767 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3768 | error_white_both(op, oplen); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3769 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | *arg = skipwhite(op + oplen); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3773 | if (may_get_next_line(op + oplen, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3774 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3775 | |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3776 | // get the second expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3777 | if (compile_expr6(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3778 | return FAIL; |
| 3779 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3780 | if (ppconst->pp_used == ppconst_used + 2 |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3781 | && (*op == '.' |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3782 | ? (ppconst->pp_tv[ppconst_used].v_type == VAR_STRING |
| 3783 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_STRING) |
| 3784 | : (ppconst->pp_tv[ppconst_used].v_type == VAR_NUMBER |
| 3785 | && ppconst->pp_tv[ppconst_used + 1].v_type == VAR_NUMBER))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3786 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3787 | typval_T *tv1 = &ppconst->pp_tv[ppconst_used]; |
| 3788 | typval_T *tv2 = &ppconst->pp_tv[ppconst_used + 1]; |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3789 | |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3790 | // concat/subtract/add constant numbers |
| 3791 | if (*op == '+') |
| 3792 | tv1->vval.v_number = tv1->vval.v_number + tv2->vval.v_number; |
| 3793 | else if (*op == '-') |
| 3794 | tv1->vval.v_number = tv1->vval.v_number - tv2->vval.v_number; |
| 3795 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3796 | { |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3797 | // concatenate constant strings |
| 3798 | char_u *s1 = tv1->vval.v_string; |
| 3799 | char_u *s2 = tv2->vval.v_string; |
| 3800 | size_t len1 = STRLEN(s1); |
| 3801 | |
| 3802 | tv1->vval.v_string = alloc((int)(len1 + STRLEN(s2) + 1)); |
| 3803 | if (tv1->vval.v_string == NULL) |
| 3804 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3805 | clear_ppconst(ppconst); |
Bram Moolenaar | 7d131b0 | 2020-05-08 19:10:34 +0200 | [diff] [blame] | 3806 | return FAIL; |
| 3807 | } |
| 3808 | mch_memmove(tv1->vval.v_string, s1, len1); |
| 3809 | STRCPY(tv1->vval.v_string + len1, s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3810 | vim_free(s1); |
| 3811 | vim_free(s2); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3812 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3813 | --ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3814 | } |
| 3815 | else |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3816 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3817 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | f0eefce | 2020-05-07 22:19:01 +0200 | [diff] [blame] | 3818 | if (*op == '.') |
| 3819 | { |
| 3820 | if (may_generate_2STRING(-2, cctx) == FAIL |
| 3821 | || may_generate_2STRING(-1, cctx) == FAIL) |
| 3822 | return FAIL; |
| 3823 | generate_instr_drop(cctx, ISN_CONCAT, 1); |
| 3824 | } |
| 3825 | else |
| 3826 | generate_two_op(cctx, op); |
| 3827 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3828 | } |
| 3829 | |
| 3830 | return OK; |
| 3831 | } |
| 3832 | |
| 3833 | /* |
| 3834 | * expr5a == expr5b |
| 3835 | * expr5a =~ expr5b |
| 3836 | * expr5a != expr5b |
| 3837 | * expr5a !~ expr5b |
| 3838 | * expr5a > expr5b |
| 3839 | * expr5a >= expr5b |
| 3840 | * expr5a < expr5b |
| 3841 | * expr5a <= expr5b |
| 3842 | * expr5a is expr5b |
| 3843 | * expr5a isnot expr5b |
| 3844 | * |
| 3845 | * Produces instructions: |
| 3846 | * EVAL expr5a Push result of "expr5a" |
| 3847 | * EVAL expr5b Push result of "expr5b" |
| 3848 | * COMPARE one of the compare instructions |
| 3849 | */ |
| 3850 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3851 | compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3852 | { |
| 3853 | exptype_T type = EXPR_UNKNOWN; |
| 3854 | char_u *p; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3855 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3856 | int len = 2; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3857 | int type_is = FALSE; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3858 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3859 | |
| 3860 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3861 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3862 | return FAIL; |
| 3863 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3864 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 080457c | 2020-03-03 21:53:32 +0100 | [diff] [blame] | 3865 | type = get_compare_type(p, &len, &type_is); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3866 | |
| 3867 | /* |
| 3868 | * If there is a comparative operator, use it. |
| 3869 | */ |
| 3870 | if (type != EXPR_UNKNOWN) |
| 3871 | { |
| 3872 | int ic = FALSE; // Default: do not ignore case |
| 3873 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3874 | if (next != NULL) |
| 3875 | { |
| 3876 | *arg = next_line_from_context(cctx, TRUE); |
| 3877 | p = skipwhite(*arg); |
| 3878 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3879 | if (type_is && (p[len] == '?' || p[len] == '#')) |
| 3880 | { |
| 3881 | semsg(_(e_invexpr2), *arg); |
| 3882 | return FAIL; |
| 3883 | } |
| 3884 | // extra question mark appended: ignore case |
| 3885 | if (p[len] == '?') |
| 3886 | { |
| 3887 | ic = TRUE; |
| 3888 | ++len; |
| 3889 | } |
| 3890 | // extra '#' appended: match case (ignored) |
| 3891 | else if (p[len] == '#') |
| 3892 | ++len; |
| 3893 | // nothing appended: match case |
| 3894 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3895 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len])) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3896 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 3897 | error_white_both(p, len); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3898 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | // get the second variable |
| 3902 | *arg = skipwhite(p + len); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3903 | if (may_get_next_line(p + len, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3904 | return FAIL; |
| 3905 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3906 | if (compile_expr5(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3907 | return FAIL; |
| 3908 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3909 | if (ppconst->pp_used == ppconst_used + 2) |
| 3910 | { |
| 3911 | typval_T * tv1 = &ppconst->pp_tv[ppconst->pp_used - 2]; |
| 3912 | typval_T *tv2 = &ppconst->pp_tv[ppconst->pp_used - 1]; |
| 3913 | int ret; |
| 3914 | |
| 3915 | // Both sides are a constant, compute the result now. |
| 3916 | // First check for a valid combination of types, this is more |
| 3917 | // strict than typval_compare(). |
Bram Moolenaar | 543e6f3 | 2020-07-10 22:45:38 +0200 | [diff] [blame] | 3918 | if (check_compare_types(type, tv1, tv2) == FAIL) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3919 | ret = FAIL; |
| 3920 | else |
| 3921 | { |
| 3922 | ret = typval_compare(tv1, tv2, type, ic); |
| 3923 | tv1->v_type = VAR_BOOL; |
| 3924 | tv1->vval.v_number = tv1->vval.v_number |
| 3925 | ? VVAL_TRUE : VVAL_FALSE; |
| 3926 | clear_tv(tv2); |
| 3927 | --ppconst->pp_used; |
| 3928 | } |
| 3929 | return ret; |
| 3930 | } |
| 3931 | |
| 3932 | generate_ppconst(cctx, ppconst); |
| 3933 | return generate_COMPARE(cctx, type, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | return OK; |
| 3937 | } |
| 3938 | |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 3939 | static int compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst); |
| 3940 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3941 | /* |
| 3942 | * Compile || or &&. |
| 3943 | */ |
| 3944 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3945 | compile_and_or( |
| 3946 | char_u **arg, |
| 3947 | cctx_T *cctx, |
| 3948 | char *op, |
| 3949 | ppconst_T *ppconst, |
| 3950 | int ppconst_used UNUSED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3951 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3952 | char_u *next; |
| 3953 | char_u *p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3954 | int opchar = *op; |
| 3955 | |
| 3956 | if (p[0] == opchar && p[1] == opchar) |
| 3957 | { |
| 3958 | garray_T *instr = &cctx->ctx_instr; |
| 3959 | garray_T end_ga; |
Bram Moolenaar | 4ed124c | 2020-09-09 20:03:46 +0200 | [diff] [blame] | 3960 | garray_T *stack = &cctx->ctx_type_stack; |
| 3961 | type_T **typep; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3962 | |
| 3963 | /* |
| 3964 | * Repeat until there is no following "||" or "&&" |
| 3965 | */ |
| 3966 | ga_init2(&end_ga, sizeof(int), 10); |
| 3967 | while (p[0] == opchar && p[1] == opchar) |
| 3968 | { |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 3969 | if (next != NULL) |
| 3970 | { |
| 3971 | *arg = next_line_from_context(cctx, TRUE); |
| 3972 | p = skipwhite(*arg); |
| 3973 | } |
| 3974 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3975 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[2])) |
| 3976 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 3977 | semsg(_(e_white_space_required_before_and_after_str), op); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3978 | return FAIL; |
| 3979 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3980 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3981 | // TODO: use ppconst if the value is a constant |
| 3982 | generate_ppconst(cctx, ppconst); |
| 3983 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3984 | if (ga_grow(&end_ga, 1) == FAIL) |
| 3985 | { |
| 3986 | ga_clear(&end_ga); |
| 3987 | return FAIL; |
| 3988 | } |
| 3989 | *(((int *)end_ga.ga_data) + end_ga.ga_len) = instr->ga_len; |
| 3990 | ++end_ga.ga_len; |
| 3991 | generate_JUMP(cctx, opchar == '|' |
| 3992 | ? JUMP_AND_KEEP_IF_TRUE : JUMP_AND_KEEP_IF_FALSE, 0); |
| 3993 | |
| 3994 | // eval the next expression |
| 3995 | *arg = skipwhite(p + 2); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 3996 | if (may_get_next_line(p + 2, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 3997 | return FAIL; |
| 3998 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 3999 | if ((opchar == '|' ? compile_expr3(arg, cctx, ppconst) |
| 4000 | : compile_expr4(arg, cctx, ppconst)) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4001 | { |
| 4002 | ga_clear(&end_ga); |
| 4003 | return FAIL; |
| 4004 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4005 | |
| 4006 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4007 | } |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4008 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4009 | |
| 4010 | // Fill in the end label in all jumps. |
| 4011 | while (end_ga.ga_len > 0) |
| 4012 | { |
| 4013 | isn_T *isn; |
| 4014 | |
| 4015 | --end_ga.ga_len; |
| 4016 | isn = ((isn_T *)instr->ga_data) |
| 4017 | + *(((int *)end_ga.ga_data) + end_ga.ga_len); |
| 4018 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4019 | } |
| 4020 | ga_clear(&end_ga); |
Bram Moolenaar | 4ed124c | 2020-09-09 20:03:46 +0200 | [diff] [blame] | 4021 | |
| 4022 | // The resulting type can be used as a bool. |
| 4023 | typep = ((type_T **)stack->ga_data) + stack->ga_len - 1; |
| 4024 | if (*typep != &t_bool) |
| 4025 | { |
| 4026 | type_T *type = alloc_type(cctx->ctx_type_list); |
| 4027 | |
| 4028 | if (type != NULL) |
| 4029 | { |
| 4030 | *type = **typep; |
| 4031 | type->tt_flags |= TTFLAG_BOOL_OK; |
| 4032 | *typep = type; |
| 4033 | } |
| 4034 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4035 | } |
| 4036 | |
| 4037 | return OK; |
| 4038 | } |
| 4039 | |
| 4040 | /* |
| 4041 | * expr4a && expr4a && expr4a logical AND |
| 4042 | * |
| 4043 | * Produces instructions: |
| 4044 | * EVAL expr4a Push result of "expr4a" |
| 4045 | * JUMP_AND_KEEP_IF_FALSE end |
| 4046 | * EVAL expr4b Push result of "expr4b" |
| 4047 | * JUMP_AND_KEEP_IF_FALSE end |
| 4048 | * EVAL expr4c Push result of "expr4c" |
| 4049 | * end: |
| 4050 | */ |
| 4051 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4052 | compile_expr3(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4053 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4054 | int ppconst_used = ppconst->pp_used; |
| 4055 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4056 | // get the first variable |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4057 | if (compile_expr4(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4058 | return FAIL; |
| 4059 | |
| 4060 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4061 | return compile_and_or(arg, cctx, "&&", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4062 | } |
| 4063 | |
| 4064 | /* |
| 4065 | * expr3a || expr3b || expr3c logical OR |
| 4066 | * |
| 4067 | * Produces instructions: |
| 4068 | * EVAL expr3a Push result of "expr3a" |
| 4069 | * JUMP_AND_KEEP_IF_TRUE end |
| 4070 | * EVAL expr3b Push result of "expr3b" |
| 4071 | * JUMP_AND_KEEP_IF_TRUE end |
| 4072 | * EVAL expr3c Push result of "expr3c" |
| 4073 | * end: |
| 4074 | */ |
| 4075 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4076 | compile_expr2(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4077 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4078 | int ppconst_used = ppconst->pp_used; |
| 4079 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4080 | // eval the first expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4081 | if (compile_expr3(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4082 | return FAIL; |
| 4083 | |
| 4084 | // || and && work almost the same |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4085 | return compile_and_or(arg, cctx, "||", ppconst, ppconst_used); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | /* |
| 4089 | * Toplevel expression: expr2 ? expr1a : expr1b |
| 4090 | * |
| 4091 | * Produces instructions: |
| 4092 | * EVAL expr2 Push result of "expr" |
| 4093 | * JUMP_IF_FALSE alt jump if false |
| 4094 | * EVAL expr1a |
| 4095 | * JUMP_ALWAYS end |
| 4096 | * alt: EVAL expr1b |
| 4097 | * end: |
| 4098 | */ |
| 4099 | static int |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4100 | compile_expr1(char_u **arg, cctx_T *cctx, ppconst_T *ppconst) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4101 | { |
| 4102 | char_u *p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4103 | int ppconst_used = ppconst->pp_used; |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4104 | char_u *next; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4105 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 4106 | // Ignore all kinds of errors when not producing code. |
| 4107 | if (cctx->ctx_skip == SKIP_YES) |
| 4108 | { |
| 4109 | skip_expr(arg); |
| 4110 | return OK; |
| 4111 | } |
| 4112 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4113 | // Evaluate the first expression. |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4114 | if (compile_expr2(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4115 | return FAIL; |
| 4116 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4117 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4118 | if (*p == '?') |
| 4119 | { |
| 4120 | garray_T *instr = &cctx->ctx_instr; |
| 4121 | garray_T *stack = &cctx->ctx_type_stack; |
| 4122 | int alt_idx = instr->ga_len; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4123 | int end_idx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4124 | isn_T *isn; |
Bram Moolenaar | 38041da | 2020-06-21 22:17:18 +0200 | [diff] [blame] | 4125 | type_T *type1 = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4126 | type_T *type2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4127 | int has_const_expr = FALSE; |
| 4128 | int const_value = FALSE; |
| 4129 | int save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4130 | |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4131 | if (next != NULL) |
| 4132 | { |
| 4133 | *arg = next_line_from_context(cctx, TRUE); |
| 4134 | p = skipwhite(*arg); |
| 4135 | } |
| 4136 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4137 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4138 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4139 | semsg(_(e_white_space_required_before_and_after_str), "?"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4140 | return FAIL; |
| 4141 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4142 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4143 | if (ppconst->pp_used == ppconst_used + 1) |
| 4144 | { |
| 4145 | // the condition is a constant, we know whether the ? or the : |
| 4146 | // expression is to be evaluated. |
| 4147 | has_const_expr = TRUE; |
| 4148 | const_value = tv2bool(&ppconst->pp_tv[ppconst_used]); |
| 4149 | clear_tv(&ppconst->pp_tv[ppconst_used]); |
| 4150 | --ppconst->pp_used; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4151 | cctx->ctx_skip = save_skip == SKIP_YES || !const_value |
| 4152 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4153 | } |
| 4154 | else |
| 4155 | { |
| 4156 | generate_ppconst(cctx, ppconst); |
| 4157 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 4158 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4159 | |
| 4160 | // evaluate the second expression; any type is accepted |
| 4161 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4162 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4163 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4164 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4165 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4166 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4167 | if (!has_const_expr) |
| 4168 | { |
| 4169 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4170 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4171 | // remember the type and drop it |
| 4172 | --stack->ga_len; |
| 4173 | type1 = ((type_T **)stack->ga_data)[stack->ga_len]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4174 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4175 | end_idx = instr->ga_len; |
| 4176 | generate_JUMP(cctx, JUMP_ALWAYS, 0); |
| 4177 | |
| 4178 | // jump here from JUMP_IF_FALSE |
| 4179 | isn = ((isn_T *)instr->ga_data) + alt_idx; |
| 4180 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4181 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4182 | |
| 4183 | // Check for the ":". |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4184 | p = may_peek_next_line(cctx, *arg, &next); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4185 | if (*p != ':') |
| 4186 | { |
| 4187 | emsg(_(e_missing_colon)); |
| 4188 | return FAIL; |
| 4189 | } |
Bram Moolenaar | 67fbdfe | 2020-06-23 22:26:05 +0200 | [diff] [blame] | 4190 | if (next != NULL) |
| 4191 | { |
| 4192 | *arg = next_line_from_context(cctx, TRUE); |
| 4193 | p = skipwhite(*arg); |
| 4194 | } |
| 4195 | |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4196 | if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[1])) |
| 4197 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4198 | semsg(_(e_white_space_required_before_and_after_str), ":"); |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4199 | return FAIL; |
| 4200 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4201 | |
| 4202 | // evaluate the third expression |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4203 | if (has_const_expr) |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4204 | cctx->ctx_skip = save_skip == SKIP_YES || const_value |
| 4205 | ? SKIP_YES : SKIP_NOT; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4206 | *arg = skipwhite(p + 1); |
Bram Moolenaar | 2c33043 | 2020-04-13 14:41:35 +0200 | [diff] [blame] | 4207 | if (may_get_next_line(p + 1, arg, cctx) == FAIL) |
Bram Moolenaar | 9c7e6dd | 2020-04-12 20:55:20 +0200 | [diff] [blame] | 4208 | return FAIL; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4209 | if (compile_expr1(arg, cctx, ppconst) == FAIL) |
Bram Moolenaar | a6d5368 | 2020-01-28 23:04:06 +0100 | [diff] [blame] | 4210 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4211 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4212 | if (!has_const_expr) |
| 4213 | { |
| 4214 | generate_ppconst(cctx, ppconst); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4215 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4216 | // If the types differ, the result has a more generic type. |
| 4217 | type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4218 | common_type(type1, type2, &type2, cctx->ctx_type_list); |
| 4219 | |
| 4220 | // jump here from JUMP_ALWAYS |
| 4221 | isn = ((isn_T *)instr->ga_data) + end_idx; |
| 4222 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 4223 | } |
| 4224 | |
| 4225 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4226 | } |
| 4227 | return OK; |
| 4228 | } |
| 4229 | |
| 4230 | /* |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4231 | * Toplevel expression. |
| 4232 | */ |
| 4233 | static int |
| 4234 | compile_expr0(char_u **arg, cctx_T *cctx) |
| 4235 | { |
| 4236 | ppconst_T ppconst; |
| 4237 | |
| 4238 | CLEAR_FIELD(ppconst); |
| 4239 | if (compile_expr1(arg, cctx, &ppconst) == FAIL) |
| 4240 | { |
| 4241 | clear_ppconst(&ppconst); |
| 4242 | return FAIL; |
| 4243 | } |
| 4244 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
| 4245 | return FAIL; |
| 4246 | return OK; |
| 4247 | } |
| 4248 | |
| 4249 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4250 | * compile "return [expr]" |
| 4251 | */ |
| 4252 | static char_u * |
| 4253 | compile_return(char_u *arg, int set_return_type, cctx_T *cctx) |
| 4254 | { |
| 4255 | char_u *p = arg; |
| 4256 | garray_T *stack = &cctx->ctx_type_stack; |
| 4257 | type_T *stack_type; |
| 4258 | |
| 4259 | if (*p != NUL && *p != '|' && *p != '\n') |
| 4260 | { |
| 4261 | // compile return argument into instructions |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 4262 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4263 | return NULL; |
| 4264 | |
| 4265 | stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 4266 | if (set_return_type) |
| 4267 | cctx->ctx_ufunc->uf_ret_type = stack_type; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4268 | else |
| 4269 | { |
| 4270 | if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID |
| 4271 | && stack_type->tt_type != VAR_VOID |
| 4272 | && stack_type->tt_type != VAR_UNKNOWN) |
| 4273 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4274 | emsg(_(e_returning_value_in_function_without_return_type)); |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4275 | return NULL; |
| 4276 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4277 | if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, |
| 4278 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4279 | return NULL; |
Bram Moolenaar | 05a5551 | 2020-07-05 15:52:19 +0200 | [diff] [blame] | 4280 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4281 | } |
| 4282 | else |
| 4283 | { |
Bram Moolenaar | 9be61bb | 2020-03-30 22:51:24 +0200 | [diff] [blame] | 4284 | // "set_return_type" cannot be TRUE, only used for a lambda which |
| 4285 | // always has an argument. |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 4286 | if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID |
| 4287 | && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4288 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4289 | emsg(_(e_missing_return_value)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4290 | return NULL; |
| 4291 | } |
| 4292 | |
| 4293 | // No argument, return zero. |
| 4294 | generate_PUSHNR(cctx, 0); |
| 4295 | } |
| 4296 | |
| 4297 | if (generate_instr(cctx, ISN_RETURN) == NULL) |
| 4298 | return NULL; |
| 4299 | |
| 4300 | // "return val | endif" is possible |
| 4301 | return skipwhite(p); |
| 4302 | } |
| 4303 | |
| 4304 | /* |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4305 | * Get a line from the compilation context, compatible with exarg_T getline(). |
| 4306 | * Return a pointer to the line in allocated memory. |
| 4307 | * Return NULL for end-of-file or some error. |
| 4308 | */ |
| 4309 | static char_u * |
| 4310 | exarg_getline( |
| 4311 | int c UNUSED, |
| 4312 | void *cookie, |
| 4313 | int indent UNUSED, |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4314 | getline_opt_T options UNUSED) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4315 | { |
| 4316 | cctx_T *cctx = (cctx_T *)cookie; |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4317 | char_u *p; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4318 | |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4319 | for (;;) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4320 | { |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 4321 | if (cctx->ctx_lnum == cctx->ctx_ufunc->uf_lines.ga_len) |
| 4322 | return NULL; |
| 4323 | ++cctx->ctx_lnum; |
| 4324 | p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum]; |
| 4325 | // Comment lines result in NULL pointers, skip them. |
| 4326 | if (p != NULL) |
| 4327 | return vim_strsave(p); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4328 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4329 | } |
| 4330 | |
| 4331 | /* |
| 4332 | * Compile a nested :def command. |
| 4333 | */ |
| 4334 | static char_u * |
| 4335 | compile_nested_function(exarg_T *eap, cctx_T *cctx) |
| 4336 | { |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4337 | int is_global = *eap->arg == 'g' && eap->arg[1] == ':'; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4338 | char_u *name_start = eap->arg; |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4339 | char_u *name_end = to_name_end(eap->arg, TRUE); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4340 | char_u *lambda_name; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4341 | lvar_T *lvar; |
| 4342 | ufunc_T *ufunc; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4343 | int r; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4344 | |
Bram Moolenaar | 8b848ca | 2020-09-10 22:28:01 +0200 | [diff] [blame] | 4345 | if (*name_start == '!') |
| 4346 | { |
| 4347 | emsg(_(e_cannot_use_bang_with_nested_def)); |
| 4348 | return NULL; |
| 4349 | } |
| 4350 | |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4351 | // Only g:Func() can use a namespace. |
| 4352 | if (name_start[1] == ':' && !is_global) |
| 4353 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4354 | semsg(_(e_namespace_not_supported_str), name_start); |
Bram Moolenaar | bcbf413 | 2020-08-01 22:35:13 +0200 | [diff] [blame] | 4355 | return NULL; |
| 4356 | } |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4357 | if (check_defined(name_start, name_end - name_start, cctx) == FAIL) |
| 4358 | return NULL; |
| 4359 | |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4360 | eap->arg = name_end; |
| 4361 | eap->getline = exarg_getline; |
| 4362 | eap->cookie = cctx; |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4363 | eap->skip = cctx->ctx_skip == SKIP_YES; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4364 | eap->forceit = FALSE; |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4365 | lambda_name = get_lambda_name(); |
| 4366 | ufunc = def_function(eap, lambda_name); |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4367 | |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4368 | if (ufunc == NULL) |
Bram Moolenaar | 7a3330f | 2020-08-27 23:57:57 +0200 | [diff] [blame] | 4369 | return eap->skip ? (char_u *)"" : NULL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 4370 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 4371 | && compile_def_function(ufunc, TRUE, cctx) == FAIL) |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4372 | return NULL; |
| 4373 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4374 | if (is_global) |
| 4375 | { |
| 4376 | char_u *func_name = vim_strnsave(name_start + 2, |
| 4377 | name_end - name_start - 2); |
Bram Moolenaar | 0e65d3d | 2020-05-05 17:53:16 +0200 | [diff] [blame] | 4378 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4379 | if (func_name == NULL) |
| 4380 | r = FAIL; |
| 4381 | else |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4382 | r = generate_NEWFUNC(cctx, lambda_name, func_name); |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4383 | } |
| 4384 | else |
| 4385 | { |
| 4386 | // Define a local variable for the function reference. |
| 4387 | lvar = reserve_local(cctx, name_start, name_end - name_start, |
| 4388 | TRUE, ufunc->uf_func_type); |
Bram Moolenaar | eef2102 | 2020-08-01 22:16:43 +0200 | [diff] [blame] | 4389 | if (lvar == NULL) |
| 4390 | return NULL; |
Bram Moolenaar | 5a849da | 2020-08-08 16:47:30 +0200 | [diff] [blame] | 4391 | if (generate_FUNCREF(cctx, ufunc) == FAIL) |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4392 | return NULL; |
| 4393 | r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 4394 | } |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4395 | |
Bram Moolenaar | 61a8981 | 2020-05-07 16:58:17 +0200 | [diff] [blame] | 4396 | // TODO: warning for trailing text? |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 4397 | return r == FAIL ? NULL : (char_u *)""; |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4398 | } |
| 4399 | |
| 4400 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4401 | * Return the length of an assignment operator, or zero if there isn't one. |
| 4402 | */ |
| 4403 | int |
| 4404 | assignment_len(char_u *p, int *heredoc) |
| 4405 | { |
| 4406 | if (*p == '=') |
| 4407 | { |
| 4408 | if (p[1] == '<' && p[2] == '<') |
| 4409 | { |
| 4410 | *heredoc = TRUE; |
| 4411 | return 3; |
| 4412 | } |
| 4413 | return 1; |
| 4414 | } |
| 4415 | if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=') |
| 4416 | return 2; |
| 4417 | if (STRNCMP(p, "..=", 3) == 0) |
| 4418 | return 3; |
| 4419 | return 0; |
| 4420 | } |
| 4421 | |
| 4422 | // words that cannot be used as a variable |
| 4423 | static char *reserved[] = { |
| 4424 | "true", |
| 4425 | "false", |
| 4426 | NULL |
| 4427 | }; |
| 4428 | |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4429 | typedef enum { |
| 4430 | dest_local, |
| 4431 | dest_option, |
| 4432 | dest_env, |
| 4433 | dest_global, |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 4434 | dest_buffer, |
| 4435 | dest_window, |
| 4436 | dest_tab, |
Bram Moolenaar | 4e12a5d | 2020-02-03 20:50:59 +0100 | [diff] [blame] | 4437 | dest_vimvar, |
| 4438 | dest_script, |
| 4439 | dest_reg, |
| 4440 | } assign_dest_T; |
| 4441 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4442 | /* |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4443 | * Generate the load instruction for "name". |
| 4444 | */ |
| 4445 | static void |
| 4446 | generate_loadvar( |
| 4447 | cctx_T *cctx, |
| 4448 | assign_dest_T dest, |
| 4449 | char_u *name, |
| 4450 | lvar_T *lvar, |
| 4451 | type_T *type) |
| 4452 | { |
| 4453 | switch (dest) |
| 4454 | { |
| 4455 | case dest_option: |
| 4456 | // TODO: check the option exists |
| 4457 | generate_LOAD(cctx, ISN_LOADOPT, 0, name, type); |
| 4458 | break; |
| 4459 | case dest_global: |
| 4460 | generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type); |
| 4461 | break; |
| 4462 | case dest_buffer: |
| 4463 | generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type); |
| 4464 | break; |
| 4465 | case dest_window: |
| 4466 | generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type); |
| 4467 | break; |
| 4468 | case dest_tab: |
| 4469 | generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type); |
| 4470 | break; |
| 4471 | case dest_script: |
| 4472 | compile_load_scriptvar(cctx, |
| 4473 | name + (name[1] == ':' ? 2 : 0), NULL, NULL, TRUE); |
| 4474 | break; |
| 4475 | case dest_env: |
| 4476 | // Include $ in the name here |
| 4477 | generate_LOAD(cctx, ISN_LOADENV, 0, name, type); |
| 4478 | break; |
| 4479 | case dest_reg: |
| 4480 | generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string); |
| 4481 | break; |
| 4482 | case dest_vimvar: |
| 4483 | generate_LOADV(cctx, name + 2, TRUE); |
| 4484 | break; |
| 4485 | case dest_local: |
| 4486 | if (lvar->lv_from_outer) |
| 4487 | generate_LOAD(cctx, ISN_LOADOUTER, lvar->lv_idx, |
| 4488 | NULL, type); |
| 4489 | else |
| 4490 | generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type); |
| 4491 | break; |
| 4492 | } |
| 4493 | } |
| 4494 | |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4495 | void |
| 4496 | vim9_declare_error(char_u *name) |
| 4497 | { |
| 4498 | char *scope = ""; |
| 4499 | |
| 4500 | switch (*name) |
| 4501 | { |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4502 | case 'g': scope = _("global"); break; |
| 4503 | case 'b': scope = _("buffer"); break; |
| 4504 | case 'w': scope = _("window"); break; |
| 4505 | case 't': scope = _("tab"); break; |
| 4506 | case 'v': scope = "v:"; break; |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4507 | case '$': semsg(_(e_cannot_declare_an_environment_variable), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4508 | return; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4509 | case '&': semsg(_(e_cannot_declare_an_option), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4510 | return; |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4511 | case '@': semsg(_(e_cannot_declare_a_register_str), name); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4512 | return; |
Bram Moolenaar | 7fe8755 | 2020-06-21 20:38:28 +0200 | [diff] [blame] | 4513 | default: return; |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4514 | } |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 4515 | semsg(_(e_cannot_declare_a_scope_variable), scope, name); |
Bram Moolenaar | e55b1c0 | 2020-06-21 15:52:59 +0200 | [diff] [blame] | 4516 | } |
| 4517 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4518 | /* |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4519 | * Compile declaration and assignment: |
| 4520 | * "let var", "let var = expr", "const var = expr" and "var = expr" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4521 | * "arg" points to "var". |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4522 | * Return NULL for an error. |
| 4523 | * Return "arg" if it does not look like a variable list. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4524 | */ |
| 4525 | static char_u * |
| 4526 | compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx) |
| 4527 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4528 | char_u *var_start; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4529 | char_u *p; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4530 | char_u *end = arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4531 | char_u *ret = NULL; |
| 4532 | int var_count = 0; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4533 | int var_idx; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4534 | int scriptvar_sid = 0; |
| 4535 | int scriptvar_idx = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4536 | int semicolon = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4537 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4538 | garray_T *stack = &cctx->ctx_type_stack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4539 | char_u *op; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4540 | int oplen = 0; |
| 4541 | int heredoc = FALSE; |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 4542 | type_T *type = &t_any; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4543 | type_T *member_type = &t_any; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4544 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4545 | char_u *sp; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4546 | int is_decl = cmdidx == CMD_let || cmdidx == CMD_const; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4547 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4548 | // Skip over the "var" or "[var, var]" to get to any "=". |
| 4549 | p = skip_var_list(arg, TRUE, &var_count, &semicolon, TRUE); |
| 4550 | if (p == NULL) |
| 4551 | return *arg == '[' ? arg : NULL; |
| 4552 | |
| 4553 | if (var_count > 0 && is_decl) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4554 | { |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 4555 | // TODO: should we allow this, and figure out type inference from list |
| 4556 | // members? |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4557 | emsg(_(e_cannot_use_list_for_declaration)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4558 | return NULL; |
| 4559 | } |
| 4560 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4561 | sp = p; |
| 4562 | p = skipwhite(p); |
| 4563 | op = p; |
| 4564 | oplen = assignment_len(p, &heredoc); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4565 | |
| 4566 | if (var_count > 0 && oplen == 0) |
| 4567 | // can be something like "[1, 2]->func()" |
| 4568 | return arg; |
| 4569 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4570 | if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen]))) |
| 4571 | { |
Bram Moolenaar | bb1b5e2 | 2020-08-05 10:53:21 +0200 | [diff] [blame] | 4572 | error_white_both(op, oplen); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4573 | return NULL; |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 4574 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4575 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4576 | if (heredoc) |
| 4577 | { |
| 4578 | list_T *l; |
| 4579 | listitem_T *li; |
| 4580 | |
| 4581 | // [let] varname =<< [trim] {end} |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 4582 | eap->getline = exarg_getline; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4583 | eap->cookie = cctx; |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 4584 | l = heredoc_get(eap, op + 3, FALSE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4585 | |
| 4586 | // Push each line and the create the list. |
Bram Moolenaar | 00d253e | 2020-04-06 22:13:01 +0200 | [diff] [blame] | 4587 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4588 | { |
| 4589 | generate_PUSHS(cctx, li->li_tv.vval.v_string); |
| 4590 | li->li_tv.vval.v_string = NULL; |
| 4591 | } |
| 4592 | generate_NEWLIST(cctx, l->lv_len); |
| 4593 | type = &t_list_string; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4594 | member_type = &t_list_string; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4595 | list_free(l); |
| 4596 | p += STRLEN(p); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4597 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4598 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4599 | else if (var_count > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4600 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4601 | // for "[var, var] = expr" evaluate the expression here, loop over the |
| 4602 | // list of variables below. |
Bram Moolenaar | d25ec2c | 2020-03-30 21:05:45 +0200 | [diff] [blame] | 4603 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4604 | p = skipwhite(op + oplen); |
| 4605 | if (compile_expr0(&p, cctx) == FAIL) |
| 4606 | return NULL; |
| 4607 | end = p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4608 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4609 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4610 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 4611 | type_T *stacktype; |
| 4612 | |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4613 | stacktype = stack->ga_len == 0 ? &t_void |
| 4614 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4615 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4616 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4617 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4618 | goto theend; |
| 4619 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 4620 | if (need_type(stacktype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4621 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 4622 | // TODO: check the length of a constant list here |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 4623 | generate_CHECKLEN(cctx, semicolon ? var_count - 1 : var_count, |
| 4624 | semicolon); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4625 | } |
| 4626 | } |
| 4627 | |
| 4628 | /* |
| 4629 | * Loop over variables in "[var, var] = expr". |
| 4630 | * For "var = expr" and "let var: type" this is done only once. |
| 4631 | */ |
| 4632 | if (var_count > 0) |
| 4633 | var_start = skipwhite(arg + 1); // skip over the "[" |
| 4634 | else |
| 4635 | var_start = arg; |
| 4636 | for (var_idx = 0; var_idx == 0 || var_idx < var_count; var_idx++) |
| 4637 | { |
| 4638 | char_u *var_end = skip_var_one(var_start, FALSE); |
| 4639 | size_t varlen; |
| 4640 | int new_local = FALSE; |
| 4641 | int opt_type; |
| 4642 | int opt_flags = 0; |
| 4643 | assign_dest_T dest = dest_local; |
| 4644 | int vimvaridx = -1; |
| 4645 | lvar_T *lvar = NULL; |
| 4646 | lvar_T arg_lvar; |
| 4647 | int has_type = FALSE; |
| 4648 | int has_index = FALSE; |
| 4649 | int instr_count = -1; |
| 4650 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4651 | if (*var_start == '@') |
| 4652 | p = var_start + 2; |
| 4653 | else |
| 4654 | { |
Bram Moolenaar | 2e80095 | 2020-08-23 19:34:48 +0200 | [diff] [blame] | 4655 | // skip over the leading "&", "&l:", "&g:" and "$" |
| 4656 | p = skip_option_env_lead(var_start); |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4657 | p = to_name_end(p, TRUE); |
| 4658 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4659 | |
| 4660 | // "a: type" is declaring variable "a" with a type, not "a:". |
| 4661 | if (is_decl && var_end == var_start + 2 && var_end[-1] == ':') |
| 4662 | --var_end; |
| 4663 | if (is_decl && p == var_start + 2 && p[-1] == ':') |
| 4664 | --p; |
| 4665 | |
| 4666 | varlen = p - var_start; |
| 4667 | vim_free(name); |
| 4668 | name = vim_strnsave(var_start, varlen); |
| 4669 | if (name == NULL) |
| 4670 | return NULL; |
| 4671 | if (!heredoc) |
| 4672 | type = &t_any; |
| 4673 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4674 | if (cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4675 | { |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4676 | int declare_error = FALSE; |
| 4677 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4678 | if (*var_start == '&') |
| 4679 | { |
| 4680 | int cc; |
| 4681 | long numval; |
| 4682 | |
| 4683 | dest = dest_option; |
| 4684 | if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4685 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4686 | emsg(_(e_const_option)); |
| 4687 | goto theend; |
| 4688 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4689 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4690 | p = var_start; |
| 4691 | p = find_option_end(&p, &opt_flags); |
| 4692 | if (p == NULL) |
| 4693 | { |
| 4694 | // cannot happen? |
| 4695 | emsg(_(e_letunexp)); |
| 4696 | goto theend; |
| 4697 | } |
| 4698 | cc = *p; |
| 4699 | *p = NUL; |
Bram Moolenaar | 2e80095 | 2020-08-23 19:34:48 +0200 | [diff] [blame] | 4700 | opt_type = get_option_value(skip_option_env_lead(var_start), |
| 4701 | &numval, NULL, opt_flags); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4702 | *p = cc; |
| 4703 | if (opt_type == -3) |
| 4704 | { |
| 4705 | semsg(_(e_unknown_option), var_start); |
| 4706 | goto theend; |
| 4707 | } |
| 4708 | if (opt_type == -2 || opt_type == 0) |
| 4709 | type = &t_string; |
| 4710 | else |
| 4711 | type = &t_number; // both number and boolean option |
| 4712 | } |
| 4713 | else if (*var_start == '$') |
| 4714 | { |
| 4715 | dest = dest_env; |
| 4716 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4717 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4718 | } |
| 4719 | else if (*var_start == '@') |
| 4720 | { |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 4721 | if (!valid_yank_reg(var_start[1], FALSE) || var_start[1] == '.') |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4722 | { |
| 4723 | emsg_invreg(var_start[1]); |
| 4724 | goto theend; |
| 4725 | } |
| 4726 | dest = dest_reg; |
| 4727 | type = &t_string; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4728 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4729 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4730 | else if (varlen > 1 && STRNCMP(var_start, "g:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4731 | { |
| 4732 | dest = dest_global; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4733 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4734 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4735 | else if (varlen > 1 && STRNCMP(var_start, "b:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4736 | { |
| 4737 | dest = dest_buffer; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4738 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4739 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4740 | else if (varlen > 1 && STRNCMP(var_start, "w:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4741 | { |
| 4742 | dest = dest_window; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4743 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4744 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4745 | else if (varlen > 1 && STRNCMP(var_start, "t:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4746 | { |
| 4747 | dest = dest_tab; |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4748 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4749 | } |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4750 | else if (varlen > 1 && STRNCMP(var_start, "v:", 2) == 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4751 | { |
| 4752 | typval_T *vtv; |
| 4753 | int di_flags; |
| 4754 | |
| 4755 | vimvaridx = find_vim_var(name + 2, &di_flags); |
| 4756 | if (vimvaridx < 0) |
| 4757 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4758 | semsg(_(e_variable_not_found_str), var_start); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4759 | goto theend; |
| 4760 | } |
| 4761 | // We use the current value of "sandbox" here, is that OK? |
| 4762 | if (var_check_ro(di_flags, name, FALSE)) |
| 4763 | goto theend; |
| 4764 | dest = dest_vimvar; |
| 4765 | vtv = get_vim_var_tv(vimvaridx); |
Bram Moolenaar | 4cdb13c | 2020-07-22 21:45:14 +0200 | [diff] [blame] | 4766 | type = typval2type_vimvar(vtv, cctx->ctx_type_list); |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4767 | declare_error = is_decl; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4768 | } |
| 4769 | else |
| 4770 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4771 | int idx; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4772 | |
| 4773 | for (idx = 0; reserved[idx] != NULL; ++idx) |
| 4774 | if (STRCMP(reserved[idx], name) == 0) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4775 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4776 | semsg(_(e_cannot_use_reserved_name), name); |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4777 | goto theend; |
| 4778 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4779 | |
| 4780 | lvar = lookup_local(var_start, varlen, cctx); |
| 4781 | if (lvar == NULL) |
| 4782 | { |
| 4783 | CLEAR_FIELD(arg_lvar); |
| 4784 | if (lookup_arg(var_start, varlen, |
| 4785 | &arg_lvar.lv_idx, &arg_lvar.lv_type, |
| 4786 | &arg_lvar.lv_from_outer, cctx) == OK) |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4787 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4788 | if (is_decl) |
| 4789 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4790 | semsg(_(e_str_is_used_as_argument), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4791 | goto theend; |
| 4792 | } |
| 4793 | lvar = &arg_lvar; |
Bram Moolenaar | ec5929d | 2020-04-07 20:53:39 +0200 | [diff] [blame] | 4794 | } |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 4795 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4796 | if (lvar != NULL) |
| 4797 | { |
| 4798 | if (is_decl) |
| 4799 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4800 | semsg(_(e_variable_already_declared), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4801 | goto theend; |
| 4802 | } |
| 4803 | else if (lvar->lv_const) |
| 4804 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4805 | semsg(_(e_cannot_assign_to_constant), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4806 | goto theend; |
| 4807 | } |
| 4808 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4809 | else |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4810 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4811 | int script_namespace = varlen > 1 |
| 4812 | && STRNCMP(var_start, "s:", 2) == 0; |
| 4813 | int script_var = (script_namespace |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 4814 | ? lookup_script(var_start + 2, varlen - 2, FALSE) |
| 4815 | : lookup_script(var_start, varlen, TRUE)) == OK; |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4816 | imported_T *import = |
| 4817 | find_imported(var_start, varlen, cctx); |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4818 | |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4819 | if (script_namespace || script_var || import != NULL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4820 | { |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4821 | char_u *rawname = name + (name[1] == ':' ? 2 : 0); |
| 4822 | |
| 4823 | if (is_decl) |
| 4824 | { |
| 4825 | if (script_namespace) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4826 | semsg(_(e_cannot_declare_script_variable_in_function), |
Bram Moolenaar | 33afa24 | 2020-07-29 19:18:00 +0200 | [diff] [blame] | 4827 | name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4828 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4829 | semsg(_(e_variable_already_declared_in_script), |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4830 | name); |
| 4831 | goto theend; |
| 4832 | } |
| 4833 | else if (cctx->ctx_ufunc->uf_script_ctx_version |
| 4834 | == SCRIPT_VERSION_VIM9 |
| 4835 | && script_namespace |
| 4836 | && !script_var && import == NULL) |
| 4837 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4838 | semsg(_(e_unknown_variable_str), name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4839 | goto theend; |
| 4840 | } |
| 4841 | |
| 4842 | dest = dest_script; |
| 4843 | |
| 4844 | // existing script-local variables should have a type |
| 4845 | scriptvar_sid = current_sctx.sc_sid; |
| 4846 | if (import != NULL) |
| 4847 | scriptvar_sid = import->imp_sid; |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 4848 | if (SCRIPT_ID_VALID(scriptvar_sid)) |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4849 | { |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 4850 | scriptvar_idx = get_script_item_idx(scriptvar_sid, |
| 4851 | rawname, TRUE); |
| 4852 | if (scriptvar_idx > 0) |
| 4853 | { |
| 4854 | scriptitem_T *si = SCRIPT_ITEM(scriptvar_sid); |
| 4855 | svar_T *sv = |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4856 | ((svar_T *)si->sn_var_vals.ga_data) |
| 4857 | + scriptvar_idx; |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 4858 | type = sv->sv_type; |
| 4859 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4860 | } |
| 4861 | } |
| 4862 | else if (name[1] == ':' && name[2] != NUL) |
| 4863 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4864 | semsg(_(e_cannot_use_namespaced_variable), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4865 | goto theend; |
| 4866 | } |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4867 | else if (!is_decl) |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4868 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4869 | semsg(_(e_unknown_variable_str), name); |
Bram Moolenaar | f9b2b49 | 2020-08-05 14:34:14 +0200 | [diff] [blame] | 4870 | goto theend; |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 4871 | } |
Bram Moolenaar | fa211f3 | 2020-08-07 22:00:26 +0200 | [diff] [blame] | 4872 | else if (check_defined(var_start, varlen, cctx) == FAIL) |
| 4873 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4874 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4875 | } |
Bram Moolenaar | c2ee44c | 2020-08-02 16:59:00 +0200 | [diff] [blame] | 4876 | |
| 4877 | if (declare_error) |
| 4878 | { |
| 4879 | vim9_declare_error(name); |
| 4880 | goto theend; |
| 4881 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | // handle "a:name" as a name, not index "name" on "a" |
| 4885 | if (varlen > 1 || var_start[varlen] != ':') |
| 4886 | p = var_end; |
| 4887 | |
| 4888 | if (dest != dest_option) |
| 4889 | { |
| 4890 | if (is_decl && *p == ':') |
| 4891 | { |
| 4892 | // parse optional type: "let var: type = expr" |
| 4893 | if (!VIM_ISWHITE(p[1])) |
| 4894 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 4895 | semsg(_(e_white_space_required_after_str), ":"); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4896 | goto theend; |
| 4897 | } |
| 4898 | p = skipwhite(p + 1); |
| 4899 | type = parse_type(&p, cctx->ctx_type_list); |
| 4900 | has_type = TRUE; |
| 4901 | } |
| 4902 | else if (lvar != NULL) |
| 4903 | type = lvar->lv_type; |
| 4904 | } |
| 4905 | |
| 4906 | if (oplen == 3 && !heredoc && dest != dest_global |
| 4907 | && type->tt_type != VAR_STRING |
| 4908 | && type->tt_type != VAR_ANY) |
| 4909 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4910 | emsg(_(e_can_only_concatenate_to_string)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4911 | goto theend; |
| 4912 | } |
| 4913 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4914 | if (lvar == NULL && dest == dest_local && cctx->ctx_skip != SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4915 | { |
| 4916 | if (oplen > 1 && !heredoc) |
| 4917 | { |
| 4918 | // +=, /=, etc. require an existing variable |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4919 | semsg(_(e_cannot_use_operator_on_new_variable), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4920 | goto theend; |
| 4921 | } |
| 4922 | |
| 4923 | // new local variable |
Bram Moolenaar | 98b4f14 | 2020-08-08 15:46:01 +0200 | [diff] [blame] | 4924 | if ((type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
| 4925 | && var_wrong_func_name(name, TRUE)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4926 | goto theend; |
| 4927 | lvar = reserve_local(cctx, var_start, varlen, |
| 4928 | cmdidx == CMD_const, type); |
| 4929 | if (lvar == NULL) |
| 4930 | goto theend; |
| 4931 | new_local = TRUE; |
| 4932 | } |
| 4933 | |
| 4934 | member_type = type; |
| 4935 | if (var_end > var_start + varlen) |
| 4936 | { |
| 4937 | // Something follows after the variable: "var[idx]". |
| 4938 | if (is_decl) |
| 4939 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4940 | emsg(_(e_cannot_use_index_when_declaring_variable)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4941 | goto theend; |
| 4942 | } |
| 4943 | |
| 4944 | if (var_start[varlen] == '[') |
| 4945 | { |
| 4946 | has_index = TRUE; |
| 4947 | if (type->tt_member == NULL) |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 4948 | member_type = &t_any; |
| 4949 | else |
| 4950 | member_type = type->tt_member; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4951 | } |
| 4952 | else |
| 4953 | { |
| 4954 | semsg("Not supported yet: %s", var_start); |
| 4955 | goto theend; |
| 4956 | } |
| 4957 | } |
| 4958 | else if (lvar == &arg_lvar) |
| 4959 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4960 | semsg(_(e_cannot_assign_to_argument), name); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4961 | goto theend; |
| 4962 | } |
| 4963 | |
| 4964 | if (!heredoc) |
| 4965 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 4966 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4967 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 4968 | if (oplen > 0 && var_count == 0) |
| 4969 | { |
| 4970 | // skip over the "=" and the expression |
| 4971 | p = skipwhite(op + oplen); |
| 4972 | compile_expr0(&p, cctx); |
| 4973 | } |
| 4974 | } |
| 4975 | else if (oplen > 0) |
| 4976 | { |
| 4977 | type_T *stacktype; |
| 4978 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 4979 | // For "var = expr" evaluate the expression. |
| 4980 | if (var_count == 0) |
| 4981 | { |
| 4982 | int r; |
| 4983 | |
| 4984 | // for "+=", "*=", "..=" etc. first load the current value |
| 4985 | if (*op != '=') |
| 4986 | { |
| 4987 | generate_loadvar(cctx, dest, name, lvar, type); |
| 4988 | |
| 4989 | if (has_index) |
| 4990 | { |
| 4991 | // TODO: get member from list or dict |
| 4992 | emsg("Index with operation not supported yet"); |
| 4993 | goto theend; |
| 4994 | } |
| 4995 | } |
| 4996 | |
| 4997 | // Compile the expression. Temporarily hide the new local |
| 4998 | // variable here, it is not available to this expression. |
| 4999 | if (new_local) |
| 5000 | --cctx->ctx_locals.ga_len; |
| 5001 | instr_count = instr->ga_len; |
| 5002 | p = skipwhite(op + oplen); |
| 5003 | r = compile_expr0(&p, cctx); |
| 5004 | if (new_local) |
| 5005 | ++cctx->ctx_locals.ga_len; |
| 5006 | if (r == FAIL) |
| 5007 | goto theend; |
| 5008 | } |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5009 | else if (semicolon && var_idx == var_count - 1) |
| 5010 | { |
| 5011 | // For "[var; var] = expr" get the rest of the list |
| 5012 | if (generate_SLICE(cctx, var_count - 1) == FAIL) |
| 5013 | goto theend; |
| 5014 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5015 | else |
| 5016 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5017 | // For "[var, var] = expr" get the "var_idx" item from the |
| 5018 | // list. |
| 5019 | if (generate_GETITEM(cctx, var_idx) == FAIL) |
| 5020 | return FAIL; |
| 5021 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5022 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5023 | stacktype = stack->ga_len == 0 ? &t_void |
Bram Moolenaar | 6802cce | 2020-07-19 15:49:49 +0200 | [diff] [blame] | 5024 | : ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5025 | if (lvar != NULL && (is_decl || !has_type)) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5026 | { |
Bram Moolenaar | 0f76981 | 2020-09-12 18:32:34 +0200 | [diff] [blame] | 5027 | if ((stacktype->tt_type == VAR_FUNC |
| 5028 | || stacktype->tt_type == VAR_PARTIAL) |
| 5029 | && var_wrong_func_name(name, TRUE)) |
| 5030 | goto theend; |
| 5031 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5032 | if (new_local && !has_type) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5033 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5034 | if (stacktype->tt_type == VAR_VOID) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5035 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5036 | emsg(_(e_cannot_use_void_value)); |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5037 | goto theend; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5038 | } |
| 5039 | else |
| 5040 | { |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5041 | // An empty list or dict has a &t_void member, |
| 5042 | // for a variable that implies &t_any. |
| 5043 | if (stacktype == &t_list_empty) |
| 5044 | lvar->lv_type = &t_list_any; |
| 5045 | else if (stacktype == &t_dict_empty) |
| 5046 | lvar->lv_type = &t_dict_any; |
| 5047 | else |
| 5048 | lvar->lv_type = stacktype; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5049 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5050 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5051 | else if (*op == '=') |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5052 | { |
| 5053 | type_T *use_type = lvar->lv_type; |
| 5054 | |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5055 | // without operator type is here, otherwise below |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5056 | if (has_index) |
| 5057 | { |
| 5058 | use_type = use_type->tt_member; |
| 5059 | if (use_type == NULL) |
| 5060 | use_type = &t_void; |
| 5061 | } |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5062 | if (need_type(stacktype, use_type, -1, cctx, FALSE) |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5063 | == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5064 | goto theend; |
| 5065 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5066 | } |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5067 | else if (*p != '=' && need_type(stacktype, member_type, -1, |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5068 | cctx, FALSE) == FAIL) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5069 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5070 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5071 | else if (cmdidx == CMD_const) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5072 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 5073 | emsg(_(e_const_requires_a_value)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5074 | goto theend; |
| 5075 | } |
| 5076 | else if (!has_type || dest == dest_option) |
| 5077 | { |
Bram Moolenaar | bc4c505 | 2020-08-13 22:47:35 +0200 | [diff] [blame] | 5078 | emsg(_(e_type_or_initialization_required)); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5079 | goto theend; |
| 5080 | } |
| 5081 | else |
| 5082 | { |
| 5083 | // variables are always initialized |
| 5084 | if (ga_grow(instr, 1) == FAIL) |
| 5085 | goto theend; |
| 5086 | switch (member_type->tt_type) |
| 5087 | { |
| 5088 | case VAR_BOOL: |
| 5089 | generate_PUSHBOOL(cctx, VVAL_FALSE); |
| 5090 | break; |
| 5091 | case VAR_FLOAT: |
| 5092 | #ifdef FEAT_FLOAT |
| 5093 | generate_PUSHF(cctx, 0.0); |
| 5094 | #endif |
| 5095 | break; |
| 5096 | case VAR_STRING: |
| 5097 | generate_PUSHS(cctx, NULL); |
| 5098 | break; |
| 5099 | case VAR_BLOB: |
| 5100 | generate_PUSHBLOB(cctx, NULL); |
| 5101 | break; |
| 5102 | case VAR_FUNC: |
| 5103 | generate_PUSHFUNC(cctx, NULL, &t_func_void); |
| 5104 | break; |
| 5105 | case VAR_LIST: |
| 5106 | generate_NEWLIST(cctx, 0); |
| 5107 | break; |
| 5108 | case VAR_DICT: |
| 5109 | generate_NEWDICT(cctx, 0); |
| 5110 | break; |
| 5111 | case VAR_JOB: |
| 5112 | generate_PUSHJOB(cctx, NULL); |
| 5113 | break; |
| 5114 | case VAR_CHANNEL: |
| 5115 | generate_PUSHCHANNEL(cctx, NULL); |
| 5116 | break; |
| 5117 | case VAR_NUMBER: |
| 5118 | case VAR_UNKNOWN: |
| 5119 | case VAR_ANY: |
| 5120 | case VAR_PARTIAL: |
| 5121 | case VAR_VOID: |
| 5122 | case VAR_SPECIAL: // cannot happen |
| 5123 | generate_PUSHNR(cctx, 0); |
| 5124 | break; |
| 5125 | } |
| 5126 | } |
| 5127 | if (var_count == 0) |
| 5128 | end = p; |
| 5129 | } |
| 5130 | |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5131 | // no need to parse more when skipping |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5132 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | 72abcf4 | 2020-06-18 18:26:24 +0200 | [diff] [blame] | 5133 | break; |
| 5134 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5135 | if (oplen > 0 && *op != '=') |
| 5136 | { |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5137 | type_T *expected; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5138 | type_T *stacktype; |
| 5139 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5140 | if (*op == '.') |
| 5141 | expected = &t_string; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5142 | else |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 5143 | expected = member_type; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5144 | stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5145 | if ( |
| 5146 | #ifdef FEAT_FLOAT |
| 5147 | // If variable is float operation with number is OK. |
| 5148 | !(expected == &t_float && stacktype == &t_number) && |
| 5149 | #endif |
| 5150 | need_type(stacktype, expected, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5151 | goto theend; |
| 5152 | |
| 5153 | if (*op == '.') |
Bram Moolenaar | dd29f1b | 2020-08-07 20:46:20 +0200 | [diff] [blame] | 5154 | { |
| 5155 | if (generate_instr_drop(cctx, ISN_CONCAT, 1) == NULL) |
| 5156 | goto theend; |
| 5157 | } |
| 5158 | else if (*op == '+') |
| 5159 | { |
| 5160 | if (generate_add_instr(cctx, |
| 5161 | operator_type(member_type, stacktype), |
| 5162 | member_type, stacktype) == FAIL) |
| 5163 | goto theend; |
| 5164 | } |
Bram Moolenaar | 93ad147 | 2020-08-19 22:02:41 +0200 | [diff] [blame] | 5165 | else if (generate_two_op(cctx, op) == FAIL) |
| 5166 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5167 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5168 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5169 | if (has_index) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5170 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5171 | int r; |
| 5172 | |
| 5173 | // Compile the "idx" in "var[idx]". |
| 5174 | if (new_local) |
| 5175 | --cctx->ctx_locals.ga_len; |
| 5176 | p = skipwhite(var_start + varlen + 1); |
| 5177 | r = compile_expr0(&p, cctx); |
| 5178 | if (new_local) |
| 5179 | ++cctx->ctx_locals.ga_len; |
| 5180 | if (r == FAIL) |
| 5181 | goto theend; |
| 5182 | if (*skipwhite(p) != ']') |
| 5183 | { |
Bram Moolenaar | d110358 | 2020-08-14 22:44:25 +0200 | [diff] [blame] | 5184 | // this should not happen |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5185 | emsg(_(e_missbrac)); |
| 5186 | goto theend; |
| 5187 | } |
Bram Moolenaar | 2caa159 | 2020-08-01 15:53:19 +0200 | [diff] [blame] | 5188 | if (type == &t_any) |
| 5189 | { |
| 5190 | type_T *idx_type = ((type_T **)stack->ga_data)[ |
| 5191 | stack->ga_len - 1]; |
| 5192 | // Index on variable of unknown type: guess the type from the |
| 5193 | // index type: number is dict, otherwise dict. |
| 5194 | // TODO: should do the assignment at runtime |
| 5195 | if (idx_type->tt_type == VAR_NUMBER) |
| 5196 | type = &t_list_any; |
| 5197 | else |
| 5198 | type = &t_dict_any; |
| 5199 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5200 | if (type->tt_type == VAR_DICT |
| 5201 | && may_generate_2STRING(-1, cctx) == FAIL) |
| 5202 | goto theend; |
| 5203 | if (type->tt_type == VAR_LIST |
| 5204 | && ((type_T **)stack->ga_data)[stack->ga_len - 1]->tt_type |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5205 | != VAR_NUMBER) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5206 | { |
| 5207 | emsg(_(e_number_exp)); |
| 5208 | goto theend; |
| 5209 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5210 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5211 | // Load the dict or list. On the stack we then have: |
| 5212 | // - value |
| 5213 | // - index |
| 5214 | // - variable |
| 5215 | generate_loadvar(cctx, dest, name, lvar, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5216 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5217 | if (type->tt_type == VAR_LIST) |
| 5218 | { |
| 5219 | if (generate_instr_drop(cctx, ISN_STORELIST, 3) == FAIL) |
| 5220 | return FAIL; |
| 5221 | } |
| 5222 | else if (type->tt_type == VAR_DICT) |
| 5223 | { |
| 5224 | if (generate_instr_drop(cctx, ISN_STOREDICT, 3) == FAIL) |
| 5225 | return FAIL; |
| 5226 | } |
| 5227 | else |
| 5228 | { |
| 5229 | emsg(_(e_listreq)); |
| 5230 | goto theend; |
| 5231 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5232 | } |
| 5233 | else |
| 5234 | { |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5235 | switch (dest) |
| 5236 | { |
| 5237 | case dest_option: |
Bram Moolenaar | 2e80095 | 2020-08-23 19:34:48 +0200 | [diff] [blame] | 5238 | generate_STOREOPT(cctx, skip_option_env_lead(name), |
| 5239 | opt_flags); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5240 | break; |
| 5241 | case dest_global: |
| 5242 | // include g: with the name, easier to execute that way |
| 5243 | generate_STORE(cctx, ISN_STOREG, 0, name); |
| 5244 | break; |
| 5245 | case dest_buffer: |
| 5246 | // include b: with the name, easier to execute that way |
| 5247 | generate_STORE(cctx, ISN_STOREB, 0, name); |
| 5248 | break; |
| 5249 | case dest_window: |
| 5250 | // include w: with the name, easier to execute that way |
| 5251 | generate_STORE(cctx, ISN_STOREW, 0, name); |
| 5252 | break; |
| 5253 | case dest_tab: |
| 5254 | // include t: with the name, easier to execute that way |
| 5255 | generate_STORE(cctx, ISN_STORET, 0, name); |
| 5256 | break; |
| 5257 | case dest_env: |
| 5258 | generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 5259 | break; |
| 5260 | case dest_reg: |
| 5261 | generate_STORE(cctx, ISN_STOREREG, name[1], NULL); |
| 5262 | break; |
| 5263 | case dest_vimvar: |
| 5264 | generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 5265 | break; |
| 5266 | case dest_script: |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 5267 | { |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5268 | if (scriptvar_idx < 0) |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5269 | { |
| 5270 | char_u *name_s = name; |
| 5271 | |
| 5272 | // Include s: in the name for store_var() |
| 5273 | if (name[1] != ':') |
| 5274 | { |
| 5275 | int len = (int)STRLEN(name) + 3; |
| 5276 | |
| 5277 | name_s = alloc(len); |
| 5278 | if (name_s == NULL) |
| 5279 | name_s = name; |
| 5280 | else |
| 5281 | vim_snprintf((char *)name_s, len, |
| 5282 | "s:%s", name); |
| 5283 | } |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5284 | generate_OLDSCRIPT(cctx, ISN_STORES, name_s, |
| 5285 | scriptvar_sid, type); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5286 | if (name_s != name) |
| 5287 | vim_free(name_s); |
| 5288 | } |
| 5289 | else |
| 5290 | generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 5291 | scriptvar_sid, scriptvar_idx, type); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5292 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5293 | break; |
| 5294 | case dest_local: |
| 5295 | if (lvar != NULL) |
| 5296 | { |
| 5297 | isn_T *isn = ((isn_T *)instr->ga_data) |
| 5298 | + instr->ga_len - 1; |
| 5299 | |
| 5300 | // optimization: turn "var = 123" from ISN_PUSHNR + |
| 5301 | // ISN_STORE into ISN_STORENR |
| 5302 | if (!lvar->lv_from_outer |
| 5303 | && instr->ga_len == instr_count + 1 |
| 5304 | && isn->isn_type == ISN_PUSHNR) |
| 5305 | { |
| 5306 | varnumber_T val = isn->isn_arg.number; |
| 5307 | |
| 5308 | isn->isn_type = ISN_STORENR; |
| 5309 | isn->isn_arg.storenr.stnr_idx = lvar->lv_idx; |
| 5310 | isn->isn_arg.storenr.stnr_val = val; |
| 5311 | if (stack->ga_len > 0) |
| 5312 | --stack->ga_len; |
| 5313 | } |
| 5314 | else if (lvar->lv_from_outer) |
| 5315 | generate_STORE(cctx, ISN_STOREOUTER, lvar->lv_idx, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5316 | NULL); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5317 | else |
| 5318 | generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL); |
| 5319 | } |
| 5320 | break; |
| 5321 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 5322 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5323 | |
| 5324 | if (var_idx + 1 < var_count) |
| 5325 | var_start = skipwhite(var_end + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5326 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5327 | |
| 5328 | // for "[var, var] = expr" drop the "expr" value |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 5329 | if (var_count > 0 && !semicolon) |
| 5330 | { |
| 5331 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5332 | goto theend; |
| 5333 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 5334 | |
Bram Moolenaar | b209750 | 2020-07-19 17:17:02 +0200 | [diff] [blame] | 5335 | ret = skipwhite(end); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5336 | |
| 5337 | theend: |
| 5338 | vim_free(name); |
| 5339 | return ret; |
| 5340 | } |
| 5341 | |
| 5342 | /* |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5343 | * Check if "name" can be "unlet". |
| 5344 | */ |
| 5345 | int |
| 5346 | check_vim9_unlet(char_u *name) |
| 5347 | { |
| 5348 | if (name[1] != ':' || vim_strchr((char_u *)"gwtb", *name) == NULL) |
| 5349 | { |
Bram Moolenaar | 8436773 | 2020-08-23 15:21:55 +0200 | [diff] [blame] | 5350 | // "unlet s:var" is allowed in legacy script. |
| 5351 | if (*name == 's' && !script_is_vim9()) |
| 5352 | return OK; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5353 | semsg(_(e_cannot_unlet_str), name); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5354 | return FAIL; |
| 5355 | } |
| 5356 | return OK; |
| 5357 | } |
| 5358 | |
| 5359 | /* |
| 5360 | * Callback passed to ex_unletlock(). |
| 5361 | */ |
| 5362 | static int |
| 5363 | compile_unlet( |
| 5364 | lval_T *lvp, |
| 5365 | char_u *name_end, |
| 5366 | exarg_T *eap, |
| 5367 | int deep UNUSED, |
| 5368 | void *coookie) |
| 5369 | { |
| 5370 | cctx_T *cctx = coookie; |
| 5371 | |
| 5372 | if (lvp->ll_tv == NULL) |
| 5373 | { |
| 5374 | char_u *p = lvp->ll_name; |
| 5375 | int cc = *name_end; |
| 5376 | int ret = OK; |
| 5377 | |
| 5378 | // Normal name. Only supports g:, w:, t: and b: namespaces. |
| 5379 | *name_end = NUL; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5380 | if (*p == '$') |
| 5381 | ret = generate_UNLET(cctx, ISN_UNLETENV, p + 1, eap->forceit); |
| 5382 | else if (check_vim9_unlet(p) == FAIL) |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5383 | ret = FAIL; |
| 5384 | else |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 5385 | ret = generate_UNLET(cctx, ISN_UNLET, p, eap->forceit); |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 5386 | |
| 5387 | *name_end = cc; |
| 5388 | return ret; |
| 5389 | } |
| 5390 | |
| 5391 | // TODO: unlet {list}[idx] |
| 5392 | // TODO: unlet {dict}[key] |
| 5393 | emsg("Sorry, :unlet not fully implemented yet"); |
| 5394 | return FAIL; |
| 5395 | } |
| 5396 | |
| 5397 | /* |
| 5398 | * compile "unlet var", "lock var" and "unlock var" |
| 5399 | * "arg" points to "var". |
| 5400 | */ |
| 5401 | static char_u * |
| 5402 | compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 5403 | { |
| 5404 | char_u *p = arg; |
| 5405 | |
| 5406 | if (eap->cmdidx != CMD_unlet) |
| 5407 | { |
| 5408 | emsg("Sorry, :lock and unlock not implemented yet"); |
| 5409 | return NULL; |
| 5410 | } |
| 5411 | |
| 5412 | if (*p == '!') |
| 5413 | { |
| 5414 | p = skipwhite(p + 1); |
| 5415 | eap->forceit = TRUE; |
| 5416 | } |
| 5417 | |
| 5418 | ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD, compile_unlet, cctx); |
| 5419 | return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd; |
| 5420 | } |
| 5421 | |
| 5422 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5423 | * Compile an :import command. |
| 5424 | */ |
| 5425 | static char_u * |
| 5426 | compile_import(char_u *arg, cctx_T *cctx) |
| 5427 | { |
Bram Moolenaar | 1c99114 | 2020-07-04 13:15:31 +0200 | [diff] [blame] | 5428 | return handle_import(arg, &cctx->ctx_imports, 0, NULL, cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5429 | } |
| 5430 | |
| 5431 | /* |
| 5432 | * generate a jump to the ":endif"/":endfor"/":endwhile"/":finally"/":endtry". |
| 5433 | */ |
| 5434 | static int |
| 5435 | compile_jump_to_end(endlabel_T **el, jumpwhen_T when, cctx_T *cctx) |
| 5436 | { |
| 5437 | garray_T *instr = &cctx->ctx_instr; |
| 5438 | endlabel_T *endlabel = ALLOC_CLEAR_ONE(endlabel_T); |
| 5439 | |
| 5440 | if (endlabel == NULL) |
| 5441 | return FAIL; |
| 5442 | endlabel->el_next = *el; |
| 5443 | *el = endlabel; |
| 5444 | endlabel->el_end_label = instr->ga_len; |
| 5445 | |
| 5446 | generate_JUMP(cctx, when, 0); |
| 5447 | return OK; |
| 5448 | } |
| 5449 | |
| 5450 | static void |
| 5451 | compile_fill_jump_to_end(endlabel_T **el, cctx_T *cctx) |
| 5452 | { |
| 5453 | garray_T *instr = &cctx->ctx_instr; |
| 5454 | |
| 5455 | while (*el != NULL) |
| 5456 | { |
| 5457 | endlabel_T *cur = (*el); |
| 5458 | isn_T *isn; |
| 5459 | |
| 5460 | isn = ((isn_T *)instr->ga_data) + cur->el_end_label; |
| 5461 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5462 | *el = cur->el_next; |
| 5463 | vim_free(cur); |
| 5464 | } |
| 5465 | } |
| 5466 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5467 | static void |
| 5468 | compile_free_jump_to_end(endlabel_T **el) |
| 5469 | { |
| 5470 | while (*el != NULL) |
| 5471 | { |
| 5472 | endlabel_T *cur = (*el); |
| 5473 | |
| 5474 | *el = cur->el_next; |
| 5475 | vim_free(cur); |
| 5476 | } |
| 5477 | } |
| 5478 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5479 | /* |
| 5480 | * Create a new scope and set up the generic items. |
| 5481 | */ |
| 5482 | static scope_T * |
| 5483 | new_scope(cctx_T *cctx, scopetype_T type) |
| 5484 | { |
| 5485 | scope_T *scope = ALLOC_CLEAR_ONE(scope_T); |
| 5486 | |
| 5487 | if (scope == NULL) |
| 5488 | return NULL; |
| 5489 | scope->se_outer = cctx->ctx_scope; |
| 5490 | cctx->ctx_scope = scope; |
| 5491 | scope->se_type = type; |
| 5492 | scope->se_local_count = cctx->ctx_locals.ga_len; |
| 5493 | return scope; |
| 5494 | } |
| 5495 | |
| 5496 | /* |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5497 | * Free the current scope and go back to the outer scope. |
| 5498 | */ |
| 5499 | static void |
| 5500 | drop_scope(cctx_T *cctx) |
| 5501 | { |
| 5502 | scope_T *scope = cctx->ctx_scope; |
| 5503 | |
| 5504 | if (scope == NULL) |
| 5505 | { |
| 5506 | iemsg("calling drop_scope() without a scope"); |
| 5507 | return; |
| 5508 | } |
| 5509 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 5510 | switch (scope->se_type) |
| 5511 | { |
| 5512 | case IF_SCOPE: |
| 5513 | compile_free_jump_to_end(&scope->se_u.se_if.is_end_label); break; |
| 5514 | case FOR_SCOPE: |
| 5515 | compile_free_jump_to_end(&scope->se_u.se_for.fs_end_label); break; |
| 5516 | case WHILE_SCOPE: |
| 5517 | compile_free_jump_to_end(&scope->se_u.se_while.ws_end_label); break; |
| 5518 | case TRY_SCOPE: |
| 5519 | compile_free_jump_to_end(&scope->se_u.se_try.ts_end_label); break; |
| 5520 | case NO_SCOPE: |
| 5521 | case BLOCK_SCOPE: |
| 5522 | break; |
| 5523 | } |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5524 | vim_free(scope); |
| 5525 | } |
| 5526 | |
| 5527 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5528 | * compile "if expr" |
| 5529 | * |
| 5530 | * "if expr" Produces instructions: |
| 5531 | * EVAL expr Push result of "expr" |
| 5532 | * JUMP_IF_FALSE end |
| 5533 | * ... body ... |
| 5534 | * end: |
| 5535 | * |
| 5536 | * "if expr | else" Produces instructions: |
| 5537 | * EVAL expr Push result of "expr" |
| 5538 | * JUMP_IF_FALSE else |
| 5539 | * ... body ... |
| 5540 | * JUMP_ALWAYS end |
| 5541 | * else: |
| 5542 | * ... body ... |
| 5543 | * end: |
| 5544 | * |
| 5545 | * "if expr1 | elseif expr2 | else" Produces instructions: |
| 5546 | * EVAL expr Push result of "expr" |
| 5547 | * JUMP_IF_FALSE elseif |
| 5548 | * ... body ... |
| 5549 | * JUMP_ALWAYS end |
| 5550 | * elseif: |
| 5551 | * EVAL expr Push result of "expr" |
| 5552 | * JUMP_IF_FALSE else |
| 5553 | * ... body ... |
| 5554 | * JUMP_ALWAYS end |
| 5555 | * else: |
| 5556 | * ... body ... |
| 5557 | * end: |
| 5558 | */ |
| 5559 | static char_u * |
| 5560 | compile_if(char_u *arg, cctx_T *cctx) |
| 5561 | { |
| 5562 | char_u *p = arg; |
| 5563 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5564 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5565 | scope_T *scope; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5566 | skip_T skip_save = cctx->ctx_skip; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5567 | ppconst_T ppconst; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5568 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5569 | CLEAR_FIELD(ppconst); |
| 5570 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5571 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5572 | clear_ppconst(&ppconst); |
| 5573 | return NULL; |
| 5574 | } |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5575 | if (cctx->ctx_skip == SKIP_YES) |
| 5576 | clear_ppconst(&ppconst); |
| 5577 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5578 | { |
| 5579 | // The expression results in a constant. |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5580 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5581 | clear_ppconst(&ppconst); |
| 5582 | } |
| 5583 | else |
| 5584 | { |
| 5585 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5586 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5587 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5588 | return NULL; |
| 5589 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5590 | |
| 5591 | scope = new_scope(cctx, IF_SCOPE); |
| 5592 | if (scope == NULL) |
| 5593 | return NULL; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5594 | scope->se_skip_save = skip_save; |
| 5595 | // "is_had_return" will be reset if any block does not end in :return |
| 5596 | scope->se_u.se_if.is_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5597 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5598 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5599 | { |
| 5600 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5601 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5602 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5603 | } |
| 5604 | else |
| 5605 | scope->se_u.se_if.is_if_label = -1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5606 | |
| 5607 | return p; |
| 5608 | } |
| 5609 | |
| 5610 | static char_u * |
| 5611 | compile_elseif(char_u *arg, cctx_T *cctx) |
| 5612 | { |
| 5613 | char_u *p = arg; |
| 5614 | garray_T *instr = &cctx->ctx_instr; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5615 | int instr_count = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5616 | isn_T *isn; |
| 5617 | scope_T *scope = cctx->ctx_scope; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5618 | ppconst_T ppconst; |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 5619 | skip_T save_skip = cctx->ctx_skip; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5620 | |
| 5621 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5622 | { |
| 5623 | emsg(_(e_elseif_without_if)); |
| 5624 | return NULL; |
| 5625 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5626 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5627 | if (!cctx->ctx_had_return) |
| 5628 | scope->se_u.se_if.is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5629 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5630 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5631 | { |
| 5632 | 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] | 5633 | JUMP_ALWAYS, cctx) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5634 | return NULL; |
| 5635 | // previous "if" or "elseif" jumps here |
| 5636 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5637 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5638 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5639 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5640 | // compile "expr"; if we know it evaluates to FALSE skip the block |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5641 | CLEAR_FIELD(ppconst); |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 5642 | if (cctx->ctx_skip == SKIP_YES) |
| 5643 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5644 | if (compile_expr1(&p, cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5645 | { |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5646 | clear_ppconst(&ppconst); |
| 5647 | return NULL; |
| 5648 | } |
Bram Moolenaar | 749639e | 2020-08-27 23:08:47 +0200 | [diff] [blame] | 5649 | cctx->ctx_skip = save_skip; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5650 | if (scope->se_skip_save == SKIP_YES) |
| 5651 | clear_ppconst(&ppconst); |
| 5652 | else if (instr->ga_len == instr_count && ppconst.pp_used == 1) |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5653 | { |
| 5654 | // The expression results in a constant. |
| 5655 | // TODO: how about nesting? |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 5656 | cctx->ctx_skip = tv2bool(&ppconst.pp_tv[0]) ? SKIP_NOT : SKIP_YES; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5657 | clear_ppconst(&ppconst); |
| 5658 | scope->se_u.se_if.is_if_label = -1; |
| 5659 | } |
| 5660 | else |
| 5661 | { |
| 5662 | // Not a constant, generate instructions for the expression. |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5663 | cctx->ctx_skip = SKIP_UNKNOWN; |
Bram Moolenaar | 7f14155 | 2020-05-09 17:35:53 +0200 | [diff] [blame] | 5664 | if (generate_ppconst(cctx, &ppconst) == FAIL) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5665 | return NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5666 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5667 | // "where" is set when ":elseif", "else" or ":endif" is found |
| 5668 | scope->se_u.se_if.is_if_label = instr->ga_len; |
| 5669 | generate_JUMP(cctx, JUMP_IF_FALSE, 0); |
| 5670 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5671 | |
| 5672 | return p; |
| 5673 | } |
| 5674 | |
| 5675 | static char_u * |
| 5676 | compile_else(char_u *arg, cctx_T *cctx) |
| 5677 | { |
| 5678 | char_u *p = arg; |
| 5679 | garray_T *instr = &cctx->ctx_instr; |
| 5680 | isn_T *isn; |
| 5681 | scope_T *scope = cctx->ctx_scope; |
| 5682 | |
| 5683 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5684 | { |
| 5685 | emsg(_(e_else_without_if)); |
| 5686 | return NULL; |
| 5687 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5688 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5689 | if (!cctx->ctx_had_return) |
| 5690 | scope->se_u.se_if.is_had_return = FALSE; |
| 5691 | scope->se_u.se_if.is_seen_else = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5692 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5693 | if (scope->se_skip_save != SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5694 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5695 | // 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] | 5696 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5697 | { |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5698 | if (!cctx->ctx_had_return |
| 5699 | && compile_jump_to_end(&scope->se_u.se_if.is_end_label, |
| 5700 | JUMP_ALWAYS, cctx) == FAIL) |
| 5701 | return NULL; |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5702 | } |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5703 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5704 | if (cctx->ctx_skip == SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5705 | { |
| 5706 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5707 | { |
| 5708 | // previous "if" or "elseif" jumps here |
| 5709 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5710 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5711 | scope->se_u.se_if.is_if_label = -1; |
| 5712 | } |
| 5713 | } |
| 5714 | |
Bram Moolenaar | 280b0dc | 2020-06-20 13:29:03 +0200 | [diff] [blame] | 5715 | if (cctx->ctx_skip != SKIP_UNKNOWN) |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5716 | cctx->ctx_skip = cctx->ctx_skip == SKIP_YES ? SKIP_NOT : SKIP_YES; |
| 5717 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5718 | |
| 5719 | return p; |
| 5720 | } |
| 5721 | |
| 5722 | static char_u * |
| 5723 | compile_endif(char_u *arg, cctx_T *cctx) |
| 5724 | { |
| 5725 | scope_T *scope = cctx->ctx_scope; |
| 5726 | ifscope_T *ifscope; |
| 5727 | garray_T *instr = &cctx->ctx_instr; |
| 5728 | isn_T *isn; |
| 5729 | |
| 5730 | if (scope == NULL || scope->se_type != IF_SCOPE) |
| 5731 | { |
| 5732 | emsg(_(e_endif_without_if)); |
| 5733 | return NULL; |
| 5734 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5735 | ifscope = &scope->se_u.se_if; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5736 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5737 | if (!cctx->ctx_had_return) |
| 5738 | ifscope->is_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5739 | |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 5740 | if (scope->se_u.se_if.is_if_label >= 0) |
| 5741 | { |
| 5742 | // previous "if" or "elseif" jumps here |
| 5743 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_if.is_if_label; |
| 5744 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 5745 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5746 | // Fill in the "end" label in jumps at the end of the blocks. |
| 5747 | compile_fill_jump_to_end(&ifscope->is_end_label, cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 5748 | cctx->ctx_skip = scope->se_skip_save; |
| 5749 | |
| 5750 | // If all the blocks end in :return and there is an :else then the |
| 5751 | // had_return flag is set. |
| 5752 | cctx->ctx_had_return = ifscope->is_had_return && ifscope->is_seen_else; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5753 | |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5754 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5755 | return arg; |
| 5756 | } |
| 5757 | |
| 5758 | /* |
| 5759 | * compile "for var in expr" |
| 5760 | * |
| 5761 | * Produces instructions: |
| 5762 | * PUSHNR -1 |
| 5763 | * STORE loop-idx Set index to -1 |
| 5764 | * EVAL expr Push result of "expr" |
| 5765 | * top: FOR loop-idx, end Increment index, use list on bottom of stack |
| 5766 | * - if beyond end, jump to "end" |
| 5767 | * - otherwise get item from list and push it |
| 5768 | * STORE var Store item in "var" |
| 5769 | * ... body ... |
| 5770 | * JUMP top Jump back to repeat |
| 5771 | * end: DROP Drop the result of "expr" |
| 5772 | * |
| 5773 | */ |
| 5774 | static char_u * |
| 5775 | compile_for(char_u *arg, cctx_T *cctx) |
| 5776 | { |
| 5777 | char_u *p; |
| 5778 | size_t varlen; |
| 5779 | garray_T *instr = &cctx->ctx_instr; |
| 5780 | garray_T *stack = &cctx->ctx_type_stack; |
| 5781 | scope_T *scope; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5782 | lvar_T *loop_lvar; // loop iteration variable |
| 5783 | lvar_T *var_lvar; // variable for "var" |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5784 | type_T *vartype; |
| 5785 | |
| 5786 | // TODO: list of variables: "for [key, value] in dict" |
| 5787 | // parse "var" |
| 5788 | for (p = arg; eval_isnamec1(*p); ++p) |
| 5789 | ; |
| 5790 | varlen = p - arg; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5791 | var_lvar = lookup_local(arg, varlen, cctx); |
| 5792 | if (var_lvar != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5793 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5794 | semsg(_(e_variable_already_declared), arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5795 | return NULL; |
| 5796 | } |
| 5797 | |
| 5798 | // consume "in" |
| 5799 | p = skipwhite(p); |
| 5800 | if (STRNCMP(p, "in", 2) != 0 || !VIM_ISWHITE(p[2])) |
| 5801 | { |
| 5802 | emsg(_(e_missing_in)); |
| 5803 | return NULL; |
| 5804 | } |
| 5805 | p = skipwhite(p + 2); |
| 5806 | |
| 5807 | |
| 5808 | scope = new_scope(cctx, FOR_SCOPE); |
| 5809 | if (scope == NULL) |
| 5810 | return NULL; |
| 5811 | |
| 5812 | // Reserve a variable to store the loop iteration counter. |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5813 | loop_lvar = reserve_local(cctx, (char_u *)"", 0, FALSE, &t_number); |
| 5814 | if (loop_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5815 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5816 | // out of memory |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5817 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5818 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5819 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5820 | |
| 5821 | // Reserve a variable to store "var" |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5822 | var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any); |
| 5823 | if (var_lvar == NULL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5824 | { |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5825 | // out of memory or used as an argument |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5826 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5827 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5828 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5829 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5830 | generate_STORENR(cctx, loop_lvar->lv_idx, -1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5831 | |
| 5832 | // compile "expr", it remains on the stack until "endfor" |
| 5833 | arg = p; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5834 | if (compile_expr0(&arg, cctx) == FAIL) |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5835 | { |
| 5836 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5837 | return NULL; |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5838 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5839 | |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5840 | // Now that we know the type of "var", check that it is a list, now or at |
| 5841 | // runtime. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5842 | vartype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 5843 | if (need_type(vartype, &t_list_any, -1, cctx, FALSE) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5844 | { |
Bram Moolenaar | 25b70c7 | 2020-04-01 16:34:17 +0200 | [diff] [blame] | 5845 | drop_scope(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5846 | return NULL; |
| 5847 | } |
Bram Moolenaar | 0ad3e89 | 2020-07-05 21:38:11 +0200 | [diff] [blame] | 5848 | 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] | 5849 | var_lvar->lv_type = vartype->tt_member; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5850 | |
| 5851 | // "for_end" is set when ":endfor" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5852 | scope->se_u.se_for.fs_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5853 | |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 5854 | generate_FOR(cctx, loop_lvar->lv_idx); |
| 5855 | generate_STORE(cctx, ISN_STORE, var_lvar->lv_idx, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5856 | |
| 5857 | return arg; |
| 5858 | } |
| 5859 | |
| 5860 | /* |
| 5861 | * compile "endfor" |
| 5862 | */ |
| 5863 | static char_u * |
| 5864 | compile_endfor(char_u *arg, cctx_T *cctx) |
| 5865 | { |
| 5866 | garray_T *instr = &cctx->ctx_instr; |
| 5867 | scope_T *scope = cctx->ctx_scope; |
| 5868 | forscope_T *forscope; |
| 5869 | isn_T *isn; |
| 5870 | |
| 5871 | if (scope == NULL || scope->se_type != FOR_SCOPE) |
| 5872 | { |
| 5873 | emsg(_(e_for)); |
| 5874 | return NULL; |
| 5875 | } |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5876 | forscope = &scope->se_u.se_for; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5877 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5878 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5879 | |
| 5880 | // At end of ":for" scope jump back to the FOR instruction. |
| 5881 | generate_JUMP(cctx, JUMP_ALWAYS, forscope->fs_top_label); |
| 5882 | |
| 5883 | // Fill in the "end" label in the FOR statement so it can jump here |
| 5884 | isn = ((isn_T *)instr->ga_data) + forscope->fs_top_label; |
| 5885 | isn->isn_arg.forloop.for_end = instr->ga_len; |
| 5886 | |
| 5887 | // Fill in the "end" label any BREAK statements |
| 5888 | compile_fill_jump_to_end(&forscope->fs_end_label, cctx); |
| 5889 | |
| 5890 | // Below the ":for" scope drop the "expr" list from the stack. |
| 5891 | if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL) |
| 5892 | return NULL; |
| 5893 | |
| 5894 | vim_free(scope); |
| 5895 | |
| 5896 | return arg; |
| 5897 | } |
| 5898 | |
| 5899 | /* |
| 5900 | * compile "while expr" |
| 5901 | * |
| 5902 | * Produces instructions: |
| 5903 | * top: EVAL expr Push result of "expr" |
| 5904 | * JUMP_IF_FALSE end jump if false |
| 5905 | * ... body ... |
| 5906 | * JUMP top Jump back to repeat |
| 5907 | * end: |
| 5908 | * |
| 5909 | */ |
| 5910 | static char_u * |
| 5911 | compile_while(char_u *arg, cctx_T *cctx) |
| 5912 | { |
| 5913 | char_u *p = arg; |
| 5914 | garray_T *instr = &cctx->ctx_instr; |
| 5915 | scope_T *scope; |
| 5916 | |
| 5917 | scope = new_scope(cctx, WHILE_SCOPE); |
| 5918 | if (scope == NULL) |
| 5919 | return NULL; |
| 5920 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5921 | scope->se_u.se_while.ws_top_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5922 | |
| 5923 | // compile "expr" |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 5924 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5925 | return NULL; |
| 5926 | |
| 5927 | // "while_end" is set when ":endwhile" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5928 | 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] | 5929 | JUMP_IF_FALSE, cctx) == FAIL) |
| 5930 | return FAIL; |
| 5931 | |
| 5932 | return p; |
| 5933 | } |
| 5934 | |
| 5935 | /* |
| 5936 | * compile "endwhile" |
| 5937 | */ |
| 5938 | static char_u * |
| 5939 | compile_endwhile(char_u *arg, cctx_T *cctx) |
| 5940 | { |
| 5941 | scope_T *scope = cctx->ctx_scope; |
| 5942 | |
| 5943 | if (scope == NULL || scope->se_type != WHILE_SCOPE) |
| 5944 | { |
| 5945 | emsg(_(e_while)); |
| 5946 | return NULL; |
| 5947 | } |
| 5948 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 5949 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5950 | |
| 5951 | // At end of ":for" scope jump back to the FOR instruction. |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5952 | 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] | 5953 | |
| 5954 | // Fill in the "end" label in the WHILE statement so it can jump here. |
| 5955 | // And in any jumps for ":break" |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5956 | 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] | 5957 | |
| 5958 | vim_free(scope); |
| 5959 | |
| 5960 | return arg; |
| 5961 | } |
| 5962 | |
| 5963 | /* |
| 5964 | * compile "continue" |
| 5965 | */ |
| 5966 | static char_u * |
| 5967 | compile_continue(char_u *arg, cctx_T *cctx) |
| 5968 | { |
| 5969 | scope_T *scope = cctx->ctx_scope; |
| 5970 | |
| 5971 | for (;;) |
| 5972 | { |
| 5973 | if (scope == NULL) |
| 5974 | { |
| 5975 | emsg(_(e_continue)); |
| 5976 | return NULL; |
| 5977 | } |
| 5978 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 5979 | break; |
| 5980 | scope = scope->se_outer; |
| 5981 | } |
| 5982 | |
| 5983 | // Jump back to the FOR or WHILE instruction. |
| 5984 | generate_JUMP(cctx, JUMP_ALWAYS, |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 5985 | scope->se_type == FOR_SCOPE ? scope->se_u.se_for.fs_top_label |
| 5986 | : scope->se_u.se_while.ws_top_label); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5987 | return arg; |
| 5988 | } |
| 5989 | |
| 5990 | /* |
| 5991 | * compile "break" |
| 5992 | */ |
| 5993 | static char_u * |
| 5994 | compile_break(char_u *arg, cctx_T *cctx) |
| 5995 | { |
| 5996 | scope_T *scope = cctx->ctx_scope; |
| 5997 | endlabel_T **el; |
| 5998 | |
| 5999 | for (;;) |
| 6000 | { |
| 6001 | if (scope == NULL) |
| 6002 | { |
| 6003 | emsg(_(e_break)); |
| 6004 | return NULL; |
| 6005 | } |
| 6006 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 6007 | break; |
| 6008 | scope = scope->se_outer; |
| 6009 | } |
| 6010 | |
| 6011 | // Jump to the end of the FOR or WHILE loop. |
| 6012 | if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6013 | el = &scope->se_u.se_for.fs_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6014 | else |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6015 | el = &scope->se_u.se_while.ws_end_label; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6016 | if (compile_jump_to_end(el, JUMP_ALWAYS, cctx) == FAIL) |
| 6017 | return FAIL; |
| 6018 | |
| 6019 | return arg; |
| 6020 | } |
| 6021 | |
| 6022 | /* |
| 6023 | * compile "{" start of block |
| 6024 | */ |
| 6025 | static char_u * |
| 6026 | compile_block(char_u *arg, cctx_T *cctx) |
| 6027 | { |
| 6028 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6029 | return NULL; |
| 6030 | return skipwhite(arg + 1); |
| 6031 | } |
| 6032 | |
| 6033 | /* |
| 6034 | * compile end of block: drop one scope |
| 6035 | */ |
| 6036 | static void |
| 6037 | compile_endblock(cctx_T *cctx) |
| 6038 | { |
| 6039 | scope_T *scope = cctx->ctx_scope; |
| 6040 | |
| 6041 | cctx->ctx_scope = scope->se_outer; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 6042 | unwind_locals(cctx, scope->se_local_count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6043 | vim_free(scope); |
| 6044 | } |
| 6045 | |
| 6046 | /* |
| 6047 | * compile "try" |
| 6048 | * Creates a new scope for the try-endtry, pointing to the first catch and |
| 6049 | * finally. |
| 6050 | * Creates another scope for the "try" block itself. |
| 6051 | * TRY instruction sets up exception handling at runtime. |
| 6052 | * |
| 6053 | * "try" |
| 6054 | * TRY -> catch1, -> finally push trystack entry |
| 6055 | * ... try block |
| 6056 | * "throw {exception}" |
| 6057 | * EVAL {exception} |
| 6058 | * THROW create exception |
| 6059 | * ... try block |
| 6060 | * " catch {expr}" |
| 6061 | * JUMP -> finally |
| 6062 | * catch1: PUSH exeception |
| 6063 | * EVAL {expr} |
| 6064 | * MATCH |
| 6065 | * JUMP nomatch -> catch2 |
| 6066 | * CATCH remove exception |
| 6067 | * ... catch block |
| 6068 | * " catch" |
| 6069 | * JUMP -> finally |
| 6070 | * catch2: CATCH remove exception |
| 6071 | * ... catch block |
| 6072 | * " finally" |
| 6073 | * finally: |
| 6074 | * ... finally block |
| 6075 | * " endtry" |
| 6076 | * ENDTRY pop trystack entry, may rethrow |
| 6077 | */ |
| 6078 | static char_u * |
| 6079 | compile_try(char_u *arg, cctx_T *cctx) |
| 6080 | { |
| 6081 | garray_T *instr = &cctx->ctx_instr; |
| 6082 | scope_T *try_scope; |
| 6083 | scope_T *scope; |
| 6084 | |
| 6085 | // scope that holds the jumps that go to catch/finally/endtry |
| 6086 | try_scope = new_scope(cctx, TRY_SCOPE); |
| 6087 | if (try_scope == NULL) |
| 6088 | return NULL; |
| 6089 | |
| 6090 | // "catch" is set when the first ":catch" is found. |
| 6091 | // "finally" is set when ":finally" or ":endtry" is found |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6092 | try_scope->se_u.se_try.ts_try_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6093 | if (generate_instr(cctx, ISN_TRY) == NULL) |
| 6094 | return NULL; |
| 6095 | |
| 6096 | // scope for the try block itself |
| 6097 | scope = new_scope(cctx, BLOCK_SCOPE); |
| 6098 | if (scope == NULL) |
| 6099 | return NULL; |
| 6100 | |
| 6101 | return arg; |
| 6102 | } |
| 6103 | |
| 6104 | /* |
| 6105 | * compile "catch {expr}" |
| 6106 | */ |
| 6107 | static char_u * |
| 6108 | compile_catch(char_u *arg, cctx_T *cctx UNUSED) |
| 6109 | { |
| 6110 | scope_T *scope = cctx->ctx_scope; |
| 6111 | garray_T *instr = &cctx->ctx_instr; |
| 6112 | char_u *p; |
| 6113 | isn_T *isn; |
| 6114 | |
| 6115 | // end block scope from :try or :catch |
| 6116 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6117 | compile_endblock(cctx); |
| 6118 | scope = cctx->ctx_scope; |
| 6119 | |
| 6120 | // Error if not in a :try scope |
| 6121 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6122 | { |
| 6123 | emsg(_(e_catch)); |
| 6124 | return NULL; |
| 6125 | } |
| 6126 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6127 | if (scope->se_u.se_try.ts_caught_all) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6128 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6129 | emsg(_(e_catch_unreachable_after_catch_all)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6130 | return NULL; |
| 6131 | } |
| 6132 | |
| 6133 | // Jump from end of previous block to :finally or :endtry |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6134 | 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] | 6135 | JUMP_ALWAYS, cctx) == FAIL) |
| 6136 | return NULL; |
| 6137 | |
| 6138 | // End :try or :catch scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6139 | 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] | 6140 | if (isn->isn_arg.try.try_catch == 0) |
| 6141 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6142 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6143 | { |
| 6144 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6145 | 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] | 6146 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6147 | } |
| 6148 | |
| 6149 | p = skipwhite(arg); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6150 | if (ends_excmd2(arg, p)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6151 | { |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6152 | scope->se_u.se_try.ts_caught_all = TRUE; |
| 6153 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6154 | } |
| 6155 | else |
| 6156 | { |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6157 | char_u *end; |
| 6158 | char_u *pat; |
| 6159 | char_u *tofree = NULL; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6160 | int dropped = 0; |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6161 | int len; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6162 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6163 | // Push v:exception, push {expr} and MATCH |
| 6164 | generate_instr_type(cctx, ISN_PUSHEXC, &t_string); |
| 6165 | |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6166 | end = skip_regexp_ex(p + 1, *p, TRUE, &tofree, &dropped); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6167 | if (*end != *p) |
| 6168 | { |
Bram Moolenaar | 7cb6fc2 | 2020-08-21 22:36:47 +0200 | [diff] [blame] | 6169 | semsg(_(e_separator_mismatch_str), p); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6170 | vim_free(tofree); |
| 6171 | return FAIL; |
| 6172 | } |
| 6173 | if (tofree == NULL) |
Bram Moolenaar | 3dd6460 | 2020-02-13 20:31:28 +0100 | [diff] [blame] | 6174 | len = (int)(end - (p + 1)); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6175 | else |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6176 | len = (int)(end - tofree); |
| 6177 | pat = vim_strnsave(tofree == NULL ? p + 1 : tofree, len); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6178 | vim_free(tofree); |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6179 | p += len + 2 + dropped; |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 6180 | if (pat == NULL) |
| 6181 | return FAIL; |
| 6182 | if (generate_PUSHS(cctx, pat) == FAIL) |
| 6183 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6184 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6185 | if (generate_COMPARE(cctx, EXPR_MATCH, FALSE) == FAIL) |
| 6186 | return NULL; |
| 6187 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6188 | scope->se_u.se_try.ts_catch_label = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6189 | if (generate_JUMP(cctx, JUMP_IF_FALSE, 0) == FAIL) |
| 6190 | return NULL; |
| 6191 | } |
| 6192 | |
| 6193 | if (generate_instr(cctx, ISN_CATCH) == NULL) |
| 6194 | return NULL; |
| 6195 | |
| 6196 | if (new_scope(cctx, BLOCK_SCOPE) == NULL) |
| 6197 | return NULL; |
| 6198 | return p; |
| 6199 | } |
| 6200 | |
| 6201 | static char_u * |
| 6202 | compile_finally(char_u *arg, cctx_T *cctx) |
| 6203 | { |
| 6204 | scope_T *scope = cctx->ctx_scope; |
| 6205 | garray_T *instr = &cctx->ctx_instr; |
| 6206 | isn_T *isn; |
| 6207 | |
| 6208 | // end block scope from :try or :catch |
| 6209 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6210 | compile_endblock(cctx); |
| 6211 | scope = cctx->ctx_scope; |
| 6212 | |
| 6213 | // Error if not in a :try scope |
| 6214 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6215 | { |
| 6216 | emsg(_(e_finally)); |
| 6217 | return NULL; |
| 6218 | } |
| 6219 | |
| 6220 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6221 | 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] | 6222 | if (isn->isn_arg.try.try_finally != 0) |
| 6223 | { |
| 6224 | emsg(_(e_finally_dup)); |
| 6225 | return NULL; |
| 6226 | } |
| 6227 | |
| 6228 | // 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] | 6229 | 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] | 6230 | |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 6231 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6232 | if (scope->se_u.se_try.ts_catch_label != 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6233 | { |
| 6234 | // Previous catch without match jumps here |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6235 | 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] | 6236 | isn->isn_arg.jump.jump_where = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6237 | scope->se_u.se_try.ts_catch_label = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6238 | } |
| 6239 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6240 | // TODO: set index in ts_finally_label jumps |
| 6241 | |
| 6242 | return arg; |
| 6243 | } |
| 6244 | |
| 6245 | static char_u * |
| 6246 | compile_endtry(char_u *arg, cctx_T *cctx) |
| 6247 | { |
| 6248 | scope_T *scope = cctx->ctx_scope; |
| 6249 | garray_T *instr = &cctx->ctx_instr; |
| 6250 | isn_T *isn; |
| 6251 | |
| 6252 | // end block scope from :catch or :finally |
| 6253 | if (scope != NULL && scope->se_type == BLOCK_SCOPE) |
| 6254 | compile_endblock(cctx); |
| 6255 | scope = cctx->ctx_scope; |
| 6256 | |
| 6257 | // Error if not in a :try scope |
| 6258 | if (scope == NULL || scope->se_type != TRY_SCOPE) |
| 6259 | { |
| 6260 | if (scope == NULL) |
| 6261 | emsg(_(e_no_endtry)); |
| 6262 | else if (scope->se_type == WHILE_SCOPE) |
| 6263 | emsg(_(e_endwhile)); |
Bram Moolenaar | 5b18c24 | 2020-01-28 22:30:32 +0100 | [diff] [blame] | 6264 | else if (scope->se_type == FOR_SCOPE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6265 | emsg(_(e_endfor)); |
| 6266 | else |
| 6267 | emsg(_(e_endif)); |
| 6268 | return NULL; |
| 6269 | } |
| 6270 | |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6271 | 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] | 6272 | if (isn->isn_arg.try.try_catch == 0 && isn->isn_arg.try.try_finally == 0) |
| 6273 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6274 | emsg(_(e_missing_catch_or_finally)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6275 | return NULL; |
| 6276 | } |
| 6277 | |
| 6278 | // Fill in the "end" label in jumps at the end of the blocks, if not done |
| 6279 | // by ":finally". |
Bram Moolenaar | 0ff6aad | 2020-01-29 21:27:21 +0100 | [diff] [blame] | 6280 | 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] | 6281 | |
| 6282 | // End :catch or :finally scope: set value in ISN_TRY instruction |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6283 | if (isn->isn_arg.try.try_catch == 0) |
| 6284 | isn->isn_arg.try.try_catch = instr->ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6285 | if (isn->isn_arg.try.try_finally == 0) |
| 6286 | isn->isn_arg.try.try_finally = instr->ga_len; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 6287 | |
| 6288 | if (scope->se_u.se_try.ts_catch_label != 0) |
| 6289 | { |
| 6290 | // Last catch without match jumps here |
| 6291 | isn = ((isn_T *)instr->ga_data) + scope->se_u.se_try.ts_catch_label; |
| 6292 | isn->isn_arg.jump.jump_where = instr->ga_len; |
| 6293 | } |
| 6294 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6295 | compile_endblock(cctx); |
| 6296 | |
| 6297 | if (generate_instr(cctx, ISN_ENDTRY) == NULL) |
| 6298 | return NULL; |
| 6299 | return arg; |
| 6300 | } |
| 6301 | |
| 6302 | /* |
| 6303 | * compile "throw {expr}" |
| 6304 | */ |
| 6305 | static char_u * |
| 6306 | compile_throw(char_u *arg, cctx_T *cctx UNUSED) |
| 6307 | { |
| 6308 | char_u *p = skipwhite(arg); |
| 6309 | |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6310 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6311 | return NULL; |
| 6312 | if (may_generate_2STRING(-1, cctx) == FAIL) |
| 6313 | return NULL; |
| 6314 | if (generate_instr_drop(cctx, ISN_THROW, 1) == NULL) |
| 6315 | return NULL; |
| 6316 | |
| 6317 | return p; |
| 6318 | } |
| 6319 | |
| 6320 | /* |
| 6321 | * compile "echo expr" |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6322 | * compile "echomsg expr" |
| 6323 | * compile "echoerr expr" |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6324 | * compile "execute expr" |
| 6325 | */ |
| 6326 | static char_u * |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6327 | compile_mult_expr(char_u *arg, int cmdidx, cctx_T *cctx) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6328 | { |
| 6329 | char_u *p = arg; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6330 | char_u *prev; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6331 | int count = 0; |
| 6332 | |
| 6333 | for (;;) |
| 6334 | { |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6335 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6336 | return NULL; |
| 6337 | ++count; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6338 | prev = p; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6339 | p = skipwhite(p); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 6340 | if (ends_excmd2(prev, p)) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6341 | break; |
| 6342 | } |
| 6343 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6344 | if (cmdidx == CMD_echo || cmdidx == CMD_echon) |
| 6345 | generate_ECHO(cctx, cmdidx == CMD_echo, count); |
| 6346 | else if (cmdidx == CMD_execute) |
| 6347 | generate_MULT_EXPR(cctx, ISN_EXECUTE, count); |
| 6348 | else if (cmdidx == CMD_echomsg) |
| 6349 | generate_MULT_EXPR(cctx, ISN_ECHOMSG, count); |
| 6350 | else |
| 6351 | generate_MULT_EXPR(cctx, ISN_ECHOERR, count); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6352 | return p; |
| 6353 | } |
| 6354 | |
| 6355 | /* |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 6356 | * :put r |
| 6357 | * :put ={expr} |
| 6358 | */ |
| 6359 | static char_u * |
| 6360 | compile_put(char_u *arg, exarg_T *eap, cctx_T *cctx) |
| 6361 | { |
| 6362 | char_u *line = arg; |
| 6363 | linenr_T lnum; |
| 6364 | char *errormsg; |
| 6365 | int above = FALSE; |
| 6366 | |
| 6367 | if (*arg == '!') |
| 6368 | { |
| 6369 | above = TRUE; |
| 6370 | line = skipwhite(arg + 1); |
| 6371 | } |
| 6372 | eap->regname = *line; |
| 6373 | |
| 6374 | if (eap->regname == '=') |
| 6375 | { |
| 6376 | char_u *p = line + 1; |
| 6377 | |
| 6378 | if (compile_expr0(&p, cctx) == FAIL) |
| 6379 | return NULL; |
| 6380 | line = p; |
| 6381 | } |
| 6382 | else if (eap->regname != NUL) |
| 6383 | ++line; |
| 6384 | |
| 6385 | // TODO: if the range is something like "$" need to evaluate at runtime |
| 6386 | if (parse_cmd_address(eap, &errormsg, FALSE) == FAIL) |
| 6387 | return NULL; |
| 6388 | if (eap->addr_count == 0) |
| 6389 | lnum = -1; |
| 6390 | else |
| 6391 | lnum = eap->line2; |
| 6392 | if (above) |
| 6393 | --lnum; |
| 6394 | |
| 6395 | generate_PUT(cctx, eap->regname, lnum); |
| 6396 | return line; |
| 6397 | } |
| 6398 | |
| 6399 | /* |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6400 | * A command that is not compiled, execute with legacy code. |
| 6401 | */ |
| 6402 | static char_u * |
| 6403 | compile_exec(char_u *line, exarg_T *eap, cctx_T *cctx) |
| 6404 | { |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6405 | char_u *p; |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6406 | int has_expr = FALSE; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6407 | char_u *nextcmd = (char_u *)""; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6408 | |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6409 | if (cctx->ctx_skip == SKIP_YES) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6410 | goto theend; |
| 6411 | |
Bram Moolenaar | 7d41aa8 | 2020-04-26 14:29:56 +0200 | [diff] [blame] | 6412 | if (eap->cmdidx >= 0 && eap->cmdidx < CMD_SIZE) |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6413 | { |
| 6414 | long argt = excmd_get_argt(eap->cmdidx); |
| 6415 | int usefilter = FALSE; |
| 6416 | |
| 6417 | has_expr = argt & (EX_XFILE | EX_EXPAND); |
| 6418 | |
| 6419 | // If the command can be followed by a bar, find the bar and truncate |
| 6420 | // it, so that the following command can be compiled. |
| 6421 | // The '|' is overwritten with a NUL, it is put back below. |
| 6422 | if ((eap->cmdidx == CMD_write || eap->cmdidx == CMD_read) |
| 6423 | && *eap->arg == '!') |
| 6424 | // :w !filter or :r !filter or :r! filter |
| 6425 | usefilter = TRUE; |
| 6426 | if ((argt & EX_TRLBAR) && !usefilter) |
| 6427 | { |
Bram Moolenaar | b8a9296 | 2020-08-20 18:02:47 +0200 | [diff] [blame] | 6428 | eap->argt = argt; |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6429 | separate_nextcmd(eap); |
| 6430 | if (eap->nextcmd != NULL) |
| 6431 | nextcmd = eap->nextcmd; |
| 6432 | } |
| 6433 | } |
| 6434 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6435 | if (eap->cmdidx == CMD_syntax && STRNCMP(eap->arg, "include ", 8) == 0) |
| 6436 | { |
| 6437 | // expand filename in "syntax include [@group] filename" |
| 6438 | has_expr = TRUE; |
| 6439 | eap->arg = skipwhite(eap->arg + 7); |
| 6440 | if (*eap->arg == '@') |
| 6441 | eap->arg = skiptowhite(eap->arg); |
| 6442 | } |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6443 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 6444 | if (has_expr && (p = (char_u *)strstr((char *)eap->arg, "`=")) != NULL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6445 | { |
| 6446 | int count = 0; |
| 6447 | char_u *start = skipwhite(line); |
| 6448 | |
| 6449 | // :cmd xxx`=expr1`yyy`=expr2`zzz |
| 6450 | // PUSHS ":cmd xxx" |
| 6451 | // eval expr1 |
| 6452 | // PUSHS "yyy" |
| 6453 | // eval expr2 |
| 6454 | // PUSHS "zzz" |
| 6455 | // EXECCONCAT 5 |
| 6456 | for (;;) |
| 6457 | { |
| 6458 | if (p > start) |
| 6459 | { |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 6460 | generate_PUSHS(cctx, vim_strnsave(start, p - start)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6461 | ++count; |
| 6462 | } |
| 6463 | p += 2; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6464 | if (compile_expr0(&p, cctx) == FAIL) |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6465 | return NULL; |
| 6466 | may_generate_2STRING(-1, cctx); |
| 6467 | ++count; |
| 6468 | p = skipwhite(p); |
| 6469 | if (*p != '`') |
| 6470 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6471 | emsg(_(e_missing_backtick)); |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6472 | return NULL; |
| 6473 | } |
| 6474 | start = p + 1; |
| 6475 | |
| 6476 | p = (char_u *)strstr((char *)start, "`="); |
| 6477 | if (p == NULL) |
| 6478 | { |
| 6479 | if (*skipwhite(start) != NUL) |
| 6480 | { |
| 6481 | generate_PUSHS(cctx, vim_strsave(start)); |
| 6482 | ++count; |
| 6483 | } |
| 6484 | break; |
| 6485 | } |
| 6486 | } |
| 6487 | generate_EXECCONCAT(cctx, count); |
| 6488 | } |
| 6489 | else |
| 6490 | generate_EXEC(cctx, line); |
| 6491 | |
| 6492 | theend: |
Bram Moolenaar | e9f262b | 2020-07-05 14:57:51 +0200 | [diff] [blame] | 6493 | if (*nextcmd != NUL) |
| 6494 | { |
| 6495 | // the parser expects a pointer to the bar, put it back |
| 6496 | --nextcmd; |
| 6497 | *nextcmd = '|'; |
| 6498 | } |
| 6499 | |
| 6500 | return nextcmd; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6501 | } |
| 6502 | |
| 6503 | /* |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6504 | * Add a function to the list of :def functions. |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6505 | * 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] | 6506 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6507 | static int |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6508 | add_def_function(ufunc_T *ufunc) |
| 6509 | { |
| 6510 | dfunc_T *dfunc; |
| 6511 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6512 | if (def_functions.ga_len == 0) |
| 6513 | { |
| 6514 | // The first position is not used, so that a zero uf_dfunc_idx means it |
| 6515 | // wasn't set. |
| 6516 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6517 | return FAIL; |
| 6518 | ++def_functions.ga_len; |
| 6519 | } |
| 6520 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6521 | // Add the function to "def_functions". |
| 6522 | if (ga_grow(&def_functions, 1) == FAIL) |
| 6523 | return FAIL; |
| 6524 | dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len; |
| 6525 | CLEAR_POINTER(dfunc); |
| 6526 | dfunc->df_idx = def_functions.ga_len; |
| 6527 | ufunc->uf_dfunc_idx = dfunc->df_idx; |
| 6528 | dfunc->df_ufunc = ufunc; |
| 6529 | ++def_functions.ga_len; |
| 6530 | return OK; |
| 6531 | } |
| 6532 | |
| 6533 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6534 | * After ex_function() has collected all the function lines: parse and compile |
| 6535 | * the lines into instructions. |
| 6536 | * Adds the function to "def_functions". |
| 6537 | * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the |
| 6538 | * return statement (used for lambda). |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6539 | * "outer_cctx" is set for a nested function. |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 6540 | * This can be used recursively through compile_lambda(), which may reallocate |
| 6541 | * "def_functions". |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6542 | * Returns OK or FAIL. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6543 | */ |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 6544 | int |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6545 | 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] | 6546 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6547 | char_u *line = NULL; |
| 6548 | char_u *p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6549 | char *errormsg = NULL; // error message |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6550 | cctx_T cctx; |
| 6551 | garray_T *instr; |
| 6552 | int called_emsg_before = called_emsg; |
| 6553 | int ret = FAIL; |
| 6554 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6555 | int do_estack_push; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6556 | int emsg_before = called_emsg; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6557 | int new_def_function = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6558 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6559 | // When using a function that was compiled before: Free old instructions. |
| 6560 | // Otherwise add a new entry in "def_functions". |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 6561 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6562 | { |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6563 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 6564 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 6565 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6566 | } |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 6567 | else |
| 6568 | { |
| 6569 | if (add_def_function(ufunc) == FAIL) |
| 6570 | return FAIL; |
| 6571 | new_def_function = TRUE; |
| 6572 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6573 | |
Bram Moolenaar | 985116a | 2020-07-12 17:31:09 +0200 | [diff] [blame] | 6574 | ufunc->uf_def_status = UF_COMPILING; |
| 6575 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6576 | CLEAR_FIELD(cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6577 | cctx.ctx_ufunc = ufunc; |
| 6578 | cctx.ctx_lnum = -1; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 6579 | cctx.ctx_outer = outer_cctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6580 | ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); |
| 6581 | ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); |
| 6582 | ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); |
| 6583 | cctx.ctx_type_list = &ufunc->uf_type_list; |
| 6584 | ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); |
| 6585 | instr = &cctx.ctx_instr; |
| 6586 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6587 | // Set the context to the function, it may be compiled when called from |
| 6588 | // another script. Set the script version to the most modern one. |
| 6589 | // The line number will be set in next_line_from_context(). |
| 6590 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6591 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 6592 | |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 6593 | // Make sure error messages are OK. |
| 6594 | do_estack_push = !estack_top_is_ufunc(ufunc, 1); |
| 6595 | if (do_estack_push) |
| 6596 | estack_push_ufunc(ufunc, 1); |
| 6597 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6598 | if (ufunc->uf_def_args.ga_len > 0) |
| 6599 | { |
| 6600 | int count = ufunc->uf_def_args.ga_len; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6601 | int first_def_arg = ufunc->uf_args.ga_len - count; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6602 | int i; |
| 6603 | char_u *arg; |
| 6604 | int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6605 | int did_set_arg_type = FALSE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6606 | |
| 6607 | // Produce instructions for the default values of optional arguments. |
| 6608 | // Store the instruction index in uf_def_arg_idx[] so that we know |
| 6609 | // where to start when the function is called, depending on the number |
| 6610 | // of arguments. |
| 6611 | ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); |
| 6612 | if (ufunc->uf_def_arg_idx == NULL) |
| 6613 | goto erret; |
| 6614 | for (i = 0; i < count; ++i) |
| 6615 | { |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6616 | garray_T *stack = &cctx.ctx_type_stack; |
| 6617 | type_T *val_type; |
| 6618 | int arg_idx = first_def_arg + i; |
| 6619 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6620 | ufunc->uf_def_arg_idx[i] = instr->ga_len; |
| 6621 | arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; |
Bram Moolenaar | a5565e4 | 2020-05-09 15:44:01 +0200 | [diff] [blame] | 6622 | if (compile_expr0(&arg, &cctx) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6623 | goto erret; |
| 6624 | |
| 6625 | // If no type specified use the type of the default value. |
| 6626 | // Otherwise check that the default value type matches the |
| 6627 | // specified type. |
| 6628 | val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; |
| 6629 | if (ufunc->uf_arg_types[arg_idx] == &t_unknown) |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6630 | { |
| 6631 | did_set_arg_type = TRUE; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6632 | ufunc->uf_arg_types[arg_idx] = val_type; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6633 | } |
Bram Moolenaar | 8b565c2 | 2020-08-30 23:24:20 +0200 | [diff] [blame] | 6634 | else if (check_type(ufunc->uf_arg_types[arg_idx], val_type, |
| 6635 | TRUE, arg_idx + 1) == FAIL) |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6636 | goto erret; |
Bram Moolenaar | 49cf7cc | 2020-04-07 22:45:00 +0200 | [diff] [blame] | 6637 | |
| 6638 | if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6639 | goto erret; |
| 6640 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6641 | ufunc->uf_def_arg_idx[count] = instr->ga_len; |
Bram Moolenaar | b8070e3 | 2020-07-23 20:56:04 +0200 | [diff] [blame] | 6642 | |
| 6643 | if (did_set_arg_type) |
| 6644 | set_function_type(ufunc); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 6645 | } |
| 6646 | |
| 6647 | /* |
| 6648 | * Loop over all the lines of the function and generate instructions. |
| 6649 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6650 | for (;;) |
| 6651 | { |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6652 | exarg_T ea; |
| 6653 | cmdmod_T save_cmdmod; |
| 6654 | int starts_with_colon = FALSE; |
| 6655 | char_u *cmd; |
| 6656 | int save_msg_scroll = msg_scroll; |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6657 | |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 6658 | // Bail out on the first error to avoid a flood of errors and report |
| 6659 | // the right line number when inside try/catch. |
| 6660 | if (emsg_before != called_emsg) |
| 6661 | goto erret; |
| 6662 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6663 | if (line != NULL && *line == '|') |
| 6664 | // the line continues after a '|' |
| 6665 | ++line; |
Bram Moolenaar | a3b7fdc | 2020-06-21 14:12:17 +0200 | [diff] [blame] | 6666 | else if (line != NULL && *skipwhite(line) != NUL |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 6667 | && !(*line == '#' && (line == cctx.ctx_line_start |
| 6668 | || VIM_ISWHITE(line[-1])))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6669 | { |
Bram Moolenaar | dd1a9af | 2020-07-23 15:38:03 +0200 | [diff] [blame] | 6670 | semsg(_(e_trailing_arg), line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6671 | goto erret; |
| 6672 | } |
| 6673 | else |
| 6674 | { |
Bram Moolenaar | 23c5527 | 2020-06-21 16:58:13 +0200 | [diff] [blame] | 6675 | line = next_line_from_context(&cctx, FALSE); |
Bram Moolenaar | 4fdae99 | 2020-04-12 16:38:57 +0200 | [diff] [blame] | 6676 | if (cctx.ctx_lnum >= ufunc->uf_lines.ga_len) |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6677 | // beyond the last line |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6678 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6679 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 6680 | emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6681 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6682 | CLEAR_FIELD(ea); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6683 | ea.cmdlinep = &line; |
| 6684 | ea.cmd = skipwhite(line); |
| 6685 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6686 | // Some things can be recognized by the first character. |
| 6687 | switch (*ea.cmd) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6688 | { |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6689 | case '#': |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 6690 | // "#" starts a comment, but "#{" does not. |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6691 | if (ea.cmd[1] != '{') |
| 6692 | { |
| 6693 | line = (char_u *)""; |
| 6694 | continue; |
| 6695 | } |
| 6696 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6697 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6698 | case '}': |
| 6699 | { |
| 6700 | // "}" ends a block scope |
| 6701 | scopetype_T stype = cctx.ctx_scope == NULL |
| 6702 | ? NO_SCOPE : cctx.ctx_scope->se_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6703 | |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6704 | if (stype == BLOCK_SCOPE) |
| 6705 | { |
| 6706 | compile_endblock(&cctx); |
| 6707 | line = ea.cmd; |
| 6708 | } |
| 6709 | else |
| 6710 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6711 | emsg(_(e_using_rcurly_outside_if_block_scope)); |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6712 | goto erret; |
| 6713 | } |
| 6714 | if (line != NULL) |
| 6715 | line = skipwhite(ea.cmd + 1); |
| 6716 | continue; |
| 6717 | } |
| 6718 | |
| 6719 | case '{': |
| 6720 | // "{" starts a block scope |
| 6721 | // "{'a': 1}->func() is something else |
| 6722 | if (ends_excmd(*skipwhite(ea.cmd + 1))) |
| 6723 | { |
| 6724 | line = compile_block(ea.cmd, &cctx); |
| 6725 | continue; |
| 6726 | } |
| 6727 | break; |
| 6728 | |
| 6729 | case ':': |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6730 | starts_with_colon = TRUE; |
Bram Moolenaar | cb711ab | 2020-04-16 13:00:29 +0200 | [diff] [blame] | 6731 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6732 | } |
| 6733 | |
| 6734 | /* |
| 6735 | * COMMAND MODIFIERS |
| 6736 | */ |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6737 | save_cmdmod = cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6738 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
| 6739 | { |
| 6740 | if (errormsg != NULL) |
| 6741 | goto erret; |
| 6742 | // empty line or comment |
| 6743 | line = (char_u *)""; |
| 6744 | continue; |
| 6745 | } |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 6746 | // TODO: use modifiers in the command |
| 6747 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | 2dd0a2c | 2020-08-08 15:10:27 +0200 | [diff] [blame] | 6748 | cmdmod = save_cmdmod; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6749 | |
| 6750 | // Skip ":call" to get to the function name. |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 6751 | p = ea.cmd; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6752 | if (checkforcmd(&ea.cmd, "call", 3)) |
Bram Moolenaar | 575f24b | 2020-08-12 14:21:11 +0200 | [diff] [blame] | 6753 | { |
| 6754 | if (*ea.cmd == '(') |
| 6755 | // not for "call()" |
| 6756 | ea.cmd = p; |
| 6757 | else |
| 6758 | ea.cmd = skipwhite(ea.cmd); |
| 6759 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6760 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6761 | if (!starts_with_colon) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6762 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6763 | char_u *pskip; |
| 6764 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6765 | // Assuming the command starts with a variable or function name, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6766 | // find what follows. |
| 6767 | // Skip over "var.member", "var[idx]" and the like. |
| 6768 | // Also "&opt = val", "$ENV = val" and "@r = val". |
| 6769 | pskip = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@') |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6770 | ? ea.cmd + 1 : ea.cmd; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6771 | p = to_name_end(pskip, TRUE); |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 6772 | if (p > ea.cmd && *p != NUL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6773 | { |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6774 | char_u *var_end; |
| 6775 | int oplen; |
| 6776 | int heredoc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6777 | |
Bram Moolenaar | 6582172 | 2020-08-02 18:58:54 +0200 | [diff] [blame] | 6778 | if (ea.cmd[0] == '@') |
| 6779 | var_end = ea.cmd + 2; |
| 6780 | else |
| 6781 | var_end = find_name_end(pskip, NULL, NULL, |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6782 | FNE_CHECK_START | FNE_INCL_BR); |
| 6783 | oplen = assignment_len(skipwhite(var_end), &heredoc); |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6784 | if (oplen > 0) |
| 6785 | { |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6786 | size_t len = p - ea.cmd; |
| 6787 | |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6788 | // Recognize an assignment if we recognize the variable |
| 6789 | // name: |
| 6790 | // "g:var = expr" |
Bram Moolenaar | 5381c7a | 2020-03-02 22:53:32 +0100 | [diff] [blame] | 6791 | // "local = expr" where "local" is a local var. |
| 6792 | // "script = expr" where "script" is a script-local var. |
| 6793 | // "import = expr" where "import" is an imported var |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6794 | // "&opt = expr" |
| 6795 | // "$ENV = expr" |
| 6796 | // "@r = expr" |
| 6797 | if (*ea.cmd == '&' |
| 6798 | || *ea.cmd == '$' |
| 6799 | || *ea.cmd == '@' |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6800 | || ((len) > 2 && ea.cmd[1] == ':') |
| 6801 | || lookup_local(ea.cmd, len, &cctx) != NULL |
| 6802 | || lookup_arg(ea.cmd, len, NULL, NULL, |
| 6803 | NULL, &cctx) == OK |
Bram Moolenaar | 5390099 | 2020-08-22 19:02:02 +0200 | [diff] [blame] | 6804 | || lookup_script(ea.cmd, len, FALSE) == OK |
Bram Moolenaar | cb2bdb1 | 2020-05-10 22:53:56 +0200 | [diff] [blame] | 6805 | || find_imported(ea.cmd, len, &cctx) != NULL) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6806 | { |
| 6807 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6808 | if (line == NULL || line == ea.cmd) |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6809 | goto erret; |
| 6810 | continue; |
| 6811 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6812 | } |
| 6813 | } |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6814 | |
| 6815 | if (*ea.cmd == '[') |
| 6816 | { |
| 6817 | // [var, var] = expr |
| 6818 | line = compile_assignment(ea.cmd, &ea, CMD_SIZE, &cctx); |
| 6819 | if (line == NULL) |
| 6820 | goto erret; |
| 6821 | if (line != ea.cmd) |
| 6822 | continue; |
| 6823 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6824 | } |
| 6825 | |
| 6826 | /* |
| 6827 | * COMMAND after range |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6828 | * 'text'->func() should not be confused with 'a mark |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6829 | */ |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6830 | cmd = ea.cmd; |
Bram Moolenaar | 7c5ad34 | 2020-08-12 15:48:55 +0200 | [diff] [blame] | 6831 | if (*cmd != '\'' || starts_with_colon) |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6832 | { |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6833 | ea.cmd = skip_range(ea.cmd, NULL); |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 6834 | if (ea.cmd > cmd) |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6835 | { |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 6836 | if (!starts_with_colon) |
| 6837 | { |
| 6838 | emsg(_(e_colon_required_before_a_range)); |
| 6839 | goto erret; |
| 6840 | } |
| 6841 | if (ends_excmd2(line, ea.cmd)) |
| 6842 | { |
| 6843 | // A range without a command: jump to the line. |
| 6844 | // TODO: compile to a more efficient command, possibly |
| 6845 | // calling parse_cmd_address(). |
| 6846 | ea.cmdidx = CMD_SIZE; |
| 6847 | line = compile_exec(line, &ea, &cctx); |
| 6848 | goto nextline; |
| 6849 | } |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 6850 | } |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 6851 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 6852 | p = find_ex_command(&ea, NULL, starts_with_colon ? NULL |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 6853 | : (void *(*)(char_u *, size_t, cctx_T *))lookup_local, |
Bram Moolenaar | 5b1c8fe | 2020-02-21 18:42:43 +0100 | [diff] [blame] | 6854 | &cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6855 | |
| 6856 | if (p == ea.cmd && ea.cmdidx != CMD_SIZE) |
| 6857 | { |
Bram Moolenaar | 9b68c82 | 2020-06-18 19:31:08 +0200 | [diff] [blame] | 6858 | if (cctx.ctx_skip == SKIP_YES) |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6859 | { |
| 6860 | line += STRLEN(line); |
| 6861 | continue; |
| 6862 | } |
| 6863 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6864 | // Expression or function call. |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6865 | if (ea.cmdidx != CMD_eval) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6866 | { |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6867 | // CMD_let cannot happen, compile_assignment() above is used |
| 6868 | iemsg("Command from find_ex_command() not handled"); |
| 6869 | goto erret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6870 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6871 | } |
| 6872 | |
| 6873 | p = skipwhite(p); |
| 6874 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 6875 | if (cctx.ctx_had_return |
Bram Moolenaar | a259d8d | 2020-01-31 20:10:50 +0100 | [diff] [blame] | 6876 | && ea.cmdidx != CMD_elseif |
| 6877 | && ea.cmdidx != CMD_else |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6878 | && ea.cmdidx != CMD_endif |
| 6879 | && ea.cmdidx != CMD_endfor |
| 6880 | && ea.cmdidx != CMD_endwhile |
| 6881 | && ea.cmdidx != CMD_catch |
| 6882 | && ea.cmdidx != CMD_finally |
| 6883 | && ea.cmdidx != CMD_endtry) |
| 6884 | { |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 6885 | emsg(_(e_unreachable_code_after_return)); |
| 6886 | goto erret; |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6887 | } |
| 6888 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6889 | switch (ea.cmdidx) |
| 6890 | { |
| 6891 | case CMD_def: |
Bram Moolenaar | 04b1269 | 2020-05-04 23:24:44 +0200 | [diff] [blame] | 6892 | ea.arg = p; |
| 6893 | line = compile_nested_function(&ea, &cctx); |
| 6894 | break; |
| 6895 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6896 | case CMD_function: |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 6897 | // TODO: should we allow this, e.g. to declare a global |
| 6898 | // function? |
| 6899 | emsg(_(e_cannot_use_function_inside_def)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6900 | goto erret; |
| 6901 | |
| 6902 | case CMD_return: |
| 6903 | line = compile_return(p, set_return_type, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6904 | cctx.ctx_had_return = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6905 | break; |
| 6906 | |
| 6907 | case CMD_let: |
| 6908 | case CMD_const: |
| 6909 | line = compile_assignment(p, &ea, ea.cmdidx, &cctx); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 6910 | if (line == p) |
| 6911 | line = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6912 | break; |
| 6913 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 6914 | case CMD_unlet: |
| 6915 | case CMD_unlockvar: |
| 6916 | case CMD_lockvar: |
| 6917 | line = compile_unletlock(p, &ea, &cctx); |
| 6918 | break; |
| 6919 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6920 | case CMD_import: |
| 6921 | line = compile_import(p, &cctx); |
| 6922 | break; |
| 6923 | |
| 6924 | case CMD_if: |
| 6925 | line = compile_if(p, &cctx); |
| 6926 | break; |
| 6927 | case CMD_elseif: |
| 6928 | line = compile_elseif(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6929 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6930 | break; |
| 6931 | case CMD_else: |
| 6932 | line = compile_else(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6933 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6934 | break; |
| 6935 | case CMD_endif: |
| 6936 | line = compile_endif(p, &cctx); |
| 6937 | break; |
| 6938 | |
| 6939 | case CMD_while: |
| 6940 | line = compile_while(p, &cctx); |
| 6941 | break; |
| 6942 | case CMD_endwhile: |
| 6943 | line = compile_endwhile(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6944 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6945 | break; |
| 6946 | |
| 6947 | case CMD_for: |
| 6948 | line = compile_for(p, &cctx); |
| 6949 | break; |
| 6950 | case CMD_endfor: |
| 6951 | line = compile_endfor(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6952 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6953 | break; |
| 6954 | case CMD_continue: |
| 6955 | line = compile_continue(p, &cctx); |
| 6956 | break; |
| 6957 | case CMD_break: |
| 6958 | line = compile_break(p, &cctx); |
| 6959 | break; |
| 6960 | |
| 6961 | case CMD_try: |
| 6962 | line = compile_try(p, &cctx); |
| 6963 | break; |
| 6964 | case CMD_catch: |
| 6965 | line = compile_catch(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6966 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6967 | break; |
| 6968 | case CMD_finally: |
| 6969 | line = compile_finally(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6970 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6971 | break; |
| 6972 | case CMD_endtry: |
| 6973 | line = compile_endtry(p, &cctx); |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 6974 | cctx.ctx_had_return = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6975 | break; |
| 6976 | case CMD_throw: |
| 6977 | line = compile_throw(p, &cctx); |
| 6978 | break; |
| 6979 | |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6980 | case CMD_eval: |
| 6981 | if (compile_expr0(&p, &cctx) == FAIL) |
| 6982 | goto erret; |
| 6983 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 6984 | // drop the result |
Bram Moolenaar | 007f9d6 | 2020-07-06 23:04:49 +0200 | [diff] [blame] | 6985 | generate_instr_drop(&cctx, ISN_DROP, 1); |
| 6986 | |
| 6987 | line = skipwhite(p); |
| 6988 | break; |
| 6989 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6990 | case CMD_echo: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6991 | case CMD_echon: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6992 | case CMD_execute: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 6993 | case CMD_echomsg: |
| 6994 | case CMD_echoerr: |
| 6995 | line = compile_mult_expr(p, ea.cmdidx, &cctx); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 6996 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 6997 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 6998 | case CMD_put: |
| 6999 | ea.cmd = cmd; |
| 7000 | line = compile_put(p, &ea, &cctx); |
| 7001 | break; |
| 7002 | |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 7003 | // TODO: any other commands with an expression argument? |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 7004 | |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 7005 | case CMD_append: |
| 7006 | case CMD_change: |
| 7007 | case CMD_insert: |
Bram Moolenaar | f5a4801 | 2020-08-01 17:00:03 +0200 | [diff] [blame] | 7008 | case CMD_t: |
Bram Moolenaar | ae61649 | 2020-07-28 20:07:27 +0200 | [diff] [blame] | 7009 | case CMD_xit: |
| 7010 | not_in_vim9(&ea); |
| 7011 | goto erret; |
| 7012 | |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7013 | case CMD_SIZE: |
Bram Moolenaar | 3988f64 | 2020-08-27 22:43:03 +0200 | [diff] [blame] | 7014 | if (cctx.ctx_skip != SKIP_YES) |
| 7015 | { |
| 7016 | semsg(_(e_invalid_command_str), ea.cmd); |
| 7017 | goto erret; |
| 7018 | } |
| 7019 | // We don't check for a next command here. |
| 7020 | line = (char_u *)""; |
| 7021 | break; |
Bram Moolenaar | 002262f | 2020-07-08 17:47:57 +0200 | [diff] [blame] | 7022 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7023 | default: |
Bram Moolenaar | 5163fcc | 2020-08-27 23:37:09 +0200 | [diff] [blame] | 7024 | if (cctx.ctx_skip == SKIP_YES) |
| 7025 | { |
| 7026 | // We don't check for a next command here. |
| 7027 | line = (char_u *)""; |
| 7028 | } |
| 7029 | else |
| 7030 | { |
| 7031 | // Not recognized, execute with do_cmdline_cmd(). |
| 7032 | ea.arg = p; |
| 7033 | line = compile_exec(line, &ea, &cctx); |
| 7034 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7035 | break; |
| 7036 | } |
Bram Moolenaar | 5d72ce6 | 2020-08-20 23:04:06 +0200 | [diff] [blame] | 7037 | nextline: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7038 | if (line == NULL) |
| 7039 | goto erret; |
Bram Moolenaar | 585fea7 | 2020-04-02 22:33:21 +0200 | [diff] [blame] | 7040 | line = skipwhite(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7041 | |
| 7042 | if (cctx.ctx_type_stack.ga_len < 0) |
| 7043 | { |
| 7044 | iemsg("Type stack underflow"); |
| 7045 | goto erret; |
| 7046 | } |
| 7047 | } |
| 7048 | |
| 7049 | if (cctx.ctx_scope != NULL) |
| 7050 | { |
| 7051 | if (cctx.ctx_scope->se_type == IF_SCOPE) |
| 7052 | emsg(_(e_endif)); |
| 7053 | else if (cctx.ctx_scope->se_type == WHILE_SCOPE) |
| 7054 | emsg(_(e_endwhile)); |
| 7055 | else if (cctx.ctx_scope->se_type == FOR_SCOPE) |
| 7056 | emsg(_(e_endfor)); |
| 7057 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7058 | emsg(_(e_missing_rcurly)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7059 | goto erret; |
| 7060 | } |
| 7061 | |
Bram Moolenaar | efd8855 | 2020-06-18 20:50:10 +0200 | [diff] [blame] | 7062 | if (!cctx.ctx_had_return) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7063 | { |
| 7064 | if (ufunc->uf_ret_type->tt_type != VAR_VOID) |
| 7065 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7066 | emsg(_(e_missing_return_statement)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7067 | goto erret; |
| 7068 | } |
| 7069 | |
| 7070 | // Return zero if there is no return at the end. |
| 7071 | generate_PUSHNR(&cctx, 0); |
| 7072 | generate_instr(&cctx, ISN_RETURN); |
| 7073 | } |
| 7074 | |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7075 | { |
| 7076 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7077 | + ufunc->uf_dfunc_idx; |
| 7078 | dfunc->df_deleted = FALSE; |
| 7079 | dfunc->df_instr = instr->ga_data; |
| 7080 | dfunc->df_instr_count = instr->ga_len; |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7081 | dfunc->df_varcount = cctx.ctx_locals_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 7082 | dfunc->df_closure_count = cctx.ctx_closure_count; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7083 | if (cctx.ctx_outer_used) |
| 7084 | ufunc->uf_flags |= FC_CLOSURE; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7085 | ufunc->uf_def_status = UF_COMPILED; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7086 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7087 | |
| 7088 | ret = OK; |
| 7089 | |
| 7090 | erret: |
| 7091 | if (ret == FAIL) |
| 7092 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7093 | int idx; |
Bram Moolenaar | 05afcee | 2020-03-31 23:32:31 +0200 | [diff] [blame] | 7094 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7095 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7096 | |
| 7097 | for (idx = 0; idx < instr->ga_len; ++idx) |
| 7098 | delete_instr(((isn_T *)instr->ga_data) + idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7099 | ga_clear(instr); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7100 | |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7101 | // If using the last entry in the table and it was added above, we |
| 7102 | // might as well remove it. |
| 7103 | if (!dfunc->df_deleted && new_def_function |
Bram Moolenaar | 45a1508 | 2020-05-25 00:28:33 +0200 | [diff] [blame] | 7104 | && ufunc->uf_dfunc_idx == def_functions.ga_len - 1) |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7105 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7106 | --def_functions.ga_len; |
Bram Moolenaar | 6c4bfe4 | 2020-07-23 18:26:30 +0200 | [diff] [blame] | 7107 | ufunc->uf_dfunc_idx = 0; |
| 7108 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7109 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7110 | |
Bram Moolenaar | 3cca299 | 2020-04-02 22:57:36 +0200 | [diff] [blame] | 7111 | while (cctx.ctx_scope != NULL) |
| 7112 | drop_scope(&cctx); |
| 7113 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7114 | // Don't execute this function body. |
| 7115 | ga_clear_strings(&ufunc->uf_lines); |
| 7116 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7117 | if (errormsg != NULL) |
| 7118 | emsg(errormsg); |
| 7119 | else if (called_emsg == called_emsg_before) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 7120 | emsg(_(e_compile_def_function_failed)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7121 | } |
| 7122 | |
| 7123 | current_sctx = save_current_sctx; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 7124 | if (do_estack_push) |
| 7125 | estack_pop(); |
| 7126 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7127 | free_imported(&cctx); |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 7128 | free_locals(&cctx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7129 | ga_clear(&cctx.ctx_type_stack); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 7130 | return ret; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7131 | } |
| 7132 | |
Bram Moolenaar | 6ff71d8 | 2020-05-24 23:45:24 +0200 | [diff] [blame] | 7133 | void |
| 7134 | set_function_type(ufunc_T *ufunc) |
| 7135 | { |
| 7136 | int varargs = ufunc->uf_va_name != NULL; |
| 7137 | int argcount = ufunc->uf_args.ga_len; |
| 7138 | |
| 7139 | // Create a type for the function, with the return type and any |
| 7140 | // argument types. |
| 7141 | // A vararg is included in uf_args.ga_len but not in uf_arg_types. |
| 7142 | // The type is included in "tt_args". |
| 7143 | if (argcount > 0 || varargs) |
| 7144 | { |
| 7145 | ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type, |
| 7146 | argcount, &ufunc->uf_type_list); |
| 7147 | // Add argument types to the function type. |
| 7148 | if (func_type_add_arg_types(ufunc->uf_func_type, |
| 7149 | argcount + varargs, |
| 7150 | &ufunc->uf_type_list) == FAIL) |
| 7151 | return; |
| 7152 | ufunc->uf_func_type->tt_argcount = argcount + varargs; |
| 7153 | ufunc->uf_func_type->tt_min_argcount = |
| 7154 | argcount - ufunc->uf_def_args.ga_len; |
| 7155 | if (ufunc->uf_arg_types == NULL) |
| 7156 | { |
| 7157 | int i; |
| 7158 | |
| 7159 | // lambda does not have argument types. |
| 7160 | for (i = 0; i < argcount; ++i) |
| 7161 | ufunc->uf_func_type->tt_args[i] = &t_any; |
| 7162 | } |
| 7163 | else |
| 7164 | mch_memmove(ufunc->uf_func_type->tt_args, |
| 7165 | ufunc->uf_arg_types, sizeof(type_T *) * argcount); |
| 7166 | if (varargs) |
| 7167 | { |
| 7168 | ufunc->uf_func_type->tt_args[argcount] = |
| 7169 | ufunc->uf_va_type == NULL ? &t_any : ufunc->uf_va_type; |
| 7170 | ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS; |
| 7171 | } |
| 7172 | } |
| 7173 | else |
| 7174 | // No arguments, can use a predefined type. |
| 7175 | ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type, |
| 7176 | argcount, &ufunc->uf_type_list); |
| 7177 | } |
| 7178 | |
| 7179 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7180 | /* |
| 7181 | * Delete an instruction, free what it contains. |
| 7182 | */ |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7183 | void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7184 | delete_instr(isn_T *isn) |
| 7185 | { |
| 7186 | switch (isn->isn_type) |
| 7187 | { |
| 7188 | case ISN_EXEC: |
| 7189 | case ISN_LOADENV: |
| 7190 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7191 | case ISN_LOADB: |
| 7192 | case ISN_LOADW: |
| 7193 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7194 | case ISN_LOADOPT: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7195 | case ISN_STRINGMEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7196 | case ISN_PUSHEXC: |
| 7197 | case ISN_PUSHS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7198 | case ISN_STOREENV: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7199 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 7200 | case ISN_STOREB: |
| 7201 | case ISN_STOREW: |
| 7202 | case ISN_STORET: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7203 | case ISN_PUSHFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7204 | vim_free(isn->isn_arg.string); |
| 7205 | break; |
| 7206 | |
| 7207 | case ISN_LOADS: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 7208 | case ISN_STORES: |
| 7209 | vim_free(isn->isn_arg.loadstore.ls_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7210 | break; |
| 7211 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7212 | case ISN_UNLET: |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 7213 | case ISN_UNLETENV: |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 7214 | vim_free(isn->isn_arg.unlet.ul_name); |
| 7215 | break; |
| 7216 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7217 | case ISN_STOREOPT: |
| 7218 | vim_free(isn->isn_arg.storeopt.so_name); |
| 7219 | break; |
| 7220 | |
| 7221 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 7222 | blob_unref(isn->isn_arg.blob); |
| 7223 | break; |
| 7224 | |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7225 | case ISN_PUSHJOB: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7226 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7227 | job_unref(isn->isn_arg.job); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7228 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7229 | break; |
| 7230 | |
| 7231 | case ISN_PUSHCHANNEL: |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7232 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7233 | channel_unref(isn->isn_arg.channel); |
Bram Moolenaar | f4f190d | 2020-03-01 13:01:16 +0100 | [diff] [blame] | 7234 | #endif |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 7235 | break; |
| 7236 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7237 | case ISN_UCALL: |
| 7238 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 7239 | break; |
| 7240 | |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 7241 | case ISN_FUNCREF: |
| 7242 | { |
| 7243 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7244 | + isn->isn_arg.funcref.fr_func; |
| 7245 | func_ptr_unref(dfunc->df_ufunc); |
| 7246 | } |
| 7247 | break; |
| 7248 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7249 | case ISN_NEWFUNC: |
Bram Moolenaar | ce65835 | 2020-07-31 23:47:12 +0200 | [diff] [blame] | 7250 | { |
| 7251 | char_u *lambda = isn->isn_arg.newfunc.nf_lambda; |
| 7252 | ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL); |
| 7253 | |
| 7254 | if (ufunc != NULL) |
| 7255 | { |
| 7256 | // Clear uf_dfunc_idx so that the function is deleted. |
| 7257 | clear_def_function(ufunc); |
| 7258 | ufunc->uf_dfunc_idx = 0; |
| 7259 | func_ptr_unref(ufunc); |
| 7260 | } |
| 7261 | |
| 7262 | vim_free(lambda); |
| 7263 | vim_free(isn->isn_arg.newfunc.nf_global); |
| 7264 | } |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 7265 | break; |
| 7266 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7267 | case ISN_2BOOL: |
| 7268 | case ISN_2STRING: |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 7269 | case ISN_2STRING_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7270 | case ISN_ADDBLOB: |
| 7271 | case ISN_ADDLIST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7272 | case ISN_ANYINDEX: |
| 7273 | case ISN_ANYSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7274 | case ISN_BCALL: |
| 7275 | case ISN_CATCH: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7276 | case ISN_CHECKLEN: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7277 | case ISN_CHECKNR: |
| 7278 | case ISN_CHECKTYPE: |
| 7279 | case ISN_COMPAREANY: |
| 7280 | case ISN_COMPAREBLOB: |
| 7281 | case ISN_COMPAREBOOL: |
| 7282 | case ISN_COMPAREDICT: |
| 7283 | case ISN_COMPAREFLOAT: |
| 7284 | case ISN_COMPAREFUNC: |
| 7285 | case ISN_COMPARELIST: |
| 7286 | case ISN_COMPARENR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7287 | case ISN_COMPARESPECIAL: |
| 7288 | case ISN_COMPARESTRING: |
| 7289 | case ISN_CONCAT: |
| 7290 | case ISN_DCALL: |
| 7291 | case ISN_DROP: |
| 7292 | case ISN_ECHO: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 7293 | case ISN_ECHOERR: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7294 | case ISN_ECHOMSG: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7295 | case ISN_ENDTRY: |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 7296 | case ISN_EXECCONCAT: |
| 7297 | case ISN_EXECUTE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7298 | case ISN_FOR: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7299 | case ISN_GETITEM: |
| 7300 | case ISN_JUMP: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 7301 | case ISN_LISTINDEX: |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 7302 | case ISN_LISTSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7303 | case ISN_LOAD: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7304 | case ISN_LOADBDICT: |
| 7305 | case ISN_LOADGDICT: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 7306 | case ISN_LOADOUTER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7307 | case ISN_LOADREG: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7308 | case ISN_LOADSCRIPT: |
| 7309 | case ISN_LOADTDICT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7310 | case ISN_LOADV: |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 7311 | case ISN_LOADWDICT: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7312 | case ISN_MEMBER: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7313 | case ISN_NEGATENR: |
| 7314 | case ISN_NEWDICT: |
| 7315 | case ISN_NEWLIST: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7316 | case ISN_OPANY: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7317 | case ISN_OPFLOAT: |
| 7318 | case ISN_OPNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7319 | case ISN_PCALL: |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 7320 | case ISN_PCALL_END: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7321 | case ISN_PUSHBOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7322 | case ISN_PUSHF: |
| 7323 | case ISN_PUSHNR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7324 | case ISN_PUSHSPEC: |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 7325 | case ISN_PUT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7326 | case ISN_RETURN: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7327 | case ISN_SHUFFLE: |
| 7328 | case ISN_SLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7329 | case ISN_STORE: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 7330 | case ISN_STOREDICT: |
| 7331 | case ISN_STORELIST: |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 7332 | case ISN_STORENR: |
| 7333 | case ISN_STOREOUTER: |
| 7334 | case ISN_STOREREG: |
| 7335 | case ISN_STORESCRIPT: |
| 7336 | case ISN_STOREV: |
| 7337 | case ISN_STRINDEX: |
| 7338 | case ISN_STRSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7339 | case ISN_THROW: |
| 7340 | case ISN_TRY: |
| 7341 | // nothing allocated |
| 7342 | break; |
| 7343 | } |
| 7344 | } |
| 7345 | |
| 7346 | /* |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7347 | * Free all instructions for "dfunc". |
| 7348 | */ |
| 7349 | static void |
| 7350 | delete_def_function_contents(dfunc_T *dfunc) |
| 7351 | { |
| 7352 | int idx; |
| 7353 | |
| 7354 | ga_clear(&dfunc->df_def_args_isn); |
| 7355 | |
| 7356 | if (dfunc->df_instr != NULL) |
| 7357 | { |
| 7358 | for (idx = 0; idx < dfunc->df_instr_count; ++idx) |
| 7359 | delete_instr(dfunc->df_instr + idx); |
| 7360 | VIM_CLEAR(dfunc->df_instr); |
| 7361 | } |
| 7362 | |
| 7363 | dfunc->df_deleted = TRUE; |
| 7364 | } |
| 7365 | |
| 7366 | /* |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7367 | * When a user function is deleted, clear the contents of any associated def |
| 7368 | * function. The position in def_functions can be re-used. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7369 | */ |
| 7370 | void |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7371 | clear_def_function(ufunc_T *ufunc) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7372 | { |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7373 | if (ufunc->uf_dfunc_idx > 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7374 | { |
| 7375 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 7376 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7377 | |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7378 | delete_def_function_contents(dfunc); |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 7379 | ufunc->uf_def_status = UF_NOT_COMPILED; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7380 | } |
| 7381 | } |
| 7382 | |
| 7383 | #if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7384 | /* |
| 7385 | * Free all functions defined with ":def". |
| 7386 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7387 | void |
| 7388 | free_def_functions(void) |
| 7389 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 7390 | int idx; |
| 7391 | |
| 7392 | for (idx = 0; idx < def_functions.ga_len; ++idx) |
| 7393 | { |
| 7394 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx; |
| 7395 | |
| 7396 | delete_def_function_contents(dfunc); |
| 7397 | } |
| 7398 | |
| 7399 | ga_clear(&def_functions); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 7400 | } |
| 7401 | #endif |
| 7402 | |
| 7403 | |
| 7404 | #endif // FEAT_EVAL |